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. */
30 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
32 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
33 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
34 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
35 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
36 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
37 (Note that it is false while we're expanding macro *arguments*.) */
38 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
40 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
41 case CPP_BUMP_LINE must not be called. */
42 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
44 /* External declarations. */
46 extern HOST_WIDEST_INT cpp_parse_expr PARAMS ((cpp_reader *));
48 /* `struct directive' defines one #-directive, including how to handle it. */
52 int length; /* Length of name */
53 int (*func) /* Function to handle directive */
54 PARAMS ((cpp_reader *, const struct directive *));
55 const char *name; /* Name of directive */
56 enum node_type type; /* Code which describes which directive. */
59 /* These functions are declared to return int instead of void since they
60 are going to be placed in a table and some old compilers have trouble with
61 pointers to functions returning void. */
63 static int do_define PARAMS ((cpp_reader *, const struct directive *));
64 static int do_line PARAMS ((cpp_reader *, const struct directive *));
65 static int do_include PARAMS ((cpp_reader *, const struct directive *));
66 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
67 static int do_error PARAMS ((cpp_reader *, const struct directive *));
68 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
69 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
70 static int do_if PARAMS ((cpp_reader *, const struct directive *));
71 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
72 static int do_else PARAMS ((cpp_reader *, const struct directive *));
73 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
74 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
76 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
78 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
79 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
80 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
82 /* Forward declarations. */
84 static void validate_else PARAMS ((cpp_reader *, const char *));
85 static HOST_WIDEST_INT eval_if_expression PARAMS ((cpp_reader *));
86 static void conditional_skip PARAMS ((cpp_reader *, int,
87 enum node_type, U_CHAR *));
88 static void skip_if_group PARAMS ((cpp_reader *));
89 static void parse_name PARAMS ((cpp_reader *, int));
90 static void parse_string PARAMS ((cpp_reader *, int));
91 static int parse_assertion PARAMS ((cpp_reader *));
92 static const char *if_directive_name PARAMS ((cpp_reader *,
94 static enum cpp_token null_underflow PARAMS ((cpp_reader *));
95 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
96 static int skip_comment PARAMS ((cpp_reader *, int));
97 static int copy_comment PARAMS ((cpp_reader *, int));
98 static void skip_string PARAMS ((cpp_reader *, int));
99 static void skip_rest_of_line PARAMS ((cpp_reader *));
100 static void cpp_skip_hspace PARAMS ((cpp_reader *));
101 static int handle_directive PARAMS ((cpp_reader *));
102 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
104 const struct directive *));
105 static int read_line_number PARAMS ((cpp_reader *, int *));
106 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
107 static int consider_directive_while_skipping PARAMS ((cpp_reader *,
109 static void skip_block_comment PARAMS ((cpp_reader *));
110 static void skip_line_comment PARAMS ((cpp_reader *));
111 static void parse_set_mark PARAMS ((cpp_reader *));
112 static void parse_goto_mark PARAMS ((cpp_reader *));
113 static int get_macro_name PARAMS ((cpp_reader *));
115 /* Here is the actual list of #-directives.
116 This table is ordered by frequency of occurrence; the numbers
117 at the end are directive counts from all the source code I have
118 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
119 linux-2.2.9, and pcmcia-cs-3.0.9). */
121 static const struct directive directive_table[] = {
123 { 6, do_define, "define", T_DEFINE }, /* 270554 */
124 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
125 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
126 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
127 { 2, do_if, "if", T_IF }, /* 18162 */
128 { 4, do_else, "else", T_ELSE }, /* 9863 */
129 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
130 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
131 { 4, do_line, "line", T_LINE }, /* 2465 */
132 { 4, do_elif, "elif", T_ELIF }, /* 610 */
133 { 5, do_error, "error", T_ERROR }, /* 475 */
134 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
136 /* Extensions. All deprecated except #warning and #include_next. */
137 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
138 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
139 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
140 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
141 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
142 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
143 #ifdef SCCS_DIRECTIVE
144 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
146 { -1, 0, "", T_UNUSED }
149 /* Place into PFILE a quoted string representing the string SRC.
150 Caller must reserve enough space in pfile->token_buffer. */
153 quote_string (pfile, src)
159 CPP_PUTC_Q (pfile, '\"');
161 switch ((c = *src++))
165 CPP_PUTC_Q (pfile, c);
168 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
169 CPP_ADJUST_WRITTEN (pfile, 4);
175 CPP_PUTC_Q (pfile, '\\');
176 CPP_PUTC_Q (pfile, c);
180 CPP_PUTC_Q (pfile, '\"');
181 CPP_NUL_TERMINATE_Q (pfile);
186 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
189 cpp_grow_buffer (pfile, n)
193 long old_written = CPP_WRITTEN (pfile);
194 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
195 pfile->token_buffer = (U_CHAR *)
196 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
197 CPP_SET_WRITTEN (pfile, old_written);
200 /* Process the string STR as if it appeared as the body of a #define.
201 If STR is just an identifier, define it with value 1.
202 If STR has anything after the identifier, then it should
203 be identifier=definition. */
206 cpp_define (pfile, str)
213 p = strchr (str, '=');
214 /* Copy the entire option so we can modify it.
215 Change the first "=" in the string to a space. If there is none,
216 tack " 1" on the end. Then add a newline and a NUL. */
220 count = strlen (str) + 2;
221 buf = (U_CHAR *) alloca (count);
222 memcpy (buf, str, count - 2);
224 buf[count - 2] = '\n';
225 buf[count - 1] = '\0';
229 count = strlen (str) + 4;
230 buf = (U_CHAR *) alloca (count);
231 memcpy (buf, str, count - 4);
232 strcpy (&buf[count-4], " 1\n");
235 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
237 do_define (pfile, NULL);
238 cpp_pop_buffer (pfile);
242 /* Process the string STR as if it appeared as the body of a #assert. */
244 cpp_assert (pfile, str)
248 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
250 do_assert (pfile, NULL);
251 cpp_pop_buffer (pfile);
255 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
257 cpp_defined (pfile, id, len)
262 HASHNODE *hp = cpp_lookup (pfile, id, len);
263 if (hp && hp->type == T_POISON)
265 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
271 static enum cpp_token
272 null_underflow (pfile)
273 cpp_reader *pfile ATTRIBUTE_UNUSED;
279 null_cleanup (pbuf, pfile)
280 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
281 cpp_reader *pfile ATTRIBUTE_UNUSED;
286 /* Skip a C-style block comment. We know it's a comment, and point is
287 at the second character of the starter. */
289 skip_block_comment (pfile)
296 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
302 cpp_error_with_line (pfile, line, col, "unterminated comment");
305 else if (c == '\n' || c == '\r')
307 /* \r cannot be a macro escape marker here. */
308 if (!ACTIVE_MARK_P())
309 CPP_BUMP_LINE (pfile);
311 else if (c == '/' && prev_c == '*')
313 else if (c == '*' && prev_c == '/'
314 && CPP_OPTIONS (pfile)->warn_comments)
315 cpp_warning (pfile, "`/*' within comment");
321 /* Skip a C++/Chill line comment. We know it's a comment, and point
322 is at the second character of the initiator. */
324 skip_line_comment (pfile)
332 /* We don't have to worry about EOF in here. */
335 /* Don't consider final '\n' to be part of comment. */
341 /* \r cannot be a macro escape marker here. */
342 if (!ACTIVE_MARK_P())
343 CPP_BUMP_LINE (pfile);
344 if (CPP_OPTIONS (pfile)->warn_comments)
345 cpp_warning (pfile, "backslash-newline within line comment");
350 /* Skip a comment - C, C++, or Chill style. M is the first character
351 of the comment marker. If this really is a comment, skip to its
352 end and return ' '. If this is not a comment, return M (which will
356 skip_comment (pfile, m)
360 if (m == '/' && PEEKC() == '*')
362 skip_block_comment (pfile);
365 else if (m == '/' && PEEKC() == '/')
367 if (CPP_BUFFER (pfile)->system_header_p)
369 /* We silently allow C++ comments in system headers, irrespective
370 of conformance mode, because lots of busted systems do that
371 and trying to clean it up in fixincludes is a nightmare. */
372 skip_line_comment (pfile);
375 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
377 if (CPP_OPTIONS (pfile)->c89
378 && CPP_PEDANTIC (pfile)
379 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
382 "C++ style comments are not allowed in ISO C89");
384 "(this will be reported only once per input file)");
385 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
387 skip_line_comment (pfile);
393 else if (m == '-' && PEEKC() == '-'
394 && CPP_OPTIONS (pfile)->chill)
396 skip_line_comment (pfile);
403 /* Identical to skip_comment except that it copies the comment into the
404 token_buffer. This is used if !discard_comments. */
406 copy_comment (pfile, m)
410 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
413 if (skip_comment (pfile, m) == m)
416 limit = CPP_BUFFER (pfile)->cur;
417 CPP_RESERVE (pfile, limit - start + 2);
418 CPP_PUTC_Q (pfile, m);
419 for (; start <= limit; start++)
421 CPP_PUTC_Q (pfile, *start);
426 /* Skip whitespace \-newline and comments. Does not macro-expand. */
429 cpp_skip_hspace (pfile)
438 else if (is_hspace(c))
440 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
441 cpp_pedwarn (pfile, "%s in preprocessing directive",
442 c == '\f' ? "formfeed" : "vertical tab");
446 /* \r is a backslash-newline marker if !has_escapes, and
447 a deletable-whitespace or no-reexpansion marker otherwise. */
448 if (CPP_BUFFER (pfile)->has_escapes)
456 CPP_BUMP_LINE (pfile);
458 else if (c == '/' || c == '-')
460 c = skip_comment (pfile, c);
470 /* Read and discard the rest of the current line. */
473 skip_rest_of_line (pfile)
487 if (! CPP_BUFFER (pfile)->has_escapes)
488 CPP_BUMP_LINE (pfile);
493 skip_string (pfile, c);
498 skip_comment (pfile, c);
503 if (CPP_PEDANTIC (pfile))
504 cpp_pedwarn (pfile, "%s in preprocessing directive",
505 c == '\f' ? "formfeed" : "vertical tab");
512 /* Handle a possible # directive.
513 '#' has already been read. */
516 handle_directive (pfile)
520 register const struct directive *kt;
523 long old_written = CPP_WRITTEN (pfile);
525 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
527 cpp_ice (pfile, "handle_directive called on macro buffer");
531 cpp_skip_hspace (pfile);
534 /* # followed by a number is equivalent to #line. Do not recognize
535 this form in assembly language source files. Complain about this
536 form if we're being pedantic, but not if this is regurgitated
537 input (preprocessed or fed back in by the C++ frontend). */
538 if (c >= '0' && c <= '9')
540 if (CPP_OPTIONS (pfile)->lang_asm)
543 if (CPP_PEDANTIC (pfile)
544 && ! CPP_PREPROCESSED (pfile)
545 && ! CPP_BUFFER (pfile)->manual_pop)
546 cpp_pedwarn (pfile, "`#' followed by integer");
547 do_line (pfile, NULL);
551 /* If we are rescanning preprocessed input, don't obey any directives
553 if (CPP_PREPROCESSED (pfile))
556 /* Now find the directive name. */
557 CPP_PUTC (pfile, '#');
558 parse_name (pfile, GETC());
559 ident = pfile->token_buffer + old_written + 1;
560 ident_length = CPP_PWRITTEN (pfile) - ident;
561 if (ident_length == 0)
563 /* A line of just `#' becomes blank. A line with something
564 other than an identifier after the # is reparsed as a non-
566 CPP_SET_WRITTEN (pfile, old_written);
567 return (PEEKC() == '\n');
570 /* Decode the keyword and call the appropriate expansion routine. */
571 for (kt = directive_table; ; kt++)
574 /* # identifier, but not a legit directive. Pass onward as a
575 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
577 if (kt->length == ident_length
578 && !strncmp (kt->name, ident, ident_length))
582 CPP_SET_WRITTEN (pfile, old_written);
584 if (pfile->no_directives)
586 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
588 skip_rest_of_line (pfile);
591 (*kt->func) (pfile, kt);
596 /* Pass a directive through to the output file.
597 BUF points to the contents of the directive, as a contiguous string.
598 LEN is the length of the string pointed to by BUF.
599 KEYWORD is the keyword-table entry for the directive. */
602 pass_thru_directive (buf, len, pfile, keyword)
606 const struct directive *keyword;
608 register unsigned keyword_length = keyword->length;
610 CPP_RESERVE (pfile, 1 + keyword_length + len);
611 CPP_PUTC_Q (pfile, '#');
612 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
613 if (len != 0 && buf[0] != ' ')
614 CPP_PUTC_Q (pfile, ' ');
615 CPP_PUTS_Q (pfile, buf, len);
618 /* Subroutine of do_define: determine the name of the macro to be
622 get_macro_name (pfile)
627 here = CPP_WRITTEN (pfile);
628 pfile->no_macro_expand++;
629 if (get_directive_token (pfile) != CPP_NAME)
631 cpp_error (pfile, "`#define' must be followed by an identifier");
635 len = CPP_WRITTEN (pfile) - here;
636 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
638 cpp_error (pfile, "`defined' is not a legal macro name");
642 pfile->no_macro_expand--;
646 skip_rest_of_line (pfile);
647 pfile->no_macro_expand--;
651 /* Process a #define command.
652 KEYWORD is the keyword-table entry for #define,
653 or NULL for a "predefined" macro. */
656 do_define (pfile, keyword)
658 const struct directive *keyword ATTRIBUTE_UNUSED;
667 here = CPP_WRITTEN (pfile);
668 len = get_macro_name (pfile);
672 /* Copy out the name so we can pop the token buffer. */
673 len = CPP_WRITTEN (pfile) - here;
674 sym = (U_CHAR *) alloca (len + 1);
675 memcpy (sym, pfile->token_buffer + here, len);
677 CPP_SET_WRITTEN (pfile, here);
679 /* If the next character, with no intervening whitespace, is '(',
680 then this is a function-like macro. */
684 else if (c != '\n' && !is_hspace (c))
685 /* Otherwise, C99 requires white space after the name. We treat it
686 as an object-like macro if this happens, with a warning. */
687 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
689 def = create_definition (pfile, funlike);
693 if ((hp = cpp_lookup (pfile, sym, len)) != NULL)
697 /* Redefining a macro is ok if the definitions are the same. */
698 if (hp->type == T_MACRO)
699 ok = ! compare_defs (pfile, def, hp->value.defn);
700 /* Redefining a constant is ok with -D. */
701 else if (hp->type == T_CONST || hp->type == T_STDC)
702 ok = ! CPP_OPTIONS (pfile)->done_initializing;
703 /* Otherwise it's not ok. */
706 /* Print the warning or error if it's not ok. */
709 if (hp->type == T_POISON)
710 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
712 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
713 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
715 DEFINITION *d = hp->value.defn;
716 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
717 "this is the location of the previous definition");
720 if (hp->type != T_POISON)
722 /* Replace the old definition. */
723 if (hp->type == T_MACRO)
724 free_definition (hp->value.defn);
726 hp->value.defn = def;
730 cpp_install (pfile, sym, len, T_MACRO, (char *) def);
732 if (CPP_OPTIONS (pfile)->debug_output
733 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
734 dump_definition (pfile, sym, len, def);
735 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
736 pass_thru_directive (sym, len, pfile, keyword);
742 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
743 If BUFFER != NULL, then use the LENGTH characters in BUFFER
744 as the new input buffer.
745 Return the new buffer, or NULL on failure. */
748 cpp_push_buffer (pfile, buffer, length)
753 cpp_buffer *buf = CPP_BUFFER (pfile);
755 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
757 cpp_fatal (pfile, "macro or `#include' recursion too deep");
761 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
763 new->if_stack = pfile->if_stack;
764 new->cleanup = null_cleanup;
765 new->underflow = null_underflow;
766 new->buf = new->cur = buffer;
767 new->alimit = new->rlimit = buffer + length;
770 new->line_base = NULL;
772 CPP_BUFFER (pfile) = new;
777 cpp_pop_buffer (pfile)
780 cpp_buffer *buf = CPP_BUFFER (pfile);
782 cpp_ice (pfile, "mark active in cpp_pop_buffer");
783 (*buf->cleanup) (buf, pfile);
784 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
786 pfile->buffer_stack_depth--;
787 return CPP_BUFFER (pfile);
790 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
791 Pop the buffer when done. */
794 cpp_scan_buffer (pfile)
797 cpp_buffer *buffer = CPP_BUFFER (pfile);
798 enum cpp_token token;
799 if (CPP_OPTIONS (pfile)->no_output)
801 long old_written = CPP_WRITTEN (pfile);
802 /* In no-output mode, we can ignore everything but directives. */
805 if (! pfile->only_seen_white)
806 skip_rest_of_line (pfile);
807 token = cpp_get_token (pfile);
808 if (token == CPP_EOF) /* Should not happen ... */
810 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
812 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
813 != CPP_NULL_BUFFER (pfile))
814 cpp_pop_buffer (pfile);
818 CPP_SET_WRITTEN (pfile, old_written);
824 token = cpp_get_token (pfile);
825 if (token == CPP_EOF) /* Should not happen ... */
827 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
829 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile))
830 != CPP_NULL_BUFFER (pfile))
831 cpp_pop_buffer (pfile);
839 * Rescan a string (which may have escape marks) into pfile's buffer.
840 * Place the result in pfile->token_buffer.
842 * The input is copied before it is scanned, so it is safe to pass
843 * it something from the token_buffer that will get overwritten
844 * (because it follows CPP_WRITTEN). This is used by do_include.
848 cpp_expand_to_buffer (pfile, buf, length)
853 register cpp_buffer *ip;
859 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
863 /* Set up the input on the input stack. */
865 buf1 = (U_CHAR *) alloca (length + 1);
866 memcpy (buf1, buf, length);
869 ip = cpp_push_buffer (pfile, buf1, length);
874 /* Scan the input, create the output. */
875 save_no_output = CPP_OPTIONS (pfile)->no_output;
876 CPP_OPTIONS (pfile)->no_output = 0;
877 CPP_OPTIONS (pfile)->no_line_commands++;
878 cpp_scan_buffer (pfile);
879 CPP_OPTIONS (pfile)->no_line_commands--;
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;
931 if (CPP_OPTIONS (pfile)->no_line_commands
932 || CPP_OPTIONS (pfile)->no_output)
935 ip = cpp_file_buffer (pfile);
936 cpp_buf_line_and_col (ip, &line, NULL);
938 /* If the current file has not changed, we omit the #line if it would
939 appear to be a no-op, and we output a few newlines instead
940 if we want to increase the line number by a small amount.
941 We cannot do this if pfile->lineno is zero, because that means we
942 haven't output any line commands yet. (The very first line command
943 output is a `same_file' command.) */
944 if (file_change == same_file && pfile->lineno != 0)
946 if (line == pfile->lineno)
949 /* If the inherited line number is a little too small,
950 output some newlines instead of a #line command. */
951 if (line > pfile->lineno && line < pfile->lineno + 8)
953 CPP_RESERVE (pfile, 20);
954 while (line > pfile->lineno)
956 CPP_PUTC_Q (pfile, '\n');
963 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
964 CPP_PUTS_Q (pfile, "# ", 2);
966 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
967 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
969 quote_string (pfile, ip->nominal_fname);
970 if (file_change != same_file && file_change != rename_file)
972 CPP_PUTC_Q (pfile, ' ');
973 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
975 /* Tell cc1 if following text comes from a system header file. */
976 if (ip->system_header_p)
978 CPP_PUTC_Q (pfile, ' ');
979 CPP_PUTC_Q (pfile, '3');
981 #ifndef NO_IMPLICIT_EXTERN_C
982 /* Tell cc1plus if following text should be treated as C. */
983 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
985 CPP_PUTC_Q (pfile, ' ');
986 CPP_PUTC_Q (pfile, '4');
989 CPP_PUTC_Q (pfile, '\n');
990 pfile->lineno = line;
994 /* Like cpp_get_token, except that it does not read past end-of-line.
995 Also, horizontal space is skipped, and macros are popped. */
998 get_directive_token (pfile)
1001 long old_written = CPP_WRITTEN (pfile);
1002 enum cpp_token token;
1006 cpp_skip_hspace (pfile);
1007 if (PEEKC () == '\n')
1010 token = cpp_get_token (pfile);
1011 /* token could be hspace at the beginning of a macro. */
1012 if (token == CPP_HSPACE || token == CPP_COMMENT)
1014 CPP_SET_WRITTEN (pfile, old_written);
1018 /* token cannot be vspace, it would have been caught above. */
1019 if (token == CPP_VSPACE)
1021 cpp_ice (pfile, "VSPACE in get_directive_token");
1025 /* token cannot be POP unless the buffer is a macro buffer. */
1026 if (token != CPP_POP)
1029 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1031 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1035 /* We must pop the buffer by hand, or else cpp_get_token might
1036 hand us white space or newline on the next invocation. */
1037 cpp_pop_buffer (pfile);
1041 /* Handle #include and #import.
1042 This function expects to see "fname" or <fname> on the input.
1044 The input is normally in part of the output_buffer following
1045 CPP_WRITTEN, and will get overwritten by output_line_command.
1046 I.e. in input file specification has been popped by handle_directive.
1050 do_include (pfile, keyword)
1052 const struct directive *keyword;
1054 int importing = (keyword->type == T_IMPORT);
1055 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1056 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1057 int before; /* included before? */
1059 unsigned char *ftok;
1062 enum cpp_token token;
1064 /* Chain of dirs to search */
1065 struct include_hash *ihash;
1066 struct file_name_list *search_start;
1068 long old_written = CPP_WRITTEN (pfile);
1072 if (CPP_PEDANTIC (pfile))
1075 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1077 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1080 if (importing && CPP_OPTIONS (pfile)->warn_import
1081 && !CPP_OPTIONS (pfile)->inhibit_warnings
1082 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1084 pfile->import_warning = 1;
1086 "#import is obsolete, use an #ifndef wrapper in the header file");
1089 pfile->parsing_include_directive++;
1090 token = get_directive_token (pfile);
1091 pfile->parsing_include_directive--;
1093 if (token == CPP_STRING)
1095 if (pfile->token_buffer[old_written] == '<')
1099 else if (token == CPP_NAME)
1101 /* Support '#include xyz' like VAX-C. It is taken as
1102 '#include <xyz.h>' and generates a warning. */
1104 "`#include filename' is obsolete, use `#include <filename.h>'");
1107 /* Append the missing `.h' to the name. */
1108 CPP_PUTS (pfile, ".h", 2);
1114 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1115 CPP_SET_WRITTEN (pfile, old_written);
1116 skip_rest_of_line (pfile);
1120 flen = CPP_WRITTEN (pfile) - old_written;
1121 ftok = (unsigned char *) alloca (flen + 1);
1122 memcpy (ftok, pfile->token_buffer + old_written, flen);
1125 if (get_directive_token (pfile) != CPP_VSPACE)
1127 cpp_error (pfile, "junk at end of `#include'");
1128 skip_rest_of_line (pfile);
1131 CPP_SET_WRITTEN (pfile, old_written);
1135 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1139 if (CPP_OPTIONS (pfile)->dump_includes)
1140 pass_thru_directive (ftok,
1143 - ((token == CPP_NAME) ? 2 : 0)
1148 if (token == CPP_STRING)
1158 for (fp = CPP_BUFFER (pfile);
1159 fp != CPP_NULL_BUFFER (pfile);
1160 fp = CPP_PREV_BUFFER (fp))
1161 if (fp->fname != NULL)
1164 if (fp == CPP_NULL_BUFFER (pfile))
1166 cpp_ice (pfile, "fp == NULL_BUFFER in do_include");
1170 /* For #include_next, skip in the search path past the dir in which the
1171 containing file was found. Treat files specified using an absolute path
1172 as if there are no more directories to search. Treat the primary source
1173 file like any other included source, but generate a warning. */
1174 if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
1176 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1177 search_start = fp->ihash->foundhere->next;
1182 cpp_warning (pfile, "#include_next in primary source file");
1185 search_start = CPP_OPTIONS (pfile)->bracket_include;
1188 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1191 search_start = fp->actual_dir;
1194 search_start = CPP_OPTIONS (pfile)->quote_include;
1200 cpp_error (pfile, "No include path in which to find %s", ftok);
1204 fd = find_include_file (pfile, ftok, search_start, &ihash, &before);
1211 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1212 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1213 (pfile->system_include_depth > 0)))
1215 if (!angle_brackets)
1216 deps_add_dep (pfile->deps, ftok);
1220 struct file_name_list *ptr;
1221 /* If requested as a system header, assume it belongs in
1222 the first system header directory. */
1223 if (CPP_OPTIONS (pfile)->bracket_include)
1224 ptr = CPP_OPTIONS (pfile)->bracket_include;
1226 ptr = CPP_OPTIONS (pfile)->quote_include;
1228 p = (char *) alloca (strlen (ptr->name)
1229 + strlen (ftok) + 2);
1230 if (*ptr->name != '\0')
1232 strcpy (p, ptr->name);
1236 deps_add_dep (pfile->deps, p);
1239 /* If -M was specified, and this header file won't be added to
1240 the dependency list, then don't count this as an error,
1241 because we can still produce correct output. Otherwise, we
1242 can't produce correct output, because there may be
1243 dependencies we need inside the missing file, and we don't
1244 know what directory this missing file exists in. */
1245 else if (CPP_PRINT_DEPS (pfile)
1246 && (CPP_PRINT_DEPS (pfile)
1247 <= (angle_brackets || (pfile->system_include_depth > 0))))
1248 cpp_warning (pfile, "No include path in which to find %s", ftok);
1250 cpp_error_from_errno (pfile, ftok);
1255 /* For -M, add the file to the dependencies on its first inclusion. */
1256 if (!before && (CPP_PRINT_DEPS (pfile)
1257 > (angle_brackets || (pfile->system_include_depth > 0))))
1258 deps_add_dep (pfile->deps, ihash->name);
1260 /* Handle -H option. */
1261 if (CPP_OPTIONS(pfile)->print_include_names)
1263 fp = CPP_BUFFER (pfile);
1264 while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
1266 fprintf (stderr, " %s\n", ihash->name);
1269 /* Actually process the file */
1272 ihash->control_macro = "";
1274 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1281 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1283 if (finclude (pfile, fd, ihash))
1285 output_line_command (pfile, enter_file);
1286 pfile->only_seen_white = 2;
1292 /* Subroutine of do_line. Read next token from PFILE without adding it to
1293 the output buffer. If it is a number between 1 and 4, store it in *NUM
1294 and return 1; otherwise, return 0 and complain if we aren't at the end
1295 of the directive. */
1298 read_line_number (pfile, num)
1302 long save_written = CPP_WRITTEN (pfile);
1303 U_CHAR *p = pfile->token_buffer + save_written;
1304 enum cpp_token token = get_directive_token (pfile);
1305 CPP_SET_WRITTEN (pfile, save_written);
1307 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1314 if (token != CPP_VSPACE && token != CPP_EOF)
1315 cpp_error (pfile, "invalid format `#line' command");
1320 /* Interpret #line command.
1321 Note that the filename string (if any) is treated as if it were an
1322 include filename. That means no escape handling. */
1325 do_line (pfile, keyword)
1327 const struct directive *keyword ATTRIBUTE_UNUSED;
1329 cpp_buffer *ip = CPP_BUFFER (pfile);
1331 long old_written = CPP_WRITTEN (pfile);
1332 enum file_change_code file_change = same_file;
1333 enum cpp_token token;
1336 token = get_directive_token (pfile);
1338 if (token != CPP_NUMBER)
1340 cpp_error (pfile, "token after `#line' is not an integer");
1341 goto bad_line_directive;
1344 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1347 cpp_error (pfile, "token after `#line' is not an integer");
1348 goto bad_line_directive;
1350 CPP_SET_WRITTEN (pfile, old_written);
1352 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1353 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1355 token = get_directive_token (pfile);
1357 if (token == CPP_STRING)
1359 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1360 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1361 int action_number = 0;
1363 file_change = rename_file;
1365 if (read_line_number (pfile, &action_number))
1367 if (CPP_PEDANTIC (pfile))
1368 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1370 if (action_number == 1)
1372 file_change = enter_file;
1373 read_line_number (pfile, &action_number);
1375 else if (action_number == 2)
1377 file_change = leave_file;
1378 read_line_number (pfile, &action_number);
1380 if (action_number == 3)
1382 ip->system_header_p = 1;
1383 read_line_number (pfile, &action_number);
1385 if (action_number == 4)
1387 ip->system_header_p = 2;
1388 read_line_number (pfile, &action_number);
1394 if (strcmp (fname, ip->nominal_fname))
1396 const char *newname, *oldname;
1397 if (!strcmp (fname, ip->fname))
1398 newname = ip->fname;
1399 else if (ip->last_nominal_fname
1400 && !strcmp (fname, ip->last_nominal_fname))
1401 newname = ip->last_nominal_fname;
1403 newname = xstrdup (fname);
1405 oldname = ip->nominal_fname;
1406 ip->nominal_fname = newname;
1408 if (ip->last_nominal_fname
1409 && ip->last_nominal_fname != oldname
1410 && ip->last_nominal_fname != newname
1411 && ip->last_nominal_fname != ip->fname)
1412 free ((void *) ip->last_nominal_fname);
1414 if (newname == ip->fname)
1415 ip->last_nominal_fname = NULL;
1417 ip->last_nominal_fname = oldname;
1420 else if (token != CPP_VSPACE && token != CPP_EOF)
1422 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1423 goto bad_line_directive;
1426 /* The Newline at the end of this line remains to be processed.
1427 To put the next line at the specified line number,
1428 we must store a line number now that is one less. */
1429 ip->lineno = new_lineno - 1;
1430 CPP_SET_WRITTEN (pfile, old_written);
1431 output_line_command (pfile, file_change);
1435 skip_rest_of_line (pfile);
1436 CPP_SET_WRITTEN (pfile, old_written);
1440 /* Remove the definition of a symbol from the symbol table.
1441 According to the C standard, it is not an error to undef
1442 something that has no definitions. */
1444 do_undef (pfile, keyword)
1446 const struct directive *keyword;
1450 U_CHAR *buf, *name, *limit;
1452 long here = CPP_WRITTEN (pfile);
1453 enum cpp_token token;
1455 cpp_skip_hspace (pfile);
1457 if (! is_idstart(c))
1459 cpp_error (pfile, "token after #undef is not an identifier");
1460 skip_rest_of_line (pfile);
1464 parse_name (pfile, c);
1465 buf = pfile->token_buffer + here;
1466 limit = CPP_PWRITTEN(pfile);
1468 /* Copy out the token so we can pop the token buffer. */
1470 name = (U_CHAR *) alloca (len + 1);
1471 memcpy (name, buf, len);
1474 token = get_directive_token (pfile);
1475 if (token != CPP_VSPACE)
1477 cpp_pedwarn (pfile, "junk on line after #undef");
1478 skip_rest_of_line (pfile);
1480 CPP_SET_WRITTEN (pfile, here);
1482 while ((hp = cpp_lookup (pfile, name, len)) != NULL)
1484 /* If we are generating additional info for debugging (with -g) we
1485 need to pass through all effective #undef commands. */
1486 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1487 pass_thru_directive (name, len, pfile, keyword);
1488 if (hp->type == T_POISON)
1489 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1492 if (hp->type != T_MACRO)
1493 cpp_warning (pfile, "undefining `%s'", hp->name);
1501 /* Wrap do_undef for -U processing. */
1503 cpp_undef (pfile, macro)
1507 /* Copy the string so we can append a newline. */
1508 size_t len = strlen (macro);
1509 U_CHAR *buf = alloca (len + 2);
1510 memcpy (buf, macro, len);
1512 buf[len + 1] = '\0';
1513 if (cpp_push_buffer (pfile, buf, len + 1))
1515 do_undef (pfile, NULL);
1516 cpp_pop_buffer (pfile);
1521 * Report an error detected by the program we are processing.
1522 * Use the text of the line in the error message.
1523 * (We use error because it prints the filename & line#.)
1527 do_error (pfile, keyword)
1529 const struct directive *keyword ATTRIBUTE_UNUSED;
1531 U_CHAR *text, *limit;
1533 cpp_skip_hspace (pfile);
1534 text = CPP_BUFFER (pfile)->cur;
1535 skip_rest_of_line (pfile);
1536 limit = CPP_BUFFER (pfile)->cur;
1538 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1543 * Report a warning detected by the program we are processing.
1544 * Use the text of the line in the warning message, then continue.
1548 do_warning (pfile, keyword)
1550 const struct directive *keyword ATTRIBUTE_UNUSED;
1552 U_CHAR *text, *limit;
1554 cpp_skip_hspace (pfile);
1555 text = CPP_BUFFER (pfile)->cur;
1556 skip_rest_of_line (pfile);
1557 limit = CPP_BUFFER (pfile)->cur;
1559 if (CPP_PEDANTIC (pfile))
1560 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1562 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1566 /* Report program identification. */
1569 do_ident (pfile, keyword)
1571 const struct directive *keyword ATTRIBUTE_UNUSED;
1573 long old_written = CPP_WRITTEN (pfile);
1575 /* Allow #ident in system headers, since that's not user's fault. */
1576 if (CPP_PEDANTIC (pfile))
1577 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1579 CPP_PUTS (pfile, "#ident ", 7);
1581 /* Next token should be a string constant. */
1582 if (get_directive_token (pfile) == CPP_STRING)
1583 /* And then a newline. */
1584 if (get_directive_token (pfile) == CPP_VSPACE)
1585 /* Good - ship it. */
1588 cpp_error (pfile, "invalid #ident");
1589 skip_rest_of_line (pfile);
1590 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1595 /* Pragmata handling. We handle some of these, and pass the rest on
1596 to the front end. C99 defines three pragmas and says that no macro
1597 expansion is to be performed on them; whether or not macro
1598 expansion happens for other pragmas is implementation defined.
1599 This implementation never macro-expands the text after #pragma.
1601 We currently do not support the _Pragma operator. Support for that
1602 has to be coordinated with the front end. Proposed implementation:
1603 both #pragma blah blah and _Pragma("blah blah") become
1604 __builtin_pragma(blah blah) and we teach the parser about that. */
1606 /* Sub-handlers for the pragmas needing treatment here.
1607 They return 1 if the token buffer is to be popped, 0 if not. */
1608 static int do_pragma_once PARAMS ((cpp_reader *));
1609 static int do_pragma_implementation PARAMS ((cpp_reader *));
1610 static int do_pragma_poison PARAMS ((cpp_reader *));
1611 static int do_pragma_default PARAMS ((cpp_reader *));
1614 do_pragma (pfile, keyword)
1616 const struct directive *keyword ATTRIBUTE_UNUSED;
1621 enum cpp_token token;
1623 here = CPP_WRITTEN (pfile);
1624 CPP_PUTS (pfile, "#pragma ", 8);
1626 key = CPP_WRITTEN (pfile);
1627 pfile->no_macro_expand++;
1628 token = get_directive_token (pfile);
1629 if (token != CPP_NAME)
1631 if (token == CPP_VSPACE)
1637 buf = pfile->token_buffer + key;
1638 CPP_PUTC (pfile, ' ');
1640 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1642 pop = do_pragma_once (pfile);
1643 else if (tokis ("implementation"))
1644 pop = do_pragma_implementation (pfile);
1645 else if (tokis ("poison"))
1646 pop = do_pragma_poison (pfile);
1648 pop = do_pragma_default (pfile);
1651 if (get_directive_token (pfile) != CPP_VSPACE)
1655 CPP_SET_WRITTEN (pfile, here);
1656 pfile->no_macro_expand--;
1660 cpp_error (pfile, "malformed #pragma directive");
1661 skip_rest_of_line (pfile);
1663 CPP_SET_WRITTEN (pfile, here);
1664 pfile->no_macro_expand--;
1669 do_pragma_default (pfile)
1672 while (get_directive_token (pfile) != CPP_VSPACE)
1673 CPP_PUTC (pfile, ' ');
1678 do_pragma_once (pfile)
1681 cpp_buffer *ip = CPP_BUFFER (pfile);
1683 if (ip->fname == NULL)
1685 cpp_ice (pfile, "ip->fname == NULL in do_pragma_once");
1689 /* Allow #pragma once in system headers, since that's not the user's
1691 if (!ip->system_header_p)
1692 cpp_warning (pfile, "`#pragma once' is obsolete");
1694 if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
1695 cpp_warning (pfile, "`#pragma once' outside include file");
1697 ip->ihash->control_macro = ""; /* never repeat */
1703 do_pragma_implementation (pfile)
1706 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1707 been included yet. */
1708 struct include_hash *ptr;
1709 enum cpp_token token;
1710 long written = CPP_WRITTEN (pfile);
1714 token = get_directive_token (pfile);
1715 if (token == CPP_VSPACE)
1717 else if (token != CPP_STRING)
1719 cpp_error (pfile, "malformed #pragma implementation");
1723 name = pfile->token_buffer + written + 1;
1724 copy = xstrdup (name);
1725 copy[strlen(copy)] = '\0'; /* trim trailing quote */
1727 ptr = include_hash (pfile, copy, 0);
1730 "`#pragma implementation' for `%s' appears after file is included",
1737 do_pragma_poison (pfile)
1740 /* Poison these symbols so that all subsequent usage produces an
1746 enum cpp_token token;
1748 /* As a rule, don't include #pragma poison commands in output,
1749 unless the user asks for them. */
1750 writeit = (CPP_OPTIONS (pfile)->debug_output
1751 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1752 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1756 written = CPP_WRITTEN (pfile);
1757 token = get_directive_token (pfile);
1758 if (token == CPP_VSPACE)
1760 if (token != CPP_NAME)
1762 cpp_error (pfile, "invalid #pragma poison directive");
1763 skip_rest_of_line (pfile);
1767 p = pfile->token_buffer + written;
1769 if ((hp = cpp_lookup (pfile, p, len)))
1771 if (hp->type != T_POISON)
1773 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1774 free_definition (hp->value.defn);
1776 hp->type = T_POISON;
1780 cpp_install (pfile, p, len, T_POISON, 0);
1782 CPP_PUTC (pfile, ' ');
1787 #ifdef SCCS_DIRECTIVE
1788 /* Just ignore #sccs, on systems where we define it at all. */
1791 do_sccs (pfile, keyword)
1793 const struct directive *keyword ATTRIBUTE_UNUSED;
1795 if (CPP_PEDANTIC (pfile))
1796 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1797 skip_rest_of_line (pfile);
1802 /* We've found an `#if' directive. If the only thing before it in
1803 this file is white space, and if it is of the form
1804 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1805 for inclusion of this file. (See redundant_include_p in cppfiles.c
1806 for an explanation of controlling macros.) If so, return a
1807 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1810 detect_if_not_defined (pfile)
1813 U_CHAR *control_macro = 0;
1815 if (pfile->only_seen_white == 2)
1818 enum cpp_token token;
1821 int need_rparen = 0;
1823 /* Save state required for restore. */
1824 pfile->no_macro_expand++;
1825 parse_set_mark (pfile);
1826 base_offset = CPP_WRITTEN (pfile);
1829 if (get_directive_token (pfile) != CPP_OTHER
1830 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1831 || CPP_PWRITTEN (pfile)[-1] != '!')
1834 /* ...then `defined', */
1835 token_offset = CPP_WRITTEN (pfile);
1836 token = get_directive_token (pfile);
1837 if (token != CPP_NAME)
1839 ident = pfile->token_buffer + token_offset;
1840 CPP_NUL_TERMINATE (pfile);
1841 if (strcmp (ident, "defined"))
1844 /* ...then an optional '(' and the name, */
1845 token_offset = CPP_WRITTEN (pfile);
1846 token = get_directive_token (pfile);
1847 if (token == CPP_LPAREN)
1849 token_offset = CPP_WRITTEN (pfile);
1850 token = get_directive_token (pfile);
1851 if (token != CPP_NAME)
1855 else if (token != CPP_NAME)
1858 ident = pfile->token_buffer + token_offset;
1859 CPP_NUL_TERMINATE (pfile);
1861 /* ...then the ')', if necessary, */
1862 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1863 /* ...and make sure there's nothing else on the line. */
1864 && get_directive_token (pfile) == CPP_VSPACE)
1865 control_macro = xstrdup (ident);
1868 CPP_SET_WRITTEN (pfile, base_offset);
1869 pfile->no_macro_expand--;
1870 parse_goto_mark (pfile);
1873 return control_macro;
1877 * handle #if command by
1878 * 1) inserting special `defined' keyword into the hash table
1879 * that gets turned into 0 or 1 by special_symbol (thus,
1880 * if the luser has a symbol called `defined' already, it won't
1881 * work inside the #if command)
1882 * 2) rescan the input into a temporary output buffer
1883 * 3) pass the output buffer to the yacc parser and collect a value
1884 * 4) clean up the mess left from steps 1 and 2.
1885 * 5) call conditional_skip to skip til the next #endif (etc.),
1886 * or not, depending on the value from step 3.
1890 do_if (pfile, keyword)
1892 const struct directive *keyword ATTRIBUTE_UNUSED;
1894 U_CHAR *control_macro = detect_if_not_defined (pfile);
1895 HOST_WIDEST_INT value = eval_if_expression (pfile);
1896 conditional_skip (pfile, value == 0, T_IF, control_macro);
1901 * handle a #elif directive by not changing if_stack either.
1902 * see the comment above do_else.
1906 do_elif (pfile, keyword)
1908 const struct directive *keyword ATTRIBUTE_UNUSED;
1910 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1912 cpp_error (pfile, "`#elif' not within a conditional");
1917 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1919 cpp_error (pfile, "`#elif' after `#else'");
1920 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1921 "the conditional began here");
1923 pfile->if_stack->type = T_ELIF;
1926 if (pfile->if_stack->if_succeeded)
1927 skip_if_group (pfile);
1930 HOST_WIDEST_INT value = eval_if_expression (pfile);
1932 skip_if_group (pfile);
1935 ++pfile->if_stack->if_succeeded; /* continue processing input */
1936 output_line_command (pfile, same_file);
1943 * evaluate a #if expression in BUF, of length LENGTH,
1944 * then parse the result as a C expression and return the value as an int.
1947 static HOST_WIDEST_INT
1948 eval_if_expression (pfile)
1951 HOST_WIDEST_INT value;
1952 long old_written = CPP_WRITTEN (pfile);
1954 pfile->parsing_if_directive++;
1955 value = cpp_parse_expr (pfile);
1956 pfile->parsing_if_directive--;
1958 skip_rest_of_line (pfile);
1959 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1965 * routine to handle ifdef/ifndef. Try to look up the symbol,
1966 * then do or don't skip to the #endif/#else/#elif depending
1967 * on what directive is actually being processed.
1971 do_xifdef (pfile, keyword)
1973 const struct directive *keyword;
1976 cpp_buffer *ip = CPP_BUFFER (pfile);
1979 enum cpp_token token;
1980 int start_of_file = 0;
1981 U_CHAR *control_macro = 0;
1982 int old_written = CPP_WRITTEN (pfile);
1984 /* Detect a #ifndef at start of file (not counting comments). */
1985 if (ip->fname != 0 && keyword->type == T_IFNDEF)
1986 start_of_file = pfile->only_seen_white == 2;
1988 pfile->no_macro_expand++;
1989 token = get_directive_token (pfile);
1990 pfile->no_macro_expand--;
1992 ident = pfile->token_buffer + old_written;
1993 ident_length = CPP_WRITTEN (pfile) - old_written;
1994 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1996 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1998 skip = (keyword->type == T_IFDEF);
1999 if (! CPP_TRADITIONAL (pfile))
2000 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
2002 else if (token == CPP_NAME)
2004 skip = cpp_defined (pfile, ident, ident_length);
2005 if (keyword->type == T_IFDEF)
2008 if (start_of_file && !skip)
2010 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
2011 bcopy (ident, control_macro, ident_length + 1);
2016 skip = (keyword->type == T_IFDEF);
2017 if (! CPP_TRADITIONAL (pfile))
2018 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2021 if (!CPP_TRADITIONAL (pfile))
2023 cpp_skip_hspace (pfile);
2025 if (c != EOF && c != '\n')
2026 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2028 skip_rest_of_line (pfile);
2030 conditional_skip (pfile, skip, T_IF, control_macro);
2034 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2035 If this is a #ifndef starting at the beginning of a file,
2036 CONTROL_MACRO is the macro name tested by the #ifndef.
2037 Otherwise, CONTROL_MACRO is 0. */
2040 conditional_skip (pfile, skip, type, control_macro)
2043 enum node_type type;
2044 U_CHAR *control_macro;
2046 IF_STACK_FRAME *temp;
2048 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
2049 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
2050 temp->lineno = CPP_BUFFER (pfile)->lineno;
2051 temp->next = pfile->if_stack;
2052 temp->control_macro = control_macro;
2053 pfile->if_stack = temp;
2055 pfile->if_stack->type = type;
2058 skip_if_group (pfile);
2061 ++pfile->if_stack->if_succeeded;
2062 output_line_command (pfile, same_file);
2066 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2067 return 0 if skipping should continue, 1 if it should halt. Also
2068 adjusts the if_stack as appropriate.
2069 The `#' has been read, but not the identifier. */
2072 consider_directive_while_skipping (pfile, stack)
2074 IF_STACK_FRAME *stack;
2076 long ident_len, ident;
2077 const struct directive *kt;
2078 IF_STACK_FRAME *temp;
2080 cpp_skip_hspace (pfile);
2082 ident = CPP_WRITTEN (pfile);
2083 parse_name (pfile, GETC());
2084 ident_len = CPP_WRITTEN (pfile) - ident;
2086 CPP_SET_WRITTEN (pfile, ident);
2088 for (kt = directive_table; kt->length >= 0; kt++)
2089 if (kt->length == ident_len
2090 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2096 temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
2097 temp->next = pfile->if_stack;
2098 pfile->if_stack = temp;
2099 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
2100 temp->type = kt->type;
2104 if (pfile->if_stack != stack)
2105 validate_else (pfile, "#else");
2108 if (pfile->if_stack == stack)
2112 pfile->if_stack->type = kt->type;
2117 if (pfile->if_stack != stack)
2118 validate_else (pfile, "#endif");
2120 if (pfile->if_stack == stack)
2123 temp = pfile->if_stack;
2124 pfile->if_stack = temp->next;
2132 /* Don't let erroneous code go by. */
2133 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2134 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2138 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2139 * leaves input ptr at the sharp sign found.
2142 skip_if_group (pfile)
2146 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
2147 U_CHAR *beg_of_line;
2150 old_written = CPP_WRITTEN (pfile);
2154 beg_of_line = CPP_BUFFER (pfile)->cur;
2156 if (! CPP_TRADITIONAL (pfile))
2157 cpp_skip_hspace (pfile);
2161 CPP_BUMP_LINE (pfile);
2166 if (consider_directive_while_skipping (pfile, save_if_stack))
2170 return; /* Caller will issue error. */
2173 skip_rest_of_line (pfile);
2177 return; /* Caller will issue error. */
2179 CPP_BUMP_LINE (pfile);
2182 /* Back up to the beginning of this line. Caller will process the
2184 CPP_BUFFER (pfile)->cur = beg_of_line;
2185 pfile->only_seen_white = 1;
2189 * handle a #else directive. Do this by just continuing processing
2190 * without changing if_stack ; this is so that the error message
2191 * for missing #endif's etc. will point to the original #if. It
2192 * is possible that something different would be better.
2196 do_else (pfile, keyword)
2198 const struct directive *keyword ATTRIBUTE_UNUSED;
2200 validate_else (pfile, "#else");
2201 skip_rest_of_line (pfile);
2203 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2205 cpp_error (pfile, "`#else' not within a conditional");
2210 /* #ifndef can't have its special treatment for containing the whole file
2211 if it has a #else clause. */
2212 pfile->if_stack->control_macro = 0;
2214 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2216 cpp_error (pfile, "`#else' after `#else'");
2217 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2218 "the conditional began here");
2220 pfile->if_stack->type = T_ELSE;
2223 if (pfile->if_stack->if_succeeded)
2224 skip_if_group (pfile);
2227 ++pfile->if_stack->if_succeeded; /* continue processing input */
2228 output_line_command (pfile, same_file);
2234 * unstack after #endif command
2238 do_endif (pfile, keyword)
2240 const struct directive *keyword ATTRIBUTE_UNUSED;
2242 validate_else (pfile, "#endif");
2243 skip_rest_of_line (pfile);
2245 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2246 cpp_error (pfile, "`#endif' not within a conditional");
2249 IF_STACK_FRAME *temp = pfile->if_stack;
2250 pfile->if_stack = temp->next;
2251 if (temp->control_macro != 0)
2253 /* This #endif matched a #ifndef at the start of the file.
2254 See if it is at the end of the file. */
2257 parse_set_mark (pfile);
2261 cpp_skip_hspace (pfile);
2266 parse_goto_mark (pfile);
2270 /* This #endif ends a #ifndef
2271 that contains all of the file (aside from whitespace).
2272 Arrange not to include the file again
2273 if the macro that was tested is defined. */
2274 struct cpp_buffer *ip;
2275 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2276 if (ip->fname != NULL)
2278 ip->ihash->control_macro = (char *) temp->control_macro;
2282 output_line_command (pfile, same_file);
2287 /* Issue -pedantic warning for text which is not a comment following
2288 an #else or #endif. Do not warn in system headers, as this is harmless
2289 and very common on old systems. */
2292 validate_else (pfile, directive)
2294 const char *directive;
2296 if (! CPP_PEDANTIC (pfile))
2299 cpp_skip_hspace (pfile);
2300 if (PEEKC () != '\n')
2302 "text following `%s' violates ANSI standard", directive);
2305 /* Convert T_IF, etc. to a string. Used in error messages. */
2307 if_directive_name (pfile, ifs)
2309 struct if_stack *ifs;
2313 case T_IF: return "#if";
2314 case T_IFDEF: return "#ifdef";
2315 case T_IFNDEF: return "#ifndef";
2316 case T_ELIF: return "#elif";
2317 case T_ELSE: return "#else";
2319 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2324 /* Get the next token, and add it to the text in pfile->token_buffer.
2325 Return the kind of token we got. */
2328 cpp_get_token (pfile)
2331 register int c, c2, c3;
2332 enum cpp_token token;
2333 struct cpp_options *opts = CPP_OPTIONS (pfile);
2339 if (CPP_BUFFER (pfile)->manual_pop)
2340 /* If we've been reading from redirected input, the
2341 frontend will pop the buffer. */
2343 else if (CPP_BUFFER (pfile)->seen_eof)
2345 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == CPP_NULL_BUFFER (pfile))
2348 cpp_pop_buffer (pfile);
2353 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2354 struct if_stack *ifs, *nifs;
2356 /* Unwind the conditional stack and generate error messages. */
2357 for (ifs = pfile->if_stack;
2358 ifs != CPP_BUFFER (pfile)->if_stack;
2361 cpp_error_with_line (pfile, ifs->lineno, -1,
2362 "unterminated `%s' conditional",
2363 if_directive_name (pfile, ifs));
2368 pfile->if_stack = ifs;
2370 if (CPP_BUFFER (pfile)->nominal_fname
2371 && next_buf != CPP_NULL_BUFFER (pfile))
2373 /* We're about to return from an #include file.
2374 Emit #line information now (as part of the CPP_POP) result.
2375 But the #line refers to the file we will pop to. */
2376 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2377 CPP_BUFFER (pfile) = next_buf;
2378 pfile->input_stack_listing_current = 0;
2379 output_line_command (pfile, leave_file);
2380 CPP_BUFFER (pfile) = cur_buffer;
2383 CPP_BUFFER (pfile)->seen_eof = 1;
2392 if (PEEKC () == '=')
2396 if (opts->discard_comments)
2397 c = skip_comment (pfile, c);
2399 c = copy_comment (pfile, c);
2403 /* Comments are equivalent to spaces.
2404 For -traditional, a comment is equivalent to nothing. */
2405 if (opts->traditional || !opts->discard_comments)
2409 CPP_PUTC (pfile, c);
2414 if (pfile->parsing_if_directive)
2416 cpp_skip_hspace (pfile);
2417 parse_assertion (pfile);
2418 return CPP_ASSERTION;
2421 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2423 CPP_RESERVE (pfile, 3);
2424 CPP_PUTC_Q (pfile, '#');
2425 CPP_NUL_TERMINATE_Q (pfile);
2426 if (PEEKC () != '#')
2427 return CPP_STRINGIZE;
2430 CPP_PUTC_Q (pfile, '#');
2431 CPP_NUL_TERMINATE_Q (pfile);
2432 return CPP_TOKPASTE;
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;
2450 parse_string (pfile, c);
2451 pfile->only_seen_white = 0;
2452 return c == '\'' ? CPP_CHAR : CPP_STRING;
2455 if (!opts->dollars_in_ident)
2460 if (opts->cplusplus && PEEKC () == ':')
2468 if (c2 == c || c2 == '=')
2477 if (PEEKC () == '=')
2483 if (c2 == '-' && opts->chill)
2484 goto comment; /* Chill style comment */
2485 if (c2 == '-' || c2 == '=')
2489 if (opts->cplusplus && PEEKN (1) == '*')
2491 /* In C++, there's a ->* operator. */
2493 pfile->only_seen_white = 0;
2494 CPP_RESERVE (pfile, 4);
2495 CPP_PUTC_Q (pfile, c);
2496 CPP_PUTC_Q (pfile, GETC ());
2497 CPP_PUTC_Q (pfile, GETC ());
2498 CPP_NUL_TERMINATE_Q (pfile);
2506 if (pfile->parsing_include_directive)
2510 CPP_PUTC (pfile, c);
2514 if (c == '\n' || c == EOF)
2517 "missing '>' in `#include <FILENAME>'");
2522 if (!CPP_BUFFER (pfile)->has_escapes)
2524 /* Backslash newline is replaced by nothing. */
2525 CPP_ADJUST_WRITTEN (pfile, -1);
2526 CPP_BUMP_LINE (pfile);
2530 /* We might conceivably get \r- or \r<space> in
2531 here. Just delete 'em. */
2533 if (d != '-' && d != ' ')
2534 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2535 CPP_ADJUST_WRITTEN (pfile, -1);
2541 /* else fall through */
2546 /* GNU C++ supports MIN and MAX operators <? and >?. */
2547 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2550 CPP_RESERVE (pfile, 4);
2551 CPP_PUTC (pfile, c);
2552 CPP_PUTC (pfile, c2);
2555 CPP_PUTC_Q (pfile, GETC ());
2556 CPP_NUL_TERMINATE_Q (pfile);
2557 pfile->only_seen_white = 0;
2564 CPP_RESERVE(pfile, 2);
2565 CPP_PUTC_Q (pfile, '.');
2570 /* In C++ there's a .* operator. */
2571 if (opts->cplusplus && c2 == '*')
2574 if (c2 == '.' && PEEKN(1) == '.')
2576 CPP_RESERVE(pfile, 4);
2577 CPP_PUTC_Q (pfile, '.');
2578 CPP_PUTC_Q (pfile, '.');
2579 CPP_PUTC_Q (pfile, '.');
2581 CPP_NUL_TERMINATE_Q (pfile);
2582 pfile->only_seen_white = 0;
2589 pfile->only_seen_white = 0;
2590 CPP_RESERVE(pfile, 3);
2591 CPP_PUTC_Q (pfile, c);
2592 CPP_PUTC_Q (pfile, GETC ());
2593 CPP_NUL_TERMINATE_Q (pfile);
2598 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2600 CPP_PUTC (pfile, c);
2602 parse_string (pfile, c);
2603 pfile->only_seen_white = 0;
2604 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2608 case '0': case '1': case '2': case '3': case '4':
2609 case '5': case '6': case '7': case '8': case '9':
2614 CPP_RESERVE (pfile, 2);
2615 CPP_PUTC_Q (pfile, c);
2619 if (!is_numchar(c) && c != '.'
2620 && ((c2 != 'e' && c2 != 'E'
2621 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2622 || (c != '+' && c != '-')))
2627 CPP_NUL_TERMINATE_Q (pfile);
2628 pfile->only_seen_white = 0;
2630 case 'b': case 'c': case 'd': case 'h': case 'o':
2631 case 'B': case 'C': case 'D': case 'H': case 'O':
2632 if (opts->chill && PEEKC () == '\'')
2634 pfile->only_seen_white = 0;
2635 CPP_RESERVE (pfile, 2);
2636 CPP_PUTC_Q (pfile, c);
2637 CPP_PUTC_Q (pfile, '\'');
2643 goto chill_number_eof;
2646 CPP_PUTC (pfile, c);
2650 CPP_RESERVE (pfile, 2);
2651 CPP_PUTC_Q (pfile, c);
2652 CPP_NUL_TERMINATE_Q (pfile);
2659 CPP_NUL_TERMINATE (pfile);
2666 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2667 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2668 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2669 case 'x': case 'y': case 'z':
2670 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2671 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2672 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2677 unsigned char *ident;
2678 int before_name_written = CPP_WRITTEN (pfile);
2680 parse_name (pfile, c);
2681 pfile->only_seen_white = 0;
2682 if (pfile->no_macro_expand)
2684 ident = pfile->token_buffer + before_name_written;
2685 ident_len = CPP_PWRITTEN (pfile) - ident;
2686 hp = cpp_lookup (pfile, ident, ident_len);
2689 if (hp->type == T_DISABLED)
2691 if (pfile->output_escapes)
2692 { /* Return "\r-IDENT", followed by '\0'. */
2694 CPP_RESERVE (pfile, 3);
2695 ident = pfile->token_buffer + before_name_written;
2696 CPP_ADJUST_WRITTEN (pfile, 2);
2697 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2704 /* If macro wants an arglist, verify that a '(' follows. */
2705 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2707 int macbuf_whitespace = 0;
2709 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2711 U_CHAR *point = CPP_BUFFER (pfile)->cur;
2714 cpp_skip_hspace (pfile);
2721 if (point != CPP_BUFFER (pfile)->cur)
2722 macbuf_whitespace = 1;
2726 goto not_macro_call;
2727 cpp_pop_buffer (pfile);
2730 parse_set_mark (pfile);
2733 cpp_skip_hspace (pfile);
2740 parse_goto_mark (pfile);
2746 if (macbuf_whitespace)
2747 CPP_PUTC (pfile, ' ');
2751 /* This is now known to be a macro call.
2752 Expand the macro, reading arguments as needed,
2753 and push the expansion on the input stack. */
2754 macroexpand (pfile, hp);
2755 CPP_SET_WRITTEN (pfile, before_name_written);
2759 case ' ': case '\t': case '\v':
2762 CPP_PUTC (pfile, c);
2764 if (c == EOF || !is_hspace(c))
2771 if (CPP_BUFFER (pfile)->has_escapes)
2776 if (pfile->output_escapes)
2777 CPP_PUTS (pfile, "\r-", 2);
2778 parse_name (pfile, GETC ());
2783 CPP_RESERVE (pfile, 2);
2784 if (pfile->output_escapes)
2785 CPP_PUTC_Q (pfile, '\r');
2786 CPP_PUTC_Q (pfile, c);
2791 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2797 /* Backslash newline is ignored. */
2798 CPP_BUMP_LINE (pfile);
2803 CPP_PUTC (pfile, c);
2804 if (pfile->only_seen_white == 0)
2805 pfile->only_seen_white = 1;
2806 CPP_BUMP_LINE (pfile);
2807 if (! CPP_OPTIONS (pfile)->no_line_commands)
2810 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2811 output_line_command (pfile, same_file);
2815 case '(': token = CPP_LPAREN; goto char1;
2816 case ')': token = CPP_RPAREN; goto char1;
2817 case '{': token = CPP_LBRACE; goto char1;
2818 case '}': token = CPP_RBRACE; goto char1;
2819 case ',': token = CPP_COMMA; goto char1;
2820 case ';': token = CPP_SEMICOLON; goto char1;
2826 pfile->only_seen_white = 0;
2827 CPP_PUTC (pfile, c);
2833 /* Like cpp_get_token, but skip spaces and comments. */
2836 cpp_get_non_space_token (pfile)
2839 int old_written = CPP_WRITTEN (pfile);
2842 enum cpp_token token = cpp_get_token (pfile);
2843 if (token != CPP_COMMENT && token != CPP_POP
2844 && token != CPP_HSPACE && token != CPP_VSPACE)
2846 CPP_SET_WRITTEN (pfile, old_written);
2850 /* Parse an identifier starting with C. */
2853 parse_name (pfile, c)
2865 if (c == '$' && CPP_PEDANTIC (pfile))
2866 cpp_pedwarn (pfile, "`$' in identifier");
2868 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2869 CPP_PUTC_Q (pfile, c);
2874 CPP_NUL_TERMINATE_Q (pfile);
2878 /* Parse and skip over a string starting with C. A single quoted
2879 string is treated like a double -- some programs (e.g., troff) are
2880 perverse this way. (However, a single quoted string is not allowed
2881 to extend over multiple lines.) */
2883 skip_string (pfile, c)
2887 long start_line, start_column;
2888 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2896 cpp_error_with_line (pfile, start_line, start_column,
2897 "unterminated string or character constant");
2898 if (pfile->multiline_string_line != start_line
2899 && pfile->multiline_string_line != 0)
2900 cpp_error_with_line (pfile,
2901 pfile->multiline_string_line, -1,
2902 "possible real start of unterminated constant");
2903 pfile->multiline_string_line = 0;
2907 CPP_BUMP_LINE (pfile);
2908 /* In Fortran and assembly language, silently terminate
2909 strings of either variety at end of line. This is a
2910 kludge around not knowing where comments are in these
2912 if (CPP_OPTIONS (pfile)->lang_fortran
2913 || CPP_OPTIONS (pfile)->lang_asm)
2918 /* Character constants may not extend over multiple lines.
2919 In Standard C, neither may strings. We accept multiline
2920 strings as an extension. */
2923 cpp_error_with_line (pfile, start_line, start_column,
2924 "unterminated character constant");
2928 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2929 cpp_pedwarn_with_line (pfile, start_line, start_column,
2930 "string constant runs past end of line");
2931 if (pfile->multiline_string_line == 0)
2932 pfile->multiline_string_line = start_line;
2936 if (CPP_BUFFER (pfile)->has_escapes)
2938 cpp_ice (pfile, "\\r escape inside string constant");
2942 /* Backslash newline is replaced by nothing at all. */
2943 CPP_BUMP_LINE (pfile);
2959 /* Parse a string and copy it to the output. */
2962 parse_string (pfile, c)
2966 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
2969 skip_string (pfile, c);
2971 limit = CPP_BUFFER (pfile)->cur;
2972 CPP_RESERVE (pfile, limit - start + 2);
2973 CPP_PUTC_Q (pfile, c);
2974 for (; start < limit; start++)
2976 CPP_PUTC_Q (pfile, *start);
2979 /* Read an assertion into the token buffer, converting to
2980 canonical form: `#predicate(a n swe r)' The next non-whitespace
2981 character to read should be the first letter of the predicate.
2982 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2983 with answer (see callers for why). In case of 0, an error has been
2986 parse_assertion (pfile)
2990 cpp_skip_hspace (pfile);
2992 if (! is_idstart(c))
2994 cpp_error (pfile, "assertion predicate is not an identifier");
2997 CPP_PUTC(pfile, '#');
2999 parse_name(pfile, c);
3004 if (is_hspace(c) || c == '\r')
3005 cpp_skip_hspace (pfile);
3011 CPP_PUTC(pfile, '(');
3014 while ((c = GETC()) != ')')
3020 CPP_PUTC(pfile, ' ');
3024 else if (c == '\n' || c == EOF)
3026 if (c == '\n') FORWARD(-1);
3027 cpp_error (pfile, "un-terminated assertion answer");
3031 /* \r cannot be a macro escape here. */
3032 CPP_BUMP_LINE (pfile);
3035 CPP_PUTC (pfile, c);
3040 if (pfile->limit[-1] == ' ')
3041 pfile->limit[-1] = ')';
3042 else if (pfile->limit[-1] == '(')
3044 cpp_error (pfile, "empty token sequence in assertion");
3048 CPP_PUTC (pfile, ')');
3050 CPP_NUL_TERMINATE (pfile);
3055 do_assert (pfile, keyword)
3057 const struct directive *keyword ATTRIBUTE_UNUSED;
3061 HASHNODE *base, *this;
3062 int baselen, thislen;
3064 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3065 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3067 cpp_skip_hspace (pfile);
3068 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3069 ret = parse_assertion (pfile);
3074 cpp_error (pfile, "missing token-sequence in `#assert'");
3078 cpp_skip_hspace (pfile);
3080 if (c != EOF && c != '\n')
3082 cpp_error (pfile, "junk at end of `#assert'");
3086 thislen = strlen (sym);
3087 baselen = index (sym, '(') - sym;
3088 this = cpp_lookup (pfile, sym, thislen);
3091 cpp_warning (pfile, "`%s' re-asserted", sym);
3095 base = cpp_lookup (pfile, sym, baselen);
3097 base = cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3098 else if (base->type != T_ASSERT)
3100 /* Token clash - but with what?! */
3101 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3105 this = cpp_install (pfile, sym, thislen, T_ASSERT,
3106 (char *)base->value.aschain);
3107 base->value.aschain = this;
3109 pfile->limit = (unsigned char *) sym; /* Pop */
3113 skip_rest_of_line (pfile);
3114 pfile->limit = (unsigned char *) sym; /* Pop */
3119 do_unassert (pfile, keyword)
3121 const struct directive *keyword ATTRIBUTE_UNUSED;
3125 long baselen, thislen;
3126 HASHNODE *base, *this, *next;
3128 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3129 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3131 cpp_skip_hspace (pfile);
3133 sym = (char *) CPP_PWRITTEN (pfile); /* remember where it starts */
3134 ret = parse_assertion (pfile);
3138 cpp_skip_hspace (pfile);
3140 if (c != EOF && c != '\n')
3141 cpp_error (pfile, "junk at end of `#unassert'");
3143 thislen = strlen (sym);
3146 base = cpp_lookup (pfile, sym, thislen);
3148 goto error; /* It isn't an error to #undef what isn't #defined,
3149 so it isn't an error to #unassert what isn't
3150 #asserted either. */
3152 for (this = base->value.aschain; this; this = next)
3154 next = this->value.aschain;
3155 delete_macro (this);
3157 delete_macro (base);
3161 baselen = index (sym, '(') - sym;
3162 base = cpp_lookup (pfile, sym, baselen);
3163 if (! base) goto error;
3164 this = cpp_lookup (pfile, sym, thislen);
3165 if (! this) goto error;
3168 while (next->value.aschain != this)
3169 next = next->value.aschain;
3171 next->value.aschain = this->value.aschain;
3172 delete_macro (this);
3174 if (base->value.aschain == NULL)
3175 delete_macro (base); /* Last answer for this predicate deleted. */
3178 pfile->limit = (unsigned char *) sym; /* Pop */
3181 skip_rest_of_line (pfile);
3182 pfile->limit = (unsigned char *) sym; /* Pop */
3186 /* Process STR as if it appeared as the body of an #unassert. */
3188 cpp_unassert (pfile, str)
3192 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3194 do_unassert (pfile, NULL);
3195 cpp_pop_buffer (pfile);
3199 /* Remember the current position of PFILE so it may be returned to
3200 after looking ahead a bit.
3202 Note that when you set a mark, you _must_ return to that mark. You
3203 may not forget about it and continue parsing. You may not pop a
3204 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3208 parse_set_mark (pfile)
3211 cpp_buffer *ip = CPP_BUFFER (pfile);
3212 if (ACTIVE_MARK_P())
3213 cpp_ice (pfile, "mark active in parse_set_mark");
3215 ip->mark = ip->cur - ip->buf;
3218 /* Backup the current position of PFILE to that saved in its mark,
3219 and clear the mark. */
3222 parse_goto_mark (pfile)
3225 cpp_buffer *ip = CPP_BUFFER (pfile);
3226 if (!ACTIVE_MARK_P())
3227 cpp_ice (pfile, "mark not active in parse_goto_mark");
3229 ip->cur = ip->buf + ip->mark;