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. */
31 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
33 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
34 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
35 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
36 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
37 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
38 (Note that it is false while we're expanding macro *arguments*.) */
39 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
41 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
42 case CPP_BUMP_LINE must not be called. */
43 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
45 /* `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 /* Stack of conditionals currently in progress
57 (including both successful and failing conditionals). */
61 struct if_stack *next;
62 int lineno; /* line number where condition started */
63 int if_succeeded; /* truth of last condition in this group */
64 const U_CHAR *control_macro; /* macro name for #ifndef around entire file */
65 enum node_type type; /* type of last directive seen in this group */
67 typedef struct if_stack IF_STACK;
70 /* These functions are declared to return int instead of void since they
71 are going to be placed in a table and some old compilers have trouble with
72 pointers to functions returning void. */
74 static int do_define PARAMS ((cpp_reader *, const struct directive *));
75 static int do_line PARAMS ((cpp_reader *, const struct directive *));
76 static int do_include PARAMS ((cpp_reader *, const struct directive *));
77 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
78 static int do_error PARAMS ((cpp_reader *, const struct directive *));
79 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
80 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
81 static int do_if PARAMS ((cpp_reader *, const struct directive *));
82 static int do_ifdef PARAMS ((cpp_reader *, const struct directive *));
83 static int do_else PARAMS ((cpp_reader *, const struct directive *));
84 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
85 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
87 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
89 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
90 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
91 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
93 /* Forward declarations. */
95 static void validate_else PARAMS ((cpp_reader *, const char *));
96 static int eval_if_expr PARAMS ((cpp_reader *));
97 static void conditional_skip PARAMS ((cpp_reader *, int,
98 enum node_type, U_CHAR *));
99 static void skip_if_group PARAMS ((cpp_reader *));
100 static void parse_name PARAMS ((cpp_reader *, int));
101 static void parse_string PARAMS ((cpp_reader *, int));
102 static int parse_assertion PARAMS ((cpp_reader *));
103 static const char *if_directive_name PARAMS ((cpp_reader *,
105 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
106 static int skip_comment PARAMS ((cpp_reader *, int));
107 static int copy_comment PARAMS ((cpp_reader *, int));
108 static void skip_string PARAMS ((cpp_reader *, int));
109 static void skip_rest_of_line PARAMS ((cpp_reader *));
110 static void cpp_skip_hspace PARAMS ((cpp_reader *));
111 static int handle_directive PARAMS ((cpp_reader *));
112 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
114 const struct directive *));
115 static int read_line_number PARAMS ((cpp_reader *, int *));
116 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
117 static int consider_directive_while_skipping
118 PARAMS ((cpp_reader *, IF_STACK *));
119 static void skip_block_comment PARAMS ((cpp_reader *));
120 static void skip_line_comment PARAMS ((cpp_reader *));
121 static void parse_set_mark PARAMS ((cpp_reader *));
122 static void parse_goto_mark PARAMS ((cpp_reader *));
123 static int get_macro_name PARAMS ((cpp_reader *));
125 /* Here is the actual list of #-directives.
126 This table is ordered by frequency of occurrence; the numbers
127 at the end are directive counts from all the source code I have
128 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
129 linux-2.2.9, and pcmcia-cs-3.0.9). */
131 static const struct directive directive_table[] = {
133 { 6, do_define, "define", T_DEFINE }, /* 270554 */
134 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
135 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
136 { 5, do_ifdef, "ifdef", T_IFDEF }, /* 22000 */
137 { 2, do_if, "if", T_IF }, /* 18162 */
138 { 4, do_else, "else", T_ELSE }, /* 9863 */
139 { 6, do_ifdef, "ifndef", T_IFNDEF }, /* 9675 */
140 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
141 { 4, do_line, "line", T_LINE }, /* 2465 */
142 { 4, do_elif, "elif", T_ELIF }, /* 610 */
143 { 5, do_error, "error", T_ERROR }, /* 475 */
144 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
146 /* Extensions. All deprecated except #warning and #include_next. */
147 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
148 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
149 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
150 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
151 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
152 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
153 #ifdef SCCS_DIRECTIVE
154 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
156 { -1, 0, "", T_UNUSED }
159 /* Place into PFILE a quoted string representing the string SRC.
160 Caller must reserve enough space in pfile->token_buffer. */
163 quote_string (pfile, src)
169 CPP_PUTC_Q (pfile, '\"');
171 switch ((c = *src++))
175 CPP_PUTC_Q (pfile, c);
178 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
179 CPP_ADJUST_WRITTEN (pfile, 4);
185 CPP_PUTC_Q (pfile, '\\');
186 CPP_PUTC_Q (pfile, c);
190 CPP_PUTC_Q (pfile, '\"');
191 CPP_NUL_TERMINATE_Q (pfile);
196 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
199 cpp_grow_buffer (pfile, n)
203 long old_written = CPP_WRITTEN (pfile);
204 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
205 pfile->token_buffer = (U_CHAR *)
206 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
207 CPP_SET_WRITTEN (pfile, old_written);
210 /* Process the string STR as if it appeared as the body of a #define.
211 If STR is just an identifier, define it with value 1.
212 If STR has anything after the identifier, then it should
213 be identifier=definition. */
216 cpp_define (pfile, str)
223 p = strchr (str, '=');
224 /* Copy the entire option so we can modify it.
225 Change the first "=" in the string to a space. If there is none,
226 tack " 1" on the end. Then add a newline and a NUL. */
230 count = strlen (str) + 2;
231 buf = alloca (count);
232 memcpy (buf, str, count - 2);
234 buf[count - 2] = '\n';
235 buf[count - 1] = '\0';
239 count = strlen (str) + 4;
240 buf = alloca (count);
241 memcpy (buf, str, count - 4);
242 strcpy (&buf[count-4], " 1\n");
245 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
247 do_define (pfile, NULL);
248 cpp_pop_buffer (pfile);
252 /* Process the string STR as if it appeared as the body of a #assert. */
254 cpp_assert (pfile, str)
258 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
260 do_assert (pfile, NULL);
261 cpp_pop_buffer (pfile);
265 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
267 cpp_defined (pfile, id, len)
272 HASHNODE *hp = _cpp_lookup (pfile, id, len);
273 if (hp && hp->type == T_POISON)
275 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
282 null_cleanup (pbuf, pfile)
283 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
284 cpp_reader *pfile ATTRIBUTE_UNUSED;
289 /* Skip a C-style block comment. We know it's a comment, and point is
290 at the second character of the starter. */
292 skip_block_comment (pfile)
299 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
305 cpp_error_with_line (pfile, line, col, "unterminated comment");
308 else if (c == '\n' || c == '\r')
310 /* \r cannot be a macro escape marker here. */
311 if (!ACTIVE_MARK_P())
312 CPP_BUMP_LINE (pfile);
314 else if (c == '/' && prev_c == '*')
316 else if (c == '*' && prev_c == '/'
317 && CPP_OPTIONS (pfile)->warn_comments)
318 cpp_warning (pfile, "`/*' within comment");
324 /* Skip a C++/Chill line comment. We know it's a comment, and point
325 is at the second character of the initiator. */
327 skip_line_comment (pfile)
335 /* We don't have to worry about EOF in here. */
338 /* Don't consider final '\n' to be part of comment. */
344 /* \r cannot be a macro escape marker here. */
345 if (!ACTIVE_MARK_P())
346 CPP_BUMP_LINE (pfile);
347 if (CPP_OPTIONS (pfile)->warn_comments)
348 cpp_warning (pfile, "backslash-newline within line comment");
353 /* Skip a comment - C, C++, or Chill style. M is the first character
354 of the comment marker. If this really is a comment, skip to its
355 end and return ' '. If this is not a comment, return M (which will
359 skip_comment (pfile, m)
363 if (m == '/' && PEEKC() == '*')
365 skip_block_comment (pfile);
368 else if (m == '/' && PEEKC() == '/')
370 if (CPP_BUFFER (pfile)->system_header_p)
372 /* We silently allow C++ comments in system headers, irrespective
373 of conformance mode, because lots of busted systems do that
374 and trying to clean it up in fixincludes is a nightmare. */
375 skip_line_comment (pfile);
378 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
380 if (CPP_OPTIONS (pfile)->c89
381 && CPP_PEDANTIC (pfile)
382 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
385 "C++ style comments are not allowed in ISO C89");
387 "(this will be reported only once per input file)");
388 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
390 skip_line_comment (pfile);
396 else if (m == '-' && PEEKC() == '-'
397 && CPP_OPTIONS (pfile)->chill)
399 skip_line_comment (pfile);
406 /* Identical to skip_comment except that it copies the comment into the
407 token_buffer. This is used if !discard_comments. */
409 copy_comment (pfile, m)
413 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
416 if (skip_comment (pfile, m) == m)
419 limit = CPP_BUFFER (pfile)->cur;
420 CPP_RESERVE (pfile, limit - start + 2);
421 CPP_PUTC_Q (pfile, m);
422 for (; start <= limit; start++)
424 CPP_PUTC_Q (pfile, *start);
429 /* Skip whitespace \-newline and comments. Does not macro-expand. */
432 cpp_skip_hspace (pfile)
441 else if (is_hspace(c))
443 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
444 cpp_pedwarn (pfile, "%s in preprocessing directive",
445 c == '\f' ? "formfeed" : "vertical tab");
449 /* \r is a backslash-newline marker if !has_escapes, and
450 a deletable-whitespace or no-reexpansion marker otherwise. */
451 if (CPP_BUFFER (pfile)->has_escapes)
459 CPP_BUMP_LINE (pfile);
461 else if (c == '/' || c == '-')
463 c = skip_comment (pfile, c);
473 /* Read and discard the rest of the current line. */
476 skip_rest_of_line (pfile)
490 if (! CPP_BUFFER (pfile)->has_escapes)
491 CPP_BUMP_LINE (pfile);
496 skip_string (pfile, c);
501 skip_comment (pfile, c);
506 if (CPP_PEDANTIC (pfile))
507 cpp_pedwarn (pfile, "%s in preprocessing directive",
508 c == '\f' ? "formfeed" : "vertical tab");
515 /* Handle a possible # directive.
516 '#' has already been read. */
519 handle_directive (pfile)
523 register const struct directive *kt;
526 long old_written = CPP_WRITTEN (pfile);
528 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
530 cpp_ice (pfile, "handle_directive called on macro buffer");
534 cpp_skip_hspace (pfile);
537 /* # followed by a number is equivalent to #line. Do not recognize
538 this form in assembly language source files. Complain about this
539 form if we're being pedantic, but not if this is regurgitated
540 input (preprocessed or fed back in by the C++ frontend). */
541 if (c >= '0' && c <= '9')
543 if (CPP_OPTIONS (pfile)->lang_asm)
546 if (CPP_PEDANTIC (pfile)
547 && ! CPP_OPTIONS (pfile)->preprocessed
548 && ! CPP_BUFFER (pfile)->manual_pop)
549 cpp_pedwarn (pfile, "`#' followed by integer");
550 do_line (pfile, NULL);
554 /* If we are rescanning preprocessed input, don't obey any directives
556 if (CPP_OPTIONS (pfile)->preprocessed)
559 /* Now find the directive name. */
560 CPP_PUTC (pfile, '#');
561 parse_name (pfile, GETC());
562 ident = pfile->token_buffer + old_written + 1;
563 ident_length = CPP_PWRITTEN (pfile) - ident;
564 if (ident_length == 0)
566 /* A line of just `#' becomes blank. A line with something
567 other than an identifier after the # is reparsed as a non-
569 CPP_SET_WRITTEN (pfile, old_written);
570 return (PEEKC() == '\n');
573 /* Decode the keyword and call the appropriate expansion routine. */
574 for (kt = directive_table; ; kt++)
577 /* # identifier, but not a legit directive. Pass onward as a
578 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
580 if (kt->length == ident_length
581 && !strncmp (kt->name, ident, ident_length))
585 CPP_SET_WRITTEN (pfile, old_written);
587 if (pfile->no_directives)
589 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
591 skip_rest_of_line (pfile);
594 (*kt->func) (pfile, kt);
599 /* Pass a directive through to the output file.
600 BUF points to the contents of the directive, as a contiguous string.
601 LEN is the length of the string pointed to by BUF.
602 KEYWORD is the keyword-table entry for the directive. */
605 pass_thru_directive (buf, len, pfile, keyword)
609 const struct directive *keyword;
611 register unsigned keyword_length = keyword->length;
613 CPP_RESERVE (pfile, 1 + keyword_length + len);
614 CPP_PUTC_Q (pfile, '#');
615 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
616 if (len != 0 && buf[0] != ' ')
617 CPP_PUTC_Q (pfile, ' ');
618 CPP_PUTS_Q (pfile, buf, len);
621 /* Subroutine of do_define: determine the name of the macro to be
625 get_macro_name (pfile)
630 here = CPP_WRITTEN (pfile);
631 pfile->no_macro_expand++;
632 if (get_directive_token (pfile) != CPP_NAME)
634 cpp_error (pfile, "`#define' must be followed by an identifier");
638 len = CPP_WRITTEN (pfile) - here;
639 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
641 cpp_error (pfile, "`defined' is not a legal macro name");
645 pfile->no_macro_expand--;
649 skip_rest_of_line (pfile);
650 pfile->no_macro_expand--;
654 /* Process a #define command.
655 KEYWORD is the keyword-table entry for #define,
656 or NULL for a "predefined" macro. */
659 do_define (pfile, keyword)
661 const struct directive *keyword ATTRIBUTE_UNUSED;
671 here = CPP_WRITTEN (pfile);
672 len = get_macro_name (pfile);
676 /* Copy out the name so we can pop the token buffer. */
677 len = CPP_WRITTEN (pfile) - here;
678 sym = (U_CHAR *) alloca (len + 1);
679 memcpy (sym, pfile->token_buffer + here, len);
681 CPP_SET_WRITTEN (pfile, here);
683 /* If the next character, with no intervening whitespace, is '(',
684 then this is a function-like macro. */
688 else if (c != '\n' && !is_hspace (c))
689 /* Otherwise, C99 requires white space after the name. We treat it
690 as an object-like macro if this happens, with a warning. */
691 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
693 def = _cpp_create_definition (pfile, funlike);
697 slot = _cpp_lookup_slot (pfile, sym, len, 1, &hash);
701 HASHNODE *hp = *slot;
703 /* Redefining a macro is ok if the definitions are the same. */
704 if (hp->type == T_MACRO)
705 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
706 /* Redefining a constant is ok with -D. */
707 else if (hp->type == T_CONST || hp->type == T_STDC)
708 ok = ! CPP_OPTIONS (pfile)->done_initializing;
709 /* Otherwise it's not ok. */
712 /* Print the warning or error if it's not ok. */
715 if (hp->type == T_POISON)
716 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
718 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
719 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
721 DEFINITION *d = hp->value.defn;
722 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
723 "this is the location of the previous definition");
726 if (hp->type != T_POISON)
728 /* Replace the old definition. */
729 if (hp->type == T_MACRO)
730 _cpp_free_definition (hp->value.defn);
732 hp->value.defn = def;
737 HASHNODE *hp = _cpp_make_hashnode (sym, len, T_MACRO, hash);
738 hp->value.defn = def;
742 if (CPP_OPTIONS (pfile)->debug_output
743 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
744 _cpp_dump_definition (pfile, sym, len, def);
745 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
746 pass_thru_directive (sym, len, pfile, keyword);
752 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
753 If BUFFER != NULL, then use the LENGTH characters in BUFFER
754 as the new input buffer.
755 Return the new buffer, or NULL on failure. */
758 cpp_push_buffer (pfile, buffer, length)
760 const U_CHAR *buffer;
763 cpp_buffer *buf = CPP_BUFFER (pfile);
765 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
767 cpp_fatal (pfile, "macro or `#include' recursion too deep");
771 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
773 new->if_stack = pfile->if_stack;
774 new->cleanup = null_cleanup;
775 new->buf = new->cur = buffer;
776 new->alimit = new->rlimit = buffer + length;
779 new->line_base = NULL;
781 CPP_BUFFER (pfile) = new;
786 cpp_pop_buffer (pfile)
789 cpp_buffer *buf = CPP_BUFFER (pfile);
791 cpp_ice (pfile, "mark active in cpp_pop_buffer");
792 (*buf->cleanup) (buf, pfile);
793 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
795 pfile->buffer_stack_depth--;
796 return CPP_BUFFER (pfile);
799 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
800 Pop the buffer when done. */
803 cpp_scan_buffer (pfile)
806 cpp_buffer *buffer = CPP_BUFFER (pfile);
807 enum cpp_token token;
808 if (CPP_OPTIONS (pfile)->no_output)
810 long old_written = CPP_WRITTEN (pfile);
811 /* In no-output mode, we can ignore everything but directives. */
814 if (! pfile->only_seen_white)
815 skip_rest_of_line (pfile);
816 token = cpp_get_token (pfile);
817 if (token == CPP_EOF) /* Should not happen ... */
819 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
821 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
822 cpp_pop_buffer (pfile);
826 CPP_SET_WRITTEN (pfile, old_written);
832 token = cpp_get_token (pfile);
833 if (token == CPP_EOF) /* Should not happen ... */
835 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
837 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
838 cpp_pop_buffer (pfile);
846 * Rescan a string (which may have escape marks) into pfile's buffer.
847 * Place the result in pfile->token_buffer.
849 * The input is copied before it is scanned, so it is safe to pass
850 * it something from the token_buffer that will get overwritten
851 * (because it follows CPP_WRITTEN). This is used by do_include.
855 cpp_expand_to_buffer (pfile, buf, length)
860 register cpp_buffer *ip;
866 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
870 /* Set up the input on the input stack. */
872 buf1 = (U_CHAR *) alloca (length + 1);
873 memcpy (buf1, buf, length);
876 ip = cpp_push_buffer (pfile, buf1, length);
881 /* Scan the input, create the output. */
882 save_no_output = CPP_OPTIONS (pfile)->no_output;
883 CPP_OPTIONS (pfile)->no_output = 0;
884 CPP_OPTIONS (pfile)->no_line_commands++;
885 cpp_scan_buffer (pfile);
886 CPP_OPTIONS (pfile)->no_line_commands--;
887 CPP_OPTIONS (pfile)->no_output = save_no_output;
889 CPP_NUL_TERMINATE (pfile);
893 cpp_buf_line_and_col (pbuf, linep, colp)
894 register cpp_buffer *pbuf;
899 *linep = pbuf->lineno;
901 *colp = pbuf->cur - pbuf->line_base;
911 /* Return the cpp_buffer that corresponds to a file (not a macro). */
914 cpp_file_buffer (pfile)
919 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
920 if (ip->ihash != NULL)
926 * write out a #line command, for instance, after an #include file.
927 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
931 output_line_command (pfile, file_change)
933 enum file_change_code file_change;
938 if (CPP_OPTIONS (pfile)->no_line_commands
939 || CPP_OPTIONS (pfile)->no_output)
942 ip = cpp_file_buffer (pfile);
943 cpp_buf_line_and_col (ip, &line, NULL);
945 /* If the current file has not changed, we omit the #line if it would
946 appear to be a no-op, and we output a few newlines instead
947 if we want to increase the line number by a small amount.
948 We cannot do this if pfile->lineno is zero, because that means we
949 haven't output any line commands yet. (The very first line command
950 output is a `same_file' command.) */
951 if (file_change == same_file && pfile->lineno != 0)
953 if (line == pfile->lineno)
956 /* If the inherited line number is a little too small,
957 output some newlines instead of a #line command. */
958 if (line > pfile->lineno && line < pfile->lineno + 8)
960 CPP_RESERVE (pfile, 20);
961 while (line > pfile->lineno)
963 CPP_PUTC_Q (pfile, '\n');
970 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
971 CPP_PUTS_Q (pfile, "# ", 2);
973 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
974 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
976 quote_string (pfile, ip->nominal_fname);
977 if (file_change != same_file && file_change != rename_file)
979 CPP_PUTC_Q (pfile, ' ');
980 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
982 /* Tell cc1 if following text comes from a system header file. */
983 if (ip->system_header_p)
985 CPP_PUTC_Q (pfile, ' ');
986 CPP_PUTC_Q (pfile, '3');
988 #ifndef NO_IMPLICIT_EXTERN_C
989 /* Tell cc1plus if following text should be treated as C. */
990 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
992 CPP_PUTC_Q (pfile, ' ');
993 CPP_PUTC_Q (pfile, '4');
996 CPP_PUTC_Q (pfile, '\n');
997 pfile->lineno = line;
1001 /* Like cpp_get_token, except that it does not read past end-of-line.
1002 Also, horizontal space is skipped, and macros are popped. */
1005 get_directive_token (pfile)
1008 long old_written = CPP_WRITTEN (pfile);
1009 enum cpp_token token;
1013 cpp_skip_hspace (pfile);
1014 if (PEEKC () == '\n')
1017 token = cpp_get_token (pfile);
1018 /* token could be hspace at the beginning of a macro. */
1019 if (token == CPP_HSPACE || token == CPP_COMMENT)
1021 CPP_SET_WRITTEN (pfile, old_written);
1025 /* token cannot be vspace, it would have been caught above. */
1026 if (token == CPP_VSPACE)
1028 cpp_ice (pfile, "VSPACE in get_directive_token");
1032 /* token cannot be POP unless the buffer is a macro buffer. */
1033 if (token != CPP_POP)
1036 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1038 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1042 /* We must pop the buffer by hand, or else cpp_get_token might
1043 hand us white space or newline on the next invocation. */
1044 cpp_pop_buffer (pfile);
1048 /* Handle #include and #import.
1049 This function expects to see "fname" or <fname> on the input.
1051 The input is normally in part of the output_buffer following
1052 CPP_WRITTEN, and will get overwritten by output_line_command.
1053 I.e. in input file specification has been popped by handle_directive.
1057 do_include (pfile, keyword)
1059 const struct directive *keyword;
1061 int importing = (keyword->type == T_IMPORT);
1062 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1063 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1064 int before; /* included before? */
1066 unsigned char *ftok;
1069 enum cpp_token token;
1071 /* Chain of dirs to search */
1073 struct file_name_list *search_start;
1075 long old_written = CPP_WRITTEN (pfile);
1079 if (CPP_PEDANTIC (pfile))
1082 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1084 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1087 if (importing && CPP_OPTIONS (pfile)->warn_import
1088 && !CPP_OPTIONS (pfile)->inhibit_warnings
1089 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1091 pfile->import_warning = 1;
1093 "#import is obsolete, use an #ifndef wrapper in the header file");
1096 pfile->parsing_include_directive++;
1097 token = get_directive_token (pfile);
1098 pfile->parsing_include_directive--;
1100 if (token == CPP_STRING)
1102 if (pfile->token_buffer[old_written] == '<')
1106 else if (token == CPP_NAME)
1108 /* Support '#include xyz' like VAX-C. It is taken as
1109 '#include <xyz.h>' and generates a warning. */
1111 "`#include filename' is obsolete, use `#include <filename.h>'");
1114 /* Append the missing `.h' to the name. */
1115 CPP_PUTS (pfile, ".h", 2);
1121 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1122 CPP_SET_WRITTEN (pfile, old_written);
1123 skip_rest_of_line (pfile);
1127 flen = CPP_WRITTEN (pfile) - old_written;
1128 ftok = (unsigned char *) alloca (flen + 1);
1129 memcpy (ftok, pfile->token_buffer + old_written, flen);
1132 if (get_directive_token (pfile) != CPP_VSPACE)
1134 cpp_error (pfile, "junk at end of `#include'");
1135 skip_rest_of_line (pfile);
1138 CPP_SET_WRITTEN (pfile, old_written);
1142 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1146 if (CPP_OPTIONS (pfile)->dump_includes)
1147 pass_thru_directive (ftok,
1150 - ((token == CPP_NAME) ? 2 : 0)
1155 if (token == CPP_STRING)
1165 fp = cpp_file_buffer (pfile);
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))
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 = _cpp_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_add_dep (pfile->deps, 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_add_dep (pfile->deps, 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_add_dep (pfile->deps, 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)) != NULL)
1263 fprintf (stderr, " %s\n", ihash->name);
1266 /* Actually process the file */
1269 ihash->control_macro = (const U_CHAR *) "";
1271 if (_cpp_read_include_file (pfile, fd, ihash))
1273 output_line_command (pfile, enter_file);
1275 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1280 /* Subroutine of do_line. Read next token from PFILE without adding it to
1281 the output buffer. If it is a number between 1 and 4, store it in *NUM
1282 and return 1; otherwise, return 0 and complain if we aren't at the end
1283 of the directive. */
1286 read_line_number (pfile, num)
1290 long save_written = CPP_WRITTEN (pfile);
1291 U_CHAR *p = pfile->token_buffer + save_written;
1292 enum cpp_token token = get_directive_token (pfile);
1293 CPP_SET_WRITTEN (pfile, save_written);
1295 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1302 if (token != CPP_VSPACE && token != CPP_EOF)
1303 cpp_error (pfile, "invalid format `#line' command");
1308 /* Interpret #line command.
1309 Note that the filename string (if any) is treated as if it were an
1310 include filename. That means no escape handling. */
1313 do_line (pfile, keyword)
1315 const struct directive *keyword ATTRIBUTE_UNUSED;
1317 cpp_buffer *ip = CPP_BUFFER (pfile);
1319 long old_written = CPP_WRITTEN (pfile);
1320 enum file_change_code file_change = same_file;
1321 enum cpp_token token;
1324 token = get_directive_token (pfile);
1326 if (token != CPP_NUMBER)
1328 cpp_error (pfile, "token after `#line' is not an integer");
1329 goto bad_line_directive;
1332 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1335 cpp_error (pfile, "token after `#line' is not an integer");
1336 goto bad_line_directive;
1338 CPP_SET_WRITTEN (pfile, old_written);
1340 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1341 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1343 token = get_directive_token (pfile);
1345 if (token == CPP_STRING)
1347 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1348 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1349 int action_number = 0;
1351 file_change = rename_file;
1353 if (read_line_number (pfile, &action_number))
1355 if (CPP_PEDANTIC (pfile))
1356 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1358 if (action_number == 1)
1360 file_change = enter_file;
1361 read_line_number (pfile, &action_number);
1363 else if (action_number == 2)
1365 file_change = leave_file;
1366 read_line_number (pfile, &action_number);
1368 if (action_number == 3)
1370 ip->system_header_p = 1;
1371 read_line_number (pfile, &action_number);
1373 if (action_number == 4)
1375 ip->system_header_p = 2;
1376 read_line_number (pfile, &action_number);
1382 if (strcmp (fname, ip->nominal_fname))
1384 const char *newname, *oldname;
1385 if (!strcmp (fname, ip->ihash->name))
1386 newname = ip->ihash->name;
1387 else if (ip->last_nominal_fname
1388 && !strcmp (fname, ip->last_nominal_fname))
1389 newname = ip->last_nominal_fname;
1391 newname = xstrdup (fname);
1393 oldname = ip->nominal_fname;
1394 ip->nominal_fname = newname;
1396 if (ip->last_nominal_fname
1397 && ip->last_nominal_fname != oldname
1398 && ip->last_nominal_fname != newname
1399 && ip->last_nominal_fname != ip->ihash->name)
1400 free ((void *) ip->last_nominal_fname);
1402 if (newname == ip->ihash->name)
1403 ip->last_nominal_fname = NULL;
1405 ip->last_nominal_fname = oldname;
1408 else if (token != CPP_VSPACE && token != CPP_EOF)
1410 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1411 goto bad_line_directive;
1414 /* The Newline at the end of this line remains to be processed.
1415 To put the next line at the specified line number,
1416 we must store a line number now that is one less. */
1417 ip->lineno = new_lineno - 1;
1418 CPP_SET_WRITTEN (pfile, old_written);
1419 output_line_command (pfile, file_change);
1423 skip_rest_of_line (pfile);
1424 CPP_SET_WRITTEN (pfile, old_written);
1428 /* Remove the definition of a symbol from the symbol table.
1429 According to the C standard, it is not an error to undef
1430 something that has no definitions. */
1432 do_undef (pfile, keyword)
1434 const struct directive *keyword;
1438 U_CHAR *buf, *name, *limit;
1440 long here = CPP_WRITTEN (pfile);
1441 enum cpp_token token;
1443 cpp_skip_hspace (pfile);
1445 if (! is_idstart(c))
1447 cpp_error (pfile, "token after #undef is not an identifier");
1448 skip_rest_of_line (pfile);
1452 parse_name (pfile, c);
1453 buf = pfile->token_buffer + here;
1454 limit = CPP_PWRITTEN(pfile);
1456 /* Copy out the token so we can pop the token buffer. */
1458 name = (U_CHAR *) alloca (len + 1);
1459 memcpy (name, buf, len);
1462 token = get_directive_token (pfile);
1463 if (token != CPP_VSPACE)
1465 cpp_pedwarn (pfile, "junk on line after #undef");
1466 skip_rest_of_line (pfile);
1468 CPP_SET_WRITTEN (pfile, here);
1470 slot = _cpp_lookup_slot (pfile, name, len, 0, 0);
1473 HASHNODE *hp = *slot;
1474 /* If we are generating additional info for debugging (with -g) we
1475 need to pass through all effective #undef commands. */
1476 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1477 pass_thru_directive (name, len, pfile, keyword);
1478 if (hp->type == T_POISON)
1479 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1482 if (hp->type != T_MACRO)
1483 cpp_warning (pfile, "undefining `%s'", hp->name);
1485 htab_clear_slot (pfile->hashtab, (void **)slot);
1492 /* Wrap do_undef for -U processing. */
1494 cpp_undef (pfile, macro)
1498 /* Copy the string so we can append a newline. */
1499 size_t len = strlen (macro);
1500 char *buf = alloca (len + 2);
1501 memcpy (buf, macro, len);
1503 buf[len + 1] = '\0';
1504 if (cpp_push_buffer (pfile, buf, len + 1))
1506 do_undef (pfile, NULL);
1507 cpp_pop_buffer (pfile);
1512 * Report an error detected by the program we are processing.
1513 * Use the text of the line in the error message.
1514 * (We use error because it prints the filename & line#.)
1518 do_error (pfile, keyword)
1520 const struct directive *keyword ATTRIBUTE_UNUSED;
1522 const U_CHAR *text, *limit;
1524 cpp_skip_hspace (pfile);
1525 text = CPP_BUFFER (pfile)->cur;
1526 skip_rest_of_line (pfile);
1527 limit = CPP_BUFFER (pfile)->cur;
1529 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1534 * Report a warning detected by the program we are processing.
1535 * Use the text of the line in the warning message, then continue.
1539 do_warning (pfile, keyword)
1541 const struct directive *keyword ATTRIBUTE_UNUSED;
1543 const U_CHAR *text, *limit;
1545 cpp_skip_hspace (pfile);
1546 text = CPP_BUFFER (pfile)->cur;
1547 skip_rest_of_line (pfile);
1548 limit = CPP_BUFFER (pfile)->cur;
1550 if (CPP_PEDANTIC (pfile))
1551 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1553 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1557 /* Report program identification. */
1560 do_ident (pfile, keyword)
1562 const struct directive *keyword ATTRIBUTE_UNUSED;
1564 long old_written = CPP_WRITTEN (pfile);
1566 /* Allow #ident in system headers, since that's not user's fault. */
1567 if (CPP_PEDANTIC (pfile))
1568 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1570 CPP_PUTS (pfile, "#ident ", 7);
1572 /* Next token should be a string constant. */
1573 if (get_directive_token (pfile) == CPP_STRING)
1574 /* And then a newline. */
1575 if (get_directive_token (pfile) == CPP_VSPACE)
1576 /* Good - ship it. */
1579 cpp_error (pfile, "invalid #ident");
1580 skip_rest_of_line (pfile);
1581 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1586 /* Pragmata handling. We handle some of these, and pass the rest on
1587 to the front end. C99 defines three pragmas and says that no macro
1588 expansion is to be performed on them; whether or not macro
1589 expansion happens for other pragmas is implementation defined.
1590 This implementation never macro-expands the text after #pragma.
1592 We currently do not support the _Pragma operator. Support for that
1593 has to be coordinated with the front end. Proposed implementation:
1594 both #pragma blah blah and _Pragma("blah blah") become
1595 __builtin_pragma(blah blah) and we teach the parser about that. */
1597 /* Sub-handlers for the pragmas needing treatment here.
1598 They return 1 if the token buffer is to be popped, 0 if not. */
1599 static int do_pragma_once PARAMS ((cpp_reader *));
1600 static int do_pragma_implementation PARAMS ((cpp_reader *));
1601 static int do_pragma_poison PARAMS ((cpp_reader *));
1602 static int do_pragma_default PARAMS ((cpp_reader *));
1605 do_pragma (pfile, keyword)
1607 const struct directive *keyword ATTRIBUTE_UNUSED;
1612 enum cpp_token token;
1614 here = CPP_WRITTEN (pfile);
1615 CPP_PUTS (pfile, "#pragma ", 8);
1617 key = CPP_WRITTEN (pfile);
1618 pfile->no_macro_expand++;
1619 token = get_directive_token (pfile);
1620 if (token != CPP_NAME)
1622 if (token == CPP_VSPACE)
1628 buf = pfile->token_buffer + key;
1629 CPP_PUTC (pfile, ' ');
1631 #define tokis(x) !strncmp((char *) buf, x, sizeof(x) - 1)
1633 pop = do_pragma_once (pfile);
1634 else if (tokis ("implementation"))
1635 pop = do_pragma_implementation (pfile);
1636 else if (tokis ("poison"))
1637 pop = do_pragma_poison (pfile);
1639 pop = do_pragma_default (pfile);
1642 if (get_directive_token (pfile) != CPP_VSPACE)
1646 CPP_SET_WRITTEN (pfile, here);
1647 pfile->no_macro_expand--;
1651 cpp_error (pfile, "malformed #pragma directive");
1652 skip_rest_of_line (pfile);
1654 CPP_SET_WRITTEN (pfile, here);
1655 pfile->no_macro_expand--;
1660 do_pragma_default (pfile)
1663 while (get_directive_token (pfile) != CPP_VSPACE)
1664 CPP_PUTC (pfile, ' ');
1669 do_pragma_once (pfile)
1672 cpp_buffer *ip = CPP_BUFFER (pfile);
1674 /* Allow #pragma once in system headers, since that's not the user's
1676 if (!ip->system_header_p)
1677 cpp_warning (pfile, "`#pragma once' is obsolete");
1679 if (CPP_PREV_BUFFER (ip) == NULL)
1680 cpp_warning (pfile, "`#pragma once' outside include file");
1682 ip->ihash->control_macro = (const U_CHAR *) ""; /* never repeat */
1688 do_pragma_implementation (pfile)
1691 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1692 been included yet. */
1693 enum cpp_token token;
1694 long written = CPP_WRITTEN (pfile);
1699 token = get_directive_token (pfile);
1700 if (token == CPP_VSPACE)
1702 else if (token != CPP_STRING)
1704 cpp_error (pfile, "malformed #pragma implementation");
1708 /* Trim the leading and trailing quote marks from the string. */
1709 name = pfile->token_buffer + written + 1;
1710 len = CPP_PWRITTEN (pfile) - name;
1711 copy = (U_CHAR *) alloca (len);
1712 memcpy (copy, name, len - 1);
1713 copy[len - 1] = '\0';
1715 if (cpp_included (pfile, copy))
1717 "`#pragma implementation' for `%s' appears after file is included",
1723 do_pragma_poison (pfile)
1726 /* Poison these symbols so that all subsequent usage produces an
1732 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 slot = _cpp_lookup_slot (pfile, p, len, 1, &hash);
1760 HASHNODE *hp = *slot;
1761 if (hp->type != T_POISON)
1763 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1764 if (hp->type == T_MACRO)
1765 _cpp_free_definition (hp->value.defn);
1767 hp->type = T_POISON;
1772 HASHNODE *hp = _cpp_make_hashnode (p, len, T_POISON, hash);
1773 hp->value.cpval = 0;
1777 CPP_PUTC (pfile, ' ');
1782 #ifdef SCCS_DIRECTIVE
1783 /* Just ignore #sccs, on systems where we define it at all. */
1786 do_sccs (pfile, keyword)
1788 const struct directive *keyword ATTRIBUTE_UNUSED;
1790 if (CPP_PEDANTIC (pfile))
1791 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1792 skip_rest_of_line (pfile);
1797 /* We've found an `#if' directive. If the only thing before it in
1798 this file is white space, and if it is of the form
1799 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1800 for inclusion of this file. (See redundant_include_p in cppfiles.c
1801 for an explanation of controlling macros.) If so, return a
1802 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1805 detect_if_not_defined (pfile)
1808 U_CHAR *control_macro = 0;
1810 if (pfile->only_seen_white == 2)
1813 enum cpp_token token;
1816 int need_rparen = 0;
1818 /* Save state required for restore. */
1819 pfile->no_macro_expand++;
1820 parse_set_mark (pfile);
1821 base_offset = CPP_WRITTEN (pfile);
1824 if (get_directive_token (pfile) != CPP_OTHER
1825 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1826 || CPP_PWRITTEN (pfile)[-1] != '!')
1829 /* ...then `defined', */
1830 token_offset = CPP_WRITTEN (pfile);
1831 token = get_directive_token (pfile);
1832 if (token != CPP_NAME)
1834 ident = pfile->token_buffer + token_offset;
1835 CPP_NUL_TERMINATE (pfile);
1836 if (strcmp (ident, "defined"))
1839 /* ...then an optional '(' and the name, */
1840 token_offset = CPP_WRITTEN (pfile);
1841 token = get_directive_token (pfile);
1842 if (token == CPP_LPAREN)
1844 token_offset = CPP_WRITTEN (pfile);
1845 token = get_directive_token (pfile);
1846 if (token != CPP_NAME)
1850 else if (token != CPP_NAME)
1853 ident = pfile->token_buffer + token_offset;
1854 CPP_NUL_TERMINATE (pfile);
1856 /* ...then the ')', if necessary, */
1857 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1858 /* ...and make sure there's nothing else on the line. */
1859 && get_directive_token (pfile) == CPP_VSPACE)
1860 control_macro = (U_CHAR *) xstrdup (ident);
1863 CPP_SET_WRITTEN (pfile, base_offset);
1864 pfile->no_macro_expand--;
1865 parse_goto_mark (pfile);
1868 return control_macro;
1872 * #if is straightforward; just call eval_if_expr, then conditional_skip.
1873 * Also, check for a reinclude preventer of the form #if !defined (MACRO).
1877 do_if (pfile, keyword)
1879 const struct directive *keyword ATTRIBUTE_UNUSED;
1881 U_CHAR *control_macro = detect_if_not_defined (pfile);
1882 int value = eval_if_expr (pfile);
1883 conditional_skip (pfile, value == 0, T_IF, control_macro);
1888 * handle a #elif directive by not changing if_stack either.
1889 * see the comment above do_else.
1893 do_elif (pfile, keyword)
1895 const struct directive *keyword ATTRIBUTE_UNUSED;
1897 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1899 cpp_error (pfile, "`#elif' not within a conditional");
1904 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1906 cpp_error (pfile, "`#elif' after `#else'");
1907 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1908 "the conditional began here");
1910 pfile->if_stack->type = T_ELIF;
1913 if (pfile->if_stack->if_succeeded)
1914 skip_if_group (pfile);
1917 int value = eval_if_expr (pfile);
1919 skip_if_group (pfile);
1922 ++pfile->if_stack->if_succeeded; /* continue processing input */
1923 output_line_command (pfile, same_file);
1929 /* Thin wrapper around _cpp_parse_expr, which doesn't have access to
1930 * skip_rest_of_line. Also centralizes toggling parsing_if_directive.
1934 eval_if_expr (pfile)
1938 long old_written = CPP_WRITTEN (pfile);
1940 pfile->parsing_if_directive++;
1941 value = _cpp_parse_expr (pfile);
1942 pfile->parsing_if_directive--;
1944 skip_rest_of_line (pfile);
1945 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1951 * routine to handle ifdef/ifndef. Try to look up the symbol,
1952 * then do or don't skip to the #endif/#else/#elif depending
1953 * on what directive is actually being processed.
1957 do_ifdef (pfile, keyword)
1959 const struct directive *keyword;
1964 enum cpp_token token;
1965 int start_of_file = 0;
1966 U_CHAR *control_macro = 0;
1967 int old_written = CPP_WRITTEN (pfile);
1969 /* Detect a #ifndef at start of file (not counting comments). */
1970 if (keyword->type == T_IFNDEF)
1971 start_of_file = pfile->only_seen_white == 2;
1973 pfile->no_macro_expand++;
1974 token = get_directive_token (pfile);
1975 pfile->no_macro_expand--;
1977 ident = pfile->token_buffer + old_written;
1978 ident_length = CPP_WRITTEN (pfile) - old_written;
1979 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1981 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1983 skip = (keyword->type == T_IFDEF);
1984 if (! CPP_TRADITIONAL (pfile))
1985 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1987 else if (token == CPP_NAME)
1989 skip = cpp_defined (pfile, ident, ident_length);
1990 if (keyword->type == T_IFDEF)
1993 if (start_of_file && !skip)
1995 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1996 memcpy (control_macro, ident, ident_length + 1);
2001 skip = (keyword->type == T_IFDEF);
2002 if (! CPP_TRADITIONAL (pfile))
2003 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
2006 if (!CPP_TRADITIONAL (pfile))
2008 cpp_skip_hspace (pfile);
2010 if (c != EOF && c != '\n')
2011 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
2013 skip_rest_of_line (pfile);
2015 conditional_skip (pfile, skip, T_IF, control_macro);
2019 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2020 If this is a #ifndef starting at the beginning of a file,
2021 CONTROL_MACRO is the macro name tested by the #ifndef.
2022 Otherwise, CONTROL_MACRO is 0. */
2025 conditional_skip (pfile, skip, type, control_macro)
2028 enum node_type type;
2029 U_CHAR *control_macro;
2033 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
2034 temp->lineno = CPP_BUFFER (pfile)->lineno;
2035 temp->next = pfile->if_stack;
2036 temp->control_macro = control_macro;
2037 pfile->if_stack = temp;
2039 pfile->if_stack->type = type;
2042 skip_if_group (pfile);
2045 ++pfile->if_stack->if_succeeded;
2046 output_line_command (pfile, same_file);
2050 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2051 return 0 if skipping should continue, 1 if it should halt. Also
2052 adjusts the if_stack as appropriate.
2053 The `#' has been read, but not the identifier. */
2056 consider_directive_while_skipping (pfile, stack)
2060 long ident_len, ident;
2061 const struct directive *kt;
2064 cpp_skip_hspace (pfile);
2066 ident = CPP_WRITTEN (pfile);
2067 parse_name (pfile, GETC());
2068 ident_len = CPP_WRITTEN (pfile) - ident;
2070 CPP_SET_WRITTEN (pfile, ident);
2072 for (kt = directive_table; kt->length >= 0; kt++)
2073 if (kt->length == ident_len
2074 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2080 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
2081 temp->next = pfile->if_stack;
2082 pfile->if_stack = temp;
2083 temp->type = kt->type;
2087 if (pfile->if_stack != stack)
2088 validate_else (pfile, "#else");
2091 if (pfile->if_stack == stack)
2095 pfile->if_stack->type = kt->type;
2100 if (pfile->if_stack != stack)
2101 validate_else (pfile, "#endif");
2103 if (pfile->if_stack == stack)
2106 temp = pfile->if_stack;
2107 pfile->if_stack = temp->next;
2115 /* Don't let erroneous code go by. */
2116 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2117 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2121 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2122 * leaves input ptr at the sharp sign found.
2125 skip_if_group (pfile)
2129 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
2130 const U_CHAR *beg_of_line;
2133 old_written = CPP_WRITTEN (pfile);
2137 beg_of_line = CPP_BUFFER (pfile)->cur;
2139 if (! CPP_TRADITIONAL (pfile))
2140 cpp_skip_hspace (pfile);
2144 CPP_BUMP_LINE (pfile);
2149 if (consider_directive_while_skipping (pfile, save_if_stack))
2153 return; /* Caller will issue error. */
2156 skip_rest_of_line (pfile);
2160 return; /* Caller will issue error. */
2162 CPP_BUMP_LINE (pfile);
2165 /* Back up to the beginning of this line. Caller will process the
2167 CPP_BUFFER (pfile)->cur = beg_of_line;
2168 pfile->only_seen_white = 1;
2172 * handle a #else directive. Do this by just continuing processing
2173 * without changing if_stack ; this is so that the error message
2174 * for missing #endif's etc. will point to the original #if. It
2175 * is possible that something different would be better.
2179 do_else (pfile, keyword)
2181 const struct directive *keyword ATTRIBUTE_UNUSED;
2183 validate_else (pfile, "#else");
2184 skip_rest_of_line (pfile);
2186 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2188 cpp_error (pfile, "`#else' not within a conditional");
2193 /* #ifndef can't have its special treatment for containing the whole file
2194 if it has a #else clause. */
2195 pfile->if_stack->control_macro = 0;
2197 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2199 cpp_error (pfile, "`#else' after `#else'");
2200 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2201 "the conditional began here");
2203 pfile->if_stack->type = T_ELSE;
2206 if (pfile->if_stack->if_succeeded)
2207 skip_if_group (pfile);
2210 ++pfile->if_stack->if_succeeded; /* continue processing input */
2211 output_line_command (pfile, same_file);
2217 * unstack after #endif command
2221 do_endif (pfile, keyword)
2223 const struct directive *keyword ATTRIBUTE_UNUSED;
2225 validate_else (pfile, "#endif");
2226 skip_rest_of_line (pfile);
2228 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2229 cpp_error (pfile, "`#endif' not within a conditional");
2232 IF_STACK *temp = pfile->if_stack;
2233 pfile->if_stack = temp->next;
2234 if (temp->control_macro != 0)
2236 /* This #endif matched a #ifndef at the start of the file.
2237 See if it is at the end of the file. */
2240 parse_set_mark (pfile);
2244 cpp_skip_hspace (pfile);
2249 parse_goto_mark (pfile);
2253 /* This #endif ends a #ifndef
2254 that contains all of the file (aside from whitespace).
2255 Arrange not to include the file again
2256 if the macro that was tested is defined. */
2257 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
2261 output_line_command (pfile, same_file);
2266 /* Issue -pedantic warning for text which is not a comment following
2267 an #else or #endif. Do not warn in system headers, as this is harmless
2268 and very common on old systems. */
2271 validate_else (pfile, directive)
2273 const char *directive;
2275 if (! CPP_PEDANTIC (pfile))
2278 cpp_skip_hspace (pfile);
2279 if (PEEKC () != '\n')
2281 "text following `%s' violates ANSI standard", directive);
2284 /* Convert T_IF, etc. to a string. Used in error messages. */
2286 if_directive_name (pfile, ifs)
2288 struct if_stack *ifs;
2292 case T_IF: return "#if";
2293 case T_IFDEF: return "#ifdef";
2294 case T_IFNDEF: return "#ifndef";
2295 case T_ELIF: return "#elif";
2296 case T_ELSE: return "#else";
2298 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2303 /* Get the next token, and add it to the text in pfile->token_buffer.
2304 Return the kind of token we got. */
2307 cpp_get_token (pfile)
2310 register int c, c2, c3;
2311 enum cpp_token token;
2312 struct cpp_options *opts = CPP_OPTIONS (pfile);
2318 if (CPP_BUFFER (pfile)->manual_pop)
2319 /* If we've been reading from redirected input, the
2320 frontend will pop the buffer. */
2322 else if (CPP_BUFFER (pfile)->seen_eof)
2324 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
2327 cpp_pop_buffer (pfile);
2332 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2333 struct if_stack *ifs, *nifs;
2335 /* Unwind the conditional stack and generate error messages. */
2336 for (ifs = pfile->if_stack;
2337 ifs != CPP_BUFFER (pfile)->if_stack;
2340 cpp_error_with_line (pfile, ifs->lineno, -1,
2341 "unterminated `%s' conditional",
2342 if_directive_name (pfile, ifs));
2347 pfile->if_stack = ifs;
2349 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
2351 /* We're about to return from an #include file.
2352 Emit #line information now (as part of the CPP_POP) result.
2353 But the #line refers to the file we will pop to. */
2354 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2355 CPP_BUFFER (pfile) = next_buf;
2356 pfile->input_stack_listing_current = 0;
2357 output_line_command (pfile, leave_file);
2358 CPP_BUFFER (pfile) = cur_buffer;
2361 CPP_BUFFER (pfile)->seen_eof = 1;
2370 if (PEEKC () == '=')
2374 if (opts->discard_comments)
2375 c = skip_comment (pfile, c);
2377 c = copy_comment (pfile, c);
2381 /* Comments are equivalent to spaces.
2382 For -traditional, a comment is equivalent to nothing. */
2383 if (opts->traditional || !opts->discard_comments)
2387 CPP_PUTC (pfile, c);
2392 if (pfile->parsing_if_directive)
2394 cpp_skip_hspace (pfile);
2395 parse_assertion (pfile);
2396 return CPP_ASSERTION;
2399 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2401 CPP_RESERVE (pfile, 3);
2402 CPP_PUTC_Q (pfile, '#');
2403 CPP_NUL_TERMINATE_Q (pfile);
2404 if (PEEKC () != '#')
2405 return CPP_STRINGIZE;
2408 CPP_PUTC_Q (pfile, '#');
2409 CPP_NUL_TERMINATE_Q (pfile);
2410 return CPP_TOKPASTE;
2413 if (!pfile->only_seen_white)
2415 /* -traditional directives are recognized only with the # in
2417 XXX Layering violation. */
2418 if (CPP_TRADITIONAL (pfile)
2419 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2421 if (handle_directive (pfile))
2422 return CPP_DIRECTIVE;
2423 pfile->only_seen_white = 0;
2428 parse_string (pfile, c);
2429 pfile->only_seen_white = 0;
2430 return c == '\'' ? CPP_CHAR : CPP_STRING;
2433 if (!opts->dollars_in_ident)
2438 if (opts->cplusplus && PEEKC () == ':')
2446 if (c2 == c || c2 == '=')
2455 if (PEEKC () == '=')
2461 if (c2 == '-' && opts->chill)
2462 goto comment; /* Chill style comment */
2463 if (c2 == '-' || c2 == '=')
2467 if (opts->cplusplus && PEEKN (1) == '*')
2469 /* In C++, there's a ->* operator. */
2471 pfile->only_seen_white = 0;
2472 CPP_RESERVE (pfile, 4);
2473 CPP_PUTC_Q (pfile, c);
2474 CPP_PUTC_Q (pfile, GETC ());
2475 CPP_PUTC_Q (pfile, GETC ());
2476 CPP_NUL_TERMINATE_Q (pfile);
2484 if (pfile->parsing_include_directive)
2488 CPP_PUTC (pfile, c);
2492 if (c == '\n' || c == EOF)
2495 "missing '>' in `#include <FILENAME>'");
2500 if (!CPP_BUFFER (pfile)->has_escapes)
2502 /* Backslash newline is replaced by nothing. */
2503 CPP_ADJUST_WRITTEN (pfile, -1);
2504 CPP_BUMP_LINE (pfile);
2508 /* We might conceivably get \r- or \r<space> in
2509 here. Just delete 'em. */
2511 if (d != '-' && d != ' ')
2512 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2513 CPP_ADJUST_WRITTEN (pfile, -1);
2519 /* else fall through */
2524 /* GNU C++ supports MIN and MAX operators <? and >?. */
2525 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2528 CPP_RESERVE (pfile, 4);
2529 CPP_PUTC (pfile, c);
2530 CPP_PUTC (pfile, c2);
2533 CPP_PUTC_Q (pfile, GETC ());
2534 CPP_NUL_TERMINATE_Q (pfile);
2535 pfile->only_seen_white = 0;
2542 CPP_RESERVE(pfile, 2);
2543 CPP_PUTC_Q (pfile, '.');
2548 /* In C++ there's a .* operator. */
2549 if (opts->cplusplus && c2 == '*')
2552 if (c2 == '.' && PEEKN(1) == '.')
2554 CPP_RESERVE(pfile, 4);
2555 CPP_PUTC_Q (pfile, '.');
2556 CPP_PUTC_Q (pfile, '.');
2557 CPP_PUTC_Q (pfile, '.');
2559 CPP_NUL_TERMINATE_Q (pfile);
2560 pfile->only_seen_white = 0;
2567 pfile->only_seen_white = 0;
2568 CPP_RESERVE(pfile, 3);
2569 CPP_PUTC_Q (pfile, c);
2570 CPP_PUTC_Q (pfile, GETC ());
2571 CPP_NUL_TERMINATE_Q (pfile);
2576 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2578 CPP_PUTC (pfile, c);
2580 parse_string (pfile, c);
2581 pfile->only_seen_white = 0;
2582 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2586 case '0': case '1': case '2': case '3': case '4':
2587 case '5': case '6': case '7': case '8': case '9':
2592 CPP_RESERVE (pfile, 2);
2593 CPP_PUTC_Q (pfile, c);
2597 if (!is_numchar(c) && c != '.'
2598 && ((c2 != 'e' && c2 != 'E'
2599 && ((c2 != 'p' && c2 != 'P')
2600 || CPP_OPTIONS (pfile)->c89))
2601 || (c != '+' && c != '-')))
2606 CPP_NUL_TERMINATE_Q (pfile);
2607 pfile->only_seen_white = 0;
2609 case 'b': case 'c': case 'd': case 'h': case 'o':
2610 case 'B': case 'C': case 'D': case 'H': case 'O':
2611 if (opts->chill && PEEKC () == '\'')
2613 pfile->only_seen_white = 0;
2614 CPP_RESERVE (pfile, 2);
2615 CPP_PUTC_Q (pfile, c);
2616 CPP_PUTC_Q (pfile, '\'');
2622 goto chill_number_eof;
2625 CPP_PUTC (pfile, c);
2629 CPP_RESERVE (pfile, 2);
2630 CPP_PUTC_Q (pfile, c);
2631 CPP_NUL_TERMINATE_Q (pfile);
2638 CPP_NUL_TERMINATE (pfile);
2645 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2646 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2647 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2648 case 'x': case 'y': case 'z':
2649 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2650 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2651 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2656 unsigned char *ident;
2657 int before_name_written = CPP_WRITTEN (pfile);
2659 parse_name (pfile, c);
2660 pfile->only_seen_white = 0;
2661 if (pfile->no_macro_expand)
2663 ident = pfile->token_buffer + before_name_written;
2664 ident_len = CPP_PWRITTEN (pfile) - ident;
2665 hp = _cpp_lookup (pfile, ident, ident_len);
2668 if (hp->type == T_DISABLED)
2670 if (pfile->output_escapes)
2671 { /* Return "\r-IDENT", followed by '\0'. */
2673 CPP_RESERVE (pfile, 3);
2674 ident = pfile->token_buffer + before_name_written;
2675 CPP_ADJUST_WRITTEN (pfile, 2);
2676 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2683 /* If macro wants an arglist, verify that a '(' follows. */
2684 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2686 int macbuf_whitespace = 0;
2688 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2690 const U_CHAR *point = CPP_BUFFER (pfile)->cur;
2693 cpp_skip_hspace (pfile);
2700 if (point != CPP_BUFFER (pfile)->cur)
2701 macbuf_whitespace = 1;
2705 goto not_macro_call;
2706 cpp_pop_buffer (pfile);
2709 parse_set_mark (pfile);
2712 cpp_skip_hspace (pfile);
2719 parse_goto_mark (pfile);
2725 if (macbuf_whitespace)
2726 CPP_PUTC (pfile, ' ');
2730 /* This is now known to be a macro call.
2731 Expand the macro, reading arguments as needed,
2732 and push the expansion on the input stack. */
2733 _cpp_macroexpand (pfile, hp);
2734 CPP_SET_WRITTEN (pfile, before_name_written);
2738 case ' ': case '\t': case '\v':
2741 CPP_PUTC (pfile, c);
2743 if (c == EOF || !is_hspace(c))
2750 if (CPP_BUFFER (pfile)->has_escapes)
2755 if (pfile->output_escapes)
2756 CPP_PUTS (pfile, "\r-", 2);
2757 parse_name (pfile, GETC ());
2762 CPP_RESERVE (pfile, 2);
2763 if (pfile->output_escapes)
2764 CPP_PUTC_Q (pfile, '\r');
2765 CPP_PUTC_Q (pfile, c);
2770 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2776 /* Backslash newline is ignored. */
2777 CPP_BUMP_LINE (pfile);
2782 CPP_PUTC (pfile, c);
2783 if (pfile->only_seen_white == 0)
2784 pfile->only_seen_white = 1;
2785 CPP_BUMP_LINE (pfile);
2786 if (! CPP_OPTIONS (pfile)->no_line_commands)
2789 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2790 output_line_command (pfile, same_file);
2794 case '(': token = CPP_LPAREN; goto char1;
2795 case ')': token = CPP_RPAREN; goto char1;
2796 case '{': token = CPP_LBRACE; goto char1;
2797 case '}': token = CPP_RBRACE; goto char1;
2798 case ',': token = CPP_COMMA; goto char1;
2799 case ';': token = CPP_SEMICOLON; goto char1;
2805 pfile->only_seen_white = 0;
2806 CPP_PUTC (pfile, c);
2812 /* Like cpp_get_token, but skip spaces and comments. */
2815 cpp_get_non_space_token (pfile)
2818 int old_written = CPP_WRITTEN (pfile);
2821 enum cpp_token token = cpp_get_token (pfile);
2822 if (token != CPP_COMMENT && token != CPP_POP
2823 && token != CPP_HSPACE && token != CPP_VSPACE)
2825 CPP_SET_WRITTEN (pfile, old_written);
2829 /* Parse an identifier starting with C. */
2832 parse_name (pfile, c)
2844 if (c == '$' && CPP_PEDANTIC (pfile))
2845 cpp_pedwarn (pfile, "`$' in identifier");
2847 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2848 CPP_PUTC_Q (pfile, c);
2853 CPP_NUL_TERMINATE_Q (pfile);
2857 /* Parse and skip over a string starting with C. A single quoted
2858 string is treated like a double -- some programs (e.g., troff) are
2859 perverse this way. (However, a single quoted string is not allowed
2860 to extend over multiple lines.) */
2862 skip_string (pfile, c)
2866 long start_line, start_column;
2867 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2875 cpp_error_with_line (pfile, start_line, start_column,
2876 "unterminated string or character constant");
2877 if (pfile->multiline_string_line != start_line
2878 && pfile->multiline_string_line != 0)
2879 cpp_error_with_line (pfile,
2880 pfile->multiline_string_line, -1,
2881 "possible real start of unterminated constant");
2882 pfile->multiline_string_line = 0;
2886 CPP_BUMP_LINE (pfile);
2887 /* In Fortran and assembly language, silently terminate
2888 strings of either variety at end of line. This is a
2889 kludge around not knowing where comments are in these
2891 if (CPP_OPTIONS (pfile)->lang_fortran
2892 || CPP_OPTIONS (pfile)->lang_asm)
2897 /* Character constants may not extend over multiple lines.
2898 In Standard C, neither may strings. We accept multiline
2899 strings as an extension. */
2902 cpp_error_with_line (pfile, start_line, start_column,
2903 "unterminated character constant");
2907 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2908 cpp_pedwarn_with_line (pfile, start_line, start_column,
2909 "string constant runs past end of line");
2910 if (pfile->multiline_string_line == 0)
2911 pfile->multiline_string_line = start_line;
2915 if (CPP_BUFFER (pfile)->has_escapes)
2917 cpp_ice (pfile, "\\r escape inside string constant");
2921 /* Backslash newline is replaced by nothing at all. */
2922 CPP_BUMP_LINE (pfile);
2938 /* Parse a string and copy it to the output. */
2941 parse_string (pfile, c)
2945 const U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
2946 const U_CHAR *limit;
2948 skip_string (pfile, c);
2950 limit = CPP_BUFFER (pfile)->cur;
2951 CPP_RESERVE (pfile, limit - start + 2);
2952 CPP_PUTC_Q (pfile, c);
2953 for (; start < limit; start++)
2955 CPP_PUTC_Q (pfile, *start);
2958 /* Read an assertion into the token buffer, converting to
2959 canonical form: `#predicate(a n swe r)' The next non-whitespace
2960 character to read should be the first letter of the predicate.
2961 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2962 with answer (see callers for why). In case of 0, an error has been
2965 parse_assertion (pfile)
2969 cpp_skip_hspace (pfile);
2971 if (! is_idstart(c))
2973 cpp_error (pfile, "assertion predicate is not an identifier");
2976 CPP_PUTC(pfile, '#');
2978 parse_name(pfile, c);
2983 if (is_hspace(c) || c == '\r')
2984 cpp_skip_hspace (pfile);
2990 CPP_PUTC(pfile, '(');
2993 while ((c = GETC()) != ')')
2999 CPP_PUTC(pfile, ' ');
3003 else if (c == '\n' || c == EOF)
3005 if (c == '\n') FORWARD(-1);
3006 cpp_error (pfile, "un-terminated assertion answer");
3010 /* \r cannot be a macro escape here. */
3011 CPP_BUMP_LINE (pfile);
3014 CPP_PUTC (pfile, c);
3019 if (pfile->limit[-1] == ' ')
3020 pfile->limit[-1] = ')';
3021 else if (pfile->limit[-1] == '(')
3023 cpp_error (pfile, "empty token sequence in assertion");
3027 CPP_PUTC (pfile, ')');
3029 CPP_NUL_TERMINATE (pfile);
3034 do_assert (pfile, keyword)
3036 const struct directive *keyword ATTRIBUTE_UNUSED;
3040 HASHNODE *base, *this;
3041 HASHNODE **bslot, **tslot;
3043 unsigned long bhash, thash;
3045 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3046 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3048 cpp_skip_hspace (pfile);
3049 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
3050 ret = parse_assertion (pfile);
3055 cpp_error (pfile, "missing token-sequence in `#assert'");
3059 cpp_skip_hspace (pfile);
3061 if (c != EOF && c != '\n')
3063 cpp_error (pfile, "junk at end of `#assert'");
3067 tlen = strlen (sym);
3068 blen = (U_CHAR *) strchr (sym, '(') - sym;
3069 tslot = _cpp_lookup_slot (pfile, sym, tlen, 1, &thash);
3072 cpp_warning (pfile, "`%s' re-asserted", sym);
3076 bslot = _cpp_lookup_slot (pfile, sym, blen, 1, &bhash);
3078 *bslot = base = _cpp_make_hashnode (sym, blen, T_ASSERT, bhash);
3082 if (base->type != T_ASSERT)
3084 /* Token clash - but with what?! */
3085 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3089 *tslot = this = _cpp_make_hashnode (sym, tlen, T_ASSERT, thash);
3090 this->value.aschain = base->value.aschain;
3091 base->value.aschain = this;
3093 pfile->limit = sym; /* Pop */
3097 skip_rest_of_line (pfile);
3098 pfile->limit = sym; /* Pop */
3103 do_unassert (pfile, keyword)
3105 const struct directive *keyword ATTRIBUTE_UNUSED;
3109 long baselen, thislen;
3110 HASHNODE *base, *this, *next;
3112 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3113 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3115 cpp_skip_hspace (pfile);
3117 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
3118 ret = parse_assertion (pfile);
3122 cpp_skip_hspace (pfile);
3124 if (c != EOF && c != '\n')
3125 cpp_error (pfile, "junk at end of `#unassert'");
3127 thislen = strlen (sym);
3130 base = _cpp_lookup (pfile, sym, thislen);
3132 goto error; /* It isn't an error to #undef what isn't #defined,
3133 so it isn't an error to #unassert what isn't
3134 #asserted either. */
3136 for (this = base->value.aschain; this; this = next)
3138 next = this->value.aschain;
3139 htab_remove_elt (pfile->hashtab, this);
3141 htab_remove_elt (pfile->hashtab, base);
3145 baselen = (U_CHAR *) strchr (sym, '(') - sym;
3146 base = _cpp_lookup (pfile, sym, baselen);
3147 if (! base) goto error;
3148 this = _cpp_lookup (pfile, sym, thislen);
3149 if (! this) goto error;
3152 while (next->value.aschain != this)
3153 next = next->value.aschain;
3155 next->value.aschain = this->value.aschain;
3156 htab_remove_elt (pfile->hashtab, this);
3158 if (base->value.aschain == NULL)
3159 /* Last answer for this predicate deleted. */
3160 htab_remove_elt (pfile->hashtab, base);
3163 pfile->limit = sym; /* Pop */
3166 skip_rest_of_line (pfile);
3167 pfile->limit = sym; /* Pop */
3171 /* Process STR as if it appeared as the body of an #unassert. */
3173 cpp_unassert (pfile, str)
3177 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3179 do_unassert (pfile, NULL);
3180 cpp_pop_buffer (pfile);
3184 /* Remember the current position of PFILE so it may be returned to
3185 after looking ahead a bit.
3187 Note that when you set a mark, you _must_ return to that mark. You
3188 may not forget about it and continue parsing. You may not pop a
3189 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3193 parse_set_mark (pfile)
3196 cpp_buffer *ip = CPP_BUFFER (pfile);
3197 if (ACTIVE_MARK_P())
3198 cpp_ice (pfile, "mark active in parse_set_mark");
3200 ip->mark = ip->cur - ip->buf;
3203 /* Backup the current position of PFILE to that saved in its mark,
3204 and clear the mark. */
3207 parse_goto_mark (pfile)
3210 cpp_buffer *ip = CPP_BUFFER (pfile);
3211 if (!ACTIVE_MARK_P())
3212 cpp_ice (pfile, "mark not active in parse_goto_mark");
3214 ip->cur = ip->buf + ip->mark;