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
*, 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
*, struct directive
*));
63 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
*, struct directive
*));
72 static int do_line
PARAMS ((cpp_reader
*, struct directive
*));
73 static int do_include
PARAMS ((cpp_reader
*, struct directive
*));
74 static int do_undef
PARAMS ((cpp_reader
*, struct directive
*));
75 static int do_error
PARAMS ((cpp_reader
*, struct directive
*));
76 static int do_pragma
PARAMS ((cpp_reader
*, struct directive
*));
77 static int do_ident
PARAMS ((cpp_reader
*, struct directive
*));
78 static int do_if
PARAMS ((cpp_reader
*, struct directive
*));
79 static int do_xifdef
PARAMS ((cpp_reader
*, struct directive
*));
80 static int do_else
PARAMS ((cpp_reader
*, struct directive
*));
81 static int do_elif
PARAMS ((cpp_reader
*, struct directive
*));
82 static int do_endif
PARAMS ((cpp_reader
*, struct directive
*));
84 static int do_sccs
PARAMS ((cpp_reader
*, struct directive
*));
86 static int do_assert
PARAMS ((cpp_reader
*, struct directive
*));
87 static int do_unassert
PARAMS ((cpp_reader
*, struct directive
*));
88 static int do_warning
PARAMS ((cpp_reader
*, struct directive
*));
90 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
91 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
93 /* Here is the actual list of #-directives, most-often-used first.
94 The initialize_builtins function assumes #define is the very first. */
96 static struct directive directive_table
[] = {
97 { 6, do_define
, "define", T_DEFINE
},
98 { 5, do_xifdef
, "ifdef", T_IFDEF
},
99 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
100 { 7, do_include
, "include", T_INCLUDE
},
101 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
102 { 6, do_include
, "import", T_IMPORT
},
103 { 5, do_endif
, "endif", T_ENDIF
},
104 { 4, do_else
, "else", T_ELSE
},
105 { 2, do_if
, "if", T_IF
},
106 { 4, do_elif
, "elif", T_ELIF
},
107 { 5, do_undef
, "undef", T_UNDEF
},
108 { 5, do_error
, "error", T_ERROR
},
109 { 7, do_warning
, "warning", T_WARNING
},
110 { 6, do_pragma
, "pragma", T_PRAGMA
},
111 { 4, do_line
, "line", T_LINE
},
112 { 5, do_ident
, "ident", T_IDENT
},
113 #ifdef SCCS_DIRECTIVE
114 { 4, do_sccs
, "sccs", T_SCCS
},
116 { 6, do_assert
, "assert", T_ASSERT
},
117 { 8, do_unassert
, "unassert", T_UNASSERT
},
118 { -1, 0, "", T_UNUSED
}
121 /* Place into PFILE a quoted string representing the string SRC.
122 Caller must reserve enough space in pfile->token_buffer. */
125 quote_string (pfile
, src
)
131 CPP_PUTC_Q (pfile
, '\"');
133 switch ((c
= *src
++))
137 CPP_PUTC_Q (pfile
, c
);
140 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
141 CPP_ADJUST_WRITTEN (pfile
, 4);
147 CPP_PUTC_Q (pfile
, '\\');
148 CPP_PUTC_Q (pfile
, c
);
152 CPP_PUTC_Q (pfile
, '\"');
153 CPP_NUL_TERMINATE_Q (pfile
);
158 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
161 cpp_grow_buffer (pfile
, n
)
165 long old_written
= CPP_WRITTEN (pfile
);
166 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
167 pfile
->token_buffer
= (U_CHAR
*)
168 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
169 CPP_SET_WRITTEN (pfile
, old_written
);
172 /* Process the string STR as if it appeared as the body of a #define
173 If STR is just an identifier, define it with value 1.
174 If STR has anything after the identifier, then it should
175 be identifier=definition. */
178 cpp_define (pfile
, str
)
185 /* Copy the entire option so we can modify it. */
186 count
= strlen (str
) + 3;
187 buf
= (U_CHAR
*) alloca (count
);
188 memcpy (buf
, str
, count
- 2);
189 /* Change the first "=" in the string to a space. If there is none,
190 tack " 1" on the end. */
191 p
= (U_CHAR
*) strchr (buf
, '=');
198 strcpy (&buf
[count
-3], " 1");
200 if (cpp_push_buffer (pfile
, buf
, count
- 1) != NULL
)
202 do_define (pfile
, NULL
);
203 cpp_pop_buffer (pfile
);
207 /* Process the string STR as if it appeared as the body of a #assert. */
209 cpp_assert (pfile
, str
)
213 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
215 do_assert (pfile
, NULL
);
216 cpp_pop_buffer (pfile
);
221 static enum cpp_token
222 null_underflow (pfile
)
223 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
229 null_cleanup (pbuf
, pfile
)
230 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
231 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
236 /* Skip a comment - C, C++, or Chill style. M is the first character
237 of the comment marker. If this really is a comment, skip to its
238 end and return ' '. If we hit end-of-file before end-of-comment,
239 return EOF. If this is not a comment, return M (which will be
243 skip_comment (pfile
, m
)
247 if (m
== '/' && PEEKC() == '*')
253 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
259 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
262 else if (c
== '\n' || c
== '\r')
263 /* \r cannot be a macro escape marker here. */
264 CPP_BUMP_LINE (pfile
);
265 else if (c
== '/' && prev_c
== '*')
267 else if (c
== '*' && prev_c
== '/'
268 && CPP_OPTIONS (pfile
)->warn_comments
)
269 cpp_warning (pfile
, "`/*' within comment");
274 else if ((m
== '/' && PEEKC() == '/'
275 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
276 || (m
== '-' && PEEKC() == '-'
277 && CPP_OPTIONS (pfile
)->chill
))
284 return ' '; /* Allow // to be terminated by EOF. */
287 /* Don't consider final '\n' to be part of comment. */
292 /* \r cannot be a macro escape marker here. */
293 CPP_BUMP_LINE (pfile
);
300 /* Identical to skip_comment except that it copies the comment into the
301 token_buffer. This is used if put_out_comments. */
303 copy_comment (pfile
, m
)
307 if (m
== '/' && PEEKC() == '*')
312 CPP_PUTC (pfile
, '/');
313 CPP_PUTC (pfile
, '*');
315 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
321 cpp_error_with_line (pfile
, line
, col
, "unterminated comment");
322 /* We must pretend this was a legitimate comment, so that the
323 output in token_buffer is not passed back tagged CPP_POP. */
328 /* \r cannot be a macro escape marker here. */
329 CPP_BUMP_LINE (pfile
);
337 CPP_BUMP_LINE (pfile
);
339 else if (c
== '/' && prev_c
== '*')
341 else if (c
== '*' && prev_c
== '/'
342 && CPP_OPTIONS (pfile
)->warn_comments
)
343 cpp_warning (pfile
, "`/*' within comment");
348 else if ((m
== '/' && PEEKC() == '/'
349 && CPP_OPTIONS (pfile
)->cplusplus_comments
)
350 || (m
== '-' && PEEKC() == '-'
351 && CPP_OPTIONS (pfile
)->chill
))
360 return ' '; /* Allow line comments to be terminated by EOF. */
363 /* Don't consider final '\n' to be part of comment. */
368 /* \r cannot be a macro escape marker here. */
369 CPP_BUMP_LINE (pfile
);
379 /* Skip whitespace \-newline and comments. Does not macro-expand. */
382 cpp_skip_hspace (pfile
)
391 else if (is_hor_space
[c
])
393 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
394 cpp_pedwarn (pfile
, "%s in preprocessing directive",
395 c
== '\f' ? "formfeed" : "vertical tab");
399 /* \r is a backslash-newline marker if !has_escapes, and
400 a deletable-whitespace or no-reexpansion marker otherwise. */
401 if (CPP_BUFFER (pfile
)->has_escapes
)
409 CPP_BUFFER (pfile
)->lineno
++;
411 else if (c
== '/' || c
== '-')
413 c
= skip_comment (pfile
, c
);
425 /* Read the rest of the current line.
426 The line is appended to PFILE's output buffer. */
429 copy_rest_of_line (pfile
)
440 CPP_NUL_TERMINATE (pfile
);
444 if (CPP_BUFFER (pfile
)->has_escapes
)
448 CPP_BUFFER (pfile
)->lineno
++;
453 parse_string (pfile
, c
);
456 if (PEEKC() == '*' && CPP_TRADITIONAL (pfile
))
458 CPP_PUTS (pfile
, "/**/", 4);
459 skip_comment (pfile
, c
);
462 /* else fall through */
464 c
= skip_comment (pfile
, c
);
469 if (CPP_PEDANTIC (pfile
))
470 cpp_pedwarn (pfile
, "%s in preprocessing directive",
471 c
== '\f' ? "formfeed" : "vertical tab");
479 /* FIXME: It is almost definitely a performance win to make this do
480 the scan itself. >75% of calls to copy_r_o_l are from here or
481 skip_if_group, which means the common case is to copy stuff into the
482 token_buffer only to discard it. */
484 skip_rest_of_line (pfile
)
487 long old
= CPP_WRITTEN (pfile
);
488 copy_rest_of_line (pfile
);
489 CPP_SET_WRITTEN (pfile
, old
);
492 /* Handle a possible # directive.
493 '#' has already been read. */
496 handle_directive (pfile
)
500 register struct directive
*kt
;
503 long old_written
= CPP_WRITTEN (pfile
);
505 cpp_skip_hspace (pfile
);
508 if (c
>= '0' && c
<= '9')
510 /* Handle # followed by a line number. */
511 if (CPP_PEDANTIC (pfile
))
512 cpp_pedwarn (pfile
, "`#' followed by integer");
513 do_line (pfile
, NULL
);
514 goto done_a_directive
;
517 /* Now find the directive name. */
518 CPP_PUTC (pfile
, '#');
519 parse_name (pfile
, GETC());
520 ident
= pfile
->token_buffer
+ old_written
+ 1;
521 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
522 if (ident_length
== 0 && PEEKC() == '\n')
524 /* A line of just `#' becomes blank. */
525 goto done_a_directive
;
529 if (ident_length
== 0 || !is_idstart
[*ident
]) {
531 while (is_idchar
[*p
]) {
532 if (*p
< '0' || *p
> '9')
536 /* Avoid error for `###' and similar cases unless -pedantic. */
538 while (*p
== '#' || is_hor_space
[*p
]) p
++;
540 if (pedantic
&& !lang_asm
)
541 cpp_warning (pfile
, "invalid preprocessor directive");
547 cpp_error (pfile
, "invalid preprocessor directive name");
553 * Decode the keyword and call the appropriate expansion
554 * routine, after moving the input pointer up to the next line.
556 for (kt
= directive_table
; ; kt
++) {
558 goto not_a_directive
;
559 if (kt
->length
== ident_length
560 && !strncmp (kt
->name
, ident
, ident_length
))
564 /* We may want to pass through #define, #undef, #pragma, and #include.
565 Other directives may create output, but we don't want the directive
566 itself out, so we pop it now. For example conditionals may emit
567 #failed ... #endfailed stuff. */
569 if (! (kt
->type
== T_DEFINE
570 || kt
->type
== T_PRAGMA
571 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
572 && CPP_OPTIONS (pfile
)->dump_includes
)))
573 CPP_SET_WRITTEN (pfile
, old_written
);
575 (*kt
->func
) (pfile
, kt
);
577 if (kt
->type
== T_DEFINE
)
579 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
581 /* Skip "#define". */
582 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
584 SKIP_WHITE_SPACE (p
);
585 while (is_idchar
[*p
]) p
++;
587 CPP_PUTC (pfile
, '\n');
589 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
590 CPP_SET_WRITTEN (pfile
, old_written
);
600 /* Pass a directive through to the output file.
601 BUF points to the contents of the directive, as a contiguous string.
602 m LIMIT points to the first character past the end of the directive.
603 KEYWORD is the keyword-table entry for the directive. */
606 pass_thru_directive (buf
, limit
, pfile
, keyword
)
609 struct directive
*keyword
;
611 register unsigned keyword_length
= keyword
->length
;
613 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
614 CPP_PUTC_Q (pfile
, '#');
615 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
616 if (limit
!= buf
&& buf
[0] != ' ')
617 CPP_PUTC_Q (pfile
, ' ');
618 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
620 CPP_PUTS_Q (pfile
, '\n');
621 /* Count the line we have just made in the output,
622 to get in sync properly. */
627 /* Check a purported macro name SYMNAME, and yield its length.
628 ASSERTION is nonzero if this is really for an assertion name. */
631 check_macro_name (pfile
, symname
, assertion
)
639 for (p
= symname
; is_idchar
[*p
]; p
++)
641 sym_length
= p
- symname
;
643 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
645 assertion
? "invalid assertion name" : "invalid macro name");
646 else if (!is_idstart
[*symname
]
647 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
648 U_CHAR
*msg
; /* what pain... */
649 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
650 bcopy (symname
, msg
, sym_length
);
654 ? "invalid assertion name `%s'"
655 : "invalid macro name `%s'"),
662 /* Process a #define command.
663 KEYWORD is the keyword-table entry for #define,
664 or NULL for a "predefined" macro. */
667 do_define (pfile
, keyword
)
669 struct directive
*keyword
;
675 U_CHAR
*macro
, *buf
, *end
;
677 here
= CPP_WRITTEN (pfile
);
678 copy_rest_of_line (pfile
);
680 /* Copy out the line so we can pop the token buffer. */
681 buf
= pfile
->token_buffer
+ here
;
682 end
= CPP_PWRITTEN (pfile
);
683 macro
= alloca (end
- buf
+ 1);
684 bcopy (buf
, macro
, end
- buf
+ 1);
685 end
= macro
+ (end
- buf
);
687 CPP_SET_WRITTEN (pfile
, here
);
690 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
691 if (pcp_outfile
&& keyword
)
692 pass_thru_directive (macro
, end
, pfile
, keyword
);
695 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
699 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
701 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
704 /* Redefining a precompiled key is ok. */
705 if (hp
->type
== T_PCSTRING
)
707 /* Redefining a macro is ok if the definitions are the same. */
708 else if (hp
->type
== T_MACRO
)
709 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
710 /* Redefining a constant is ok with -D. */
711 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
712 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
713 /* Print the warning if it's not ok. */
716 /* If we are passing through #define and #undef directives, do
717 that for this re-definition now. */
718 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
719 pass_thru_directive (macro
, end
, pfile
, keyword
);
721 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
722 if (hp
->type
== T_MACRO
)
723 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
724 "this is the location of the previous definition");
726 /* Replace the old definition. */
728 hp
->value
.defn
= mdef
.defn
;
732 /* If we are passing through #define and #undef directives, do
733 that for this new definition now. */
734 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
735 pass_thru_directive (macro
, end
, pfile
, keyword
);
736 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, T_MACRO
,
737 (char *) mdef
.defn
, hashcode
);
748 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
749 If BUFFER != NULL, then use the LENGTH characters in BUFFER
750 as the new input buffer.
751 Return the new buffer, or NULL on failure. */
754 cpp_push_buffer (pfile
, buffer
, length
)
759 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
761 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
763 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
767 new = (cpp_buffer
*) xcalloc (sizeof (cpp_buffer
), 1);
769 new->if_stack
= pfile
->if_stack
;
770 new->cleanup
= null_cleanup
;
771 new->underflow
= null_underflow
;
772 new->buf
= new->cur
= buffer
;
773 new->alimit
= new->rlimit
= buffer
+ length
;
777 CPP_BUFFER (pfile
) = new;
782 cpp_pop_buffer (pfile
)
785 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
786 (*buf
->cleanup
) (buf
, pfile
);
787 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
789 pfile
->buffer_stack_depth
--;
790 return CPP_BUFFER (pfile
);
793 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
794 Pop the buffer when done. */
797 cpp_scan_buffer (pfile
)
800 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
803 enum cpp_token token
= cpp_get_token (pfile
);
804 if (token
== CPP_EOF
) /* Should not happen ... */
806 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
808 cpp_pop_buffer (pfile
);
815 * Rescan a string (which may have escape marks) into pfile's buffer.
816 * Place the result in pfile->token_buffer.
818 * The input is copied before it is scanned, so it is safe to pass
819 * it something from the token_buffer that will get overwritten
820 * (because it follows CPP_WRITTEN). This is used by do_include.
824 cpp_expand_to_buffer (pfile
, buf
, length
)
829 register cpp_buffer
*ip
;
835 int odepth
= indepth
;
841 /* Set up the input on the input stack. */
843 buf1
= (U_CHAR
*) alloca (length
+ 1);
844 memcpy (buf1
, buf
, length
);
847 ip
= cpp_push_buffer (pfile
, buf1
, length
);
852 ip
->lineno
= obuf
.lineno
= 1;
855 /* Scan the input, create the output. */
856 cpp_scan_buffer (pfile
);
858 CPP_NUL_TERMINATE (pfile
);
862 cpp_buf_line_and_col (pbuf
, linep
, colp
)
863 register cpp_buffer
*pbuf
;
868 *linep
= pbuf
->lineno
;
870 *colp
= pbuf
->cur
- pbuf
->line_base
;
880 /* Return the cpp_buffer that corresponds to a file (not a macro). */
883 cpp_file_buffer (pfile
)
886 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
888 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
889 if (ip
->fname
!= NULL
)
895 * write out a #line command, for instance, after an #include file.
896 * If CONDITIONAL is nonzero, we can omit the #line if it would
897 * appear to be a no-op, and we can output a few newlines instead
898 * if we want to increase the line number by a small amount.
899 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
903 output_line_command (pfile
, conditional
, file_change
)
906 enum file_change_code file_change
;
909 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
911 if (ip
->fname
== NULL
)
914 if (CPP_OPTIONS (pfile
)->no_line_commands
915 || CPP_OPTIONS (pfile
)->no_output
)
918 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
922 if (line
== pfile
->lineno
)
925 /* If the inherited line number is a little too small,
926 output some newlines instead of a #line command. */
927 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
929 CPP_RESERVE (pfile
, 20);
930 while (line
> pfile
->lineno
)
932 CPP_PUTC_Q (pfile
, '\n');
939 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
940 CPP_PUTS_Q (pfile
, "# ", 2);
942 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
943 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
945 quote_string (pfile
, ip
->nominal_fname
);
946 if (file_change
!= same_file
)
948 CPP_PUTC_Q (pfile
, ' ');
949 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
951 /* Tell cc1 if following text comes from a system header file. */
952 if (ip
->system_header_p
)
954 CPP_PUTC_Q (pfile
, ' ');
955 CPP_PUTC_Q (pfile
, '3');
957 #ifndef NO_IMPLICIT_EXTERN_C
958 /* Tell cc1plus if following text should be treated as C. */
959 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
961 CPP_PUTC_Q (pfile
, ' ');
962 CPP_PUTC_Q (pfile
, '4');
965 CPP_PUTC_Q (pfile
, '\n');
966 pfile
->lineno
= line
;
970 /* Like cpp_get_token, except that it does not read past end-of-line.
971 Also, horizontal space is skipped, and macros are popped. */
973 static enum cpp_token
974 get_directive_token (pfile
)
979 long old_written
= CPP_WRITTEN (pfile
);
980 enum cpp_token token
;
981 cpp_skip_hspace (pfile
);
982 if (PEEKC () == '\n')
984 token
= cpp_get_token (pfile
);
988 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
990 /* ... else fall though ... */
991 case CPP_HSPACE
: case CPP_COMMENT
:
992 CPP_SET_WRITTEN (pfile
, old_written
);
1000 /* Handle #include and #import.
1001 This function expects to see "fname" or <fname> on the input.
1003 The input is normally in part of the output_buffer following
1004 CPP_WRITTEN, and will get overwritten by output_line_command.
1005 I.e. in input file specification has been popped by handle_directive.
1009 do_include (pfile
, keyword
)
1011 struct directive
*keyword
;
1013 int importing
= (keyword
->type
== T_IMPORT
);
1014 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
1015 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
1016 int before
; /* included before? */
1018 unsigned char *fbeg
, *fend
;
1021 enum cpp_token token
;
1023 /* Chain of dirs to search */
1024 struct include_hash
*ihash
;
1025 struct file_name_list
*search_start
;
1027 long old_written
= CPP_WRITTEN (pfile
);
1031 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1034 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
1036 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
1039 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
1040 && !CPP_OPTIONS (pfile
)->inhibit_warnings
1041 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
1043 pfile
->import_warning
= 1;
1044 cpp_warning (pfile
, "`#import' is obsolete, use an #ifndef wrapper in the header file");
1047 pfile
->parsing_include_directive
++;
1048 token
= get_directive_token (pfile
);
1049 pfile
->parsing_include_directive
--;
1051 if (token
== CPP_STRING
)
1053 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
1054 fend
= CPP_PWRITTEN (pfile
) - 1;
1056 if (fbeg
[-1] == '<')
1060 else if (token
== CPP_NAME
)
1062 /* Support '#include xyz' like VAX-C to allow for easy use of
1063 * all the decwindow include files. It defaults to '#include
1064 * <xyz.h>' and generates a warning. */
1066 "VAX-C-style include specification found, use '#include <filename.h>' !");
1069 /* Append the missing `.h' to the name. */
1070 CPP_PUTS (pfile
, ".h", 3)
1071 CPP_NUL_TERMINATE_Q (pfile
);
1073 fbeg
= pfile
->token_buffer
+ old_written
;
1074 fend
= CPP_PWRITTEN (pfile
);
1080 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1081 CPP_SET_WRITTEN (pfile
, old_written
);
1082 skip_rest_of_line (pfile
);
1086 token
= get_directive_token (pfile
);
1087 if (token
!= CPP_VSPACE
)
1089 cpp_error (pfile
, "junk at end of `#include'");
1090 skip_rest_of_line (pfile
);
1093 CPP_SET_WRITTEN (pfile
, old_written
);
1099 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1105 for (fp
= CPP_BUFFER (pfile
);
1106 fp
!= CPP_NULL_BUFFER (pfile
);
1107 fp
= CPP_PREV_BUFFER (fp
))
1108 if (fp
->fname
!= NULL
)
1111 if (fp
== CPP_NULL_BUFFER (pfile
))
1113 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
1117 /* For #include_next, skip in the search path past the dir in which the
1118 containing file was found. Treat files specified using an absolute path
1119 as if there are no more directories to search. Treat the primary source
1120 file like any other included source, but generate a warning. */
1121 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1123 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1124 search_start
= fp
->ihash
->foundhere
->next
;
1129 cpp_warning (pfile
, "#include_next in primary source file");
1132 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1135 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1138 search_start
= fp
->actual_dir
;
1141 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1147 cpp_error (pfile
, "No include path in which to find %s", fbeg
);
1151 fd
= find_include_file (pfile
, fbeg
, search_start
, &ihash
, &before
);
1158 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1159 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1160 (pfile
->system_include_depth
> 0)))
1162 if (!angle_brackets
)
1163 deps_output (pfile
, fbeg
, ' ');
1167 struct file_name_list
*ptr
;
1168 /* If requested as a system header, assume it belongs in
1169 the first system header directory. */
1170 if (CPP_OPTIONS (pfile
)->bracket_include
)
1171 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1173 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1175 p
= (char *) alloca (strlen (ptr
->name
)
1176 + strlen (fbeg
) + 2);
1177 if (*ptr
->name
!= '\0')
1179 strcpy (p
, ptr
->name
);
1183 deps_output (pfile
, p
, ' ');
1186 /* If -M was specified, and this header file won't be added to
1187 the dependency list, then don't count this as an error,
1188 because we can still produce correct output. Otherwise, we
1189 can't produce correct output, because there may be
1190 dependencies we need inside the missing file, and we don't
1191 know what directory this missing file exists in. */
1192 else if (CPP_PRINT_DEPS (pfile
)
1193 && (CPP_PRINT_DEPS (pfile
)
1194 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1195 cpp_warning (pfile
, "No include path in which to find %s", fbeg
);
1197 cpp_error_from_errno (pfile
, fbeg
);
1202 /* For -M, add the file to the dependencies on its first inclusion. */
1203 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1204 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1205 deps_output (pfile
, ihash
->name
, ' ');
1207 /* Handle -H option. */
1208 if (CPP_OPTIONS(pfile
)->print_include_names
)
1210 fp
= CPP_BUFFER (pfile
);
1211 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1213 fprintf (stderr
, " %s\n", ihash
->name
);
1216 /* Actually process the file */
1219 ihash
->control_macro
= "";
1221 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1228 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1230 if (finclude (pfile
, fd
, ihash
))
1232 output_line_command (pfile
, 0, enter_file
);
1233 pfile
->only_seen_white
= 2;
1239 /* Interpret #line command.
1240 Note that the filename string (if any) is treated as if it were an
1241 include filename. That means no escape handling. */
1244 do_line (pfile
, keyword
)
1246 struct directive
*keyword ATTRIBUTE_UNUSED
;
1248 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1250 long old_written
= CPP_WRITTEN (pfile
);
1251 enum file_change_code file_change
= same_file
;
1252 enum cpp_token token
;
1255 token
= get_directive_token (pfile
);
1257 if (token
!= CPP_NUMBER
)
1259 cpp_error (pfile
, "token after `#line' is not an integer");
1260 goto bad_line_directive
;
1263 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1266 cpp_error (pfile
, "token after `#line' is not an integer");
1267 goto bad_line_directive
;
1269 CPP_SET_WRITTEN (pfile
, old_written
);
1271 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1272 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1274 token
= get_directive_token (pfile
);
1276 if (token
== CPP_STRING
)
1278 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1279 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1280 long num_start
= CPP_WRITTEN (pfile
);
1282 token
= get_directive_token (pfile
);
1283 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1285 U_CHAR
*p
= pfile
->token_buffer
+ num_start
;
1286 if (CPP_PEDANTIC (pfile
))
1287 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1289 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
1291 cpp_error (pfile
, "invalid format `#line' command");
1292 goto bad_line_directive
;
1295 file_change
= enter_file
;
1297 file_change
= leave_file
;
1299 ip
->system_header_p
= 1;
1300 else /* if (*p == '4') */
1301 ip
->system_header_p
= 2;
1303 CPP_SET_WRITTEN (pfile
, num_start
);
1304 token
= get_directive_token (pfile
);
1305 p
= pfile
->token_buffer
+ num_start
;
1306 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4'))
1308 ip
->system_header_p
= *p
== '3' ? 1 : 2;
1309 token
= get_directive_token (pfile
);
1311 if (token
!= CPP_VSPACE
)
1313 cpp_error (pfile
, "invalid format `#line' command");
1314 goto bad_line_directive
;
1320 if (strcmp (fname
, ip
->nominal_fname
))
1322 char *newname
, *oldname
;
1323 if (!strcmp (fname
, ip
->fname
))
1324 newname
= ip
->fname
;
1325 else if (ip
->last_nominal_fname
1326 && !strcmp (fname
, ip
->last_nominal_fname
))
1327 newname
= ip
->last_nominal_fname
;
1329 newname
= xstrdup (fname
);
1331 oldname
= ip
->nominal_fname
;
1332 ip
->nominal_fname
= newname
;
1334 if (ip
->last_nominal_fname
1335 && ip
->last_nominal_fname
!= oldname
1336 && ip
->last_nominal_fname
!= newname
1337 && ip
->last_nominal_fname
!= ip
->fname
)
1338 free (ip
->last_nominal_fname
);
1340 if (newname
== ip
->fname
)
1341 ip
->last_nominal_fname
= NULL
;
1343 ip
->last_nominal_fname
= oldname
;
1346 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1348 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1349 goto bad_line_directive
;
1352 /* The Newline at the end of this line remains to be processed.
1353 To put the next line at the specified line number,
1354 we must store a line number now that is one less. */
1355 ip
->lineno
= new_lineno
- 1;
1356 CPP_SET_WRITTEN (pfile
, old_written
);
1357 output_line_command (pfile
, 0, file_change
);
1361 skip_rest_of_line (pfile
);
1362 CPP_SET_WRITTEN (pfile
, old_written
);
1366 /* Remove the definition of a symbol from the symbol table.
1367 According to the C standard, it is not an error to undef
1368 something that has no definitions. */
1370 do_undef (pfile
, keyword
)
1372 struct directive
*keyword
;
1376 U_CHAR
*buf
, *name
, *limit
;
1378 long here
= CPP_WRITTEN (pfile
);
1379 enum cpp_token token
;
1381 cpp_skip_hspace (pfile
);
1383 if (! is_idstart
[c
])
1385 cpp_error (pfile
, "token after #undef is not an identifier");
1386 skip_rest_of_line (pfile
);
1390 parse_name (pfile
, c
);
1391 buf
= pfile
->token_buffer
+ here
;
1392 limit
= CPP_PWRITTEN(pfile
);
1394 /* Copy out the token so we can pop the token buffer. */
1395 name
= alloca (limit
- buf
+ 1);
1396 bcopy(buf
, name
, limit
- buf
);
1397 name
[limit
- buf
] = '\0';
1399 token
= get_directive_token (pfile
);
1400 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1402 cpp_pedwarn (pfile
, "junk on line after #undef");
1403 skip_rest_of_line (pfile
);
1406 CPP_SET_WRITTEN (pfile
, here
);
1409 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
1410 if (pcp_outfile
&& keyword
)
1411 pass_thru_directive (buf
, limit
, pfile
, keyword
);
1414 sym_length
= check_macro_name (pfile
, buf
, 0);
1416 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1418 /* If we are generating additional info for debugging (with -g) we
1419 need to pass through all effective #undef commands. */
1420 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1421 pass_thru_directive (name
, name
+sym_length
, pfile
, keyword
);
1422 if (hp
->type
!= T_MACRO
)
1423 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1430 /* Wrap do_undef for -U processing. */
1432 cpp_undef (pfile
, macro
)
1436 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1438 do_undef (pfile
, NULL
);
1439 cpp_pop_buffer (pfile
);
1445 * Report an error detected by the program we are processing.
1446 * Use the text of the line in the error message.
1447 * (We use error because it prints the filename & line#.)
1451 do_error (pfile
, keyword
)
1453 struct directive
*keyword ATTRIBUTE_UNUSED
;
1455 long here
= CPP_WRITTEN (pfile
);
1457 copy_rest_of_line (pfile
);
1458 text
= pfile
->token_buffer
+ here
;
1459 SKIP_WHITE_SPACE(text
);
1461 cpp_error (pfile
, "#error %s", text
);
1462 CPP_SET_WRITTEN (pfile
, here
);
1467 * Report a warning detected by the program we are processing.
1468 * Use the text of the line in the warning message, then continue.
1472 do_warning (pfile
, keyword
)
1474 struct directive
*keyword ATTRIBUTE_UNUSED
;
1477 long here
= CPP_WRITTEN(pfile
);
1478 copy_rest_of_line (pfile
);
1479 text
= pfile
->token_buffer
+ here
;
1480 SKIP_WHITE_SPACE(text
);
1482 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1483 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1485 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1486 if -pedantic-errors is given, #warning should cause an error. */
1487 cpp_pedwarn (pfile
, "#warning %s", text
);
1488 CPP_SET_WRITTEN (pfile
, here
);
1492 /* Report program identification. */
1495 do_ident (pfile
, keyword
)
1497 struct directive
*keyword ATTRIBUTE_UNUSED
;
1499 /* Allow #ident in system headers, since that's not user's fault. */
1500 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1501 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1503 skip_rest_of_line (pfile
); /* Correct? Appears to match cccp. */
1508 /* Just check for some recognized pragmas that need validation here,
1509 and leave the text in the token buffer to be output. */
1512 do_pragma (pfile
, keyword
)
1514 struct directive
*keyword ATTRIBUTE_UNUSED
;
1516 long here
= CPP_WRITTEN (pfile
);
1519 copy_rest_of_line (pfile
);
1520 buf
= pfile
->token_buffer
+ here
;
1521 SKIP_WHITE_SPACE (buf
);
1523 if (!strncmp (buf
, "once", 4))
1525 cpp_buffer
*ip
= NULL
;
1527 /* Allow #pragma once in system headers, since that's not the user's
1529 if (!CPP_BUFFER (pfile
)->system_header_p
)
1530 cpp_warning (pfile
, "`#pragma once' is obsolete");
1532 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1534 if (ip
== CPP_NULL_BUFFER (pfile
))
1536 if (ip
->fname
!= NULL
)
1540 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1541 cpp_warning (pfile
, "`#pragma once' outside include file");
1543 ip
->ihash
->control_macro
= ""; /* never repeat */
1546 if (!strncmp (buf
, "implementation", 14))
1548 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1549 been included yet. */
1550 struct include_hash
*ptr
;
1551 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1552 SKIP_WHITE_SPACE (p
);
1553 if (*p
== '\n' || *p
!= '\"')
1557 p
= (U_CHAR
*) index (fname
, '\"');
1559 fcopy
= alloca (p
- fname
+ 1);
1560 bcopy (fname
, fcopy
, p
- fname
);
1561 fcopy
[p
-fname
] = '\0';
1563 ptr
= include_hash (pfile
, fcopy
, 0);
1566 "`#pragma implementation' for `%s' appears after file is included",
1573 #ifdef SCCS_DIRECTIVE
1574 /* Just ignore #sccs, on systems where we define it at all. */
1577 do_sccs (pfile
, keyword
)
1579 struct directive
*keyword ATTRIBUTE_UNUSED
;
1581 if (CPP_PEDANTIC (pfile
))
1582 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1583 skip_rest_of_line (pfile
);
1589 * handle #if command by
1590 * 1) inserting special `defined' keyword into the hash table
1591 * that gets turned into 0 or 1 by special_symbol (thus,
1592 * if the luser has a symbol called `defined' already, it won't
1593 * work inside the #if command)
1594 * 2) rescan the input into a temporary output buffer
1595 * 3) pass the output buffer to the yacc parser and collect a value
1596 * 4) clean up the mess left from steps 1 and 2.
1597 * 5) call conditional_skip to skip til the next #endif (etc.),
1598 * or not, depending on the value from step 3.
1602 do_if (pfile
, keyword
)
1604 struct directive
*keyword ATTRIBUTE_UNUSED
;
1606 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1607 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
1612 * handle a #elif directive by not changing if_stack either.
1613 * see the comment above do_else.
1617 do_elif (pfile
, keyword
)
1619 struct directive
*keyword ATTRIBUTE_UNUSED
;
1621 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1622 cpp_error (pfile
, "`#elif' not within a conditional");
1625 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1626 cpp_error (pfile
, "`#elif' after `#else'");
1628 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1630 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
1631 && strcmp (pfile
->if_stack
->fname
,
1632 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
1633 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1634 fprintf (stderr
, ")\n");
1636 pfile
->if_stack
->type
= T_ELIF
;
1639 if (pfile
->if_stack
->if_succeeded
)
1640 skip_if_group (pfile
);
1642 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1644 skip_if_group (pfile
);
1646 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1647 output_line_command (pfile
, 1, same_file
);
1654 * evaluate a #if expression in BUF, of length LENGTH,
1655 * then parse the result as a C expression and return the value as an int.
1658 static HOST_WIDEST_INT
1659 eval_if_expression (pfile
)
1662 HOST_WIDEST_INT value
;
1663 long old_written
= CPP_WRITTEN (pfile
);
1665 pfile
->pcp_inside_if
= 1;
1666 value
= cpp_parse_expr (pfile
);
1667 pfile
->pcp_inside_if
= 0;
1669 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1675 * routine to handle ifdef/ifndef. Try to look up the symbol,
1676 * then do or don't skip to the #endif/#else/#elif depending
1677 * on what directive is actually being processed.
1681 do_xifdef (pfile
, keyword
)
1683 struct directive
*keyword
;
1686 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1689 enum cpp_token token
;
1690 int start_of_file
= 0;
1691 U_CHAR
*control_macro
= 0;
1692 int old_written
= CPP_WRITTEN (pfile
);
1694 /* Detect a #ifndef at start of file (not counting comments). */
1695 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1696 start_of_file
= pfile
->only_seen_white
== 2;
1698 pfile
->no_macro_expand
++;
1699 token
= get_directive_token (pfile
);
1700 pfile
->no_macro_expand
--;
1702 ident
= pfile
->token_buffer
+ old_written
;
1703 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1704 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1706 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1708 skip
= (keyword
->type
== T_IFDEF
);
1709 if (! CPP_TRADITIONAL (pfile
))
1710 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1712 else if (token
== CPP_NAME
)
1714 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1715 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1716 if (start_of_file
&& !skip
)
1718 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1719 bcopy (ident
, control_macro
, ident_length
+ 1);
1724 skip
= (keyword
->type
== T_IFDEF
);
1725 if (! CPP_TRADITIONAL (pfile
))
1726 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1729 if (!CPP_TRADITIONAL (pfile
))
1731 cpp_skip_hspace (pfile
);
1733 if (c
!= EOF
&& c
!= '\n')
1734 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1736 skip_rest_of_line (pfile
);
1740 /* Output a precondition for this macro. */
1741 if (hp
&& hp
->value
.defn
->predefined
)
1742 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
1745 fprintf (pcp_outfile
, "#undef ");
1746 while (is_idchar
[*cp
]) /* Ick! */
1747 fputc (*cp
++, pcp_outfile
);
1748 putc ('\n', pcp_outfile
);
1752 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1756 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1757 If this is a #ifndef starting at the beginning of a file,
1758 CONTROL_MACRO is the macro name tested by the #ifndef.
1759 Otherwise, CONTROL_MACRO is 0. */
1762 conditional_skip (pfile
, skip
, type
, control_macro
)
1765 enum node_type type
;
1766 U_CHAR
*control_macro
;
1768 IF_STACK_FRAME
*temp
;
1770 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1771 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1773 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1775 temp
->next
= pfile
->if_stack
;
1776 temp
->control_macro
= control_macro
;
1777 pfile
->if_stack
= temp
;
1779 pfile
->if_stack
->type
= type
;
1782 skip_if_group (pfile
);
1785 ++pfile
->if_stack
->if_succeeded
;
1786 output_line_command (pfile
, 1, same_file
);
1790 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1791 return 0 if skipping should continue, 1 if it should halt. Also
1792 adjusts the if_stack as appropriate.
1793 The `#' has been read, but not the identifier. */
1796 consider_directive_while_skipping (pfile
, stack
)
1798 IF_STACK_FRAME
*stack
;
1800 long ident_len
, ident
;
1801 struct directive
*kt
;
1802 IF_STACK_FRAME
*temp
;
1804 cpp_skip_hspace (pfile
);
1806 ident
= CPP_WRITTEN (pfile
);
1807 parse_name (pfile
, GETC());
1808 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1810 CPP_SET_WRITTEN (pfile
, ident
);
1812 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1813 if (kt
->length
== ident_len
1814 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
1820 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
1821 temp
->next
= pfile
->if_stack
;
1822 pfile
->if_stack
= temp
;
1823 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
1824 temp
->type
= kt
->type
;
1828 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
1829 validate_else (pfile
, "#else");
1832 if (pfile
->if_stack
->type
== T_ELSE
)
1833 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
1835 if (pfile
->if_stack
== stack
)
1839 pfile
->if_stack
->type
= kt
->type
;
1844 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
1845 validate_else (pfile
, "#endif");
1847 if (pfile
->if_stack
== stack
)
1850 temp
= pfile
->if_stack
;
1851 pfile
->if_stack
= temp
->next
;
1859 /* Don't let erroneous code go by. */
1860 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
1861 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
1865 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1866 * leaves input ptr at the sharp sign found.
1869 skip_if_group (pfile
)
1873 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
1874 U_CHAR
*beg_of_line
;
1877 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1879 CPP_PUTS (pfile
, "#failed\n", 8);
1881 output_line_command (pfile
, 1, same_file
);
1884 old_written
= CPP_WRITTEN (pfile
);
1888 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
1890 if (! CPP_TRADITIONAL (pfile
))
1891 cpp_skip_hspace (pfile
);
1895 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1896 CPP_PUTC (pfile
, c
);
1897 CPP_BUMP_LINE (pfile
);
1902 if (consider_directive_while_skipping (pfile
, save_if_stack
))
1906 return; /* Caller will issue error. */
1909 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1911 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
1912 copy_rest_of_line (pfile
);
1916 copy_rest_of_line (pfile
);
1917 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
1922 return; /* Caller will issue error. */
1926 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1928 CPP_PUTC (pfile
, c
);
1931 CPP_BUMP_LINE (pfile
);
1935 /* Back up to the beginning of this line. Caller will process the
1937 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
1938 pfile
->only_seen_white
= 1;
1939 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1941 CPP_PUTS (pfile
, "#endfailed\n", 11);
1947 * handle a #else directive. Do this by just continuing processing
1948 * without changing if_stack ; this is so that the error message
1949 * for missing #endif's etc. will point to the original #if. It
1950 * is possible that something different would be better.
1954 do_else (pfile
, keyword
)
1956 struct directive
*keyword ATTRIBUTE_UNUSED
;
1958 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1960 if (CPP_PEDANTIC (pfile
))
1961 validate_else (pfile
, "#else");
1962 skip_rest_of_line (pfile
);
1964 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1965 cpp_error (pfile
, "`#else' not within a conditional");
1968 /* #ifndef can't have its special treatment for containing the whole file
1969 if it has a #else clause. */
1970 pfile
->if_stack
->control_macro
= 0;
1972 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1973 cpp_error (pfile
, "`#else' after `#else'");
1974 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1975 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
1976 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1977 fprintf (stderr
, ")\n");
1979 pfile
->if_stack
->type
= T_ELSE
;
1982 if (pfile
->if_stack
->if_succeeded
)
1983 skip_if_group (pfile
);
1985 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1986 output_line_command (pfile
, 1, same_file
);
1992 * unstack after #endif command
1996 do_endif (pfile
, keyword
)
1998 struct directive
*keyword ATTRIBUTE_UNUSED
;
2000 if (CPP_PEDANTIC (pfile
))
2001 validate_else (pfile
, "#endif");
2002 skip_rest_of_line (pfile
);
2004 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
2005 cpp_error (pfile
, "unbalanced `#endif'");
2008 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
2009 pfile
->if_stack
= temp
->next
;
2010 if (temp
->control_macro
!= 0)
2012 /* This #endif matched a #ifndef at the start of the file.
2013 See if it is at the end of the file. */
2016 parse_set_mark (pfile
);
2020 cpp_skip_hspace (pfile
);
2025 parse_goto_mark (pfile
);
2029 /* This #endif ends a #ifndef
2030 that contains all of the file (aside from whitespace).
2031 Arrange not to include the file again
2032 if the macro that was tested is defined. */
2033 struct cpp_buffer
*ip
;
2034 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2035 if (ip
->fname
!= NULL
)
2037 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
2041 output_line_command (pfile
, 1, same_file
);
2046 /* When an #else or #endif is found while skipping failed conditional,
2047 if -pedantic was specified, this is called to warn about text after
2048 the command name. P points to the first char after the command name. */
2051 validate_else (pfile
, directive
)
2056 cpp_skip_hspace (pfile
);
2058 if (c
!= EOF
&& c
!= '\n')
2060 "text following `%s' violates ANSI standard", directive
);
2063 /* Get the next token, and add it to the text in pfile->token_buffer.
2064 Return the kind of token we got. */
2067 cpp_get_token (pfile
)
2070 register int c
, c2
, c3
;
2071 enum cpp_token token
;
2072 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2079 if (CPP_BUFFER (pfile
)->seen_eof
)
2081 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2084 cpp_pop_buffer (pfile
);
2089 cpp_buffer
*next_buf
2090 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2091 CPP_BUFFER (pfile
)->seen_eof
= 1;
2092 if (CPP_BUFFER (pfile
)->nominal_fname
2093 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2095 /* We're about to return from an #include file.
2096 Emit #line information now (as part of the CPP_POP) result.
2097 But the #line refers to the file we will pop to. */
2098 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2099 CPP_BUFFER (pfile
) = next_buf
;
2100 pfile
->input_stack_listing_current
= 0;
2101 output_line_command (pfile
, 0, leave_file
);
2102 CPP_BUFFER (pfile
) = cur_buffer
;
2112 if (PEEKC () == '=')
2116 if (opts
->put_out_comments
)
2117 c
= copy_comment (pfile
, c
);
2119 c
= skip_comment (pfile
, c
);
2125 /* Comments are equivalent to spaces.
2126 For -traditional, a comment is equivalent to nothing. */
2127 if (opts
->traditional
|| opts
->put_out_comments
)
2131 CPP_PUTC (pfile
, c
);
2135 if (opts
->for_lint
) {
2138 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2140 if (lintcmd
!= NULL
) {
2141 /* I believe it is always safe to emit this newline: */
2143 bcopy ("#pragma lint ", (char *) obp
, 13);
2145 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2150 bcopy (argbp
, (char *) obp
, arglen
);
2154 /* OK, now bring us back to the state we were in before we entered
2155 this branch. We need #line because the newline for the pragma
2156 could mess things up. */
2157 output_line_command (pfile
, 0, same_file
);
2158 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2166 /* If this is expanding a macro definition, don't recognize
2167 preprocessor directives. */
2170 /* If this is expand_into_temp_buffer, recognize them
2171 only after an actual newline at this level,
2172 not at the beginning of the input level. */
2173 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
2179 if (!pfile
->only_seen_white
)
2181 if (handle_directive (pfile
))
2182 return CPP_DIRECTIVE
;
2183 pfile
->only_seen_white
= 0;
2189 parse_string (pfile
, c
);
2190 pfile
->only_seen_white
= 0;
2191 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2194 if (!opts
->dollars_in_ident
)
2199 if (opts
->cplusplus
&& PEEKC () == ':')
2207 if (c2
== c
|| c2
== '=')
2216 if (PEEKC () == '=')
2222 if (c2
== '-' && opts
->chill
)
2223 goto comment
; /* Chill style comment */
2224 if (c2
== '-' || c2
== '=' || c2
== '>')
2229 if (pfile
->parsing_include_directive
)
2233 CPP_PUTC (pfile
, c
);
2237 if (c
== '\n' || c
== EOF
)
2240 "missing '>' in `#include <FILENAME>'");
2245 if (!CPP_BUFFER (pfile
)->has_escapes
)
2247 /* Backslash newline is replaced by nothing. */
2248 CPP_ADJUST_WRITTEN (pfile
, -1);
2249 CPP_BUMP_LINE (pfile
);
2253 /* We might conceivably get \r- or \r<space> in
2254 here. Just delete 'em. */
2256 if (d
!= '-' && d
!= ' ')
2258 "internal error: unrecognized escape \\r%c",
2260 CPP_ADJUST_WRITTEN (pfile
, -1);
2266 /* else fall through */
2274 CPP_RESERVE (pfile
, 4);
2275 CPP_PUTC (pfile
, c
);
2276 CPP_PUTC (pfile
, c2
);
2279 CPP_PUTC_Q (pfile
, GETC ());
2280 CPP_NUL_TERMINATE_Q (pfile
);
2281 pfile
->only_seen_white
= 0;
2288 CPP_RESERVE(pfile
, 2);
2289 CPP_PUTC_Q (pfile
, '.');
2293 if (c2
== '.' && PEEKN(1) == '.')
2295 CPP_RESERVE(pfile
, 4);
2296 CPP_PUTC_Q (pfile
, '.');
2297 CPP_PUTC_Q (pfile
, '.');
2298 CPP_PUTC_Q (pfile
, '.');
2300 CPP_NUL_TERMINATE_Q (pfile
);
2301 pfile
->only_seen_white
= 0;
2308 pfile
->only_seen_white
= 0;
2309 CPP_RESERVE(pfile
, 3);
2310 CPP_PUTC_Q (pfile
, c
);
2311 CPP_PUTC_Q (pfile
, GETC ());
2312 CPP_NUL_TERMINATE_Q (pfile
);
2317 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2319 CPP_PUTC (pfile
, c
);
2325 case '0': case '1': case '2': case '3': case '4':
2326 case '5': case '6': case '7': case '8': case '9':
2331 CPP_RESERVE (pfile
, 2);
2332 CPP_PUTC_Q (pfile
, c
);
2336 if (!is_idchar
[c
] && c
!= '.'
2337 && ((c2
!= 'e' && c2
!= 'E'
2338 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2339 || (c
!= '+' && c
!= '-')))
2344 CPP_NUL_TERMINATE_Q (pfile
);
2345 pfile
->only_seen_white
= 0;
2347 case 'b': case 'c': case 'd': case 'h': case 'o':
2348 case 'B': case 'C': case 'D': case 'H': case 'O':
2349 if (opts
->chill
&& PEEKC () == '\'')
2351 pfile
->only_seen_white
= 0;
2352 CPP_RESERVE (pfile
, 2);
2353 CPP_PUTC_Q (pfile
, c
);
2354 CPP_PUTC_Q (pfile
, '\'');
2360 goto chill_number_eof
;
2363 CPP_PUTC (pfile
, c
);
2367 CPP_RESERVE (pfile
, 2);
2368 CPP_PUTC_Q (pfile
, c
);
2369 CPP_NUL_TERMINATE_Q (pfile
);
2376 CPP_NUL_TERMINATE (pfile
);
2383 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2384 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2385 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2386 case 'x': case 'y': case 'z':
2387 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2388 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2389 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2394 unsigned char *ident
;
2395 int before_name_written
= CPP_WRITTEN (pfile
);
2397 parse_name (pfile
, c
);
2398 pfile
->only_seen_white
= 0;
2399 if (pfile
->no_macro_expand
)
2401 ident
= pfile
->token_buffer
+ before_name_written
;
2402 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2403 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2406 if (hp
->type
== T_DISABLED
)
2408 if (pfile
->output_escapes
)
2409 { /* Return "\r-IDENT", followed by '\0'. */
2411 CPP_RESERVE (pfile
, 3);
2412 ident
= pfile
->token_buffer
+ before_name_written
;
2413 CPP_ADJUST_WRITTEN (pfile
, 2);
2414 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2421 /* If macro wants an arglist, verify that a '(' follows.
2422 first skip all whitespace, copying it to the output
2423 after the macro name. Then, if there is no '(',
2424 decide this is not a macro call and leave things that way. */
2425 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2427 int is_macro_call
, macbuf_whitespace
= 0;
2429 parse_set_mark (pfile
);
2432 cpp_skip_hspace (pfile
);
2434 is_macro_call
= c
== '(';
2443 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2445 if (CPP_BUFFER (pfile
)->mark
!=
2446 (CPP_BUFFER (pfile
)->cur
2447 - CPP_BUFFER (pfile
)->buf
))
2448 macbuf_whitespace
= 1;
2450 /* The mark goes away automatically when
2451 the buffer is popped. */
2452 cpp_pop_buffer (pfile
);
2453 parse_set_mark (pfile
);
2461 parse_goto_mark (pfile
);
2462 if (macbuf_whitespace
)
2463 CPP_PUTC (pfile
, ' ');
2466 parse_clear_mark (pfile
);
2470 /* This is now known to be a macro call.
2471 Expand the macro, reading arguments as needed,
2472 and push the expansion on the input stack. */
2473 macroexpand (pfile
, hp
);
2474 CPP_SET_WRITTEN (pfile
, before_name_written
);
2478 case ' ': case '\t': case '\v':
2481 CPP_PUTC (pfile
, c
);
2483 if (c
== EOF
|| !is_hor_space
[c
])
2490 if (CPP_BUFFER (pfile
)->has_escapes
)
2495 if (pfile
->output_escapes
)
2496 CPP_PUTS (pfile
, "\r-", 2);
2497 parse_name (pfile
, GETC ());
2502 CPP_RESERVE (pfile
, 2);
2503 if (pfile
->output_escapes
)
2504 CPP_PUTC_Q (pfile
, '\r');
2505 CPP_PUTC_Q (pfile
, c
);
2511 "internal error: unrecognized escape \\r%c", c
);
2517 /* Backslash newline is ignored. */
2518 CPP_BUMP_LINE (pfile
);
2523 CPP_PUTC (pfile
, c
);
2524 if (pfile
->only_seen_white
== 0)
2525 pfile
->only_seen_white
= 1;
2526 CPP_BUMP_LINE (pfile
);
2528 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2529 output_line_command (pfile
, 1, same_file
);
2532 case '(': token
= CPP_LPAREN
; goto char1
;
2533 case ')': token
= CPP_RPAREN
; goto char1
;
2534 case '{': token
= CPP_LBRACE
; goto char1
;
2535 case '}': token
= CPP_RBRACE
; goto char1
;
2536 case ',': token
= CPP_COMMA
; goto char1
;
2537 case ';': token
= CPP_SEMICOLON
; goto char1
;
2543 pfile
->only_seen_white
= 0;
2544 CPP_PUTC (pfile
, c
);
2550 /* Like cpp_get_token, but skip spaces and comments. */
2553 cpp_get_non_space_token (pfile
)
2556 int old_written
= CPP_WRITTEN (pfile
);
2559 enum cpp_token token
= cpp_get_token (pfile
);
2560 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2561 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2563 CPP_SET_WRITTEN (pfile
, old_written
);
2567 /* Parse an identifier starting with C. */
2570 parse_name (pfile
, c
)
2582 if (c
== '$' && CPP_PEDANTIC (pfile
))
2583 cpp_pedwarn (pfile
, "`$' in identifier");
2585 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2586 CPP_PUTC_Q (pfile
, c
);
2591 CPP_NUL_TERMINATE_Q (pfile
);
2595 /* Parse a string starting with C. A single quoted string is treated
2596 like a double -- some programs (e.g., troff) are perverse this way.
2597 (However, a single quoted string is not allowed to extend over
2600 parse_string (pfile
, c
)
2604 long start_line
, start_column
;
2606 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2608 CPP_PUTC (pfile
, c
);
2614 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2616 /* try harder: this string crosses a macro expansion
2617 boundary. This can happen naturally if -traditional.
2618 Otherwise, only -D can make a macro with an unmatched
2620 cpp_pop_buffer (pfile
);
2623 if (!CPP_TRADITIONAL (pfile
))
2625 cpp_error_with_line (pfile
, start_line
, start_column
,
2626 "unterminated string or character constant");
2627 if (pfile
->multiline_string_line
!= start_line
2628 && pfile
->multiline_string_line
!= 0)
2629 cpp_error_with_line (pfile
,
2630 pfile
->multiline_string_line
, -1,
2631 "possible real start of unterminated constant");
2632 pfile
->multiline_string_line
= 0;
2636 CPP_PUTC (pfile
, cc
);
2640 CPP_BUMP_LINE (pfile
);
2642 /* Traditionally, end of line ends a string constant with
2644 if (CPP_TRADITIONAL (pfile
))
2646 /* Character constants may not extend over multiple lines. */
2649 cpp_error_with_line (pfile
, start_line
, start_column
,
2650 "unterminated character constant");
2653 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2655 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2656 "string constant runs past end of line");
2658 if (pfile
->multiline_string_line
== 0)
2659 pfile
->multiline_string_line
= start_line
;
2663 CPP_ADJUST_WRITTEN (pfile
, -1);
2664 if (CPP_BUFFER (pfile
)->has_escapes
)
2667 "internal error: \\r escape inside string constant");
2671 /* Backslash newline is replaced by nothing at all. */
2672 CPP_BUMP_LINE (pfile
);
2678 CPP_PUTC (pfile
, cc
);
2690 /* Read an assertion into the token buffer, converting to
2691 canonical form: `#predicate(a n swe r)' The next non-whitespace
2692 character to read should be the first letter of the predicate.
2693 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2694 with answer (see callers for why). In case of 0, an error has been
2697 parse_assertion (pfile
)
2701 cpp_skip_hspace (pfile
);
2703 if (! is_idstart
[c
])
2705 cpp_error (pfile
, "assertion predicate is not an identifier");
2708 CPP_PUTC(pfile
, '#');
2710 parse_name(pfile
, c
);
2715 if (is_hor_space
[c
] || c
== '\r')
2716 cpp_skip_hspace (pfile
);
2722 CPP_PUTC(pfile
, '(');
2725 while ((c
= GETC()) != ')')
2727 if (is_hor_space
[c
])
2731 CPP_PUTC(pfile
, ' ');
2735 else if (c
== '\n' || c
== EOF
)
2737 if (c
== '\n') FORWARD(-1);
2738 cpp_error (pfile
, "un-terminated assertion answer");
2742 /* \r cannot be a macro escape here. */
2743 CPP_BUMP_LINE (pfile
);
2746 CPP_PUTC (pfile
, c
);
2751 if (pfile
->limit
[-1] == ' ')
2752 pfile
->limit
[-1] = ')';
2753 else if (pfile
->limit
[-1] == '(')
2755 cpp_error (pfile
, "empty token sequence in assertion");
2759 CPP_PUTC (pfile
, ')');
2761 CPP_NUL_TERMINATE (pfile
);
2766 do_assert (pfile
, keyword
)
2768 struct directive
*keyword ATTRIBUTE_UNUSED
;
2772 HASHNODE
*base
, *this;
2773 int baselen
, thislen
;
2775 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2776 && !CPP_BUFFER (pfile
)->system_header_p
)
2777 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
2779 cpp_skip_hspace (pfile
);
2780 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2781 ret
= parse_assertion (pfile
);
2786 cpp_error (pfile
, "missing token-sequence in `#assert'");
2790 cpp_skip_hspace (pfile
);
2792 if (c
!= EOF
&& c
!= '\n')
2794 cpp_error (pfile
, "junk at end of `#assert'");
2798 thislen
= strlen (sym
);
2799 baselen
= index (sym
, '(') - sym
;
2800 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2803 cpp_warning (pfile
, "`%s' re-asserted", sym
);
2807 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
2809 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
2810 else if (base
->type
!= T_ASSERT
)
2812 /* Token clash - but with what?! */
2814 "cpp internal error: base->type != T_ASSERT in do_assert");
2818 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
2819 (char *)base
->value
.aschain
, -1);
2820 base
->value
.aschain
= this;
2822 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2826 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2827 skip_rest_of_line (pfile
);
2832 do_unassert (pfile
, keyword
)
2834 struct directive
*keyword ATTRIBUTE_UNUSED
;
2838 long baselen
, thislen
;
2839 HASHNODE
*base
, *this, *next
;
2841 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2842 && !CPP_BUFFER (pfile
)->system_header_p
)
2843 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
2845 cpp_skip_hspace (pfile
);
2847 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2848 ret
= parse_assertion (pfile
);
2852 cpp_skip_hspace (pfile
);
2854 if (c
!= EOF
&& c
!= '\n')
2855 cpp_error (pfile
, "junk at end of `#unassert'");
2857 thislen
= strlen (sym
);
2860 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
2862 goto error
; /* It isn't an error to #undef what isn't #defined,
2863 so it isn't an error to #unassert what isn't
2864 #asserted either. */
2866 for (this = base
->value
.aschain
; this; this = next
)
2868 next
= this->value
.aschain
;
2869 delete_macro (this);
2871 delete_macro (base
);
2875 baselen
= index (sym
, '(') - sym
;
2876 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
2877 if (! base
) goto error
;
2878 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2879 if (! this) goto error
;
2882 while (next
->value
.aschain
!= this)
2883 next
= next
->value
.aschain
;
2885 next
->value
.aschain
= this->value
.aschain
;
2886 delete_macro (this);
2888 if (base
->value
.aschain
== NULL
)
2889 delete_macro (base
); /* Last answer for this predicate deleted. */
2892 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2895 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2896 skip_rest_of_line (pfile
);
2900 /* Process STR as if it appeared as the body of an #unassert. */
2902 cpp_unassert (pfile
, str
)
2906 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
2908 do_assert (pfile
, NULL
);
2909 cpp_pop_buffer (pfile
);
2914 cpp_read_check_assertion (pfile
)
2917 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
2921 FORWARD (1); /* Skip '#' */
2922 cpp_skip_hspace (pfile
);
2923 if (! parse_assertion (pfile
))
2927 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
2931 pfile
->limit
= name
;
2935 /* Remember the current position of PFILE. */
2938 parse_set_mark (pfile
)
2941 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2944 "cpp internal error: ip->mark != -1 in parse_set_mark");
2946 ip
->mark
= ip
->cur
- ip
->buf
;
2949 /* Clear the current mark - we no longer need it. */
2952 parse_clear_mark (pfile
)
2955 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2958 "cpp internal error: ip->mark == -1 in parse_clear_mark");
2963 /* Backup the current position of PFILE to that saved in its mark,
2964 and clear the mark. */
2967 parse_goto_mark (pfile
)
2970 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2973 "cpp internal error: ip->mark == -1 in parse_goto_mark");
2975 ip
->cur
= ip
->buf
+ ip
->mark
;
2980 cpp_print_file_and_line (pfile
)
2983 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
2988 cpp_buf_line_and_col (ip
, &line
, &col
);
2989 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
2990 line
, pfile
->show_column
? col
: -1);
2995 v_cpp_error (pfile
, msgid
, ap
)
3000 cpp_print_containing_files (pfile
);
3001 cpp_print_file_and_line (pfile
);
3002 v_cpp_message (pfile
, 1, msgid
, ap
);
3006 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3008 #ifndef ANSI_PROTOTYPES
3014 VA_START(ap
, msgid
);
3016 #ifndef ANSI_PROTOTYPES
3017 pfile
= va_arg (ap
, cpp_reader
*);
3018 msgid
= va_arg (ap
, const char *);
3021 v_cpp_error (pfile
, msgid
, ap
);
3025 /* Print error message but don't count it. */
3028 v_cpp_warning (pfile
, msgid
, ap
)
3033 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3036 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3039 cpp_print_containing_files (pfile
);
3040 cpp_print_file_and_line (pfile
);
3041 v_cpp_message (pfile
, 0, msgid
, ap
);
3045 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3047 #ifndef ANSI_PROTOTYPES
3053 VA_START (ap
, msgid
);
3055 #ifndef ANSI_PROTOTYPES
3056 pfile
= va_arg (ap
, cpp_reader
*);
3057 msgid
= va_arg (ap
, const char *);
3060 v_cpp_warning (pfile
, msgid
, ap
);
3064 /* Print an error message and maybe count it. */
3067 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3069 #ifndef ANSI_PROTOTYPES
3075 VA_START (ap
, msgid
);
3077 #ifndef ANSI_PROTOTYPES
3078 pfile
= va_arg (ap
, cpp_reader
*);
3079 msgid
= va_arg (ap
, const char *);
3082 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3083 v_cpp_error (pfile
, msgid
, ap
);
3085 v_cpp_warning (pfile
, msgid
, ap
);
3090 v_cpp_error_with_line (pfile
, line
, column
, msgid
, ap
)
3097 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3099 cpp_print_containing_files (pfile
);
3102 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3104 v_cpp_message (pfile
, 1, msgid
, ap
);
3108 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3109 const char *msgid
, ...))
3111 #ifndef ANSI_PROTOTYPES
3119 VA_START (ap
, msgid
);
3121 #ifndef ANSI_PROTOTYPES
3122 pfile
= va_arg (ap
, cpp_reader
*);
3123 line
= va_arg (ap
, int);
3124 column
= va_arg (ap
, int);
3125 msgid
= va_arg (ap
, const char *);
3128 v_cpp_error_with_line(pfile
, line
, column
, msgid
, ap
);
3133 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
)
3142 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3145 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3148 cpp_print_containing_files (pfile
);
3150 ip
= cpp_file_buffer (pfile
);
3153 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3155 v_cpp_message (pfile
, 0, msgid
, ap
);
3159 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3160 const char *msgid
, ...))
3162 #ifndef ANSI_PROTOTYPES
3170 VA_START (ap
, msgid
);
3172 #ifndef ANSI_PROTOTYPES
3173 pfile
= va_arg (ap
, cpp_reader
*);
3174 line
= va_arg (ap
, int);
3175 column
= va_arg (ap
, int);
3176 msgid
= va_arg (ap
, const char *);
3179 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3184 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3185 const char *msgid
, ...))
3187 #ifndef ANSI_PROTOTYPES
3195 VA_START (ap
, msgid
);
3197 #ifndef ANSI_PROTOTYPES
3198 pfile
= va_arg (ap
, cpp_reader
*);
3199 line
= va_arg (ap
, int);
3200 column
= va_arg (ap
, int);
3201 msgid
= va_arg (ap
, const char *);
3204 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3205 v_cpp_error_with_line (pfile
, column
, line
, msgid
, ap
);
3207 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3211 /* Report a warning (or an error if pedantic_errors)
3212 giving specified file name and line number, not current. */
3215 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, char *file
, int line
,
3216 const char *msgid
, ...))
3218 #ifndef ANSI_PROTOTYPES
3226 VA_START (ap
, msgid
);
3228 #ifndef ANSI_PROTOTYPES
3229 pfile
= va_arg (ap
, cpp_reader
*);
3230 file
= va_arg (ap
, char *);
3231 line
= va_arg (ap
, int);
3232 msgid
= va_arg (ap
, const char *);
3235 if (!CPP_OPTIONS (pfile
)->pedantic_errors
3236 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
3239 cpp_file_line_for_message (pfile
, file
, line
, -1);
3240 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msgid
, ap
);
3244 /* my_strerror - return the descriptive text associated with an
3248 my_strerror (errnum
)
3254 #ifndef HAVE_STRERROR
3255 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
3257 result
= strerror (errnum
);
3260 /* VAXCRTL's strerror() takes an optional second argument, which only
3261 matters when the first argument is EVMSERR. However, it's simplest
3262 just to pass it unconditionally. `vaxc$errno' is declared in
3263 <errno.h>, and maintained by the library in parallel with `errno'.
3264 We assume that caller's `errnum' either matches the last setting of
3265 `errno' by the library or else does not have the value `EVMSERR'. */
3267 result
= strerror (errnum
, vaxc$errno
);
3271 result
= "errno = ?";
3276 /* Error including a message from `errno'. */
3279 cpp_error_from_errno (pfile
, name
)
3283 cpp_message_from_errno (pfile
, 1, name
);
3287 cpp_message_from_errno (pfile
, is_error
, name
)
3293 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3295 cpp_print_containing_files (pfile
);
3298 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
3300 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
3304 cpp_perror_with_name (pfile
, name
)
3308 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
3312 * No pre-compiled header file support.
3314 * Possibly different enum token codes for each C/C++ token.
3316 * Find and cleanup remaining uses of static variables,
3318 * Support -dM flag (dump_all_macros).
3320 * Support for_lint flag.