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 /* `struct directive' defines one #-directive, including how to handle it. */
48 int length; /* Length of name */
49 int (*func) /* Function to handle directive */
50 PARAMS ((cpp_reader *, const struct directive *));
51 const char *name; /* Name of directive */
52 enum node_type type; /* Code which describes which directive. */
55 /* These functions are declared to return int instead of void since they
56 are going to be placed in a table and some old compilers have trouble with
57 pointers to functions returning void. */
59 static int do_define PARAMS ((cpp_reader *, const struct directive *));
60 static int do_line PARAMS ((cpp_reader *, const struct directive *));
61 static int do_include PARAMS ((cpp_reader *, const struct directive *));
62 static int do_undef PARAMS ((cpp_reader *, const struct directive *));
63 static int do_error PARAMS ((cpp_reader *, const struct directive *));
64 static int do_pragma PARAMS ((cpp_reader *, const struct directive *));
65 static int do_ident PARAMS ((cpp_reader *, const struct directive *));
66 static int do_if PARAMS ((cpp_reader *, const struct directive *));
67 static int do_xifdef PARAMS ((cpp_reader *, const struct directive *));
68 static int do_else PARAMS ((cpp_reader *, const struct directive *));
69 static int do_elif PARAMS ((cpp_reader *, const struct directive *));
70 static int do_endif PARAMS ((cpp_reader *, const struct directive *));
72 static int do_sccs PARAMS ((cpp_reader *, const struct directive *));
74 static int do_assert PARAMS ((cpp_reader *, const struct directive *));
75 static int do_unassert PARAMS ((cpp_reader *, const struct directive *));
76 static int do_warning PARAMS ((cpp_reader *, const struct directive *));
78 /* Forward declarations. */
80 static void validate_else PARAMS ((cpp_reader *, const char *));
81 static HOST_WIDEST_INT eval_if_expr PARAMS ((cpp_reader *));
82 static void conditional_skip PARAMS ((cpp_reader *, int,
83 enum node_type, U_CHAR *));
84 static void skip_if_group PARAMS ((cpp_reader *));
85 static void parse_name PARAMS ((cpp_reader *, int));
86 static void parse_string PARAMS ((cpp_reader *, int));
87 static int parse_assertion PARAMS ((cpp_reader *));
88 static const char *if_directive_name PARAMS ((cpp_reader *,
90 static int null_cleanup PARAMS ((cpp_buffer *, cpp_reader *));
91 static int skip_comment PARAMS ((cpp_reader *, int));
92 static int copy_comment PARAMS ((cpp_reader *, int));
93 static void skip_string PARAMS ((cpp_reader *, int));
94 static void skip_rest_of_line PARAMS ((cpp_reader *));
95 static void cpp_skip_hspace PARAMS ((cpp_reader *));
96 static int handle_directive PARAMS ((cpp_reader *));
97 static void pass_thru_directive PARAMS ((const U_CHAR *, size_t,
99 const struct directive *));
100 static int read_line_number PARAMS ((cpp_reader *, int *));
101 static U_CHAR *detect_if_not_defined PARAMS ((cpp_reader *));
102 static int consider_directive_while_skipping
103 PARAMS ((cpp_reader *, IF_STACK *));
104 static void skip_block_comment PARAMS ((cpp_reader *));
105 static void skip_line_comment PARAMS ((cpp_reader *));
106 static void parse_set_mark PARAMS ((cpp_reader *));
107 static void parse_goto_mark PARAMS ((cpp_reader *));
108 static int get_macro_name PARAMS ((cpp_reader *));
110 /* Here is the actual list of #-directives.
111 This table is ordered by frequency of occurrence; the numbers
112 at the end are directive counts from all the source code I have
113 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
114 linux-2.2.9, and pcmcia-cs-3.0.9). */
116 static const struct directive directive_table[] = {
118 { 6, do_define, "define", T_DEFINE }, /* 270554 */
119 { 7, do_include, "include", T_INCLUDE }, /* 52262 */
120 { 5, do_endif, "endif", T_ENDIF }, /* 45855 */
121 { 5, do_xifdef, "ifdef", T_IFDEF }, /* 22000 */
122 { 2, do_if, "if", T_IF }, /* 18162 */
123 { 4, do_else, "else", T_ELSE }, /* 9863 */
124 { 6, do_xifdef, "ifndef", T_IFNDEF }, /* 9675 */
125 { 5, do_undef, "undef", T_UNDEF }, /* 4837 */
126 { 4, do_line, "line", T_LINE }, /* 2465 */
127 { 4, do_elif, "elif", T_ELIF }, /* 610 */
128 { 5, do_error, "error", T_ERROR }, /* 475 */
129 { 6, do_pragma, "pragma", T_PRAGMA }, /* 195 */
131 /* Extensions. All deprecated except #warning and #include_next. */
132 { 7, do_warning, "warning", T_WARNING }, /* 22 - GNU */
133 { 12, do_include, "include_next", T_INCLUDE_NEXT }, /* 19 - GNU */
134 { 5, do_ident, "ident", T_IDENT }, /* 11 - SVR4 */
135 { 6, do_include, "import", T_IMPORT }, /* 0 - ObjC */
136 { 6, do_assert, "assert", T_ASSERT }, /* 0 - SVR4 */
137 { 8, do_unassert, "unassert", T_UNASSERT }, /* 0 - SVR4 */
138 #ifdef SCCS_DIRECTIVE
139 { 4, do_sccs, "sccs", T_SCCS }, /* 0 - SVR2? */
141 { -1, 0, "", T_UNUSED }
144 /* Place into PFILE a quoted string representing the string SRC.
145 Caller must reserve enough space in pfile->token_buffer. */
148 quote_string (pfile, src)
154 CPP_PUTC_Q (pfile, '\"');
156 switch ((c = *src++))
160 CPP_PUTC_Q (pfile, c);
163 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
164 CPP_ADJUST_WRITTEN (pfile, 4);
170 CPP_PUTC_Q (pfile, '\\');
171 CPP_PUTC_Q (pfile, c);
175 CPP_PUTC_Q (pfile, '\"');
176 CPP_NUL_TERMINATE_Q (pfile);
181 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
184 cpp_grow_buffer (pfile, n)
188 long old_written = CPP_WRITTEN (pfile);
189 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
190 pfile->token_buffer = (U_CHAR *)
191 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
192 CPP_SET_WRITTEN (pfile, old_written);
195 /* Process the string STR as if it appeared as the body of a #define.
196 If STR is just an identifier, define it with value 1.
197 If STR has anything after the identifier, then it should
198 be identifier=definition. */
201 cpp_define (pfile, str)
208 p = strchr (str, '=');
209 /* Copy the entire option so we can modify it.
210 Change the first "=" in the string to a space. If there is none,
211 tack " 1" on the end. Then add a newline and a NUL. */
215 count = strlen (str) + 2;
216 buf = (U_CHAR *) alloca (count);
217 memcpy (buf, str, count - 2);
219 buf[count - 2] = '\n';
220 buf[count - 1] = '\0';
224 count = strlen (str) + 4;
225 buf = (U_CHAR *) alloca (count);
226 memcpy (buf, str, count - 4);
227 strcpy (&buf[count-4], " 1\n");
230 if (cpp_push_buffer (pfile, buf, count - 1) != NULL)
232 do_define (pfile, NULL);
233 cpp_pop_buffer (pfile);
237 /* Process the string STR as if it appeared as the body of a #assert. */
239 cpp_assert (pfile, str)
243 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
245 do_assert (pfile, NULL);
246 cpp_pop_buffer (pfile);
250 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
252 cpp_defined (pfile, id, len)
257 HASHNODE *hp = _cpp_lookup (pfile, id, len);
258 if (hp && hp->type == T_POISON)
260 cpp_error (pfile, "attempt to use poisoned `%s'", hp->name);
267 null_cleanup (pbuf, pfile)
268 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
269 cpp_reader *pfile ATTRIBUTE_UNUSED;
274 /* Skip a C-style block comment. We know it's a comment, and point is
275 at the second character of the starter. */
277 skip_block_comment (pfile)
284 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
290 cpp_error_with_line (pfile, line, col, "unterminated comment");
293 else if (c == '\n' || c == '\r')
295 /* \r cannot be a macro escape marker here. */
296 if (!ACTIVE_MARK_P())
297 CPP_BUMP_LINE (pfile);
299 else if (c == '/' && prev_c == '*')
301 else if (c == '*' && prev_c == '/'
302 && CPP_OPTIONS (pfile)->warn_comments)
303 cpp_warning (pfile, "`/*' within comment");
309 /* Skip a C++/Chill line comment. We know it's a comment, and point
310 is at the second character of the initiator. */
312 skip_line_comment (pfile)
320 /* We don't have to worry about EOF in here. */
323 /* Don't consider final '\n' to be part of comment. */
329 /* \r cannot be a macro escape marker here. */
330 if (!ACTIVE_MARK_P())
331 CPP_BUMP_LINE (pfile);
332 if (CPP_OPTIONS (pfile)->warn_comments)
333 cpp_warning (pfile, "backslash-newline within line comment");
338 /* Skip a comment - C, C++, or Chill style. M is the first character
339 of the comment marker. If this really is a comment, skip to its
340 end and return ' '. If this is not a comment, return M (which will
344 skip_comment (pfile, m)
348 if (m == '/' && PEEKC() == '*')
350 skip_block_comment (pfile);
353 else if (m == '/' && PEEKC() == '/')
355 if (CPP_BUFFER (pfile)->system_header_p)
357 /* We silently allow C++ comments in system headers, irrespective
358 of conformance mode, because lots of busted systems do that
359 and trying to clean it up in fixincludes is a nightmare. */
360 skip_line_comment (pfile);
363 else if (CPP_OPTIONS (pfile)->cplusplus_comments)
365 if (CPP_OPTIONS (pfile)->c89
366 && CPP_PEDANTIC (pfile)
367 && ! CPP_BUFFER (pfile)->warned_cplusplus_comments)
370 "C++ style comments are not allowed in ISO C89");
372 "(this will be reported only once per input file)");
373 CPP_BUFFER (pfile)->warned_cplusplus_comments = 1;
375 skip_line_comment (pfile);
381 else if (m == '-' && PEEKC() == '-'
382 && CPP_OPTIONS (pfile)->chill)
384 skip_line_comment (pfile);
391 /* Identical to skip_comment except that it copies the comment into the
392 token_buffer. This is used if !discard_comments. */
394 copy_comment (pfile, m)
398 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
401 if (skip_comment (pfile, m) == m)
404 limit = CPP_BUFFER (pfile)->cur;
405 CPP_RESERVE (pfile, limit - start + 2);
406 CPP_PUTC_Q (pfile, m);
407 for (; start <= limit; start++)
409 CPP_PUTC_Q (pfile, *start);
414 /* Skip whitespace \-newline and comments. Does not macro-expand. */
417 cpp_skip_hspace (pfile)
426 else if (is_hspace(c))
428 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
429 cpp_pedwarn (pfile, "%s in preprocessing directive",
430 c == '\f' ? "formfeed" : "vertical tab");
434 /* \r is a backslash-newline marker if !has_escapes, and
435 a deletable-whitespace or no-reexpansion marker otherwise. */
436 if (CPP_BUFFER (pfile)->has_escapes)
444 CPP_BUMP_LINE (pfile);
446 else if (c == '/' || c == '-')
448 c = skip_comment (pfile, c);
458 /* Read and discard the rest of the current line. */
461 skip_rest_of_line (pfile)
475 if (! CPP_BUFFER (pfile)->has_escapes)
476 CPP_BUMP_LINE (pfile);
481 skip_string (pfile, c);
486 skip_comment (pfile, c);
491 if (CPP_PEDANTIC (pfile))
492 cpp_pedwarn (pfile, "%s in preprocessing directive",
493 c == '\f' ? "formfeed" : "vertical tab");
500 /* Handle a possible # directive.
501 '#' has already been read. */
504 handle_directive (pfile)
508 register const struct directive *kt;
511 long old_written = CPP_WRITTEN (pfile);
513 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
515 cpp_ice (pfile, "handle_directive called on macro buffer");
519 cpp_skip_hspace (pfile);
522 /* # followed by a number is equivalent to #line. Do not recognize
523 this form in assembly language source files. Complain about this
524 form if we're being pedantic, but not if this is regurgitated
525 input (preprocessed or fed back in by the C++ frontend). */
526 if (c >= '0' && c <= '9')
528 if (CPP_OPTIONS (pfile)->lang_asm)
531 if (CPP_PEDANTIC (pfile)
532 && ! CPP_PREPROCESSED (pfile)
533 && ! CPP_BUFFER (pfile)->manual_pop)
534 cpp_pedwarn (pfile, "`#' followed by integer");
535 do_line (pfile, NULL);
539 /* If we are rescanning preprocessed input, don't obey any directives
541 if (CPP_PREPROCESSED (pfile))
544 /* Now find the directive name. */
545 CPP_PUTC (pfile, '#');
546 parse_name (pfile, GETC());
547 ident = pfile->token_buffer + old_written + 1;
548 ident_length = CPP_PWRITTEN (pfile) - ident;
549 if (ident_length == 0)
551 /* A line of just `#' becomes blank. A line with something
552 other than an identifier after the # is reparsed as a non-
554 CPP_SET_WRITTEN (pfile, old_written);
555 return (PEEKC() == '\n');
558 /* Decode the keyword and call the appropriate expansion routine. */
559 for (kt = directive_table; ; kt++)
562 /* # identifier, but not a legit directive. Pass onward as a
563 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
565 if (kt->length == ident_length
566 && !strncmp (kt->name, ident, ident_length))
570 CPP_SET_WRITTEN (pfile, old_written);
572 if (pfile->no_directives)
574 cpp_error (pfile, "`#%s' may not be used inside a macro argument",
576 skip_rest_of_line (pfile);
579 (*kt->func) (pfile, kt);
584 /* Pass a directive through to the output file.
585 BUF points to the contents of the directive, as a contiguous string.
586 LEN is the length of the string pointed to by BUF.
587 KEYWORD is the keyword-table entry for the directive. */
590 pass_thru_directive (buf, len, pfile, keyword)
594 const struct directive *keyword;
596 register unsigned keyword_length = keyword->length;
598 CPP_RESERVE (pfile, 1 + keyword_length + len);
599 CPP_PUTC_Q (pfile, '#');
600 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
601 if (len != 0 && buf[0] != ' ')
602 CPP_PUTC_Q (pfile, ' ');
603 CPP_PUTS_Q (pfile, buf, len);
606 /* Subroutine of do_define: determine the name of the macro to be
610 get_macro_name (pfile)
615 here = CPP_WRITTEN (pfile);
616 pfile->no_macro_expand++;
617 if (get_directive_token (pfile) != CPP_NAME)
619 cpp_error (pfile, "`#define' must be followed by an identifier");
623 len = CPP_WRITTEN (pfile) - here;
624 if (len == 7 && !strncmp (pfile->token_buffer + here, "defined", 7))
626 cpp_error (pfile, "`defined' is not a legal macro name");
630 pfile->no_macro_expand--;
634 skip_rest_of_line (pfile);
635 pfile->no_macro_expand--;
639 /* Process a #define command.
640 KEYWORD is the keyword-table entry for #define,
641 or NULL for a "predefined" macro. */
644 do_define (pfile, keyword)
646 const struct directive *keyword ATTRIBUTE_UNUSED;
655 here = CPP_WRITTEN (pfile);
656 len = get_macro_name (pfile);
660 /* Copy out the name so we can pop the token buffer. */
661 len = CPP_WRITTEN (pfile) - here;
662 sym = (U_CHAR *) alloca (len + 1);
663 memcpy (sym, pfile->token_buffer + here, len);
665 CPP_SET_WRITTEN (pfile, here);
667 /* If the next character, with no intervening whitespace, is '(',
668 then this is a function-like macro. */
672 else if (c != '\n' && !is_hspace (c))
673 /* Otherwise, C99 requires white space after the name. We treat it
674 as an object-like macro if this happens, with a warning. */
675 cpp_pedwarn (pfile, "missing white space after `#define %.*s'", len, sym);
677 def = _cpp_create_definition (pfile, funlike);
681 if ((hp = _cpp_lookup (pfile, sym, len)) != NULL)
685 /* Redefining a macro is ok if the definitions are the same. */
686 if (hp->type == T_MACRO)
687 ok = ! _cpp_compare_defs (pfile, def, hp->value.defn);
688 /* Redefining a constant is ok with -D. */
689 else if (hp->type == T_CONST || hp->type == T_STDC)
690 ok = ! CPP_OPTIONS (pfile)->done_initializing;
691 /* Otherwise it's not ok. */
694 /* Print the warning or error if it's not ok. */
697 if (hp->type == T_POISON)
698 cpp_error (pfile, "redefining poisoned `%.*s'", len, sym);
700 cpp_pedwarn (pfile, "`%.*s' redefined", len, sym);
701 if (hp->type == T_MACRO && CPP_OPTIONS (pfile)->done_initializing)
703 DEFINITION *d = hp->value.defn;
704 cpp_pedwarn_with_file_and_line (pfile, d->file, d->line, d->col,
705 "this is the location of the previous definition");
708 if (hp->type != T_POISON)
710 /* Replace the old definition. */
711 if (hp->type == T_MACRO)
712 _cpp_free_definition (hp->value.defn);
714 hp->value.defn = def;
718 _cpp_install (pfile, sym, len, T_MACRO, (char *) def);
720 if (CPP_OPTIONS (pfile)->debug_output
721 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions)
722 _cpp_dump_definition (pfile, sym, len, def);
723 else if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
724 pass_thru_directive (sym, len, pfile, keyword);
730 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
731 If BUFFER != NULL, then use the LENGTH characters in BUFFER
732 as the new input buffer.
733 Return the new buffer, or NULL on failure. */
736 cpp_push_buffer (pfile, buffer, length)
741 cpp_buffer *buf = CPP_BUFFER (pfile);
743 if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
745 cpp_fatal (pfile, "macro or `#include' recursion too deep");
749 new = (cpp_buffer *) xcalloc (1, sizeof (cpp_buffer));
751 new->if_stack = pfile->if_stack;
752 new->cleanup = null_cleanup;
753 new->buf = new->cur = buffer;
754 new->alimit = new->rlimit = buffer + length;
757 new->line_base = NULL;
759 CPP_BUFFER (pfile) = new;
764 cpp_pop_buffer (pfile)
767 cpp_buffer *buf = CPP_BUFFER (pfile);
769 cpp_ice (pfile, "mark active in cpp_pop_buffer");
770 (*buf->cleanup) (buf, pfile);
771 CPP_BUFFER (pfile) = CPP_PREV_BUFFER (buf);
773 pfile->buffer_stack_depth--;
774 return CPP_BUFFER (pfile);
777 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
778 Pop the buffer when done. */
781 cpp_scan_buffer (pfile)
784 cpp_buffer *buffer = CPP_BUFFER (pfile);
785 enum cpp_token token;
786 if (CPP_OPTIONS (pfile)->no_output)
788 long old_written = CPP_WRITTEN (pfile);
789 /* In no-output mode, we can ignore everything but directives. */
792 if (! pfile->only_seen_white)
793 skip_rest_of_line (pfile);
794 token = cpp_get_token (pfile);
795 if (token == CPP_EOF) /* Should not happen ... */
797 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
799 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
800 cpp_pop_buffer (pfile);
804 CPP_SET_WRITTEN (pfile, old_written);
810 token = cpp_get_token (pfile);
811 if (token == CPP_EOF) /* Should not happen ... */
813 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
815 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != NULL)
816 cpp_pop_buffer (pfile);
824 * Rescan a string (which may have escape marks) into pfile's buffer.
825 * Place the result in pfile->token_buffer.
827 * The input is copied before it is scanned, so it is safe to pass
828 * it something from the token_buffer that will get overwritten
829 * (because it follows CPP_WRITTEN). This is used by do_include.
833 cpp_expand_to_buffer (pfile, buf, length)
838 register cpp_buffer *ip;
844 cpp_ice (pfile, "length < 0 in cpp_expand_to_buffer");
848 /* Set up the input on the input stack. */
850 buf1 = (U_CHAR *) alloca (length + 1);
851 memcpy (buf1, buf, length);
854 ip = cpp_push_buffer (pfile, buf1, length);
859 /* Scan the input, create the output. */
860 save_no_output = CPP_OPTIONS (pfile)->no_output;
861 CPP_OPTIONS (pfile)->no_output = 0;
862 CPP_OPTIONS (pfile)->no_line_commands++;
863 cpp_scan_buffer (pfile);
864 CPP_OPTIONS (pfile)->no_line_commands--;
865 CPP_OPTIONS (pfile)->no_output = save_no_output;
867 CPP_NUL_TERMINATE (pfile);
871 cpp_buf_line_and_col (pbuf, linep, colp)
872 register cpp_buffer *pbuf;
877 *linep = pbuf->lineno;
879 *colp = pbuf->cur - pbuf->line_base;
889 /* Return the cpp_buffer that corresponds to a file (not a macro). */
892 cpp_file_buffer (pfile)
897 for (ip = CPP_BUFFER (pfile); ip; ip = CPP_PREV_BUFFER (ip))
898 if (ip->ihash != NULL)
904 * write out a #line command, for instance, after an #include file.
905 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
909 output_line_command (pfile, file_change)
911 enum file_change_code file_change;
916 if (CPP_OPTIONS (pfile)->no_line_commands
917 || CPP_OPTIONS (pfile)->no_output)
920 ip = cpp_file_buffer (pfile);
921 cpp_buf_line_and_col (ip, &line, NULL);
923 /* If the current file has not changed, we omit the #line if it would
924 appear to be a no-op, and we output a few newlines instead
925 if we want to increase the line number by a small amount.
926 We cannot do this if pfile->lineno is zero, because that means we
927 haven't output any line commands yet. (The very first line command
928 output is a `same_file' command.) */
929 if (file_change == same_file && pfile->lineno != 0)
931 if (line == pfile->lineno)
934 /* If the inherited line number is a little too small,
935 output some newlines instead of a #line command. */
936 if (line > pfile->lineno && line < pfile->lineno + 8)
938 CPP_RESERVE (pfile, 20);
939 while (line > pfile->lineno)
941 CPP_PUTC_Q (pfile, '\n');
948 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
949 CPP_PUTS_Q (pfile, "# ", 2);
951 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
952 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
954 quote_string (pfile, ip->nominal_fname);
955 if (file_change != same_file && file_change != rename_file)
957 CPP_PUTC_Q (pfile, ' ');
958 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
960 /* Tell cc1 if following text comes from a system header file. */
961 if (ip->system_header_p)
963 CPP_PUTC_Q (pfile, ' ');
964 CPP_PUTC_Q (pfile, '3');
966 #ifndef NO_IMPLICIT_EXTERN_C
967 /* Tell cc1plus if following text should be treated as C. */
968 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus)
970 CPP_PUTC_Q (pfile, ' ');
971 CPP_PUTC_Q (pfile, '4');
974 CPP_PUTC_Q (pfile, '\n');
975 pfile->lineno = line;
979 /* Like cpp_get_token, except that it does not read past end-of-line.
980 Also, horizontal space is skipped, and macros are popped. */
983 get_directive_token (pfile)
986 long old_written = CPP_WRITTEN (pfile);
987 enum cpp_token token;
991 cpp_skip_hspace (pfile);
992 if (PEEKC () == '\n')
995 token = cpp_get_token (pfile);
996 /* token could be hspace at the beginning of a macro. */
997 if (token == CPP_HSPACE || token == CPP_COMMENT)
999 CPP_SET_WRITTEN (pfile, old_written);
1003 /* token cannot be vspace, it would have been caught above. */
1004 if (token == CPP_VSPACE)
1006 cpp_ice (pfile, "VSPACE in get_directive_token");
1010 /* token cannot be POP unless the buffer is a macro buffer. */
1011 if (token != CPP_POP)
1014 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1016 cpp_ice (pfile, "POP of file buffer in get_directive_token");
1020 /* We must pop the buffer by hand, or else cpp_get_token might
1021 hand us white space or newline on the next invocation. */
1022 cpp_pop_buffer (pfile);
1026 /* Handle #include and #import.
1027 This function expects to see "fname" or <fname> on the input.
1029 The input is normally in part of the output_buffer following
1030 CPP_WRITTEN, and will get overwritten by output_line_command.
1031 I.e. in input file specification has been popped by handle_directive.
1035 do_include (pfile, keyword)
1037 const struct directive *keyword;
1039 int importing = (keyword->type == T_IMPORT);
1040 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
1041 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
1042 int before; /* included before? */
1044 unsigned char *ftok;
1047 enum cpp_token token;
1049 /* Chain of dirs to search */
1051 struct file_name_list *search_start;
1053 long old_written = CPP_WRITTEN (pfile);
1057 if (CPP_PEDANTIC (pfile))
1060 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
1062 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
1065 if (importing && CPP_OPTIONS (pfile)->warn_import
1066 && !CPP_OPTIONS (pfile)->inhibit_warnings
1067 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
1069 pfile->import_warning = 1;
1071 "#import is obsolete, use an #ifndef wrapper in the header file");
1074 pfile->parsing_include_directive++;
1075 token = get_directive_token (pfile);
1076 pfile->parsing_include_directive--;
1078 if (token == CPP_STRING)
1080 if (pfile->token_buffer[old_written] == '<')
1084 else if (token == CPP_NAME)
1086 /* Support '#include xyz' like VAX-C. It is taken as
1087 '#include <xyz.h>' and generates a warning. */
1089 "`#include filename' is obsolete, use `#include <filename.h>'");
1092 /* Append the missing `.h' to the name. */
1093 CPP_PUTS (pfile, ".h", 2);
1099 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
1100 CPP_SET_WRITTEN (pfile, old_written);
1101 skip_rest_of_line (pfile);
1105 flen = CPP_WRITTEN (pfile) - old_written;
1106 ftok = (unsigned char *) alloca (flen + 1);
1107 memcpy (ftok, pfile->token_buffer + old_written, flen);
1110 if (get_directive_token (pfile) != CPP_VSPACE)
1112 cpp_error (pfile, "junk at end of `#include'");
1113 skip_rest_of_line (pfile);
1116 CPP_SET_WRITTEN (pfile, old_written);
1120 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
1124 if (CPP_OPTIONS (pfile)->dump_includes)
1125 pass_thru_directive (ftok,
1128 - ((token == CPP_NAME) ? 2 : 0)
1133 if (token == CPP_STRING)
1143 fp = cpp_file_buffer (pfile);
1145 /* For #include_next, skip in the search path past the dir in which the
1146 containing file was found. Treat files specified using an absolute path
1147 as if there are no more directories to search. Treat the primary source
1148 file like any other included source, but generate a warning. */
1149 if (skip_dirs && CPP_PREV_BUFFER (fp))
1151 if (fp->ihash->foundhere != ABSOLUTE_PATH)
1152 search_start = fp->ihash->foundhere->next;
1157 cpp_warning (pfile, "#include_next in primary source file");
1160 search_start = CPP_OPTIONS (pfile)->bracket_include;
1163 if (!CPP_OPTIONS (pfile)->ignore_srcdir)
1166 search_start = fp->actual_dir;
1169 search_start = CPP_OPTIONS (pfile)->quote_include;
1175 cpp_error (pfile, "No include path in which to find %s", ftok);
1179 fd = _cpp_find_include_file (pfile, ftok, search_start, &ihash, &before);
1186 if (CPP_OPTIONS (pfile)->print_deps_missing_files
1187 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
1188 (pfile->system_include_depth > 0)))
1190 if (!angle_brackets)
1191 deps_add_dep (pfile->deps, ftok);
1195 struct file_name_list *ptr;
1196 /* If requested as a system header, assume it belongs in
1197 the first system header directory. */
1198 if (CPP_OPTIONS (pfile)->bracket_include)
1199 ptr = CPP_OPTIONS (pfile)->bracket_include;
1201 ptr = CPP_OPTIONS (pfile)->quote_include;
1203 p = (char *) alloca (strlen (ptr->name)
1204 + strlen (ftok) + 2);
1205 if (*ptr->name != '\0')
1207 strcpy (p, ptr->name);
1211 deps_add_dep (pfile->deps, p);
1214 /* If -M was specified, and this header file won't be added to
1215 the dependency list, then don't count this as an error,
1216 because we can still produce correct output. Otherwise, we
1217 can't produce correct output, because there may be
1218 dependencies we need inside the missing file, and we don't
1219 know what directory this missing file exists in. */
1220 else if (CPP_PRINT_DEPS (pfile)
1221 && (CPP_PRINT_DEPS (pfile)
1222 <= (angle_brackets || (pfile->system_include_depth > 0))))
1223 cpp_warning (pfile, "No include path in which to find %s", ftok);
1225 cpp_error_from_errno (pfile, ftok);
1230 /* For -M, add the file to the dependencies on its first inclusion. */
1231 if (!before && (CPP_PRINT_DEPS (pfile)
1232 > (angle_brackets || (pfile->system_include_depth > 0))))
1233 deps_add_dep (pfile->deps, ihash->name);
1235 /* Handle -H option. */
1236 if (CPP_OPTIONS(pfile)->print_include_names)
1238 fp = CPP_BUFFER (pfile);
1239 while ((fp = CPP_PREV_BUFFER (fp)) != NULL)
1241 fprintf (stderr, " %s\n", ihash->name);
1244 /* Actually process the file */
1247 ihash->control_macro = "";
1249 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
1256 pfile->system_include_depth++; /* Decremented in file_cleanup. */
1258 if (_cpp_read_include_file (pfile, fd, ihash))
1260 output_line_command (pfile, enter_file);
1261 pfile->only_seen_white = 2;
1267 /* Subroutine of do_line. Read next token from PFILE without adding it to
1268 the output buffer. If it is a number between 1 and 4, store it in *NUM
1269 and return 1; otherwise, return 0 and complain if we aren't at the end
1270 of the directive. */
1273 read_line_number (pfile, num)
1277 long save_written = CPP_WRITTEN (pfile);
1278 U_CHAR *p = pfile->token_buffer + save_written;
1279 enum cpp_token token = get_directive_token (pfile);
1280 CPP_SET_WRITTEN (pfile, save_written);
1282 if (token == CPP_NUMBER && *p >= '1' && *p <= '4' && p[1] == '\0')
1289 if (token != CPP_VSPACE && token != CPP_EOF)
1290 cpp_error (pfile, "invalid format `#line' command");
1295 /* Interpret #line command.
1296 Note that the filename string (if any) is treated as if it were an
1297 include filename. That means no escape handling. */
1300 do_line (pfile, keyword)
1302 const struct directive *keyword ATTRIBUTE_UNUSED;
1304 cpp_buffer *ip = CPP_BUFFER (pfile);
1306 long old_written = CPP_WRITTEN (pfile);
1307 enum file_change_code file_change = same_file;
1308 enum cpp_token token;
1311 token = get_directive_token (pfile);
1313 if (token != CPP_NUMBER)
1315 cpp_error (pfile, "token after `#line' is not an integer");
1316 goto bad_line_directive;
1319 new_lineno = strtol (pfile->token_buffer + old_written, &x, 10);
1322 cpp_error (pfile, "token after `#line' is not an integer");
1323 goto bad_line_directive;
1325 CPP_SET_WRITTEN (pfile, old_written);
1327 if (CPP_PEDANTIC (pfile) && (new_lineno <= 0 || new_lineno > 32767))
1328 cpp_pedwarn (pfile, "line number out of range in `#line' command");
1330 token = get_directive_token (pfile);
1332 if (token == CPP_STRING)
1334 U_CHAR *fname = pfile->token_buffer + old_written + 1;
1335 U_CHAR *end_name = CPP_PWRITTEN (pfile) - 1;
1336 int action_number = 0;
1338 file_change = rename_file;
1340 if (read_line_number (pfile, &action_number))
1342 if (CPP_PEDANTIC (pfile))
1343 cpp_pedwarn (pfile, "garbage at end of `#line' command");
1345 if (action_number == 1)
1347 file_change = enter_file;
1348 read_line_number (pfile, &action_number);
1350 else if (action_number == 2)
1352 file_change = leave_file;
1353 read_line_number (pfile, &action_number);
1355 if (action_number == 3)
1357 ip->system_header_p = 1;
1358 read_line_number (pfile, &action_number);
1360 if (action_number == 4)
1362 ip->system_header_p = 2;
1363 read_line_number (pfile, &action_number);
1369 if (strcmp (fname, ip->nominal_fname))
1371 const char *newname, *oldname;
1372 if (!strcmp (fname, ip->ihash->name))
1373 newname = ip->ihash->name;
1374 else if (ip->last_nominal_fname
1375 && !strcmp (fname, ip->last_nominal_fname))
1376 newname = ip->last_nominal_fname;
1378 newname = xstrdup (fname);
1380 oldname = ip->nominal_fname;
1381 ip->nominal_fname = newname;
1383 if (ip->last_nominal_fname
1384 && ip->last_nominal_fname != oldname
1385 && ip->last_nominal_fname != newname
1386 && ip->last_nominal_fname != ip->ihash->name)
1387 free ((void *) ip->last_nominal_fname);
1389 if (newname == ip->ihash->name)
1390 ip->last_nominal_fname = NULL;
1392 ip->last_nominal_fname = oldname;
1395 else if (token != CPP_VSPACE && token != CPP_EOF)
1397 cpp_error (pfile, "token after `#line %d' is not a string", new_lineno);
1398 goto bad_line_directive;
1401 /* The Newline at the end of this line remains to be processed.
1402 To put the next line at the specified line number,
1403 we must store a line number now that is one less. */
1404 ip->lineno = new_lineno - 1;
1405 CPP_SET_WRITTEN (pfile, old_written);
1406 output_line_command (pfile, file_change);
1410 skip_rest_of_line (pfile);
1411 CPP_SET_WRITTEN (pfile, old_written);
1415 /* Remove the definition of a symbol from the symbol table.
1416 According to the C standard, it is not an error to undef
1417 something that has no definitions. */
1419 do_undef (pfile, keyword)
1421 const struct directive *keyword;
1425 U_CHAR *buf, *name, *limit;
1427 long here = CPP_WRITTEN (pfile);
1428 enum cpp_token token;
1430 cpp_skip_hspace (pfile);
1432 if (! is_idstart(c))
1434 cpp_error (pfile, "token after #undef is not an identifier");
1435 skip_rest_of_line (pfile);
1439 parse_name (pfile, c);
1440 buf = pfile->token_buffer + here;
1441 limit = CPP_PWRITTEN(pfile);
1443 /* Copy out the token so we can pop the token buffer. */
1445 name = (U_CHAR *) alloca (len + 1);
1446 memcpy (name, buf, len);
1449 token = get_directive_token (pfile);
1450 if (token != CPP_VSPACE)
1452 cpp_pedwarn (pfile, "junk on line after #undef");
1453 skip_rest_of_line (pfile);
1455 CPP_SET_WRITTEN (pfile, here);
1457 while ((hp = _cpp_lookup (pfile, name, len)) != NULL)
1459 /* If we are generating additional info for debugging (with -g) we
1460 need to pass through all effective #undef commands. */
1461 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1462 pass_thru_directive (name, len, pfile, keyword);
1463 if (hp->type == T_POISON)
1464 cpp_error (pfile, "cannot undefine poisoned `%s'", hp->name);
1467 if (hp->type != T_MACRO)
1468 cpp_warning (pfile, "undefining `%s'", hp->name);
1469 _cpp_delete_macro (hp);
1476 /* Wrap do_undef for -U processing. */
1478 cpp_undef (pfile, macro)
1482 /* Copy the string so we can append a newline. */
1483 size_t len = strlen (macro);
1484 U_CHAR *buf = alloca (len + 2);
1485 memcpy (buf, macro, len);
1487 buf[len + 1] = '\0';
1488 if (cpp_push_buffer (pfile, buf, len + 1))
1490 do_undef (pfile, NULL);
1491 cpp_pop_buffer (pfile);
1496 * Report an error detected by the program we are processing.
1497 * Use the text of the line in the error message.
1498 * (We use error because it prints the filename & line#.)
1502 do_error (pfile, keyword)
1504 const struct directive *keyword ATTRIBUTE_UNUSED;
1506 U_CHAR *text, *limit;
1508 cpp_skip_hspace (pfile);
1509 text = CPP_BUFFER (pfile)->cur;
1510 skip_rest_of_line (pfile);
1511 limit = CPP_BUFFER (pfile)->cur;
1513 cpp_error (pfile, "#error %.*s", (int)(limit - text), text);
1518 * Report a warning detected by the program we are processing.
1519 * Use the text of the line in the warning message, then continue.
1523 do_warning (pfile, keyword)
1525 const struct directive *keyword ATTRIBUTE_UNUSED;
1527 U_CHAR *text, *limit;
1529 cpp_skip_hspace (pfile);
1530 text = CPP_BUFFER (pfile)->cur;
1531 skip_rest_of_line (pfile);
1532 limit = CPP_BUFFER (pfile)->cur;
1534 if (CPP_PEDANTIC (pfile))
1535 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
1537 cpp_warning (pfile, "#warning %.*s", (int)(limit - text), text);
1541 /* Report program identification. */
1544 do_ident (pfile, keyword)
1546 const struct directive *keyword ATTRIBUTE_UNUSED;
1548 long old_written = CPP_WRITTEN (pfile);
1550 /* Allow #ident in system headers, since that's not user's fault. */
1551 if (CPP_PEDANTIC (pfile))
1552 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
1554 CPP_PUTS (pfile, "#ident ", 7);
1556 /* Next token should be a string constant. */
1557 if (get_directive_token (pfile) == CPP_STRING)
1558 /* And then a newline. */
1559 if (get_directive_token (pfile) == CPP_VSPACE)
1560 /* Good - ship it. */
1563 cpp_error (pfile, "invalid #ident");
1564 skip_rest_of_line (pfile);
1565 CPP_SET_WRITTEN (pfile, old_written); /* discard directive */
1570 /* Pragmata handling. We handle some of these, and pass the rest on
1571 to the front end. C99 defines three pragmas and says that no macro
1572 expansion is to be performed on them; whether or not macro
1573 expansion happens for other pragmas is implementation defined.
1574 This implementation never macro-expands the text after #pragma.
1576 We currently do not support the _Pragma operator. Support for that
1577 has to be coordinated with the front end. Proposed implementation:
1578 both #pragma blah blah and _Pragma("blah blah") become
1579 __builtin_pragma(blah blah) and we teach the parser about that. */
1581 /* Sub-handlers for the pragmas needing treatment here.
1582 They return 1 if the token buffer is to be popped, 0 if not. */
1583 static int do_pragma_once PARAMS ((cpp_reader *));
1584 static int do_pragma_implementation PARAMS ((cpp_reader *));
1585 static int do_pragma_poison PARAMS ((cpp_reader *));
1586 static int do_pragma_default PARAMS ((cpp_reader *));
1589 do_pragma (pfile, keyword)
1591 const struct directive *keyword ATTRIBUTE_UNUSED;
1596 enum cpp_token token;
1598 here = CPP_WRITTEN (pfile);
1599 CPP_PUTS (pfile, "#pragma ", 8);
1601 key = CPP_WRITTEN (pfile);
1602 pfile->no_macro_expand++;
1603 token = get_directive_token (pfile);
1604 if (token != CPP_NAME)
1606 if (token == CPP_VSPACE)
1612 buf = pfile->token_buffer + key;
1613 CPP_PUTC (pfile, ' ');
1615 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1617 pop = do_pragma_once (pfile);
1618 else if (tokis ("implementation"))
1619 pop = do_pragma_implementation (pfile);
1620 else if (tokis ("poison"))
1621 pop = do_pragma_poison (pfile);
1623 pop = do_pragma_default (pfile);
1626 if (get_directive_token (pfile) != CPP_VSPACE)
1630 CPP_SET_WRITTEN (pfile, here);
1631 pfile->no_macro_expand--;
1635 cpp_error (pfile, "malformed #pragma directive");
1636 skip_rest_of_line (pfile);
1638 CPP_SET_WRITTEN (pfile, here);
1639 pfile->no_macro_expand--;
1644 do_pragma_default (pfile)
1647 while (get_directive_token (pfile) != CPP_VSPACE)
1648 CPP_PUTC (pfile, ' ');
1653 do_pragma_once (pfile)
1656 cpp_buffer *ip = CPP_BUFFER (pfile);
1658 /* Allow #pragma once in system headers, since that's not the user's
1660 if (!ip->system_header_p)
1661 cpp_warning (pfile, "`#pragma once' is obsolete");
1663 if (CPP_PREV_BUFFER (ip) == NULL)
1664 cpp_warning (pfile, "`#pragma once' outside include file");
1666 ip->ihash->control_macro = ""; /* never repeat */
1672 do_pragma_implementation (pfile)
1675 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1676 been included yet. */
1677 enum cpp_token token;
1678 long written = CPP_WRITTEN (pfile);
1682 token = get_directive_token (pfile);
1683 if (token == CPP_VSPACE)
1685 else if (token != CPP_STRING)
1687 cpp_error (pfile, "malformed #pragma implementation");
1691 name = pfile->token_buffer + written + 1;
1692 copy = xstrdup (name);
1693 copy[strlen(copy)] = '\0'; /* trim trailing quote */
1695 if (cpp_included (pfile, copy))
1697 "`#pragma implementation' for `%s' appears after file is included",
1704 do_pragma_poison (pfile)
1707 /* Poison these symbols so that all subsequent usage produces an
1713 enum cpp_token token;
1715 /* As a rule, don't include #pragma poison commands in output,
1716 unless the user asks for them. */
1717 writeit = (CPP_OPTIONS (pfile)->debug_output
1718 || CPP_OPTIONS (pfile)->dump_macros == dump_definitions
1719 || CPP_OPTIONS (pfile)->dump_macros == dump_names);
1723 written = CPP_WRITTEN (pfile);
1724 token = get_directive_token (pfile);
1725 if (token == CPP_VSPACE)
1727 if (token != CPP_NAME)
1729 cpp_error (pfile, "invalid #pragma poison directive");
1730 skip_rest_of_line (pfile);
1734 p = pfile->token_buffer + written;
1736 if ((hp = _cpp_lookup (pfile, p, len)))
1738 if (hp->type != T_POISON)
1740 cpp_warning (pfile, "poisoning existing macro `%s'", p);
1741 if (hp->type == T_MACRO)
1742 _cpp_free_definition (hp->value.defn);
1744 hp->type = T_POISON;
1748 _cpp_install (pfile, p, len, T_POISON, 0);
1750 CPP_PUTC (pfile, ' ');
1755 #ifdef SCCS_DIRECTIVE
1756 /* Just ignore #sccs, on systems where we define it at all. */
1759 do_sccs (pfile, keyword)
1761 const struct directive *keyword ATTRIBUTE_UNUSED;
1763 if (CPP_PEDANTIC (pfile))
1764 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
1765 skip_rest_of_line (pfile);
1770 /* We've found an `#if' directive. If the only thing before it in
1771 this file is white space, and if it is of the form
1772 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1773 for inclusion of this file. (See redundant_include_p in cppfiles.c
1774 for an explanation of controlling macros.) If so, return a
1775 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1778 detect_if_not_defined (pfile)
1781 U_CHAR *control_macro = 0;
1783 if (pfile->only_seen_white == 2)
1786 enum cpp_token token;
1789 int need_rparen = 0;
1791 /* Save state required for restore. */
1792 pfile->no_macro_expand++;
1793 parse_set_mark (pfile);
1794 base_offset = CPP_WRITTEN (pfile);
1797 if (get_directive_token (pfile) != CPP_OTHER
1798 || CPP_WRITTEN (pfile) != (size_t) base_offset + 1
1799 || CPP_PWRITTEN (pfile)[-1] != '!')
1802 /* ...then `defined', */
1803 token_offset = CPP_WRITTEN (pfile);
1804 token = get_directive_token (pfile);
1805 if (token != CPP_NAME)
1807 ident = pfile->token_buffer + token_offset;
1808 CPP_NUL_TERMINATE (pfile);
1809 if (strcmp (ident, "defined"))
1812 /* ...then an optional '(' and the name, */
1813 token_offset = CPP_WRITTEN (pfile);
1814 token = get_directive_token (pfile);
1815 if (token == CPP_LPAREN)
1817 token_offset = CPP_WRITTEN (pfile);
1818 token = get_directive_token (pfile);
1819 if (token != CPP_NAME)
1823 else if (token != CPP_NAME)
1826 ident = pfile->token_buffer + token_offset;
1827 CPP_NUL_TERMINATE (pfile);
1829 /* ...then the ')', if necessary, */
1830 if ((!need_rparen || get_directive_token (pfile) == CPP_RPAREN)
1831 /* ...and make sure there's nothing else on the line. */
1832 && get_directive_token (pfile) == CPP_VSPACE)
1833 control_macro = xstrdup (ident);
1836 CPP_SET_WRITTEN (pfile, base_offset);
1837 pfile->no_macro_expand--;
1838 parse_goto_mark (pfile);
1841 return control_macro;
1845 * handle #if command by
1846 * 1) inserting special `defined' keyword into the hash table
1847 * that gets turned into 0 or 1 by special_symbol (thus,
1848 * if the luser has a symbol called `defined' already, it won't
1849 * work inside the #if command)
1850 * 2) rescan the input into a temporary output buffer
1851 * 3) pass the output buffer to the yacc parser and collect a value
1852 * 4) clean up the mess left from steps 1 and 2.
1853 * 5) call conditional_skip to skip til the next #endif (etc.),
1854 * or not, depending on the value from step 3.
1858 do_if (pfile, keyword)
1860 const struct directive *keyword ATTRIBUTE_UNUSED;
1862 U_CHAR *control_macro = detect_if_not_defined (pfile);
1863 HOST_WIDEST_INT value = eval_if_expr (pfile);
1864 conditional_skip (pfile, value == 0, T_IF, control_macro);
1869 * handle a #elif directive by not changing if_stack either.
1870 * see the comment above do_else.
1874 do_elif (pfile, keyword)
1876 const struct directive *keyword ATTRIBUTE_UNUSED;
1878 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
1880 cpp_error (pfile, "`#elif' not within a conditional");
1885 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
1887 cpp_error (pfile, "`#elif' after `#else'");
1888 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
1889 "the conditional began here");
1891 pfile->if_stack->type = T_ELIF;
1894 if (pfile->if_stack->if_succeeded)
1895 skip_if_group (pfile);
1898 HOST_WIDEST_INT value = eval_if_expr (pfile);
1900 skip_if_group (pfile);
1903 ++pfile->if_stack->if_succeeded; /* continue processing input */
1904 output_line_command (pfile, same_file);
1911 * evaluate a #if expression in BUF, of length LENGTH,
1912 * then parse the result as a C expression and return the value as an int.
1915 static HOST_WIDEST_INT
1916 eval_if_expr (pfile)
1919 HOST_WIDEST_INT value;
1920 long old_written = CPP_WRITTEN (pfile);
1922 pfile->parsing_if_directive++;
1923 value = _cpp_parse_expr (pfile);
1924 pfile->parsing_if_directive--;
1926 skip_rest_of_line (pfile);
1927 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1933 * routine to handle ifdef/ifndef. Try to look up the symbol,
1934 * then do or don't skip to the #endif/#else/#elif depending
1935 * on what directive is actually being processed.
1939 do_xifdef (pfile, keyword)
1941 const struct directive *keyword;
1946 enum cpp_token token;
1947 int start_of_file = 0;
1948 U_CHAR *control_macro = 0;
1949 int old_written = CPP_WRITTEN (pfile);
1951 /* Detect a #ifndef at start of file (not counting comments). */
1952 if (keyword->type == T_IFNDEF)
1953 start_of_file = pfile->only_seen_white == 2;
1955 pfile->no_macro_expand++;
1956 token = get_directive_token (pfile);
1957 pfile->no_macro_expand--;
1959 ident = pfile->token_buffer + old_written;
1960 ident_length = CPP_WRITTEN (pfile) - old_written;
1961 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
1963 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
1965 skip = (keyword->type == T_IFDEF);
1966 if (! CPP_TRADITIONAL (pfile))
1967 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
1969 else if (token == CPP_NAME)
1971 skip = cpp_defined (pfile, ident, ident_length);
1972 if (keyword->type == T_IFDEF)
1975 if (start_of_file && !skip)
1977 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
1978 bcopy (ident, control_macro, ident_length + 1);
1983 skip = (keyword->type == T_IFDEF);
1984 if (! CPP_TRADITIONAL (pfile))
1985 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
1988 if (!CPP_TRADITIONAL (pfile))
1990 cpp_skip_hspace (pfile);
1992 if (c != EOF && c != '\n')
1993 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
1995 skip_rest_of_line (pfile);
1997 conditional_skip (pfile, skip, T_IF, control_macro);
2001 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2002 If this is a #ifndef starting at the beginning of a file,
2003 CONTROL_MACRO is the macro name tested by the #ifndef.
2004 Otherwise, CONTROL_MACRO is 0. */
2007 conditional_skip (pfile, skip, type, control_macro)
2010 enum node_type type;
2011 U_CHAR *control_macro;
2015 temp = (IF_STACK *) xcalloc (1, sizeof (IF_STACK));
2016 temp->lineno = CPP_BUFFER (pfile)->lineno;
2017 temp->next = pfile->if_stack;
2018 temp->control_macro = control_macro;
2019 pfile->if_stack = temp;
2021 pfile->if_stack->type = type;
2024 skip_if_group (pfile);
2027 ++pfile->if_stack->if_succeeded;
2028 output_line_command (pfile, same_file);
2032 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2033 return 0 if skipping should continue, 1 if it should halt. Also
2034 adjusts the if_stack as appropriate.
2035 The `#' has been read, but not the identifier. */
2038 consider_directive_while_skipping (pfile, stack)
2042 long ident_len, ident;
2043 const struct directive *kt;
2046 cpp_skip_hspace (pfile);
2048 ident = CPP_WRITTEN (pfile);
2049 parse_name (pfile, GETC());
2050 ident_len = CPP_WRITTEN (pfile) - ident;
2052 CPP_SET_WRITTEN (pfile, ident);
2054 for (kt = directive_table; kt->length >= 0; kt++)
2055 if (kt->length == ident_len
2056 && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
2062 temp = (IF_STACK *) xmalloc (sizeof (IF_STACK));
2063 temp->next = pfile->if_stack;
2064 pfile->if_stack = temp;
2065 temp->type = kt->type;
2069 if (pfile->if_stack != stack)
2070 validate_else (pfile, "#else");
2073 if (pfile->if_stack == stack)
2077 pfile->if_stack->type = kt->type;
2082 if (pfile->if_stack != stack)
2083 validate_else (pfile, "#endif");
2085 if (pfile->if_stack == stack)
2088 temp = pfile->if_stack;
2089 pfile->if_stack = temp->next;
2097 /* Don't let erroneous code go by. */
2098 if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
2099 cpp_pedwarn (pfile, "invalid preprocessor directive name");
2103 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2104 * leaves input ptr at the sharp sign found.
2107 skip_if_group (pfile)
2111 IF_STACK *save_if_stack = pfile->if_stack; /* don't pop past here */
2112 U_CHAR *beg_of_line;
2115 old_written = CPP_WRITTEN (pfile);
2119 beg_of_line = CPP_BUFFER (pfile)->cur;
2121 if (! CPP_TRADITIONAL (pfile))
2122 cpp_skip_hspace (pfile);
2126 CPP_BUMP_LINE (pfile);
2131 if (consider_directive_while_skipping (pfile, save_if_stack))
2135 return; /* Caller will issue error. */
2138 skip_rest_of_line (pfile);
2142 return; /* Caller will issue error. */
2144 CPP_BUMP_LINE (pfile);
2147 /* Back up to the beginning of this line. Caller will process the
2149 CPP_BUFFER (pfile)->cur = beg_of_line;
2150 pfile->only_seen_white = 1;
2154 * handle a #else directive. Do this by just continuing processing
2155 * without changing if_stack ; this is so that the error message
2156 * for missing #endif's etc. will point to the original #if. It
2157 * is possible that something different would be better.
2161 do_else (pfile, keyword)
2163 const struct directive *keyword ATTRIBUTE_UNUSED;
2165 validate_else (pfile, "#else");
2166 skip_rest_of_line (pfile);
2168 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2170 cpp_error (pfile, "`#else' not within a conditional");
2175 /* #ifndef can't have its special treatment for containing the whole file
2176 if it has a #else clause. */
2177 pfile->if_stack->control_macro = 0;
2179 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF)
2181 cpp_error (pfile, "`#else' after `#else'");
2182 cpp_error_with_line (pfile, pfile->if_stack->lineno, -1,
2183 "the conditional began here");
2185 pfile->if_stack->type = T_ELSE;
2188 if (pfile->if_stack->if_succeeded)
2189 skip_if_group (pfile);
2192 ++pfile->if_stack->if_succeeded; /* continue processing input */
2193 output_line_command (pfile, same_file);
2199 * unstack after #endif command
2203 do_endif (pfile, keyword)
2205 const struct directive *keyword ATTRIBUTE_UNUSED;
2207 validate_else (pfile, "#endif");
2208 skip_rest_of_line (pfile);
2210 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
2211 cpp_error (pfile, "`#endif' not within a conditional");
2214 IF_STACK *temp = pfile->if_stack;
2215 pfile->if_stack = temp->next;
2216 if (temp->control_macro != 0)
2218 /* This #endif matched a #ifndef at the start of the file.
2219 See if it is at the end of the file. */
2222 parse_set_mark (pfile);
2226 cpp_skip_hspace (pfile);
2231 parse_goto_mark (pfile);
2235 /* This #endif ends a #ifndef
2236 that contains all of the file (aside from whitespace).
2237 Arrange not to include the file again
2238 if the macro that was tested is defined. */
2239 CPP_BUFFER (pfile)->ihash->control_macro = temp->control_macro;
2243 output_line_command (pfile, same_file);
2248 /* Issue -pedantic warning for text which is not a comment following
2249 an #else or #endif. Do not warn in system headers, as this is harmless
2250 and very common on old systems. */
2253 validate_else (pfile, directive)
2255 const char *directive;
2257 if (! CPP_PEDANTIC (pfile))
2260 cpp_skip_hspace (pfile);
2261 if (PEEKC () != '\n')
2263 "text following `%s' violates ANSI standard", directive);
2266 /* Convert T_IF, etc. to a string. Used in error messages. */
2268 if_directive_name (pfile, ifs)
2270 struct if_stack *ifs;
2274 case T_IF: return "#if";
2275 case T_IFDEF: return "#ifdef";
2276 case T_IFNDEF: return "#ifndef";
2277 case T_ELIF: return "#elif";
2278 case T_ELSE: return "#else";
2280 cpp_ice (pfile, "impossible if_stack->type value %d", ifs->type);
2285 /* Get the next token, and add it to the text in pfile->token_buffer.
2286 Return the kind of token we got. */
2289 cpp_get_token (pfile)
2292 register int c, c2, c3;
2293 enum cpp_token token;
2294 struct cpp_options *opts = CPP_OPTIONS (pfile);
2300 if (CPP_BUFFER (pfile)->manual_pop)
2301 /* If we've been reading from redirected input, the
2302 frontend will pop the buffer. */
2304 else if (CPP_BUFFER (pfile)->seen_eof)
2306 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) == NULL)
2309 cpp_pop_buffer (pfile);
2314 cpp_buffer *next_buf = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
2315 struct if_stack *ifs, *nifs;
2317 /* Unwind the conditional stack and generate error messages. */
2318 for (ifs = pfile->if_stack;
2319 ifs != CPP_BUFFER (pfile)->if_stack;
2322 cpp_error_with_line (pfile, ifs->lineno, -1,
2323 "unterminated `%s' conditional",
2324 if_directive_name (pfile, ifs));
2329 pfile->if_stack = ifs;
2331 if (CPP_BUFFER (pfile)->nominal_fname && next_buf != NULL)
2333 /* We're about to return from an #include file.
2334 Emit #line information now (as part of the CPP_POP) result.
2335 But the #line refers to the file we will pop to. */
2336 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
2337 CPP_BUFFER (pfile) = next_buf;
2338 pfile->input_stack_listing_current = 0;
2339 output_line_command (pfile, leave_file);
2340 CPP_BUFFER (pfile) = cur_buffer;
2343 CPP_BUFFER (pfile)->seen_eof = 1;
2352 if (PEEKC () == '=')
2356 if (opts->discard_comments)
2357 c = skip_comment (pfile, c);
2359 c = copy_comment (pfile, c);
2363 /* Comments are equivalent to spaces.
2364 For -traditional, a comment is equivalent to nothing. */
2365 if (opts->traditional || !opts->discard_comments)
2369 CPP_PUTC (pfile, c);
2374 if (pfile->parsing_if_directive)
2376 cpp_skip_hspace (pfile);
2377 parse_assertion (pfile);
2378 return CPP_ASSERTION;
2381 if (pfile->parsing_define_directive && ! CPP_TRADITIONAL (pfile))
2383 CPP_RESERVE (pfile, 3);
2384 CPP_PUTC_Q (pfile, '#');
2385 CPP_NUL_TERMINATE_Q (pfile);
2386 if (PEEKC () != '#')
2387 return CPP_STRINGIZE;
2390 CPP_PUTC_Q (pfile, '#');
2391 CPP_NUL_TERMINATE_Q (pfile);
2392 return CPP_TOKPASTE;
2395 if (!pfile->only_seen_white)
2397 /* -traditional directives are recognized only with the # in
2399 XXX Layering violation. */
2400 if (CPP_TRADITIONAL (pfile)
2401 && CPP_BUFFER (pfile)->cur - CPP_BUFFER (pfile)->line_base != 1)
2403 if (handle_directive (pfile))
2404 return CPP_DIRECTIVE;
2405 pfile->only_seen_white = 0;
2410 parse_string (pfile, c);
2411 pfile->only_seen_white = 0;
2412 return c == '\'' ? CPP_CHAR : CPP_STRING;
2415 if (!opts->dollars_in_ident)
2420 if (opts->cplusplus && PEEKC () == ':')
2428 if (c2 == c || c2 == '=')
2437 if (PEEKC () == '=')
2443 if (c2 == '-' && opts->chill)
2444 goto comment; /* Chill style comment */
2445 if (c2 == '-' || c2 == '=')
2449 if (opts->cplusplus && PEEKN (1) == '*')
2451 /* In C++, there's a ->* operator. */
2453 pfile->only_seen_white = 0;
2454 CPP_RESERVE (pfile, 4);
2455 CPP_PUTC_Q (pfile, c);
2456 CPP_PUTC_Q (pfile, GETC ());
2457 CPP_PUTC_Q (pfile, GETC ());
2458 CPP_NUL_TERMINATE_Q (pfile);
2466 if (pfile->parsing_include_directive)
2470 CPP_PUTC (pfile, c);
2474 if (c == '\n' || c == EOF)
2477 "missing '>' in `#include <FILENAME>'");
2482 if (!CPP_BUFFER (pfile)->has_escapes)
2484 /* Backslash newline is replaced by nothing. */
2485 CPP_ADJUST_WRITTEN (pfile, -1);
2486 CPP_BUMP_LINE (pfile);
2490 /* We might conceivably get \r- or \r<space> in
2491 here. Just delete 'em. */
2493 if (d != '-' && d != ' ')
2494 cpp_ice (pfile, "unrecognized escape \\r%c", d);
2495 CPP_ADJUST_WRITTEN (pfile, -1);
2501 /* else fall through */
2506 /* GNU C++ supports MIN and MAX operators <? and >?. */
2507 if (c2 != c && (!opts->cplusplus || c2 != '?'))
2510 CPP_RESERVE (pfile, 4);
2511 CPP_PUTC (pfile, c);
2512 CPP_PUTC (pfile, c2);
2515 CPP_PUTC_Q (pfile, GETC ());
2516 CPP_NUL_TERMINATE_Q (pfile);
2517 pfile->only_seen_white = 0;
2524 CPP_RESERVE(pfile, 2);
2525 CPP_PUTC_Q (pfile, '.');
2530 /* In C++ there's a .* operator. */
2531 if (opts->cplusplus && c2 == '*')
2534 if (c2 == '.' && PEEKN(1) == '.')
2536 CPP_RESERVE(pfile, 4);
2537 CPP_PUTC_Q (pfile, '.');
2538 CPP_PUTC_Q (pfile, '.');
2539 CPP_PUTC_Q (pfile, '.');
2541 CPP_NUL_TERMINATE_Q (pfile);
2542 pfile->only_seen_white = 0;
2549 pfile->only_seen_white = 0;
2550 CPP_RESERVE(pfile, 3);
2551 CPP_PUTC_Q (pfile, c);
2552 CPP_PUTC_Q (pfile, GETC ());
2553 CPP_NUL_TERMINATE_Q (pfile);
2558 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
2560 CPP_PUTC (pfile, c);
2562 parse_string (pfile, c);
2563 pfile->only_seen_white = 0;
2564 return c == '\'' ? CPP_WCHAR : CPP_WSTRING;
2568 case '0': case '1': case '2': case '3': case '4':
2569 case '5': case '6': case '7': case '8': case '9':
2574 CPP_RESERVE (pfile, 2);
2575 CPP_PUTC_Q (pfile, c);
2579 if (!is_numchar(c) && c != '.'
2580 && ((c2 != 'e' && c2 != 'E'
2581 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
2582 || (c != '+' && c != '-')))
2587 CPP_NUL_TERMINATE_Q (pfile);
2588 pfile->only_seen_white = 0;
2590 case 'b': case 'c': case 'd': case 'h': case 'o':
2591 case 'B': case 'C': case 'D': case 'H': case 'O':
2592 if (opts->chill && PEEKC () == '\'')
2594 pfile->only_seen_white = 0;
2595 CPP_RESERVE (pfile, 2);
2596 CPP_PUTC_Q (pfile, c);
2597 CPP_PUTC_Q (pfile, '\'');
2603 goto chill_number_eof;
2606 CPP_PUTC (pfile, c);
2610 CPP_RESERVE (pfile, 2);
2611 CPP_PUTC_Q (pfile, c);
2612 CPP_NUL_TERMINATE_Q (pfile);
2619 CPP_NUL_TERMINATE (pfile);
2626 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2627 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2628 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2629 case 'x': case 'y': case 'z':
2630 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2631 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2632 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2637 unsigned char *ident;
2638 int before_name_written = CPP_WRITTEN (pfile);
2640 parse_name (pfile, c);
2641 pfile->only_seen_white = 0;
2642 if (pfile->no_macro_expand)
2644 ident = pfile->token_buffer + before_name_written;
2645 ident_len = CPP_PWRITTEN (pfile) - ident;
2646 hp = _cpp_lookup (pfile, ident, ident_len);
2649 if (hp->type == T_DISABLED)
2651 if (pfile->output_escapes)
2652 { /* Return "\r-IDENT", followed by '\0'. */
2654 CPP_RESERVE (pfile, 3);
2655 ident = pfile->token_buffer + before_name_written;
2656 CPP_ADJUST_WRITTEN (pfile, 2);
2657 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
2664 /* If macro wants an arglist, verify that a '(' follows. */
2665 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
2667 int macbuf_whitespace = 0;
2669 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2671 U_CHAR *point = CPP_BUFFER (pfile)->cur;
2674 cpp_skip_hspace (pfile);
2681 if (point != CPP_BUFFER (pfile)->cur)
2682 macbuf_whitespace = 1;
2686 goto not_macro_call;
2687 cpp_pop_buffer (pfile);
2690 parse_set_mark (pfile);
2693 cpp_skip_hspace (pfile);
2700 parse_goto_mark (pfile);
2706 if (macbuf_whitespace)
2707 CPP_PUTC (pfile, ' ');
2711 /* This is now known to be a macro call.
2712 Expand the macro, reading arguments as needed,
2713 and push the expansion on the input stack. */
2714 _cpp_macroexpand (pfile, hp);
2715 CPP_SET_WRITTEN (pfile, before_name_written);
2719 case ' ': case '\t': case '\v':
2722 CPP_PUTC (pfile, c);
2724 if (c == EOF || !is_hspace(c))
2731 if (CPP_BUFFER (pfile)->has_escapes)
2736 if (pfile->output_escapes)
2737 CPP_PUTS (pfile, "\r-", 2);
2738 parse_name (pfile, GETC ());
2743 CPP_RESERVE (pfile, 2);
2744 if (pfile->output_escapes)
2745 CPP_PUTC_Q (pfile, '\r');
2746 CPP_PUTC_Q (pfile, c);
2751 cpp_ice (pfile, "unrecognized escape \\r%c", c);
2757 /* Backslash newline is ignored. */
2758 CPP_BUMP_LINE (pfile);
2763 CPP_PUTC (pfile, c);
2764 if (pfile->only_seen_white == 0)
2765 pfile->only_seen_white = 1;
2766 CPP_BUMP_LINE (pfile);
2767 if (! CPP_OPTIONS (pfile)->no_line_commands)
2770 if (CPP_BUFFER (pfile)->lineno != pfile->lineno)
2771 output_line_command (pfile, same_file);
2775 case '(': token = CPP_LPAREN; goto char1;
2776 case ')': token = CPP_RPAREN; goto char1;
2777 case '{': token = CPP_LBRACE; goto char1;
2778 case '}': token = CPP_RBRACE; goto char1;
2779 case ',': token = CPP_COMMA; goto char1;
2780 case ';': token = CPP_SEMICOLON; goto char1;
2786 pfile->only_seen_white = 0;
2787 CPP_PUTC (pfile, c);
2793 /* Like cpp_get_token, but skip spaces and comments. */
2796 cpp_get_non_space_token (pfile)
2799 int old_written = CPP_WRITTEN (pfile);
2802 enum cpp_token token = cpp_get_token (pfile);
2803 if (token != CPP_COMMENT && token != CPP_POP
2804 && token != CPP_HSPACE && token != CPP_VSPACE)
2806 CPP_SET_WRITTEN (pfile, old_written);
2810 /* Parse an identifier starting with C. */
2813 parse_name (pfile, c)
2825 if (c == '$' && CPP_PEDANTIC (pfile))
2826 cpp_pedwarn (pfile, "`$' in identifier");
2828 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
2829 CPP_PUTC_Q (pfile, c);
2834 CPP_NUL_TERMINATE_Q (pfile);
2838 /* Parse and skip over a string starting with C. A single quoted
2839 string is treated like a double -- some programs (e.g., troff) are
2840 perverse this way. (However, a single quoted string is not allowed
2841 to extend over multiple lines.) */
2843 skip_string (pfile, c)
2847 long start_line, start_column;
2848 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2856 cpp_error_with_line (pfile, start_line, start_column,
2857 "unterminated string or character constant");
2858 if (pfile->multiline_string_line != start_line
2859 && pfile->multiline_string_line != 0)
2860 cpp_error_with_line (pfile,
2861 pfile->multiline_string_line, -1,
2862 "possible real start of unterminated constant");
2863 pfile->multiline_string_line = 0;
2867 CPP_BUMP_LINE (pfile);
2868 /* In Fortran and assembly language, silently terminate
2869 strings of either variety at end of line. This is a
2870 kludge around not knowing where comments are in these
2872 if (CPP_OPTIONS (pfile)->lang_fortran
2873 || CPP_OPTIONS (pfile)->lang_asm)
2878 /* Character constants may not extend over multiple lines.
2879 In Standard C, neither may strings. We accept multiline
2880 strings as an extension. */
2883 cpp_error_with_line (pfile, start_line, start_column,
2884 "unterminated character constant");
2888 if (CPP_PEDANTIC (pfile) && pfile->multiline_string_line == 0)
2889 cpp_pedwarn_with_line (pfile, start_line, start_column,
2890 "string constant runs past end of line");
2891 if (pfile->multiline_string_line == 0)
2892 pfile->multiline_string_line = start_line;
2896 if (CPP_BUFFER (pfile)->has_escapes)
2898 cpp_ice (pfile, "\\r escape inside string constant");
2902 /* Backslash newline is replaced by nothing at all. */
2903 CPP_BUMP_LINE (pfile);
2919 /* Parse a string and copy it to the output. */
2922 parse_string (pfile, c)
2926 U_CHAR *start = CPP_BUFFER (pfile)->cur; /* XXX Layering violation */
2929 skip_string (pfile, c);
2931 limit = CPP_BUFFER (pfile)->cur;
2932 CPP_RESERVE (pfile, limit - start + 2);
2933 CPP_PUTC_Q (pfile, c);
2934 for (; start < limit; start++)
2936 CPP_PUTC_Q (pfile, *start);
2939 /* Read an assertion into the token buffer, converting to
2940 canonical form: `#predicate(a n swe r)' The next non-whitespace
2941 character to read should be the first letter of the predicate.
2942 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2943 with answer (see callers for why). In case of 0, an error has been
2946 parse_assertion (pfile)
2950 cpp_skip_hspace (pfile);
2952 if (! is_idstart(c))
2954 cpp_error (pfile, "assertion predicate is not an identifier");
2957 CPP_PUTC(pfile, '#');
2959 parse_name(pfile, c);
2964 if (is_hspace(c) || c == '\r')
2965 cpp_skip_hspace (pfile);
2971 CPP_PUTC(pfile, '(');
2974 while ((c = GETC()) != ')')
2980 CPP_PUTC(pfile, ' ');
2984 else if (c == '\n' || c == EOF)
2986 if (c == '\n') FORWARD(-1);
2987 cpp_error (pfile, "un-terminated assertion answer");
2991 /* \r cannot be a macro escape here. */
2992 CPP_BUMP_LINE (pfile);
2995 CPP_PUTC (pfile, c);
3000 if (pfile->limit[-1] == ' ')
3001 pfile->limit[-1] = ')';
3002 else if (pfile->limit[-1] == '(')
3004 cpp_error (pfile, "empty token sequence in assertion");
3008 CPP_PUTC (pfile, ')');
3010 CPP_NUL_TERMINATE (pfile);
3015 do_assert (pfile, keyword)
3017 const struct directive *keyword ATTRIBUTE_UNUSED;
3021 HASHNODE *base, *this;
3022 int baselen, thislen;
3024 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3025 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
3027 cpp_skip_hspace (pfile);
3028 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
3029 ret = parse_assertion (pfile);
3034 cpp_error (pfile, "missing token-sequence in `#assert'");
3038 cpp_skip_hspace (pfile);
3040 if (c != EOF && c != '\n')
3042 cpp_error (pfile, "junk at end of `#assert'");
3046 thislen = strlen (sym);
3047 baselen = (U_CHAR *) index (sym, '(') - sym;
3048 this = _cpp_lookup (pfile, sym, thislen);
3051 cpp_warning (pfile, "`%s' re-asserted", sym);
3055 base = _cpp_lookup (pfile, sym, baselen);
3057 base = _cpp_install (pfile, sym, baselen, T_ASSERT, 0);
3058 else if (base->type != T_ASSERT)
3060 /* Token clash - but with what?! */
3061 cpp_ice (pfile, "base->type != T_ASSERT in do_assert");
3065 this = _cpp_install (pfile, sym, thislen, T_ASSERT,
3066 (char *)base->value.aschain);
3067 base->value.aschain = this;
3069 pfile->limit = sym; /* Pop */
3073 skip_rest_of_line (pfile);
3074 pfile->limit = sym; /* Pop */
3079 do_unassert (pfile, keyword)
3081 const struct directive *keyword ATTRIBUTE_UNUSED;
3085 long baselen, thislen;
3086 HASHNODE *base, *this, *next;
3088 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing)
3089 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
3091 cpp_skip_hspace (pfile);
3093 sym = CPP_PWRITTEN (pfile); /* remember where it starts */
3094 ret = parse_assertion (pfile);
3098 cpp_skip_hspace (pfile);
3100 if (c != EOF && c != '\n')
3101 cpp_error (pfile, "junk at end of `#unassert'");
3103 thislen = strlen (sym);
3106 base = _cpp_lookup (pfile, sym, thislen);
3108 goto error; /* It isn't an error to #undef what isn't #defined,
3109 so it isn't an error to #unassert what isn't
3110 #asserted either. */
3112 for (this = base->value.aschain; this; this = next)
3114 next = this->value.aschain;
3115 _cpp_delete_macro (this);
3117 _cpp_delete_macro (base);
3121 baselen = (U_CHAR *) index (sym, '(') - sym;
3122 base = _cpp_lookup (pfile, sym, baselen);
3123 if (! base) goto error;
3124 this = _cpp_lookup (pfile, sym, thislen);
3125 if (! this) goto error;
3128 while (next->value.aschain != this)
3129 next = next->value.aschain;
3131 next->value.aschain = this->value.aschain;
3132 _cpp_delete_macro (this);
3134 if (base->value.aschain == NULL)
3135 _cpp_delete_macro (base); /* Last answer for this predicate deleted. */
3138 pfile->limit = sym; /* Pop */
3141 skip_rest_of_line (pfile);
3142 pfile->limit = sym; /* Pop */
3146 /* Process STR as if it appeared as the body of an #unassert. */
3148 cpp_unassert (pfile, str)
3152 if (cpp_push_buffer (pfile, str, strlen (str)) != NULL)
3154 do_unassert (pfile, NULL);
3155 cpp_pop_buffer (pfile);
3159 /* Remember the current position of PFILE so it may be returned to
3160 after looking ahead a bit.
3162 Note that when you set a mark, you _must_ return to that mark. You
3163 may not forget about it and continue parsing. You may not pop a
3164 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3168 parse_set_mark (pfile)
3171 cpp_buffer *ip = CPP_BUFFER (pfile);
3172 if (ACTIVE_MARK_P())
3173 cpp_ice (pfile, "mark active in parse_set_mark");
3175 ip->mark = ip->cur - ip->buf;
3178 /* Backup the current position of PFILE to that saved in its mark,
3179 and clear the mark. */
3182 parse_goto_mark (pfile)
3185 cpp_buffer *ip = CPP_BUFFER (pfile);
3186 if (!ACTIVE_MARK_P())
3187 cpp_ice (pfile, "mark not active in parse_goto_mark");
3189 ip->cur = ip->buf + ip->mark;