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
);
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)
524 /* A line of just `#' becomes blank. */
532 * Decode the keyword and call the appropriate expansion
533 * routine, after moving the input pointer up to the next line.
535 for (kt
= directive_table
; ; kt
++)
539 if (kt
->length
== ident_length
540 && !strncmp (kt
->name
, ident
, ident_length
))
544 CPP_SET_WRITTEN (pfile
, old_written
);
545 (*kt
->func
) (pfile
, kt
);
550 /* Pass a directive through to the output file.
551 BUF points to the contents of the directive, as a contiguous string.
552 LEN is the length of the string pointed to by BUF.
553 KEYWORD is the keyword-table entry for the directive. */
556 pass_thru_directive (buf
, len
, pfile
, keyword
)
560 struct directive
*keyword
;
562 register unsigned keyword_length
= keyword
->length
;
564 CPP_RESERVE (pfile
, 1 + keyword_length
+ len
);
565 CPP_PUTC_Q (pfile
, '#');
566 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
567 if (len
!= 0 && buf
[0] != ' ')
568 CPP_PUTC_Q (pfile
, ' ');
569 CPP_PUTS_Q (pfile
, buf
, len
);
572 /* Check a purported macro name SYMNAME, and yield its length.
573 ASSERTION is nonzero if this is really for an assertion name. */
576 check_macro_name (pfile
, symname
, assertion
)
584 for (p
= symname
; is_idchar
[*p
]; p
++)
586 sym_length
= p
- symname
;
588 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
590 assertion
? "invalid assertion name" : "invalid macro name");
591 else if (!is_idstart
[*symname
]
592 || (! strncmp (symname
, "defined", 7) && sym_length
== 7)) {
593 U_CHAR
*msg
; /* what pain... */
594 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
595 bcopy (symname
, msg
, sym_length
);
599 ? "invalid assertion name `%s'"
600 : "invalid macro name `%s'"),
606 /* Process a #define command.
607 KEYWORD is the keyword-table entry for #define,
608 or NULL for a "predefined" macro. */
611 do_define (pfile
, keyword
)
613 struct directive
*keyword
;
619 U_CHAR
*macro
, *buf
, *end
;
621 here
= CPP_WRITTEN (pfile
);
622 copy_rest_of_line (pfile
);
624 /* Copy out the line so we can pop the token buffer. */
625 buf
= pfile
->token_buffer
+ here
;
626 end
= CPP_PWRITTEN (pfile
);
627 macro
= alloca (end
- buf
+ 1);
628 bcopy (buf
, macro
, end
- buf
+ 1);
629 end
= macro
+ (end
- buf
);
631 CPP_SET_WRITTEN (pfile
, here
);
633 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
637 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
639 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
642 /* Redefining a precompiled key is ok. */
643 if (hp
->type
== T_PCSTRING
)
645 /* Redefining a macro is ok if the definitions are the same. */
646 else if (hp
->type
== T_MACRO
)
647 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
648 /* Redefining a constant is ok with -D. */
649 else if (hp
->type
== T_CONST
|| hp
->type
== T_STDC
)
650 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
651 /* Print the warning if it's not ok. */
654 cpp_pedwarn (pfile
, "`%.*s' redefined", mdef
.symlen
, mdef
.symnam
);
655 if (hp
->type
== T_MACRO
)
656 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
,
657 hp
->value
.defn
->line
,
658 "this is the location of the previous definition");
660 /* Replace the old definition. */
662 hp
->value
.defn
= mdef
.defn
;
665 cpp_install (pfile
, mdef
.symnam
, mdef
.symlen
, T_MACRO
,
666 (char *) mdef
.defn
, hashcode
);
670 if (CPP_OPTIONS (pfile
)->debug_output
671 || CPP_OPTIONS (pfile
)->dump_macros
== dump_definitions
)
672 dump_definition (pfile
, mdef
);
673 else if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
674 pass_thru_directive (mdef
.symnam
, mdef
.symlen
, pfile
, keyword
);
681 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
682 If BUFFER != NULL, then use the LENGTH characters in BUFFER
683 as the new input buffer.
684 Return the new buffer, or NULL on failure. */
687 cpp_push_buffer (pfile
, buffer
, length
)
692 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
694 if (++pfile
->buffer_stack_depth
== CPP_STACK_MAX
)
696 cpp_fatal (pfile
, "macro or `#include' recursion too deep");
700 new = (cpp_buffer
*) xcalloc (sizeof (cpp_buffer
), 1);
702 new->if_stack
= pfile
->if_stack
;
703 new->cleanup
= null_cleanup
;
704 new->underflow
= null_underflow
;
705 new->buf
= new->cur
= buffer
;
706 new->alimit
= new->rlimit
= buffer
+ length
;
710 CPP_BUFFER (pfile
) = new;
715 cpp_pop_buffer (pfile
)
718 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
719 (*buf
->cleanup
) (buf
, pfile
);
720 CPP_BUFFER (pfile
) = CPP_PREV_BUFFER (buf
);
722 pfile
->buffer_stack_depth
--;
723 return CPP_BUFFER (pfile
);
726 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
727 Pop the buffer when done. */
730 cpp_scan_buffer (pfile
)
733 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
736 enum cpp_token token
= cpp_get_token (pfile
);
737 if (token
== CPP_EOF
) /* Should not happen ... */
739 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
741 cpp_pop_buffer (pfile
);
748 * Rescan a string (which may have escape marks) into pfile's buffer.
749 * Place the result in pfile->token_buffer.
751 * The input is copied before it is scanned, so it is safe to pass
752 * it something from the token_buffer that will get overwritten
753 * (because it follows CPP_WRITTEN). This is used by do_include.
757 cpp_expand_to_buffer (pfile
, buf
, length
)
762 register cpp_buffer
*ip
;
768 int odepth
= indepth
;
773 cpp_fatal (pfile
, "internal error: length < 0 in cpp_expand_to_buffer");
777 /* Set up the input on the input stack. */
779 buf1
= (U_CHAR
*) alloca (length
+ 1);
780 memcpy (buf1
, buf
, length
);
783 ip
= cpp_push_buffer (pfile
, buf1
, length
);
788 ip
->lineno
= obuf
.lineno
= 1;
791 /* Scan the input, create the output. */
792 cpp_scan_buffer (pfile
);
794 CPP_NUL_TERMINATE (pfile
);
798 cpp_buf_line_and_col (pbuf
, linep
, colp
)
799 register cpp_buffer
*pbuf
;
804 *linep
= pbuf
->lineno
;
806 *colp
= pbuf
->cur
- pbuf
->line_base
;
816 /* Return the cpp_buffer that corresponds to a file (not a macro). */
819 cpp_file_buffer (pfile
)
822 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
824 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
825 if (ip
->fname
!= NULL
)
831 * write out a #line command, for instance, after an #include file.
832 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
836 output_line_command (pfile
, file_change
)
838 enum file_change_code file_change
;
841 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
843 if (ip
->fname
== NULL
)
846 if (CPP_OPTIONS (pfile
)->no_line_commands
847 || CPP_OPTIONS (pfile
)->no_output
)
850 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, NULL
);
852 /* If the current file has not changed, we omit the #line if it would
853 appear to be a no-op, and we output a few newlines instead
854 if we want to increase the line number by a small amount.
855 We cannot do this if pfile->lineno is zero, because that means we
856 haven't output any line commands yet. (The very first line command
857 output is a `same_file' command.) */
858 if (file_change
== same_file
&& pfile
->lineno
!= 0)
860 if (line
== pfile
->lineno
)
863 /* If the inherited line number is a little too small,
864 output some newlines instead of a #line command. */
865 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8)
867 CPP_RESERVE (pfile
, 20);
868 while (line
> pfile
->lineno
)
870 CPP_PUTC_Q (pfile
, '\n');
877 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
878 CPP_PUTS_Q (pfile
, "# ", 2);
880 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
881 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
883 quote_string (pfile
, ip
->nominal_fname
);
884 if (file_change
!= same_file
)
886 CPP_PUTC_Q (pfile
, ' ');
887 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
889 /* Tell cc1 if following text comes from a system header file. */
890 if (ip
->system_header_p
)
892 CPP_PUTC_Q (pfile
, ' ');
893 CPP_PUTC_Q (pfile
, '3');
895 #ifndef NO_IMPLICIT_EXTERN_C
896 /* Tell cc1plus if following text should be treated as C. */
897 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
)
899 CPP_PUTC_Q (pfile
, ' ');
900 CPP_PUTC_Q (pfile
, '4');
903 CPP_PUTC_Q (pfile
, '\n');
904 pfile
->lineno
= line
;
908 /* Like cpp_get_token, except that it does not read past end-of-line.
909 Also, horizontal space is skipped, and macros are popped. */
911 static enum cpp_token
912 get_directive_token (pfile
)
917 long old_written
= CPP_WRITTEN (pfile
);
918 enum cpp_token token
;
919 cpp_skip_hspace (pfile
);
920 if (PEEKC () == '\n')
922 token
= cpp_get_token (pfile
);
926 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
928 /* ... else fall though ... */
929 case CPP_HSPACE
: case CPP_COMMENT
:
930 CPP_SET_WRITTEN (pfile
, old_written
);
938 /* Handle #include and #import.
939 This function expects to see "fname" or <fname> on the input.
941 The input is normally in part of the output_buffer following
942 CPP_WRITTEN, and will get overwritten by output_line_command.
943 I.e. in input file specification has been popped by handle_directive.
947 do_include (pfile
, keyword
)
949 struct directive
*keyword
;
951 int importing
= (keyword
->type
== T_IMPORT
);
952 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
953 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
954 int before
; /* included before? */
959 enum cpp_token token
;
961 /* Chain of dirs to search */
962 struct include_hash
*ihash
;
963 struct file_name_list
*search_start
;
965 long old_written
= CPP_WRITTEN (pfile
);
969 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
972 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
974 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
977 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
978 && !CPP_OPTIONS (pfile
)->inhibit_warnings
979 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
981 pfile
->import_warning
= 1;
983 "#import is obsolete, use an #ifndef wrapper in the header file");
986 pfile
->parsing_include_directive
++;
987 token
= get_directive_token (pfile
);
988 pfile
->parsing_include_directive
--;
990 if (token
== CPP_STRING
)
992 if (pfile
->token_buffer
[old_written
] == '<')
996 else if (token
== CPP_NAME
)
998 /* Support '#include xyz' like VAX-C. It is taken as
999 '#include <xyz.h>' and generates a warning. */
1001 "`#include filename' is obsolete, use `#include <filename.h>'");
1004 /* Append the missing `.h' to the name. */
1005 CPP_PUTS (pfile
, ".h", 2);
1011 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
1012 CPP_SET_WRITTEN (pfile
, old_written
);
1013 skip_rest_of_line (pfile
);
1017 flen
= CPP_WRITTEN (pfile
) - old_written
;
1018 ftok
= alloca (flen
+ 1);
1019 memcpy (ftok
, pfile
->token_buffer
+ old_written
, flen
);
1022 if (get_directive_token (pfile
) != CPP_VSPACE
)
1024 cpp_error (pfile
, "junk at end of `#include'");
1025 skip_rest_of_line (pfile
);
1028 CPP_SET_WRITTEN (pfile
, old_written
);
1032 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
1036 if (CPP_OPTIONS (pfile
)->dump_includes
)
1037 pass_thru_directive (ftok
,
1040 - ((token
== CPP_NAME
) ? 2 : 0)
1045 if (token
== CPP_STRING
)
1055 for (fp
= CPP_BUFFER (pfile
);
1056 fp
!= CPP_NULL_BUFFER (pfile
);
1057 fp
= CPP_PREV_BUFFER (fp
))
1058 if (fp
->fname
!= NULL
)
1061 if (fp
== CPP_NULL_BUFFER (pfile
))
1063 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
1067 /* For #include_next, skip in the search path past the dir in which the
1068 containing file was found. Treat files specified using an absolute path
1069 as if there are no more directories to search. Treat the primary source
1070 file like any other included source, but generate a warning. */
1071 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
1073 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
1074 search_start
= fp
->ihash
->foundhere
->next
;
1079 cpp_warning (pfile
, "#include_next in primary source file");
1082 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
1085 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
1088 search_start
= fp
->actual_dir
;
1091 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
1097 cpp_error (pfile
, "No include path in which to find %s", ftok
);
1101 fd
= find_include_file (pfile
, ftok
, search_start
, &ihash
, &before
);
1108 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
1109 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
1110 (pfile
->system_include_depth
> 0)))
1112 if (!angle_brackets
)
1113 deps_output (pfile
, ftok
, ' ');
1117 struct file_name_list
*ptr
;
1118 /* If requested as a system header, assume it belongs in
1119 the first system header directory. */
1120 if (CPP_OPTIONS (pfile
)->bracket_include
)
1121 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
1123 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
1125 p
= (char *) alloca (strlen (ptr
->name
)
1126 + strlen (ftok
) + 2);
1127 if (*ptr
->name
!= '\0')
1129 strcpy (p
, ptr
->name
);
1133 deps_output (pfile
, p
, ' ');
1136 /* If -M was specified, and this header file won't be added to
1137 the dependency list, then don't count this as an error,
1138 because we can still produce correct output. Otherwise, we
1139 can't produce correct output, because there may be
1140 dependencies we need inside the missing file, and we don't
1141 know what directory this missing file exists in. */
1142 else if (CPP_PRINT_DEPS (pfile
)
1143 && (CPP_PRINT_DEPS (pfile
)
1144 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1145 cpp_warning (pfile
, "No include path in which to find %s", ftok
);
1147 cpp_error_from_errno (pfile
, ftok
);
1152 /* For -M, add the file to the dependencies on its first inclusion. */
1153 if (!before
&& (CPP_PRINT_DEPS (pfile
)
1154 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
1155 deps_output (pfile
, ihash
->name
, ' ');
1157 /* Handle -H option. */
1158 if (CPP_OPTIONS(pfile
)->print_include_names
)
1160 fp
= CPP_BUFFER (pfile
);
1161 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
1163 fprintf (stderr
, " %s\n", ihash
->name
);
1166 /* Actually process the file */
1169 ihash
->control_macro
= "";
1171 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
1178 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
1180 if (finclude (pfile
, fd
, ihash
))
1182 output_line_command (pfile
, enter_file
);
1183 pfile
->only_seen_white
= 2;
1189 /* Interpret #line command.
1190 Note that the filename string (if any) is treated as if it were an
1191 include filename. That means no escape handling. */
1194 do_line (pfile
, keyword
)
1196 struct directive
*keyword ATTRIBUTE_UNUSED
;
1198 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1200 long old_written
= CPP_WRITTEN (pfile
);
1201 enum file_change_code file_change
= same_file
;
1202 enum cpp_token token
;
1205 token
= get_directive_token (pfile
);
1207 if (token
!= CPP_NUMBER
)
1209 cpp_error (pfile
, "token after `#line' is not an integer");
1210 goto bad_line_directive
;
1213 new_lineno
= strtol (pfile
->token_buffer
+ old_written
, &x
, 10);
1216 cpp_error (pfile
, "token after `#line' is not an integer");
1217 goto bad_line_directive
;
1219 CPP_SET_WRITTEN (pfile
, old_written
);
1221 if (CPP_PEDANTIC (pfile
) && new_lineno
<= 0)
1222 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
1224 token
= get_directive_token (pfile
);
1226 if (token
== CPP_STRING
)
1228 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
+ 1;
1229 U_CHAR
*end_name
= CPP_PWRITTEN (pfile
) - 1;
1230 long num_start
= CPP_WRITTEN (pfile
);
1232 token
= get_directive_token (pfile
);
1233 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
)
1235 U_CHAR
*p
= pfile
->token_buffer
+ num_start
;
1236 if (CPP_PEDANTIC (pfile
))
1237 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
1239 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
1241 cpp_error (pfile
, "invalid format `#line' command");
1242 goto bad_line_directive
;
1245 file_change
= enter_file
;
1247 file_change
= leave_file
;
1249 ip
->system_header_p
= 1;
1250 else /* if (*p == '4') */
1251 ip
->system_header_p
= 2;
1253 CPP_SET_WRITTEN (pfile
, num_start
);
1254 token
= get_directive_token (pfile
);
1255 p
= pfile
->token_buffer
+ num_start
;
1256 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4'))
1258 ip
->system_header_p
= *p
== '3' ? 1 : 2;
1259 token
= get_directive_token (pfile
);
1261 if (token
!= CPP_VSPACE
)
1263 cpp_error (pfile
, "invalid format `#line' command");
1264 goto bad_line_directive
;
1270 if (strcmp (fname
, ip
->nominal_fname
))
1272 char *newname
, *oldname
;
1273 if (!strcmp (fname
, ip
->fname
))
1274 newname
= ip
->fname
;
1275 else if (ip
->last_nominal_fname
1276 && !strcmp (fname
, ip
->last_nominal_fname
))
1277 newname
= ip
->last_nominal_fname
;
1279 newname
= xstrdup (fname
);
1281 oldname
= ip
->nominal_fname
;
1282 ip
->nominal_fname
= newname
;
1284 if (ip
->last_nominal_fname
1285 && ip
->last_nominal_fname
!= oldname
1286 && ip
->last_nominal_fname
!= newname
1287 && ip
->last_nominal_fname
!= ip
->fname
)
1288 free (ip
->last_nominal_fname
);
1290 if (newname
== ip
->fname
)
1291 ip
->last_nominal_fname
= NULL
;
1293 ip
->last_nominal_fname
= oldname
;
1296 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
)
1298 cpp_error (pfile
, "token after `#line %d' is not a string", new_lineno
);
1299 goto bad_line_directive
;
1302 /* The Newline at the end of this line remains to be processed.
1303 To put the next line at the specified line number,
1304 we must store a line number now that is one less. */
1305 ip
->lineno
= new_lineno
- 1;
1306 CPP_SET_WRITTEN (pfile
, old_written
);
1307 output_line_command (pfile
, file_change
);
1311 skip_rest_of_line (pfile
);
1312 CPP_SET_WRITTEN (pfile
, old_written
);
1316 /* Remove the definition of a symbol from the symbol table.
1317 According to the C standard, it is not an error to undef
1318 something that has no definitions. */
1320 do_undef (pfile
, keyword
)
1322 struct directive
*keyword
;
1326 U_CHAR
*buf
, *name
, *limit
;
1328 long here
= CPP_WRITTEN (pfile
);
1329 enum cpp_token token
;
1331 cpp_skip_hspace (pfile
);
1333 if (! is_idstart
[c
])
1335 cpp_error (pfile
, "token after #undef is not an identifier");
1336 skip_rest_of_line (pfile
);
1340 parse_name (pfile
, c
);
1341 buf
= pfile
->token_buffer
+ here
;
1342 limit
= CPP_PWRITTEN(pfile
);
1344 /* Copy out the token so we can pop the token buffer. */
1345 name
= alloca (limit
- buf
+ 1);
1346 bcopy(buf
, name
, limit
- buf
);
1347 name
[limit
- buf
] = '\0';
1349 token
= get_directive_token (pfile
);
1350 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
1352 cpp_pedwarn (pfile
, "junk on line after #undef");
1353 skip_rest_of_line (pfile
);
1356 CPP_SET_WRITTEN (pfile
, here
);
1358 sym_length
= check_macro_name (pfile
, buf
, 0);
1360 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
1362 /* If we are generating additional info for debugging (with -g) we
1363 need to pass through all effective #undef commands. */
1364 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1365 pass_thru_directive (name
, sym_length
, pfile
, keyword
);
1366 if (hp
->type
!= T_MACRO
)
1367 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
1374 /* Wrap do_undef for -U processing. */
1376 cpp_undef (pfile
, macro
)
1380 if (cpp_push_buffer (pfile
, macro
, strlen (macro
)))
1382 do_undef (pfile
, NULL
);
1383 cpp_pop_buffer (pfile
);
1389 * Report an error detected by the program we are processing.
1390 * Use the text of the line in the error message.
1391 * (We use error because it prints the filename & line#.)
1395 do_error (pfile
, keyword
)
1397 struct directive
*keyword ATTRIBUTE_UNUSED
;
1399 long here
= CPP_WRITTEN (pfile
);
1401 copy_rest_of_line (pfile
);
1402 text
= pfile
->token_buffer
+ here
;
1403 SKIP_WHITE_SPACE(text
);
1405 cpp_error (pfile
, "#error %s", text
);
1406 CPP_SET_WRITTEN (pfile
, here
);
1411 * Report a warning detected by the program we are processing.
1412 * Use the text of the line in the warning message, then continue.
1416 do_warning (pfile
, keyword
)
1418 struct directive
*keyword ATTRIBUTE_UNUSED
;
1421 long here
= CPP_WRITTEN(pfile
);
1422 copy_rest_of_line (pfile
);
1423 text
= pfile
->token_buffer
+ here
;
1424 SKIP_WHITE_SPACE(text
);
1426 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1427 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
1429 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
1430 if -pedantic-errors is given, #warning should cause an error. */
1431 cpp_pedwarn (pfile
, "#warning %s", text
);
1432 CPP_SET_WRITTEN (pfile
, here
);
1436 /* Report program identification.
1437 This is not precisely what cccp does with #ident, however I believe
1438 it matches `closely enough' (behavior is identical as long as there
1439 are no macros on the #ident line, which is pathological in my opinion). */
1442 do_ident (pfile
, keyword
)
1444 struct directive
*keyword ATTRIBUTE_UNUSED
;
1446 /* Allow #ident in system headers, since that's not user's fault. */
1447 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
1448 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
1450 CPP_PUTS (pfile
, "#ident ", 7);
1451 cpp_skip_hspace (pfile
);
1452 copy_rest_of_line (pfile
);
1457 /* Just check for some recognized pragmas that need validation here,
1458 and leave the text in the token buffer to be output. */
1461 do_pragma (pfile
, keyword
)
1463 struct directive
*keyword ATTRIBUTE_UNUSED
;
1468 CPP_PUTS (pfile
, "#pragma ", 8);
1469 cpp_skip_hspace (pfile
);
1471 here
= CPP_WRITTEN (pfile
);
1472 copy_rest_of_line (pfile
);
1473 buf
= pfile
->token_buffer
+ here
;
1475 if (!strncmp (buf
, "once", 4))
1477 cpp_buffer
*ip
= NULL
;
1479 /* Allow #pragma once in system headers, since that's not the user's
1481 if (!CPP_BUFFER (pfile
)->system_header_p
)
1482 cpp_warning (pfile
, "`#pragma once' is obsolete");
1484 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1486 if (ip
== CPP_NULL_BUFFER (pfile
))
1488 if (ip
->fname
!= NULL
)
1492 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
1493 cpp_warning (pfile
, "`#pragma once' outside include file");
1495 ip
->ihash
->control_macro
= ""; /* never repeat */
1497 else if (!strncmp (buf
, "implementation", 14))
1499 /* Be quiet about `#pragma implementation' for a file only if it hasn't
1500 been included yet. */
1501 struct include_hash
*ptr
;
1502 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
1503 SKIP_WHITE_SPACE (p
);
1504 if (*p
== '\n' || *p
!= '\"')
1508 p
= (U_CHAR
*) index (fname
, '\"');
1510 fcopy
= alloca (p
- fname
+ 1);
1511 bcopy (fname
, fcopy
, p
- fname
);
1512 fcopy
[p
-fname
] = '\0';
1514 ptr
= include_hash (pfile
, fcopy
, 0);
1517 "`#pragma implementation' for `%s' appears after file is included",
1524 #ifdef SCCS_DIRECTIVE
1525 /* Just ignore #sccs, on systems where we define it at all. */
1528 do_sccs (pfile
, keyword
)
1530 struct directive
*keyword ATTRIBUTE_UNUSED
;
1532 if (CPP_PEDANTIC (pfile
))
1533 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
1534 skip_rest_of_line (pfile
);
1540 * handle #if command by
1541 * 1) inserting special `defined' keyword into the hash table
1542 * that gets turned into 0 or 1 by special_symbol (thus,
1543 * if the luser has a symbol called `defined' already, it won't
1544 * work inside the #if command)
1545 * 2) rescan the input into a temporary output buffer
1546 * 3) pass the output buffer to the yacc parser and collect a value
1547 * 4) clean up the mess left from steps 1 and 2.
1548 * 5) call conditional_skip to skip til the next #endif (etc.),
1549 * or not, depending on the value from step 3.
1553 do_if (pfile
, keyword
)
1555 struct directive
*keyword ATTRIBUTE_UNUSED
;
1557 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1558 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
1563 * handle a #elif directive by not changing if_stack either.
1564 * see the comment above do_else.
1568 do_elif (pfile
, keyword
)
1570 struct directive
*keyword ATTRIBUTE_UNUSED
;
1572 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1573 cpp_error (pfile
, "`#elif' not within a conditional");
1576 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1577 cpp_error (pfile
, "`#elif' after `#else'");
1579 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1581 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
1582 && strcmp (pfile
->if_stack
->fname
,
1583 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
1584 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1585 fprintf (stderr
, ")\n");
1587 pfile
->if_stack
->type
= T_ELIF
;
1590 if (pfile
->if_stack
->if_succeeded
)
1591 skip_if_group (pfile
);
1593 HOST_WIDEST_INT value
= eval_if_expression (pfile
);
1595 skip_if_group (pfile
);
1597 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1598 output_line_command (pfile
, same_file
);
1605 * evaluate a #if expression in BUF, of length LENGTH,
1606 * then parse the result as a C expression and return the value as an int.
1609 static HOST_WIDEST_INT
1610 eval_if_expression (pfile
)
1613 HOST_WIDEST_INT value
;
1614 long old_written
= CPP_WRITTEN (pfile
);
1616 pfile
->pcp_inside_if
= 1;
1617 value
= cpp_parse_expr (pfile
);
1618 pfile
->pcp_inside_if
= 0;
1620 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1626 * routine to handle ifdef/ifndef. Try to look up the symbol,
1627 * then do or don't skip to the #endif/#else/#elif depending
1628 * on what directive is actually being processed.
1632 do_xifdef (pfile
, keyword
)
1634 struct directive
*keyword
;
1637 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1640 enum cpp_token token
;
1641 int start_of_file
= 0;
1642 U_CHAR
*control_macro
= 0;
1643 int old_written
= CPP_WRITTEN (pfile
);
1645 /* Detect a #ifndef at start of file (not counting comments). */
1646 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
1647 start_of_file
= pfile
->only_seen_white
== 2;
1649 pfile
->no_macro_expand
++;
1650 token
= get_directive_token (pfile
);
1651 pfile
->no_macro_expand
--;
1653 ident
= pfile
->token_buffer
+ old_written
;
1654 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
1655 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
1657 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
1659 skip
= (keyword
->type
== T_IFDEF
);
1660 if (! CPP_TRADITIONAL (pfile
))
1661 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
1663 else if (token
== CPP_NAME
)
1665 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
1666 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
1667 if (start_of_file
&& !skip
)
1669 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
1670 bcopy (ident
, control_macro
, ident_length
+ 1);
1675 skip
= (keyword
->type
== T_IFDEF
);
1676 if (! CPP_TRADITIONAL (pfile
))
1677 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
1680 if (!CPP_TRADITIONAL (pfile
))
1682 cpp_skip_hspace (pfile
);
1684 if (c
!= EOF
&& c
!= '\n')
1685 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
1687 skip_rest_of_line (pfile
);
1691 /* Output a precondition for this macro. */
1692 if (hp
&& hp
->value
.defn
->predefined
)
1693 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
1696 fprintf (pcp_outfile
, "#undef ");
1697 while (is_idchar
[*cp
]) /* Ick! */
1698 fputc (*cp
++, pcp_outfile
);
1699 putc ('\n', pcp_outfile
);
1703 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
1707 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
1708 If this is a #ifndef starting at the beginning of a file,
1709 CONTROL_MACRO is the macro name tested by the #ifndef.
1710 Otherwise, CONTROL_MACRO is 0. */
1713 conditional_skip (pfile
, skip
, type
, control_macro
)
1716 enum node_type type
;
1717 U_CHAR
*control_macro
;
1719 IF_STACK_FRAME
*temp
;
1721 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
1722 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
1724 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
1726 temp
->next
= pfile
->if_stack
;
1727 temp
->control_macro
= control_macro
;
1728 pfile
->if_stack
= temp
;
1730 pfile
->if_stack
->type
= type
;
1733 skip_if_group (pfile
);
1736 ++pfile
->if_stack
->if_succeeded
;
1737 output_line_command (pfile
, same_file
);
1741 /* Subroutine of skip_if_group. Examine one preprocessing directive and
1742 return 0 if skipping should continue, 1 if it should halt. Also
1743 adjusts the if_stack as appropriate.
1744 The `#' has been read, but not the identifier. */
1747 consider_directive_while_skipping (pfile
, stack
)
1749 IF_STACK_FRAME
*stack
;
1751 long ident_len
, ident
;
1752 struct directive
*kt
;
1753 IF_STACK_FRAME
*temp
;
1755 cpp_skip_hspace (pfile
);
1757 ident
= CPP_WRITTEN (pfile
);
1758 parse_name (pfile
, GETC());
1759 ident_len
= CPP_WRITTEN (pfile
) - ident
;
1761 CPP_SET_WRITTEN (pfile
, ident
);
1763 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
1764 if (kt
->length
== ident_len
1765 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
1771 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
1772 temp
->next
= pfile
->if_stack
;
1773 pfile
->if_stack
= temp
;
1774 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
1775 temp
->type
= kt
->type
;
1779 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
1780 validate_else (pfile
, "#else");
1783 if (pfile
->if_stack
->type
== T_ELSE
)
1784 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
1786 if (pfile
->if_stack
== stack
)
1790 pfile
->if_stack
->type
= kt
->type
;
1795 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
1796 validate_else (pfile
, "#endif");
1798 if (pfile
->if_stack
== stack
)
1801 temp
= pfile
->if_stack
;
1802 pfile
->if_stack
= temp
->next
;
1810 /* Don't let erroneous code go by. */
1811 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
1812 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
1816 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
1817 * leaves input ptr at the sharp sign found.
1820 skip_if_group (pfile
)
1824 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
1825 U_CHAR
*beg_of_line
;
1828 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1830 CPP_PUTS (pfile
, "#failed\n", 8);
1832 output_line_command (pfile
, same_file
);
1835 old_written
= CPP_WRITTEN (pfile
);
1839 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
1841 if (! CPP_TRADITIONAL (pfile
))
1842 cpp_skip_hspace (pfile
);
1846 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1847 CPP_PUTC (pfile
, c
);
1848 CPP_BUMP_LINE (pfile
);
1853 if (consider_directive_while_skipping (pfile
, save_if_stack
))
1857 return; /* Caller will issue error. */
1860 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1862 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
1863 copy_rest_of_line (pfile
);
1867 copy_rest_of_line (pfile
);
1868 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
1873 return; /* Caller will issue error. */
1877 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1879 CPP_PUTC (pfile
, c
);
1882 CPP_BUMP_LINE (pfile
);
1886 /* Back up to the beginning of this line. Caller will process the
1888 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
1889 pfile
->only_seen_white
= 1;
1890 if (CPP_OPTIONS (pfile
)->output_conditionals
)
1892 CPP_PUTS (pfile
, "#endfailed\n", 11);
1898 * handle a #else directive. Do this by just continuing processing
1899 * without changing if_stack ; this is so that the error message
1900 * for missing #endif's etc. will point to the original #if. It
1901 * is possible that something different would be better.
1905 do_else (pfile
, keyword
)
1907 struct directive
*keyword ATTRIBUTE_UNUSED
;
1909 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1911 if (CPP_PEDANTIC (pfile
))
1912 validate_else (pfile
, "#else");
1913 skip_rest_of_line (pfile
);
1915 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
1916 cpp_error (pfile
, "`#else' not within a conditional");
1919 /* #ifndef can't have its special treatment for containing the whole file
1920 if it has a #else clause. */
1921 pfile
->if_stack
->control_macro
= 0;
1923 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
1924 cpp_error (pfile
, "`#else' after `#else'");
1925 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
1926 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
1927 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
1928 fprintf (stderr
, ")\n");
1930 pfile
->if_stack
->type
= T_ELSE
;
1933 if (pfile
->if_stack
->if_succeeded
)
1934 skip_if_group (pfile
);
1936 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
1937 output_line_command (pfile
, same_file
);
1943 * unstack after #endif command
1947 do_endif (pfile
, keyword
)
1949 struct directive
*keyword ATTRIBUTE_UNUSED
;
1951 if (CPP_PEDANTIC (pfile
))
1952 validate_else (pfile
, "#endif");
1953 skip_rest_of_line (pfile
);
1955 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
1956 cpp_error (pfile
, "unbalanced `#endif'");
1959 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
1960 pfile
->if_stack
= temp
->next
;
1961 if (temp
->control_macro
!= 0)
1963 /* This #endif matched a #ifndef at the start of the file.
1964 See if it is at the end of the file. */
1967 parse_set_mark (pfile
);
1971 cpp_skip_hspace (pfile
);
1976 parse_goto_mark (pfile
);
1980 /* This #endif ends a #ifndef
1981 that contains all of the file (aside from whitespace).
1982 Arrange not to include the file again
1983 if the macro that was tested is defined. */
1984 struct cpp_buffer
*ip
;
1985 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
1986 if (ip
->fname
!= NULL
)
1988 ip
->ihash
->control_macro
= (char *) temp
->control_macro
;
1992 output_line_command (pfile
, same_file
);
1997 /* When an #else or #endif is found while skipping failed conditional,
1998 if -pedantic was specified, this is called to warn about text after
1999 the command name. P points to the first char after the command name. */
2002 validate_else (pfile
, directive
)
2007 cpp_skip_hspace (pfile
);
2009 if (c
!= EOF
&& c
!= '\n')
2011 "text following `%s' violates ANSI standard", directive
);
2014 /* Get the next token, and add it to the text in pfile->token_buffer.
2015 Return the kind of token we got. */
2018 cpp_get_token (pfile
)
2021 register int c
, c2
, c3
;
2022 enum cpp_token token
;
2023 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
2030 if (CPP_BUFFER (pfile
)->seen_eof
)
2032 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) == CPP_NULL_BUFFER (pfile
))
2035 cpp_pop_buffer (pfile
);
2040 cpp_buffer
*next_buf
2041 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
2042 CPP_BUFFER (pfile
)->seen_eof
= 1;
2043 if (CPP_BUFFER (pfile
)->nominal_fname
2044 && next_buf
!= CPP_NULL_BUFFER (pfile
))
2046 /* We're about to return from an #include file.
2047 Emit #line information now (as part of the CPP_POP) result.
2048 But the #line refers to the file we will pop to. */
2049 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
2050 CPP_BUFFER (pfile
) = next_buf
;
2051 pfile
->input_stack_listing_current
= 0;
2052 output_line_command (pfile
, leave_file
);
2053 CPP_BUFFER (pfile
) = cur_buffer
;
2063 if (PEEKC () == '=')
2067 if (opts
->put_out_comments
)
2068 c
= copy_comment (pfile
, c
);
2070 c
= skip_comment (pfile
, c
);
2076 /* Comments are equivalent to spaces.
2077 For -traditional, a comment is equivalent to nothing. */
2078 if (opts
->traditional
|| opts
->put_out_comments
)
2082 CPP_PUTC (pfile
, c
);
2086 if (opts
->for_lint
) {
2089 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
2091 if (lintcmd
!= NULL
) {
2092 /* I believe it is always safe to emit this newline: */
2094 bcopy ("#pragma lint ", (char *) obp
, 13);
2096 bcopy (lintcmd
, (char *) obp
, cmdlen
);
2101 bcopy (argbp
, (char *) obp
, arglen
);
2105 /* OK, now bring us back to the state we were in before we entered
2106 this branch. We need #line because the newline for the pragma
2107 could mess things up. */
2108 output_line_command (pfile
, same_file
);
2109 *(obp
++) = ' '; /* just in case, if comments are copied thru */
2117 /* If this is expanding a macro definition, don't recognize
2118 preprocessor directives. */
2121 /* If this is expand_into_temp_buffer, recognize them
2122 only after an actual newline at this level,
2123 not at the beginning of the input level. */
2124 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
2130 if (!pfile
->only_seen_white
)
2132 if (handle_directive (pfile
))
2133 return CPP_DIRECTIVE
;
2134 pfile
->only_seen_white
= 0;
2140 parse_string (pfile
, c
);
2141 pfile
->only_seen_white
= 0;
2142 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
2145 if (!opts
->dollars_in_ident
)
2150 if (opts
->cplusplus
&& PEEKC () == ':')
2158 if (c2
== c
|| c2
== '=')
2167 if (PEEKC () == '=')
2173 if (c2
== '-' && opts
->chill
)
2174 goto comment
; /* Chill style comment */
2175 if (c2
== '-' || c2
== '=' || c2
== '>')
2180 if (pfile
->parsing_include_directive
)
2184 CPP_PUTC (pfile
, c
);
2188 if (c
== '\n' || c
== EOF
)
2191 "missing '>' in `#include <FILENAME>'");
2196 if (!CPP_BUFFER (pfile
)->has_escapes
)
2198 /* Backslash newline is replaced by nothing. */
2199 CPP_ADJUST_WRITTEN (pfile
, -1);
2200 CPP_BUMP_LINE (pfile
);
2204 /* We might conceivably get \r- or \r<space> in
2205 here. Just delete 'em. */
2207 if (d
!= '-' && d
!= ' ')
2209 "internal error: unrecognized escape \\r%c",
2211 CPP_ADJUST_WRITTEN (pfile
, -1);
2217 /* else fall through */
2225 CPP_RESERVE (pfile
, 4);
2226 CPP_PUTC (pfile
, c
);
2227 CPP_PUTC (pfile
, c2
);
2230 CPP_PUTC_Q (pfile
, GETC ());
2231 CPP_NUL_TERMINATE_Q (pfile
);
2232 pfile
->only_seen_white
= 0;
2239 CPP_RESERVE(pfile
, 2);
2240 CPP_PUTC_Q (pfile
, '.');
2244 if (c2
== '.' && PEEKN(1) == '.')
2246 CPP_RESERVE(pfile
, 4);
2247 CPP_PUTC_Q (pfile
, '.');
2248 CPP_PUTC_Q (pfile
, '.');
2249 CPP_PUTC_Q (pfile
, '.');
2251 CPP_NUL_TERMINATE_Q (pfile
);
2252 pfile
->only_seen_white
= 0;
2259 pfile
->only_seen_white
= 0;
2260 CPP_RESERVE(pfile
, 3);
2261 CPP_PUTC_Q (pfile
, c
);
2262 CPP_PUTC_Q (pfile
, GETC ());
2263 CPP_NUL_TERMINATE_Q (pfile
);
2268 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
2270 CPP_PUTC (pfile
, c
);
2276 case '0': case '1': case '2': case '3': case '4':
2277 case '5': case '6': case '7': case '8': case '9':
2282 CPP_RESERVE (pfile
, 2);
2283 CPP_PUTC_Q (pfile
, c
);
2287 if (!is_idchar
[c
] && c
!= '.'
2288 && ((c2
!= 'e' && c2
!= 'E'
2289 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
2290 || (c
!= '+' && c
!= '-')))
2295 CPP_NUL_TERMINATE_Q (pfile
);
2296 pfile
->only_seen_white
= 0;
2298 case 'b': case 'c': case 'd': case 'h': case 'o':
2299 case 'B': case 'C': case 'D': case 'H': case 'O':
2300 if (opts
->chill
&& PEEKC () == '\'')
2302 pfile
->only_seen_white
= 0;
2303 CPP_RESERVE (pfile
, 2);
2304 CPP_PUTC_Q (pfile
, c
);
2305 CPP_PUTC_Q (pfile
, '\'');
2311 goto chill_number_eof
;
2314 CPP_PUTC (pfile
, c
);
2318 CPP_RESERVE (pfile
, 2);
2319 CPP_PUTC_Q (pfile
, c
);
2320 CPP_NUL_TERMINATE_Q (pfile
);
2327 CPP_NUL_TERMINATE (pfile
);
2334 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
2335 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
2336 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
2337 case 'x': case 'y': case 'z':
2338 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
2339 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
2340 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2345 unsigned char *ident
;
2346 int before_name_written
= CPP_WRITTEN (pfile
);
2348 parse_name (pfile
, c
);
2349 pfile
->only_seen_white
= 0;
2350 if (pfile
->no_macro_expand
)
2352 ident
= pfile
->token_buffer
+ before_name_written
;
2353 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
2354 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
2357 if (hp
->type
== T_DISABLED
)
2359 if (pfile
->output_escapes
)
2360 { /* Return "\r-IDENT", followed by '\0'. */
2362 CPP_RESERVE (pfile
, 3);
2363 ident
= pfile
->token_buffer
+ before_name_written
;
2364 CPP_ADJUST_WRITTEN (pfile
, 2);
2365 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
2372 /* If macro wants an arglist, verify that a '(' follows.
2373 first skip all whitespace, copying it to the output
2374 after the macro name. Then, if there is no '(',
2375 decide this is not a macro call and leave things that way. */
2376 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
2378 int is_macro_call
, macbuf_whitespace
= 0;
2380 parse_set_mark (pfile
);
2383 cpp_skip_hspace (pfile
);
2385 is_macro_call
= c
== '(';
2394 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2396 if (CPP_BUFFER (pfile
)->mark
!=
2397 (CPP_BUFFER (pfile
)->cur
2398 - CPP_BUFFER (pfile
)->buf
))
2399 macbuf_whitespace
= 1;
2401 /* The mark goes away automatically when
2402 the buffer is popped. */
2403 cpp_pop_buffer (pfile
);
2404 parse_set_mark (pfile
);
2412 parse_goto_mark (pfile
);
2413 if (macbuf_whitespace
)
2414 CPP_PUTC (pfile
, ' ');
2417 parse_clear_mark (pfile
);
2421 /* This is now known to be a macro call.
2422 Expand the macro, reading arguments as needed,
2423 and push the expansion on the input stack. */
2424 macroexpand (pfile
, hp
);
2425 CPP_SET_WRITTEN (pfile
, before_name_written
);
2429 case ' ': case '\t': case '\v':
2432 CPP_PUTC (pfile
, c
);
2434 if (c
== EOF
|| !is_hor_space
[c
])
2441 if (CPP_BUFFER (pfile
)->has_escapes
)
2446 if (pfile
->output_escapes
)
2447 CPP_PUTS (pfile
, "\r-", 2);
2448 parse_name (pfile
, GETC ());
2453 CPP_RESERVE (pfile
, 2);
2454 if (pfile
->output_escapes
)
2455 CPP_PUTC_Q (pfile
, '\r');
2456 CPP_PUTC_Q (pfile
, c
);
2462 "internal error: unrecognized escape \\r%c", c
);
2468 /* Backslash newline is ignored. */
2469 CPP_BUMP_LINE (pfile
);
2474 CPP_PUTC (pfile
, c
);
2475 if (pfile
->only_seen_white
== 0)
2476 pfile
->only_seen_white
= 1;
2477 CPP_BUMP_LINE (pfile
);
2478 if (! CPP_OPTIONS (pfile
)->no_line_commands
)
2481 if (CPP_BUFFER (pfile
)->lineno
!= pfile
->lineno
)
2482 output_line_command (pfile
, same_file
);
2486 case '(': token
= CPP_LPAREN
; goto char1
;
2487 case ')': token
= CPP_RPAREN
; goto char1
;
2488 case '{': token
= CPP_LBRACE
; goto char1
;
2489 case '}': token
= CPP_RBRACE
; goto char1
;
2490 case ',': token
= CPP_COMMA
; goto char1
;
2491 case ';': token
= CPP_SEMICOLON
; goto char1
;
2497 pfile
->only_seen_white
= 0;
2498 CPP_PUTC (pfile
, c
);
2504 /* Like cpp_get_token, but skip spaces and comments. */
2507 cpp_get_non_space_token (pfile
)
2510 int old_written
= CPP_WRITTEN (pfile
);
2513 enum cpp_token token
= cpp_get_token (pfile
);
2514 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
2515 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
2517 CPP_SET_WRITTEN (pfile
, old_written
);
2521 /* Parse an identifier starting with C. */
2524 parse_name (pfile
, c
)
2536 if (c
== '$' && CPP_PEDANTIC (pfile
))
2537 cpp_pedwarn (pfile
, "`$' in identifier");
2539 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
2540 CPP_PUTC_Q (pfile
, c
);
2545 CPP_NUL_TERMINATE_Q (pfile
);
2549 /* Parse a string starting with C. A single quoted string is treated
2550 like a double -- some programs (e.g., troff) are perverse this way.
2551 (However, a single quoted string is not allowed to extend over
2554 parse_string (pfile
, c
)
2558 long start_line
, start_column
;
2560 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2562 CPP_PUTC (pfile
, c
);
2568 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2570 /* try harder: this string crosses a macro expansion
2571 boundary. This can happen naturally if -traditional.
2572 Otherwise, only -D can make a macro with an unmatched
2574 cpp_pop_buffer (pfile
);
2577 if (!CPP_TRADITIONAL (pfile
))
2579 cpp_error_with_line (pfile
, start_line
, start_column
,
2580 "unterminated string or character constant");
2581 if (pfile
->multiline_string_line
!= start_line
2582 && pfile
->multiline_string_line
!= 0)
2583 cpp_error_with_line (pfile
,
2584 pfile
->multiline_string_line
, -1,
2585 "possible real start of unterminated constant");
2586 pfile
->multiline_string_line
= 0;
2590 CPP_PUTC (pfile
, cc
);
2594 CPP_BUMP_LINE (pfile
);
2596 /* Traditionally, end of line ends a string constant with
2598 if (CPP_TRADITIONAL (pfile
))
2600 /* Character constants may not extend over multiple lines. */
2603 cpp_error_with_line (pfile
, start_line
, start_column
,
2604 "unterminated character constant");
2607 if (CPP_PEDANTIC (pfile
) && pfile
->multiline_string_line
== 0)
2609 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
2610 "string constant runs past end of line");
2612 if (pfile
->multiline_string_line
== 0)
2613 pfile
->multiline_string_line
= start_line
;
2617 CPP_ADJUST_WRITTEN (pfile
, -1);
2618 if (CPP_BUFFER (pfile
)->has_escapes
)
2621 "internal error: \\r escape inside string constant");
2625 /* Backslash newline is replaced by nothing at all. */
2626 CPP_BUMP_LINE (pfile
);
2632 CPP_PUTC (pfile
, cc
);
2644 /* Read an assertion into the token buffer, converting to
2645 canonical form: `#predicate(a n swe r)' The next non-whitespace
2646 character to read should be the first letter of the predicate.
2647 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
2648 with answer (see callers for why). In case of 0, an error has been
2651 parse_assertion (pfile
)
2655 cpp_skip_hspace (pfile
);
2657 if (! is_idstart
[c
])
2659 cpp_error (pfile
, "assertion predicate is not an identifier");
2662 CPP_PUTC(pfile
, '#');
2664 parse_name(pfile
, c
);
2669 if (is_hor_space
[c
] || c
== '\r')
2670 cpp_skip_hspace (pfile
);
2676 CPP_PUTC(pfile
, '(');
2679 while ((c
= GETC()) != ')')
2681 if (is_hor_space
[c
])
2685 CPP_PUTC(pfile
, ' ');
2689 else if (c
== '\n' || c
== EOF
)
2691 if (c
== '\n') FORWARD(-1);
2692 cpp_error (pfile
, "un-terminated assertion answer");
2696 /* \r cannot be a macro escape here. */
2697 CPP_BUMP_LINE (pfile
);
2700 CPP_PUTC (pfile
, c
);
2705 if (pfile
->limit
[-1] == ' ')
2706 pfile
->limit
[-1] = ')';
2707 else if (pfile
->limit
[-1] == '(')
2709 cpp_error (pfile
, "empty token sequence in assertion");
2713 CPP_PUTC (pfile
, ')');
2715 CPP_NUL_TERMINATE (pfile
);
2720 do_assert (pfile
, keyword
)
2722 struct directive
*keyword ATTRIBUTE_UNUSED
;
2726 HASHNODE
*base
, *this;
2727 int baselen
, thislen
;
2729 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2730 && !CPP_BUFFER (pfile
)->system_header_p
)
2731 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
2733 cpp_skip_hspace (pfile
);
2734 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2735 ret
= parse_assertion (pfile
);
2740 cpp_error (pfile
, "missing token-sequence in `#assert'");
2744 cpp_skip_hspace (pfile
);
2746 if (c
!= EOF
&& c
!= '\n')
2748 cpp_error (pfile
, "junk at end of `#assert'");
2752 thislen
= strlen (sym
);
2753 baselen
= index (sym
, '(') - sym
;
2754 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2757 cpp_warning (pfile
, "`%s' re-asserted", sym
);
2761 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
2763 base
= cpp_install (pfile
, sym
, baselen
, T_ASSERT
, 0, -1);
2764 else if (base
->type
!= T_ASSERT
)
2766 /* Token clash - but with what?! */
2768 "cpp internal error: base->type != T_ASSERT in do_assert");
2772 this = cpp_install (pfile
, sym
, thislen
, T_ASSERT
,
2773 (char *)base
->value
.aschain
, -1);
2774 base
->value
.aschain
= this;
2776 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2780 skip_rest_of_line (pfile
);
2781 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2786 do_unassert (pfile
, keyword
)
2788 struct directive
*keyword ATTRIBUTE_UNUSED
;
2792 long baselen
, thislen
;
2793 HASHNODE
*base
, *this, *next
;
2795 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
2796 && !CPP_BUFFER (pfile
)->system_header_p
)
2797 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
2799 cpp_skip_hspace (pfile
);
2801 sym
= (char *) CPP_PWRITTEN (pfile
); /* remember where it starts */
2802 ret
= parse_assertion (pfile
);
2806 cpp_skip_hspace (pfile
);
2808 if (c
!= EOF
&& c
!= '\n')
2809 cpp_error (pfile
, "junk at end of `#unassert'");
2811 thislen
= strlen (sym
);
2814 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
2816 goto error
; /* It isn't an error to #undef what isn't #defined,
2817 so it isn't an error to #unassert what isn't
2818 #asserted either. */
2820 for (this = base
->value
.aschain
; this; this = next
)
2822 next
= this->value
.aschain
;
2823 delete_macro (this);
2825 delete_macro (base
);
2829 baselen
= index (sym
, '(') - sym
;
2830 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
2831 if (! base
) goto error
;
2832 this = cpp_lookup (pfile
, sym
, thislen
, -1);
2833 if (! this) goto error
;
2836 while (next
->value
.aschain
!= this)
2837 next
= next
->value
.aschain
;
2839 next
->value
.aschain
= this->value
.aschain
;
2840 delete_macro (this);
2842 if (base
->value
.aschain
== NULL
)
2843 delete_macro (base
); /* Last answer for this predicate deleted. */
2846 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2849 skip_rest_of_line (pfile
);
2850 pfile
->limit
= (unsigned char *) sym
; /* Pop */
2854 /* Process STR as if it appeared as the body of an #unassert. */
2856 cpp_unassert (pfile
, str
)
2860 if (cpp_push_buffer (pfile
, str
, strlen (str
)) != NULL
)
2862 do_assert (pfile
, NULL
);
2863 cpp_pop_buffer (pfile
);
2868 cpp_read_check_assertion (pfile
)
2871 U_CHAR
*name
= CPP_PWRITTEN (pfile
);
2875 FORWARD (1); /* Skip '#' */
2876 cpp_skip_hspace (pfile
);
2877 if (! parse_assertion (pfile
))
2881 hp
= cpp_lookup (pfile
, name
, CPP_PWRITTEN (pfile
) - name
, -1);
2885 pfile
->limit
= name
;
2889 /* Remember the current position of PFILE. */
2892 parse_set_mark (pfile
)
2895 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2898 "cpp internal error: ip->mark != -1 in parse_set_mark");
2900 ip
->mark
= ip
->cur
- ip
->buf
;
2903 /* Clear the current mark - we no longer need it. */
2906 parse_clear_mark (pfile
)
2909 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2912 "cpp internal error: ip->mark == -1 in parse_clear_mark");
2917 /* Backup the current position of PFILE to that saved in its mark,
2918 and clear the mark. */
2921 parse_goto_mark (pfile
)
2924 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
2927 "cpp internal error: ip->mark == -1 in parse_goto_mark");
2929 ip
->cur
= ip
->buf
+ ip
->mark
;
2934 cpp_print_file_and_line (pfile
)
2937 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
2942 cpp_buf_line_and_col (ip
, &line
, &col
);
2943 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
2944 line
, pfile
->show_column
? col
: -1);
2949 v_cpp_error (pfile
, msgid
, ap
)
2954 cpp_print_containing_files (pfile
);
2955 cpp_print_file_and_line (pfile
);
2956 v_cpp_message (pfile
, 1, msgid
, ap
);
2960 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
2962 #ifndef ANSI_PROTOTYPES
2968 VA_START(ap
, msgid
);
2970 #ifndef ANSI_PROTOTYPES
2971 pfile
= va_arg (ap
, cpp_reader
*);
2972 msgid
= va_arg (ap
, const char *);
2975 v_cpp_error (pfile
, msgid
, ap
);
2979 /* Print error message but don't count it. */
2982 v_cpp_warning (pfile
, msgid
, ap
)
2987 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
2990 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
2993 cpp_print_containing_files (pfile
);
2994 cpp_print_file_and_line (pfile
);
2995 v_cpp_message (pfile
, 0, msgid
, ap
);
2999 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3001 #ifndef ANSI_PROTOTYPES
3007 VA_START (ap
, msgid
);
3009 #ifndef ANSI_PROTOTYPES
3010 pfile
= va_arg (ap
, cpp_reader
*);
3011 msgid
= va_arg (ap
, const char *);
3014 v_cpp_warning (pfile
, msgid
, ap
);
3018 /* Print an error message and maybe count it. */
3021 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msgid
, ...))
3023 #ifndef ANSI_PROTOTYPES
3029 VA_START (ap
, msgid
);
3031 #ifndef ANSI_PROTOTYPES
3032 pfile
= va_arg (ap
, cpp_reader
*);
3033 msgid
= va_arg (ap
, const char *);
3036 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3037 v_cpp_error (pfile
, msgid
, ap
);
3039 v_cpp_warning (pfile
, msgid
, ap
);
3044 v_cpp_error_with_line (pfile
, line
, column
, msgid
, ap
)
3051 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3053 cpp_print_containing_files (pfile
);
3056 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3058 v_cpp_message (pfile
, 1, msgid
, ap
);
3062 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3063 const char *msgid
, ...))
3065 #ifndef ANSI_PROTOTYPES
3073 VA_START (ap
, msgid
);
3075 #ifndef ANSI_PROTOTYPES
3076 pfile
= va_arg (ap
, cpp_reader
*);
3077 line
= va_arg (ap
, int);
3078 column
= va_arg (ap
, int);
3079 msgid
= va_arg (ap
, const char *);
3082 v_cpp_error_with_line(pfile
, line
, column
, msgid
, ap
);
3087 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
)
3096 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
3099 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
3102 cpp_print_containing_files (pfile
);
3104 ip
= cpp_file_buffer (pfile
);
3107 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
3109 v_cpp_message (pfile
, 0, msgid
, ap
);
3113 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3114 const char *msgid
, ...))
3116 #ifndef ANSI_PROTOTYPES
3124 VA_START (ap
, msgid
);
3126 #ifndef ANSI_PROTOTYPES
3127 pfile
= va_arg (ap
, cpp_reader
*);
3128 line
= va_arg (ap
, int);
3129 column
= va_arg (ap
, int);
3130 msgid
= va_arg (ap
, const char *);
3133 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3138 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
,
3139 const char *msgid
, ...))
3141 #ifndef ANSI_PROTOTYPES
3149 VA_START (ap
, msgid
);
3151 #ifndef ANSI_PROTOTYPES
3152 pfile
= va_arg (ap
, cpp_reader
*);
3153 line
= va_arg (ap
, int);
3154 column
= va_arg (ap
, int);
3155 msgid
= va_arg (ap
, const char *);
3158 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
3159 v_cpp_error_with_line (pfile
, column
, line
, msgid
, ap
);
3161 v_cpp_warning_with_line (pfile
, line
, column
, msgid
, ap
);
3165 /* Report a warning (or an error if pedantic_errors)
3166 giving specified file name and line number, not current. */
3169 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, char *file
, int line
,
3170 const char *msgid
, ...))
3172 #ifndef ANSI_PROTOTYPES
3180 VA_START (ap
, msgid
);
3182 #ifndef ANSI_PROTOTYPES
3183 pfile
= va_arg (ap
, cpp_reader
*);
3184 file
= va_arg (ap
, char *);
3185 line
= va_arg (ap
, int);
3186 msgid
= va_arg (ap
, const char *);
3189 if (!CPP_OPTIONS (pfile
)->pedantic_errors
3190 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
3193 cpp_file_line_for_message (pfile
, file
, line
, -1);
3194 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msgid
, ap
);
3198 /* my_strerror - return the descriptive text associated with an
3202 my_strerror (errnum
)
3208 #ifndef HAVE_STRERROR
3209 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
3211 result
= strerror (errnum
);
3214 /* VAXCRTL's strerror() takes an optional second argument, which only
3215 matters when the first argument is EVMSERR. However, it's simplest
3216 just to pass it unconditionally. `vaxc$errno' is declared in
3217 <errno.h>, and maintained by the library in parallel with `errno'.
3218 We assume that caller's `errnum' either matches the last setting of
3219 `errno' by the library or else does not have the value `EVMSERR'. */
3221 result
= strerror (errnum
, vaxc$errno
);
3225 result
= "errno = ?";
3230 /* Error including a message from `errno'. */
3233 cpp_error_from_errno (pfile
, name
)
3237 cpp_message_from_errno (pfile
, 1, name
);
3241 cpp_message_from_errno (pfile
, is_error
, name
)
3247 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
3249 cpp_print_containing_files (pfile
);
3252 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
3254 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
3258 cpp_perror_with_name (pfile
, name
)
3262 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
3266 * No pre-compiled header file support.
3268 * Possibly different enum token codes for each C/C++ token.
3270 * Find and cleanup remaining uses of static variables,
3272 * Support -dM flag (dump_all_macros).
3274 * Support for_lint flag.