2 Copyright (C) 1986, 87, 89, 92-99, 2000 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #define SKIP_WHITE_SPACE(p) do { while (is_hspace(*p)) p++; } while (0)
30 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
31 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
32 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
33 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
34 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
35 (Note that it is false while we're expanding macro *arguments*.) */
36 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
38 /* External declarations. */
40 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
42 /* `struct directive' defines one #-directive, including how to handle it. */
45 int length
; /* Length of name */
46 int (*func
) /* Function to handle directive */
47 PARAMS ((cpp_reader
*, const struct directive
*));
48 const char *name
; /* Name of directive */
49 enum node_type type
; /* Code which describes which directive. */
52 /* These functions are declared to return int instead of void since they
53 are going to be placed in a table and some old compilers have trouble with
54 pointers to functions returning void. */
56 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
57 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
58 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
59 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
60 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
61 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
62 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
63 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
64 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
65 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
66 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
67 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
69 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
71 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
72 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
73 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
75 /* Forward declarations. */
77 static void validate_else
PARAMS ((cpp_reader
*, const char *));
78 static HOST_WIDEST_INT eval_if_expression
PARAMS ((cpp_reader
*));
79 static void conditional_skip
PARAMS ((cpp_reader
*, int,
80 enum node_type
, U_CHAR
*));
81 static void skip_if_group
PARAMS ((cpp_reader
*));
82 static void parse_name
PARAMS ((cpp_reader
*, int));
83 static void parse_string
PARAMS ((cpp_reader
*, int));
84 static int parse_assertion
PARAMS ((cpp_reader
*));
85 static const char *if_directive_name
PARAMS ((cpp_reader
*,
87 static enum cpp_token null_underflow
PARAMS ((cpp_reader
*));
88 static int null_cleanup
PARAMS ((cpp_buffer
*, cpp_reader
*));
89 static int skip_comment
PARAMS ((cpp_reader
*, int));
90 static int copy_comment
PARAMS ((cpp_reader
*, int));
91 static void copy_rest_of_line
PARAMS ((cpp_reader
*));
92 static int handle_directive
PARAMS ((cpp_reader
*));
93 static void pass_thru_directive
PARAMS ((const U_CHAR
*, size_t,
95 const struct directive
*));
96 static enum cpp_token get_directive_token
PARAMS ((cpp_reader
*));
97 static int read_line_number
PARAMS ((cpp_reader
*, int *));
98 static U_CHAR
*detect_if_not_defined
PARAMS ((cpp_reader
*));
99 static int consider_directive_while_skipping
PARAMS ((cpp_reader
*,
101 static void skip_block_comment
PARAMS ((cpp_reader
*));
102 static void skip_line_comment
PARAMS ((cpp_reader
*));
104 /* Here is the actual list of #-directives.
105 This table is ordered by frequency of occurrence; the numbers
106 at the end are directive counts from all the source code I have
107 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
108 linux-2.2.9, and pcmcia-cs-3.0.9). */
110 static const struct directive directive_table
[] = {
112 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
113 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
114 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
115 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
116 { 2, do_if
, "if", T_IF
}, /* 18162 */
117 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
118 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
119 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
120 { 4, do_line
, "line", T_LINE
}, /* 2465 */
121 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
122 { 5, do_error
, "error", T_ERROR
}, /* 475 */
123 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
125 /* Extensions. All deprecated except #warning and #include_next. */
126 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
127 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
128 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
129 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
130 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
131 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
132 #ifdef SCCS_DIRECTIVE
133 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
135 { -1, 0, "", T_UNUSED
}
138 /* Place into PFILE a quoted string representing the string SRC.
139 Caller must reserve enough space in pfile->token_buffer. */
142 quote_string (pfile
, src
)
148 CPP_PUTC_Q (pfile
, '\"');
150 switch ((c
= *src
++))
154 CPP_PUTC_Q (pfile
, c
);
157 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
158 CPP_ADJUST_WRITTEN (pfile
, 4);
164 CPP_PUTC_Q (pfile
, '\\');
165 CPP_PUTC_Q (pfile
, c
);
169 CPP_PUTC_Q (pfile
, '\"');
170 CPP_NUL_TERMINATE_Q (pfile
);
175 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
178 cpp_grow_buffer (pfile
, n
)
182 long old_written
= CPP_WRITTEN (pfile
);
183 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
184 pfile
->token_buffer
= (U_CHAR
*)
185 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
186 CPP_SET_WRITTEN (pfile
, old_written
);
189 /* Process the string STR as if it appeared as the body of a #define
190 If STR is just an identifier, define it with value 1.
191 If STR has anything after the identifier, then it should
192 be identifier=definition. */
195 cpp_define (pfile
, str
)
202 /* Copy the entire option so we can modify it. */
203 count
= strlen (str
) + 3;
204 buf
= (U_CHAR
*) alloca (count
);
205 memcpy (buf
, str
, count
- 2);
206 /* Change the first "=" in the string to a space. If there is none,
207 tack " 1" on the end. */
208 p
= (U_CHAR
*) strchr (buf
, '=');
215 strcpy (&buf
[count
-3], " 1");
217 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
219 do_define (pfile
, NULL
);
220 cpp_pop_buffer (pfile
);
224 /* Process the string STR as if it appeared as the body of a #assert. */
226 cpp_assert (pfile
, str
)
230 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
232 do_assert (pfile
, NULL
);
233 cpp_pop_buffer (pfile
);
238 static enum cpp_token
239 null_underflow (pfile
)
240 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
246 null_cleanup (pbuf
, pfile
)
247 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
248 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
253 /* Skip a C-style block comment. We know it's a comment, and point is
254 at the second character of the starter. */
256 skip_block_comment (pfile
)
263 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
269 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
272 else if (c
== '\n' || c
== '\r')
273 /* \r cannot be a macro escape marker here. */
274 CPP_BUMP_LINE (pfile
);
275 else if (c
== '/' && prev_c
== '*')
277 else if (c
== '*' && prev_c
== '/'
278 && CPP_OPTIONS (pfile
)->warn_comments
)
279 cpp_warning (pfile
, "`/*' within comment");
285 /* Skip a C++/Chill line comment. We know it's a comment, and point
286 is at the second character of the initiator. */
288 skip_line_comment (pfile
)
296 /* We don't have to worry about EOF in here. */
299 /* Don't consider final '\n' to be part of comment. */
305 /* \r cannot be a macro escape marker here. */
306 CPP_BUMP_LINE (pfile
);
307 if (CPP_OPTIONS (pfile
)->warn_comments
)
308 cpp_warning (pfile
, "backslash-newline within line comment");
313 /* Skip a comment - C, C++, or Chill style. M is the first character
314 of the comment marker. If this really is a comment, skip to its
315 end and return ' '. If this is not a comment, return M (which will
319 skip_comment (pfile
, m
)
323 if (m
== '/' && PEEKC() == '*')
325 skip_block_comment (pfile
);
328 else if (m
== '/' && PEEKC() == '/')
330 if (CPP_BUFFER (pfile
)->system_header_p
)
332 /* We silently allow C++ comments in system headers, irrespective
333 of conformance mode, because lots of busted systems do that
334 and trying to clean it up in fixincludes is a nightmare. */
335 skip_line_comment (pfile
);
338 else if (CPP_OPTIONS (pfile
)->cplusplus_comments
)
340 if (CPP_OPTIONS (pfile
)->c89
341 && CPP_PEDANTIC (pfile
)
342 && ! CPP_BUFFER (pfile
)->warned_cplusplus_comments
)
345 "C++ style comments are not allowed in ISO C89");
347 "(this will be reported only once per input file)");
348 CPP_BUFFER (pfile
)->warned_cplusplus_comments
= 1;
350 skip_line_comment (pfile
);
356 else if (m
== '-' && PEEKC() == '-'
357 && CPP_OPTIONS (pfile
)->chill
)
359 skip_line_comment (pfile
);
366 /* Identical to skip_comment except that it copies the comment into the
367 token_buffer. This is used if put_out_comments. */
369 copy_comment (pfile
, m
)
373 U_CHAR
*start
= CPP_BUFFER (pfile
)->cur
; /* XXX Layering violation */
376 if (skip_comment (pfile
, m
) == m
)
380 for (limit
= CPP_BUFFER (pfile
)->cur
; start
<= limit
; start
++)
382 CPP_PUTC (pfile
, *start
);
386 /* Skip whitespace \-newline and comments. Does not macro-expand. */
389 cpp_skip_hspace (pfile
)
398 else if (is_hspace(c
))
400 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
401 cpp_pedwarn (pfile
, "%s in preprocessing directive",
402 c
== '\f' ? "formfeed" : "vertical tab");
406 /* \r is a backslash-newline marker if !has_escapes, and
407 a deletable-whitespace or no-reexpansion marker otherwise. */
408 if (CPP_BUFFER (pfile
)->has_escapes
)
416 CPP_BUFFER (pfile
)->lineno
++;
418 else if (c
== '/' || c
== '-')
420 c
= skip_comment (pfile
, c
);
430 /* Read the rest of the current line.
431 The line is appended to PFILE's output buffer. */
434 copy_rest_of_line (pfile
)
445 CPP_NUL_TERMINATE (pfile
);
449 if (CPP_BUFFER (pfile
)->has_escapes
)
453 CPP_BUFFER (pfile
)->lineno
++;
458 parse_string (pfile
, c
);
463 if (CPP_TRADITIONAL (pfile
))
464 CPP_PUTS (pfile
, "/**/", 4);
465 skip_block_comment (pfile
);
468 /* else fall through */
470 c
= skip_comment (pfile
, c
);
475 if (CPP_PEDANTIC (pfile
))
476 cpp_pedwarn (pfile
, "%s in preprocessing directive",
477 c
== '\f' ? "formfeed" : "vertical tab");
485 /* FIXME: It is almost definitely a performance win to make this do
486 the scan itself. >75% of calls to copy_r_o_l are from here or
487 skip_if_group, which means the common case is to copy stuff into the
488 token_buffer only to discard it. */
490 skip_rest_of_line (pfile
)
493 long old
= CPP_WRITTEN (pfile
);
494 copy_rest_of_line (pfile
);
495 CPP_SET_WRITTEN (pfile
, old
);
498 /* Handle a possible # directive.
499 '#' has already been read. */
502 handle_directive (pfile
)
506 register const struct directive
*kt
;
509 long old_written
= CPP_WRITTEN (pfile
);
511 cpp_skip_hspace (pfile
);
514 /* # followed by a number is equivalent to #line. Do not recognize
515 this form in assembly language source files. Complain about this
516 form if we're being pedantic, but not if this is regurgitated
517 input (preprocessed or fed back in by the C++ frontend). */
518 if (c
>= '0' && c
<= '9')
520 if (CPP_OPTIONS (pfile
)->lang_asm
)
523 if (CPP_PEDANTIC (pfile
)
524 && ! CPP_PREPROCESSED (pfile
)
525 && ! CPP_BUFFER (pfile
)->manual_pop
)
526 cpp_pedwarn (pfile
, "`#' followed by integer");
527 do_line (pfile
, NULL
);
531 /* If we are rescanning preprocessed input, don't obey any directives
533 if (CPP_PREPROCESSED (pfile
))
536 /* Now find the directive name. */
537 CPP_PUTC (pfile
, '#');
538 parse_name (pfile
, GETC());
539 ident
= pfile
->token_buffer
+ old_written
+ 1;
540 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
541 if (ident_length
== 0)
543 /* A line of just `#' becomes blank. A line with something
544 other than an identifier after the # is reparsed as a non-
546 CPP_SET_WRITTEN (pfile
, old_written
);
547 return (PEEKC() == '\n');
550 /* Decode the keyword and call the appropriate expansion routine. */
551 for (kt
= directive_table
; ; kt
++)
554 /* # identifier, but not a legit directive. Pass onward as a
555 CPP_DIRECTIVE token anyway - let the consumer worry about it. */
557 if (kt
->length
== ident_length
558 && !strncmp (kt
->name
, ident
, ident_length
))
562 CPP_SET_WRITTEN (pfile
, old_written
);
564 if (pfile
->no_directives
)
566 cpp_error (pfile
, "`#%s' may not be used inside a macro argument",
568 skip_rest_of_line (pfile
);
571 (*kt
->func
) (pfile
, kt
);
576 /* Pass a directive through to the output file.
577 BUF points to the contents of the directive, as a contiguous string.
578 LEN is the length of the string pointed to by BUF.
579 KEYWORD is the keyword-table entry for the directive. */
582 pass_thru_directive (buf
, len
, pfile
, keyword
)
586 const struct directive
*keyword
;
588 register unsigned keyword_length
= keyword
->length
;
590 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
591 CPP_PUTC_Q (pfile
, '#');
592 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
593 if (len
!= 0 && buf
[0] != ' ')
594 CPP_PUTC_Q (pfile
, ' ');
595 CPP_PUTS_Q (pfile
, buf
, len
);
598 /* Check a purported macro name SYMNAME, and yield its length. */
601 check_macro_name (pfile
, symname
)
603 const U_CHAR
*symname
;
608 for (p
= symname
; is_idchar(*p
); p
++)
610 sym_length
= p
- symname
;
612 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
613 cpp_error (pfile
, "invalid macro name");
614 else if (!is_idstart(*symname
)
615 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
616 U_CHAR
*msg
; /* what pain... */
617 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
618 bcopy (symname
, msg
, sym_length
);
620 cpp_error (pfile
, "invalid macro name `%s'", msg
);
625 /* Process a #define command.
626 KEYWORD is the keyword-table entry for #define,
627 or NULL for a "predefined" macro,
628 or the keyword-table entry for #pragma in the case of a #pragma poison. */
631 do_define (pfile
, keyword
)
633 const struct directive
*keyword
;
639 U_CHAR
*macro
, *buf
, *end
;
640 enum node_type new_type
;
642 here
= CPP_WRITTEN (pfile
);
643 copy_rest_of_line (pfile
);
645 if (keyword
== NULL
|| keyword
->type
== T_DEFINE
)
650 /* Copy out the line so we can pop the token buffer. */
651 buf
= pfile
->token_buffer
+ here
;
652 end
= CPP_PWRITTEN (pfile
);
653 macro
= (U_CHAR
*) alloca (end
- buf
+ 1);
654 memcpy (macro
, buf
, end
- buf
+ 1);
655 end
= macro
+ (end
- buf
);
657 CPP_SET_WRITTEN (pfile
, here
);
659 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
663 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
665 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
668 /* Redefining a precompiled key is ok. */
669 if (hp
->type
== T_PCSTRING
)
671 /* Redefining a poisoned identifier is even worse than `not ok'. */
672 else if (hp
->type
== T_POISON
)
674 /* Redefining a macro is ok if the definitions are the same. */
675 else if (hp
->type
== T_MACRO
)
676 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
677 /* Redefining a constant is ok with -D. */
678 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
679 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
680 /* Print the warning or error if it's not ok. */
683 if (hp
->type
== T_POISON
)
684 cpp_error (pfile
, "redefining poisoned `%.*s'",
685 mdef
.symlen
, mdef
.symnam
);
687 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
688 if (hp
->type
== T_MACRO
&& CPP_OPTIONS (pfile
)->done_initializing
)
689 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
690 hp
->value
.defn
->line
, -1,
691 "this is the location of the previous definition");
693 if (hp
->type
!= T_POISON
)
695 /* Replace the old definition. */
697 hp
->value
.defn
= mdef
.defn
;
701 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, new_type
,
702 (char *) mdef
.defn
, hashcode
);
704 if (keyword
!= NULL
&& keyword
->type
== T_DEFINE
)
706 if (CPP_OPTIONS (pfile
)->debug_output
707 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
708 dump_definition (pfile
, mdef
);
709 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
710 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
717 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
718 If BUFFER != NULL, then use the LENGTH characters in BUFFER
719 as the new input buffer.
720 Return the new buffer, or NULL on failure. */
723 cpp_push_buffer (pfile
, buffer
, length
)
728 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
730 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
732 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
736 new = (cpp_buffer
*) xcalloc (1, sizeof (cpp_buffer
));
738 new->if_stack
= pfile
->if_stack
;
739 new->cleanup
= null_cleanup
;
740 new->underflow
= null_underflow
;
741 new->buf
= new->cur
= buffer
;
742 new->alimit
= new->rlimit
= buffer
+ length
;
746 CPP_BUFFER (pfile
) = new;
751 cpp_pop_buffer (pfile
)
754 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
755 (*buf
->cleanup
) (buf
, pfile
);
756 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
758 pfile
->buffer_stack_depth
--;
759 return CPP_BUFFER (pfile
);
762 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
763 Pop the buffer when done. */
766 cpp_scan_buffer (pfile
)
769 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
770 enum cpp_token token
;
771 if (CPP_OPTIONS (pfile
)->no_output
)
773 long old_written
= CPP_WRITTEN (pfile
);
774 /* In no-output mode, we can ignore everything but directives. */
777 if (! pfile
->only_seen_white
)
778 skip_rest_of_line (pfile
);
779 token
= cpp_get_token (pfile
);
780 if (token
== CPP_EOF
) /* Should not happen ... */
782 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
784 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
785 != CPP_NULL_BUFFER (pfile
))
786 cpp_pop_buffer (pfile
);
790 CPP_SET_WRITTEN (pfile
, old_written
);
796 token
= cpp_get_token (pfile
);
797 if (token
== CPP_EOF
) /* Should not happen ... */
799 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
801 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
802 != CPP_NULL_BUFFER (pfile
))
803 cpp_pop_buffer (pfile
);
811 * Rescan a string (which may have escape marks) into pfile's buffer.
812 * Place the result in pfile->token_buffer.
814 * The input is copied before it is scanned, so it is safe to pass
815 * it something from the token_buffer that will get overwritten
816 * (because it follows CPP_WRITTEN). This is used by do_include.
820 cpp_expand_to_buffer (pfile
, buf
, length
)
825 register cpp_buffer
*ip
;
831 cpp_ice (pfile
, "length < 0 in cpp_expand_to_buffer");
835 /* Set up the input on the input stack. */
837 buf1
= (U_CHAR
*) alloca (length
+ 1);
838 memcpy (buf1
, buf
, length
);
841 ip
= cpp_push_buffer (pfile
, buf1
, length
);
846 /* Scan the input, create the output. */
847 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
848 CPP_OPTIONS (pfile
)->no_output
= 0;
849 cpp_scan_buffer (pfile
);
850 CPP_OPTIONS (pfile
)->no_output
= save_no_output
;
852 CPP_NUL_TERMINATE (pfile
);
856 cpp_buf_line_and_col (pbuf
, linep
, colp
)
857 register cpp_buffer
*pbuf
;
862 *linep
= pbuf
->lineno
;
864 *colp
= pbuf
->cur
- pbuf
->line_base
;
874 /* Return the cpp_buffer that corresponds to a file (not a macro). */
877 cpp_file_buffer (pfile
)
880 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
882 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
883 if (ip
->fname
!= NULL
)
889 * write out a #line command, for instance, after an #include file.
890 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
894 output_line_command (pfile
, file_change
)
896 enum file_change_code file_change
;
899 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
901 if (ip
->fname
== NULL
)
904 if (CPP_OPTIONS (pfile
)->no_line_commands
905 || CPP_OPTIONS (pfile
)->no_output
)
908 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
910 /* If the current file has not changed, we omit the #line if it would
911 appear to be a no-op, and we output a few newlines instead
912 if we want to increase the line number by a small amount.
913 We cannot do this if pfile->lineno is zero, because that means we
914 haven't output any line commands yet. (The very first line command
915 output is a `same_file' command.) */
916 if (file_change
== same_file
&& pfile
->lineno
!= 0)
918 if (line
== pfile
->lineno
)
921 /* If the inherited line number is a little too small,
922 output some newlines instead of a #line command. */
923 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
925 CPP_RESERVE (pfile
, 20);
926 while (line
> pfile
->lineno
)
928 CPP_PUTC_Q (pfile
, '\n');
935 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
936 CPP_PUTS_Q (pfile
, "# ", 2);
938 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
939 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
941 quote_string (pfile
, ip
->nominal_fname
);
942 if (file_change
!= same_file
)
944 CPP_PUTC_Q (pfile
, ' ');
945 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
947 /* Tell cc1 if following text comes from a system header file. */
948 if (ip
->system_header_p
)
950 CPP_PUTC_Q (pfile
, ' ');
951 CPP_PUTC_Q (pfile
, '3');
953 #ifndef NO_IMPLICIT_EXTERN_C
954 /* Tell cc1plus if following text should be treated as C. */
955 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
957 CPP_PUTC_Q (pfile
, ' ');
958 CPP_PUTC_Q (pfile
, '4');
961 CPP_PUTC_Q (pfile
, '\n');
962 pfile
->lineno
= line
;
966 /* Like cpp_get_token, except that it does not read past end-of-line.
967 Also, horizontal space is skipped, and macros are popped. */
969 static enum cpp_token
970 get_directive_token (pfile
)
973 long old_written
= CPP_WRITTEN (pfile
);
974 enum cpp_token token
;
978 cpp_skip_hspace (pfile
);
979 if (PEEKC () == '\n')
982 token
= cpp_get_token (pfile
);
983 /* token could be hspace at the beginning of a macro. */
984 if (token
== CPP_HSPACE
|| token
== CPP_COMMENT
)
986 CPP_SET_WRITTEN (pfile
, old_written
);
990 /* token cannot be vspace, it would have been caught above. */
991 if (token
== CPP_VSPACE
)
993 cpp_ice (pfile
, "VSPACE in get_directive_token");
997 /* token cannot be POP unless the buffer is a macro buffer. */
998 if (token
!= CPP_POP
)
1001 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
1003 cpp_ice (pfile
, "POP of file buffer in get_directive_token");
1007 /* We must pop the buffer by hand, or else cpp_get_token might
1008 hand us white space or newline on the next invocation. */
1009 cpp_pop_buffer (pfile
);
1013 /* Handle #include and #import.
1014 This function expects to see "fname" or <fname> on the input.
1016 The input is normally in part of the output_buffer following
1017 CPP_WRITTEN, and will get overwritten by output_line_command.
1018 I.e. in input file specification has been popped by handle_directive.
1022 do_include (pfile
, keyword
)
1024 const struct directive
*keyword
;
1026 int importing
= (keyword
->type
== T_IMPORT
);
1027 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1028 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1029 int before
; /* included before? */
1031 unsigned char *ftok
;
1034 enum cpp_token token
;
1036 /* Chain of dirs to search */
1037 struct include_hash
*ihash
;
1038 struct file_name_list
*search_start
;
1040 long old_written
= CPP_WRITTEN (pfile
);
1044 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1047 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1049 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1052 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1053 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1054 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1056 pfile
->import_warning
= 1;
1058 "#import is obsolete, use an #ifndef wrapper in the header file");
1061 pfile
->parsing_include_directive
++;
1062 token
= get_directive_token (pfile
);
1063 pfile
->parsing_include_directive
--;
1065 if (token
== CPP_STRING
)
1067 if (pfile
->token_buffer
[old_written
] == '<')
1071 else if (token
== CPP_NAME
)
1073 /* Support '#include xyz' like VAX-C. It is taken as
1074 '#include <xyz.h>' and generates a warning. */
1076 "`#include filename' is obsolete, use `#include <filename.h>'");
1079 /* Append the missing `.h' to the name. */
1080 CPP_PUTS (pfile
, ".h", 2);
1086 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1087 CPP_SET_WRITTEN (pfile
, old_written
);
1088 skip_rest_of_line (pfile
);
1092 flen
= CPP_WRITTEN (pfile
) - old_written
;
1093 ftok
= (unsigned char *) alloca (flen
+ 1);
1094 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1097 if (get_directive_token (pfile
) != CPP_VSPACE
)
1099 cpp_error (pfile
, "junk at end of `#include'");
1100 skip_rest_of_line (pfile
);
1103 CPP_SET_WRITTEN (pfile
, old_written
);
1107 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1111 if (CPP_OPTIONS (pfile
)->dump_includes
)
1112 pass_thru_directive (ftok
,
1115 - ((token
== CPP_NAME
) ? 2 : 0)
1120 if (token
== CPP_STRING
)
1130 for (fp
= CPP_BUFFER (pfile
);
1131 fp
!= CPP_NULL_BUFFER (pfile
);
1132 fp
= CPP_PREV_BUFFER (fp
))
1133 if (fp
->fname
!= NULL
)
1136 if (fp
== CPP_NULL_BUFFER (pfile
))
1138 cpp_ice (pfile
, "fp == NULL_BUFFER in do_include");
1142 /* For #include_next, skip in the search path past the dir in which the
1143 containing file was found. Treat files specified using an absolute path
1144 as if there are no more directories to search. Treat the primary source
1145 file like any other included source, but generate a warning. */
1146 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1148 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1149 search_start
= fp
->ihash
->foundhere
->next
;
1154 cpp_warning (pfile
, "#include_next in primary source file");
1157 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1160 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1163 search_start
= fp
->actual_dir
;
1166 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1172 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1176 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1183 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1184 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1185 (pfile
->system_include_depth
> 0)))
1187 if (!angle_brackets
)
1188 deps_output (pfile
, ftok
, ' ');
1192 struct file_name_list
*ptr
;
1193 /* If requested as a system header, assume it belongs in
1194 the first system header directory. */
1195 if (CPP_OPTIONS (pfile
)->bracket_include
)
1196 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1198 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1200 p
= (char *) alloca (strlen (ptr
->name
)
1201 + strlen (ftok
) + 2);
1202 if (*ptr
->name
!= '\0')
1204 strcpy (p
, ptr
->name
);
1208 deps_output (pfile
, p
, ' ');
1211 /* If -M was specified, and this header file won't be added to
1212 the dependency list, then don't count this as an error,
1213 because we can still produce correct output. Otherwise, we
1214 can't produce correct output, because there may be
1215 dependencies we need inside the missing file, and we don't
1216 know what directory this missing file exists in. */
1217 else if (CPP_PRINT_DEPS (pfile
)
1218 && (CPP_PRINT_DEPS (pfile
)
1219 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1220 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1222 cpp_error_from_errno (pfile
, ftok
);
1227 /* For -M, add the file to the dependencies on its first inclusion. */
1228 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1229 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1230 deps_output (pfile
, ihash
->name
, ' ');
1232 /* Handle -H option. */
1233 if (CPP_OPTIONS(pfile
)->print_include_names
)
1235 fp
= CPP_BUFFER (pfile
);
1236 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1238 fprintf (stderr
, " %s\n", ihash
->name
);
1241 /* Actually process the file */
1244 ihash
->control_macro
= "";
1246 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1253 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1255 if (finclude (pfile
, fd
, ihash
))
1257 output_line_command (pfile
, enter_file
);
1258 pfile
->only_seen_white
= 2;
1264 /* Subroutine of do_line. Read next token from PFILE without adding it to
1265 the output buffer. If it is a number between 1 and 4, store it in *NUM
1266 and return 1; otherwise, return 0 and complain if we aren't at the end
1267 of the directive. */
1270 read_line_number (pfile
, num
)
1274 long save_written
= CPP_WRITTEN (pfile
);
1275 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1276 enum cpp_token token
= get_directive_token (pfile
);
1277 CPP_SET_WRITTEN (pfile
, save_written
);
1279 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1286 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1287 cpp_error (pfile
, "invalid format `#line' command");
1292 /* Interpret #line command.
1293 Note that the filename string (if any) is treated as if it were an
1294 include filename. That means no escape handling. */
1297 do_line (pfile
, keyword
)
1299 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1301 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1303 long old_written
= CPP_WRITTEN (pfile
);
1304 enum file_change_code file_change
= same_file
;
1305 enum cpp_token token
;
1308 token
= get_directive_token (pfile
);
1310 if (token
!= CPP_NUMBER
)
1312 cpp_error (pfile
, "token after `#line' is not an integer");
1313 goto bad_line_directive
;
1316 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1319 cpp_error (pfile
, "token after `#line' is not an integer");
1320 goto bad_line_directive
;
1322 CPP_SET_WRITTEN (pfile
, old_written
);
1324 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1325 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1327 token
= get_directive_token (pfile
);
1329 if (token
== CPP_STRING
)
1331 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1332 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1333 int action_number
= 0;
1335 if (read_line_number (pfile
, &action_number
))
1337 if (CPP_PEDANTIC (pfile
))
1338 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1340 if (action_number
== 1)
1342 file_change
= enter_file
;
1343 read_line_number (pfile
, &action_number
);
1345 else if (action_number
== 2)
1347 file_change
= leave_file
;
1348 read_line_number (pfile
, &action_number
);
1350 if (action_number
== 3)
1352 ip
->system_header_p
= 1;
1353 read_line_number (pfile
, &action_number
);
1355 if (action_number
== 4)
1357 ip
->system_header_p
= 2;
1358 read_line_number (pfile
, &action_number
);
1364 if (strcmp (fname
, ip
->nominal_fname
))
1366 const char *newname
, *oldname
;
1367 if (!strcmp (fname
, ip
->fname
))
1368 newname
= ip
->fname
;
1369 else if (ip
->last_nominal_fname
1370 && !strcmp (fname
, ip
->last_nominal_fname
))
1371 newname
= ip
->last_nominal_fname
;
1373 newname
= xstrdup (fname
);
1375 oldname
= ip
->nominal_fname
;
1376 ip
->nominal_fname
= newname
;
1378 if (ip
->last_nominal_fname
1379 && ip
->last_nominal_fname
!= oldname
1380 && ip
->last_nominal_fname
!= newname
1381 && ip
->last_nominal_fname
!= ip
->fname
)
1382 free ((void *) ip
->last_nominal_fname
);
1384 if (newname
== ip
->fname
)
1385 ip
->last_nominal_fname
= NULL
;
1387 ip
->last_nominal_fname
= oldname
;
1390 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1392 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1393 goto bad_line_directive
;
1396 /* The Newline at the end of this line remains to be processed.
1397 To put the next line at the specified line number,
1398 we must store a line number now that is one less. */
1399 ip
->lineno
= new_lineno
- 1;
1400 CPP_SET_WRITTEN (pfile
, old_written
);
1401 output_line_command (pfile
, file_change
);
1405 skip_rest_of_line (pfile
);
1406 CPP_SET_WRITTEN (pfile
, old_written
);
1410 /* Remove the definition of a symbol from the symbol table.
1411 According to the C standard, it is not an error to undef
1412 something that has no definitions. */
1414 do_undef (pfile
, keyword
)
1416 const struct directive
*keyword
;
1420 U_CHAR
*buf
, *name
, *limit
;
1422 long here
= CPP_WRITTEN (pfile
);
1423 enum cpp_token token
;
1425 cpp_skip_hspace (pfile
);
1427 if (! is_idstart(c
))
1429 cpp_error (pfile
, "token after #undef is not an identifier");
1430 skip_rest_of_line (pfile
);
1434 parse_name (pfile
, c
);
1435 buf
= pfile
->token_buffer
+ here
;
1436 limit
= CPP_PWRITTEN(pfile
);
1438 /* Copy out the token so we can pop the token buffer. */
1439 name
= (U_CHAR
*) alloca (limit
- buf
+ 1);
1440 bcopy(buf
, name
, limit
- buf
);
1441 name
[limit
- buf
] = '\0';
1443 token
= get_directive_token (pfile
);
1444 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1446 cpp_pedwarn (pfile
, "junk on line after #undef");
1447 skip_rest_of_line (pfile
);
1450 CPP_SET_WRITTEN (pfile
, here
);
1452 sym_length
= check_macro_name (pfile
, buf
);
1454 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1456 /* If we are generating additional info for debugging (with -g) we
1457 need to pass through all effective #undef commands. */
1458 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1459 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1460 if (hp
->type
== T_POISON
)
1461 cpp_error (pfile
, "cannot undefine poisoned `%s'", hp
->name
);
1464 if (hp
->type
!= T_MACRO
)
1465 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1473 /* Wrap do_undef for -U processing. */
1475 cpp_undef (pfile
, macro
)
1479 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1481 do_undef (pfile
, NULL
);
1482 cpp_pop_buffer (pfile
);
1488 * Report an error detected by the program we are processing.
1489 * Use the text of the line in the error message.
1490 * (We use error because it prints the filename & line#.)
1494 do_error (pfile
, keyword
)
1496 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1498 long here
= CPP_WRITTEN (pfile
);
1500 copy_rest_of_line (pfile
);
1501 text
= pfile
->token_buffer
+ here
;
1502 SKIP_WHITE_SPACE(text
);
1504 cpp_error (pfile
, "#error %s", text
);
1505 CPP_SET_WRITTEN (pfile
, here
);
1510 * Report a warning detected by the program we are processing.
1511 * Use the text of the line in the warning message, then continue.
1515 do_warning (pfile
, keyword
)
1517 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1520 long here
= CPP_WRITTEN(pfile
);
1521 copy_rest_of_line (pfile
);
1522 text
= pfile
->token_buffer
+ here
;
1523 SKIP_WHITE_SPACE(text
);
1525 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1526 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1528 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1529 if -pedantic-errors is given, #warning should cause an error. */
1530 cpp_pedwarn (pfile
, "#warning %s", text
);
1531 CPP_SET_WRITTEN (pfile
, here
);
1535 /* Report program identification.
1536 This is not precisely what cccp does with #ident, however I believe
1537 it matches `closely enough' (behavior is identical as long as there
1538 are no macros on the #ident line, which is pathological in my opinion). */
1541 do_ident (pfile
, keyword
)
1543 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1545 /* Allow #ident in system headers, since that's not user's fault. */
1546 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1547 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1549 CPP_PUTS (pfile
, "#ident ", 7);
1550 cpp_skip_hspace (pfile
);
1551 copy_rest_of_line (pfile
);
1556 /* Just check for some recognized pragmas that need validation here,
1557 and leave the text in the token buffer to be output. */
1560 do_pragma (pfile
, keyword
)
1562 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1567 CPP_PUTS (pfile
, "#pragma ", 8);
1568 cpp_skip_hspace (pfile
);
1570 here
= CPP_WRITTEN (pfile
);
1571 copy_rest_of_line (pfile
);
1572 buf
= pfile
->token_buffer
+ here
;
1574 if (!strncmp (buf
, "once", 4))
1576 cpp_buffer
*ip
= NULL
;
1578 /* Allow #pragma once in system headers, since that's not the user's
1580 if (!CPP_BUFFER (pfile
)->system_header_p
)
1581 cpp_warning (pfile
, "`#pragma once' is obsolete");
1583 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1585 if (ip
== CPP_NULL_BUFFER (pfile
))
1587 if (ip
->fname
!= NULL
)
1591 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1592 cpp_warning (pfile
, "`#pragma once' outside include file");
1594 ip
->ihash
->control_macro
= ""; /* never repeat */
1596 else if (!strncmp (buf
, "implementation", 14))
1598 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1599 been included yet. */
1600 struct include_hash
*ptr
;
1601 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1602 SKIP_WHITE_SPACE (p
);
1603 if (*p
== '\n' || *p
!= '\"')
1607 p
= (U_CHAR
*) index (fname
, '\"');
1609 fcopy
= (U_CHAR
*) alloca (p
- fname
+ 1);
1610 bcopy (fname
, fcopy
, p
- fname
);
1611 fcopy
[p
-fname
] = '\0';
1613 ptr
= include_hash (pfile
, fcopy
, 0);
1616 "`#pragma implementation' for `%s' appears after file is included",
1619 else if (!strncmp (buf
, "poison", 6))
1621 /* Poison these symbols so that all subsequent usage produces an
1623 U_CHAR
*p
= buf
+ 6;
1628 SKIP_WHITE_SPACE (p
);
1629 plen
= strlen(p
) + 1;
1631 syms
= (U_CHAR
*) alloca (plen
);
1632 memcpy (syms
, p
, plen
);
1634 /* As a rule, don't include #pragma poison commands in output,
1635 unless the user asks for them. */
1636 writeit
= (CPP_OPTIONS (pfile
)->debug_output
1637 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
1638 || CPP_OPTIONS (pfile
)->dump_macros
== dump_names
);
1641 CPP_SET_WRITTEN (pfile
, here
);
1643 CPP_SET_WRITTEN (pfile
, here
-8);
1647 CPP_RESERVE (pfile
, plen
+ 7);
1648 CPP_PUTS_Q (pfile
, "poison", 7);
1651 while (*syms
!= '\0')
1655 while (is_idchar(*end
))
1658 if (!is_hspace(*end
) && *end
!= '\0')
1660 cpp_error (pfile
, "invalid #pragma poison directive");
1664 if (cpp_push_buffer (pfile
, syms
, end
- syms
) != NULL
)
1666 do_define (pfile
, keyword
);
1667 cpp_pop_buffer (pfile
);
1671 CPP_PUTC_Q (pfile
, ' ');
1672 CPP_PUTS_Q (pfile
, syms
, end
- syms
);
1675 SKIP_WHITE_SPACE (syms
);
1682 #ifdef SCCS_DIRECTIVE
1683 /* Just ignore #sccs, on systems where we define it at all. */
1686 do_sccs (pfile
, keyword
)
1688 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1690 if (CPP_PEDANTIC (pfile
))
1691 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1692 skip_rest_of_line (pfile
);
1698 /* We've found an `#if' directive. If the only thing before it in
1699 this file is white space, and if it is of the form
1700 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1701 for inclusion of this file. (See redundant_include_p in cppfiles.c
1702 for an explanation of controlling macros.) If so, return a
1703 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1706 detect_if_not_defined (pfile
)
1709 U_CHAR
*control_macro
= 0;
1711 if (pfile
->only_seen_white
== 2)
1714 enum cpp_token token
;
1717 int need_rparen
= 0;
1719 /* Save state required for restore. */
1720 pfile
->no_macro_expand
++;
1721 parse_set_mark (pfile
);
1722 base_offset
= CPP_WRITTEN (pfile
);
1725 if (get_directive_token (pfile
) != CPP_OTHER
1726 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1727 || CPP_PWRITTEN (pfile
)[-1] != '!')
1730 /* ...then `defined', */
1731 token_offset
= CPP_WRITTEN (pfile
);
1732 token
= get_directive_token (pfile
);
1733 if (token
!= CPP_NAME
)
1735 ident
= pfile
->token_buffer
+ token_offset
;
1736 CPP_NUL_TERMINATE (pfile
);
1737 if (strcmp (ident
, "defined"))
1740 /* ...then an optional '(' and the name, */
1741 token_offset
= CPP_WRITTEN (pfile
);
1742 token
= get_directive_token (pfile
);
1743 if (token
== CPP_LPAREN
)
1745 token_offset
= CPP_WRITTEN (pfile
);
1746 token
= get_directive_token (pfile
);
1747 if (token
!= CPP_NAME
)
1751 else if (token
!= CPP_NAME
)
1754 ident
= pfile
->token_buffer
+ token_offset
;
1755 CPP_NUL_TERMINATE (pfile
);
1757 /* ...then the ')', if necessary, */
1758 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1759 /* ...and make sure there's nothing else on the line. */
1760 && get_directive_token (pfile
) == CPP_VSPACE
)
1761 control_macro
= xstrdup (ident
);
1764 CPP_SET_WRITTEN (pfile
, base_offset
);
1765 pfile
->no_macro_expand
--;
1766 parse_goto_mark (pfile
);
1769 return control_macro
;
1773 * handle #if command by
1774 * 1) inserting special `defined' keyword into the hash table
1775 * that gets turned into 0 or 1 by special_symbol (thus,
1776 * if the luser has a symbol called `defined' already, it won't
1777 * work inside the #if command)
1778 * 2) rescan the input into a temporary output buffer
1779 * 3) pass the output buffer to the yacc parser and collect a value
1780 * 4) clean up the mess left from steps 1 and 2.
1781 * 5) call conditional_skip to skip til the next #endif (etc.),
1782 * or not, depending on the value from step 3.
1786 do_if (pfile
, keyword
)
1788 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1790 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1791 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1792 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1797 * handle a #elif directive by not changing if_stack either.
1798 * see the comment above do_else.
1802 do_elif (pfile
, keyword
)
1804 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1806 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1808 cpp_error (pfile
, "`#elif' not within a conditional");
1813 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
1815 cpp_error (pfile
, "`#elif' after `#else'");
1816 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
1817 "the conditional began here");
1819 pfile
->if_stack
->type
= T_ELIF
;
1822 if (pfile
->if_stack
->if_succeeded
)
1823 skip_if_group (pfile
);
1826 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1828 skip_if_group (pfile
);
1831 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1832 output_line_command (pfile
, same_file
);
1839 * evaluate a #if expression in BUF, of length LENGTH,
1840 * then parse the result as a C expression and return the value as an int.
1843 static HOST_WIDEST_INT
1844 eval_if_expression (pfile
)
1847 HOST_WIDEST_INT value
;
1848 long old_written
= CPP_WRITTEN (pfile
);
1850 value
= cpp_parse_expr (pfile
);
1852 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1858 * routine to handle ifdef/ifndef. Try to look up the symbol,
1859 * then do or don't skip to the #endif/#else/#elif depending
1860 * on what directive is actually being processed.
1864 do_xifdef (pfile
, keyword
)
1866 const struct directive
*keyword
;
1869 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1872 enum cpp_token token
;
1873 int start_of_file
= 0;
1874 U_CHAR
*control_macro
= 0;
1875 int old_written
= CPP_WRITTEN (pfile
);
1877 /* Detect a #ifndef at start of file (not counting comments). */
1878 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1879 start_of_file
= pfile
->only_seen_white
== 2;
1881 pfile
->no_macro_expand
++;
1882 token
= get_directive_token (pfile
);
1883 pfile
->no_macro_expand
--;
1885 ident
= pfile
->token_buffer
+ old_written
;
1886 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1887 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1889 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1891 skip
= (keyword
->type
== T_IFDEF
);
1892 if (! CPP_TRADITIONAL (pfile
))
1893 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1895 else if (token
== CPP_NAME
)
1897 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1898 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1899 if (start_of_file
&& !skip
)
1901 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1902 bcopy (ident
, control_macro
, ident_length
+ 1);
1904 if (hp
!= NULL
&& hp
->type
== T_POISON
)
1906 cpp_error (pfile
, "attempt to use poisoned `%s'", hp
->name
);
1912 skip
= (keyword
->type
== T_IFDEF
);
1913 if (! CPP_TRADITIONAL (pfile
))
1914 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1917 if (!CPP_TRADITIONAL (pfile
))
1919 cpp_skip_hspace (pfile
);
1921 if (c
!= EOF
&& c
!= '\n')
1922 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1924 skip_rest_of_line (pfile
);
1926 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1930 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1931 If this is a #ifndef starting at the beginning of a file,
1932 CONTROL_MACRO is the macro name tested by the #ifndef.
1933 Otherwise, CONTROL_MACRO is 0. */
1936 conditional_skip (pfile
, skip
, type
, control_macro
)
1939 enum node_type type
;
1940 U_CHAR
*control_macro
;
1942 IF_STACK_FRAME
*temp
;
1944 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1945 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1946 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1947 temp
->next
= pfile
->if_stack
;
1948 temp
->control_macro
= control_macro
;
1949 pfile
->if_stack
= temp
;
1951 pfile
->if_stack
->type
= type
;
1954 skip_if_group (pfile
);
1957 ++pfile
->if_stack
->if_succeeded
;
1958 output_line_command (pfile
, same_file
);
1962 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1963 return 0 if skipping should continue, 1 if it should halt. Also
1964 adjusts the if_stack as appropriate.
1965 The `#' has been read, but not the identifier. */
1968 consider_directive_while_skipping (pfile
, stack
)
1970 IF_STACK_FRAME
*stack
;
1972 long ident_len
, ident
;
1973 const struct directive
*kt
;
1974 IF_STACK_FRAME
*temp
;
1976 cpp_skip_hspace (pfile
);
1978 ident
= CPP_WRITTEN (pfile
);
1979 parse_name (pfile
, GETC());
1980 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1982 CPP_SET_WRITTEN (pfile
, ident
);
1984 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1985 if (kt
->length
== ident_len
1986 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
1992 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
1993 temp
->next
= pfile
->if_stack
;
1994 pfile
->if_stack
= temp
;
1995 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
1996 temp
->type
= kt
->type
;
2000 if (pfile
->if_stack
!= stack
)
2001 validate_else (pfile
, "#else");
2004 if (pfile
->if_stack
== stack
)
2008 pfile
->if_stack
->type
= kt
->type
;
2013 if (pfile
->if_stack
!= stack
)
2014 validate_else (pfile
, "#endif");
2016 if (pfile
->if_stack
== stack
)
2019 temp
= pfile
->if_stack
;
2020 pfile
->if_stack
= temp
->next
;
2028 /* Don't let erroneous code go by. */
2029 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
2030 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
2034 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
2035 * leaves input ptr at the sharp sign found.
2038 skip_if_group (pfile
)
2042 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
2043 U_CHAR
*beg_of_line
;
2046 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2048 CPP_PUTS (pfile
, "#failed\n", 8);
2050 output_line_command (pfile
, same_file
);
2053 old_written
= CPP_WRITTEN (pfile
);
2057 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
2059 if (! CPP_TRADITIONAL (pfile
))
2060 cpp_skip_hspace (pfile
);
2064 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2065 CPP_PUTC (pfile
, c
);
2066 CPP_BUMP_LINE (pfile
);
2071 if (consider_directive_while_skipping (pfile
, save_if_stack
))
2075 return; /* Caller will issue error. */
2078 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2080 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
2081 copy_rest_of_line (pfile
);
2085 copy_rest_of_line (pfile
);
2086 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
2091 return; /* Caller will issue error. */
2095 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2097 CPP_PUTC (pfile
, c
);
2100 CPP_BUMP_LINE (pfile
);
2104 /* Back up to the beginning of this line. Caller will process the
2106 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
2107 pfile
->only_seen_white
= 1;
2108 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2110 CPP_PUTS (pfile
, "#endfailed\n", 11);
2116 * handle a #else directive. Do this by just continuing processing
2117 * without changing if_stack ; this is so that the error message
2118 * for missing #endif's etc. will point to the original #if. It
2119 * is possible that something different would be better.
2123 do_else (pfile
, keyword
)
2125 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2127 validate_else (pfile
, "#else");
2128 skip_rest_of_line (pfile
);
2130 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2132 cpp_error (pfile
, "`#else' not within a conditional");
2137 /* #ifndef can't have its special treatment for containing the whole file
2138 if it has a #else clause. */
2139 pfile
->if_stack
->control_macro
= 0;
2141 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
)
2143 cpp_error (pfile
, "`#else' after `#else'");
2144 cpp_error_with_line (pfile
, pfile
->if_stack
->lineno
, -1,
2145 "the conditional began here");
2147 pfile
->if_stack
->type
= T_ELSE
;
2150 if (pfile
->if_stack
->if_succeeded
)
2151 skip_if_group (pfile
);
2154 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
2155 output_line_command (pfile
, same_file
);
2161 * unstack after #endif command
2165 do_endif (pfile
, keyword
)
2167 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2169 validate_else (pfile
, "#endif");
2170 skip_rest_of_line (pfile
);
2172 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2173 cpp_error (pfile
, "`#endif' not within a conditional");
2176 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2177 pfile
->if_stack
= temp
->next
;
2178 if (temp
->control_macro
!= 0)
2180 /* This #endif matched a #ifndef at the start of the file.
2181 See if it is at the end of the file. */
2184 parse_set_mark (pfile
);
2188 cpp_skip_hspace (pfile
);
2193 parse_goto_mark (pfile
);
2197 /* This #endif ends a #ifndef
2198 that contains all of the file (aside from whitespace).
2199 Arrange not to include the file again
2200 if the macro that was tested is defined. */
2201 struct cpp_buffer
*ip
;
2202 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2203 if (ip
->fname
!= NULL
)
2205 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2209 output_line_command (pfile
, same_file
);
2214 /* Issue -pedantic warning for text which is not a comment following
2215 an #else or #endif. Do not warn in system headers, as this is harmless
2216 and very common on old systems. */
2219 validate_else (pfile
, directive
)
2221 const char *directive
;
2223 if (! CPP_PEDANTIC (pfile
) || CPP_BUFFER (pfile
)->system_header_p
)
2226 cpp_skip_hspace (pfile
);
2227 if (PEEKC () != '\n')
2229 "text following `%s' violates ANSI standard", directive
);
2232 /* Convert T_IF, etc. to a string. Used in error messages. */
2234 if_directive_name (pfile
, ifs
)
2236 struct if_stack
*ifs
;
2240 case T_IF
: return "#if";
2241 case T_IFDEF
: return "#ifdef";
2242 case T_IFNDEF
: return "#ifndef";
2243 case T_ELIF
: return "#elif";
2244 case T_ELSE
: return "#else";
2246 cpp_ice (pfile
, "impossible if_stack->type value %d", ifs
->type
);
2251 /* Get the next token, and add it to the text in pfile->token_buffer.
2252 Return the kind of token we got. */
2255 cpp_get_token (pfile
)
2258 register int c
, c2
, c3
;
2259 enum cpp_token token
;
2260 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2266 if (CPP_BUFFER (pfile
)->manual_pop
)
2267 /* If we've been reading from redirected input, the
2268 frontend will pop the buffer. */
2270 else if (CPP_BUFFER (pfile
)->seen_eof
)
2272 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2275 cpp_pop_buffer (pfile
);
2280 cpp_buffer
*next_buf
= CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2281 struct if_stack
*ifs
, *nifs
;
2283 /* Unwind the conditional stack and generate error messages. */
2284 for (ifs
= pfile
->if_stack
;
2285 ifs
!= CPP_BUFFER (pfile
)->if_stack
;
2288 cpp_error_with_line (pfile
, ifs
->lineno
, -1,
2289 "unterminated `%s' conditional",
2290 if_directive_name (pfile
, ifs
));
2295 pfile
->if_stack
= ifs
;
2297 if (CPP_BUFFER (pfile
)->nominal_fname
2298 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2300 /* We're about to return from an #include file.
2301 Emit #line information now (as part of the CPP_POP) result.
2302 But the #line refers to the file we will pop to. */
2303 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2304 CPP_BUFFER (pfile
) = next_buf
;
2305 pfile
->input_stack_listing_current
= 0;
2306 output_line_command (pfile
, leave_file
);
2307 CPP_BUFFER (pfile
) = cur_buffer
;
2310 CPP_BUFFER (pfile
)->seen_eof
= 1;
2319 if (PEEKC () == '=')
2323 if (opts
->put_out_comments
)
2324 c
= copy_comment (pfile
, c
);
2326 c
= skip_comment (pfile
, c
);
2330 /* Comments are equivalent to spaces.
2331 For -traditional, a comment is equivalent to nothing. */
2332 if (opts
->traditional
|| opts
->put_out_comments
)
2336 CPP_PUTC (pfile
, c
);
2341 if (!pfile
->only_seen_white
)
2343 /* -traditional directives are recognized only with the # in
2345 XXX Layering violation. */
2346 if (CPP_TRADITIONAL (pfile
)
2347 && CPP_BUFFER (pfile
)->cur
- CPP_BUFFER (pfile
)->line_base
!= 1)
2349 if (handle_directive (pfile
))
2350 return CPP_DIRECTIVE
;
2351 pfile
->only_seen_white
= 0;
2357 parse_string (pfile
, c
);
2358 pfile
->only_seen_white
= 0;
2359 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2362 if (!opts
->dollars_in_ident
)
2367 if (opts
->cplusplus
&& PEEKC () == ':')
2375 if (c2
== c
|| c2
== '=')
2384 if (PEEKC () == '=')
2390 if (c2
== '-' && opts
->chill
)
2391 goto comment
; /* Chill style comment */
2392 if (c2
== '-' || c2
== '=')
2396 if (opts
->cplusplus
&& PEEKN (1) == '*')
2398 /* In C++, there's a ->* operator. */
2400 pfile
->only_seen_white
= 0;
2401 CPP_RESERVE (pfile
, 4);
2402 CPP_PUTC_Q (pfile
, c
);
2403 CPP_PUTC_Q (pfile
, GETC ());
2404 CPP_PUTC_Q (pfile
, GETC ());
2405 CPP_NUL_TERMINATE_Q (pfile
);
2413 if (pfile
->parsing_include_directive
)
2417 CPP_PUTC (pfile
, c
);
2421 if (c
== '\n' || c
== EOF
)
2424 "missing '>' in `#include <FILENAME>'");
2429 if (!CPP_BUFFER (pfile
)->has_escapes
)
2431 /* Backslash newline is replaced by nothing. */
2432 CPP_ADJUST_WRITTEN (pfile
, -1);
2433 CPP_BUMP_LINE (pfile
);
2437 /* We might conceivably get \r- or \r<space> in
2438 here. Just delete 'em. */
2440 if (d
!= '-' && d
!= ' ')
2441 cpp_ice (pfile
, "unrecognized escape \\r%c", d
);
2442 CPP_ADJUST_WRITTEN (pfile
, -1);
2448 /* else fall through */
2453 /* GNU C++ supports MIN and MAX operators <? and >?. */
2454 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2457 CPP_RESERVE (pfile
, 4);
2458 CPP_PUTC (pfile
, c
);
2459 CPP_PUTC (pfile
, c2
);
2462 CPP_PUTC_Q (pfile
, GETC ());
2463 CPP_NUL_TERMINATE_Q (pfile
);
2464 pfile
->only_seen_white
= 0;
2471 CPP_RESERVE(pfile
, 2);
2472 CPP_PUTC_Q (pfile
, '.');
2477 /* In C++ there's a .* operator. */
2478 if (opts
->cplusplus
&& c2
== '*')
2481 if (c2
== '.' && PEEKN(1) == '.')
2483 CPP_RESERVE(pfile
, 4);
2484 CPP_PUTC_Q (pfile
, '.');
2485 CPP_PUTC_Q (pfile
, '.');
2486 CPP_PUTC_Q (pfile
, '.');
2488 CPP_NUL_TERMINATE_Q (pfile
);
2489 pfile
->only_seen_white
= 0;
2496 pfile
->only_seen_white
= 0;
2497 CPP_RESERVE(pfile
, 3);
2498 CPP_PUTC_Q (pfile
, c
);
2499 CPP_PUTC_Q (pfile
, GETC ());
2500 CPP_NUL_TERMINATE_Q (pfile
);
2505 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2507 CPP_PUTC (pfile
, c
);
2513 case '0': case '1': case '2': case '3': case '4':
2514 case '5': case '6': case '7': case '8': case '9':
2519 CPP_RESERVE (pfile
, 2);
2520 CPP_PUTC_Q (pfile
, c
);
2524 if (!is_idchar(c
) && c
!= '.'
2525 && ((c2
!= 'e' && c2
!= 'E'
2526 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2527 || (c
!= '+' && c
!= '-')))
2532 CPP_NUL_TERMINATE_Q (pfile
);
2533 pfile
->only_seen_white
= 0;
2535 case 'b': case 'c': case 'd': case 'h': case 'o':
2536 case 'B': case 'C': case 'D': case 'H': case 'O':
2537 if (opts
->chill
&& PEEKC () == '\'')
2539 pfile
->only_seen_white
= 0;
2540 CPP_RESERVE (pfile
, 2);
2541 CPP_PUTC_Q (pfile
, c
);
2542 CPP_PUTC_Q (pfile
, '\'');
2548 goto chill_number_eof
;
2551 CPP_PUTC (pfile
, c
);
2555 CPP_RESERVE (pfile
, 2);
2556 CPP_PUTC_Q (pfile
, c
);
2557 CPP_NUL_TERMINATE_Q (pfile
);
2564 CPP_NUL_TERMINATE (pfile
);
2571 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2572 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2573 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2574 case 'x': case 'y': case 'z':
2575 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2576 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2577 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2582 unsigned char *ident
;
2583 int before_name_written
= CPP_WRITTEN (pfile
);
2585 parse_name (pfile
, c
);
2586 pfile
->only_seen_white
= 0;
2587 if (pfile
->no_macro_expand
)
2589 ident
= pfile
->token_buffer
+ before_name_written
;
2590 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2591 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2594 if (hp
->type
== T_DISABLED
)
2596 if (pfile
->output_escapes
)
2597 { /* Return "\r-IDENT", followed by '\0'. */
2599 CPP_RESERVE (pfile
, 3);
2600 ident
= pfile
->token_buffer
+ before_name_written
;
2601 CPP_ADJUST_WRITTEN (pfile
, 2);
2602 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2609 /* If macro wants an arglist, verify that a '(' follows.
2610 first skip all whitespace, copying it to the output
2611 after the macro name. Then, if there is no '(',
2612 decide this is not a macro call and leave things that way. */
2613 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2615 int is_macro_call
, macbuf_whitespace
= 0;
2617 parse_set_mark (pfile
);
2620 cpp_skip_hspace (pfile
);
2622 is_macro_call
= c
== '(';
2631 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2633 if (CPP_BUFFER (pfile
)->mark
!=
2634 (CPP_BUFFER (pfile
)->cur
2635 - CPP_BUFFER (pfile
)->buf
))
2636 macbuf_whitespace
= 1;
2638 /* The mark goes away automatically when
2639 the buffer is popped. */
2640 cpp_pop_buffer (pfile
);
2641 parse_set_mark (pfile
);
2649 parse_goto_mark (pfile
);
2650 if (macbuf_whitespace
)
2651 CPP_PUTC (pfile
, ' ');
2654 parse_clear_mark (pfile
);
2658 /* This is now known to be a macro call.
2659 Expand the macro, reading arguments as needed,
2660 and push the expansion on the input stack. */
2661 macroexpand (pfile
, hp
);
2662 CPP_SET_WRITTEN (pfile
, before_name_written
);
2666 case ' ': case '\t': case '\v':
2669 CPP_PUTC (pfile
, c
);
2671 if (c
== EOF
|| !is_hspace(c
))
2678 if (CPP_BUFFER (pfile
)->has_escapes
)
2683 if (pfile
->output_escapes
)
2684 CPP_PUTS (pfile
, "\r-", 2);
2685 parse_name (pfile
, GETC ());
2690 CPP_RESERVE (pfile
, 2);
2691 if (pfile
->output_escapes
)
2692 CPP_PUTC_Q (pfile
, '\r');
2693 CPP_PUTC_Q (pfile
, c
);
2698 cpp_ice (pfile
, "unrecognized escape \\r%c", c
);
2704 /* Backslash newline is ignored. */
2705 CPP_BUMP_LINE (pfile
);
2710 CPP_PUTC (pfile
, c
);
2711 if (pfile
->only_seen_white
== 0)
2712 pfile
->only_seen_white
= 1;
2713 CPP_BUMP_LINE (pfile
);
2714 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2717 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2718 output_line_command (pfile
, same_file
);
2722 case '(': token
= CPP_LPAREN
; goto char1
;
2723 case ')': token
= CPP_RPAREN
; goto char1
;
2724 case '{': token
= CPP_LBRACE
; goto char1
;
2725 case '}': token
= CPP_RBRACE
; goto char1
;
2726 case ',': token
= CPP_COMMA
; goto char1
;
2727 case ';': token
= CPP_SEMICOLON
; goto char1
;
2733 pfile
->only_seen_white
= 0;
2734 CPP_PUTC (pfile
, c
);
2740 /* Like cpp_get_token, but skip spaces and comments. */
2743 cpp_get_non_space_token (pfile
)
2746 int old_written
= CPP_WRITTEN (pfile
);
2749 enum cpp_token token
= cpp_get_token (pfile
);
2750 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2751 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2753 CPP_SET_WRITTEN (pfile
, old_written
);
2757 /* Parse an identifier starting with C. */
2760 parse_name (pfile
, c
)
2772 if (c
== '$' && CPP_PEDANTIC (pfile
))
2773 cpp_pedwarn (pfile
, "`$' in identifier");
2775 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2776 CPP_PUTC_Q (pfile
, c
);
2781 CPP_NUL_TERMINATE_Q (pfile
);
2785 /* Parse a string starting with C. A single quoted string is treated
2786 like a double -- some programs (e.g., troff) are perverse this way.
2787 (However, a single quoted string is not allowed to extend over
2790 parse_string (pfile
, c
)
2794 long start_line
, start_column
;
2796 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2798 CPP_PUTC (pfile
, c
);
2804 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2806 /* try harder: this string crosses a macro expansion
2807 boundary. This can happen naturally if -traditional.
2808 Otherwise, only -D can make a macro with an unmatched
2810 cpp_pop_buffer (pfile
);
2814 cpp_error_with_line (pfile
, start_line
, start_column
,
2815 "unterminated string or character constant");
2816 if (pfile
->multiline_string_line
!= start_line
2817 && pfile
->multiline_string_line
!= 0)
2818 cpp_error_with_line (pfile
,
2819 pfile
->multiline_string_line
, -1,
2820 "possible real start of unterminated constant");
2821 pfile
->multiline_string_line
= 0;
2824 CPP_PUTC (pfile
, cc
);
2828 CPP_BUMP_LINE (pfile
);
2831 /* In Fortran and assembly language, silently terminate
2832 strings of either variety at end of line. This is a
2833 kludge around not knowing where comments are in these
2835 if (CPP_OPTIONS (pfile
)->lang_fortran
2836 || CPP_OPTIONS (pfile
)->lang_asm
)
2838 /* Character constants may not extend over multiple lines.
2839 In Standard C, neither may strings. We accept multiline
2840 strings as an extension. */
2843 cpp_error_with_line (pfile
, start_line
, start_column
,
2844 "unterminated character constant");
2847 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2848 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2849 "string constant runs past end of line");
2850 if (pfile
->multiline_string_line
== 0)
2851 pfile
->multiline_string_line
= start_line
;
2855 CPP_ADJUST_WRITTEN (pfile
, -1);
2856 if (CPP_BUFFER (pfile
)->has_escapes
)
2858 cpp_ice (pfile
, "\\r escape inside string constant");
2862 /* Backslash newline is replaced by nothing at all. */
2863 CPP_BUMP_LINE (pfile
);
2869 CPP_PUTC (pfile
, cc
);
2881 /* Read an assertion into the token buffer, converting to
2882 canonical form: `#predicate(a n swe r)' The next non-whitespace
2883 character to read should be the first letter of the predicate.
2884 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2885 with answer (see callers for why). In case of 0, an error has been
2888 parse_assertion (pfile
)
2892 cpp_skip_hspace (pfile
);
2894 if (! is_idstart(c
))
2896 cpp_error (pfile
, "assertion predicate is not an identifier");
2899 CPP_PUTC(pfile
, '#');
2901 parse_name(pfile
, c
);
2906 if (is_hspace(c
) || c
== '\r')
2907 cpp_skip_hspace (pfile
);
2913 CPP_PUTC(pfile
, '(');
2916 while ((c
= GETC()) != ')')
2922 CPP_PUTC(pfile
, ' ');
2926 else if (c
== '\n' || c
== EOF
)
2928 if (c
== '\n') FORWARD(-1);
2929 cpp_error (pfile
, "un-terminated assertion answer");
2933 /* \r cannot be a macro escape here. */
2934 CPP_BUMP_LINE (pfile
);
2937 CPP_PUTC (pfile
, c
);
2942 if (pfile
->limit
[-1] == ' ')
2943 pfile
->limit
[-1] = ')';
2944 else if (pfile
->limit
[-1] == '(')
2946 cpp_error (pfile
, "empty token sequence in assertion");
2950 CPP_PUTC (pfile
, ')');
2952 CPP_NUL_TERMINATE (pfile
);
2957 do_assert (pfile
, keyword
)
2959 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2963 HASHNODE
*base
, *this;
2964 int baselen
, thislen
;
2966 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2967 && !CPP_BUFFER (pfile
)->system_header_p
)
2968 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
2970 cpp_skip_hspace (pfile
);
2971 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2972 ret
= parse_assertion (pfile
);
2977 cpp_error (pfile
, "missing token-sequence in `#assert'");
2981 cpp_skip_hspace (pfile
);
2983 if (c
!= EOF
&& c
!= '\n')
2985 cpp_error (pfile
, "junk at end of `#assert'");
2989 thislen
= strlen (sym
);
2990 baselen
= index (sym
, '(') - sym
;
2991 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2994 cpp_warning (pfile
, "`%s' re-asserted", sym
);
2998 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3000 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
3001 else if (base
->type
!= T_ASSERT
)
3003 /* Token clash - but with what?! */
3004 cpp_ice (pfile
, "base->type != T_ASSERT in do_assert");
3008 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
3009 (char *)base
->value
.aschain
, -1);
3010 base
->value
.aschain
= this;
3012 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3016 skip_rest_of_line (pfile
);
3017 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3022 do_unassert (pfile
, keyword
)
3024 const struct directive
*keyword ATTRIBUTE_UNUSED
;
3028 long baselen
, thislen
;
3029 HASHNODE
*base
, *this, *next
;
3031 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
3032 && !CPP_BUFFER (pfile
)->system_header_p
)
3033 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
3035 cpp_skip_hspace (pfile
);
3037 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
3038 ret
= parse_assertion (pfile
);
3042 cpp_skip_hspace (pfile
);
3044 if (c
!= EOF
&& c
!= '\n')
3045 cpp_error (pfile
, "junk at end of `#unassert'");
3047 thislen
= strlen (sym
);
3050 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
3052 goto error
; /* It isn't an error to #undef what isn't #defined,
3053 so it isn't an error to #unassert what isn't
3054 #asserted either. */
3056 for (this = base
->value
.aschain
; this; this = next
)
3058 next
= this->value
.aschain
;
3059 delete_macro (this);
3061 delete_macro (base
);
3065 baselen
= index (sym
, '(') - sym
;
3066 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
3067 if (! base
) goto error
;
3068 this = cpp_lookup (pfile
, sym
, thislen
, -1);
3069 if (! this) goto error
;
3072 while (next
->value
.aschain
!= this)
3073 next
= next
->value
.aschain
;
3075 next
->value
.aschain
= this->value
.aschain
;
3076 delete_macro (this);
3078 if (base
->value
.aschain
== NULL
)
3079 delete_macro (base
); /* Last answer for this predicate deleted. */
3082 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3085 skip_rest_of_line (pfile
);
3086 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3090 /* Process STR as if it appeared as the body of an #unassert. */
3092 cpp_unassert (pfile
, str
)
3096 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3098 do_assert (pfile
, NULL
);
3099 cpp_pop_buffer (pfile
);
3104 cpp_read_check_assertion (pfile
)
3107 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
3111 FORWARD (1); /* Skip '#' */
3112 cpp_skip_hspace (pfile
);
3113 if (! parse_assertion (pfile
))
3117 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
3121 pfile
->limit
= name
;
3125 /* Remember the current position of PFILE. */
3128 parse_set_mark (pfile
)
3131 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3133 cpp_ice (pfile
, "ip->mark != -1 in parse_set_mark");
3135 ip
->mark
= ip
->cur
- ip
->buf
;
3138 /* Clear the current mark - we no longer need it. */
3141 parse_clear_mark (pfile
)
3144 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3146 cpp_ice (pfile
, "ip->mark == -1 in parse_clear_mark");
3151 /* Backup the current position of PFILE to that saved in its mark,
3152 and clear the mark. */
3155 parse_goto_mark (pfile
)
3158 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3160 cpp_ice (pfile
, "ip->mark == -1 in parse_goto_mark");
3162 ip
->cur
= ip
->buf
+ ip
->mark
;