2 Copyright (C) 1986, 87, 89, 92-99, 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
31 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
32 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
33 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
34 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
35 (Note that it is false while we're expanding macro *arguments*.) */
36 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
38 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
39 case CPP_BUMP_LINE must not be called. */
40 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
42 /* External declarations. */
44 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
46 /* `struct directive' defines one #-directive, including how to handle it. */
49 int length; /* Length of name */
50 int (*func) /* Function to handle directive */
51 PARAMS ((cpp_reader *, const struct directive *));
52 const char *name; /* Name of directive */
53 enum node_type type; /* Code which describes which directive. */
56 /* These functions are declared to return int instead of void since they
57 are going to be placed in a table and some old compilers have trouble with
58 pointers to functions returning void. */
60 static int do_define PARAMS ((cpp_reader *, const struct directive *));
61 static int do_line PARAMS ((cpp_reader *, const struct directive *));
62 static int do_include PARAMS ((cpp_reader *, const struct directive *));
63 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
64 static int do_error PARAMS ((cpp_reader *, const struct directive *));
65 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
66 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
67 static int do_if PARAMS ((cpp_reader *, const struct directive *));
68 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
69 static int do_else PARAMS ((cpp_reader *, const struct directive *));
70 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
71 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
73 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
75 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
76 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
77 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
79 /* Forward declarations. */
81 static void validate_else PARAMS ((cpp_reader *, const char *));
82 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
83 static void conditional_skip PARAMS ((cpp_reader *, int,
84 enum node_type, U_CHAR *));
85 static void skip_if_group PARAMS ((cpp_reader *));
86 static void parse_name PARAMS ((cpp_reader *, int));
87 static void parse_string PARAMS ((cpp_reader *, int));
88 static int parse_assertion PARAMS ((cpp_reader *));
89 static const char *if_directive_name PARAMS ((cpp_reader *,
91 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
92 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
93 static int skip_comment PARAMS ((cpp_reader *, int));
94 static int copy_comment PARAMS ((cpp_reader *, int));
95 static void copy_rest_of_line PARAMS ((cpp_reader *));
96 static void skip_rest_of_line PARAMS ((cpp_reader *));
97 static void cpp_skip_hspace PARAMS ((cpp_reader *));
98 static int handle_directive PARAMS ((cpp_reader *));
99 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
101 const struct directive *));
102 static int read_line_number PARAMS ((cpp_reader *, int *));
103 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
104 static int consider_directive_while_skipping PARAMS ((cpp_reader *,
106 static void skip_block_comment PARAMS ((cpp_reader *));
107 static void skip_line_comment PARAMS ((cpp_reader *));
108 static void parse_set_mark PARAMS ((cpp_reader *));
109 static void parse_goto_mark PARAMS ((cpp_reader *));
111 /* Here is the actual list of #-directives.
112 This table is ordered by frequency of occurrence; the numbers
113 at the end are directive counts from all the source code I have
114 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
115 linux-2.2.9, and pcmcia-cs-3.0.9). */
117 static const struct directive directive_table[] = {
119 { 6, do_define, "define", T_DEFINE }, /* 270554 */
120 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
121 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
122 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
123 { 2, do_if, "if", T_IF }, /* 18162 */
124 { 4, do_else, "else", T_ELSE }, /* 9863 */
125 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
126 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
127 { 4, do_line, "line", T_LINE }, /* 2465 */
128 { 4, do_elif, "elif", T_ELIF }, /* 610 */
129 { 5, do_error, "error", T_ERROR }, /* 475 */
130 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
132 /* Extensions. All deprecated except #warning and #include_next. */
133 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
134 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
135 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
136 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
137 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
138 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
139 #ifdef SCCS_DIRECTIVE
140 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
142 { -1, 0, "", T_UNUSED }
145 /* Place into PFILE a quoted string representing the string SRC.
146 Caller must reserve enough space in pfile->token_buffer. */
149 quote_string (pfile, src)
155 CPP_PUTC_Q (pfile, '\"');
157 switch ((c = *src++))
161 CPP_PUTC_Q (pfile, c);
164 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
165 CPP_ADJUST_WRITTEN (pfile, 4);
171 CPP_PUTC_Q (pfile, '\\');
172 CPP_PUTC_Q (pfile, c);
176 CPP_PUTC_Q (pfile, '\"');
177 CPP_NUL_TERMINATE_Q (pfile);
182 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
185 cpp_grow_buffer (pfile, n)
189 long old_written = CPP_WRITTEN (pfile);
190 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
191 pfile->token_buffer = (U_CHAR *)
192 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
193 CPP_SET_WRITTEN (pfile, old_written);
196 /* Process the string STR as if it appeared as the body of a #define.
197 If STR is just an identifier, define it with value 1.
198 If STR has anything after the identifier, then it should
199 be identifier=definition. */
202 cpp_define (pfile, str)
209 p = strchr (str, '=');
210 /* Copy the entire option so we can modify it.
211 Change the first "=" in the string to a space. If there is none,
212 tack " 1" on the end. Then add a newline and a NUL. */
216 count = strlen (str) + 2;
217 buf = (U_CHAR *) alloca (count);
218 memcpy (buf, str, count - 2);
220 buf[count - 2] = '\n';
221 buf[count - 1] = '\0';
225 count = strlen (str) + 4;
226 buf = (U_CHAR *) alloca (count);
227 memcpy (buf, str, count - 4);
228 strcpy (&buf[count-4], " 1\n");
231 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
233 do_define (pfile, NULL);
234 cpp_pop_buffer (pfile);
238 /* Process the string STR as if it appeared as the body of a #assert. */
240 cpp_assert (pfile, str)
244 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
246 do_assert (pfile, NULL);
247 cpp_pop_buffer (pfile);
251 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
253 cpp_defined (pfile, id, len)
258 HASHNODE *hp = cpp_lookup (pfile, id, len);
259 if (hp && hp->type == T_POISON)
261 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
267 static enum cpp_token
268 null_underflow (pfile)
269 cpp_reader *pfile ATTRIBUTE_UNUSED;
275 null_cleanup (pbuf, pfile)
276 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
277 cpp_reader *pfile ATTRIBUTE_UNUSED;
282 /* Skip a C-style block comment. We know it's a comment, and point is
283 at the second character of the starter. */
285 skip_block_comment (pfile)
292 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
298 cpp_error_with_line (pfile, line, col, "unterminated comment");
301 else if (c == '\n' || c == '\r')
303 /* \r cannot be a macro escape marker here. */
304 if (!ACTIVE_MARK_P())
305 CPP_BUMP_LINE (pfile);
307 else if (c == '/' && prev_c == '*')
309 else if (c == '*' && prev_c == '/'
310 && CPP_OPTIONS (pfile)->warn_comments)
311 cpp_warning (pfile, "`/*' within comment");
317 /* Skip a C++/Chill line comment. We know it's a comment, and point
318 is at the second character of the initiator. */
320 skip_line_comment (pfile)
328 /* We don't have to worry about EOF in here. */
331 /* Don't consider final '\n' to be part of comment. */
337 /* \r cannot be a macro escape marker here. */
338 if (!ACTIVE_MARK_P())
339 CPP_BUMP_LINE (pfile);
340 if (CPP_OPTIONS (pfile)->warn_comments)
341 cpp_warning (pfile, "backslash-newline within line comment");
346 /* Skip a comment - C, C++, or Chill style. M is the first character
347 of the comment marker. If this really is a comment, skip to its
348 end and return ' '. If this is not a comment, return M (which will
352 skip_comment (pfile, m)
356 if (m == '/' && PEEKC() == '*')
358 skip_block_comment (pfile);
361 else if (m == '/' && PEEKC() == '/')
363 if (CPP_BUFFER (pfile)->system_header_p)
365 /* We silently allow C++ comments in system headers, irrespective
366 of conformance mode, because lots of busted systems do that
367 and trying to clean it up in fixincludes is a nightmare. */
368 skip_line_comment (pfile);
371 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
373 if (CPP_OPTIONS (pfile)->c89
374 && CPP_PEDANTIC (pfile)
375 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
378 "C++ style comments are not allowed in ISO C89");
380 "(this will be reported only once per input file)");
381 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
383 skip_line_comment (pfile);
389 else if (m == '-' && PEEKC() == '-'
390 && CPP_OPTIONS (pfile)->chill)
392 skip_line_comment (pfile);
399 /* Identical to skip_comment except that it copies the comment into the
400 token_buffer. This is used if !discard_comments. */
402 copy_comment (pfile, m)
406 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
409 if (skip_comment (pfile, m) == m)
413 for (limit = CPP_BUFFER (pfile)->cur; start <= limit; start++)
415 CPP_PUTC (pfile, *start);
419 /* Skip whitespace \-newline and comments. Does not macro-expand. */
422 cpp_skip_hspace (pfile)
431 else if (is_hspace(c))
433 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
434 cpp_pedwarn (pfile, "%s in preprocessing directive",
435 c == '\f' ? "formfeed" : "vertical tab");
439 /* \r is a backslash-newline marker if !has_escapes, and
440 a deletable-whitespace or no-reexpansion marker otherwise. */
441 if (CPP_BUFFER (pfile)->has_escapes)
449 CPP_BUFFER (pfile)->lineno++;
451 else if (c == '/' || c == '-')
453 c = skip_comment (pfile, c);
463 /* Read the rest of the current line.
464 The line is appended to PFILE's output buffer. */
467 copy_rest_of_line (pfile)
478 CPP_NUL_TERMINATE (pfile);
482 if (CPP_BUFFER (pfile)->has_escapes)
486 CPP_BUFFER (pfile)->lineno++;
491 parse_string (pfile, c);
496 if (CPP_TRADITIONAL (pfile))
497 CPP_PUTS (pfile, "/**/", 4);
498 skip_block_comment (pfile);
501 /* else fall through */
503 c = skip_comment (pfile, c);
508 if (CPP_PEDANTIC (pfile))
509 cpp_pedwarn (pfile, "%s in preprocessing directive",
510 c == '\f' ? "formfeed" : "vertical tab");
518 /* FIXME: It is almost definitely a performance win to make this do
519 the scan itself. >75% of calls to copy_r_o_l are from here or
520 skip_if_group, which means the common case is to copy stuff into the
521 token_buffer only to discard it. */
523 skip_rest_of_line (pfile)
526 long old = CPP_WRITTEN (pfile);
527 copy_rest_of_line (pfile);
528 CPP_SET_WRITTEN (pfile, old);
531 /* Handle a possible # directive.
532 '#' has already been read. */
535 handle_directive (pfile)
539 register const struct directive *kt;
542 long old_written = CPP_WRITTEN (pfile);
544 cpp_skip_hspace (pfile);
547 /* # followed by a number is equivalent to #line. Do not recognize
548 this form in assembly language source files. Complain about this
549 form if we're being pedantic, but not if this is regurgitated
550 input (preprocessed or fed back in by the C++ frontend). */
551 if (c >= '0' && c <= '9')
553 if (CPP_OPTIONS (pfile)->lang_asm)
556 if (CPP_PEDANTIC (pfile)
557 && ! CPP_PREPROCESSED (pfile)
558 && ! CPP_BUFFER (pfile)->manual_pop)
559 cpp_pedwarn (pfile, "`#' followed by integer");
560 do_line (pfile, NULL);
564 /* If we are rescanning preprocessed input, don't obey any directives
566 if (CPP_PREPROCESSED (pfile))
569 /* Now find the directive name. */
570 CPP_PUTC (pfile, '#');
571 parse_name (pfile, GETC());
572 ident = pfile->token_buffer + old_written + 1;
573 ident_length = CPP_PWRITTEN (pfile) - ident;
574 if (ident_length == 0)
576 /* A line of just `#' becomes blank. A line with something
577 other than an identifier after the # is reparsed as a non-
579 CPP_SET_WRITTEN (pfile, old_written);
580 return (PEEKC() == '\n');
583 /* Decode the keyword and call the appropriate expansion routine. */
584 for (kt = directive_table; ; kt++)
587 /* # identifier, but not a legit directive. Pass onward as a
588 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
590 if (kt->length == ident_length
591 && !strncmp (kt->name, ident, ident_length))
595 CPP_SET_WRITTEN (pfile, old_written);
597 if (pfile->no_directives)
599 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
601 skip_rest_of_line (pfile);
604 (*kt->func) (pfile, kt);
609 /* Pass a directive through to the output file.
610 BUF points to the contents of the directive, as a contiguous string.
611 LEN is the length of the string pointed to by BUF.
612 KEYWORD is the keyword-table entry for the directive. */
615 pass_thru_directive (buf, len, pfile, keyword)
619 const struct directive *keyword;
621 register unsigned keyword_length = keyword->length;
623 CPP_RESERVE (pfile, 1 + keyword_length + len);
624 CPP_PUTC_Q (pfile, '#');
625 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
626 if (len != 0 && buf[0] != ' ')
627 CPP_PUTC_Q (pfile, ' ');
628 CPP_PUTS_Q (pfile, buf, len);
631 /* Check a purported macro name SYMNAME, and yield its length. */
634 check_macro_name (pfile, symname)
636 const U_CHAR *symname;
641 for (p = symname; is_idchar(*p); p++)
643 sym_length = p - symname;
645 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
646 cpp_error (pfile, "invalid macro name");
647 else if (!is_idstart(*symname)
648 || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
649 U_CHAR *msg; /* what pain... */
650 msg = (U_CHAR *) alloca (sym_length + 1);
651 bcopy (symname, msg, sym_length);
653 cpp_error (pfile, "invalid macro name `%s'", msg);
658 /* Process a #define command.
659 KEYWORD is the keyword-table entry for #define,
660 or NULL for a "predefined" macro,
661 or the keyword-table entry for #pragma in the case of a #pragma poison. */
664 do_define (pfile, keyword)
666 const struct directive *keyword;
671 U_CHAR *macro, *buf, *end;
673 here = CPP_WRITTEN (pfile);
674 copy_rest_of_line (pfile);
676 /* Copy out the line so we can pop the token buffer. */
677 buf = pfile->token_buffer + here;
678 end = CPP_PWRITTEN (pfile);
679 macro = (U_CHAR *) alloca (end - buf + 1);
680 memcpy (macro, buf, end - buf + 1);
681 end = macro + (end - buf);
683 CPP_SET_WRITTEN (pfile, here);
685 mdef = create_definition (macro, end, pfile);
689 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen)) != NULL)
693 /* Redefining a macro is ok if the definitions are the same. */
694 if (hp->type == T_MACRO)
695 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
696 /* Redefining a constant is ok with -D. */
697 else if (hp->type == T_CONST || hp->type == T_STDC)
698 ok = ! CPP_OPTIONS (pfile)->done_initializing;
699 /* Otherwise it's not ok. */
702 /* Print the warning or error if it's not ok. */
705 if (hp->type == T_POISON)
706 cpp_error (pfile, "redefining poisoned `%.*s'",
707 mdef.symlen, mdef.symnam);
709 cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
710 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
711 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file,
712 hp->value.defn->line, -1,
713 "this is the location of the previous definition");
715 if (hp->type != T_POISON)
717 /* Replace the old definition. */
718 if (hp->type == T_MACRO)
719 free_definition (hp->value.defn);
721 hp->value.defn = mdef.defn;
725 cpp_install (pfile, mdef.symnam, mdef.symlen, T_MACRO, (char *)mdef.defn);
727 if (keyword != NULL && keyword->type == T_DEFINE)
729 if (CPP_OPTIONS (pfile)->debug_output
730 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
731 dump_definition (pfile, mdef.symnam, mdef.symlen, mdef.defn);
732 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
733 pass_thru_directive (mdef.symnam, mdef.symlen, pfile, keyword);
740 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
741 If BUFFER != NULL, then use the LENGTH characters in BUFFER
742 as the new input buffer.
743 Return the new buffer, or NULL on failure. */
746 cpp_push_buffer (pfile, buffer, length)
751 cpp_buffer *buf = CPP_BUFFER (pfile);
753 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
755 cpp_fatal (pfile, "macro or `#include' recursion too deep");
759 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
761 new->if_stack = pfile->if_stack;
762 new->cleanup = null_cleanup;
763 new->underflow = null_underflow;
764 new->buf = new->cur = buffer;
765 new->alimit = new->rlimit = buffer + length;
769 CPP_BUFFER (pfile) = new;
774 cpp_pop_buffer (pfile)
777 cpp_buffer *buf = CPP_BUFFER (pfile);
779 cpp_ice (pfile, "mark active in cpp_pop_buffer");
780 (*buf->cleanup) (buf, pfile);
781 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
783 pfile->buffer_stack_depth--;
784 return CPP_BUFFER (pfile);
787 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
788 Pop the buffer when done. */
791 cpp_scan_buffer (pfile)
794 cpp_buffer *buffer = CPP_BUFFER (pfile);
795 enum cpp_token token;
796 if (CPP_OPTIONS (pfile)->no_output)
798 long old_written = CPP_WRITTEN (pfile);
799 /* In no-output mode, we can ignore everything but directives. */
802 if (! pfile->only_seen_white)
803 skip_rest_of_line (pfile);
804 token = cpp_get_token (pfile);
805 if (token == CPP_EOF) /* Should not happen ... */
807 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
809 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
810 != CPP_NULL_BUFFER (pfile))
811 cpp_pop_buffer (pfile);
815 CPP_SET_WRITTEN (pfile, old_written);
821 token = cpp_get_token (pfile);
822 if (token == CPP_EOF) /* Should not happen ... */
824 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
826 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
827 != CPP_NULL_BUFFER (pfile))
828 cpp_pop_buffer (pfile);
836 * Rescan a string (which may have escape marks) into pfile's buffer.
837 * Place the result in pfile->token_buffer.
839 * The input is copied before it is scanned, so it is safe to pass
840 * it something from the token_buffer that will get overwritten
841 * (because it follows CPP_WRITTEN). This is used by do_include.
845 cpp_expand_to_buffer (pfile, buf, length)
850 register cpp_buffer *ip;
856 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
860 /* Set up the input on the input stack. */
862 buf1 = (U_CHAR *) alloca (length + 1);
863 memcpy (buf1, buf, length);
866 ip = cpp_push_buffer (pfile, buf1, length);
871 /* Scan the input, create the output. */
872 save_no_output = CPP_OPTIONS (pfile)->no_output;
873 CPP_OPTIONS (pfile)->no_output = 0;
874 cpp_scan_buffer (pfile);
875 CPP_OPTIONS (pfile)->no_output = save_no_output;
877 CPP_NUL_TERMINATE (pfile);
881 cpp_buf_line_and_col (pbuf, linep, colp)
882 register cpp_buffer *pbuf;
887 *linep = pbuf->lineno;
889 *colp = pbuf->cur - pbuf->line_base;
899 /* Return the cpp_buffer that corresponds to a file (not a macro). */
902 cpp_file_buffer (pfile)
905 cpp_buffer *ip = CPP_BUFFER (pfile);
907 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
908 if (ip->fname != NULL)
914 * write out a #line command, for instance, after an #include file.
915 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
919 output_line_command (pfile, file_change)
921 enum file_change_code file_change;
924 cpp_buffer *ip = CPP_BUFFER (pfile);
926 if (ip->fname == NULL)
929 if (CPP_OPTIONS (pfile)->no_line_commands
930 || CPP_OPTIONS (pfile)->no_output)
933 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, NULL);
935 /* If the current file has not changed, we omit the #line if it would
936 appear to be a no-op, and we output a few newlines instead
937 if we want to increase the line number by a small amount.
938 We cannot do this if pfile->lineno is zero, because that means we
939 haven't output any line commands yet. (The very first line command
940 output is a `same_file' command.) */
941 if (file_change == same_file && pfile->lineno != 0)
943 if (line == pfile->lineno)
946 /* If the inherited line number is a little too small,
947 output some newlines instead of a #line command. */
948 if (line > pfile->lineno && line < pfile->lineno + 8)
950 CPP_RESERVE (pfile, 20);
951 while (line > pfile->lineno)
953 CPP_PUTC_Q (pfile, '\n');
960 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
961 CPP_PUTS_Q (pfile, "# ", 2);
963 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
964 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
966 quote_string (pfile, ip->nominal_fname);
967 if (file_change != same_file)
969 CPP_PUTC_Q (pfile, ' ');
970 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
972 /* Tell cc1 if following text comes from a system header file. */
973 if (ip->system_header_p)
975 CPP_PUTC_Q (pfile, ' ');
976 CPP_PUTC_Q (pfile, '3');
978 #ifndef NO_IMPLICIT_EXTERN_C
979 /* Tell cc1plus if following text should be treated as C. */
980 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
982 CPP_PUTC_Q (pfile, ' ');
983 CPP_PUTC_Q (pfile, '4');
986 CPP_PUTC_Q (pfile, '\n');
987 pfile->lineno = line;
991 /* Like cpp_get_token, except that it does not read past end-of-line.
992 Also, horizontal space is skipped, and macros are popped. */
995 get_directive_token (pfile)
998 long old_written = CPP_WRITTEN (pfile);
999 enum cpp_token token;
1003 cpp_skip_hspace (pfile);
1004 if (PEEKC () == '\n')
1007 token = cpp_get_token (pfile);
1008 /* token could be hspace at the beginning of a macro. */
1009 if (token == CPP_HSPACE || token == CPP_COMMENT)
1011 CPP_SET_WRITTEN (pfile, old_written);
1015 /* token cannot be vspace, it would have been caught above. */
1016 if (token == CPP_VSPACE)
1018 cpp_ice (pfile, "VSPACE in get_directive_token");
1022 /* token cannot be POP unless the buffer is a macro buffer. */
1023 if (token != CPP_POP)
1026 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1028 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1032 /* We must pop the buffer by hand, or else cpp_get_token might
1033 hand us white space or newline on the next invocation. */
1034 cpp_pop_buffer (pfile);
1038 /* Handle #include and #import.
1039 This function expects to see "fname" or <fname> on the input.
1041 The input is normally in part of the output_buffer following
1042 CPP_WRITTEN, and will get overwritten by output_line_command.
1043 I.e. in input file specification has been popped by handle_directive.
1047 do_include (pfile, keyword)
1049 const struct directive *keyword;
1051 int importing = (keyword->type == T_IMPORT);
1052 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1053 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1054 int before; /* included before? */
1056 unsigned char *ftok;
1059 enum cpp_token token;
1061 /* Chain of dirs to search */
1062 struct include_hash *ihash;
1063 struct file_name_list *search_start;
1065 long old_written = CPP_WRITTEN (pfile);
1069 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1072 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1074 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1077 if (importing && CPP_OPTIONS (pfile)->warn_import
1078 && !CPP_OPTIONS (pfile)->inhibit_warnings
1079 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1081 pfile->import_warning = 1;
1083 "#import is obsolete, use an #ifndef wrapper in the header file");
1086 pfile->parsing_include_directive++;
1087 token = get_directive_token (pfile);
1088 pfile->parsing_include_directive--;
1090 if (token == CPP_STRING)
1092 if (pfile->token_buffer[old_written] == '<')
1096 else if (token == CPP_NAME)
1098 /* Support '#include xyz' like VAX-C. It is taken as
1099 '#include <xyz.h>' and generates a warning. */
1101 "`#include filename' is obsolete, use `#include <filename.h>'");
1104 /* Append the missing `.h' to the name. */
1105 CPP_PUTS (pfile, ".h", 2);
1111 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1112 CPP_SET_WRITTEN (pfile, old_written);
1113 skip_rest_of_line (pfile);
1117 flen = CPP_WRITTEN (pfile) - old_written;
1118 ftok = (unsigned char *) alloca (flen + 1);
1119 memcpy (ftok, pfile->token_buffer + old_written, flen);
1122 if (get_directive_token (pfile) != CPP_VSPACE)
1124 cpp_error (pfile, "junk at end of `#include'");
1125 skip_rest_of_line (pfile);
1128 CPP_SET_WRITTEN (pfile, old_written);
1132 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1136 if (CPP_OPTIONS (pfile)->dump_includes)
1137 pass_thru_directive (ftok,
1140 - ((token == CPP_NAME) ? 2 : 0)
1145 if (token == CPP_STRING)
1155 for (fp = CPP_BUFFER (pfile);
1156 fp != CPP_NULL_BUFFER (pfile);
1157 fp = CPP_PREV_BUFFER (fp))
1158 if (fp->fname != NULL)
1161 if (fp == CPP_NULL_BUFFER (pfile))
1163 cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1167 /* For #include_next, skip in the search path past the dir in which the
1168 containing file was found. Treat files specified using an absolute path
1169 as if there are no more directories to search. Treat the primary source
1170 file like any other included source, but generate a warning. */
1171 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1173 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1174 search_start = fp->ihash->foundhere->next;
1179 cpp_warning (pfile, "#include_next in primary source file");
1182 search_start = CPP_OPTIONS (pfile)->bracket_include;
1185 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1188 search_start = fp->actual_dir;
1191 search_start = CPP_OPTIONS (pfile)->quote_include;
1197 cpp_error (pfile, "No include path in which to find %s", ftok);
1201 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1208 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1209 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1210 (pfile->system_include_depth > 0)))
1212 if (!angle_brackets)
1213 deps_output (pfile, ftok, ' ');
1217 struct file_name_list *ptr;
1218 /* If requested as a system header, assume it belongs in
1219 the first system header directory. */
1220 if (CPP_OPTIONS (pfile)->bracket_include)
1221 ptr = CPP_OPTIONS (pfile)->bracket_include;
1223 ptr = CPP_OPTIONS (pfile)->quote_include;
1225 p = (char *) alloca (strlen (ptr->name)
1226 + strlen (ftok) + 2);
1227 if (*ptr->name != '\0')
1229 strcpy (p, ptr->name);
1233 deps_output (pfile, p, ' ');
1236 /* If -M was specified, and this header file won't be added to
1237 the dependency list, then don't count this as an error,
1238 because we can still produce correct output. Otherwise, we
1239 can't produce correct output, because there may be
1240 dependencies we need inside the missing file, and we don't
1241 know what directory this missing file exists in. */
1242 else if (CPP_PRINT_DEPS (pfile)
1243 && (CPP_PRINT_DEPS (pfile)
1244 <= (angle_brackets || (pfile->system_include_depth > 0))))
1245 cpp_warning (pfile, "No include path in which to find %s", ftok);
1247 cpp_error_from_errno (pfile, ftok);
1252 /* For -M, add the file to the dependencies on its first inclusion. */
1253 if (!before && (CPP_PRINT_DEPS (pfile)
1254 > (angle_brackets || (pfile->system_include_depth > 0))))
1255 deps_output (pfile, ihash->name, ' ');
1257 /* Handle -H option. */
1258 if (CPP_OPTIONS(pfile)->print_include_names)
1260 fp = CPP_BUFFER (pfile);
1261 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1263 fprintf (stderr, " %s\n", ihash->name);
1266 /* Actually process the file */
1269 ihash->control_macro = "";
1271 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1278 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1280 if (finclude (pfile, fd, ihash))
1282 output_line_command (pfile, enter_file);
1283 pfile->only_seen_white = 2;
1289 /* Subroutine of do_line. Read next token from PFILE without adding it to
1290 the output buffer. If it is a number between 1 and 4, store it in *NUM
1291 and return 1; otherwise, return 0 and complain if we aren't at the end
1292 of the directive. */
1295 read_line_number (pfile, num)
1299 long save_written = CPP_WRITTEN (pfile);
1300 U_CHAR *p = pfile->token_buffer + save_written;
1301 enum cpp_token token = get_directive_token (pfile);
1302 CPP_SET_WRITTEN (pfile, save_written);
1304 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1311 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP)
1312 cpp_error (pfile, "invalid format `#line' command");
1317 /* Interpret #line command.
1318 Note that the filename string (if any) is treated as if it were an
1319 include filename. That means no escape handling. */
1322 do_line (pfile, keyword)
1324 const struct directive *keyword ATTRIBUTE_UNUSED;
1326 cpp_buffer *ip = CPP_BUFFER (pfile);
1328 long old_written = CPP_WRITTEN (pfile);
1329 enum file_change_code file_change = same_file;
1330 enum cpp_token token;
1333 token = get_directive_token (pfile);
1335 if (token != CPP_NUMBER)
1337 cpp_error (pfile, "token after `#line' is not an integer");
1338 goto bad_line_directive;
1341 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1344 cpp_error (pfile, "token after `#line' is not an integer");
1345 goto bad_line_directive;
1347 CPP_SET_WRITTEN (pfile, old_written);
1349 if (CPP_PEDANTIC (pfile) && new_lineno <= 0)
1350 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1352 token = get_directive_token (pfile);
1354 if (token == CPP_STRING)
1356 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1357 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1358 int action_number = 0;
1360 if (read_line_number (pfile, &action_number))
1362 if (CPP_PEDANTIC (pfile))
1363 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1365 if (action_number == 1)
1367 file_change = enter_file;
1368 read_line_number (pfile, &action_number);
1370 else if (action_number == 2)
1372 file_change = leave_file;
1373 read_line_number (pfile, &action_number);
1375 if (action_number == 3)
1377 ip->system_header_p = 1;
1378 read_line_number (pfile, &action_number);
1380 if (action_number == 4)
1382 ip->system_header_p = 2;
1383 read_line_number (pfile, &action_number);
1389 if (strcmp (fname, ip->nominal_fname))
1391 const char *newname, *oldname;
1392 if (!strcmp (fname, ip->fname))
1393 newname = ip->fname;
1394 else if (ip->last_nominal_fname
1395 && !strcmp (fname, ip->last_nominal_fname))
1396 newname = ip->last_nominal_fname;
1398 newname = xstrdup (fname);
1400 oldname = ip->nominal_fname;
1401 ip->nominal_fname = newname;
1403 if (ip->last_nominal_fname
1404 && ip->last_nominal_fname != oldname
1405 && ip->last_nominal_fname != newname
1406 && ip->last_nominal_fname != ip->fname)
1407 free ((void *) ip->last_nominal_fname);
1409 if (newname == ip->fname)
1410 ip->last_nominal_fname = NULL;
1412 ip->last_nominal_fname = oldname;
1415 else if (token != CPP_VSPACE && token != CPP_EOF)
1417 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1418 goto bad_line_directive;
1421 /* The Newline at the end of this line remains to be processed.
1422 To put the next line at the specified line number,
1423 we must store a line number now that is one less. */
1424 ip->lineno = new_lineno - 1;
1425 CPP_SET_WRITTEN (pfile, old_written);
1426 output_line_command (pfile, file_change);
1430 skip_rest_of_line (pfile);
1431 CPP_SET_WRITTEN (pfile, old_written);
1435 /* Remove the definition of a symbol from the symbol table.
1436 According to the C standard, it is not an error to undef
1437 something that has no definitions. */
1439 do_undef (pfile, keyword)
1441 const struct directive *keyword;
1445 U_CHAR *buf, *name, *limit;
1447 long here = CPP_WRITTEN (pfile);
1448 enum cpp_token token;
1450 cpp_skip_hspace (pfile);
1452 if (! is_idstart(c))
1454 cpp_error (pfile, "token after #undef is not an identifier");
1455 skip_rest_of_line (pfile);
1459 parse_name (pfile, c);
1460 buf = pfile->token_buffer + here;
1461 limit = CPP_PWRITTEN(pfile);
1463 /* Copy out the token so we can pop the token buffer. */
1465 name = (U_CHAR *) alloca (len + 1);
1466 memcpy (name, buf, len);
1467 name[limit - buf] = '\0';
1469 token = get_directive_token (pfile);
1470 if (token != CPP_VSPACE && token != CPP_POP)
1472 cpp_pedwarn (pfile, "junk on line after #undef");
1473 skip_rest_of_line (pfile);
1476 CPP_SET_WRITTEN (pfile, here);
1478 while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1480 /* If we are generating additional info for debugging (with -g) we
1481 need to pass through all effective #undef commands. */
1482 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1483 pass_thru_directive (name, len, pfile, keyword);
1484 if (hp->type == T_POISON)
1485 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1488 if (hp->type != T_MACRO)
1489 cpp_warning (pfile, "undefining `%s'", hp->name);
1497 /* Wrap do_undef for -U processing. */
1499 cpp_undef (pfile, macro)
1503 /* Copy the string so we can append a newline. */
1504 size_t len = strlen (macro);
1505 U_CHAR *buf = alloca (len + 2);
1506 memcpy (buf, macro, len);
1508 buf[len + 1] = '\0';
1509 if (cpp_push_buffer (pfile, buf, len + 1))
1511 do_undef (pfile, NULL);
1512 cpp_pop_buffer (pfile);
1517 * Report an error detected by the program we are processing.
1518 * Use the text of the line in the error message.
1519 * (We use error because it prints the filename & line#.)
1523 do_error (pfile, keyword)
1525 const struct directive *keyword ATTRIBUTE_UNUSED;
1527 U_CHAR *text, *limit;
1529 cpp_skip_hspace (pfile);
1530 text = CPP_BUFFER (pfile)->cur;
1531 skip_rest_of_line (pfile);
1532 limit = CPP_BUFFER (pfile)->cur;
1534 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1539 * Report a warning detected by the program we are processing.
1540 * Use the text of the line in the warning message, then continue.
1544 do_warning (pfile, keyword)
1546 const struct directive *keyword ATTRIBUTE_UNUSED;
1548 U_CHAR *text, *limit;
1550 cpp_skip_hspace (pfile);
1551 text = CPP_BUFFER (pfile)->cur;
1552 skip_rest_of_line (pfile);
1553 limit = CPP_BUFFER (pfile)->cur;
1555 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1556 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1558 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1562 /* Report program identification. */
1565 do_ident (pfile, keyword)
1567 const struct directive *keyword ATTRIBUTE_UNUSED;
1569 long old_written = CPP_WRITTEN (pfile);
1571 /* Allow #ident in system headers, since that's not user's fault. */
1572 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
1573 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1575 CPP_PUTS (pfile, "#ident ", 7);
1577 /* Next token should be a string constant. */
1578 if (get_directive_token (pfile) == CPP_STRING)
1579 /* And then a newline. */
1580 if (get_directive_token (pfile) == CPP_VSPACE)
1581 /* Good - ship it. */
1584 cpp_error (pfile, "invalid #ident");
1585 skip_rest_of_line (pfile);
1586 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1591 /* Pragmata handling. We handle some of these, and pass the rest on
1592 to the front end. C99 defines three pragmas and says that no macro
1593 expansion is to be performed on them; whether or not macro
1594 expansion happens for other pragmas is implementation defined.
1595 This implementation never macro-expands the text after #pragma.
1597 We currently do not support the _Pragma operator. Support for that
1598 has to be coordinated with the front end. Proposed implementation:
1599 both #pragma blah blah and _Pragma("blah blah") become
1600 __builtin_pragma(blah blah) and we teach the parser about that. */
1602 /* Sub-handlers for the pragmas needing treatment here.
1603 They return 1 if the token buffer is to be popped, 0 if not. */
1604 static int do_pragma_once PARAMS ((cpp_reader *));
1605 static int do_pragma_implementation PARAMS ((cpp_reader *));
1606 static int do_pragma_poison PARAMS ((cpp_reader *));
1607 static int do_pragma_default PARAMS ((cpp_reader *));
1610 do_pragma (pfile, keyword)
1612 const struct directive *keyword ATTRIBUTE_UNUSED;
1618 here = CPP_WRITTEN (pfile);
1619 CPP_PUTS (pfile, "#pragma ", 8);
1621 key = CPP_WRITTEN (pfile);
1622 pfile->no_macro_expand++;
1623 if (get_directive_token (pfile) != CPP_NAME)
1626 buf = pfile->token_buffer + key;
1627 CPP_PUTC (pfile, ' ');
1629 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1631 pop = do_pragma_once (pfile);
1632 else if (tokis ("implementation"))
1633 pop = do_pragma_implementation (pfile);
1634 else if (tokis ("poison"))
1635 pop = do_pragma_poison (pfile);
1637 pop = do_pragma_default (pfile);
1640 if (get_directive_token (pfile) != CPP_VSPACE)
1644 CPP_SET_WRITTEN (pfile, here);
1645 pfile->no_macro_expand--;
1649 cpp_error (pfile, "malformed #pragma directive");
1650 skip_rest_of_line (pfile);
1651 CPP_SET_WRITTEN (pfile, here);
1652 pfile->no_macro_expand--;
1657 do_pragma_default (pfile)
1660 while (get_directive_token (pfile) != CPP_VSPACE)
1661 CPP_PUTC (pfile, ' ');
1666 do_pragma_once (pfile)
1669 cpp_buffer *ip = CPP_BUFFER (pfile);
1671 if (ip->fname == NULL)
1673 cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1677 /* Allow #pragma once in system headers, since that's not the user's
1679 if (!ip->system_header_p)
1680 cpp_warning (pfile, "`#pragma once' is obsolete");
1682 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1683 cpp_warning (pfile, "`#pragma once' outside include file");
1685 ip->ihash->control_macro = ""; /* never repeat */
1691 do_pragma_implementation (pfile)
1694 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1695 been included yet. */
1696 struct include_hash *ptr;
1697 enum cpp_token token;
1698 long written = CPP_WRITTEN (pfile);
1702 token = get_directive_token (pfile);
1703 if (token == CPP_VSPACE)
1705 else if (token != CPP_STRING)
1707 cpp_error (pfile, "malformed #pragma implementation");
1711 name = pfile->token_buffer + written + 1;
1712 copy = xstrdup (name);
1713 copy[strlen(copy)] = '\0'; /* trim trailing quote */
1715 ptr = include_hash (pfile, copy, 0);
1718 "`#pragma implementation' for `%s' appears after file is included",
1725 do_pragma_poison (pfile)
1728 /* Poison these symbols so that all subsequent usage produces an
1734 enum cpp_token token;
1736 /* As a rule, don't include #pragma poison commands in output,
1737 unless the user asks for them. */
1738 writeit = (CPP_OPTIONS (pfile)->debug_output
1739 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1740 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1744 written = CPP_WRITTEN (pfile);
1745 token = get_directive_token (pfile);
1746 if (token == CPP_VSPACE)
1748 if (token != CPP_NAME)
1750 cpp_error (pfile, "invalid #pragma poison directive");
1751 skip_rest_of_line (pfile);
1755 p = pfile->token_buffer + written;
1757 if ((hp = cpp_lookup (pfile, p, len)))
1759 if (hp->type != T_POISON)
1761 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1762 free_definition (hp->value.defn);
1764 hp->type = T_POISON;
1768 cpp_install (pfile, p, len, T_POISON, 0);
1770 CPP_PUTC (pfile, ' ');
1775 #ifdef SCCS_DIRECTIVE
1776 /* Just ignore #sccs, on systems where we define it at all. */
1779 do_sccs (pfile, keyword)
1781 const struct directive *keyword ATTRIBUTE_UNUSED;
1783 if (CPP_PEDANTIC (pfile))
1784 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1785 skip_rest_of_line (pfile);
1790 /* We've found an `#if' directive. If the only thing before it in
1791 this file is white space, and if it is of the form
1792 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1793 for inclusion of this file. (See redundant_include_p in cppfiles.c
1794 for an explanation of controlling macros.) If so, return a
1795 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1798 detect_if_not_defined (pfile)
1801 U_CHAR *control_macro = 0;
1803 if (pfile->only_seen_white == 2)
1806 enum cpp_token token;
1809 int need_rparen = 0;
1811 /* Save state required for restore. */
1812 pfile->no_macro_expand++;
1813 parse_set_mark (pfile);
1814 base_offset = CPP_WRITTEN (pfile);
1817 if (get_directive_token (pfile) != CPP_OTHER
1818 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1819 || CPP_PWRITTEN (pfile)[-1] != '!')
1822 /* ...then `defined', */
1823 token_offset = CPP_WRITTEN (pfile);
1824 token = get_directive_token (pfile);
1825 if (token != CPP_NAME)
1827 ident = pfile->token_buffer + token_offset;
1828 CPP_NUL_TERMINATE (pfile);
1829 if (strcmp (ident, "defined"))
1832 /* ...then an optional '(' and the name, */
1833 token_offset = CPP_WRITTEN (pfile);
1834 token = get_directive_token (pfile);
1835 if (token == CPP_LPAREN)
1837 token_offset = CPP_WRITTEN (pfile);
1838 token = get_directive_token (pfile);
1839 if (token != CPP_NAME)
1843 else if (token != CPP_NAME)
1846 ident = pfile->token_buffer + token_offset;
1847 CPP_NUL_TERMINATE (pfile);
1849 /* ...then the ')', if necessary, */
1850 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1851 /* ...and make sure there's nothing else on the line. */
1852 && get_directive_token (pfile) == CPP_VSPACE)
1853 control_macro = xstrdup (ident);
1856 CPP_SET_WRITTEN (pfile, base_offset);
1857 pfile->no_macro_expand--;
1858 parse_goto_mark (pfile);
1861 return control_macro;
1865 * handle #if command by
1866 * 1) inserting special `defined' keyword into the hash table
1867 * that gets turned into 0 or 1 by special_symbol (thus,
1868 * if the luser has a symbol called `defined' already, it won't
1869 * work inside the #if command)
1870 * 2) rescan the input into a temporary output buffer
1871 * 3) pass the output buffer to the yacc parser and collect a value
1872 * 4) clean up the mess left from steps 1 and 2.
1873 * 5) call conditional_skip to skip til the next #endif (etc.),
1874 * or not, depending on the value from step 3.
1878 do_if (pfile, keyword)
1880 const struct directive *keyword ATTRIBUTE_UNUSED;
1882 U_CHAR *control_macro = detect_if_not_defined (pfile);
1883 HOST_WIDEST_INT value = eval_if_expression (pfile);
1884 conditional_skip (pfile, value == 0, T_IF, control_macro);
1889 * handle a #elif directive by not changing if_stack either.
1890 * see the comment above do_else.
1894 do_elif (pfile, keyword)
1896 const struct directive *keyword ATTRIBUTE_UNUSED;
1898 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1900 cpp_error (pfile, "`#elif' not within a conditional");
1905 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1907 cpp_error (pfile, "`#elif' after `#else'");
1908 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1909 "the conditional began here");
1911 pfile->if_stack->type = T_ELIF;
1914 if (pfile->if_stack->if_succeeded)
1915 skip_if_group (pfile);
1918 HOST_WIDEST_INT value = eval_if_expression (pfile);
1920 skip_if_group (pfile);
1923 ++pfile->if_stack->if_succeeded; /* continue processing input */
1924 output_line_command (pfile, same_file);
1931 * evaluate a #if expression in BUF, of length LENGTH,
1932 * then parse the result as a C expression and return the value as an int.
1935 static HOST_WIDEST_INT
1936 eval_if_expression (pfile)
1939 HOST_WIDEST_INT value;
1940 long old_written = CPP_WRITTEN (pfile);
1942 /* Work around bug in cpp_get_token where it may mistake an
1943 assertion for a directive. */
1944 pfile->only_seen_white = 0;
1946 value = cpp_parse_expr (pfile);
1948 skip_rest_of_line (pfile);
1949 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1955 * routine to handle ifdef/ifndef. Try to look up the symbol,
1956 * then do or don't skip to the #endif/#else/#elif depending
1957 * on what directive is actually being processed.
1961 do_xifdef (pfile, keyword)
1963 const struct directive *keyword;
1966 cpp_buffer *ip = CPP_BUFFER (pfile);
1969 enum cpp_token token;
1970 int start_of_file = 0;
1971 U_CHAR *control_macro = 0;
1972 int old_written = CPP_WRITTEN (pfile);
1974 /* Detect a #ifndef at start of file (not counting comments). */
1975 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1976 start_of_file = pfile->only_seen_white == 2;
1978 pfile->no_macro_expand++;
1979 token = get_directive_token (pfile);
1980 pfile->no_macro_expand--;
1982 ident = pfile->token_buffer + old_written;
1983 ident_length = CPP_WRITTEN (pfile) - old_written;
1984 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1986 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1988 skip = (keyword->type == T_IFDEF);
1989 if (! CPP_TRADITIONAL (pfile))
1990 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1992 else if (token == CPP_NAME)
1994 skip = cpp_defined (pfile, ident, ident_length);
1995 if (keyword->type == T_IFDEF)
1998 if (start_of_file && !skip)
2000 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2001 bcopy (ident, control_macro, ident_length + 1);
2006 skip = (keyword->type == T_IFDEF);
2007 if (! CPP_TRADITIONAL (pfile))
2008 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2011 if (!CPP_TRADITIONAL (pfile))
2013 cpp_skip_hspace (pfile);
2015 if (c != EOF && c != '\n')
2016 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2018 skip_rest_of_line (pfile);
2020 conditional_skip (pfile, skip, T_IF, control_macro);
2024 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2025 If this is a #ifndef starting at the beginning of a file,
2026 CONTROL_MACRO is the macro name tested by the #ifndef.
2027 Otherwise, CONTROL_MACRO is 0. */
2030 conditional_skip (pfile, skip, type, control_macro)
2033 enum node_type type;
2034 U_CHAR *control_macro;
2036 IF_STACK_FRAME *temp;
2038 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2039 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2040 temp->lineno = CPP_BUFFER (pfile)->lineno;
2041 temp->next = pfile->if_stack;
2042 temp->control_macro = control_macro;
2043 pfile->if_stack = temp;
2045 pfile->if_stack->type = type;
2048 skip_if_group (pfile);
2051 ++pfile->if_stack->if_succeeded;
2052 output_line_command (pfile, same_file);
2056 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2057 return 0 if skipping should continue, 1 if it should halt. Also
2058 adjusts the if_stack as appropriate.
2059 The `#' has been read, but not the identifier. */
2062 consider_directive_while_skipping (pfile, stack)
2064 IF_STACK_FRAME *stack;
2066 long ident_len, ident;
2067 const struct directive *kt;
2068 IF_STACK_FRAME *temp;
2070 cpp_skip_hspace (pfile);
2072 ident = CPP_WRITTEN (pfile);
2073 parse_name (pfile, GETC());
2074 ident_len = CPP_WRITTEN (pfile) - ident;
2076 CPP_SET_WRITTEN (pfile, ident);
2078 for (kt = directive_table; kt->length >= 0; kt++)
2079 if (kt->length == ident_len
2080 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2086 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2087 temp->next = pfile->if_stack;
2088 pfile->if_stack = temp;
2089 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2090 temp->type = kt->type;
2094 if (pfile->if_stack != stack)
2095 validate_else (pfile, "#else");
2098 if (pfile->if_stack == stack)
2102 pfile->if_stack->type = kt->type;
2107 if (pfile->if_stack != stack)
2108 validate_else (pfile, "#endif");
2110 if (pfile->if_stack == stack)
2113 temp = pfile->if_stack;
2114 pfile->if_stack = temp->next;
2122 /* Don't let erroneous code go by. */
2123 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2124 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2128 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2129 * leaves input ptr at the sharp sign found.
2132 skip_if_group (pfile)
2136 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2137 U_CHAR *beg_of_line;
2140 if (CPP_OPTIONS (pfile)->output_conditionals)
2142 CPP_PUTS (pfile, "#failed\n", 8);
2144 output_line_command (pfile, same_file);
2147 old_written = CPP_WRITTEN (pfile);
2151 beg_of_line = CPP_BUFFER (pfile)->cur;
2153 if (! CPP_TRADITIONAL (pfile))
2154 cpp_skip_hspace (pfile);
2158 if (CPP_OPTIONS (pfile)->output_conditionals)
2159 CPP_PUTC (pfile, c);
2160 CPP_BUMP_LINE (pfile);
2165 if (consider_directive_while_skipping (pfile, save_if_stack))
2169 return; /* Caller will issue error. */
2172 if (CPP_OPTIONS (pfile)->output_conditionals)
2174 CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
2175 copy_rest_of_line (pfile);
2179 copy_rest_of_line (pfile);
2180 CPP_SET_WRITTEN (pfile, old_written); /* discard it */
2185 return; /* Caller will issue error. */
2189 if (CPP_OPTIONS (pfile)->output_conditionals)
2191 CPP_PUTC (pfile, c);
2194 CPP_BUMP_LINE (pfile);
2198 /* Back up to the beginning of this line. Caller will process the
2200 CPP_BUFFER (pfile)->cur = beg_of_line;
2201 pfile->only_seen_white = 1;
2202 if (CPP_OPTIONS (pfile)->output_conditionals)
2204 CPP_PUTS (pfile, "#endfailed\n", 11);
2210 * handle a #else directive. Do this by just continuing processing
2211 * without changing if_stack ; this is so that the error message
2212 * for missing #endif's etc. will point to the original #if. It
2213 * is possible that something different would be better.
2217 do_else (pfile, keyword)
2219 const struct directive *keyword ATTRIBUTE_UNUSED;
2221 validate_else (pfile, "#else");
2222 skip_rest_of_line (pfile);
2224 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2226 cpp_error (pfile, "`#else' not within a conditional");
2231 /* #ifndef can't have its special treatment for containing the whole file
2232 if it has a #else clause. */
2233 pfile->if_stack->control_macro = 0;
2235 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2237 cpp_error (pfile, "`#else' after `#else'");
2238 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2239 "the conditional began here");
2241 pfile->if_stack->type = T_ELSE;
2244 if (pfile->if_stack->if_succeeded)
2245 skip_if_group (pfile);
2248 ++pfile->if_stack->if_succeeded; /* continue processing input */
2249 output_line_command (pfile, same_file);
2255 * unstack after #endif command
2259 do_endif (pfile, keyword)
2261 const struct directive *keyword ATTRIBUTE_UNUSED;
2263 validate_else (pfile, "#endif");
2264 skip_rest_of_line (pfile);
2266 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2267 cpp_error (pfile, "`#endif' not within a conditional");
2270 IF_STACK_FRAME *temp = pfile->if_stack;
2271 pfile->if_stack = temp->next;
2272 if (temp->control_macro != 0)
2274 /* This #endif matched a #ifndef at the start of the file.
2275 See if it is at the end of the file. */
2278 parse_set_mark (pfile);
2282 cpp_skip_hspace (pfile);
2287 parse_goto_mark (pfile);
2291 /* This #endif ends a #ifndef
2292 that contains all of the file (aside from whitespace).
2293 Arrange not to include the file again
2294 if the macro that was tested is defined. */
2295 struct cpp_buffer *ip;
2296 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2297 if (ip->fname != NULL)
2299 ip->ihash->control_macro = (char *) temp->control_macro;
2303 output_line_command (pfile, same_file);
2308 /* Issue -pedantic warning for text which is not a comment following
2309 an #else or #endif. Do not warn in system headers, as this is harmless
2310 and very common on old systems. */
2313 validate_else (pfile, directive)
2315 const char *directive;
2317 if (! CPP_PEDANTIC (pfile) || CPP_BUFFER (pfile)->system_header_p)
2320 cpp_skip_hspace (pfile);
2321 if (PEEKC () != '\n')
2323 "text following `%s' violates ANSI standard", directive);
2326 /* Convert T_IF, etc. to a string. Used in error messages. */
2328 if_directive_name (pfile, ifs)
2330 struct if_stack *ifs;
2334 case T_IF: return "#if";
2335 case T_IFDEF: return "#ifdef";
2336 case T_IFNDEF: return "#ifndef";
2337 case T_ELIF: return "#elif";
2338 case T_ELSE: return "#else";
2340 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2345 /* Get the next token, and add it to the text in pfile->token_buffer.
2346 Return the kind of token we got. */
2349 cpp_get_token (pfile)
2352 register int c, c2, c3;
2353 enum cpp_token token;
2354 struct cpp_options *opts = CPP_OPTIONS (pfile);
2360 if (CPP_BUFFER (pfile)->manual_pop)
2361 /* If we've been reading from redirected input, the
2362 frontend will pop the buffer. */
2364 else if (CPP_BUFFER (pfile)->seen_eof)
2366 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2369 cpp_pop_buffer (pfile);
2374 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2375 struct if_stack *ifs, *nifs;
2377 /* Unwind the conditional stack and generate error messages. */
2378 for (ifs = pfile->if_stack;
2379 ifs != CPP_BUFFER (pfile)->if_stack;
2382 cpp_error_with_line (pfile, ifs->lineno, -1,
2383 "unterminated `%s' conditional",
2384 if_directive_name (pfile, ifs));
2389 pfile->if_stack = ifs;
2391 if (CPP_BUFFER (pfile)->nominal_fname
2392 && next_buf != CPP_NULL_BUFFER (pfile))
2394 /* We're about to return from an #include file.
2395 Emit #line information now (as part of the CPP_POP) result.
2396 But the #line refers to the file we will pop to. */
2397 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2398 CPP_BUFFER (pfile) = next_buf;
2399 pfile->input_stack_listing_current = 0;
2400 output_line_command (pfile, leave_file);
2401 CPP_BUFFER (pfile) = cur_buffer;
2404 CPP_BUFFER (pfile)->seen_eof = 1;
2413 if (PEEKC () == '=')
2417 if (opts->discard_comments)
2418 c = skip_comment (pfile, c);
2420 c = copy_comment (pfile, c);
2424 /* Comments are equivalent to spaces.
2425 For -traditional, a comment is equivalent to nothing. */
2426 if (opts->traditional || !opts->discard_comments)
2430 CPP_PUTC (pfile, c);
2435 if (!pfile->only_seen_white)
2437 /* -traditional directives are recognized only with the # in
2439 XXX Layering violation. */
2440 if (CPP_TRADITIONAL (pfile)
2441 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2443 if (handle_directive (pfile))
2444 return CPP_DIRECTIVE;
2445 pfile->only_seen_white = 0;
2451 parse_string (pfile, c);
2452 pfile->only_seen_white = 0;
2453 return c == '\'' ? CPP_CHAR : CPP_STRING;
2456 if (!opts->dollars_in_ident)
2461 if (opts->cplusplus && PEEKC () == ':')
2469 if (c2 == c || c2 == '=')
2478 if (PEEKC () == '=')
2484 if (c2 == '-' && opts->chill)
2485 goto comment; /* Chill style comment */
2486 if (c2 == '-' || c2 == '=')
2490 if (opts->cplusplus && PEEKN (1) == '*')
2492 /* In C++, there's a ->* operator. */
2494 pfile->only_seen_white = 0;
2495 CPP_RESERVE (pfile, 4);
2496 CPP_PUTC_Q (pfile, c);
2497 CPP_PUTC_Q (pfile, GETC ());
2498 CPP_PUTC_Q (pfile, GETC ());
2499 CPP_NUL_TERMINATE_Q (pfile);
2507 if (pfile->parsing_include_directive)
2511 CPP_PUTC (pfile, c);
2515 if (c == '\n' || c == EOF)
2518 "missing '>' in `#include <FILENAME>'");
2523 if (!CPP_BUFFER (pfile)->has_escapes)
2525 /* Backslash newline is replaced by nothing. */
2526 CPP_ADJUST_WRITTEN (pfile, -1);
2527 CPP_BUMP_LINE (pfile);
2531 /* We might conceivably get \r- or \r<space> in
2532 here. Just delete 'em. */
2534 if (d != '-' && d != ' ')
2535 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2536 CPP_ADJUST_WRITTEN (pfile, -1);
2542 /* else fall through */
2547 /* GNU C++ supports MIN and MAX operators <? and >?. */
2548 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2551 CPP_RESERVE (pfile, 4);
2552 CPP_PUTC (pfile, c);
2553 CPP_PUTC (pfile, c2);
2556 CPP_PUTC_Q (pfile, GETC ());
2557 CPP_NUL_TERMINATE_Q (pfile);
2558 pfile->only_seen_white = 0;
2565 CPP_RESERVE(pfile, 2);
2566 CPP_PUTC_Q (pfile, '.');
2571 /* In C++ there's a .* operator. */
2572 if (opts->cplusplus && c2 == '*')
2575 if (c2 == '.' && PEEKN(1) == '.')
2577 CPP_RESERVE(pfile, 4);
2578 CPP_PUTC_Q (pfile, '.');
2579 CPP_PUTC_Q (pfile, '.');
2580 CPP_PUTC_Q (pfile, '.');
2582 CPP_NUL_TERMINATE_Q (pfile);
2583 pfile->only_seen_white = 0;
2590 pfile->only_seen_white = 0;
2591 CPP_RESERVE(pfile, 3);
2592 CPP_PUTC_Q (pfile, c);
2593 CPP_PUTC_Q (pfile, GETC ());
2594 CPP_NUL_TERMINATE_Q (pfile);
2599 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2601 CPP_PUTC (pfile, c);
2607 case '0': case '1': case '2': case '3': case '4':
2608 case '5': case '6': case '7': case '8': case '9':
2613 CPP_RESERVE (pfile, 2);
2614 CPP_PUTC_Q (pfile, c);
2618 if (!is_idchar(c) && c != '.'
2619 && ((c2 != 'e' && c2 != 'E'
2620 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2621 || (c != '+' && c != '-')))
2626 CPP_NUL_TERMINATE_Q (pfile);
2627 pfile->only_seen_white = 0;
2629 case 'b': case 'c': case 'd': case 'h': case 'o':
2630 case 'B': case 'C': case 'D': case 'H': case 'O':
2631 if (opts->chill && PEEKC () == '\'')
2633 pfile->only_seen_white = 0;
2634 CPP_RESERVE (pfile, 2);
2635 CPP_PUTC_Q (pfile, c);
2636 CPP_PUTC_Q (pfile, '\'');
2642 goto chill_number_eof;
2645 CPP_PUTC (pfile, c);
2649 CPP_RESERVE (pfile, 2);
2650 CPP_PUTC_Q (pfile, c);
2651 CPP_NUL_TERMINATE_Q (pfile);
2658 CPP_NUL_TERMINATE (pfile);
2665 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2666 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2667 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2668 case 'x': case 'y': case 'z':
2669 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2670 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2671 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2676 unsigned char *ident;
2677 int before_name_written = CPP_WRITTEN (pfile);
2679 parse_name (pfile, c);
2680 pfile->only_seen_white = 0;
2681 if (pfile->no_macro_expand)
2683 ident = pfile->token_buffer + before_name_written;
2684 ident_len = CPP_PWRITTEN (pfile) - ident;
2685 hp = cpp_lookup (pfile, ident, ident_len);
2688 if (hp->type == T_DISABLED)
2690 if (pfile->output_escapes)
2691 { /* Return "\r-IDENT", followed by '\0'. */
2693 CPP_RESERVE (pfile, 3);
2694 ident = pfile->token_buffer + before_name_written;
2695 CPP_ADJUST_WRITTEN (pfile, 2);
2696 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2703 /* If macro wants an arglist, verify that a '(' follows. */
2704 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2706 int macbuf_whitespace = 0;
2708 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2710 U_CHAR *point = CPP_BUFFER (pfile)->cur;
2713 cpp_skip_hspace (pfile);
2720 if (point != CPP_BUFFER (pfile)->cur)
2721 macbuf_whitespace = 1;
2725 goto not_macro_call;
2726 cpp_pop_buffer (pfile);
2729 parse_set_mark (pfile);
2732 cpp_skip_hspace (pfile);
2739 parse_goto_mark (pfile);
2745 if (macbuf_whitespace)
2746 CPP_PUTC (pfile, ' ');
2750 /* This is now known to be a macro call.
2751 Expand the macro, reading arguments as needed,
2752 and push the expansion on the input stack. */
2753 macroexpand (pfile, hp);
2754 CPP_SET_WRITTEN (pfile, before_name_written);
2758 case ' ': case '\t': case '\v':
2761 CPP_PUTC (pfile, c);
2763 if (c == EOF || !is_hspace(c))
2770 if (CPP_BUFFER (pfile)->has_escapes)
2775 if (pfile->output_escapes)
2776 CPP_PUTS (pfile, "\r-", 2);
2777 parse_name (pfile, GETC ());
2782 CPP_RESERVE (pfile, 2);
2783 if (pfile->output_escapes)
2784 CPP_PUTC_Q (pfile, '\r');
2785 CPP_PUTC_Q (pfile, c);
2790 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2796 /* Backslash newline is ignored. */
2797 CPP_BUMP_LINE (pfile);
2802 CPP_PUTC (pfile, c);
2803 if (pfile->only_seen_white == 0)
2804 pfile->only_seen_white = 1;
2805 CPP_BUMP_LINE (pfile);
2806 if (! CPP_OPTIONS (pfile)->no_line_commands)
2809 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2810 output_line_command (pfile, same_file);
2814 case '(': token = CPP_LPAREN; goto char1;
2815 case ')': token = CPP_RPAREN; goto char1;
2816 case '{': token = CPP_LBRACE; goto char1;
2817 case '}': token = CPP_RBRACE; goto char1;
2818 case ',': token = CPP_COMMA; goto char1;
2819 case ';': token = CPP_SEMICOLON; goto char1;
2825 pfile->only_seen_white = 0;
2826 CPP_PUTC (pfile, c);
2832 /* Like cpp_get_token, but skip spaces and comments. */
2835 cpp_get_non_space_token (pfile)
2838 int old_written = CPP_WRITTEN (pfile);
2841 enum cpp_token token = cpp_get_token (pfile);
2842 if (token != CPP_COMMENT && token != CPP_POP
2843 && token != CPP_HSPACE && token != CPP_VSPACE)
2845 CPP_SET_WRITTEN (pfile, old_written);
2849 /* Parse an identifier starting with C. */
2852 parse_name (pfile, c)
2864 if (c == '$' && CPP_PEDANTIC (pfile))
2865 cpp_pedwarn (pfile, "`$' in identifier");
2867 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2868 CPP_PUTC_Q (pfile, c);
2873 CPP_NUL_TERMINATE_Q (pfile);
2877 /* Parse a string starting with C. A single quoted string is treated
2878 like a double -- some programs (e.g., troff) are perverse this way.
2879 (However, a single quoted string is not allowed to extend over
2882 parse_string (pfile, c)
2886 long start_line, start_column;
2888 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2890 CPP_PUTC (pfile, c);
2896 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2898 /* try harder: this string crosses a macro expansion
2899 boundary. This can happen naturally if -traditional.
2900 Otherwise, only -D can make a macro with an unmatched
2902 cpp_pop_buffer (pfile);
2906 cpp_error_with_line (pfile, start_line, start_column,
2907 "unterminated string or character constant");
2908 if (pfile->multiline_string_line != start_line
2909 && pfile->multiline_string_line != 0)
2910 cpp_error_with_line (pfile,
2911 pfile->multiline_string_line, -1,
2912 "possible real start of unterminated constant");
2913 pfile->multiline_string_line = 0;
2916 CPP_PUTC (pfile, cc);
2920 CPP_BUMP_LINE (pfile);
2923 /* In Fortran and assembly language, silently terminate
2924 strings of either variety at end of line. This is a
2925 kludge around not knowing where comments are in these
2927 if (CPP_OPTIONS (pfile)->lang_fortran
2928 || CPP_OPTIONS (pfile)->lang_asm)
2930 /* Character constants may not extend over multiple lines.
2931 In Standard C, neither may strings. We accept multiline
2932 strings as an extension. */
2935 cpp_error_with_line (pfile, start_line, start_column,
2936 "unterminated character constant");
2939 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2940 cpp_pedwarn_with_line (pfile, start_line, start_column,
2941 "string constant runs past end of line");
2942 if (pfile->multiline_string_line == 0)
2943 pfile->multiline_string_line = start_line;
2947 CPP_ADJUST_WRITTEN (pfile, -1);
2948 if (CPP_BUFFER (pfile)->has_escapes)
2950 cpp_ice (pfile, "\\r escape inside string constant");
2954 /* Backslash newline is replaced by nothing at all. */
2955 CPP_BUMP_LINE (pfile);
2961 CPP_PUTC (pfile, cc);
2973 /* Read an assertion into the token buffer, converting to
2974 canonical form: `#predicate(a n swe r)' The next non-whitespace
2975 character to read should be the first letter of the predicate.
2976 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2977 with answer (see callers for why). In case of 0, an error has been
2980 parse_assertion (pfile)
2984 cpp_skip_hspace (pfile);
2986 if (! is_idstart(c))
2988 cpp_error (pfile, "assertion predicate is not an identifier");
2991 CPP_PUTC(pfile, '#');
2993 parse_name(pfile, c);
2998 if (is_hspace(c) || c == '\r')
2999 cpp_skip_hspace (pfile);
3005 CPP_PUTC(pfile, '(');
3008 while ((c = GETC()) != ')')
3014 CPP_PUTC(pfile, ' ');
3018 else if (c == '\n' || c == EOF)
3020 if (c == '\n') FORWARD(-1);
3021 cpp_error (pfile, "un-terminated assertion answer");
3025 /* \r cannot be a macro escape here. */
3026 CPP_BUMP_LINE (pfile);
3029 CPP_PUTC (pfile, c);
3034 if (pfile->limit[-1] == ' ')
3035 pfile->limit[-1] = ')';
3036 else if (pfile->limit[-1] == '(')
3038 cpp_error (pfile, "empty token sequence in assertion");
3042 CPP_PUTC (pfile, ')');
3044 CPP_NUL_TERMINATE (pfile);
3049 do_assert (pfile, keyword)
3051 const struct directive *keyword ATTRIBUTE_UNUSED;
3055 HASHNODE *base, *this;
3056 int baselen, thislen;
3058 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3059 && !CPP_BUFFER (pfile)->system_header_p)
3060 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3062 cpp_skip_hspace (pfile);
3063 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3064 ret = parse_assertion (pfile);
3069 cpp_error (pfile, "missing token-sequence in `#assert'");
3073 cpp_skip_hspace (pfile);
3075 if (c != EOF && c != '\n')
3077 cpp_error (pfile, "junk at end of `#assert'");
3081 thislen = strlen (sym);
3082 baselen = index (sym, '(') - sym;
3083 this = cpp_lookup (pfile, sym, thislen);
3086 cpp_warning (pfile, "`%s' re-asserted", sym);
3090 base = cpp_lookup (pfile, sym, baselen);
3092 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3093 else if (base->type != T_ASSERT)
3095 /* Token clash - but with what?! */
3096 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3100 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3101 (char *)base->value.aschain);
3102 base->value.aschain = this;
3104 pfile->limit = (unsigned char *) sym; /* Pop */
3108 skip_rest_of_line (pfile);
3109 pfile->limit = (unsigned char *) sym; /* Pop */
3114 do_unassert (pfile, keyword)
3116 const struct directive *keyword ATTRIBUTE_UNUSED;
3120 long baselen, thislen;
3121 HASHNODE *base, *this, *next;
3123 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
3124 && !CPP_BUFFER (pfile)->system_header_p)
3125 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3127 cpp_skip_hspace (pfile);
3129 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3130 ret = parse_assertion (pfile);
3134 cpp_skip_hspace (pfile);
3136 if (c != EOF && c != '\n')
3137 cpp_error (pfile, "junk at end of `#unassert'");
3139 thislen = strlen (sym);
3142 base = cpp_lookup (pfile, sym, thislen);
3144 goto error; /* It isn't an error to #undef what isn't #defined,
3145 so it isn't an error to #unassert what isn't
3146 #asserted either. */
3148 for (this = base->value.aschain; this; this = next)
3150 next = this->value.aschain;
3151 delete_macro (this);
3153 delete_macro (base);
3157 baselen = index (sym, '(') - sym;
3158 base = cpp_lookup (pfile, sym, baselen);
3159 if (! base) goto error;
3160 this = cpp_lookup (pfile, sym, thislen);
3161 if (! this) goto error;
3164 while (next->value.aschain != this)
3165 next = next->value.aschain;
3167 next->value.aschain = this->value.aschain;
3168 delete_macro (this);
3170 if (base->value.aschain == NULL)
3171 delete_macro (base); /* Last answer for this predicate deleted. */
3174 pfile->limit = (unsigned char *) sym; /* Pop */
3177 skip_rest_of_line (pfile);
3178 pfile->limit = (unsigned char *) sym; /* Pop */
3182 /* Process STR as if it appeared as the body of an #unassert. */
3184 cpp_unassert (pfile, str)
3188 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3190 do_assert (pfile, NULL);
3191 cpp_pop_buffer (pfile);
3196 cpp_read_check_assertion (pfile)
3201 long written = CPP_WRITTEN (pfile);
3203 FORWARD (1); /* Skip '#' */
3204 cpp_skip_hspace (pfile);
3205 if (! parse_assertion (pfile))
3209 name = pfile->token_buffer + written;
3210 result = cpp_defined (pfile, name, CPP_PWRITTEN (pfile) - name);
3213 CPP_SET_WRITTEN (pfile, written);
3217 /* Remember the current position of PFILE so it may be returned to
3218 after looking ahead a bit.
3220 Note that when you set a mark, you _must_ return to that mark. You
3221 may not forget about it and continue parsing. You may not pop a
3222 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3226 parse_set_mark (pfile)
3229 cpp_buffer *ip = CPP_BUFFER (pfile);
3230 if (ACTIVE_MARK_P())
3231 cpp_ice (pfile, "mark active in parse_set_mark");
3233 ip->mark = ip->cur - ip->buf;
3236 /* Backup the current position of PFILE to that saved in its mark,
3237 and clear the mark. */
3240 parse_goto_mark (pfile)
3243 cpp_buffer *ip = CPP_BUFFER (pfile);
3244 if (!ACTIVE_MARK_P())
3245 cpp_ice (pfile, "mark not active in parse_goto_mark");
3247 ip->cur = ip->buf + ip->mark;