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. */
51 int length; /* Length of name */
52 int (*func) /* Function to handle directive */
53 PARAMS ((cpp_reader *, const struct directive *));
54 const char *name; /* Name of directive */
55 enum node_type type; /* Code which describes which directive. */
58 /* These functions are declared to return int instead of void since they
59 are going to be placed in a table and some old compilers have trouble with
60 pointers to functions returning void. */
62 static int do_define PARAMS ((cpp_reader *, const struct directive *));
63 static int do_line PARAMS ((cpp_reader *, const struct directive *));
64 static int do_include PARAMS ((cpp_reader *, const struct directive *));
65 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
66 static int do_error PARAMS ((cpp_reader *, const struct directive *));
67 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
68 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
69 static int do_if PARAMS ((cpp_reader *, const struct directive *));
70 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
71 static int do_else PARAMS ((cpp_reader *, const struct directive *));
72 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
73 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
75 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
77 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
78 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
79 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
81 /* Forward declarations. */
83 static void validate_else PARAMS ((cpp_reader *, const char *));
84 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
85 static void conditional_skip PARAMS ((cpp_reader *, int,
86 enum node_type, U_CHAR *));
87 static void skip_if_group PARAMS ((cpp_reader *));
88 static void parse_name PARAMS ((cpp_reader *, int));
89 static void parse_string PARAMS ((cpp_reader *, int));
90 static int parse_assertion PARAMS ((cpp_reader *));
91 static const char *if_directive_name PARAMS ((cpp_reader *,
93 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
94 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
95 static int skip_comment PARAMS ((cpp_reader *, int));
96 static int copy_comment PARAMS ((cpp_reader *, int));
97 static void skip_string PARAMS ((cpp_reader *, int));
98 static void skip_rest_of_line PARAMS ((cpp_reader *));
99 static void cpp_skip_hspace PARAMS ((cpp_reader *));
100 static int handle_directive PARAMS ((cpp_reader *));
101 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
103 const struct directive *));
104 static int read_line_number PARAMS ((cpp_reader *, int *));
105 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
106 static int consider_directive_while_skipping PARAMS ((cpp_reader *,
108 static void skip_block_comment PARAMS ((cpp_reader *));
109 static void skip_line_comment PARAMS ((cpp_reader *));
110 static void parse_set_mark PARAMS ((cpp_reader *));
111 static void parse_goto_mark PARAMS ((cpp_reader *));
112 static int get_macro_name PARAMS ((cpp_reader *));
114 /* Here is the actual list of #-directives.
115 This table is ordered by frequency of occurrence; the numbers
116 at the end are directive counts from all the source code I have
117 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
118 linux-2.2.9, and pcmcia-cs-3.0.9). */
120 static const struct directive directive_table[] = {
122 { 6, do_define, "define", T_DEFINE }, /* 270554 */
123 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
124 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
125 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
126 { 2, do_if, "if", T_IF }, /* 18162 */
127 { 4, do_else, "else", T_ELSE }, /* 9863 */
128 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
129 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
130 { 4, do_line, "line", T_LINE }, /* 2465 */
131 { 4, do_elif, "elif", T_ELIF }, /* 610 */
132 { 5, do_error, "error", T_ERROR }, /* 475 */
133 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
135 /* Extensions. All deprecated except #warning and #include_next. */
136 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
137 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
138 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
139 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
140 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
141 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
142 #ifdef SCCS_DIRECTIVE
143 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
145 { -1, 0, "", T_UNUSED }
148 /* Place into PFILE a quoted string representing the string SRC.
149 Caller must reserve enough space in pfile->token_buffer. */
152 quote_string (pfile, src)
158 CPP_PUTC_Q (pfile, '\"');
160 switch ((c = *src++))
164 CPP_PUTC_Q (pfile, c);
167 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
168 CPP_ADJUST_WRITTEN (pfile, 4);
174 CPP_PUTC_Q (pfile, '\\');
175 CPP_PUTC_Q (pfile, c);
179 CPP_PUTC_Q (pfile, '\"');
180 CPP_NUL_TERMINATE_Q (pfile);
185 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
188 cpp_grow_buffer (pfile, n)
192 long old_written = CPP_WRITTEN (pfile);
193 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
194 pfile->token_buffer = (U_CHAR *)
195 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
196 CPP_SET_WRITTEN (pfile, old_written);
199 /* Process the string STR as if it appeared as the body of a #define.
200 If STR is just an identifier, define it with value 1.
201 If STR has anything after the identifier, then it should
202 be identifier=definition. */
205 cpp_define (pfile, str)
212 p = strchr (str, '=');
213 /* Copy the entire option so we can modify it.
214 Change the first "=" in the string to a space. If there is none,
215 tack " 1" on the end. Then add a newline and a NUL. */
219 count = strlen (str) + 2;
220 buf = (U_CHAR *) alloca (count);
221 memcpy (buf, str, count - 2);
223 buf[count - 2] = '\n';
224 buf[count - 1] = '\0';
228 count = strlen (str) + 4;
229 buf = (U_CHAR *) alloca (count);
230 memcpy (buf, str, count - 4);
231 strcpy (&buf[count-4], " 1\n");
234 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
236 do_define (pfile, NULL);
237 cpp_pop_buffer (pfile);
241 /* Process the string STR as if it appeared as the body of a #assert. */
243 cpp_assert (pfile, str)
247 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
249 do_assert (pfile, NULL);
250 cpp_pop_buffer (pfile);
254 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
256 cpp_defined (pfile, id, len)
261 HASHNODE *hp = cpp_lookup (pfile, id, len);
262 if (hp && hp->type == T_POISON)
264 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
270 static enum cpp_token
271 null_underflow (pfile)
272 cpp_reader *pfile ATTRIBUTE_UNUSED;
278 null_cleanup (pbuf, pfile)
279 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
280 cpp_reader *pfile ATTRIBUTE_UNUSED;
285 /* Skip a C-style block comment. We know it's a comment, and point is
286 at the second character of the starter. */
288 skip_block_comment (pfile)
295 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
301 cpp_error_with_line (pfile, line, col, "unterminated comment");
304 else if (c == '\n' || c == '\r')
306 /* \r cannot be a macro escape marker here. */
307 if (!ACTIVE_MARK_P())
308 CPP_BUMP_LINE (pfile);
310 else if (c == '/' && prev_c == '*')
312 else if (c == '*' && prev_c == '/'
313 && CPP_OPTIONS (pfile)->warn_comments)
314 cpp_warning (pfile, "`/*' within comment");
320 /* Skip a C++/Chill line comment. We know it's a comment, and point
321 is at the second character of the initiator. */
323 skip_line_comment (pfile)
331 /* We don't have to worry about EOF in here. */
334 /* Don't consider final '\n' to be part of comment. */
340 /* \r cannot be a macro escape marker here. */
341 if (!ACTIVE_MARK_P())
342 CPP_BUMP_LINE (pfile);
343 if (CPP_OPTIONS (pfile)->warn_comments)
344 cpp_warning (pfile, "backslash-newline within line comment");
349 /* Skip a comment - C, C++, or Chill style. M is the first character
350 of the comment marker. If this really is a comment, skip to its
351 end and return ' '. If this is not a comment, return M (which will
355 skip_comment (pfile, m)
359 if (m == '/' && PEEKC() == '*')
361 skip_block_comment (pfile);
364 else if (m == '/' && PEEKC() == '/')
366 if (CPP_BUFFER (pfile)->system_header_p)
368 /* We silently allow C++ comments in system headers, irrespective
369 of conformance mode, because lots of busted systems do that
370 and trying to clean it up in fixincludes is a nightmare. */
371 skip_line_comment (pfile);
374 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
376 if (CPP_OPTIONS (pfile)->c89
377 && CPP_PEDANTIC (pfile)
378 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
381 "C++ style comments are not allowed in ISO C89");
383 "(this will be reported only once per input file)");
384 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
386 skip_line_comment (pfile);
392 else if (m == '-' && PEEKC() == '-'
393 && CPP_OPTIONS (pfile)->chill)
395 skip_line_comment (pfile);
402 /* Identical to skip_comment except that it copies the comment into the
403 token_buffer. This is used if !discard_comments. */
405 copy_comment (pfile, m)
409 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
412 if (skip_comment (pfile, m) == m)
415 limit = CPP_BUFFER (pfile)->cur;
416 CPP_RESERVE (pfile, limit - start + 2);
417 CPP_PUTC_Q (pfile, m);
418 for (; start <= limit; start++)
420 CPP_PUTC_Q (pfile, *start);
425 /* Skip whitespace \-newline and comments. Does not macro-expand. */
428 cpp_skip_hspace (pfile)
437 else if (is_hspace(c))
439 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
440 cpp_pedwarn (pfile, "%s in preprocessing directive",
441 c == '\f' ? "formfeed" : "vertical tab");
445 /* \r is a backslash-newline marker if !has_escapes, and
446 a deletable-whitespace or no-reexpansion marker otherwise. */
447 if (CPP_BUFFER (pfile)->has_escapes)
455 CPP_BUMP_LINE (pfile);
457 else if (c == '/' || c == '-')
459 c = skip_comment (pfile, c);
469 /* Read and discard the rest of the current line. */
472 skip_rest_of_line (pfile)
486 if (! CPP_BUFFER (pfile)->has_escapes)
487 CPP_BUMP_LINE (pfile);
492 skip_string (pfile, c);
497 skip_comment (pfile, c);
502 if (CPP_PEDANTIC (pfile))
503 cpp_pedwarn (pfile, "%s in preprocessing directive",
504 c == '\f' ? "formfeed" : "vertical tab");
511 /* Handle a possible # directive.
512 '#' has already been read. */
515 handle_directive (pfile)
519 register const struct directive *kt;
522 long old_written = CPP_WRITTEN (pfile);
524 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
526 cpp_ice (pfile, "handle_directive called on macro buffer");
530 cpp_skip_hspace (pfile);
533 /* # followed by a number is equivalent to #line. Do not recognize
534 this form in assembly language source files. Complain about this
535 form if we're being pedantic, but not if this is regurgitated
536 input (preprocessed or fed back in by the C++ frontend). */
537 if (c >= '0' && c <= '9')
539 if (CPP_OPTIONS (pfile)->lang_asm)
542 if (CPP_PEDANTIC (pfile)
543 && ! CPP_PREPROCESSED (pfile)
544 && ! CPP_BUFFER (pfile)->manual_pop)
545 cpp_pedwarn (pfile, "`#' followed by integer");
546 do_line (pfile, NULL);
550 /* If we are rescanning preprocessed input, don't obey any directives
552 if (CPP_PREPROCESSED (pfile))
555 /* Now find the directive name. */
556 CPP_PUTC (pfile, '#');
557 parse_name (pfile, GETC());
558 ident = pfile->token_buffer + old_written + 1;
559 ident_length = CPP_PWRITTEN (pfile) - ident;
560 if (ident_length == 0)
562 /* A line of just `#' becomes blank. A line with something
563 other than an identifier after the # is reparsed as a non-
565 CPP_SET_WRITTEN (pfile, old_written);
566 return (PEEKC() == '\n');
569 /* Decode the keyword and call the appropriate expansion routine. */
570 for (kt = directive_table; ; kt++)
573 /* # identifier, but not a legit directive. Pass onward as a
574 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
576 if (kt->length == ident_length
577 && !strncmp (kt->name, ident, ident_length))
581 CPP_SET_WRITTEN (pfile, old_written);
583 if (pfile->no_directives)
585 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
587 skip_rest_of_line (pfile);
590 (*kt->func) (pfile, kt);
595 /* Pass a directive through to the output file.
596 BUF points to the contents of the directive, as a contiguous string.
597 LEN is the length of the string pointed to by BUF.
598 KEYWORD is the keyword-table entry for the directive. */
601 pass_thru_directive (buf, len, pfile, keyword)
605 const struct directive *keyword;
607 register unsigned keyword_length = keyword->length;
609 CPP_RESERVE (pfile, 1 + keyword_length + len);
610 CPP_PUTC_Q (pfile, '#');
611 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
612 if (len != 0 && buf[0] != ' ')
613 CPP_PUTC_Q (pfile, ' ');
614 CPP_PUTS_Q (pfile, buf, len);
617 /* Subroutine of do_define: determine the name of the macro to be
621 get_macro_name (pfile)
626 here = CPP_WRITTEN (pfile);
627 pfile->no_macro_expand++;
628 if (get_directive_token (pfile) != CPP_NAME)
630 cpp_error (pfile, "`#define' must be followed by an identifier");
634 len = CPP_WRITTEN (pfile) - here;
635 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
637 cpp_error (pfile, "`defined' is not a legal macro name");
641 pfile->no_macro_expand--;
645 skip_rest_of_line (pfile);
646 pfile->no_macro_expand--;
650 /* Process a #define command.
651 KEYWORD is the keyword-table entry for #define,
652 or NULL for a "predefined" macro. */
655 do_define (pfile, keyword)
657 const struct directive *keyword;
666 here = CPP_WRITTEN (pfile);
667 len = get_macro_name (pfile);
671 /* Copy out the name so we can pop the token buffer. */
672 len = CPP_WRITTEN (pfile) - here;
673 sym = (U_CHAR *) alloca (len + 1);
674 memcpy (sym, pfile->token_buffer + here, len);
676 CPP_SET_WRITTEN (pfile, here);
678 /* If the next character, with no intervening whitespace, is '(',
679 then this is a function-like macro. */
683 else if (c != '\n' && !is_hspace (c))
684 /* Otherwise, C99 requires white space after the name. We treat it
685 as an object-like macro if this happens, with a warning. */
686 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
688 def = create_definition (pfile, funlike);
692 if ((hp = cpp_lookup (pfile, sym, len)) != NULL)
696 /* Redefining a macro is ok if the definitions are the same. */
697 if (hp->type == T_MACRO)
698 ok = ! compare_defs (pfile, def, hp->value.defn);
699 /* Redefining a constant is ok with -D. */
700 else if (hp->type == T_CONST || hp->type == T_STDC)
701 ok = ! CPP_OPTIONS (pfile)->done_initializing;
702 /* Otherwise it's not ok. */
705 /* Print the warning or error if it's not ok. */
708 if (hp->type == T_POISON)
709 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
711 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
712 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
714 DEFINITION *d = hp->value.defn;
715 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
716 "this is the location of the previous definition");
719 if (hp->type != T_POISON)
721 /* Replace the old definition. */
722 if (hp->type == T_MACRO)
723 free_definition (hp->value.defn);
725 hp->value.defn = def;
729 cpp_install (pfile, sym, len, T_MACRO, (char *) def);
731 if (keyword != NULL && keyword->type == T_DEFINE)
733 if (CPP_OPTIONS (pfile)->debug_output
734 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
735 dump_definition (pfile, sym, len, def);
736 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
737 pass_thru_directive (sym, len, pfile, keyword);
744 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
745 If BUFFER != NULL, then use the LENGTH characters in BUFFER
746 as the new input buffer.
747 Return the new buffer, or NULL on failure. */
750 cpp_push_buffer (pfile, buffer, length)
755 cpp_buffer *buf = CPP_BUFFER (pfile);
757 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
759 cpp_fatal (pfile, "macro or `#include' recursion too deep");
763 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
765 new->if_stack = pfile->if_stack;
766 new->cleanup = null_cleanup;
767 new->underflow = null_underflow;
768 new->buf = new->cur = buffer;
769 new->alimit = new->rlimit = buffer + length;
772 new->line_base = NULL;
774 CPP_BUFFER (pfile) = new;
779 cpp_pop_buffer (pfile)
782 cpp_buffer *buf = CPP_BUFFER (pfile);
784 cpp_ice (pfile, "mark active in cpp_pop_buffer");
785 (*buf->cleanup) (buf, pfile);
786 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
788 pfile->buffer_stack_depth--;
789 return CPP_BUFFER (pfile);
792 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
793 Pop the buffer when done. */
796 cpp_scan_buffer (pfile)
799 cpp_buffer *buffer = CPP_BUFFER (pfile);
800 enum cpp_token token;
801 if (CPP_OPTIONS (pfile)->no_output)
803 long old_written = CPP_WRITTEN (pfile);
804 /* In no-output mode, we can ignore everything but directives. */
807 if (! pfile->only_seen_white)
808 skip_rest_of_line (pfile);
809 token = cpp_get_token (pfile);
810 if (token == CPP_EOF) /* Should not happen ... */
812 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
814 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
815 != CPP_NULL_BUFFER (pfile))
816 cpp_pop_buffer (pfile);
820 CPP_SET_WRITTEN (pfile, old_written);
826 token = cpp_get_token (pfile);
827 if (token == CPP_EOF) /* Should not happen ... */
829 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
831 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
832 != CPP_NULL_BUFFER (pfile))
833 cpp_pop_buffer (pfile);
841 * Rescan a string (which may have escape marks) into pfile's buffer.
842 * Place the result in pfile->token_buffer.
844 * The input is copied before it is scanned, so it is safe to pass
845 * it something from the token_buffer that will get overwritten
846 * (because it follows CPP_WRITTEN). This is used by do_include.
850 cpp_expand_to_buffer (pfile, buf, length)
855 register cpp_buffer *ip;
861 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
865 /* Set up the input on the input stack. */
867 buf1 = (U_CHAR *) alloca (length + 1);
868 memcpy (buf1, buf, length);
871 ip = cpp_push_buffer (pfile, buf1, length);
876 /* Scan the input, create the output. */
877 save_no_output = CPP_OPTIONS (pfile)->no_output;
878 CPP_OPTIONS (pfile)->no_output = 0;
879 cpp_scan_buffer (pfile);
880 CPP_OPTIONS (pfile)->no_output = save_no_output;
882 CPP_NUL_TERMINATE (pfile);
886 cpp_buf_line_and_col (pbuf, linep, colp)
887 register cpp_buffer *pbuf;
892 *linep = pbuf->lineno;
894 *colp = pbuf->cur - pbuf->line_base;
904 /* Return the cpp_buffer that corresponds to a file (not a macro). */
907 cpp_file_buffer (pfile)
910 cpp_buffer *ip = CPP_BUFFER (pfile);
912 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
913 if (ip->fname != NULL)
919 * write out a #line command, for instance, after an #include file.
920 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
924 output_line_command (pfile, file_change)
926 enum file_change_code file_change;
929 cpp_buffer *ip = CPP_BUFFER (pfile);
931 if (ip->fname == NULL)
934 if (CPP_OPTIONS (pfile)->no_line_commands
935 || CPP_OPTIONS (pfile)->no_output)
938 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
940 /* If the current file has not changed, we omit the #line if it would
941 appear to be a no-op, and we output a few newlines instead
942 if we want to increase the line number by a small amount.
943 We cannot do this if pfile->lineno is zero, because that means we
944 haven't output any line commands yet. (The very first line command
945 output is a `same_file' command.) */
946 if (file_change == same_file && pfile->lineno != 0)
948 if (line == pfile->lineno)
951 /* If the inherited line number is a little too small,
952 output some newlines instead of a #line command. */
953 if (line > pfile->lineno && line < pfile->lineno + 8)
955 CPP_RESERVE (pfile, 20);
956 while (line > pfile->lineno)
958 CPP_PUTC_Q (pfile, '\n');
965 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
966 CPP_PUTS_Q (pfile, "# ", 2);
968 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
969 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
971 quote_string (pfile, ip->nominal_fname);
972 if (file_change != same_file && file_change != rename_file)
974 CPP_PUTC_Q (pfile, ' ');
975 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
977 /* Tell cc1 if following text comes from a system header file. */
978 if (ip->system_header_p)
980 CPP_PUTC_Q (pfile, ' ');
981 CPP_PUTC_Q (pfile, '3');
983 #ifndef NO_IMPLICIT_EXTERN_C
984 /* Tell cc1plus if following text should be treated as C. */
985 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
987 CPP_PUTC_Q (pfile, ' ');
988 CPP_PUTC_Q (pfile, '4');
991 CPP_PUTC_Q (pfile, '\n');
992 pfile->lineno = line;
996 /* Like cpp_get_token, except that it does not read past end-of-line.
997 Also, horizontal space is skipped, and macros are popped. */
1000 get_directive_token (pfile)
1003 long old_written = CPP_WRITTEN (pfile);
1004 enum cpp_token token;
1008 cpp_skip_hspace (pfile);
1009 if (PEEKC () == '\n')
1012 token = cpp_get_token (pfile);
1013 /* token could be hspace at the beginning of a macro. */
1014 if (token == CPP_HSPACE || token == CPP_COMMENT)
1016 CPP_SET_WRITTEN (pfile, old_written);
1020 /* token cannot be vspace, it would have been caught above. */
1021 if (token == CPP_VSPACE)
1023 cpp_ice (pfile, "VSPACE in get_directive_token");
1027 /* token cannot be POP unless the buffer is a macro buffer. */
1028 if (token != CPP_POP)
1031 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1033 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1037 /* We must pop the buffer by hand, or else cpp_get_token might
1038 hand us white space or newline on the next invocation. */
1039 cpp_pop_buffer (pfile);
1043 /* Handle #include and #import.
1044 This function expects to see "fname" or <fname> on the input.
1046 The input is normally in part of the output_buffer following
1047 CPP_WRITTEN, and will get overwritten by output_line_command.
1048 I.e. in input file specification has been popped by handle_directive.
1052 do_include (pfile, keyword)
1054 const struct directive *keyword;
1056 int importing = (keyword->type == T_IMPORT);
1057 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1058 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1059 int before; /* included before? */
1061 unsigned char *ftok;
1064 enum cpp_token token;
1066 /* Chain of dirs to search */
1067 struct include_hash *ihash;
1068 struct file_name_list *search_start;
1070 long old_written = CPP_WRITTEN (pfile);
1074 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1077 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1079 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1082 if (importing && CPP_OPTIONS (pfile)->warn_import
1083 && !CPP_OPTIONS (pfile)->inhibit_warnings
1084 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1086 pfile->import_warning = 1;
1088 "#import is obsolete, use an #ifndef wrapper in the header file");
1091 pfile->parsing_include_directive++;
1092 token = get_directive_token (pfile);
1093 pfile->parsing_include_directive--;
1095 if (token == CPP_STRING)
1097 if (pfile->token_buffer[old_written] == '<')
1101 else if (token == CPP_NAME)
1103 /* Support '#include xyz' like VAX-C. It is taken as
1104 '#include <xyz.h>' and generates a warning. */
1106 "`#include filename' is obsolete, use `#include <filename.h>'");
1109 /* Append the missing `.h' to the name. */
1110 CPP_PUTS (pfile, ".h", 2);
1116 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1117 CPP_SET_WRITTEN (pfile, old_written);
1118 skip_rest_of_line (pfile);
1122 flen = CPP_WRITTEN (pfile) - old_written;
1123 ftok = (unsigned char *) alloca (flen + 1);
1124 memcpy (ftok, pfile->token_buffer + old_written, flen);
1127 if (get_directive_token (pfile) != CPP_VSPACE)
1129 cpp_error (pfile, "junk at end of `#include'");
1130 skip_rest_of_line (pfile);
1133 CPP_SET_WRITTEN (pfile, old_written);
1137 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1141 if (CPP_OPTIONS (pfile)->dump_includes)
1142 pass_thru_directive (ftok,
1145 - ((token == CPP_NAME) ? 2 : 0)
1150 if (token == CPP_STRING)
1160 for (fp = CPP_BUFFER (pfile);
1161 fp != CPP_NULL_BUFFER (pfile);
1162 fp = CPP_PREV_BUFFER (fp))
1163 if (fp->fname != NULL)
1166 if (fp == CPP_NULL_BUFFER (pfile))
1168 cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1172 /* For #include_next, skip in the search path past the dir in which the
1173 containing file was found. Treat files specified using an absolute path
1174 as if there are no more directories to search. Treat the primary source
1175 file like any other included source, but generate a warning. */
1176 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1178 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1179 search_start = fp->ihash->foundhere->next;
1184 cpp_warning (pfile, "#include_next in primary source file");
1187 search_start = CPP_OPTIONS (pfile)->bracket_include;
1190 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1193 search_start = fp->actual_dir;
1196 search_start = CPP_OPTIONS (pfile)->quote_include;
1202 cpp_error (pfile, "No include path in which to find %s", ftok);
1206 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1213 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1214 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1215 (pfile->system_include_depth > 0)))
1217 if (!angle_brackets)
1218 deps_output (pfile, ftok, ' ');
1222 struct file_name_list *ptr;
1223 /* If requested as a system header, assume it belongs in
1224 the first system header directory. */
1225 if (CPP_OPTIONS (pfile)->bracket_include)
1226 ptr = CPP_OPTIONS (pfile)->bracket_include;
1228 ptr = CPP_OPTIONS (pfile)->quote_include;
1230 p = (char *) alloca (strlen (ptr->name)
1231 + strlen (ftok) + 2);
1232 if (*ptr->name != '\0')
1234 strcpy (p, ptr->name);
1238 deps_output (pfile, p, ' ');
1241 /* If -M was specified, and this header file won't be added to
1242 the dependency list, then don't count this as an error,
1243 because we can still produce correct output. Otherwise, we
1244 can't produce correct output, because there may be
1245 dependencies we need inside the missing file, and we don't
1246 know what directory this missing file exists in. */
1247 else if (CPP_PRINT_DEPS (pfile)
1248 && (CPP_PRINT_DEPS (pfile)
1249 <= (angle_brackets || (pfile->system_include_depth > 0))))
1250 cpp_warning (pfile, "No include path in which to find %s", ftok);
1252 cpp_error_from_errno (pfile, ftok);
1257 /* For -M, add the file to the dependencies on its first inclusion. */
1258 if (!before && (CPP_PRINT_DEPS (pfile)
1259 > (angle_brackets || (pfile->system_include_depth > 0))))
1260 deps_output (pfile, ihash->name, ' ');
1262 /* Handle -H option. */
1263 if (CPP_OPTIONS(pfile)->print_include_names)
1265 fp = CPP_BUFFER (pfile);
1266 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1268 fprintf (stderr, " %s\n", ihash->name);
1271 /* Actually process the file */
1274 ihash->control_macro = "";
1276 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1283 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1285 if (finclude (pfile, fd, ihash))
1287 output_line_command (pfile, enter_file);
1288 pfile->only_seen_white = 2;
1294 /* Subroutine of do_line. Read next token from PFILE without adding it to
1295 the output buffer. If it is a number between 1 and 4, store it in *NUM
1296 and return 1; otherwise, return 0 and complain if we aren't at the end
1297 of the directive. */
1300 read_line_number (pfile, num)
1304 long save_written = CPP_WRITTEN (pfile);
1305 U_CHAR *p = pfile->token_buffer + save_written;
1306 enum cpp_token token = get_directive_token (pfile);
1307 CPP_SET_WRITTEN (pfile, save_written);
1309 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1316 if (token != CPP_VSPACE && token != CPP_EOF)
1317 cpp_error (pfile, "invalid format `#line' command");
1322 /* Interpret #line command.
1323 Note that the filename string (if any) is treated as if it were an
1324 include filename. That means no escape handling. */
1327 do_line (pfile, keyword)
1329 const struct directive *keyword ATTRIBUTE_UNUSED;
1331 cpp_buffer *ip = CPP_BUFFER (pfile);
1333 long old_written = CPP_WRITTEN (pfile);
1334 enum file_change_code file_change = same_file;
1335 enum cpp_token token;
1338 token = get_directive_token (pfile);
1340 if (token != CPP_NUMBER)
1342 cpp_error (pfile, "token after `#line' is not an integer");
1343 goto bad_line_directive;
1346 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1349 cpp_error (pfile, "token after `#line' is not an integer");
1350 goto bad_line_directive;
1352 CPP_SET_WRITTEN (pfile, old_written);
1354 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1355 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1357 token = get_directive_token (pfile);
1359 if (token == CPP_STRING)
1361 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1362 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1363 int action_number = 0;
1365 file_change = rename_file;
1367 if (read_line_number (pfile, &action_number))
1369 if (CPP_PEDANTIC (pfile))
1370 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1372 if (action_number == 1)
1374 file_change = enter_file;
1375 read_line_number (pfile, &action_number);
1377 else if (action_number == 2)
1379 file_change = leave_file;
1380 read_line_number (pfile, &action_number);
1382 if (action_number == 3)
1384 ip->system_header_p = 1;
1385 read_line_number (pfile, &action_number);
1387 if (action_number == 4)
1389 ip->system_header_p = 2;
1390 read_line_number (pfile, &action_number);
1396 if (strcmp (fname, ip->nominal_fname))
1398 const char *newname, *oldname;
1399 if (!strcmp (fname, ip->fname))
1400 newname = ip->fname;
1401 else if (ip->last_nominal_fname
1402 && !strcmp (fname, ip->last_nominal_fname))
1403 newname = ip->last_nominal_fname;
1405 newname = xstrdup (fname);
1407 oldname = ip->nominal_fname;
1408 ip->nominal_fname = newname;
1410 if (ip->last_nominal_fname
1411 && ip->last_nominal_fname != oldname
1412 && ip->last_nominal_fname != newname
1413 && ip->last_nominal_fname != ip->fname)
1414 free ((void *) ip->last_nominal_fname);
1416 if (newname == ip->fname)
1417 ip->last_nominal_fname = NULL;
1419 ip->last_nominal_fname = oldname;
1422 else if (token != CPP_VSPACE && token != CPP_EOF)
1424 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1425 goto bad_line_directive;
1428 /* The Newline at the end of this line remains to be processed.
1429 To put the next line at the specified line number,
1430 we must store a line number now that is one less. */
1431 ip->lineno = new_lineno - 1;
1432 CPP_SET_WRITTEN (pfile, old_written);
1433 output_line_command (pfile, file_change);
1437 skip_rest_of_line (pfile);
1438 CPP_SET_WRITTEN (pfile, old_written);
1442 /* Remove the definition of a symbol from the symbol table.
1443 According to the C standard, it is not an error to undef
1444 something that has no definitions. */
1446 do_undef (pfile, keyword)
1448 const struct directive *keyword;
1452 U_CHAR *buf, *name, *limit;
1454 long here = CPP_WRITTEN (pfile);
1455 enum cpp_token token;
1457 cpp_skip_hspace (pfile);
1459 if (! is_idstart(c))
1461 cpp_error (pfile, "token after #undef is not an identifier");
1462 skip_rest_of_line (pfile);
1466 parse_name (pfile, c);
1467 buf = pfile->token_buffer + here;
1468 limit = CPP_PWRITTEN(pfile);
1470 /* Copy out the token so we can pop the token buffer. */
1472 name = (U_CHAR *) alloca (len + 1);
1473 memcpy (name, buf, len);
1476 token = get_directive_token (pfile);
1477 if (token != CPP_VSPACE)
1479 cpp_pedwarn (pfile, "junk on line after #undef");
1480 skip_rest_of_line (pfile);
1482 CPP_SET_WRITTEN (pfile, here);
1484 while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1486 /* If we are generating additional info for debugging (with -g) we
1487 need to pass through all effective #undef commands. */
1488 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1489 pass_thru_directive (name, len, pfile, keyword);
1490 if (hp->type == T_POISON)
1491 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1494 if (hp->type != T_MACRO)
1495 cpp_warning (pfile, "undefining `%s'", hp->name);
1503 /* Wrap do_undef for -U processing. */
1505 cpp_undef (pfile, macro)
1509 /* Copy the string so we can append a newline. */
1510 size_t len = strlen (macro);
1511 U_CHAR *buf = alloca (len + 2);
1512 memcpy (buf, macro, len);
1514 buf[len + 1] = '\0';
1515 if (cpp_push_buffer (pfile, buf, len + 1))
1517 do_undef (pfile, NULL);
1518 cpp_pop_buffer (pfile);
1523 * Report an error detected by the program we are processing.
1524 * Use the text of the line in the error message.
1525 * (We use error because it prints the filename & line#.)
1529 do_error (pfile, keyword)
1531 const struct directive *keyword ATTRIBUTE_UNUSED;
1533 U_CHAR *text, *limit;
1535 cpp_skip_hspace (pfile);
1536 text = CPP_BUFFER (pfile)->cur;
1537 skip_rest_of_line (pfile);
1538 limit = CPP_BUFFER (pfile)->cur;
1540 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1545 * Report a warning detected by the program we are processing.
1546 * Use the text of the line in the warning message, then continue.
1550 do_warning (pfile, keyword)
1552 const struct directive *keyword ATTRIBUTE_UNUSED;
1554 U_CHAR *text, *limit;
1556 cpp_skip_hspace (pfile);
1557 text = CPP_BUFFER (pfile)->cur;
1558 skip_rest_of_line (pfile);
1559 limit = CPP_BUFFER (pfile)->cur;
1561 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1562 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1564 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1568 /* Report program identification. */
1571 do_ident (pfile, keyword)
1573 const struct directive *keyword ATTRIBUTE_UNUSED;
1575 long old_written = CPP_WRITTEN (pfile);
1577 /* Allow #ident in system headers, since that's not user's fault. */
1578 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1579 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1581 CPP_PUTS (pfile, "#ident ", 7);
1583 /* Next token should be a string constant. */
1584 if (get_directive_token (pfile) == CPP_STRING)
1585 /* And then a newline. */
1586 if (get_directive_token (pfile) == CPP_VSPACE)
1587 /* Good - ship it. */
1590 cpp_error (pfile, "invalid #ident");
1591 skip_rest_of_line (pfile);
1592 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1597 /* Pragmata handling. We handle some of these, and pass the rest on
1598 to the front end. C99 defines three pragmas and says that no macro
1599 expansion is to be performed on them; whether or not macro
1600 expansion happens for other pragmas is implementation defined.
1601 This implementation never macro-expands the text after #pragma.
1603 We currently do not support the _Pragma operator. Support for that
1604 has to be coordinated with the front end. Proposed implementation:
1605 both #pragma blah blah and _Pragma("blah blah") become
1606 __builtin_pragma(blah blah) and we teach the parser about that. */
1608 /* Sub-handlers for the pragmas needing treatment here.
1609 They return 1 if the token buffer is to be popped, 0 if not. */
1610 static int do_pragma_once PARAMS ((cpp_reader *));
1611 static int do_pragma_implementation PARAMS ((cpp_reader *));
1612 static int do_pragma_poison PARAMS ((cpp_reader *));
1613 static int do_pragma_default PARAMS ((cpp_reader *));
1616 do_pragma (pfile, keyword)
1618 const struct directive *keyword ATTRIBUTE_UNUSED;
1623 enum cpp_token token;
1625 here = CPP_WRITTEN (pfile);
1626 CPP_PUTS (pfile, "#pragma ", 8);
1628 key = CPP_WRITTEN (pfile);
1629 pfile->no_macro_expand++;
1630 token = get_directive_token (pfile);
1631 if (token != CPP_NAME)
1633 if (token == CPP_VSPACE)
1639 buf = pfile->token_buffer + key;
1640 CPP_PUTC (pfile, ' ');
1642 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1644 pop = do_pragma_once (pfile);
1645 else if (tokis ("implementation"))
1646 pop = do_pragma_implementation (pfile);
1647 else if (tokis ("poison"))
1648 pop = do_pragma_poison (pfile);
1650 pop = do_pragma_default (pfile);
1653 if (get_directive_token (pfile) != CPP_VSPACE)
1657 CPP_SET_WRITTEN (pfile, here);
1658 pfile->no_macro_expand--;
1662 cpp_error (pfile, "malformed #pragma directive");
1663 skip_rest_of_line (pfile);
1665 CPP_SET_WRITTEN (pfile, here);
1666 pfile->no_macro_expand--;
1671 do_pragma_default (pfile)
1674 while (get_directive_token (pfile) != CPP_VSPACE)
1675 CPP_PUTC (pfile, ' ');
1680 do_pragma_once (pfile)
1683 cpp_buffer *ip = CPP_BUFFER (pfile);
1685 if (ip->fname == NULL)
1687 cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1691 /* Allow #pragma once in system headers, since that's not the user's
1693 if (!ip->system_header_p)
1694 cpp_warning (pfile, "`#pragma once' is obsolete");
1696 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1697 cpp_warning (pfile, "`#pragma once' outside include file");
1699 ip->ihash->control_macro = ""; /* never repeat */
1705 do_pragma_implementation (pfile)
1708 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1709 been included yet. */
1710 struct include_hash *ptr;
1711 enum cpp_token token;
1712 long written = CPP_WRITTEN (pfile);
1716 token = get_directive_token (pfile);
1717 if (token == CPP_VSPACE)
1719 else if (token != CPP_STRING)
1721 cpp_error (pfile, "malformed #pragma implementation");
1725 name = pfile->token_buffer + written + 1;
1726 copy = xstrdup (name);
1727 copy[strlen(copy)] = '\0'; /* trim trailing quote */
1729 ptr = include_hash (pfile, copy, 0);
1732 "`#pragma implementation' for `%s' appears after file is included",
1739 do_pragma_poison (pfile)
1742 /* Poison these symbols so that all subsequent usage produces an
1748 enum cpp_token token;
1750 /* As a rule, don't include #pragma poison commands in output,
1751 unless the user asks for them. */
1752 writeit = (CPP_OPTIONS (pfile)->debug_output
1753 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1754 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1758 written = CPP_WRITTEN (pfile);
1759 token = get_directive_token (pfile);
1760 if (token == CPP_VSPACE)
1762 if (token != CPP_NAME)
1764 cpp_error (pfile, "invalid #pragma poison directive");
1765 skip_rest_of_line (pfile);
1769 p = pfile->token_buffer + written;
1771 if ((hp = cpp_lookup (pfile, p, len)))
1773 if (hp->type != T_POISON)
1775 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1776 free_definition (hp->value.defn);
1778 hp->type = T_POISON;
1782 cpp_install (pfile, p, len, T_POISON, 0);
1784 CPP_PUTC (pfile, ' ');
1789 #ifdef SCCS_DIRECTIVE
1790 /* Just ignore #sccs, on systems where we define it at all. */
1793 do_sccs (pfile, keyword)
1795 const struct directive *keyword ATTRIBUTE_UNUSED;
1797 if (CPP_PEDANTIC (pfile))
1798 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1799 skip_rest_of_line (pfile);
1804 /* We've found an `#if' directive. If the only thing before it in
1805 this file is white space, and if it is of the form
1806 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1807 for inclusion of this file. (See redundant_include_p in cppfiles.c
1808 for an explanation of controlling macros.) If so, return a
1809 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1812 detect_if_not_defined (pfile)
1815 U_CHAR *control_macro = 0;
1817 if (pfile->only_seen_white == 2)
1820 enum cpp_token token;
1823 int need_rparen = 0;
1825 /* Save state required for restore. */
1826 pfile->no_macro_expand++;
1827 parse_set_mark (pfile);
1828 base_offset = CPP_WRITTEN (pfile);
1831 if (get_directive_token (pfile) != CPP_OTHER
1832 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1833 || CPP_PWRITTEN (pfile)[-1] != '!')
1836 /* ...then `defined', */
1837 token_offset = CPP_WRITTEN (pfile);
1838 token = get_directive_token (pfile);
1839 if (token != CPP_NAME)
1841 ident = pfile->token_buffer + token_offset;
1842 CPP_NUL_TERMINATE (pfile);
1843 if (strcmp (ident, "defined"))
1846 /* ...then an optional '(' and the name, */
1847 token_offset = CPP_WRITTEN (pfile);
1848 token = get_directive_token (pfile);
1849 if (token == CPP_LPAREN)
1851 token_offset = CPP_WRITTEN (pfile);
1852 token = get_directive_token (pfile);
1853 if (token != CPP_NAME)
1857 else if (token != CPP_NAME)
1860 ident = pfile->token_buffer + token_offset;
1861 CPP_NUL_TERMINATE (pfile);
1863 /* ...then the ')', if necessary, */
1864 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1865 /* ...and make sure there's nothing else on the line. */
1866 && get_directive_token (pfile) == CPP_VSPACE)
1867 control_macro = xstrdup (ident);
1870 CPP_SET_WRITTEN (pfile, base_offset);
1871 pfile->no_macro_expand--;
1872 parse_goto_mark (pfile);
1875 return control_macro;
1879 * handle #if command by
1880 * 1) inserting special `defined' keyword into the hash table
1881 * that gets turned into 0 or 1 by special_symbol (thus,
1882 * if the luser has a symbol called `defined' already, it won't
1883 * work inside the #if command)
1884 * 2) rescan the input into a temporary output buffer
1885 * 3) pass the output buffer to the yacc parser and collect a value
1886 * 4) clean up the mess left from steps 1 and 2.
1887 * 5) call conditional_skip to skip til the next #endif (etc.),
1888 * or not, depending on the value from step 3.
1892 do_if (pfile, keyword)
1894 const struct directive *keyword ATTRIBUTE_UNUSED;
1896 U_CHAR *control_macro = detect_if_not_defined (pfile);
1897 HOST_WIDEST_INT value = eval_if_expression (pfile);
1898 conditional_skip (pfile, value == 0, T_IF, control_macro);
1903 * handle a #elif directive by not changing if_stack either.
1904 * see the comment above do_else.
1908 do_elif (pfile, keyword)
1910 const struct directive *keyword ATTRIBUTE_UNUSED;
1912 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1914 cpp_error (pfile, "`#elif' not within a conditional");
1919 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1921 cpp_error (pfile, "`#elif' after `#else'");
1922 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1923 "the conditional began here");
1925 pfile->if_stack->type = T_ELIF;
1928 if (pfile->if_stack->if_succeeded)
1929 skip_if_group (pfile);
1932 HOST_WIDEST_INT value = eval_if_expression (pfile);
1934 skip_if_group (pfile);
1937 ++pfile->if_stack->if_succeeded; /* continue processing input */
1938 output_line_command (pfile, same_file);
1945 * evaluate a #if expression in BUF, of length LENGTH,
1946 * then parse the result as a C expression and return the value as an int.
1949 static HOST_WIDEST_INT
1950 eval_if_expression (pfile)
1953 HOST_WIDEST_INT value;
1954 long old_written = CPP_WRITTEN (pfile);
1956 pfile->parsing_if_directive++;
1957 value = cpp_parse_expr (pfile);
1958 pfile->parsing_if_directive--;
1960 skip_rest_of_line (pfile);
1961 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1967 * routine to handle ifdef/ifndef. Try to look up the symbol,
1968 * then do or don't skip to the #endif/#else/#elif depending
1969 * on what directive is actually being processed.
1973 do_xifdef (pfile, keyword)
1975 const struct directive *keyword;
1978 cpp_buffer *ip = CPP_BUFFER (pfile);
1981 enum cpp_token token;
1982 int start_of_file = 0;
1983 U_CHAR *control_macro = 0;
1984 int old_written = CPP_WRITTEN (pfile);
1986 /* Detect a #ifndef at start of file (not counting comments). */
1987 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1988 start_of_file = pfile->only_seen_white == 2;
1990 pfile->no_macro_expand++;
1991 token = get_directive_token (pfile);
1992 pfile->no_macro_expand--;
1994 ident = pfile->token_buffer + old_written;
1995 ident_length = CPP_WRITTEN (pfile) - old_written;
1996 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1998 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
2000 skip = (keyword->type == T_IFDEF);
2001 if (! CPP_TRADITIONAL (pfile))
2002 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2004 else if (token == CPP_NAME)
2006 skip = cpp_defined (pfile, ident, ident_length);
2007 if (keyword->type == T_IFDEF)
2010 if (start_of_file && !skip)
2012 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2013 bcopy (ident, control_macro, ident_length + 1);
2018 skip = (keyword->type == T_IFDEF);
2019 if (! CPP_TRADITIONAL (pfile))
2020 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2023 if (!CPP_TRADITIONAL (pfile))
2025 cpp_skip_hspace (pfile);
2027 if (c != EOF && c != '\n')
2028 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2030 skip_rest_of_line (pfile);
2032 conditional_skip (pfile, skip, T_IF, control_macro);
2036 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2037 If this is a #ifndef starting at the beginning of a file,
2038 CONTROL_MACRO is the macro name tested by the #ifndef.
2039 Otherwise, CONTROL_MACRO is 0. */
2042 conditional_skip (pfile, skip, type, control_macro)
2045 enum node_type type;
2046 U_CHAR *control_macro;
2048 IF_STACK_FRAME *temp;
2050 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2051 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2052 temp->lineno = CPP_BUFFER (pfile)->lineno;
2053 temp->next = pfile->if_stack;
2054 temp->control_macro = control_macro;
2055 pfile->if_stack = temp;
2057 pfile->if_stack->type = type;
2060 skip_if_group (pfile);
2063 ++pfile->if_stack->if_succeeded;
2064 output_line_command (pfile, same_file);
2068 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2069 return 0 if skipping should continue, 1 if it should halt. Also
2070 adjusts the if_stack as appropriate.
2071 The `#' has been read, but not the identifier. */
2074 consider_directive_while_skipping (pfile, stack)
2076 IF_STACK_FRAME *stack;
2078 long ident_len, ident;
2079 const struct directive *kt;
2080 IF_STACK_FRAME *temp;
2082 cpp_skip_hspace (pfile);
2084 ident = CPP_WRITTEN (pfile);
2085 parse_name (pfile, GETC());
2086 ident_len = CPP_WRITTEN (pfile) - ident;
2088 CPP_SET_WRITTEN (pfile, ident);
2090 for (kt = directive_table; kt->length >= 0; kt++)
2091 if (kt->length == ident_len
2092 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2098 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2099 temp->next = pfile->if_stack;
2100 pfile->if_stack = temp;
2101 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2102 temp->type = kt->type;
2106 if (pfile->if_stack != stack)
2107 validate_else (pfile, "#else");
2110 if (pfile->if_stack == stack)
2114 pfile->if_stack->type = kt->type;
2119 if (pfile->if_stack != stack)
2120 validate_else (pfile, "#endif");
2122 if (pfile->if_stack == stack)
2125 temp = pfile->if_stack;
2126 pfile->if_stack = temp->next;
2134 /* Don't let erroneous code go by. */
2135 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2136 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2140 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2141 * leaves input ptr at the sharp sign found.
2144 skip_if_group (pfile)
2148 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2149 U_CHAR *beg_of_line;
2152 old_written = CPP_WRITTEN (pfile);
2156 beg_of_line = CPP_BUFFER (pfile)->cur;
2158 if (! CPP_TRADITIONAL (pfile))
2159 cpp_skip_hspace (pfile);
2163 CPP_BUMP_LINE (pfile);
2168 if (consider_directive_while_skipping (pfile, save_if_stack))
2172 return; /* Caller will issue error. */
2175 skip_rest_of_line (pfile);
2179 return; /* Caller will issue error. */
2181 CPP_BUMP_LINE (pfile);
2184 /* Back up to the beginning of this line. Caller will process the
2186 CPP_BUFFER (pfile)->cur = beg_of_line;
2187 pfile->only_seen_white = 1;
2191 * handle a #else directive. Do this by just continuing processing
2192 * without changing if_stack ; this is so that the error message
2193 * for missing #endif's etc. will point to the original #if. It
2194 * is possible that something different would be better.
2198 do_else (pfile, keyword)
2200 const struct directive *keyword ATTRIBUTE_UNUSED;
2202 validate_else (pfile, "#else");
2203 skip_rest_of_line (pfile);
2205 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2207 cpp_error (pfile, "`#else' not within a conditional");
2212 /* #ifndef can't have its special treatment for containing the whole file
2213 if it has a #else clause. */
2214 pfile->if_stack->control_macro = 0;
2216 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2218 cpp_error (pfile, "`#else' after `#else'");
2219 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2220 "the conditional began here");
2222 pfile->if_stack->type = T_ELSE;
2225 if (pfile->if_stack->if_succeeded)
2226 skip_if_group (pfile);
2229 ++pfile->if_stack->if_succeeded; /* continue processing input */
2230 output_line_command (pfile, same_file);
2236 * unstack after #endif command
2240 do_endif (pfile, keyword)
2242 const struct directive *keyword ATTRIBUTE_UNUSED;
2244 validate_else (pfile, "#endif");
2245 skip_rest_of_line (pfile);
2247 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2248 cpp_error (pfile, "`#endif' not within a conditional");
2251 IF_STACK_FRAME *temp = pfile->if_stack;
2252 pfile->if_stack = temp->next;
2253 if (temp->control_macro != 0)
2255 /* This #endif matched a #ifndef at the start of the file.
2256 See if it is at the end of the file. */
2259 parse_set_mark (pfile);
2263 cpp_skip_hspace (pfile);
2268 parse_goto_mark (pfile);
2272 /* This #endif ends a #ifndef
2273 that contains all of the file (aside from whitespace).
2274 Arrange not to include the file again
2275 if the macro that was tested is defined. */
2276 struct cpp_buffer *ip;
2277 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2278 if (ip->fname != NULL)
2280 ip->ihash->control_macro = (char *) temp->control_macro;
2284 output_line_command (pfile, same_file);
2289 /* Issue -pedantic warning for text which is not a comment following
2290 an #else or #endif. Do not warn in system headers, as this is harmless
2291 and very common on old systems. */
2294 validate_else (pfile, directive)
2296 const char *directive;
2298 if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2301 cpp_skip_hspace (pfile);
2302 if (PEEKC () != '\n')
2304 "text following `%s' violates ANSI standard", directive);
2307 /* Convert T_IF, etc. to a string. Used in error messages. */
2309 if_directive_name (pfile, ifs)
2311 struct if_stack *ifs;
2315 case T_IF: return "#if";
2316 case T_IFDEF: return "#ifdef";
2317 case T_IFNDEF: return "#ifndef";
2318 case T_ELIF: return "#elif";
2319 case T_ELSE: return "#else";
2321 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2326 /* Get the next token, and add it to the text in pfile->token_buffer.
2327 Return the kind of token we got. */
2330 cpp_get_token (pfile)
2333 register int c, c2, c3;
2334 enum cpp_token token;
2335 struct cpp_options *opts = CPP_OPTIONS (pfile);
2341 if (CPP_BUFFER (pfile)->manual_pop)
2342 /* If we've been reading from redirected input, the
2343 frontend will pop the buffer. */
2345 else if (CPP_BUFFER (pfile)->seen_eof)
2347 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2350 cpp_pop_buffer (pfile);
2355 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2356 struct if_stack *ifs, *nifs;
2358 /* Unwind the conditional stack and generate error messages. */
2359 for (ifs = pfile->if_stack;
2360 ifs != CPP_BUFFER (pfile)->if_stack;
2363 cpp_error_with_line (pfile, ifs->lineno, -1,
2364 "unterminated `%s' conditional",
2365 if_directive_name (pfile, ifs));
2370 pfile->if_stack = ifs;
2372 if (CPP_BUFFER (pfile)->nominal_fname
2373 && next_buf != CPP_NULL_BUFFER (pfile))
2375 /* We're about to return from an #include file.
2376 Emit #line information now (as part of the CPP_POP) result.
2377 But the #line refers to the file we will pop to. */
2378 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2379 CPP_BUFFER (pfile) = next_buf;
2380 pfile->input_stack_listing_current = 0;
2381 output_line_command (pfile, leave_file);
2382 CPP_BUFFER (pfile) = cur_buffer;
2385 CPP_BUFFER (pfile)->seen_eof = 1;
2394 if (PEEKC () == '=')
2398 if (opts->discard_comments)
2399 c = skip_comment (pfile, c);
2401 c = copy_comment (pfile, c);
2405 /* Comments are equivalent to spaces.
2406 For -traditional, a comment is equivalent to nothing. */
2407 if (opts->traditional || !opts->discard_comments)
2411 CPP_PUTC (pfile, c);
2416 if (pfile->parsing_if_directive)
2418 cpp_skip_hspace (pfile);
2419 parse_assertion (pfile);
2420 return CPP_ASSERTION;
2423 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2425 CPP_RESERVE (pfile, 3);
2426 CPP_PUTC_Q (pfile, '#');
2427 CPP_NUL_TERMINATE_Q (pfile);
2428 if (PEEKC () != '#')
2429 return CPP_STRINGIZE;
2432 CPP_PUTC_Q (pfile, '#');
2433 CPP_NUL_TERMINATE_Q (pfile);
2434 return CPP_TOKPASTE;
2437 if (!pfile->only_seen_white)
2439 /* -traditional directives are recognized only with the # in
2441 XXX Layering violation. */
2442 if (CPP_TRADITIONAL (pfile)
2443 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2445 if (handle_directive (pfile))
2446 return CPP_DIRECTIVE;
2447 pfile->only_seen_white = 0;
2452 parse_string (pfile, c);
2453 pfile->only_seen_white = 0;
2454 return c == '\'' ? CPP_CHAR : CPP_STRING;
2457 if (!opts->dollars_in_ident)
2462 if (opts->cplusplus && PEEKC () == ':')
2470 if (c2 == c || c2 == '=')
2479 if (PEEKC () == '=')
2485 if (c2 == '-' && opts->chill)
2486 goto comment; /* Chill style comment */
2487 if (c2 == '-' || c2 == '=')
2491 if (opts->cplusplus && PEEKN (1) == '*')
2493 /* In C++, there's a ->* operator. */
2495 pfile->only_seen_white = 0;
2496 CPP_RESERVE (pfile, 4);
2497 CPP_PUTC_Q (pfile, c);
2498 CPP_PUTC_Q (pfile, GETC ());
2499 CPP_PUTC_Q (pfile, GETC ());
2500 CPP_NUL_TERMINATE_Q (pfile);
2508 if (pfile->parsing_include_directive)
2512 CPP_PUTC (pfile, c);
2516 if (c == '\n' || c == EOF)
2519 "missing '>' in `#include <FILENAME>'");
2524 if (!CPP_BUFFER (pfile)->has_escapes)
2526 /* Backslash newline is replaced by nothing. */
2527 CPP_ADJUST_WRITTEN (pfile, -1);
2528 CPP_BUMP_LINE (pfile);
2532 /* We might conceivably get \r- or \r<space> in
2533 here. Just delete 'em. */
2535 if (d != '-' && d != ' ')
2536 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2537 CPP_ADJUST_WRITTEN (pfile, -1);
2543 /* else fall through */
2548 /* GNU C++ supports MIN and MAX operators <? and >?. */
2549 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2552 CPP_RESERVE (pfile, 4);
2553 CPP_PUTC (pfile, c);
2554 CPP_PUTC (pfile, c2);
2557 CPP_PUTC_Q (pfile, GETC ());
2558 CPP_NUL_TERMINATE_Q (pfile);
2559 pfile->only_seen_white = 0;
2566 CPP_RESERVE(pfile, 2);
2567 CPP_PUTC_Q (pfile, '.');
2572 /* In C++ there's a .* operator. */
2573 if (opts->cplusplus && c2 == '*')
2576 if (c2 == '.' && PEEKN(1) == '.')
2578 CPP_RESERVE(pfile, 4);
2579 CPP_PUTC_Q (pfile, '.');
2580 CPP_PUTC_Q (pfile, '.');
2581 CPP_PUTC_Q (pfile, '.');
2583 CPP_NUL_TERMINATE_Q (pfile);
2584 pfile->only_seen_white = 0;
2591 pfile->only_seen_white = 0;
2592 CPP_RESERVE(pfile, 3);
2593 CPP_PUTC_Q (pfile, c);
2594 CPP_PUTC_Q (pfile, GETC ());
2595 CPP_NUL_TERMINATE_Q (pfile);
2600 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2602 CPP_PUTC (pfile, c);
2604 parse_string (pfile, c);
2605 pfile->only_seen_white = 0;
2606 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2610 case '0': case '1': case '2': case '3': case '4':
2611 case '5': case '6': case '7': case '8': case '9':
2616 CPP_RESERVE (pfile, 2);
2617 CPP_PUTC_Q (pfile, c);
2621 if (!is_idchar(c) && c != '.'
2622 && ((c2 != 'e' && c2 != 'E'
2623 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2624 || (c != '+' && c != '-')))
2629 CPP_NUL_TERMINATE_Q (pfile);
2630 pfile->only_seen_white = 0;
2632 case 'b': case 'c': case 'd': case 'h': case 'o':
2633 case 'B': case 'C': case 'D': case 'H': case 'O':
2634 if (opts->chill && PEEKC () == '\'')
2636 pfile->only_seen_white = 0;
2637 CPP_RESERVE (pfile, 2);
2638 CPP_PUTC_Q (pfile, c);
2639 CPP_PUTC_Q (pfile, '\'');
2645 goto chill_number_eof;
2648 CPP_PUTC (pfile, c);
2652 CPP_RESERVE (pfile, 2);
2653 CPP_PUTC_Q (pfile, c);
2654 CPP_NUL_TERMINATE_Q (pfile);
2661 CPP_NUL_TERMINATE (pfile);
2668 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2669 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2670 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2671 case 'x': case 'y': case 'z':
2672 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2673 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2674 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2679 unsigned char *ident;
2680 int before_name_written = CPP_WRITTEN (pfile);
2682 parse_name (pfile, c);
2683 pfile->only_seen_white = 0;
2684 if (pfile->no_macro_expand)
2686 ident = pfile->token_buffer + before_name_written;
2687 ident_len = CPP_PWRITTEN (pfile) - ident;
2688 hp = cpp_lookup (pfile, ident, ident_len);
2691 if (hp->type == T_DISABLED)
2693 if (pfile->output_escapes)
2694 { /* Return "\r-IDENT", followed by '\0'. */
2696 CPP_RESERVE (pfile, 3);
2697 ident = pfile->token_buffer + before_name_written;
2698 CPP_ADJUST_WRITTEN (pfile, 2);
2699 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2706 /* If macro wants an arglist, verify that a '(' follows. */
2707 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2709 int macbuf_whitespace = 0;
2711 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2713 U_CHAR *point = CPP_BUFFER (pfile)->cur;
2716 cpp_skip_hspace (pfile);
2723 if (point != CPP_BUFFER (pfile)->cur)
2724 macbuf_whitespace = 1;
2728 goto not_macro_call;
2729 cpp_pop_buffer (pfile);
2732 parse_set_mark (pfile);
2735 cpp_skip_hspace (pfile);
2742 parse_goto_mark (pfile);
2748 if (macbuf_whitespace)
2749 CPP_PUTC (pfile, ' ');
2753 /* This is now known to be a macro call.
2754 Expand the macro, reading arguments as needed,
2755 and push the expansion on the input stack. */
2756 macroexpand (pfile, hp);
2757 CPP_SET_WRITTEN (pfile, before_name_written);
2761 case ' ': case '\t': case '\v':
2764 CPP_PUTC (pfile, c);
2766 if (c == EOF || !is_hspace(c))
2773 if (CPP_BUFFER (pfile)->has_escapes)
2778 if (pfile->output_escapes)
2779 CPP_PUTS (pfile, "\r-", 2);
2780 parse_name (pfile, GETC ());
2785 CPP_RESERVE (pfile, 2);
2786 if (pfile->output_escapes)
2787 CPP_PUTC_Q (pfile, '\r');
2788 CPP_PUTC_Q (pfile, c);
2793 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2799 /* Backslash newline is ignored. */
2800 CPP_BUMP_LINE (pfile);
2805 CPP_PUTC (pfile, c);
2806 if (pfile->only_seen_white == 0)
2807 pfile->only_seen_white = 1;
2808 CPP_BUMP_LINE (pfile);
2809 if (! CPP_OPTIONS (pfile)->no_line_commands)
2812 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2813 output_line_command (pfile, same_file);
2817 case '(': token = CPP_LPAREN; goto char1;
2818 case ')': token = CPP_RPAREN; goto char1;
2819 case '{': token = CPP_LBRACE; goto char1;
2820 case '}': token = CPP_RBRACE; goto char1;
2821 case ',': token = CPP_COMMA; goto char1;
2822 case ';': token = CPP_SEMICOLON; goto char1;
2828 pfile->only_seen_white = 0;
2829 CPP_PUTC (pfile, c);
2835 /* Like cpp_get_token, but skip spaces and comments. */
2838 cpp_get_non_space_token (pfile)
2841 int old_written = CPP_WRITTEN (pfile);
2844 enum cpp_token token = cpp_get_token (pfile);
2845 if (token != CPP_COMMENT && token != CPP_POP
2846 && token != CPP_HSPACE && token != CPP_VSPACE)
2848 CPP_SET_WRITTEN (pfile, old_written);
2852 /* Parse an identifier starting with C. */
2855 parse_name (pfile, c)
2867 if (c == '$' && CPP_PEDANTIC (pfile))
2868 cpp_pedwarn (pfile, "`$' in identifier");
2870 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2871 CPP_PUTC_Q (pfile, c);
2876 CPP_NUL_TERMINATE_Q (pfile);
2880 /* Parse and skip over a string starting with C. A single quoted
2881 string is treated like a double -- some programs (e.g., troff) are
2882 perverse this way. (However, a single quoted string is not allowed
2883 to extend over multiple lines.) */
2885 skip_string (pfile, c)
2889 long start_line, start_column;
2890 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2898 cpp_error_with_line (pfile, start_line, start_column,
2899 "unterminated string or character constant");
2900 if (pfile->multiline_string_line != start_line
2901 && pfile->multiline_string_line != 0)
2902 cpp_error_with_line (pfile,
2903 pfile->multiline_string_line, -1,
2904 "possible real start of unterminated constant");
2905 pfile->multiline_string_line = 0;
2909 CPP_BUMP_LINE (pfile);
2910 /* In Fortran and assembly language, silently terminate
2911 strings of either variety at end of line. This is a
2912 kludge around not knowing where comments are in these
2914 if (CPP_OPTIONS (pfile)->lang_fortran
2915 || CPP_OPTIONS (pfile)->lang_asm)
2920 /* Character constants may not extend over multiple lines.
2921 In Standard C, neither may strings. We accept multiline
2922 strings as an extension. */
2925 cpp_error_with_line (pfile, start_line, start_column,
2926 "unterminated character constant");
2930 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2931 cpp_pedwarn_with_line (pfile, start_line, start_column,
2932 "string constant runs past end of line");
2933 if (pfile->multiline_string_line == 0)
2934 pfile->multiline_string_line = start_line;
2938 if (CPP_BUFFER (pfile)->has_escapes)
2940 cpp_ice (pfile, "\\r escape inside string constant");
2944 /* Backslash newline is replaced by nothing at all. */
2945 CPP_BUMP_LINE (pfile);
2961 /* Parse a string and copy it to the output. */
2964 parse_string (pfile, c)
2968 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
2971 skip_string (pfile, c);
2973 limit = CPP_BUFFER (pfile)->cur;
2974 CPP_RESERVE (pfile, limit - start + 2);
2975 CPP_PUTC_Q (pfile, c);
2976 for (; start < limit; start++)
2978 CPP_PUTC_Q (pfile, *start);
2981 /* Read an assertion into the token buffer, converting to
2982 canonical form: `#predicate(a n swe r)' The next non-whitespace
2983 character to read should be the first letter of the predicate.
2984 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2985 with answer (see callers for why). In case of 0, an error has been
2988 parse_assertion (pfile)
2992 cpp_skip_hspace (pfile);
2994 if (! is_idstart(c))
2996 cpp_error (pfile, "assertion predicate is not an identifier");
2999 CPP_PUTC(pfile, '#');
3001 parse_name(pfile, c);
3006 if (is_hspace(c) || c == '\r')
3007 cpp_skip_hspace (pfile);
3013 CPP_PUTC(pfile, '(');
3016 while ((c = GETC()) != ')')
3022 CPP_PUTC(pfile, ' ');
3026 else if (c == '\n' || c == EOF)
3028 if (c == '\n') FORWARD(-1);
3029 cpp_error (pfile, "un-terminated assertion answer");
3033 /* \r cannot be a macro escape here. */
3034 CPP_BUMP_LINE (pfile);
3037 CPP_PUTC (pfile, c);
3042 if (pfile->limit[-1] == ' ')
3043 pfile->limit[-1] = ')';
3044 else if (pfile->limit[-1] == '(')
3046 cpp_error (pfile, "empty token sequence in assertion");
3050 CPP_PUTC (pfile, ')');
3052 CPP_NUL_TERMINATE (pfile);
3057 do_assert (pfile, keyword)
3059 const struct directive *keyword ATTRIBUTE_UNUSED;
3063 HASHNODE *base, *this;
3064 int baselen, thislen;
3066 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3067 && !CPP_BUFFER (pfile)->system_header_p)
3068 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3070 cpp_skip_hspace (pfile);
3071 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3072 ret = parse_assertion (pfile);
3077 cpp_error (pfile, "missing token-sequence in `#assert'");
3081 cpp_skip_hspace (pfile);
3083 if (c != EOF && c != '\n')
3085 cpp_error (pfile, "junk at end of `#assert'");
3089 thislen = strlen (sym);
3090 baselen = index (sym, '(') - sym;
3091 this = cpp_lookup (pfile, sym, thislen);
3094 cpp_warning (pfile, "`%s' re-asserted", sym);
3098 base = cpp_lookup (pfile, sym, baselen);
3100 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3101 else if (base->type != T_ASSERT)
3103 /* Token clash - but with what?! */
3104 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3108 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3109 (char *)base->value.aschain);
3110 base->value.aschain = this;
3112 pfile->limit = (unsigned char *) sym; /* Pop */
3116 skip_rest_of_line (pfile);
3117 pfile->limit = (unsigned char *) sym; /* Pop */
3122 do_unassert (pfile, keyword)
3124 const struct directive *keyword ATTRIBUTE_UNUSED;
3128 long baselen, thislen;
3129 HASHNODE *base, *this, *next;
3131 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3132 && !CPP_BUFFER (pfile)->system_header_p)
3133 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3135 cpp_skip_hspace (pfile);
3137 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3138 ret = parse_assertion (pfile);
3142 cpp_skip_hspace (pfile);
3144 if (c != EOF && c != '\n')
3145 cpp_error (pfile, "junk at end of `#unassert'");
3147 thislen = strlen (sym);
3150 base = cpp_lookup (pfile, sym, thislen);
3152 goto error; /* It isn't an error to #undef what isn't #defined,
3153 so it isn't an error to #unassert what isn't
3154 #asserted either. */
3156 for (this = base->value.aschain; this; this = next)
3158 next = this->value.aschain;
3159 delete_macro (this);
3161 delete_macro (base);
3165 baselen = index (sym, '(') - sym;
3166 base = cpp_lookup (pfile, sym, baselen);
3167 if (! base) goto error;
3168 this = cpp_lookup (pfile, sym, thislen);
3169 if (! this) goto error;
3172 while (next->value.aschain != this)
3173 next = next->value.aschain;
3175 next->value.aschain = this->value.aschain;
3176 delete_macro (this);
3178 if (base->value.aschain == NULL)
3179 delete_macro (base); /* Last answer for this predicate deleted. */
3182 pfile->limit = (unsigned char *) sym; /* Pop */
3185 skip_rest_of_line (pfile);
3186 pfile->limit = (unsigned char *) sym; /* Pop */
3190 /* Process STR as if it appeared as the body of an #unassert. */
3192 cpp_unassert (pfile, str)
3196 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3198 do_unassert (pfile, NULL);
3199 cpp_pop_buffer (pfile);
3203 /* Remember the current position of PFILE so it may be returned to
3204 after looking ahead a bit.
3206 Note that when you set a mark, you _must_ return to that mark. You
3207 may not forget about it and continue parsing. You may not pop a
3208 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3212 parse_set_mark (pfile)
3215 cpp_buffer *ip = CPP_BUFFER (pfile);
3216 if (ACTIVE_MARK_P())
3217 cpp_ice (pfile, "mark active in parse_set_mark");
3219 ip->mark = ip->cur - ip->buf;
3222 /* Backup the current position of PFILE to that saved in its mark,
3223 and clear the mark. */
3226 parse_goto_mark (pfile)
3229 cpp_buffer *ip = CPP_BUFFER (pfile);
3230 if (!ACTIVE_MARK_P())
3231 cpp_ice (pfile, "mark not active in parse_goto_mark");
3233 ip->cur = ip->buf + ip->mark;