2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
29 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*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 /* ACTIVE_MARK_P is true if there's a live mark in the buffer, in which
40 case CPP_BUMP_LINE must not be called. */
41 #define ACTIVE_MARK_P() (CPP_BUFFER (pfile)->mark != -1)
43 /* External declarations. */
45 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
47 /* `struct directive' defines one #-directive, including how to handle it. */
51 int length
; /* Length of name */
52 int (*func
) /* Function to handle directive */
53 PARAMS ((cpp_reader
*, const struct directive
*));
54 const char *name
; /* Name of directive */
55 enum node_type type
; /* Code which describes which directive. */
58 /* These functions are declared to return int instead of void since they
59 are going to be placed in a table and some old compilers have trouble with
60 pointers to functions returning void. */
62 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
63 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
64 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
65 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
66 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
67 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
68 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
69 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
70 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
71 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
72 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
73 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
75 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
77 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
78 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
79 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
81 /* Forward declarations. */
83 static void validate_else
PARAMS ((cpp_reader
*, const char *));
84 static HOST_WIDEST_INT eval_if_expression
PARAMS ((cpp_reader
*));
85 static void conditional_skip
PARAMS ((cpp_reader
*, int,
86 enum node_type
, U_CHAR
*));
87 static void skip_if_group
PARAMS ((cpp_reader
*));
88 static void parse_name
PARAMS ((cpp_reader
*, int));
89 static void parse_string
PARAMS ((cpp_reader
*, int));
90 static int parse_assertion
PARAMS ((cpp_reader
*));
91 static const char *if_directive_name
PARAMS ((cpp_reader
*,
93 static enum cpp_token null_underflow
PARAMS ((cpp_reader
*));
94 static int null_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
95 static int skip_comment
PARAMS ((cpp_reader
*, int));
96 static int copy_comment
PARAMS ((cpp_reader
*, int));
97 static void skip_string
PARAMS ((cpp_reader
*, int));
98 static void skip_rest_of_line
PARAMS ((cpp_reader
*));
99 static void cpp_skip_hspace
PARAMS ((cpp_reader
*));
100 static int handle_directive
PARAMS ((cpp_reader
*));
101 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t,
103 const struct directive
*));
104 static int read_line_number
PARAMS ((cpp_reader
*, int *));
105 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
106 static int consider_directive_while_skipping
PARAMS ((cpp_reader
*,
108 static void skip_block_comment
PARAMS ((cpp_reader
*));
109 static void skip_line_comment
PARAMS ((cpp_reader
*));
110 static void parse_set_mark
PARAMS ((cpp_reader
*));
111 static void parse_goto_mark
PARAMS ((cpp_reader
*));
112 static int get_macro_name
PARAMS ((cpp_reader
*));
114 /* Here is the actual list of #-directives.
115 This table is ordered by frequency of occurrence; the numbers
116 at the end are directive counts from all the source code I have
117 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
118 linux-2.2.9, and pcmcia-cs-3.0.9). */
120 static const struct directive directive_table
[] = {
122 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
123 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
124 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
125 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
126 { 2, do_if
, "if", T_IF
}, /* 18162 */
127 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
128 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
129 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
130 { 4, do_line
, "line", T_LINE
}, /* 2465 */
131 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
132 { 5, do_error
, "error", T_ERROR
}, /* 475 */
133 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
135 /* Extensions. All deprecated except #warning and #include_next. */
136 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
137 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
138 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
139 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
140 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
141 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
142 #ifdef SCCS_DIRECTIVE
143 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
145 { -1, 0, "", T_UNUSED
}
148 /* Place into PFILE a quoted string representing the string SRC.
149 Caller must reserve enough space in pfile->token_buffer. */
152 quote_string (pfile
, src
)
158 CPP_PUTC_Q (pfile
, '\"');
160 switch ((c
= *src
++))
164 CPP_PUTC_Q (pfile
, c
);
167 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
168 CPP_ADJUST_WRITTEN (pfile
, 4);
174 CPP_PUTC_Q (pfile
, '\\');
175 CPP_PUTC_Q (pfile
, c
);
179 CPP_PUTC_Q (pfile
, '\"');
180 CPP_NUL_TERMINATE_Q (pfile
);
185 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
188 cpp_grow_buffer (pfile
, n
)
192 long old_written
= CPP_WRITTEN (pfile
);
193 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
194 pfile
->token_buffer
= (U_CHAR
*)
195 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
196 CPP_SET_WRITTEN (pfile
, old_written
);
199 /* Process the string STR as if it appeared as the body of a #define.
200 If STR is just an identifier, define it with value 1.
201 If STR has anything after the identifier, then it should
202 be identifier=definition. */
205 cpp_define (pfile
, str
)
212 p
= strchr (str
, '=');
213 /* Copy the entire option so we can modify it.
214 Change the first "=" in the string to a space. If there is none,
215 tack " 1" on the end. Then add a newline and a NUL. */
219 count
= strlen (str
) + 2;
220 buf
= (U_CHAR
*) alloca (count
);
221 memcpy (buf
, str
, count
- 2);
223 buf
[count
- 2] = '\n';
224 buf
[count
- 1] = '\0';
228 count
= strlen (str
) + 4;
229 buf
= (U_CHAR
*) alloca (count
);
230 memcpy (buf
, str
, count
- 4);
231 strcpy (&buf
[count
-4], " 1\n");
234 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
236 do_define (pfile
, NULL
);
237 cpp_pop_buffer (pfile
);
241 /* Process the string STR as if it appeared as the body of a #assert. */
243 cpp_assert (pfile
, str
)
247 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
249 do_assert (pfile
, NULL
);
250 cpp_pop_buffer (pfile
);
254 /* Determine whether the identifier ID, of length LEN, is a defined macro. */
256 cpp_defined (pfile
, id
, len
)
261 HASHNODE
*hp
= cpp_lookup (pfile
, id
, len
);
262 if (hp
&& hp
->type
== T_POISON
)
264 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
270 static enum cpp_token
271 null_underflow (pfile
)
272 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
278 null_cleanup (pbuf
, pfile
)
279 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
280 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
285 /* Skip a C-style block comment. We know it's a comment, and point is
286 at the second character of the starter. */
288 skip_block_comment (pfile
)
295 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
301 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
304 else if (c
== '\n' || c
== '\r')
306 /* \r cannot be a macro escape marker here. */
307 if (!ACTIVE_MARK_P())
308 CPP_BUMP_LINE (pfile
);
310 else if (c
== '/' && prev_c
== '*')
312 else if (c
== '*' && prev_c
== '/'
313 && CPP_OPTIONS (pfile
)->warn_comments
)
314 cpp_warning (pfile
, "`/*' within comment");
320 /* Skip a C++/Chill line comment. We know it's a comment, and point
321 is at the second character of the initiator. */
323 skip_line_comment (pfile
)
331 /* We don't have to worry about EOF in here. */
334 /* Don't consider final '\n' to be part of comment. */
340 /* \r cannot be a macro escape marker here. */
341 if (!ACTIVE_MARK_P())
342 CPP_BUMP_LINE (pfile
);
343 if (CPP_OPTIONS (pfile
)->warn_comments
)
344 cpp_warning (pfile
, "backslash-newline within line comment");
349 /* Skip a comment - C, C++, or Chill style. M is the first character
350 of the comment marker. If this really is a comment, skip to its
351 end and return ' '. If this is not a comment, return M (which will
355 skip_comment (pfile
, m
)
359 if (m
== '/' && PEEKC() == '*')
361 skip_block_comment (pfile
);
364 else if (m
== '/' && PEEKC() == '/')
366 if (CPP_BUFFER (pfile
)->system_header_p
)
368 /* We silently allow C++ comments in system headers, irrespective
369 of conformance mode, because lots of busted systems do that
370 and trying to clean it up in fixincludes is a nightmare. */
371 skip_line_comment (pfile
);
374 else if (CPP_OPTIONS (pfile
)->cplusplus_comments
)
376 if (CPP_OPTIONS (pfile
)->c89
377 && CPP_PEDANTIC (pfile
)
378 && ! CPP_BUFFER (pfile
)->warned_cplusplus_comments
)
381 "C++ style comments are not allowed in ISO C89");
383 "(this will be reported only once per input file)");
384 CPP_BUFFER (pfile
)->warned_cplusplus_comments
= 1;
386 skip_line_comment (pfile
);
392 else if (m
== '-' && PEEKC() == '-'
393 && CPP_OPTIONS (pfile
)->chill
)
395 skip_line_comment (pfile
);
402 /* Identical to skip_comment except that it copies the comment into the
403 token_buffer. This is used if !discard_comments. */
405 copy_comment (pfile
, m
)
409 U_CHAR
*start
= CPP_BUFFER (pfile
)->cur
; /* XXX Layering violation */
412 if (skip_comment (pfile
, m
) == m
)
415 limit
= CPP_BUFFER (pfile
)->cur
;
416 CPP_RESERVE (pfile
, limit
- start
+ 2);
417 CPP_PUTC_Q (pfile
, m
);
418 for (; start
<= limit
; start
++)
420 CPP_PUTC_Q (pfile
, *start
);
425 /* Skip whitespace \-newline and comments. Does not macro-expand. */
428 cpp_skip_hspace (pfile
)
437 else if (is_hspace(c
))
439 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
440 cpp_pedwarn (pfile
, "%s in preprocessing directive",
441 c
== '\f' ? "formfeed" : "vertical tab");
445 /* \r is a backslash-newline marker if !has_escapes, and
446 a deletable-whitespace or no-reexpansion marker otherwise. */
447 if (CPP_BUFFER (pfile
)->has_escapes
)
455 CPP_BUMP_LINE (pfile
);
457 else if (c
== '/' || c
== '-')
459 c
= skip_comment (pfile
, c
);
469 /* Read and discard the rest of the current line. */
472 skip_rest_of_line (pfile
)
486 if (! CPP_BUFFER (pfile
)->has_escapes
)
487 CPP_BUMP_LINE (pfile
);
492 skip_string (pfile
, c
);
497 skip_comment (pfile
, c
);
502 if (CPP_PEDANTIC (pfile
))
503 cpp_pedwarn (pfile
, "%s in preprocessing directive",
504 c
== '\f' ? "formfeed" : "vertical tab");
511 /* Handle a possible # directive.
512 '#' has already been read. */
515 handle_directive (pfile
)
519 register const struct directive
*kt
;
522 long old_written
= CPP_WRITTEN (pfile
);
524 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
526 cpp_ice (pfile
, "handle_directive called on macro buffer");
530 cpp_skip_hspace (pfile
);
533 /* # followed by a number is equivalent to #line. Do not recognize
534 this form in assembly language source files. Complain about this
535 form if we're being pedantic, but not if this is regurgitated
536 input (preprocessed or fed back in by the C++ frontend). */
537 if (c
>= '0' && c
<= '9')
539 if (CPP_OPTIONS (pfile
)->lang_asm
)
542 if (CPP_PEDANTIC (pfile
)
543 && ! CPP_PREPROCESSED (pfile
)
544 && ! CPP_BUFFER (pfile
)->manual_pop
)
545 cpp_pedwarn (pfile
, "`#' followed by integer");
546 do_line (pfile
, NULL
);
550 /* If we are rescanning preprocessed input, don't obey any directives
552 if (CPP_PREPROCESSED (pfile
))
555 /* Now find the directive name. */
556 CPP_PUTC (pfile
, '#');
557 parse_name (pfile
, GETC());
558 ident
= pfile
->token_buffer
+ old_written
+ 1;
559 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
560 if (ident_length
== 0)
562 /* A line of just `#' becomes blank. A line with something
563 other than an identifier after the # is reparsed as a non-
565 CPP_SET_WRITTEN (pfile
, old_written
);
566 return (PEEKC() == '\n');
569 /* Decode the keyword and call the appropriate expansion routine. */
570 for (kt
= directive_table
; ; kt
++)
573 /* # identifier, but not a legit directive. Pass onward as a
574 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
576 if (kt
->length
== ident_length
577 && !strncmp (kt
->name
, ident
, ident_length
))
581 CPP_SET_WRITTEN (pfile
, old_written
);
583 if (pfile
->no_directives
)
585 cpp_error (pfile
, "`#%s' may not be used inside a macro argument",
587 skip_rest_of_line (pfile
);
590 (*kt
->func
) (pfile
, kt
);
595 /* Pass a directive through to the output file.
596 BUF points to the contents of the directive, as a contiguous string.
597 LEN is the length of the string pointed to by BUF.
598 KEYWORD is the keyword-table entry for the directive. */
601 pass_thru_directive (buf
, len
, pfile
, keyword
)
605 const struct directive
*keyword
;
607 register unsigned keyword_length
= keyword
->length
;
609 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
610 CPP_PUTC_Q (pfile
, '#');
611 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
612 if (len
!= 0 && buf
[0] != ' ')
613 CPP_PUTC_Q (pfile
, ' ');
614 CPP_PUTS_Q (pfile
, buf
, len
);
617 /* Subroutine of do_define: determine the name of the macro to be
621 get_macro_name (pfile
)
626 here
= CPP_WRITTEN (pfile
);
627 pfile
->no_macro_expand
++;
628 if (get_directive_token (pfile
) != CPP_NAME
)
630 cpp_error (pfile
, "`#define' must be followed by an identifier");
634 len
= CPP_WRITTEN (pfile
) - here
;
635 if (len
== 7 && !strncmp (pfile
->token_buffer
+ here
, "defined", 7))
637 cpp_error (pfile
, "`defined' is not a legal macro name");
641 pfile
->no_macro_expand
--;
645 skip_rest_of_line (pfile
);
646 pfile
->no_macro_expand
--;
650 /* Process a #define command.
651 KEYWORD is the keyword-table entry for #define,
652 or NULL for a "predefined" macro. */
655 do_define (pfile
, keyword
)
657 const struct directive
*keyword ATTRIBUTE_UNUSED
;
666 here
= CPP_WRITTEN (pfile
);
667 len
= get_macro_name (pfile
);
671 /* Copy out the name so we can pop the token buffer. */
672 len
= CPP_WRITTEN (pfile
) - here
;
673 sym
= (U_CHAR
*) alloca (len
+ 1);
674 memcpy (sym
, pfile
->token_buffer
+ here
, len
);
676 CPP_SET_WRITTEN (pfile
, here
);
678 /* If the next character, with no intervening whitespace, is '(',
679 then this is a function-like macro. */
683 else if (c
!= '\n' && !is_hspace (c
))
684 /* Otherwise, C99 requires white space after the name. We treat it
685 as an object-like macro if this happens, with a warning. */
686 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'", len
, sym
);
688 def
= create_definition (pfile
, funlike
);
692 if ((hp
= cpp_lookup (pfile
, sym
, len
)) != NULL
)
696 /* Redefining a macro is ok if the definitions are the same. */
697 if (hp
->type
== T_MACRO
)
698 ok
= ! compare_defs (pfile
, def
, hp
->value
.defn
);
699 /* Redefining a constant is ok with -D. */
700 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
701 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
702 /* Otherwise it's not ok. */
705 /* Print the warning or error if it's not ok. */
708 if (hp
->type
== T_POISON
)
709 cpp_error (pfile
, "redefining poisoned `%.*s'", len
, sym
);
711 cpp_pedwarn (pfile
, "`%.*s' redefined", len
, sym
);
712 if (hp
->type
== T_MACRO
&& CPP_OPTIONS (pfile
)->done_initializing
)
714 DEFINITION
*d
= hp
->value
.defn
;
715 cpp_pedwarn_with_file_and_line (pfile
, d
->file
, d
->line
, d
->col
,
716 "this is the location of the previous definition");
719 if (hp
->type
!= T_POISON
)
721 /* Replace the old definition. */
722 if (hp
->type
== T_MACRO
)
723 free_definition (hp
->value
.defn
);
725 hp
->value
.defn
= def
;
729 cpp_install (pfile
, sym
, len
, T_MACRO
, (char *) def
);
731 if (CPP_OPTIONS (pfile
)->debug_output
732 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
733 dump_definition (pfile
, sym
, len
, def
);
734 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
735 pass_thru_directive (sym
, len
, pfile
, keyword
);
741 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
742 If BUFFER != NULL, then use the LENGTH characters in BUFFER
743 as the new input buffer.
744 Return the new buffer, or NULL on failure. */
747 cpp_push_buffer (pfile
, buffer
, length
)
752 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
754 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
756 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
760 new = (cpp_buffer
*) xcalloc (1, sizeof (cpp_buffer
));
762 new->if_stack
= pfile
->if_stack
;
763 new->cleanup
= null_cleanup
;
764 new->underflow
= null_underflow
;
765 new->buf
= new->cur
= buffer
;
766 new->alimit
= new->rlimit
= buffer
+ length
;
769 new->line_base
= NULL
;
771 CPP_BUFFER (pfile
) = new;
776 cpp_pop_buffer (pfile
)
779 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
781 cpp_ice (pfile
, "mark active in cpp_pop_buffer");
782 (*buf
->cleanup
) (buf
, pfile
);
783 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
785 pfile
->buffer_stack_depth
--;
786 return CPP_BUFFER (pfile
);
789 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
790 Pop the buffer when done. */
793 cpp_scan_buffer (pfile
)
796 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
797 enum cpp_token token
;
798 if (CPP_OPTIONS (pfile
)->no_output
)
800 long old_written
= CPP_WRITTEN (pfile
);
801 /* In no-output mode, we can ignore everything but directives. */
804 if (! pfile
->only_seen_white
)
805 skip_rest_of_line (pfile
);
806 token
= cpp_get_token (pfile
);
807 if (token
== CPP_EOF
) /* Should not happen ... */
809 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
811 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
812 != CPP_NULL_BUFFER (pfile
))
813 cpp_pop_buffer (pfile
);
817 CPP_SET_WRITTEN (pfile
, old_written
);
823 token
= cpp_get_token (pfile
);
824 if (token
== CPP_EOF
) /* Should not happen ... */
826 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
828 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
829 != CPP_NULL_BUFFER (pfile
))
830 cpp_pop_buffer (pfile
);
838 * Rescan a string (which may have escape marks) into pfile's buffer.
839 * Place the result in pfile->token_buffer.
841 * The input is copied before it is scanned, so it is safe to pass
842 * it something from the token_buffer that will get overwritten
843 * (because it follows CPP_WRITTEN). This is used by do_include.
847 cpp_expand_to_buffer (pfile
, buf
, length
)
852 register cpp_buffer
*ip
;
858 cpp_ice (pfile
, "length < 0 in cpp_expand_to_buffer");
862 /* Set up the input on the input stack. */
864 buf1
= (U_CHAR
*) alloca (length
+ 1);
865 memcpy (buf1
, buf
, length
);
868 ip
= cpp_push_buffer (pfile
, buf1
, length
);
873 /* Scan the input, create the output. */
874 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
875 CPP_OPTIONS (pfile
)->no_output
= 0;
876 CPP_OPTIONS (pfile
)->no_line_commands
++;
877 cpp_scan_buffer (pfile
);
878 CPP_OPTIONS (pfile
)->no_line_commands
--;
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
;
930 if (CPP_OPTIONS (pfile
)->no_line_commands
931 || CPP_OPTIONS (pfile
)->no_output
)
934 ip
= cpp_file_buffer (pfile
);
935 cpp_buf_line_and_col (ip
, &line
, NULL
);
937 /* If the current file has not changed, we omit the #line if it would
938 appear to be a no-op, and we output a few newlines instead
939 if we want to increase the line number by a small amount.
940 We cannot do this if pfile->lineno is zero, because that means we
941 haven't output any line commands yet. (The very first line command
942 output is a `same_file' command.) */
943 if (file_change
== same_file
&& pfile
->lineno
!= 0)
945 if (line
== pfile
->lineno
)
948 /* If the inherited line number is a little too small,
949 output some newlines instead of a #line command. */
950 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
952 CPP_RESERVE (pfile
, 20);
953 while (line
> pfile
->lineno
)
955 CPP_PUTC_Q (pfile
, '\n');
962 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
963 CPP_PUTS_Q (pfile
, "# ", 2);
965 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
966 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
968 quote_string (pfile
, ip
->nominal_fname
);
969 if (file_change
!= same_file
&& file_change
!= rename_file
)
971 CPP_PUTC_Q (pfile
, ' ');
972 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
974 /* Tell cc1 if following text comes from a system header file. */
975 if (ip
->system_header_p
)
977 CPP_PUTC_Q (pfile
, ' ');
978 CPP_PUTC_Q (pfile
, '3');
980 #ifndef NO_IMPLICIT_EXTERN_C
981 /* Tell cc1plus if following text should be treated as C. */
982 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
984 CPP_PUTC_Q (pfile
, ' ');
985 CPP_PUTC_Q (pfile
, '4');
988 CPP_PUTC_Q (pfile
, '\n');
989 pfile
->lineno
= line
;
993 /* Like cpp_get_token, except that it does not read past end-of-line.
994 Also, horizontal space is skipped, and macros are popped. */
997 get_directive_token (pfile
)
1000 long old_written
= CPP_WRITTEN (pfile
);
1001 enum cpp_token token
;
1005 cpp_skip_hspace (pfile
);
1006 if (PEEKC () == '\n')
1009 token
= cpp_get_token (pfile
);
1010 /* token could be hspace at the beginning of a macro. */
1011 if (token
== CPP_HSPACE
|| token
== CPP_COMMENT
)
1013 CPP_SET_WRITTEN (pfile
, old_written
);
1017 /* token cannot be vspace, it would have been caught above. */
1018 if (token
== CPP_VSPACE
)
1020 cpp_ice (pfile
, "VSPACE in get_directive_token");
1024 /* token cannot be POP unless the buffer is a macro buffer. */
1025 if (token
!= CPP_POP
)
1028 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
1030 cpp_ice (pfile
, "POP of file buffer in get_directive_token");
1034 /* We must pop the buffer by hand, or else cpp_get_token might
1035 hand us white space or newline on the next invocation. */
1036 cpp_pop_buffer (pfile
);
1040 /* Handle #include and #import.
1041 This function expects to see "fname" or <fname> on the input.
1043 The input is normally in part of the output_buffer following
1044 CPP_WRITTEN, and will get overwritten by output_line_command.
1045 I.e. in input file specification has been popped by handle_directive.
1049 do_include (pfile
, keyword
)
1051 const struct directive
*keyword
;
1053 int importing
= (keyword
->type
== T_IMPORT
);
1054 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1055 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1056 int before
; /* included before? */
1058 unsigned char *ftok
;
1061 enum cpp_token token
;
1063 /* Chain of dirs to search */
1064 struct include_hash
*ihash
;
1065 struct file_name_list
*search_start
;
1067 long old_written
= CPP_WRITTEN (pfile
);
1071 if (CPP_PEDANTIC (pfile
))
1074 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1076 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1079 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1080 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1081 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1083 pfile
->import_warning
= 1;
1085 "#import is obsolete, use an #ifndef wrapper in the header file");
1088 pfile
->parsing_include_directive
++;
1089 token
= get_directive_token (pfile
);
1090 pfile
->parsing_include_directive
--;
1092 if (token
== CPP_STRING
)
1094 if (pfile
->token_buffer
[old_written
] == '<')
1098 else if (token
== CPP_NAME
)
1100 /* Support '#include xyz' like VAX-C. It is taken as
1101 '#include <xyz.h>' and generates a warning. */
1103 "`#include filename' is obsolete, use `#include <filename.h>'");
1106 /* Append the missing `.h' to the name. */
1107 CPP_PUTS (pfile
, ".h", 2);
1113 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1114 CPP_SET_WRITTEN (pfile
, old_written
);
1115 skip_rest_of_line (pfile
);
1119 flen
= CPP_WRITTEN (pfile
) - old_written
;
1120 ftok
= (unsigned char *) alloca (flen
+ 1);
1121 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1124 if (get_directive_token (pfile
) != CPP_VSPACE
)
1126 cpp_error (pfile
, "junk at end of `#include'");
1127 skip_rest_of_line (pfile
);
1130 CPP_SET_WRITTEN (pfile
, old_written
);
1134 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1138 if (CPP_OPTIONS (pfile
)->dump_includes
)
1139 pass_thru_directive (ftok
,
1142 - ((token
== CPP_NAME
) ? 2 : 0)
1147 if (token
== CPP_STRING
)
1157 for (fp
= CPP_BUFFER (pfile
);
1158 fp
!= CPP_NULL_BUFFER (pfile
);
1159 fp
= CPP_PREV_BUFFER (fp
))
1160 if (fp
->fname
!= NULL
)
1163 if (fp
== CPP_NULL_BUFFER (pfile
))
1165 cpp_ice (pfile
, "fp == NULL_BUFFER in do_include");
1169 /* For #include_next, skip in the search path past the dir in which the
1170 containing file was found. Treat files specified using an absolute path
1171 as if there are no more directories to search. Treat the primary source
1172 file like any other included source, but generate a warning. */
1173 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1175 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1176 search_start
= fp
->ihash
->foundhere
->next
;
1181 cpp_warning (pfile
, "#include_next in primary source file");
1184 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1187 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1190 search_start
= fp
->actual_dir
;
1193 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1199 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1203 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1210 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1211 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1212 (pfile
->system_include_depth
> 0)))
1214 if (!angle_brackets
)
1215 deps_output (pfile
, ftok
, ' ');
1219 struct file_name_list
*ptr
;
1220 /* If requested as a system header, assume it belongs in
1221 the first system header directory. */
1222 if (CPP_OPTIONS (pfile
)->bracket_include
)
1223 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1225 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1227 p
= (char *) alloca (strlen (ptr
->name
)
1228 + strlen (ftok
) + 2);
1229 if (*ptr
->name
!= '\0')
1231 strcpy (p
, ptr
->name
);
1235 deps_output (pfile
, p
, ' ');
1238 /* If -M was specified, and this header file won't be added to
1239 the dependency list, then don't count this as an error,
1240 because we can still produce correct output. Otherwise, we
1241 can't produce correct output, because there may be
1242 dependencies we need inside the missing file, and we don't
1243 know what directory this missing file exists in. */
1244 else if (CPP_PRINT_DEPS (pfile
)
1245 && (CPP_PRINT_DEPS (pfile
)
1246 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1247 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1249 cpp_error_from_errno (pfile
, ftok
);
1254 /* For -M, add the file to the dependencies on its first inclusion. */
1255 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1256 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1257 deps_output (pfile
, ihash
->name
, ' ');
1259 /* Handle -H option. */
1260 if (CPP_OPTIONS(pfile
)->print_include_names
)
1262 fp
= CPP_BUFFER (pfile
);
1263 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1265 fprintf (stderr
, " %s\n", ihash
->name
);
1268 /* Actually process the file */
1271 ihash
->control_macro
= "";
1273 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1280 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1282 if (finclude (pfile
, fd
, ihash
))
1284 output_line_command (pfile
, enter_file
);
1285 pfile
->only_seen_white
= 2;
1291 /* Subroutine of do_line. Read next token from PFILE without adding it to
1292 the output buffer. If it is a number between 1 and 4, store it in *NUM
1293 and return 1; otherwise, return 0 and complain if we aren't at the end
1294 of the directive. */
1297 read_line_number (pfile
, num
)
1301 long save_written
= CPP_WRITTEN (pfile
);
1302 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1303 enum cpp_token token
= get_directive_token (pfile
);
1304 CPP_SET_WRITTEN (pfile
, save_written
);
1306 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1313 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1314 cpp_error (pfile
, "invalid format `#line' command");
1319 /* Interpret #line command.
1320 Note that the filename string (if any) is treated as if it were an
1321 include filename. That means no escape handling. */
1324 do_line (pfile
, keyword
)
1326 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1328 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1330 long old_written
= CPP_WRITTEN (pfile
);
1331 enum file_change_code file_change
= same_file
;
1332 enum cpp_token token
;
1335 token
= get_directive_token (pfile
);
1337 if (token
!= CPP_NUMBER
)
1339 cpp_error (pfile
, "token after `#line' is not an integer");
1340 goto bad_line_directive
;
1343 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1346 cpp_error (pfile
, "token after `#line' is not an integer");
1347 goto bad_line_directive
;
1349 CPP_SET_WRITTEN (pfile
, old_written
);
1351 if (CPP_PEDANTIC (pfile
) && (new_lineno
<= 0 || new_lineno
> 32767))
1352 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1354 token
= get_directive_token (pfile
);
1356 if (token
== CPP_STRING
)
1358 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1359 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1360 int action_number
= 0;
1362 file_change
= rename_file
;
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. */
1469 name
= (U_CHAR
*) alloca (len
+ 1);
1470 memcpy (name
, buf
, len
);
1473 token
= get_directive_token (pfile
);
1474 if (token
!= CPP_VSPACE
)
1476 cpp_pedwarn (pfile
, "junk on line after #undef");
1477 skip_rest_of_line (pfile
);
1479 CPP_SET_WRITTEN (pfile
, here
);
1481 while ((hp
= cpp_lookup (pfile
, name
, len
)) != NULL
)
1483 /* If we are generating additional info for debugging (with -g) we
1484 need to pass through all effective #undef commands. */
1485 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1486 pass_thru_directive (name
, len
, pfile
, keyword
);
1487 if (hp
->type
== T_POISON
)
1488 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
1491 if (hp
->type
!= T_MACRO
)
1492 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1500 /* Wrap do_undef for -U processing. */
1502 cpp_undef (pfile
, macro
)
1506 /* Copy the string so we can append a newline. */
1507 size_t len
= strlen (macro
);
1508 U_CHAR
*buf
= alloca (len
+ 2);
1509 memcpy (buf
, macro
, len
);
1511 buf
[len
+ 1] = '\0';
1512 if (cpp_push_buffer (pfile
, buf
, len
+ 1))
1514 do_undef (pfile
, NULL
);
1515 cpp_pop_buffer (pfile
);
1520 * Report an error detected by the program we are processing.
1521 * Use the text of the line in the error message.
1522 * (We use error because it prints the filename & line#.)
1526 do_error (pfile
, keyword
)
1528 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1530 U_CHAR
*text
, *limit
;
1532 cpp_skip_hspace (pfile
);
1533 text
= CPP_BUFFER (pfile
)->cur
;
1534 skip_rest_of_line (pfile
);
1535 limit
= CPP_BUFFER (pfile
)->cur
;
1537 cpp_error (pfile
, "#error %.*s", (int)(limit
- text
), text
);
1542 * Report a warning detected by the program we are processing.
1543 * Use the text of the line in the warning message, then continue.
1547 do_warning (pfile
, keyword
)
1549 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1551 U_CHAR
*text
, *limit
;
1553 cpp_skip_hspace (pfile
);
1554 text
= CPP_BUFFER (pfile
)->cur
;
1555 skip_rest_of_line (pfile
);
1556 limit
= CPP_BUFFER (pfile
)->cur
;
1558 if (CPP_PEDANTIC (pfile
))
1559 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1561 cpp_warning (pfile
, "#warning %.*s", (int)(limit
- text
), text
);
1565 /* Report program identification. */
1568 do_ident (pfile
, keyword
)
1570 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1572 long old_written
= CPP_WRITTEN (pfile
);
1574 /* Allow #ident in system headers, since that's not user's fault. */
1575 if (CPP_PEDANTIC (pfile
))
1576 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1578 CPP_PUTS (pfile
, "#ident ", 7);
1580 /* Next token should be a string constant. */
1581 if (get_directive_token (pfile
) == CPP_STRING
)
1582 /* And then a newline. */
1583 if (get_directive_token (pfile
) == CPP_VSPACE
)
1584 /* Good - ship it. */
1587 cpp_error (pfile
, "invalid #ident");
1588 skip_rest_of_line (pfile
);
1589 CPP_SET_WRITTEN (pfile
, old_written
); /* discard directive */
1594 /* Pragmata handling. We handle some of these, and pass the rest on
1595 to the front end. C99 defines three pragmas and says that no macro
1596 expansion is to be performed on them; whether or not macro
1597 expansion happens for other pragmas is implementation defined.
1598 This implementation never macro-expands the text after #pragma.
1600 We currently do not support the _Pragma operator. Support for that
1601 has to be coordinated with the front end. Proposed implementation:
1602 both #pragma blah blah and _Pragma("blah blah") become
1603 __builtin_pragma(blah blah) and we teach the parser about that. */
1605 /* Sub-handlers for the pragmas needing treatment here.
1606 They return 1 if the token buffer is to be popped, 0 if not. */
1607 static int do_pragma_once
PARAMS ((cpp_reader
*));
1608 static int do_pragma_implementation
PARAMS ((cpp_reader
*));
1609 static int do_pragma_poison
PARAMS ((cpp_reader
*));
1610 static int do_pragma_default
PARAMS ((cpp_reader
*));
1613 do_pragma (pfile
, keyword
)
1615 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1620 enum cpp_token token
;
1622 here
= CPP_WRITTEN (pfile
);
1623 CPP_PUTS (pfile
, "#pragma ", 8);
1625 key
= CPP_WRITTEN (pfile
);
1626 pfile
->no_macro_expand
++;
1627 token
= get_directive_token (pfile
);
1628 if (token
!= CPP_NAME
)
1630 if (token
== CPP_VSPACE
)
1636 buf
= pfile
->token_buffer
+ key
;
1637 CPP_PUTC (pfile
, ' ');
1639 #define tokis(x) !strncmp(buf, x, sizeof(x) - 1)
1641 pop
= do_pragma_once (pfile
);
1642 else if (tokis ("implementation"))
1643 pop
= do_pragma_implementation (pfile
);
1644 else if (tokis ("poison"))
1645 pop
= do_pragma_poison (pfile
);
1647 pop
= do_pragma_default (pfile
);
1650 if (get_directive_token (pfile
) != CPP_VSPACE
)
1654 CPP_SET_WRITTEN (pfile
, here
);
1655 pfile
->no_macro_expand
--;
1659 cpp_error (pfile
, "malformed #pragma directive");
1660 skip_rest_of_line (pfile
);
1662 CPP_SET_WRITTEN (pfile
, here
);
1663 pfile
->no_macro_expand
--;
1668 do_pragma_default (pfile
)
1671 while (get_directive_token (pfile
) != CPP_VSPACE
)
1672 CPP_PUTC (pfile
, ' ');
1677 do_pragma_once (pfile
)
1680 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1682 if (ip
->fname
== NULL
)
1684 cpp_ice (pfile
, "ip->fname == NULL in do_pragma_once");
1688 /* Allow #pragma once in system headers, since that's not the user's
1690 if (!ip
->system_header_p
)
1691 cpp_warning (pfile
, "`#pragma once' is obsolete");
1693 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1694 cpp_warning (pfile
, "`#pragma once' outside include file");
1696 ip
->ihash
->control_macro
= ""; /* never repeat */
1702 do_pragma_implementation (pfile
)
1705 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1706 been included yet. */
1707 struct include_hash
*ptr
;
1708 enum cpp_token token
;
1709 long written
= CPP_WRITTEN (pfile
);
1713 token
= get_directive_token (pfile
);
1714 if (token
== CPP_VSPACE
)
1716 else if (token
!= CPP_STRING
)
1718 cpp_error (pfile
, "malformed #pragma implementation");
1722 name
= pfile
->token_buffer
+ written
+ 1;
1723 copy
= xstrdup (name
);
1724 copy
[strlen(copy
)] = '\0'; /* trim trailing quote */
1726 ptr
= include_hash (pfile
, copy
, 0);
1729 "`#pragma implementation' for `%s' appears after file is included",
1736 do_pragma_poison (pfile
)
1739 /* Poison these symbols so that all subsequent usage produces an
1745 enum cpp_token token
;
1747 /* As a rule, don't include #pragma poison commands in output,
1748 unless the user asks for them. */
1749 writeit
= (CPP_OPTIONS (pfile
)->debug_output
1750 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
1751 || CPP_OPTIONS (pfile
)->dump_macros
== dump_names
);
1755 written
= CPP_WRITTEN (pfile
);
1756 token
= get_directive_token (pfile
);
1757 if (token
== CPP_VSPACE
)
1759 if (token
!= CPP_NAME
)
1761 cpp_error (pfile
, "invalid #pragma poison directive");
1762 skip_rest_of_line (pfile
);
1766 p
= pfile
->token_buffer
+ written
;
1768 if ((hp
= cpp_lookup (pfile
, p
, len
)))
1770 if (hp
->type
!= T_POISON
)
1772 cpp_warning (pfile
, "poisoning existing macro `%s'", p
);
1773 free_definition (hp
->value
.defn
);
1775 hp
->type
= T_POISON
;
1779 cpp_install (pfile
, p
, len
, T_POISON
, 0);
1781 CPP_PUTC (pfile
, ' ');
1786 #ifdef SCCS_DIRECTIVE
1787 /* Just ignore #sccs, on systems where we define it at all. */
1790 do_sccs (pfile
, keyword
)
1792 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1794 if (CPP_PEDANTIC (pfile
))
1795 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1796 skip_rest_of_line (pfile
);
1801 /* We've found an `#if' directive. If the only thing before it in
1802 this file is white space, and if it is of the form
1803 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1804 for inclusion of this file. (See redundant_include_p in cppfiles.c
1805 for an explanation of controlling macros.) If so, return a
1806 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1809 detect_if_not_defined (pfile
)
1812 U_CHAR
*control_macro
= 0;
1814 if (pfile
->only_seen_white
== 2)
1817 enum cpp_token token
;
1820 int need_rparen
= 0;
1822 /* Save state required for restore. */
1823 pfile
->no_macro_expand
++;
1824 parse_set_mark (pfile
);
1825 base_offset
= CPP_WRITTEN (pfile
);
1828 if (get_directive_token (pfile
) != CPP_OTHER
1829 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1830 || CPP_PWRITTEN (pfile
)[-1] != '!')
1833 /* ...then `defined', */
1834 token_offset
= CPP_WRITTEN (pfile
);
1835 token
= get_directive_token (pfile
);
1836 if (token
!= CPP_NAME
)
1838 ident
= pfile
->token_buffer
+ token_offset
;
1839 CPP_NUL_TERMINATE (pfile
);
1840 if (strcmp (ident
, "defined"))
1843 /* ...then an optional '(' and the name, */
1844 token_offset
= CPP_WRITTEN (pfile
);
1845 token
= get_directive_token (pfile
);
1846 if (token
== CPP_LPAREN
)
1848 token_offset
= CPP_WRITTEN (pfile
);
1849 token
= get_directive_token (pfile
);
1850 if (token
!= CPP_NAME
)
1854 else if (token
!= CPP_NAME
)
1857 ident
= pfile
->token_buffer
+ token_offset
;
1858 CPP_NUL_TERMINATE (pfile
);
1860 /* ...then the ')', if necessary, */
1861 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1862 /* ...and make sure there's nothing else on the line. */
1863 && get_directive_token (pfile
) == CPP_VSPACE
)
1864 control_macro
= xstrdup (ident
);
1867 CPP_SET_WRITTEN (pfile
, base_offset
);
1868 pfile
->no_macro_expand
--;
1869 parse_goto_mark (pfile
);
1872 return control_macro
;
1876 * handle #if command by
1877 * 1) inserting special `defined' keyword into the hash table
1878 * that gets turned into 0 or 1 by special_symbol (thus,
1879 * if the luser has a symbol called `defined' already, it won't
1880 * work inside the #if command)
1881 * 2) rescan the input into a temporary output buffer
1882 * 3) pass the output buffer to the yacc parser and collect a value
1883 * 4) clean up the mess left from steps 1 and 2.
1884 * 5) call conditional_skip to skip til the next #endif (etc.),
1885 * or not, depending on the value from step 3.
1889 do_if (pfile
, keyword
)
1891 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1893 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1894 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1895 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1900 * handle a #elif directive by not changing if_stack either.
1901 * see the comment above do_else.
1905 do_elif (pfile
, keyword
)
1907 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1909 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1911 cpp_error (pfile
, "`#elif' not within a conditional");
1916 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
1918 cpp_error (pfile
, "`#elif' after `#else'");
1919 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1920 "the conditional began here");
1922 pfile
->if_stack
->type
= T_ELIF
;
1925 if (pfile
->if_stack
->if_succeeded
)
1926 skip_if_group (pfile
);
1929 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1931 skip_if_group (pfile
);
1934 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1935 output_line_command (pfile
, same_file
);
1942 * evaluate a #if expression in BUF, of length LENGTH,
1943 * then parse the result as a C expression and return the value as an int.
1946 static HOST_WIDEST_INT
1947 eval_if_expression (pfile
)
1950 HOST_WIDEST_INT value
;
1951 long old_written
= CPP_WRITTEN (pfile
);
1953 pfile
->parsing_if_directive
++;
1954 value
= cpp_parse_expr (pfile
);
1955 pfile
->parsing_if_directive
--;
1957 skip_rest_of_line (pfile
);
1958 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1964 * routine to handle ifdef/ifndef. Try to look up the symbol,
1965 * then do or don't skip to the #endif/#else/#elif depending
1966 * on what directive is actually being processed.
1970 do_xifdef (pfile
, keyword
)
1972 const struct directive
*keyword
;
1975 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1978 enum cpp_token token
;
1979 int start_of_file
= 0;
1980 U_CHAR
*control_macro
= 0;
1981 int old_written
= CPP_WRITTEN (pfile
);
1983 /* Detect a #ifndef at start of file (not counting comments). */
1984 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1985 start_of_file
= pfile
->only_seen_white
== 2;
1987 pfile
->no_macro_expand
++;
1988 token
= get_directive_token (pfile
);
1989 pfile
->no_macro_expand
--;
1991 ident
= pfile
->token_buffer
+ old_written
;
1992 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1993 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1995 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1997 skip
= (keyword
->type
== T_IFDEF
);
1998 if (! CPP_TRADITIONAL (pfile
))
1999 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
2001 else if (token
== CPP_NAME
)
2003 skip
= cpp_defined (pfile
, ident
, ident_length
);
2004 if (keyword
->type
== T_IFDEF
)
2007 if (start_of_file
&& !skip
)
2009 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
2010 bcopy (ident
, control_macro
, ident_length
+ 1);
2015 skip
= (keyword
->type
== T_IFDEF
);
2016 if (! CPP_TRADITIONAL (pfile
))
2017 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
2020 if (!CPP_TRADITIONAL (pfile
))
2022 cpp_skip_hspace (pfile
);
2024 if (c
!= EOF
&& c
!= '\n')
2025 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
2027 skip_rest_of_line (pfile
);
2029 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
2033 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
2034 If this is a #ifndef starting at the beginning of a file,
2035 CONTROL_MACRO is the macro name tested by the #ifndef.
2036 Otherwise, CONTROL_MACRO is 0. */
2039 conditional_skip (pfile
, skip
, type
, control_macro
)
2042 enum node_type type
;
2043 U_CHAR
*control_macro
;
2045 IF_STACK_FRAME
*temp
;
2047 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
2048 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
2049 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
2050 temp
->next
= pfile
->if_stack
;
2051 temp
->control_macro
= control_macro
;
2052 pfile
->if_stack
= temp
;
2054 pfile
->if_stack
->type
= type
;
2057 skip_if_group (pfile
);
2060 ++pfile
->if_stack
->if_succeeded
;
2061 output_line_command (pfile
, same_file
);
2065 /* Subroutine of skip_if_group. Examine one preprocessing directive and
2066 return 0 if skipping should continue, 1 if it should halt. Also
2067 adjusts the if_stack as appropriate.
2068 The `#' has been read, but not the identifier. */
2071 consider_directive_while_skipping (pfile
, stack
)
2073 IF_STACK_FRAME
*stack
;
2075 long ident_len
, ident
;
2076 const struct directive
*kt
;
2077 IF_STACK_FRAME
*temp
;
2079 cpp_skip_hspace (pfile
);
2081 ident
= CPP_WRITTEN (pfile
);
2082 parse_name (pfile
, GETC());
2083 ident_len
= CPP_WRITTEN (pfile
) - ident
;
2085 CPP_SET_WRITTEN (pfile
, ident
);
2087 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
2088 if (kt
->length
== ident_len
2089 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
2095 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
2096 temp
->next
= pfile
->if_stack
;
2097 pfile
->if_stack
= temp
;
2098 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
2099 temp
->type
= kt
->type
;
2103 if (pfile
->if_stack
!= stack
)
2104 validate_else (pfile
, "#else");
2107 if (pfile
->if_stack
== stack
)
2111 pfile
->if_stack
->type
= kt
->type
;
2116 if (pfile
->if_stack
!= stack
)
2117 validate_else (pfile
, "#endif");
2119 if (pfile
->if_stack
== stack
)
2122 temp
= pfile
->if_stack
;
2123 pfile
->if_stack
= temp
->next
;
2131 /* Don't let erroneous code go by. */
2132 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
2133 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
2137 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2138 * leaves input ptr at the sharp sign found.
2141 skip_if_group (pfile
)
2145 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
2146 U_CHAR
*beg_of_line
;
2149 old_written
= CPP_WRITTEN (pfile
);
2153 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
2155 if (! CPP_TRADITIONAL (pfile
))
2156 cpp_skip_hspace (pfile
);
2160 CPP_BUMP_LINE (pfile
);
2165 if (consider_directive_while_skipping (pfile
, save_if_stack
))
2169 return; /* Caller will issue error. */
2172 skip_rest_of_line (pfile
);
2176 return; /* Caller will issue error. */
2178 CPP_BUMP_LINE (pfile
);
2181 /* Back up to the beginning of this line. Caller will process the
2183 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
2184 pfile
->only_seen_white
= 1;
2188 * handle a #else directive. Do this by just continuing processing
2189 * without changing if_stack ; this is so that the error message
2190 * for missing #endif's etc. will point to the original #if. It
2191 * is possible that something different would be better.
2195 do_else (pfile
, keyword
)
2197 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2199 validate_else (pfile
, "#else");
2200 skip_rest_of_line (pfile
);
2202 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2204 cpp_error (pfile
, "`#else' not within a conditional");
2209 /* #ifndef can't have its special treatment for containing the whole file
2210 if it has a #else clause. */
2211 pfile
->if_stack
->control_macro
= 0;
2213 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
2215 cpp_error (pfile
, "`#else' after `#else'");
2216 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
2217 "the conditional began here");
2219 pfile
->if_stack
->type
= T_ELSE
;
2222 if (pfile
->if_stack
->if_succeeded
)
2223 skip_if_group (pfile
);
2226 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
2227 output_line_command (pfile
, same_file
);
2233 * unstack after #endif command
2237 do_endif (pfile
, keyword
)
2239 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2241 validate_else (pfile
, "#endif");
2242 skip_rest_of_line (pfile
);
2244 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2245 cpp_error (pfile
, "`#endif' not within a conditional");
2248 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2249 pfile
->if_stack
= temp
->next
;
2250 if (temp
->control_macro
!= 0)
2252 /* This #endif matched a #ifndef at the start of the file.
2253 See if it is at the end of the file. */
2256 parse_set_mark (pfile
);
2260 cpp_skip_hspace (pfile
);
2265 parse_goto_mark (pfile
);
2269 /* This #endif ends a #ifndef
2270 that contains all of the file (aside from whitespace).
2271 Arrange not to include the file again
2272 if the macro that was tested is defined. */
2273 struct cpp_buffer
*ip
;
2274 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2275 if (ip
->fname
!= NULL
)
2277 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2281 output_line_command (pfile
, same_file
);
2286 /* Issue -pedantic warning for text which is not a comment following
2287 an #else or #endif. Do not warn in system headers, as this is harmless
2288 and very common on old systems. */
2291 validate_else (pfile
, directive
)
2293 const char *directive
;
2295 if (! CPP_PEDANTIC (pfile
))
2298 cpp_skip_hspace (pfile
);
2299 if (PEEKC () != '\n')
2301 "text following `%s' violates ANSI standard", directive
);
2304 /* Convert T_IF, etc. to a string. Used in error messages. */
2306 if_directive_name (pfile
, ifs
)
2308 struct if_stack
*ifs
;
2312 case T_IF
: return "#if";
2313 case T_IFDEF
: return "#ifdef";
2314 case T_IFNDEF
: return "#ifndef";
2315 case T_ELIF
: return "#elif";
2316 case T_ELSE
: return "#else";
2318 cpp_ice (pfile
, "impossible if_stack->type value %d", ifs
->type
);
2323 /* Get the next token, and add it to the text in pfile->token_buffer.
2324 Return the kind of token we got. */
2327 cpp_get_token (pfile
)
2330 register int c
, c2
, c3
;
2331 enum cpp_token token
;
2332 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2338 if (CPP_BUFFER (pfile
)->manual_pop
)
2339 /* If we've been reading from redirected input, the
2340 frontend will pop the buffer. */
2342 else if (CPP_BUFFER (pfile
)->seen_eof
)
2344 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2347 cpp_pop_buffer (pfile
);
2352 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2353 struct if_stack
*ifs
, *nifs
;
2355 /* Unwind the conditional stack and generate error messages. */
2356 for (ifs
= pfile
->if_stack
;
2357 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
2360 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
2361 "unterminated `%s' conditional",
2362 if_directive_name (pfile
, ifs
));
2367 pfile
->if_stack
= ifs
;
2369 if (CPP_BUFFER (pfile
)->nominal_fname
2370 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2372 /* We're about to return from an #include file.
2373 Emit #line information now (as part of the CPP_POP) result.
2374 But the #line refers to the file we will pop to. */
2375 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2376 CPP_BUFFER (pfile
) = next_buf
;
2377 pfile
->input_stack_listing_current
= 0;
2378 output_line_command (pfile
, leave_file
);
2379 CPP_BUFFER (pfile
) = cur_buffer
;
2382 CPP_BUFFER (pfile
)->seen_eof
= 1;
2391 if (PEEKC () == '=')
2395 if (opts
->discard_comments
)
2396 c
= skip_comment (pfile
, c
);
2398 c
= copy_comment (pfile
, c
);
2402 /* Comments are equivalent to spaces.
2403 For -traditional, a comment is equivalent to nothing. */
2404 if (opts
->traditional
|| !opts
->discard_comments
)
2408 CPP_PUTC (pfile
, c
);
2413 if (pfile
->parsing_if_directive
)
2415 cpp_skip_hspace (pfile
);
2416 parse_assertion (pfile
);
2417 return CPP_ASSERTION
;
2420 if (pfile
->parsing_define_directive
&& ! CPP_TRADITIONAL (pfile
))
2422 CPP_RESERVE (pfile
, 3);
2423 CPP_PUTC_Q (pfile
, '#');
2424 CPP_NUL_TERMINATE_Q (pfile
);
2425 if (PEEKC () != '#')
2426 return CPP_STRINGIZE
;
2429 CPP_PUTC_Q (pfile
, '#');
2430 CPP_NUL_TERMINATE_Q (pfile
);
2431 return CPP_TOKPASTE
;
2434 if (!pfile
->only_seen_white
)
2436 /* -traditional directives are recognized only with the # in
2438 XXX Layering violation. */
2439 if (CPP_TRADITIONAL (pfile
)
2440 && CPP_BUFFER (pfile
)->cur
- CPP_BUFFER (pfile
)->line_base
!= 1)
2442 if (handle_directive (pfile
))
2443 return CPP_DIRECTIVE
;
2444 pfile
->only_seen_white
= 0;
2449 parse_string (pfile
, c
);
2450 pfile
->only_seen_white
= 0;
2451 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2454 if (!opts
->dollars_in_ident
)
2459 if (opts
->cplusplus
&& PEEKC () == ':')
2467 if (c2
== c
|| c2
== '=')
2476 if (PEEKC () == '=')
2482 if (c2
== '-' && opts
->chill
)
2483 goto comment
; /* Chill style comment */
2484 if (c2
== '-' || c2
== '=')
2488 if (opts
->cplusplus
&& PEEKN (1) == '*')
2490 /* In C++, there's a ->* operator. */
2492 pfile
->only_seen_white
= 0;
2493 CPP_RESERVE (pfile
, 4);
2494 CPP_PUTC_Q (pfile
, c
);
2495 CPP_PUTC_Q (pfile
, GETC ());
2496 CPP_PUTC_Q (pfile
, GETC ());
2497 CPP_NUL_TERMINATE_Q (pfile
);
2505 if (pfile
->parsing_include_directive
)
2509 CPP_PUTC (pfile
, c
);
2513 if (c
== '\n' || c
== EOF
)
2516 "missing '>' in `#include <FILENAME>'");
2521 if (!CPP_BUFFER (pfile
)->has_escapes
)
2523 /* Backslash newline is replaced by nothing. */
2524 CPP_ADJUST_WRITTEN (pfile
, -1);
2525 CPP_BUMP_LINE (pfile
);
2529 /* We might conceivably get \r- or \r<space> in
2530 here. Just delete 'em. */
2532 if (d
!= '-' && d
!= ' ')
2533 cpp_ice (pfile
, "unrecognized escape \\r%c", d
);
2534 CPP_ADJUST_WRITTEN (pfile
, -1);
2540 /* else fall through */
2545 /* GNU C++ supports MIN and MAX operators <? and >?. */
2546 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2549 CPP_RESERVE (pfile
, 4);
2550 CPP_PUTC (pfile
, c
);
2551 CPP_PUTC (pfile
, c2
);
2554 CPP_PUTC_Q (pfile
, GETC ());
2555 CPP_NUL_TERMINATE_Q (pfile
);
2556 pfile
->only_seen_white
= 0;
2563 CPP_RESERVE(pfile
, 2);
2564 CPP_PUTC_Q (pfile
, '.');
2569 /* In C++ there's a .* operator. */
2570 if (opts
->cplusplus
&& c2
== '*')
2573 if (c2
== '.' && PEEKN(1) == '.')
2575 CPP_RESERVE(pfile
, 4);
2576 CPP_PUTC_Q (pfile
, '.');
2577 CPP_PUTC_Q (pfile
, '.');
2578 CPP_PUTC_Q (pfile
, '.');
2580 CPP_NUL_TERMINATE_Q (pfile
);
2581 pfile
->only_seen_white
= 0;
2588 pfile
->only_seen_white
= 0;
2589 CPP_RESERVE(pfile
, 3);
2590 CPP_PUTC_Q (pfile
, c
);
2591 CPP_PUTC_Q (pfile
, GETC ());
2592 CPP_NUL_TERMINATE_Q (pfile
);
2597 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2599 CPP_PUTC (pfile
, c
);
2601 parse_string (pfile
, c
);
2602 pfile
->only_seen_white
= 0;
2603 return c
== '\'' ? CPP_WCHAR
: CPP_WSTRING
;
2607 case '0': case '1': case '2': case '3': case '4':
2608 case '5': case '6': case '7': case '8': case '9':
2613 CPP_RESERVE (pfile
, 2);
2614 CPP_PUTC_Q (pfile
, c
);
2618 if (!is_numchar(c
) && c
!= '.'
2619 && ((c2
!= 'e' && c2
!= 'E'
2620 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2621 || (c
!= '+' && c
!= '-')))
2626 CPP_NUL_TERMINATE_Q (pfile
);
2627 pfile
->only_seen_white
= 0;
2629 case 'b': case 'c': case 'd': case 'h': case 'o':
2630 case 'B': case 'C': case 'D': case 'H': case 'O':
2631 if (opts
->chill
&& PEEKC () == '\'')
2633 pfile
->only_seen_white
= 0;
2634 CPP_RESERVE (pfile
, 2);
2635 CPP_PUTC_Q (pfile
, c
);
2636 CPP_PUTC_Q (pfile
, '\'');
2642 goto chill_number_eof
;
2645 CPP_PUTC (pfile
, c
);
2649 CPP_RESERVE (pfile
, 2);
2650 CPP_PUTC_Q (pfile
, c
);
2651 CPP_NUL_TERMINATE_Q (pfile
);
2658 CPP_NUL_TERMINATE (pfile
);
2665 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2666 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2667 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2668 case 'x': case 'y': case 'z':
2669 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2670 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2671 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2676 unsigned char *ident
;
2677 int before_name_written
= CPP_WRITTEN (pfile
);
2679 parse_name (pfile
, c
);
2680 pfile
->only_seen_white
= 0;
2681 if (pfile
->no_macro_expand
)
2683 ident
= pfile
->token_buffer
+ before_name_written
;
2684 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2685 hp
= cpp_lookup (pfile
, ident
, ident_len
);
2688 if (hp
->type
== T_DISABLED
)
2690 if (pfile
->output_escapes
)
2691 { /* Return "\r-IDENT", followed by '\0'. */
2693 CPP_RESERVE (pfile
, 3);
2694 ident
= pfile
->token_buffer
+ before_name_written
;
2695 CPP_ADJUST_WRITTEN (pfile
, 2);
2696 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2703 /* If macro wants an arglist, verify that a '(' follows. */
2704 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2706 int macbuf_whitespace
= 0;
2708 while (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2710 U_CHAR
*point
= CPP_BUFFER (pfile
)->cur
;
2713 cpp_skip_hspace (pfile
);
2720 if (point
!= CPP_BUFFER (pfile
)->cur
)
2721 macbuf_whitespace
= 1;
2725 goto not_macro_call
;
2726 cpp_pop_buffer (pfile
);
2729 parse_set_mark (pfile
);
2732 cpp_skip_hspace (pfile
);
2739 parse_goto_mark (pfile
);
2745 if (macbuf_whitespace
)
2746 CPP_PUTC (pfile
, ' ');
2750 /* This is now known to be a macro call.
2751 Expand the macro, reading arguments as needed,
2752 and push the expansion on the input stack. */
2753 macroexpand (pfile
, hp
);
2754 CPP_SET_WRITTEN (pfile
, before_name_written
);
2758 case ' ': case '\t': case '\v':
2761 CPP_PUTC (pfile
, c
);
2763 if (c
== EOF
|| !is_hspace(c
))
2770 if (CPP_BUFFER (pfile
)->has_escapes
)
2775 if (pfile
->output_escapes
)
2776 CPP_PUTS (pfile
, "\r-", 2);
2777 parse_name (pfile
, GETC ());
2782 CPP_RESERVE (pfile
, 2);
2783 if (pfile
->output_escapes
)
2784 CPP_PUTC_Q (pfile
, '\r');
2785 CPP_PUTC_Q (pfile
, c
);
2790 cpp_ice (pfile
, "unrecognized escape \\r%c", c
);
2796 /* Backslash newline is ignored. */
2797 CPP_BUMP_LINE (pfile
);
2802 CPP_PUTC (pfile
, c
);
2803 if (pfile
->only_seen_white
== 0)
2804 pfile
->only_seen_white
= 1;
2805 CPP_BUMP_LINE (pfile
);
2806 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2809 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2810 output_line_command (pfile
, same_file
);
2814 case '(': token
= CPP_LPAREN
; goto char1
;
2815 case ')': token
= CPP_RPAREN
; goto char1
;
2816 case '{': token
= CPP_LBRACE
; goto char1
;
2817 case '}': token
= CPP_RBRACE
; goto char1
;
2818 case ',': token
= CPP_COMMA
; goto char1
;
2819 case ';': token
= CPP_SEMICOLON
; goto char1
;
2825 pfile
->only_seen_white
= 0;
2826 CPP_PUTC (pfile
, c
);
2832 /* Like cpp_get_token, but skip spaces and comments. */
2835 cpp_get_non_space_token (pfile
)
2838 int old_written
= CPP_WRITTEN (pfile
);
2841 enum cpp_token token
= cpp_get_token (pfile
);
2842 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2843 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2845 CPP_SET_WRITTEN (pfile
, old_written
);
2849 /* Parse an identifier starting with C. */
2852 parse_name (pfile
, c
)
2864 if (c
== '$' && CPP_PEDANTIC (pfile
))
2865 cpp_pedwarn (pfile
, "`$' in identifier");
2867 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2868 CPP_PUTC_Q (pfile
, c
);
2873 CPP_NUL_TERMINATE_Q (pfile
);
2877 /* Parse and skip over a string starting with C. A single quoted
2878 string is treated like a double -- some programs (e.g., troff) are
2879 perverse this way. (However, a single quoted string is not allowed
2880 to extend over multiple lines.) */
2882 skip_string (pfile
, c
)
2886 long start_line
, start_column
;
2887 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2895 cpp_error_with_line (pfile
, start_line
, start_column
,
2896 "unterminated string or character constant");
2897 if (pfile
->multiline_string_line
!= start_line
2898 && pfile
->multiline_string_line
!= 0)
2899 cpp_error_with_line (pfile
,
2900 pfile
->multiline_string_line
, -1,
2901 "possible real start of unterminated constant");
2902 pfile
->multiline_string_line
= 0;
2906 CPP_BUMP_LINE (pfile
);
2907 /* In Fortran and assembly language, silently terminate
2908 strings of either variety at end of line. This is a
2909 kludge around not knowing where comments are in these
2911 if (CPP_OPTIONS (pfile
)->lang_fortran
2912 || CPP_OPTIONS (pfile
)->lang_asm
)
2917 /* Character constants may not extend over multiple lines.
2918 In Standard C, neither may strings. We accept multiline
2919 strings as an extension. */
2922 cpp_error_with_line (pfile
, start_line
, start_column
,
2923 "unterminated character constant");
2927 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2928 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2929 "string constant runs past end of line");
2930 if (pfile
->multiline_string_line
== 0)
2931 pfile
->multiline_string_line
= start_line
;
2935 if (CPP_BUFFER (pfile
)->has_escapes
)
2937 cpp_ice (pfile
, "\\r escape inside string constant");
2941 /* Backslash newline is replaced by nothing at all. */
2942 CPP_BUMP_LINE (pfile
);
2958 /* Parse a string and copy it to the output. */
2961 parse_string (pfile
, c
)
2965 U_CHAR
*start
= CPP_BUFFER (pfile
)->cur
; /* XXX Layering violation */
2968 skip_string (pfile
, c
);
2970 limit
= CPP_BUFFER (pfile
)->cur
;
2971 CPP_RESERVE (pfile
, limit
- start
+ 2);
2972 CPP_PUTC_Q (pfile
, c
);
2973 for (; start
< limit
; start
++)
2975 CPP_PUTC_Q (pfile
, *start
);
2978 /* Read an assertion into the token buffer, converting to
2979 canonical form: `#predicate(a n swe r)' The next non-whitespace
2980 character to read should be the first letter of the predicate.
2981 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2982 with answer (see callers for why). In case of 0, an error has been
2985 parse_assertion (pfile
)
2989 cpp_skip_hspace (pfile
);
2991 if (! is_idstart(c
))
2993 cpp_error (pfile
, "assertion predicate is not an identifier");
2996 CPP_PUTC(pfile
, '#');
2998 parse_name(pfile
, c
);
3003 if (is_hspace(c
) || c
== '\r')
3004 cpp_skip_hspace (pfile
);
3010 CPP_PUTC(pfile
, '(');
3013 while ((c
= GETC()) != ')')
3019 CPP_PUTC(pfile
, ' ');
3023 else if (c
== '\n' || c
== EOF
)
3025 if (c
== '\n') FORWARD(-1);
3026 cpp_error (pfile
, "un-terminated assertion answer");
3030 /* \r cannot be a macro escape here. */
3031 CPP_BUMP_LINE (pfile
);
3034 CPP_PUTC (pfile
, c
);
3039 if (pfile
->limit
[-1] == ' ')
3040 pfile
->limit
[-1] = ')';
3041 else if (pfile
->limit
[-1] == '(')
3043 cpp_error (pfile
, "empty token sequence in assertion");
3047 CPP_PUTC (pfile
, ')');
3049 CPP_NUL_TERMINATE (pfile
);
3054 do_assert (pfile
, keyword
)
3056 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3060 HASHNODE
*base
, *this;
3061 int baselen
, thislen
;
3063 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
)
3064 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
3066 cpp_skip_hspace (pfile
);
3067 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3068 ret
= parse_assertion (pfile
);
3073 cpp_error (pfile
, "missing token-sequence in `#assert'");
3077 cpp_skip_hspace (pfile
);
3079 if (c
!= EOF
&& c
!= '\n')
3081 cpp_error (pfile
, "junk at end of `#assert'");
3085 thislen
= strlen (sym
);
3086 baselen
= index (sym
, '(') - sym
;
3087 this = cpp_lookup (pfile
, sym
, thislen
);
3090 cpp_warning (pfile
, "`%s' re-asserted", sym
);
3094 base
= cpp_lookup (pfile
, sym
, baselen
);
3096 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0);
3097 else if (base
->type
!= T_ASSERT
)
3099 /* Token clash - but with what?! */
3100 cpp_ice (pfile
, "base->type != T_ASSERT in do_assert");
3104 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
3105 (char *)base
->value
.aschain
);
3106 base
->value
.aschain
= this;
3108 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3112 skip_rest_of_line (pfile
);
3113 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3118 do_unassert (pfile
, keyword
)
3120 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3124 long baselen
, thislen
;
3125 HASHNODE
*base
, *this, *next
;
3127 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
)
3128 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
3130 cpp_skip_hspace (pfile
);
3132 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3133 ret
= parse_assertion (pfile
);
3137 cpp_skip_hspace (pfile
);
3139 if (c
!= EOF
&& c
!= '\n')
3140 cpp_error (pfile
, "junk at end of `#unassert'");
3142 thislen
= strlen (sym
);
3145 base
= cpp_lookup (pfile
, sym
, thislen
);
3147 goto error
; /* It isn't an error to #undef what isn't #defined,
3148 so it isn't an error to #unassert what isn't
3149 #asserted either. */
3151 for (this = base
->value
.aschain
; this; this = next
)
3153 next
= this->value
.aschain
;
3154 delete_macro (this);
3156 delete_macro (base
);
3160 baselen
= index (sym
, '(') - sym
;
3161 base
= cpp_lookup (pfile
, sym
, baselen
);
3162 if (! base
) goto error
;
3163 this = cpp_lookup (pfile
, sym
, thislen
);
3164 if (! this) goto error
;
3167 while (next
->value
.aschain
!= this)
3168 next
= next
->value
.aschain
;
3170 next
->value
.aschain
= this->value
.aschain
;
3171 delete_macro (this);
3173 if (base
->value
.aschain
== NULL
)
3174 delete_macro (base
); /* Last answer for this predicate deleted. */
3177 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3180 skip_rest_of_line (pfile
);
3181 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3185 /* Process STR as if it appeared as the body of an #unassert. */
3187 cpp_unassert (pfile
, str
)
3191 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3193 do_unassert (pfile
, NULL
);
3194 cpp_pop_buffer (pfile
);
3198 /* Remember the current position of PFILE so it may be returned to
3199 after looking ahead a bit.
3201 Note that when you set a mark, you _must_ return to that mark. You
3202 may not forget about it and continue parsing. You may not pop a
3203 buffer with an active mark. You may not call CPP_BUMP_LINE while a
3207 parse_set_mark (pfile
)
3210 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3211 if (ACTIVE_MARK_P())
3212 cpp_ice (pfile
, "mark active in parse_set_mark");
3214 ip
->mark
= ip
->cur
- ip
->buf
;
3217 /* Backup the current position of PFILE to that saved in its mark,
3218 and clear the mark. */
3221 parse_goto_mark (pfile
)
3224 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3225 if (!ACTIVE_MARK_P())
3226 cpp_ice (pfile
, "mark not active in parse_goto_mark");
3228 ip
->cur
= ip
->buf
+ ip
->mark
;