2 Copyright (C) 1986, 87, 89, 92-99, 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
31 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
32 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
33 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
34 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
35 (Note that it is false while we're expanding macro *arguments*.) */
36 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
38 /* External declarations. */
40 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
42 /* `struct directive' defines one #-directive, including how to handle it. */
45 int length
; /* Length of name */
46 int (*func
) /* Function to handle directive */
47 PARAMS ((cpp_reader
*, const struct directive
*));
48 const char *name
; /* Name of directive */
49 enum node_type type
; /* Code which describes which directive. */
52 /* These functions are declared to return int instead of void since they
53 are going to be placed in a table and some old compilers have trouble with
54 pointers to functions returning void. */
56 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
57 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
58 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
59 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
60 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
61 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
62 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
63 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
64 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
65 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
66 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
67 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
69 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
71 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
72 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
73 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
75 /* Forward declarations. */
77 static const char *my_strerror
PARAMS ((int));
78 static void validate_else
PARAMS ((cpp_reader
*, const char *));
79 static HOST_WIDEST_INT eval_if_expression
PARAMS ((cpp_reader
*));
80 static void conditional_skip
PARAMS ((cpp_reader
*, int,
81 enum node_type
, U_CHAR
*));
82 static void skip_if_group
PARAMS ((cpp_reader
*));
83 static void parse_name
PARAMS ((cpp_reader
*, int));
84 static void parse_string
PARAMS ((cpp_reader
*, int));
85 static int parse_assertion
PARAMS ((cpp_reader
*));
86 static const char *if_directive_name
PARAMS ((cpp_reader
*,
88 static enum cpp_token null_underflow
PARAMS ((cpp_reader
*));
89 static int null_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
90 static int skip_comment
PARAMS ((cpp_reader
*, int));
91 static int copy_comment
PARAMS ((cpp_reader
*, int));
92 static void copy_rest_of_line
PARAMS ((cpp_reader
*));
93 static int handle_directive
PARAMS ((cpp_reader
*));
94 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t,
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 *,
102 static void v_cpp_warning
PARAMS ((cpp_reader
*, const char *,
104 static void v_cpp_error_with_line
PARAMS ((cpp_reader
*, int, int,
105 const char *, va_list));
106 static void v_cpp_warning_with_line
PARAMS ((cpp_reader
*, int, int,
107 const char *, va_list));
108 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
109 static int consider_directive_while_skipping
PARAMS ((cpp_reader
*,
111 static void skip_block_comment
PARAMS ((cpp_reader
*));
112 static void skip_line_comment
PARAMS ((cpp_reader
*));
114 /* Here is the actual list of #-directives.
115 This table is ordered by frequency of occurrence; the numbers
116 at the end are directive counts from all the source code I have
117 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
118 linux-2.2.9, and pcmcia-cs-3.0.9). */
120 static const struct directive directive_table
[] = {
122 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
123 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
124 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
125 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
126 { 2, do_if
, "if", T_IF
}, /* 18162 */
127 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
128 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
129 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
130 { 4, do_line
, "line", T_LINE
}, /* 2465 */
131 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
132 { 5, do_error
, "error", T_ERROR
}, /* 475 */
133 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
135 /* Extensions. All deprecated except #warning and #include_next. */
136 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
137 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
138 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
139 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
140 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
141 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
142 #ifdef SCCS_DIRECTIVE
143 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
145 { -1, 0, "", T_UNUSED
}
148 /* Place into PFILE a quoted string representing the string SRC.
149 Caller must reserve enough space in pfile->token_buffer. */
152 quote_string (pfile
, src
)
158 CPP_PUTC_Q (pfile
, '\"');
160 switch ((c
= *src
++))
164 CPP_PUTC_Q (pfile
, c
);
167 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
168 CPP_ADJUST_WRITTEN (pfile
, 4);
174 CPP_PUTC_Q (pfile
, '\\');
175 CPP_PUTC_Q (pfile
, c
);
179 CPP_PUTC_Q (pfile
, '\"');
180 CPP_NUL_TERMINATE_Q (pfile
);
185 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
188 cpp_grow_buffer (pfile
, n
)
192 long old_written
= CPP_WRITTEN (pfile
);
193 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
194 pfile
->token_buffer
= (U_CHAR
*)
195 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
196 CPP_SET_WRITTEN (pfile
, old_written
);
199 /* Process the string STR as if it appeared as the body of a #define
200 If STR is just an identifier, define it with value 1.
201 If STR has anything after the identifier, then it should
202 be identifier=definition. */
205 cpp_define (pfile
, str
)
212 /* Copy the entire option so we can modify it. */
213 count
= strlen (str
) + 3;
214 buf
= (U_CHAR
*) alloca (count
);
215 memcpy (buf
, str
, count
- 2);
216 /* Change the first "=" in the string to a space. If there is none,
217 tack " 1" on the end. */
218 p
= (U_CHAR
*) strchr (buf
, '=');
225 strcpy (&buf
[count
-3], " 1");
227 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
229 do_define (pfile
, NULL
);
230 cpp_pop_buffer (pfile
);
234 /* Process the string STR as if it appeared as the body of a #assert. */
236 cpp_assert (pfile
, str
)
240 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
242 do_assert (pfile
, NULL
);
243 cpp_pop_buffer (pfile
);
248 static enum cpp_token
249 null_underflow (pfile
)
250 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
256 null_cleanup (pbuf
, pfile
)
257 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
258 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
263 /* Skip a C-style block comment. We know it's a comment, and point is
264 at the second character of the starter. */
266 skip_block_comment (pfile
)
273 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
279 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
282 else if (c
== '\n' || c
== '\r')
283 /* \r cannot be a macro escape marker here. */
284 CPP_BUMP_LINE (pfile
);
285 else if (c
== '/' && prev_c
== '*')
287 else if (c
== '*' && prev_c
== '/'
288 && CPP_OPTIONS (pfile
)->warn_comments
)
289 cpp_warning (pfile
, "`/*' within comment");
295 /* Skip a C++/Chill line comment. We know it's a comment, and point
296 is at the second character of the initiator. */
298 skip_line_comment (pfile
)
306 /* We don't have to worry about EOF in here. */
309 /* Don't consider final '\n' to be part of comment. */
315 /* \r cannot be a macro escape marker here. */
316 CPP_BUMP_LINE (pfile
);
317 if (CPP_OPTIONS (pfile
)->warn_comments
)
318 cpp_warning (pfile
, "backslash-newline within line comment");
323 /* Skip a comment - C, C++, or Chill style. M is the first character
324 of the comment marker. If this really is a comment, skip to its
325 end and return ' '. If this is not a comment, return M (which will
329 skip_comment (pfile
, m
)
333 if (m
== '/' && PEEKC() == '*')
335 skip_block_comment (pfile
);
338 else if (m
== '/' && PEEKC() == '/')
340 if (CPP_BUFFER (pfile
)->system_header_p
)
342 /* We silently allow C++ comments in system headers, irrespective
343 of conformance mode, because lots of busted systems do that
344 and trying to clean it up in fixincludes is a nightmare. */
345 skip_line_comment (pfile
);
348 else if (CPP_OPTIONS (pfile
)->cplusplus_comments
)
350 if (CPP_OPTIONS (pfile
)->c89
351 && CPP_PEDANTIC (pfile
)
352 && ! CPP_BUFFER (pfile
)->warned_cplusplus_comments
)
355 "C++ style comments are not allowed in ISO C89");
357 "(this will be reported only once per input file)");
358 CPP_BUFFER (pfile
)->warned_cplusplus_comments
= 1;
360 skip_line_comment (pfile
);
366 else if (m
== '-' && PEEKC() == '-'
367 && CPP_OPTIONS (pfile
)->chill
)
369 skip_line_comment (pfile
);
376 /* Identical to skip_comment except that it copies the comment into the
377 token_buffer. This is used if put_out_comments. */
379 copy_comment (pfile
, m
)
383 U_CHAR
*start
= CPP_BUFFER (pfile
)->cur
; /* XXX Layering violation */
386 if (skip_comment (pfile
, m
) == m
)
390 for (limit
= CPP_BUFFER (pfile
)->cur
; start
<= limit
; start
++)
392 CPP_PUTC (pfile
, *start
);
396 /* Skip whitespace \-newline and comments. Does not macro-expand. */
399 cpp_skip_hspace (pfile
)
408 else if (is_hspace(c
))
410 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
411 cpp_pedwarn (pfile
, "%s in preprocessing directive",
412 c
== '\f' ? "formfeed" : "vertical tab");
416 /* \r is a backslash-newline marker if !has_escapes, and
417 a deletable-whitespace or no-reexpansion marker otherwise. */
418 if (CPP_BUFFER (pfile
)->has_escapes
)
426 CPP_BUFFER (pfile
)->lineno
++;
428 else if (c
== '/' || c
== '-')
430 c
= skip_comment (pfile
, c
);
440 /* Read the rest of the current line.
441 The line is appended to PFILE's output buffer. */
444 copy_rest_of_line (pfile
)
455 CPP_NUL_TERMINATE (pfile
);
459 if (CPP_BUFFER (pfile
)->has_escapes
)
463 CPP_BUFFER (pfile
)->lineno
++;
468 parse_string (pfile
, c
);
473 if (CPP_TRADITIONAL (pfile
))
474 CPP_PUTS (pfile
, "/**/", 4);
475 skip_block_comment (pfile
);
478 /* else fall through */
480 c
= skip_comment (pfile
, c
);
485 if (CPP_PEDANTIC (pfile
))
486 cpp_pedwarn (pfile
, "%s in preprocessing directive",
487 c
== '\f' ? "formfeed" : "vertical tab");
495 /* FIXME: It is almost definitely a performance win to make this do
496 the scan itself. >75% of calls to copy_r_o_l are from here or
497 skip_if_group, which means the common case is to copy stuff into the
498 token_buffer only to discard it. */
500 skip_rest_of_line (pfile
)
503 long old
= CPP_WRITTEN (pfile
);
504 copy_rest_of_line (pfile
);
505 CPP_SET_WRITTEN (pfile
, old
);
508 /* Handle a possible # directive.
509 '#' has already been read. */
512 handle_directive (pfile
)
516 register const struct directive
*kt
;
519 long old_written
= CPP_WRITTEN (pfile
);
521 cpp_skip_hspace (pfile
);
524 /* # followed by a number is equivalent to #line. Do not recognize
525 this form in assembly language source files. Complain about this
526 form if we're being pedantic, but not if this is regurgitated
527 input (preprocessed or fed back in by the C++ frontend). */
528 if (c
>= '0' && c
<= '9')
530 if (CPP_OPTIONS (pfile
)->lang_asm
)
533 if (CPP_PEDANTIC (pfile
)
534 && ! CPP_PREPROCESSED (pfile
)
535 && ! CPP_BUFFER (pfile
)->manual_pop
)
536 cpp_pedwarn (pfile
, "`#' followed by integer");
537 do_line (pfile
, NULL
);
541 /* If we are rescanning preprocessed input, don't obey any directives
543 if (CPP_PREPROCESSED (pfile
))
546 /* Now find the directive name. */
547 CPP_PUTC (pfile
, '#');
548 parse_name (pfile
, GETC());
549 ident
= pfile
->token_buffer
+ old_written
+ 1;
550 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
551 if (ident_length
== 0)
553 /* A line of just `#' becomes blank. A line with something
554 other than an identifier after the # is reparsed as a non-
556 CPP_SET_WRITTEN (pfile
, old_written
);
557 return (PEEKC() == '\n');
560 /* Decode the keyword and call the appropriate expansion routine. */
561 for (kt
= directive_table
; ; kt
++)
564 /* # identifier, but not a legit directive. Pass onward as a
565 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
567 if (kt
->length
== ident_length
568 && !strncmp (kt
->name
, ident
, ident_length
))
572 CPP_SET_WRITTEN (pfile
, old_written
);
574 if (pfile
->no_directives
)
576 cpp_error (pfile
, "`#%s' may not be used inside a macro argument",
578 skip_rest_of_line (pfile
);
581 (*kt
->func
) (pfile
, kt
);
586 /* Pass a directive through to the output file.
587 BUF points to the contents of the directive, as a contiguous string.
588 LEN is the length of the string pointed to by BUF.
589 KEYWORD is the keyword-table entry for the directive. */
592 pass_thru_directive (buf
, len
, pfile
, keyword
)
596 const struct directive
*keyword
;
598 register unsigned keyword_length
= keyword
->length
;
600 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
601 CPP_PUTC_Q (pfile
, '#');
602 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
603 if (len
!= 0 && buf
[0] != ' ')
604 CPP_PUTC_Q (pfile
, ' ');
605 CPP_PUTS_Q (pfile
, buf
, len
);
608 /* Check a purported macro name SYMNAME, and yield its length. */
611 check_macro_name (pfile
, symname
)
613 const U_CHAR
*symname
;
618 for (p
= symname
; is_idchar(*p
); p
++)
620 sym_length
= p
- symname
;
622 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
623 cpp_error (pfile
, "invalid macro name");
624 else if (!is_idstart(*symname
)
625 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
626 U_CHAR
*msg
; /* what pain... */
627 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
628 bcopy (symname
, msg
, sym_length
);
630 cpp_error (pfile
, "invalid macro name `%s'", msg
);
635 /* Process a #define command.
636 KEYWORD is the keyword-table entry for #define,
637 or NULL for a "predefined" macro,
638 or the keyword-table entry for #pragma in the case of a #pragma poison. */
641 do_define (pfile
, keyword
)
643 const struct directive
*keyword
;
649 U_CHAR
*macro
, *buf
, *end
;
650 enum node_type new_type
;
652 here
= CPP_WRITTEN (pfile
);
653 copy_rest_of_line (pfile
);
655 if (keyword
== NULL
|| keyword
->type
== T_DEFINE
)
660 /* Copy out the line so we can pop the token buffer. */
661 buf
= pfile
->token_buffer
+ here
;
662 end
= CPP_PWRITTEN (pfile
);
663 macro
= (U_CHAR
*) alloca (end
- buf
+ 1);
664 bcopy (buf
, macro
, end
- buf
+ 1);
665 end
= macro
+ (end
- buf
);
667 CPP_SET_WRITTEN (pfile
, here
);
669 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
673 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
675 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
678 /* Redefining a precompiled key is ok. */
679 if (hp
->type
== T_PCSTRING
)
681 /* Redefining a poisoned identifier is even worse than `not ok'. */
682 else if (hp
->type
== T_POISON
)
684 /* Redefining a macro is ok if the definitions are the same. */
685 else if (hp
->type
== T_MACRO
)
686 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
687 /* Redefining a constant is ok with -D. */
688 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
689 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
690 /* Print the warning or error if it's not ok. */
693 if (hp
->type
== T_POISON
)
694 cpp_error (pfile
, "redefining poisoned `%.*s'",
695 mdef
.symlen
, mdef
.symnam
);
697 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
698 if (hp
->type
== T_MACRO
&& CPP_OPTIONS (pfile
)->done_initializing
)
699 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
700 hp
->value
.defn
->line
,
701 "this is the location of the previous definition");
703 if (hp
->type
!= T_POISON
)
705 /* Replace the old definition. */
707 hp
->value
.defn
= mdef
.defn
;
711 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, new_type
,
712 (char *) mdef
.defn
, hashcode
);
714 if (keyword
!= NULL
&& keyword
->type
== T_DEFINE
)
716 if (CPP_OPTIONS (pfile
)->debug_output
717 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
718 dump_definition (pfile
, mdef
);
719 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
720 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
727 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
728 If BUFFER != NULL, then use the LENGTH characters in BUFFER
729 as the new input buffer.
730 Return the new buffer, or NULL on failure. */
733 cpp_push_buffer (pfile
, buffer
, length
)
738 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
740 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
742 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
746 new = (cpp_buffer
*) xcalloc (1, sizeof (cpp_buffer
));
748 new->if_stack
= pfile
->if_stack
;
749 new->cleanup
= null_cleanup
;
750 new->underflow
= null_underflow
;
751 new->buf
= new->cur
= buffer
;
752 new->alimit
= new->rlimit
= buffer
+ length
;
756 CPP_BUFFER (pfile
) = new;
761 cpp_pop_buffer (pfile
)
764 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
765 (*buf
->cleanup
) (buf
, pfile
);
766 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
768 pfile
->buffer_stack_depth
--;
769 return CPP_BUFFER (pfile
);
772 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
773 Pop the buffer when done. */
776 cpp_scan_buffer (pfile
)
779 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
780 enum cpp_token token
;
781 if (CPP_OPTIONS (pfile
)->no_output
)
783 long old_written
= CPP_WRITTEN (pfile
);
784 /* In no-output mode, we can ignore everything but directives. */
787 if (! pfile
->only_seen_white
)
788 skip_rest_of_line (pfile
);
789 token
= cpp_get_token (pfile
);
790 if (token
== CPP_EOF
) /* Should not happen ... */
792 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
794 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
795 != CPP_NULL_BUFFER (pfile
))
796 cpp_pop_buffer (pfile
);
800 CPP_SET_WRITTEN (pfile
, old_written
);
806 token
= cpp_get_token (pfile
);
807 if (token
== CPP_EOF
) /* Should not happen ... */
809 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
811 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
812 != CPP_NULL_BUFFER (pfile
))
813 cpp_pop_buffer (pfile
);
821 * Rescan a string (which may have escape marks) into pfile's buffer.
822 * Place the result in pfile->token_buffer.
824 * The input is copied before it is scanned, so it is safe to pass
825 * it something from the token_buffer that will get overwritten
826 * (because it follows CPP_WRITTEN). This is used by do_include.
830 cpp_expand_to_buffer (pfile
, buf
, length
)
835 register cpp_buffer
*ip
;
841 cpp_fatal (pfile
, "internal error: length < 0 in cpp_expand_to_buffer");
845 /* Set up the input on the input stack. */
847 buf1
= (U_CHAR
*) alloca (length
+ 1);
848 memcpy (buf1
, buf
, length
);
851 ip
= cpp_push_buffer (pfile
, buf1
, length
);
856 /* Scan the input, create the output. */
857 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
858 CPP_OPTIONS (pfile
)->no_output
= 0;
859 cpp_scan_buffer (pfile
);
860 CPP_OPTIONS (pfile
)->no_output
= save_no_output
;
862 CPP_NUL_TERMINATE (pfile
);
866 cpp_buf_line_and_col (pbuf
, linep
, colp
)
867 register cpp_buffer
*pbuf
;
872 *linep
= pbuf
->lineno
;
874 *colp
= pbuf
->cur
- pbuf
->line_base
;
884 /* Return the cpp_buffer that corresponds to a file (not a macro). */
887 cpp_file_buffer (pfile
)
890 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
892 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
893 if (ip
->fname
!= NULL
)
899 * write out a #line command, for instance, after an #include file.
900 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
904 output_line_command (pfile
, file_change
)
906 enum file_change_code file_change
;
909 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
911 if (ip
->fname
== NULL
)
914 if (CPP_OPTIONS (pfile
)->no_line_commands
915 || CPP_OPTIONS (pfile
)->no_output
)
918 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
920 /* If the current file has not changed, we omit the #line if it would
921 appear to be a no-op, and we output a few newlines instead
922 if we want to increase the line number by a small amount.
923 We cannot do this if pfile->lineno is zero, because that means we
924 haven't output any line commands yet. (The very first line command
925 output is a `same_file' command.) */
926 if (file_change
== same_file
&& pfile
->lineno
!= 0)
928 if (line
== pfile
->lineno
)
931 /* If the inherited line number is a little too small,
932 output some newlines instead of a #line command. */
933 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
935 CPP_RESERVE (pfile
, 20);
936 while (line
> pfile
->lineno
)
938 CPP_PUTC_Q (pfile
, '\n');
945 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
946 CPP_PUTS_Q (pfile
, "# ", 2);
948 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
949 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
951 quote_string (pfile
, ip
->nominal_fname
);
952 if (file_change
!= same_file
)
954 CPP_PUTC_Q (pfile
, ' ');
955 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
957 /* Tell cc1 if following text comes from a system header file. */
958 if (ip
->system_header_p
)
960 CPP_PUTC_Q (pfile
, ' ');
961 CPP_PUTC_Q (pfile
, '3');
963 #ifndef NO_IMPLICIT_EXTERN_C
964 /* Tell cc1plus if following text should be treated as C. */
965 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
967 CPP_PUTC_Q (pfile
, ' ');
968 CPP_PUTC_Q (pfile
, '4');
971 CPP_PUTC_Q (pfile
, '\n');
972 pfile
->lineno
= line
;
976 /* Like cpp_get_token, except that it does not read past end-of-line.
977 Also, horizontal space is skipped, and macros are popped. */
979 static enum cpp_token
980 get_directive_token (pfile
)
983 long old_written
= CPP_WRITTEN (pfile
);
984 enum cpp_token token
;
988 cpp_skip_hspace (pfile
);
989 if (PEEKC () == '\n')
992 token
= cpp_get_token (pfile
);
993 /* token could be hspace at the beginning of a macro. */
994 if (token
== CPP_HSPACE
|| token
== CPP_COMMENT
)
996 CPP_SET_WRITTEN (pfile
, old_written
);
1000 /* token cannot be vspace, it would have been caught above. */
1001 if (token
== CPP_VSPACE
)
1003 cpp_fatal (pfile
, "VSPACE in get_directive_token");
1007 /* token cannot be POP unless the buffer is a macro buffer. */
1008 if (token
!= CPP_POP
)
1011 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
1013 cpp_fatal (pfile
, "POP of file buffer in get_directive_token");
1017 /* We must pop the buffer by hand, else cpp_get_token might hand
1018 us whitespace or newline on the next invocation. */
1019 cpp_pop_buffer (pfile
);
1023 /* Handle #include and #import.
1024 This function expects to see "fname" or <fname> on the input.
1026 The input is normally in part of the output_buffer following
1027 CPP_WRITTEN, and will get overwritten by output_line_command.
1028 I.e. in input file specification has been popped by handle_directive.
1032 do_include (pfile
, keyword
)
1034 const struct directive
*keyword
;
1036 int importing
= (keyword
->type
== T_IMPORT
);
1037 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1038 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1039 int before
; /* included before? */
1041 unsigned char *ftok
;
1044 enum cpp_token token
;
1046 /* Chain of dirs to search */
1047 struct include_hash
*ihash
;
1048 struct file_name_list
*search_start
;
1050 long old_written
= CPP_WRITTEN (pfile
);
1054 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1057 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1059 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1062 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1063 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1064 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1066 pfile
->import_warning
= 1;
1068 "#import is obsolete, use an #ifndef wrapper in the header file");
1071 pfile
->parsing_include_directive
++;
1072 token
= get_directive_token (pfile
);
1073 pfile
->parsing_include_directive
--;
1075 if (token
== CPP_STRING
)
1077 if (pfile
->token_buffer
[old_written
] == '<')
1081 else if (token
== CPP_NAME
)
1083 /* Support '#include xyz' like VAX-C. It is taken as
1084 '#include <xyz.h>' and generates a warning. */
1086 "`#include filename' is obsolete, use `#include <filename.h>'");
1089 /* Append the missing `.h' to the name. */
1090 CPP_PUTS (pfile
, ".h", 2);
1096 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1097 CPP_SET_WRITTEN (pfile
, old_written
);
1098 skip_rest_of_line (pfile
);
1102 flen
= CPP_WRITTEN (pfile
) - old_written
;
1103 ftok
= (unsigned char *) alloca (flen
+ 1);
1104 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1107 if (get_directive_token (pfile
) != CPP_VSPACE
)
1109 cpp_error (pfile
, "junk at end of `#include'");
1110 skip_rest_of_line (pfile
);
1113 CPP_SET_WRITTEN (pfile
, old_written
);
1117 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1121 if (CPP_OPTIONS (pfile
)->dump_includes
)
1122 pass_thru_directive (ftok
,
1125 - ((token
== CPP_NAME
) ? 2 : 0)
1130 if (token
== CPP_STRING
)
1140 for (fp
= CPP_BUFFER (pfile
);
1141 fp
!= CPP_NULL_BUFFER (pfile
);
1142 fp
= CPP_PREV_BUFFER (fp
))
1143 if (fp
->fname
!= NULL
)
1146 if (fp
== CPP_NULL_BUFFER (pfile
))
1148 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
1152 /* For #include_next, skip in the search path past the dir in which the
1153 containing file was found. Treat files specified using an absolute path
1154 as if there are no more directories to search. Treat the primary source
1155 file like any other included source, but generate a warning. */
1156 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1158 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1159 search_start
= fp
->ihash
->foundhere
->next
;
1164 cpp_warning (pfile
, "#include_next in primary source file");
1167 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1170 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1173 search_start
= fp
->actual_dir
;
1176 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1182 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1186 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1193 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1194 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1195 (pfile
->system_include_depth
> 0)))
1197 if (!angle_brackets
)
1198 deps_output (pfile
, ftok
, ' ');
1202 struct file_name_list
*ptr
;
1203 /* If requested as a system header, assume it belongs in
1204 the first system header directory. */
1205 if (CPP_OPTIONS (pfile
)->bracket_include
)
1206 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1208 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1210 p
= (char *) alloca (strlen (ptr
->name
)
1211 + strlen (ftok
) + 2);
1212 if (*ptr
->name
!= '\0')
1214 strcpy (p
, ptr
->name
);
1218 deps_output (pfile
, p
, ' ');
1221 /* If -M was specified, and this header file won't be added to
1222 the dependency list, then don't count this as an error,
1223 because we can still produce correct output. Otherwise, we
1224 can't produce correct output, because there may be
1225 dependencies we need inside the missing file, and we don't
1226 know what directory this missing file exists in. */
1227 else if (CPP_PRINT_DEPS (pfile
)
1228 && (CPP_PRINT_DEPS (pfile
)
1229 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1230 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1232 cpp_error_from_errno (pfile
, ftok
);
1237 /* For -M, add the file to the dependencies on its first inclusion. */
1238 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1239 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1240 deps_output (pfile
, ihash
->name
, ' ');
1242 /* Handle -H option. */
1243 if (CPP_OPTIONS(pfile
)->print_include_names
)
1245 fp
= CPP_BUFFER (pfile
);
1246 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1248 fprintf (stderr
, " %s\n", ihash
->name
);
1251 /* Actually process the file */
1254 ihash
->control_macro
= "";
1256 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1263 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1265 if (finclude (pfile
, fd
, ihash
))
1267 output_line_command (pfile
, enter_file
);
1268 pfile
->only_seen_white
= 2;
1274 /* Subroutine of do_line. Read next token from PFILE without adding it to
1275 the output buffer. If it is a number between 1 and 4, store it in *NUM
1276 and return 1; otherwise, return 0 and complain if we aren't at the end
1277 of the directive. */
1280 read_line_number (pfile
, num
)
1284 long save_written
= CPP_WRITTEN (pfile
);
1285 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1286 enum cpp_token token
= get_directive_token (pfile
);
1287 CPP_SET_WRITTEN (pfile
, save_written
);
1289 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1296 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1297 cpp_error (pfile
, "invalid format `#line' command");
1302 /* Interpret #line command.
1303 Note that the filename string (if any) is treated as if it were an
1304 include filename. That means no escape handling. */
1307 do_line (pfile
, keyword
)
1309 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1311 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1313 long old_written
= CPP_WRITTEN (pfile
);
1314 enum file_change_code file_change
= same_file
;
1315 enum cpp_token token
;
1318 token
= get_directive_token (pfile
);
1320 if (token
!= CPP_NUMBER
)
1322 cpp_error (pfile
, "token after `#line' is not an integer");
1323 goto bad_line_directive
;
1326 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1329 cpp_error (pfile
, "token after `#line' is not an integer");
1330 goto bad_line_directive
;
1332 CPP_SET_WRITTEN (pfile
, old_written
);
1334 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1335 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1337 token
= get_directive_token (pfile
);
1339 if (token
== CPP_STRING
)
1341 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1342 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1343 int action_number
= 0;
1345 if (read_line_number (pfile
, &action_number
))
1347 if (CPP_PEDANTIC (pfile
))
1348 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1350 if (action_number
== 1)
1352 file_change
= enter_file
;
1353 read_line_number (pfile
, &action_number
);
1355 else if (action_number
== 2)
1357 file_change
= leave_file
;
1358 read_line_number (pfile
, &action_number
);
1360 if (action_number
== 3)
1362 ip
->system_header_p
= 1;
1363 read_line_number (pfile
, &action_number
);
1365 if (action_number
== 4)
1367 ip
->system_header_p
= 2;
1368 read_line_number (pfile
, &action_number
);
1374 if (strcmp (fname
, ip
->nominal_fname
))
1376 const char *newname
, *oldname
;
1377 if (!strcmp (fname
, ip
->fname
))
1378 newname
= ip
->fname
;
1379 else if (ip
->last_nominal_fname
1380 && !strcmp (fname
, ip
->last_nominal_fname
))
1381 newname
= ip
->last_nominal_fname
;
1383 newname
= xstrdup (fname
);
1385 oldname
= ip
->nominal_fname
;
1386 ip
->nominal_fname
= newname
;
1388 if (ip
->last_nominal_fname
1389 && ip
->last_nominal_fname
!= oldname
1390 && ip
->last_nominal_fname
!= newname
1391 && ip
->last_nominal_fname
!= ip
->fname
)
1392 free ((void *) ip
->last_nominal_fname
);
1394 if (newname
== ip
->fname
)
1395 ip
->last_nominal_fname
= NULL
;
1397 ip
->last_nominal_fname
= oldname
;
1400 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1402 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1403 goto bad_line_directive
;
1406 /* The Newline at the end of this line remains to be processed.
1407 To put the next line at the specified line number,
1408 we must store a line number now that is one less. */
1409 ip
->lineno
= new_lineno
- 1;
1410 CPP_SET_WRITTEN (pfile
, old_written
);
1411 output_line_command (pfile
, file_change
);
1415 skip_rest_of_line (pfile
);
1416 CPP_SET_WRITTEN (pfile
, old_written
);
1420 /* Remove the definition of a symbol from the symbol table.
1421 According to the C standard, it is not an error to undef
1422 something that has no definitions. */
1424 do_undef (pfile
, keyword
)
1426 const struct directive
*keyword
;
1430 U_CHAR
*buf
, *name
, *limit
;
1432 long here
= CPP_WRITTEN (pfile
);
1433 enum cpp_token token
;
1435 cpp_skip_hspace (pfile
);
1437 if (! is_idstart(c
))
1439 cpp_error (pfile
, "token after #undef is not an identifier");
1440 skip_rest_of_line (pfile
);
1444 parse_name (pfile
, c
);
1445 buf
= pfile
->token_buffer
+ here
;
1446 limit
= CPP_PWRITTEN(pfile
);
1448 /* Copy out the token so we can pop the token buffer. */
1449 name
= (U_CHAR
*) alloca (limit
- buf
+ 1);
1450 bcopy(buf
, name
, limit
- buf
);
1451 name
[limit
- buf
] = '\0';
1453 token
= get_directive_token (pfile
);
1454 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1456 cpp_pedwarn (pfile
, "junk on line after #undef");
1457 skip_rest_of_line (pfile
);
1460 CPP_SET_WRITTEN (pfile
, here
);
1462 sym_length
= check_macro_name (pfile
, buf
);
1464 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1466 /* If we are generating additional info for debugging (with -g) we
1467 need to pass through all effective #undef commands. */
1468 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1469 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1470 if (hp
->type
== T_POISON
)
1471 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
1474 if (hp
->type
!= T_MACRO
)
1475 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1483 /* Wrap do_undef for -U processing. */
1485 cpp_undef (pfile
, macro
)
1489 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1491 do_undef (pfile
, NULL
);
1492 cpp_pop_buffer (pfile
);
1498 * Report an error detected by the program we are processing.
1499 * Use the text of the line in the error message.
1500 * (We use error because it prints the filename & line#.)
1504 do_error (pfile
, keyword
)
1506 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1508 long here
= CPP_WRITTEN (pfile
);
1510 copy_rest_of_line (pfile
);
1511 text
= pfile
->token_buffer
+ here
;
1512 SKIP_WHITE_SPACE(text
);
1514 cpp_error (pfile
, "#error %s", text
);
1515 CPP_SET_WRITTEN (pfile
, here
);
1520 * Report a warning detected by the program we are processing.
1521 * Use the text of the line in the warning message, then continue.
1525 do_warning (pfile
, keyword
)
1527 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1530 long here
= CPP_WRITTEN(pfile
);
1531 copy_rest_of_line (pfile
);
1532 text
= pfile
->token_buffer
+ here
;
1533 SKIP_WHITE_SPACE(text
);
1535 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1536 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1538 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1539 if -pedantic-errors is given, #warning should cause an error. */
1540 cpp_pedwarn (pfile
, "#warning %s", text
);
1541 CPP_SET_WRITTEN (pfile
, here
);
1545 /* Report program identification.
1546 This is not precisely what cccp does with #ident, however I believe
1547 it matches `closely enough' (behavior is identical as long as there
1548 are no macros on the #ident line, which is pathological in my opinion). */
1551 do_ident (pfile
, keyword
)
1553 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1555 /* Allow #ident in system headers, since that's not user's fault. */
1556 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1557 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1559 CPP_PUTS (pfile
, "#ident ", 7);
1560 cpp_skip_hspace (pfile
);
1561 copy_rest_of_line (pfile
);
1566 /* Just check for some recognized pragmas that need validation here,
1567 and leave the text in the token buffer to be output. */
1570 do_pragma (pfile
, keyword
)
1572 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1577 CPP_PUTS (pfile
, "#pragma ", 8);
1578 cpp_skip_hspace (pfile
);
1580 here
= CPP_WRITTEN (pfile
);
1581 copy_rest_of_line (pfile
);
1582 buf
= pfile
->token_buffer
+ here
;
1584 if (!strncmp (buf
, "once", 4))
1586 cpp_buffer
*ip
= NULL
;
1588 /* Allow #pragma once in system headers, since that's not the user's
1590 if (!CPP_BUFFER (pfile
)->system_header_p
)
1591 cpp_warning (pfile
, "`#pragma once' is obsolete");
1593 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1595 if (ip
== CPP_NULL_BUFFER (pfile
))
1597 if (ip
->fname
!= NULL
)
1601 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1602 cpp_warning (pfile
, "`#pragma once' outside include file");
1604 ip
->ihash
->control_macro
= ""; /* never repeat */
1606 else if (!strncmp (buf
, "implementation", 14))
1608 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1609 been included yet. */
1610 struct include_hash
*ptr
;
1611 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1612 SKIP_WHITE_SPACE (p
);
1613 if (*p
== '\n' || *p
!= '\"')
1617 p
= (U_CHAR
*) index (fname
, '\"');
1619 fcopy
= (U_CHAR
*) alloca (p
- fname
+ 1);
1620 bcopy (fname
, fcopy
, p
- fname
);
1621 fcopy
[p
-fname
] = '\0';
1623 ptr
= include_hash (pfile
, fcopy
, 0);
1626 "`#pragma implementation' for `%s' appears after file is included",
1629 else if (!strncmp (buf
, "poison", 6))
1631 /* Poison these symbols so that all subsequent usage produces an
1633 U_CHAR
*p
= buf
+ 6;
1638 SKIP_WHITE_SPACE (p
);
1639 plen
= strlen(p
) + 1;
1641 syms
= (U_CHAR
*) alloca (plen
);
1642 memcpy (syms
, p
, plen
);
1644 /* As a rule, don't include #pragma poison commands in output,
1645 unless the user asks for them. */
1646 writeit
= (CPP_OPTIONS (pfile
)->debug_output
1647 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
1648 || CPP_OPTIONS (pfile
)->dump_macros
== dump_names
);
1651 CPP_SET_WRITTEN (pfile
, here
);
1653 CPP_SET_WRITTEN (pfile
, here
-8);
1657 CPP_RESERVE (pfile
, plen
+ 7);
1658 CPP_PUTS_Q (pfile
, "poison", 7);
1661 while (*syms
!= '\0')
1665 while (is_idchar(*end
))
1668 if (!is_hspace(*end
) && *end
!= '\0')
1670 cpp_error (pfile
, "invalid #pragma poison directive");
1674 if (cpp_push_buffer (pfile
, syms
, end
- syms
) != NULL
)
1676 do_define (pfile
, keyword
);
1677 cpp_pop_buffer (pfile
);
1681 CPP_PUTC_Q (pfile
, ' ');
1682 CPP_PUTS_Q (pfile
, syms
, end
- syms
);
1685 SKIP_WHITE_SPACE (syms
);
1692 #ifdef SCCS_DIRECTIVE
1693 /* Just ignore #sccs, on systems where we define it at all. */
1696 do_sccs (pfile
, keyword
)
1698 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1700 if (CPP_PEDANTIC (pfile
))
1701 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1702 skip_rest_of_line (pfile
);
1708 /* We've found an `#if' directive. If the only thing before it in
1709 this file is white space, and if it is of the form
1710 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1711 for inclusion of this file. (See redundant_include_p in cppfiles.c
1712 for an explanation of controlling macros.) If so, return a
1713 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1716 detect_if_not_defined (pfile
)
1719 U_CHAR
*control_macro
= 0;
1721 if (pfile
->only_seen_white
== 2)
1724 enum cpp_token token
;
1727 int need_rparen
= 0;
1729 /* Save state required for restore. */
1730 pfile
->no_macro_expand
++;
1731 parse_set_mark (pfile
);
1732 base_offset
= CPP_WRITTEN (pfile
);
1735 if (get_directive_token (pfile
) != CPP_OTHER
1736 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1737 || CPP_PWRITTEN (pfile
)[-1] != '!')
1740 /* ...then `defined', */
1741 token_offset
= CPP_WRITTEN (pfile
);
1742 token
= get_directive_token (pfile
);
1743 if (token
!= CPP_NAME
)
1745 ident
= pfile
->token_buffer
+ token_offset
;
1746 CPP_NUL_TERMINATE (pfile
);
1747 if (strcmp (ident
, "defined"))
1750 /* ...then an optional '(' and the name, */
1751 token_offset
= CPP_WRITTEN (pfile
);
1752 token
= get_directive_token (pfile
);
1753 if (token
== CPP_LPAREN
)
1755 token_offset
= CPP_WRITTEN (pfile
);
1756 token
= get_directive_token (pfile
);
1757 if (token
!= CPP_NAME
)
1761 else if (token
!= CPP_NAME
)
1764 ident
= pfile
->token_buffer
+ token_offset
;
1765 CPP_NUL_TERMINATE (pfile
);
1767 /* ...then the ')', if necessary, */
1768 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1769 /* ...and make sure there's nothing else on the line. */
1770 && get_directive_token (pfile
) == CPP_VSPACE
)
1771 control_macro
= xstrdup (ident
);
1774 CPP_SET_WRITTEN (pfile
, base_offset
);
1775 pfile
->no_macro_expand
--;
1776 parse_goto_mark (pfile
);
1779 return control_macro
;
1783 * handle #if command by
1784 * 1) inserting special `defined' keyword into the hash table
1785 * that gets turned into 0 or 1 by special_symbol (thus,
1786 * if the luser has a symbol called `defined' already, it won't
1787 * work inside the #if command)
1788 * 2) rescan the input into a temporary output buffer
1789 * 3) pass the output buffer to the yacc parser and collect a value
1790 * 4) clean up the mess left from steps 1 and 2.
1791 * 5) call conditional_skip to skip til the next #endif (etc.),
1792 * or not, depending on the value from step 3.
1796 do_if (pfile
, keyword
)
1798 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1800 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1801 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1802 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1807 * handle a #elif directive by not changing if_stack either.
1808 * see the comment above do_else.
1812 do_elif (pfile
, keyword
)
1814 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1816 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1818 cpp_error (pfile
, "`#elif' not within a conditional");
1823 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
1825 cpp_error (pfile
, "`#elif' after `#else'");
1826 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1827 "the conditional began here");
1829 pfile
->if_stack
->type
= T_ELIF
;
1832 if (pfile
->if_stack
->if_succeeded
)
1833 skip_if_group (pfile
);
1836 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1838 skip_if_group (pfile
);
1841 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1842 output_line_command (pfile
, same_file
);
1849 * evaluate a #if expression in BUF, of length LENGTH,
1850 * then parse the result as a C expression and return the value as an int.
1853 static HOST_WIDEST_INT
1854 eval_if_expression (pfile
)
1857 HOST_WIDEST_INT value
;
1858 long old_written
= CPP_WRITTEN (pfile
);
1860 value
= cpp_parse_expr (pfile
);
1862 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1868 * routine to handle ifdef/ifndef. Try to look up the symbol,
1869 * then do or don't skip to the #endif/#else/#elif depending
1870 * on what directive is actually being processed.
1874 do_xifdef (pfile
, keyword
)
1876 const struct directive
*keyword
;
1879 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1882 enum cpp_token token
;
1883 int start_of_file
= 0;
1884 U_CHAR
*control_macro
= 0;
1885 int old_written
= CPP_WRITTEN (pfile
);
1887 /* Detect a #ifndef at start of file (not counting comments). */
1888 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1889 start_of_file
= pfile
->only_seen_white
== 2;
1891 pfile
->no_macro_expand
++;
1892 token
= get_directive_token (pfile
);
1893 pfile
->no_macro_expand
--;
1895 ident
= pfile
->token_buffer
+ old_written
;
1896 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1897 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1899 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1901 skip
= (keyword
->type
== T_IFDEF
);
1902 if (! CPP_TRADITIONAL (pfile
))
1903 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1905 else if (token
== CPP_NAME
)
1907 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1908 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1909 if (start_of_file
&& !skip
)
1911 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1912 bcopy (ident
, control_macro
, ident_length
+ 1);
1914 if (hp
!= NULL
&& hp
->type
== T_POISON
)
1916 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1922 skip
= (keyword
->type
== T_IFDEF
);
1923 if (! CPP_TRADITIONAL (pfile
))
1924 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1927 if (!CPP_TRADITIONAL (pfile
))
1929 cpp_skip_hspace (pfile
);
1931 if (c
!= EOF
&& c
!= '\n')
1932 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1934 skip_rest_of_line (pfile
);
1936 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1940 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1941 If this is a #ifndef starting at the beginning of a file,
1942 CONTROL_MACRO is the macro name tested by the #ifndef.
1943 Otherwise, CONTROL_MACRO is 0. */
1946 conditional_skip (pfile
, skip
, type
, control_macro
)
1949 enum node_type type
;
1950 U_CHAR
*control_macro
;
1952 IF_STACK_FRAME
*temp
;
1954 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1955 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1956 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1957 temp
->next
= pfile
->if_stack
;
1958 temp
->control_macro
= control_macro
;
1959 pfile
->if_stack
= temp
;
1961 pfile
->if_stack
->type
= type
;
1964 skip_if_group (pfile
);
1967 ++pfile
->if_stack
->if_succeeded
;
1968 output_line_command (pfile
, same_file
);
1972 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1973 return 0 if skipping should continue, 1 if it should halt. Also
1974 adjusts the if_stack as appropriate.
1975 The `#' has been read, but not the identifier. */
1978 consider_directive_while_skipping (pfile
, stack
)
1980 IF_STACK_FRAME
*stack
;
1982 long ident_len
, ident
;
1983 const struct directive
*kt
;
1984 IF_STACK_FRAME
*temp
;
1986 cpp_skip_hspace (pfile
);
1988 ident
= CPP_WRITTEN (pfile
);
1989 parse_name (pfile
, GETC());
1990 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1992 CPP_SET_WRITTEN (pfile
, ident
);
1994 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1995 if (kt
->length
== ident_len
1996 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
2002 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
2003 temp
->next
= pfile
->if_stack
;
2004 pfile
->if_stack
= temp
;
2005 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
2006 temp
->type
= kt
->type
;
2010 if (pfile
->if_stack
!= stack
)
2011 validate_else (pfile
, "#else");
2014 if (pfile
->if_stack
== stack
)
2018 pfile
->if_stack
->type
= kt
->type
;
2023 if (pfile
->if_stack
!= stack
)
2024 validate_else (pfile
, "#endif");
2026 if (pfile
->if_stack
== stack
)
2029 temp
= pfile
->if_stack
;
2030 pfile
->if_stack
= temp
->next
;
2038 /* Don't let erroneous code go by. */
2039 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
2040 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
2044 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2045 * leaves input ptr at the sharp sign found.
2048 skip_if_group (pfile
)
2052 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
2053 U_CHAR
*beg_of_line
;
2056 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2058 CPP_PUTS (pfile
, "#failed\n", 8);
2060 output_line_command (pfile
, same_file
);
2063 old_written
= CPP_WRITTEN (pfile
);
2067 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
2069 if (! CPP_TRADITIONAL (pfile
))
2070 cpp_skip_hspace (pfile
);
2074 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2075 CPP_PUTC (pfile
, c
);
2076 CPP_BUMP_LINE (pfile
);
2081 if (consider_directive_while_skipping (pfile
, save_if_stack
))
2085 return; /* Caller will issue error. */
2088 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2090 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
2091 copy_rest_of_line (pfile
);
2095 copy_rest_of_line (pfile
);
2096 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
2101 return; /* Caller will issue error. */
2105 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2107 CPP_PUTC (pfile
, c
);
2110 CPP_BUMP_LINE (pfile
);
2114 /* Back up to the beginning of this line. Caller will process the
2116 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
2117 pfile
->only_seen_white
= 1;
2118 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2120 CPP_PUTS (pfile
, "#endfailed\n", 11);
2126 * handle a #else directive. Do this by just continuing processing
2127 * without changing if_stack ; this is so that the error message
2128 * for missing #endif's etc. will point to the original #if. It
2129 * is possible that something different would be better.
2133 do_else (pfile
, keyword
)
2135 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2137 validate_else (pfile
, "#else");
2138 skip_rest_of_line (pfile
);
2140 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2142 cpp_error (pfile
, "`#else' not within a conditional");
2147 /* #ifndef can't have its special treatment for containing the whole file
2148 if it has a #else clause. */
2149 pfile
->if_stack
->control_macro
= 0;
2151 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
2153 cpp_error (pfile
, "`#else' after `#else'");
2154 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
2155 "the conditional began here");
2157 pfile
->if_stack
->type
= T_ELSE
;
2160 if (pfile
->if_stack
->if_succeeded
)
2161 skip_if_group (pfile
);
2164 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
2165 output_line_command (pfile
, same_file
);
2171 * unstack after #endif command
2175 do_endif (pfile
, keyword
)
2177 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2179 validate_else (pfile
, "#endif");
2180 skip_rest_of_line (pfile
);
2182 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2183 cpp_error (pfile
, "`#endif' not within a conditional");
2186 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2187 pfile
->if_stack
= temp
->next
;
2188 if (temp
->control_macro
!= 0)
2190 /* This #endif matched a #ifndef at the start of the file.
2191 See if it is at the end of the file. */
2194 parse_set_mark (pfile
);
2198 cpp_skip_hspace (pfile
);
2203 parse_goto_mark (pfile
);
2207 /* This #endif ends a #ifndef
2208 that contains all of the file (aside from whitespace).
2209 Arrange not to include the file again
2210 if the macro that was tested is defined. */
2211 struct cpp_buffer
*ip
;
2212 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2213 if (ip
->fname
!= NULL
)
2215 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2219 output_line_command (pfile
, same_file
);
2224 /* Issue -pedantic warning for text which is not a comment following
2225 an #else or #endif. Do not warn in system headers, as this is harmless
2226 and very common on old systems. */
2229 validate_else (pfile
, directive
)
2231 const char *directive
;
2233 if (! CPP_PEDANTIC (pfile
) || CPP_BUFFER (pfile
)->system_header_p
)
2236 cpp_skip_hspace (pfile
);
2237 if (PEEKC () != '\n')
2239 "text following `%s' violates ANSI standard", directive
);
2242 /* Convert T_IF, etc. to a string. Used in error messages. */
2244 if_directive_name (pfile
, ifs
)
2246 struct if_stack
*ifs
;
2250 case T_IF
: return "#if";
2251 case T_IFDEF
: return "#ifdef";
2252 case T_IFNDEF
: return "#ifndef";
2253 case T_ELIF
: return "#elif";
2254 case T_ELSE
: return "#else";
2256 cpp_fatal (pfile
, "impossible if_stack->type value %d", ifs
->type
);
2261 /* Get the next token, and add it to the text in pfile->token_buffer.
2262 Return the kind of token we got. */
2265 cpp_get_token (pfile
)
2268 register int c
, c2
, c3
;
2269 enum cpp_token token
;
2270 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2276 if (CPP_BUFFER (pfile
)->manual_pop
)
2277 /* If we've been reading from redirected input, the
2278 frontend will pop the buffer. */
2280 else if (CPP_BUFFER (pfile
)->seen_eof
)
2282 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2285 cpp_pop_buffer (pfile
);
2290 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2291 struct if_stack
*ifs
, *nifs
;
2293 /* Unwind the conditional stack and generate error messages. */
2294 for (ifs
= pfile
->if_stack
;
2295 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
2298 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
2299 "unterminated `%s' conditional",
2300 if_directive_name (pfile
, ifs
));
2305 pfile
->if_stack
= ifs
;
2307 if (CPP_BUFFER (pfile
)->nominal_fname
2308 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2310 /* We're about to return from an #include file.
2311 Emit #line information now (as part of the CPP_POP) result.
2312 But the #line refers to the file we will pop to. */
2313 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2314 CPP_BUFFER (pfile
) = next_buf
;
2315 pfile
->input_stack_listing_current
= 0;
2316 output_line_command (pfile
, leave_file
);
2317 CPP_BUFFER (pfile
) = cur_buffer
;
2320 CPP_BUFFER (pfile
)->seen_eof
= 1;
2329 if (PEEKC () == '=')
2333 if (opts
->put_out_comments
)
2334 c
= copy_comment (pfile
, c
);
2336 c
= skip_comment (pfile
, c
);
2340 /* Comments are equivalent to spaces.
2341 For -traditional, a comment is equivalent to nothing. */
2342 if (opts
->traditional
|| opts
->put_out_comments
)
2346 CPP_PUTC (pfile
, c
);
2351 if (!pfile
->only_seen_white
)
2353 /* -traditional directives are recognized only with the # in
2355 XXX Layering violation. */
2356 if (CPP_TRADITIONAL (pfile
)
2357 && CPP_BUFFER (pfile
)->cur
- CPP_BUFFER (pfile
)->line_base
!= 1)
2359 if (handle_directive (pfile
))
2360 return CPP_DIRECTIVE
;
2361 pfile
->only_seen_white
= 0;
2367 parse_string (pfile
, c
);
2368 pfile
->only_seen_white
= 0;
2369 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2372 if (!opts
->dollars_in_ident
)
2377 if (opts
->cplusplus
&& PEEKC () == ':')
2385 if (c2
== c
|| c2
== '=')
2394 if (PEEKC () == '=')
2400 if (c2
== '-' && opts
->chill
)
2401 goto comment
; /* Chill style comment */
2402 if (c2
== '-' || c2
== '=')
2406 if (opts
->cplusplus
&& PEEKN (1) == '*')
2408 /* In C++, there's a ->* operator. */
2410 pfile
->only_seen_white
= 0;
2411 CPP_RESERVE (pfile
, 4);
2412 CPP_PUTC_Q (pfile
, c
);
2413 CPP_PUTC_Q (pfile
, GETC ());
2414 CPP_PUTC_Q (pfile
, GETC ());
2415 CPP_NUL_TERMINATE_Q (pfile
);
2423 if (pfile
->parsing_include_directive
)
2427 CPP_PUTC (pfile
, c
);
2431 if (c
== '\n' || c
== EOF
)
2434 "missing '>' in `#include <FILENAME>'");
2439 if (!CPP_BUFFER (pfile
)->has_escapes
)
2441 /* Backslash newline is replaced by nothing. */
2442 CPP_ADJUST_WRITTEN (pfile
, -1);
2443 CPP_BUMP_LINE (pfile
);
2447 /* We might conceivably get \r- or \r<space> in
2448 here. Just delete 'em. */
2450 if (d
!= '-' && d
!= ' ')
2452 "internal error: unrecognized escape \\r%c",
2454 CPP_ADJUST_WRITTEN (pfile
, -1);
2460 /* else fall through */
2465 /* GNU C++ supports MIN and MAX operators <? and >?. */
2466 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2469 CPP_RESERVE (pfile
, 4);
2470 CPP_PUTC (pfile
, c
);
2471 CPP_PUTC (pfile
, c2
);
2474 CPP_PUTC_Q (pfile
, GETC ());
2475 CPP_NUL_TERMINATE_Q (pfile
);
2476 pfile
->only_seen_white
= 0;
2483 CPP_RESERVE(pfile
, 2);
2484 CPP_PUTC_Q (pfile
, '.');
2489 /* In C++ there's a .* operator. */
2490 if (opts
->cplusplus
&& c2
== '*')
2493 if (c2
== '.' && PEEKN(1) == '.')
2495 CPP_RESERVE(pfile
, 4);
2496 CPP_PUTC_Q (pfile
, '.');
2497 CPP_PUTC_Q (pfile
, '.');
2498 CPP_PUTC_Q (pfile
, '.');
2500 CPP_NUL_TERMINATE_Q (pfile
);
2501 pfile
->only_seen_white
= 0;
2508 pfile
->only_seen_white
= 0;
2509 CPP_RESERVE(pfile
, 3);
2510 CPP_PUTC_Q (pfile
, c
);
2511 CPP_PUTC_Q (pfile
, GETC ());
2512 CPP_NUL_TERMINATE_Q (pfile
);
2517 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2519 CPP_PUTC (pfile
, c
);
2525 case '0': case '1': case '2': case '3': case '4':
2526 case '5': case '6': case '7': case '8': case '9':
2531 CPP_RESERVE (pfile
, 2);
2532 CPP_PUTC_Q (pfile
, c
);
2536 if (!is_idchar(c
) && c
!= '.'
2537 && ((c2
!= 'e' && c2
!= 'E'
2538 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2539 || (c
!= '+' && c
!= '-')))
2544 CPP_NUL_TERMINATE_Q (pfile
);
2545 pfile
->only_seen_white
= 0;
2547 case 'b': case 'c': case 'd': case 'h': case 'o':
2548 case 'B': case 'C': case 'D': case 'H': case 'O':
2549 if (opts
->chill
&& PEEKC () == '\'')
2551 pfile
->only_seen_white
= 0;
2552 CPP_RESERVE (pfile
, 2);
2553 CPP_PUTC_Q (pfile
, c
);
2554 CPP_PUTC_Q (pfile
, '\'');
2560 goto chill_number_eof
;
2563 CPP_PUTC (pfile
, c
);
2567 CPP_RESERVE (pfile
, 2);
2568 CPP_PUTC_Q (pfile
, c
);
2569 CPP_NUL_TERMINATE_Q (pfile
);
2576 CPP_NUL_TERMINATE (pfile
);
2583 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2584 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2585 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2586 case 'x': case 'y': case 'z':
2587 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2588 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2589 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2594 unsigned char *ident
;
2595 int before_name_written
= CPP_WRITTEN (pfile
);
2597 parse_name (pfile
, c
);
2598 pfile
->only_seen_white
= 0;
2599 if (pfile
->no_macro_expand
)
2601 ident
= pfile
->token_buffer
+ before_name_written
;
2602 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2603 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2606 if (hp
->type
== T_DISABLED
)
2608 if (pfile
->output_escapes
)
2609 { /* Return "\r-IDENT", followed by '\0'. */
2611 CPP_RESERVE (pfile
, 3);
2612 ident
= pfile
->token_buffer
+ before_name_written
;
2613 CPP_ADJUST_WRITTEN (pfile
, 2);
2614 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2621 /* If macro wants an arglist, verify that a '(' follows.
2622 first skip all whitespace, copying it to the output
2623 after the macro name. Then, if there is no '(',
2624 decide this is not a macro call and leave things that way. */
2625 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2627 int is_macro_call
, macbuf_whitespace
= 0;
2629 parse_set_mark (pfile
);
2632 cpp_skip_hspace (pfile
);
2634 is_macro_call
= c
== '(';
2643 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2645 if (CPP_BUFFER (pfile
)->mark
!=
2646 (CPP_BUFFER (pfile
)->cur
2647 - CPP_BUFFER (pfile
)->buf
))
2648 macbuf_whitespace
= 1;
2650 /* The mark goes away automatically when
2651 the buffer is popped. */
2652 cpp_pop_buffer (pfile
);
2653 parse_set_mark (pfile
);
2661 parse_goto_mark (pfile
);
2662 if (macbuf_whitespace
)
2663 CPP_PUTC (pfile
, ' ');
2666 parse_clear_mark (pfile
);
2670 /* This is now known to be a macro call.
2671 Expand the macro, reading arguments as needed,
2672 and push the expansion on the input stack. */
2673 macroexpand (pfile
, hp
);
2674 CPP_SET_WRITTEN (pfile
, before_name_written
);
2678 case ' ': case '\t': case '\v':
2681 CPP_PUTC (pfile
, c
);
2683 if (c
== EOF
|| !is_hspace(c
))
2690 if (CPP_BUFFER (pfile
)->has_escapes
)
2695 if (pfile
->output_escapes
)
2696 CPP_PUTS (pfile
, "\r-", 2);
2697 parse_name (pfile
, GETC ());
2702 CPP_RESERVE (pfile
, 2);
2703 if (pfile
->output_escapes
)
2704 CPP_PUTC_Q (pfile
, '\r');
2705 CPP_PUTC_Q (pfile
, c
);
2711 "internal error: unrecognized escape \\r%c", c
);
2717 /* Backslash newline is ignored. */
2718 CPP_BUMP_LINE (pfile
);
2723 CPP_PUTC (pfile
, c
);
2724 if (pfile
->only_seen_white
== 0)
2725 pfile
->only_seen_white
= 1;
2726 CPP_BUMP_LINE (pfile
);
2727 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2730 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2731 output_line_command (pfile
, same_file
);
2735 case '(': token
= CPP_LPAREN
; goto char1
;
2736 case ')': token
= CPP_RPAREN
; goto char1
;
2737 case '{': token
= CPP_LBRACE
; goto char1
;
2738 case '}': token
= CPP_RBRACE
; goto char1
;
2739 case ',': token
= CPP_COMMA
; goto char1
;
2740 case ';': token
= CPP_SEMICOLON
; goto char1
;
2746 pfile
->only_seen_white
= 0;
2747 CPP_PUTC (pfile
, c
);
2753 /* Like cpp_get_token, but skip spaces and comments. */
2756 cpp_get_non_space_token (pfile
)
2759 int old_written
= CPP_WRITTEN (pfile
);
2762 enum cpp_token token
= cpp_get_token (pfile
);
2763 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2764 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2766 CPP_SET_WRITTEN (pfile
, old_written
);
2770 /* Parse an identifier starting with C. */
2773 parse_name (pfile
, c
)
2785 if (c
== '$' && CPP_PEDANTIC (pfile
))
2786 cpp_pedwarn (pfile
, "`$' in identifier");
2788 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2789 CPP_PUTC_Q (pfile
, c
);
2794 CPP_NUL_TERMINATE_Q (pfile
);
2798 /* Parse a string starting with C. A single quoted string is treated
2799 like a double -- some programs (e.g., troff) are perverse this way.
2800 (However, a single quoted string is not allowed to extend over
2803 parse_string (pfile
, c
)
2807 long start_line
, start_column
;
2809 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2811 CPP_PUTC (pfile
, c
);
2817 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2819 /* try harder: this string crosses a macro expansion
2820 boundary. This can happen naturally if -traditional.
2821 Otherwise, only -D can make a macro with an unmatched
2823 cpp_pop_buffer (pfile
);
2827 cpp_error_with_line (pfile
, start_line
, start_column
,
2828 "unterminated string or character constant");
2829 if (pfile
->multiline_string_line
!= start_line
2830 && pfile
->multiline_string_line
!= 0)
2831 cpp_error_with_line (pfile
,
2832 pfile
->multiline_string_line
, -1,
2833 "possible real start of unterminated constant");
2834 pfile
->multiline_string_line
= 0;
2837 CPP_PUTC (pfile
, cc
);
2841 CPP_BUMP_LINE (pfile
);
2844 /* In Fortran and assembly language, silently terminate
2845 strings of either variety at end of line. This is a
2846 kludge around not knowing where comments are in these
2848 if (CPP_OPTIONS (pfile
)->lang_fortran
2849 || CPP_OPTIONS (pfile
)->lang_asm
)
2851 /* Character constants may not extend over multiple lines.
2852 In Standard C, neither may strings. We accept multiline
2853 strings as an extension. */
2856 cpp_error_with_line (pfile
, start_line
, start_column
,
2857 "unterminated character constant");
2860 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2861 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2862 "string constant runs past end of line");
2863 if (pfile
->multiline_string_line
== 0)
2864 pfile
->multiline_string_line
= start_line
;
2868 CPP_ADJUST_WRITTEN (pfile
, -1);
2869 if (CPP_BUFFER (pfile
)->has_escapes
)
2872 "internal error: \\r escape inside string constant");
2876 /* Backslash newline is replaced by nothing at all. */
2877 CPP_BUMP_LINE (pfile
);
2883 CPP_PUTC (pfile
, cc
);
2895 /* Read an assertion into the token buffer, converting to
2896 canonical form: `#predicate(a n swe r)' The next non-whitespace
2897 character to read should be the first letter of the predicate.
2898 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2899 with answer (see callers for why). In case of 0, an error has been
2902 parse_assertion (pfile
)
2906 cpp_skip_hspace (pfile
);
2908 if (! is_idstart(c
))
2910 cpp_error (pfile
, "assertion predicate is not an identifier");
2913 CPP_PUTC(pfile
, '#');
2915 parse_name(pfile
, c
);
2920 if (is_hspace(c
) || c
== '\r')
2921 cpp_skip_hspace (pfile
);
2927 CPP_PUTC(pfile
, '(');
2930 while ((c
= GETC()) != ')')
2936 CPP_PUTC(pfile
, ' ');
2940 else if (c
== '\n' || c
== EOF
)
2942 if (c
== '\n') FORWARD(-1);
2943 cpp_error (pfile
, "un-terminated assertion answer");
2947 /* \r cannot be a macro escape here. */
2948 CPP_BUMP_LINE (pfile
);
2951 CPP_PUTC (pfile
, c
);
2956 if (pfile
->limit
[-1] == ' ')
2957 pfile
->limit
[-1] = ')';
2958 else if (pfile
->limit
[-1] == '(')
2960 cpp_error (pfile
, "empty token sequence in assertion");
2964 CPP_PUTC (pfile
, ')');
2966 CPP_NUL_TERMINATE (pfile
);
2971 do_assert (pfile
, keyword
)
2973 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2977 HASHNODE
*base
, *this;
2978 int baselen
, thislen
;
2980 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2981 && !CPP_BUFFER (pfile
)->system_header_p
)
2982 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
2984 cpp_skip_hspace (pfile
);
2985 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2986 ret
= parse_assertion (pfile
);
2991 cpp_error (pfile
, "missing token-sequence in `#assert'");
2995 cpp_skip_hspace (pfile
);
2997 if (c
!= EOF
&& c
!= '\n')
2999 cpp_error (pfile
, "junk at end of `#assert'");
3003 thislen
= strlen (sym
);
3004 baselen
= index (sym
, '(') - sym
;
3005 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3008 cpp_warning (pfile
, "`%s' re-asserted", sym
);
3012 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3014 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
3015 else if (base
->type
!= T_ASSERT
)
3017 /* Token clash - but with what?! */
3019 "cpp internal error: base->type != T_ASSERT in do_assert");
3023 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
3024 (char *)base
->value
.aschain
, -1);
3025 base
->value
.aschain
= this;
3027 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3031 skip_rest_of_line (pfile
);
3032 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3037 do_unassert (pfile
, keyword
)
3039 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3043 long baselen
, thislen
;
3044 HASHNODE
*base
, *this, *next
;
3046 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3047 && !CPP_BUFFER (pfile
)->system_header_p
)
3048 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
3050 cpp_skip_hspace (pfile
);
3052 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3053 ret
= parse_assertion (pfile
);
3057 cpp_skip_hspace (pfile
);
3059 if (c
!= EOF
&& c
!= '\n')
3060 cpp_error (pfile
, "junk at end of `#unassert'");
3062 thislen
= strlen (sym
);
3065 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
3067 goto error
; /* It isn't an error to #undef what isn't #defined,
3068 so it isn't an error to #unassert what isn't
3069 #asserted either. */
3071 for (this = base
->value
.aschain
; this; this = next
)
3073 next
= this->value
.aschain
;
3074 delete_macro (this);
3076 delete_macro (base
);
3080 baselen
= index (sym
, '(') - sym
;
3081 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3082 if (! base
) goto error
;
3083 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3084 if (! this) goto error
;
3087 while (next
->value
.aschain
!= this)
3088 next
= next
->value
.aschain
;
3090 next
->value
.aschain
= this->value
.aschain
;
3091 delete_macro (this);
3093 if (base
->value
.aschain
== NULL
)
3094 delete_macro (base
); /* Last answer for this predicate deleted. */
3097 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3100 skip_rest_of_line (pfile
);
3101 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3105 /* Process STR as if it appeared as the body of an #unassert. */
3107 cpp_unassert (pfile
, str
)
3111 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3113 do_assert (pfile
, NULL
);
3114 cpp_pop_buffer (pfile
);
3119 cpp_read_check_assertion (pfile
)
3122 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
3126 FORWARD (1); /* Skip '#' */
3127 cpp_skip_hspace (pfile
);
3128 if (! parse_assertion (pfile
))
3132 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
3136 pfile
->limit
= name
;
3140 /* Remember the current position of PFILE. */
3143 parse_set_mark (pfile
)
3146 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3149 "cpp internal error: ip->mark != -1 in parse_set_mark");
3151 ip
->mark
= ip
->cur
- ip
->buf
;
3154 /* Clear the current mark - we no longer need it. */
3157 parse_clear_mark (pfile
)
3160 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3163 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3168 /* Backup the current position of PFILE to that saved in its mark,
3169 and clear the mark. */
3172 parse_goto_mark (pfile
)
3175 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3178 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3180 ip
->cur
= ip
->buf
+ ip
->mark
;
3185 cpp_print_file_and_line (pfile
)
3188 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3193 cpp_buf_line_and_col (ip
, &line
, &col
);
3194 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
3195 line
, pfile
->show_column
? col
: -1);
3200 v_cpp_error (pfile
, msgid
, ap
)
3205 cpp_print_containing_files (pfile
);
3206 cpp_print_file_and_line (pfile
);
3207 v_cpp_message (pfile
, 1, msgid
, ap
);
3211 cpp_error
VPARAMS ((cpp_reader
* pfile
, const char *msgid
, ...))
3213 #ifndef ANSI_PROTOTYPES
3219 VA_START(ap
, msgid
);
3221 #ifndef ANSI_PROTOTYPES
3222 pfile
= va_arg (ap
, cpp_reader
*);
3223 msgid
= va_arg (ap
, const char *);
3226 v_cpp_error (pfile
, msgid
, ap
);
3230 /* Print error message but don't count it. */
3233 v_cpp_warning (pfile
, msgid
, ap
)
3238 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3241 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3244 cpp_print_containing_files (pfile
);
3245 cpp_print_file_and_line (pfile
);
3246 v_cpp_message (pfile
, 0, msgid
, ap
);
3250 cpp_warning
VPARAMS ((cpp_reader
* pfile
, const char *msgid
, ...))
3252 #ifndef ANSI_PROTOTYPES
3258 VA_START (ap
, msgid
);
3260 #ifndef ANSI_PROTOTYPES
3261 pfile
= va_arg (ap
, cpp_reader
*);
3262 msgid
= va_arg (ap
, const char *);
3265 v_cpp_warning (pfile
, msgid
, ap
);
3269 /* Print an error message and maybe count it. */
3272 cpp_pedwarn
VPARAMS ((cpp_reader
* pfile
, const char *msgid
, ...))
3274 #ifndef ANSI_PROTOTYPES
3280 VA_START (ap
, msgid
);
3282 #ifndef ANSI_PROTOTYPES
3283 pfile
= va_arg (ap
, cpp_reader
*);
3284 msgid
= va_arg (ap
, const char *);
3287 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3288 v_cpp_error (pfile
, msgid
, ap
);
3290 v_cpp_warning (pfile
, msgid
, ap
);
3295 v_cpp_error_with_line (pfile
, line
, column
, msgid
, ap
)
3302 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3304 cpp_print_containing_files (pfile
);
3307 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3309 v_cpp_message (pfile
, 1, msgid
, ap
);
3313 cpp_error_with_line
VPARAMS ((cpp_reader
* pfile
, int line
, int column
,
3314 const char *msgid
, ...))
3316 #ifndef ANSI_PROTOTYPES
3324 VA_START (ap
, msgid
);
3326 #ifndef ANSI_PROTOTYPES
3327 pfile
= va_arg (ap
, cpp_reader
*);
3328 line
= va_arg (ap
, int);
3329 column
= va_arg (ap
, int);
3330 msgid
= va_arg (ap
, const char *);
3333 v_cpp_error_with_line(pfile
, line
, column
, msgid
, ap
);
3338 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
)
3347 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3350 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3353 cpp_print_containing_files (pfile
);
3355 ip
= cpp_file_buffer (pfile
);
3358 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3360 v_cpp_message (pfile
, 0, msgid
, ap
);
3364 cpp_warning_with_line
VPARAMS ((cpp_reader
* pfile
, int line
, int column
,
3365 const char *msgid
, ...))
3367 #ifndef ANSI_PROTOTYPES
3375 VA_START (ap
, msgid
);
3377 #ifndef ANSI_PROTOTYPES
3378 pfile
= va_arg (ap
, cpp_reader
*);
3379 line
= va_arg (ap
, int);
3380 column
= va_arg (ap
, int);
3381 msgid
= va_arg (ap
, const char *);
3384 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3389 cpp_pedwarn_with_line
VPARAMS ((cpp_reader
* pfile
, int line
, int column
,
3390 const char *msgid
, ...))
3392 #ifndef ANSI_PROTOTYPES
3400 VA_START (ap
, msgid
);
3402 #ifndef ANSI_PROTOTYPES
3403 pfile
= va_arg (ap
, cpp_reader
*);
3404 line
= va_arg (ap
, int);
3405 column
= va_arg (ap
, int);
3406 msgid
= va_arg (ap
, const char *);
3409 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3410 v_cpp_error_with_line (pfile
, column
, line
, msgid
, ap
);
3412 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3416 /* Report a warning (or an error if pedantic_errors)
3417 giving specified file name and line number, not current. */
3420 cpp_pedwarn_with_file_and_line
VPARAMS ((cpp_reader
*pfile
, const char *file
,
3421 int line
, const char *msgid
, ...))
3423 #ifndef ANSI_PROTOTYPES
3431 VA_START (ap
, msgid
);
3433 #ifndef ANSI_PROTOTYPES
3434 pfile
= va_arg (ap
, cpp_reader
*);
3435 file
= va_arg (ap
, const char *);
3436 line
= va_arg (ap
, int);
3437 msgid
= va_arg (ap
, const char *);
3440 if (!CPP_OPTIONS (pfile
)->pedantic_errors
3441 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
3443 cpp_file_line_for_message (pfile
, file
, line
, -1);
3444 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msgid
, ap
);
3448 /* my_strerror - return the descriptive text associated with an
3452 my_strerror (errnum
)
3458 #ifndef HAVE_STRERROR
3459 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
3461 result
= strerror (errnum
);
3464 /* VAXCRTL's strerror() takes an optional second argument, which only
3465 matters when the first argument is EVMSERR. However, it's simplest
3466 just to pass it unconditionally. `vaxc$errno' is declared in
3467 <errno.h>, and maintained by the library in parallel with `errno'.
3468 We assume that caller's `errnum' either matches the last setting of
3469 `errno' by the library or else does not have the value `EVMSERR'. */
3471 result
= strerror (errnum
, vaxc$errno
);
3475 result
= "errno = ?";
3480 /* Error including a message from `errno'. */
3483 cpp_error_from_errno (pfile
, name
)
3487 cpp_message_from_errno (pfile
, 1, name
);
3491 cpp_message_from_errno (pfile
, is_error
, name
)
3497 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3499 cpp_print_containing_files (pfile
);
3502 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
3504 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
3508 cpp_perror_with_name (pfile
, name
)
3512 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));