2 Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Per Bothner, 1994-95.
4 Based on CCCP program by Paul Rubin, June 1986
5 Adapted to ANSI C, Richard Stallman, Jan 1987
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
29 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
31 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
32 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
33 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
34 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
35 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
36 (Note that it is false while we're expanding macro *arguments*.) */
37 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->data != NULL)
39 /* Forward declarations. */
41 static char *my_strerror
PROTO ((int));
42 static void validate_else
PROTO ((cpp_reader
*, const char *));
43 static HOST_WIDEST_INT eval_if_expression
PROTO ((cpp_reader
*));
45 static void conditional_skip
PROTO ((cpp_reader
*, int,
46 enum node_type
, U_CHAR
*));
47 static void skip_if_group
PROTO ((cpp_reader
*));
49 static void parse_name
PARAMS ((cpp_reader
*, int));
50 static void parse_string
PARAMS ((cpp_reader
*, int));
51 static int parse_assertion
PARAMS ((cpp_reader
*));
53 /* External declarations. */
55 extern HOST_WIDEST_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
57 /* `struct directive' defines one #-directive, including how to handle it. */
60 int length
; /* Length of name */
61 int (*func
) /* Function to handle directive */
62 PARAMS ((cpp_reader
*, const struct directive
*));
63 const char *name
; /* Name of directive */
64 enum node_type type
; /* Code which describes which directive. */
67 /* These functions are declared to return int instead of void since they
68 are going to be placed in a table and some old compilers have trouble with
69 pointers to functions returning void. */
71 static int do_define
PARAMS ((cpp_reader
*, const struct directive
*));
72 static int do_line
PARAMS ((cpp_reader
*, const struct directive
*));
73 static int do_include
PARAMS ((cpp_reader
*, const struct directive
*));
74 static int do_undef
PARAMS ((cpp_reader
*, const struct directive
*));
75 static int do_error
PARAMS ((cpp_reader
*, const struct directive
*));
76 static int do_pragma
PARAMS ((cpp_reader
*, const struct directive
*));
77 static int do_ident
PARAMS ((cpp_reader
*, const struct directive
*));
78 static int do_if
PARAMS ((cpp_reader
*, const struct directive
*));
79 static int do_xifdef
PARAMS ((cpp_reader
*, const struct directive
*));
80 static int do_else
PARAMS ((cpp_reader
*, const struct directive
*));
81 static int do_elif
PARAMS ((cpp_reader
*, const struct directive
*));
82 static int do_endif
PARAMS ((cpp_reader
*, const struct directive
*));
84 static int do_sccs
PARAMS ((cpp_reader
*, const struct directive
*));
86 static int do_assert
PARAMS ((cpp_reader
*, const struct directive
*));
87 static int do_unassert
PARAMS ((cpp_reader
*, const struct directive
*));
88 static int do_warning
PARAMS ((cpp_reader
*, const struct directive
*));
90 /* Here is the actual list of #-directives.
91 This table is ordered by frequency of occurrence; the numbers
92 at the end are directive counts from all the source code I have
93 lying around (egcs and libc CVS as of 1999-05-18, plus grub-0.5.91,
94 linux-2.2.9, and pcmcia-cs-3.0.9). */
96 static const struct directive directive_table
[] = {
98 { 6, do_define
, "define", T_DEFINE
}, /* 270554 */
99 { 7, do_include
, "include", T_INCLUDE
}, /* 52262 */
100 { 5, do_endif
, "endif", T_ENDIF
}, /* 45855 */
101 { 5, do_xifdef
, "ifdef", T_IFDEF
}, /* 22000 */
102 { 2, do_if
, "if", T_IF
}, /* 18162 */
103 { 4, do_else
, "else", T_ELSE
}, /* 9863 */
104 { 6, do_xifdef
, "ifndef", T_IFNDEF
}, /* 9675 */
105 { 5, do_undef
, "undef", T_UNDEF
}, /* 4837 */
106 { 4, do_line
, "line", T_LINE
}, /* 2465 */
107 { 4, do_elif
, "elif", T_ELIF
}, /* 610 */
108 { 5, do_error
, "error", T_ERROR
}, /* 475 */
109 { 6, do_pragma
, "pragma", T_PRAGMA
}, /* 195 */
111 /* Extensions. All deprecated except #warning and #include_next. */
112 { 7, do_warning
, "warning", T_WARNING
}, /* 22 - GNU */
113 { 12, do_include
, "include_next", T_INCLUDE_NEXT
}, /* 19 - GNU */
114 { 5, do_ident
, "ident", T_IDENT
}, /* 11 - SVR4 */
115 { 6, do_include
, "import", T_IMPORT
}, /* 0 - ObjC */
116 { 6, do_assert
, "assert", T_ASSERT
}, /* 0 - SVR4 */
117 { 8, do_unassert
, "unassert", T_UNASSERT
}, /* 0 - SVR4 */
118 #ifdef SCCS_DIRECTIVE
119 { 4, do_sccs
, "sccs", T_SCCS
}, /* 0 - SVR2? */
121 { -1, 0, "", T_UNUSED
}
124 /* Place into PFILE a quoted string representing the string SRC.
125 Caller must reserve enough space in pfile->token_buffer. */
128 quote_string (pfile
, src
)
134 CPP_PUTC_Q (pfile
, '\"');
136 switch ((c
= *src
++))
140 CPP_PUTC_Q (pfile
, c
);
143 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
144 CPP_ADJUST_WRITTEN (pfile
, 4);
150 CPP_PUTC_Q (pfile
, '\\');
151 CPP_PUTC_Q (pfile
, c
);
155 CPP_PUTC_Q (pfile
, '\"');
156 CPP_NUL_TERMINATE_Q (pfile
);
161 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
164 cpp_grow_buffer (pfile
, n
)
168 long old_written
= CPP_WRITTEN (pfile
);
169 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
170 pfile
->token_buffer
= (U_CHAR
*)
171 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
172 CPP_SET_WRITTEN (pfile
, old_written
);
175 /* Process the string STR as if it appeared as the body of a #define
176 If STR is just an identifier, define it with value 1.
177 If STR has anything after the identifier, then it should
178 be identifier=definition. */
181 cpp_define (pfile
, str
)
188 /* Copy the entire option so we can modify it. */
189 count
= strlen (str
) + 3;
190 buf
= (U_CHAR
*) alloca (count
);
191 memcpy (buf
, str
, count
- 2);
192 /* Change the first "=" in the string to a space. If there is none,
193 tack " 1" on the end. */
194 p
= (U_CHAR
*) strchr (buf
, '=');
201 strcpy (&buf
[count
-3], " 1");
203 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
205 do_define (pfile
, NULL
);
206 cpp_pop_buffer (pfile
);
210 /* Process the string STR as if it appeared as the body of a #assert. */
212 cpp_assert (pfile
, str
)
216 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
218 do_assert (pfile
, NULL
);
219 cpp_pop_buffer (pfile
);
224 static enum cpp_token
225 null_underflow (pfile
)
226 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
232 null_cleanup (pbuf
, pfile
)
233 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
234 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
239 /* Skip a comment - C, C++, or Chill style. M is the first character
240 of the comment marker. If this really is a comment, skip to its
241 end and return ' '. If we hit end-of-file before end-of-comment,
242 return EOF. If this is not a comment, return M (which will be
246 skip_comment (pfile
, m
)
250 if (m
== '/' && PEEKC() == '*')
256 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
262 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
265 else if (c
== '\n' || c
== '\r')
266 /* \r cannot be a macro escape marker here. */
267 CPP_BUMP_LINE (pfile
);
268 else if (c
== '/' && prev_c
== '*')
270 else if (c
== '*' && prev_c
== '/'
271 && CPP_OPTIONS (pfile
)->warn_comments
)
272 cpp_warning (pfile
, "`/*' within comment");
277 else if ((m
== '/' && PEEKC() == '/'
278 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
279 || (m
== '-' && PEEKC() == '-'
280 && CPP_OPTIONS (pfile
)->chill
))
287 return ' '; /* Allow // to be terminated by EOF. */
290 /* Don't consider final '\n' to be part of comment. */
295 /* \r cannot be a macro escape marker here. */
296 CPP_BUMP_LINE (pfile
);
303 /* Identical to skip_comment except that it copies the comment into the
304 token_buffer. This is used if put_out_comments. */
306 copy_comment (pfile
, m
)
310 if (m
== '/' && PEEKC() == '*')
315 CPP_PUTC (pfile
, '/');
316 CPP_PUTC (pfile
, '*');
318 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
324 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
325 /* We must pretend this was a legitimate comment, so that the
326 output in token_buffer is not passed back tagged CPP_POP. */
331 /* \r cannot be a macro escape marker here. */
332 CPP_BUMP_LINE (pfile
);
340 CPP_BUMP_LINE (pfile
);
342 else if (c
== '/' && prev_c
== '*')
344 else if (c
== '*' && prev_c
== '/'
345 && CPP_OPTIONS (pfile
)->warn_comments
)
346 cpp_warning (pfile
, "`/*' within comment");
351 else if ((m
== '/' && PEEKC() == '/'
352 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
353 || (m
== '-' && PEEKC() == '-'
354 && CPP_OPTIONS (pfile
)->chill
))
363 return ' '; /* Allow line comments to be terminated by EOF. */
366 /* Don't consider final '\n' to be part of comment. */
371 /* \r cannot be a macro escape marker here. */
372 CPP_BUMP_LINE (pfile
);
382 /* Skip whitespace \-newline and comments. Does not macro-expand. */
385 cpp_skip_hspace (pfile
)
394 else if (is_hor_space
[c
])
396 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
397 cpp_pedwarn (pfile
, "%s in preprocessing directive",
398 c
== '\f' ? "formfeed" : "vertical tab");
402 /* \r is a backslash-newline marker if !has_escapes, and
403 a deletable-whitespace or no-reexpansion marker otherwise. */
404 if (CPP_BUFFER (pfile
)->has_escapes
)
412 CPP_BUFFER (pfile
)->lineno
++;
414 else if (c
== '/' || c
== '-')
416 c
= skip_comment (pfile
, c
);
428 /* Read the rest of the current line.
429 The line is appended to PFILE's output buffer. */
432 copy_rest_of_line (pfile
)
443 CPP_NUL_TERMINATE (pfile
);
447 if (CPP_BUFFER (pfile
)->has_escapes
)
451 CPP_BUFFER (pfile
)->lineno
++;
456 parse_string (pfile
, c
);
459 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile
))
461 CPP_PUTS (pfile
, "/**/", 4);
462 skip_comment (pfile
, c
);
465 /* else fall through */
467 c
= skip_comment (pfile
, c
);
472 if (CPP_PEDANTIC (pfile
))
473 cpp_pedwarn (pfile
, "%s in preprocessing directive",
474 c
== '\f' ? "formfeed" : "vertical tab");
482 /* FIXME: It is almost definitely a performance win to make this do
483 the scan itself. >75% of calls to copy_r_o_l are from here or
484 skip_if_group, which means the common case is to copy stuff into the
485 token_buffer only to discard it. */
487 skip_rest_of_line (pfile
)
490 long old
= CPP_WRITTEN (pfile
);
491 copy_rest_of_line (pfile
);
492 CPP_SET_WRITTEN (pfile
, old
);
495 /* Handle a possible # directive.
496 '#' has already been read. */
499 handle_directive (pfile
)
503 register const struct directive
*kt
;
506 long old_written
= CPP_WRITTEN (pfile
);
508 cpp_skip_hspace (pfile
);
511 if (c
>= '0' && c
<= '9')
513 /* Handle # followed by a line number. */
514 if (CPP_PEDANTIC (pfile
))
515 cpp_pedwarn (pfile
, "`#' followed by integer");
516 do_line (pfile
, NULL
);
520 /* Now find the directive name. */
521 CPP_PUTC (pfile
, '#');
522 parse_name (pfile
, GETC());
523 ident
= pfile
->token_buffer
+ old_written
+ 1;
524 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
525 if (ident_length
== 0)
527 /* A line of just `#' becomes blank. */
535 * Decode the keyword and call the appropriate expansion
536 * routine, after moving the input pointer up to the next line.
538 for (kt
= directive_table
; ; kt
++)
542 if (kt
->length
== ident_length
543 && !strncmp (kt
->name
, ident
, ident_length
))
547 CPP_SET_WRITTEN (pfile
, old_written
);
548 (*kt
->func
) (pfile
, kt
);
553 /* Pass a directive through to the output file.
554 BUF points to the contents of the directive, as a contiguous string.
555 LEN is the length of the string pointed to by BUF.
556 KEYWORD is the keyword-table entry for the directive. */
559 pass_thru_directive (buf
, len
, pfile
, keyword
)
563 const struct directive
*keyword
;
565 register unsigned keyword_length
= keyword
->length
;
567 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
568 CPP_PUTC_Q (pfile
, '#');
569 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
570 if (len
!= 0 && buf
[0] != ' ')
571 CPP_PUTC_Q (pfile
, ' ');
572 CPP_PUTS_Q (pfile
, buf
, len
);
575 /* Check a purported macro name SYMNAME, and yield its length.
576 ASSERTION is nonzero if this is really for an assertion name. */
579 check_macro_name (pfile
, symname
, assertion
)
587 for (p
= symname
; is_idchar
[*p
]; p
++)
589 sym_length
= p
- symname
;
591 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
593 assertion
? "invalid assertion name" : "invalid macro name");
594 else if (!is_idstart
[*symname
]
595 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
596 U_CHAR
*msg
; /* what pain... */
597 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
598 bcopy (symname
, msg
, sym_length
);
602 ? "invalid assertion name `%s'"
603 : "invalid macro name `%s'"),
609 /* Process a #define command.
610 KEYWORD is the keyword-table entry for #define,
611 or NULL for a "predefined" macro. */
614 do_define (pfile
, keyword
)
616 const struct directive
*keyword
;
622 U_CHAR
*macro
, *buf
, *end
;
624 here
= CPP_WRITTEN (pfile
);
625 copy_rest_of_line (pfile
);
627 /* Copy out the line so we can pop the token buffer. */
628 buf
= pfile
->token_buffer
+ here
;
629 end
= CPP_PWRITTEN (pfile
);
630 macro
= (U_CHAR
*) alloca (end
- buf
+ 1);
631 bcopy (buf
, macro
, end
- buf
+ 1);
632 end
= macro
+ (end
- buf
);
634 CPP_SET_WRITTEN (pfile
, here
);
636 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
640 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
642 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
645 /* Redefining a precompiled key is ok. */
646 if (hp
->type
== T_PCSTRING
)
648 /* Redefining a macro is ok if the definitions are the same. */
649 else if (hp
->type
== T_MACRO
)
650 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
651 /* Redefining a constant is ok with -D. */
652 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
653 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
654 /* Print the warning if it's not ok. */
657 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
658 if (hp
->type
== T_MACRO
)
659 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
660 hp
->value
.defn
->line
,
661 "this is the location of the previous definition");
663 /* Replace the old definition. */
665 hp
->value
.defn
= mdef
.defn
;
668 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, T_MACRO
,
669 (char *) mdef
.defn
, hashcode
);
673 if (CPP_OPTIONS (pfile
)->debug_output
674 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
675 dump_definition (pfile
, mdef
);
676 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
677 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
684 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
685 If BUFFER != NULL, then use the LENGTH characters in BUFFER
686 as the new input buffer.
687 Return the new buffer, or NULL on failure. */
690 cpp_push_buffer (pfile
, buffer
, length
)
695 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
697 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
699 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
703 new = (cpp_buffer
*) xcalloc (sizeof (cpp_buffer
), 1);
705 new->if_stack
= pfile
->if_stack
;
706 new->cleanup
= null_cleanup
;
707 new->underflow
= null_underflow
;
708 new->buf
= new->cur
= buffer
;
709 new->alimit
= new->rlimit
= buffer
+ length
;
713 CPP_BUFFER (pfile
) = new;
718 cpp_pop_buffer (pfile
)
721 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
722 (*buf
->cleanup
) (buf
, pfile
);
723 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
725 pfile
->buffer_stack_depth
--;
726 return CPP_BUFFER (pfile
);
729 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
730 Pop the buffer when done. */
733 cpp_scan_buffer (pfile
)
736 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
737 enum cpp_token token
;
738 if (CPP_OPTIONS (pfile
)->no_output
)
740 long old_written
= CPP_WRITTEN (pfile
);
741 /* In no-output mode, we can ignore everything but directives. */
744 if (! pfile
->only_seen_white
)
745 skip_rest_of_line (pfile
);
746 token
= cpp_get_token (pfile
);
747 if (token
== CPP_EOF
) /* Should not happen ... */
749 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
751 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
752 != CPP_NULL_BUFFER (pfile
))
753 cpp_pop_buffer (pfile
);
757 CPP_SET_WRITTEN (pfile
, old_written
);
763 token
= cpp_get_token (pfile
);
764 if (token
== CPP_EOF
) /* Should not happen ... */
766 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
768 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
))
769 != CPP_NULL_BUFFER (pfile
))
770 cpp_pop_buffer (pfile
);
778 * Rescan a string (which may have escape marks) into pfile's buffer.
779 * Place the result in pfile->token_buffer.
781 * The input is copied before it is scanned, so it is safe to pass
782 * it something from the token_buffer that will get overwritten
783 * (because it follows CPP_WRITTEN). This is used by do_include.
787 cpp_expand_to_buffer (pfile
, buf
, length
)
792 register cpp_buffer
*ip
;
798 cpp_fatal (pfile
, "internal error: length < 0 in cpp_expand_to_buffer");
802 /* Set up the input on the input stack. */
804 buf1
= (U_CHAR
*) alloca (length
+ 1);
805 memcpy (buf1
, buf
, length
);
808 ip
= cpp_push_buffer (pfile
, buf1
, length
);
813 /* Scan the input, create the output. */
814 save_no_output
= CPP_OPTIONS (pfile
)->no_output
;
815 CPP_OPTIONS (pfile
)->no_output
= 0;
816 cpp_scan_buffer (pfile
);
817 CPP_OPTIONS (pfile
)->no_output
= save_no_output
;
819 CPP_NUL_TERMINATE (pfile
);
823 cpp_buf_line_and_col (pbuf
, linep
, colp
)
824 register cpp_buffer
*pbuf
;
829 *linep
= pbuf
->lineno
;
831 *colp
= pbuf
->cur
- pbuf
->line_base
;
841 /* Return the cpp_buffer that corresponds to a file (not a macro). */
844 cpp_file_buffer (pfile
)
847 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
849 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
850 if (ip
->fname
!= NULL
)
856 * write out a #line command, for instance, after an #include file.
857 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
861 output_line_command (pfile
, file_change
)
863 enum file_change_code file_change
;
866 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
868 if (ip
->fname
== NULL
)
871 if (CPP_OPTIONS (pfile
)->no_line_commands
872 || CPP_OPTIONS (pfile
)->no_output
)
875 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
877 /* If the current file has not changed, we omit the #line if it would
878 appear to be a no-op, and we output a few newlines instead
879 if we want to increase the line number by a small amount.
880 We cannot do this if pfile->lineno is zero, because that means we
881 haven't output any line commands yet. (The very first line command
882 output is a `same_file' command.) */
883 if (file_change
== same_file
&& pfile
->lineno
!= 0)
885 if (line
== pfile
->lineno
)
888 /* If the inherited line number is a little too small,
889 output some newlines instead of a #line command. */
890 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
892 CPP_RESERVE (pfile
, 20);
893 while (line
> pfile
->lineno
)
895 CPP_PUTC_Q (pfile
, '\n');
902 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
903 CPP_PUTS_Q (pfile
, "# ", 2);
905 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
906 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
908 quote_string (pfile
, ip
->nominal_fname
);
909 if (file_change
!= same_file
)
911 CPP_PUTC_Q (pfile
, ' ');
912 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
914 /* Tell cc1 if following text comes from a system header file. */
915 if (ip
->system_header_p
)
917 CPP_PUTC_Q (pfile
, ' ');
918 CPP_PUTC_Q (pfile
, '3');
920 #ifndef NO_IMPLICIT_EXTERN_C
921 /* Tell cc1plus if following text should be treated as C. */
922 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
924 CPP_PUTC_Q (pfile
, ' ');
925 CPP_PUTC_Q (pfile
, '4');
928 CPP_PUTC_Q (pfile
, '\n');
929 pfile
->lineno
= line
;
933 /* Like cpp_get_token, except that it does not read past end-of-line.
934 Also, horizontal space is skipped, and macros are popped. */
936 static enum cpp_token
937 get_directive_token (pfile
)
942 long old_written
= CPP_WRITTEN (pfile
);
943 enum cpp_token token
;
944 cpp_skip_hspace (pfile
);
945 if (PEEKC () == '\n')
947 token
= cpp_get_token (pfile
);
951 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
953 /* ... else fall though ... */
954 case CPP_HSPACE
: case CPP_COMMENT
:
955 CPP_SET_WRITTEN (pfile
, old_written
);
963 /* Handle #include and #import.
964 This function expects to see "fname" or <fname> on the input.
966 The input is normally in part of the output_buffer following
967 CPP_WRITTEN, and will get overwritten by output_line_command.
968 I.e. in input file specification has been popped by handle_directive.
972 do_include (pfile
, keyword
)
974 const struct directive
*keyword
;
976 int importing
= (keyword
->type
== T_IMPORT
);
977 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
978 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
979 int before
; /* included before? */
984 enum cpp_token token
;
986 /* Chain of dirs to search */
987 struct include_hash
*ihash
;
988 struct file_name_list
*search_start
;
990 long old_written
= CPP_WRITTEN (pfile
);
994 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
997 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
999 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1002 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1003 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1004 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1006 pfile
->import_warning
= 1;
1008 "#import is obsolete, use an #ifndef wrapper in the header file");
1011 pfile
->parsing_include_directive
++;
1012 token
= get_directive_token (pfile
);
1013 pfile
->parsing_include_directive
--;
1015 if (token
== CPP_STRING
)
1017 if (pfile
->token_buffer
[old_written
] == '<')
1021 else if (token
== CPP_NAME
)
1023 /* Support '#include xyz' like VAX-C. It is taken as
1024 '#include <xyz.h>' and generates a warning. */
1026 "`#include filename' is obsolete, use `#include <filename.h>'");
1029 /* Append the missing `.h' to the name. */
1030 CPP_PUTS (pfile
, ".h", 2);
1036 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1037 CPP_SET_WRITTEN (pfile
, old_written
);
1038 skip_rest_of_line (pfile
);
1042 flen
= CPP_WRITTEN (pfile
) - old_written
;
1043 ftok
= (unsigned char *) alloca (flen
+ 1);
1044 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1047 if (get_directive_token (pfile
) != CPP_VSPACE
)
1049 cpp_error (pfile
, "junk at end of `#include'");
1050 skip_rest_of_line (pfile
);
1053 CPP_SET_WRITTEN (pfile
, old_written
);
1057 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1061 if (CPP_OPTIONS (pfile
)->dump_includes
)
1062 pass_thru_directive (ftok
,
1065 - ((token
== CPP_NAME
) ? 2 : 0)
1070 if (token
== CPP_STRING
)
1080 for (fp
= CPP_BUFFER (pfile
);
1081 fp
!= CPP_NULL_BUFFER (pfile
);
1082 fp
= CPP_PREV_BUFFER (fp
))
1083 if (fp
->fname
!= NULL
)
1086 if (fp
== CPP_NULL_BUFFER (pfile
))
1088 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
1092 /* For #include_next, skip in the search path past the dir in which the
1093 containing file was found. Treat files specified using an absolute path
1094 as if there are no more directories to search. Treat the primary source
1095 file like any other included source, but generate a warning. */
1096 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1098 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1099 search_start
= fp
->ihash
->foundhere
->next
;
1104 cpp_warning (pfile
, "#include_next in primary source file");
1107 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1110 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1113 search_start
= fp
->actual_dir
;
1116 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1122 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1126 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1133 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1134 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1135 (pfile
->system_include_depth
> 0)))
1137 if (!angle_brackets
)
1138 deps_output (pfile
, ftok
, ' ');
1142 struct file_name_list
*ptr
;
1143 /* If requested as a system header, assume it belongs in
1144 the first system header directory. */
1145 if (CPP_OPTIONS (pfile
)->bracket_include
)
1146 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1148 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1150 p
= (char *) alloca (strlen (ptr
->name
)
1151 + strlen (ftok
) + 2);
1152 if (*ptr
->name
!= '\0')
1154 strcpy (p
, ptr
->name
);
1158 deps_output (pfile
, p
, ' ');
1161 /* If -M was specified, and this header file won't be added to
1162 the dependency list, then don't count this as an error,
1163 because we can still produce correct output. Otherwise, we
1164 can't produce correct output, because there may be
1165 dependencies we need inside the missing file, and we don't
1166 know what directory this missing file exists in. */
1167 else if (CPP_PRINT_DEPS (pfile
)
1168 && (CPP_PRINT_DEPS (pfile
)
1169 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1170 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1172 cpp_error_from_errno (pfile
, ftok
);
1177 /* For -M, add the file to the dependencies on its first inclusion. */
1178 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1179 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1180 deps_output (pfile
, ihash
->name
, ' ');
1182 /* Handle -H option. */
1183 if (CPP_OPTIONS(pfile
)->print_include_names
)
1185 fp
= CPP_BUFFER (pfile
);
1186 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1188 fprintf (stderr
, " %s\n", ihash
->name
);
1191 /* Actually process the file */
1194 ihash
->control_macro
= "";
1196 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1203 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1205 if (finclude (pfile
, fd
, ihash
))
1207 output_line_command (pfile
, enter_file
);
1208 pfile
->only_seen_white
= 2;
1214 /* Subroutine of do_line. Read next token from PFILE without adding it to
1215 the output buffer. If it is a number between 1 and 4, store it in *NUM
1216 and return 1; otherwise, return 0 and complain if we aren't at the end
1217 of the directive. */
1220 read_line_number (pfile
, num
)
1224 long save_written
= CPP_WRITTEN (pfile
);
1225 U_CHAR
*p
= pfile
->token_buffer
+ save_written
;
1226 enum cpp_token token
= get_directive_token (pfile
);
1227 CPP_SET_WRITTEN (pfile
, save_written
);
1229 if (token
== CPP_NUMBER
&& *p
>= '1' && *p
<= '4' && p
[1] == '\0')
1236 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1237 cpp_error (pfile
, "invalid format `#line' command");
1242 /* Interpret #line command.
1243 Note that the filename string (if any) is treated as if it were an
1244 include filename. That means no escape handling. */
1247 do_line (pfile
, keyword
)
1249 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1251 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1253 long old_written
= CPP_WRITTEN (pfile
);
1254 enum file_change_code file_change
= same_file
;
1255 enum cpp_token token
;
1258 token
= get_directive_token (pfile
);
1260 if (token
!= CPP_NUMBER
)
1262 cpp_error (pfile
, "token after `#line' is not an integer");
1263 goto bad_line_directive
;
1266 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1269 cpp_error (pfile
, "token after `#line' is not an integer");
1270 goto bad_line_directive
;
1272 CPP_SET_WRITTEN (pfile
, old_written
);
1274 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1275 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1277 token
= get_directive_token (pfile
);
1279 if (token
== CPP_STRING
)
1281 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1282 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1283 int action_number
= 0;
1285 if (read_line_number (pfile
, &action_number
))
1287 if (CPP_PEDANTIC (pfile
))
1288 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1290 if (action_number
== 1)
1292 file_change
= enter_file
;
1293 read_line_number (pfile
, &action_number
);
1295 else if (action_number
== 2)
1297 file_change
= leave_file
;
1298 read_line_number (pfile
, &action_number
);
1300 if (action_number
== 3)
1302 ip
->system_header_p
= 1;
1303 read_line_number (pfile
, &action_number
);
1305 if (action_number
== 4)
1307 ip
->system_header_p
= 2;
1308 read_line_number (pfile
, &action_number
);
1314 if (strcmp (fname
, ip
->nominal_fname
))
1316 char *newname
, *oldname
;
1317 if (!strcmp (fname
, ip
->fname
))
1318 newname
= ip
->fname
;
1319 else if (ip
->last_nominal_fname
1320 && !strcmp (fname
, ip
->last_nominal_fname
))
1321 newname
= ip
->last_nominal_fname
;
1323 newname
= xstrdup (fname
);
1325 oldname
= ip
->nominal_fname
;
1326 ip
->nominal_fname
= newname
;
1328 if (ip
->last_nominal_fname
1329 && ip
->last_nominal_fname
!= oldname
1330 && ip
->last_nominal_fname
!= newname
1331 && ip
->last_nominal_fname
!= ip
->fname
)
1332 free (ip
->last_nominal_fname
);
1334 if (newname
== ip
->fname
)
1335 ip
->last_nominal_fname
= NULL
;
1337 ip
->last_nominal_fname
= oldname
;
1340 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1342 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1343 goto bad_line_directive
;
1346 /* The Newline at the end of this line remains to be processed.
1347 To put the next line at the specified line number,
1348 we must store a line number now that is one less. */
1349 ip
->lineno
= new_lineno
- 1;
1350 CPP_SET_WRITTEN (pfile
, old_written
);
1351 output_line_command (pfile
, file_change
);
1355 skip_rest_of_line (pfile
);
1356 CPP_SET_WRITTEN (pfile
, old_written
);
1360 /* Remove the definition of a symbol from the symbol table.
1361 According to the C standard, it is not an error to undef
1362 something that has no definitions. */
1364 do_undef (pfile
, keyword
)
1366 const struct directive
*keyword
;
1370 U_CHAR
*buf
, *name
, *limit
;
1372 long here
= CPP_WRITTEN (pfile
);
1373 enum cpp_token token
;
1375 cpp_skip_hspace (pfile
);
1377 if (! is_idstart
[c
])
1379 cpp_error (pfile
, "token after #undef is not an identifier");
1380 skip_rest_of_line (pfile
);
1384 parse_name (pfile
, c
);
1385 buf
= pfile
->token_buffer
+ here
;
1386 limit
= CPP_PWRITTEN(pfile
);
1388 /* Copy out the token so we can pop the token buffer. */
1389 name
= (U_CHAR
*) alloca (limit
- buf
+ 1);
1390 bcopy(buf
, name
, limit
- buf
);
1391 name
[limit
- buf
] = '\0';
1393 token
= get_directive_token (pfile
);
1394 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1396 cpp_pedwarn (pfile
, "junk on line after #undef");
1397 skip_rest_of_line (pfile
);
1400 CPP_SET_WRITTEN (pfile
, here
);
1402 sym_length
= check_macro_name (pfile
, buf
, 0);
1404 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1406 /* If we are generating additional info for debugging (with -g) we
1407 need to pass through all effective #undef commands. */
1408 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1409 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1410 if (hp
->type
!= T_MACRO
)
1411 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1418 /* Wrap do_undef for -U processing. */
1420 cpp_undef (pfile
, macro
)
1424 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1426 do_undef (pfile
, NULL
);
1427 cpp_pop_buffer (pfile
);
1433 * Report an error detected by the program we are processing.
1434 * Use the text of the line in the error message.
1435 * (We use error because it prints the filename & line#.)
1439 do_error (pfile
, keyword
)
1441 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1443 long here
= CPP_WRITTEN (pfile
);
1445 copy_rest_of_line (pfile
);
1446 text
= pfile
->token_buffer
+ here
;
1447 SKIP_WHITE_SPACE(text
);
1449 cpp_error (pfile
, "#error %s", text
);
1450 CPP_SET_WRITTEN (pfile
, here
);
1455 * Report a warning detected by the program we are processing.
1456 * Use the text of the line in the warning message, then continue.
1460 do_warning (pfile
, keyword
)
1462 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1465 long here
= CPP_WRITTEN(pfile
);
1466 copy_rest_of_line (pfile
);
1467 text
= pfile
->token_buffer
+ here
;
1468 SKIP_WHITE_SPACE(text
);
1470 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1471 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1473 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1474 if -pedantic-errors is given, #warning should cause an error. */
1475 cpp_pedwarn (pfile
, "#warning %s", text
);
1476 CPP_SET_WRITTEN (pfile
, here
);
1480 /* Report program identification.
1481 This is not precisely what cccp does with #ident, however I believe
1482 it matches `closely enough' (behavior is identical as long as there
1483 are no macros on the #ident line, which is pathological in my opinion). */
1486 do_ident (pfile
, keyword
)
1488 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1490 /* Allow #ident in system headers, since that's not user's fault. */
1491 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1492 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1494 CPP_PUTS (pfile
, "#ident ", 7);
1495 cpp_skip_hspace (pfile
);
1496 copy_rest_of_line (pfile
);
1501 /* Just check for some recognized pragmas that need validation here,
1502 and leave the text in the token buffer to be output. */
1505 do_pragma (pfile
, keyword
)
1507 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1512 CPP_PUTS (pfile
, "#pragma ", 8);
1513 cpp_skip_hspace (pfile
);
1515 here
= CPP_WRITTEN (pfile
);
1516 copy_rest_of_line (pfile
);
1517 buf
= pfile
->token_buffer
+ here
;
1519 if (!strncmp (buf
, "once", 4))
1521 cpp_buffer
*ip
= NULL
;
1523 /* Allow #pragma once in system headers, since that's not the user's
1525 if (!CPP_BUFFER (pfile
)->system_header_p
)
1526 cpp_warning (pfile
, "`#pragma once' is obsolete");
1528 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1530 if (ip
== CPP_NULL_BUFFER (pfile
))
1532 if (ip
->fname
!= NULL
)
1536 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1537 cpp_warning (pfile
, "`#pragma once' outside include file");
1539 ip
->ihash
->control_macro
= ""; /* never repeat */
1541 else if (!strncmp (buf
, "implementation", 14))
1543 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1544 been included yet. */
1545 struct include_hash
*ptr
;
1546 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1547 SKIP_WHITE_SPACE (p
);
1548 if (*p
== '\n' || *p
!= '\"')
1552 p
= (U_CHAR
*) index (fname
, '\"');
1554 fcopy
= (U_CHAR
*) alloca (p
- fname
+ 1);
1555 bcopy (fname
, fcopy
, p
- fname
);
1556 fcopy
[p
-fname
] = '\0';
1558 ptr
= include_hash (pfile
, fcopy
, 0);
1561 "`#pragma implementation' for `%s' appears after file is included",
1568 #ifdef SCCS_DIRECTIVE
1569 /* Just ignore #sccs, on systems where we define it at all. */
1572 do_sccs (pfile
, keyword
)
1574 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1576 if (CPP_PEDANTIC (pfile
))
1577 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1578 skip_rest_of_line (pfile
);
1584 /* We've found an `#if' directive. If the only thing before it in
1585 this file is white space, and if it is of the form
1586 `#if ! defined SYMBOL', then SYMBOL is a possible controlling macro
1587 for inclusion of this file. (See redundant_include_p in cppfiles.c
1588 for an explanation of controlling macros.) If so, return a
1589 malloc'd copy of SYMBOL. Otherwise, return NULL. */
1592 detect_if_not_defined (pfile
)
1595 U_CHAR
*control_macro
= 0;
1597 if (pfile
->only_seen_white
== 2)
1600 enum cpp_token token
;
1603 int need_rparen
= 0;
1605 /* Save state required for restore. */
1606 pfile
->no_macro_expand
++;
1607 parse_set_mark (pfile
);
1608 base_offset
= CPP_WRITTEN (pfile
);
1611 if (get_directive_token (pfile
) != CPP_OTHER
1612 || CPP_WRITTEN (pfile
) != (size_t) base_offset
+ 1
1613 || CPP_PWRITTEN (pfile
)[-1] != '!')
1616 /* ...then `defined', */
1617 token_offset
= CPP_WRITTEN (pfile
);
1618 token
= get_directive_token (pfile
);
1619 if (token
!= CPP_NAME
)
1621 ident
= pfile
->token_buffer
+ token_offset
;
1622 CPP_NUL_TERMINATE (pfile
);
1623 if (strcmp (ident
, "defined"))
1626 /* ...then an optional '(' and the name, */
1627 token_offset
= CPP_WRITTEN (pfile
);
1628 token
= get_directive_token (pfile
);
1629 if (token
== CPP_LPAREN
)
1631 token_offset
= CPP_WRITTEN (pfile
);
1632 token
= get_directive_token (pfile
);
1633 if (token
!= CPP_NAME
)
1637 else if (token
!= CPP_NAME
)
1640 ident
= pfile
->token_buffer
+ token_offset
;
1641 CPP_NUL_TERMINATE (pfile
);
1643 /* ...then the ')', if necessary, */
1644 if ((!need_rparen
|| get_directive_token (pfile
) == CPP_RPAREN
)
1645 /* ...and make sure there's nothing else on the line. */
1646 && get_directive_token (pfile
) == CPP_VSPACE
)
1647 control_macro
= xstrdup (ident
);
1650 CPP_SET_WRITTEN (pfile
, base_offset
);
1651 pfile
->no_macro_expand
--;
1652 parse_goto_mark (pfile
);
1655 return control_macro
;
1659 * handle #if command by
1660 * 1) inserting special `defined' keyword into the hash table
1661 * that gets turned into 0 or 1 by special_symbol (thus,
1662 * if the luser has a symbol called `defined' already, it won't
1663 * work inside the #if command)
1664 * 2) rescan the input into a temporary output buffer
1665 * 3) pass the output buffer to the yacc parser and collect a value
1666 * 4) clean up the mess left from steps 1 and 2.
1667 * 5) call conditional_skip to skip til the next #endif (etc.),
1668 * or not, depending on the value from step 3.
1672 do_if (pfile
, keyword
)
1674 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1676 U_CHAR
*control_macro
= detect_if_not_defined (pfile
);
1677 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1678 conditional_skip (pfile
, value
== 0, T_IF
, control_macro
);
1683 * handle a #elif directive by not changing if_stack either.
1684 * see the comment above do_else.
1688 do_elif (pfile
, keyword
)
1690 const struct directive
*keyword ATTRIBUTE_UNUSED
;
1692 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1693 cpp_error (pfile
, "`#elif' not within a conditional");
1696 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1697 cpp_error (pfile
, "`#elif' after `#else'");
1699 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1701 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
1702 && strcmp (pfile
->if_stack
->fname
,
1703 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
1704 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1705 fprintf (stderr
, ")\n");
1707 pfile
->if_stack
->type
= T_ELIF
;
1710 if (pfile
->if_stack
->if_succeeded
)
1711 skip_if_group (pfile
);
1713 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1715 skip_if_group (pfile
);
1717 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1718 output_line_command (pfile
, same_file
);
1725 * evaluate a #if expression in BUF, of length LENGTH,
1726 * then parse the result as a C expression and return the value as an int.
1729 static HOST_WIDEST_INT
1730 eval_if_expression (pfile
)
1733 HOST_WIDEST_INT value
;
1734 long old_written
= CPP_WRITTEN (pfile
);
1736 pfile
->pcp_inside_if
= 1;
1737 value
= cpp_parse_expr (pfile
);
1738 pfile
->pcp_inside_if
= 0;
1740 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1746 * routine to handle ifdef/ifndef. Try to look up the symbol,
1747 * then do or don't skip to the #endif/#else/#elif depending
1748 * on what directive is actually being processed.
1752 do_xifdef (pfile
, keyword
)
1754 const struct directive
*keyword
;
1757 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1760 enum cpp_token token
;
1761 int start_of_file
= 0;
1762 U_CHAR
*control_macro
= 0;
1763 int old_written
= CPP_WRITTEN (pfile
);
1765 /* Detect a #ifndef at start of file (not counting comments). */
1766 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1767 start_of_file
= pfile
->only_seen_white
== 2;
1769 pfile
->no_macro_expand
++;
1770 token
= get_directive_token (pfile
);
1771 pfile
->no_macro_expand
--;
1773 ident
= pfile
->token_buffer
+ old_written
;
1774 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1775 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1777 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1779 skip
= (keyword
->type
== T_IFDEF
);
1780 if (! CPP_TRADITIONAL (pfile
))
1781 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1783 else if (token
== CPP_NAME
)
1785 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1786 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1787 if (start_of_file
&& !skip
)
1789 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1790 bcopy (ident
, control_macro
, ident_length
+ 1);
1795 skip
= (keyword
->type
== T_IFDEF
);
1796 if (! CPP_TRADITIONAL (pfile
))
1797 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1800 if (!CPP_TRADITIONAL (pfile
))
1802 cpp_skip_hspace (pfile
);
1804 if (c
!= EOF
&& c
!= '\n')
1805 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1807 skip_rest_of_line (pfile
);
1811 /* Output a precondition for this macro. */
1812 if (hp
&& hp
->value
.defn
->predefined
)
1813 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
1816 fprintf (pcp_outfile
, "#undef ");
1817 while (is_idchar
[*cp
]) /* Ick! */
1818 fputc (*cp
++, pcp_outfile
);
1819 putc ('\n', pcp_outfile
);
1823 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1827 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1828 If this is a #ifndef starting at the beginning of a file,
1829 CONTROL_MACRO is the macro name tested by the #ifndef.
1830 Otherwise, CONTROL_MACRO is 0. */
1833 conditional_skip (pfile
, skip
, type
, control_macro
)
1836 enum node_type type
;
1837 U_CHAR
*control_macro
;
1839 IF_STACK_FRAME
*temp
;
1841 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1842 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1844 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1846 temp
->next
= pfile
->if_stack
;
1847 temp
->control_macro
= control_macro
;
1848 pfile
->if_stack
= temp
;
1850 pfile
->if_stack
->type
= type
;
1853 skip_if_group (pfile
);
1856 ++pfile
->if_stack
->if_succeeded
;
1857 output_line_command (pfile
, same_file
);
1861 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1862 return 0 if skipping should continue, 1 if it should halt. Also
1863 adjusts the if_stack as appropriate.
1864 The `#' has been read, but not the identifier. */
1867 consider_directive_while_skipping (pfile
, stack
)
1869 IF_STACK_FRAME
*stack
;
1871 long ident_len
, ident
;
1872 const struct directive
*kt
;
1873 IF_STACK_FRAME
*temp
;
1875 cpp_skip_hspace (pfile
);
1877 ident
= CPP_WRITTEN (pfile
);
1878 parse_name (pfile
, GETC());
1879 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1881 CPP_SET_WRITTEN (pfile
, ident
);
1883 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1884 if (kt
->length
== ident_len
1885 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
1891 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
1892 temp
->next
= pfile
->if_stack
;
1893 pfile
->if_stack
= temp
;
1894 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
1895 temp
->type
= kt
->type
;
1899 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
1900 validate_else (pfile
, "#else");
1903 if (pfile
->if_stack
->type
== T_ELSE
)
1904 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
1906 if (pfile
->if_stack
== stack
)
1910 pfile
->if_stack
->type
= kt
->type
;
1915 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
1916 validate_else (pfile
, "#endif");
1918 if (pfile
->if_stack
== stack
)
1921 temp
= pfile
->if_stack
;
1922 pfile
->if_stack
= temp
->next
;
1930 /* Don't let erroneous code go by. */
1931 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
1932 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
1936 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1937 * leaves input ptr at the sharp sign found.
1940 skip_if_group (pfile
)
1944 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
1945 U_CHAR
*beg_of_line
;
1948 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1950 CPP_PUTS (pfile
, "#failed\n", 8);
1952 output_line_command (pfile
, same_file
);
1955 old_written
= CPP_WRITTEN (pfile
);
1959 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
1961 if (! CPP_TRADITIONAL (pfile
))
1962 cpp_skip_hspace (pfile
);
1966 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1967 CPP_PUTC (pfile
, c
);
1968 CPP_BUMP_LINE (pfile
);
1973 if (consider_directive_while_skipping (pfile
, save_if_stack
))
1977 return; /* Caller will issue error. */
1980 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1982 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
1983 copy_rest_of_line (pfile
);
1987 copy_rest_of_line (pfile
);
1988 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
1993 return; /* Caller will issue error. */
1997 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1999 CPP_PUTC (pfile
, c
);
2002 CPP_BUMP_LINE (pfile
);
2006 /* Back up to the beginning of this line. Caller will process the
2008 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
2009 pfile
->only_seen_white
= 1;
2010 if (CPP_OPTIONS (pfile
)->output_conditionals
)
2012 CPP_PUTS (pfile
, "#endfailed\n", 11);
2018 * handle a #else directive. Do this by just continuing processing
2019 * without changing if_stack ; this is so that the error message
2020 * for missing #endif's etc. will point to the original #if. It
2021 * is possible that something different would be better.
2025 do_else (pfile
, keyword
)
2027 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2029 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2031 if (CPP_PEDANTIC (pfile
))
2032 validate_else (pfile
, "#else");
2033 skip_rest_of_line (pfile
);
2035 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
2036 cpp_error (pfile
, "`#else' not within a conditional");
2039 /* #ifndef can't have its special treatment for containing the whole file
2040 if it has a #else clause. */
2041 pfile
->if_stack
->control_macro
= 0;
2043 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
2044 cpp_error (pfile
, "`#else' after `#else'");
2045 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
2046 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
2047 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
2048 fprintf (stderr
, ")\n");
2050 pfile
->if_stack
->type
= T_ELSE
;
2053 if (pfile
->if_stack
->if_succeeded
)
2054 skip_if_group (pfile
);
2056 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
2057 output_line_command (pfile
, same_file
);
2063 * unstack after #endif command
2067 do_endif (pfile
, keyword
)
2069 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2071 if (CPP_PEDANTIC (pfile
))
2072 validate_else (pfile
, "#endif");
2073 skip_rest_of_line (pfile
);
2075 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2076 cpp_error (pfile
, "unbalanced `#endif'");
2079 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2080 pfile
->if_stack
= temp
->next
;
2081 if (temp
->control_macro
!= 0)
2083 /* This #endif matched a #ifndef at the start of the file.
2084 See if it is at the end of the file. */
2087 parse_set_mark (pfile
);
2091 cpp_skip_hspace (pfile
);
2096 parse_goto_mark (pfile
);
2100 /* This #endif ends a #ifndef
2101 that contains all of the file (aside from whitespace).
2102 Arrange not to include the file again
2103 if the macro that was tested is defined. */
2104 struct cpp_buffer
*ip
;
2105 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2106 if (ip
->fname
!= NULL
)
2108 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2112 output_line_command (pfile
, same_file
);
2117 /* When an #else or #endif is found while skipping failed conditional,
2118 if -pedantic was specified, this is called to warn about text after
2119 the command name. P points to the first char after the command name. */
2122 validate_else (pfile
, directive
)
2124 const char *directive
;
2127 cpp_skip_hspace (pfile
);
2129 if (c
!= EOF
&& c
!= '\n')
2131 "text following `%s' violates ANSI standard", directive
);
2134 /* Get the next token, and add it to the text in pfile->token_buffer.
2135 Return the kind of token we got. */
2138 cpp_get_token (pfile
)
2141 register int c
, c2
, c3
;
2142 enum cpp_token token
;
2143 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2150 if (CPP_BUFFER (pfile
)->manual_pop
)
2151 /* If we've been reading from redirected input, the
2152 frontend will pop the buffer. */
2154 else if (CPP_BUFFER (pfile
)->seen_eof
)
2156 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2159 cpp_pop_buffer (pfile
);
2164 cpp_buffer
*next_buf
2165 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2166 CPP_BUFFER (pfile
)->seen_eof
= 1;
2167 if (CPP_BUFFER (pfile
)->nominal_fname
2168 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2170 /* We're about to return from an #include file.
2171 Emit #line information now (as part of the CPP_POP) result.
2172 But the #line refers to the file we will pop to. */
2173 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2174 CPP_BUFFER (pfile
) = next_buf
;
2175 pfile
->input_stack_listing_current
= 0;
2176 output_line_command (pfile
, leave_file
);
2177 CPP_BUFFER (pfile
) = cur_buffer
;
2187 if (PEEKC () == '=')
2191 if (opts
->put_out_comments
)
2192 c
= copy_comment (pfile
, c
);
2194 c
= skip_comment (pfile
, c
);
2200 /* Comments are equivalent to spaces.
2201 For -traditional, a comment is equivalent to nothing. */
2202 if (opts
->traditional
|| opts
->put_out_comments
)
2206 CPP_PUTC (pfile
, c
);
2210 if (opts
->for_lint
) {
2213 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2215 if (lintcmd
!= NULL
) {
2216 /* I believe it is always safe to emit this newline: */
2218 bcopy ("#pragma lint ", (char *) obp
, 13);
2220 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2225 bcopy (argbp
, (char *) obp
, arglen
);
2229 /* OK, now bring us back to the state we were in before we entered
2230 this branch. We need #line because the newline for the pragma
2231 could mess things up. */
2232 output_line_command (pfile
, same_file
);
2233 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2241 /* If this is expanding a macro definition, don't recognize
2242 preprocessor directives. */
2245 /* If this is expand_into_temp_buffer, recognize them
2246 only after an actual newline at this level,
2247 not at the beginning of the input level. */
2248 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
2254 if (!pfile
->only_seen_white
)
2256 if (handle_directive (pfile
))
2257 return CPP_DIRECTIVE
;
2258 pfile
->only_seen_white
= 0;
2264 parse_string (pfile
, c
);
2265 pfile
->only_seen_white
= 0;
2266 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2269 if (!opts
->dollars_in_ident
)
2274 if (opts
->cplusplus
&& PEEKC () == ':')
2282 if (c2
== c
|| c2
== '=')
2291 if (PEEKC () == '=')
2297 if (c2
== '-' && opts
->chill
)
2298 goto comment
; /* Chill style comment */
2299 if (c2
== '-' || c2
== '=')
2303 if (opts
->cplusplus
&& PEEKN (1) == '*')
2305 /* In C++, there's a ->* operator. */
2307 pfile
->only_seen_white
= 0;
2308 CPP_RESERVE (pfile
, 4);
2309 CPP_PUTC_Q (pfile
, c
);
2310 CPP_PUTC_Q (pfile
, GETC ());
2311 CPP_PUTC_Q (pfile
, GETC ());
2312 CPP_NUL_TERMINATE_Q (pfile
);
2320 if (pfile
->parsing_include_directive
)
2324 CPP_PUTC (pfile
, c
);
2328 if (c
== '\n' || c
== EOF
)
2331 "missing '>' in `#include <FILENAME>'");
2336 if (!CPP_BUFFER (pfile
)->has_escapes
)
2338 /* Backslash newline is replaced by nothing. */
2339 CPP_ADJUST_WRITTEN (pfile
, -1);
2340 CPP_BUMP_LINE (pfile
);
2344 /* We might conceivably get \r- or \r<space> in
2345 here. Just delete 'em. */
2347 if (d
!= '-' && d
!= ' ')
2349 "internal error: unrecognized escape \\r%c",
2351 CPP_ADJUST_WRITTEN (pfile
, -1);
2357 /* else fall through */
2362 /* GNU C++ supports MIN and MAX operators <? and >?. */
2363 if (c2
!= c
&& (!opts
->cplusplus
|| c2
!= '?'))
2366 CPP_RESERVE (pfile
, 4);
2367 CPP_PUTC (pfile
, c
);
2368 CPP_PUTC (pfile
, c2
);
2371 CPP_PUTC_Q (pfile
, GETC ());
2372 CPP_NUL_TERMINATE_Q (pfile
);
2373 pfile
->only_seen_white
= 0;
2380 CPP_RESERVE(pfile
, 2);
2381 CPP_PUTC_Q (pfile
, '.');
2386 /* In C++ there's a .* operator. */
2387 if (opts
->cplusplus
&& c2
== '*')
2390 if (c2
== '.' && PEEKN(1) == '.')
2392 CPP_RESERVE(pfile
, 4);
2393 CPP_PUTC_Q (pfile
, '.');
2394 CPP_PUTC_Q (pfile
, '.');
2395 CPP_PUTC_Q (pfile
, '.');
2397 CPP_NUL_TERMINATE_Q (pfile
);
2398 pfile
->only_seen_white
= 0;
2405 pfile
->only_seen_white
= 0;
2406 CPP_RESERVE(pfile
, 3);
2407 CPP_PUTC_Q (pfile
, c
);
2408 CPP_PUTC_Q (pfile
, GETC ());
2409 CPP_NUL_TERMINATE_Q (pfile
);
2414 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2416 CPP_PUTC (pfile
, c
);
2422 case '0': case '1': case '2': case '3': case '4':
2423 case '5': case '6': case '7': case '8': case '9':
2428 CPP_RESERVE (pfile
, 2);
2429 CPP_PUTC_Q (pfile
, c
);
2433 if (!is_idchar
[c
] && c
!= '.'
2434 && ((c2
!= 'e' && c2
!= 'E'
2435 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2436 || (c
!= '+' && c
!= '-')))
2441 CPP_NUL_TERMINATE_Q (pfile
);
2442 pfile
->only_seen_white
= 0;
2444 case 'b': case 'c': case 'd': case 'h': case 'o':
2445 case 'B': case 'C': case 'D': case 'H': case 'O':
2446 if (opts
->chill
&& PEEKC () == '\'')
2448 pfile
->only_seen_white
= 0;
2449 CPP_RESERVE (pfile
, 2);
2450 CPP_PUTC_Q (pfile
, c
);
2451 CPP_PUTC_Q (pfile
, '\'');
2457 goto chill_number_eof
;
2460 CPP_PUTC (pfile
, c
);
2464 CPP_RESERVE (pfile
, 2);
2465 CPP_PUTC_Q (pfile
, c
);
2466 CPP_NUL_TERMINATE_Q (pfile
);
2473 CPP_NUL_TERMINATE (pfile
);
2480 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2481 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2482 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2483 case 'x': case 'y': case 'z':
2484 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2485 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2486 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2491 unsigned char *ident
;
2492 int before_name_written
= CPP_WRITTEN (pfile
);
2494 parse_name (pfile
, c
);
2495 pfile
->only_seen_white
= 0;
2496 if (pfile
->no_macro_expand
)
2498 ident
= pfile
->token_buffer
+ before_name_written
;
2499 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2500 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2503 if (hp
->type
== T_DISABLED
)
2505 if (pfile
->output_escapes
)
2506 { /* Return "\r-IDENT", followed by '\0'. */
2508 CPP_RESERVE (pfile
, 3);
2509 ident
= pfile
->token_buffer
+ before_name_written
;
2510 CPP_ADJUST_WRITTEN (pfile
, 2);
2511 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2518 /* If macro wants an arglist, verify that a '(' follows.
2519 first skip all whitespace, copying it to the output
2520 after the macro name. Then, if there is no '(',
2521 decide this is not a macro call and leave things that way. */
2522 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2524 int is_macro_call
, macbuf_whitespace
= 0;
2526 parse_set_mark (pfile
);
2529 cpp_skip_hspace (pfile
);
2531 is_macro_call
= c
== '(';
2540 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2542 if (CPP_BUFFER (pfile
)->mark
!=
2543 (CPP_BUFFER (pfile
)->cur
2544 - CPP_BUFFER (pfile
)->buf
))
2545 macbuf_whitespace
= 1;
2547 /* The mark goes away automatically when
2548 the buffer is popped. */
2549 cpp_pop_buffer (pfile
);
2550 parse_set_mark (pfile
);
2558 parse_goto_mark (pfile
);
2559 if (macbuf_whitespace
)
2560 CPP_PUTC (pfile
, ' ');
2563 parse_clear_mark (pfile
);
2567 /* This is now known to be a macro call.
2568 Expand the macro, reading arguments as needed,
2569 and push the expansion on the input stack. */
2570 macroexpand (pfile
, hp
);
2571 CPP_SET_WRITTEN (pfile
, before_name_written
);
2575 case ' ': case '\t': case '\v':
2578 CPP_PUTC (pfile
, c
);
2580 if (c
== EOF
|| !is_hor_space
[c
])
2587 if (CPP_BUFFER (pfile
)->has_escapes
)
2592 if (pfile
->output_escapes
)
2593 CPP_PUTS (pfile
, "\r-", 2);
2594 parse_name (pfile
, GETC ());
2599 CPP_RESERVE (pfile
, 2);
2600 if (pfile
->output_escapes
)
2601 CPP_PUTC_Q (pfile
, '\r');
2602 CPP_PUTC_Q (pfile
, c
);
2608 "internal error: unrecognized escape \\r%c", c
);
2614 /* Backslash newline is ignored. */
2615 CPP_BUMP_LINE (pfile
);
2620 CPP_PUTC (pfile
, c
);
2621 if (pfile
->only_seen_white
== 0)
2622 pfile
->only_seen_white
= 1;
2623 CPP_BUMP_LINE (pfile
);
2624 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2627 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2628 output_line_command (pfile
, same_file
);
2632 case '(': token
= CPP_LPAREN
; goto char1
;
2633 case ')': token
= CPP_RPAREN
; goto char1
;
2634 case '{': token
= CPP_LBRACE
; goto char1
;
2635 case '}': token
= CPP_RBRACE
; goto char1
;
2636 case ',': token
= CPP_COMMA
; goto char1
;
2637 case ';': token
= CPP_SEMICOLON
; goto char1
;
2643 pfile
->only_seen_white
= 0;
2644 CPP_PUTC (pfile
, c
);
2650 /* Like cpp_get_token, but skip spaces and comments. */
2653 cpp_get_non_space_token (pfile
)
2656 int old_written
= CPP_WRITTEN (pfile
);
2659 enum cpp_token token
= cpp_get_token (pfile
);
2660 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2661 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2663 CPP_SET_WRITTEN (pfile
, old_written
);
2667 /* Parse an identifier starting with C. */
2670 parse_name (pfile
, c
)
2682 if (c
== '$' && CPP_PEDANTIC (pfile
))
2683 cpp_pedwarn (pfile
, "`$' in identifier");
2685 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2686 CPP_PUTC_Q (pfile
, c
);
2691 CPP_NUL_TERMINATE_Q (pfile
);
2695 /* Parse a string starting with C. A single quoted string is treated
2696 like a double -- some programs (e.g., troff) are perverse this way.
2697 (However, a single quoted string is not allowed to extend over
2700 parse_string (pfile
, c
)
2704 long start_line
, start_column
;
2706 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2708 CPP_PUTC (pfile
, c
);
2714 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2716 /* try harder: this string crosses a macro expansion
2717 boundary. This can happen naturally if -traditional.
2718 Otherwise, only -D can make a macro with an unmatched
2720 cpp_pop_buffer (pfile
);
2723 if (!CPP_TRADITIONAL (pfile
))
2725 cpp_error_with_line (pfile
, start_line
, start_column
,
2726 "unterminated string or character constant");
2727 if (pfile
->multiline_string_line
!= start_line
2728 && pfile
->multiline_string_line
!= 0)
2729 cpp_error_with_line (pfile
,
2730 pfile
->multiline_string_line
, -1,
2731 "possible real start of unterminated constant");
2732 pfile
->multiline_string_line
= 0;
2736 CPP_PUTC (pfile
, cc
);
2740 CPP_BUMP_LINE (pfile
);
2742 /* Traditionally, end of line ends a string constant with
2744 if (CPP_TRADITIONAL (pfile
))
2746 /* Character constants may not extend over multiple lines. */
2749 cpp_error_with_line (pfile
, start_line
, start_column
,
2750 "unterminated character constant");
2753 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2755 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2756 "string constant runs past end of line");
2758 if (pfile
->multiline_string_line
== 0)
2759 pfile
->multiline_string_line
= start_line
;
2763 CPP_ADJUST_WRITTEN (pfile
, -1);
2764 if (CPP_BUFFER (pfile
)->has_escapes
)
2767 "internal error: \\r escape inside string constant");
2771 /* Backslash newline is replaced by nothing at all. */
2772 CPP_BUMP_LINE (pfile
);
2778 CPP_PUTC (pfile
, cc
);
2790 /* Read an assertion into the token buffer, converting to
2791 canonical form: `#predicate(a n swe r)' The next non-whitespace
2792 character to read should be the first letter of the predicate.
2793 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2794 with answer (see callers for why). In case of 0, an error has been
2797 parse_assertion (pfile
)
2801 cpp_skip_hspace (pfile
);
2803 if (! is_idstart
[c
])
2805 cpp_error (pfile
, "assertion predicate is not an identifier");
2808 CPP_PUTC(pfile
, '#');
2810 parse_name(pfile
, c
);
2815 if (is_hor_space
[c
] || c
== '\r')
2816 cpp_skip_hspace (pfile
);
2822 CPP_PUTC(pfile
, '(');
2825 while ((c
= GETC()) != ')')
2827 if (is_hor_space
[c
])
2831 CPP_PUTC(pfile
, ' ');
2835 else if (c
== '\n' || c
== EOF
)
2837 if (c
== '\n') FORWARD(-1);
2838 cpp_error (pfile
, "un-terminated assertion answer");
2842 /* \r cannot be a macro escape here. */
2843 CPP_BUMP_LINE (pfile
);
2846 CPP_PUTC (pfile
, c
);
2851 if (pfile
->limit
[-1] == ' ')
2852 pfile
->limit
[-1] = ')';
2853 else if (pfile
->limit
[-1] == '(')
2855 cpp_error (pfile
, "empty token sequence in assertion");
2859 CPP_PUTC (pfile
, ')');
2861 CPP_NUL_TERMINATE (pfile
);
2866 do_assert (pfile
, keyword
)
2868 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2872 HASHNODE
*base
, *this;
2873 int baselen
, thislen
;
2875 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2876 && !CPP_BUFFER (pfile
)->system_header_p
)
2877 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
2879 cpp_skip_hspace (pfile
);
2880 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2881 ret
= parse_assertion (pfile
);
2886 cpp_error (pfile
, "missing token-sequence in `#assert'");
2890 cpp_skip_hspace (pfile
);
2892 if (c
!= EOF
&& c
!= '\n')
2894 cpp_error (pfile
, "junk at end of `#assert'");
2898 thislen
= strlen (sym
);
2899 baselen
= index (sym
, '(') - sym
;
2900 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2903 cpp_warning (pfile
, "`%s' re-asserted", sym
);
2907 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
2909 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
2910 else if (base
->type
!= T_ASSERT
)
2912 /* Token clash - but with what?! */
2914 "cpp internal error: base->type != T_ASSERT in do_assert");
2918 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
2919 (char *)base
->value
.aschain
, -1);
2920 base
->value
.aschain
= this;
2922 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2926 skip_rest_of_line (pfile
);
2927 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2932 do_unassert (pfile
, keyword
)
2934 const struct directive
*keyword ATTRIBUTE_UNUSED
;
2938 long baselen
, thislen
;
2939 HASHNODE
*base
, *this, *next
;
2941 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2942 && !CPP_BUFFER (pfile
)->system_header_p
)
2943 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
2945 cpp_skip_hspace (pfile
);
2947 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2948 ret
= parse_assertion (pfile
);
2952 cpp_skip_hspace (pfile
);
2954 if (c
!= EOF
&& c
!= '\n')
2955 cpp_error (pfile
, "junk at end of `#unassert'");
2957 thislen
= strlen (sym
);
2960 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
2962 goto error
; /* It isn't an error to #undef what isn't #defined,
2963 so it isn't an error to #unassert what isn't
2964 #asserted either. */
2966 for (this = base
->value
.aschain
; this; this = next
)
2968 next
= this->value
.aschain
;
2969 delete_macro (this);
2971 delete_macro (base
);
2975 baselen
= index (sym
, '(') - sym
;
2976 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
2977 if (! base
) goto error
;
2978 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2979 if (! this) goto error
;
2982 while (next
->value
.aschain
!= this)
2983 next
= next
->value
.aschain
;
2985 next
->value
.aschain
= this->value
.aschain
;
2986 delete_macro (this);
2988 if (base
->value
.aschain
== NULL
)
2989 delete_macro (base
); /* Last answer for this predicate deleted. */
2992 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2995 skip_rest_of_line (pfile
);
2996 pfile
->limit
= (unsigned char *) sym
; /* Pop */
3000 /* Process STR as if it appeared as the body of an #unassert. */
3002 cpp_unassert (pfile
, str
)
3006 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
3008 do_assert (pfile
, NULL
);
3009 cpp_pop_buffer (pfile
);
3014 cpp_read_check_assertion (pfile
)
3017 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
3021 FORWARD (1); /* Skip '#' */
3022 cpp_skip_hspace (pfile
);
3023 if (! parse_assertion (pfile
))
3027 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
3031 pfile
->limit
= name
;
3035 /* Remember the current position of PFILE. */
3038 parse_set_mark (pfile
)
3041 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3044 "cpp internal error: ip->mark != -1 in parse_set_mark");
3046 ip
->mark
= ip
->cur
- ip
->buf
;
3049 /* Clear the current mark - we no longer need it. */
3052 parse_clear_mark (pfile
)
3055 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3058 "cpp internal error: ip->mark == -1 in parse_clear_mark");
3063 /* Backup the current position of PFILE to that saved in its mark,
3064 and clear the mark. */
3067 parse_goto_mark (pfile
)
3070 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3073 "cpp internal error: ip->mark == -1 in parse_goto_mark");
3075 ip
->cur
= ip
->buf
+ ip
->mark
;
3080 cpp_print_file_and_line (pfile
)
3083 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3088 cpp_buf_line_and_col (ip
, &line
, &col
);
3089 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
3090 line
, pfile
->show_column
? col
: -1);
3095 v_cpp_error (pfile
, msgid
, ap
)
3100 cpp_print_containing_files (pfile
);
3101 cpp_print_file_and_line (pfile
);
3102 v_cpp_message (pfile
, 1, msgid
, ap
);
3106 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3108 #ifndef ANSI_PROTOTYPES
3114 VA_START(ap
, msgid
);
3116 #ifndef ANSI_PROTOTYPES
3117 pfile
= va_arg (ap
, cpp_reader
*);
3118 msgid
= va_arg (ap
, const char *);
3121 v_cpp_error (pfile
, msgid
, ap
);
3125 /* Print error message but don't count it. */
3128 v_cpp_warning (pfile
, msgid
, ap
)
3133 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3136 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3139 cpp_print_containing_files (pfile
);
3140 cpp_print_file_and_line (pfile
);
3141 v_cpp_message (pfile
, 0, msgid
, ap
);
3145 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3147 #ifndef ANSI_PROTOTYPES
3153 VA_START (ap
, msgid
);
3155 #ifndef ANSI_PROTOTYPES
3156 pfile
= va_arg (ap
, cpp_reader
*);
3157 msgid
= va_arg (ap
, const char *);
3160 v_cpp_warning (pfile
, msgid
, ap
);
3164 /* Print an error message and maybe count it. */
3167 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3169 #ifndef ANSI_PROTOTYPES
3175 VA_START (ap
, msgid
);
3177 #ifndef ANSI_PROTOTYPES
3178 pfile
= va_arg (ap
, cpp_reader
*);
3179 msgid
= va_arg (ap
, const char *);
3182 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3183 v_cpp_error (pfile
, msgid
, ap
);
3185 v_cpp_warning (pfile
, msgid
, ap
);
3190 v_cpp_error_with_line (pfile
, line
, column
, msgid
, ap
)
3197 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3199 cpp_print_containing_files (pfile
);
3202 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3204 v_cpp_message (pfile
, 1, msgid
, ap
);
3208 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3209 const char *msgid
, ...))
3211 #ifndef ANSI_PROTOTYPES
3219 VA_START (ap
, msgid
);
3221 #ifndef ANSI_PROTOTYPES
3222 pfile
= va_arg (ap
, cpp_reader
*);
3223 line
= va_arg (ap
, int);
3224 column
= va_arg (ap
, int);
3225 msgid
= va_arg (ap
, const char *);
3228 v_cpp_error_with_line(pfile
, line
, column
, msgid
, ap
);
3233 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
)
3242 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3245 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3248 cpp_print_containing_files (pfile
);
3250 ip
= cpp_file_buffer (pfile
);
3253 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3255 v_cpp_message (pfile
, 0, msgid
, ap
);
3259 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3260 const char *msgid
, ...))
3262 #ifndef ANSI_PROTOTYPES
3270 VA_START (ap
, msgid
);
3272 #ifndef ANSI_PROTOTYPES
3273 pfile
= va_arg (ap
, cpp_reader
*);
3274 line
= va_arg (ap
, int);
3275 column
= va_arg (ap
, int);
3276 msgid
= va_arg (ap
, const char *);
3279 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3284 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3285 const char *msgid
, ...))
3287 #ifndef ANSI_PROTOTYPES
3295 VA_START (ap
, msgid
);
3297 #ifndef ANSI_PROTOTYPES
3298 pfile
= va_arg (ap
, cpp_reader
*);
3299 line
= va_arg (ap
, int);
3300 column
= va_arg (ap
, int);
3301 msgid
= va_arg (ap
, const char *);
3304 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3305 v_cpp_error_with_line (pfile
, column
, line
, msgid
, ap
);
3307 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3311 /* Report a warning (or an error if pedantic_errors)
3312 giving specified file name and line number, not current. */
3315 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, char *file
, int line
,
3316 const char *msgid
, ...))
3318 #ifndef ANSI_PROTOTYPES
3326 VA_START (ap
, msgid
);
3328 #ifndef ANSI_PROTOTYPES
3329 pfile
= va_arg (ap
, cpp_reader
*);
3330 file
= va_arg (ap
, char *);
3331 line
= va_arg (ap
, int);
3332 msgid
= va_arg (ap
, const char *);
3335 if (!CPP_OPTIONS (pfile
)->pedantic_errors
3336 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
3339 cpp_file_line_for_message (pfile
, file
, line
, -1);
3340 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msgid
, ap
);
3344 /* my_strerror - return the descriptive text associated with an
3348 my_strerror (errnum
)
3354 #ifndef HAVE_STRERROR
3355 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
3357 result
= strerror (errnum
);
3360 /* VAXCRTL's strerror() takes an optional second argument, which only
3361 matters when the first argument is EVMSERR. However, it's simplest
3362 just to pass it unconditionally. `vaxc$errno' is declared in
3363 <errno.h>, and maintained by the library in parallel with `errno'.
3364 We assume that caller's `errnum' either matches the last setting of
3365 `errno' by the library or else does not have the value `EVMSERR'. */
3367 result
= strerror (errnum
, vaxc$errno
);
3371 result
= "errno = ?";
3376 /* Error including a message from `errno'. */
3379 cpp_error_from_errno (pfile
, name
)
3383 cpp_message_from_errno (pfile
, 1, name
);
3387 cpp_message_from_errno (pfile
, is_error
, name
)
3393 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3395 cpp_print_containing_files (pfile
);
3398 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
3400 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
3404 cpp_perror_with_name (pfile
, name
)
3408 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
3412 * No pre-compiled header file support.
3414 * Possibly different enum token codes for each C/C++ token.
3416 * Find and cleanup remaining uses of static variables,
3418 * Support -dM flag (dump_all_macros).
3420 * Support for_lint flag.