oops - omitted from previous delta fixing UNIQUE_SECTION
[official-gcc.git] / gcc / cpplib.c
blobcdfdaa3e29d49cf9815c0d3c96fe8f6fb87c45bf
1 /* CPP Library.
2 Copyright (C) 1986, 87, 89, 92-99, 2000 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_hspace(*p)) p++; } while (0)
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
31 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
32 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
33 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
34 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
35 (Note that it is false while we're expanding macro *arguments*.) */
36 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
38 /* Forward declarations. */
40 static const char *my_strerror PARAMS ((int));
41 static void validate_else PARAMS ((cpp_reader *, const char *));
42 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
44 static void conditional_skip PARAMS ((cpp_reader *, int,
45 enum node_type, U_CHAR *));
46 static void skip_if_group PARAMS ((cpp_reader *));
48 static void parse_name PARAMS ((cpp_reader *, int));
49 static void parse_string PARAMS ((cpp_reader *, int));
50 static int parse_assertion PARAMS ((cpp_reader *));
51 static const char *if_directive_name PARAMS ((cpp_reader *,
52 struct if_stack *));
54 /* External declarations. */
56 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
58 /* `struct directive' defines one #-directive, including how to handle it. */
60 struct directive {
61 int length; /* Length of name */
62 int (*func) /* Function to handle directive */
63 PARAMS ((cpp_reader *, const struct directive *));
64 const char *name; /* Name of directive */
65 enum node_type type; /* Code which describes which directive. */
68 /* These functions are declared to return int instead of void since they
69 are going to be placed in a table and some old compilers have trouble with
70 pointers to functions returning void. */
72 static int do_define PARAMS ((cpp_reader *, const struct directive *));
73 static int do_line PARAMS ((cpp_reader *, const struct directive *));
74 static int do_include PARAMS ((cpp_reader *, const struct directive *));
75 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
76 static int do_error PARAMS ((cpp_reader *, const struct directive *));
77 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
78 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
79 static int do_if PARAMS ((cpp_reader *, const struct directive *));
80 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
81 static int do_else PARAMS ((cpp_reader *, const struct directive *));
82 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
83 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
84 #ifdef SCCS_DIRECTIVE
85 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
86 #endif
87 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
88 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
89 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
90 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
91 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
92 static int skip_comment PARAMS ((cpp_reader *, int));
93 static int copy_comment PARAMS ((cpp_reader *, int));
94 static void copy_rest_of_line PARAMS ((cpp_reader *));
95 static int handle_directive PARAMS ((cpp_reader *));
96 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t, cpp_reader *,
97 const struct directive *));
98 static enum cpp_token get_directive_token PARAMS ((cpp_reader *));
99 static int read_line_number PARAMS ((cpp_reader *, int *));
100 static void cpp_print_file_and_line PARAMS ((cpp_reader *));
101 static void v_cpp_error PARAMS ((cpp_reader *, const char *, va_list));
102 static void v_cpp_warning PARAMS ((cpp_reader *, const char *, va_list));
103 static void v_cpp_error_with_line PARAMS ((cpp_reader *, int, int,
104 const char *, va_list));
105 static void v_cpp_warning_with_line PARAMS ((cpp_reader *, int, int, const char *, va_list));
106 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
107 static int consider_directive_while_skipping PARAMS ((cpp_reader *, IF_STACK_FRAME *));
109 /* Here is the actual list of #-directives.
110 This table is ordered by frequency of occurrence; the numbers
111 at the end are directive counts from all the source code I have
112 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
113 linux-2.2.9, and pcmcia-cs-3.0.9). */
115 static const struct directive directive_table[] = {
116 /* In C89 */
117 { 6, do_define, "define", T_DEFINE }, /* 270554 */
118 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
119 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
120 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
121 { 2, do_if, "if", T_IF }, /* 18162 */
122 { 4, do_else, "else", T_ELSE }, /* 9863 */
123 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
124 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
125 { 4, do_line, "line", T_LINE }, /* 2465 */
126 { 4, do_elif, "elif", T_ELIF }, /* 610 */
127 { 5, do_error, "error", T_ERROR }, /* 475 */
128 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
130 /* Extensions. All deprecated except #warning and #include_next. */
131 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
132 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
133 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
134 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
135 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
136 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
137 #ifdef SCCS_DIRECTIVE
138 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
139 #endif
140 { -1, 0, "", T_UNUSED }
143 /* Place into PFILE a quoted string representing the string SRC.
144 Caller must reserve enough space in pfile->token_buffer. */
146 void
147 quote_string (pfile, src)
148 cpp_reader *pfile;
149 const char *src;
151 U_CHAR c;
153 CPP_PUTC_Q (pfile, '\"');
154 for (;;)
155 switch ((c = *src++))
157 default:
158 if (ISPRINT (c))
159 CPP_PUTC_Q (pfile, c);
160 else
162 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
163 CPP_ADJUST_WRITTEN (pfile, 4);
165 break;
167 case '\"':
168 case '\\':
169 CPP_PUTC_Q (pfile, '\\');
170 CPP_PUTC_Q (pfile, c);
171 break;
173 case '\0':
174 CPP_PUTC_Q (pfile, '\"');
175 CPP_NUL_TERMINATE_Q (pfile);
176 return;
180 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
182 void
183 cpp_grow_buffer (pfile, n)
184 cpp_reader *pfile;
185 long n;
187 long old_written = CPP_WRITTEN (pfile);
188 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
189 pfile->token_buffer = (U_CHAR *)
190 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
191 CPP_SET_WRITTEN (pfile, old_written);
194 /* Process the string STR as if it appeared as the body of a #define
195 If STR is just an identifier, define it with value 1.
196 If STR has anything after the identifier, then it should
197 be identifier=definition. */
199 void
200 cpp_define (pfile, str)
201 cpp_reader *pfile;
202 U_CHAR *str;
204 U_CHAR *buf, *p;
205 size_t count;
207 /* Copy the entire option so we can modify it. */
208 count = strlen (str) + 3;
209 buf = (U_CHAR *) alloca (count);
210 memcpy (buf, str, count - 2);
211 /* Change the first "=" in the string to a space. If there is none,
212 tack " 1" on the end. */
213 p = (U_CHAR *) strchr (buf, '=');
214 if (p)
216 *p = ' ';
217 count -= 2;
219 else
220 strcpy (&buf[count-3], " 1");
222 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
224 do_define (pfile, NULL);
225 cpp_pop_buffer (pfile);
229 /* Process the string STR as if it appeared as the body of a #assert. */
230 void
231 cpp_assert (pfile, str)
232 cpp_reader *pfile;
233 U_CHAR *str;
235 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
237 do_assert (pfile, NULL);
238 cpp_pop_buffer (pfile);
243 static enum cpp_token
244 null_underflow (pfile)
245 cpp_reader *pfile ATTRIBUTE_UNUSED;
247 return CPP_EOF;
250 static int
251 null_cleanup (pbuf, pfile)
252 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
253 cpp_reader *pfile ATTRIBUTE_UNUSED;
255 return 0;
258 /* Skip a C-style block comment. We know it's a comment, and point is
259 at the second character of the starter. */
260 static void
261 skip_block_comment (pfile)
262 cpp_reader *pfile;
264 int c, prev_c = -1;
265 long line, col;
267 FORWARD(1);
268 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
269 for (;;)
271 c = GETC ();
272 if (c == EOF)
274 cpp_error_with_line (pfile, line, col, "unterminated comment");
275 return;
277 else if (c == '\n' || c == '\r')
278 /* \r cannot be a macro escape marker here. */
279 CPP_BUMP_LINE (pfile);
280 else if (c == '/' && prev_c == '*')
281 return;
282 else if (c == '*' && prev_c == '/'
283 && CPP_OPTIONS (pfile)->warn_comments)
284 cpp_warning (pfile, "`/*' within comment");
286 prev_c = c;
290 /* Skip a C++/Chill line comment. We know it's a comment, and point
291 is at the second character of the initiator. */
292 static void
293 skip_line_comment (pfile)
294 cpp_reader *pfile;
296 FORWARD(1);
297 for (;;)
299 int c = GETC ();
301 /* We don't have to worry about EOF in here. */
302 if (c == '\n')
304 /* Don't consider final '\n' to be part of comment. */
305 FORWARD(-1);
306 return;
308 else if (c == '\r')
310 /* \r cannot be a macro escape marker here. */
311 CPP_BUMP_LINE (pfile);
312 if (CPP_OPTIONS (pfile)->warn_comments)
313 cpp_warning (pfile, "backslash-newline within line comment");
318 /* Skip a comment - C, C++, or Chill style. M is the first character
319 of the comment marker. If this really is a comment, skip to its
320 end and return ' '. If this is not a comment, return M (which will
321 be '/' or '-'). */
323 static int
324 skip_comment (pfile, m)
325 cpp_reader *pfile;
326 int m;
328 if (m == '/' && PEEKC() == '*')
330 skip_block_comment (pfile);
331 return ' ';
333 else if (m == '/' && PEEKC() == '/')
335 if (CPP_BUFFER (pfile)->system_header_p)
337 /* We silently allow C++ comments in system headers, irrespective
338 of conformance mode, because lots of busted systems do that
339 and trying to clean it up in fixincludes is a nightmare. */
340 skip_line_comment (pfile);
341 return ' ';
343 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
345 if (CPP_OPTIONS (pfile)->c89
346 && CPP_PEDANTIC (pfile)
347 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
349 cpp_pedwarn (pfile,
350 "C++ style comments are not allowed in ISO C89");
351 cpp_pedwarn (pfile,
352 "(this will be reported only once per input file)");
353 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
355 skip_line_comment (pfile);
356 return ' ';
358 else
359 return m;
361 else if (m == '-' && PEEKC() == '-'
362 && CPP_OPTIONS (pfile)->chill)
364 skip_line_comment (pfile);
365 return ' ';
367 else
368 return m;
371 /* Identical to skip_comment except that it copies the comment into the
372 token_buffer. This is used if put_out_comments. */
373 static int
374 copy_comment (pfile, m)
375 cpp_reader *pfile;
376 int m;
378 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
379 U_CHAR *limit;
381 if (skip_comment (pfile, m) == m)
382 return m;
384 CPP_PUTC (pfile, m);
385 for (limit = CPP_BUFFER (pfile)->cur; start <= limit; start++)
386 if (*start != '\r')
387 CPP_PUTC (pfile, *start);
388 return ' ';
391 /* Skip whitespace \-newline and comments. Does not macro-expand. */
393 void
394 cpp_skip_hspace (pfile)
395 cpp_reader *pfile;
397 int c;
398 while (1)
400 c = GETC();
401 if (c == EOF)
402 return;
403 else if (is_hspace(c))
405 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
406 cpp_pedwarn (pfile, "%s in preprocessing directive",
407 c == '\f' ? "formfeed" : "vertical tab");
409 else if (c == '\r')
411 /* \r is a backslash-newline marker if !has_escapes, and
412 a deletable-whitespace or no-reexpansion marker otherwise. */
413 if (CPP_BUFFER (pfile)->has_escapes)
415 if (PEEKC() == ' ')
416 FORWARD(1);
417 else
418 break;
420 else
421 CPP_BUFFER (pfile)->lineno++;
423 else if (c == '/' || c == '-')
425 c = skip_comment (pfile, c);
426 if (c != ' ')
427 break;
429 else
430 break;
432 FORWARD(-1);
435 /* Read the rest of the current line.
436 The line is appended to PFILE's output buffer. */
438 static void
439 copy_rest_of_line (pfile)
440 cpp_reader *pfile;
442 for (;;)
444 int c = GETC();
445 switch (c)
447 case '\n':
448 FORWARD(-1);
449 case EOF:
450 CPP_NUL_TERMINATE (pfile);
451 return;
453 case '\r':
454 if (CPP_BUFFER (pfile)->has_escapes)
455 break;
456 else
458 CPP_BUFFER (pfile)->lineno++;
459 continue;
461 case '\'':
462 case '\"':
463 parse_string (pfile, c);
464 continue;
465 case '/':
466 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile))
468 CPP_PUTS (pfile, "/**/", 4);
469 skip_comment (pfile, c);
470 continue;
472 /* else fall through */
473 case '-':
474 c = skip_comment (pfile, c);
475 break;
477 case '\f':
478 case '\v':
479 if (CPP_PEDANTIC (pfile))
480 cpp_pedwarn (pfile, "%s in preprocessing directive",
481 c == '\f' ? "formfeed" : "vertical tab");
482 break;
485 CPP_PUTC (pfile, c);
489 /* FIXME: It is almost definitely a performance win to make this do
490 the scan itself. >75% of calls to copy_r_o_l are from here or
491 skip_if_group, which means the common case is to copy stuff into the
492 token_buffer only to discard it. */
493 void
494 skip_rest_of_line (pfile)
495 cpp_reader *pfile;
497 long old = CPP_WRITTEN (pfile);
498 copy_rest_of_line (pfile);
499 CPP_SET_WRITTEN (pfile, old);
502 /* Handle a possible # directive.
503 '#' has already been read. */
505 static int
506 handle_directive (pfile)
507 cpp_reader *pfile;
509 int c;
510 register const struct directive *kt;
511 int ident_length;
512 U_CHAR *ident;
513 long old_written = CPP_WRITTEN (pfile);
515 cpp_skip_hspace (pfile);
517 c = PEEKC ();
518 /* # followed by a number is equivalent to #line. Do not recognize
519 this form in assembly language source files. Complain about this
520 form if we're being pedantic, but not if this is regurgitated
521 input (preprocessed or fed back in by the C++ frontend). */
522 if (c >= '0' && c <= '9')
524 if (CPP_OPTIONS (pfile)->lang_asm)
526 skip_rest_of_line (pfile);
527 return 1;
530 if (CPP_PEDANTIC (pfile)
531 && ! CPP_PREPROCESSED (pfile)
532 && ! CPP_BUFFER (pfile)->manual_pop)
533 cpp_pedwarn (pfile, "`#' followed by integer");
534 do_line (pfile, NULL);
535 return 1;
538 /* If we are rescanning preprocessed input, don't obey any directives
539 other than # nnn. */
540 if (CPP_PREPROCESSED (pfile))
541 return 0;
543 /* Now find the directive name. */
544 CPP_PUTC (pfile, '#');
545 parse_name (pfile, GETC());
546 ident = pfile->token_buffer + old_written + 1;
547 ident_length = CPP_PWRITTEN (pfile) - ident;
548 if (ident_length == 0)
550 /* A line of just `#' becomes blank. */
551 if (PEEKC() == '\n')
552 return 1;
553 else
554 return 0;
558 * Decode the keyword and call the appropriate expansion
559 * routine, after moving the input pointer up to the next line.
561 for (kt = directive_table; ; kt++)
563 if (kt->length <= 0)
564 return 0;
565 if (kt->length == ident_length
566 && !strncmp (kt->name, ident, ident_length))
567 break;
570 CPP_SET_WRITTEN (pfile, old_written);
571 (*kt->func) (pfile, kt);
573 return 1;
576 /* Pass a directive through to the output file.
577 BUF points to the contents of the directive, as a contiguous string.
578 LEN is the length of the string pointed to by BUF.
579 KEYWORD is the keyword-table entry for the directive. */
581 static void
582 pass_thru_directive (buf, len, pfile, keyword)
583 const U_CHAR *buf;
584 size_t len;
585 cpp_reader *pfile;
586 const struct directive *keyword;
588 register unsigned keyword_length = keyword->length;
590 CPP_RESERVE (pfile, 1 + keyword_length + len);
591 CPP_PUTC_Q (pfile, '#');
592 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
593 if (len != 0 && buf[0] != ' ')
594 CPP_PUTC_Q (pfile, ' ');
595 CPP_PUTS_Q (pfile, buf, len);
598 /* Check a purported macro name SYMNAME, and yield its length. */
601 check_macro_name (pfile, symname)
602 cpp_reader *pfile;
603 const U_CHAR *symname;
605 const U_CHAR *p;
606 int sym_length;
608 for (p = symname; is_idchar(*p); p++)
610 sym_length = p - symname;
611 if (sym_length == 0
612 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
613 cpp_error (pfile, "invalid macro name");
614 else if (!is_idstart(*symname)
615 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
616 U_CHAR *msg; /* what pain... */
617 msg = (U_CHAR *) alloca (sym_length + 1);
618 bcopy (symname, msg, sym_length);
619 msg[sym_length] = 0;
620 cpp_error (pfile, "invalid macro name `%s'", msg);
622 return sym_length;
625 /* Process a #define command.
626 KEYWORD is the keyword-table entry for #define,
627 or NULL for a "predefined" macro,
628 or the keyword-table entry for #pragma in the case of a #pragma poison. */
630 static int
631 do_define (pfile, keyword)
632 cpp_reader *pfile;
633 const struct directive *keyword;
635 int hashcode;
636 MACRODEF mdef;
637 HASHNODE *hp;
638 long here;
639 U_CHAR *macro, *buf, *end;
640 enum node_type new_type;
642 here = CPP_WRITTEN (pfile);
643 copy_rest_of_line (pfile);
645 if (keyword == NULL || keyword->type == T_DEFINE)
646 new_type = T_MACRO;
647 else
648 new_type = T_POISON;
650 /* Copy out the line so we can pop the token buffer. */
651 buf = pfile->token_buffer + here;
652 end = CPP_PWRITTEN (pfile);
653 macro = (U_CHAR *) alloca (end - buf + 1);
654 bcopy (buf, macro, end - buf + 1);
655 end = macro + (end - buf);
657 CPP_SET_WRITTEN (pfile, here);
659 mdef = create_definition (macro, end, pfile, keyword == NULL);
660 if (mdef.defn == 0)
661 return 0;
663 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
665 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
667 int ok = 0;
668 /* Redefining a precompiled key is ok. */
669 if (hp->type == T_PCSTRING)
670 ok = 1;
671 /* Redefining a poisoned identifier is even worse than `not ok'. */
672 else if (hp->type == T_POISON)
673 ok = -1;
674 /* Redefining a macro is ok if the definitions are the same. */
675 else if (hp->type == T_MACRO)
676 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
677 /* Redefining a constant is ok with -D. */
678 else if (hp->type == T_CONST || hp->type == T_STDC)
679 ok = ! CPP_OPTIONS (pfile)->done_initializing;
680 /* Print the warning or error if it's not ok. */
681 if (ok <= 0)
683 if (hp->type == T_POISON)
684 cpp_error (pfile, "redefining poisoned `%.*s'",
685 mdef.symlen, mdef.symnam);
686 else
687 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
688 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
689 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
690 hp->value.defn->line,
691 "this is the location of the previous definition");
693 if (hp->type != T_POISON)
695 /* Replace the old definition. */
696 hp->type = new_type;
697 hp->value.defn = mdef.defn;
700 else
701 cpp_install (pfile, mdef.symnam, mdef.symlen, new_type,
702 (char *) mdef.defn, hashcode);
704 if (keyword != NULL && keyword->type == T_DEFINE)
706 if (CPP_OPTIONS (pfile)->debug_output
707 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
708 dump_definition (pfile, mdef);
709 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
710 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
713 return 0;
717 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
718 If BUFFER != NULL, then use the LENGTH characters in BUFFER
719 as the new input buffer.
720 Return the new buffer, or NULL on failure. */
722 cpp_buffer *
723 cpp_push_buffer (pfile, buffer, length)
724 cpp_reader *pfile;
725 U_CHAR *buffer;
726 long length;
728 cpp_buffer *buf = CPP_BUFFER (pfile);
729 cpp_buffer *new;
730 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
732 cpp_fatal (pfile, "macro or `#include' recursion too deep");
733 return NULL;
736 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
738 new->if_stack = pfile->if_stack;
739 new->cleanup = null_cleanup;
740 new->underflow = null_underflow;
741 new->buf = new->cur = buffer;
742 new->alimit = new->rlimit = buffer + length;
743 new->prev = buf;
744 new->mark = -1;
746 CPP_BUFFER (pfile) = new;
747 return new;
750 cpp_buffer *
751 cpp_pop_buffer (pfile)
752 cpp_reader *pfile;
754 cpp_buffer *buf = CPP_BUFFER (pfile);
755 (*buf->cleanup) (buf, pfile);
756 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
757 free (buf);
758 pfile->buffer_stack_depth--;
759 return CPP_BUFFER (pfile);
762 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
763 Pop the buffer when done. */
765 void
766 cpp_scan_buffer (pfile)
767 cpp_reader *pfile;
769 cpp_buffer *buffer = CPP_BUFFER (pfile);
770 enum cpp_token token;
771 if (CPP_OPTIONS (pfile)->no_output)
773 long old_written = CPP_WRITTEN (pfile);
774 /* In no-output mode, we can ignore everything but directives. */
775 for (;;)
777 if (! pfile->only_seen_white)
778 skip_rest_of_line (pfile);
779 token = cpp_get_token (pfile);
780 if (token == CPP_EOF) /* Should not happen ... */
781 break;
782 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
784 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
785 != CPP_NULL_BUFFER (pfile))
786 cpp_pop_buffer (pfile);
787 break;
790 CPP_SET_WRITTEN (pfile, old_written);
792 else
794 for (;;)
796 token = cpp_get_token (pfile);
797 if (token == CPP_EOF) /* Should not happen ... */
798 break;
799 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
801 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
802 != CPP_NULL_BUFFER (pfile))
803 cpp_pop_buffer (pfile);
804 break;
811 * Rescan a string (which may have escape marks) into pfile's buffer.
812 * Place the result in pfile->token_buffer.
814 * The input is copied before it is scanned, so it is safe to pass
815 * it something from the token_buffer that will get overwritten
816 * (because it follows CPP_WRITTEN). This is used by do_include.
819 void
820 cpp_expand_to_buffer (pfile, buf, length)
821 cpp_reader *pfile;
822 const U_CHAR *buf;
823 int length;
825 register cpp_buffer *ip;
826 U_CHAR *buf1;
827 int save_no_output;
829 if (length < 0)
831 cpp_fatal (pfile, "internal error: length < 0 in cpp_expand_to_buffer");
832 return;
835 /* Set up the input on the input stack. */
837 buf1 = (U_CHAR *) alloca (length + 1);
838 memcpy (buf1, buf, length);
839 buf1[length] = 0;
841 ip = cpp_push_buffer (pfile, buf1, length);
842 if (ip == NULL)
843 return;
844 ip->has_escapes = 1;
846 /* Scan the input, create the output. */
847 save_no_output = CPP_OPTIONS (pfile)->no_output;
848 CPP_OPTIONS (pfile)->no_output = 0;
849 cpp_scan_buffer (pfile);
850 CPP_OPTIONS (pfile)->no_output = save_no_output;
852 CPP_NUL_TERMINATE (pfile);
855 void
856 cpp_buf_line_and_col (pbuf, linep, colp)
857 register cpp_buffer *pbuf;
858 long *linep, *colp;
860 if (pbuf)
862 *linep = pbuf->lineno;
863 if (colp)
864 *colp = pbuf->cur - pbuf->line_base;
866 else
868 *linep = 0;
869 if (colp)
870 *colp = 0;
874 /* Return the cpp_buffer that corresponds to a file (not a macro). */
876 cpp_buffer *
877 cpp_file_buffer (pfile)
878 cpp_reader *pfile;
880 cpp_buffer *ip = CPP_BUFFER (pfile);
882 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
883 if (ip->fname != NULL)
884 return ip;
885 return NULL;
889 * write out a #line command, for instance, after an #include file.
890 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
893 void
894 output_line_command (pfile, file_change)
895 cpp_reader *pfile;
896 enum file_change_code file_change;
898 long line;
899 cpp_buffer *ip = CPP_BUFFER (pfile);
901 if (ip->fname == NULL)
902 return;
904 if (CPP_OPTIONS (pfile)->no_line_commands
905 || CPP_OPTIONS (pfile)->no_output)
906 return;
908 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
910 /* If the current file has not changed, we omit the #line if it would
911 appear to be a no-op, and we output a few newlines instead
912 if we want to increase the line number by a small amount.
913 We cannot do this if pfile->lineno is zero, because that means we
914 haven't output any line commands yet. (The very first line command
915 output is a `same_file' command.) */
916 if (file_change == same_file && pfile->lineno != 0)
918 if (line == pfile->lineno)
919 return;
921 /* If the inherited line number is a little too small,
922 output some newlines instead of a #line command. */
923 if (line > pfile->lineno && line < pfile->lineno + 8)
925 CPP_RESERVE (pfile, 20);
926 while (line > pfile->lineno)
928 CPP_PUTC_Q (pfile, '\n');
929 pfile->lineno++;
931 return;
935 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
936 CPP_PUTS_Q (pfile, "# ", 2);
938 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
939 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
941 quote_string (pfile, ip->nominal_fname);
942 if (file_change != same_file)
944 CPP_PUTC_Q (pfile, ' ');
945 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
947 /* Tell cc1 if following text comes from a system header file. */
948 if (ip->system_header_p)
950 CPP_PUTC_Q (pfile, ' ');
951 CPP_PUTC_Q (pfile, '3');
953 #ifndef NO_IMPLICIT_EXTERN_C
954 /* Tell cc1plus if following text should be treated as C. */
955 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
957 CPP_PUTC_Q (pfile, ' ');
958 CPP_PUTC_Q (pfile, '4');
960 #endif
961 CPP_PUTC_Q (pfile, '\n');
962 pfile->lineno = line;
966 /* Like cpp_get_token, except that it does not read past end-of-line.
967 Also, horizontal space is skipped, and macros are popped. */
969 static enum cpp_token
970 get_directive_token (pfile)
971 cpp_reader *pfile;
973 for (;;)
975 long old_written = CPP_WRITTEN (pfile);
976 enum cpp_token token;
977 cpp_skip_hspace (pfile);
978 if (PEEKC () == '\n')
979 return CPP_VSPACE;
980 token = cpp_get_token (pfile);
981 switch (token)
983 case CPP_POP:
984 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
985 return token;
986 /* ... else fall though ... */
987 case CPP_HSPACE: case CPP_COMMENT:
988 CPP_SET_WRITTEN (pfile, old_written);
989 break;
990 default:
991 return token;
996 /* Handle #include and #import.
997 This function expects to see "fname" or <fname> on the input.
999 The input is normally in part of the output_buffer following
1000 CPP_WRITTEN, and will get overwritten by output_line_command.
1001 I.e. in input file specification has been popped by handle_directive.
1002 This is safe. */
1004 static int
1005 do_include (pfile, keyword)
1006 cpp_reader *pfile;
1007 const struct directive *keyword;
1009 int importing = (keyword->type == T_IMPORT);
1010 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1011 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1012 int before; /* included before? */
1013 long flen;
1014 unsigned char *ftok;
1015 cpp_buffer *fp;
1017 enum cpp_token token;
1019 /* Chain of dirs to search */
1020 struct include_hash *ihash;
1021 struct file_name_list *search_start;
1023 long old_written = CPP_WRITTEN (pfile);
1025 int fd;
1027 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1029 if (importing)
1030 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1031 if (skip_dirs)
1032 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1035 if (importing && CPP_OPTIONS (pfile)->warn_import
1036 && !CPP_OPTIONS (pfile)->inhibit_warnings
1037 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1039 pfile->import_warning = 1;
1040 cpp_warning (pfile,
1041 "#import is obsolete, use an #ifndef wrapper in the header file");
1044 pfile->parsing_include_directive++;
1045 token = get_directive_token (pfile);
1046 pfile->parsing_include_directive--;
1048 if (token == CPP_STRING)
1050 if (pfile->token_buffer[old_written] == '<')
1051 angle_brackets = 1;
1053 #ifdef VMS
1054 else if (token == CPP_NAME)
1056 /* Support '#include xyz' like VAX-C. It is taken as
1057 '#include <xyz.h>' and generates a warning. */
1058 cpp_warning (pfile,
1059 "`#include filename' is obsolete, use `#include <filename.h>'");
1060 angle_brackets = 1;
1062 /* Append the missing `.h' to the name. */
1063 CPP_PUTS (pfile, ".h", 2);
1065 #endif
1066 else
1068 cpp_error (pfile,
1069 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1070 CPP_SET_WRITTEN (pfile, old_written);
1071 skip_rest_of_line (pfile);
1072 return 0;
1075 flen = CPP_WRITTEN (pfile) - old_written;
1076 ftok = (unsigned char *) alloca (flen + 1);
1077 memcpy (ftok, pfile->token_buffer + old_written, flen);
1078 ftok[flen] = '\0';
1080 if (get_directive_token (pfile) != CPP_VSPACE)
1082 cpp_error (pfile, "junk at end of `#include'");
1083 skip_rest_of_line (pfile);
1086 CPP_SET_WRITTEN (pfile, old_written);
1088 if (flen == 0)
1090 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1091 return 0;
1094 if (CPP_OPTIONS (pfile)->dump_includes)
1095 pass_thru_directive (ftok,
1096 flen
1097 #ifdef VMS
1098 - ((token == CPP_NAME) ? 2 : 0)
1099 #endif
1100 , pfile, keyword);
1102 #ifdef VMS
1103 if (token == CPP_STRING)
1104 #endif
1106 ftok++;
1107 flen -= 2;
1108 ftok[flen] = '\0';
1111 search_start = 0;
1113 for (fp = CPP_BUFFER (pfile);
1114 fp != CPP_NULL_BUFFER (pfile);
1115 fp = CPP_PREV_BUFFER (fp))
1116 if (fp->fname != NULL)
1117 break;
1119 if (fp == CPP_NULL_BUFFER (pfile))
1121 cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
1122 return 0;
1125 /* For #include_next, skip in the search path past the dir in which the
1126 containing file was found. Treat files specified using an absolute path
1127 as if there are no more directories to search. Treat the primary source
1128 file like any other included source, but generate a warning. */
1129 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1131 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1132 search_start = fp->ihash->foundhere->next;
1134 else
1136 if (skip_dirs)
1137 cpp_warning (pfile, "#include_next in primary source file");
1139 if (angle_brackets)
1140 search_start = CPP_OPTIONS (pfile)->bracket_include;
1141 else
1143 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1145 if (fp)
1146 search_start = fp->actual_dir;
1148 else
1149 search_start = CPP_OPTIONS (pfile)->quote_include;
1153 if (!search_start)
1155 cpp_error (pfile, "No include path in which to find %s", ftok);
1156 return 0;
1159 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1161 if (fd == -2)
1162 return 0;
1164 if (fd == -1)
1166 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1167 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1168 (pfile->system_include_depth > 0)))
1170 if (!angle_brackets)
1171 deps_output (pfile, ftok, ' ');
1172 else
1174 char *p;
1175 struct file_name_list *ptr;
1176 /* If requested as a system header, assume it belongs in
1177 the first system header directory. */
1178 if (CPP_OPTIONS (pfile)->bracket_include)
1179 ptr = CPP_OPTIONS (pfile)->bracket_include;
1180 else
1181 ptr = CPP_OPTIONS (pfile)->quote_include;
1183 p = (char *) alloca (strlen (ptr->name)
1184 + strlen (ftok) + 2);
1185 if (*ptr->name != '\0')
1187 strcpy (p, ptr->name);
1188 strcat (p, "/");
1190 strcat (p, ftok);
1191 deps_output (pfile, p, ' ');
1194 /* If -M was specified, and this header file won't be added to
1195 the dependency list, then don't count this as an error,
1196 because we can still produce correct output. Otherwise, we
1197 can't produce correct output, because there may be
1198 dependencies we need inside the missing file, and we don't
1199 know what directory this missing file exists in. */
1200 else if (CPP_PRINT_DEPS (pfile)
1201 && (CPP_PRINT_DEPS (pfile)
1202 <= (angle_brackets || (pfile->system_include_depth > 0))))
1203 cpp_warning (pfile, "No include path in which to find %s", ftok);
1204 else
1205 cpp_error_from_errno (pfile, ftok);
1207 return 0;
1210 /* For -M, add the file to the dependencies on its first inclusion. */
1211 if (!before && (CPP_PRINT_DEPS (pfile)
1212 > (angle_brackets || (pfile->system_include_depth > 0))))
1213 deps_output (pfile, ihash->name, ' ');
1215 /* Handle -H option. */
1216 if (CPP_OPTIONS(pfile)->print_include_names)
1218 fp = CPP_BUFFER (pfile);
1219 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1220 putc ('.', stderr);
1221 fprintf (stderr, " %s\n", ihash->name);
1224 /* Actually process the file */
1226 if (importing)
1227 ihash->control_macro = "";
1229 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1231 close (fd);
1232 return 0;
1235 if (angle_brackets)
1236 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1238 if (finclude (pfile, fd, ihash))
1240 output_line_command (pfile, enter_file);
1241 pfile->only_seen_white = 2;
1244 return 0;
1247 /* Subroutine of do_line. Read next token from PFILE without adding it to
1248 the output buffer. If it is a number between 1 and 4, store it in *NUM
1249 and return 1; otherwise, return 0 and complain if we aren't at the end
1250 of the directive. */
1252 static int
1253 read_line_number (pfile, num)
1254 cpp_reader *pfile;
1255 int *num;
1257 long save_written = CPP_WRITTEN (pfile);
1258 U_CHAR *p = pfile->token_buffer + save_written;
1259 enum cpp_token token = get_directive_token (pfile);
1260 CPP_SET_WRITTEN (pfile, save_written);
1262 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1264 *num = p[0] - '0';
1265 return 1;
1267 else
1269 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1270 cpp_error (pfile, "invalid format `#line' command");
1271 return 0;
1275 /* Interpret #line command.
1276 Note that the filename string (if any) is treated as if it were an
1277 include filename. That means no escape handling. */
1279 static int
1280 do_line (pfile, keyword)
1281 cpp_reader *pfile;
1282 const struct directive *keyword ATTRIBUTE_UNUSED;
1284 cpp_buffer *ip = CPP_BUFFER (pfile);
1285 int new_lineno;
1286 long old_written = CPP_WRITTEN (pfile);
1287 enum file_change_code file_change = same_file;
1288 enum cpp_token token;
1289 char *x;
1291 token = get_directive_token (pfile);
1293 if (token != CPP_NUMBER)
1295 cpp_error (pfile, "token after `#line' is not an integer");
1296 goto bad_line_directive;
1299 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1300 if (x[0] != '\0')
1302 cpp_error (pfile, "token after `#line' is not an integer");
1303 goto bad_line_directive;
1305 CPP_SET_WRITTEN (pfile, old_written);
1307 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1308 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1310 token = get_directive_token (pfile);
1312 if (token == CPP_STRING)
1314 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1315 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1316 int action_number = 0;
1318 if (read_line_number (pfile, &action_number))
1320 if (CPP_PEDANTIC (pfile))
1321 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1323 if (action_number == 1)
1325 file_change = enter_file;
1326 read_line_number (pfile, &action_number);
1328 else if (action_number == 2)
1330 file_change = leave_file;
1331 read_line_number (pfile, &action_number);
1333 if (action_number == 3)
1335 ip->system_header_p = 1;
1336 read_line_number (pfile, &action_number);
1338 if (action_number == 4)
1340 ip->system_header_p = 2;
1341 read_line_number (pfile, &action_number);
1345 *end_name = '\0';
1347 if (strcmp (fname, ip->nominal_fname))
1349 const char *newname, *oldname;
1350 if (!strcmp (fname, ip->fname))
1351 newname = ip->fname;
1352 else if (ip->last_nominal_fname
1353 && !strcmp (fname, ip->last_nominal_fname))
1354 newname = ip->last_nominal_fname;
1355 else
1356 newname = xstrdup (fname);
1358 oldname = ip->nominal_fname;
1359 ip->nominal_fname = newname;
1361 if (ip->last_nominal_fname
1362 && ip->last_nominal_fname != oldname
1363 && ip->last_nominal_fname != newname
1364 && ip->last_nominal_fname != ip->fname)
1365 free ((void *) ip->last_nominal_fname);
1367 if (newname == ip->fname)
1368 ip->last_nominal_fname = NULL;
1369 else
1370 ip->last_nominal_fname = oldname;
1373 else if (token != CPP_VSPACE && token != CPP_EOF)
1375 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1376 goto bad_line_directive;
1379 /* The Newline at the end of this line remains to be processed.
1380 To put the next line at the specified line number,
1381 we must store a line number now that is one less. */
1382 ip->lineno = new_lineno - 1;
1383 CPP_SET_WRITTEN (pfile, old_written);
1384 output_line_command (pfile, file_change);
1385 return 0;
1387 bad_line_directive:
1388 skip_rest_of_line (pfile);
1389 CPP_SET_WRITTEN (pfile, old_written);
1390 return 0;
1393 /* Remove the definition of a symbol from the symbol table.
1394 According to the C standard, it is not an error to undef
1395 something that has no definitions. */
1396 static int
1397 do_undef (pfile, keyword)
1398 cpp_reader *pfile;
1399 const struct directive *keyword;
1401 int sym_length;
1402 HASHNODE *hp;
1403 U_CHAR *buf, *name, *limit;
1404 int c;
1405 long here = CPP_WRITTEN (pfile);
1406 enum cpp_token token;
1408 cpp_skip_hspace (pfile);
1409 c = GETC();
1410 if (! is_idstart(c))
1412 cpp_error (pfile, "token after #undef is not an identifier");
1413 skip_rest_of_line (pfile);
1414 return 1;
1417 parse_name (pfile, c);
1418 buf = pfile->token_buffer + here;
1419 limit = CPP_PWRITTEN(pfile);
1421 /* Copy out the token so we can pop the token buffer. */
1422 name = (U_CHAR *) alloca (limit - buf + 1);
1423 bcopy(buf, name, limit - buf);
1424 name[limit - buf] = '\0';
1426 token = get_directive_token (pfile);
1427 if (token != CPP_VSPACE && token != CPP_POP)
1429 cpp_pedwarn (pfile, "junk on line after #undef");
1430 skip_rest_of_line (pfile);
1433 CPP_SET_WRITTEN (pfile, here);
1435 sym_length = check_macro_name (pfile, buf);
1437 while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
1439 /* If we are generating additional info for debugging (with -g) we
1440 need to pass through all effective #undef commands. */
1441 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1442 pass_thru_directive (name, sym_length, pfile, keyword);
1443 if (hp->type == T_POISON)
1444 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1445 else
1447 if (hp->type != T_MACRO)
1448 cpp_warning (pfile, "undefining `%s'", hp->name);
1449 delete_macro (hp);
1453 return 0;
1456 /* Wrap do_undef for -U processing. */
1457 void
1458 cpp_undef (pfile, macro)
1459 cpp_reader *pfile;
1460 U_CHAR *macro;
1462 if (cpp_push_buffer (pfile, macro, strlen (macro)))
1464 do_undef (pfile, NULL);
1465 cpp_pop_buffer (pfile);
1471 * Report an error detected by the program we are processing.
1472 * Use the text of the line in the error message.
1473 * (We use error because it prints the filename & line#.)
1476 static int
1477 do_error (pfile, keyword)
1478 cpp_reader *pfile;
1479 const struct directive *keyword ATTRIBUTE_UNUSED;
1481 long here = CPP_WRITTEN (pfile);
1482 U_CHAR *text;
1483 copy_rest_of_line (pfile);
1484 text = pfile->token_buffer + here;
1485 SKIP_WHITE_SPACE(text);
1487 cpp_error (pfile, "#error %s", text);
1488 CPP_SET_WRITTEN (pfile, here);
1489 return 0;
1493 * Report a warning detected by the program we are processing.
1494 * Use the text of the line in the warning message, then continue.
1497 static int
1498 do_warning (pfile, keyword)
1499 cpp_reader *pfile;
1500 const struct directive *keyword ATTRIBUTE_UNUSED;
1502 U_CHAR *text;
1503 long here = CPP_WRITTEN(pfile);
1504 copy_rest_of_line (pfile);
1505 text = pfile->token_buffer + here;
1506 SKIP_WHITE_SPACE(text);
1508 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1509 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1511 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1512 if -pedantic-errors is given, #warning should cause an error. */
1513 cpp_pedwarn (pfile, "#warning %s", text);
1514 CPP_SET_WRITTEN (pfile, here);
1515 return 0;
1518 /* Report program identification.
1519 This is not precisely what cccp does with #ident, however I believe
1520 it matches `closely enough' (behavior is identical as long as there
1521 are no macros on the #ident line, which is pathological in my opinion). */
1523 static int
1524 do_ident (pfile, keyword)
1525 cpp_reader *pfile;
1526 const struct directive *keyword ATTRIBUTE_UNUSED;
1528 /* Allow #ident in system headers, since that's not user's fault. */
1529 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1530 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1532 CPP_PUTS (pfile, "#ident ", 7);
1533 cpp_skip_hspace (pfile);
1534 copy_rest_of_line (pfile);
1536 return 0;
1539 /* Just check for some recognized pragmas that need validation here,
1540 and leave the text in the token buffer to be output. */
1542 static int
1543 do_pragma (pfile, keyword)
1544 cpp_reader *pfile;
1545 const struct directive *keyword ATTRIBUTE_UNUSED;
1547 long here;
1548 U_CHAR *buf;
1550 CPP_PUTS (pfile, "#pragma ", 8);
1551 cpp_skip_hspace (pfile);
1553 here = CPP_WRITTEN (pfile);
1554 copy_rest_of_line (pfile);
1555 buf = pfile->token_buffer + here;
1557 if (!strncmp (buf, "once", 4))
1559 cpp_buffer *ip = NULL;
1561 /* Allow #pragma once in system headers, since that's not the user's
1562 fault. */
1563 if (!CPP_BUFFER (pfile)->system_header_p)
1564 cpp_warning (pfile, "`#pragma once' is obsolete");
1566 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
1568 if (ip == CPP_NULL_BUFFER (pfile))
1569 return 0;
1570 if (ip->fname != NULL)
1571 break;
1574 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1575 cpp_warning (pfile, "`#pragma once' outside include file");
1576 else
1577 ip->ihash->control_macro = ""; /* never repeat */
1579 else if (!strncmp (buf, "implementation", 14))
1581 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1582 been included yet. */
1583 struct include_hash *ptr;
1584 U_CHAR *p = buf + 14, *fname, *fcopy;
1585 SKIP_WHITE_SPACE (p);
1586 if (*p == '\n' || *p != '\"')
1587 return 0;
1589 fname = p + 1;
1590 p = (U_CHAR *) index (fname, '\"');
1592 fcopy = (U_CHAR *) alloca (p - fname + 1);
1593 bcopy (fname, fcopy, p - fname);
1594 fcopy[p-fname] = '\0';
1596 ptr = include_hash (pfile, fcopy, 0);
1597 if (ptr)
1598 cpp_warning (pfile,
1599 "`#pragma implementation' for `%s' appears after file is included",
1600 fcopy);
1602 else if (!strncmp (buf, "poison", 6))
1604 /* Poison these symbols so that all subsequent usage produces an
1605 error message. */
1606 U_CHAR *p = buf + 6;
1607 size_t plen;
1608 U_CHAR *syms;
1609 int writeit;
1611 SKIP_WHITE_SPACE (p);
1612 plen = strlen(p) + 1;
1614 syms = (U_CHAR *) alloca (plen);
1615 memcpy (syms, p, plen);
1617 /* As a rule, don't include #pragma poison commands in output,
1618 unless the user asks for them. */
1619 writeit = (CPP_OPTIONS (pfile)->debug_output
1620 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1621 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1623 if (writeit)
1624 CPP_SET_WRITTEN (pfile, here);
1625 else
1626 CPP_SET_WRITTEN (pfile, here-8);
1628 if (writeit)
1630 CPP_RESERVE (pfile, plen + 7);
1631 CPP_PUTS_Q (pfile, "poison", 7);
1634 while (*syms != '\0')
1636 U_CHAR *end = syms;
1638 while (is_idchar(*end))
1639 end++;
1641 if (!is_hspace(*end) && *end != '\0')
1643 cpp_error (pfile, "invalid #pragma poison directive");
1644 return 1;
1647 if (cpp_push_buffer (pfile, syms, end - syms) != NULL)
1649 do_define (pfile, keyword);
1650 cpp_pop_buffer (pfile);
1652 if (writeit)
1654 CPP_PUTC_Q (pfile, ' ');
1655 CPP_PUTS_Q (pfile, syms, end - syms);
1657 syms = end;
1658 SKIP_WHITE_SPACE (syms);
1662 return 0;
1665 #ifdef SCCS_DIRECTIVE
1666 /* Just ignore #sccs, on systems where we define it at all. */
1668 static int
1669 do_sccs (pfile, keyword)
1670 cpp_reader *pfile;
1671 const struct directive *keyword ATTRIBUTE_UNUSED;
1673 if (CPP_PEDANTIC (pfile))
1674 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1675 skip_rest_of_line (pfile);
1676 return 0;
1678 #endif
1681 /* We've found an `#if' directive. If the only thing before it in
1682 this file is white space, and if it is of the form
1683 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1684 for inclusion of this file. (See redundant_include_p in cppfiles.c
1685 for an explanation of controlling macros.) If so, return a
1686 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1688 static U_CHAR *
1689 detect_if_not_defined (pfile)
1690 cpp_reader *pfile;
1692 U_CHAR *control_macro = 0;
1694 if (pfile->only_seen_white == 2)
1696 char *ident;
1697 enum cpp_token token;
1698 int base_offset;
1699 int token_offset;
1700 int need_rparen = 0;
1702 /* Save state required for restore. */
1703 pfile->no_macro_expand++;
1704 parse_set_mark (pfile);
1705 base_offset = CPP_WRITTEN (pfile);
1707 /* Look for `!', */
1708 if (get_directive_token (pfile) != CPP_OTHER
1709 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1710 || CPP_PWRITTEN (pfile)[-1] != '!')
1711 goto restore;
1713 /* ...then `defined', */
1714 token_offset = CPP_WRITTEN (pfile);
1715 token = get_directive_token (pfile);
1716 if (token != CPP_NAME)
1717 goto restore;
1718 ident = pfile->token_buffer + token_offset;
1719 CPP_NUL_TERMINATE (pfile);
1720 if (strcmp (ident, "defined"))
1721 goto restore;
1723 /* ...then an optional '(' and the name, */
1724 token_offset = CPP_WRITTEN (pfile);
1725 token = get_directive_token (pfile);
1726 if (token == CPP_LPAREN)
1728 token_offset = CPP_WRITTEN (pfile);
1729 token = get_directive_token (pfile);
1730 if (token != CPP_NAME)
1731 goto restore;
1732 need_rparen = 1;
1734 else if (token != CPP_NAME)
1735 goto restore;
1737 ident = pfile->token_buffer + token_offset;
1738 CPP_NUL_TERMINATE (pfile);
1740 /* ...then the ')', if necessary, */
1741 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1742 /* ...and make sure there's nothing else on the line. */
1743 && get_directive_token (pfile) == CPP_VSPACE)
1744 control_macro = xstrdup (ident);
1746 restore:
1747 CPP_SET_WRITTEN (pfile, base_offset);
1748 pfile->no_macro_expand--;
1749 parse_goto_mark (pfile);
1752 return control_macro;
1756 * handle #if command by
1757 * 1) inserting special `defined' keyword into the hash table
1758 * that gets turned into 0 or 1 by special_symbol (thus,
1759 * if the luser has a symbol called `defined' already, it won't
1760 * work inside the #if command)
1761 * 2) rescan the input into a temporary output buffer
1762 * 3) pass the output buffer to the yacc parser and collect a value
1763 * 4) clean up the mess left from steps 1 and 2.
1764 * 5) call conditional_skip to skip til the next #endif (etc.),
1765 * or not, depending on the value from step 3.
1768 static int
1769 do_if (pfile, keyword)
1770 cpp_reader *pfile;
1771 const struct directive *keyword ATTRIBUTE_UNUSED;
1773 U_CHAR *control_macro = detect_if_not_defined (pfile);
1774 HOST_WIDEST_INT value = eval_if_expression (pfile);
1775 conditional_skip (pfile, value == 0, T_IF, control_macro);
1776 return 0;
1780 * handle a #elif directive by not changing if_stack either.
1781 * see the comment above do_else.
1784 static int
1785 do_elif (pfile, keyword)
1786 cpp_reader *pfile;
1787 const struct directive *keyword ATTRIBUTE_UNUSED;
1789 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
1790 cpp_error (pfile, "`#elif' not within a conditional");
1791 return 0;
1792 } else {
1793 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
1794 cpp_error (pfile, "`#elif' after `#else'");
1795 #if 0
1796 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
1797 #endif
1798 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
1799 && strcmp (pfile->if_stack->fname,
1800 CPP_BUFFER (pfile)->nominal_fname) != 0)
1801 fprintf (stderr, ", file %s", pfile->if_stack->fname);
1802 fprintf (stderr, ")\n");
1804 pfile->if_stack->type = T_ELIF;
1807 if (pfile->if_stack->if_succeeded)
1808 skip_if_group (pfile);
1809 else {
1810 HOST_WIDEST_INT value = eval_if_expression (pfile);
1811 if (value == 0)
1812 skip_if_group (pfile);
1813 else {
1814 ++pfile->if_stack->if_succeeded; /* continue processing input */
1815 output_line_command (pfile, same_file);
1818 return 0;
1822 * evaluate a #if expression in BUF, of length LENGTH,
1823 * then parse the result as a C expression and return the value as an int.
1826 static HOST_WIDEST_INT
1827 eval_if_expression (pfile)
1828 cpp_reader *pfile;
1830 HOST_WIDEST_INT value;
1831 long old_written = CPP_WRITTEN (pfile);
1833 pfile->pcp_inside_if = 1;
1834 value = cpp_parse_expr (pfile);
1835 pfile->pcp_inside_if = 0;
1837 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1839 return value;
1843 * routine to handle ifdef/ifndef. Try to look up the symbol,
1844 * then do or don't skip to the #endif/#else/#elif depending
1845 * on what directive is actually being processed.
1848 static int
1849 do_xifdef (pfile, keyword)
1850 cpp_reader *pfile;
1851 const struct directive *keyword;
1853 int skip;
1854 cpp_buffer *ip = CPP_BUFFER (pfile);
1855 U_CHAR *ident;
1856 int ident_length;
1857 enum cpp_token token;
1858 int start_of_file = 0;
1859 U_CHAR *control_macro = 0;
1860 int old_written = CPP_WRITTEN (pfile);
1862 /* Detect a #ifndef at start of file (not counting comments). */
1863 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1864 start_of_file = pfile->only_seen_white == 2;
1866 pfile->no_macro_expand++;
1867 token = get_directive_token (pfile);
1868 pfile->no_macro_expand--;
1870 ident = pfile->token_buffer + old_written;
1871 ident_length = CPP_WRITTEN (pfile) - old_written;
1872 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1874 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1876 skip = (keyword->type == T_IFDEF);
1877 if (! CPP_TRADITIONAL (pfile))
1878 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1880 else if (token == CPP_NAME)
1882 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
1883 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
1884 if (start_of_file && !skip)
1886 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1887 bcopy (ident, control_macro, ident_length + 1);
1889 if (hp != NULL && hp->type == T_POISON)
1891 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
1892 skip = !skip;
1895 else
1897 skip = (keyword->type == T_IFDEF);
1898 if (! CPP_TRADITIONAL (pfile))
1899 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1902 if (!CPP_TRADITIONAL (pfile))
1903 { int c;
1904 cpp_skip_hspace (pfile);
1905 c = PEEKC ();
1906 if (c != EOF && c != '\n')
1907 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1909 skip_rest_of_line (pfile);
1911 #if 0
1912 if (pcp_outfile) {
1913 /* Output a precondition for this macro. */
1914 if (hp && hp->value.defn->predefined)
1915 fprintf (pcp_outfile, "#define %s\n", hp->name);
1916 else {
1917 U_CHAR *cp = buf;
1918 fprintf (pcp_outfile, "#undef ");
1919 while (is_idchar(*cp)) /* Ick! */
1920 fputc (*cp++, pcp_outfile);
1921 putc ('\n', pcp_outfile);
1923 #endif
1925 conditional_skip (pfile, skip, T_IF, control_macro);
1926 return 0;
1929 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1930 If this is a #ifndef starting at the beginning of a file,
1931 CONTROL_MACRO is the macro name tested by the #ifndef.
1932 Otherwise, CONTROL_MACRO is 0. */
1934 static void
1935 conditional_skip (pfile, skip, type, control_macro)
1936 cpp_reader *pfile;
1937 int skip;
1938 enum node_type type;
1939 U_CHAR *control_macro;
1941 IF_STACK_FRAME *temp;
1943 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
1944 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
1945 temp->lineno = CPP_BUFFER (pfile)->lineno;
1946 temp->next = pfile->if_stack;
1947 temp->control_macro = control_macro;
1948 pfile->if_stack = temp;
1950 pfile->if_stack->type = type;
1952 if (skip != 0) {
1953 skip_if_group (pfile);
1954 return;
1955 } else {
1956 ++pfile->if_stack->if_succeeded;
1957 output_line_command (pfile, same_file);
1961 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1962 return 0 if skipping should continue, 1 if it should halt. Also
1963 adjusts the if_stack as appropriate.
1964 The `#' has been read, but not the identifier. */
1966 static int
1967 consider_directive_while_skipping (pfile, stack)
1968 cpp_reader *pfile;
1969 IF_STACK_FRAME *stack;
1971 long ident_len, ident;
1972 const struct directive *kt;
1973 IF_STACK_FRAME *temp;
1975 cpp_skip_hspace (pfile);
1977 ident = CPP_WRITTEN (pfile);
1978 parse_name (pfile, GETC());
1979 ident_len = CPP_WRITTEN (pfile) - ident;
1981 CPP_SET_WRITTEN (pfile, ident);
1983 for (kt = directive_table; kt->length >= 0; kt++)
1984 if (kt->length == ident_len
1985 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
1986 switch (kt->type)
1988 case T_IF:
1989 case T_IFDEF:
1990 case T_IFNDEF:
1991 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
1992 temp->next = pfile->if_stack;
1993 pfile->if_stack = temp;
1994 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
1995 temp->type = kt->type;
1996 return 0;
1998 case T_ELSE:
1999 if (pfile->if_stack != stack)
2000 validate_else (pfile, "#else");
2001 /* fall through */
2002 case T_ELIF:
2003 if (pfile->if_stack->type == T_ELSE)
2004 cpp_error (pfile, "`%s' after `#else'", kt->name);
2006 if (pfile->if_stack == stack)
2007 return 1;
2008 else
2010 pfile->if_stack->type = kt->type;
2011 return 0;
2014 case T_ENDIF:
2015 if (pfile->if_stack != stack)
2016 validate_else (pfile, "#endif");
2018 if (pfile->if_stack == stack)
2019 return 1;
2021 temp = pfile->if_stack;
2022 pfile->if_stack = temp->next;
2023 free (temp);
2024 return 0;
2026 default:
2027 return 0;
2030 /* Don't let erroneous code go by. */
2031 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2032 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2033 return 0;
2036 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2037 * leaves input ptr at the sharp sign found.
2039 static void
2040 skip_if_group (pfile)
2041 cpp_reader *pfile;
2043 int c;
2044 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2045 U_CHAR *beg_of_line;
2046 long old_written;
2048 if (CPP_OPTIONS (pfile)->output_conditionals)
2050 CPP_PUTS (pfile, "#failed\n", 8);
2051 pfile->lineno++;
2052 output_line_command (pfile, same_file);
2055 old_written = CPP_WRITTEN (pfile);
2057 for (;;)
2059 beg_of_line = CPP_BUFFER (pfile)->cur;
2061 if (! CPP_TRADITIONAL (pfile))
2062 cpp_skip_hspace (pfile);
2063 c = GETC();
2064 if (c == '\n')
2066 if (CPP_OPTIONS (pfile)->output_conditionals)
2067 CPP_PUTC (pfile, c);
2068 CPP_BUMP_LINE (pfile);
2069 continue;
2071 else if (c == '#')
2073 if (consider_directive_while_skipping (pfile, save_if_stack))
2074 break;
2076 else if (c == EOF)
2077 return; /* Caller will issue error. */
2079 FORWARD(-1);
2080 if (CPP_OPTIONS (pfile)->output_conditionals)
2082 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2083 copy_rest_of_line (pfile);
2085 else
2087 copy_rest_of_line (pfile);
2088 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2091 c = GETC();
2092 if (c == EOF)
2093 return; /* Caller will issue error. */
2094 else
2096 /* \n */
2097 if (CPP_OPTIONS (pfile)->output_conditionals)
2099 CPP_PUTC (pfile, c);
2100 pfile->lineno++;
2102 CPP_BUMP_LINE (pfile);
2106 /* Back up to the beginning of this line. Caller will process the
2107 directive. */
2108 CPP_BUFFER (pfile)->cur = beg_of_line;
2109 pfile->only_seen_white = 1;
2110 if (CPP_OPTIONS (pfile)->output_conditionals)
2112 CPP_PUTS (pfile, "#endfailed\n", 11);
2113 pfile->lineno++;
2118 * handle a #else directive. Do this by just continuing processing
2119 * without changing if_stack ; this is so that the error message
2120 * for missing #endif's etc. will point to the original #if. It
2121 * is possible that something different would be better.
2124 static int
2125 do_else (pfile, keyword)
2126 cpp_reader *pfile;
2127 const struct directive *keyword ATTRIBUTE_UNUSED;
2129 cpp_buffer *ip = CPP_BUFFER (pfile);
2131 validate_else (pfile, "#else");
2132 skip_rest_of_line (pfile);
2134 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
2135 cpp_error (pfile, "`#else' not within a conditional");
2136 return 0;
2137 } else {
2138 /* #ifndef can't have its special treatment for containing the whole file
2139 if it has a #else clause. */
2140 pfile->if_stack->control_macro = 0;
2142 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
2143 cpp_error (pfile, "`#else' after `#else'");
2144 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
2145 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
2146 fprintf (stderr, ", file %s", pfile->if_stack->fname);
2147 fprintf (stderr, ")\n");
2149 pfile->if_stack->type = T_ELSE;
2152 if (pfile->if_stack->if_succeeded)
2153 skip_if_group (pfile);
2154 else {
2155 ++pfile->if_stack->if_succeeded; /* continue processing input */
2156 output_line_command (pfile, same_file);
2158 return 0;
2162 * unstack after #endif command
2165 static int
2166 do_endif (pfile, keyword)
2167 cpp_reader *pfile;
2168 const struct directive *keyword ATTRIBUTE_UNUSED;
2170 validate_else (pfile, "#endif");
2171 skip_rest_of_line (pfile);
2173 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2174 cpp_error (pfile, "`#endif' not within a conditional");
2175 else
2177 IF_STACK_FRAME *temp = pfile->if_stack;
2178 pfile->if_stack = temp->next;
2179 if (temp->control_macro != 0)
2181 /* This #endif matched a #ifndef at the start of the file.
2182 See if it is at the end of the file. */
2183 int c;
2185 parse_set_mark (pfile);
2187 for (;;)
2189 cpp_skip_hspace (pfile);
2190 c = GETC ();
2191 if (c != '\n')
2192 break;
2194 parse_goto_mark (pfile);
2196 if (c == EOF)
2198 /* This #endif ends a #ifndef
2199 that contains all of the file (aside from whitespace).
2200 Arrange not to include the file again
2201 if the macro that was tested is defined. */
2202 struct cpp_buffer *ip;
2203 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2204 if (ip->fname != NULL)
2205 break;
2206 ip->ihash->control_macro = (char *) temp->control_macro;
2209 free (temp);
2210 output_line_command (pfile, same_file);
2212 return 0;
2215 /* Issue -pedantic warning for text which is not a comment following
2216 an #else or #endif. Do not warn in system headers, as this is harmless
2217 and very common on old systems. */
2219 static void
2220 validate_else (pfile, directive)
2221 cpp_reader *pfile;
2222 const char *directive;
2224 if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2225 return;
2227 cpp_skip_hspace (pfile);
2228 if (PEEKC () != '\n')
2229 cpp_pedwarn (pfile,
2230 "text following `%s' violates ANSI standard", directive);
2233 /* Convert T_IF, etc. to a string. Used in error messages. */
2234 static const char *
2235 if_directive_name (pfile, ifs)
2236 cpp_reader *pfile;
2237 struct if_stack *ifs;
2239 switch (ifs->type)
2241 case T_IF: return "#if";
2242 case T_IFDEF: return "#ifdef";
2243 case T_IFNDEF: return "#ifndef";
2244 case T_ELIF: return "#elif";
2245 case T_ELSE: return "#else";
2246 default:
2247 cpp_fatal (pfile, "impossible if_stack->type value %d", ifs->type);
2248 return "unknown";
2252 /* Get the next token, and add it to the text in pfile->token_buffer.
2253 Return the kind of token we got. */
2255 enum cpp_token
2256 cpp_get_token (pfile)
2257 cpp_reader *pfile;
2259 register int c, c2, c3;
2260 enum cpp_token token;
2261 struct cpp_options *opts = CPP_OPTIONS (pfile);
2263 get_next:
2264 c = GETC();
2265 if (c == EOF)
2267 if (CPP_BUFFER (pfile)->manual_pop)
2268 /* If we've been reading from redirected input, the
2269 frontend will pop the buffer. */
2270 return CPP_EOF;
2271 else if (CPP_BUFFER (pfile)->seen_eof)
2273 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2274 return CPP_EOF;
2276 cpp_pop_buffer (pfile);
2277 goto get_next;
2279 else
2281 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2282 struct if_stack *ifs, *nifs;
2284 /* Unwind the conditional stack and generate error messages. */
2285 for (ifs = pfile->if_stack;
2286 ifs != CPP_BUFFER (pfile)->if_stack;
2287 ifs = nifs)
2289 cpp_error_with_line (pfile, ifs->lineno, -1,
2290 "unterminated `%s' conditional",
2291 if_directive_name (pfile, ifs));
2293 nifs = ifs->next;
2294 free (ifs);
2296 pfile->if_stack = ifs;
2298 if (CPP_BUFFER (pfile)->nominal_fname
2299 && next_buf != CPP_NULL_BUFFER (pfile))
2301 /* We're about to return from an #include file.
2302 Emit #line information now (as part of the CPP_POP) result.
2303 But the #line refers to the file we will pop to. */
2304 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2305 CPP_BUFFER (pfile) = next_buf;
2306 pfile->input_stack_listing_current = 0;
2307 output_line_command (pfile, leave_file);
2308 CPP_BUFFER (pfile) = cur_buffer;
2311 CPP_BUFFER (pfile)->seen_eof = 1;
2312 return CPP_POP;
2315 else
2317 switch (c)
2319 case '/':
2320 if (PEEKC () == '=')
2321 goto op2;
2323 comment:
2324 if (opts->put_out_comments)
2325 c = copy_comment (pfile, c);
2326 else
2327 c = skip_comment (pfile, c);
2328 if (c != ' ')
2329 goto randomchar;
2331 /* Comments are equivalent to spaces.
2332 For -traditional, a comment is equivalent to nothing. */
2333 if (opts->traditional || opts->put_out_comments)
2334 return CPP_COMMENT;
2335 else
2337 CPP_PUTC (pfile, c);
2338 return CPP_HSPACE;
2340 #if 0
2341 if (opts->for_lint) {
2342 U_CHAR *argbp;
2343 int cmdlen, arglen;
2344 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
2346 if (lintcmd != NULL) {
2347 /* I believe it is always safe to emit this newline: */
2348 obp[-1] = '\n';
2349 bcopy ("#pragma lint ", (char *) obp, 13);
2350 obp += 13;
2351 bcopy (lintcmd, (char *) obp, cmdlen);
2352 obp += cmdlen;
2354 if (arglen != 0) {
2355 *(obp++) = ' ';
2356 bcopy (argbp, (char *) obp, arglen);
2357 obp += arglen;
2360 /* OK, now bring us back to the state we were in before we entered
2361 this branch. We need #line because the newline for the pragma
2362 could mess things up. */
2363 output_line_command (pfile, same_file);
2364 *(obp++) = ' '; /* just in case, if comments are copied thru */
2365 *(obp++) = '/';
2368 #endif
2370 case '#':
2371 #if 0
2372 /* If this is expanding a macro definition, don't recognize
2373 preprocessor directives. */
2374 if (ip->macro != 0)
2375 goto randomchar;
2376 /* If this is expand_into_temp_buffer, recognize them
2377 only after an actual newline at this level,
2378 not at the beginning of the input level. */
2379 if (ip->fname == 0 && beg_of_line == ip->buf)
2380 goto randomchar;
2381 if (ident_length)
2382 goto specialchar;
2383 #endif
2385 if (!pfile->only_seen_white)
2386 goto randomchar;
2387 /* -traditional directives are recognized only with the # in
2388 column 1.
2389 XXX Layering violation. */
2390 if (CPP_TRADITIONAL (pfile)
2391 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2392 goto randomchar;
2393 if (handle_directive (pfile))
2394 return CPP_DIRECTIVE;
2395 pfile->only_seen_white = 0;
2396 return CPP_OTHER;
2398 case '\"':
2399 case '\'':
2400 string:
2401 parse_string (pfile, c);
2402 pfile->only_seen_white = 0;
2403 return c == '\'' ? CPP_CHAR : CPP_STRING;
2405 case '$':
2406 if (!opts->dollars_in_ident)
2407 goto randomchar;
2408 goto letter;
2410 case ':':
2411 if (opts->cplusplus && PEEKC () == ':')
2412 goto op2;
2413 goto randomchar;
2415 case '&':
2416 case '+':
2417 case '|':
2418 c2 = PEEKC ();
2419 if (c2 == c || c2 == '=')
2420 goto op2;
2421 goto randomchar;
2423 case '*':
2424 case '!':
2425 case '%':
2426 case '=':
2427 case '^':
2428 if (PEEKC () == '=')
2429 goto op2;
2430 goto randomchar;
2432 case '-':
2433 c2 = PEEKC ();
2434 if (c2 == '-' && opts->chill)
2435 goto comment; /* Chill style comment */
2436 if (c2 == '-' || c2 == '=')
2437 goto op2;
2438 if (c2 == '>')
2440 if (opts->cplusplus && PEEKN (1) == '*')
2442 /* In C++, there's a ->* operator. */
2443 token = CPP_OTHER;
2444 pfile->only_seen_white = 0;
2445 CPP_RESERVE (pfile, 4);
2446 CPP_PUTC_Q (pfile, c);
2447 CPP_PUTC_Q (pfile, GETC ());
2448 CPP_PUTC_Q (pfile, GETC ());
2449 CPP_NUL_TERMINATE_Q (pfile);
2450 return token;
2452 goto op2;
2454 goto randomchar;
2456 case '<':
2457 if (pfile->parsing_include_directive)
2459 for (;;)
2461 CPP_PUTC (pfile, c);
2462 if (c == '>')
2463 break;
2464 c = GETC ();
2465 if (c == '\n' || c == EOF)
2467 cpp_error (pfile,
2468 "missing '>' in `#include <FILENAME>'");
2469 break;
2471 else if (c == '\r')
2473 if (!CPP_BUFFER (pfile)->has_escapes)
2475 /* Backslash newline is replaced by nothing. */
2476 CPP_ADJUST_WRITTEN (pfile, -1);
2477 CPP_BUMP_LINE (pfile);
2479 else
2481 /* We might conceivably get \r- or \r<space> in
2482 here. Just delete 'em. */
2483 int d = GETC();
2484 if (d != '-' && d != ' ')
2485 cpp_fatal (pfile,
2486 "internal error: unrecognized escape \\r%c",
2488 CPP_ADJUST_WRITTEN (pfile, -1);
2492 return CPP_STRING;
2494 /* else fall through */
2495 case '>':
2496 c2 = PEEKC ();
2497 if (c2 == '=')
2498 goto op2;
2499 /* GNU C++ supports MIN and MAX operators <? and >?. */
2500 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2501 goto randomchar;
2502 FORWARD(1);
2503 CPP_RESERVE (pfile, 4);
2504 CPP_PUTC (pfile, c);
2505 CPP_PUTC (pfile, c2);
2506 c3 = PEEKC ();
2507 if (c3 == '=')
2508 CPP_PUTC_Q (pfile, GETC ());
2509 CPP_NUL_TERMINATE_Q (pfile);
2510 pfile->only_seen_white = 0;
2511 return CPP_OTHER;
2513 case '.':
2514 c2 = PEEKC ();
2515 if (ISDIGIT(c2))
2517 CPP_RESERVE(pfile, 2);
2518 CPP_PUTC_Q (pfile, '.');
2519 c = GETC ();
2520 goto number;
2523 /* In C++ there's a .* operator. */
2524 if (opts->cplusplus && c2 == '*')
2525 goto op2;
2527 if (c2 == '.' && PEEKN(1) == '.')
2529 CPP_RESERVE(pfile, 4);
2530 CPP_PUTC_Q (pfile, '.');
2531 CPP_PUTC_Q (pfile, '.');
2532 CPP_PUTC_Q (pfile, '.');
2533 FORWARD (2);
2534 CPP_NUL_TERMINATE_Q (pfile);
2535 pfile->only_seen_white = 0;
2536 return CPP_3DOTS;
2538 goto randomchar;
2540 op2:
2541 token = CPP_OTHER;
2542 pfile->only_seen_white = 0;
2543 CPP_RESERVE(pfile, 3);
2544 CPP_PUTC_Q (pfile, c);
2545 CPP_PUTC_Q (pfile, GETC ());
2546 CPP_NUL_TERMINATE_Q (pfile);
2547 return token;
2549 case 'L':
2550 c2 = PEEKC ();
2551 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2553 CPP_PUTC (pfile, c);
2554 c = GETC ();
2555 goto string;
2557 goto letter;
2559 case '0': case '1': case '2': case '3': case '4':
2560 case '5': case '6': case '7': case '8': case '9':
2561 number:
2562 c2 = '.';
2563 for (;;)
2565 CPP_RESERVE (pfile, 2);
2566 CPP_PUTC_Q (pfile, c);
2567 c = PEEKC ();
2568 if (c == EOF)
2569 break;
2570 if (!is_idchar(c) && c != '.'
2571 && ((c2 != 'e' && c2 != 'E'
2572 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2573 || (c != '+' && c != '-')))
2574 break;
2575 FORWARD(1);
2576 c2= c;
2578 CPP_NUL_TERMINATE_Q (pfile);
2579 pfile->only_seen_white = 0;
2580 return CPP_NUMBER;
2581 case 'b': case 'c': case 'd': case 'h': case 'o':
2582 case 'B': case 'C': case 'D': case 'H': case 'O':
2583 if (opts->chill && PEEKC () == '\'')
2585 pfile->only_seen_white = 0;
2586 CPP_RESERVE (pfile, 2);
2587 CPP_PUTC_Q (pfile, c);
2588 CPP_PUTC_Q (pfile, '\'');
2589 FORWARD(1);
2590 for (;;)
2592 c = GETC();
2593 if (c == EOF)
2594 goto chill_number_eof;
2595 if (!is_idchar(c))
2596 break;
2597 CPP_PUTC (pfile, c);
2599 if (c == '\'')
2601 CPP_RESERVE (pfile, 2);
2602 CPP_PUTC_Q (pfile, c);
2603 CPP_NUL_TERMINATE_Q (pfile);
2604 return CPP_STRING;
2606 else
2608 FORWARD(-1);
2609 chill_number_eof:
2610 CPP_NUL_TERMINATE (pfile);
2611 return CPP_NUMBER;
2614 else
2615 goto letter;
2616 case '_':
2617 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2618 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2619 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2620 case 'x': case 'y': case 'z':
2621 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2622 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2623 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2624 case 'Y': case 'Z':
2625 letter:
2627 HASHNODE *hp;
2628 unsigned char *ident;
2629 int before_name_written = CPP_WRITTEN (pfile);
2630 int ident_len;
2631 parse_name (pfile, c);
2632 pfile->only_seen_white = 0;
2633 if (pfile->no_macro_expand)
2634 return CPP_NAME;
2635 ident = pfile->token_buffer + before_name_written;
2636 ident_len = CPP_PWRITTEN (pfile) - ident;
2637 hp = cpp_lookup (pfile, ident, ident_len, -1);
2638 if (!hp)
2639 return CPP_NAME;
2640 if (hp->type == T_DISABLED)
2642 if (pfile->output_escapes)
2643 { /* Return "\r-IDENT", followed by '\0'. */
2644 int i;
2645 CPP_RESERVE (pfile, 3);
2646 ident = pfile->token_buffer + before_name_written;
2647 CPP_ADJUST_WRITTEN (pfile, 2);
2648 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2649 ident[0] = '\r';
2650 ident[1] = '-';
2652 return CPP_NAME;
2655 /* If macro wants an arglist, verify that a '(' follows.
2656 first skip all whitespace, copying it to the output
2657 after the macro name. Then, if there is no '(',
2658 decide this is not a macro call and leave things that way. */
2659 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2661 int is_macro_call, macbuf_whitespace = 0;
2663 parse_set_mark (pfile);
2664 for (;;)
2666 cpp_skip_hspace (pfile);
2667 c = PEEKC ();
2668 is_macro_call = c == '(';
2669 if (c != EOF)
2671 if (c != '\n')
2672 break;
2673 FORWARD (1);
2675 else
2677 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2679 if (CPP_BUFFER (pfile)->mark !=
2680 (CPP_BUFFER (pfile)->cur
2681 - CPP_BUFFER (pfile)->buf))
2682 macbuf_whitespace = 1;
2684 /* The mark goes away automatically when
2685 the buffer is popped. */
2686 cpp_pop_buffer (pfile);
2687 parse_set_mark (pfile);
2689 else
2690 break;
2693 if (!is_macro_call)
2695 parse_goto_mark (pfile);
2696 if (macbuf_whitespace)
2697 CPP_PUTC (pfile, ' ');
2699 else
2700 parse_clear_mark (pfile);
2701 if (!is_macro_call)
2702 return CPP_NAME;
2704 /* This is now known to be a macro call.
2705 Expand the macro, reading arguments as needed,
2706 and push the expansion on the input stack. */
2707 macroexpand (pfile, hp);
2708 CPP_SET_WRITTEN (pfile, before_name_written);
2710 goto get_next;
2712 case ' ': case '\t': case '\v':
2713 for (;;)
2715 CPP_PUTC (pfile, c);
2716 c = PEEKC ();
2717 if (c == EOF || !is_hspace(c))
2718 break;
2719 FORWARD(1);
2721 return CPP_HSPACE;
2723 case '\r':
2724 if (CPP_BUFFER (pfile)->has_escapes)
2726 c = GETC ();
2727 if (c == '-')
2729 if (pfile->output_escapes)
2730 CPP_PUTS (pfile, "\r-", 2);
2731 parse_name (pfile, GETC ());
2732 return CPP_NAME;
2734 else if (c == ' ')
2736 CPP_RESERVE (pfile, 2);
2737 if (pfile->output_escapes)
2738 CPP_PUTC_Q (pfile, '\r');
2739 CPP_PUTC_Q (pfile, c);
2740 return CPP_HSPACE;
2742 else
2744 cpp_fatal (pfile,
2745 "internal error: unrecognized escape \\r%c", c);
2746 goto get_next;
2749 else
2751 /* Backslash newline is ignored. */
2752 CPP_BUMP_LINE (pfile);
2753 goto get_next;
2756 case '\n':
2757 CPP_PUTC (pfile, c);
2758 if (pfile->only_seen_white == 0)
2759 pfile->only_seen_white = 1;
2760 CPP_BUMP_LINE (pfile);
2761 if (! CPP_OPTIONS (pfile)->no_line_commands)
2763 pfile->lineno++;
2764 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2765 output_line_command (pfile, same_file);
2767 return CPP_VSPACE;
2769 case '(': token = CPP_LPAREN; goto char1;
2770 case ')': token = CPP_RPAREN; goto char1;
2771 case '{': token = CPP_LBRACE; goto char1;
2772 case '}': token = CPP_RBRACE; goto char1;
2773 case ',': token = CPP_COMMA; goto char1;
2774 case ';': token = CPP_SEMICOLON; goto char1;
2776 randomchar:
2777 default:
2778 token = CPP_OTHER;
2779 char1:
2780 pfile->only_seen_white = 0;
2781 CPP_PUTC (pfile, c);
2782 return token;
2787 /* Like cpp_get_token, but skip spaces and comments. */
2789 enum cpp_token
2790 cpp_get_non_space_token (pfile)
2791 cpp_reader *pfile;
2793 int old_written = CPP_WRITTEN (pfile);
2794 for (;;)
2796 enum cpp_token token = cpp_get_token (pfile);
2797 if (token != CPP_COMMENT && token != CPP_POP
2798 && token != CPP_HSPACE && token != CPP_VSPACE)
2799 return token;
2800 CPP_SET_WRITTEN (pfile, old_written);
2804 /* Parse an identifier starting with C. */
2806 static void
2807 parse_name (pfile, c)
2808 cpp_reader *pfile;
2809 int c;
2811 for (;;)
2813 if (! is_idchar(c))
2815 FORWARD (-1);
2816 break;
2819 if (c == '$' && CPP_PEDANTIC (pfile))
2820 cpp_pedwarn (pfile, "`$' in identifier");
2822 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2823 CPP_PUTC_Q (pfile, c);
2824 c = GETC();
2825 if (c == EOF)
2826 break;
2828 CPP_NUL_TERMINATE_Q (pfile);
2829 return;
2832 /* Parse a string starting with C. A single quoted string is treated
2833 like a double -- some programs (e.g., troff) are perverse this way.
2834 (However, a single quoted string is not allowed to extend over
2835 multiple lines.) */
2836 static void
2837 parse_string (pfile, c)
2838 cpp_reader *pfile;
2839 int c;
2841 long start_line, start_column;
2843 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2845 CPP_PUTC (pfile, c);
2846 while (1)
2848 int cc = GETC();
2849 if (cc == EOF)
2851 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2853 /* try harder: this string crosses a macro expansion
2854 boundary. This can happen naturally if -traditional.
2855 Otherwise, only -D can make a macro with an unmatched
2856 quote. */
2857 cpp_pop_buffer (pfile);
2858 continue;
2861 cpp_error_with_line (pfile, start_line, start_column,
2862 "unterminated string or character constant");
2863 if (pfile->multiline_string_line != start_line
2864 && pfile->multiline_string_line != 0)
2865 cpp_error_with_line (pfile,
2866 pfile->multiline_string_line, -1,
2867 "possible real start of unterminated constant");
2868 pfile->multiline_string_line = 0;
2869 break;
2871 CPP_PUTC (pfile, cc);
2872 switch (cc)
2874 case '\n':
2875 CPP_BUMP_LINE (pfile);
2876 pfile->lineno++;
2878 /* In Fortran and assembly language, silently terminate
2879 strings of either variety at end of line. This is a
2880 kludge around not knowing where comments are in these
2881 languages. */
2882 if (CPP_OPTIONS (pfile)->lang_fortran
2883 || CPP_OPTIONS (pfile)->lang_asm)
2884 return;
2885 /* Character constants may not extend over multiple lines.
2886 In Standard C, neither may strings. We accept multiline
2887 strings as an extension. */
2888 if (c == '\'')
2890 cpp_error_with_line (pfile, start_line, start_column,
2891 "unterminated character constant");
2892 return;
2894 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2895 cpp_pedwarn_with_line (pfile, start_line, start_column,
2896 "string constant runs past end of line");
2897 if (pfile->multiline_string_line == 0)
2898 pfile->multiline_string_line = start_line;
2899 break;
2901 case '\r':
2902 CPP_ADJUST_WRITTEN (pfile, -1);
2903 if (CPP_BUFFER (pfile)->has_escapes)
2905 cpp_fatal (pfile,
2906 "internal error: \\r escape inside string constant");
2907 FORWARD(1);
2909 else
2910 /* Backslash newline is replaced by nothing at all. */
2911 CPP_BUMP_LINE (pfile);
2912 break;
2914 case '\\':
2915 cc = GETC();
2916 if (cc != EOF)
2917 CPP_PUTC (pfile, cc);
2918 break;
2920 case '\"':
2921 case '\'':
2922 if (cc == c)
2923 return;
2924 break;
2929 /* Read an assertion into the token buffer, converting to
2930 canonical form: `#predicate(a n swe r)' The next non-whitespace
2931 character to read should be the first letter of the predicate.
2932 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2933 with answer (see callers for why). In case of 0, an error has been
2934 printed. */
2935 static int
2936 parse_assertion (pfile)
2937 cpp_reader *pfile;
2939 int c, dropwhite;
2940 cpp_skip_hspace (pfile);
2941 c = PEEKC();
2942 if (! is_idstart(c))
2944 cpp_error (pfile, "assertion predicate is not an identifier");
2945 return 0;
2947 CPP_PUTC(pfile, '#');
2948 FORWARD(1);
2949 parse_name(pfile, c);
2951 c = PEEKC();
2952 if (c != '(')
2954 if (is_hspace(c) || c == '\r')
2955 cpp_skip_hspace (pfile);
2956 c = PEEKC();
2958 if (c != '(')
2959 return 1;
2961 CPP_PUTC(pfile, '(');
2962 FORWARD(1);
2963 dropwhite = 1;
2964 while ((c = GETC()) != ')')
2966 if (is_space(c))
2968 if (! dropwhite)
2970 CPP_PUTC(pfile, ' ');
2971 dropwhite = 1;
2974 else if (c == '\n' || c == EOF)
2976 if (c == '\n') FORWARD(-1);
2977 cpp_error (pfile, "un-terminated assertion answer");
2978 return 0;
2980 else if (c == '\r')
2981 /* \r cannot be a macro escape here. */
2982 CPP_BUMP_LINE (pfile);
2983 else
2985 CPP_PUTC (pfile, c);
2986 dropwhite = 0;
2990 if (pfile->limit[-1] == ' ')
2991 pfile->limit[-1] = ')';
2992 else if (pfile->limit[-1] == '(')
2994 cpp_error (pfile, "empty token sequence in assertion");
2995 return 0;
2997 else
2998 CPP_PUTC (pfile, ')');
3000 CPP_NUL_TERMINATE (pfile);
3001 return 2;
3004 static int
3005 do_assert (pfile, keyword)
3006 cpp_reader *pfile;
3007 const struct directive *keyword ATTRIBUTE_UNUSED;
3009 char *sym;
3010 int ret, c;
3011 HASHNODE *base, *this;
3012 int baselen, thislen;
3014 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3015 && !CPP_BUFFER (pfile)->system_header_p)
3016 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3018 cpp_skip_hspace (pfile);
3019 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3020 ret = parse_assertion (pfile);
3021 if (ret == 0)
3022 goto error;
3023 else if (ret == 1)
3025 cpp_error (pfile, "missing token-sequence in `#assert'");
3026 goto error;
3029 cpp_skip_hspace (pfile);
3030 c = PEEKC();
3031 if (c != EOF && c != '\n')
3033 cpp_error (pfile, "junk at end of `#assert'");
3034 goto error;
3037 thislen = strlen (sym);
3038 baselen = index (sym, '(') - sym;
3039 this = cpp_lookup (pfile, sym, thislen, -1);
3040 if (this)
3042 cpp_warning (pfile, "`%s' re-asserted", sym);
3043 goto error;
3046 base = cpp_lookup (pfile, sym, baselen, -1);
3047 if (! base)
3048 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0, -1);
3049 else if (base->type != T_ASSERT)
3051 /* Token clash - but with what?! */
3052 cpp_fatal (pfile,
3053 "cpp internal error: base->type != T_ASSERT in do_assert");
3054 goto error;
3057 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3058 (char *)base->value.aschain, -1);
3059 base->value.aschain = this;
3061 pfile->limit = (unsigned char *) sym; /* Pop */
3062 return 0;
3064 error:
3065 skip_rest_of_line (pfile);
3066 pfile->limit = (unsigned char *) sym; /* Pop */
3067 return 0;
3070 static int
3071 do_unassert (pfile, keyword)
3072 cpp_reader *pfile;
3073 const struct directive *keyword ATTRIBUTE_UNUSED;
3075 int c, ret;
3076 char *sym;
3077 long baselen, thislen;
3078 HASHNODE *base, *this, *next;
3080 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3081 && !CPP_BUFFER (pfile)->system_header_p)
3082 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3084 cpp_skip_hspace (pfile);
3086 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3087 ret = parse_assertion (pfile);
3088 if (ret == 0)
3089 goto error;
3091 cpp_skip_hspace (pfile);
3092 c = PEEKC ();
3093 if (c != EOF && c != '\n')
3094 cpp_error (pfile, "junk at end of `#unassert'");
3096 thislen = strlen (sym);
3097 if (ret == 1)
3099 base = cpp_lookup (pfile, sym, thislen, -1);
3100 if (! base)
3101 goto error; /* It isn't an error to #undef what isn't #defined,
3102 so it isn't an error to #unassert what isn't
3103 #asserted either. */
3105 for (this = base->value.aschain; this; this = next)
3107 next = this->value.aschain;
3108 delete_macro (this);
3110 delete_macro (base);
3112 else
3114 baselen = index (sym, '(') - sym;
3115 base = cpp_lookup (pfile, sym, baselen, -1);
3116 if (! base) goto error;
3117 this = cpp_lookup (pfile, sym, thislen, -1);
3118 if (! this) goto error;
3120 next = base;
3121 while (next->value.aschain != this)
3122 next = next->value.aschain;
3124 next->value.aschain = this->value.aschain;
3125 delete_macro (this);
3127 if (base->value.aschain == NULL)
3128 delete_macro (base); /* Last answer for this predicate deleted. */
3131 pfile->limit = (unsigned char *) sym; /* Pop */
3132 return 0;
3133 error:
3134 skip_rest_of_line (pfile);
3135 pfile->limit = (unsigned char *) sym; /* Pop */
3136 return 0;
3139 /* Process STR as if it appeared as the body of an #unassert. */
3140 void
3141 cpp_unassert (pfile, str)
3142 cpp_reader *pfile;
3143 unsigned char *str;
3145 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3147 do_assert (pfile, NULL);
3148 cpp_pop_buffer (pfile);
3153 cpp_read_check_assertion (pfile)
3154 cpp_reader *pfile;
3156 U_CHAR *name = CPP_PWRITTEN (pfile);
3157 int result;
3158 HASHNODE *hp;
3160 FORWARD (1); /* Skip '#' */
3161 cpp_skip_hspace (pfile);
3162 if (! parse_assertion (pfile))
3163 result = 0;
3164 else
3166 hp = cpp_lookup (pfile, name, CPP_PWRITTEN (pfile) - name, -1);
3167 result = (hp != 0);
3170 pfile->limit = name;
3171 return result;
3174 /* Remember the current position of PFILE. */
3176 void
3177 parse_set_mark (pfile)
3178 cpp_reader *pfile;
3180 cpp_buffer *ip = CPP_BUFFER (pfile);
3181 if (ip->mark != -1)
3182 cpp_fatal (pfile,
3183 "cpp internal error: ip->mark != -1 in parse_set_mark");
3185 ip->mark = ip->cur - ip->buf;
3188 /* Clear the current mark - we no longer need it. */
3190 void
3191 parse_clear_mark (pfile)
3192 cpp_reader *pfile;
3194 cpp_buffer *ip = CPP_BUFFER (pfile);
3195 if (ip->mark == -1)
3196 cpp_fatal (pfile,
3197 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3199 ip->mark = -1;
3202 /* Backup the current position of PFILE to that saved in its mark,
3203 and clear the mark. */
3205 void
3206 parse_goto_mark (pfile)
3207 cpp_reader *pfile;
3209 cpp_buffer *ip = CPP_BUFFER (pfile);
3210 if (ip->mark == -1)
3211 cpp_fatal (pfile,
3212 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3214 ip->cur = ip->buf + ip->mark;
3215 ip->mark = -1;
3218 static void
3219 cpp_print_file_and_line (pfile)
3220 cpp_reader *pfile;
3222 cpp_buffer *ip = cpp_file_buffer (pfile);
3224 if (ip != NULL)
3226 long line, col;
3227 cpp_buf_line_and_col (ip, &line, &col);
3228 cpp_file_line_for_message (pfile, ip->nominal_fname,
3229 line, pfile->show_column ? col : -1);
3233 static void
3234 v_cpp_error (pfile, msgid, ap)
3235 cpp_reader *pfile;
3236 const char *msgid;
3237 va_list ap;
3239 cpp_print_containing_files (pfile);
3240 cpp_print_file_and_line (pfile);
3241 v_cpp_message (pfile, 1, msgid, ap);
3244 void
3245 cpp_error VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
3247 #ifndef ANSI_PROTOTYPES
3248 cpp_reader *pfile;
3249 const char *msgid;
3250 #endif
3251 va_list ap;
3253 VA_START(ap, msgid);
3255 #ifndef ANSI_PROTOTYPES
3256 pfile = va_arg (ap, cpp_reader *);
3257 msgid = va_arg (ap, const char *);
3258 #endif
3260 v_cpp_error (pfile, msgid, ap);
3261 va_end(ap);
3264 /* Print error message but don't count it. */
3266 static void
3267 v_cpp_warning (pfile, msgid, ap)
3268 cpp_reader *pfile;
3269 const char *msgid;
3270 va_list ap;
3272 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3273 return;
3275 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3276 pfile->errors++;
3278 cpp_print_containing_files (pfile);
3279 cpp_print_file_and_line (pfile);
3280 v_cpp_message (pfile, 0, msgid, ap);
3283 void
3284 cpp_warning VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
3286 #ifndef ANSI_PROTOTYPES
3287 cpp_reader *pfile;
3288 const char *msgid;
3289 #endif
3290 va_list ap;
3292 VA_START (ap, msgid);
3294 #ifndef ANSI_PROTOTYPES
3295 pfile = va_arg (ap, cpp_reader *);
3296 msgid = va_arg (ap, const char *);
3297 #endif
3299 v_cpp_warning (pfile, msgid, ap);
3300 va_end(ap);
3303 /* Print an error message and maybe count it. */
3305 void
3306 cpp_pedwarn VPARAMS ((cpp_reader * pfile, const char *msgid, ...))
3308 #ifndef ANSI_PROTOTYPES
3309 cpp_reader *pfile;
3310 const char *msgid;
3311 #endif
3312 va_list ap;
3314 VA_START (ap, msgid);
3316 #ifndef ANSI_PROTOTYPES
3317 pfile = va_arg (ap, cpp_reader *);
3318 msgid = va_arg (ap, const char *);
3319 #endif
3321 if (CPP_OPTIONS (pfile)->pedantic_errors)
3322 v_cpp_error (pfile, msgid, ap);
3323 else
3324 v_cpp_warning (pfile, msgid, ap);
3325 va_end(ap);
3328 static void
3329 v_cpp_error_with_line (pfile, line, column, msgid, ap)
3330 cpp_reader * pfile;
3331 int line;
3332 int column;
3333 const char * msgid;
3334 va_list ap;
3336 cpp_buffer *ip = cpp_file_buffer (pfile);
3338 cpp_print_containing_files (pfile);
3340 if (ip != NULL)
3341 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3343 v_cpp_message (pfile, 1, msgid, ap);
3346 void
3347 cpp_error_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
3348 const char *msgid, ...))
3350 #ifndef ANSI_PROTOTYPES
3351 cpp_reader *pfile;
3352 int line;
3353 int column;
3354 const char *msgid;
3355 #endif
3356 va_list ap;
3358 VA_START (ap, msgid);
3360 #ifndef ANSI_PROTOTYPES
3361 pfile = va_arg (ap, cpp_reader *);
3362 line = va_arg (ap, int);
3363 column = va_arg (ap, int);
3364 msgid = va_arg (ap, const char *);
3365 #endif
3367 v_cpp_error_with_line(pfile, line, column, msgid, ap);
3368 va_end(ap);
3371 static void
3372 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
3373 cpp_reader * pfile;
3374 int line;
3375 int column;
3376 const char *msgid;
3377 va_list ap;
3379 cpp_buffer *ip;
3381 if (CPP_OPTIONS (pfile)->inhibit_warnings)
3382 return;
3384 if (CPP_OPTIONS (pfile)->warnings_are_errors)
3385 pfile->errors++;
3387 cpp_print_containing_files (pfile);
3389 ip = cpp_file_buffer (pfile);
3391 if (ip != NULL)
3392 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
3394 v_cpp_message (pfile, 0, msgid, ap);
3397 void
3398 cpp_warning_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
3399 const char *msgid, ...))
3401 #ifndef ANSI_PROTOTYPES
3402 cpp_reader *pfile;
3403 int line;
3404 int column;
3405 const char *msgid;
3406 #endif
3407 va_list ap;
3409 VA_START (ap, msgid);
3411 #ifndef ANSI_PROTOTYPES
3412 pfile = va_arg (ap, cpp_reader *);
3413 line = va_arg (ap, int);
3414 column = va_arg (ap, int);
3415 msgid = va_arg (ap, const char *);
3416 #endif
3418 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3419 va_end(ap);
3422 void
3423 cpp_pedwarn_with_line VPARAMS ((cpp_reader * pfile, int line, int column,
3424 const char *msgid, ...))
3426 #ifndef ANSI_PROTOTYPES
3427 cpp_reader *pfile;
3428 int line;
3429 int column;
3430 const char *msgid;
3431 #endif
3432 va_list ap;
3434 VA_START (ap, msgid);
3436 #ifndef ANSI_PROTOTYPES
3437 pfile = va_arg (ap, cpp_reader *);
3438 line = va_arg (ap, int);
3439 column = va_arg (ap, int);
3440 msgid = va_arg (ap, const char *);
3441 #endif
3443 if (CPP_OPTIONS (pfile)->pedantic_errors)
3444 v_cpp_error_with_line (pfile, column, line, msgid, ap);
3445 else
3446 v_cpp_warning_with_line (pfile, line, column, msgid, ap);
3447 va_end(ap);
3450 /* Report a warning (or an error if pedantic_errors)
3451 giving specified file name and line number, not current. */
3453 void
3454 cpp_pedwarn_with_file_and_line VPARAMS ((cpp_reader *pfile, const char *file,
3455 int line, const char *msgid, ...))
3457 #ifndef ANSI_PROTOTYPES
3458 cpp_reader *pfile;
3459 const char *file;
3460 int line;
3461 const char *msgid;
3462 #endif
3463 va_list ap;
3465 VA_START (ap, msgid);
3467 #ifndef ANSI_PROTOTYPES
3468 pfile = va_arg (ap, cpp_reader *);
3469 file = va_arg (ap, const char *);
3470 line = va_arg (ap, int);
3471 msgid = va_arg (ap, const char *);
3472 #endif
3474 if (!CPP_OPTIONS (pfile)->pedantic_errors
3475 && CPP_OPTIONS (pfile)->inhibit_warnings)
3476 return;
3477 cpp_file_line_for_message (pfile, file, line, -1);
3478 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
3479 va_end(ap);
3482 /* my_strerror - return the descriptive text associated with an
3483 `errno' code. */
3485 static const char *
3486 my_strerror (errnum)
3487 int errnum;
3489 const char *result;
3491 #ifndef VMS
3492 #ifndef HAVE_STRERROR
3493 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
3494 #else
3495 result = strerror (errnum);
3496 #endif
3497 #else /* VMS */
3498 /* VAXCRTL's strerror() takes an optional second argument, which only
3499 matters when the first argument is EVMSERR. However, it's simplest
3500 just to pass it unconditionally. `vaxc$errno' is declared in
3501 <errno.h>, and maintained by the library in parallel with `errno'.
3502 We assume that caller's `errnum' either matches the last setting of
3503 `errno' by the library or else does not have the value `EVMSERR'. */
3505 result = strerror (errnum, vaxc$errno);
3506 #endif
3508 if (!result)
3509 result = "errno = ?";
3511 return result;
3514 /* Error including a message from `errno'. */
3516 void
3517 cpp_error_from_errno (pfile, name)
3518 cpp_reader *pfile;
3519 const char *name;
3521 cpp_message_from_errno (pfile, 1, name);
3524 void
3525 cpp_message_from_errno (pfile, is_error, name)
3526 cpp_reader *pfile;
3527 int is_error;
3528 const char *name;
3530 int e = errno;
3531 cpp_buffer *ip = cpp_file_buffer (pfile);
3533 cpp_print_containing_files (pfile);
3535 if (ip != NULL)
3536 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
3538 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
3541 void
3542 cpp_perror_with_name (pfile, name)
3543 cpp_reader *pfile;
3544 const char *name;
3546 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
3549 /* TODO:
3550 * No pre-compiled header file support.
3552 * Possibly different enum token codes for each C/C++ token.
3554 * Find and cleanup remaining uses of static variables,
3556 * Support -dM flag (dump_all_macros).
3558 * Support for_lint flag.