2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*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 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
40 case CPP_BUMP_LINE must not be called. */
41 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
43 /* External declarations. */
45 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
47 /* `struct directive' defines one #-directive, including how to handle it. */
50 int length; /* Length of name */
51 int (*func) /* Function to handle directive */
52 PARAMS ((cpp_reader *, const struct directive *));
53 const char *name; /* Name of directive */
54 enum node_type type; /* Code which describes which directive. */
57 /* These functions are declared to return int instead of void since they
58 are going to be placed in a table and some old compilers have trouble with
59 pointers to functions returning void. */
61 static int do_define PARAMS ((cpp_reader *, const struct directive *));
62 static int do_line PARAMS ((cpp_reader *, const struct directive *));
63 static int do_include PARAMS ((cpp_reader *, const struct directive *));
64 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
65 static int do_error PARAMS ((cpp_reader *, const struct directive *));
66 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
67 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
68 static int do_if PARAMS ((cpp_reader *, const struct directive *));
69 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
70 static int do_else PARAMS ((cpp_reader *, const struct directive *));
71 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
72 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
74 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
76 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
77 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
78 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
80 /* Forward declarations. */
82 static void validate_else PARAMS ((cpp_reader *, const char *));
83 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
84 static void conditional_skip PARAMS ((cpp_reader *, int,
85 enum node_type, U_CHAR *));
86 static void skip_if_group PARAMS ((cpp_reader *));
87 static void parse_name PARAMS ((cpp_reader *, int));
88 static void parse_string PARAMS ((cpp_reader *, int));
89 static int parse_assertion PARAMS ((cpp_reader *));
90 static const char *if_directive_name PARAMS ((cpp_reader *,
92 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
93 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
94 static int skip_comment PARAMS ((cpp_reader *, int));
95 static int copy_comment PARAMS ((cpp_reader *, int));
96 static void copy_rest_of_line PARAMS ((cpp_reader *));
97 static void skip_rest_of_line PARAMS ((cpp_reader *));
98 static void cpp_skip_hspace PARAMS ((cpp_reader *));
99 static int handle_directive PARAMS ((cpp_reader *));
100 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
102 const struct directive *));
103 static int read_line_number PARAMS ((cpp_reader *, int *));
104 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
105 static int consider_directive_while_skipping PARAMS ((cpp_reader *,
107 static void skip_block_comment PARAMS ((cpp_reader *));
108 static void skip_line_comment PARAMS ((cpp_reader *));
109 static void parse_set_mark PARAMS ((cpp_reader *));
110 static void parse_goto_mark PARAMS ((cpp_reader *));
112 /* Here is the actual list of #-directives.
113 This table is ordered by frequency of occurrence; the numbers
114 at the end are directive counts from all the source code I have
115 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
116 linux-2.2.9, and pcmcia-cs-3.0.9). */
118 static const struct directive directive_table[] = {
120 { 6, do_define, "define", T_DEFINE }, /* 270554 */
121 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
122 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
123 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
124 { 2, do_if, "if", T_IF }, /* 18162 */
125 { 4, do_else, "else", T_ELSE }, /* 9863 */
126 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
127 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
128 { 4, do_line, "line", T_LINE }, /* 2465 */
129 { 4, do_elif, "elif", T_ELIF }, /* 610 */
130 { 5, do_error, "error", T_ERROR }, /* 475 */
131 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
133 /* Extensions. All deprecated except #warning and #include_next. */
134 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
135 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
136 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
137 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
138 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
139 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
140 #ifdef SCCS_DIRECTIVE
141 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
143 { -1, 0, "", T_UNUSED }
146 /* Place into PFILE a quoted string representing the string SRC.
147 Caller must reserve enough space in pfile->token_buffer. */
150 quote_string (pfile, src)
156 CPP_PUTC_Q (pfile, '\"');
158 switch ((c = *src++))
162 CPP_PUTC_Q (pfile, c);
165 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
166 CPP_ADJUST_WRITTEN (pfile, 4);
172 CPP_PUTC_Q (pfile, '\\');
173 CPP_PUTC_Q (pfile, c);
177 CPP_PUTC_Q (pfile, '\"');
178 CPP_NUL_TERMINATE_Q (pfile);
183 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
186 cpp_grow_buffer (pfile, n)
190 long old_written = CPP_WRITTEN (pfile);
191 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
192 pfile->token_buffer = (U_CHAR *)
193 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
194 CPP_SET_WRITTEN (pfile, old_written);
197 /* Process the string STR as if it appeared as the body of a #define.
198 If STR is just an identifier, define it with value 1.
199 If STR has anything after the identifier, then it should
200 be identifier=definition. */
203 cpp_define (pfile, str)
210 p = strchr (str, '=');
211 /* Copy the entire option so we can modify it.
212 Change the first "=" in the string to a space. If there is none,
213 tack " 1" on the end. Then add a newline and a NUL. */
217 count = strlen (str) + 2;
218 buf = (U_CHAR *) alloca (count);
219 memcpy (buf, str, count - 2);
221 buf[count - 2] = '\n';
222 buf[count - 1] = '\0';
226 count = strlen (str) + 4;
227 buf = (U_CHAR *) alloca (count);
228 memcpy (buf, str, count - 4);
229 strcpy (&buf[count-4], " 1\n");
232 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
234 do_define (pfile, NULL);
235 cpp_pop_buffer (pfile);
239 /* Process the string STR as if it appeared as the body of a #assert. */
241 cpp_assert (pfile, str)
245 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
247 do_assert (pfile, NULL);
248 cpp_pop_buffer (pfile);
252 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
254 cpp_defined (pfile, id, len)
259 HASHNODE *hp = cpp_lookup (pfile, id, len);
260 if (hp && hp->type == T_POISON)
262 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
268 static enum cpp_token
269 null_underflow (pfile)
270 cpp_reader *pfile ATTRIBUTE_UNUSED;
276 null_cleanup (pbuf, pfile)
277 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
278 cpp_reader *pfile ATTRIBUTE_UNUSED;
283 /* Skip a C-style block comment. We know it's a comment, and point is
284 at the second character of the starter. */
286 skip_block_comment (pfile)
293 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
299 cpp_error_with_line (pfile, line, col, "unterminated comment");
302 else if (c == '\n' || c == '\r')
304 /* \r cannot be a macro escape marker here. */
305 if (!ACTIVE_MARK_P())
306 CPP_BUMP_LINE (pfile);
308 else if (c == '/' && prev_c == '*')
310 else if (c == '*' && prev_c == '/'
311 && CPP_OPTIONS (pfile)->warn_comments)
312 cpp_warning (pfile, "`/*' within comment");
318 /* Skip a C++/Chill line comment. We know it's a comment, and point
319 is at the second character of the initiator. */
321 skip_line_comment (pfile)
329 /* We don't have to worry about EOF in here. */
332 /* Don't consider final '\n' to be part of comment. */
338 /* \r cannot be a macro escape marker here. */
339 if (!ACTIVE_MARK_P())
340 CPP_BUMP_LINE (pfile);
341 if (CPP_OPTIONS (pfile)->warn_comments)
342 cpp_warning (pfile, "backslash-newline within line comment");
347 /* Skip a comment - C, C++, or Chill style. M is the first character
348 of the comment marker. If this really is a comment, skip to its
349 end and return ' '. If this is not a comment, return M (which will
353 skip_comment (pfile, m)
357 if (m == '/' && PEEKC() == '*')
359 skip_block_comment (pfile);
362 else if (m == '/' && PEEKC() == '/')
364 if (CPP_BUFFER (pfile)->system_header_p)
366 /* We silently allow C++ comments in system headers, irrespective
367 of conformance mode, because lots of busted systems do that
368 and trying to clean it up in fixincludes is a nightmare. */
369 skip_line_comment (pfile);
372 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
374 if (CPP_OPTIONS (pfile)->c89
375 && CPP_PEDANTIC (pfile)
376 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
379 "C++ style comments are not allowed in ISO C89");
381 "(this will be reported only once per input file)");
382 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
384 skip_line_comment (pfile);
390 else if (m == '-' && PEEKC() == '-'
391 && CPP_OPTIONS (pfile)->chill)
393 skip_line_comment (pfile);
400 /* Identical to skip_comment except that it copies the comment into the
401 token_buffer. This is used if !discard_comments. */
403 copy_comment (pfile, m)
407 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
410 if (skip_comment (pfile, m) == m)
414 for (limit = CPP_BUFFER (pfile)->cur; start <= limit; start++)
416 CPP_PUTC (pfile, *start);
420 /* Skip whitespace \-newline and comments. Does not macro-expand. */
423 cpp_skip_hspace (pfile)
432 else if (is_hspace(c))
434 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
435 cpp_pedwarn (pfile, "%s in preprocessing directive",
436 c == '\f' ? "formfeed" : "vertical tab");
440 /* \r is a backslash-newline marker if !has_escapes, and
441 a deletable-whitespace or no-reexpansion marker otherwise. */
442 if (CPP_BUFFER (pfile)->has_escapes)
450 CPP_BUFFER (pfile)->lineno++;
452 else if (c == '/' || c == '-')
454 c = skip_comment (pfile, c);
464 /* Read the rest of the current line.
465 The line is appended to PFILE's output buffer. */
468 copy_rest_of_line (pfile)
479 CPP_NUL_TERMINATE (pfile);
483 if (CPP_BUFFER (pfile)->has_escapes)
487 CPP_BUFFER (pfile)->lineno++;
492 parse_string (pfile, c);
497 if (CPP_TRADITIONAL (pfile))
498 CPP_PUTS (pfile, "/**/", 4);
499 skip_block_comment (pfile);
502 /* else fall through */
504 c = skip_comment (pfile, c);
509 if (CPP_PEDANTIC (pfile))
510 cpp_pedwarn (pfile, "%s in preprocessing directive",
511 c == '\f' ? "formfeed" : "vertical tab");
519 /* FIXME: It is almost definitely a performance win to make this do
520 the scan itself. >75% of calls to copy_r_o_l are from here or
521 skip_if_group, which means the common case is to copy stuff into the
522 token_buffer only to discard it. */
524 skip_rest_of_line (pfile)
527 long old = CPP_WRITTEN (pfile);
528 copy_rest_of_line (pfile);
529 CPP_SET_WRITTEN (pfile, old);
532 /* Handle a possible # directive.
533 '#' has already been read. */
536 handle_directive (pfile)
540 register const struct directive *kt;
543 long old_written = CPP_WRITTEN (pfile);
545 cpp_skip_hspace (pfile);
548 /* # followed by a number is equivalent to #line. Do not recognize
549 this form in assembly language source files. Complain about this
550 form if we're being pedantic, but not if this is regurgitated
551 input (preprocessed or fed back in by the C++ frontend). */
552 if (c >= '0' && c <= '9')
554 if (CPP_OPTIONS (pfile)->lang_asm)
557 if (CPP_PEDANTIC (pfile)
558 && ! CPP_PREPROCESSED (pfile)
559 && ! CPP_BUFFER (pfile)->manual_pop)
560 cpp_pedwarn (pfile, "`#' followed by integer");
561 do_line (pfile, NULL);
565 /* If we are rescanning preprocessed input, don't obey any directives
567 if (CPP_PREPROCESSED (pfile))
570 /* Now find the directive name. */
571 CPP_PUTC (pfile, '#');
572 parse_name (pfile, GETC());
573 ident = pfile->token_buffer + old_written + 1;
574 ident_length = CPP_PWRITTEN (pfile) - ident;
575 if (ident_length == 0)
577 /* A line of just `#' becomes blank. A line with something
578 other than an identifier after the # is reparsed as a non-
580 CPP_SET_WRITTEN (pfile, old_written);
581 return (PEEKC() == '\n');
584 /* Decode the keyword and call the appropriate expansion routine. */
585 for (kt = directive_table; ; kt++)
588 /* # identifier, but not a legit directive. Pass onward as a
589 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
591 if (kt->length == ident_length
592 && !strncmp (kt->name, ident, ident_length))
596 CPP_SET_WRITTEN (pfile, old_written);
598 if (pfile->no_directives)
600 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
602 skip_rest_of_line (pfile);
605 (*kt->func) (pfile, kt);
610 /* Pass a directive through to the output file.
611 BUF points to the contents of the directive, as a contiguous string.
612 LEN is the length of the string pointed to by BUF.
613 KEYWORD is the keyword-table entry for the directive. */
616 pass_thru_directive (buf, len, pfile, keyword)
620 const struct directive *keyword;
622 register unsigned keyword_length = keyword->length;
624 CPP_RESERVE (pfile, 1 + keyword_length + len);
625 CPP_PUTC_Q (pfile, '#');
626 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
627 if (len != 0 && buf[0] != ' ')
628 CPP_PUTC_Q (pfile, ' ');
629 CPP_PUTS_Q (pfile, buf, len);
632 /* Check a purported macro name SYMNAME, and yield its length. */
635 check_macro_name (pfile, symname)
637 const U_CHAR *symname;
642 for (p = symname; is_idchar(*p); p++)
644 sym_length = p - symname;
646 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
647 cpp_error (pfile, "invalid macro name");
648 else if (!is_idstart(*symname)
649 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
650 U_CHAR *msg; /* what pain... */
651 msg = (U_CHAR *) alloca (sym_length + 1);
652 bcopy (symname, msg, sym_length);
654 cpp_error (pfile, "invalid macro name `%s'", msg);
659 /* Process a #define command.
660 KEYWORD is the keyword-table entry for #define,
661 or NULL for a "predefined" macro,
662 or the keyword-table entry for #pragma in the case of a #pragma poison. */
665 do_define (pfile, keyword)
667 const struct directive *keyword;
672 U_CHAR *macro, *buf, *end;
674 here = CPP_WRITTEN (pfile);
675 copy_rest_of_line (pfile);
677 /* Copy out the line so we can pop the token buffer. */
678 buf = pfile->token_buffer + here;
679 end = CPP_PWRITTEN (pfile);
680 macro = (U_CHAR *) alloca (end - buf + 1);
681 memcpy (macro, buf, end - buf + 1);
682 end = macro + (end - buf);
684 CPP_SET_WRITTEN (pfile, here);
686 mdef = create_definition (macro, end, pfile);
690 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen)) != NULL)
694 /* Redefining a macro is ok if the definitions are the same. */
695 if (hp->type == T_MACRO)
696 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
697 /* Redefining a constant is ok with -D. */
698 else if (hp->type == T_CONST || hp->type == T_STDC)
699 ok = ! CPP_OPTIONS (pfile)->done_initializing;
700 /* Otherwise it's not ok. */
703 /* Print the warning or error if it's not ok. */
706 if (hp->type == T_POISON)
707 cpp_error (pfile, "redefining poisoned `%.*s'",
708 mdef.symlen, mdef.symnam);
710 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
711 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
712 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
713 hp->value.defn->line, -1,
714 "this is the location of the previous definition");
716 if (hp->type != T_POISON)
718 /* Replace the old definition. */
719 if (hp->type == T_MACRO)
720 free_definition (hp->value.defn);
722 hp->value.defn = mdef.defn;
726 cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO, (char *)mdef.defn);
728 if (keyword != NULL && keyword->type == T_DEFINE)
730 if (CPP_OPTIONS (pfile)->debug_output
731 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
732 dump_definition (pfile, mdef.symnam, mdef.symlen, mdef.defn);
733 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
734 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
741 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
742 If BUFFER != NULL, then use the LENGTH characters in BUFFER
743 as the new input buffer.
744 Return the new buffer, or NULL on failure. */
747 cpp_push_buffer (pfile, buffer, length)
752 cpp_buffer *buf = CPP_BUFFER (pfile);
754 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
756 cpp_fatal (pfile, "macro or `#include' recursion too deep");
760 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
762 new->if_stack = pfile->if_stack;
763 new->cleanup = null_cleanup;
764 new->underflow = null_underflow;
765 new->buf = new->cur = buffer;
766 new->alimit = new->rlimit = buffer + length;
770 CPP_BUFFER (pfile) = new;
775 cpp_pop_buffer (pfile)
778 cpp_buffer *buf = CPP_BUFFER (pfile);
780 cpp_ice (pfile, "mark active in cpp_pop_buffer");
781 (*buf->cleanup) (buf, pfile);
782 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
784 pfile->buffer_stack_depth--;
785 return CPP_BUFFER (pfile);
788 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
789 Pop the buffer when done. */
792 cpp_scan_buffer (pfile)
795 cpp_buffer *buffer = CPP_BUFFER (pfile);
796 enum cpp_token token;
797 if (CPP_OPTIONS (pfile)->no_output)
799 long old_written = CPP_WRITTEN (pfile);
800 /* In no-output mode, we can ignore everything but directives. */
803 if (! pfile->only_seen_white)
804 skip_rest_of_line (pfile);
805 token = cpp_get_token (pfile);
806 if (token == CPP_EOF) /* Should not happen ... */
808 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
810 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
811 != CPP_NULL_BUFFER (pfile))
812 cpp_pop_buffer (pfile);
816 CPP_SET_WRITTEN (pfile, old_written);
822 token = cpp_get_token (pfile);
823 if (token == CPP_EOF) /* Should not happen ... */
825 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
827 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
828 != CPP_NULL_BUFFER (pfile))
829 cpp_pop_buffer (pfile);
837 * Rescan a string (which may have escape marks) into pfile's buffer.
838 * Place the result in pfile->token_buffer.
840 * The input is copied before it is scanned, so it is safe to pass
841 * it something from the token_buffer that will get overwritten
842 * (because it follows CPP_WRITTEN). This is used by do_include.
846 cpp_expand_to_buffer (pfile, buf, length)
851 register cpp_buffer *ip;
857 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
861 /* Set up the input on the input stack. */
863 buf1 = (U_CHAR *) alloca (length + 1);
864 memcpy (buf1, buf, length);
867 ip = cpp_push_buffer (pfile, buf1, length);
872 /* Scan the input, create the output. */
873 save_no_output = CPP_OPTIONS (pfile)->no_output;
874 CPP_OPTIONS (pfile)->no_output = 0;
875 cpp_scan_buffer (pfile);
876 CPP_OPTIONS (pfile)->no_output = save_no_output;
878 CPP_NUL_TERMINATE (pfile);
882 cpp_buf_line_and_col (pbuf, linep, colp)
883 register cpp_buffer *pbuf;
888 *linep = pbuf->lineno;
890 *colp = pbuf->cur - pbuf->line_base;
900 /* Return the cpp_buffer that corresponds to a file (not a macro). */
903 cpp_file_buffer (pfile)
906 cpp_buffer *ip = CPP_BUFFER (pfile);
908 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
909 if (ip->fname != NULL)
915 * write out a #line command, for instance, after an #include file.
916 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
920 output_line_command (pfile, file_change)
922 enum file_change_code file_change;
925 cpp_buffer *ip = CPP_BUFFER (pfile);
927 if (ip->fname == NULL)
930 if (CPP_OPTIONS (pfile)->no_line_commands
931 || CPP_OPTIONS (pfile)->no_output)
934 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
936 /* If the current file has not changed, we omit the #line if it would
937 appear to be a no-op, and we output a few newlines instead
938 if we want to increase the line number by a small amount.
939 We cannot do this if pfile->lineno is zero, because that means we
940 haven't output any line commands yet. (The very first line command
941 output is a `same_file' command.) */
942 if (file_change == same_file && pfile->lineno != 0)
944 if (line == pfile->lineno)
947 /* If the inherited line number is a little too small,
948 output some newlines instead of a #line command. */
949 if (line > pfile->lineno && line < pfile->lineno + 8)
951 CPP_RESERVE (pfile, 20);
952 while (line > pfile->lineno)
954 CPP_PUTC_Q (pfile, '\n');
961 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
962 CPP_PUTS_Q (pfile, "# ", 2);
964 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
965 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
967 quote_string (pfile, ip->nominal_fname);
968 if (file_change != same_file && file_change != rename_file)
970 CPP_PUTC_Q (pfile, ' ');
971 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
973 /* Tell cc1 if following text comes from a system header file. */
974 if (ip->system_header_p)
976 CPP_PUTC_Q (pfile, ' ');
977 CPP_PUTC_Q (pfile, '3');
979 #ifndef NO_IMPLICIT_EXTERN_C
980 /* Tell cc1plus if following text should be treated as C. */
981 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
983 CPP_PUTC_Q (pfile, ' ');
984 CPP_PUTC_Q (pfile, '4');
987 CPP_PUTC_Q (pfile, '\n');
988 pfile->lineno = line;
992 /* Like cpp_get_token, except that it does not read past end-of-line.
993 Also, horizontal space is skipped, and macros are popped. */
996 get_directive_token (pfile)
999 long old_written = CPP_WRITTEN (pfile);
1000 enum cpp_token token;
1004 cpp_skip_hspace (pfile);
1005 if (PEEKC () == '\n')
1008 token = cpp_get_token (pfile);
1009 /* token could be hspace at the beginning of a macro. */
1010 if (token == CPP_HSPACE || token == CPP_COMMENT)
1012 CPP_SET_WRITTEN (pfile, old_written);
1016 /* token cannot be vspace, it would have been caught above. */
1017 if (token == CPP_VSPACE)
1019 cpp_ice (pfile, "VSPACE in get_directive_token");
1023 /* token cannot be POP unless the buffer is a macro buffer. */
1024 if (token != CPP_POP)
1027 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1029 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1033 /* We must pop the buffer by hand, or else cpp_get_token might
1034 hand us white space or newline on the next invocation. */
1035 cpp_pop_buffer (pfile);
1039 /* Handle #include and #import.
1040 This function expects to see "fname" or <fname> on the input.
1042 The input is normally in part of the output_buffer following
1043 CPP_WRITTEN, and will get overwritten by output_line_command.
1044 I.e. in input file specification has been popped by handle_directive.
1048 do_include (pfile, keyword)
1050 const struct directive *keyword;
1052 int importing = (keyword->type == T_IMPORT);
1053 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1054 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1055 int before; /* included before? */
1057 unsigned char *ftok;
1060 enum cpp_token token;
1062 /* Chain of dirs to search */
1063 struct include_hash *ihash;
1064 struct file_name_list *search_start;
1066 long old_written = CPP_WRITTEN (pfile);
1070 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1073 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1075 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1078 if (importing && CPP_OPTIONS (pfile)->warn_import
1079 && !CPP_OPTIONS (pfile)->inhibit_warnings
1080 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1082 pfile->import_warning = 1;
1084 "#import is obsolete, use an #ifndef wrapper in the header file");
1087 pfile->parsing_include_directive++;
1088 token = get_directive_token (pfile);
1089 pfile->parsing_include_directive--;
1091 if (token == CPP_STRING)
1093 if (pfile->token_buffer[old_written] == '<')
1097 else if (token == CPP_NAME)
1099 /* Support '#include xyz' like VAX-C. It is taken as
1100 '#include <xyz.h>' and generates a warning. */
1102 "`#include filename' is obsolete, use `#include <filename.h>'");
1105 /* Append the missing `.h' to the name. */
1106 CPP_PUTS (pfile, ".h", 2);
1112 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1113 CPP_SET_WRITTEN (pfile, old_written);
1114 skip_rest_of_line (pfile);
1118 flen = CPP_WRITTEN (pfile) - old_written;
1119 ftok = (unsigned char *) alloca (flen + 1);
1120 memcpy (ftok, pfile->token_buffer + old_written, flen);
1123 if (get_directive_token (pfile) != CPP_VSPACE)
1125 cpp_error (pfile, "junk at end of `#include'");
1126 skip_rest_of_line (pfile);
1129 CPP_SET_WRITTEN (pfile, old_written);
1133 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1137 if (CPP_OPTIONS (pfile)->dump_includes)
1138 pass_thru_directive (ftok,
1141 - ((token == CPP_NAME) ? 2 : 0)
1146 if (token == CPP_STRING)
1156 for (fp = CPP_BUFFER (pfile);
1157 fp != CPP_NULL_BUFFER (pfile);
1158 fp = CPP_PREV_BUFFER (fp))
1159 if (fp->fname != NULL)
1162 if (fp == CPP_NULL_BUFFER (pfile))
1164 cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1168 /* For #include_next, skip in the search path past the dir in which the
1169 containing file was found. Treat files specified using an absolute path
1170 as if there are no more directories to search. Treat the primary source
1171 file like any other included source, but generate a warning. */
1172 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1174 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1175 search_start = fp->ihash->foundhere->next;
1180 cpp_warning (pfile, "#include_next in primary source file");
1183 search_start = CPP_OPTIONS (pfile)->bracket_include;
1186 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1189 search_start = fp->actual_dir;
1192 search_start = CPP_OPTIONS (pfile)->quote_include;
1198 cpp_error (pfile, "No include path in which to find %s", ftok);
1202 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1209 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1210 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1211 (pfile->system_include_depth > 0)))
1213 if (!angle_brackets)
1214 deps_output (pfile, ftok, ' ');
1218 struct file_name_list *ptr;
1219 /* If requested as a system header, assume it belongs in
1220 the first system header directory. */
1221 if (CPP_OPTIONS (pfile)->bracket_include)
1222 ptr = CPP_OPTIONS (pfile)->bracket_include;
1224 ptr = CPP_OPTIONS (pfile)->quote_include;
1226 p = (char *) alloca (strlen (ptr->name)
1227 + strlen (ftok) + 2);
1228 if (*ptr->name != '\0')
1230 strcpy (p, ptr->name);
1234 deps_output (pfile, p, ' ');
1237 /* If -M was specified, and this header file won't be added to
1238 the dependency list, then don't count this as an error,
1239 because we can still produce correct output. Otherwise, we
1240 can't produce correct output, because there may be
1241 dependencies we need inside the missing file, and we don't
1242 know what directory this missing file exists in. */
1243 else if (CPP_PRINT_DEPS (pfile)
1244 && (CPP_PRINT_DEPS (pfile)
1245 <= (angle_brackets || (pfile->system_include_depth > 0))))
1246 cpp_warning (pfile, "No include path in which to find %s", ftok);
1248 cpp_error_from_errno (pfile, ftok);
1253 /* For -M, add the file to the dependencies on its first inclusion. */
1254 if (!before && (CPP_PRINT_DEPS (pfile)
1255 > (angle_brackets || (pfile->system_include_depth > 0))))
1256 deps_output (pfile, ihash->name, ' ');
1258 /* Handle -H option. */
1259 if (CPP_OPTIONS(pfile)->print_include_names)
1261 fp = CPP_BUFFER (pfile);
1262 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1264 fprintf (stderr, " %s\n", ihash->name);
1267 /* Actually process the file */
1270 ihash->control_macro = "";
1272 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1279 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1281 if (finclude (pfile, fd, ihash))
1283 output_line_command (pfile, enter_file);
1284 pfile->only_seen_white = 2;
1290 /* Subroutine of do_line. Read next token from PFILE without adding it to
1291 the output buffer. If it is a number between 1 and 4, store it in *NUM
1292 and return 1; otherwise, return 0 and complain if we aren't at the end
1293 of the directive. */
1296 read_line_number (pfile, num)
1300 long save_written = CPP_WRITTEN (pfile);
1301 U_CHAR *p = pfile->token_buffer + save_written;
1302 enum cpp_token token = get_directive_token (pfile);
1303 CPP_SET_WRITTEN (pfile, save_written);
1305 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1312 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1313 cpp_error (pfile, "invalid format `#line' command");
1318 /* Interpret #line command.
1319 Note that the filename string (if any) is treated as if it were an
1320 include filename. That means no escape handling. */
1323 do_line (pfile, keyword)
1325 const struct directive *keyword ATTRIBUTE_UNUSED;
1327 cpp_buffer *ip = CPP_BUFFER (pfile);
1329 long old_written = CPP_WRITTEN (pfile);
1330 enum file_change_code file_change = same_file;
1331 enum cpp_token token;
1334 token = get_directive_token (pfile);
1336 if (token != CPP_NUMBER)
1338 cpp_error (pfile, "token after `#line' is not an integer");
1339 goto bad_line_directive;
1342 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1345 cpp_error (pfile, "token after `#line' is not an integer");
1346 goto bad_line_directive;
1348 CPP_SET_WRITTEN (pfile, old_written);
1350 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1351 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1353 token = get_directive_token (pfile);
1355 if (token == CPP_STRING)
1357 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1358 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1359 int action_number = 0;
1361 file_change = rename_file;
1363 if (read_line_number (pfile, &action_number))
1365 if (CPP_PEDANTIC (pfile))
1366 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1368 if (action_number == 1)
1370 file_change = enter_file;
1371 read_line_number (pfile, &action_number);
1373 else if (action_number == 2)
1375 file_change = leave_file;
1376 read_line_number (pfile, &action_number);
1378 if (action_number == 3)
1380 ip->system_header_p = 1;
1381 read_line_number (pfile, &action_number);
1383 if (action_number == 4)
1385 ip->system_header_p = 2;
1386 read_line_number (pfile, &action_number);
1392 if (strcmp (fname, ip->nominal_fname))
1394 const char *newname, *oldname;
1395 if (!strcmp (fname, ip->fname))
1396 newname = ip->fname;
1397 else if (ip->last_nominal_fname
1398 && !strcmp (fname, ip->last_nominal_fname))
1399 newname = ip->last_nominal_fname;
1401 newname = xstrdup (fname);
1403 oldname = ip->nominal_fname;
1404 ip->nominal_fname = newname;
1406 if (ip->last_nominal_fname
1407 && ip->last_nominal_fname != oldname
1408 && ip->last_nominal_fname != newname
1409 && ip->last_nominal_fname != ip->fname)
1410 free ((void *) ip->last_nominal_fname);
1412 if (newname == ip->fname)
1413 ip->last_nominal_fname = NULL;
1415 ip->last_nominal_fname = oldname;
1418 else if (token != CPP_VSPACE && token != CPP_EOF)
1420 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1421 goto bad_line_directive;
1424 /* The Newline at the end of this line remains to be processed.
1425 To put the next line at the specified line number,
1426 we must store a line number now that is one less. */
1427 ip->lineno = new_lineno - 1;
1428 CPP_SET_WRITTEN (pfile, old_written);
1429 output_line_command (pfile, file_change);
1433 skip_rest_of_line (pfile);
1434 CPP_SET_WRITTEN (pfile, old_written);
1438 /* Remove the definition of a symbol from the symbol table.
1439 According to the C standard, it is not an error to undef
1440 something that has no definitions. */
1442 do_undef (pfile, keyword)
1444 const struct directive *keyword;
1448 U_CHAR *buf, *name, *limit;
1450 long here = CPP_WRITTEN (pfile);
1451 enum cpp_token token;
1453 cpp_skip_hspace (pfile);
1455 if (! is_idstart(c))
1457 cpp_error (pfile, "token after #undef is not an identifier");
1458 skip_rest_of_line (pfile);
1462 parse_name (pfile, c);
1463 buf = pfile->token_buffer + here;
1464 limit = CPP_PWRITTEN(pfile);
1466 /* Copy out the token so we can pop the token buffer. */
1468 name = (U_CHAR *) alloca (len + 1);
1469 memcpy (name, buf, len);
1470 name[limit - buf] = '\0';
1472 token = get_directive_token (pfile);
1473 if (token != CPP_VSPACE && token != CPP_POP)
1475 cpp_pedwarn (pfile, "junk on line after #undef");
1476 skip_rest_of_line (pfile);
1479 CPP_SET_WRITTEN (pfile, here);
1481 while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1483 /* If we are generating additional info for debugging (with -g) we
1484 need to pass through all effective #undef commands. */
1485 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1486 pass_thru_directive (name, len, pfile, keyword);
1487 if (hp->type == T_POISON)
1488 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1491 if (hp->type != T_MACRO)
1492 cpp_warning (pfile, "undefining `%s'", hp->name);
1500 /* Wrap do_undef for -U processing. */
1502 cpp_undef (pfile, macro)
1506 /* Copy the string so we can append a newline. */
1507 size_t len = strlen (macro);
1508 U_CHAR *buf = alloca (len + 2);
1509 memcpy (buf, macro, len);
1511 buf[len + 1] = '\0';
1512 if (cpp_push_buffer (pfile, buf, len + 1))
1514 do_undef (pfile, NULL);
1515 cpp_pop_buffer (pfile);
1520 * Report an error detected by the program we are processing.
1521 * Use the text of the line in the error message.
1522 * (We use error because it prints the filename & line#.)
1526 do_error (pfile, keyword)
1528 const struct directive *keyword ATTRIBUTE_UNUSED;
1530 U_CHAR *text, *limit;
1532 cpp_skip_hspace (pfile);
1533 text = CPP_BUFFER (pfile)->cur;
1534 skip_rest_of_line (pfile);
1535 limit = CPP_BUFFER (pfile)->cur;
1537 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1542 * Report a warning detected by the program we are processing.
1543 * Use the text of the line in the warning message, then continue.
1547 do_warning (pfile, keyword)
1549 const struct directive *keyword ATTRIBUTE_UNUSED;
1551 U_CHAR *text, *limit;
1553 cpp_skip_hspace (pfile);
1554 text = CPP_BUFFER (pfile)->cur;
1555 skip_rest_of_line (pfile);
1556 limit = CPP_BUFFER (pfile)->cur;
1558 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1559 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1561 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1565 /* Report program identification. */
1568 do_ident (pfile, keyword)
1570 const struct directive *keyword ATTRIBUTE_UNUSED;
1572 long old_written = CPP_WRITTEN (pfile);
1574 /* Allow #ident in system headers, since that's not user's fault. */
1575 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1576 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1578 CPP_PUTS (pfile, "#ident ", 7);
1580 /* Next token should be a string constant. */
1581 if (get_directive_token (pfile) == CPP_STRING)
1582 /* And then a newline. */
1583 if (get_directive_token (pfile) == CPP_VSPACE)
1584 /* Good - ship it. */
1587 cpp_error (pfile, "invalid #ident");
1588 skip_rest_of_line (pfile);
1589 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1594 /* Pragmata handling. We handle some of these, and pass the rest on
1595 to the front end. C99 defines three pragmas and says that no macro
1596 expansion is to be performed on them; whether or not macro
1597 expansion happens for other pragmas is implementation defined.
1598 This implementation never macro-expands the text after #pragma.
1600 We currently do not support the _Pragma operator. Support for that
1601 has to be coordinated with the front end. Proposed implementation:
1602 both #pragma blah blah and _Pragma("blah blah") become
1603 __builtin_pragma(blah blah) and we teach the parser about that. */
1605 /* Sub-handlers for the pragmas needing treatment here.
1606 They return 1 if the token buffer is to be popped, 0 if not. */
1607 static int do_pragma_once PARAMS ((cpp_reader *));
1608 static int do_pragma_implementation PARAMS ((cpp_reader *));
1609 static int do_pragma_poison PARAMS ((cpp_reader *));
1610 static int do_pragma_default PARAMS ((cpp_reader *));
1613 do_pragma (pfile, keyword)
1615 const struct directive *keyword ATTRIBUTE_UNUSED;
1620 enum cpp_token token;
1622 here = CPP_WRITTEN (pfile);
1623 CPP_PUTS (pfile, "#pragma ", 8);
1625 key = CPP_WRITTEN (pfile);
1626 pfile->no_macro_expand++;
1627 token = get_directive_token (pfile);
1628 if (token != CPP_NAME)
1630 if (token == CPP_VSPACE)
1636 buf = pfile->token_buffer + key;
1637 CPP_PUTC (pfile, ' ');
1639 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1641 pop = do_pragma_once (pfile);
1642 else if (tokis ("implementation"))
1643 pop = do_pragma_implementation (pfile);
1644 else if (tokis ("poison"))
1645 pop = do_pragma_poison (pfile);
1647 pop = do_pragma_default (pfile);
1650 if (get_directive_token (pfile) != CPP_VSPACE)
1654 CPP_SET_WRITTEN (pfile, here);
1655 pfile->no_macro_expand--;
1659 cpp_error (pfile, "malformed #pragma directive");
1660 skip_rest_of_line (pfile);
1662 CPP_SET_WRITTEN (pfile, here);
1663 pfile->no_macro_expand--;
1668 do_pragma_default (pfile)
1671 while (get_directive_token (pfile) != CPP_VSPACE)
1672 CPP_PUTC (pfile, ' ');
1677 do_pragma_once (pfile)
1680 cpp_buffer *ip = CPP_BUFFER (pfile);
1682 if (ip->fname == NULL)
1684 cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1688 /* Allow #pragma once in system headers, since that's not the user's
1690 if (!ip->system_header_p)
1691 cpp_warning (pfile, "`#pragma once' is obsolete");
1693 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1694 cpp_warning (pfile, "`#pragma once' outside include file");
1696 ip->ihash->control_macro = ""; /* never repeat */
1702 do_pragma_implementation (pfile)
1705 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1706 been included yet. */
1707 struct include_hash *ptr;
1708 enum cpp_token token;
1709 long written = CPP_WRITTEN (pfile);
1713 token = get_directive_token (pfile);
1714 if (token == CPP_VSPACE)
1716 else if (token != CPP_STRING)
1718 cpp_error (pfile, "malformed #pragma implementation");
1722 name = pfile->token_buffer + written + 1;
1723 copy = xstrdup (name);
1724 copy[strlen(copy)] = '\0'; /* trim trailing quote */
1726 ptr = include_hash (pfile, copy, 0);
1729 "`#pragma implementation' for `%s' appears after file is included",
1736 do_pragma_poison (pfile)
1739 /* Poison these symbols so that all subsequent usage produces an
1745 enum cpp_token token;
1747 /* As a rule, don't include #pragma poison commands in output,
1748 unless the user asks for them. */
1749 writeit = (CPP_OPTIONS (pfile)->debug_output
1750 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1751 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1755 written = CPP_WRITTEN (pfile);
1756 token = get_directive_token (pfile);
1757 if (token == CPP_VSPACE)
1759 if (token != CPP_NAME)
1761 cpp_error (pfile, "invalid #pragma poison directive");
1762 skip_rest_of_line (pfile);
1766 p = pfile->token_buffer + written;
1768 if ((hp = cpp_lookup (pfile, p, len)))
1770 if (hp->type != T_POISON)
1772 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1773 free_definition (hp->value.defn);
1775 hp->type = T_POISON;
1779 cpp_install (pfile, p, len, T_POISON, 0);
1781 CPP_PUTC (pfile, ' ');
1786 #ifdef SCCS_DIRECTIVE
1787 /* Just ignore #sccs, on systems where we define it at all. */
1790 do_sccs (pfile, keyword)
1792 const struct directive *keyword ATTRIBUTE_UNUSED;
1794 if (CPP_PEDANTIC (pfile))
1795 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1796 skip_rest_of_line (pfile);
1801 /* We've found an `#if' directive. If the only thing before it in
1802 this file is white space, and if it is of the form
1803 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1804 for inclusion of this file. (See redundant_include_p in cppfiles.c
1805 for an explanation of controlling macros.) If so, return a
1806 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1809 detect_if_not_defined (pfile)
1812 U_CHAR *control_macro = 0;
1814 if (pfile->only_seen_white == 2)
1817 enum cpp_token token;
1820 int need_rparen = 0;
1822 /* Save state required for restore. */
1823 pfile->no_macro_expand++;
1824 parse_set_mark (pfile);
1825 base_offset = CPP_WRITTEN (pfile);
1828 if (get_directive_token (pfile) != CPP_OTHER
1829 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1830 || CPP_PWRITTEN (pfile)[-1] != '!')
1833 /* ...then `defined', */
1834 token_offset = CPP_WRITTEN (pfile);
1835 token = get_directive_token (pfile);
1836 if (token != CPP_NAME)
1838 ident = pfile->token_buffer + token_offset;
1839 CPP_NUL_TERMINATE (pfile);
1840 if (strcmp (ident, "defined"))
1843 /* ...then an optional '(' and the name, */
1844 token_offset = CPP_WRITTEN (pfile);
1845 token = get_directive_token (pfile);
1846 if (token == CPP_LPAREN)
1848 token_offset = CPP_WRITTEN (pfile);
1849 token = get_directive_token (pfile);
1850 if (token != CPP_NAME)
1854 else if (token != CPP_NAME)
1857 ident = pfile->token_buffer + token_offset;
1858 CPP_NUL_TERMINATE (pfile);
1860 /* ...then the ')', if necessary, */
1861 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1862 /* ...and make sure there's nothing else on the line. */
1863 && get_directive_token (pfile) == CPP_VSPACE)
1864 control_macro = xstrdup (ident);
1867 CPP_SET_WRITTEN (pfile, base_offset);
1868 pfile->no_macro_expand--;
1869 parse_goto_mark (pfile);
1872 return control_macro;
1876 * handle #if command by
1877 * 1) inserting special `defined' keyword into the hash table
1878 * that gets turned into 0 or 1 by special_symbol (thus,
1879 * if the luser has a symbol called `defined' already, it won't
1880 * work inside the #if command)
1881 * 2) rescan the input into a temporary output buffer
1882 * 3) pass the output buffer to the yacc parser and collect a value
1883 * 4) clean up the mess left from steps 1 and 2.
1884 * 5) call conditional_skip to skip til the next #endif (etc.),
1885 * or not, depending on the value from step 3.
1889 do_if (pfile, keyword)
1891 const struct directive *keyword ATTRIBUTE_UNUSED;
1893 U_CHAR *control_macro = detect_if_not_defined (pfile);
1894 HOST_WIDEST_INT value = eval_if_expression (pfile);
1895 conditional_skip (pfile, value == 0, T_IF, control_macro);
1900 * handle a #elif directive by not changing if_stack either.
1901 * see the comment above do_else.
1905 do_elif (pfile, keyword)
1907 const struct directive *keyword ATTRIBUTE_UNUSED;
1909 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1911 cpp_error (pfile, "`#elif' not within a conditional");
1916 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1918 cpp_error (pfile, "`#elif' after `#else'");
1919 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1920 "the conditional began here");
1922 pfile->if_stack->type = T_ELIF;
1925 if (pfile->if_stack->if_succeeded)
1926 skip_if_group (pfile);
1929 HOST_WIDEST_INT value = eval_if_expression (pfile);
1931 skip_if_group (pfile);
1934 ++pfile->if_stack->if_succeeded; /* continue processing input */
1935 output_line_command (pfile, same_file);
1942 * evaluate a #if expression in BUF, of length LENGTH,
1943 * then parse the result as a C expression and return the value as an int.
1946 static HOST_WIDEST_INT
1947 eval_if_expression (pfile)
1950 HOST_WIDEST_INT value;
1951 long old_written = CPP_WRITTEN (pfile);
1953 /* Work around bug in cpp_get_token where it may mistake an
1954 assertion for a directive. */
1955 pfile->only_seen_white = 0;
1957 value = cpp_parse_expr (pfile);
1959 skip_rest_of_line (pfile);
1960 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1966 * routine to handle ifdef/ifndef. Try to look up the symbol,
1967 * then do or don't skip to the #endif/#else/#elif depending
1968 * on what directive is actually being processed.
1972 do_xifdef (pfile, keyword)
1974 const struct directive *keyword;
1977 cpp_buffer *ip = CPP_BUFFER (pfile);
1980 enum cpp_token token;
1981 int start_of_file = 0;
1982 U_CHAR *control_macro = 0;
1983 int old_written = CPP_WRITTEN (pfile);
1985 /* Detect a #ifndef at start of file (not counting comments). */
1986 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1987 start_of_file = pfile->only_seen_white == 2;
1989 pfile->no_macro_expand++;
1990 token = get_directive_token (pfile);
1991 pfile->no_macro_expand--;
1993 ident = pfile->token_buffer + old_written;
1994 ident_length = CPP_WRITTEN (pfile) - old_written;
1995 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1997 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1999 skip = (keyword->type == T_IFDEF);
2000 if (! CPP_TRADITIONAL (pfile))
2001 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2003 else if (token == CPP_NAME)
2005 skip = cpp_defined (pfile, ident, ident_length);
2006 if (keyword->type == T_IFDEF)
2009 if (start_of_file && !skip)
2011 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2012 bcopy (ident, control_macro, ident_length + 1);
2017 skip = (keyword->type == T_IFDEF);
2018 if (! CPP_TRADITIONAL (pfile))
2019 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2022 if (!CPP_TRADITIONAL (pfile))
2024 cpp_skip_hspace (pfile);
2026 if (c != EOF && c != '\n')
2027 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2029 skip_rest_of_line (pfile);
2031 conditional_skip (pfile, skip, T_IF, control_macro);
2035 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2036 If this is a #ifndef starting at the beginning of a file,
2037 CONTROL_MACRO is the macro name tested by the #ifndef.
2038 Otherwise, CONTROL_MACRO is 0. */
2041 conditional_skip (pfile, skip, type, control_macro)
2044 enum node_type type;
2045 U_CHAR *control_macro;
2047 IF_STACK_FRAME *temp;
2049 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2050 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2051 temp->lineno = CPP_BUFFER (pfile)->lineno;
2052 temp->next = pfile->if_stack;
2053 temp->control_macro = control_macro;
2054 pfile->if_stack = temp;
2056 pfile->if_stack->type = type;
2059 skip_if_group (pfile);
2062 ++pfile->if_stack->if_succeeded;
2063 output_line_command (pfile, same_file);
2067 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2068 return 0 if skipping should continue, 1 if it should halt. Also
2069 adjusts the if_stack as appropriate.
2070 The `#' has been read, but not the identifier. */
2073 consider_directive_while_skipping (pfile, stack)
2075 IF_STACK_FRAME *stack;
2077 long ident_len, ident;
2078 const struct directive *kt;
2079 IF_STACK_FRAME *temp;
2081 cpp_skip_hspace (pfile);
2083 ident = CPP_WRITTEN (pfile);
2084 parse_name (pfile, GETC());
2085 ident_len = CPP_WRITTEN (pfile) - ident;
2087 CPP_SET_WRITTEN (pfile, ident);
2089 for (kt = directive_table; kt->length >= 0; kt++)
2090 if (kt->length == ident_len
2091 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2097 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2098 temp->next = pfile->if_stack;
2099 pfile->if_stack = temp;
2100 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2101 temp->type = kt->type;
2105 if (pfile->if_stack != stack)
2106 validate_else (pfile, "#else");
2109 if (pfile->if_stack == stack)
2113 pfile->if_stack->type = kt->type;
2118 if (pfile->if_stack != stack)
2119 validate_else (pfile, "#endif");
2121 if (pfile->if_stack == stack)
2124 temp = pfile->if_stack;
2125 pfile->if_stack = temp->next;
2133 /* Don't let erroneous code go by. */
2134 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2135 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2139 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2140 * leaves input ptr at the sharp sign found.
2143 skip_if_group (pfile)
2147 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2148 U_CHAR *beg_of_line;
2151 if (CPP_OPTIONS (pfile)->output_conditionals)
2153 CPP_PUTS (pfile, "#failed\n", 8);
2155 output_line_command (pfile, same_file);
2158 old_written = CPP_WRITTEN (pfile);
2162 beg_of_line = CPP_BUFFER (pfile)->cur;
2164 if (! CPP_TRADITIONAL (pfile))
2165 cpp_skip_hspace (pfile);
2169 if (CPP_OPTIONS (pfile)->output_conditionals)
2170 CPP_PUTC (pfile, c);
2171 CPP_BUMP_LINE (pfile);
2176 if (consider_directive_while_skipping (pfile, save_if_stack))
2180 return; /* Caller will issue error. */
2183 if (CPP_OPTIONS (pfile)->output_conditionals)
2185 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2186 copy_rest_of_line (pfile);
2190 copy_rest_of_line (pfile);
2191 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2196 return; /* Caller will issue error. */
2200 if (CPP_OPTIONS (pfile)->output_conditionals)
2202 CPP_PUTC (pfile, c);
2205 CPP_BUMP_LINE (pfile);
2209 /* Back up to the beginning of this line. Caller will process the
2211 CPP_BUFFER (pfile)->cur = beg_of_line;
2212 pfile->only_seen_white = 1;
2213 if (CPP_OPTIONS (pfile)->output_conditionals)
2215 CPP_PUTS (pfile, "#endfailed\n", 11);
2221 * handle a #else directive. Do this by just continuing processing
2222 * without changing if_stack ; this is so that the error message
2223 * for missing #endif's etc. will point to the original #if. It
2224 * is possible that something different would be better.
2228 do_else (pfile, keyword)
2230 const struct directive *keyword ATTRIBUTE_UNUSED;
2232 validate_else (pfile, "#else");
2233 skip_rest_of_line (pfile);
2235 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2237 cpp_error (pfile, "`#else' not within a conditional");
2242 /* #ifndef can't have its special treatment for containing the whole file
2243 if it has a #else clause. */
2244 pfile->if_stack->control_macro = 0;
2246 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2248 cpp_error (pfile, "`#else' after `#else'");
2249 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2250 "the conditional began here");
2252 pfile->if_stack->type = T_ELSE;
2255 if (pfile->if_stack->if_succeeded)
2256 skip_if_group (pfile);
2259 ++pfile->if_stack->if_succeeded; /* continue processing input */
2260 output_line_command (pfile, same_file);
2266 * unstack after #endif command
2270 do_endif (pfile, keyword)
2272 const struct directive *keyword ATTRIBUTE_UNUSED;
2274 validate_else (pfile, "#endif");
2275 skip_rest_of_line (pfile);
2277 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2278 cpp_error (pfile, "`#endif' not within a conditional");
2281 IF_STACK_FRAME *temp = pfile->if_stack;
2282 pfile->if_stack = temp->next;
2283 if (temp->control_macro != 0)
2285 /* This #endif matched a #ifndef at the start of the file.
2286 See if it is at the end of the file. */
2289 parse_set_mark (pfile);
2293 cpp_skip_hspace (pfile);
2298 parse_goto_mark (pfile);
2302 /* This #endif ends a #ifndef
2303 that contains all of the file (aside from whitespace).
2304 Arrange not to include the file again
2305 if the macro that was tested is defined. */
2306 struct cpp_buffer *ip;
2307 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2308 if (ip->fname != NULL)
2310 ip->ihash->control_macro = (char *) temp->control_macro;
2314 output_line_command (pfile, same_file);
2319 /* Issue -pedantic warning for text which is not a comment following
2320 an #else or #endif. Do not warn in system headers, as this is harmless
2321 and very common on old systems. */
2324 validate_else (pfile, directive)
2326 const char *directive;
2328 if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2331 cpp_skip_hspace (pfile);
2332 if (PEEKC () != '\n')
2334 "text following `%s' violates ANSI standard", directive);
2337 /* Convert T_IF, etc. to a string. Used in error messages. */
2339 if_directive_name (pfile, ifs)
2341 struct if_stack *ifs;
2345 case T_IF: return "#if";
2346 case T_IFDEF: return "#ifdef";
2347 case T_IFNDEF: return "#ifndef";
2348 case T_ELIF: return "#elif";
2349 case T_ELSE: return "#else";
2351 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2356 /* Get the next token, and add it to the text in pfile->token_buffer.
2357 Return the kind of token we got. */
2360 cpp_get_token (pfile)
2363 register int c, c2, c3;
2364 enum cpp_token token;
2365 struct cpp_options *opts = CPP_OPTIONS (pfile);
2371 if (CPP_BUFFER (pfile)->manual_pop)
2372 /* If we've been reading from redirected input, the
2373 frontend will pop the buffer. */
2375 else if (CPP_BUFFER (pfile)->seen_eof)
2377 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2380 cpp_pop_buffer (pfile);
2385 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2386 struct if_stack *ifs, *nifs;
2388 /* Unwind the conditional stack and generate error messages. */
2389 for (ifs = pfile->if_stack;
2390 ifs != CPP_BUFFER (pfile)->if_stack;
2393 cpp_error_with_line (pfile, ifs->lineno, -1,
2394 "unterminated `%s' conditional",
2395 if_directive_name (pfile, ifs));
2400 pfile->if_stack = ifs;
2402 if (CPP_BUFFER (pfile)->nominal_fname
2403 && next_buf != CPP_NULL_BUFFER (pfile))
2405 /* We're about to return from an #include file.
2406 Emit #line information now (as part of the CPP_POP) result.
2407 But the #line refers to the file we will pop to. */
2408 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2409 CPP_BUFFER (pfile) = next_buf;
2410 pfile->input_stack_listing_current = 0;
2411 output_line_command (pfile, leave_file);
2412 CPP_BUFFER (pfile) = cur_buffer;
2415 CPP_BUFFER (pfile)->seen_eof = 1;
2424 if (PEEKC () == '=')
2428 if (opts->discard_comments)
2429 c = skip_comment (pfile, c);
2431 c = copy_comment (pfile, c);
2435 /* Comments are equivalent to spaces.
2436 For -traditional, a comment is equivalent to nothing. */
2437 if (opts->traditional || !opts->discard_comments)
2441 CPP_PUTC (pfile, c);
2446 if (!pfile->only_seen_white)
2448 /* -traditional directives are recognized only with the # in
2450 XXX Layering violation. */
2451 if (CPP_TRADITIONAL (pfile)
2452 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2454 if (handle_directive (pfile))
2455 return CPP_DIRECTIVE;
2456 pfile->only_seen_white = 0;
2461 parse_string (pfile, c);
2462 pfile->only_seen_white = 0;
2463 return c == '\'' ? CPP_CHAR : CPP_STRING;
2466 if (!opts->dollars_in_ident)
2471 if (opts->cplusplus && PEEKC () == ':')
2479 if (c2 == c || c2 == '=')
2488 if (PEEKC () == '=')
2494 if (c2 == '-' && opts->chill)
2495 goto comment; /* Chill style comment */
2496 if (c2 == '-' || c2 == '=')
2500 if (opts->cplusplus && PEEKN (1) == '*')
2502 /* In C++, there's a ->* operator. */
2504 pfile->only_seen_white = 0;
2505 CPP_RESERVE (pfile, 4);
2506 CPP_PUTC_Q (pfile, c);
2507 CPP_PUTC_Q (pfile, GETC ());
2508 CPP_PUTC_Q (pfile, GETC ());
2509 CPP_NUL_TERMINATE_Q (pfile);
2517 if (pfile->parsing_include_directive)
2521 CPP_PUTC (pfile, c);
2525 if (c == '\n' || c == EOF)
2528 "missing '>' in `#include <FILENAME>'");
2533 if (!CPP_BUFFER (pfile)->has_escapes)
2535 /* Backslash newline is replaced by nothing. */
2536 CPP_ADJUST_WRITTEN (pfile, -1);
2537 CPP_BUMP_LINE (pfile);
2541 /* We might conceivably get \r- or \r<space> in
2542 here. Just delete 'em. */
2544 if (d != '-' && d != ' ')
2545 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2546 CPP_ADJUST_WRITTEN (pfile, -1);
2552 /* else fall through */
2557 /* GNU C++ supports MIN and MAX operators <? and >?. */
2558 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2561 CPP_RESERVE (pfile, 4);
2562 CPP_PUTC (pfile, c);
2563 CPP_PUTC (pfile, c2);
2566 CPP_PUTC_Q (pfile, GETC ());
2567 CPP_NUL_TERMINATE_Q (pfile);
2568 pfile->only_seen_white = 0;
2575 CPP_RESERVE(pfile, 2);
2576 CPP_PUTC_Q (pfile, '.');
2581 /* In C++ there's a .* operator. */
2582 if (opts->cplusplus && c2 == '*')
2585 if (c2 == '.' && PEEKN(1) == '.')
2587 CPP_RESERVE(pfile, 4);
2588 CPP_PUTC_Q (pfile, '.');
2589 CPP_PUTC_Q (pfile, '.');
2590 CPP_PUTC_Q (pfile, '.');
2592 CPP_NUL_TERMINATE_Q (pfile);
2593 pfile->only_seen_white = 0;
2600 pfile->only_seen_white = 0;
2601 CPP_RESERVE(pfile, 3);
2602 CPP_PUTC_Q (pfile, c);
2603 CPP_PUTC_Q (pfile, GETC ());
2604 CPP_NUL_TERMINATE_Q (pfile);
2609 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2611 CPP_PUTC (pfile, c);
2613 parse_string (pfile, c);
2614 pfile->only_seen_white = 0;
2615 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2619 case '0': case '1': case '2': case '3': case '4':
2620 case '5': case '6': case '7': case '8': case '9':
2625 CPP_RESERVE (pfile, 2);
2626 CPP_PUTC_Q (pfile, c);
2630 if (!is_idchar(c) && c != '.'
2631 && ((c2 != 'e' && c2 != 'E'
2632 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2633 || (c != '+' && c != '-')))
2638 CPP_NUL_TERMINATE_Q (pfile);
2639 pfile->only_seen_white = 0;
2641 case 'b': case 'c': case 'd': case 'h': case 'o':
2642 case 'B': case 'C': case 'D': case 'H': case 'O':
2643 if (opts->chill && PEEKC () == '\'')
2645 pfile->only_seen_white = 0;
2646 CPP_RESERVE (pfile, 2);
2647 CPP_PUTC_Q (pfile, c);
2648 CPP_PUTC_Q (pfile, '\'');
2654 goto chill_number_eof;
2657 CPP_PUTC (pfile, c);
2661 CPP_RESERVE (pfile, 2);
2662 CPP_PUTC_Q (pfile, c);
2663 CPP_NUL_TERMINATE_Q (pfile);
2670 CPP_NUL_TERMINATE (pfile);
2677 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2678 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2679 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2680 case 'x': case 'y': case 'z':
2681 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2682 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2683 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2688 unsigned char *ident;
2689 int before_name_written = CPP_WRITTEN (pfile);
2691 parse_name (pfile, c);
2692 pfile->only_seen_white = 0;
2693 if (pfile->no_macro_expand)
2695 ident = pfile->token_buffer + before_name_written;
2696 ident_len = CPP_PWRITTEN (pfile) - ident;
2697 hp = cpp_lookup (pfile, ident, ident_len);
2700 if (hp->type == T_DISABLED)
2702 if (pfile->output_escapes)
2703 { /* Return "\r-IDENT", followed by '\0'. */
2705 CPP_RESERVE (pfile, 3);
2706 ident = pfile->token_buffer + before_name_written;
2707 CPP_ADJUST_WRITTEN (pfile, 2);
2708 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2715 /* If macro wants an arglist, verify that a '(' follows. */
2716 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2718 int macbuf_whitespace = 0;
2720 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2722 U_CHAR *point = CPP_BUFFER (pfile)->cur;
2725 cpp_skip_hspace (pfile);
2732 if (point != CPP_BUFFER (pfile)->cur)
2733 macbuf_whitespace = 1;
2737 goto not_macro_call;
2738 cpp_pop_buffer (pfile);
2741 parse_set_mark (pfile);
2744 cpp_skip_hspace (pfile);
2751 parse_goto_mark (pfile);
2757 if (macbuf_whitespace)
2758 CPP_PUTC (pfile, ' ');
2762 /* This is now known to be a macro call.
2763 Expand the macro, reading arguments as needed,
2764 and push the expansion on the input stack. */
2765 macroexpand (pfile, hp);
2766 CPP_SET_WRITTEN (pfile, before_name_written);
2770 case ' ': case '\t': case '\v':
2773 CPP_PUTC (pfile, c);
2775 if (c == EOF || !is_hspace(c))
2782 if (CPP_BUFFER (pfile)->has_escapes)
2787 if (pfile->output_escapes)
2788 CPP_PUTS (pfile, "\r-", 2);
2789 parse_name (pfile, GETC ());
2794 CPP_RESERVE (pfile, 2);
2795 if (pfile->output_escapes)
2796 CPP_PUTC_Q (pfile, '\r');
2797 CPP_PUTC_Q (pfile, c);
2802 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2808 /* Backslash newline is ignored. */
2809 CPP_BUMP_LINE (pfile);
2814 CPP_PUTC (pfile, c);
2815 if (pfile->only_seen_white == 0)
2816 pfile->only_seen_white = 1;
2817 CPP_BUMP_LINE (pfile);
2818 if (! CPP_OPTIONS (pfile)->no_line_commands)
2821 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2822 output_line_command (pfile, same_file);
2826 case '(': token = CPP_LPAREN; goto char1;
2827 case ')': token = CPP_RPAREN; goto char1;
2828 case '{': token = CPP_LBRACE; goto char1;
2829 case '}': token = CPP_RBRACE; goto char1;
2830 case ',': token = CPP_COMMA; goto char1;
2831 case ';': token = CPP_SEMICOLON; goto char1;
2837 pfile->only_seen_white = 0;
2838 CPP_PUTC (pfile, c);
2844 /* Like cpp_get_token, but skip spaces and comments. */
2847 cpp_get_non_space_token (pfile)
2850 int old_written = CPP_WRITTEN (pfile);
2853 enum cpp_token token = cpp_get_token (pfile);
2854 if (token != CPP_COMMENT && token != CPP_POP
2855 && token != CPP_HSPACE && token != CPP_VSPACE)
2857 CPP_SET_WRITTEN (pfile, old_written);
2861 /* Parse an identifier starting with C. */
2864 parse_name (pfile, c)
2876 if (c == '$' && CPP_PEDANTIC (pfile))
2877 cpp_pedwarn (pfile, "`$' in identifier");
2879 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2880 CPP_PUTC_Q (pfile, c);
2885 CPP_NUL_TERMINATE_Q (pfile);
2889 /* Parse a string starting with C. A single quoted string is treated
2890 like a double -- some programs (e.g., troff) are perverse this way.
2891 (However, a single quoted string is not allowed to extend over
2894 parse_string (pfile, c)
2898 long start_line, start_column;
2900 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2902 CPP_PUTC (pfile, c);
2908 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2910 /* try harder: this string crosses a macro expansion
2911 boundary. This can happen naturally if -traditional.
2912 Otherwise, only -D can make a macro with an unmatched
2914 cpp_pop_buffer (pfile);
2918 cpp_error_with_line (pfile, start_line, start_column,
2919 "unterminated string or character constant");
2920 if (pfile->multiline_string_line != start_line
2921 && pfile->multiline_string_line != 0)
2922 cpp_error_with_line (pfile,
2923 pfile->multiline_string_line, -1,
2924 "possible real start of unterminated constant");
2925 pfile->multiline_string_line = 0;
2928 CPP_PUTC (pfile, cc);
2932 CPP_BUMP_LINE (pfile);
2935 /* In Fortran and assembly language, silently terminate
2936 strings of either variety at end of line. This is a
2937 kludge around not knowing where comments are in these
2939 if (CPP_OPTIONS (pfile)->lang_fortran
2940 || CPP_OPTIONS (pfile)->lang_asm)
2942 /* Character constants may not extend over multiple lines.
2943 In Standard C, neither may strings. We accept multiline
2944 strings as an extension. */
2947 cpp_error_with_line (pfile, start_line, start_column,
2948 "unterminated character constant");
2951 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2952 cpp_pedwarn_with_line (pfile, start_line, start_column,
2953 "string constant runs past end of line");
2954 if (pfile->multiline_string_line == 0)
2955 pfile->multiline_string_line = start_line;
2959 CPP_ADJUST_WRITTEN (pfile, -1);
2960 if (CPP_BUFFER (pfile)->has_escapes)
2962 cpp_ice (pfile, "\\r escape inside string constant");
2966 /* Backslash newline is replaced by nothing at all. */
2967 CPP_BUMP_LINE (pfile);
2973 CPP_PUTC (pfile, cc);
2985 /* Read an assertion into the token buffer, converting to
2986 canonical form: `#predicate(a n swe r)' The next non-whitespace
2987 character to read should be the first letter of the predicate.
2988 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2989 with answer (see callers for why). In case of 0, an error has been
2992 parse_assertion (pfile)
2996 cpp_skip_hspace (pfile);
2998 if (! is_idstart(c))
3000 cpp_error (pfile, "assertion predicate is not an identifier");
3003 CPP_PUTC(pfile, '#');
3005 parse_name(pfile, c);
3010 if (is_hspace(c) || c == '\r')
3011 cpp_skip_hspace (pfile);
3017 CPP_PUTC(pfile, '(');
3020 while ((c = GETC()) != ')')
3026 CPP_PUTC(pfile, ' ');
3030 else if (c == '\n' || c == EOF)
3032 if (c == '\n') FORWARD(-1);
3033 cpp_error (pfile, "un-terminated assertion answer");
3037 /* \r cannot be a macro escape here. */
3038 CPP_BUMP_LINE (pfile);
3041 CPP_PUTC (pfile, c);
3046 if (pfile->limit[-1] == ' ')
3047 pfile->limit[-1] = ')';
3048 else if (pfile->limit[-1] == '(')
3050 cpp_error (pfile, "empty token sequence in assertion");
3054 CPP_PUTC (pfile, ')');
3056 CPP_NUL_TERMINATE (pfile);
3061 do_assert (pfile, keyword)
3063 const struct directive *keyword ATTRIBUTE_UNUSED;
3067 HASHNODE *base, *this;
3068 int baselen, thislen;
3070 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3071 && !CPP_BUFFER (pfile)->system_header_p)
3072 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3074 cpp_skip_hspace (pfile);
3075 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3076 ret = parse_assertion (pfile);
3081 cpp_error (pfile, "missing token-sequence in `#assert'");
3085 cpp_skip_hspace (pfile);
3087 if (c != EOF && c != '\n')
3089 cpp_error (pfile, "junk at end of `#assert'");
3093 thislen = strlen (sym);
3094 baselen = index (sym, '(') - sym;
3095 this = cpp_lookup (pfile, sym, thislen);
3098 cpp_warning (pfile, "`%s' re-asserted", sym);
3102 base = cpp_lookup (pfile, sym, baselen);
3104 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3105 else if (base->type != T_ASSERT)
3107 /* Token clash - but with what?! */
3108 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3112 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3113 (char *)base->value.aschain);
3114 base->value.aschain = this;
3116 pfile->limit = (unsigned char *) sym; /* Pop */
3120 skip_rest_of_line (pfile);
3121 pfile->limit = (unsigned char *) sym; /* Pop */
3126 do_unassert (pfile, keyword)
3128 const struct directive *keyword ATTRIBUTE_UNUSED;
3132 long baselen, thislen;
3133 HASHNODE *base, *this, *next;
3135 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3136 && !CPP_BUFFER (pfile)->system_header_p)
3137 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3139 cpp_skip_hspace (pfile);
3141 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3142 ret = parse_assertion (pfile);
3146 cpp_skip_hspace (pfile);
3148 if (c != EOF && c != '\n')
3149 cpp_error (pfile, "junk at end of `#unassert'");
3151 thislen = strlen (sym);
3154 base = cpp_lookup (pfile, sym, thislen);
3156 goto error; /* It isn't an error to #undef what isn't #defined,
3157 so it isn't an error to #unassert what isn't
3158 #asserted either. */
3160 for (this = base->value.aschain; this; this = next)
3162 next = this->value.aschain;
3163 delete_macro (this);
3165 delete_macro (base);
3169 baselen = index (sym, '(') - sym;
3170 base = cpp_lookup (pfile, sym, baselen);
3171 if (! base) goto error;
3172 this = cpp_lookup (pfile, sym, thislen);
3173 if (! this) goto error;
3176 while (next->value.aschain != this)
3177 next = next->value.aschain;
3179 next->value.aschain = this->value.aschain;
3180 delete_macro (this);
3182 if (base->value.aschain == NULL)
3183 delete_macro (base); /* Last answer for this predicate deleted. */
3186 pfile->limit = (unsigned char *) sym; /* Pop */
3189 skip_rest_of_line (pfile);
3190 pfile->limit = (unsigned char *) sym; /* Pop */
3194 /* Process STR as if it appeared as the body of an #unassert. */
3196 cpp_unassert (pfile, str)
3200 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3202 do_assert (pfile, NULL);
3203 cpp_pop_buffer (pfile);
3208 cpp_read_check_assertion (pfile)
3213 long written = CPP_WRITTEN (pfile);
3215 FORWARD (1); /* Skip '#' */
3216 cpp_skip_hspace (pfile);
3217 if (! parse_assertion (pfile))
3221 name = pfile->token_buffer + written;
3222 result = cpp_defined (pfile, name, CPP_PWRITTEN (pfile) - name);
3225 CPP_SET_WRITTEN (pfile, written);
3229 /* Remember the current position of PFILE so it may be returned to
3230 after looking ahead a bit.
3232 Note that when you set a mark, you _must_ return to that mark. You
3233 may not forget about it and continue parsing. You may not pop a
3234 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3238 parse_set_mark (pfile)
3241 cpp_buffer *ip = CPP_BUFFER (pfile);
3242 if (ACTIVE_MARK_P())
3243 cpp_ice (pfile, "mark active in parse_set_mark");
3245 ip->mark = ip->cur - ip->buf;
3248 /* Backup the current position of PFILE to that saved in its mark,
3249 and clear the mark. */
3252 parse_goto_mark (pfile)
3255 cpp_buffer *ip = CPP_BUFFER (pfile);
3256 if (!ACTIVE_MARK_P())
3257 cpp_ice (pfile, "mark not active in parse_goto_mark");
3259 ip->cur = ip->buf + ip->mark;