Move *-*-gnu* pattern below *-*-linux*.
[official-gcc.git] / gcc / cpplib.c
blob8194246adb7895dabe5065b239f5c1a52aaa2452
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 *, const char *));
43 static HOST_WIDEST_INT eval_if_expression PROTO ((cpp_reader *));
45 static void conditional_skip PROTO ((cpp_reader *, int,
46 enum node_type, U_CHAR *));
47 static void skip_if_group PROTO ((cpp_reader *));
49 static void parse_name PARAMS ((cpp_reader *, int));
50 static void parse_string PARAMS ((cpp_reader *, int));
51 static int parse_assertion PARAMS ((cpp_reader *));
53 /* External declarations. */
55 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
57 /* `struct directive' defines one #-directive, including how to handle it. */
59 struct directive {
60 int length; /* Length of name */
61 int (*func) /* Function to handle directive */
62 PARAMS ((cpp_reader *, const struct directive *));
63 const char *name; /* Name of directive */
64 enum node_type type; /* Code which describes which directive. */
67 /* These functions are declared to return int instead of void since they
68 are going to be placed in a table and some old compilers have trouble with
69 pointers to functions returning void. */
71 static int do_define PARAMS ((cpp_reader *, const struct directive *));
72 static int do_line PARAMS ((cpp_reader *, const struct directive *));
73 static int do_include PARAMS ((cpp_reader *, const struct directive *));
74 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
75 static int do_error PARAMS ((cpp_reader *, const struct directive *));
76 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
77 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
78 static int do_if PARAMS ((cpp_reader *, const struct directive *));
79 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
80 static int do_else PARAMS ((cpp_reader *, const struct directive *));
81 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
82 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
83 #ifdef SCCS_DIRECTIVE
84 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
85 #endif
86 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
87 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
88 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
90 /* Here is the actual list of #-directives.
91 This table is ordered by frequency of occurrence; the numbers
92 at the end are directive counts from all the source code I have
93 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
94 linux-2.2.9, and pcmcia-cs-3.0.9). */
96 static const struct directive directive_table[] = {
97 /* In C89 */
98 { 6, do_define, "define", T_DEFINE }, /* 270554 */
99 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
100 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
101 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
102 { 2, do_if, "if", T_IF }, /* 18162 */
103 { 4, do_else, "else", T_ELSE }, /* 9863 */
104 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
105 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
106 { 4, do_line, "line", T_LINE }, /* 2465 */
107 { 4, do_elif, "elif", T_ELIF }, /* 610 */
108 { 5, do_error, "error", T_ERROR }, /* 475 */
109 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
111 /* Extensions. All deprecated except #warning and #include_next. */
112 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
113 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
114 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
115 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
116 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
117 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
118 #ifdef SCCS_DIRECTIVE
119 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
120 #endif
121 { -1, 0, "", T_UNUSED }
124 /* Place into PFILE a quoted string representing the string SRC.
125 Caller must reserve enough space in pfile->token_buffer. */
127 void
128 quote_string (pfile, src)
129 cpp_reader *pfile;
130 const char *src;
132 U_CHAR c;
134 CPP_PUTC_Q (pfile, '\"');
135 for (;;)
136 switch ((c = *src++))
138 default:
139 if (ISPRINT (c))
140 CPP_PUTC_Q (pfile, c);
141 else
143 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
144 CPP_ADJUST_WRITTEN (pfile, 4);
146 break;
148 case '\"':
149 case '\\':
150 CPP_PUTC_Q (pfile, '\\');
151 CPP_PUTC_Q (pfile, c);
152 break;
154 case '\0':
155 CPP_PUTC_Q (pfile, '\"');
156 CPP_NUL_TERMINATE_Q (pfile);
157 return;
161 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
163 void
164 cpp_grow_buffer (pfile, n)
165 cpp_reader *pfile;
166 long n;
168 long old_written = CPP_WRITTEN (pfile);
169 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
170 pfile->token_buffer = (U_CHAR *)
171 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
172 CPP_SET_WRITTEN (pfile, old_written);
175 /* Process the string STR as if it appeared as the body of a #define
176 If STR is just an identifier, define it with value 1.
177 If STR has anything after the identifier, then it should
178 be identifier=definition. */
180 void
181 cpp_define (pfile, str)
182 cpp_reader *pfile;
183 U_CHAR *str;
185 U_CHAR *buf, *p;
186 size_t count;
188 /* Copy the entire option so we can modify it. */
189 count = strlen (str) + 3;
190 buf = (U_CHAR *) alloca (count);
191 memcpy (buf, str, count - 2);
192 /* Change the first "=" in the string to a space. If there is none,
193 tack " 1" on the end. */
194 p = (U_CHAR *) strchr (buf, '=');
195 if (p)
197 *p = ' ';
198 count -= 2;
200 else
201 strcpy (&buf[count-3], " 1");
203 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
205 do_define (pfile, NULL);
206 cpp_pop_buffer (pfile);
210 /* Process the string STR as if it appeared as the body of a #assert. */
211 void
212 cpp_assert (pfile, str)
213 cpp_reader *pfile;
214 U_CHAR *str;
216 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
218 do_assert (pfile, NULL);
219 cpp_pop_buffer (pfile);
224 static enum cpp_token
225 null_underflow (pfile)
226 cpp_reader *pfile ATTRIBUTE_UNUSED;
228 return CPP_EOF;
231 static int
232 null_cleanup (pbuf, pfile)
233 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
234 cpp_reader *pfile ATTRIBUTE_UNUSED;
236 return 0;
239 /* Skip a comment - C, C++, or Chill style. M is the first character
240 of the comment marker. If this really is a comment, skip to its
241 end and return ' '. If we hit end-of-file before end-of-comment,
242 return EOF. If this is not a comment, return M (which will be
243 '/' or '-'). */
245 static int
246 skip_comment (pfile, m)
247 cpp_reader *pfile;
248 int m;
250 if (m == '/' && PEEKC() == '*')
252 int c, prev_c = -1;
253 long line, col;
255 FORWARD(1);
256 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
257 for (;;)
259 c = GETC ();
260 if (c == EOF)
262 cpp_error_with_line (pfile, line, col, "unterminated comment");
263 return EOF;
265 else if (c == '\n' || c == '\r')
266 /* \r cannot be a macro escape marker here. */
267 CPP_BUMP_LINE (pfile);
268 else if (c == '/' && prev_c == '*')
269 return ' ';
270 else if (c == '*' && prev_c == '/'
271 && CPP_OPTIONS (pfile)->warn_comments)
272 cpp_warning (pfile, "`/*' within comment");
274 prev_c = c;
277 else if ((m == '/' && PEEKC() == '/'
278 && CPP_OPTIONS (pfile)->cplusplus_comments)
279 || (m == '-' && PEEKC() == '-'
280 && CPP_OPTIONS (pfile)->chill))
282 FORWARD(1);
283 for (;;)
285 int c = GETC ();
286 if (c == EOF)
287 return ' '; /* Allow // to be terminated by EOF. */
288 if (c == '\n')
290 /* Don't consider final '\n' to be part of comment. */
291 FORWARD(-1);
292 return ' ';
294 else if (c == '\r')
295 /* \r cannot be a macro escape marker here. */
296 CPP_BUMP_LINE (pfile);
299 else
300 return m;
303 /* Identical to skip_comment except that it copies the comment into the
304 token_buffer. This is used if put_out_comments. */
305 static int
306 copy_comment (pfile, m)
307 cpp_reader *pfile;
308 int m;
310 if (m == '/' && PEEKC() == '*')
312 int c, prev_c = -1;
313 long line, col;
315 CPP_PUTC (pfile, '/');
316 CPP_PUTC (pfile, '*');
317 FORWARD(1);
318 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
319 for (;;)
321 c = GETC ();
322 if (c == EOF)
324 cpp_error_with_line (pfile, line, col, "unterminated comment");
325 /* We must pretend this was a legitimate comment, so that the
326 output in token_buffer is not passed back tagged CPP_POP. */
327 return ' ';
329 else if (c == '\r')
331 /* \r cannot be a macro escape marker here. */
332 CPP_BUMP_LINE (pfile);
333 continue;
336 CPP_PUTC (pfile, c);
337 if (c == '\n')
339 pfile->lineno++;
340 CPP_BUMP_LINE (pfile);
342 else if (c == '/' && prev_c == '*')
343 return ' ';
344 else if (c == '*' && prev_c == '/'
345 && CPP_OPTIONS (pfile)->warn_comments)
346 cpp_warning (pfile, "`/*' within comment");
348 prev_c = c;
351 else if ((m == '/' && PEEKC() == '/'
352 && CPP_OPTIONS (pfile)->cplusplus_comments)
353 || (m == '-' && PEEKC() == '-'
354 && CPP_OPTIONS (pfile)->chill))
356 CPP_PUTC (pfile, m);
357 CPP_PUTC (pfile, m);
358 FORWARD(1);
359 for (;;)
361 int c = GETC ();
362 if (c == EOF)
363 return ' '; /* Allow line comments to be terminated by EOF. */
364 else if (c == '\n')
366 /* Don't consider final '\n' to be part of comment. */
367 FORWARD(-1);
368 return ' ';
370 else if (c == '\r')
371 /* \r cannot be a macro escape marker here. */
372 CPP_BUMP_LINE (pfile);
374 CPP_PUTC (pfile, c);
377 else
378 return m;
382 /* Skip whitespace \-newline and comments. Does not macro-expand. */
384 void
385 cpp_skip_hspace (pfile)
386 cpp_reader *pfile;
388 int c;
389 while (1)
391 c = GETC();
392 if (c == EOF)
393 return;
394 else if (is_hor_space[c])
396 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
397 cpp_pedwarn (pfile, "%s in preprocessing directive",
398 c == '\f' ? "formfeed" : "vertical tab");
400 else if (c == '\r')
402 /* \r is a backslash-newline marker if !has_escapes, and
403 a deletable-whitespace or no-reexpansion marker otherwise. */
404 if (CPP_BUFFER (pfile)->has_escapes)
406 if (PEEKC() == ' ')
407 FORWARD(1);
408 else
409 break;
411 else
412 CPP_BUFFER (pfile)->lineno++;
414 else if (c == '/' || c == '-')
416 c = skip_comment (pfile, c);
417 if (c == EOF)
418 return;
419 else if (c != ' ')
420 break;
422 else
423 break;
425 FORWARD(-1);
428 /* Read the rest of the current line.
429 The line is appended to PFILE's output buffer. */
431 static void
432 copy_rest_of_line (pfile)
433 cpp_reader *pfile;
435 for (;;)
437 int c = GETC();
438 switch (c)
440 case '\n':
441 FORWARD(-1);
442 case EOF:
443 CPP_NUL_TERMINATE (pfile);
444 return;
446 case '\r':
447 if (CPP_BUFFER (pfile)->has_escapes)
448 break;
449 else
451 CPP_BUFFER (pfile)->lineno++;
452 continue;
454 case '\'':
455 case '\"':
456 parse_string (pfile, c);
457 continue;
458 case '/':
459 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile))
461 CPP_PUTS (pfile, "/**/", 4);
462 skip_comment (pfile, c);
463 continue;
465 /* else fall through */
466 case '-':
467 c = skip_comment (pfile, c);
468 break;
470 case '\f':
471 case '\v':
472 if (CPP_PEDANTIC (pfile))
473 cpp_pedwarn (pfile, "%s in preprocessing directive",
474 c == '\f' ? "formfeed" : "vertical tab");
475 break;
478 CPP_PUTC (pfile, c);
482 /* FIXME: It is almost definitely a performance win to make this do
483 the scan itself. >75% of calls to copy_r_o_l are from here or
484 skip_if_group, which means the common case is to copy stuff into the
485 token_buffer only to discard it. */
486 void
487 skip_rest_of_line (pfile)
488 cpp_reader *pfile;
490 long old = CPP_WRITTEN (pfile);
491 copy_rest_of_line (pfile);
492 CPP_SET_WRITTEN (pfile, old);
495 /* Handle a possible # directive.
496 '#' has already been read. */
498 static int
499 handle_directive (pfile)
500 cpp_reader *pfile;
502 int c;
503 register const struct directive *kt;
504 int ident_length;
505 U_CHAR *ident;
506 long old_written = CPP_WRITTEN (pfile);
508 cpp_skip_hspace (pfile);
510 c = PEEKC ();
511 if (c >= '0' && c <= '9')
513 /* Handle # followed by a line number. */
514 if (CPP_PEDANTIC (pfile))
515 cpp_pedwarn (pfile, "`#' followed by integer");
516 do_line (pfile, NULL);
517 return 1;
520 /* Now find the directive name. */
521 CPP_PUTC (pfile, '#');
522 parse_name (pfile, GETC());
523 ident = pfile->token_buffer + old_written + 1;
524 ident_length = CPP_PWRITTEN (pfile) - ident;
525 if (ident_length == 0)
527 /* A line of just `#' becomes blank. */
528 if (PEEKC() == '\n')
529 return 1;
530 else
531 return 0;
535 * Decode the keyword and call the appropriate expansion
536 * routine, after moving the input pointer up to the next line.
538 for (kt = directive_table; ; kt++)
540 if (kt->length <= 0)
541 return 0;
542 if (kt->length == ident_length
543 && !strncmp (kt->name, ident, ident_length))
544 break;
547 CPP_SET_WRITTEN (pfile, old_written);
548 (*kt->func) (pfile, kt);
550 return 1;
553 /* Pass a directive through to the output file.
554 BUF points to the contents of the directive, as a contiguous string.
555 LEN is the length of the string pointed to by BUF.
556 KEYWORD is the keyword-table entry for the directive. */
558 static void
559 pass_thru_directive (buf, len, pfile, keyword)
560 U_CHAR *buf;
561 size_t len;
562 cpp_reader *pfile;
563 const struct directive *keyword;
565 register unsigned keyword_length = keyword->length;
567 CPP_RESERVE (pfile, 1 + keyword_length + len);
568 CPP_PUTC_Q (pfile, '#');
569 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
570 if (len != 0 && buf[0] != ' ')
571 CPP_PUTC_Q (pfile, ' ');
572 CPP_PUTS_Q (pfile, buf, len);
575 /* Check a purported macro name SYMNAME, and yield its length.
576 ASSERTION is nonzero if this is really for an assertion name. */
579 check_macro_name (pfile, symname, assertion)
580 cpp_reader *pfile;
581 U_CHAR *symname;
582 int assertion;
584 U_CHAR *p;
585 int sym_length;
587 for (p = symname; is_idchar[*p]; p++)
589 sym_length = p - symname;
590 if (sym_length == 0
591 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
592 cpp_error (pfile,
593 assertion ? "invalid assertion name" : "invalid macro name");
594 else if (!is_idstart[*symname]
595 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
596 U_CHAR *msg; /* what pain... */
597 msg = (U_CHAR *) alloca (sym_length + 1);
598 bcopy (symname, msg, sym_length);
599 msg[sym_length] = 0;
600 cpp_error (pfile,
601 (assertion
602 ? "invalid assertion name `%s'"
603 : "invalid macro name `%s'"),
604 msg);
606 return sym_length;
609 /* Process a #define command.
610 KEYWORD is the keyword-table entry for #define,
611 or NULL for a "predefined" macro. */
613 static int
614 do_define (pfile, keyword)
615 cpp_reader *pfile;
616 const struct directive *keyword;
618 int hashcode;
619 MACRODEF mdef;
620 HASHNODE *hp;
621 long here;
622 U_CHAR *macro, *buf, *end;
624 here = CPP_WRITTEN (pfile);
625 copy_rest_of_line (pfile);
627 /* Copy out the line so we can pop the token buffer. */
628 buf = pfile->token_buffer + here;
629 end = CPP_PWRITTEN (pfile);
630 macro = (U_CHAR *) alloca (end - buf + 1);
631 bcopy (buf, macro, end - buf + 1);
632 end = macro + (end - buf);
634 CPP_SET_WRITTEN (pfile, here);
636 mdef = create_definition (macro, end, pfile, keyword == NULL);
637 if (mdef.defn == 0)
638 return 0;
640 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
642 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
644 int ok = 0;
645 /* Redefining a precompiled key is ok. */
646 if (hp->type == T_PCSTRING)
647 ok = 1;
648 /* Redefining a macro is ok if the definitions are the same. */
649 else if (hp->type == T_MACRO)
650 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
651 /* Redefining a constant is ok with -D. */
652 else if (hp->type == T_CONST || hp->type == T_STDC)
653 ok = ! CPP_OPTIONS (pfile)->done_initializing;
654 /* Print the warning if it's not ok. */
655 if (!ok)
657 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
658 if (hp->type == T_MACRO)
659 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
660 hp->value.defn->line,
661 "this is the location of the previous definition");
663 /* Replace the old definition. */
664 hp->type = T_MACRO;
665 hp->value.defn = mdef.defn;
667 else
668 cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO,
669 (char *) mdef.defn, hashcode);
671 if (keyword)
673 if (CPP_OPTIONS (pfile)->debug_output
674 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
675 dump_definition (pfile, mdef);
676 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
677 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
680 return 0;
684 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
685 If BUFFER != NULL, then use the LENGTH characters in BUFFER
686 as the new input buffer.
687 Return the new buffer, or NULL on failure. */
689 cpp_buffer *
690 cpp_push_buffer (pfile, buffer, length)
691 cpp_reader *pfile;
692 U_CHAR *buffer;
693 long length;
695 cpp_buffer *buf = CPP_BUFFER (pfile);
696 cpp_buffer *new;
697 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
699 cpp_fatal (pfile, "macro or `#include' recursion too deep");
700 return NULL;
703 new = (cpp_buffer *) xcalloc (sizeof (cpp_buffer), 1);
705 new->if_stack = pfile->if_stack;
706 new->cleanup = null_cleanup;
707 new->underflow = null_underflow;
708 new->buf = new->cur = buffer;
709 new->alimit = new->rlimit = buffer + length;
710 new->prev = buf;
711 new->mark = -1;
713 CPP_BUFFER (pfile) = new;
714 return new;
717 cpp_buffer *
718 cpp_pop_buffer (pfile)
719 cpp_reader *pfile;
721 cpp_buffer *buf = CPP_BUFFER (pfile);
722 (*buf->cleanup) (buf, pfile);
723 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
724 free (buf);
725 pfile->buffer_stack_depth--;
726 return CPP_BUFFER (pfile);
729 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
730 Pop the buffer when done. */
732 void
733 cpp_scan_buffer (pfile)
734 cpp_reader *pfile;
736 cpp_buffer *buffer = CPP_BUFFER (pfile);
737 enum cpp_token token;
738 if (CPP_OPTIONS (pfile)->no_output)
740 long old_written = CPP_WRITTEN (pfile);
741 /* In no-output mode, we can ignore everything but directives. */
742 for (;;)
744 if (! pfile->only_seen_white)
745 skip_rest_of_line (pfile);
746 token = cpp_get_token (pfile);
747 if (token == CPP_EOF) /* Should not happen ... */
748 break;
749 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
751 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
752 != CPP_NULL_BUFFER (pfile))
753 cpp_pop_buffer (pfile);
754 break;
757 CPP_SET_WRITTEN (pfile, old_written);
759 else
761 for (;;)
763 token = cpp_get_token (pfile);
764 if (token == CPP_EOF) /* Should not happen ... */
765 break;
766 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
768 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
769 != CPP_NULL_BUFFER (pfile))
770 cpp_pop_buffer (pfile);
771 break;
778 * Rescan a string (which may have escape marks) into pfile's buffer.
779 * Place the result in pfile->token_buffer.
781 * The input is copied before it is scanned, so it is safe to pass
782 * it something from the token_buffer that will get overwritten
783 * (because it follows CPP_WRITTEN). This is used by do_include.
786 void
787 cpp_expand_to_buffer (pfile, buf, length)
788 cpp_reader *pfile;
789 U_CHAR *buf;
790 int length;
792 register cpp_buffer *ip;
793 U_CHAR *buf1;
794 int save_no_output;
796 if (length < 0)
798 cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
799 return;
802 /* Set up the input on the input stack. */
804 buf1 = (U_CHAR *) alloca (length + 1);
805 memcpy (buf1, buf, length);
806 buf1[length] = 0;
808 ip = cpp_push_buffer (pfile, buf1, length);
809 if (ip == NULL)
810 return;
811 ip->has_escapes = 1;
813 /* Scan the input, create the output. */
814 save_no_output = CPP_OPTIONS (pfile)->no_output;
815 CPP_OPTIONS (pfile)->no_output = 0;
816 cpp_scan_buffer (pfile);
817 CPP_OPTIONS (pfile)->no_output = save_no_output;
819 CPP_NUL_TERMINATE (pfile);
822 void
823 cpp_buf_line_and_col (pbuf, linep, colp)
824 register cpp_buffer *pbuf;
825 long *linep, *colp;
827 if (pbuf)
829 *linep = pbuf->lineno;
830 if (colp)
831 *colp = pbuf->cur - pbuf->line_base;
833 else
835 *linep = 0;
836 if (colp)
837 *colp = 0;
841 /* Return the cpp_buffer that corresponds to a file (not a macro). */
843 cpp_buffer *
844 cpp_file_buffer (pfile)
845 cpp_reader *pfile;
847 cpp_buffer *ip = CPP_BUFFER (pfile);
849 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
850 if (ip->fname != NULL)
851 return ip;
852 return NULL;
856 * write out a #line command, for instance, after an #include file.
857 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
860 void
861 output_line_command (pfile, file_change)
862 cpp_reader *pfile;
863 enum file_change_code file_change;
865 long line;
866 cpp_buffer *ip = CPP_BUFFER (pfile);
868 if (ip->fname == NULL)
869 return;
871 if (CPP_OPTIONS (pfile)->no_line_commands
872 || CPP_OPTIONS (pfile)->no_output)
873 return;
875 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
877 /* If the current file has not changed, we omit the #line if it would
878 appear to be a no-op, and we output a few newlines instead
879 if we want to increase the line number by a small amount.
880 We cannot do this if pfile->lineno is zero, because that means we
881 haven't output any line commands yet. (The very first line command
882 output is a `same_file' command.) */
883 if (file_change == same_file && pfile->lineno != 0)
885 if (line == pfile->lineno)
886 return;
888 /* If the inherited line number is a little too small,
889 output some newlines instead of a #line command. */
890 if (line > pfile->lineno && line < pfile->lineno + 8)
892 CPP_RESERVE (pfile, 20);
893 while (line > pfile->lineno)
895 CPP_PUTC_Q (pfile, '\n');
896 pfile->lineno++;
898 return;
902 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
903 CPP_PUTS_Q (pfile, "# ", 2);
905 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
906 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
908 quote_string (pfile, ip->nominal_fname);
909 if (file_change != same_file)
911 CPP_PUTC_Q (pfile, ' ');
912 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
914 /* Tell cc1 if following text comes from a system header file. */
915 if (ip->system_header_p)
917 CPP_PUTC_Q (pfile, ' ');
918 CPP_PUTC_Q (pfile, '3');
920 #ifndef NO_IMPLICIT_EXTERN_C
921 /* Tell cc1plus if following text should be treated as C. */
922 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
924 CPP_PUTC_Q (pfile, ' ');
925 CPP_PUTC_Q (pfile, '4');
927 #endif
928 CPP_PUTC_Q (pfile, '\n');
929 pfile->lineno = line;
933 /* Like cpp_get_token, except that it does not read past end-of-line.
934 Also, horizontal space is skipped, and macros are popped. */
936 static enum cpp_token
937 get_directive_token (pfile)
938 cpp_reader *pfile;
940 for (;;)
942 long old_written = CPP_WRITTEN (pfile);
943 enum cpp_token token;
944 cpp_skip_hspace (pfile);
945 if (PEEKC () == '\n')
946 return CPP_VSPACE;
947 token = cpp_get_token (pfile);
948 switch (token)
950 case CPP_POP:
951 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
952 return token;
953 /* ... else fall though ... */
954 case CPP_HSPACE: case CPP_COMMENT:
955 CPP_SET_WRITTEN (pfile, old_written);
956 break;
957 default:
958 return token;
963 /* Handle #include and #import.
964 This function expects to see "fname" or <fname> on the input.
966 The input is normally in part of the output_buffer following
967 CPP_WRITTEN, and will get overwritten by output_line_command.
968 I.e. in input file specification has been popped by handle_directive.
969 This is safe. */
971 static int
972 do_include (pfile, keyword)
973 cpp_reader *pfile;
974 const struct directive *keyword;
976 int importing = (keyword->type == T_IMPORT);
977 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
978 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
979 int before; /* included before? */
980 long flen;
981 unsigned char *ftok;
982 cpp_buffer *fp;
984 enum cpp_token token;
986 /* Chain of dirs to search */
987 struct include_hash *ihash;
988 struct file_name_list *search_start;
990 long old_written = CPP_WRITTEN (pfile);
992 int fd;
994 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
996 if (importing)
997 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
998 if (skip_dirs)
999 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1002 if (importing && CPP_OPTIONS (pfile)->warn_import
1003 && !CPP_OPTIONS (pfile)->inhibit_warnings
1004 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1006 pfile->import_warning = 1;
1007 cpp_warning (pfile,
1008 "#import is obsolete, use an #ifndef wrapper in the header file");
1011 pfile->parsing_include_directive++;
1012 token = get_directive_token (pfile);
1013 pfile->parsing_include_directive--;
1015 if (token == CPP_STRING)
1017 if (pfile->token_buffer[old_written] == '<')
1018 angle_brackets = 1;
1020 #ifdef VMS
1021 else if (token == CPP_NAME)
1023 /* Support '#include xyz' like VAX-C. It is taken as
1024 '#include <xyz.h>' and generates a warning. */
1025 cpp_warning (pfile,
1026 "`#include filename' is obsolete, use `#include <filename.h>'");
1027 angle_brackets = 1;
1029 /* Append the missing `.h' to the name. */
1030 CPP_PUTS (pfile, ".h", 2);
1032 #endif
1033 else
1035 cpp_error (pfile,
1036 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1037 CPP_SET_WRITTEN (pfile, old_written);
1038 skip_rest_of_line (pfile);
1039 return 0;
1042 flen = CPP_WRITTEN (pfile) - old_written;
1043 ftok = (unsigned char *) alloca (flen + 1);
1044 memcpy (ftok, pfile->token_buffer + old_written, flen);
1045 ftok[flen] = '\0';
1047 if (get_directive_token (pfile) != CPP_VSPACE)
1049 cpp_error (pfile, "junk at end of `#include'");
1050 skip_rest_of_line (pfile);
1053 CPP_SET_WRITTEN (pfile, old_written);
1055 if (flen == 0)
1057 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1058 return 0;
1061 if (CPP_OPTIONS (pfile)->dump_includes)
1062 pass_thru_directive (ftok,
1063 flen
1064 #ifdef VMS
1065 - ((token == CPP_NAME) ? 2 : 0)
1066 #endif
1067 , pfile, keyword);
1069 #ifdef VMS
1070 if (token == CPP_STRING)
1071 #endif
1073 ftok++;
1074 flen -= 2;
1075 ftok[flen] = '\0';
1078 search_start = 0;
1080 for (fp = CPP_BUFFER (pfile);
1081 fp != CPP_NULL_BUFFER (pfile);
1082 fp = CPP_PREV_BUFFER (fp))
1083 if (fp->fname != NULL)
1084 break;
1086 if (fp == CPP_NULL_BUFFER (pfile))
1088 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1089 return 0;
1092 /* For #include_next, skip in the search path past the dir in which the
1093 containing file was found. Treat files specified using an absolute path
1094 as if there are no more directories to search. Treat the primary source
1095 file like any other included source, but generate a warning. */
1096 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1098 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1099 search_start = fp->ihash->foundhere->next;
1101 else
1103 if (skip_dirs)
1104 cpp_warning (pfile, "#include_next in primary source file");
1106 if (angle_brackets)
1107 search_start = CPP_OPTIONS (pfile)->bracket_include;
1108 else
1110 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1112 if (fp)
1113 search_start = fp->actual_dir;
1115 else
1116 search_start = CPP_OPTIONS (pfile)->quote_include;
1120 if (!search_start)
1122 cpp_error (pfile, "No include path in which to find %s", ftok);
1123 return 0;
1126 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1128 if (fd == -2)
1129 return 0;
1131 if (fd == -1)
1133 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1134 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1135 (pfile->system_include_depth > 0)))
1137 if (!angle_brackets)
1138 deps_output (pfile, ftok, ' ');
1139 else
1141 char *p;
1142 struct file_name_list *ptr;
1143 /* If requested as a system header, assume it belongs in
1144 the first system header directory. */
1145 if (CPP_OPTIONS (pfile)->bracket_include)
1146 ptr = CPP_OPTIONS (pfile)->bracket_include;
1147 else
1148 ptr = CPP_OPTIONS (pfile)->quote_include;
1150 p = (char *) alloca (strlen (ptr->name)
1151 + strlen (ftok) + 2);
1152 if (*ptr->name != '\0')
1154 strcpy (p, ptr->name);
1155 strcat (p, "/");
1157 strcat (p, ftok);
1158 deps_output (pfile, p, ' ');
1161 /* If -M was specified, and this header file won't be added to
1162 the dependency list, then don't count this as an error,
1163 because we can still produce correct output. Otherwise, we
1164 can't produce correct output, because there may be
1165 dependencies we need inside the missing file, and we don't
1166 know what directory this missing file exists in. */
1167 else if (CPP_PRINT_DEPS (pfile)
1168 && (CPP_PRINT_DEPS (pfile)
1169 <= (angle_brackets || (pfile->system_include_depth > 0))))
1170 cpp_warning (pfile, "No include path in which to find %s", ftok);
1171 else
1172 cpp_error_from_errno (pfile, ftok);
1174 return 0;
1177 /* For -M, add the file to the dependencies on its first inclusion. */
1178 if (!before && (CPP_PRINT_DEPS (pfile)
1179 > (angle_brackets || (pfile->system_include_depth > 0))))
1180 deps_output (pfile, ihash->name, ' ');
1182 /* Handle -H option. */
1183 if (CPP_OPTIONS(pfile)->print_include_names)
1185 fp = CPP_BUFFER (pfile);
1186 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1187 putc ('.', stderr);
1188 fprintf (stderr, " %s\n", ihash->name);
1191 /* Actually process the file */
1193 if (importing)
1194 ihash->control_macro = "";
1196 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1198 close (fd);
1199 return 0;
1202 if (angle_brackets)
1203 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1205 if (finclude (pfile, fd, ihash))
1207 output_line_command (pfile, enter_file);
1208 pfile->only_seen_white = 2;
1211 return 0;
1214 /* Subroutine of do_line. Read next token from PFILE without adding it to
1215 the output buffer. If it is a number between 1 and 4, store it in *NUM
1216 and return 1; otherwise, return 0 and complain if we aren't at the end
1217 of the directive. */
1219 static int
1220 read_line_number (pfile, num)
1221 cpp_reader *pfile;
1222 int *num;
1224 long save_written = CPP_WRITTEN (pfile);
1225 U_CHAR *p = pfile->token_buffer + save_written;
1226 enum cpp_token token = get_directive_token (pfile);
1227 CPP_SET_WRITTEN (pfile, save_written);
1229 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1231 *num = p[0] - '0';
1232 return 1;
1234 else
1236 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1237 cpp_error (pfile, "invalid format `#line' command");
1238 return 0;
1242 /* Interpret #line command.
1243 Note that the filename string (if any) is treated as if it were an
1244 include filename. That means no escape handling. */
1246 static int
1247 do_line (pfile, keyword)
1248 cpp_reader *pfile;
1249 const struct directive *keyword ATTRIBUTE_UNUSED;
1251 cpp_buffer *ip = CPP_BUFFER (pfile);
1252 int new_lineno;
1253 long old_written = CPP_WRITTEN (pfile);
1254 enum file_change_code file_change = same_file;
1255 enum cpp_token token;
1256 char *x;
1258 token = get_directive_token (pfile);
1260 if (token != CPP_NUMBER)
1262 cpp_error (pfile, "token after `#line' is not an integer");
1263 goto bad_line_directive;
1266 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1267 if (x[0] != '\0')
1269 cpp_error (pfile, "token after `#line' is not an integer");
1270 goto bad_line_directive;
1272 CPP_SET_WRITTEN (pfile, old_written);
1274 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1275 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1277 token = get_directive_token (pfile);
1279 if (token == CPP_STRING)
1281 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1282 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1283 int action_number = 0;
1285 if (read_line_number (pfile, &action_number))
1287 if (CPP_PEDANTIC (pfile))
1288 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1290 if (action_number == 1)
1292 file_change = enter_file;
1293 read_line_number (pfile, &action_number);
1295 else if (action_number == 2)
1297 file_change = leave_file;
1298 read_line_number (pfile, &action_number);
1300 if (action_number == 3)
1302 ip->system_header_p = 1;
1303 read_line_number (pfile, &action_number);
1305 if (action_number == 4)
1307 ip->system_header_p = 2;
1308 read_line_number (pfile, &action_number);
1312 *end_name = '\0';
1314 if (strcmp (fname, ip->nominal_fname))
1316 char *newname, *oldname;
1317 if (!strcmp (fname, ip->fname))
1318 newname = ip->fname;
1319 else if (ip->last_nominal_fname
1320 && !strcmp (fname, ip->last_nominal_fname))
1321 newname = ip->last_nominal_fname;
1322 else
1323 newname = xstrdup (fname);
1325 oldname = ip->nominal_fname;
1326 ip->nominal_fname = newname;
1328 if (ip->last_nominal_fname
1329 && ip->last_nominal_fname != oldname
1330 && ip->last_nominal_fname != newname
1331 && ip->last_nominal_fname != ip->fname)
1332 free (ip->last_nominal_fname);
1334 if (newname == ip->fname)
1335 ip->last_nominal_fname = NULL;
1336 else
1337 ip->last_nominal_fname = oldname;
1340 else if (token != CPP_VSPACE && token != CPP_EOF)
1342 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1343 goto bad_line_directive;
1346 /* The Newline at the end of this line remains to be processed.
1347 To put the next line at the specified line number,
1348 we must store a line number now that is one less. */
1349 ip->lineno = new_lineno - 1;
1350 CPP_SET_WRITTEN (pfile, old_written);
1351 output_line_command (pfile, file_change);
1352 return 0;
1354 bad_line_directive:
1355 skip_rest_of_line (pfile);
1356 CPP_SET_WRITTEN (pfile, old_written);
1357 return 0;
1360 /* Remove the definition of a symbol from the symbol table.
1361 According to the C standard, it is not an error to undef
1362 something that has no definitions. */
1363 static int
1364 do_undef (pfile, keyword)
1365 cpp_reader *pfile;
1366 const struct directive *keyword;
1368 int sym_length;
1369 HASHNODE *hp;
1370 U_CHAR *buf, *name, *limit;
1371 int c;
1372 long here = CPP_WRITTEN (pfile);
1373 enum cpp_token token;
1375 cpp_skip_hspace (pfile);
1376 c = GETC();
1377 if (! is_idstart[c])
1379 cpp_error (pfile, "token after #undef is not an identifier");
1380 skip_rest_of_line (pfile);
1381 return 1;
1384 parse_name (pfile, c);
1385 buf = pfile->token_buffer + here;
1386 limit = CPP_PWRITTEN(pfile);
1388 /* Copy out the token so we can pop the token buffer. */
1389 name = (U_CHAR *) alloca (limit - buf + 1);
1390 bcopy(buf, name, limit - buf);
1391 name[limit - buf] = '\0';
1393 token = get_directive_token (pfile);
1394 if (token != CPP_VSPACE && token != CPP_POP)
1396 cpp_pedwarn (pfile, "junk on line after #undef");
1397 skip_rest_of_line (pfile);
1400 CPP_SET_WRITTEN (pfile, here);
1402 sym_length = check_macro_name (pfile, buf, 0);
1404 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1406 /* If we are generating additional info for debugging (with -g) we
1407 need to pass through all effective #undef commands. */
1408 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1409 pass_thru_directive (name, sym_length, pfile, keyword);
1410 if (hp->type != T_MACRO)
1411 cpp_warning (pfile, "undefining `%s'", hp->name);
1412 delete_macro (hp);
1415 return 0;
1418 /* Wrap do_undef for -U processing. */
1419 void
1420 cpp_undef (pfile, macro)
1421 cpp_reader *pfile;
1422 U_CHAR *macro;
1424 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1426 do_undef (pfile, NULL);
1427 cpp_pop_buffer (pfile);
1433 * Report an error detected by the program we are processing.
1434 * Use the text of the line in the error message.
1435 * (We use error because it prints the filename & line#.)
1438 static int
1439 do_error (pfile, keyword)
1440 cpp_reader *pfile;
1441 const struct directive *keyword ATTRIBUTE_UNUSED;
1443 long here = CPP_WRITTEN (pfile);
1444 U_CHAR *text;
1445 copy_rest_of_line (pfile);
1446 text = pfile->token_buffer + here;
1447 SKIP_WHITE_SPACE(text);
1449 cpp_error (pfile, "#error %s", text);
1450 CPP_SET_WRITTEN (pfile, here);
1451 return 0;
1455 * Report a warning detected by the program we are processing.
1456 * Use the text of the line in the warning message, then continue.
1459 static int
1460 do_warning (pfile, keyword)
1461 cpp_reader *pfile;
1462 const struct directive *keyword ATTRIBUTE_UNUSED;
1464 U_CHAR *text;
1465 long here = CPP_WRITTEN(pfile);
1466 copy_rest_of_line (pfile);
1467 text = pfile->token_buffer + here;
1468 SKIP_WHITE_SPACE(text);
1470 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1471 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1473 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1474 if -pedantic-errors is given, #warning should cause an error. */
1475 cpp_pedwarn (pfile, "#warning %s", text);
1476 CPP_SET_WRITTEN (pfile, here);
1477 return 0;
1480 /* Report program identification.
1481 This is not precisely what cccp does with #ident, however I believe
1482 it matches `closely enough' (behavior is identical as long as there
1483 are no macros on the #ident line, which is pathological in my opinion). */
1485 static int
1486 do_ident (pfile, keyword)
1487 cpp_reader *pfile;
1488 const struct directive *keyword ATTRIBUTE_UNUSED;
1490 /* Allow #ident in system headers, since that's not user's fault. */
1491 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1492 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1494 CPP_PUTS (pfile, "#ident ", 7);
1495 cpp_skip_hspace (pfile);
1496 copy_rest_of_line (pfile);
1498 return 0;
1501 /* Just check for some recognized pragmas that need validation here,
1502 and leave the text in the token buffer to be output. */
1504 static int
1505 do_pragma (pfile, keyword)
1506 cpp_reader *pfile;
1507 const struct directive *keyword ATTRIBUTE_UNUSED;
1509 long here;
1510 U_CHAR *buf;
1512 CPP_PUTS (pfile, "#pragma ", 8);
1513 cpp_skip_hspace (pfile);
1515 here = CPP_WRITTEN (pfile);
1516 copy_rest_of_line (pfile);
1517 buf = pfile->token_buffer + here;
1519 if (!strncmp (buf, "once", 4))
1521 cpp_buffer *ip = NULL;
1523 /* Allow #pragma once in system headers, since that's not the user's
1524 fault. */
1525 if (!CPP_BUFFER (pfile)->system_header_p)
1526 cpp_warning (pfile, "`#pragma once' is obsolete");
1528 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1530 if (ip == CPP_NULL_BUFFER (pfile))
1531 return 0;
1532 if (ip->fname != NULL)
1533 break;
1536 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1537 cpp_warning (pfile, "`#pragma once' outside include file");
1538 else
1539 ip->ihash->control_macro = ""; /* never repeat */
1541 else if (!strncmp (buf, "implementation", 14))
1543 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1544 been included yet. */
1545 struct include_hash *ptr;
1546 U_CHAR *p = buf + 14, *fname, *fcopy;
1547 SKIP_WHITE_SPACE (p);
1548 if (*p == '\n' || *p != '\"')
1549 return 0;
1551 fname = p + 1;
1552 p = (U_CHAR *) index (fname, '\"');
1554 fcopy = (U_CHAR *) alloca (p - fname + 1);
1555 bcopy (fname, fcopy, p - fname);
1556 fcopy[p-fname] = '\0';
1558 ptr = include_hash (pfile, fcopy, 0);
1559 if (ptr)
1560 cpp_warning (pfile,
1561 "`#pragma implementation' for `%s' appears after file is included",
1562 fcopy);
1565 return 0;
1568 #ifdef SCCS_DIRECTIVE
1569 /* Just ignore #sccs, on systems where we define it at all. */
1571 static int
1572 do_sccs (pfile, keyword)
1573 cpp_reader *pfile;
1574 const struct directive *keyword ATTRIBUTE_UNUSED;
1576 if (CPP_PEDANTIC (pfile))
1577 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1578 skip_rest_of_line (pfile);
1579 return 0;
1581 #endif
1584 /* We've found an `#if' directive. If the only thing before it in
1585 this file is white space, and if it is of the form
1586 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1587 for inclusion of this file. (See redundant_include_p in cppfiles.c
1588 for an explanation of controlling macros.) If so, return a
1589 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1591 static U_CHAR *
1592 detect_if_not_defined (pfile)
1593 cpp_reader *pfile;
1595 U_CHAR *control_macro = 0;
1597 if (pfile->only_seen_white == 2)
1599 char *ident;
1600 enum cpp_token token;
1601 int base_offset;
1602 int token_offset;
1603 int need_rparen = 0;
1605 /* Save state required for restore. */
1606 pfile->no_macro_expand++;
1607 parse_set_mark (pfile);
1608 base_offset = CPP_WRITTEN (pfile);
1610 /* Look for `!', */
1611 if (get_directive_token (pfile) != CPP_OTHER
1612 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1613 || CPP_PWRITTEN (pfile)[-1] != '!')
1614 goto restore;
1616 /* ...then `defined', */
1617 token_offset = CPP_WRITTEN (pfile);
1618 token = get_directive_token (pfile);
1619 if (token != CPP_NAME)
1620 goto restore;
1621 ident = pfile->token_buffer + token_offset;
1622 CPP_NUL_TERMINATE (pfile);
1623 if (strcmp (ident, "defined"))
1624 goto restore;
1626 /* ...then an optional '(' and the name, */
1627 token_offset = CPP_WRITTEN (pfile);
1628 token = get_directive_token (pfile);
1629 if (token == CPP_LPAREN)
1631 token_offset = CPP_WRITTEN (pfile);
1632 token = get_directive_token (pfile);
1633 if (token != CPP_NAME)
1634 goto restore;
1635 need_rparen = 1;
1637 else if (token != CPP_NAME)
1638 goto restore;
1640 ident = pfile->token_buffer + token_offset;
1641 CPP_NUL_TERMINATE (pfile);
1643 /* ...then the ')', if necessary, */
1644 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1645 /* ...and make sure there's nothing else on the line. */
1646 && get_directive_token (pfile) == CPP_VSPACE)
1647 control_macro = xstrdup (ident);
1649 restore:
1650 CPP_SET_WRITTEN (pfile, base_offset);
1651 pfile->no_macro_expand--;
1652 parse_goto_mark (pfile);
1655 return control_macro;
1659 * handle #if command by
1660 * 1) inserting special `defined' keyword into the hash table
1661 * that gets turned into 0 or 1 by special_symbol (thus,
1662 * if the luser has a symbol called `defined' already, it won't
1663 * work inside the #if command)
1664 * 2) rescan the input into a temporary output buffer
1665 * 3) pass the output buffer to the yacc parser and collect a value
1666 * 4) clean up the mess left from steps 1 and 2.
1667 * 5) call conditional_skip to skip til the next #endif (etc.),
1668 * or not, depending on the value from step 3.
1671 static int
1672 do_if (pfile, keyword)
1673 cpp_reader *pfile;
1674 const struct directive *keyword ATTRIBUTE_UNUSED;
1676 U_CHAR *control_macro = detect_if_not_defined (pfile);
1677 HOST_WIDEST_INT value = eval_if_expression (pfile);
1678 conditional_skip (pfile, value == 0, T_IF, control_macro);
1679 return 0;
1683 * handle a #elif directive by not changing if_stack either.
1684 * see the comment above do_else.
1687 static int
1688 do_elif (pfile, keyword)
1689 cpp_reader *pfile;
1690 const struct directive *keyword ATTRIBUTE_UNUSED;
1692 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1693 cpp_error (pfile, "`#elif' not within a conditional");
1694 return 0;
1695 } else {
1696 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1697 cpp_error (pfile, "`#elif' after `#else'");
1698 #if 0
1699 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1700 #endif
1701 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1702 && strcmp (pfile->if_stack->fname,
1703 CPP_BUFFER (pfile)->nominal_fname) != 0)
1704 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1705 fprintf (stderr, ")\n");
1707 pfile->if_stack->type = T_ELIF;
1710 if (pfile->if_stack->if_succeeded)
1711 skip_if_group (pfile);
1712 else {
1713 HOST_WIDEST_INT value = eval_if_expression (pfile);
1714 if (value == 0)
1715 skip_if_group (pfile);
1716 else {
1717 ++pfile->if_stack->if_succeeded; /* continue processing input */
1718 output_line_command (pfile, same_file);
1721 return 0;
1725 * evaluate a #if expression in BUF, of length LENGTH,
1726 * then parse the result as a C expression and return the value as an int.
1729 static HOST_WIDEST_INT
1730 eval_if_expression (pfile)
1731 cpp_reader *pfile;
1733 HOST_WIDEST_INT value;
1734 long old_written = CPP_WRITTEN (pfile);
1736 pfile->pcp_inside_if = 1;
1737 value = cpp_parse_expr (pfile);
1738 pfile->pcp_inside_if = 0;
1740 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1742 return value;
1746 * routine to handle ifdef/ifndef. Try to look up the symbol,
1747 * then do or don't skip to the #endif/#else/#elif depending
1748 * on what directive is actually being processed.
1751 static int
1752 do_xifdef (pfile, keyword)
1753 cpp_reader *pfile;
1754 const struct directive *keyword;
1756 int skip;
1757 cpp_buffer *ip = CPP_BUFFER (pfile);
1758 U_CHAR *ident;
1759 int ident_length;
1760 enum cpp_token token;
1761 int start_of_file = 0;
1762 U_CHAR *control_macro = 0;
1763 int old_written = CPP_WRITTEN (pfile);
1765 /* Detect a #ifndef at start of file (not counting comments). */
1766 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1767 start_of_file = pfile->only_seen_white == 2;
1769 pfile->no_macro_expand++;
1770 token = get_directive_token (pfile);
1771 pfile->no_macro_expand--;
1773 ident = pfile->token_buffer + old_written;
1774 ident_length = CPP_WRITTEN (pfile) - old_written;
1775 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1777 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1779 skip = (keyword->type == T_IFDEF);
1780 if (! CPP_TRADITIONAL (pfile))
1781 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1783 else if (token == CPP_NAME)
1785 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1786 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1787 if (start_of_file && !skip)
1789 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1790 bcopy (ident, control_macro, ident_length + 1);
1793 else
1795 skip = (keyword->type == T_IFDEF);
1796 if (! CPP_TRADITIONAL (pfile))
1797 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1800 if (!CPP_TRADITIONAL (pfile))
1801 { int c;
1802 cpp_skip_hspace (pfile);
1803 c = PEEKC ();
1804 if (c != EOF && c != '\n')
1805 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1807 skip_rest_of_line (pfile);
1809 #if 0
1810 if (pcp_outfile) {
1811 /* Output a precondition for this macro. */
1812 if (hp && hp->value.defn->predefined)
1813 fprintf (pcp_outfile, "#define %s\n", hp->name);
1814 else {
1815 U_CHAR *cp = buf;
1816 fprintf (pcp_outfile, "#undef ");
1817 while (is_idchar[*cp]) /* Ick! */
1818 fputc (*cp++, pcp_outfile);
1819 putc ('\n', pcp_outfile);
1821 #endif
1823 conditional_skip (pfile, skip, T_IF, control_macro);
1824 return 0;
1827 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1828 If this is a #ifndef starting at the beginning of a file,
1829 CONTROL_MACRO is the macro name tested by the #ifndef.
1830 Otherwise, CONTROL_MACRO is 0. */
1832 static void
1833 conditional_skip (pfile, skip, type, control_macro)
1834 cpp_reader *pfile;
1835 int skip;
1836 enum node_type type;
1837 U_CHAR *control_macro;
1839 IF_STACK_FRAME *temp;
1841 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1842 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1843 #if 0
1844 temp->lineno = CPP_BUFFER (pfile)->lineno;
1845 #endif
1846 temp->next = pfile->if_stack;
1847 temp->control_macro = control_macro;
1848 pfile->if_stack = temp;
1850 pfile->if_stack->type = type;
1852 if (skip != 0) {
1853 skip_if_group (pfile);
1854 return;
1855 } else {
1856 ++pfile->if_stack->if_succeeded;
1857 output_line_command (pfile, same_file);
1861 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1862 return 0 if skipping should continue, 1 if it should halt. Also
1863 adjusts the if_stack as appropriate.
1864 The `#' has been read, but not the identifier. */
1866 static int
1867 consider_directive_while_skipping (pfile, stack)
1868 cpp_reader *pfile;
1869 IF_STACK_FRAME *stack;
1871 long ident_len, ident;
1872 const struct directive *kt;
1873 IF_STACK_FRAME *temp;
1875 cpp_skip_hspace (pfile);
1877 ident = CPP_WRITTEN (pfile);
1878 parse_name (pfile, GETC());
1879 ident_len = CPP_WRITTEN (pfile) - ident;
1881 CPP_SET_WRITTEN (pfile, ident);
1883 for (kt = directive_table; kt->length >= 0; kt++)
1884 if (kt->length == ident_len
1885 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1886 switch (kt->type)
1888 case T_IF:
1889 case T_IFDEF:
1890 case T_IFNDEF:
1891 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1892 temp->next = pfile->if_stack;
1893 pfile->if_stack = temp;
1894 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1895 temp->type = kt->type;
1896 return 0;
1898 case T_ELSE:
1899 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1900 validate_else (pfile, "#else");
1901 /* fall through */
1902 case T_ELIF:
1903 if (pfile->if_stack->type == T_ELSE)
1904 cpp_error (pfile, "`%s' after `#else'", kt->name);
1906 if (pfile->if_stack == stack)
1907 return 1;
1908 else
1910 pfile->if_stack->type = kt->type;
1911 return 0;
1914 case T_ENDIF:
1915 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
1916 validate_else (pfile, "#endif");
1918 if (pfile->if_stack == stack)
1919 return 1;
1921 temp = pfile->if_stack;
1922 pfile->if_stack = temp->next;
1923 free (temp);
1924 return 0;
1926 default:
1927 return 0;
1930 /* Don't let erroneous code go by. */
1931 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
1932 cpp_pedwarn (pfile, "invalid preprocessor directive name");
1933 return 0;
1936 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1937 * leaves input ptr at the sharp sign found.
1939 static void
1940 skip_if_group (pfile)
1941 cpp_reader *pfile;
1943 int c;
1944 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
1945 U_CHAR *beg_of_line;
1946 long old_written;
1948 if (CPP_OPTIONS (pfile)->output_conditionals)
1950 CPP_PUTS (pfile, "#failed\n", 8);
1951 pfile->lineno++;
1952 output_line_command (pfile, same_file);
1955 old_written = CPP_WRITTEN (pfile);
1957 for (;;)
1959 beg_of_line = CPP_BUFFER (pfile)->cur;
1961 if (! CPP_TRADITIONAL (pfile))
1962 cpp_skip_hspace (pfile);
1963 c = GETC();
1964 if (c == '\n')
1966 if (CPP_OPTIONS (pfile)->output_conditionals)
1967 CPP_PUTC (pfile, c);
1968 CPP_BUMP_LINE (pfile);
1969 continue;
1971 else if (c == '#')
1973 if (consider_directive_while_skipping (pfile, save_if_stack))
1974 break;
1976 else if (c == EOF)
1977 return; /* Caller will issue error. */
1979 FORWARD(-1);
1980 if (CPP_OPTIONS (pfile)->output_conditionals)
1982 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
1983 copy_rest_of_line (pfile);
1985 else
1987 copy_rest_of_line (pfile);
1988 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
1991 c = GETC();
1992 if (c == EOF)
1993 return; /* Caller will issue error. */
1994 else
1996 /* \n */
1997 if (CPP_OPTIONS (pfile)->output_conditionals)
1999 CPP_PUTC (pfile, c);
2000 pfile->lineno++;
2002 CPP_BUMP_LINE (pfile);
2006 /* Back up to the beginning of this line. Caller will process the
2007 directive. */
2008 CPP_BUFFER (pfile)->cur = beg_of_line;
2009 pfile->only_seen_white = 1;
2010 if (CPP_OPTIONS (pfile)->output_conditionals)
2012 CPP_PUTS (pfile, "#endfailed\n", 11);
2013 pfile->lineno++;
2018 * handle a #else directive. Do this by just continuing processing
2019 * without changing if_stack ; this is so that the error message
2020 * for missing #endif's etc. will point to the original #if. It
2021 * is possible that something different would be better.
2024 static int
2025 do_else (pfile, keyword)
2026 cpp_reader *pfile;
2027 const struct directive *keyword ATTRIBUTE_UNUSED;
2029 cpp_buffer *ip = CPP_BUFFER (pfile);
2031 if (CPP_PEDANTIC (pfile))
2032 validate_else (pfile, "#else");
2033 skip_rest_of_line (pfile);
2035 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
2036 cpp_error (pfile, "`#else' not within a conditional");
2037 return 0;
2038 } else {
2039 /* #ifndef can't have its special treatment for containing the whole file
2040 if it has a #else clause. */
2041 pfile->if_stack->control_macro = 0;
2043 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
2044 cpp_error (pfile, "`#else' after `#else'");
2045 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
2046 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
2047 fprintf (stderr, ", file %s", pfile->if_stack->fname);
2048 fprintf (stderr, ")\n");
2050 pfile->if_stack->type = T_ELSE;
2053 if (pfile->if_stack->if_succeeded)
2054 skip_if_group (pfile);
2055 else {
2056 ++pfile->if_stack->if_succeeded; /* continue processing input */
2057 output_line_command (pfile, same_file);
2059 return 0;
2063 * unstack after #endif command
2066 static int
2067 do_endif (pfile, keyword)
2068 cpp_reader *pfile;
2069 const struct directive *keyword ATTRIBUTE_UNUSED;
2071 if (CPP_PEDANTIC (pfile))
2072 validate_else (pfile, "#endif");
2073 skip_rest_of_line (pfile);
2075 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2076 cpp_error (pfile, "unbalanced `#endif'");
2077 else
2079 IF_STACK_FRAME *temp = pfile->if_stack;
2080 pfile->if_stack = temp->next;
2081 if (temp->control_macro != 0)
2083 /* This #endif matched a #ifndef at the start of the file.
2084 See if it is at the end of the file. */
2085 int c;
2087 parse_set_mark (pfile);
2089 for (;;)
2091 cpp_skip_hspace (pfile);
2092 c = GETC ();
2093 if (c != '\n')
2094 break;
2096 parse_goto_mark (pfile);
2098 if (c == EOF)
2100 /* This #endif ends a #ifndef
2101 that contains all of the file (aside from whitespace).
2102 Arrange not to include the file again
2103 if the macro that was tested is defined. */
2104 struct cpp_buffer *ip;
2105 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2106 if (ip->fname != NULL)
2107 break;
2108 ip->ihash->control_macro = (char *) temp->control_macro;
2111 free (temp);
2112 output_line_command (pfile, same_file);
2114 return 0;
2117 /* When an #else or #endif is found while skipping failed conditional,
2118 if -pedantic was specified, this is called to warn about text after
2119 the command name. P points to the first char after the command name. */
2121 static void
2122 validate_else (pfile, directive)
2123 cpp_reader *pfile;
2124 const char *directive;
2126 int c;
2127 cpp_skip_hspace (pfile);
2128 c = PEEKC ();
2129 if (c != EOF && c != '\n')
2130 cpp_pedwarn (pfile,
2131 "text following `%s' violates ANSI standard", directive);
2134 /* Get the next token, and add it to the text in pfile->token_buffer.
2135 Return the kind of token we got. */
2137 enum cpp_token
2138 cpp_get_token (pfile)
2139 cpp_reader *pfile;
2141 register int c, c2, c3;
2142 enum cpp_token token;
2143 struct cpp_options *opts = CPP_OPTIONS (pfile);
2145 get_next:
2146 c = GETC();
2147 if (c == EOF)
2149 handle_eof:
2150 if (CPP_BUFFER (pfile)->manual_pop)
2151 /* If we've been reading from redirected input, the
2152 frontend will pop the buffer. */
2153 return CPP_EOF;
2154 else if (CPP_BUFFER (pfile)->seen_eof)
2156 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2157 return CPP_EOF;
2159 cpp_pop_buffer (pfile);
2160 goto get_next;
2162 else
2164 cpp_buffer *next_buf
2165 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2166 CPP_BUFFER (pfile)->seen_eof = 1;
2167 if (CPP_BUFFER (pfile)->nominal_fname
2168 && next_buf != CPP_NULL_BUFFER (pfile))
2170 /* We're about to return from an #include file.
2171 Emit #line information now (as part of the CPP_POP) result.
2172 But the #line refers to the file we will pop to. */
2173 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2174 CPP_BUFFER (pfile) = next_buf;
2175 pfile->input_stack_listing_current = 0;
2176 output_line_command (pfile, leave_file);
2177 CPP_BUFFER (pfile) = cur_buffer;
2179 return CPP_POP;
2182 else
2184 switch (c)
2186 case '/':
2187 if (PEEKC () == '=')
2188 goto op2;
2190 comment:
2191 if (opts->put_out_comments)
2192 c = copy_comment (pfile, c);
2193 else
2194 c = skip_comment (pfile, c);
2195 if (c == EOF)
2196 goto handle_eof;
2197 else if (c != ' ')
2198 goto randomchar;
2200 /* Comments are equivalent to spaces.
2201 For -traditional, a comment is equivalent to nothing. */
2202 if (opts->traditional || opts->put_out_comments)
2203 return CPP_COMMENT;
2204 else
2206 CPP_PUTC (pfile, c);
2207 return CPP_HSPACE;
2209 #if 0
2210 if (opts->for_lint) {
2211 U_CHAR *argbp;
2212 int cmdlen, arglen;
2213 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2215 if (lintcmd != NULL) {
2216 /* I believe it is always safe to emit this newline: */
2217 obp[-1] = '\n';
2218 bcopy ("#pragma lint ", (char *) obp, 13);
2219 obp += 13;
2220 bcopy (lintcmd, (char *) obp, cmdlen);
2221 obp += cmdlen;
2223 if (arglen != 0) {
2224 *(obp++) = ' ';
2225 bcopy (argbp, (char *) obp, arglen);
2226 obp += arglen;
2229 /* OK, now bring us back to the state we were in before we entered
2230 this branch. We need #line because the newline for the pragma
2231 could mess things up. */
2232 output_line_command (pfile, same_file);
2233 *(obp++) = ' '; /* just in case, if comments are copied thru */
2234 *(obp++) = '/';
2237 #endif
2239 case '#':
2240 #if 0
2241 /* If this is expanding a macro definition, don't recognize
2242 preprocessor directives. */
2243 if (ip->macro != 0)
2244 goto randomchar;
2245 /* If this is expand_into_temp_buffer, recognize them
2246 only after an actual newline at this level,
2247 not at the beginning of the input level. */
2248 if (ip->fname == 0 && beg_of_line == ip->buf)
2249 goto randomchar;
2250 if (ident_length)
2251 goto specialchar;
2252 #endif
2254 if (!pfile->only_seen_white)
2255 goto randomchar;
2256 if (handle_directive (pfile))
2257 return CPP_DIRECTIVE;
2258 pfile->only_seen_white = 0;
2259 return CPP_OTHER;
2261 case '\"':
2262 case '\'':
2263 string:
2264 parse_string (pfile, c);
2265 pfile->only_seen_white = 0;
2266 return c == '\'' ? CPP_CHAR : CPP_STRING;
2268 case '$':
2269 if (!opts->dollars_in_ident)
2270 goto randomchar;
2271 goto letter;
2273 case ':':
2274 if (opts->cplusplus && PEEKC () == ':')
2275 goto op2;
2276 goto randomchar;
2278 case '&':
2279 case '+':
2280 case '|':
2281 c2 = PEEKC ();
2282 if (c2 == c || c2 == '=')
2283 goto op2;
2284 goto randomchar;
2286 case '*':
2287 case '!':
2288 case '%':
2289 case '=':
2290 case '^':
2291 if (PEEKC () == '=')
2292 goto op2;
2293 goto randomchar;
2295 case '-':
2296 c2 = PEEKC ();
2297 if (c2 == '-' && opts->chill)
2298 goto comment; /* Chill style comment */
2299 if (c2 == '-' || c2 == '=')
2300 goto op2;
2301 if (c2 == '>')
2303 if (opts->cplusplus && PEEKN (1) == '*')
2305 /* In C++, there's a ->* operator. */
2306 token = CPP_OTHER;
2307 pfile->only_seen_white = 0;
2308 CPP_RESERVE (pfile, 4);
2309 CPP_PUTC_Q (pfile, c);
2310 CPP_PUTC_Q (pfile, GETC ());
2311 CPP_PUTC_Q (pfile, GETC ());
2312 CPP_NUL_TERMINATE_Q (pfile);
2313 return token;
2315 goto op2;
2317 goto randomchar;
2319 case '<':
2320 if (pfile->parsing_include_directive)
2322 for (;;)
2324 CPP_PUTC (pfile, c);
2325 if (c == '>')
2326 break;
2327 c = GETC ();
2328 if (c == '\n' || c == EOF)
2330 cpp_error (pfile,
2331 "missing '>' in `#include <FILENAME>'");
2332 break;
2334 else if (c == '\r')
2336 if (!CPP_BUFFER (pfile)->has_escapes)
2338 /* Backslash newline is replaced by nothing. */
2339 CPP_ADJUST_WRITTEN (pfile, -1);
2340 CPP_BUMP_LINE (pfile);
2342 else
2344 /* We might conceivably get \r- or \r<space> in
2345 here. Just delete 'em. */
2346 int d = GETC();
2347 if (d != '-' && d != ' ')
2348 cpp_fatal (pfile,
2349 "internal error: unrecognized escape \\r%c",
2351 CPP_ADJUST_WRITTEN (pfile, -1);
2355 return CPP_STRING;
2357 /* else fall through */
2358 case '>':
2359 c2 = PEEKC ();
2360 if (c2 == '=')
2361 goto op2;
2362 /* GNU C++ supports MIN and MAX operators <? and >?. */
2363 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2364 goto randomchar;
2365 FORWARD(1);
2366 CPP_RESERVE (pfile, 4);
2367 CPP_PUTC (pfile, c);
2368 CPP_PUTC (pfile, c2);
2369 c3 = PEEKC ();
2370 if (c3 == '=')
2371 CPP_PUTC_Q (pfile, GETC ());
2372 CPP_NUL_TERMINATE_Q (pfile);
2373 pfile->only_seen_white = 0;
2374 return CPP_OTHER;
2376 case '.':
2377 c2 = PEEKC ();
2378 if (ISDIGIT(c2))
2380 CPP_RESERVE(pfile, 2);
2381 CPP_PUTC_Q (pfile, '.');
2382 c = GETC ();
2383 goto number;
2386 /* In C++ there's a .* operator. */
2387 if (opts->cplusplus && c2 == '*')
2388 goto op2;
2390 if (c2 == '.' && PEEKN(1) == '.')
2392 CPP_RESERVE(pfile, 4);
2393 CPP_PUTC_Q (pfile, '.');
2394 CPP_PUTC_Q (pfile, '.');
2395 CPP_PUTC_Q (pfile, '.');
2396 FORWARD (2);
2397 CPP_NUL_TERMINATE_Q (pfile);
2398 pfile->only_seen_white = 0;
2399 return CPP_3DOTS;
2401 goto randomchar;
2403 op2:
2404 token = CPP_OTHER;
2405 pfile->only_seen_white = 0;
2406 CPP_RESERVE(pfile, 3);
2407 CPP_PUTC_Q (pfile, c);
2408 CPP_PUTC_Q (pfile, GETC ());
2409 CPP_NUL_TERMINATE_Q (pfile);
2410 return token;
2412 case 'L':
2413 c2 = PEEKC ();
2414 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2416 CPP_PUTC (pfile, c);
2417 c = GETC ();
2418 goto string;
2420 goto letter;
2422 case '0': case '1': case '2': case '3': case '4':
2423 case '5': case '6': case '7': case '8': case '9':
2424 number:
2425 c2 = '.';
2426 for (;;)
2428 CPP_RESERVE (pfile, 2);
2429 CPP_PUTC_Q (pfile, c);
2430 c = PEEKC ();
2431 if (c == EOF)
2432 break;
2433 if (!is_idchar[c] && c != '.'
2434 && ((c2 != 'e' && c2 != 'E'
2435 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2436 || (c != '+' && c != '-')))
2437 break;
2438 FORWARD(1);
2439 c2= c;
2441 CPP_NUL_TERMINATE_Q (pfile);
2442 pfile->only_seen_white = 0;
2443 return CPP_NUMBER;
2444 case 'b': case 'c': case 'd': case 'h': case 'o':
2445 case 'B': case 'C': case 'D': case 'H': case 'O':
2446 if (opts->chill && PEEKC () == '\'')
2448 pfile->only_seen_white = 0;
2449 CPP_RESERVE (pfile, 2);
2450 CPP_PUTC_Q (pfile, c);
2451 CPP_PUTC_Q (pfile, '\'');
2452 FORWARD(1);
2453 for (;;)
2455 c = GETC();
2456 if (c == EOF)
2457 goto chill_number_eof;
2458 if (!is_idchar[c])
2459 break;
2460 CPP_PUTC (pfile, c);
2462 if (c == '\'')
2464 CPP_RESERVE (pfile, 2);
2465 CPP_PUTC_Q (pfile, c);
2466 CPP_NUL_TERMINATE_Q (pfile);
2467 return CPP_STRING;
2469 else
2471 FORWARD(-1);
2472 chill_number_eof:
2473 CPP_NUL_TERMINATE (pfile);
2474 return CPP_NUMBER;
2477 else
2478 goto letter;
2479 case '_':
2480 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2481 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2482 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2483 case 'x': case 'y': case 'z':
2484 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2485 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2486 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2487 case 'Y': case 'Z':
2488 letter:
2490 HASHNODE *hp;
2491 unsigned char *ident;
2492 int before_name_written = CPP_WRITTEN (pfile);
2493 int ident_len;
2494 parse_name (pfile, c);
2495 pfile->only_seen_white = 0;
2496 if (pfile->no_macro_expand)
2497 return CPP_NAME;
2498 ident = pfile->token_buffer + before_name_written;
2499 ident_len = CPP_PWRITTEN (pfile) - ident;
2500 hp = cpp_lookup (pfile, ident, ident_len, -1);
2501 if (!hp)
2502 return CPP_NAME;
2503 if (hp->type == T_DISABLED)
2505 if (pfile->output_escapes)
2506 { /* Return "\r-IDENT", followed by '\0'. */
2507 int i;
2508 CPP_RESERVE (pfile, 3);
2509 ident = pfile->token_buffer + before_name_written;
2510 CPP_ADJUST_WRITTEN (pfile, 2);
2511 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2512 ident[0] = '\r';
2513 ident[1] = '-';
2515 return CPP_NAME;
2518 /* If macro wants an arglist, verify that a '(' follows.
2519 first skip all whitespace, copying it to the output
2520 after the macro name. Then, if there is no '(',
2521 decide this is not a macro call and leave things that way. */
2522 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2524 int is_macro_call, macbuf_whitespace = 0;
2526 parse_set_mark (pfile);
2527 for (;;)
2529 cpp_skip_hspace (pfile);
2530 c = PEEKC ();
2531 is_macro_call = c == '(';
2532 if (c != EOF)
2534 if (c != '\n')
2535 break;
2536 FORWARD (1);
2538 else
2540 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2542 if (CPP_BUFFER (pfile)->mark !=
2543 (CPP_BUFFER (pfile)->cur
2544 - CPP_BUFFER (pfile)->buf))
2545 macbuf_whitespace = 1;
2547 /* The mark goes away automatically when
2548 the buffer is popped. */
2549 cpp_pop_buffer (pfile);
2550 parse_set_mark (pfile);
2552 else
2553 break;
2556 if (!is_macro_call)
2558 parse_goto_mark (pfile);
2559 if (macbuf_whitespace)
2560 CPP_PUTC (pfile, ' ');
2562 else
2563 parse_clear_mark (pfile);
2564 if (!is_macro_call)
2565 return CPP_NAME;
2567 /* This is now known to be a macro call.
2568 Expand the macro, reading arguments as needed,
2569 and push the expansion on the input stack. */
2570 macroexpand (pfile, hp);
2571 CPP_SET_WRITTEN (pfile, before_name_written);
2573 goto get_next;
2575 case ' ': case '\t': case '\v':
2576 for (;;)
2578 CPP_PUTC (pfile, c);
2579 c = PEEKC ();
2580 if (c == EOF || !is_hor_space[c])
2581 break;
2582 FORWARD(1);
2584 return CPP_HSPACE;
2586 case '\r':
2587 if (CPP_BUFFER (pfile)->has_escapes)
2589 c = GETC ();
2590 if (c == '-')
2592 if (pfile->output_escapes)
2593 CPP_PUTS (pfile, "\r-", 2);
2594 parse_name (pfile, GETC ());
2595 return CPP_NAME;
2597 else if (c == ' ')
2599 CPP_RESERVE (pfile, 2);
2600 if (pfile->output_escapes)
2601 CPP_PUTC_Q (pfile, '\r');
2602 CPP_PUTC_Q (pfile, c);
2603 return CPP_HSPACE;
2605 else
2607 cpp_fatal (pfile,
2608 "internal error: unrecognized escape \\r%c", c);
2609 goto get_next;
2612 else
2614 /* Backslash newline is ignored. */
2615 CPP_BUMP_LINE (pfile);
2616 goto get_next;
2619 case '\n':
2620 CPP_PUTC (pfile, c);
2621 if (pfile->only_seen_white == 0)
2622 pfile->only_seen_white = 1;
2623 CPP_BUMP_LINE (pfile);
2624 if (! CPP_OPTIONS (pfile)->no_line_commands)
2626 pfile->lineno++;
2627 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2628 output_line_command (pfile, same_file);
2630 return CPP_VSPACE;
2632 case '(': token = CPP_LPAREN; goto char1;
2633 case ')': token = CPP_RPAREN; goto char1;
2634 case '{': token = CPP_LBRACE; goto char1;
2635 case '}': token = CPP_RBRACE; goto char1;
2636 case ',': token = CPP_COMMA; goto char1;
2637 case ';': token = CPP_SEMICOLON; goto char1;
2639 randomchar:
2640 default:
2641 token = CPP_OTHER;
2642 char1:
2643 pfile->only_seen_white = 0;
2644 CPP_PUTC (pfile, c);
2645 return token;
2650 /* Like cpp_get_token, but skip spaces and comments. */
2652 enum cpp_token
2653 cpp_get_non_space_token (pfile)
2654 cpp_reader *pfile;
2656 int old_written = CPP_WRITTEN (pfile);
2657 for (;;)
2659 enum cpp_token token = cpp_get_token (pfile);
2660 if (token != CPP_COMMENT && token != CPP_POP
2661 && token != CPP_HSPACE && token != CPP_VSPACE)
2662 return token;
2663 CPP_SET_WRITTEN (pfile, old_written);
2667 /* Parse an identifier starting with C. */
2669 static void
2670 parse_name (pfile, c)
2671 cpp_reader *pfile;
2672 int c;
2674 for (;;)
2676 if (! is_idchar[c])
2678 FORWARD (-1);
2679 break;
2682 if (c == '$' && CPP_PEDANTIC (pfile))
2683 cpp_pedwarn (pfile, "`$' in identifier");
2685 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2686 CPP_PUTC_Q (pfile, c);
2687 c = GETC();
2688 if (c == EOF)
2689 break;
2691 CPP_NUL_TERMINATE_Q (pfile);
2692 return;
2695 /* Parse a string starting with C. A single quoted string is treated
2696 like a double -- some programs (e.g., troff) are perverse this way.
2697 (However, a single quoted string is not allowed to extend over
2698 multiple lines.) */
2699 static void
2700 parse_string (pfile, c)
2701 cpp_reader *pfile;
2702 int c;
2704 long start_line, start_column;
2706 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2708 CPP_PUTC (pfile, c);
2709 while (1)
2711 int cc = GETC();
2712 if (cc == EOF)
2714 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2716 /* try harder: this string crosses a macro expansion
2717 boundary. This can happen naturally if -traditional.
2718 Otherwise, only -D can make a macro with an unmatched
2719 quote. */
2720 cpp_pop_buffer (pfile);
2721 continue;
2723 if (!CPP_TRADITIONAL (pfile))
2725 cpp_error_with_line (pfile, start_line, start_column,
2726 "unterminated string or character constant");
2727 if (pfile->multiline_string_line != start_line
2728 && pfile->multiline_string_line != 0)
2729 cpp_error_with_line (pfile,
2730 pfile->multiline_string_line, -1,
2731 "possible real start of unterminated constant");
2732 pfile->multiline_string_line = 0;
2734 break;
2736 CPP_PUTC (pfile, cc);
2737 switch (cc)
2739 case '\n':
2740 CPP_BUMP_LINE (pfile);
2741 pfile->lineno++;
2742 /* Traditionally, end of line ends a string constant with
2743 no error. */
2744 if (CPP_TRADITIONAL (pfile))
2745 return;
2746 /* Character constants may not extend over multiple lines. */
2747 if (c == '\'')
2749 cpp_error_with_line (pfile, start_line, start_column,
2750 "unterminated character constant");
2751 return;
2753 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2755 cpp_pedwarn_with_line (pfile, start_line, start_column,
2756 "string constant runs past end of line");
2758 if (pfile->multiline_string_line == 0)
2759 pfile->multiline_string_line = start_line;
2760 break;
2762 case '\r':
2763 CPP_ADJUST_WRITTEN (pfile, -1);
2764 if (CPP_BUFFER (pfile)->has_escapes)
2766 cpp_fatal (pfile,
2767 "internal error: \\r escape inside string constant");
2768 FORWARD(1);
2770 else
2771 /* Backslash newline is replaced by nothing at all. */
2772 CPP_BUMP_LINE (pfile);
2773 break;
2775 case '\\':
2776 cc = GETC();
2777 if (cc != EOF)
2778 CPP_PUTC (pfile, cc);
2779 break;
2781 case '\"':
2782 case '\'':
2783 if (cc == c)
2784 return;
2785 break;
2790 /* Read an assertion into the token buffer, converting to
2791 canonical form: `#predicate(a n swe r)' The next non-whitespace
2792 character to read should be the first letter of the predicate.
2793 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2794 with answer (see callers for why). In case of 0, an error has been
2795 printed. */
2796 static int
2797 parse_assertion (pfile)
2798 cpp_reader *pfile;
2800 int c, dropwhite;
2801 cpp_skip_hspace (pfile);
2802 c = PEEKC();
2803 if (! is_idstart[c])
2805 cpp_error (pfile, "assertion predicate is not an identifier");
2806 return 0;
2808 CPP_PUTC(pfile, '#');
2809 FORWARD(1);
2810 parse_name(pfile, c);
2812 c = PEEKC();
2813 if (c != '(')
2815 if (is_hor_space[c] || c == '\r')
2816 cpp_skip_hspace (pfile);
2817 c = PEEKC();
2819 if (c != '(')
2820 return 1;
2822 CPP_PUTC(pfile, '(');
2823 FORWARD(1);
2824 dropwhite = 1;
2825 while ((c = GETC()) != ')')
2827 if (is_hor_space[c])
2829 if (! dropwhite)
2831 CPP_PUTC(pfile, ' ');
2832 dropwhite = 1;
2835 else if (c == '\n' || c == EOF)
2837 if (c == '\n') FORWARD(-1);
2838 cpp_error (pfile, "un-terminated assertion answer");
2839 return 0;
2841 else if (c == '\r')
2842 /* \r cannot be a macro escape here. */
2843 CPP_BUMP_LINE (pfile);
2844 else
2846 CPP_PUTC (pfile, c);
2847 dropwhite = 0;
2851 if (pfile->limit[-1] == ' ')
2852 pfile->limit[-1] = ')';
2853 else if (pfile->limit[-1] == '(')
2855 cpp_error (pfile, "empty token sequence in assertion");
2856 return 0;
2858 else
2859 CPP_PUTC (pfile, ')');
2861 CPP_NUL_TERMINATE (pfile);
2862 return 2;
2865 static int
2866 do_assert (pfile, keyword)
2867 cpp_reader *pfile;
2868 const struct directive *keyword ATTRIBUTE_UNUSED;
2870 char *sym;
2871 int ret, c;
2872 HASHNODE *base, *this;
2873 int baselen, thislen;
2875 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2876 && !CPP_BUFFER (pfile)->system_header_p)
2877 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
2879 cpp_skip_hspace (pfile);
2880 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2881 ret = parse_assertion (pfile);
2882 if (ret == 0)
2883 goto error;
2884 else if (ret == 1)
2886 cpp_error (pfile, "missing token-sequence in `#assert'");
2887 goto error;
2890 cpp_skip_hspace (pfile);
2891 c = PEEKC();
2892 if (c != EOF && c != '\n')
2894 cpp_error (pfile, "junk at end of `#assert'");
2895 goto error;
2898 thislen = strlen (sym);
2899 baselen = index (sym, '(') - sym;
2900 this = cpp_lookup (pfile, sym, thislen, -1);
2901 if (this)
2903 cpp_warning (pfile, "`%s' re-asserted", sym);
2904 goto error;
2907 base = cpp_lookup (pfile, sym, baselen, -1);
2908 if (! base)
2909 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
2910 else if (base->type != T_ASSERT)
2912 /* Token clash - but with what?! */
2913 cpp_fatal (pfile,
2914 "cpp internal error: base->type != T_ASSERT in do_assert");
2915 goto error;
2918 this = cpp_install (pfile, sym, thislen, T_ASSERT,
2919 (char *)base->value.aschain, -1);
2920 base->value.aschain = this;
2922 pfile->limit = (unsigned char *) sym; /* Pop */
2923 return 0;
2925 error:
2926 skip_rest_of_line (pfile);
2927 pfile->limit = (unsigned char *) sym; /* Pop */
2928 return 0;
2931 static int
2932 do_unassert (pfile, keyword)
2933 cpp_reader *pfile;
2934 const struct directive *keyword ATTRIBUTE_UNUSED;
2936 int c, ret;
2937 char *sym;
2938 long baselen, thislen;
2939 HASHNODE *base, *this, *next;
2941 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
2942 && !CPP_BUFFER (pfile)->system_header_p)
2943 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
2945 cpp_skip_hspace (pfile);
2947 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
2948 ret = parse_assertion (pfile);
2949 if (ret == 0)
2950 goto error;
2952 cpp_skip_hspace (pfile);
2953 c = PEEKC ();
2954 if (c != EOF && c != '\n')
2955 cpp_error (pfile, "junk at end of `#unassert'");
2957 thislen = strlen (sym);
2958 if (ret == 1)
2960 base = cpp_lookup (pfile, sym, thislen, -1);
2961 if (! base)
2962 goto error; /* It isn't an error to #undef what isn't #defined,
2963 so it isn't an error to #unassert what isn't
2964 #asserted either. */
2966 for (this = base->value.aschain; this; this = next)
2968 next = this->value.aschain;
2969 delete_macro (this);
2971 delete_macro (base);
2973 else
2975 baselen = index (sym, '(') - sym;
2976 base = cpp_lookup (pfile, sym, baselen, -1);
2977 if (! base) goto error;
2978 this = cpp_lookup (pfile, sym, thislen, -1);
2979 if (! this) goto error;
2981 next = base;
2982 while (next->value.aschain != this)
2983 next = next->value.aschain;
2985 next->value.aschain = this->value.aschain;
2986 delete_macro (this);
2988 if (base->value.aschain == NULL)
2989 delete_macro (base); /* Last answer for this predicate deleted. */
2992 pfile->limit = (unsigned char *) sym; /* Pop */
2993 return 0;
2994 error:
2995 skip_rest_of_line (pfile);
2996 pfile->limit = (unsigned char *) sym; /* Pop */
2997 return 0;
3000 /* Process STR as if it appeared as the body of an #unassert. */
3001 void
3002 cpp_unassert (pfile, str)
3003 cpp_reader *pfile;
3004 unsigned char *str;
3006 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3008 do_assert (pfile, NULL);
3009 cpp_pop_buffer (pfile);
3014 cpp_read_check_assertion (pfile)
3015 cpp_reader *pfile;
3017 U_CHAR *name = CPP_PWRITTEN (pfile);
3018 int result;
3019 HASHNODE *hp;
3021 FORWARD (1); /* Skip '#' */
3022 cpp_skip_hspace (pfile);
3023 if (! parse_assertion (pfile))
3024 result = 0;
3025 else
3027 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
3028 result = (hp != 0);
3031 pfile->limit = name;
3032 return result;
3035 /* Remember the current position of PFILE. */
3037 void
3038 parse_set_mark (pfile)
3039 cpp_reader *pfile;
3041 cpp_buffer *ip = CPP_BUFFER (pfile);
3042 if (ip->mark != -1)
3043 cpp_fatal (pfile,
3044 "cpp internal error: ip->mark != -1 in parse_set_mark");
3046 ip->mark = ip->cur - ip->buf;
3049 /* Clear the current mark - we no longer need it. */
3051 void
3052 parse_clear_mark (pfile)
3053 cpp_reader *pfile;
3055 cpp_buffer *ip = CPP_BUFFER (pfile);
3056 if (ip->mark == -1)
3057 cpp_fatal (pfile,
3058 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3060 ip->mark = -1;
3063 /* Backup the current position of PFILE to that saved in its mark,
3064 and clear the mark. */
3066 void
3067 parse_goto_mark (pfile)
3068 cpp_reader *pfile;
3070 cpp_buffer *ip = CPP_BUFFER (pfile);
3071 if (ip->mark == -1)
3072 cpp_fatal (pfile,
3073 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3075 ip->cur = ip->buf + ip->mark;
3076 ip->mark = -1;
3079 void
3080 cpp_print_file_and_line (pfile)
3081 cpp_reader *pfile;
3083 cpp_buffer *ip = cpp_file_buffer (pfile);
3085 if (ip != NULL)
3087 long line, col;
3088 cpp_buf_line_and_col (ip, &line, &col);
3089 cpp_file_line_for_message (pfile, ip->nominal_fname,
3090 line, pfile->show_column ? col : -1);
3094 static void
3095 v_cpp_error (pfile, msgid, ap)
3096 cpp_reader *pfile;
3097 const char *msgid;
3098 va_list ap;
3100 cpp_print_containing_files (pfile);
3101 cpp_print_file_and_line (pfile);
3102 v_cpp_message (pfile, 1, msgid, ap);
3105 void
3106 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3108 #ifndef ANSI_PROTOTYPES
3109 cpp_reader *pfile;
3110 const char *msgid;
3111 #endif
3112 va_list ap;
3114 VA_START(ap, msgid);
3116 #ifndef ANSI_PROTOTYPES
3117 pfile = va_arg (ap, cpp_reader *);
3118 msgid = va_arg (ap, const char *);
3119 #endif
3121 v_cpp_error (pfile, msgid, ap);
3122 va_end(ap);
3125 /* Print error message but don't count it. */
3127 static void
3128 v_cpp_warning (pfile, msgid, ap)
3129 cpp_reader *pfile;
3130 const char *msgid;
3131 va_list ap;
3133 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3134 return;
3136 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3137 pfile->errors++;
3139 cpp_print_containing_files (pfile);
3140 cpp_print_file_and_line (pfile);
3141 v_cpp_message (pfile, 0, msgid, ap);
3144 void
3145 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3147 #ifndef ANSI_PROTOTYPES
3148 cpp_reader *pfile;
3149 const char *msgid;
3150 #endif
3151 va_list ap;
3153 VA_START (ap, msgid);
3155 #ifndef ANSI_PROTOTYPES
3156 pfile = va_arg (ap, cpp_reader *);
3157 msgid = va_arg (ap, const char *);
3158 #endif
3160 v_cpp_warning (pfile, msgid, ap);
3161 va_end(ap);
3164 /* Print an error message and maybe count it. */
3166 void
3167 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
3169 #ifndef ANSI_PROTOTYPES
3170 cpp_reader *pfile;
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 msgid = va_arg (ap, const char *);
3180 #endif
3182 if (CPP_OPTIONS (pfile)->pedantic_errors)
3183 v_cpp_error (pfile, msgid, ap);
3184 else
3185 v_cpp_warning (pfile, msgid, ap);
3186 va_end(ap);
3189 static void
3190 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3191 cpp_reader * pfile;
3192 int line;
3193 int column;
3194 const char * msgid;
3195 va_list ap;
3197 cpp_buffer *ip = cpp_file_buffer (pfile);
3199 cpp_print_containing_files (pfile);
3201 if (ip != NULL)
3202 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3204 v_cpp_message (pfile, 1, msgid, ap);
3207 void
3208 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3209 const char *msgid, ...))
3211 #ifndef ANSI_PROTOTYPES
3212 cpp_reader *pfile;
3213 int line;
3214 int column;
3215 const char *msgid;
3216 #endif
3217 va_list ap;
3219 VA_START (ap, msgid);
3221 #ifndef ANSI_PROTOTYPES
3222 pfile = va_arg (ap, cpp_reader *);
3223 line = va_arg (ap, int);
3224 column = va_arg (ap, int);
3225 msgid = va_arg (ap, const char *);
3226 #endif
3228 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3229 va_end(ap);
3232 static void
3233 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3234 cpp_reader * pfile;
3235 int line;
3236 int column;
3237 const char *msgid;
3238 va_list ap;
3240 cpp_buffer *ip;
3242 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3243 return;
3245 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3246 pfile->errors++;
3248 cpp_print_containing_files (pfile);
3250 ip = cpp_file_buffer (pfile);
3252 if (ip != NULL)
3253 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3255 v_cpp_message (pfile, 0, msgid, ap);
3258 void
3259 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3260 const char *msgid, ...))
3262 #ifndef ANSI_PROTOTYPES
3263 cpp_reader *pfile;
3264 int line;
3265 int column;
3266 const char *msgid;
3267 #endif
3268 va_list ap;
3270 VA_START (ap, msgid);
3272 #ifndef ANSI_PROTOTYPES
3273 pfile = va_arg (ap, cpp_reader *);
3274 line = va_arg (ap, int);
3275 column = va_arg (ap, int);
3276 msgid = va_arg (ap, const char *);
3277 #endif
3279 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3280 va_end(ap);
3283 void
3284 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
3285 const char *msgid, ...))
3287 #ifndef ANSI_PROTOTYPES
3288 cpp_reader *pfile;
3289 int line;
3290 int column;
3291 const char *msgid;
3292 #endif
3293 va_list ap;
3295 VA_START (ap, msgid);
3297 #ifndef ANSI_PROTOTYPES
3298 pfile = va_arg (ap, cpp_reader *);
3299 line = va_arg (ap, int);
3300 column = va_arg (ap, int);
3301 msgid = va_arg (ap, const char *);
3302 #endif
3304 if (CPP_OPTIONS (pfile)->pedantic_errors)
3305 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3306 else
3307 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3308 va_end(ap);
3311 /* Report a warning (or an error if pedantic_errors)
3312 giving specified file name and line number, not current. */
3314 void
3315 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line,
3316 const char *msgid, ...))
3318 #ifndef ANSI_PROTOTYPES
3319 cpp_reader *pfile;
3320 char *file;
3321 int line;
3322 const char *msgid;
3323 #endif
3324 va_list ap;
3326 VA_START (ap, msgid);
3328 #ifndef ANSI_PROTOTYPES
3329 pfile = va_arg (ap, cpp_reader *);
3330 file = va_arg (ap, char *);
3331 line = va_arg (ap, int);
3332 msgid = va_arg (ap, const char *);
3333 #endif
3335 if (!CPP_OPTIONS (pfile)->pedantic_errors
3336 && CPP_OPTIONS (pfile)->inhibit_warnings)
3337 return;
3338 if (file != NULL)
3339 cpp_file_line_for_message (pfile, file, line, -1);
3340 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3341 va_end(ap);
3344 /* my_strerror - return the descriptive text associated with an
3345 `errno' code. */
3347 static char *
3348 my_strerror (errnum)
3349 int errnum;
3351 char *result;
3353 #ifndef VMS
3354 #ifndef HAVE_STRERROR
3355 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3356 #else
3357 result = strerror (errnum);
3358 #endif
3359 #else /* VMS */
3360 /* VAXCRTL's strerror() takes an optional second argument, which only
3361 matters when the first argument is EVMSERR. However, it's simplest
3362 just to pass it unconditionally. `vaxc$errno' is declared in
3363 <errno.h>, and maintained by the library in parallel with `errno'.
3364 We assume that caller's `errnum' either matches the last setting of
3365 `errno' by the library or else does not have the value `EVMSERR'. */
3367 result = strerror (errnum, vaxc$errno);
3368 #endif
3370 if (!result)
3371 result = "errno = ?";
3373 return result;
3376 /* Error including a message from `errno'. */
3378 void
3379 cpp_error_from_errno (pfile, name)
3380 cpp_reader *pfile;
3381 const char *name;
3383 cpp_message_from_errno (pfile, 1, name);
3386 void
3387 cpp_message_from_errno (pfile, is_error, name)
3388 cpp_reader *pfile;
3389 int is_error;
3390 const char *name;
3392 int e = errno;
3393 cpp_buffer *ip = cpp_file_buffer (pfile);
3395 cpp_print_containing_files (pfile);
3397 if (ip != NULL)
3398 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3400 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3403 void
3404 cpp_perror_with_name (pfile, name)
3405 cpp_reader *pfile;
3406 const char *name;
3408 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3411 /* TODO:
3412 * No pre-compiled header file support.
3414 * Possibly different enum token codes for each C/C++ token.
3416 * Find and cleanup remaining uses of static variables,
3418 * Support -dM flag (dump_all_macros).
3420 * Support for_lint flag.