2 Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
29 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
31 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
32 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
33 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
34 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
35 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
36 (Note that it is false while we're expanding macro *arguments*.) */
37 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
39 /* Forward declarations. */
41 static const char *my_strerror
PROTO ((int));
42 static void validate_else
PROTO ((cpp_reader
*, const char *));
43 static HOST_WIDEST_INT eval_if_expression
PROTO ((cpp_reader
*));
45 static void conditional_skip
PROTO ((cpp_reader
*, int,
46 enum node_type
, U_CHAR
*));
47 static void skip_if_group
PROTO ((cpp_reader
*));
49 static void parse_name
PARAMS ((cpp_reader
*, int));
50 static void parse_string
PARAMS ((cpp_reader
*, int));
51 static int parse_assertion
PARAMS ((cpp_reader
*));
53 /* External declarations. */
55 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
57 /* `struct directive' defines one #-directive, including how to handle it. */
60 int length
; /* Length of name */
61 int (*func
) /* Function to handle directive */
62 PARAMS ((cpp_reader
*, const struct directive
*));
63 const char *name
; /* Name of directive */
64 enum node_type type
; /* Code which describes which directive. */
67 /* These functions are declared to return int instead of void since they
68 are going to be placed in a table and some old compilers have trouble with
69 pointers to functions returning void. */
71 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
72 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
73 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
74 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
75 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
76 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
77 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
78 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
79 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
80 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
81 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
82 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
84 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
86 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
87 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
88 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
89 static enum cpp_token null_underflow
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 copy_rest_of_line
PARAMS ((cpp_reader
*));
94 static int handle_directive
PARAMS ((cpp_reader
*));
95 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t, cpp_reader
*,
96 const struct directive
*));
97 static enum cpp_token get_directive_token
PARAMS ((cpp_reader
*));
98 static int read_line_number
PARAMS ((cpp_reader
*, int *));
99 static void cpp_print_file_and_line
PARAMS ((cpp_reader
*));
100 static void v_cpp_error
PARAMS ((cpp_reader
*, const char *, va_list));
101 static void v_cpp_warning
PARAMS ((cpp_reader
*, const char *, va_list));
102 static void v_cpp_error_with_line
PARAMS ((cpp_reader
*, int, int,
103 const char *, va_list));
104 static void v_cpp_warning_with_line
PARAMS ((cpp_reader
*, int, int, const char *, va_list));
105 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
106 static int consider_directive_while_skipping
PARAMS ((cpp_reader
*, IF_STACK_FRAME
*));
108 /* Here is the actual list of #-directives.
109 This table is ordered by frequency of occurrence; the numbers
110 at the end are directive counts from all the source code I have
111 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
112 linux-2.2.9, and pcmcia-cs-3.0.9). */
114 static const struct directive directive_table
[] = {
116 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
117 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
118 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
119 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
120 { 2, do_if
, "if", T_IF
}, /* 18162 */
121 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
122 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
123 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
124 { 4, do_line
, "line", T_LINE
}, /* 2465 */
125 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
126 { 5, do_error
, "error", T_ERROR
}, /* 475 */
127 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
129 /* Extensions. All deprecated except #warning and #include_next. */
130 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
131 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
132 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
133 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
134 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
135 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
136 #ifdef SCCS_DIRECTIVE
137 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
139 { -1, 0, "", T_UNUSED
}
142 /* Place into PFILE a quoted string representing the string SRC.
143 Caller must reserve enough space in pfile->token_buffer. */
146 quote_string (pfile
, src
)
152 CPP_PUTC_Q (pfile
, '\"');
154 switch ((c
= *src
++))
158 CPP_PUTC_Q (pfile
, c
);
161 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
162 CPP_ADJUST_WRITTEN (pfile
, 4);
168 CPP_PUTC_Q (pfile
, '\\');
169 CPP_PUTC_Q (pfile
, c
);
173 CPP_PUTC_Q (pfile
, '\"');
174 CPP_NUL_TERMINATE_Q (pfile
);
179 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
182 cpp_grow_buffer (pfile
, n
)
186 long old_written
= CPP_WRITTEN (pfile
);
187 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
188 pfile
->token_buffer
= (U_CHAR
*)
189 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
190 CPP_SET_WRITTEN (pfile
, old_written
);
193 /* Process the string STR as if it appeared as the body of a #define
194 If STR is just an identifier, define it with value 1.
195 If STR has anything after the identifier, then it should
196 be identifier=definition. */
199 cpp_define (pfile
, str
)
206 /* Copy the entire option so we can modify it. */
207 count
= strlen (str
) + 3;
208 buf
= (U_CHAR
*) alloca (count
);
209 memcpy (buf
, str
, count
- 2);
210 /* Change the first "=" in the string to a space. If there is none,
211 tack " 1" on the end. */
212 p
= (U_CHAR
*) strchr (buf
, '=');
219 strcpy (&buf
[count
-3], " 1");
221 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
223 do_define (pfile
, NULL
);
224 cpp_pop_buffer (pfile
);
228 /* Process the string STR as if it appeared as the body of a #assert. */
230 cpp_assert (pfile
, str
)
234 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
236 do_assert (pfile
, NULL
);
237 cpp_pop_buffer (pfile
);
242 static enum cpp_token
243 null_underflow (pfile
)
244 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
250 null_cleanup (pbuf
, pfile
)
251 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
252 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
257 /* Skip a comment - C, C++, or Chill style. M is the first character
258 of the comment marker. If this really is a comment, skip to its
259 end and return ' '. If we hit end-of-file before end-of-comment,
260 return EOF. If this is not a comment, return M (which will be
264 skip_comment (pfile
, m
)
268 if (m
== '/' && PEEKC() == '*')
274 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
280 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
283 else if (c
== '\n' || c
== '\r')
284 /* \r cannot be a macro escape marker here. */
285 CPP_BUMP_LINE (pfile
);
286 else if (c
== '/' && prev_c
== '*')
288 else if (c
== '*' && prev_c
== '/'
289 && CPP_OPTIONS (pfile
)->warn_comments
)
290 cpp_warning (pfile
, "`/*' within comment");
295 else if ((m
== '/' && PEEKC() == '/'
296 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
297 || (m
== '-' && PEEKC() == '-'
298 && CPP_OPTIONS (pfile
)->chill
))
305 return ' '; /* Allow // to be terminated by EOF. */
308 /* Don't consider final '\n' to be part of comment. */
313 /* \r cannot be a macro escape marker here. */
314 CPP_BUMP_LINE (pfile
);
321 /* Identical to skip_comment except that it copies the comment into the
322 token_buffer. This is used if put_out_comments. */
324 copy_comment (pfile
, m
)
328 if (m
== '/' && PEEKC() == '*')
333 CPP_PUTC (pfile
, '/');
334 CPP_PUTC (pfile
, '*');
336 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
342 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
343 /* We must pretend this was a legitimate comment, so that the
344 output in token_buffer is not passed back tagged CPP_POP. */
349 /* \r cannot be a macro escape marker here. */
350 CPP_BUMP_LINE (pfile
);
358 CPP_BUMP_LINE (pfile
);
360 else if (c
== '/' && prev_c
== '*')
362 else if (c
== '*' && prev_c
== '/'
363 && CPP_OPTIONS (pfile
)->warn_comments
)
364 cpp_warning (pfile
, "`/*' within comment");
369 else if ((m
== '/' && PEEKC() == '/'
370 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
371 || (m
== '-' && PEEKC() == '-'
372 && CPP_OPTIONS (pfile
)->chill
))
381 return ' '; /* Allow line comments to be terminated by EOF. */
384 /* Don't consider final '\n' to be part of comment. */
389 /* \r cannot be a macro escape marker here. */
390 CPP_BUMP_LINE (pfile
);
400 /* Skip whitespace \-newline and comments. Does not macro-expand. */
403 cpp_skip_hspace (pfile
)
412 else if (is_hor_space
[c
])
414 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
415 cpp_pedwarn (pfile
, "%s in preprocessing directive",
416 c
== '\f' ? "formfeed" : "vertical tab");
420 /* \r is a backslash-newline marker if !has_escapes, and
421 a deletable-whitespace or no-reexpansion marker otherwise. */
422 if (CPP_BUFFER (pfile
)->has_escapes
)
430 CPP_BUFFER (pfile
)->lineno
++;
432 else if (c
== '/' || c
== '-')
434 c
= skip_comment (pfile
, c
);
446 /* Read the rest of the current line.
447 The line is appended to PFILE's output buffer. */
450 copy_rest_of_line (pfile
)
461 CPP_NUL_TERMINATE (pfile
);
465 if (CPP_BUFFER (pfile
)->has_escapes
)
469 CPP_BUFFER (pfile
)->lineno
++;
474 parse_string (pfile
, c
);
477 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile
))
479 CPP_PUTS (pfile
, "/**/", 4);
480 skip_comment (pfile
, c
);
483 /* else fall through */
485 c
= skip_comment (pfile
, c
);
490 if (CPP_PEDANTIC (pfile
))
491 cpp_pedwarn (pfile
, "%s in preprocessing directive",
492 c
== '\f' ? "formfeed" : "vertical tab");
500 /* FIXME: It is almost definitely a performance win to make this do
501 the scan itself. >75% of calls to copy_r_o_l are from here or
502 skip_if_group, which means the common case is to copy stuff into the
503 token_buffer only to discard it. */
505 skip_rest_of_line (pfile
)
508 long old
= CPP_WRITTEN (pfile
);
509 copy_rest_of_line (pfile
);
510 CPP_SET_WRITTEN (pfile
, old
);
513 /* Handle a possible # directive.
514 '#' has already been read. */
517 handle_directive (pfile
)
521 register const struct directive
*kt
;
524 long old_written
= CPP_WRITTEN (pfile
);
526 cpp_skip_hspace (pfile
);
529 if (c
>= '0' && c
<= '9')
531 /* Handle # followed by a line number. Complain about using that
532 form if we're being pedantic, but not if this is regurgitated
533 input (preprocessed or fed back in by the C++ frontend). */
534 if (CPP_PEDANTIC (pfile
)
535 && ! CPP_PREPROCESSED (pfile
)
536 && ! CPP_BUFFER (pfile
)->manual_pop
)
537 cpp_pedwarn (pfile
, "`#' followed by integer");
538 do_line (pfile
, NULL
);
542 /* Now find the directive name. */
543 CPP_PUTC (pfile
, '#');
544 parse_name (pfile
, GETC());
545 ident
= pfile
->token_buffer
+ old_written
+ 1;
546 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
547 if (ident_length
== 0)
549 /* A line of just `#' becomes blank. */
557 * Decode the keyword and call the appropriate expansion
558 * routine, after moving the input pointer up to the next line.
560 for (kt
= directive_table
; ; kt
++)
564 if (kt
->length
== ident_length
565 && !strncmp (kt
->name
, ident
, ident_length
))
569 CPP_SET_WRITTEN (pfile
, old_written
);
570 (*kt
->func
) (pfile
, kt
);
575 /* Pass a directive through to the output file.
576 BUF points to the contents of the directive, as a contiguous string.
577 LEN is the length of the string pointed to by BUF.
578 KEYWORD is the keyword-table entry for the directive. */
581 pass_thru_directive (buf
, len
, pfile
, keyword
)
585 const struct directive
*keyword
;
587 register unsigned keyword_length
= keyword
->length
;
589 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
590 CPP_PUTC_Q (pfile
, '#');
591 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
592 if (len
!= 0 && buf
[0] != ' ')
593 CPP_PUTC_Q (pfile
, ' ');
594 CPP_PUTS_Q (pfile
, buf
, len
);
597 /* Check a purported macro name SYMNAME, and yield its length.
598 ASSERTION is nonzero if this is really for an assertion name. */
601 check_macro_name (pfile
, symname
, assertion
)
603 const U_CHAR
*symname
;
609 for (p
= symname
; is_idchar
[*p
]; p
++)
611 sym_length
= p
- symname
;
613 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
615 assertion
? "invalid assertion name" : "invalid macro name");
616 else if (!is_idstart
[*symname
]
617 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
618 U_CHAR
*msg
; /* what pain... */
619 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
620 bcopy (symname
, msg
, sym_length
);
624 ? "invalid assertion name `%s'"
625 : "invalid macro name `%s'"),
631 /* Process a #define command.
632 KEYWORD is the keyword-table entry for #define,
633 or NULL for a "predefined" macro,
634 or the keyword-table entry for #pragma in the case of a #pragma poison. */
637 do_define (pfile
, keyword
)
639 const struct directive
*keyword
;
645 U_CHAR
*macro
, *buf
, *end
;
646 enum node_type new_type
;
648 here
= CPP_WRITTEN (pfile
);
649 copy_rest_of_line (pfile
);
651 if (keyword
== NULL
|| keyword
->type
== T_DEFINE
)
656 /* Copy out the line so we can pop the token buffer. */
657 buf
= pfile
->token_buffer
+ here
;
658 end
= CPP_PWRITTEN (pfile
);
659 macro
= (U_CHAR
*) alloca (end
- buf
+ 1);
660 bcopy (buf
, macro
, end
- buf
+ 1);
661 end
= macro
+ (end
- buf
);
663 CPP_SET_WRITTEN (pfile
, here
);
665 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
669 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
671 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
674 /* Redefining a precompiled key is ok. */
675 if (hp
->type
== T_PCSTRING
)
677 /* Redefining a poisoned identifier is even worse than `not ok'. */
678 else if (hp
->type
== T_POISON
)
680 /* Redefining a macro is ok if the definitions are the same. */
681 else if (hp
->type
== T_MACRO
)
682 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
683 /* Redefining a constant is ok with -D. */
684 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
685 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
686 /* Print the warning or error if it's not ok. */
689 if (hp
->type
== T_POISON
)
690 cpp_error (pfile
, "redefining poisoned `%.*s'",
691 mdef
.symlen
, mdef
.symnam
);
693 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
694 if (hp
->type
== T_MACRO
)
695 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
696 hp
->value
.defn
->line
,
697 "this is the location of the previous definition");
699 if (hp
->type
!= T_POISON
)
701 /* Replace the old definition. */
703 hp
->value
.defn
= mdef
.defn
;
707 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, new_type
,
708 (char *) mdef
.defn
, hashcode
);
710 if (keyword
!= NULL
&& keyword
->type
== T_DEFINE
)
712 if (CPP_OPTIONS (pfile
)->debug_output
713 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
714 dump_definition (pfile
, mdef
);
715 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
716 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
723 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
724 If BUFFER != NULL, then use the LENGTH characters in BUFFER
725 as the new input buffer.
726 Return the new buffer, or NULL on failure. */
729 cpp_push_buffer (pfile
, buffer
, length
)
734 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
736 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
738 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
742 new = (cpp_buffer
*) xcalloc (1, sizeof (cpp_buffer
));
744 new->if_stack
= pfile
->if_stack
;
745 new->cleanup
= null_cleanup
;
746 new->underflow
= null_underflow
;
747 new->buf
= new->cur
= buffer
;
748 new->alimit
= new->rlimit
= buffer
+ length
;
752 CPP_BUFFER (pfile
) = new;
757 cpp_pop_buffer (pfile
)
760 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
761 (*buf
->cleanup
) (buf
, pfile
);
762 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
764 pfile
->buffer_stack_depth
--;
765 return CPP_BUFFER (pfile
);
768 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
769 Pop the buffer when done. */
772 cpp_scan_buffer (pfile
)
775 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
776 enum cpp_token token
;
777 if (CPP_OPTIONS (pfile
)->no_output
)
779 long old_written
= CPP_WRITTEN (pfile
);
780 /* In no-output mode, we can ignore everything but directives. */
783 if (! pfile
->only_seen_white
)
784 skip_rest_of_line (pfile
);
785 token
= cpp_get_token (pfile
);
786 if (token
== CPP_EOF
) /* Should not happen ... */
788 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
790 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
791 != CPP_NULL_BUFFER (pfile
))
792 cpp_pop_buffer (pfile
);
796 CPP_SET_WRITTEN (pfile
, old_written
);
802 token
= cpp_get_token (pfile
);
803 if (token
== CPP_EOF
) /* Should not happen ... */
805 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
807 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
808 != CPP_NULL_BUFFER (pfile
))
809 cpp_pop_buffer (pfile
);
817 * Rescan a string (which may have escape marks) into pfile's buffer.
818 * Place the result in pfile->token_buffer.
820 * The input is copied before it is scanned, so it is safe to pass
821 * it something from the token_buffer that will get overwritten
822 * (because it follows CPP_WRITTEN). This is used by do_include.
826 cpp_expand_to_buffer (pfile
, buf
, length
)
831 register cpp_buffer
*ip
;
837 cpp_fatal (pfile
, "internal error: length < 0 in cpp_expand_to_buffer");
841 /* Set up the input on the input stack. */
843 buf1
= (U_CHAR
*) alloca (length
+ 1);
844 memcpy (buf1
, buf
, length
);
847 ip
= cpp_push_buffer (pfile
, buf1
, length
);
852 /* Scan the input, create the output. */
853 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
854 CPP_OPTIONS (pfile
)->no_output
= 0;
855 cpp_scan_buffer (pfile
);
856 CPP_OPTIONS (pfile
)->no_output
= save_no_output
;
858 CPP_NUL_TERMINATE (pfile
);
862 cpp_buf_line_and_col (pbuf
, linep
, colp
)
863 register cpp_buffer
*pbuf
;
868 *linep
= pbuf
->lineno
;
870 *colp
= pbuf
->cur
- pbuf
->line_base
;
880 /* Return the cpp_buffer that corresponds to a file (not a macro). */
883 cpp_file_buffer (pfile
)
886 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
888 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
889 if (ip
->fname
!= NULL
)
895 * write out a #line command, for instance, after an #include file.
896 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
900 output_line_command (pfile
, file_change
)
902 enum file_change_code file_change
;
905 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
907 if (ip
->fname
== NULL
)
910 if (CPP_OPTIONS (pfile
)->no_line_commands
911 || CPP_OPTIONS (pfile
)->no_output
)
914 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
916 /* If the current file has not changed, we omit the #line if it would
917 appear to be a no-op, and we output a few newlines instead
918 if we want to increase the line number by a small amount.
919 We cannot do this if pfile->lineno is zero, because that means we
920 haven't output any line commands yet. (The very first line command
921 output is a `same_file' command.) */
922 if (file_change
== same_file
&& pfile
->lineno
!= 0)
924 if (line
== pfile
->lineno
)
927 /* If the inherited line number is a little too small,
928 output some newlines instead of a #line command. */
929 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
931 CPP_RESERVE (pfile
, 20);
932 while (line
> pfile
->lineno
)
934 CPP_PUTC_Q (pfile
, '\n');
941 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
942 CPP_PUTS_Q (pfile
, "# ", 2);
944 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
945 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
947 quote_string (pfile
, ip
->nominal_fname
);
948 if (file_change
!= same_file
)
950 CPP_PUTC_Q (pfile
, ' ');
951 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
953 /* Tell cc1 if following text comes from a system header file. */
954 if (ip
->system_header_p
)
956 CPP_PUTC_Q (pfile
, ' ');
957 CPP_PUTC_Q (pfile
, '3');
959 #ifndef NO_IMPLICIT_EXTERN_C
960 /* Tell cc1plus if following text should be treated as C. */
961 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
963 CPP_PUTC_Q (pfile
, ' ');
964 CPP_PUTC_Q (pfile
, '4');
967 CPP_PUTC_Q (pfile
, '\n');
968 pfile
->lineno
= line
;
972 /* Like cpp_get_token, except that it does not read past end-of-line.
973 Also, horizontal space is skipped, and macros are popped. */
975 static enum cpp_token
976 get_directive_token (pfile
)
981 long old_written
= CPP_WRITTEN (pfile
);
982 enum cpp_token token
;
983 cpp_skip_hspace (pfile
);
984 if (PEEKC () == '\n')
986 token
= cpp_get_token (pfile
);
990 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
992 /* ... else fall though ... */
993 case CPP_HSPACE
: case CPP_COMMENT
:
994 CPP_SET_WRITTEN (pfile
, old_written
);
1002 /* Handle #include and #import.
1003 This function expects to see "fname" or <fname> on the input.
1005 The input is normally in part of the output_buffer following
1006 CPP_WRITTEN, and will get overwritten by output_line_command.
1007 I.e. in input file specification has been popped by handle_directive.
1011 do_include (pfile
, keyword
)
1013 const struct directive
*keyword
;
1015 int importing
= (keyword
->type
== T_IMPORT
);
1016 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1017 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1018 int before
; /* included before? */
1020 unsigned char *ftok
;
1023 enum cpp_token token
;
1025 /* Chain of dirs to search */
1026 struct include_hash
*ihash
;
1027 struct file_name_list
*search_start
;
1029 long old_written
= CPP_WRITTEN (pfile
);
1033 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1036 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1038 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1041 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1042 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1043 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1045 pfile
->import_warning
= 1;
1047 "#import is obsolete, use an #ifndef wrapper in the header file");
1050 pfile
->parsing_include_directive
++;
1051 token
= get_directive_token (pfile
);
1052 pfile
->parsing_include_directive
--;
1054 if (token
== CPP_STRING
)
1056 if (pfile
->token_buffer
[old_written
] == '<')
1060 else if (token
== CPP_NAME
)
1062 /* Support '#include xyz' like VAX-C. It is taken as
1063 '#include <xyz.h>' and generates a warning. */
1065 "`#include filename' is obsolete, use `#include <filename.h>'");
1068 /* Append the missing `.h' to the name. */
1069 CPP_PUTS (pfile
, ".h", 2);
1075 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1076 CPP_SET_WRITTEN (pfile
, old_written
);
1077 skip_rest_of_line (pfile
);
1081 flen
= CPP_WRITTEN (pfile
) - old_written
;
1082 ftok
= (unsigned char *) alloca (flen
+ 1);
1083 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1086 if (get_directive_token (pfile
) != CPP_VSPACE
)
1088 cpp_error (pfile
, "junk at end of `#include'");
1089 skip_rest_of_line (pfile
);
1092 CPP_SET_WRITTEN (pfile
, old_written
);
1096 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1100 if (CPP_OPTIONS (pfile
)->dump_includes
)
1101 pass_thru_directive (ftok
,
1104 - ((token
== CPP_NAME
) ? 2 : 0)
1109 if (token
== CPP_STRING
)
1119 for (fp
= CPP_BUFFER (pfile
);
1120 fp
!= CPP_NULL_BUFFER (pfile
);
1121 fp
= CPP_PREV_BUFFER (fp
))
1122 if (fp
->fname
!= NULL
)
1125 if (fp
== CPP_NULL_BUFFER (pfile
))
1127 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
1131 /* For #include_next, skip in the search path past the dir in which the
1132 containing file was found. Treat files specified using an absolute path
1133 as if there are no more directories to search. Treat the primary source
1134 file like any other included source, but generate a warning. */
1135 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1137 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1138 search_start
= fp
->ihash
->foundhere
->next
;
1143 cpp_warning (pfile
, "#include_next in primary source file");
1146 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1149 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1152 search_start
= fp
->actual_dir
;
1155 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1161 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1165 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1172 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1173 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1174 (pfile
->system_include_depth
> 0)))
1176 if (!angle_brackets
)
1177 deps_output (pfile
, ftok
, ' ');
1181 struct file_name_list
*ptr
;
1182 /* If requested as a system header, assume it belongs in
1183 the first system header directory. */
1184 if (CPP_OPTIONS (pfile
)->bracket_include
)
1185 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1187 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1189 p
= (char *) alloca (strlen (ptr
->name
)
1190 + strlen (ftok
) + 2);
1191 if (*ptr
->name
!= '\0')
1193 strcpy (p
, ptr
->name
);
1197 deps_output (pfile
, p
, ' ');
1200 /* If -M was specified, and this header file won't be added to
1201 the dependency list, then don't count this as an error,
1202 because we can still produce correct output. Otherwise, we
1203 can't produce correct output, because there may be
1204 dependencies we need inside the missing file, and we don't
1205 know what directory this missing file exists in. */
1206 else if (CPP_PRINT_DEPS (pfile
)
1207 && (CPP_PRINT_DEPS (pfile
)
1208 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1209 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1211 cpp_error_from_errno (pfile
, ftok
);
1216 /* For -M, add the file to the dependencies on its first inclusion. */
1217 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1218 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1219 deps_output (pfile
, ihash
->name
, ' ');
1221 /* Handle -H option. */
1222 if (CPP_OPTIONS(pfile
)->print_include_names
)
1224 fp
= CPP_BUFFER (pfile
);
1225 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1227 fprintf (stderr
, " %s\n", ihash
->name
);
1230 /* Actually process the file */
1233 ihash
->control_macro
= "";
1235 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1242 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1244 if (finclude (pfile
, fd
, ihash
))
1246 output_line_command (pfile
, enter_file
);
1247 pfile
->only_seen_white
= 2;
1253 /* Subroutine of do_line. Read next token from PFILE without adding it to
1254 the output buffer. If it is a number between 1 and 4, store it in *NUM
1255 and return 1; otherwise, return 0 and complain if we aren't at the end
1256 of the directive. */
1259 read_line_number (pfile
, num
)
1263 long save_written
= CPP_WRITTEN (pfile
);
1264 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1265 enum cpp_token token
= get_directive_token (pfile
);
1266 CPP_SET_WRITTEN (pfile
, save_written
);
1268 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1275 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1276 cpp_error (pfile
, "invalid format `#line' command");
1281 /* Interpret #line command.
1282 Note that the filename string (if any) is treated as if it were an
1283 include filename. That means no escape handling. */
1286 do_line (pfile
, keyword
)
1288 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1290 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1292 long old_written
= CPP_WRITTEN (pfile
);
1293 enum file_change_code file_change
= same_file
;
1294 enum cpp_token token
;
1297 token
= get_directive_token (pfile
);
1299 if (token
!= CPP_NUMBER
)
1301 cpp_error (pfile
, "token after `#line' is not an integer");
1302 goto bad_line_directive
;
1305 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1308 cpp_error (pfile
, "token after `#line' is not an integer");
1309 goto bad_line_directive
;
1311 CPP_SET_WRITTEN (pfile
, old_written
);
1313 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1314 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1316 token
= get_directive_token (pfile
);
1318 if (token
== CPP_STRING
)
1320 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1321 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1322 int action_number
= 0;
1324 if (read_line_number (pfile
, &action_number
))
1326 if (CPP_PEDANTIC (pfile
))
1327 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1329 if (action_number
== 1)
1331 file_change
= enter_file
;
1332 read_line_number (pfile
, &action_number
);
1334 else if (action_number
== 2)
1336 file_change
= leave_file
;
1337 read_line_number (pfile
, &action_number
);
1339 if (action_number
== 3)
1341 ip
->system_header_p
= 1;
1342 read_line_number (pfile
, &action_number
);
1344 if (action_number
== 4)
1346 ip
->system_header_p
= 2;
1347 read_line_number (pfile
, &action_number
);
1353 if (strcmp (fname
, ip
->nominal_fname
))
1355 char *newname
, *oldname
;
1356 if (!strcmp (fname
, ip
->fname
))
1357 newname
= ip
->fname
;
1358 else if (ip
->last_nominal_fname
1359 && !strcmp (fname
, ip
->last_nominal_fname
))
1360 newname
= ip
->last_nominal_fname
;
1362 newname
= xstrdup (fname
);
1364 oldname
= ip
->nominal_fname
;
1365 ip
->nominal_fname
= newname
;
1367 if (ip
->last_nominal_fname
1368 && ip
->last_nominal_fname
!= oldname
1369 && ip
->last_nominal_fname
!= newname
1370 && ip
->last_nominal_fname
!= ip
->fname
)
1371 free (ip
->last_nominal_fname
);
1373 if (newname
== ip
->fname
)
1374 ip
->last_nominal_fname
= NULL
;
1376 ip
->last_nominal_fname
= oldname
;
1379 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1381 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1382 goto bad_line_directive
;
1385 /* The Newline at the end of this line remains to be processed.
1386 To put the next line at the specified line number,
1387 we must store a line number now that is one less. */
1388 ip
->lineno
= new_lineno
- 1;
1389 CPP_SET_WRITTEN (pfile
, old_written
);
1390 output_line_command (pfile
, file_change
);
1394 skip_rest_of_line (pfile
);
1395 CPP_SET_WRITTEN (pfile
, old_written
);
1399 /* Remove the definition of a symbol from the symbol table.
1400 According to the C standard, it is not an error to undef
1401 something that has no definitions. */
1403 do_undef (pfile
, keyword
)
1405 const struct directive
*keyword
;
1409 U_CHAR
*buf
, *name
, *limit
;
1411 long here
= CPP_WRITTEN (pfile
);
1412 enum cpp_token token
;
1414 cpp_skip_hspace (pfile
);
1416 if (! is_idstart
[c
])
1418 cpp_error (pfile
, "token after #undef is not an identifier");
1419 skip_rest_of_line (pfile
);
1423 parse_name (pfile
, c
);
1424 buf
= pfile
->token_buffer
+ here
;
1425 limit
= CPP_PWRITTEN(pfile
);
1427 /* Copy out the token so we can pop the token buffer. */
1428 name
= (U_CHAR
*) alloca (limit
- buf
+ 1);
1429 bcopy(buf
, name
, limit
- buf
);
1430 name
[limit
- buf
] = '\0';
1432 token
= get_directive_token (pfile
);
1433 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1435 cpp_pedwarn (pfile
, "junk on line after #undef");
1436 skip_rest_of_line (pfile
);
1439 CPP_SET_WRITTEN (pfile
, here
);
1441 sym_length
= check_macro_name (pfile
, buf
, 0);
1443 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1445 /* If we are generating additional info for debugging (with -g) we
1446 need to pass through all effective #undef commands. */
1447 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1448 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1449 if (hp
->type
== T_POISON
)
1450 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
1453 if (hp
->type
!= T_MACRO
)
1454 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1462 /* Wrap do_undef for -U processing. */
1464 cpp_undef (pfile
, macro
)
1468 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1470 do_undef (pfile
, NULL
);
1471 cpp_pop_buffer (pfile
);
1477 * Report an error detected by the program we are processing.
1478 * Use the text of the line in the error message.
1479 * (We use error because it prints the filename & line#.)
1483 do_error (pfile
, keyword
)
1485 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1487 long here
= CPP_WRITTEN (pfile
);
1489 copy_rest_of_line (pfile
);
1490 text
= pfile
->token_buffer
+ here
;
1491 SKIP_WHITE_SPACE(text
);
1493 cpp_error (pfile
, "#error %s", text
);
1494 CPP_SET_WRITTEN (pfile
, here
);
1499 * Report a warning detected by the program we are processing.
1500 * Use the text of the line in the warning message, then continue.
1504 do_warning (pfile
, keyword
)
1506 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1509 long here
= CPP_WRITTEN(pfile
);
1510 copy_rest_of_line (pfile
);
1511 text
= pfile
->token_buffer
+ here
;
1512 SKIP_WHITE_SPACE(text
);
1514 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1515 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1517 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1518 if -pedantic-errors is given, #warning should cause an error. */
1519 cpp_pedwarn (pfile
, "#warning %s", text
);
1520 CPP_SET_WRITTEN (pfile
, here
);
1524 /* Report program identification.
1525 This is not precisely what cccp does with #ident, however I believe
1526 it matches `closely enough' (behavior is identical as long as there
1527 are no macros on the #ident line, which is pathological in my opinion). */
1530 do_ident (pfile
, keyword
)
1532 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1534 /* Allow #ident in system headers, since that's not user's fault. */
1535 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1536 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1538 CPP_PUTS (pfile
, "#ident ", 7);
1539 cpp_skip_hspace (pfile
);
1540 copy_rest_of_line (pfile
);
1545 /* Just check for some recognized pragmas that need validation here,
1546 and leave the text in the token buffer to be output. */
1549 do_pragma (pfile
, keyword
)
1551 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1556 CPP_PUTS (pfile
, "#pragma ", 8);
1557 cpp_skip_hspace (pfile
);
1559 here
= CPP_WRITTEN (pfile
);
1560 copy_rest_of_line (pfile
);
1561 buf
= pfile
->token_buffer
+ here
;
1563 if (!strncmp (buf
, "once", 4))
1565 cpp_buffer
*ip
= NULL
;
1567 /* Allow #pragma once in system headers, since that's not the user's
1569 if (!CPP_BUFFER (pfile
)->system_header_p
)
1570 cpp_warning (pfile
, "`#pragma once' is obsolete");
1572 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1574 if (ip
== CPP_NULL_BUFFER (pfile
))
1576 if (ip
->fname
!= NULL
)
1580 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1581 cpp_warning (pfile
, "`#pragma once' outside include file");
1583 ip
->ihash
->control_macro
= ""; /* never repeat */
1585 else if (!strncmp (buf
, "implementation", 14))
1587 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1588 been included yet. */
1589 struct include_hash
*ptr
;
1590 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1591 SKIP_WHITE_SPACE (p
);
1592 if (*p
== '\n' || *p
!= '\"')
1596 p
= (U_CHAR
*) index (fname
, '\"');
1598 fcopy
= (U_CHAR
*) alloca (p
- fname
+ 1);
1599 bcopy (fname
, fcopy
, p
- fname
);
1600 fcopy
[p
-fname
] = '\0';
1602 ptr
= include_hash (pfile
, fcopy
, 0);
1605 "`#pragma implementation' for `%s' appears after file is included",
1608 else if (!strncmp (buf
, "poison", 6))
1610 /* Poison these symbols so that all subsequent usage produces an
1612 U_CHAR
*p
= buf
+ 6;
1617 SKIP_WHITE_SPACE (p
);
1618 plen
= strlen(p
) + 1;
1620 syms
= (U_CHAR
*) alloca (plen
);
1621 memcpy (syms
, p
, plen
);
1623 /* As a rule, don't include #pragma poison commands in output,
1624 unless the user asks for them. */
1625 writeit
= (CPP_OPTIONS (pfile
)->debug_output
1626 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
1627 || CPP_OPTIONS (pfile
)->dump_macros
== dump_names
);
1630 CPP_SET_WRITTEN (pfile
, here
);
1632 CPP_SET_WRITTEN (pfile
, here
-8);
1636 CPP_RESERVE (pfile
, plen
+ 7);
1637 CPP_PUTS_Q (pfile
, "poison", 7);
1640 while (*syms
!= '\0')
1644 while (is_idchar
[*end
])
1647 if (!is_hor_space
[*end
] && *end
!= '\0')
1649 cpp_error (pfile
, "invalid #pragma poison directive");
1653 if (cpp_push_buffer (pfile
, syms
, end
- syms
) != NULL
)
1655 do_define (pfile
, keyword
);
1656 cpp_pop_buffer (pfile
);
1660 CPP_PUTC_Q (pfile
, ' ');
1661 CPP_PUTS_Q (pfile
, syms
, end
- syms
);
1664 SKIP_WHITE_SPACE (syms
);
1671 #ifdef SCCS_DIRECTIVE
1672 /* Just ignore #sccs, on systems where we define it at all. */
1675 do_sccs (pfile
, keyword
)
1677 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1679 if (CPP_PEDANTIC (pfile
))
1680 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1681 skip_rest_of_line (pfile
);
1687 /* We've found an `#if' directive. If the only thing before it in
1688 this file is white space, and if it is of the form
1689 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1690 for inclusion of this file. (See redundant_include_p in cppfiles.c
1691 for an explanation of controlling macros.) If so, return a
1692 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1695 detect_if_not_defined (pfile
)
1698 U_CHAR
*control_macro
= 0;
1700 if (pfile
->only_seen_white
== 2)
1703 enum cpp_token token
;
1706 int need_rparen
= 0;
1708 /* Save state required for restore. */
1709 pfile
->no_macro_expand
++;
1710 parse_set_mark (pfile
);
1711 base_offset
= CPP_WRITTEN (pfile
);
1714 if (get_directive_token (pfile
) != CPP_OTHER
1715 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1716 || CPP_PWRITTEN (pfile
)[-1] != '!')
1719 /* ...then `defined', */
1720 token_offset
= CPP_WRITTEN (pfile
);
1721 token
= get_directive_token (pfile
);
1722 if (token
!= CPP_NAME
)
1724 ident
= pfile
->token_buffer
+ token_offset
;
1725 CPP_NUL_TERMINATE (pfile
);
1726 if (strcmp (ident
, "defined"))
1729 /* ...then an optional '(' and the name, */
1730 token_offset
= CPP_WRITTEN (pfile
);
1731 token
= get_directive_token (pfile
);
1732 if (token
== CPP_LPAREN
)
1734 token_offset
= CPP_WRITTEN (pfile
);
1735 token
= get_directive_token (pfile
);
1736 if (token
!= CPP_NAME
)
1740 else if (token
!= CPP_NAME
)
1743 ident
= pfile
->token_buffer
+ token_offset
;
1744 CPP_NUL_TERMINATE (pfile
);
1746 /* ...then the ')', if necessary, */
1747 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1748 /* ...and make sure there's nothing else on the line. */
1749 && get_directive_token (pfile
) == CPP_VSPACE
)
1750 control_macro
= xstrdup (ident
);
1753 CPP_SET_WRITTEN (pfile
, base_offset
);
1754 pfile
->no_macro_expand
--;
1755 parse_goto_mark (pfile
);
1758 return control_macro
;
1762 * handle #if command by
1763 * 1) inserting special `defined' keyword into the hash table
1764 * that gets turned into 0 or 1 by special_symbol (thus,
1765 * if the luser has a symbol called `defined' already, it won't
1766 * work inside the #if command)
1767 * 2) rescan the input into a temporary output buffer
1768 * 3) pass the output buffer to the yacc parser and collect a value
1769 * 4) clean up the mess left from steps 1 and 2.
1770 * 5) call conditional_skip to skip til the next #endif (etc.),
1771 * or not, depending on the value from step 3.
1775 do_if (pfile
, keyword
)
1777 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1779 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1780 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1781 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1786 * handle a #elif directive by not changing if_stack either.
1787 * see the comment above do_else.
1791 do_elif (pfile
, keyword
)
1793 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1795 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1796 cpp_error (pfile
, "`#elif' not within a conditional");
1799 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1800 cpp_error (pfile
, "`#elif' after `#else'");
1802 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1804 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
1805 && strcmp (pfile
->if_stack
->fname
,
1806 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
1807 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1808 fprintf (stderr
, ")\n");
1810 pfile
->if_stack
->type
= T_ELIF
;
1813 if (pfile
->if_stack
->if_succeeded
)
1814 skip_if_group (pfile
);
1816 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1818 skip_if_group (pfile
);
1820 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1821 output_line_command (pfile
, same_file
);
1828 * evaluate a #if expression in BUF, of length LENGTH,
1829 * then parse the result as a C expression and return the value as an int.
1832 static HOST_WIDEST_INT
1833 eval_if_expression (pfile
)
1836 HOST_WIDEST_INT value
;
1837 long old_written
= CPP_WRITTEN (pfile
);
1839 pfile
->pcp_inside_if
= 1;
1840 value
= cpp_parse_expr (pfile
);
1841 pfile
->pcp_inside_if
= 0;
1843 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1849 * routine to handle ifdef/ifndef. Try to look up the symbol,
1850 * then do or don't skip to the #endif/#else/#elif depending
1851 * on what directive is actually being processed.
1855 do_xifdef (pfile
, keyword
)
1857 const struct directive
*keyword
;
1860 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1863 enum cpp_token token
;
1864 int start_of_file
= 0;
1865 U_CHAR
*control_macro
= 0;
1866 int old_written
= CPP_WRITTEN (pfile
);
1868 /* Detect a #ifndef at start of file (not counting comments). */
1869 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1870 start_of_file
= pfile
->only_seen_white
== 2;
1872 pfile
->no_macro_expand
++;
1873 token
= get_directive_token (pfile
);
1874 pfile
->no_macro_expand
--;
1876 ident
= pfile
->token_buffer
+ old_written
;
1877 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1878 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1880 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1882 skip
= (keyword
->type
== T_IFDEF
);
1883 if (! CPP_TRADITIONAL (pfile
))
1884 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1886 else if (token
== CPP_NAME
)
1888 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1889 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1890 if (start_of_file
&& !skip
)
1892 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1893 bcopy (ident
, control_macro
, ident_length
+ 1);
1895 if (hp
!= NULL
&& hp
->type
== T_POISON
)
1897 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1903 skip
= (keyword
->type
== T_IFDEF
);
1904 if (! CPP_TRADITIONAL (pfile
))
1905 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1908 if (!CPP_TRADITIONAL (pfile
))
1910 cpp_skip_hspace (pfile
);
1912 if (c
!= EOF
&& c
!= '\n')
1913 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1915 skip_rest_of_line (pfile
);
1919 /* Output a precondition for this macro. */
1920 if (hp
&& hp
->value
.defn
->predefined
)
1921 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
1924 fprintf (pcp_outfile
, "#undef ");
1925 while (is_idchar
[*cp
]) /* Ick! */
1926 fputc (*cp
++, pcp_outfile
);
1927 putc ('\n', pcp_outfile
);
1931 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1935 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1936 If this is a #ifndef starting at the beginning of a file,
1937 CONTROL_MACRO is the macro name tested by the #ifndef.
1938 Otherwise, CONTROL_MACRO is 0. */
1941 conditional_skip (pfile
, skip
, type
, control_macro
)
1944 enum node_type type
;
1945 U_CHAR
*control_macro
;
1947 IF_STACK_FRAME
*temp
;
1949 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1950 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1951 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1952 temp
->next
= pfile
->if_stack
;
1953 temp
->control_macro
= control_macro
;
1954 pfile
->if_stack
= temp
;
1956 pfile
->if_stack
->type
= type
;
1959 skip_if_group (pfile
);
1962 ++pfile
->if_stack
->if_succeeded
;
1963 output_line_command (pfile
, same_file
);
1967 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1968 return 0 if skipping should continue, 1 if it should halt. Also
1969 adjusts the if_stack as appropriate.
1970 The `#' has been read, but not the identifier. */
1973 consider_directive_while_skipping (pfile
, stack
)
1975 IF_STACK_FRAME
*stack
;
1977 long ident_len
, ident
;
1978 const struct directive
*kt
;
1979 IF_STACK_FRAME
*temp
;
1981 cpp_skip_hspace (pfile
);
1983 ident
= CPP_WRITTEN (pfile
);
1984 parse_name (pfile
, GETC());
1985 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1987 CPP_SET_WRITTEN (pfile
, ident
);
1989 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1990 if (kt
->length
== ident_len
1991 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
1997 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
1998 temp
->next
= pfile
->if_stack
;
1999 pfile
->if_stack
= temp
;
2000 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
2001 temp
->type
= kt
->type
;
2005 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
2006 validate_else (pfile
, "#else");
2009 if (pfile
->if_stack
->type
== T_ELSE
)
2010 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
2012 if (pfile
->if_stack
== stack
)
2016 pfile
->if_stack
->type
= kt
->type
;
2021 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
2022 validate_else (pfile
, "#endif");
2024 if (pfile
->if_stack
== stack
)
2027 temp
= pfile
->if_stack
;
2028 pfile
->if_stack
= temp
->next
;
2036 /* Don't let erroneous code go by. */
2037 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
2038 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
2042 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2043 * leaves input ptr at the sharp sign found.
2046 skip_if_group (pfile
)
2050 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
2051 U_CHAR
*beg_of_line
;
2054 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2056 CPP_PUTS (pfile
, "#failed\n", 8);
2058 output_line_command (pfile
, same_file
);
2061 old_written
= CPP_WRITTEN (pfile
);
2065 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
2067 if (! CPP_TRADITIONAL (pfile
))
2068 cpp_skip_hspace (pfile
);
2072 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2073 CPP_PUTC (pfile
, c
);
2074 CPP_BUMP_LINE (pfile
);
2079 if (consider_directive_while_skipping (pfile
, save_if_stack
))
2083 return; /* Caller will issue error. */
2086 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2088 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
2089 copy_rest_of_line (pfile
);
2093 copy_rest_of_line (pfile
);
2094 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
2099 return; /* Caller will issue error. */
2103 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2105 CPP_PUTC (pfile
, c
);
2108 CPP_BUMP_LINE (pfile
);
2112 /* Back up to the beginning of this line. Caller will process the
2114 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
2115 pfile
->only_seen_white
= 1;
2116 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2118 CPP_PUTS (pfile
, "#endfailed\n", 11);
2124 * handle a #else directive. Do this by just continuing processing
2125 * without changing if_stack ; this is so that the error message
2126 * for missing #endif's etc. will point to the original #if. It
2127 * is possible that something different would be better.
2131 do_else (pfile
, keyword
)
2133 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2135 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2137 if (CPP_PEDANTIC (pfile
))
2138 validate_else (pfile
, "#else");
2139 skip_rest_of_line (pfile
);
2141 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
2142 cpp_error (pfile
, "`#else' not within a conditional");
2145 /* #ifndef can't have its special treatment for containing the whole file
2146 if it has a #else clause. */
2147 pfile
->if_stack
->control_macro
= 0;
2149 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
2150 cpp_error (pfile
, "`#else' after `#else'");
2151 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
2152 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
2153 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
2154 fprintf (stderr
, ")\n");
2156 pfile
->if_stack
->type
= T_ELSE
;
2159 if (pfile
->if_stack
->if_succeeded
)
2160 skip_if_group (pfile
);
2162 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
2163 output_line_command (pfile
, same_file
);
2169 * unstack after #endif command
2173 do_endif (pfile
, keyword
)
2175 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2177 if (CPP_PEDANTIC (pfile
))
2178 validate_else (pfile
, "#endif");
2179 skip_rest_of_line (pfile
);
2181 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2182 cpp_error (pfile
, "`#endif' not within a conditional");
2185 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2186 pfile
->if_stack
= temp
->next
;
2187 if (temp
->control_macro
!= 0)
2189 /* This #endif matched a #ifndef at the start of the file.
2190 See if it is at the end of the file. */
2193 parse_set_mark (pfile
);
2197 cpp_skip_hspace (pfile
);
2202 parse_goto_mark (pfile
);
2206 /* This #endif ends a #ifndef
2207 that contains all of the file (aside from whitespace).
2208 Arrange not to include the file again
2209 if the macro that was tested is defined. */
2210 struct cpp_buffer
*ip
;
2211 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2212 if (ip
->fname
!= NULL
)
2214 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2218 output_line_command (pfile
, same_file
);
2223 /* When an #else or #endif is found while skipping failed conditional,
2224 if -pedantic was specified, this is called to warn about text after
2225 the command name. P points to the first char after the command name. */
2228 validate_else (pfile
, directive
)
2230 const char *directive
;
2233 cpp_skip_hspace (pfile
);
2235 if (c
!= EOF
&& c
!= '\n')
2237 "text following `%s' violates ANSI standard", directive
);
2240 /* Convert T_IF, etc. to a string. Used in error messages. */
2242 if_directive_name (pfile
, ifs
)
2244 struct if_stack
*ifs
;
2248 case T_IF
: return "#if";
2249 case T_IFDEF
: return "#ifdef";
2250 case T_IFNDEF
: return "#ifndef";
2251 case T_ELIF
: return "#elif";
2252 case T_ELSE
: return "#else";
2254 cpp_fatal (pfile
, "impossible if_stack->type value %d", ifs
->type
);
2259 /* Get the next token, and add it to the text in pfile->token_buffer.
2260 Return the kind of token we got. */
2263 cpp_get_token (pfile
)
2266 register int c
, c2
, c3
;
2267 enum cpp_token token
;
2268 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2275 if (CPP_BUFFER (pfile
)->manual_pop
)
2276 /* If we've been reading from redirected input, the
2277 frontend will pop the buffer. */
2279 else if (CPP_BUFFER (pfile
)->seen_eof
)
2281 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2284 cpp_pop_buffer (pfile
);
2289 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2290 struct if_stack
*ifs
, *nifs
;
2292 /* Unwind the conditional stack and generate error messages. */
2293 for (ifs
= pfile
->if_stack
;
2294 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
2297 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
2298 "unterminated `%s' conditional",
2299 if_directive_name (pfile
, ifs
));
2304 pfile
->if_stack
= ifs
;
2306 if (CPP_BUFFER (pfile
)->nominal_fname
2307 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2309 /* We're about to return from an #include file.
2310 Emit #line information now (as part of the CPP_POP) result.
2311 But the #line refers to the file we will pop to. */
2312 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2313 CPP_BUFFER (pfile
) = next_buf
;
2314 pfile
->input_stack_listing_current
= 0;
2315 output_line_command (pfile
, leave_file
);
2316 CPP_BUFFER (pfile
) = cur_buffer
;
2319 CPP_BUFFER (pfile
)->seen_eof
= 1;
2328 if (PEEKC () == '=')
2332 if (opts
->put_out_comments
)
2333 c
= copy_comment (pfile
, c
);
2335 c
= skip_comment (pfile
, c
);
2341 /* Comments are equivalent to spaces.
2342 For -traditional, a comment is equivalent to nothing. */
2343 if (opts
->traditional
|| opts
->put_out_comments
)
2347 CPP_PUTC (pfile
, c
);
2351 if (opts
->for_lint
) {
2354 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2356 if (lintcmd
!= NULL
) {
2357 /* I believe it is always safe to emit this newline: */
2359 bcopy ("#pragma lint ", (char *) obp
, 13);
2361 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2366 bcopy (argbp
, (char *) obp
, arglen
);
2370 /* OK, now bring us back to the state we were in before we entered
2371 this branch. We need #line because the newline for the pragma
2372 could mess things up. */
2373 output_line_command (pfile
, same_file
);
2374 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2382 /* If this is expanding a macro definition, don't recognize
2383 preprocessor directives. */
2386 /* If this is expand_into_temp_buffer, recognize them
2387 only after an actual newline at this level,
2388 not at the beginning of the input level. */
2389 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
2395 if (!pfile
->only_seen_white
)
2397 if (handle_directive (pfile
))
2398 return CPP_DIRECTIVE
;
2399 pfile
->only_seen_white
= 0;
2405 parse_string (pfile
, c
);
2406 pfile
->only_seen_white
= 0;
2407 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2410 if (!opts
->dollars_in_ident
)
2415 if (opts
->cplusplus
&& PEEKC () == ':')
2423 if (c2
== c
|| c2
== '=')
2432 if (PEEKC () == '=')
2438 if (c2
== '-' && opts
->chill
)
2439 goto comment
; /* Chill style comment */
2440 if (c2
== '-' || c2
== '=')
2444 if (opts
->cplusplus
&& PEEKN (1) == '*')
2446 /* In C++, there's a ->* operator. */
2448 pfile
->only_seen_white
= 0;
2449 CPP_RESERVE (pfile
, 4);
2450 CPP_PUTC_Q (pfile
, c
);
2451 CPP_PUTC_Q (pfile
, GETC ());
2452 CPP_PUTC_Q (pfile
, GETC ());
2453 CPP_NUL_TERMINATE_Q (pfile
);
2461 if (pfile
->parsing_include_directive
)
2465 CPP_PUTC (pfile
, c
);
2469 if (c
== '\n' || c
== EOF
)
2472 "missing '>' in `#include <FILENAME>'");
2477 if (!CPP_BUFFER (pfile
)->has_escapes
)
2479 /* Backslash newline is replaced by nothing. */
2480 CPP_ADJUST_WRITTEN (pfile
, -1);
2481 CPP_BUMP_LINE (pfile
);
2485 /* We might conceivably get \r- or \r<space> in
2486 here. Just delete 'em. */
2488 if (d
!= '-' && d
!= ' ')
2490 "internal error: unrecognized escape \\r%c",
2492 CPP_ADJUST_WRITTEN (pfile
, -1);
2498 /* else fall through */
2503 /* GNU C++ supports MIN and MAX operators <? and >?. */
2504 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2507 CPP_RESERVE (pfile
, 4);
2508 CPP_PUTC (pfile
, c
);
2509 CPP_PUTC (pfile
, c2
);
2512 CPP_PUTC_Q (pfile
, GETC ());
2513 CPP_NUL_TERMINATE_Q (pfile
);
2514 pfile
->only_seen_white
= 0;
2521 CPP_RESERVE(pfile
, 2);
2522 CPP_PUTC_Q (pfile
, '.');
2527 /* In C++ there's a .* operator. */
2528 if (opts
->cplusplus
&& c2
== '*')
2531 if (c2
== '.' && PEEKN(1) == '.')
2533 CPP_RESERVE(pfile
, 4);
2534 CPP_PUTC_Q (pfile
, '.');
2535 CPP_PUTC_Q (pfile
, '.');
2536 CPP_PUTC_Q (pfile
, '.');
2538 CPP_NUL_TERMINATE_Q (pfile
);
2539 pfile
->only_seen_white
= 0;
2546 pfile
->only_seen_white
= 0;
2547 CPP_RESERVE(pfile
, 3);
2548 CPP_PUTC_Q (pfile
, c
);
2549 CPP_PUTC_Q (pfile
, GETC ());
2550 CPP_NUL_TERMINATE_Q (pfile
);
2555 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2557 CPP_PUTC (pfile
, c
);
2563 case '0': case '1': case '2': case '3': case '4':
2564 case '5': case '6': case '7': case '8': case '9':
2569 CPP_RESERVE (pfile
, 2);
2570 CPP_PUTC_Q (pfile
, c
);
2574 if (!is_idchar
[c
] && c
!= '.'
2575 && ((c2
!= 'e' && c2
!= 'E'
2576 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2577 || (c
!= '+' && c
!= '-')))
2582 CPP_NUL_TERMINATE_Q (pfile
);
2583 pfile
->only_seen_white
= 0;
2585 case 'b': case 'c': case 'd': case 'h': case 'o':
2586 case 'B': case 'C': case 'D': case 'H': case 'O':
2587 if (opts
->chill
&& PEEKC () == '\'')
2589 pfile
->only_seen_white
= 0;
2590 CPP_RESERVE (pfile
, 2);
2591 CPP_PUTC_Q (pfile
, c
);
2592 CPP_PUTC_Q (pfile
, '\'');
2598 goto chill_number_eof
;
2601 CPP_PUTC (pfile
, c
);
2605 CPP_RESERVE (pfile
, 2);
2606 CPP_PUTC_Q (pfile
, c
);
2607 CPP_NUL_TERMINATE_Q (pfile
);
2614 CPP_NUL_TERMINATE (pfile
);
2621 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2622 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2623 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2624 case 'x': case 'y': case 'z':
2625 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2626 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2627 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2632 unsigned char *ident
;
2633 int before_name_written
= CPP_WRITTEN (pfile
);
2635 parse_name (pfile
, c
);
2636 pfile
->only_seen_white
= 0;
2637 if (pfile
->no_macro_expand
)
2639 ident
= pfile
->token_buffer
+ before_name_written
;
2640 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2641 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2644 if (hp
->type
== T_DISABLED
)
2646 if (pfile
->output_escapes
)
2647 { /* Return "\r-IDENT", followed by '\0'. */
2649 CPP_RESERVE (pfile
, 3);
2650 ident
= pfile
->token_buffer
+ before_name_written
;
2651 CPP_ADJUST_WRITTEN (pfile
, 2);
2652 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2659 /* If macro wants an arglist, verify that a '(' follows.
2660 first skip all whitespace, copying it to the output
2661 after the macro name. Then, if there is no '(',
2662 decide this is not a macro call and leave things that way. */
2663 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2665 int is_macro_call
, macbuf_whitespace
= 0;
2667 parse_set_mark (pfile
);
2670 cpp_skip_hspace (pfile
);
2672 is_macro_call
= c
== '(';
2681 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2683 if (CPP_BUFFER (pfile
)->mark
!=
2684 (CPP_BUFFER (pfile
)->cur
2685 - CPP_BUFFER (pfile
)->buf
))
2686 macbuf_whitespace
= 1;
2688 /* The mark goes away automatically when
2689 the buffer is popped. */
2690 cpp_pop_buffer (pfile
);
2691 parse_set_mark (pfile
);
2699 parse_goto_mark (pfile
);
2700 if (macbuf_whitespace
)
2701 CPP_PUTC (pfile
, ' ');
2704 parse_clear_mark (pfile
);
2708 /* This is now known to be a macro call.
2709 Expand the macro, reading arguments as needed,
2710 and push the expansion on the input stack. */
2711 macroexpand (pfile
, hp
);
2712 CPP_SET_WRITTEN (pfile
, before_name_written
);
2716 case ' ': case '\t': case '\v':
2719 CPP_PUTC (pfile
, c
);
2721 if (c
== EOF
|| !is_hor_space
[c
])
2728 if (CPP_BUFFER (pfile
)->has_escapes
)
2733 if (pfile
->output_escapes
)
2734 CPP_PUTS (pfile
, "\r-", 2);
2735 parse_name (pfile
, GETC ());
2740 CPP_RESERVE (pfile
, 2);
2741 if (pfile
->output_escapes
)
2742 CPP_PUTC_Q (pfile
, '\r');
2743 CPP_PUTC_Q (pfile
, c
);
2749 "internal error: unrecognized escape \\r%c", c
);
2755 /* Backslash newline is ignored. */
2756 CPP_BUMP_LINE (pfile
);
2761 CPP_PUTC (pfile
, c
);
2762 if (pfile
->only_seen_white
== 0)
2763 pfile
->only_seen_white
= 1;
2764 CPP_BUMP_LINE (pfile
);
2765 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2768 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2769 output_line_command (pfile
, same_file
);
2773 case '(': token
= CPP_LPAREN
; goto char1
;
2774 case ')': token
= CPP_RPAREN
; goto char1
;
2775 case '{': token
= CPP_LBRACE
; goto char1
;
2776 case '}': token
= CPP_RBRACE
; goto char1
;
2777 case ',': token
= CPP_COMMA
; goto char1
;
2778 case ';': token
= CPP_SEMICOLON
; goto char1
;
2784 pfile
->only_seen_white
= 0;
2785 CPP_PUTC (pfile
, c
);
2791 /* Like cpp_get_token, but skip spaces and comments. */
2794 cpp_get_non_space_token (pfile
)
2797 int old_written
= CPP_WRITTEN (pfile
);
2800 enum cpp_token token
= cpp_get_token (pfile
);
2801 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2802 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2804 CPP_SET_WRITTEN (pfile
, old_written
);
2808 /* Parse an identifier starting with C. */
2811 parse_name (pfile
, c
)
2823 if (c
== '$' && CPP_PEDANTIC (pfile
))
2824 cpp_pedwarn (pfile
, "`$' in identifier");
2826 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2827 CPP_PUTC_Q (pfile
, c
);
2832 CPP_NUL_TERMINATE_Q (pfile
);
2836 /* Parse a string starting with C. A single quoted string is treated
2837 like a double -- some programs (e.g., troff) are perverse this way.
2838 (However, a single quoted string is not allowed to extend over
2841 parse_string (pfile
, c
)
2845 long start_line
, start_column
;
2847 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2849 CPP_PUTC (pfile
, c
);
2855 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2857 /* try harder: this string crosses a macro expansion
2858 boundary. This can happen naturally if -traditional.
2859 Otherwise, only -D can make a macro with an unmatched
2861 cpp_pop_buffer (pfile
);
2865 cpp_error_with_line (pfile
, start_line
, start_column
,
2866 "unterminated string or character constant");
2867 if (pfile
->multiline_string_line
!= start_line
2868 && pfile
->multiline_string_line
!= 0)
2869 cpp_error_with_line (pfile
,
2870 pfile
->multiline_string_line
, -1,
2871 "possible real start of unterminated constant");
2872 pfile
->multiline_string_line
= 0;
2875 CPP_PUTC (pfile
, cc
);
2879 CPP_BUMP_LINE (pfile
);
2881 /* Character constants may not extend over multiple lines.
2882 In ANSI, neither may strings. We accept multiline strings
2886 cpp_error_with_line (pfile
, start_line
, start_column
,
2887 "unterminated character constant");
2890 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2892 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2893 "string constant runs past end of line");
2895 if (pfile
->multiline_string_line
== 0)
2896 pfile
->multiline_string_line
= start_line
;
2900 CPP_ADJUST_WRITTEN (pfile
, -1);
2901 if (CPP_BUFFER (pfile
)->has_escapes
)
2904 "internal error: \\r escape inside string constant");
2908 /* Backslash newline is replaced by nothing at all. */
2909 CPP_BUMP_LINE (pfile
);
2915 CPP_PUTC (pfile
, cc
);
2927 /* Read an assertion into the token buffer, converting to
2928 canonical form: `#predicate(a n swe r)' The next non-whitespace
2929 character to read should be the first letter of the predicate.
2930 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2931 with answer (see callers for why). In case of 0, an error has been
2934 parse_assertion (pfile
)
2938 cpp_skip_hspace (pfile
);
2940 if (! is_idstart
[c
])
2942 cpp_error (pfile
, "assertion predicate is not an identifier");
2945 CPP_PUTC(pfile
, '#');
2947 parse_name(pfile
, c
);
2952 if (is_hor_space
[c
] || c
== '\r')
2953 cpp_skip_hspace (pfile
);
2959 CPP_PUTC(pfile
, '(');
2962 while ((c
= GETC()) != ')')
2964 if (is_hor_space
[c
])
2968 CPP_PUTC(pfile
, ' ');
2972 else if (c
== '\n' || c
== EOF
)
2974 if (c
== '\n') FORWARD(-1);
2975 cpp_error (pfile
, "un-terminated assertion answer");
2979 /* \r cannot be a macro escape here. */
2980 CPP_BUMP_LINE (pfile
);
2983 CPP_PUTC (pfile
, c
);
2988 if (pfile
->limit
[-1] == ' ')
2989 pfile
->limit
[-1] = ')';
2990 else if (pfile
->limit
[-1] == '(')
2992 cpp_error (pfile
, "empty token sequence in assertion");
2996 CPP_PUTC (pfile
, ')');
2998 CPP_NUL_TERMINATE (pfile
);
3003 do_assert (pfile
, keyword
)
3005 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3009 HASHNODE
*base
, *this;
3010 int baselen
, thislen
;
3012 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3013 && !CPP_BUFFER (pfile
)->system_header_p
)
3014 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
3016 cpp_skip_hspace (pfile
);
3017 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3018 ret
= parse_assertion (pfile
);
3023 cpp_error (pfile
, "missing token-sequence in `#assert'");
3027 cpp_skip_hspace (pfile
);
3029 if (c
!= EOF
&& c
!= '\n')
3031 cpp_error (pfile
, "junk at end of `#assert'");
3035 thislen
= strlen (sym
);
3036 baselen
= index (sym
, '(') - sym
;
3037 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3040 cpp_warning (pfile
, "`%s' re-asserted", sym
);
3044 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3046 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
3047 else if (base
->type
!= T_ASSERT
)
3049 /* Token clash - but with what?! */
3051 "cpp internal error: base->type != T_ASSERT in do_assert");
3055 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
3056 (char *)base
->value
.aschain
, -1);
3057 base
->value
.aschain
= this;
3059 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3063 skip_rest_of_line (pfile
);
3064 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3069 do_unassert (pfile
, keyword
)
3071 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3075 long baselen
, thislen
;
3076 HASHNODE
*base
, *this, *next
;
3078 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3079 && !CPP_BUFFER (pfile
)->system_header_p
)
3080 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
3082 cpp_skip_hspace (pfile
);
3084 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3085 ret
= parse_assertion (pfile
);
3089 cpp_skip_hspace (pfile
);
3091 if (c
!= EOF
&& c
!= '\n')
3092 cpp_error (pfile
, "junk at end of `#unassert'");
3094 thislen
= strlen (sym
);
3097 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
3099 goto error
; /* It isn't an error to #undef what isn't #defined,
3100 so it isn't an error to #unassert what isn't
3101 #asserted either. */
3103 for (this = base
->value
.aschain
; this; this = next
)
3105 next
= this->value
.aschain
;
3106 delete_macro (this);
3108 delete_macro (base
);
3112 baselen
= index (sym
, '(') - sym
;
3113 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3114 if (! base
) goto error
;
3115 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3116 if (! this) goto error
;
3119 while (next
->value
.aschain
!= this)
3120 next
= next
->value
.aschain
;
3122 next
->value
.aschain
= this->value
.aschain
;
3123 delete_macro (this);
3125 if (base
->value
.aschain
== NULL
)
3126 delete_macro (base
); /* Last answer for this predicate deleted. */
3129 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3132 skip_rest_of_line (pfile
);
3133 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3137 /* Process STR as if it appeared as the body of an #unassert. */
3139 cpp_unassert (pfile
, str
)
3143 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3145 do_assert (pfile
, NULL
);
3146 cpp_pop_buffer (pfile
);
3151 cpp_read_check_assertion (pfile
)
3154 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
3158 FORWARD (1); /* Skip '#' */
3159 cpp_skip_hspace (pfile
);
3160 if (! parse_assertion (pfile
))
3164 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
3168 pfile
->limit
= name
;
3172 /* Remember the current position of PFILE. */
3175 parse_set_mark (pfile
)
3178 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3181 "cpp internal error: ip->mark != -1 in parse_set_mark");
3183 ip
->mark
= ip
->cur
- ip
->buf
;
3186 /* Clear the current mark - we no longer need it. */
3189 parse_clear_mark (pfile
)
3192 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3195 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3200 /* Backup the current position of PFILE to that saved in its mark,
3201 and clear the mark. */
3204 parse_goto_mark (pfile
)
3207 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3210 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3212 ip
->cur
= ip
->buf
+ ip
->mark
;
3217 cpp_print_file_and_line (pfile
)
3220 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3225 cpp_buf_line_and_col (ip
, &line
, &col
);
3226 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
3227 line
, pfile
->show_column
? col
: -1);
3232 v_cpp_error (pfile
, msgid
, ap
)
3237 cpp_print_containing_files (pfile
);
3238 cpp_print_file_and_line (pfile
);
3239 v_cpp_message (pfile
, 1, msgid
, ap
);
3243 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3245 #ifndef ANSI_PROTOTYPES
3251 VA_START(ap
, msgid
);
3253 #ifndef ANSI_PROTOTYPES
3254 pfile
= va_arg (ap
, cpp_reader
*);
3255 msgid
= va_arg (ap
, const char *);
3258 v_cpp_error (pfile
, msgid
, ap
);
3262 /* Print error message but don't count it. */
3265 v_cpp_warning (pfile
, msgid
, ap
)
3270 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3273 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3276 cpp_print_containing_files (pfile
);
3277 cpp_print_file_and_line (pfile
);
3278 v_cpp_message (pfile
, 0, msgid
, ap
);
3282 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3284 #ifndef ANSI_PROTOTYPES
3290 VA_START (ap
, msgid
);
3292 #ifndef ANSI_PROTOTYPES
3293 pfile
= va_arg (ap
, cpp_reader
*);
3294 msgid
= va_arg (ap
, const char *);
3297 v_cpp_warning (pfile
, msgid
, ap
);
3301 /* Print an error message and maybe count it. */
3304 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3306 #ifndef ANSI_PROTOTYPES
3312 VA_START (ap
, msgid
);
3314 #ifndef ANSI_PROTOTYPES
3315 pfile
= va_arg (ap
, cpp_reader
*);
3316 msgid
= va_arg (ap
, const char *);
3319 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3320 v_cpp_error (pfile
, msgid
, ap
);
3322 v_cpp_warning (pfile
, msgid
, ap
);
3327 v_cpp_error_with_line (pfile
, line
, column
, msgid
, ap
)
3334 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3336 cpp_print_containing_files (pfile
);
3339 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3341 v_cpp_message (pfile
, 1, msgid
, ap
);
3345 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3346 const char *msgid
, ...))
3348 #ifndef ANSI_PROTOTYPES
3356 VA_START (ap
, msgid
);
3358 #ifndef ANSI_PROTOTYPES
3359 pfile
= va_arg (ap
, cpp_reader
*);
3360 line
= va_arg (ap
, int);
3361 column
= va_arg (ap
, int);
3362 msgid
= va_arg (ap
, const char *);
3365 v_cpp_error_with_line(pfile
, line
, column
, msgid
, ap
);
3370 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
)
3379 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3382 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3385 cpp_print_containing_files (pfile
);
3387 ip
= cpp_file_buffer (pfile
);
3390 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3392 v_cpp_message (pfile
, 0, msgid
, ap
);
3396 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3397 const char *msgid
, ...))
3399 #ifndef ANSI_PROTOTYPES
3407 VA_START (ap
, msgid
);
3409 #ifndef ANSI_PROTOTYPES
3410 pfile
= va_arg (ap
, cpp_reader
*);
3411 line
= va_arg (ap
, int);
3412 column
= va_arg (ap
, int);
3413 msgid
= va_arg (ap
, const char *);
3416 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3421 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3422 const char *msgid
, ...))
3424 #ifndef ANSI_PROTOTYPES
3432 VA_START (ap
, msgid
);
3434 #ifndef ANSI_PROTOTYPES
3435 pfile
= va_arg (ap
, cpp_reader
*);
3436 line
= va_arg (ap
, int);
3437 column
= va_arg (ap
, int);
3438 msgid
= va_arg (ap
, const char *);
3441 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3442 v_cpp_error_with_line (pfile
, column
, line
, msgid
, ap
);
3444 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3448 /* Report a warning (or an error if pedantic_errors)
3449 giving specified file name and line number, not current. */
3452 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, const char *file
,
3453 int line
, const char *msgid
, ...))
3455 #ifndef ANSI_PROTOTYPES
3463 VA_START (ap
, msgid
);
3465 #ifndef ANSI_PROTOTYPES
3466 pfile
= va_arg (ap
, cpp_reader
*);
3467 file
= va_arg (ap
, const char *);
3468 line
= va_arg (ap
, int);
3469 msgid
= va_arg (ap
, const char *);
3472 if (!CPP_OPTIONS (pfile
)->pedantic_errors
3473 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
3476 cpp_file_line_for_message (pfile
, file
, line
, -1);
3477 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msgid
, ap
);
3481 /* my_strerror - return the descriptive text associated with an
3485 my_strerror (errnum
)
3491 #ifndef HAVE_STRERROR
3492 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
3494 result
= strerror (errnum
);
3497 /* VAXCRTL's strerror() takes an optional second argument, which only
3498 matters when the first argument is EVMSERR. However, it's simplest
3499 just to pass it unconditionally. `vaxc$errno' is declared in
3500 <errno.h>, and maintained by the library in parallel with `errno'.
3501 We assume that caller's `errnum' either matches the last setting of
3502 `errno' by the library or else does not have the value `EVMSERR'. */
3504 result
= strerror (errnum
, vaxc$errno
);
3508 result
= "errno = ?";
3513 /* Error including a message from `errno'. */
3516 cpp_error_from_errno (pfile
, name
)
3520 cpp_message_from_errno (pfile
, 1, name
);
3524 cpp_message_from_errno (pfile
, is_error
, name
)
3530 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3532 cpp_print_containing_files (pfile
);
3535 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
3537 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
3541 cpp_perror_with_name (pfile
, name
)
3545 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
3549 * No pre-compiled header file support.
3551 * Possibly different enum token codes for each C/C++ token.
3553 * Find and cleanup remaining uses of static variables,
3555 * Support -dM flag (dump_all_macros).
3557 * Support for_lint flag.