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)
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 if (read_line_number (pfile, &action_number))
1363 if (CPP_PEDANTIC (pfile))
1364 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1366 if (action_number == 1)
1368 file_change = enter_file;
1369 read_line_number (pfile, &action_number);
1371 else if (action_number == 2)
1373 file_change = leave_file;
1374 read_line_number (pfile, &action_number);
1376 if (action_number == 3)
1378 ip->system_header_p = 1;
1379 read_line_number (pfile, &action_number);
1381 if (action_number == 4)
1383 ip->system_header_p = 2;
1384 read_line_number (pfile, &action_number);
1390 if (strcmp (fname, ip->nominal_fname))
1392 const char *newname, *oldname;
1393 if (!strcmp (fname, ip->fname))
1394 newname = ip->fname;
1395 else if (ip->last_nominal_fname
1396 && !strcmp (fname, ip->last_nominal_fname))
1397 newname = ip->last_nominal_fname;
1399 newname = xstrdup (fname);
1401 oldname = ip->nominal_fname;
1402 ip->nominal_fname = newname;
1404 if (ip->last_nominal_fname
1405 && ip->last_nominal_fname != oldname
1406 && ip->last_nominal_fname != newname
1407 && ip->last_nominal_fname != ip->fname)
1408 free ((void *) ip->last_nominal_fname);
1410 if (newname == ip->fname)
1411 ip->last_nominal_fname = NULL;
1413 ip->last_nominal_fname = oldname;
1416 else if (token != CPP_VSPACE && token != CPP_EOF)
1418 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1419 goto bad_line_directive;
1422 /* The Newline at the end of this line remains to be processed.
1423 To put the next line at the specified line number,
1424 we must store a line number now that is one less. */
1425 ip->lineno = new_lineno - 1;
1426 CPP_SET_WRITTEN (pfile, old_written);
1427 output_line_command (pfile, file_change);
1431 skip_rest_of_line (pfile);
1432 CPP_SET_WRITTEN (pfile, old_written);
1436 /* Remove the definition of a symbol from the symbol table.
1437 According to the C standard, it is not an error to undef
1438 something that has no definitions. */
1440 do_undef (pfile, keyword)
1442 const struct directive *keyword;
1446 U_CHAR *buf, *name, *limit;
1448 long here = CPP_WRITTEN (pfile);
1449 enum cpp_token token;
1451 cpp_skip_hspace (pfile);
1453 if (! is_idstart(c))
1455 cpp_error (pfile, "token after #undef is not an identifier");
1456 skip_rest_of_line (pfile);
1460 parse_name (pfile, c);
1461 buf = pfile->token_buffer + here;
1462 limit = CPP_PWRITTEN(pfile);
1464 /* Copy out the token so we can pop the token buffer. */
1466 name = (U_CHAR *) alloca (len + 1);
1467 memcpy (name, buf, len);
1468 name[limit - buf] = '\0';
1470 token = get_directive_token (pfile);
1471 if (token != CPP_VSPACE && token != CPP_POP)
1473 cpp_pedwarn (pfile, "junk on line after #undef");
1474 skip_rest_of_line (pfile);
1477 CPP_SET_WRITTEN (pfile, here);
1479 while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1481 /* If we are generating additional info for debugging (with -g) we
1482 need to pass through all effective #undef commands. */
1483 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1484 pass_thru_directive (name, len, pfile, keyword);
1485 if (hp->type == T_POISON)
1486 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1489 if (hp->type != T_MACRO)
1490 cpp_warning (pfile, "undefining `%s'", hp->name);
1498 /* Wrap do_undef for -U processing. */
1500 cpp_undef (pfile, macro)
1504 /* Copy the string so we can append a newline. */
1505 size_t len = strlen (macro);
1506 U_CHAR *buf = alloca (len + 2);
1507 memcpy (buf, macro, len);
1509 buf[len + 1] = '\0';
1510 if (cpp_push_buffer (pfile, buf, len + 1))
1512 do_undef (pfile, NULL);
1513 cpp_pop_buffer (pfile);
1518 * Report an error detected by the program we are processing.
1519 * Use the text of the line in the error message.
1520 * (We use error because it prints the filename & line#.)
1524 do_error (pfile, keyword)
1526 const struct directive *keyword ATTRIBUTE_UNUSED;
1528 U_CHAR *text, *limit;
1530 cpp_skip_hspace (pfile);
1531 text = CPP_BUFFER (pfile)->cur;
1532 skip_rest_of_line (pfile);
1533 limit = CPP_BUFFER (pfile)->cur;
1535 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1540 * Report a warning detected by the program we are processing.
1541 * Use the text of the line in the warning message, then continue.
1545 do_warning (pfile, keyword)
1547 const struct directive *keyword ATTRIBUTE_UNUSED;
1549 U_CHAR *text, *limit;
1551 cpp_skip_hspace (pfile);
1552 text = CPP_BUFFER (pfile)->cur;
1553 skip_rest_of_line (pfile);
1554 limit = CPP_BUFFER (pfile)->cur;
1556 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1557 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1559 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1563 /* Report program identification. */
1566 do_ident (pfile, keyword)
1568 const struct directive *keyword ATTRIBUTE_UNUSED;
1570 long old_written = CPP_WRITTEN (pfile);
1572 /* Allow #ident in system headers, since that's not user's fault. */
1573 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1574 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1576 CPP_PUTS (pfile, "#ident ", 7);
1578 /* Next token should be a string constant. */
1579 if (get_directive_token (pfile) == CPP_STRING)
1580 /* And then a newline. */
1581 if (get_directive_token (pfile) == CPP_VSPACE)
1582 /* Good - ship it. */
1585 cpp_error (pfile, "invalid #ident");
1586 skip_rest_of_line (pfile);
1587 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1592 /* Pragmata handling. We handle some of these, and pass the rest on
1593 to the front end. C99 defines three pragmas and says that no macro
1594 expansion is to be performed on them; whether or not macro
1595 expansion happens for other pragmas is implementation defined.
1596 This implementation never macro-expands the text after #pragma.
1598 We currently do not support the _Pragma operator. Support for that
1599 has to be coordinated with the front end. Proposed implementation:
1600 both #pragma blah blah and _Pragma("blah blah") become
1601 __builtin_pragma(blah blah) and we teach the parser about that. */
1603 /* Sub-handlers for the pragmas needing treatment here.
1604 They return 1 if the token buffer is to be popped, 0 if not. */
1605 static int do_pragma_once PARAMS ((cpp_reader *));
1606 static int do_pragma_implementation PARAMS ((cpp_reader *));
1607 static int do_pragma_poison PARAMS ((cpp_reader *));
1608 static int do_pragma_default PARAMS ((cpp_reader *));
1611 do_pragma (pfile, keyword)
1613 const struct directive *keyword ATTRIBUTE_UNUSED;
1618 enum cpp_token token;
1620 here = CPP_WRITTEN (pfile);
1621 CPP_PUTS (pfile, "#pragma ", 8);
1623 key = CPP_WRITTEN (pfile);
1624 pfile->no_macro_expand++;
1625 token = get_directive_token (pfile);
1626 if (token != CPP_NAME)
1628 if (token == CPP_VSPACE)
1634 buf = pfile->token_buffer + key;
1635 CPP_PUTC (pfile, ' ');
1637 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1639 pop = do_pragma_once (pfile);
1640 else if (tokis ("implementation"))
1641 pop = do_pragma_implementation (pfile);
1642 else if (tokis ("poison"))
1643 pop = do_pragma_poison (pfile);
1645 pop = do_pragma_default (pfile);
1648 if (get_directive_token (pfile) != CPP_VSPACE)
1652 CPP_SET_WRITTEN (pfile, here);
1653 pfile->no_macro_expand--;
1657 cpp_error (pfile, "malformed #pragma directive");
1658 skip_rest_of_line (pfile);
1660 CPP_SET_WRITTEN (pfile, here);
1661 pfile->no_macro_expand--;
1666 do_pragma_default (pfile)
1669 while (get_directive_token (pfile) != CPP_VSPACE)
1670 CPP_PUTC (pfile, ' ');
1675 do_pragma_once (pfile)
1678 cpp_buffer *ip = CPP_BUFFER (pfile);
1680 if (ip->fname == NULL)
1682 cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1686 /* Allow #pragma once in system headers, since that's not the user's
1688 if (!ip->system_header_p)
1689 cpp_warning (pfile, "`#pragma once' is obsolete");
1691 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1692 cpp_warning (pfile, "`#pragma once' outside include file");
1694 ip->ihash->control_macro = ""; /* never repeat */
1700 do_pragma_implementation (pfile)
1703 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1704 been included yet. */
1705 struct include_hash *ptr;
1706 enum cpp_token token;
1707 long written = CPP_WRITTEN (pfile);
1711 token = get_directive_token (pfile);
1712 if (token == CPP_VSPACE)
1714 else if (token != CPP_STRING)
1716 cpp_error (pfile, "malformed #pragma implementation");
1720 name = pfile->token_buffer + written + 1;
1721 copy = xstrdup (name);
1722 copy[strlen(copy)] = '\0'; /* trim trailing quote */
1724 ptr = include_hash (pfile, copy, 0);
1727 "`#pragma implementation' for `%s' appears after file is included",
1734 do_pragma_poison (pfile)
1737 /* Poison these symbols so that all subsequent usage produces an
1743 enum cpp_token token;
1745 /* As a rule, don't include #pragma poison commands in output,
1746 unless the user asks for them. */
1747 writeit = (CPP_OPTIONS (pfile)->debug_output
1748 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1749 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1753 written = CPP_WRITTEN (pfile);
1754 token = get_directive_token (pfile);
1755 if (token == CPP_VSPACE)
1757 if (token != CPP_NAME)
1759 cpp_error (pfile, "invalid #pragma poison directive");
1760 skip_rest_of_line (pfile);
1764 p = pfile->token_buffer + written;
1766 if ((hp = cpp_lookup (pfile, p, len)))
1768 if (hp->type != T_POISON)
1770 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1771 free_definition (hp->value.defn);
1773 hp->type = T_POISON;
1777 cpp_install (pfile, p, len, T_POISON, 0);
1779 CPP_PUTC (pfile, ' ');
1784 #ifdef SCCS_DIRECTIVE
1785 /* Just ignore #sccs, on systems where we define it at all. */
1788 do_sccs (pfile, keyword)
1790 const struct directive *keyword ATTRIBUTE_UNUSED;
1792 if (CPP_PEDANTIC (pfile))
1793 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1794 skip_rest_of_line (pfile);
1799 /* We've found an `#if' directive. If the only thing before it in
1800 this file is white space, and if it is of the form
1801 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1802 for inclusion of this file. (See redundant_include_p in cppfiles.c
1803 for an explanation of controlling macros.) If so, return a
1804 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1807 detect_if_not_defined (pfile)
1810 U_CHAR *control_macro = 0;
1812 if (pfile->only_seen_white == 2)
1815 enum cpp_token token;
1818 int need_rparen = 0;
1820 /* Save state required for restore. */
1821 pfile->no_macro_expand++;
1822 parse_set_mark (pfile);
1823 base_offset = CPP_WRITTEN (pfile);
1826 if (get_directive_token (pfile) != CPP_OTHER
1827 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1828 || CPP_PWRITTEN (pfile)[-1] != '!')
1831 /* ...then `defined', */
1832 token_offset = CPP_WRITTEN (pfile);
1833 token = get_directive_token (pfile);
1834 if (token != CPP_NAME)
1836 ident = pfile->token_buffer + token_offset;
1837 CPP_NUL_TERMINATE (pfile);
1838 if (strcmp (ident, "defined"))
1841 /* ...then an optional '(' and the name, */
1842 token_offset = CPP_WRITTEN (pfile);
1843 token = get_directive_token (pfile);
1844 if (token == CPP_LPAREN)
1846 token_offset = CPP_WRITTEN (pfile);
1847 token = get_directive_token (pfile);
1848 if (token != CPP_NAME)
1852 else if (token != CPP_NAME)
1855 ident = pfile->token_buffer + token_offset;
1856 CPP_NUL_TERMINATE (pfile);
1858 /* ...then the ')', if necessary, */
1859 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1860 /* ...and make sure there's nothing else on the line. */
1861 && get_directive_token (pfile) == CPP_VSPACE)
1862 control_macro = xstrdup (ident);
1865 CPP_SET_WRITTEN (pfile, base_offset);
1866 pfile->no_macro_expand--;
1867 parse_goto_mark (pfile);
1870 return control_macro;
1874 * handle #if command by
1875 * 1) inserting special `defined' keyword into the hash table
1876 * that gets turned into 0 or 1 by special_symbol (thus,
1877 * if the luser has a symbol called `defined' already, it won't
1878 * work inside the #if command)
1879 * 2) rescan the input into a temporary output buffer
1880 * 3) pass the output buffer to the yacc parser and collect a value
1881 * 4) clean up the mess left from steps 1 and 2.
1882 * 5) call conditional_skip to skip til the next #endif (etc.),
1883 * or not, depending on the value from step 3.
1887 do_if (pfile, keyword)
1889 const struct directive *keyword ATTRIBUTE_UNUSED;
1891 U_CHAR *control_macro = detect_if_not_defined (pfile);
1892 HOST_WIDEST_INT value = eval_if_expression (pfile);
1893 conditional_skip (pfile, value == 0, T_IF, control_macro);
1898 * handle a #elif directive by not changing if_stack either.
1899 * see the comment above do_else.
1903 do_elif (pfile, keyword)
1905 const struct directive *keyword ATTRIBUTE_UNUSED;
1907 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1909 cpp_error (pfile, "`#elif' not within a conditional");
1914 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1916 cpp_error (pfile, "`#elif' after `#else'");
1917 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1918 "the conditional began here");
1920 pfile->if_stack->type = T_ELIF;
1923 if (pfile->if_stack->if_succeeded)
1924 skip_if_group (pfile);
1927 HOST_WIDEST_INT value = eval_if_expression (pfile);
1929 skip_if_group (pfile);
1932 ++pfile->if_stack->if_succeeded; /* continue processing input */
1933 output_line_command (pfile, same_file);
1940 * evaluate a #if expression in BUF, of length LENGTH,
1941 * then parse the result as a C expression and return the value as an int.
1944 static HOST_WIDEST_INT
1945 eval_if_expression (pfile)
1948 HOST_WIDEST_INT value;
1949 long old_written = CPP_WRITTEN (pfile);
1951 /* Work around bug in cpp_get_token where it may mistake an
1952 assertion for a directive. */
1953 pfile->only_seen_white = 0;
1955 value = cpp_parse_expr (pfile);
1957 skip_rest_of_line (pfile);
1958 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1964 * routine to handle ifdef/ifndef. Try to look up the symbol,
1965 * then do or don't skip to the #endif/#else/#elif depending
1966 * on what directive is actually being processed.
1970 do_xifdef (pfile, keyword)
1972 const struct directive *keyword;
1975 cpp_buffer *ip = CPP_BUFFER (pfile);
1978 enum cpp_token token;
1979 int start_of_file = 0;
1980 U_CHAR *control_macro = 0;
1981 int old_written = CPP_WRITTEN (pfile);
1983 /* Detect a #ifndef at start of file (not counting comments). */
1984 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1985 start_of_file = pfile->only_seen_white == 2;
1987 pfile->no_macro_expand++;
1988 token = get_directive_token (pfile);
1989 pfile->no_macro_expand--;
1991 ident = pfile->token_buffer + old_written;
1992 ident_length = CPP_WRITTEN (pfile) - old_written;
1993 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1995 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1997 skip = (keyword->type == T_IFDEF);
1998 if (! CPP_TRADITIONAL (pfile))
1999 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2001 else if (token == CPP_NAME)
2003 skip = cpp_defined (pfile, ident, ident_length);
2004 if (keyword->type == T_IFDEF)
2007 if (start_of_file && !skip)
2009 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2010 bcopy (ident, control_macro, ident_length + 1);
2015 skip = (keyword->type == T_IFDEF);
2016 if (! CPP_TRADITIONAL (pfile))
2017 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2020 if (!CPP_TRADITIONAL (pfile))
2022 cpp_skip_hspace (pfile);
2024 if (c != EOF && c != '\n')
2025 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2027 skip_rest_of_line (pfile);
2029 conditional_skip (pfile, skip, T_IF, control_macro);
2033 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2034 If this is a #ifndef starting at the beginning of a file,
2035 CONTROL_MACRO is the macro name tested by the #ifndef.
2036 Otherwise, CONTROL_MACRO is 0. */
2039 conditional_skip (pfile, skip, type, control_macro)
2042 enum node_type type;
2043 U_CHAR *control_macro;
2045 IF_STACK_FRAME *temp;
2047 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2048 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2049 temp->lineno = CPP_BUFFER (pfile)->lineno;
2050 temp->next = pfile->if_stack;
2051 temp->control_macro = control_macro;
2052 pfile->if_stack = temp;
2054 pfile->if_stack->type = type;
2057 skip_if_group (pfile);
2060 ++pfile->if_stack->if_succeeded;
2061 output_line_command (pfile, same_file);
2065 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2066 return 0 if skipping should continue, 1 if it should halt. Also
2067 adjusts the if_stack as appropriate.
2068 The `#' has been read, but not the identifier. */
2071 consider_directive_while_skipping (pfile, stack)
2073 IF_STACK_FRAME *stack;
2075 long ident_len, ident;
2076 const struct directive *kt;
2077 IF_STACK_FRAME *temp;
2079 cpp_skip_hspace (pfile);
2081 ident = CPP_WRITTEN (pfile);
2082 parse_name (pfile, GETC());
2083 ident_len = CPP_WRITTEN (pfile) - ident;
2085 CPP_SET_WRITTEN (pfile, ident);
2087 for (kt = directive_table; kt->length >= 0; kt++)
2088 if (kt->length == ident_len
2089 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2095 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2096 temp->next = pfile->if_stack;
2097 pfile->if_stack = temp;
2098 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2099 temp->type = kt->type;
2103 if (pfile->if_stack != stack)
2104 validate_else (pfile, "#else");
2107 if (pfile->if_stack == stack)
2111 pfile->if_stack->type = kt->type;
2116 if (pfile->if_stack != stack)
2117 validate_else (pfile, "#endif");
2119 if (pfile->if_stack == stack)
2122 temp = pfile->if_stack;
2123 pfile->if_stack = temp->next;
2131 /* Don't let erroneous code go by. */
2132 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2133 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2137 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2138 * leaves input ptr at the sharp sign found.
2141 skip_if_group (pfile)
2145 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2146 U_CHAR *beg_of_line;
2149 if (CPP_OPTIONS (pfile)->output_conditionals)
2151 CPP_PUTS (pfile, "#failed\n", 8);
2153 output_line_command (pfile, same_file);
2156 old_written = CPP_WRITTEN (pfile);
2160 beg_of_line = CPP_BUFFER (pfile)->cur;
2162 if (! CPP_TRADITIONAL (pfile))
2163 cpp_skip_hspace (pfile);
2167 if (CPP_OPTIONS (pfile)->output_conditionals)
2168 CPP_PUTC (pfile, c);
2169 CPP_BUMP_LINE (pfile);
2174 if (consider_directive_while_skipping (pfile, save_if_stack))
2178 return; /* Caller will issue error. */
2181 if (CPP_OPTIONS (pfile)->output_conditionals)
2183 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2184 copy_rest_of_line (pfile);
2188 copy_rest_of_line (pfile);
2189 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2194 return; /* Caller will issue error. */
2198 if (CPP_OPTIONS (pfile)->output_conditionals)
2200 CPP_PUTC (pfile, c);
2203 CPP_BUMP_LINE (pfile);
2207 /* Back up to the beginning of this line. Caller will process the
2209 CPP_BUFFER (pfile)->cur = beg_of_line;
2210 pfile->only_seen_white = 1;
2211 if (CPP_OPTIONS (pfile)->output_conditionals)
2213 CPP_PUTS (pfile, "#endfailed\n", 11);
2219 * handle a #else directive. Do this by just continuing processing
2220 * without changing if_stack ; this is so that the error message
2221 * for missing #endif's etc. will point to the original #if. It
2222 * is possible that something different would be better.
2226 do_else (pfile, keyword)
2228 const struct directive *keyword ATTRIBUTE_UNUSED;
2230 validate_else (pfile, "#else");
2231 skip_rest_of_line (pfile);
2233 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2235 cpp_error (pfile, "`#else' not within a conditional");
2240 /* #ifndef can't have its special treatment for containing the whole file
2241 if it has a #else clause. */
2242 pfile->if_stack->control_macro = 0;
2244 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2246 cpp_error (pfile, "`#else' after `#else'");
2247 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2248 "the conditional began here");
2250 pfile->if_stack->type = T_ELSE;
2253 if (pfile->if_stack->if_succeeded)
2254 skip_if_group (pfile);
2257 ++pfile->if_stack->if_succeeded; /* continue processing input */
2258 output_line_command (pfile, same_file);
2264 * unstack after #endif command
2268 do_endif (pfile, keyword)
2270 const struct directive *keyword ATTRIBUTE_UNUSED;
2272 validate_else (pfile, "#endif");
2273 skip_rest_of_line (pfile);
2275 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2276 cpp_error (pfile, "`#endif' not within a conditional");
2279 IF_STACK_FRAME *temp = pfile->if_stack;
2280 pfile->if_stack = temp->next;
2281 if (temp->control_macro != 0)
2283 /* This #endif matched a #ifndef at the start of the file.
2284 See if it is at the end of the file. */
2287 parse_set_mark (pfile);
2291 cpp_skip_hspace (pfile);
2296 parse_goto_mark (pfile);
2300 /* This #endif ends a #ifndef
2301 that contains all of the file (aside from whitespace).
2302 Arrange not to include the file again
2303 if the macro that was tested is defined. */
2304 struct cpp_buffer *ip;
2305 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2306 if (ip->fname != NULL)
2308 ip->ihash->control_macro = (char *) temp->control_macro;
2312 output_line_command (pfile, same_file);
2317 /* Issue -pedantic warning for text which is not a comment following
2318 an #else or #endif. Do not warn in system headers, as this is harmless
2319 and very common on old systems. */
2322 validate_else (pfile, directive)
2324 const char *directive;
2326 if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2329 cpp_skip_hspace (pfile);
2330 if (PEEKC () != '\n')
2332 "text following `%s' violates ANSI standard", directive);
2335 /* Convert T_IF, etc. to a string. Used in error messages. */
2337 if_directive_name (pfile, ifs)
2339 struct if_stack *ifs;
2343 case T_IF: return "#if";
2344 case T_IFDEF: return "#ifdef";
2345 case T_IFNDEF: return "#ifndef";
2346 case T_ELIF: return "#elif";
2347 case T_ELSE: return "#else";
2349 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2354 /* Get the next token, and add it to the text in pfile->token_buffer.
2355 Return the kind of token we got. */
2358 cpp_get_token (pfile)
2361 register int c, c2, c3;
2362 enum cpp_token token;
2363 struct cpp_options *opts = CPP_OPTIONS (pfile);
2369 if (CPP_BUFFER (pfile)->manual_pop)
2370 /* If we've been reading from redirected input, the
2371 frontend will pop the buffer. */
2373 else if (CPP_BUFFER (pfile)->seen_eof)
2375 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2378 cpp_pop_buffer (pfile);
2383 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2384 struct if_stack *ifs, *nifs;
2386 /* Unwind the conditional stack and generate error messages. */
2387 for (ifs = pfile->if_stack;
2388 ifs != CPP_BUFFER (pfile)->if_stack;
2391 cpp_error_with_line (pfile, ifs->lineno, -1,
2392 "unterminated `%s' conditional",
2393 if_directive_name (pfile, ifs));
2398 pfile->if_stack = ifs;
2400 if (CPP_BUFFER (pfile)->nominal_fname
2401 && next_buf != CPP_NULL_BUFFER (pfile))
2403 /* We're about to return from an #include file.
2404 Emit #line information now (as part of the CPP_POP) result.
2405 But the #line refers to the file we will pop to. */
2406 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2407 CPP_BUFFER (pfile) = next_buf;
2408 pfile->input_stack_listing_current = 0;
2409 output_line_command (pfile, leave_file);
2410 CPP_BUFFER (pfile) = cur_buffer;
2413 CPP_BUFFER (pfile)->seen_eof = 1;
2422 if (PEEKC () == '=')
2426 if (opts->discard_comments)
2427 c = skip_comment (pfile, c);
2429 c = copy_comment (pfile, c);
2433 /* Comments are equivalent to spaces.
2434 For -traditional, a comment is equivalent to nothing. */
2435 if (opts->traditional || !opts->discard_comments)
2439 CPP_PUTC (pfile, c);
2444 if (!pfile->only_seen_white)
2446 /* -traditional directives are recognized only with the # in
2448 XXX Layering violation. */
2449 if (CPP_TRADITIONAL (pfile)
2450 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2452 if (handle_directive (pfile))
2453 return CPP_DIRECTIVE;
2454 pfile->only_seen_white = 0;
2459 parse_string (pfile, c);
2460 pfile->only_seen_white = 0;
2461 return c == '\'' ? CPP_CHAR : CPP_STRING;
2464 if (!opts->dollars_in_ident)
2469 if (opts->cplusplus && PEEKC () == ':')
2477 if (c2 == c || c2 == '=')
2486 if (PEEKC () == '=')
2492 if (c2 == '-' && opts->chill)
2493 goto comment; /* Chill style comment */
2494 if (c2 == '-' || c2 == '=')
2498 if (opts->cplusplus && PEEKN (1) == '*')
2500 /* In C++, there's a ->* operator. */
2502 pfile->only_seen_white = 0;
2503 CPP_RESERVE (pfile, 4);
2504 CPP_PUTC_Q (pfile, c);
2505 CPP_PUTC_Q (pfile, GETC ());
2506 CPP_PUTC_Q (pfile, GETC ());
2507 CPP_NUL_TERMINATE_Q (pfile);
2515 if (pfile->parsing_include_directive)
2519 CPP_PUTC (pfile, c);
2523 if (c == '\n' || c == EOF)
2526 "missing '>' in `#include <FILENAME>'");
2531 if (!CPP_BUFFER (pfile)->has_escapes)
2533 /* Backslash newline is replaced by nothing. */
2534 CPP_ADJUST_WRITTEN (pfile, -1);
2535 CPP_BUMP_LINE (pfile);
2539 /* We might conceivably get \r- or \r<space> in
2540 here. Just delete 'em. */
2542 if (d != '-' && d != ' ')
2543 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2544 CPP_ADJUST_WRITTEN (pfile, -1);
2550 /* else fall through */
2555 /* GNU C++ supports MIN and MAX operators <? and >?. */
2556 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2559 CPP_RESERVE (pfile, 4);
2560 CPP_PUTC (pfile, c);
2561 CPP_PUTC (pfile, c2);
2564 CPP_PUTC_Q (pfile, GETC ());
2565 CPP_NUL_TERMINATE_Q (pfile);
2566 pfile->only_seen_white = 0;
2573 CPP_RESERVE(pfile, 2);
2574 CPP_PUTC_Q (pfile, '.');
2579 /* In C++ there's a .* operator. */
2580 if (opts->cplusplus && c2 == '*')
2583 if (c2 == '.' && PEEKN(1) == '.')
2585 CPP_RESERVE(pfile, 4);
2586 CPP_PUTC_Q (pfile, '.');
2587 CPP_PUTC_Q (pfile, '.');
2588 CPP_PUTC_Q (pfile, '.');
2590 CPP_NUL_TERMINATE_Q (pfile);
2591 pfile->only_seen_white = 0;
2598 pfile->only_seen_white = 0;
2599 CPP_RESERVE(pfile, 3);
2600 CPP_PUTC_Q (pfile, c);
2601 CPP_PUTC_Q (pfile, GETC ());
2602 CPP_NUL_TERMINATE_Q (pfile);
2607 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2609 CPP_PUTC (pfile, c);
2611 parse_string (pfile, c);
2612 pfile->only_seen_white = 0;
2613 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2617 case '0': case '1': case '2': case '3': case '4':
2618 case '5': case '6': case '7': case '8': case '9':
2623 CPP_RESERVE (pfile, 2);
2624 CPP_PUTC_Q (pfile, c);
2628 if (!is_idchar(c) && c != '.'
2629 && ((c2 != 'e' && c2 != 'E'
2630 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2631 || (c != '+' && c != '-')))
2636 CPP_NUL_TERMINATE_Q (pfile);
2637 pfile->only_seen_white = 0;
2639 case 'b': case 'c': case 'd': case 'h': case 'o':
2640 case 'B': case 'C': case 'D': case 'H': case 'O':
2641 if (opts->chill && PEEKC () == '\'')
2643 pfile->only_seen_white = 0;
2644 CPP_RESERVE (pfile, 2);
2645 CPP_PUTC_Q (pfile, c);
2646 CPP_PUTC_Q (pfile, '\'');
2652 goto chill_number_eof;
2655 CPP_PUTC (pfile, c);
2659 CPP_RESERVE (pfile, 2);
2660 CPP_PUTC_Q (pfile, c);
2661 CPP_NUL_TERMINATE_Q (pfile);
2668 CPP_NUL_TERMINATE (pfile);
2675 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2676 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2677 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2678 case 'x': case 'y': case 'z':
2679 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2680 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2681 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2686 unsigned char *ident;
2687 int before_name_written = CPP_WRITTEN (pfile);
2689 parse_name (pfile, c);
2690 pfile->only_seen_white = 0;
2691 if (pfile->no_macro_expand)
2693 ident = pfile->token_buffer + before_name_written;
2694 ident_len = CPP_PWRITTEN (pfile) - ident;
2695 hp = cpp_lookup (pfile, ident, ident_len);
2698 if (hp->type == T_DISABLED)
2700 if (pfile->output_escapes)
2701 { /* Return "\r-IDENT", followed by '\0'. */
2703 CPP_RESERVE (pfile, 3);
2704 ident = pfile->token_buffer + before_name_written;
2705 CPP_ADJUST_WRITTEN (pfile, 2);
2706 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2713 /* If macro wants an arglist, verify that a '(' follows. */
2714 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2716 int macbuf_whitespace = 0;
2718 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2720 U_CHAR *point = CPP_BUFFER (pfile)->cur;
2723 cpp_skip_hspace (pfile);
2730 if (point != CPP_BUFFER (pfile)->cur)
2731 macbuf_whitespace = 1;
2735 goto not_macro_call;
2736 cpp_pop_buffer (pfile);
2739 parse_set_mark (pfile);
2742 cpp_skip_hspace (pfile);
2749 parse_goto_mark (pfile);
2755 if (macbuf_whitespace)
2756 CPP_PUTC (pfile, ' ');
2760 /* This is now known to be a macro call.
2761 Expand the macro, reading arguments as needed,
2762 and push the expansion on the input stack. */
2763 macroexpand (pfile, hp);
2764 CPP_SET_WRITTEN (pfile, before_name_written);
2768 case ' ': case '\t': case '\v':
2771 CPP_PUTC (pfile, c);
2773 if (c == EOF || !is_hspace(c))
2780 if (CPP_BUFFER (pfile)->has_escapes)
2785 if (pfile->output_escapes)
2786 CPP_PUTS (pfile, "\r-", 2);
2787 parse_name (pfile, GETC ());
2792 CPP_RESERVE (pfile, 2);
2793 if (pfile->output_escapes)
2794 CPP_PUTC_Q (pfile, '\r');
2795 CPP_PUTC_Q (pfile, c);
2800 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2806 /* Backslash newline is ignored. */
2807 CPP_BUMP_LINE (pfile);
2812 CPP_PUTC (pfile, c);
2813 if (pfile->only_seen_white == 0)
2814 pfile->only_seen_white = 1;
2815 CPP_BUMP_LINE (pfile);
2816 if (! CPP_OPTIONS (pfile)->no_line_commands)
2819 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2820 output_line_command (pfile, same_file);
2824 case '(': token = CPP_LPAREN; goto char1;
2825 case ')': token = CPP_RPAREN; goto char1;
2826 case '{': token = CPP_LBRACE; goto char1;
2827 case '}': token = CPP_RBRACE; goto char1;
2828 case ',': token = CPP_COMMA; goto char1;
2829 case ';': token = CPP_SEMICOLON; goto char1;
2835 pfile->only_seen_white = 0;
2836 CPP_PUTC (pfile, c);
2842 /* Like cpp_get_token, but skip spaces and comments. */
2845 cpp_get_non_space_token (pfile)
2848 int old_written = CPP_WRITTEN (pfile);
2851 enum cpp_token token = cpp_get_token (pfile);
2852 if (token != CPP_COMMENT && token != CPP_POP
2853 && token != CPP_HSPACE && token != CPP_VSPACE)
2855 CPP_SET_WRITTEN (pfile, old_written);
2859 /* Parse an identifier starting with C. */
2862 parse_name (pfile, c)
2874 if (c == '$' && CPP_PEDANTIC (pfile))
2875 cpp_pedwarn (pfile, "`$' in identifier");
2877 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2878 CPP_PUTC_Q (pfile, c);
2883 CPP_NUL_TERMINATE_Q (pfile);
2887 /* Parse a string starting with C. A single quoted string is treated
2888 like a double -- some programs (e.g., troff) are perverse this way.
2889 (However, a single quoted string is not allowed to extend over
2892 parse_string (pfile, c)
2896 long start_line, start_column;
2898 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2900 CPP_PUTC (pfile, c);
2906 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2908 /* try harder: this string crosses a macro expansion
2909 boundary. This can happen naturally if -traditional.
2910 Otherwise, only -D can make a macro with an unmatched
2912 cpp_pop_buffer (pfile);
2916 cpp_error_with_line (pfile, start_line, start_column,
2917 "unterminated string or character constant");
2918 if (pfile->multiline_string_line != start_line
2919 && pfile->multiline_string_line != 0)
2920 cpp_error_with_line (pfile,
2921 pfile->multiline_string_line, -1,
2922 "possible real start of unterminated constant");
2923 pfile->multiline_string_line = 0;
2926 CPP_PUTC (pfile, cc);
2930 CPP_BUMP_LINE (pfile);
2933 /* In Fortran and assembly language, silently terminate
2934 strings of either variety at end of line. This is a
2935 kludge around not knowing where comments are in these
2937 if (CPP_OPTIONS (pfile)->lang_fortran
2938 || CPP_OPTIONS (pfile)->lang_asm)
2940 /* Character constants may not extend over multiple lines.
2941 In Standard C, neither may strings. We accept multiline
2942 strings as an extension. */
2945 cpp_error_with_line (pfile, start_line, start_column,
2946 "unterminated character constant");
2949 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2950 cpp_pedwarn_with_line (pfile, start_line, start_column,
2951 "string constant runs past end of line");
2952 if (pfile->multiline_string_line == 0)
2953 pfile->multiline_string_line = start_line;
2957 CPP_ADJUST_WRITTEN (pfile, -1);
2958 if (CPP_BUFFER (pfile)->has_escapes)
2960 cpp_ice (pfile, "\\r escape inside string constant");
2964 /* Backslash newline is replaced by nothing at all. */
2965 CPP_BUMP_LINE (pfile);
2971 CPP_PUTC (pfile, cc);
2983 /* Read an assertion into the token buffer, converting to
2984 canonical form: `#predicate(a n swe r)' The next non-whitespace
2985 character to read should be the first letter of the predicate.
2986 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2987 with answer (see callers for why). In case of 0, an error has been
2990 parse_assertion (pfile)
2994 cpp_skip_hspace (pfile);
2996 if (! is_idstart(c))
2998 cpp_error (pfile, "assertion predicate is not an identifier");
3001 CPP_PUTC(pfile, '#');
3003 parse_name(pfile, c);
3008 if (is_hspace(c) || c == '\r')
3009 cpp_skip_hspace (pfile);
3015 CPP_PUTC(pfile, '(');
3018 while ((c = GETC()) != ')')
3024 CPP_PUTC(pfile, ' ');
3028 else if (c == '\n' || c == EOF)
3030 if (c == '\n') FORWARD(-1);
3031 cpp_error (pfile, "un-terminated assertion answer");
3035 /* \r cannot be a macro escape here. */
3036 CPP_BUMP_LINE (pfile);
3039 CPP_PUTC (pfile, c);
3044 if (pfile->limit[-1] == ' ')
3045 pfile->limit[-1] = ')';
3046 else if (pfile->limit[-1] == '(')
3048 cpp_error (pfile, "empty token sequence in assertion");
3052 CPP_PUTC (pfile, ')');
3054 CPP_NUL_TERMINATE (pfile);
3059 do_assert (pfile, keyword)
3061 const struct directive *keyword ATTRIBUTE_UNUSED;
3065 HASHNODE *base, *this;
3066 int baselen, thislen;
3068 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3069 && !CPP_BUFFER (pfile)->system_header_p)
3070 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3072 cpp_skip_hspace (pfile);
3073 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3074 ret = parse_assertion (pfile);
3079 cpp_error (pfile, "missing token-sequence in `#assert'");
3083 cpp_skip_hspace (pfile);
3085 if (c != EOF && c != '\n')
3087 cpp_error (pfile, "junk at end of `#assert'");
3091 thislen = strlen (sym);
3092 baselen = index (sym, '(') - sym;
3093 this = cpp_lookup (pfile, sym, thislen);
3096 cpp_warning (pfile, "`%s' re-asserted", sym);
3100 base = cpp_lookup (pfile, sym, baselen);
3102 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3103 else if (base->type != T_ASSERT)
3105 /* Token clash - but with what?! */
3106 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3110 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3111 (char *)base->value.aschain);
3112 base->value.aschain = this;
3114 pfile->limit = (unsigned char *) sym; /* Pop */
3118 skip_rest_of_line (pfile);
3119 pfile->limit = (unsigned char *) sym; /* Pop */
3124 do_unassert (pfile, keyword)
3126 const struct directive *keyword ATTRIBUTE_UNUSED;
3130 long baselen, thislen;
3131 HASHNODE *base, *this, *next;
3133 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3134 && !CPP_BUFFER (pfile)->system_header_p)
3135 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3137 cpp_skip_hspace (pfile);
3139 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3140 ret = parse_assertion (pfile);
3144 cpp_skip_hspace (pfile);
3146 if (c != EOF && c != '\n')
3147 cpp_error (pfile, "junk at end of `#unassert'");
3149 thislen = strlen (sym);
3152 base = cpp_lookup (pfile, sym, thislen);
3154 goto error; /* It isn't an error to #undef what isn't #defined,
3155 so it isn't an error to #unassert what isn't
3156 #asserted either. */
3158 for (this = base->value.aschain; this; this = next)
3160 next = this->value.aschain;
3161 delete_macro (this);
3163 delete_macro (base);
3167 baselen = index (sym, '(') - sym;
3168 base = cpp_lookup (pfile, sym, baselen);
3169 if (! base) goto error;
3170 this = cpp_lookup (pfile, sym, thislen);
3171 if (! this) goto error;
3174 while (next->value.aschain != this)
3175 next = next->value.aschain;
3177 next->value.aschain = this->value.aschain;
3178 delete_macro (this);
3180 if (base->value.aschain == NULL)
3181 delete_macro (base); /* Last answer for this predicate deleted. */
3184 pfile->limit = (unsigned char *) sym; /* Pop */
3187 skip_rest_of_line (pfile);
3188 pfile->limit = (unsigned char *) sym; /* Pop */
3192 /* Process STR as if it appeared as the body of an #unassert. */
3194 cpp_unassert (pfile, str)
3198 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3200 do_assert (pfile, NULL);
3201 cpp_pop_buffer (pfile);
3206 cpp_read_check_assertion (pfile)
3211 long written = CPP_WRITTEN (pfile);
3213 FORWARD (1); /* Skip '#' */
3214 cpp_skip_hspace (pfile);
3215 if (! parse_assertion (pfile))
3219 name = pfile->token_buffer + written;
3220 result = cpp_defined (pfile, name, CPP_PWRITTEN (pfile) - name);
3223 CPP_SET_WRITTEN (pfile, written);
3227 /* Remember the current position of PFILE so it may be returned to
3228 after looking ahead a bit.
3230 Note that when you set a mark, you _must_ return to that mark. You
3231 may not forget about it and continue parsing. You may not pop a
3232 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3236 parse_set_mark (pfile)
3239 cpp_buffer *ip = CPP_BUFFER (pfile);
3240 if (ACTIVE_MARK_P())
3241 cpp_ice (pfile, "mark active in parse_set_mark");
3243 ip->mark = ip->cur - ip->buf;
3246 /* Backup the current position of PFILE to that saved in its mark,
3247 and clear the mark. */
3250 parse_goto_mark (pfile)
3253 cpp_buffer *ip = CPP_BUFFER (pfile);
3254 if (!ACTIVE_MARK_P())
3255 cpp_ice (pfile, "mark not active in parse_goto_mark");
3257 ip->cur = ip->buf + ip->mark;