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
*));
52 static const char *if_directive_name
PARAMS ((cpp_reader
*,
55 /* External declarations. */
57 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
59 /* `struct directive' defines one #-directive, including how to handle it. */
62 int length
; /* Length of name */
63 int (*func
) /* Function to handle directive */
64 PARAMS ((cpp_reader
*, const struct directive
*));
65 const char *name
; /* Name of directive */
66 enum node_type type
; /* Code which describes which directive. */
69 /* These functions are declared to return int instead of void since they
70 are going to be placed in a table and some old compilers have trouble with
71 pointers to functions returning void. */
73 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
74 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
75 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
76 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
77 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
78 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
79 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
80 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
81 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
82 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
83 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
84 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
86 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
88 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
89 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
90 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
91 static enum cpp_token null_underflow
PARAMS ((cpp_reader
*));
92 static int null_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
93 static int skip_comment
PARAMS ((cpp_reader
*, int));
94 static int copy_comment
PARAMS ((cpp_reader
*, int));
95 static void copy_rest_of_line
PARAMS ((cpp_reader
*));
96 static int handle_directive
PARAMS ((cpp_reader
*));
97 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t, cpp_reader
*,
98 const struct directive
*));
99 static enum cpp_token get_directive_token
PARAMS ((cpp_reader
*));
100 static int read_line_number
PARAMS ((cpp_reader
*, int *));
101 static void cpp_print_file_and_line
PARAMS ((cpp_reader
*));
102 static void v_cpp_error
PARAMS ((cpp_reader
*, const char *, va_list));
103 static void v_cpp_warning
PARAMS ((cpp_reader
*, const char *, va_list));
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, const char *, va_list));
107 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
108 static int consider_directive_while_skipping
PARAMS ((cpp_reader
*, IF_STACK_FRAME
*));
110 /* Here is the actual list of #-directives.
111 This table is ordered by frequency of occurrence; the numbers
112 at the end are directive counts from all the source code I have
113 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
114 linux-2.2.9, and pcmcia-cs-3.0.9). */
116 static const struct directive directive_table
[] = {
118 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
119 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
120 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
121 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
122 { 2, do_if
, "if", T_IF
}, /* 18162 */
123 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
124 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
125 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
126 { 4, do_line
, "line", T_LINE
}, /* 2465 */
127 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
128 { 5, do_error
, "error", T_ERROR
}, /* 475 */
129 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
131 /* Extensions. All deprecated except #warning and #include_next. */
132 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
133 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
134 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
135 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
136 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
137 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
138 #ifdef SCCS_DIRECTIVE
139 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
141 { -1, 0, "", T_UNUSED
}
144 /* Place into PFILE a quoted string representing the string SRC.
145 Caller must reserve enough space in pfile->token_buffer. */
148 quote_string (pfile
, src
)
154 CPP_PUTC_Q (pfile
, '\"');
156 switch ((c
= *src
++))
160 CPP_PUTC_Q (pfile
, c
);
163 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
164 CPP_ADJUST_WRITTEN (pfile
, 4);
170 CPP_PUTC_Q (pfile
, '\\');
171 CPP_PUTC_Q (pfile
, c
);
175 CPP_PUTC_Q (pfile
, '\"');
176 CPP_NUL_TERMINATE_Q (pfile
);
181 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
184 cpp_grow_buffer (pfile
, n
)
188 long old_written
= CPP_WRITTEN (pfile
);
189 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
190 pfile
->token_buffer
= (U_CHAR
*)
191 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
192 CPP_SET_WRITTEN (pfile
, old_written
);
195 /* Process the string STR as if it appeared as the body of a #define
196 If STR is just an identifier, define it with value 1.
197 If STR has anything after the identifier, then it should
198 be identifier=definition. */
201 cpp_define (pfile
, str
)
208 /* Copy the entire option so we can modify it. */
209 count
= strlen (str
) + 3;
210 buf
= (U_CHAR
*) alloca (count
);
211 memcpy (buf
, str
, count
- 2);
212 /* Change the first "=" in the string to a space. If there is none,
213 tack " 1" on the end. */
214 p
= (U_CHAR
*) strchr (buf
, '=');
221 strcpy (&buf
[count
-3], " 1");
223 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
225 do_define (pfile
, NULL
);
226 cpp_pop_buffer (pfile
);
230 /* Process the string STR as if it appeared as the body of a #assert. */
232 cpp_assert (pfile
, str
)
236 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
238 do_assert (pfile
, NULL
);
239 cpp_pop_buffer (pfile
);
244 static enum cpp_token
245 null_underflow (pfile
)
246 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
252 null_cleanup (pbuf
, pfile
)
253 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
254 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
259 /* Skip a comment - C, C++, or Chill style. M is the first character
260 of the comment marker. If this really is a comment, skip to its
261 end and return ' '. If we hit end-of-file before end-of-comment,
262 return EOF. If this is not a comment, return M (which will be
266 skip_comment (pfile
, m
)
270 if (m
== '/' && PEEKC() == '*')
276 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
282 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
285 else if (c
== '\n' || c
== '\r')
286 /* \r cannot be a macro escape marker here. */
287 CPP_BUMP_LINE (pfile
);
288 else if (c
== '/' && prev_c
== '*')
290 else if (c
== '*' && prev_c
== '/'
291 && CPP_OPTIONS (pfile
)->warn_comments
)
292 cpp_warning (pfile
, "`/*' within comment");
297 else if ((m
== '/' && PEEKC() == '/'
298 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
299 || (m
== '-' && PEEKC() == '-'
300 && CPP_OPTIONS (pfile
)->chill
))
307 return ' '; /* Allow // to be terminated by EOF. */
310 /* Don't consider final '\n' to be part of comment. */
315 /* \r cannot be a macro escape marker here. */
316 CPP_BUMP_LINE (pfile
);
323 /* Identical to skip_comment except that it copies the comment into the
324 token_buffer. This is used if put_out_comments. */
326 copy_comment (pfile
, m
)
330 if (m
== '/' && PEEKC() == '*')
335 CPP_PUTC (pfile
, '/');
336 CPP_PUTC (pfile
, '*');
338 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
344 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
345 /* We must pretend this was a legitimate comment, so that the
346 output in token_buffer is not passed back tagged CPP_POP. */
351 /* \r cannot be a macro escape marker here. */
352 CPP_BUMP_LINE (pfile
);
360 CPP_BUMP_LINE (pfile
);
362 else if (c
== '/' && prev_c
== '*')
364 else if (c
== '*' && prev_c
== '/'
365 && CPP_OPTIONS (pfile
)->warn_comments
)
366 cpp_warning (pfile
, "`/*' within comment");
371 else if ((m
== '/' && PEEKC() == '/'
372 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
373 || (m
== '-' && PEEKC() == '-'
374 && CPP_OPTIONS (pfile
)->chill
))
383 return ' '; /* Allow line comments to be terminated by EOF. */
386 /* Don't consider final '\n' to be part of comment. */
391 /* \r cannot be a macro escape marker here. */
392 CPP_BUMP_LINE (pfile
);
402 /* Skip whitespace \-newline and comments. Does not macro-expand. */
405 cpp_skip_hspace (pfile
)
414 else if (is_hor_space
[c
])
416 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
417 cpp_pedwarn (pfile
, "%s in preprocessing directive",
418 c
== '\f' ? "formfeed" : "vertical tab");
422 /* \r is a backslash-newline marker if !has_escapes, and
423 a deletable-whitespace or no-reexpansion marker otherwise. */
424 if (CPP_BUFFER (pfile
)->has_escapes
)
432 CPP_BUFFER (pfile
)->lineno
++;
434 else if (c
== '/' || c
== '-')
436 c
= skip_comment (pfile
, c
);
448 /* Read the rest of the current line.
449 The line is appended to PFILE's output buffer. */
452 copy_rest_of_line (pfile
)
463 CPP_NUL_TERMINATE (pfile
);
467 if (CPP_BUFFER (pfile
)->has_escapes
)
471 CPP_BUFFER (pfile
)->lineno
++;
476 parse_string (pfile
, c
);
479 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile
))
481 CPP_PUTS (pfile
, "/**/", 4);
482 skip_comment (pfile
, c
);
485 /* else fall through */
487 c
= skip_comment (pfile
, c
);
492 if (CPP_PEDANTIC (pfile
))
493 cpp_pedwarn (pfile
, "%s in preprocessing directive",
494 c
== '\f' ? "formfeed" : "vertical tab");
502 /* FIXME: It is almost definitely a performance win to make this do
503 the scan itself. >75% of calls to copy_r_o_l are from here or
504 skip_if_group, which means the common case is to copy stuff into the
505 token_buffer only to discard it. */
507 skip_rest_of_line (pfile
)
510 long old
= CPP_WRITTEN (pfile
);
511 copy_rest_of_line (pfile
);
512 CPP_SET_WRITTEN (pfile
, old
);
515 /* Handle a possible # directive.
516 '#' has already been read. */
519 handle_directive (pfile
)
523 register const struct directive
*kt
;
526 long old_written
= CPP_WRITTEN (pfile
);
528 cpp_skip_hspace (pfile
);
531 if (c
>= '0' && c
<= '9')
533 /* Handle # followed by a line number. Complain about using that
534 form if we're being pedantic, but not if this is regurgitated
535 input (preprocessed or fed back in by the C++ frontend). */
536 if (CPP_PEDANTIC (pfile
)
537 && ! CPP_PREPROCESSED (pfile
)
538 && ! CPP_BUFFER (pfile
)->manual_pop
)
539 cpp_pedwarn (pfile
, "`#' followed by integer");
540 do_line (pfile
, NULL
);
544 /* Now find the directive name. */
545 CPP_PUTC (pfile
, '#');
546 parse_name (pfile
, GETC());
547 ident
= pfile
->token_buffer
+ old_written
+ 1;
548 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
549 if (ident_length
== 0)
551 /* A line of just `#' becomes blank. */
559 * Decode the keyword and call the appropriate expansion
560 * routine, after moving the input pointer up to the next line.
562 for (kt
= directive_table
; ; kt
++)
566 if (kt
->length
== ident_length
567 && !strncmp (kt
->name
, ident
, ident_length
))
571 CPP_SET_WRITTEN (pfile
, old_written
);
572 (*kt
->func
) (pfile
, kt
);
577 /* Pass a directive through to the output file.
578 BUF points to the contents of the directive, as a contiguous string.
579 LEN is the length of the string pointed to by BUF.
580 KEYWORD is the keyword-table entry for the directive. */
583 pass_thru_directive (buf
, len
, pfile
, keyword
)
587 const struct directive
*keyword
;
589 register unsigned keyword_length
= keyword
->length
;
591 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
592 CPP_PUTC_Q (pfile
, '#');
593 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
594 if (len
!= 0 && buf
[0] != ' ')
595 CPP_PUTC_Q (pfile
, ' ');
596 CPP_PUTS_Q (pfile
, buf
, len
);
599 /* Check a purported macro name SYMNAME, and yield its length.
600 ASSERTION is nonzero if this is really for an assertion name. */
603 check_macro_name (pfile
, symname
, assertion
)
605 const U_CHAR
*symname
;
611 for (p
= symname
; is_idchar
[*p
]; p
++)
613 sym_length
= p
- symname
;
615 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
617 assertion
? "invalid assertion name" : "invalid macro name");
618 else if (!is_idstart
[*symname
]
619 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
620 U_CHAR
*msg
; /* what pain... */
621 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
622 bcopy (symname
, msg
, sym_length
);
626 ? "invalid assertion name `%s'"
627 : "invalid macro name `%s'"),
633 /* Process a #define command.
634 KEYWORD is the keyword-table entry for #define,
635 or NULL for a "predefined" macro,
636 or the keyword-table entry for #pragma in the case of a #pragma poison. */
639 do_define (pfile
, keyword
)
641 const struct directive
*keyword
;
647 U_CHAR
*macro
, *buf
, *end
;
648 enum node_type new_type
;
650 here
= CPP_WRITTEN (pfile
);
651 copy_rest_of_line (pfile
);
653 if (keyword
== NULL
|| keyword
->type
== T_DEFINE
)
658 /* Copy out the line so we can pop the token buffer. */
659 buf
= pfile
->token_buffer
+ here
;
660 end
= CPP_PWRITTEN (pfile
);
661 macro
= (U_CHAR
*) alloca (end
- buf
+ 1);
662 bcopy (buf
, macro
, end
- buf
+ 1);
663 end
= macro
+ (end
- buf
);
665 CPP_SET_WRITTEN (pfile
, here
);
667 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
671 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
673 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
676 /* Redefining a precompiled key is ok. */
677 if (hp
->type
== T_PCSTRING
)
679 /* Redefining a poisoned identifier is even worse than `not ok'. */
680 else if (hp
->type
== T_POISON
)
682 /* Redefining a macro is ok if the definitions are the same. */
683 else if (hp
->type
== T_MACRO
)
684 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
685 /* Redefining a constant is ok with -D. */
686 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
687 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
688 /* Print the warning or error if it's not ok. */
691 if (hp
->type
== T_POISON
)
692 cpp_error (pfile
, "redefining poisoned `%.*s'",
693 mdef
.symlen
, mdef
.symnam
);
695 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
696 if (hp
->type
== T_MACRO
)
697 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
698 hp
->value
.defn
->line
,
699 "this is the location of the previous definition");
701 if (hp
->type
!= T_POISON
)
703 /* Replace the old definition. */
705 hp
->value
.defn
= mdef
.defn
;
709 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, new_type
,
710 (char *) mdef
.defn
, hashcode
);
712 if (keyword
!= NULL
&& keyword
->type
== T_DEFINE
)
714 if (CPP_OPTIONS (pfile
)->debug_output
715 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
716 dump_definition (pfile
, mdef
);
717 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
718 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
725 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
726 If BUFFER != NULL, then use the LENGTH characters in BUFFER
727 as the new input buffer.
728 Return the new buffer, or NULL on failure. */
731 cpp_push_buffer (pfile
, buffer
, length
)
736 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
738 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
740 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
744 new = (cpp_buffer
*) xcalloc (1, sizeof (cpp_buffer
));
746 new->if_stack
= pfile
->if_stack
;
747 new->cleanup
= null_cleanup
;
748 new->underflow
= null_underflow
;
749 new->buf
= new->cur
= buffer
;
750 new->alimit
= new->rlimit
= buffer
+ length
;
754 CPP_BUFFER (pfile
) = new;
759 cpp_pop_buffer (pfile
)
762 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
763 (*buf
->cleanup
) (buf
, pfile
);
764 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
766 pfile
->buffer_stack_depth
--;
767 return CPP_BUFFER (pfile
);
770 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
771 Pop the buffer when done. */
774 cpp_scan_buffer (pfile
)
777 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
778 enum cpp_token token
;
779 if (CPP_OPTIONS (pfile
)->no_output
)
781 long old_written
= CPP_WRITTEN (pfile
);
782 /* In no-output mode, we can ignore everything but directives. */
785 if (! pfile
->only_seen_white
)
786 skip_rest_of_line (pfile
);
787 token
= cpp_get_token (pfile
);
788 if (token
== CPP_EOF
) /* Should not happen ... */
790 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
792 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
793 != CPP_NULL_BUFFER (pfile
))
794 cpp_pop_buffer (pfile
);
798 CPP_SET_WRITTEN (pfile
, old_written
);
804 token
= cpp_get_token (pfile
);
805 if (token
== CPP_EOF
) /* Should not happen ... */
807 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
809 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
810 != CPP_NULL_BUFFER (pfile
))
811 cpp_pop_buffer (pfile
);
819 * Rescan a string (which may have escape marks) into pfile's buffer.
820 * Place the result in pfile->token_buffer.
822 * The input is copied before it is scanned, so it is safe to pass
823 * it something from the token_buffer that will get overwritten
824 * (because it follows CPP_WRITTEN). This is used by do_include.
828 cpp_expand_to_buffer (pfile
, buf
, length
)
833 register cpp_buffer
*ip
;
839 cpp_fatal (pfile
, "internal error: length < 0 in cpp_expand_to_buffer");
843 /* Set up the input on the input stack. */
845 buf1
= (U_CHAR
*) alloca (length
+ 1);
846 memcpy (buf1
, buf
, length
);
849 ip
= cpp_push_buffer (pfile
, buf1
, length
);
854 /* Scan the input, create the output. */
855 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
856 CPP_OPTIONS (pfile
)->no_output
= 0;
857 cpp_scan_buffer (pfile
);
858 CPP_OPTIONS (pfile
)->no_output
= save_no_output
;
860 CPP_NUL_TERMINATE (pfile
);
864 cpp_buf_line_and_col (pbuf
, linep
, colp
)
865 register cpp_buffer
*pbuf
;
870 *linep
= pbuf
->lineno
;
872 *colp
= pbuf
->cur
- pbuf
->line_base
;
882 /* Return the cpp_buffer that corresponds to a file (not a macro). */
885 cpp_file_buffer (pfile
)
888 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
890 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
891 if (ip
->fname
!= NULL
)
897 * write out a #line command, for instance, after an #include file.
898 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
902 output_line_command (pfile
, file_change
)
904 enum file_change_code file_change
;
907 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
909 if (ip
->fname
== NULL
)
912 if (CPP_OPTIONS (pfile
)->no_line_commands
913 || CPP_OPTIONS (pfile
)->no_output
)
916 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
918 /* If the current file has not changed, we omit the #line if it would
919 appear to be a no-op, and we output a few newlines instead
920 if we want to increase the line number by a small amount.
921 We cannot do this if pfile->lineno is zero, because that means we
922 haven't output any line commands yet. (The very first line command
923 output is a `same_file' command.) */
924 if (file_change
== same_file
&& pfile
->lineno
!= 0)
926 if (line
== pfile
->lineno
)
929 /* If the inherited line number is a little too small,
930 output some newlines instead of a #line command. */
931 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
933 CPP_RESERVE (pfile
, 20);
934 while (line
> pfile
->lineno
)
936 CPP_PUTC_Q (pfile
, '\n');
943 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
944 CPP_PUTS_Q (pfile
, "# ", 2);
946 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
947 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
949 quote_string (pfile
, ip
->nominal_fname
);
950 if (file_change
!= same_file
)
952 CPP_PUTC_Q (pfile
, ' ');
953 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
955 /* Tell cc1 if following text comes from a system header file. */
956 if (ip
->system_header_p
)
958 CPP_PUTC_Q (pfile
, ' ');
959 CPP_PUTC_Q (pfile
, '3');
961 #ifndef NO_IMPLICIT_EXTERN_C
962 /* Tell cc1plus if following text should be treated as C. */
963 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
965 CPP_PUTC_Q (pfile
, ' ');
966 CPP_PUTC_Q (pfile
, '4');
969 CPP_PUTC_Q (pfile
, '\n');
970 pfile
->lineno
= line
;
974 /* Like cpp_get_token, except that it does not read past end-of-line.
975 Also, horizontal space is skipped, and macros are popped. */
977 static enum cpp_token
978 get_directive_token (pfile
)
983 long old_written
= CPP_WRITTEN (pfile
);
984 enum cpp_token token
;
985 cpp_skip_hspace (pfile
);
986 if (PEEKC () == '\n')
988 token
= cpp_get_token (pfile
);
992 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
994 /* ... else fall though ... */
995 case CPP_HSPACE
: case CPP_COMMENT
:
996 CPP_SET_WRITTEN (pfile
, old_written
);
1004 /* Handle #include and #import.
1005 This function expects to see "fname" or <fname> on the input.
1007 The input is normally in part of the output_buffer following
1008 CPP_WRITTEN, and will get overwritten by output_line_command.
1009 I.e. in input file specification has been popped by handle_directive.
1013 do_include (pfile
, keyword
)
1015 const struct directive
*keyword
;
1017 int importing
= (keyword
->type
== T_IMPORT
);
1018 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1019 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1020 int before
; /* included before? */
1022 unsigned char *ftok
;
1025 enum cpp_token token
;
1027 /* Chain of dirs to search */
1028 struct include_hash
*ihash
;
1029 struct file_name_list
*search_start
;
1031 long old_written
= CPP_WRITTEN (pfile
);
1035 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1038 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1040 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1043 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1044 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1045 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1047 pfile
->import_warning
= 1;
1049 "#import is obsolete, use an #ifndef wrapper in the header file");
1052 pfile
->parsing_include_directive
++;
1053 token
= get_directive_token (pfile
);
1054 pfile
->parsing_include_directive
--;
1056 if (token
== CPP_STRING
)
1058 if (pfile
->token_buffer
[old_written
] == '<')
1062 else if (token
== CPP_NAME
)
1064 /* Support '#include xyz' like VAX-C. It is taken as
1065 '#include <xyz.h>' and generates a warning. */
1067 "`#include filename' is obsolete, use `#include <filename.h>'");
1070 /* Append the missing `.h' to the name. */
1071 CPP_PUTS (pfile
, ".h", 2);
1077 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1078 CPP_SET_WRITTEN (pfile
, old_written
);
1079 skip_rest_of_line (pfile
);
1083 flen
= CPP_WRITTEN (pfile
) - old_written
;
1084 ftok
= (unsigned char *) alloca (flen
+ 1);
1085 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1088 if (get_directive_token (pfile
) != CPP_VSPACE
)
1090 cpp_error (pfile
, "junk at end of `#include'");
1091 skip_rest_of_line (pfile
);
1094 CPP_SET_WRITTEN (pfile
, old_written
);
1098 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1102 if (CPP_OPTIONS (pfile
)->dump_includes
)
1103 pass_thru_directive (ftok
,
1106 - ((token
== CPP_NAME
) ? 2 : 0)
1111 if (token
== CPP_STRING
)
1121 for (fp
= CPP_BUFFER (pfile
);
1122 fp
!= CPP_NULL_BUFFER (pfile
);
1123 fp
= CPP_PREV_BUFFER (fp
))
1124 if (fp
->fname
!= NULL
)
1127 if (fp
== CPP_NULL_BUFFER (pfile
))
1129 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
1133 /* For #include_next, skip in the search path past the dir in which the
1134 containing file was found. Treat files specified using an absolute path
1135 as if there are no more directories to search. Treat the primary source
1136 file like any other included source, but generate a warning. */
1137 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1139 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1140 search_start
= fp
->ihash
->foundhere
->next
;
1145 cpp_warning (pfile
, "#include_next in primary source file");
1148 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1151 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1154 search_start
= fp
->actual_dir
;
1157 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1163 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1167 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1174 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1175 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1176 (pfile
->system_include_depth
> 0)))
1178 if (!angle_brackets
)
1179 deps_output (pfile
, ftok
, ' ');
1183 struct file_name_list
*ptr
;
1184 /* If requested as a system header, assume it belongs in
1185 the first system header directory. */
1186 if (CPP_OPTIONS (pfile
)->bracket_include
)
1187 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1189 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1191 p
= (char *) alloca (strlen (ptr
->name
)
1192 + strlen (ftok
) + 2);
1193 if (*ptr
->name
!= '\0')
1195 strcpy (p
, ptr
->name
);
1199 deps_output (pfile
, p
, ' ');
1202 /* If -M was specified, and this header file won't be added to
1203 the dependency list, then don't count this as an error,
1204 because we can still produce correct output. Otherwise, we
1205 can't produce correct output, because there may be
1206 dependencies we need inside the missing file, and we don't
1207 know what directory this missing file exists in. */
1208 else if (CPP_PRINT_DEPS (pfile
)
1209 && (CPP_PRINT_DEPS (pfile
)
1210 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1211 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1213 cpp_error_from_errno (pfile
, ftok
);
1218 /* For -M, add the file to the dependencies on its first inclusion. */
1219 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1220 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1221 deps_output (pfile
, ihash
->name
, ' ');
1223 /* Handle -H option. */
1224 if (CPP_OPTIONS(pfile
)->print_include_names
)
1226 fp
= CPP_BUFFER (pfile
);
1227 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1229 fprintf (stderr
, " %s\n", ihash
->name
);
1232 /* Actually process the file */
1235 ihash
->control_macro
= "";
1237 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1244 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1246 if (finclude (pfile
, fd
, ihash
))
1248 output_line_command (pfile
, enter_file
);
1249 pfile
->only_seen_white
= 2;
1255 /* Subroutine of do_line. Read next token from PFILE without adding it to
1256 the output buffer. If it is a number between 1 and 4, store it in *NUM
1257 and return 1; otherwise, return 0 and complain if we aren't at the end
1258 of the directive. */
1261 read_line_number (pfile
, num
)
1265 long save_written
= CPP_WRITTEN (pfile
);
1266 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1267 enum cpp_token token
= get_directive_token (pfile
);
1268 CPP_SET_WRITTEN (pfile
, save_written
);
1270 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1277 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1278 cpp_error (pfile
, "invalid format `#line' command");
1283 /* Interpret #line command.
1284 Note that the filename string (if any) is treated as if it were an
1285 include filename. That means no escape handling. */
1288 do_line (pfile
, keyword
)
1290 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1292 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1294 long old_written
= CPP_WRITTEN (pfile
);
1295 enum file_change_code file_change
= same_file
;
1296 enum cpp_token token
;
1299 token
= get_directive_token (pfile
);
1301 if (token
!= CPP_NUMBER
)
1303 cpp_error (pfile
, "token after `#line' is not an integer");
1304 goto bad_line_directive
;
1307 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1310 cpp_error (pfile
, "token after `#line' is not an integer");
1311 goto bad_line_directive
;
1313 CPP_SET_WRITTEN (pfile
, old_written
);
1315 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1316 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1318 token
= get_directive_token (pfile
);
1320 if (token
== CPP_STRING
)
1322 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1323 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1324 int action_number
= 0;
1326 if (read_line_number (pfile
, &action_number
))
1328 if (CPP_PEDANTIC (pfile
))
1329 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1331 if (action_number
== 1)
1333 file_change
= enter_file
;
1334 read_line_number (pfile
, &action_number
);
1336 else if (action_number
== 2)
1338 file_change
= leave_file
;
1339 read_line_number (pfile
, &action_number
);
1341 if (action_number
== 3)
1343 ip
->system_header_p
= 1;
1344 read_line_number (pfile
, &action_number
);
1346 if (action_number
== 4)
1348 ip
->system_header_p
= 2;
1349 read_line_number (pfile
, &action_number
);
1355 if (strcmp (fname
, ip
->nominal_fname
))
1357 char *newname
, *oldname
;
1358 if (!strcmp (fname
, ip
->fname
))
1359 newname
= ip
->fname
;
1360 else if (ip
->last_nominal_fname
1361 && !strcmp (fname
, ip
->last_nominal_fname
))
1362 newname
= ip
->last_nominal_fname
;
1364 newname
= xstrdup (fname
);
1366 oldname
= ip
->nominal_fname
;
1367 ip
->nominal_fname
= newname
;
1369 if (ip
->last_nominal_fname
1370 && ip
->last_nominal_fname
!= oldname
1371 && ip
->last_nominal_fname
!= newname
1372 && ip
->last_nominal_fname
!= ip
->fname
)
1373 free (ip
->last_nominal_fname
);
1375 if (newname
== ip
->fname
)
1376 ip
->last_nominal_fname
= NULL
;
1378 ip
->last_nominal_fname
= oldname
;
1381 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1383 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1384 goto bad_line_directive
;
1387 /* The Newline at the end of this line remains to be processed.
1388 To put the next line at the specified line number,
1389 we must store a line number now that is one less. */
1390 ip
->lineno
= new_lineno
- 1;
1391 CPP_SET_WRITTEN (pfile
, old_written
);
1392 output_line_command (pfile
, file_change
);
1396 skip_rest_of_line (pfile
);
1397 CPP_SET_WRITTEN (pfile
, old_written
);
1401 /* Remove the definition of a symbol from the symbol table.
1402 According to the C standard, it is not an error to undef
1403 something that has no definitions. */
1405 do_undef (pfile
, keyword
)
1407 const struct directive
*keyword
;
1411 U_CHAR
*buf
, *name
, *limit
;
1413 long here
= CPP_WRITTEN (pfile
);
1414 enum cpp_token token
;
1416 cpp_skip_hspace (pfile
);
1418 if (! is_idstart
[c
])
1420 cpp_error (pfile
, "token after #undef is not an identifier");
1421 skip_rest_of_line (pfile
);
1425 parse_name (pfile
, c
);
1426 buf
= pfile
->token_buffer
+ here
;
1427 limit
= CPP_PWRITTEN(pfile
);
1429 /* Copy out the token so we can pop the token buffer. */
1430 name
= (U_CHAR
*) alloca (limit
- buf
+ 1);
1431 bcopy(buf
, name
, limit
- buf
);
1432 name
[limit
- buf
] = '\0';
1434 token
= get_directive_token (pfile
);
1435 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1437 cpp_pedwarn (pfile
, "junk on line after #undef");
1438 skip_rest_of_line (pfile
);
1441 CPP_SET_WRITTEN (pfile
, here
);
1443 sym_length
= check_macro_name (pfile
, buf
, 0);
1445 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1447 /* If we are generating additional info for debugging (with -g) we
1448 need to pass through all effective #undef commands. */
1449 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1450 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1451 if (hp
->type
== T_POISON
)
1452 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
1455 if (hp
->type
!= T_MACRO
)
1456 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1464 /* Wrap do_undef for -U processing. */
1466 cpp_undef (pfile
, macro
)
1470 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1472 do_undef (pfile
, NULL
);
1473 cpp_pop_buffer (pfile
);
1479 * Report an error detected by the program we are processing.
1480 * Use the text of the line in the error message.
1481 * (We use error because it prints the filename & line#.)
1485 do_error (pfile
, keyword
)
1487 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1489 long here
= CPP_WRITTEN (pfile
);
1491 copy_rest_of_line (pfile
);
1492 text
= pfile
->token_buffer
+ here
;
1493 SKIP_WHITE_SPACE(text
);
1495 cpp_error (pfile
, "#error %s", text
);
1496 CPP_SET_WRITTEN (pfile
, here
);
1501 * Report a warning detected by the program we are processing.
1502 * Use the text of the line in the warning message, then continue.
1506 do_warning (pfile
, keyword
)
1508 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1511 long here
= CPP_WRITTEN(pfile
);
1512 copy_rest_of_line (pfile
);
1513 text
= pfile
->token_buffer
+ here
;
1514 SKIP_WHITE_SPACE(text
);
1516 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1517 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1519 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1520 if -pedantic-errors is given, #warning should cause an error. */
1521 cpp_pedwarn (pfile
, "#warning %s", text
);
1522 CPP_SET_WRITTEN (pfile
, here
);
1526 /* Report program identification.
1527 This is not precisely what cccp does with #ident, however I believe
1528 it matches `closely enough' (behavior is identical as long as there
1529 are no macros on the #ident line, which is pathological in my opinion). */
1532 do_ident (pfile
, keyword
)
1534 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1536 /* Allow #ident in system headers, since that's not user's fault. */
1537 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1538 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1540 CPP_PUTS (pfile
, "#ident ", 7);
1541 cpp_skip_hspace (pfile
);
1542 copy_rest_of_line (pfile
);
1547 /* Just check for some recognized pragmas that need validation here,
1548 and leave the text in the token buffer to be output. */
1551 do_pragma (pfile
, keyword
)
1553 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1558 CPP_PUTS (pfile
, "#pragma ", 8);
1559 cpp_skip_hspace (pfile
);
1561 here
= CPP_WRITTEN (pfile
);
1562 copy_rest_of_line (pfile
);
1563 buf
= pfile
->token_buffer
+ here
;
1565 if (!strncmp (buf
, "once", 4))
1567 cpp_buffer
*ip
= NULL
;
1569 /* Allow #pragma once in system headers, since that's not the user's
1571 if (!CPP_BUFFER (pfile
)->system_header_p
)
1572 cpp_warning (pfile
, "`#pragma once' is obsolete");
1574 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1576 if (ip
== CPP_NULL_BUFFER (pfile
))
1578 if (ip
->fname
!= NULL
)
1582 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1583 cpp_warning (pfile
, "`#pragma once' outside include file");
1585 ip
->ihash
->control_macro
= ""; /* never repeat */
1587 else if (!strncmp (buf
, "implementation", 14))
1589 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1590 been included yet. */
1591 struct include_hash
*ptr
;
1592 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1593 SKIP_WHITE_SPACE (p
);
1594 if (*p
== '\n' || *p
!= '\"')
1598 p
= (U_CHAR
*) index (fname
, '\"');
1600 fcopy
= (U_CHAR
*) alloca (p
- fname
+ 1);
1601 bcopy (fname
, fcopy
, p
- fname
);
1602 fcopy
[p
-fname
] = '\0';
1604 ptr
= include_hash (pfile
, fcopy
, 0);
1607 "`#pragma implementation' for `%s' appears after file is included",
1610 else if (!strncmp (buf
, "poison", 6))
1612 /* Poison these symbols so that all subsequent usage produces an
1614 U_CHAR
*p
= buf
+ 6;
1619 SKIP_WHITE_SPACE (p
);
1620 plen
= strlen(p
) + 1;
1622 syms
= (U_CHAR
*) alloca (plen
);
1623 memcpy (syms
, p
, plen
);
1625 /* As a rule, don't include #pragma poison commands in output,
1626 unless the user asks for them. */
1627 writeit
= (CPP_OPTIONS (pfile
)->debug_output
1628 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
1629 || CPP_OPTIONS (pfile
)->dump_macros
== dump_names
);
1632 CPP_SET_WRITTEN (pfile
, here
);
1634 CPP_SET_WRITTEN (pfile
, here
-8);
1638 CPP_RESERVE (pfile
, plen
+ 7);
1639 CPP_PUTS_Q (pfile
, "poison", 7);
1642 while (*syms
!= '\0')
1646 while (is_idchar
[*end
])
1649 if (!is_hor_space
[*end
] && *end
!= '\0')
1651 cpp_error (pfile
, "invalid #pragma poison directive");
1655 if (cpp_push_buffer (pfile
, syms
, end
- syms
) != NULL
)
1657 do_define (pfile
, keyword
);
1658 cpp_pop_buffer (pfile
);
1662 CPP_PUTC_Q (pfile
, ' ');
1663 CPP_PUTS_Q (pfile
, syms
, end
- syms
);
1666 SKIP_WHITE_SPACE (syms
);
1673 #ifdef SCCS_DIRECTIVE
1674 /* Just ignore #sccs, on systems where we define it at all. */
1677 do_sccs (pfile
, keyword
)
1679 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1681 if (CPP_PEDANTIC (pfile
))
1682 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1683 skip_rest_of_line (pfile
);
1689 /* We've found an `#if' directive. If the only thing before it in
1690 this file is white space, and if it is of the form
1691 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1692 for inclusion of this file. (See redundant_include_p in cppfiles.c
1693 for an explanation of controlling macros.) If so, return a
1694 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1697 detect_if_not_defined (pfile
)
1700 U_CHAR
*control_macro
= 0;
1702 if (pfile
->only_seen_white
== 2)
1705 enum cpp_token token
;
1708 int need_rparen
= 0;
1710 /* Save state required for restore. */
1711 pfile
->no_macro_expand
++;
1712 parse_set_mark (pfile
);
1713 base_offset
= CPP_WRITTEN (pfile
);
1716 if (get_directive_token (pfile
) != CPP_OTHER
1717 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1718 || CPP_PWRITTEN (pfile
)[-1] != '!')
1721 /* ...then `defined', */
1722 token_offset
= CPP_WRITTEN (pfile
);
1723 token
= get_directive_token (pfile
);
1724 if (token
!= CPP_NAME
)
1726 ident
= pfile
->token_buffer
+ token_offset
;
1727 CPP_NUL_TERMINATE (pfile
);
1728 if (strcmp (ident
, "defined"))
1731 /* ...then an optional '(' and the name, */
1732 token_offset
= CPP_WRITTEN (pfile
);
1733 token
= get_directive_token (pfile
);
1734 if (token
== CPP_LPAREN
)
1736 token_offset
= CPP_WRITTEN (pfile
);
1737 token
= get_directive_token (pfile
);
1738 if (token
!= CPP_NAME
)
1742 else if (token
!= CPP_NAME
)
1745 ident
= pfile
->token_buffer
+ token_offset
;
1746 CPP_NUL_TERMINATE (pfile
);
1748 /* ...then the ')', if necessary, */
1749 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1750 /* ...and make sure there's nothing else on the line. */
1751 && get_directive_token (pfile
) == CPP_VSPACE
)
1752 control_macro
= xstrdup (ident
);
1755 CPP_SET_WRITTEN (pfile
, base_offset
);
1756 pfile
->no_macro_expand
--;
1757 parse_goto_mark (pfile
);
1760 return control_macro
;
1764 * handle #if command by
1765 * 1) inserting special `defined' keyword into the hash table
1766 * that gets turned into 0 or 1 by special_symbol (thus,
1767 * if the luser has a symbol called `defined' already, it won't
1768 * work inside the #if command)
1769 * 2) rescan the input into a temporary output buffer
1770 * 3) pass the output buffer to the yacc parser and collect a value
1771 * 4) clean up the mess left from steps 1 and 2.
1772 * 5) call conditional_skip to skip til the next #endif (etc.),
1773 * or not, depending on the value from step 3.
1777 do_if (pfile
, keyword
)
1779 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1781 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1782 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1783 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1788 * handle a #elif directive by not changing if_stack either.
1789 * see the comment above do_else.
1793 do_elif (pfile
, keyword
)
1795 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1797 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1798 cpp_error (pfile
, "`#elif' not within a conditional");
1801 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1802 cpp_error (pfile
, "`#elif' after `#else'");
1804 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1806 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
1807 && strcmp (pfile
->if_stack
->fname
,
1808 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
1809 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1810 fprintf (stderr
, ")\n");
1812 pfile
->if_stack
->type
= T_ELIF
;
1815 if (pfile
->if_stack
->if_succeeded
)
1816 skip_if_group (pfile
);
1818 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1820 skip_if_group (pfile
);
1822 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1823 output_line_command (pfile
, same_file
);
1830 * evaluate a #if expression in BUF, of length LENGTH,
1831 * then parse the result as a C expression and return the value as an int.
1834 static HOST_WIDEST_INT
1835 eval_if_expression (pfile
)
1838 HOST_WIDEST_INT value
;
1839 long old_written
= CPP_WRITTEN (pfile
);
1841 pfile
->pcp_inside_if
= 1;
1842 value
= cpp_parse_expr (pfile
);
1843 pfile
->pcp_inside_if
= 0;
1845 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1851 * routine to handle ifdef/ifndef. Try to look up the symbol,
1852 * then do or don't skip to the #endif/#else/#elif depending
1853 * on what directive is actually being processed.
1857 do_xifdef (pfile
, keyword
)
1859 const struct directive
*keyword
;
1862 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1865 enum cpp_token token
;
1866 int start_of_file
= 0;
1867 U_CHAR
*control_macro
= 0;
1868 int old_written
= CPP_WRITTEN (pfile
);
1870 /* Detect a #ifndef at start of file (not counting comments). */
1871 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1872 start_of_file
= pfile
->only_seen_white
== 2;
1874 pfile
->no_macro_expand
++;
1875 token
= get_directive_token (pfile
);
1876 pfile
->no_macro_expand
--;
1878 ident
= pfile
->token_buffer
+ old_written
;
1879 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1880 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1882 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1884 skip
= (keyword
->type
== T_IFDEF
);
1885 if (! CPP_TRADITIONAL (pfile
))
1886 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1888 else if (token
== CPP_NAME
)
1890 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1891 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1892 if (start_of_file
&& !skip
)
1894 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1895 bcopy (ident
, control_macro
, ident_length
+ 1);
1897 if (hp
!= NULL
&& hp
->type
== T_POISON
)
1899 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1905 skip
= (keyword
->type
== T_IFDEF
);
1906 if (! CPP_TRADITIONAL (pfile
))
1907 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1910 if (!CPP_TRADITIONAL (pfile
))
1912 cpp_skip_hspace (pfile
);
1914 if (c
!= EOF
&& c
!= '\n')
1915 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1917 skip_rest_of_line (pfile
);
1921 /* Output a precondition for this macro. */
1922 if (hp
&& hp
->value
.defn
->predefined
)
1923 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
1926 fprintf (pcp_outfile
, "#undef ");
1927 while (is_idchar
[*cp
]) /* Ick! */
1928 fputc (*cp
++, pcp_outfile
);
1929 putc ('\n', pcp_outfile
);
1933 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1937 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1938 If this is a #ifndef starting at the beginning of a file,
1939 CONTROL_MACRO is the macro name tested by the #ifndef.
1940 Otherwise, CONTROL_MACRO is 0. */
1943 conditional_skip (pfile
, skip
, type
, control_macro
)
1946 enum node_type type
;
1947 U_CHAR
*control_macro
;
1949 IF_STACK_FRAME
*temp
;
1951 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1952 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1953 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1954 temp
->next
= pfile
->if_stack
;
1955 temp
->control_macro
= control_macro
;
1956 pfile
->if_stack
= temp
;
1958 pfile
->if_stack
->type
= type
;
1961 skip_if_group (pfile
);
1964 ++pfile
->if_stack
->if_succeeded
;
1965 output_line_command (pfile
, same_file
);
1969 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1970 return 0 if skipping should continue, 1 if it should halt. Also
1971 adjusts the if_stack as appropriate.
1972 The `#' has been read, but not the identifier. */
1975 consider_directive_while_skipping (pfile
, stack
)
1977 IF_STACK_FRAME
*stack
;
1979 long ident_len
, ident
;
1980 const struct directive
*kt
;
1981 IF_STACK_FRAME
*temp
;
1983 cpp_skip_hspace (pfile
);
1985 ident
= CPP_WRITTEN (pfile
);
1986 parse_name (pfile
, GETC());
1987 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1989 CPP_SET_WRITTEN (pfile
, ident
);
1991 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1992 if (kt
->length
== ident_len
1993 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
1999 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
2000 temp
->next
= pfile
->if_stack
;
2001 pfile
->if_stack
= temp
;
2002 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
2003 temp
->type
= kt
->type
;
2007 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
2008 validate_else (pfile
, "#else");
2011 if (pfile
->if_stack
->type
== T_ELSE
)
2012 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
2014 if (pfile
->if_stack
== stack
)
2018 pfile
->if_stack
->type
= kt
->type
;
2023 if (CPP_PEDANTIC (pfile
) && 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 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2139 if (CPP_PEDANTIC (pfile
))
2140 validate_else (pfile
, "#else");
2141 skip_rest_of_line (pfile
);
2143 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
2144 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
) {
2152 cpp_error (pfile
, "`#else' after `#else'");
2153 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
2154 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
2155 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
2156 fprintf (stderr
, ")\n");
2158 pfile
->if_stack
->type
= T_ELSE
;
2161 if (pfile
->if_stack
->if_succeeded
)
2162 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 if (CPP_PEDANTIC (pfile
))
2180 validate_else (pfile
, "#endif");
2181 skip_rest_of_line (pfile
);
2183 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2184 cpp_error (pfile
, "`#endif' not within a conditional");
2187 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2188 pfile
->if_stack
= temp
->next
;
2189 if (temp
->control_macro
!= 0)
2191 /* This #endif matched a #ifndef at the start of the file.
2192 See if it is at the end of the file. */
2195 parse_set_mark (pfile
);
2199 cpp_skip_hspace (pfile
);
2204 parse_goto_mark (pfile
);
2208 /* This #endif ends a #ifndef
2209 that contains all of the file (aside from whitespace).
2210 Arrange not to include the file again
2211 if the macro that was tested is defined. */
2212 struct cpp_buffer
*ip
;
2213 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2214 if (ip
->fname
!= NULL
)
2216 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2220 output_line_command (pfile
, same_file
);
2225 /* When an #else or #endif is found while skipping failed conditional,
2226 if -pedantic was specified, this is called to warn about text after
2227 the command name. P points to the first char after the command name. */
2230 validate_else (pfile
, directive
)
2232 const char *directive
;
2235 cpp_skip_hspace (pfile
);
2237 if (c
!= EOF
&& c
!= '\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
);
2277 if (CPP_BUFFER (pfile
)->manual_pop
)
2278 /* If we've been reading from redirected input, the
2279 frontend will pop the buffer. */
2281 else if (CPP_BUFFER (pfile
)->seen_eof
)
2283 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2286 cpp_pop_buffer (pfile
);
2291 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2292 struct if_stack
*ifs
, *nifs
;
2294 /* Unwind the conditional stack and generate error messages. */
2295 for (ifs
= pfile
->if_stack
;
2296 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
2299 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
2300 "unterminated `%s' conditional",
2301 if_directive_name (pfile
, ifs
));
2306 pfile
->if_stack
= ifs
;
2308 if (CPP_BUFFER (pfile
)->nominal_fname
2309 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2311 /* We're about to return from an #include file.
2312 Emit #line information now (as part of the CPP_POP) result.
2313 But the #line refers to the file we will pop to. */
2314 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2315 CPP_BUFFER (pfile
) = next_buf
;
2316 pfile
->input_stack_listing_current
= 0;
2317 output_line_command (pfile
, leave_file
);
2318 CPP_BUFFER (pfile
) = cur_buffer
;
2321 CPP_BUFFER (pfile
)->seen_eof
= 1;
2330 if (PEEKC () == '=')
2334 if (opts
->put_out_comments
)
2335 c
= copy_comment (pfile
, c
);
2337 c
= skip_comment (pfile
, c
);
2343 /* Comments are equivalent to spaces.
2344 For -traditional, a comment is equivalent to nothing. */
2345 if (opts
->traditional
|| opts
->put_out_comments
)
2349 CPP_PUTC (pfile
, c
);
2353 if (opts
->for_lint
) {
2356 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2358 if (lintcmd
!= NULL
) {
2359 /* I believe it is always safe to emit this newline: */
2361 bcopy ("#pragma lint ", (char *) obp
, 13);
2363 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2368 bcopy (argbp
, (char *) obp
, arglen
);
2372 /* OK, now bring us back to the state we were in before we entered
2373 this branch. We need #line because the newline for the pragma
2374 could mess things up. */
2375 output_line_command (pfile
, same_file
);
2376 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2384 /* If this is expanding a macro definition, don't recognize
2385 preprocessor directives. */
2388 /* If this is expand_into_temp_buffer, recognize them
2389 only after an actual newline at this level,
2390 not at the beginning of the input level. */
2391 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
2397 if (!pfile
->only_seen_white
)
2399 if (handle_directive (pfile
))
2400 return CPP_DIRECTIVE
;
2401 pfile
->only_seen_white
= 0;
2407 parse_string (pfile
, c
);
2408 pfile
->only_seen_white
= 0;
2409 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2412 if (!opts
->dollars_in_ident
)
2417 if (opts
->cplusplus
&& PEEKC () == ':')
2425 if (c2
== c
|| c2
== '=')
2434 if (PEEKC () == '=')
2440 if (c2
== '-' && opts
->chill
)
2441 goto comment
; /* Chill style comment */
2442 if (c2
== '-' || c2
== '=')
2446 if (opts
->cplusplus
&& PEEKN (1) == '*')
2448 /* In C++, there's a ->* operator. */
2450 pfile
->only_seen_white
= 0;
2451 CPP_RESERVE (pfile
, 4);
2452 CPP_PUTC_Q (pfile
, c
);
2453 CPP_PUTC_Q (pfile
, GETC ());
2454 CPP_PUTC_Q (pfile
, GETC ());
2455 CPP_NUL_TERMINATE_Q (pfile
);
2463 if (pfile
->parsing_include_directive
)
2467 CPP_PUTC (pfile
, c
);
2471 if (c
== '\n' || c
== EOF
)
2474 "missing '>' in `#include <FILENAME>'");
2479 if (!CPP_BUFFER (pfile
)->has_escapes
)
2481 /* Backslash newline is replaced by nothing. */
2482 CPP_ADJUST_WRITTEN (pfile
, -1);
2483 CPP_BUMP_LINE (pfile
);
2487 /* We might conceivably get \r- or \r<space> in
2488 here. Just delete 'em. */
2490 if (d
!= '-' && d
!= ' ')
2492 "internal error: unrecognized escape \\r%c",
2494 CPP_ADJUST_WRITTEN (pfile
, -1);
2500 /* else fall through */
2505 /* GNU C++ supports MIN and MAX operators <? and >?. */
2506 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2509 CPP_RESERVE (pfile
, 4);
2510 CPP_PUTC (pfile
, c
);
2511 CPP_PUTC (pfile
, c2
);
2514 CPP_PUTC_Q (pfile
, GETC ());
2515 CPP_NUL_TERMINATE_Q (pfile
);
2516 pfile
->only_seen_white
= 0;
2523 CPP_RESERVE(pfile
, 2);
2524 CPP_PUTC_Q (pfile
, '.');
2529 /* In C++ there's a .* operator. */
2530 if (opts
->cplusplus
&& c2
== '*')
2533 if (c2
== '.' && PEEKN(1) == '.')
2535 CPP_RESERVE(pfile
, 4);
2536 CPP_PUTC_Q (pfile
, '.');
2537 CPP_PUTC_Q (pfile
, '.');
2538 CPP_PUTC_Q (pfile
, '.');
2540 CPP_NUL_TERMINATE_Q (pfile
);
2541 pfile
->only_seen_white
= 0;
2548 pfile
->only_seen_white
= 0;
2549 CPP_RESERVE(pfile
, 3);
2550 CPP_PUTC_Q (pfile
, c
);
2551 CPP_PUTC_Q (pfile
, GETC ());
2552 CPP_NUL_TERMINATE_Q (pfile
);
2557 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2559 CPP_PUTC (pfile
, c
);
2565 case '0': case '1': case '2': case '3': case '4':
2566 case '5': case '6': case '7': case '8': case '9':
2571 CPP_RESERVE (pfile
, 2);
2572 CPP_PUTC_Q (pfile
, c
);
2576 if (!is_idchar
[c
] && c
!= '.'
2577 && ((c2
!= 'e' && c2
!= 'E'
2578 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2579 || (c
!= '+' && c
!= '-')))
2584 CPP_NUL_TERMINATE_Q (pfile
);
2585 pfile
->only_seen_white
= 0;
2587 case 'b': case 'c': case 'd': case 'h': case 'o':
2588 case 'B': case 'C': case 'D': case 'H': case 'O':
2589 if (opts
->chill
&& PEEKC () == '\'')
2591 pfile
->only_seen_white
= 0;
2592 CPP_RESERVE (pfile
, 2);
2593 CPP_PUTC_Q (pfile
, c
);
2594 CPP_PUTC_Q (pfile
, '\'');
2600 goto chill_number_eof
;
2603 CPP_PUTC (pfile
, c
);
2607 CPP_RESERVE (pfile
, 2);
2608 CPP_PUTC_Q (pfile
, c
);
2609 CPP_NUL_TERMINATE_Q (pfile
);
2616 CPP_NUL_TERMINATE (pfile
);
2623 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2624 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2625 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2626 case 'x': case 'y': case 'z':
2627 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2628 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2629 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2634 unsigned char *ident
;
2635 int before_name_written
= CPP_WRITTEN (pfile
);
2637 parse_name (pfile
, c
);
2638 pfile
->only_seen_white
= 0;
2639 if (pfile
->no_macro_expand
)
2641 ident
= pfile
->token_buffer
+ before_name_written
;
2642 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2643 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2646 if (hp
->type
== T_DISABLED
)
2648 if (pfile
->output_escapes
)
2649 { /* Return "\r-IDENT", followed by '\0'. */
2651 CPP_RESERVE (pfile
, 3);
2652 ident
= pfile
->token_buffer
+ before_name_written
;
2653 CPP_ADJUST_WRITTEN (pfile
, 2);
2654 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2661 /* If macro wants an arglist, verify that a '(' follows.
2662 first skip all whitespace, copying it to the output
2663 after the macro name. Then, if there is no '(',
2664 decide this is not a macro call and leave things that way. */
2665 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2667 int is_macro_call
, macbuf_whitespace
= 0;
2669 parse_set_mark (pfile
);
2672 cpp_skip_hspace (pfile
);
2674 is_macro_call
= c
== '(';
2683 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2685 if (CPP_BUFFER (pfile
)->mark
!=
2686 (CPP_BUFFER (pfile
)->cur
2687 - CPP_BUFFER (pfile
)->buf
))
2688 macbuf_whitespace
= 1;
2690 /* The mark goes away automatically when
2691 the buffer is popped. */
2692 cpp_pop_buffer (pfile
);
2693 parse_set_mark (pfile
);
2701 parse_goto_mark (pfile
);
2702 if (macbuf_whitespace
)
2703 CPP_PUTC (pfile
, ' ');
2706 parse_clear_mark (pfile
);
2710 /* This is now known to be a macro call.
2711 Expand the macro, reading arguments as needed,
2712 and push the expansion on the input stack. */
2713 macroexpand (pfile
, hp
);
2714 CPP_SET_WRITTEN (pfile
, before_name_written
);
2718 case ' ': case '\t': case '\v':
2721 CPP_PUTC (pfile
, c
);
2723 if (c
== EOF
|| !is_hor_space
[c
])
2730 if (CPP_BUFFER (pfile
)->has_escapes
)
2735 if (pfile
->output_escapes
)
2736 CPP_PUTS (pfile
, "\r-", 2);
2737 parse_name (pfile
, GETC ());
2742 CPP_RESERVE (pfile
, 2);
2743 if (pfile
->output_escapes
)
2744 CPP_PUTC_Q (pfile
, '\r');
2745 CPP_PUTC_Q (pfile
, c
);
2751 "internal error: unrecognized escape \\r%c", c
);
2757 /* Backslash newline is ignored. */
2758 CPP_BUMP_LINE (pfile
);
2763 CPP_PUTC (pfile
, c
);
2764 if (pfile
->only_seen_white
== 0)
2765 pfile
->only_seen_white
= 1;
2766 CPP_BUMP_LINE (pfile
);
2767 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2770 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2771 output_line_command (pfile
, same_file
);
2775 case '(': token
= CPP_LPAREN
; goto char1
;
2776 case ')': token
= CPP_RPAREN
; goto char1
;
2777 case '{': token
= CPP_LBRACE
; goto char1
;
2778 case '}': token
= CPP_RBRACE
; goto char1
;
2779 case ',': token
= CPP_COMMA
; goto char1
;
2780 case ';': token
= CPP_SEMICOLON
; goto char1
;
2786 pfile
->only_seen_white
= 0;
2787 CPP_PUTC (pfile
, c
);
2793 /* Like cpp_get_token, but skip spaces and comments. */
2796 cpp_get_non_space_token (pfile
)
2799 int old_written
= CPP_WRITTEN (pfile
);
2802 enum cpp_token token
= cpp_get_token (pfile
);
2803 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2804 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2806 CPP_SET_WRITTEN (pfile
, old_written
);
2810 /* Parse an identifier starting with C. */
2813 parse_name (pfile
, c
)
2825 if (c
== '$' && CPP_PEDANTIC (pfile
))
2826 cpp_pedwarn (pfile
, "`$' in identifier");
2828 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2829 CPP_PUTC_Q (pfile
, c
);
2834 CPP_NUL_TERMINATE_Q (pfile
);
2838 /* Parse a string starting with C. A single quoted string is treated
2839 like a double -- some programs (e.g., troff) are perverse this way.
2840 (However, a single quoted string is not allowed to extend over
2843 parse_string (pfile
, c
)
2847 long start_line
, start_column
;
2849 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2851 CPP_PUTC (pfile
, c
);
2857 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2859 /* try harder: this string crosses a macro expansion
2860 boundary. This can happen naturally if -traditional.
2861 Otherwise, only -D can make a macro with an unmatched
2863 cpp_pop_buffer (pfile
);
2867 cpp_error_with_line (pfile
, start_line
, start_column
,
2868 "unterminated string or character constant");
2869 if (pfile
->multiline_string_line
!= start_line
2870 && pfile
->multiline_string_line
!= 0)
2871 cpp_error_with_line (pfile
,
2872 pfile
->multiline_string_line
, -1,
2873 "possible real start of unterminated constant");
2874 pfile
->multiline_string_line
= 0;
2877 CPP_PUTC (pfile
, cc
);
2881 CPP_BUMP_LINE (pfile
);
2883 /* Character constants may not extend over multiple lines.
2884 In ANSI, neither may strings. We accept multiline strings
2888 cpp_error_with_line (pfile
, start_line
, start_column
,
2889 "unterminated character constant");
2892 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2894 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2895 "string constant runs past end of line");
2897 if (pfile
->multiline_string_line
== 0)
2898 pfile
->multiline_string_line
= start_line
;
2902 CPP_ADJUST_WRITTEN (pfile
, -1);
2903 if (CPP_BUFFER (pfile
)->has_escapes
)
2906 "internal error: \\r escape inside string constant");
2910 /* Backslash newline is replaced by nothing at all. */
2911 CPP_BUMP_LINE (pfile
);
2917 CPP_PUTC (pfile
, cc
);
2929 /* Read an assertion into the token buffer, converting to
2930 canonical form: `#predicate(a n swe r)' The next non-whitespace
2931 character to read should be the first letter of the predicate.
2932 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2933 with answer (see callers for why). In case of 0, an error has been
2936 parse_assertion (pfile
)
2940 cpp_skip_hspace (pfile
);
2942 if (! is_idstart
[c
])
2944 cpp_error (pfile
, "assertion predicate is not an identifier");
2947 CPP_PUTC(pfile
, '#');
2949 parse_name(pfile
, c
);
2954 if (is_hor_space
[c
] || c
== '\r')
2955 cpp_skip_hspace (pfile
);
2961 CPP_PUTC(pfile
, '(');
2964 while ((c
= GETC()) != ')')
2966 if (is_hor_space
[c
])
2970 CPP_PUTC(pfile
, ' ');
2974 else if (c
== '\n' || c
== EOF
)
2976 if (c
== '\n') FORWARD(-1);
2977 cpp_error (pfile
, "un-terminated assertion answer");
2981 /* \r cannot be a macro escape here. */
2982 CPP_BUMP_LINE (pfile
);
2985 CPP_PUTC (pfile
, c
);
2990 if (pfile
->limit
[-1] == ' ')
2991 pfile
->limit
[-1] = ')';
2992 else if (pfile
->limit
[-1] == '(')
2994 cpp_error (pfile
, "empty token sequence in assertion");
2998 CPP_PUTC (pfile
, ')');
3000 CPP_NUL_TERMINATE (pfile
);
3005 do_assert (pfile
, keyword
)
3007 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3011 HASHNODE
*base
, *this;
3012 int baselen
, thislen
;
3014 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3015 && !CPP_BUFFER (pfile
)->system_header_p
)
3016 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
3018 cpp_skip_hspace (pfile
);
3019 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3020 ret
= parse_assertion (pfile
);
3025 cpp_error (pfile
, "missing token-sequence in `#assert'");
3029 cpp_skip_hspace (pfile
);
3031 if (c
!= EOF
&& c
!= '\n')
3033 cpp_error (pfile
, "junk at end of `#assert'");
3037 thislen
= strlen (sym
);
3038 baselen
= index (sym
, '(') - sym
;
3039 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3042 cpp_warning (pfile
, "`%s' re-asserted", sym
);
3046 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3048 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
3049 else if (base
->type
!= T_ASSERT
)
3051 /* Token clash - but with what?! */
3053 "cpp internal error: base->type != T_ASSERT in do_assert");
3057 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
3058 (char *)base
->value
.aschain
, -1);
3059 base
->value
.aschain
= this;
3061 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3065 skip_rest_of_line (pfile
);
3066 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3071 do_unassert (pfile
, keyword
)
3073 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3077 long baselen
, thislen
;
3078 HASHNODE
*base
, *this, *next
;
3080 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3081 && !CPP_BUFFER (pfile
)->system_header_p
)
3082 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
3084 cpp_skip_hspace (pfile
);
3086 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3087 ret
= parse_assertion (pfile
);
3091 cpp_skip_hspace (pfile
);
3093 if (c
!= EOF
&& c
!= '\n')
3094 cpp_error (pfile
, "junk at end of `#unassert'");
3096 thislen
= strlen (sym
);
3099 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
3101 goto error
; /* It isn't an error to #undef what isn't #defined,
3102 so it isn't an error to #unassert what isn't
3103 #asserted either. */
3105 for (this = base
->value
.aschain
; this; this = next
)
3107 next
= this->value
.aschain
;
3108 delete_macro (this);
3110 delete_macro (base
);
3114 baselen
= index (sym
, '(') - sym
;
3115 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3116 if (! base
) goto error
;
3117 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3118 if (! this) goto error
;
3121 while (next
->value
.aschain
!= this)
3122 next
= next
->value
.aschain
;
3124 next
->value
.aschain
= this->value
.aschain
;
3125 delete_macro (this);
3127 if (base
->value
.aschain
== NULL
)
3128 delete_macro (base
); /* Last answer for this predicate deleted. */
3131 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3134 skip_rest_of_line (pfile
);
3135 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3139 /* Process STR as if it appeared as the body of an #unassert. */
3141 cpp_unassert (pfile
, str
)
3145 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3147 do_assert (pfile
, NULL
);
3148 cpp_pop_buffer (pfile
);
3153 cpp_read_check_assertion (pfile
)
3156 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
3160 FORWARD (1); /* Skip '#' */
3161 cpp_skip_hspace (pfile
);
3162 if (! parse_assertion (pfile
))
3166 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
3170 pfile
->limit
= name
;
3174 /* Remember the current position of PFILE. */
3177 parse_set_mark (pfile
)
3180 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3183 "cpp internal error: ip->mark != -1 in parse_set_mark");
3185 ip
->mark
= ip
->cur
- ip
->buf
;
3188 /* Clear the current mark - we no longer need it. */
3191 parse_clear_mark (pfile
)
3194 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3197 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3202 /* Backup the current position of PFILE to that saved in its mark,
3203 and clear the mark. */
3206 parse_goto_mark (pfile
)
3209 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3212 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3214 ip
->cur
= ip
->buf
+ ip
->mark
;
3219 cpp_print_file_and_line (pfile
)
3222 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3227 cpp_buf_line_and_col (ip
, &line
, &col
);
3228 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
3229 line
, pfile
->show_column
? col
: -1);
3234 v_cpp_error (pfile
, msgid
, ap
)
3239 cpp_print_containing_files (pfile
);
3240 cpp_print_file_and_line (pfile
);
3241 v_cpp_message (pfile
, 1, msgid
, ap
);
3245 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3247 #ifndef ANSI_PROTOTYPES
3253 VA_START(ap
, msgid
);
3255 #ifndef ANSI_PROTOTYPES
3256 pfile
= va_arg (ap
, cpp_reader
*);
3257 msgid
= va_arg (ap
, const char *);
3260 v_cpp_error (pfile
, msgid
, ap
);
3264 /* Print error message but don't count it. */
3267 v_cpp_warning (pfile
, msgid
, ap
)
3272 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3275 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3278 cpp_print_containing_files (pfile
);
3279 cpp_print_file_and_line (pfile
);
3280 v_cpp_message (pfile
, 0, msgid
, ap
);
3284 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3286 #ifndef ANSI_PROTOTYPES
3292 VA_START (ap
, msgid
);
3294 #ifndef ANSI_PROTOTYPES
3295 pfile
= va_arg (ap
, cpp_reader
*);
3296 msgid
= va_arg (ap
, const char *);
3299 v_cpp_warning (pfile
, msgid
, ap
);
3303 /* Print an error message and maybe count it. */
3306 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3308 #ifndef ANSI_PROTOTYPES
3314 VA_START (ap
, msgid
);
3316 #ifndef ANSI_PROTOTYPES
3317 pfile
= va_arg (ap
, cpp_reader
*);
3318 msgid
= va_arg (ap
, const char *);
3321 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3322 v_cpp_error (pfile
, msgid
, ap
);
3324 v_cpp_warning (pfile
, msgid
, ap
);
3329 v_cpp_error_with_line (pfile
, line
, column
, msgid
, ap
)
3336 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3338 cpp_print_containing_files (pfile
);
3341 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3343 v_cpp_message (pfile
, 1, msgid
, ap
);
3347 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3348 const char *msgid
, ...))
3350 #ifndef ANSI_PROTOTYPES
3358 VA_START (ap
, msgid
);
3360 #ifndef ANSI_PROTOTYPES
3361 pfile
= va_arg (ap
, cpp_reader
*);
3362 line
= va_arg (ap
, int);
3363 column
= va_arg (ap
, int);
3364 msgid
= va_arg (ap
, const char *);
3367 v_cpp_error_with_line(pfile
, line
, column
, msgid
, ap
);
3372 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
)
3381 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3384 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3387 cpp_print_containing_files (pfile
);
3389 ip
= cpp_file_buffer (pfile
);
3392 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3394 v_cpp_message (pfile
, 0, msgid
, ap
);
3398 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3399 const char *msgid
, ...))
3401 #ifndef ANSI_PROTOTYPES
3409 VA_START (ap
, msgid
);
3411 #ifndef ANSI_PROTOTYPES
3412 pfile
= va_arg (ap
, cpp_reader
*);
3413 line
= va_arg (ap
, int);
3414 column
= va_arg (ap
, int);
3415 msgid
= va_arg (ap
, const char *);
3418 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3423 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3424 const char *msgid
, ...))
3426 #ifndef ANSI_PROTOTYPES
3434 VA_START (ap
, msgid
);
3436 #ifndef ANSI_PROTOTYPES
3437 pfile
= va_arg (ap
, cpp_reader
*);
3438 line
= va_arg (ap
, int);
3439 column
= va_arg (ap
, int);
3440 msgid
= va_arg (ap
, const char *);
3443 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3444 v_cpp_error_with_line (pfile
, column
, line
, msgid
, ap
);
3446 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3450 /* Report a warning (or an error if pedantic_errors)
3451 giving specified file name and line number, not current. */
3454 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, const char *file
,
3455 int line
, const char *msgid
, ...))
3457 #ifndef ANSI_PROTOTYPES
3465 VA_START (ap
, msgid
);
3467 #ifndef ANSI_PROTOTYPES
3468 pfile
= va_arg (ap
, cpp_reader
*);
3469 file
= va_arg (ap
, const char *);
3470 line
= va_arg (ap
, int);
3471 msgid
= va_arg (ap
, const char *);
3474 if (!CPP_OPTIONS (pfile
)->pedantic_errors
3475 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
3478 cpp_file_line_for_message (pfile
, file
, line
, -1);
3479 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msgid
, ap
);
3483 /* my_strerror - return the descriptive text associated with an
3487 my_strerror (errnum
)
3493 #ifndef HAVE_STRERROR
3494 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
3496 result
= strerror (errnum
);
3499 /* VAXCRTL's strerror() takes an optional second argument, which only
3500 matters when the first argument is EVMSERR. However, it's simplest
3501 just to pass it unconditionally. `vaxc$errno' is declared in
3502 <errno.h>, and maintained by the library in parallel with `errno'.
3503 We assume that caller's `errnum' either matches the last setting of
3504 `errno' by the library or else does not have the value `EVMSERR'. */
3506 result
= strerror (errnum
, vaxc$errno
);
3510 result
= "errno = ?";
3515 /* Error including a message from `errno'. */
3518 cpp_error_from_errno (pfile
, name
)
3522 cpp_message_from_errno (pfile
, 1, name
);
3526 cpp_message_from_errno (pfile
, is_error
, name
)
3532 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3534 cpp_print_containing_files (pfile
);
3537 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
3539 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
3543 cpp_perror_with_name (pfile
, name
)
3547 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
3551 * No pre-compiled header file support.
3553 * Possibly different enum token codes for each C/C++ token.
3555 * Find and cleanup remaining uses of static variables,
3557 * Support -dM flag (dump_all_macros).
3559 * Support for_lint flag.