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 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
39 case CPP_BUMP_LINE must not be called. */
40 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
42 /* External declarations. */
44 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
46 /* `struct directive' defines one #-directive, including how to handle it. */
49 int length
; /* Length of name */
50 int (*func
) /* Function to handle directive */
51 PARAMS ((cpp_reader
*, const struct directive
*));
52 const char *name
; /* Name of directive */
53 enum node_type type
; /* Code which describes which directive. */
56 /* These functions are declared to return int instead of void since they
57 are going to be placed in a table and some old compilers have trouble with
58 pointers to functions returning void. */
60 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
61 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
62 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
63 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
64 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
65 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
66 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
67 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
68 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
69 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
70 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
71 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
73 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
75 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
76 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
77 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
79 /* Forward declarations. */
81 static void validate_else
PARAMS ((cpp_reader
*, const char *));
82 static HOST_WIDEST_INT eval_if_expression
PARAMS ((cpp_reader
*));
83 static void conditional_skip
PARAMS ((cpp_reader
*, int,
84 enum node_type
, U_CHAR
*));
85 static void skip_if_group
PARAMS ((cpp_reader
*));
86 static void parse_name
PARAMS ((cpp_reader
*, int));
87 static void parse_string
PARAMS ((cpp_reader
*, int));
88 static int parse_assertion
PARAMS ((cpp_reader
*));
89 static const char *if_directive_name
PARAMS ((cpp_reader
*,
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 void skip_rest_of_line
PARAMS ((cpp_reader
*));
97 static void cpp_skip_hspace
PARAMS ((cpp_reader
*));
98 static int handle_directive
PARAMS ((cpp_reader
*));
99 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t,
101 const struct directive
*));
102 static int read_line_number
PARAMS ((cpp_reader
*, int *));
103 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
104 static int consider_directive_while_skipping
PARAMS ((cpp_reader
*,
106 static void skip_block_comment
PARAMS ((cpp_reader
*));
107 static void skip_line_comment
PARAMS ((cpp_reader
*));
108 static void parse_set_mark
PARAMS ((cpp_reader
*));
109 static void parse_goto_mark
PARAMS ((cpp_reader
*));
111 /* Here is the actual list of #-directives.
112 This table is ordered by frequency of occurrence; the numbers
113 at the end are directive counts from all the source code I have
114 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
115 linux-2.2.9, and pcmcia-cs-3.0.9). */
117 static const struct directive directive_table
[] = {
119 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
120 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
121 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
122 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
123 { 2, do_if
, "if", T_IF
}, /* 18162 */
124 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
125 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
126 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
127 { 4, do_line
, "line", T_LINE
}, /* 2465 */
128 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
129 { 5, do_error
, "error", T_ERROR
}, /* 475 */
130 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
132 /* Extensions. All deprecated except #warning and #include_next. */
133 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
134 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
135 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
136 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
137 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
138 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
139 #ifdef SCCS_DIRECTIVE
140 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
142 { -1, 0, "", T_UNUSED
}
145 /* Place into PFILE a quoted string representing the string SRC.
146 Caller must reserve enough space in pfile->token_buffer. */
149 quote_string (pfile
, src
)
155 CPP_PUTC_Q (pfile
, '\"');
157 switch ((c
= *src
++))
161 CPP_PUTC_Q (pfile
, c
);
164 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
165 CPP_ADJUST_WRITTEN (pfile
, 4);
171 CPP_PUTC_Q (pfile
, '\\');
172 CPP_PUTC_Q (pfile
, c
);
176 CPP_PUTC_Q (pfile
, '\"');
177 CPP_NUL_TERMINATE_Q (pfile
);
182 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
185 cpp_grow_buffer (pfile
, n
)
189 long old_written
= CPP_WRITTEN (pfile
);
190 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
191 pfile
->token_buffer
= (U_CHAR
*)
192 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
193 CPP_SET_WRITTEN (pfile
, old_written
);
196 /* Process the string STR as if it appeared as the body of a #define.
197 If STR is just an identifier, define it with value 1.
198 If STR has anything after the identifier, then it should
199 be identifier=definition. */
202 cpp_define (pfile
, str
)
209 p
= strchr (str
, '=');
210 /* Copy the entire option so we can modify it.
211 Change the first "=" in the string to a space. If there is none,
212 tack " 1" on the end. Then add a newline and a NUL. */
216 count
= strlen (str
) + 2;
217 buf
= (U_CHAR
*) alloca (count
);
218 memcpy (buf
, str
, count
- 2);
220 buf
[count
- 2] = '\n';
221 buf
[count
- 1] = '\0';
225 count
= strlen (str
) + 4;
226 buf
= (U_CHAR
*) alloca (count
);
227 memcpy (buf
, str
, count
- 4);
228 strcpy (&buf
[count
-4], " 1\n");
231 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
233 do_define (pfile
, NULL
);
234 cpp_pop_buffer (pfile
);
238 /* Process the string STR as if it appeared as the body of a #assert. */
240 cpp_assert (pfile
, str
)
244 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
246 do_assert (pfile
, NULL
);
247 cpp_pop_buffer (pfile
);
251 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
253 cpp_defined (pfile
, id
, len
)
258 HASHNODE
*hp
= cpp_lookup (pfile
, id
, len
);
259 if (hp
&& hp
->type
== T_POISON
)
261 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
267 static enum cpp_token
268 null_underflow (pfile
)
269 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
275 null_cleanup (pbuf
, pfile
)
276 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
277 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
282 /* Skip a C-style block comment. We know it's a comment, and point is
283 at the second character of the starter. */
285 skip_block_comment (pfile
)
292 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
298 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
301 else if (c
== '\n' || c
== '\r')
303 /* \r cannot be a macro escape marker here. */
304 if (!ACTIVE_MARK_P())
305 CPP_BUMP_LINE (pfile
);
307 else if (c
== '/' && prev_c
== '*')
309 else if (c
== '*' && prev_c
== '/'
310 && CPP_OPTIONS (pfile
)->warn_comments
)
311 cpp_warning (pfile
, "`/*' within comment");
317 /* Skip a C++/Chill line comment. We know it's a comment, and point
318 is at the second character of the initiator. */
320 skip_line_comment (pfile
)
328 /* We don't have to worry about EOF in here. */
331 /* Don't consider final '\n' to be part of comment. */
337 /* \r cannot be a macro escape marker here. */
338 if (!ACTIVE_MARK_P())
339 CPP_BUMP_LINE (pfile
);
340 if (CPP_OPTIONS (pfile
)->warn_comments
)
341 cpp_warning (pfile
, "backslash-newline within line comment");
346 /* Skip a comment - C, C++, or Chill style. M is the first character
347 of the comment marker. If this really is a comment, skip to its
348 end and return ' '. If this is not a comment, return M (which will
352 skip_comment (pfile
, m
)
356 if (m
== '/' && PEEKC() == '*')
358 skip_block_comment (pfile
);
361 else if (m
== '/' && PEEKC() == '/')
363 if (CPP_BUFFER (pfile
)->system_header_p
)
365 /* We silently allow C++ comments in system headers, irrespective
366 of conformance mode, because lots of busted systems do that
367 and trying to clean it up in fixincludes is a nightmare. */
368 skip_line_comment (pfile
);
371 else if (CPP_OPTIONS (pfile
)->cplusplus_comments
)
373 if (CPP_OPTIONS (pfile
)->c89
374 && CPP_PEDANTIC (pfile
)
375 && ! CPP_BUFFER (pfile
)->warned_cplusplus_comments
)
378 "C++ style comments are not allowed in ISO C89");
380 "(this will be reported only once per input file)");
381 CPP_BUFFER (pfile
)->warned_cplusplus_comments
= 1;
383 skip_line_comment (pfile
);
389 else if (m
== '-' && PEEKC() == '-'
390 && CPP_OPTIONS (pfile
)->chill
)
392 skip_line_comment (pfile
);
399 /* Identical to skip_comment except that it copies the comment into the
400 token_buffer. This is used if !discard_comments. */
402 copy_comment (pfile
, m
)
406 U_CHAR
*start
= CPP_BUFFER (pfile
)->cur
; /* XXX Layering violation */
409 if (skip_comment (pfile
, m
) == m
)
413 for (limit
= CPP_BUFFER (pfile
)->cur
; start
<= limit
; start
++)
415 CPP_PUTC (pfile
, *start
);
419 /* Skip whitespace \-newline and comments. Does not macro-expand. */
422 cpp_skip_hspace (pfile
)
431 else if (is_hspace(c
))
433 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
434 cpp_pedwarn (pfile
, "%s in preprocessing directive",
435 c
== '\f' ? "formfeed" : "vertical tab");
439 /* \r is a backslash-newline marker if !has_escapes, and
440 a deletable-whitespace or no-reexpansion marker otherwise. */
441 if (CPP_BUFFER (pfile
)->has_escapes
)
449 CPP_BUFFER (pfile
)->lineno
++;
451 else if (c
== '/' || c
== '-')
453 c
= skip_comment (pfile
, c
);
463 /* Read the rest of the current line.
464 The line is appended to PFILE's output buffer. */
467 copy_rest_of_line (pfile
)
478 CPP_NUL_TERMINATE (pfile
);
482 if (CPP_BUFFER (pfile
)->has_escapes
)
486 CPP_BUFFER (pfile
)->lineno
++;
491 parse_string (pfile
, c
);
496 if (CPP_TRADITIONAL (pfile
))
497 CPP_PUTS (pfile
, "/**/", 4);
498 skip_block_comment (pfile
);
501 /* else fall through */
503 c
= skip_comment (pfile
, c
);
508 if (CPP_PEDANTIC (pfile
))
509 cpp_pedwarn (pfile
, "%s in preprocessing directive",
510 c
== '\f' ? "formfeed" : "vertical tab");
518 /* FIXME: It is almost definitely a performance win to make this do
519 the scan itself. >75% of calls to copy_r_o_l are from here or
520 skip_if_group, which means the common case is to copy stuff into the
521 token_buffer only to discard it. */
523 skip_rest_of_line (pfile
)
526 long old
= CPP_WRITTEN (pfile
);
527 copy_rest_of_line (pfile
);
528 CPP_SET_WRITTEN (pfile
, old
);
531 /* Handle a possible # directive.
532 '#' has already been read. */
535 handle_directive (pfile
)
539 register const struct directive
*kt
;
542 long old_written
= CPP_WRITTEN (pfile
);
544 cpp_skip_hspace (pfile
);
547 /* # followed by a number is equivalent to #line. Do not recognize
548 this form in assembly language source files. Complain about this
549 form if we're being pedantic, but not if this is regurgitated
550 input (preprocessed or fed back in by the C++ frontend). */
551 if (c
>= '0' && c
<= '9')
553 if (CPP_OPTIONS (pfile
)->lang_asm
)
556 if (CPP_PEDANTIC (pfile
)
557 && ! CPP_PREPROCESSED (pfile
)
558 && ! CPP_BUFFER (pfile
)->manual_pop
)
559 cpp_pedwarn (pfile
, "`#' followed by integer");
560 do_line (pfile
, NULL
);
564 /* If we are rescanning preprocessed input, don't obey any directives
566 if (CPP_PREPROCESSED (pfile
))
569 /* Now find the directive name. */
570 CPP_PUTC (pfile
, '#');
571 parse_name (pfile
, GETC());
572 ident
= pfile
->token_buffer
+ old_written
+ 1;
573 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
574 if (ident_length
== 0)
576 /* A line of just `#' becomes blank. A line with something
577 other than an identifier after the # is reparsed as a non-
579 CPP_SET_WRITTEN (pfile
, old_written
);
580 return (PEEKC() == '\n');
583 /* Decode the keyword and call the appropriate expansion routine. */
584 for (kt
= directive_table
; ; kt
++)
587 /* # identifier, but not a legit directive. Pass onward as a
588 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
590 if (kt
->length
== ident_length
591 && !strncmp (kt
->name
, ident
, ident_length
))
595 CPP_SET_WRITTEN (pfile
, old_written
);
597 if (pfile
->no_directives
)
599 cpp_error (pfile
, "`#%s' may not be used inside a macro argument",
601 skip_rest_of_line (pfile
);
604 (*kt
->func
) (pfile
, kt
);
609 /* Pass a directive through to the output file.
610 BUF points to the contents of the directive, as a contiguous string.
611 LEN is the length of the string pointed to by BUF.
612 KEYWORD is the keyword-table entry for the directive. */
615 pass_thru_directive (buf
, len
, pfile
, keyword
)
619 const struct directive
*keyword
;
621 register unsigned keyword_length
= keyword
->length
;
623 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
624 CPP_PUTC_Q (pfile
, '#');
625 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
626 if (len
!= 0 && buf
[0] != ' ')
627 CPP_PUTC_Q (pfile
, ' ');
628 CPP_PUTS_Q (pfile
, buf
, len
);
631 /* Check a purported macro name SYMNAME, and yield its length. */
634 check_macro_name (pfile
, symname
)
636 const U_CHAR
*symname
;
641 for (p
= symname
; is_idchar(*p
); p
++)
643 sym_length
= p
- symname
;
645 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
646 cpp_error (pfile
, "invalid macro name");
647 else if (!is_idstart(*symname
)
648 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
649 U_CHAR
*msg
; /* what pain... */
650 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
651 bcopy (symname
, msg
, sym_length
);
653 cpp_error (pfile
, "invalid macro name `%s'", msg
);
658 /* Process a #define command.
659 KEYWORD is the keyword-table entry for #define,
660 or NULL for a "predefined" macro,
661 or the keyword-table entry for #pragma in the case of a #pragma poison. */
664 do_define (pfile
, keyword
)
666 const struct directive
*keyword
;
671 U_CHAR
*macro
, *buf
, *end
;
672 enum node_type new_type
;
674 here
= CPP_WRITTEN (pfile
);
675 copy_rest_of_line (pfile
);
677 if (keyword
== NULL
|| keyword
->type
== T_DEFINE
)
682 /* Copy out the line so we can pop the token buffer. */
683 buf
= pfile
->token_buffer
+ here
;
684 end
= CPP_PWRITTEN (pfile
);
685 macro
= (U_CHAR
*) alloca (end
- buf
+ 1);
686 memcpy (macro
, buf
, end
- buf
+ 1);
687 end
= macro
+ (end
- buf
);
689 CPP_SET_WRITTEN (pfile
, here
);
691 mdef
= create_definition (macro
, end
, pfile
);
695 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
)) != NULL
)
698 /* Redefining a poisoned identifier is even worse than `not ok'. */
699 if (hp
->type
== T_POISON
)
701 /* Redefining a macro is ok if the definitions are the same. */
702 else if (hp
->type
== T_MACRO
)
703 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
704 /* Redefining a constant is ok with -D. */
705 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
706 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
707 /* Print the warning or error if it's not ok. */
710 if (hp
->type
== T_POISON
)
711 cpp_error (pfile
, "redefining poisoned `%.*s'",
712 mdef
.symlen
, mdef
.symnam
);
714 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
715 if (hp
->type
== T_MACRO
&& CPP_OPTIONS (pfile
)->done_initializing
)
716 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
717 hp
->value
.defn
->line
, -1,
718 "this is the location of the previous definition");
720 if (hp
->type
!= T_POISON
)
722 /* Replace the old definition. */
724 free_definition (hp
->value
.defn
);
725 hp
->value
.defn
= mdef
.defn
;
729 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, new_type
, (char *)mdef
.defn
);
731 if (keyword
!= NULL
&& keyword
->type
== T_DEFINE
)
733 if (CPP_OPTIONS (pfile
)->debug_output
734 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
735 dump_definition (pfile
, mdef
);
736 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
737 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
744 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
745 If BUFFER != NULL, then use the LENGTH characters in BUFFER
746 as the new input buffer.
747 Return the new buffer, or NULL on failure. */
750 cpp_push_buffer (pfile
, buffer
, length
)
755 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
757 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
759 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
763 new = (cpp_buffer
*) xcalloc (1, sizeof (cpp_buffer
));
765 new->if_stack
= pfile
->if_stack
;
766 new->cleanup
= null_cleanup
;
767 new->underflow
= null_underflow
;
768 new->buf
= new->cur
= buffer
;
769 new->alimit
= new->rlimit
= buffer
+ length
;
773 CPP_BUFFER (pfile
) = new;
778 cpp_pop_buffer (pfile
)
781 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
783 cpp_ice (pfile
, "mark active in cpp_pop_buffer");
784 (*buf
->cleanup
) (buf
, pfile
);
785 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
787 pfile
->buffer_stack_depth
--;
788 return CPP_BUFFER (pfile
);
791 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
792 Pop the buffer when done. */
795 cpp_scan_buffer (pfile
)
798 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
799 enum cpp_token token
;
800 if (CPP_OPTIONS (pfile
)->no_output
)
802 long old_written
= CPP_WRITTEN (pfile
);
803 /* In no-output mode, we can ignore everything but directives. */
806 if (! pfile
->only_seen_white
)
807 skip_rest_of_line (pfile
);
808 token
= cpp_get_token (pfile
);
809 if (token
== CPP_EOF
) /* Should not happen ... */
811 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
813 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
814 != CPP_NULL_BUFFER (pfile
))
815 cpp_pop_buffer (pfile
);
819 CPP_SET_WRITTEN (pfile
, old_written
);
825 token
= cpp_get_token (pfile
);
826 if (token
== CPP_EOF
) /* Should not happen ... */
828 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
830 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
831 != CPP_NULL_BUFFER (pfile
))
832 cpp_pop_buffer (pfile
);
840 * Rescan a string (which may have escape marks) into pfile's buffer.
841 * Place the result in pfile->token_buffer.
843 * The input is copied before it is scanned, so it is safe to pass
844 * it something from the token_buffer that will get overwritten
845 * (because it follows CPP_WRITTEN). This is used by do_include.
849 cpp_expand_to_buffer (pfile
, buf
, length
)
854 register cpp_buffer
*ip
;
860 cpp_ice (pfile
, "length < 0 in cpp_expand_to_buffer");
864 /* Set up the input on the input stack. */
866 buf1
= (U_CHAR
*) alloca (length
+ 1);
867 memcpy (buf1
, buf
, length
);
870 ip
= cpp_push_buffer (pfile
, buf1
, length
);
875 /* Scan the input, create the output. */
876 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
877 CPP_OPTIONS (pfile
)->no_output
= 0;
878 cpp_scan_buffer (pfile
);
879 CPP_OPTIONS (pfile
)->no_output
= save_no_output
;
881 CPP_NUL_TERMINATE (pfile
);
885 cpp_buf_line_and_col (pbuf
, linep
, colp
)
886 register cpp_buffer
*pbuf
;
891 *linep
= pbuf
->lineno
;
893 *colp
= pbuf
->cur
- pbuf
->line_base
;
903 /* Return the cpp_buffer that corresponds to a file (not a macro). */
906 cpp_file_buffer (pfile
)
909 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
911 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
912 if (ip
->fname
!= NULL
)
918 * write out a #line command, for instance, after an #include file.
919 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
923 output_line_command (pfile
, file_change
)
925 enum file_change_code file_change
;
928 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
930 if (ip
->fname
== NULL
)
933 if (CPP_OPTIONS (pfile
)->no_line_commands
934 || CPP_OPTIONS (pfile
)->no_output
)
937 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
939 /* If the current file has not changed, we omit the #line if it would
940 appear to be a no-op, and we output a few newlines instead
941 if we want to increase the line number by a small amount.
942 We cannot do this if pfile->lineno is zero, because that means we
943 haven't output any line commands yet. (The very first line command
944 output is a `same_file' command.) */
945 if (file_change
== same_file
&& pfile
->lineno
!= 0)
947 if (line
== pfile
->lineno
)
950 /* If the inherited line number is a little too small,
951 output some newlines instead of a #line command. */
952 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
954 CPP_RESERVE (pfile
, 20);
955 while (line
> pfile
->lineno
)
957 CPP_PUTC_Q (pfile
, '\n');
964 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
965 CPP_PUTS_Q (pfile
, "# ", 2);
967 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
968 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
970 quote_string (pfile
, ip
->nominal_fname
);
971 if (file_change
!= same_file
)
973 CPP_PUTC_Q (pfile
, ' ');
974 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
976 /* Tell cc1 if following text comes from a system header file. */
977 if (ip
->system_header_p
)
979 CPP_PUTC_Q (pfile
, ' ');
980 CPP_PUTC_Q (pfile
, '3');
982 #ifndef NO_IMPLICIT_EXTERN_C
983 /* Tell cc1plus if following text should be treated as C. */
984 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
986 CPP_PUTC_Q (pfile
, ' ');
987 CPP_PUTC_Q (pfile
, '4');
990 CPP_PUTC_Q (pfile
, '\n');
991 pfile
->lineno
= line
;
995 /* Like cpp_get_token, except that it does not read past end-of-line.
996 Also, horizontal space is skipped, and macros are popped. */
999 get_directive_token (pfile
)
1002 long old_written
= CPP_WRITTEN (pfile
);
1003 enum cpp_token token
;
1007 cpp_skip_hspace (pfile
);
1008 if (PEEKC () == '\n')
1011 token
= cpp_get_token (pfile
);
1012 /* token could be hspace at the beginning of a macro. */
1013 if (token
== CPP_HSPACE
|| token
== CPP_COMMENT
)
1015 CPP_SET_WRITTEN (pfile
, old_written
);
1019 /* token cannot be vspace, it would have been caught above. */
1020 if (token
== CPP_VSPACE
)
1022 cpp_ice (pfile
, "VSPACE in get_directive_token");
1026 /* token cannot be POP unless the buffer is a macro buffer. */
1027 if (token
!= CPP_POP
)
1030 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
1032 cpp_ice (pfile
, "POP of file buffer in get_directive_token");
1036 /* We must pop the buffer by hand, or else cpp_get_token might
1037 hand us white space or newline on the next invocation. */
1038 cpp_pop_buffer (pfile
);
1042 /* Handle #include and #import.
1043 This function expects to see "fname" or <fname> on the input.
1045 The input is normally in part of the output_buffer following
1046 CPP_WRITTEN, and will get overwritten by output_line_command.
1047 I.e. in input file specification has been popped by handle_directive.
1051 do_include (pfile
, keyword
)
1053 const struct directive
*keyword
;
1055 int importing
= (keyword
->type
== T_IMPORT
);
1056 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1057 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1058 int before
; /* included before? */
1060 unsigned char *ftok
;
1063 enum cpp_token token
;
1065 /* Chain of dirs to search */
1066 struct include_hash
*ihash
;
1067 struct file_name_list
*search_start
;
1069 long old_written
= CPP_WRITTEN (pfile
);
1073 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1076 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1078 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1081 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1082 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1083 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1085 pfile
->import_warning
= 1;
1087 "#import is obsolete, use an #ifndef wrapper in the header file");
1090 pfile
->parsing_include_directive
++;
1091 token
= get_directive_token (pfile
);
1092 pfile
->parsing_include_directive
--;
1094 if (token
== CPP_STRING
)
1096 if (pfile
->token_buffer
[old_written
] == '<')
1100 else if (token
== CPP_NAME
)
1102 /* Support '#include xyz' like VAX-C. It is taken as
1103 '#include <xyz.h>' and generates a warning. */
1105 "`#include filename' is obsolete, use `#include <filename.h>'");
1108 /* Append the missing `.h' to the name. */
1109 CPP_PUTS (pfile
, ".h", 2);
1115 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1116 CPP_SET_WRITTEN (pfile
, old_written
);
1117 skip_rest_of_line (pfile
);
1121 flen
= CPP_WRITTEN (pfile
) - old_written
;
1122 ftok
= (unsigned char *) alloca (flen
+ 1);
1123 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1126 if (get_directive_token (pfile
) != CPP_VSPACE
)
1128 cpp_error (pfile
, "junk at end of `#include'");
1129 skip_rest_of_line (pfile
);
1132 CPP_SET_WRITTEN (pfile
, old_written
);
1136 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1140 if (CPP_OPTIONS (pfile
)->dump_includes
)
1141 pass_thru_directive (ftok
,
1144 - ((token
== CPP_NAME
) ? 2 : 0)
1149 if (token
== CPP_STRING
)
1159 for (fp
= CPP_BUFFER (pfile
);
1160 fp
!= CPP_NULL_BUFFER (pfile
);
1161 fp
= CPP_PREV_BUFFER (fp
))
1162 if (fp
->fname
!= NULL
)
1165 if (fp
== CPP_NULL_BUFFER (pfile
))
1167 cpp_ice (pfile
, "fp == NULL_BUFFER in do_include");
1171 /* For #include_next, skip in the search path past the dir in which the
1172 containing file was found. Treat files specified using an absolute path
1173 as if there are no more directories to search. Treat the primary source
1174 file like any other included source, but generate a warning. */
1175 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1177 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1178 search_start
= fp
->ihash
->foundhere
->next
;
1183 cpp_warning (pfile
, "#include_next in primary source file");
1186 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1189 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1192 search_start
= fp
->actual_dir
;
1195 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1201 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1205 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1212 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1213 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1214 (pfile
->system_include_depth
> 0)))
1216 if (!angle_brackets
)
1217 deps_output (pfile
, ftok
, ' ');
1221 struct file_name_list
*ptr
;
1222 /* If requested as a system header, assume it belongs in
1223 the first system header directory. */
1224 if (CPP_OPTIONS (pfile
)->bracket_include
)
1225 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1227 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1229 p
= (char *) alloca (strlen (ptr
->name
)
1230 + strlen (ftok
) + 2);
1231 if (*ptr
->name
!= '\0')
1233 strcpy (p
, ptr
->name
);
1237 deps_output (pfile
, p
, ' ');
1240 /* If -M was specified, and this header file won't be added to
1241 the dependency list, then don't count this as an error,
1242 because we can still produce correct output. Otherwise, we
1243 can't produce correct output, because there may be
1244 dependencies we need inside the missing file, and we don't
1245 know what directory this missing file exists in. */
1246 else if (CPP_PRINT_DEPS (pfile
)
1247 && (CPP_PRINT_DEPS (pfile
)
1248 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1249 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1251 cpp_error_from_errno (pfile
, ftok
);
1256 /* For -M, add the file to the dependencies on its first inclusion. */
1257 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1258 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1259 deps_output (pfile
, ihash
->name
, ' ');
1261 /* Handle -H option. */
1262 if (CPP_OPTIONS(pfile
)->print_include_names
)
1264 fp
= CPP_BUFFER (pfile
);
1265 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1267 fprintf (stderr
, " %s\n", ihash
->name
);
1270 /* Actually process the file */
1273 ihash
->control_macro
= "";
1275 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1282 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1284 if (finclude (pfile
, fd
, ihash
))
1286 output_line_command (pfile
, enter_file
);
1287 pfile
->only_seen_white
= 2;
1293 /* Subroutine of do_line. Read next token from PFILE without adding it to
1294 the output buffer. If it is a number between 1 and 4, store it in *NUM
1295 and return 1; otherwise, return 0 and complain if we aren't at the end
1296 of the directive. */
1299 read_line_number (pfile
, num
)
1303 long save_written
= CPP_WRITTEN (pfile
);
1304 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1305 enum cpp_token token
= get_directive_token (pfile
);
1306 CPP_SET_WRITTEN (pfile
, save_written
);
1308 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1315 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1316 cpp_error (pfile
, "invalid format `#line' command");
1321 /* Interpret #line command.
1322 Note that the filename string (if any) is treated as if it were an
1323 include filename. That means no escape handling. */
1326 do_line (pfile
, keyword
)
1328 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1330 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1332 long old_written
= CPP_WRITTEN (pfile
);
1333 enum file_change_code file_change
= same_file
;
1334 enum cpp_token token
;
1337 token
= get_directive_token (pfile
);
1339 if (token
!= CPP_NUMBER
)
1341 cpp_error (pfile
, "token after `#line' is not an integer");
1342 goto bad_line_directive
;
1345 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1348 cpp_error (pfile
, "token after `#line' is not an integer");
1349 goto bad_line_directive
;
1351 CPP_SET_WRITTEN (pfile
, old_written
);
1353 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1354 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1356 token
= get_directive_token (pfile
);
1358 if (token
== CPP_STRING
)
1360 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1361 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1362 int action_number
= 0;
1364 if (read_line_number (pfile
, &action_number
))
1366 if (CPP_PEDANTIC (pfile
))
1367 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1369 if (action_number
== 1)
1371 file_change
= enter_file
;
1372 read_line_number (pfile
, &action_number
);
1374 else if (action_number
== 2)
1376 file_change
= leave_file
;
1377 read_line_number (pfile
, &action_number
);
1379 if (action_number
== 3)
1381 ip
->system_header_p
= 1;
1382 read_line_number (pfile
, &action_number
);
1384 if (action_number
== 4)
1386 ip
->system_header_p
= 2;
1387 read_line_number (pfile
, &action_number
);
1393 if (strcmp (fname
, ip
->nominal_fname
))
1395 const char *newname
, *oldname
;
1396 if (!strcmp (fname
, ip
->fname
))
1397 newname
= ip
->fname
;
1398 else if (ip
->last_nominal_fname
1399 && !strcmp (fname
, ip
->last_nominal_fname
))
1400 newname
= ip
->last_nominal_fname
;
1402 newname
= xstrdup (fname
);
1404 oldname
= ip
->nominal_fname
;
1405 ip
->nominal_fname
= newname
;
1407 if (ip
->last_nominal_fname
1408 && ip
->last_nominal_fname
!= oldname
1409 && ip
->last_nominal_fname
!= newname
1410 && ip
->last_nominal_fname
!= ip
->fname
)
1411 free ((void *) ip
->last_nominal_fname
);
1413 if (newname
== ip
->fname
)
1414 ip
->last_nominal_fname
= NULL
;
1416 ip
->last_nominal_fname
= oldname
;
1419 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1421 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1422 goto bad_line_directive
;
1425 /* The Newline at the end of this line remains to be processed.
1426 To put the next line at the specified line number,
1427 we must store a line number now that is one less. */
1428 ip
->lineno
= new_lineno
- 1;
1429 CPP_SET_WRITTEN (pfile
, old_written
);
1430 output_line_command (pfile
, file_change
);
1434 skip_rest_of_line (pfile
);
1435 CPP_SET_WRITTEN (pfile
, old_written
);
1439 /* Remove the definition of a symbol from the symbol table.
1440 According to the C standard, it is not an error to undef
1441 something that has no definitions. */
1443 do_undef (pfile
, keyword
)
1445 const struct directive
*keyword
;
1449 U_CHAR
*buf
, *name
, *limit
;
1451 long here
= CPP_WRITTEN (pfile
);
1452 enum cpp_token token
;
1454 cpp_skip_hspace (pfile
);
1456 if (! is_idstart(c
))
1458 cpp_error (pfile
, "token after #undef is not an identifier");
1459 skip_rest_of_line (pfile
);
1463 parse_name (pfile
, c
);
1464 buf
= pfile
->token_buffer
+ here
;
1465 limit
= CPP_PWRITTEN(pfile
);
1467 /* Copy out the token so we can pop the token buffer. */
1468 name
= (U_CHAR
*) alloca (limit
- buf
+ 1);
1469 bcopy(buf
, name
, limit
- buf
);
1470 name
[limit
- buf
] = '\0';
1472 token
= get_directive_token (pfile
);
1473 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1475 cpp_pedwarn (pfile
, "junk on line after #undef");
1476 skip_rest_of_line (pfile
);
1479 CPP_SET_WRITTEN (pfile
, here
);
1481 sym_length
= check_macro_name (pfile
, buf
);
1483 while ((hp
= cpp_lookup (pfile
, name
, sym_length
)) != NULL
)
1485 /* If we are generating additional info for debugging (with -g) we
1486 need to pass through all effective #undef commands. */
1487 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1488 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1489 if (hp
->type
== T_POISON
)
1490 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
1493 if (hp
->type
!= T_MACRO
)
1494 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1502 /* Wrap do_undef for -U processing. */
1504 cpp_undef (pfile
, macro
)
1508 /* Copy the string so we can append a newline. */
1509 size_t len
= strlen (macro
);
1510 U_CHAR
*buf
= alloca (len
+ 2);
1511 memcpy (buf
, macro
, len
);
1513 buf
[len
+ 1] = '\0';
1514 if (cpp_push_buffer (pfile
, buf
, len
+ 1))
1516 do_undef (pfile
, NULL
);
1517 cpp_pop_buffer (pfile
);
1522 * Report an error detected by the program we are processing.
1523 * Use the text of the line in the error message.
1524 * (We use error because it prints the filename & line#.)
1528 do_error (pfile
, keyword
)
1530 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1532 long here
= CPP_WRITTEN (pfile
);
1534 copy_rest_of_line (pfile
);
1535 text
= pfile
->token_buffer
+ here
;
1536 SKIP_WHITE_SPACE(text
);
1538 cpp_error (pfile
, "#error %s", text
);
1539 CPP_SET_WRITTEN (pfile
, here
);
1544 * Report a warning detected by the program we are processing.
1545 * Use the text of the line in the warning message, then continue.
1549 do_warning (pfile
, keyword
)
1551 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1554 long here
= CPP_WRITTEN(pfile
);
1555 copy_rest_of_line (pfile
);
1556 text
= pfile
->token_buffer
+ here
;
1557 SKIP_WHITE_SPACE(text
);
1559 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1560 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1562 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1563 if -pedantic-errors is given, #warning should cause an error. */
1564 cpp_pedwarn (pfile
, "#warning %s", text
);
1565 CPP_SET_WRITTEN (pfile
, here
);
1569 /* Report program identification.
1570 This is not precisely what cccp does with #ident, however I believe
1571 it matches `closely enough' (behavior is identical as long as there
1572 are no macros on the #ident line, which is pathological in my opinion). */
1575 do_ident (pfile
, keyword
)
1577 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1579 /* Allow #ident in system headers, since that's not user's fault. */
1580 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1581 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1583 CPP_PUTS (pfile
, "#ident ", 7);
1584 cpp_skip_hspace (pfile
);
1585 copy_rest_of_line (pfile
);
1590 /* Just check for some recognized pragmas that need validation here,
1591 and leave the text in the token buffer to be output. */
1594 do_pragma (pfile
, keyword
)
1596 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1601 CPP_PUTS (pfile
, "#pragma ", 8);
1602 cpp_skip_hspace (pfile
);
1604 here
= CPP_WRITTEN (pfile
);
1605 copy_rest_of_line (pfile
);
1606 buf
= pfile
->token_buffer
+ here
;
1608 if (!strncmp (buf
, "once", 4))
1610 cpp_buffer
*ip
= NULL
;
1612 /* Allow #pragma once in system headers, since that's not the user's
1614 if (!CPP_BUFFER (pfile
)->system_header_p
)
1615 cpp_warning (pfile
, "`#pragma once' is obsolete");
1617 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1619 if (ip
== CPP_NULL_BUFFER (pfile
))
1621 if (ip
->fname
!= NULL
)
1625 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1626 cpp_warning (pfile
, "`#pragma once' outside include file");
1628 ip
->ihash
->control_macro
= ""; /* never repeat */
1630 else if (!strncmp (buf
, "implementation", 14))
1632 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1633 been included yet. */
1634 struct include_hash
*ptr
;
1635 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1636 SKIP_WHITE_SPACE (p
);
1637 if (*p
== '\n' || *p
!= '\"')
1641 p
= (U_CHAR
*) index (fname
, '\"');
1643 fcopy
= (U_CHAR
*) alloca (p
- fname
+ 1);
1644 bcopy (fname
, fcopy
, p
- fname
);
1645 fcopy
[p
-fname
] = '\0';
1647 ptr
= include_hash (pfile
, fcopy
, 0);
1650 "`#pragma implementation' for `%s' appears after file is included",
1653 else if (!strncmp (buf
, "poison", 6))
1655 /* Poison these symbols so that all subsequent usage produces an
1657 U_CHAR
*p
= buf
+ 6;
1662 SKIP_WHITE_SPACE (p
);
1663 plen
= strlen(p
) + 1;
1665 syms
= (U_CHAR
*) alloca (plen
);
1666 memcpy (syms
, p
, plen
);
1668 /* As a rule, don't include #pragma poison commands in output,
1669 unless the user asks for them. */
1670 writeit
= (CPP_OPTIONS (pfile
)->debug_output
1671 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
1672 || CPP_OPTIONS (pfile
)->dump_macros
== dump_names
);
1675 CPP_SET_WRITTEN (pfile
, here
);
1677 CPP_SET_WRITTEN (pfile
, here
-8);
1681 CPP_RESERVE (pfile
, plen
+ 7);
1682 CPP_PUTS_Q (pfile
, "poison", 7);
1685 while (*syms
!= '\0')
1689 while (is_idchar(*end
))
1692 if (!is_hspace(*end
) && *end
!= '\0')
1694 cpp_error (pfile
, "invalid #pragma poison directive");
1698 if (cpp_push_buffer (pfile
, syms
, end
- syms
) != NULL
)
1700 do_define (pfile
, keyword
);
1701 cpp_pop_buffer (pfile
);
1705 CPP_PUTC_Q (pfile
, ' ');
1706 CPP_PUTS_Q (pfile
, syms
, end
- syms
);
1709 SKIP_WHITE_SPACE (syms
);
1716 #ifdef SCCS_DIRECTIVE
1717 /* Just ignore #sccs, on systems where we define it at all. */
1720 do_sccs (pfile
, keyword
)
1722 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1724 if (CPP_PEDANTIC (pfile
))
1725 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1726 skip_rest_of_line (pfile
);
1732 /* We've found an `#if' directive. If the only thing before it in
1733 this file is white space, and if it is of the form
1734 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1735 for inclusion of this file. (See redundant_include_p in cppfiles.c
1736 for an explanation of controlling macros.) If so, return a
1737 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1740 detect_if_not_defined (pfile
)
1743 U_CHAR
*control_macro
= 0;
1745 if (pfile
->only_seen_white
== 2)
1748 enum cpp_token token
;
1751 int need_rparen
= 0;
1753 /* Save state required for restore. */
1754 pfile
->no_macro_expand
++;
1755 parse_set_mark (pfile
);
1756 base_offset
= CPP_WRITTEN (pfile
);
1759 if (get_directive_token (pfile
) != CPP_OTHER
1760 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1761 || CPP_PWRITTEN (pfile
)[-1] != '!')
1764 /* ...then `defined', */
1765 token_offset
= CPP_WRITTEN (pfile
);
1766 token
= get_directive_token (pfile
);
1767 if (token
!= CPP_NAME
)
1769 ident
= pfile
->token_buffer
+ token_offset
;
1770 CPP_NUL_TERMINATE (pfile
);
1771 if (strcmp (ident
, "defined"))
1774 /* ...then an optional '(' and the name, */
1775 token_offset
= CPP_WRITTEN (pfile
);
1776 token
= get_directive_token (pfile
);
1777 if (token
== CPP_LPAREN
)
1779 token_offset
= CPP_WRITTEN (pfile
);
1780 token
= get_directive_token (pfile
);
1781 if (token
!= CPP_NAME
)
1785 else if (token
!= CPP_NAME
)
1788 ident
= pfile
->token_buffer
+ token_offset
;
1789 CPP_NUL_TERMINATE (pfile
);
1791 /* ...then the ')', if necessary, */
1792 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1793 /* ...and make sure there's nothing else on the line. */
1794 && get_directive_token (pfile
) == CPP_VSPACE
)
1795 control_macro
= xstrdup (ident
);
1798 CPP_SET_WRITTEN (pfile
, base_offset
);
1799 pfile
->no_macro_expand
--;
1800 parse_goto_mark (pfile
);
1803 return control_macro
;
1807 * handle #if command by
1808 * 1) inserting special `defined' keyword into the hash table
1809 * that gets turned into 0 or 1 by special_symbol (thus,
1810 * if the luser has a symbol called `defined' already, it won't
1811 * work inside the #if command)
1812 * 2) rescan the input into a temporary output buffer
1813 * 3) pass the output buffer to the yacc parser and collect a value
1814 * 4) clean up the mess left from steps 1 and 2.
1815 * 5) call conditional_skip to skip til the next #endif (etc.),
1816 * or not, depending on the value from step 3.
1820 do_if (pfile
, keyword
)
1822 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1824 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1825 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1826 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1831 * handle a #elif directive by not changing if_stack either.
1832 * see the comment above do_else.
1836 do_elif (pfile
, keyword
)
1838 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1840 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1842 cpp_error (pfile
, "`#elif' not within a conditional");
1847 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
1849 cpp_error (pfile
, "`#elif' after `#else'");
1850 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1851 "the conditional began here");
1853 pfile
->if_stack
->type
= T_ELIF
;
1856 if (pfile
->if_stack
->if_succeeded
)
1857 skip_if_group (pfile
);
1860 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1862 skip_if_group (pfile
);
1865 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1866 output_line_command (pfile
, same_file
);
1873 * evaluate a #if expression in BUF, of length LENGTH,
1874 * then parse the result as a C expression and return the value as an int.
1877 static HOST_WIDEST_INT
1878 eval_if_expression (pfile
)
1881 HOST_WIDEST_INT value
;
1882 long old_written
= CPP_WRITTEN (pfile
);
1884 /* Work around bug in cpp_get_token where it may mistake an
1885 assertion for a directive. */
1886 pfile
->only_seen_white
= 0;
1888 value
= cpp_parse_expr (pfile
);
1890 skip_rest_of_line (pfile
);
1891 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1897 * routine to handle ifdef/ifndef. Try to look up the symbol,
1898 * then do or don't skip to the #endif/#else/#elif depending
1899 * on what directive is actually being processed.
1903 do_xifdef (pfile
, keyword
)
1905 const struct directive
*keyword
;
1908 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1911 enum cpp_token token
;
1912 int start_of_file
= 0;
1913 U_CHAR
*control_macro
= 0;
1914 int old_written
= CPP_WRITTEN (pfile
);
1916 /* Detect a #ifndef at start of file (not counting comments). */
1917 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1918 start_of_file
= pfile
->only_seen_white
== 2;
1920 pfile
->no_macro_expand
++;
1921 token
= get_directive_token (pfile
);
1922 pfile
->no_macro_expand
--;
1924 ident
= pfile
->token_buffer
+ old_written
;
1925 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1926 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1928 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1930 skip
= (keyword
->type
== T_IFDEF
);
1931 if (! CPP_TRADITIONAL (pfile
))
1932 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1934 else if (token
== CPP_NAME
)
1936 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
);
1937 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1938 if (start_of_file
&& !skip
)
1940 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1941 bcopy (ident
, control_macro
, ident_length
+ 1);
1943 if (hp
!= NULL
&& hp
->type
== T_POISON
)
1945 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1951 skip
= (keyword
->type
== T_IFDEF
);
1952 if (! CPP_TRADITIONAL (pfile
))
1953 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1956 if (!CPP_TRADITIONAL (pfile
))
1958 cpp_skip_hspace (pfile
);
1960 if (c
!= EOF
&& c
!= '\n')
1961 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1963 skip_rest_of_line (pfile
);
1965 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1969 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1970 If this is a #ifndef starting at the beginning of a file,
1971 CONTROL_MACRO is the macro name tested by the #ifndef.
1972 Otherwise, CONTROL_MACRO is 0. */
1975 conditional_skip (pfile
, skip
, type
, control_macro
)
1978 enum node_type type
;
1979 U_CHAR
*control_macro
;
1981 IF_STACK_FRAME
*temp
;
1983 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1984 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1985 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1986 temp
->next
= pfile
->if_stack
;
1987 temp
->control_macro
= control_macro
;
1988 pfile
->if_stack
= temp
;
1990 pfile
->if_stack
->type
= type
;
1993 skip_if_group (pfile
);
1996 ++pfile
->if_stack
->if_succeeded
;
1997 output_line_command (pfile
, same_file
);
2001 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2002 return 0 if skipping should continue, 1 if it should halt. Also
2003 adjusts the if_stack as appropriate.
2004 The `#' has been read, but not the identifier. */
2007 consider_directive_while_skipping (pfile
, stack
)
2009 IF_STACK_FRAME
*stack
;
2011 long ident_len
, ident
;
2012 const struct directive
*kt
;
2013 IF_STACK_FRAME
*temp
;
2015 cpp_skip_hspace (pfile
);
2017 ident
= CPP_WRITTEN (pfile
);
2018 parse_name (pfile
, GETC());
2019 ident_len
= CPP_WRITTEN (pfile
) - ident
;
2021 CPP_SET_WRITTEN (pfile
, ident
);
2023 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
2024 if (kt
->length
== ident_len
2025 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
2031 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
2032 temp
->next
= pfile
->if_stack
;
2033 pfile
->if_stack
= temp
;
2034 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
2035 temp
->type
= kt
->type
;
2039 if (pfile
->if_stack
!= stack
)
2040 validate_else (pfile
, "#else");
2043 if (pfile
->if_stack
== stack
)
2047 pfile
->if_stack
->type
= kt
->type
;
2052 if (pfile
->if_stack
!= stack
)
2053 validate_else (pfile
, "#endif");
2055 if (pfile
->if_stack
== stack
)
2058 temp
= pfile
->if_stack
;
2059 pfile
->if_stack
= temp
->next
;
2067 /* Don't let erroneous code go by. */
2068 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
2069 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
2073 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2074 * leaves input ptr at the sharp sign found.
2077 skip_if_group (pfile
)
2081 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
2082 U_CHAR
*beg_of_line
;
2085 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2087 CPP_PUTS (pfile
, "#failed\n", 8);
2089 output_line_command (pfile
, same_file
);
2092 old_written
= CPP_WRITTEN (pfile
);
2096 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
2098 if (! CPP_TRADITIONAL (pfile
))
2099 cpp_skip_hspace (pfile
);
2103 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2104 CPP_PUTC (pfile
, c
);
2105 CPP_BUMP_LINE (pfile
);
2110 if (consider_directive_while_skipping (pfile
, save_if_stack
))
2114 return; /* Caller will issue error. */
2117 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2119 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
2120 copy_rest_of_line (pfile
);
2124 copy_rest_of_line (pfile
);
2125 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
2130 return; /* Caller will issue error. */
2134 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2136 CPP_PUTC (pfile
, c
);
2139 CPP_BUMP_LINE (pfile
);
2143 /* Back up to the beginning of this line. Caller will process the
2145 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
2146 pfile
->only_seen_white
= 1;
2147 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2149 CPP_PUTS (pfile
, "#endfailed\n", 11);
2155 * handle a #else directive. Do this by just continuing processing
2156 * without changing if_stack ; this is so that the error message
2157 * for missing #endif's etc. will point to the original #if. It
2158 * is possible that something different would be better.
2162 do_else (pfile
, keyword
)
2164 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2166 validate_else (pfile
, "#else");
2167 skip_rest_of_line (pfile
);
2169 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2171 cpp_error (pfile
, "`#else' not within a conditional");
2176 /* #ifndef can't have its special treatment for containing the whole file
2177 if it has a #else clause. */
2178 pfile
->if_stack
->control_macro
= 0;
2180 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
2182 cpp_error (pfile
, "`#else' after `#else'");
2183 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
2184 "the conditional began here");
2186 pfile
->if_stack
->type
= T_ELSE
;
2189 if (pfile
->if_stack
->if_succeeded
)
2190 skip_if_group (pfile
);
2193 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
2194 output_line_command (pfile
, same_file
);
2200 * unstack after #endif command
2204 do_endif (pfile
, keyword
)
2206 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2208 validate_else (pfile
, "#endif");
2209 skip_rest_of_line (pfile
);
2211 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2212 cpp_error (pfile
, "`#endif' not within a conditional");
2215 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2216 pfile
->if_stack
= temp
->next
;
2217 if (temp
->control_macro
!= 0)
2219 /* This #endif matched a #ifndef at the start of the file.
2220 See if it is at the end of the file. */
2223 parse_set_mark (pfile
);
2227 cpp_skip_hspace (pfile
);
2232 parse_goto_mark (pfile
);
2236 /* This #endif ends a #ifndef
2237 that contains all of the file (aside from whitespace).
2238 Arrange not to include the file again
2239 if the macro that was tested is defined. */
2240 struct cpp_buffer
*ip
;
2241 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2242 if (ip
->fname
!= NULL
)
2244 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2248 output_line_command (pfile
, same_file
);
2253 /* Issue -pedantic warning for text which is not a comment following
2254 an #else or #endif. Do not warn in system headers, as this is harmless
2255 and very common on old systems. */
2258 validate_else (pfile
, directive
)
2260 const char *directive
;
2262 if (! CPP_PEDANTIC (pfile
) || CPP_BUFFER (pfile
)->system_header_p
)
2265 cpp_skip_hspace (pfile
);
2266 if (PEEKC () != '\n')
2268 "text following `%s' violates ANSI standard", directive
);
2271 /* Convert T_IF, etc. to a string. Used in error messages. */
2273 if_directive_name (pfile
, ifs
)
2275 struct if_stack
*ifs
;
2279 case T_IF
: return "#if";
2280 case T_IFDEF
: return "#ifdef";
2281 case T_IFNDEF
: return "#ifndef";
2282 case T_ELIF
: return "#elif";
2283 case T_ELSE
: return "#else";
2285 cpp_ice (pfile
, "impossible if_stack->type value %d", ifs
->type
);
2290 /* Get the next token, and add it to the text in pfile->token_buffer.
2291 Return the kind of token we got. */
2294 cpp_get_token (pfile
)
2297 register int c
, c2
, c3
;
2298 enum cpp_token token
;
2299 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2305 if (CPP_BUFFER (pfile
)->manual_pop
)
2306 /* If we've been reading from redirected input, the
2307 frontend will pop the buffer. */
2309 else if (CPP_BUFFER (pfile
)->seen_eof
)
2311 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2314 cpp_pop_buffer (pfile
);
2319 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2320 struct if_stack
*ifs
, *nifs
;
2322 /* Unwind the conditional stack and generate error messages. */
2323 for (ifs
= pfile
->if_stack
;
2324 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
2327 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
2328 "unterminated `%s' conditional",
2329 if_directive_name (pfile
, ifs
));
2334 pfile
->if_stack
= ifs
;
2336 if (CPP_BUFFER (pfile
)->nominal_fname
2337 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2339 /* We're about to return from an #include file.
2340 Emit #line information now (as part of the CPP_POP) result.
2341 But the #line refers to the file we will pop to. */
2342 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2343 CPP_BUFFER (pfile
) = next_buf
;
2344 pfile
->input_stack_listing_current
= 0;
2345 output_line_command (pfile
, leave_file
);
2346 CPP_BUFFER (pfile
) = cur_buffer
;
2349 CPP_BUFFER (pfile
)->seen_eof
= 1;
2358 if (PEEKC () == '=')
2362 if (opts
->discard_comments
)
2363 c
= skip_comment (pfile
, c
);
2365 c
= copy_comment (pfile
, c
);
2369 /* Comments are equivalent to spaces.
2370 For -traditional, a comment is equivalent to nothing. */
2371 if (opts
->traditional
|| !opts
->discard_comments
)
2375 CPP_PUTC (pfile
, c
);
2380 if (!pfile
->only_seen_white
)
2382 /* -traditional directives are recognized only with the # in
2384 XXX Layering violation. */
2385 if (CPP_TRADITIONAL (pfile
)
2386 && CPP_BUFFER (pfile
)->cur
- CPP_BUFFER (pfile
)->line_base
!= 1)
2388 if (handle_directive (pfile
))
2389 return CPP_DIRECTIVE
;
2390 pfile
->only_seen_white
= 0;
2396 parse_string (pfile
, c
);
2397 pfile
->only_seen_white
= 0;
2398 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2401 if (!opts
->dollars_in_ident
)
2406 if (opts
->cplusplus
&& PEEKC () == ':')
2414 if (c2
== c
|| c2
== '=')
2423 if (PEEKC () == '=')
2429 if (c2
== '-' && opts
->chill
)
2430 goto comment
; /* Chill style comment */
2431 if (c2
== '-' || c2
== '=')
2435 if (opts
->cplusplus
&& PEEKN (1) == '*')
2437 /* In C++, there's a ->* operator. */
2439 pfile
->only_seen_white
= 0;
2440 CPP_RESERVE (pfile
, 4);
2441 CPP_PUTC_Q (pfile
, c
);
2442 CPP_PUTC_Q (pfile
, GETC ());
2443 CPP_PUTC_Q (pfile
, GETC ());
2444 CPP_NUL_TERMINATE_Q (pfile
);
2452 if (pfile
->parsing_include_directive
)
2456 CPP_PUTC (pfile
, c
);
2460 if (c
== '\n' || c
== EOF
)
2463 "missing '>' in `#include <FILENAME>'");
2468 if (!CPP_BUFFER (pfile
)->has_escapes
)
2470 /* Backslash newline is replaced by nothing. */
2471 CPP_ADJUST_WRITTEN (pfile
, -1);
2472 CPP_BUMP_LINE (pfile
);
2476 /* We might conceivably get \r- or \r<space> in
2477 here. Just delete 'em. */
2479 if (d
!= '-' && d
!= ' ')
2480 cpp_ice (pfile
, "unrecognized escape \\r%c", d
);
2481 CPP_ADJUST_WRITTEN (pfile
, -1);
2487 /* else fall through */
2492 /* GNU C++ supports MIN and MAX operators <? and >?. */
2493 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2496 CPP_RESERVE (pfile
, 4);
2497 CPP_PUTC (pfile
, c
);
2498 CPP_PUTC (pfile
, c2
);
2501 CPP_PUTC_Q (pfile
, GETC ());
2502 CPP_NUL_TERMINATE_Q (pfile
);
2503 pfile
->only_seen_white
= 0;
2510 CPP_RESERVE(pfile
, 2);
2511 CPP_PUTC_Q (pfile
, '.');
2516 /* In C++ there's a .* operator. */
2517 if (opts
->cplusplus
&& c2
== '*')
2520 if (c2
== '.' && PEEKN(1) == '.')
2522 CPP_RESERVE(pfile
, 4);
2523 CPP_PUTC_Q (pfile
, '.');
2524 CPP_PUTC_Q (pfile
, '.');
2525 CPP_PUTC_Q (pfile
, '.');
2527 CPP_NUL_TERMINATE_Q (pfile
);
2528 pfile
->only_seen_white
= 0;
2535 pfile
->only_seen_white
= 0;
2536 CPP_RESERVE(pfile
, 3);
2537 CPP_PUTC_Q (pfile
, c
);
2538 CPP_PUTC_Q (pfile
, GETC ());
2539 CPP_NUL_TERMINATE_Q (pfile
);
2544 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2546 CPP_PUTC (pfile
, c
);
2552 case '0': case '1': case '2': case '3': case '4':
2553 case '5': case '6': case '7': case '8': case '9':
2558 CPP_RESERVE (pfile
, 2);
2559 CPP_PUTC_Q (pfile
, c
);
2563 if (!is_idchar(c
) && c
!= '.'
2564 && ((c2
!= 'e' && c2
!= 'E'
2565 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2566 || (c
!= '+' && c
!= '-')))
2571 CPP_NUL_TERMINATE_Q (pfile
);
2572 pfile
->only_seen_white
= 0;
2574 case 'b': case 'c': case 'd': case 'h': case 'o':
2575 case 'B': case 'C': case 'D': case 'H': case 'O':
2576 if (opts
->chill
&& PEEKC () == '\'')
2578 pfile
->only_seen_white
= 0;
2579 CPP_RESERVE (pfile
, 2);
2580 CPP_PUTC_Q (pfile
, c
);
2581 CPP_PUTC_Q (pfile
, '\'');
2587 goto chill_number_eof
;
2590 CPP_PUTC (pfile
, c
);
2594 CPP_RESERVE (pfile
, 2);
2595 CPP_PUTC_Q (pfile
, c
);
2596 CPP_NUL_TERMINATE_Q (pfile
);
2603 CPP_NUL_TERMINATE (pfile
);
2610 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2611 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2612 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2613 case 'x': case 'y': case 'z':
2614 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2615 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2616 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2621 unsigned char *ident
;
2622 int before_name_written
= CPP_WRITTEN (pfile
);
2624 parse_name (pfile
, c
);
2625 pfile
->only_seen_white
= 0;
2626 if (pfile
->no_macro_expand
)
2628 ident
= pfile
->token_buffer
+ before_name_written
;
2629 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2630 hp
= cpp_lookup (pfile
, ident
, ident_len
);
2633 if (hp
->type
== T_DISABLED
)
2635 if (pfile
->output_escapes
)
2636 { /* Return "\r-IDENT", followed by '\0'. */
2638 CPP_RESERVE (pfile
, 3);
2639 ident
= pfile
->token_buffer
+ before_name_written
;
2640 CPP_ADJUST_WRITTEN (pfile
, 2);
2641 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2648 /* If macro wants an arglist, verify that a '(' follows. */
2649 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2651 int macbuf_whitespace
= 0;
2653 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2655 U_CHAR
*point
= CPP_BUFFER (pfile
)->cur
;
2658 cpp_skip_hspace (pfile
);
2665 if (point
!= CPP_BUFFER (pfile
)->cur
)
2666 macbuf_whitespace
= 1;
2670 goto not_macro_call
;
2671 cpp_pop_buffer (pfile
);
2674 parse_set_mark (pfile
);
2677 cpp_skip_hspace (pfile
);
2684 parse_goto_mark (pfile
);
2690 if (macbuf_whitespace
)
2691 CPP_PUTC (pfile
, ' ');
2695 /* This is now known to be a macro call.
2696 Expand the macro, reading arguments as needed,
2697 and push the expansion on the input stack. */
2698 macroexpand (pfile
, hp
);
2699 CPP_SET_WRITTEN (pfile
, before_name_written
);
2703 case ' ': case '\t': case '\v':
2706 CPP_PUTC (pfile
, c
);
2708 if (c
== EOF
|| !is_hspace(c
))
2715 if (CPP_BUFFER (pfile
)->has_escapes
)
2720 if (pfile
->output_escapes
)
2721 CPP_PUTS (pfile
, "\r-", 2);
2722 parse_name (pfile
, GETC ());
2727 CPP_RESERVE (pfile
, 2);
2728 if (pfile
->output_escapes
)
2729 CPP_PUTC_Q (pfile
, '\r');
2730 CPP_PUTC_Q (pfile
, c
);
2735 cpp_ice (pfile
, "unrecognized escape \\r%c", c
);
2741 /* Backslash newline is ignored. */
2742 CPP_BUMP_LINE (pfile
);
2747 CPP_PUTC (pfile
, c
);
2748 if (pfile
->only_seen_white
== 0)
2749 pfile
->only_seen_white
= 1;
2750 CPP_BUMP_LINE (pfile
);
2751 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2754 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2755 output_line_command (pfile
, same_file
);
2759 case '(': token
= CPP_LPAREN
; goto char1
;
2760 case ')': token
= CPP_RPAREN
; goto char1
;
2761 case '{': token
= CPP_LBRACE
; goto char1
;
2762 case '}': token
= CPP_RBRACE
; goto char1
;
2763 case ',': token
= CPP_COMMA
; goto char1
;
2764 case ';': token
= CPP_SEMICOLON
; goto char1
;
2770 pfile
->only_seen_white
= 0;
2771 CPP_PUTC (pfile
, c
);
2777 /* Like cpp_get_token, but skip spaces and comments. */
2780 cpp_get_non_space_token (pfile
)
2783 int old_written
= CPP_WRITTEN (pfile
);
2786 enum cpp_token token
= cpp_get_token (pfile
);
2787 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2788 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2790 CPP_SET_WRITTEN (pfile
, old_written
);
2794 /* Parse an identifier starting with C. */
2797 parse_name (pfile
, c
)
2809 if (c
== '$' && CPP_PEDANTIC (pfile
))
2810 cpp_pedwarn (pfile
, "`$' in identifier");
2812 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2813 CPP_PUTC_Q (pfile
, c
);
2818 CPP_NUL_TERMINATE_Q (pfile
);
2822 /* Parse a string starting with C. A single quoted string is treated
2823 like a double -- some programs (e.g., troff) are perverse this way.
2824 (However, a single quoted string is not allowed to extend over
2827 parse_string (pfile
, c
)
2831 long start_line
, start_column
;
2833 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2835 CPP_PUTC (pfile
, c
);
2841 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2843 /* try harder: this string crosses a macro expansion
2844 boundary. This can happen naturally if -traditional.
2845 Otherwise, only -D can make a macro with an unmatched
2847 cpp_pop_buffer (pfile
);
2851 cpp_error_with_line (pfile
, start_line
, start_column
,
2852 "unterminated string or character constant");
2853 if (pfile
->multiline_string_line
!= start_line
2854 && pfile
->multiline_string_line
!= 0)
2855 cpp_error_with_line (pfile
,
2856 pfile
->multiline_string_line
, -1,
2857 "possible real start of unterminated constant");
2858 pfile
->multiline_string_line
= 0;
2861 CPP_PUTC (pfile
, cc
);
2865 CPP_BUMP_LINE (pfile
);
2868 /* In Fortran and assembly language, silently terminate
2869 strings of either variety at end of line. This is a
2870 kludge around not knowing where comments are in these
2872 if (CPP_OPTIONS (pfile
)->lang_fortran
2873 || CPP_OPTIONS (pfile
)->lang_asm
)
2875 /* Character constants may not extend over multiple lines.
2876 In Standard C, neither may strings. We accept multiline
2877 strings as an extension. */
2880 cpp_error_with_line (pfile
, start_line
, start_column
,
2881 "unterminated character constant");
2884 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2885 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2886 "string constant runs past end of line");
2887 if (pfile
->multiline_string_line
== 0)
2888 pfile
->multiline_string_line
= start_line
;
2892 CPP_ADJUST_WRITTEN (pfile
, -1);
2893 if (CPP_BUFFER (pfile
)->has_escapes
)
2895 cpp_ice (pfile
, "\\r escape inside string constant");
2899 /* Backslash newline is replaced by nothing at all. */
2900 CPP_BUMP_LINE (pfile
);
2906 CPP_PUTC (pfile
, cc
);
2918 /* Read an assertion into the token buffer, converting to
2919 canonical form: `#predicate(a n swe r)' The next non-whitespace
2920 character to read should be the first letter of the predicate.
2921 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2922 with answer (see callers for why). In case of 0, an error has been
2925 parse_assertion (pfile
)
2929 cpp_skip_hspace (pfile
);
2931 if (! is_idstart(c
))
2933 cpp_error (pfile
, "assertion predicate is not an identifier");
2936 CPP_PUTC(pfile
, '#');
2938 parse_name(pfile
, c
);
2943 if (is_hspace(c
) || c
== '\r')
2944 cpp_skip_hspace (pfile
);
2950 CPP_PUTC(pfile
, '(');
2953 while ((c
= GETC()) != ')')
2959 CPP_PUTC(pfile
, ' ');
2963 else if (c
== '\n' || c
== EOF
)
2965 if (c
== '\n') FORWARD(-1);
2966 cpp_error (pfile
, "un-terminated assertion answer");
2970 /* \r cannot be a macro escape here. */
2971 CPP_BUMP_LINE (pfile
);
2974 CPP_PUTC (pfile
, c
);
2979 if (pfile
->limit
[-1] == ' ')
2980 pfile
->limit
[-1] = ')';
2981 else if (pfile
->limit
[-1] == '(')
2983 cpp_error (pfile
, "empty token sequence in assertion");
2987 CPP_PUTC (pfile
, ')');
2989 CPP_NUL_TERMINATE (pfile
);
2994 do_assert (pfile
, keyword
)
2996 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3000 HASHNODE
*base
, *this;
3001 int baselen
, thislen
;
3003 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3004 && !CPP_BUFFER (pfile
)->system_header_p
)
3005 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
3007 cpp_skip_hspace (pfile
);
3008 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3009 ret
= parse_assertion (pfile
);
3014 cpp_error (pfile
, "missing token-sequence in `#assert'");
3018 cpp_skip_hspace (pfile
);
3020 if (c
!= EOF
&& c
!= '\n')
3022 cpp_error (pfile
, "junk at end of `#assert'");
3026 thislen
= strlen (sym
);
3027 baselen
= index (sym
, '(') - sym
;
3028 this = cpp_lookup (pfile
, sym
, thislen
);
3031 cpp_warning (pfile
, "`%s' re-asserted", sym
);
3035 base
= cpp_lookup (pfile
, sym
, baselen
);
3037 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0);
3038 else if (base
->type
!= T_ASSERT
)
3040 /* Token clash - but with what?! */
3041 cpp_ice (pfile
, "base->type != T_ASSERT in do_assert");
3045 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
3046 (char *)base
->value
.aschain
);
3047 base
->value
.aschain
= this;
3049 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3053 skip_rest_of_line (pfile
);
3054 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3059 do_unassert (pfile
, keyword
)
3061 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3065 long baselen
, thislen
;
3066 HASHNODE
*base
, *this, *next
;
3068 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3069 && !CPP_BUFFER (pfile
)->system_header_p
)
3070 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
3072 cpp_skip_hspace (pfile
);
3074 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3075 ret
= parse_assertion (pfile
);
3079 cpp_skip_hspace (pfile
);
3081 if (c
!= EOF
&& c
!= '\n')
3082 cpp_error (pfile
, "junk at end of `#unassert'");
3084 thislen
= strlen (sym
);
3087 base
= cpp_lookup (pfile
, sym
, thislen
);
3089 goto error
; /* It isn't an error to #undef what isn't #defined,
3090 so it isn't an error to #unassert what isn't
3091 #asserted either. */
3093 for (this = base
->value
.aschain
; this; this = next
)
3095 next
= this->value
.aschain
;
3096 delete_macro (this);
3098 delete_macro (base
);
3102 baselen
= index (sym
, '(') - sym
;
3103 base
= cpp_lookup (pfile
, sym
, baselen
);
3104 if (! base
) goto error
;
3105 this = cpp_lookup (pfile
, sym
, thislen
);
3106 if (! this) goto error
;
3109 while (next
->value
.aschain
!= this)
3110 next
= next
->value
.aschain
;
3112 next
->value
.aschain
= this->value
.aschain
;
3113 delete_macro (this);
3115 if (base
->value
.aschain
== NULL
)
3116 delete_macro (base
); /* Last answer for this predicate deleted. */
3119 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3122 skip_rest_of_line (pfile
);
3123 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3127 /* Process STR as if it appeared as the body of an #unassert. */
3129 cpp_unassert (pfile
, str
)
3133 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3135 do_assert (pfile
, NULL
);
3136 cpp_pop_buffer (pfile
);
3141 cpp_read_check_assertion (pfile
)
3146 long written
= CPP_WRITTEN (pfile
);
3148 FORWARD (1); /* Skip '#' */
3149 cpp_skip_hspace (pfile
);
3150 if (! parse_assertion (pfile
))
3154 name
= pfile
->token_buffer
+ written
;
3155 result
= cpp_defined (pfile
, name
, CPP_PWRITTEN (pfile
) - name
);
3158 CPP_SET_WRITTEN (pfile
, written
);
3162 /* Remember the current position of PFILE so it may be returned to
3163 after looking ahead a bit.
3165 Note that when you set a mark, you _must_ return to that mark. You
3166 may not forget about it and continue parsing. You may not pop a
3167 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3171 parse_set_mark (pfile
)
3174 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3175 if (ACTIVE_MARK_P())
3176 cpp_ice (pfile
, "mark active in parse_set_mark");
3178 ip
->mark
= ip
->cur
- ip
->buf
;
3181 /* Backup the current position of PFILE to that saved in its mark,
3182 and clear the mark. */
3185 parse_goto_mark (pfile
)
3188 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3189 if (!ACTIVE_MARK_P())
3190 cpp_ice (pfile
, "mark not active in parse_goto_mark");
3192 ip
->cur
= ip
->buf
+ ip
->mark
;