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. */
30 #ifdef HAVE_SYS_TIMES_H
31 #include <sys/times.h>
34 #ifdef HAVE_SYS_RESOURCE_H
35 # include <sys/resource.h>
43 #ifndef GET_ENV_PATH_LIST
44 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
47 /* By default, colon separates directories in a path. */
48 #ifndef PATH_SEPARATOR
49 #define PATH_SEPARATOR ':'
52 #ifndef STANDARD_INCLUDE_DIR
53 #define STANDARD_INCLUDE_DIR "/usr/include"
56 /* Symbols to predefine. */
59 static char *predefs
= CPP_PREDEFINES
;
61 static char *predefs
= "";
64 /* We let tm.h override the types used here, to handle trivial differences
65 such as the choice of unsigned int or long unsigned int for size_t.
66 When machines start needing nontrivial differences in the size type,
67 it would be best to do something here to figure out automatically
68 from other information what type to use. */
70 /* The string value for __SIZE_TYPE__. */
73 #define SIZE_TYPE "long unsigned int"
76 /* The string value for __PTRDIFF_TYPE__. */
79 #define PTRDIFF_TYPE "long int"
82 /* The string value for __WCHAR_TYPE__. */
85 #define WCHAR_TYPE "int"
87 #define CPP_WCHAR_TYPE(PFILE) \
88 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
90 /* The string value for __USER_LABEL_PREFIX__ */
92 #ifndef USER_LABEL_PREFIX
93 #define USER_LABEL_PREFIX ""
96 /* The string value for __REGISTER_PREFIX__ */
98 #ifndef REGISTER_PREFIX
99 #define REGISTER_PREFIX ""
102 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
103 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
105 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
106 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
107 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
108 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
109 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
110 (Note that it is false while we're expanding marco *arguments*.) */
111 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
113 /* Move all backslash-newline pairs out of embarrassing places.
114 Exchange all such pairs following BP
115 with any potentially-embarrassing characters that follow them.
116 Potentially-embarrassing characters are / and *
117 (because a backslash-newline inside a comment delimiter
118 would cause it not to be recognized). */
120 #define NEWLINE_FIX \
121 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
123 /* Same, but assume we've already read the potential '\\' into C. */
124 #define NEWLINE_FIX1(C) do { \
125 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
129 struct cpp_pending
*next
;
134 /* Forward declarations. */
136 extern void cpp_hash_cleanup
PARAMS ((cpp_reader
*));
138 static char *my_strerror
PROTO ((int));
139 static void make_assertion
PROTO ((cpp_reader
*, char *, U_CHAR
*));
140 static void path_include
PROTO ((cpp_reader
*, char *));
141 static void initialize_builtins
PROTO ((cpp_reader
*));
142 static void initialize_char_syntax
PROTO ((void));
144 static void trigraph_pcp ();
146 static void validate_else
PROTO ((cpp_reader
*, char *));
147 static int comp_def_part
PROTO ((int, U_CHAR
*, int, U_CHAR
*,
150 extern void fancy_abort ();
152 static int check_macro_name
PROTO ((cpp_reader
*, U_CHAR
*, char *));
153 static int compare_defs
PROTO ((cpp_reader
*,
154 DEFINITION
*, DEFINITION
*));
155 static HOST_WIDE_INT eval_if_expression
PROTO ((cpp_reader
*));
156 static int change_newlines
PROTO ((U_CHAR
*, int));
157 static void push_macro_expansion
PARAMS ((cpp_reader
*,
158 U_CHAR
*, int, HASHNODE
*));
159 static struct cpp_pending
*nreverse_pending
PARAMS ((struct cpp_pending
*));
161 static void conditional_skip
PROTO ((cpp_reader
*, int,
162 enum node_type
, U_CHAR
*));
163 static void skip_if_group
PROTO ((cpp_reader
*));
164 static int parse_name
PARAMS ((cpp_reader
*, int));
165 static void print_help
PROTO ((void));
167 /* Last arg to output_line_command. */
168 enum file_change_code
{same_file
, enter_file
, leave_file
};
170 /* External declarations. */
172 extern HOST_WIDE_INT cpp_parse_expr
PARAMS ((cpp_reader
*));
174 extern char *version_string
;
175 extern struct tm
*localtime ();
178 /* #include "file" looks in source file dir, then stack. */
179 /* #include <file> just looks in the stack. */
180 /* -I directories are added to the end, then the defaults are added. */
182 static struct default_include
{
183 char *fname
; /* The name of the directory. */
184 char *component
; /* The component containing the directory */
185 int cplusplus
; /* Only look here if we're compiling C++. */
186 int cxx_aware
; /* Includes in this directory don't need to
187 be wrapped in extern "C" when compiling
189 } include_defaults_array
[]
190 #ifdef INCLUDE_DEFAULTS
194 /* Pick up GNU C++ specific include files. */
195 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
197 /* This is the dir for fixincludes. Put it just before
198 the files that we fix. */
199 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
200 /* For cross-compilation, this dir name is generated
201 automatically in Makefile.in. */
202 { CROSS_INCLUDE_DIR
, "GCC",0, 0 },
203 #ifdef TOOL_INCLUDE_DIR
204 /* This is another place that the target system's headers might be. */
205 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
207 #else /* not CROSS_COMPILE */
208 #ifdef LOCAL_INCLUDE_DIR
209 /* This should be /usr/local/include and should come before
210 the fixincludes-fixed header files. */
211 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
213 #ifdef TOOL_INCLUDE_DIR
214 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
215 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
216 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
218 /* This is the dir for fixincludes. Put it just before
219 the files that we fix. */
220 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
221 /* Some systems have an extra dir of include files. */
222 #ifdef SYSTEM_INCLUDE_DIR
223 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
225 #ifndef STANDARD_INCLUDE_COMPONENT
226 #define STANDARD_INCLUDE_COMPONENT 0
228 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
229 #endif /* not CROSS_COMPILE */
232 #endif /* no INCLUDE_DEFAULTS */
234 /* `struct directive' defines one #-directive, including how to handle it. */
237 int length
; /* Length of name */
238 int (*func
) /* Function to handle directive */
239 PARAMS ((cpp_reader
*, struct directive
*));
240 char *name
; /* Name of directive */
241 enum node_type type
; /* Code which describes which directive. */
244 /* These functions are declared to return int instead of void since they
245 are going to be placed in a table and some old compilers have trouble with
246 pointers to functions returning void. */
248 static int do_define
PARAMS ((cpp_reader
*, struct directive
*));
249 static int do_line
PARAMS ((cpp_reader
*, struct directive
*));
250 static int do_include
PARAMS ((cpp_reader
*, struct directive
*));
251 static int do_undef
PARAMS ((cpp_reader
*, struct directive
*));
252 static int do_error
PARAMS ((cpp_reader
*, struct directive
*));
253 static int do_pragma
PARAMS ((cpp_reader
*, struct directive
*));
254 static int do_ident
PARAMS ((cpp_reader
*, struct directive
*));
255 static int do_if
PARAMS ((cpp_reader
*, struct directive
*));
256 static int do_xifdef
PARAMS ((cpp_reader
*, struct directive
*));
257 static int do_else
PARAMS ((cpp_reader
*, struct directive
*));
258 static int do_elif
PARAMS ((cpp_reader
*, struct directive
*));
259 static int do_endif
PARAMS ((cpp_reader
*, struct directive
*));
260 #ifdef SCCS_DIRECTIVE
261 static int do_sccs
PARAMS ((cpp_reader
*, struct directive
*));
263 static int do_assert
PARAMS ((cpp_reader
*, struct directive
*));
264 static int do_unassert
PARAMS ((cpp_reader
*, struct directive
*));
265 static int do_warning
PARAMS ((cpp_reader
*, struct directive
*));
267 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
268 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
270 /* Here is the actual list of #-directives, most-often-used first.
271 The initialize_builtins function assumes #define is the very first. */
273 static struct directive directive_table
[] = {
274 { 6, do_define
, "define", T_DEFINE
},
275 { 5, do_xifdef
, "ifdef", T_IFDEF
},
276 { 6, do_xifdef
, "ifndef", T_IFNDEF
},
277 { 7, do_include
, "include", T_INCLUDE
},
278 { 12, do_include
, "include_next", T_INCLUDE_NEXT
},
279 { 6, do_include
, "import", T_IMPORT
},
280 { 5, do_endif
, "endif", T_ENDIF
},
281 { 4, do_else
, "else", T_ELSE
},
282 { 2, do_if
, "if", T_IF
},
283 { 4, do_elif
, "elif", T_ELIF
},
284 { 5, do_undef
, "undef", T_UNDEF
},
285 { 5, do_error
, "error", T_ERROR
},
286 { 7, do_warning
, "warning", T_WARNING
},
287 { 6, do_pragma
, "pragma", T_PRAGMA
},
288 { 4, do_line
, "line", T_LINE
},
289 { 5, do_ident
, "ident", T_IDENT
},
290 #ifdef SCCS_DIRECTIVE
291 { 4, do_sccs
, "sccs", T_SCCS
},
293 { 6, do_assert
, "assert", T_ASSERT
},
294 { 8, do_unassert
, "unassert", T_UNASSERT
},
295 { -1, 0, "", T_UNUSED
}
298 /* table to tell if char can be part of a C identifier. */
299 U_CHAR is_idchar
[256] = { 0 };
300 /* table to tell if char can be first char of a c identifier. */
301 U_CHAR is_idstart
[256] = { 0 };
302 /* table to tell if c is horizontal space. */
303 U_CHAR is_hor_space
[256] = { 0 };
304 /* table to tell if c is horizontal or vertical space. */
305 U_CHAR is_space
[256] = { 0 };
307 /* Initialize syntactic classifications of characters. */
309 initialize_char_syntax ()
314 * Set up is_idchar and is_idstart tables. These should be
315 * faster than saying (is_alpha (c) || c == '_'), etc.
316 * Set up these things before calling any routines tthat
318 * XXX We should setlocale(LC_CTYPE, "C") here for safety.
320 for (i
= 0; i
< 256; i
++)
322 is_idchar
[i
] = ISALNUM (i
);
323 is_idstart
[i
] = ISALPHA (i
);
329 /* These will be reset later if -$ is in effect. */
333 /* horizontal space table */
334 is_hor_space
[' '] = 1;
335 is_hor_space
['\t'] = 1;
336 is_hor_space
['\v'] = 1;
337 is_hor_space
['\f'] = 1;
338 is_hor_space
['\r'] = 1;
349 /* Place into PFILE a quoted string representing the string SRC.
350 Caller must reserve enough space in pfile->token_buffer. */
353 quote_string (pfile
, src
)
359 CPP_PUTC_Q (pfile
, '\"');
361 switch ((c
= *src
++))
365 CPP_PUTC_Q (pfile
, c
);
368 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o", c
);
369 CPP_ADJUST_WRITTEN (pfile
, 4);
375 CPP_PUTC_Q (pfile
, '\\');
376 CPP_PUTC_Q (pfile
, c
);
380 CPP_PUTC_Q (pfile
, '\"');
381 CPP_NUL_TERMINATE_Q (pfile
);
386 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
389 cpp_grow_buffer (pfile
, n
)
393 long old_written
= CPP_WRITTEN (pfile
);
394 pfile
->token_buffer_size
= n
+ 2 * pfile
->token_buffer_size
;
395 pfile
->token_buffer
= (U_CHAR
*)
396 xrealloc(pfile
->token_buffer
, pfile
->token_buffer_size
);
397 CPP_SET_WRITTEN (pfile
, old_written
);
402 * process a given definition string, for initialization
403 * If STR is just an identifier, define it with value 1.
404 * If STR has anything after the identifier, then it should
405 * be identifier=definition.
409 cpp_define (pfile
, str
)
419 cpp_error (pfile
, "malformed option `-D %s'", str
);
422 while (is_idchar
[*++p
])
425 while (is_idchar
[*++p
] || *p
== ',' || is_hor_space
[*p
])
428 p
= (U_CHAR
*) str
; /* Error */
432 buf
= (U_CHAR
*) alloca (p
- buf
+ 4);
433 strcpy ((char *)buf
, str
);
434 strcat ((char *)buf
, " 1");
438 cpp_error (pfile
, "malformed option `-D %s'", str
);
444 /* Copy the entire option so we can modify it. */
445 buf
= (U_CHAR
*) alloca (2 * strlen (str
) + 1);
446 strncpy (buf
, str
, p
- str
);
447 /* Change the = to a space. */
449 /* Scan for any backslash-newline and remove it. */
454 if (*p
== '\\' && p
[1] == '\n')
462 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
464 do_define (pfile
, NULL
);
465 cpp_pop_buffer (pfile
);
469 /* Process the string STR as if it appeared as the body of a #assert.
470 OPTION is the option name for which STR was the argument. */
473 make_assertion (pfile
, option
, str
)
480 /* Copy the entire option so we can modify it. */
481 buf
= (U_CHAR
*) alloca (strlen (str
) + 1);
482 strcpy ((char *) buf
, str
);
483 /* Scan for any backslash-newline and remove it. */
487 if (*p
== '\\' && p
[1] == '\n')
496 if (!is_idstart
[*p
]) {
497 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
500 while (is_idchar
[*++p
])
502 while (*p
== ' ' || *p
== '\t') p
++;
503 if (! (*p
== 0 || *p
== '(')) {
504 cpp_error (pfile
, "malformed option `%s %s'", option
, str
);
508 if (cpp_push_buffer (pfile
, buf
, strlen (buf
)) != NULL
)
510 do_assert (pfile
, NULL
);
511 cpp_pop_buffer (pfile
);
515 /* Given a colon-separated list of file names PATH,
516 add all the names to the search path for include files. */
519 path_include (pfile
, path
)
532 /* Find the end of this name. */
533 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
535 /* An empty name in the path stands for the current directory. */
536 name
= (char *) xmalloc (2);
540 /* Otherwise use the directory that is named. */
541 name
= (char *) xmalloc (q
- p
+ 1);
542 bcopy (p
, name
, q
- p
);
546 append_include_chain (pfile
,
547 &(CPP_OPTIONS (pfile
)->bracket_include
), name
, 0);
549 /* Advance past this name. */
553 /* Skip the colon. */
559 cpp_options_init (opts
)
562 bzero ((char *) opts
, sizeof *opts
);
563 opts
->in_fname
= NULL
;
564 opts
->out_fname
= NULL
;
566 opts
->dollars_in_ident
= 1;
567 initialize_char_syntax ();
569 opts
->no_line_commands
= 0;
570 opts
->no_trigraphs
= 1;
571 opts
->put_out_comments
= 0;
572 opts
->print_include_names
= 0;
573 opts
->dump_macros
= dump_none
;
577 opts
->cplusplus_comments
= 1;
584 opts
->pedantic_errors
= 0;
585 opts
->inhibit_warnings
= 0;
586 opts
->warn_comments
= 0;
587 opts
->warn_import
= 1;
588 opts
->warnings_are_errors
= 0;
592 null_underflow (pfile
)
593 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
599 null_cleanup (pbuf
, pfile
)
600 cpp_buffer
*pbuf ATTRIBUTE_UNUSED
;
601 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
607 macro_cleanup (pbuf
, pfile
)
609 cpp_reader
*pfile ATTRIBUTE_UNUSED
;
611 HASHNODE
*macro
= (HASHNODE
*) pbuf
->data
;
612 if (macro
->type
== T_DISABLED
)
613 macro
->type
= T_MACRO
;
614 if (macro
->type
!= T_MACRO
|| pbuf
->buf
!= macro
->value
.defn
->expansion
)
619 /* Assuming we have read '/'.
620 If this is the start of a comment (followed by '*' or '/'),
621 skip to the end of the comment, and return ' '.
622 Return EOF if we reached the end of file before the end of the comment.
623 If not the start of a comment, return '/'. */
626 skip_comment (pfile
, linep
)
631 while (PEEKC() == '\\' && PEEKN(1) == '\n')
646 while (c
== '\\' && PEEKC() == '\n')
650 FORWARD(1), c
= GETC();
652 if (prev_c
== '*' && c
== '/')
654 if (c
== '\n' && linep
)
658 else if (PEEKC() == '/' && CPP_OPTIONS (pfile
)->cplusplus_comments
)
665 return ' '; /* Allow // to be terminated by EOF. */
666 while (c
== '\\' && PEEKC() == '\n')
675 /* Don't consider final '\n' to be part of comment. */
685 /* Skip whitespace \-newline and comments. Does not macro-expand. */
688 cpp_skip_hspace (pfile
)
698 if ((c
== '\f' || c
== '\v') && CPP_PEDANTIC (pfile
))
699 cpp_pedwarn (pfile
, "%s in preprocessing directive",
700 c
== '\f' ? "formfeed" : "vertical tab");
706 c
= skip_comment (pfile
, NULL
);
709 if (c
== EOF
|| c
== '/')
712 else if (c
== '\\' && PEEKN(1) == '\n') {
715 else if (c
== '@' && CPP_BUFFER (pfile
)->has_escapes
716 && is_hor_space
[PEEKN(1)])
722 /* Read the rest of the current line.
723 The line is appended to PFILE's output buffer. */
726 copy_rest_of_line (pfile
)
729 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
746 goto scan_directive_token
;
750 if (nextc
== '*' || (opts
->cplusplus_comments
&& nextc
== '/'))
751 goto scan_directive_token
;
755 if (CPP_PEDANTIC (pfile
))
756 cpp_pedwarn (pfile
, "%s in preprocessing directive",
757 c
== '\f' ? "formfeed" : "vertical tab");
763 scan_directive_token
:
765 cpp_get_token (pfile
);
771 CPP_NUL_TERMINATE (pfile
);
775 skip_rest_of_line (pfile
)
778 long old
= CPP_WRITTEN (pfile
);
779 copy_rest_of_line (pfile
);
780 CPP_SET_WRITTEN (pfile
, old
);
783 /* Handle a possible # directive.
784 '#' has already been read. */
787 handle_directive (pfile
)
790 register struct directive
*kt
;
793 long old_written
= CPP_WRITTEN (pfile
);
795 cpp_skip_hspace (pfile
);
798 if (c
>= '0' && c
<= '9')
800 /* Handle # followed by a line number. */
801 if (CPP_PEDANTIC (pfile
))
802 cpp_pedwarn (pfile
, "`#' followed by integer");
803 do_line (pfile
, NULL
);
804 goto done_a_directive
;
807 /* Now find the directive name. */
808 CPP_PUTC (pfile
, '#');
809 parse_name (pfile
, GETC());
810 ident
= pfile
->token_buffer
+ old_written
+ 1;
811 ident_length
= CPP_PWRITTEN (pfile
) - ident
;
812 if (ident_length
== 0 && PEEKC() == '\n')
814 /* A line of just `#' becomes blank. */
815 goto done_a_directive
;
819 if (ident_length
== 0 || !is_idstart
[*ident
]) {
821 while (is_idchar
[*p
]) {
822 if (*p
< '0' || *p
> '9')
826 /* Avoid error for `###' and similar cases unless -pedantic. */
828 while (*p
== '#' || is_hor_space
[*p
]) p
++;
830 if (pedantic
&& !lang_asm
)
831 cpp_warning (pfile
, "invalid preprocessor directive");
837 cpp_error (pfile
, "invalid preprocessor directive name");
843 * Decode the keyword and call the appropriate expansion
844 * routine, after moving the input pointer up to the next line.
846 for (kt
= directive_table
; ; kt
++) {
848 goto not_a_directive
;
849 if (kt
->length
== ident_length
850 && !strncmp (kt
->name
, ident
, ident_length
))
854 /* We may want to pass through #define, #pragma, and #include.
855 Other directives may create output, but we don't want the directive
856 itself out, so we pop it now. For example conditionals may emit
857 #failed ... #endfailed stuff. */
859 if (! (kt
->type
== T_DEFINE
860 || kt
->type
== T_PRAGMA
861 || (IS_INCLUDE_DIRECTIVE_TYPE (kt
->type
)
862 && CPP_OPTIONS (pfile
)->dump_includes
)))
863 CPP_SET_WRITTEN (pfile
, old_written
);
865 (*kt
->func
) (pfile
, kt
);
867 if (kt
->type
== T_DEFINE
)
869 if (CPP_OPTIONS (pfile
)->dump_macros
== dump_names
)
871 /* Skip "#define". */
872 U_CHAR
*p
= pfile
->token_buffer
+ old_written
+ 7;
874 SKIP_WHITE_SPACE (p
);
875 while (is_idchar
[*p
]) p
++;
877 CPP_PUTC (pfile
, '\n');
879 else if (CPP_OPTIONS (pfile
)->dump_macros
!= dump_definitions
)
880 CPP_SET_WRITTEN (pfile
, old_written
);
890 /* Pass a directive through to the output file.
891 BUF points to the contents of the directive, as a contiguous string.
892 m LIMIT points to the first character past the end of the directive.
893 KEYWORD is the keyword-table entry for the directive. */
896 pass_thru_directive (buf
, limit
, pfile
, keyword
)
899 struct directive
*keyword
;
901 register unsigned keyword_length
= keyword
->length
;
903 CPP_RESERVE (pfile
, 1 + keyword_length
+ (limit
- buf
));
904 CPP_PUTC_Q (pfile
, '#');
905 CPP_PUTS_Q (pfile
, keyword
->name
, keyword_length
);
906 if (limit
!= buf
&& buf
[0] != ' ')
907 CPP_PUTC_Q (pfile
, ' ');
908 CPP_PUTS_Q (pfile
, buf
, limit
- buf
);
910 CPP_PUTS_Q (pfile
, '\n');
911 /* Count the line we have just made in the output,
912 to get in sync properly. */
917 /* The arglist structure is built by do_define to tell
918 collect_definition where the argument names begin. That
919 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
920 would contain pointers to the strings x, y, and z.
921 Collect_definition would then build a DEFINITION node,
922 with reflist nodes pointing to the places x, y, and z had
923 appeared. So the arglist is just convenience data passed
924 between these two routines. It is not kept around after
925 the current #define has been processed and entered into the
929 struct arglist
*next
;
936 /* Read a replacement list for a macro with parameters.
937 Build the DEFINITION structure.
938 Reads characters of text starting at BUF until END.
939 ARGLIST specifies the formal parameters to look for
940 in the text of the definition; NARGS is the number of args
941 in that list, or -1 for a macro name that wants no argument list.
942 MACRONAME is the macro name itself (so we can avoid recursive expansion)
943 and NAMELEN is its length in characters.
945 Note that comments, backslash-newlines, and leading white space
946 have already been deleted from the argument. */
949 collect_expansion (pfile
, buf
, limit
, nargs
, arglist
)
953 struct arglist
*arglist
;
956 register U_CHAR
*p
, *lastp
, *exp_p
;
957 struct reflist
*endpat
= NULL
;
958 /* Pointer to first nonspace after last ## seen. */
960 /* Pointer to first nonspace after last single-# seen. */
961 U_CHAR
*stringify
= 0;
963 int expected_delimiter
= '\0';
965 /* Scan thru the replacement list, ignoring comments and quoted
966 strings, picking up on the macro calls. It does a linear search
967 thru the arg list on every potential symbol. Profiling might say
968 that something smarter should happen. */
973 /* Find the beginning of the trailing whitespace. */
975 while (p
< limit
&& is_space
[limit
[-1]]) limit
--;
977 /* Allocate space for the text in the macro definition.
978 Leading and trailing whitespace chars need 2 bytes each.
979 Each other input char may or may not need 1 byte,
980 so this is an upper bound. The extra 5 are for invented
981 leading and trailing newline-marker and final null. */
982 maxsize
= (sizeof (DEFINITION
)
984 /* Occurrences of '@' get doubled, so allocate extra space for them. */
988 defn
= (DEFINITION
*) xcalloc (1, maxsize
);
991 exp_p
= defn
->expansion
= (U_CHAR
*) defn
+ sizeof (DEFINITION
);
996 /* Add one initial space escape-marker to prevent accidental
997 token-pasting (often removed by macroexpand). */
1001 if (limit
- p
>= 2 && p
[0] == '#' && p
[1] == '#') {
1002 cpp_error (pfile
, "`##' at start of macro definition");
1006 /* Process the main body of the definition. */
1008 int skipped_arg
= 0;
1009 register U_CHAR c
= *p
++;
1013 if (!CPP_TRADITIONAL (pfile
)) {
1017 if (expected_delimiter
!= '\0') {
1018 if (c
== expected_delimiter
)
1019 expected_delimiter
= '\0';
1021 expected_delimiter
= c
;
1025 if (p
< limit
&& expected_delimiter
) {
1026 /* In a string, backslash goes through
1027 and makes next char ordinary. */
1033 /* An '@' in a string or character constant stands for itself,
1034 and does not need to be escaped. */
1035 if (!expected_delimiter
)
1040 /* # is ordinary inside a string. */
1041 if (expected_delimiter
)
1043 if (p
< limit
&& *p
== '#') {
1044 /* ##: concatenate preceding and following tokens. */
1045 /* Take out the first #, discard preceding whitespace. */
1047 while (exp_p
> lastp
&& is_hor_space
[exp_p
[-1]])
1049 /* Skip the second #. */
1051 /* Discard following whitespace. */
1052 SKIP_WHITE_SPACE (p
);
1055 cpp_error (pfile
, "`##' at end of macro definition");
1056 } else if (nargs
>= 0) {
1057 /* Single #: stringify following argument ref.
1058 Don't leave the # in the expansion. */
1060 SKIP_WHITE_SPACE (p
);
1061 if (p
== limit
|| ! is_idstart
[*p
]
1062 || (*p
== 'L' && p
+ 1 < limit
&& (p
[1] == '\'' || p
[1] == '"')))
1064 "`#' operator is not followed by a macro argument name");
1071 /* In -traditional mode, recognize arguments inside strings and
1072 character constants, and ignore special properties of #.
1073 Arguments inside strings are considered "stringified", but no
1074 extra quote marks are supplied. */
1078 if (expected_delimiter
!= '\0') {
1079 if (c
== expected_delimiter
)
1080 expected_delimiter
= '\0';
1082 expected_delimiter
= c
;
1086 /* Backslash quotes delimiters and itself, but not macro args. */
1087 if (expected_delimiter
!= 0 && p
< limit
1088 && (*p
== expected_delimiter
|| *p
== '\\')) {
1095 if (expected_delimiter
!= '\0') /* No comments inside strings. */
1098 /* If we find a comment that wasn't removed by handle_directive,
1099 this must be -traditional. So replace the comment with
1103 while (p
< limit
&& !(p
[-2] == '*' && p
[-1] == '/'))
1106 /* Mark this as a concatenation-point, as if it had been ##. */
1114 /* Handle the start of a symbol. */
1115 if (is_idchar
[c
] && nargs
> 0) {
1116 U_CHAR
*id_beg
= p
- 1;
1120 while (p
!= limit
&& is_idchar
[*p
]) p
++;
1121 id_len
= p
- id_beg
;
1124 && ! (id_len
== 1 && c
== 'L' && (*p
== '\'' || *p
== '"'))) {
1125 register struct arglist
*arg
;
1127 for (arg
= arglist
; arg
!= NULL
; arg
= arg
->next
) {
1128 struct reflist
*tpat
;
1130 if (arg
->name
[0] == c
1131 && arg
->length
== id_len
1132 && strncmp (arg
->name
, id_beg
, id_len
) == 0) {
1133 if (expected_delimiter
&& CPP_OPTIONS (pfile
)->warn_stringify
) {
1134 if (CPP_TRADITIONAL (pfile
)) {
1135 cpp_warning (pfile
, "macro argument `%.*s' is stringified.",
1139 "macro arg `%.*s' would be stringified with -traditional.",
1143 /* If ANSI, don't actually substitute inside a string. */
1144 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
)
1146 /* make a pat node for this arg and append it to the end of
1148 tpat
= (struct reflist
*) xmalloc (sizeof (struct reflist
));
1150 tpat
->raw_before
= concat
== id_beg
;
1151 tpat
->raw_after
= 0;
1152 tpat
->rest_args
= arg
->rest_args
;
1153 tpat
->stringify
= (CPP_TRADITIONAL (pfile
)
1154 ? expected_delimiter
!= '\0'
1155 : stringify
== id_beg
);
1158 defn
->pattern
= tpat
;
1160 endpat
->next
= tpat
;
1163 tpat
->argno
= arg
->argno
;
1164 tpat
->nchars
= exp_p
- lastp
;
1166 register U_CHAR
*p1
= p
;
1167 SKIP_WHITE_SPACE (p1
);
1168 if (p1
+ 2 <= limit
&& p1
[0] == '#' && p1
[1] == '#')
1169 tpat
->raw_after
= 1;
1171 lastp
= exp_p
; /* place to start copying from next time */
1178 /* If this was not a macro arg, copy it into the expansion. */
1179 if (! skipped_arg
) {
1180 register U_CHAR
*lim1
= p
;
1184 if (stringify
== id_beg
)
1186 "`#' operator should be followed by a macro argument name");
1191 if (!CPP_TRADITIONAL (pfile
) && expected_delimiter
== 0)
1193 /* If ANSI, put in a "@ " marker to prevent token pasting.
1194 But not if "inside a string" (which in ANSI mode
1195 happens only for -D option). */
1202 defn
->length
= exp_p
- defn
->expansion
;
1204 /* Crash now if we overrun the allocated size. */
1205 if (defn
->length
+ 1 > maxsize
)
1209 /* This isn't worth the time it takes. */
1210 /* give back excess storage */
1211 defn
->expansion
= (U_CHAR
*) xrealloc (defn
->expansion
, defn
->length
+ 1);
1218 * special extension string that can be added to the last macro argument to
1219 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1220 * #define wow(a, b...) process (b, a, b)
1221 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1222 * { wow (one, two); } -> { process (two, one, two); }
1223 * if this "rest_arg" is used with the concat token '##' and if it is not
1224 * supplied then the token attached to with ## will not be outputted. Ex:
1225 * #define wow (a, b...) process (b ## , a, ## b)
1226 * { wow (1, 2); } -> { process (2, 1, 2); }
1227 * { wow (one); } -> { process (one); {
1229 static char rest_extension
[] = "...";
1230 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1232 /* Create a DEFINITION node from a #define directive. Arguments are
1233 as for do_define. */
1236 create_definition (buf
, limit
, pfile
, predefinition
)
1237 U_CHAR
*buf
, *limit
;
1241 U_CHAR
*bp
; /* temp ptr into input buffer */
1242 U_CHAR
*symname
; /* remember where symbol name starts */
1243 int sym_length
; /* and how long it is */
1246 char *file
= CPP_BUFFER (pfile
) ? CPP_BUFFER (pfile
)->nominal_fname
: "";
1248 int arglengths
= 0; /* Accumulate lengths of arg names
1249 plus number of args. */
1251 cpp_buf_line_and_col (CPP_BUFFER (pfile
), &line
, &col
);
1255 while (is_hor_space
[*bp
])
1258 symname
= bp
; /* remember where it starts */
1260 sym_length
= check_macro_name (pfile
, bp
, "macro");
1263 /* Lossage will occur if identifiers or control keywords are broken
1264 across lines using backslash. This is not the right place to take
1268 struct arglist
*arg_ptrs
= NULL
;
1271 bp
++; /* skip '(' */
1272 SKIP_WHITE_SPACE (bp
);
1274 /* Loop over macro argument names. */
1275 while (*bp
!= ')') {
1276 struct arglist
*temp
;
1278 temp
= (struct arglist
*) alloca (sizeof (struct arglist
));
1280 temp
->next
= arg_ptrs
;
1281 temp
->argno
= argno
++;
1282 temp
->rest_args
= 0;
1286 cpp_pedwarn (pfile
, "another parameter follows `%s'", rest_extension
);
1288 if (!is_idstart
[*bp
])
1289 cpp_pedwarn (pfile
, "invalid character in macro parameter name");
1291 /* Find the end of the arg name. */
1292 while (is_idchar
[*bp
]) {
1294 /* do we have a "special" rest-args extension here? */
1295 if ((size_t)(limit
- bp
) > REST_EXTENSION_LENGTH
1296 && strncmp (rest_extension
, bp
, REST_EXTENSION_LENGTH
) == 0) {
1298 temp
->rest_args
= 1;
1302 temp
->length
= bp
- temp
->name
;
1304 bp
+= REST_EXTENSION_LENGTH
;
1305 arglengths
+= temp
->length
+ 2;
1306 SKIP_WHITE_SPACE (bp
);
1307 if (temp
->length
== 0 || (*bp
!= ',' && *bp
!= ')')) {
1308 cpp_error (pfile
, "badly punctuated parameter list in `#define'");
1313 SKIP_WHITE_SPACE (bp
);
1316 cpp_error (pfile
, "unterminated parameter list in `#define'");
1320 struct arglist
*otemp
;
1322 for (otemp
= temp
->next
; otemp
!= NULL
; otemp
= otemp
->next
)
1323 if (temp
->length
== otemp
->length
1324 && strncmp (temp
->name
, otemp
->name
, temp
->length
) == 0) {
1327 name
= (U_CHAR
*) alloca (temp
->length
+ 1);
1328 (void) strncpy (name
, temp
->name
, temp
->length
);
1329 name
[temp
->length
] = '\0';
1331 "duplicate argument name `%s' in `#define'", name
);
1337 ++bp
; /* skip paren */
1338 SKIP_WHITE_SPACE (bp
);
1339 /* now everything from bp before limit is the definition. */
1340 defn
= collect_expansion (pfile
, bp
, limit
, argno
, arg_ptrs
);
1341 defn
->rest_args
= rest_args
;
1343 /* Now set defn->args.argnames to the result of concatenating
1344 the argument names in reverse order
1345 with comma-space between them. */
1346 defn
->args
.argnames
= (U_CHAR
*) xmalloc (arglengths
+ 1);
1348 struct arglist
*temp
;
1350 for (temp
= arg_ptrs
; temp
; temp
= temp
->next
) {
1351 bcopy (temp
->name
, &defn
->args
.argnames
[i
], temp
->length
);
1353 if (temp
->next
!= 0) {
1354 defn
->args
.argnames
[i
++] = ',';
1355 defn
->args
.argnames
[i
++] = ' ';
1358 defn
->args
.argnames
[i
] = 0;
1361 /* Simple expansion or empty definition. */
1365 if (is_hor_space
[*bp
]) {
1367 SKIP_WHITE_SPACE (bp
);
1370 case '!': case '"': case '#': case '%': case '&': case '\'':
1371 case ')': case '*': case '+': case ',': case '-': case '.':
1372 case '/': case ':': case ';': case '<': case '=': case '>':
1373 case '?': case '[': case '\\': case ']': case '^': case '{':
1374 case '|': case '}': case '~':
1375 cpp_warning (pfile
, "missing white space after `#define %.*s'",
1376 sym_length
, symname
);
1380 cpp_pedwarn (pfile
, "missing white space after `#define %.*s'",
1381 sym_length
, symname
);
1386 /* now everything from bp before limit is the definition. */
1387 defn
= collect_expansion (pfile
, bp
, limit
, -1, NULL_PTR
);
1388 defn
->args
.argnames
= (U_CHAR
*) "";
1394 /* OP is null if this is a predefinition */
1395 defn
->predefined
= predefinition
;
1397 mdef
.symnam
= symname
;
1398 mdef
.symlen
= sym_length
;
1407 /* Check a purported macro name SYMNAME, and yield its length.
1408 USAGE is the kind of name this is intended for. */
1411 check_macro_name (pfile
, symname
, usage
)
1419 for (p
= symname
; is_idchar
[*p
]; p
++)
1421 sym_length
= p
- symname
;
1423 || (sym_length
== 1 && *symname
== 'L' && (*p
== '\'' || *p
== '"')))
1424 cpp_error (pfile
, "invalid %s name", usage
);
1425 else if (!is_idstart
[*symname
]) {
1426 U_CHAR
*msg
; /* what pain... */
1427 msg
= (U_CHAR
*) alloca (sym_length
+ 1);
1428 bcopy (symname
, msg
, sym_length
);
1429 msg
[sym_length
] = 0;
1430 cpp_error (pfile
, "invalid %s name `%s'", usage
, msg
);
1432 if (! strncmp (symname
, "defined", 7) && sym_length
== 7)
1433 cpp_error (pfile
, "invalid %s name `defined'", usage
);
1438 /* Return zero if two DEFINITIONs are isomorphic. */
1441 compare_defs (pfile
, d1
, d2
)
1443 DEFINITION
*d1
, *d2
;
1445 register struct reflist
*a1
, *a2
;
1446 register U_CHAR
*p1
= d1
->expansion
;
1447 register U_CHAR
*p2
= d2
->expansion
;
1450 if (d1
->nargs
!= d2
->nargs
)
1452 if (CPP_PEDANTIC (pfile
)
1453 && strcmp ((char *)d1
->args
.argnames
, (char *)d2
->args
.argnames
))
1455 for (a1
= d1
->pattern
, a2
= d2
->pattern
; a1
&& a2
;
1456 a1
= a1
->next
, a2
= a2
->next
) {
1457 if (!((a1
->nchars
== a2
->nchars
&& ! strncmp (p1
, p2
, a1
->nchars
))
1458 || ! comp_def_part (first
, p1
, a1
->nchars
, p2
, a2
->nchars
, 0))
1459 || a1
->argno
!= a2
->argno
1460 || a1
->stringify
!= a2
->stringify
1461 || a1
->raw_before
!= a2
->raw_before
1462 || a1
->raw_after
!= a2
->raw_after
)
1470 if (comp_def_part (first
, p1
, d1
->length
- (p1
- d1
->expansion
),
1471 p2
, d2
->length
- (p2
- d2
->expansion
), 1))
1476 /* Return 1 if two parts of two macro definitions are effectively different.
1477 One of the parts starts at BEG1 and has LEN1 chars;
1478 the other has LEN2 chars at BEG2.
1479 Any sequence of whitespace matches any other sequence of whitespace.
1480 FIRST means these parts are the first of a macro definition;
1481 so ignore leading whitespace entirely.
1482 LAST means these parts are the last of a macro definition;
1483 so ignore trailing whitespace entirely. */
1486 comp_def_part (first
, beg1
, len1
, beg2
, len2
, last
)
1488 U_CHAR
*beg1
, *beg2
;
1492 register U_CHAR
*end1
= beg1
+ len1
;
1493 register U_CHAR
*end2
= beg2
+ len2
;
1495 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1496 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1499 while (beg1
!= end1
&& is_space
[end1
[-1]]) end1
--;
1500 while (beg2
!= end2
&& is_space
[end2
[-1]]) end2
--;
1502 while (beg1
!= end1
&& beg2
!= end2
) {
1503 if (is_space
[*beg1
] && is_space
[*beg2
]) {
1504 while (beg1
!= end1
&& is_space
[*beg1
]) beg1
++;
1505 while (beg2
!= end2
&& is_space
[*beg2
]) beg2
++;
1506 } else if (*beg1
== *beg2
) {
1510 return (beg1
!= end1
) || (beg2
!= end2
);
1513 /* Process a #define command.
1514 KEYWORD is the keyword-table entry for #define,
1515 or NULL for a "predefined" macro. */
1518 do_define (pfile
, keyword
)
1520 struct directive
*keyword
;
1525 int save_put_out_comments
;
1527 U_CHAR
*macro
, *buf
, *end
;
1529 here
= CPP_WRITTEN (pfile
);
1531 save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1532 CPP_OPTIONS (pfile
)->put_out_comments
= CPP_TRADITIONAL (pfile
);
1533 copy_rest_of_line (pfile
);
1534 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1536 /* Copy out the line so we can pop the token buffer. */
1537 buf
= pfile
->token_buffer
+ here
;
1538 end
= CPP_PWRITTEN (pfile
);
1539 macro
= alloca (end
- buf
+ 1);
1540 bcopy (buf
, macro
, end
- buf
+ 1);
1541 end
= macro
+ (end
- buf
);
1543 CPP_SET_WRITTEN (pfile
, here
);
1546 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1547 if (pcp_outfile
&& keyword
)
1548 pass_thru_directive (macro
, end
, pfile
, keyword
);
1551 mdef
= create_definition (macro
, end
, pfile
, keyword
== NULL
);
1555 hashcode
= hashf (mdef
.symnam
, mdef
.symlen
, HASHSIZE
);
1557 if ((hp
= cpp_lookup (pfile
, mdef
.symnam
, mdef
.symlen
, hashcode
)) != NULL
)
1560 /* Redefining a precompiled key is ok. */
1561 if (hp
->type
== T_PCSTRING
)
1563 /* Redefining a macro is ok if the definitions are the same. */
1564 else if (hp
->type
== T_MACRO
)
1565 ok
= ! compare_defs (pfile
, mdef
.defn
, hp
->value
.defn
);
1566 /* Redefining a constant is ok with -D. */
1567 else if (hp
->type
== T_CONST
)
1568 ok
= ! CPP_OPTIONS (pfile
)->done_initializing
;
1569 /* Print the warning if it's not ok. */
1572 U_CHAR
*msg
; /* what pain... */
1574 /* If we are passing through #define and #undef directives, do
1575 that for this re-definition now. */
1576 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1577 pass_thru_directive (macro
, end
, pfile
, keyword
);
1579 msg
= (U_CHAR
*) alloca (mdef
.symlen
+ 22);
1581 bcopy (mdef
.symnam
, msg
+ 1, mdef
.symlen
);
1582 strcpy ((char *) (msg
+ mdef
.symlen
+ 1), "' redefined");
1583 cpp_pedwarn (pfile
, msg
);
1584 if (hp
->type
== T_MACRO
)
1585 cpp_pedwarn_with_file_and_line (pfile
, hp
->value
.defn
->file
, hp
->value
.defn
->line
,
1586 "this is the location of the previous definition");
1588 /* Replace the old definition. */
1590 hp
->value
.defn
= mdef
.defn
;
1594 /* If we are passing through #define and #undef directives, do
1595 that for this new definition now. */
1596 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
1597 pass_thru_directive (macro
, end
, pfile
, keyword
);
1598 install (mdef
.symnam
, mdef
.symlen
, T_MACRO
, 0,
1599 (char *) mdef
.defn
, hashcode
);
1609 /* This structure represents one parsed argument in a macro call.
1610 `raw' points to the argument text as written (`raw_length' is its length).
1611 `expanded' points to the argument's macro-expansion
1612 (its length is `expand_length').
1613 `stringified_length' is the length the argument would have
1615 `use_count' is the number of times this macro arg is substituted
1616 into the macro. If the actual use count exceeds 10,
1617 the value stored is 10. */
1619 /* raw and expanded are relative to ARG_BASE */
1620 #define ARG_BASE ((pfile)->token_buffer)
1623 /* Strings relative to pfile->token_buffer */
1624 long raw
, expanded
, stringified
;
1625 int raw_length
, expand_length
;
1626 int stringified_length
;
1631 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1632 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1633 as the new input buffer.
1634 Return the new buffer, or NULL on failure. */
1637 cpp_push_buffer (pfile
, buffer
, length
)
1642 register cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1643 if (buf
== pfile
->buffer_stack
)
1645 cpp_fatal (pfile
, "%s: macro or `#include' recursion too deep",
1650 bzero ((char *) buf
, sizeof (cpp_buffer
));
1651 CPP_BUFFER (pfile
) = buf
;
1652 buf
->if_stack
= pfile
->if_stack
;
1653 buf
->cleanup
= null_cleanup
;
1654 buf
->underflow
= null_underflow
;
1655 buf
->buf
= buf
->cur
= buffer
;
1656 buf
->alimit
= buf
->rlimit
= buffer
+ length
;
1662 cpp_pop_buffer (pfile
)
1665 cpp_buffer
*buf
= CPP_BUFFER (pfile
);
1666 (*buf
->cleanup
) (buf
, pfile
);
1667 return ++CPP_BUFFER (pfile
);
1670 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1671 Pop the buffer when done. */
1674 cpp_scan_buffer (pfile
)
1677 cpp_buffer
*buffer
= CPP_BUFFER (pfile
);
1680 enum cpp_token token
= cpp_get_token (pfile
);
1681 if (token
== CPP_EOF
) /* Should not happen ... */
1683 if (token
== CPP_POP
&& CPP_BUFFER (pfile
) == buffer
)
1685 cpp_pop_buffer (pfile
);
1692 * Rescan a string (which may have escape marks) into pfile's buffer.
1693 * Place the result in pfile->token_buffer.
1695 * The input is copied before it is scanned, so it is safe to pass
1696 * it something from the token_buffer that will get overwritten
1697 * (because it follows CPP_WRITTEN). This is used by do_include.
1701 cpp_expand_to_buffer (pfile
, buf
, length
)
1706 register cpp_buffer
*ip
;
1710 U_CHAR
*limit
= buf
+ length
;
1713 int odepth
= indepth
;
1719 /* Set up the input on the input stack. */
1721 buf1
= (U_CHAR
*) alloca (length
+ 1);
1723 register U_CHAR
*p1
= buf
;
1724 register U_CHAR
*p2
= buf1
;
1731 ip
= cpp_push_buffer (pfile
, buf1
, length
);
1734 ip
->has_escapes
= 1;
1736 ip
->lineno
= obuf
.lineno
= 1;
1739 /* Scan the input, create the output. */
1740 cpp_scan_buffer (pfile
);
1743 if (indepth
!= odepth
)
1747 CPP_NUL_TERMINATE (pfile
);
1752 adjust_position (buf
, limit
, linep
, colp
)
1762 (*linep
)++, (*colp
) = 1;
1768 /* Move line_base forward, updating lineno and colno. */
1771 update_position (pbuf
)
1772 register cpp_buffer
*pbuf
;
1774 unsigned char *old_pos
= pbuf
->buf
+ pbuf
->line_base
;
1775 unsigned char *new_pos
= pbuf
->cur
;
1776 register struct parse_marker
*mark
;
1777 for (mark
= pbuf
->marks
; mark
!= NULL
; mark
= mark
->next
)
1779 if (pbuf
->buf
+ mark
->position
< new_pos
)
1780 new_pos
= pbuf
->buf
+ mark
->position
;
1782 pbuf
->line_base
+= new_pos
- old_pos
;
1783 adjust_position (old_pos
, new_pos
, &pbuf
->lineno
, &pbuf
->colno
);
1787 cpp_buf_line_and_col (pbuf
, linep
, colp
)
1788 register cpp_buffer
*pbuf
;
1796 *linep
= pbuf
->lineno
;
1797 *colp
= pbuf
->colno
;
1798 adjust_position (pbuf
->buf
+ pbuf
->line_base
, pbuf
->cur
, linep
, colp
);
1807 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1810 cpp_file_buffer (pfile
)
1813 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1815 for ( ; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
1816 if (ip
->fname
!= NULL
)
1822 count_newlines (buf
, limit
)
1823 register U_CHAR
*buf
;
1824 register U_CHAR
*limit
;
1826 register long count
= 0;
1837 * write out a #line command, for instance, after an #include file.
1838 * If CONDITIONAL is nonzero, we can omit the #line if it would
1839 * appear to be a no-op, and we can output a few newlines instead
1840 * if we want to increase the line number by a small amount.
1841 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
1845 output_line_command (pfile
, conditional
, file_change
)
1848 enum file_change_code file_change
;
1851 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
1853 if (ip
->fname
== NULL
)
1856 update_position (ip
);
1858 if (CPP_OPTIONS (pfile
)->no_line_commands
1859 || CPP_OPTIONS (pfile
)->no_output
)
1862 line
= CPP_BUFFER (pfile
)->lineno
;
1863 col
= CPP_BUFFER (pfile
)->colno
;
1864 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
1866 if (CPP_OPTIONS (pfile
)->no_line_commands
)
1870 if (line
== pfile
->lineno
)
1873 /* If the inherited line number is a little too small,
1874 output some newlines instead of a #line command. */
1875 if (line
> pfile
->lineno
&& line
< pfile
->lineno
+ 8) {
1876 CPP_RESERVE (pfile
, 20);
1877 while (line
> pfile
->lineno
) {
1878 CPP_PUTC_Q (pfile
, '\n');
1886 /* Don't output a line number of 0 if we can help it. */
1887 if (ip
->lineno
== 0 && ip
->bufp
- ip
->buf
< ip
->length
1888 && *ip
->bufp
== '\n') {
1894 CPP_RESERVE (pfile
, 4 * strlen (ip
->nominal_fname
) + 50);
1896 #ifdef OUTPUT_LINE_COMMANDS
1897 static char sharp_line
[] = "#line ";
1899 static char sharp_line
[] = "# ";
1901 CPP_PUTS_Q (pfile
, sharp_line
, sizeof(sharp_line
)-1);
1904 sprintf ((char *) CPP_PWRITTEN (pfile
), "%ld ", line
);
1905 CPP_ADJUST_WRITTEN (pfile
, strlen (CPP_PWRITTEN (pfile
)));
1907 quote_string (pfile
, ip
->nominal_fname
);
1908 if (file_change
!= same_file
) {
1909 CPP_PUTC_Q (pfile
, ' ');
1910 CPP_PUTC_Q (pfile
, file_change
== enter_file
? '1' : '2');
1912 /* Tell cc1 if following text comes from a system header file. */
1913 if (ip
->system_header_p
) {
1914 CPP_PUTC_Q (pfile
, ' ');
1915 CPP_PUTC_Q (pfile
, '3');
1917 #ifndef NO_IMPLICIT_EXTERN_C
1918 /* Tell cc1plus if following text should be treated as C. */
1919 if (ip
->system_header_p
== 2 && CPP_OPTIONS (pfile
)->cplusplus
) {
1920 CPP_PUTC_Q (pfile
, ' ');
1921 CPP_PUTC_Q (pfile
, '4');
1924 CPP_PUTC_Q (pfile
, '\n');
1925 pfile
->lineno
= line
;
1929 * Parse a macro argument and append the info on PFILE's token_buffer.
1930 * REST_ARGS means to absorb the rest of the args.
1931 * Return nonzero to indicate a syntax error.
1934 static enum cpp_token
1935 macarg (pfile
, rest_args
)
1940 enum cpp_token token
;
1941 char save_put_out_comments
= CPP_OPTIONS (pfile
)->put_out_comments
;
1942 CPP_OPTIONS (pfile
)->put_out_comments
= 0;
1944 /* Try to parse as much of the argument as exists at this
1945 input stack level. */
1946 pfile
->no_macro_expand
++;
1949 token
= cpp_get_token (pfile
);
1955 /* If we've hit end of file, it's an error (reported by caller).
1956 Ditto if it's the end of cpp_expand_to_buffer text.
1957 If we've hit end of macro, just continue. */
1958 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
1969 /* if we've returned to lowest level and
1970 we aren't absorbing all args */
1971 if (paren
== 0 && rest_args
== 0)
1975 /* Remove ',' or ')' from argument buffer. */
1976 CPP_ADJUST_WRITTEN (pfile
, -1);
1983 CPP_OPTIONS (pfile
)->put_out_comments
= save_put_out_comments
;
1984 pfile
->no_macro_expand
--;
1989 /* Turn newlines to spaces in the string of length LENGTH at START,
1990 except inside of string constants.
1991 The string is copied into itself with its beginning staying fixed. */
1994 change_newlines (start
, length
)
1998 register U_CHAR
*ibp
;
1999 register U_CHAR
*obp
;
2000 register U_CHAR
*limit
;
2004 limit
= start
+ length
;
2007 while (ibp
< limit
) {
2008 *obp
++ = c
= *ibp
++;
2013 /* Notice and skip strings, so that we don't delete newlines in them. */
2016 while (ibp
< limit
) {
2017 *obp
++ = c
= *ibp
++;
2020 if (c
== '\n' && quotec
== '\'')
2036 if (!pfile
->timebuf
) {
2037 time_t t
= time ((time_t *) 0);
2038 pfile
->timebuf
= localtime (&t
);
2040 return pfile
->timebuf
;
2043 static char *monthnames
[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2044 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2048 * expand things like __FILE__. Place the expansion into the output
2049 * buffer *without* rescanning.
2053 special_symbol (hp
, pfile
)
2060 cpp_buffer
*ip
= NULL
;
2063 int paren
= 0; /* For special `defined' keyword */
2066 if (pcp_outfile
&& pcp_inside_if
2067 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2069 "Predefined macro `%s' used inside `#if' during precompilation",
2073 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2075 if (ip
== CPP_NULL_BUFFER (pfile
))
2077 cpp_error (pfile
, "cccp error: not in any file?!");
2078 return; /* the show must go on */
2080 if (ip
->fname
!= NULL
)
2090 if (hp
->type
== T_BASE_FILE
)
2092 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2093 ip
= CPP_PREV_BUFFER (ip
);
2095 string
= ip
->nominal_fname
;
2099 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2100 quote_string (pfile
, string
);
2104 case T_INCLUDE_LEVEL
:
2106 ip
= CPP_BUFFER (pfile
);
2107 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2108 if (ip
->fname
!= NULL
)
2111 buf
= (char *) alloca (8); /* Eight bytes ought to be more than enough */
2112 sprintf (buf
, "%d", true_indepth
- 1);
2116 buf
= (char *) alloca (3 + strlen (version_string
));
2117 sprintf (buf
, "\"%s\"", version_string
);
2120 #ifndef NO_BUILTIN_SIZE_TYPE
2126 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2127 case T_PTRDIFF_TYPE
:
2133 buf
= CPP_WCHAR_TYPE (pfile
);
2136 case T_USER_LABEL_PREFIX_TYPE
:
2137 buf
= user_label_prefix
;
2140 case T_REGISTER_PREFIX_TYPE
:
2141 buf
= REGISTER_PREFIX
;
2145 buf
= (char *) alloca (4 * sizeof (int));
2146 sprintf (buf
, "%d", hp
->value
.ival
);
2147 #ifdef STDC_0_IN_SYSTEM_HEADERS
2148 if (ip
->system_header_p
2149 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2150 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2154 if (pcp_inside_if
&& pcp_outfile
)
2155 /* Output a precondition for this macro use */
2156 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2162 long line
= ip
->lineno
;
2163 long col
= ip
->colno
;
2164 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2166 buf
= (char *) alloca (10);
2167 sprintf (buf
, "%ld", line
);
2173 buf
= (char *) alloca (20);
2174 timebuf
= timestamp (pfile
);
2175 if (hp
->type
== T_DATE
)
2176 sprintf (buf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2177 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2179 sprintf (buf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2183 case T_SPEC_DEFINED
:
2184 buf
= " 0 "; /* Assume symbol is not defined */
2185 ip
= CPP_BUFFER (pfile
);
2186 SKIP_WHITE_SPACE (ip
->cur
);
2187 if (*ip
->cur
== '(')
2190 ip
->cur
++; /* Skip over the paren */
2191 SKIP_WHITE_SPACE (ip
->cur
);
2194 if (!is_idstart
[*ip
->cur
])
2196 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2198 if ((hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1)))
2201 if (pcp_outfile
&& pcp_inside_if
2202 && (hp
->type
== T_CONST
2203 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2204 /* Output a precondition for this macro use. */
2205 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2211 if (pcp_outfile
&& pcp_inside_if
)
2213 /* Output a precondition for this macro use */
2214 U_CHAR
*cp
= ip
->bufp
;
2215 fprintf (pcp_outfile
, "#undef ");
2216 while (is_idchar
[*cp
]) /* Ick! */
2217 fputc (*cp
++, pcp_outfile
);
2218 putc ('\n', pcp_outfile
);
2221 while (is_idchar
[*ip
->cur
])
2223 SKIP_WHITE_SPACE (ip
->cur
);
2226 if (*ip
->cur
!= ')')
2234 cpp_error (pfile
, "`defined' without an identifier");
2238 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2242 CPP_RESERVE (pfile
, len
+ 1);
2243 CPP_PUTS_Q (pfile
, buf
, len
);
2244 CPP_NUL_TERMINATE_Q (pfile
);
2249 /* Write out a #define command for the special named MACRO_NAME
2250 to PFILE's token_buffer. */
2253 dump_special_to_buffer (pfile
, macro_name
)
2257 static char define_directive
[] = "#define ";
2258 int macro_name_length
= strlen (macro_name
);
2259 output_line_command (pfile
, 0, same_file
);
2260 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2261 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2262 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2263 CPP_PUTC_Q (pfile
, ' ');
2264 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2265 CPP_PUTC (pfile
, '\n');
2268 /* Initialize the built-in macros. */
2271 initialize_builtins (pfile
)
2274 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2275 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2276 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2277 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2278 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2279 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2280 #ifndef NO_BUILTIN_SIZE_TYPE
2281 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2283 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2284 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2286 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2287 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2288 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2289 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2290 if (!CPP_TRADITIONAL (pfile
))
2291 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2292 if (CPP_OPTIONS (pfile
)->objc
)
2293 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2294 /* This is supplied using a -D by the compiler driver
2295 so that it is present only when truly compiling with GNU C. */
2296 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2298 if (CPP_OPTIONS (pfile
)->debug_output
)
2300 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2301 dump_special_to_buffer (pfile
, "__VERSION__");
2302 #ifndef NO_BUILTIN_SIZE_TYPE
2303 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2305 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2306 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2308 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2309 dump_special_to_buffer (pfile
, "__DATE__");
2310 dump_special_to_buffer (pfile
, "__TIME__");
2311 if (!CPP_TRADITIONAL (pfile
))
2312 dump_special_to_buffer (pfile
, "__STDC__");
2313 if (CPP_OPTIONS (pfile
)->objc
)
2314 dump_special_to_buffer (pfile
, "__OBJC__");
2318 /* Return 1 iff a token ending in C1 followed directly by a token C2
2319 could cause mis-tokenization. */
2322 unsafe_chars (c1
, c2
)
2328 if (c2
== c1
|| c2
== '=')
2332 case '0': case '1': case '2': case '3': case '4':
2333 case '5': case '6': case '7': case '8': case '9':
2334 case 'e': case 'E': case 'p': case 'P':
2335 if (c2
== '-' || c2
== '+')
2336 return 1; /* could extend a pre-processing number */
2339 if (c2
== '\'' || c2
== '\"')
2340 return 1; /* Could turn into L"xxx" or L'xxx'. */
2344 case 'a': case 'b': case 'c': case 'd': case 'f':
2345 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2346 case 'm': case 'n': case 'o': case 'q': case 'r':
2347 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2349 case 'A': case 'B': case 'C': case 'D': case 'F':
2350 case 'G': case 'H': case 'I': case 'J': case 'K':
2351 case 'M': case 'N': case 'O': case 'Q': case 'R':
2352 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2354 /* We're in the middle of either a name or a pre-processing number. */
2355 return (is_idchar
[c2
] || c2
== '.');
2356 case '<': case '>': case '!': case '%': case '#': case ':':
2357 case '^': case '&': case '|': case '*': case '/': case '=':
2358 return (c2
== c1
|| c2
== '=');
2363 /* Expand a macro call.
2364 HP points to the symbol that is the macro being called.
2365 Put the result of expansion onto the input stack
2366 so that subsequent input by our caller will use it.
2368 If macro wants arguments, caller has already verified that
2369 an argument list follows; arguments come from the input stack. */
2372 macroexpand (pfile
, hp
)
2377 DEFINITION
*defn
= hp
->value
.defn
;
2378 register U_CHAR
*xbuf
;
2379 long start_line
, start_column
;
2381 struct argdata
*args
;
2382 long old_written
= CPP_WRITTEN (pfile
);
2384 int start_line
= instack
[indepth
].lineno
;
2386 int rest_args
, rest_zero
;
2390 CHECK_DEPTH (return;);
2394 /* This macro is being used inside a #if, which means it must be */
2395 /* recorded as a precondition. */
2396 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2397 dump_single_macro (hp
, pcp_outfile
);
2400 pfile
->output_escapes
++;
2401 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2403 nargs
= defn
->nargs
;
2407 enum cpp_token token
;
2409 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2411 for (i
= 0; i
< nargs
; i
++)
2413 args
[i
].raw
= args
[i
].expanded
= 0;
2414 args
[i
].raw_length
= 0;
2415 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2416 args
[i
].use_count
= 0;
2419 /* Parse all the macro args that are supplied. I counts them.
2420 The first NARGS args are stored in ARGS.
2421 The rest are discarded. If rest_args is set then we assume
2422 macarg absorbed the rest of the args. */
2426 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2431 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2433 /* if we are working on last arg which absorbs rest of args... */
2434 if (i
== nargs
- 1 && defn
->rest_args
)
2436 args
[i
].raw
= CPP_WRITTEN (pfile
);
2437 token
= macarg (pfile
, rest_args
);
2438 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2439 args
[i
].newlines
= 0; /* FIXME */
2442 token
= macarg (pfile
, 0);
2443 if (token
== CPP_EOF
|| token
== CPP_POP
)
2445 cpp_error_with_line (pfile
, start_line
, start_column
,
2446 "unterminated macro call");
2450 } while (token
== CPP_COMMA
);
2452 /* If we got one arg but it was just whitespace, call that 0 args. */
2455 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2456 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2457 /* cpp.texi says for foo ( ) we provide one argument.
2458 However, if foo wants just 0 arguments, treat this as 0. */
2460 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2465 /* Don't output an error message if we have already output one for
2466 a parse error above. */
2468 if (nargs
== 0 && i
> 0)
2470 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2474 /* traditional C allows foo() if foo wants one argument. */
2475 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2477 /* the rest args token is allowed to absorb 0 tokens */
2478 else if (i
== nargs
- 1 && defn
->rest_args
)
2481 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2483 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2485 cpp_error (pfile
, "macro `%s' used with only %d args",
2491 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2495 /* If macro wants zero args, we parsed the arglist for checking only.
2496 Read directly from the macro definition. */
2499 xbuf
= defn
->expansion
;
2500 xbuf_len
= defn
->length
;
2504 register U_CHAR
*exp
= defn
->expansion
;
2505 register int offset
; /* offset in expansion,
2506 copied a piece at a time */
2507 register int totlen
; /* total amount of exp buffer filled so far */
2509 register struct reflist
*ap
, *last_ap
;
2511 /* Macro really takes args. Compute the expansion of this call. */
2513 /* Compute length in characters of the macro's expansion.
2514 Also count number of times each arg is used. */
2515 xbuf_len
= defn
->length
;
2516 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2520 register struct argdata
*arg
= &args
[ap
->argno
];
2521 /* Stringify if it hasn't already been */
2522 if (arg
->stringified_length
< 0)
2524 int arglen
= arg
->raw_length
;
2528 /* Initially need_space is -1. Otherwise, 1 means the
2529 previous character was a space, but we suppressed it;
2530 0 means the previous character was a non-space. */
2531 int need_space
= -1;
2533 arg
->stringified
= CPP_WRITTEN (pfile
);
2534 if (!CPP_TRADITIONAL (pfile
))
2535 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2536 for (; i
< arglen
; i
++)
2538 c
= (ARG_BASE
+ arg
->raw
)[i
];
2542 /* Internal sequences of whitespace are replaced by
2543 one space except within an string or char token.*/
2546 if (CPP_WRITTEN (pfile
) > (unsigned)arg
->stringified
2547 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2549 /* "@ " escape markers are removed */
2550 CPP_ADJUST_WRITTEN (pfile
, -1);
2553 if (need_space
== 0)
2557 else if (need_space
> 0)
2558 CPP_PUTC (pfile
, ' ');
2573 else if (c
== '\"' || c
== '\'')
2577 /* Escape these chars */
2578 if (c
== '\"' || (in_string
&& c
== '\\'))
2579 CPP_PUTC (pfile
, '\\');
2581 CPP_PUTC (pfile
, c
);
2584 CPP_RESERVE (pfile
, 4);
2585 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2587 CPP_ADJUST_WRITTEN (pfile
, 4);
2590 if (!CPP_TRADITIONAL (pfile
))
2591 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2592 arg
->stringified_length
2593 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2595 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2597 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2598 /* Add 4 for two newline-space markers to prevent
2599 token concatenation. */
2600 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2603 /* We have an ordinary (expanded) occurrence of the arg.
2604 So compute its expansion, if we have not already. */
2605 if (args
[ap
->argno
].expand_length
< 0)
2607 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2608 cpp_expand_to_buffer (pfile
,
2609 ARG_BASE
+ args
[ap
->argno
].raw
,
2610 args
[ap
->argno
].raw_length
);
2612 args
[ap
->argno
].expand_length
2613 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2616 /* Add 4 for two newline-space markers to prevent
2617 token concatenation. */
2618 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2620 if (args
[ap
->argno
].use_count
< 10)
2621 args
[ap
->argno
].use_count
++;
2624 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2626 /* Generate in XBUF the complete expansion
2627 with arguments substituted in.
2628 TOTLEN is the total size generated so far.
2629 OFFSET is the index in the definition
2630 of where we are copying from. */
2631 offset
= totlen
= 0;
2632 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2633 last_ap
= ap
, ap
= ap
->next
)
2635 register struct argdata
*arg
= &args
[ap
->argno
];
2636 int count_before
= totlen
;
2638 /* Add chars to XBUF. */
2639 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2640 xbuf
[totlen
++] = exp
[offset
];
2642 /* If followed by an empty rest arg with concatenation,
2643 delete the last run of nonwhite chars. */
2644 if (rest_zero
&& totlen
> count_before
2645 && ((ap
->rest_args
&& ap
->raw_before
)
2646 || (last_ap
!= NULL
&& last_ap
->rest_args
2647 && last_ap
->raw_after
)))
2649 /* Delete final whitespace. */
2650 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2653 /* Delete the nonwhites before them. */
2654 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2658 if (ap
->stringify
!= 0)
2660 bcopy (ARG_BASE
+ arg
->stringified
,
2661 xbuf
+ totlen
, arg
->stringified_length
);
2662 totlen
+= arg
->stringified_length
;
2664 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2666 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2667 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2670 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2671 while (p1
!= l1
&& is_idchar
[*p1
])
2672 xbuf
[totlen
++] = *p1
++;
2676 /* Arg is concatenated after: delete trailing whitespace,
2677 whitespace markers, and no-reexpansion markers. */
2680 if (is_space
[l1
[-1]]) l1
--;
2681 else if (l1
[-1] == '@')
2683 U_CHAR
*p2
= l1
- 1;
2684 /* If whitespace is preceded by an odd number
2685 of `@' signs, the last `@' was a whitespace
2686 marker; drop it too. */
2687 while (p2
!= p1
&& p2
[0] == '@') p2
--;
2692 else if (l1
[-1] == '-')
2694 U_CHAR
*p2
= l1
- 1;
2695 /* If a `-' is preceded by an odd number of
2696 `@' signs then it and the last `@' are
2697 a no-reexpansion marker. */
2698 while (p2
!= p1
&& p2
[0] == '@') p2
--;
2708 /* Delete any no-reexpansion marker that precedes
2709 an identifier at the beginning of the argument. */
2710 if (p1
[0] == '@' && p1
[1] == '-')
2713 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2718 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2719 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2720 && !CPP_TRADITIONAL(pfile
)
2721 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2723 xbuf
[totlen
++] = '@';
2724 xbuf
[totlen
++] = ' ';
2727 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2728 totlen
+= arg
->expand_length
;
2730 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2731 && !CPP_TRADITIONAL(pfile
)
2732 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2734 xbuf
[totlen
++] = '@';
2735 xbuf
[totlen
++] = ' ';
2738 /* If a macro argument with newlines is used multiple times,
2739 then only expand the newlines once. This avoids creating
2740 output lines which don't correspond to any input line,
2741 which confuses gdb and gcov. */
2742 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2744 /* Don't bother doing change_newlines for subsequent
2748 = change_newlines (expanded
, arg
->expand_length
);
2752 if (totlen
> xbuf_len
)
2756 /* if there is anything left of the definition
2757 after handling the arg list, copy that in too. */
2759 for (i
= offset
; i
< defn
->length
; i
++)
2761 /* if we've reached the end of the macro */
2764 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2765 && last_ap
->raw_after
))
2766 xbuf
[totlen
++] = exp
[i
];
2774 pfile
->output_escapes
--;
2776 /* Now put the expansion on the input stack
2777 so our caller will commence reading from it. */
2778 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2779 CPP_BUFFER (pfile
)->has_escapes
= 1;
2781 /* Pop the space we've used in the token_buffer for argument expansion. */
2782 CPP_SET_WRITTEN (pfile
, old_written
);
2784 /* Recursive macro use sometimes works traditionally.
2785 #define foo(x,y) bar (x (y,0), y)
2788 if (!CPP_TRADITIONAL (pfile
))
2789 hp
->type
= T_DISABLED
;
2793 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
2795 register U_CHAR
*xbuf
;
2799 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
2802 mbuf
->cleanup
= macro_cleanup
;
2805 /* The first chars of the expansion should be a "@ " added by
2806 collect_expansion. This is to prevent accidental token-pasting
2807 between the text preceding the macro invocation, and the macro
2810 We would like to avoid adding unneeded spaces (for the sake of
2811 tools that use cpp, such as imake). In some common cases we can
2812 tell that it is safe to omit the space.
2814 The character before the macro invocation cannot have been an
2815 idchar (or else it would have been pasted with the idchars of
2816 the macro name). Therefore, if the first non-space character
2817 of the expansion is an idchar, we do not need the extra space
2818 to prevent token pasting.
2820 Also, we don't need the extra space if the first char is '(',
2821 or some other (less common) characters. */
2823 if (xbuf
[0] == '@' && xbuf
[1] == ' '
2824 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
2825 || xbuf
[2] == '\"'))
2829 /* Like cpp_get_token, except that it does not read past end-of-line.
2830 Also, horizontal space is skipped, and macros are popped. */
2832 static enum cpp_token
2833 get_directive_token (pfile
)
2838 long old_written
= CPP_WRITTEN (pfile
);
2839 enum cpp_token token
;
2840 cpp_skip_hspace (pfile
);
2841 if (PEEKC () == '\n')
2843 token
= cpp_get_token (pfile
);
2847 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2849 /* ... else fall though ... */
2850 case CPP_HSPACE
: case CPP_COMMENT
:
2851 CPP_SET_WRITTEN (pfile
, old_written
);
2859 /* Handle #include and #import.
2860 This function expects to see "fname" or <fname> on the input.
2862 The input is normally in part of the output_buffer following
2863 CPP_WRITTEN, and will get overwritten by output_line_command.
2864 I.e. in input file specification has been popped by handle_directive.
2868 do_include (pfile
, keyword
)
2870 struct directive
*keyword
;
2872 int importing
= (keyword
->type
== T_IMPORT
);
2873 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
2874 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
2875 int before
; /* included before? */
2880 enum cpp_token token
;
2882 /* Chain of dirs to search */
2883 struct include_hash
*ihash
;
2884 struct file_name_list
*search_start
;
2886 long old_written
= CPP_WRITTEN (pfile
);
2890 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
2893 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
2895 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
2898 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
2899 && !CPP_OPTIONS (pfile
)->inhibit_warnings
2900 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
2902 pfile
->import_warning
= 1;
2903 cpp_warning (pfile
, "`#import' is obsolete, use an #ifdef wrapper in the header file");
2906 pfile
->parsing_include_directive
++;
2907 token
= get_directive_token (pfile
);
2908 pfile
->parsing_include_directive
--;
2910 if (token
== CPP_STRING
)
2912 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
2913 fend
= CPP_PWRITTEN (pfile
) - 1;
2915 if (fbeg
[-1] == '<')
2919 else if (token
== CPP_NAME
)
2921 /* Support '#include xyz' like VAX-C to allow for easy use of
2922 * all the decwindow include files. It defaults to '#include
2923 * <xyz.h>' and generates a warning. */
2925 "VAX-C-style include specification found, use '#include <filename.h>' !");
2928 /* Append the missing `.h' to the name. */
2929 CPP_PUTS (pfile
, ".h", 3)
2930 CPP_NUL_TERMINATE_Q (pfile
);
2932 fbeg
= pfile
->token_buffer
+ old_written
;
2933 fend
= CPP_PWRITTEN (pfile
);
2939 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
2940 CPP_SET_WRITTEN (pfile
, old_written
);
2941 skip_rest_of_line (pfile
);
2945 token
= get_directive_token (pfile
);
2946 if (token
!= CPP_VSPACE
)
2948 cpp_error (pfile
, "junk at end of `#include'");
2949 skip_rest_of_line (pfile
);
2952 CPP_SET_WRITTEN (pfile
, old_written
);
2958 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
2964 for (fp
= CPP_BUFFER (pfile
);
2965 fp
!= CPP_NULL_BUFFER (pfile
);
2966 fp
= CPP_PREV_BUFFER (fp
))
2967 if (fp
->fname
!= NULL
)
2970 if (fp
== CPP_NULL_BUFFER (pfile
))
2972 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
2976 /* For #include_next, skip in the search path past the dir in which the
2977 containing file was found. Treat files specified using an absolute path
2978 as if there are no more directories to search. Treat the primary source
2979 file like any other included source, but generate a warning. */
2980 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
2982 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
2983 search_start
= fp
->ihash
->foundhere
->next
;
2988 cpp_warning (pfile
, "#include_next in primary source file");
2991 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
2994 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
2997 search_start
= fp
->actual_dir
;
3000 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
3006 cpp_error (pfile
, "No include path in which to find %s", fbeg
);
3010 fd
= find_include_file (pfile
, fbeg
, search_start
, &ihash
, &before
);
3017 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
3018 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
3019 (pfile
->system_include_depth
> 0)))
3021 if (!angle_brackets
)
3022 deps_output (pfile
, fbeg
, ' ');
3026 struct file_name_list
*ptr
;
3027 /* If requested as a system header, assume it belongs in
3028 the first system header directory. */
3029 if (CPP_OPTIONS (pfile
)->bracket_include
)
3030 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
3032 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
3034 p
= (char *) alloca (strlen (ptr
->name
)
3035 + strlen (fbeg
) + 2);
3036 if (*ptr
->name
!= '\0')
3038 strcpy (p
, ptr
->name
);
3042 deps_output (pfile
, p
, ' ');
3045 /* If -M was specified, and this header file won't be added to
3046 the dependency list, then don't count this as an error,
3047 because we can still produce correct output. Otherwise, we
3048 can't produce correct output, because there may be
3049 dependencies we need inside the missing file, and we don't
3050 know what directory this missing file exists in. */
3051 else if (CPP_PRINT_DEPS (pfile
)
3052 && (CPP_PRINT_DEPS (pfile
)
3053 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3054 cpp_warning (pfile
, "No include path in which to find %s", fbeg
);
3056 cpp_error_from_errno (pfile
, fbeg
);
3061 /* For -M, add the file to the dependencies on its first inclusion. */
3062 if (!before
&& (CPP_PRINT_DEPS (pfile
)
3063 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3064 deps_output (pfile
, ihash
->name
, ' ');
3066 /* Handle -H option. */
3067 if (CPP_OPTIONS(pfile
)->print_include_names
)
3069 fp
= CPP_BUFFER (pfile
);
3070 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
3072 fprintf (stderr
, " %s\n", ihash
->name
);
3075 /* Actually process the file */
3078 ihash
->control_macro
= "";
3080 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3087 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
3089 if (finclude (pfile
, fd
, ihash
))
3091 output_line_command (pfile
, 0, enter_file
);
3092 pfile
->only_seen_white
= 2;
3099 /* Convert a character string literal into a nul-terminated string.
3100 The input string is [IN ... LIMIT).
3101 The result is placed in RESULT. RESULT can be the same as IN.
3102 The value returned in the end of the string written to RESULT,
3103 or NULL on error. */
3106 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3108 register U_CHAR
*result
, *in
, *limit
;
3128 char *bpc
= (char *) in
;
3129 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
, 0x00ff);
3130 in
= (U_CHAR
*) bpc
;
3132 *result
++ = (U_CHAR
)c
;
3135 /* else fall through */
3145 * interpret #line command. Remembers previously seen fnames
3146 * in its very own hash table.
3148 #define FNAME_HASHSIZE 37
3151 do_line (pfile
, keyword
)
3153 struct directive
*keyword ATTRIBUTE_UNUSED
;
3155 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3157 long old_written
= CPP_WRITTEN (pfile
);
3158 enum file_change_code file_change
= same_file
;
3159 enum cpp_token token
;
3161 token
= get_directive_token (pfile
);
3163 if (token
!= CPP_NUMBER
3164 || !ISDIGIT(pfile
->token_buffer
[old_written
]))
3166 cpp_error (pfile
, "invalid format `#line' command");
3167 goto bad_line_directive
;
3170 /* The Newline at the end of this line remains to be processed.
3171 To put the next line at the specified line number,
3172 we must store a line number now that is one less. */
3173 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3174 CPP_SET_WRITTEN (pfile
, old_written
);
3176 /* NEW_LINENO is one less than the actual line number here. */
3177 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3178 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3180 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3181 if (PEEKC() && !is_space
[PEEKC()]) {
3182 cpp_error (pfile
, "invalid format `#line' command");
3183 goto bad_line_directive
;
3187 token
= get_directive_token (pfile
);
3189 if (token
== CPP_STRING
) {
3190 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3192 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3193 HASHNODE
*hp
, **hash_bucket
;
3198 /* Turn the file name, which is a character string literal,
3199 into a null-terminated string. Do this in place. */
3200 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3201 if (end_name
== NULL
)
3203 cpp_error (pfile
, "invalid format `#line' command");
3204 goto bad_line_directive
;
3207 fname_length
= end_name
- fname
;
3209 num_start
= CPP_WRITTEN (pfile
);
3210 token
= get_directive_token (pfile
);
3211 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3212 p
= pfile
->token_buffer
+ num_start
;
3213 if (CPP_PEDANTIC (pfile
))
3214 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3216 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3218 cpp_error (pfile
, "invalid format `#line' command");
3219 goto bad_line_directive
;
3222 file_change
= enter_file
;
3224 file_change
= leave_file
;
3226 ip
->system_header_p
= 1;
3227 else /* if (*p == '4') */
3228 ip
->system_header_p
= 2;
3230 CPP_SET_WRITTEN (pfile
, num_start
);
3231 token
= get_directive_token (pfile
);
3232 p
= pfile
->token_buffer
+ num_start
;
3233 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3234 ip
->system_header_p
= *p
== '3' ? 1 : 2;
3235 token
= get_directive_token (pfile
);
3237 if (token
!= CPP_VSPACE
) {
3238 cpp_error (pfile
, "invalid format `#line' command");
3239 goto bad_line_directive
;
3243 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3244 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3245 if (hp
->length
== fname_length
3246 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3247 ip
->nominal_fname
= hp
->value
.cpval
;
3251 /* Didn't find it; cons up a new one. */
3252 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3253 hp
->next
= *hash_bucket
;
3256 hp
->length
= fname_length
;
3257 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3258 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3261 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3262 cpp_error (pfile
, "invalid format `#line' command");
3263 goto bad_line_directive
;
3266 ip
->lineno
= new_lineno
;
3268 skip_rest_of_line (pfile
);
3269 CPP_SET_WRITTEN (pfile
, old_written
);
3270 output_line_command (pfile
, 0, file_change
);
3275 * remove the definition of a symbol from the symbol table.
3276 * according to un*x /lib/cpp, it is not an error to undef
3277 * something that has no definitions, so it isn't one here either.
3281 do_undef (pfile
, keyword
)
3283 struct directive
*keyword
;
3287 U_CHAR
*buf
, *name
, *limit
;
3289 long here
= CPP_WRITTEN (pfile
);
3290 enum cpp_token token
;
3292 cpp_skip_hspace (pfile
);
3294 if (! is_idstart
[c
])
3296 cpp_error (pfile
, "token after #undef is not an identifier");
3297 skip_rest_of_line (pfile
);
3301 parse_name (pfile
, c
);
3302 buf
= pfile
->token_buffer
+ here
;
3303 limit
= CPP_PWRITTEN(pfile
);
3305 /* Copy out the token so we can pop the token buffer. */
3306 name
= alloca (limit
- buf
+ 1);
3307 bcopy(buf
, name
, limit
- buf
);
3308 name
[limit
- buf
] = '\0';
3310 token
= get_directive_token (pfile
);
3311 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
3313 cpp_pedwarn (pfile
, "junk on line after #undef");
3314 skip_rest_of_line (pfile
);
3317 CPP_SET_WRITTEN (pfile
, here
);
3320 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3321 if (pcp_outfile
&& keyword
)
3322 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3325 sym_length
= check_macro_name (pfile
, name
, "macro");
3327 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
3329 /* If we are generating additional info for debugging (with -g) we
3330 need to pass through all effective #undef commands. */
3331 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3332 pass_thru_directive (name
, name
+sym_length
, pfile
, keyword
);
3333 if (hp
->type
!= T_MACRO
)
3334 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3341 /* Wrap do_undef for -U processing. */
3343 cpp_undef (pfile
, macro
)
3347 if (cpp_push_buffer (pfile
, macro
, strlen(macro
)))
3349 do_undef (pfile
, NULL
);
3350 cpp_pop_buffer (pfile
);
3356 * Report an error detected by the program we are processing.
3357 * Use the text of the line in the error message.
3358 * (We use error because it prints the filename & line#.)
3362 do_error (pfile
, keyword
)
3364 struct directive
*keyword ATTRIBUTE_UNUSED
;
3366 long here
= CPP_WRITTEN (pfile
);
3368 copy_rest_of_line (pfile
);
3369 text
= pfile
->token_buffer
+ here
;
3370 SKIP_WHITE_SPACE(text
);
3372 cpp_error (pfile
, "#error %s", text
);
3373 CPP_SET_WRITTEN (pfile
, here
);
3378 * Report a warning detected by the program we are processing.
3379 * Use the text of the line in the warning message, then continue.
3383 do_warning (pfile
, keyword
)
3385 struct directive
*keyword ATTRIBUTE_UNUSED
;
3388 long here
= CPP_WRITTEN(pfile
);
3389 copy_rest_of_line (pfile
);
3390 text
= pfile
->token_buffer
+ here
;
3391 SKIP_WHITE_SPACE(text
);
3393 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3394 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
3396 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3397 if -pedantic-errors is given, #warning should cause an error. */
3398 cpp_pedwarn (pfile
, "#warning %s", text
);
3399 CPP_SET_WRITTEN (pfile
, here
);
3403 /* Report program identification. */
3406 do_ident (pfile
, keyword
)
3408 struct directive
*keyword ATTRIBUTE_UNUSED
;
3410 /* Allow #ident in system headers, since that's not user's fault. */
3411 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3412 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3414 skip_rest_of_line (pfile
); /* Correct? Appears to match cccp. */
3419 /* Just check for some recognized pragmas that need validation here,
3420 and leave the text in the token buffer to be output. */
3423 do_pragma (pfile
, keyword
)
3425 struct directive
*keyword ATTRIBUTE_UNUSED
;
3427 long here
= CPP_WRITTEN (pfile
);
3430 copy_rest_of_line (pfile
);
3431 buf
= pfile
->token_buffer
+ here
;
3432 SKIP_WHITE_SPACE (buf
);
3434 if (!strncmp (buf
, "once", 4))
3436 cpp_buffer
*ip
= NULL
;
3438 /* Allow #pragma once in system headers, since that's not the user's
3440 if (!CPP_BUFFER (pfile
)->system_header_p
)
3441 cpp_warning (pfile
, "`#pragma once' is obsolete");
3443 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3445 if (ip
== CPP_NULL_BUFFER (pfile
))
3447 if (ip
->fname
!= NULL
)
3451 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
3452 cpp_warning (pfile
, "`#pragma once' outside include file");
3454 ip
->ihash
->control_macro
= ""; /* never repeat */
3457 if (!strncmp (buf
, "implementation", 14))
3459 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3460 been included yet. */
3461 struct include_hash
*ptr
;
3462 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
3463 SKIP_WHITE_SPACE (p
);
3464 if (*p
== '\n' || *p
!= '\"')
3468 p
= (U_CHAR
*) index (fname
, '\"');
3470 fcopy
= alloca (p
- fname
+ 1);
3471 bcopy (fname
, fcopy
, p
- fname
);
3472 fcopy
[p
-fname
] = '\0';
3474 ptr
= include_hash (pfile
, fcopy
, 0);
3477 "`#pragma implementation' for `%s' appears after file is included",
3484 #ifdef SCCS_DIRECTIVE
3485 /* Just ignore #sccs, on systems where we define it at all. */
3488 do_sccs (pfile
, keyword
)
3490 struct directive
*keyword ATTRIBUTE_UNUSED
;
3492 if (CPP_PEDANTIC (pfile
))
3493 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
3494 skip_rest_of_line (pfile
);
3500 * handle #if command by
3501 * 1) inserting special `defined' keyword into the hash table
3502 * that gets turned into 0 or 1 by special_symbol (thus,
3503 * if the luser has a symbol called `defined' already, it won't
3504 * work inside the #if command)
3505 * 2) rescan the input into a temporary output buffer
3506 * 3) pass the output buffer to the yacc parser and collect a value
3507 * 4) clean up the mess left from steps 1 and 2.
3508 * 5) call conditional_skip to skip til the next #endif (etc.),
3509 * or not, depending on the value from step 3.
3513 do_if (pfile
, keyword
)
3515 struct directive
*keyword ATTRIBUTE_UNUSED
;
3517 HOST_WIDE_INT value
= eval_if_expression (pfile
);
3518 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
3523 * handle a #elif directive by not changing if_stack either.
3524 * see the comment above do_else.
3528 do_elif (pfile
, keyword
)
3530 struct directive
*keyword ATTRIBUTE_UNUSED
;
3532 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
3533 cpp_error (pfile
, "`#elif' not within a conditional");
3536 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
3537 cpp_error (pfile
, "`#elif' after `#else'");
3539 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
3541 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
3542 && strcmp (pfile
->if_stack
->fname
,
3543 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
3544 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
3545 fprintf (stderr
, ")\n");
3547 pfile
->if_stack
->type
= T_ELIF
;
3550 if (pfile
->if_stack
->if_succeeded
)
3551 skip_if_group (pfile
);
3553 HOST_WIDE_INT value
= eval_if_expression (pfile
);
3555 skip_if_group (pfile
);
3557 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
3558 output_line_command (pfile
, 1, same_file
);
3565 * evaluate a #if expression in BUF, of length LENGTH,
3566 * then parse the result as a C expression and return the value as an int.
3569 static HOST_WIDE_INT
3570 eval_if_expression (pfile
)
3573 HASHNODE
*save_defined
;
3574 HOST_WIDE_INT value
;
3575 long old_written
= CPP_WRITTEN (pfile
);
3577 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
3578 pfile
->pcp_inside_if
= 1;
3580 value
= cpp_parse_expr (pfile
);
3581 pfile
->pcp_inside_if
= 0;
3582 delete_macro (save_defined
); /* clean up special symbol */
3584 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
3590 * routine to handle ifdef/ifndef. Try to look up the symbol,
3591 * then do or don't skip to the #endif/#else/#elif depending
3592 * on what directive is actually being processed.
3596 do_xifdef (pfile
, keyword
)
3598 struct directive
*keyword
;
3601 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3604 enum cpp_token token
;
3605 int start_of_file
= 0;
3606 U_CHAR
*control_macro
= 0;
3607 int old_written
= CPP_WRITTEN (pfile
);
3609 /* Detect a #ifndef at start of file (not counting comments). */
3610 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
3611 start_of_file
= pfile
->only_seen_white
== 2;
3613 pfile
->no_macro_expand
++;
3614 token
= get_directive_token (pfile
);
3615 pfile
->no_macro_expand
--;
3617 ident
= pfile
->token_buffer
+ old_written
;
3618 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
3619 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
3621 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
3623 skip
= (keyword
->type
== T_IFDEF
);
3624 if (! CPP_TRADITIONAL (pfile
))
3625 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
3627 else if (token
== CPP_NAME
)
3629 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
3630 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
3631 if (start_of_file
&& !skip
)
3633 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
3634 bcopy (ident
, control_macro
, ident_length
+ 1);
3639 skip
= (keyword
->type
== T_IFDEF
);
3640 if (! CPP_TRADITIONAL (pfile
))
3641 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
3644 if (!CPP_TRADITIONAL (pfile
))
3646 cpp_skip_hspace (pfile
);
3648 if (c
!= EOF
&& c
!= '\n')
3649 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
3651 skip_rest_of_line (pfile
);
3655 /* Output a precondition for this macro. */
3656 if (hp
&& hp
->value
.defn
->predefined
)
3657 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
3660 fprintf (pcp_outfile
, "#undef ");
3661 while (is_idchar
[*cp
]) /* Ick! */
3662 fputc (*cp
++, pcp_outfile
);
3663 putc ('\n', pcp_outfile
);
3667 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
3671 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3672 If this is a #ifndef starting at the beginning of a file,
3673 CONTROL_MACRO is the macro name tested by the #ifndef.
3674 Otherwise, CONTROL_MACRO is 0. */
3677 conditional_skip (pfile
, skip
, type
, control_macro
)
3680 enum node_type type
;
3681 U_CHAR
*control_macro
;
3683 IF_STACK_FRAME
*temp
;
3685 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3686 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
3688 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
3690 temp
->next
= pfile
->if_stack
;
3691 temp
->control_macro
= control_macro
;
3692 pfile
->if_stack
= temp
;
3694 pfile
->if_stack
->type
= type
;
3697 skip_if_group (pfile
);
3700 ++pfile
->if_stack
->if_succeeded
;
3701 output_line_command (pfile
, 1, same_file
);
3705 /* Subroutine of skip_if_group. Examine one preprocessing directive and
3706 return 0 if skipping should continue, 1 if it should halt. Also
3707 adjusts the if_stack as appropriate.
3708 The `#' has been read, but not the identifier. */
3711 consider_directive_while_skipping (pfile
, stack
)
3713 IF_STACK_FRAME
*stack
;
3715 long ident_len
, ident
;
3716 struct directive
*kt
;
3717 IF_STACK_FRAME
*temp
;
3719 cpp_skip_hspace (pfile
);
3721 ident
= CPP_WRITTEN (pfile
);
3722 parse_name (pfile
, GETC());
3723 ident_len
= CPP_WRITTEN (pfile
) - ident
;
3725 CPP_SET_WRITTEN (pfile
, ident
);
3727 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
3728 if (kt
->length
== ident_len
3729 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
3735 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
3736 temp
->next
= pfile
->if_stack
;
3737 pfile
->if_stack
= temp
;
3738 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
3739 temp
->type
= kt
->type
;
3743 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
3744 validate_else (pfile
, "#else");
3747 if (pfile
->if_stack
->type
== T_ELSE
)
3748 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
3750 if (pfile
->if_stack
== stack
)
3754 pfile
->if_stack
->type
= kt
->type
;
3759 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
3760 validate_else (pfile
, "#endif");
3762 if (pfile
->if_stack
== stack
)
3765 temp
= pfile
->if_stack
;
3766 pfile
->if_stack
= temp
->next
;
3774 /* Don't let erroneous code go by. */
3775 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
3776 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
3780 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
3781 * leaves input ptr at the sharp sign found.
3784 skip_if_group (pfile
)
3788 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
3789 U_CHAR
*beg_of_line
;
3792 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3794 CPP_PUTS (pfile
, "#failed\n", 8);
3796 output_line_command (pfile
, 1, same_file
);
3799 old_written
= CPP_WRITTEN (pfile
);
3803 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
3805 if (! CPP_TRADITIONAL (pfile
))
3806 cpp_skip_hspace (pfile
);
3810 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3811 CPP_PUTC (pfile
, c
);
3816 if (consider_directive_while_skipping (pfile
, save_if_stack
))
3820 return; /* Caller will issue error. */
3823 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3825 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
3826 copy_rest_of_line (pfile
);
3830 copy_rest_of_line (pfile
);
3831 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
3836 return; /* Caller will issue error. */
3840 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3841 CPP_PUTC (pfile
, c
);
3845 /* Back up to the beginning of this line. Caller will process the
3847 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
3848 pfile
->only_seen_white
= 1;
3849 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3851 CPP_PUTS (pfile
, "#endfailed\n", 11);
3857 * handle a #else directive. Do this by just continuing processing
3858 * without changing if_stack ; this is so that the error message
3859 * for missing #endif's etc. will point to the original #if. It
3860 * is possible that something different would be better.
3864 do_else (pfile
, keyword
)
3866 struct directive
*keyword ATTRIBUTE_UNUSED
;
3868 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3870 if (CPP_PEDANTIC (pfile
))
3871 validate_else (pfile
, "#else");
3872 skip_rest_of_line (pfile
);
3874 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
3875 cpp_error (pfile
, "`#else' not within a conditional");
3878 /* #ifndef can't have its special treatment for containing the whole file
3879 if it has a #else clause. */
3880 pfile
->if_stack
->control_macro
= 0;
3882 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
3883 cpp_error (pfile
, "`#else' after `#else'");
3884 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
3885 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
3886 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
3887 fprintf (stderr
, ")\n");
3889 pfile
->if_stack
->type
= T_ELSE
;
3892 if (pfile
->if_stack
->if_succeeded
)
3893 skip_if_group (pfile
);
3895 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
3896 output_line_command (pfile
, 1, same_file
);
3902 * unstack after #endif command
3906 do_endif (pfile
, keyword
)
3908 struct directive
*keyword ATTRIBUTE_UNUSED
;
3910 if (CPP_PEDANTIC (pfile
))
3911 validate_else (pfile
, "#endif");
3912 skip_rest_of_line (pfile
);
3914 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
3915 cpp_error (pfile
, "unbalanced `#endif'");
3918 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
3919 pfile
->if_stack
= temp
->next
;
3920 if (temp
->control_macro
!= 0)
3922 /* This #endif matched a #ifndef at the start of the file.
3923 See if it is at the end of the file. */
3924 struct parse_marker start_mark
;
3927 parse_set_mark (&start_mark
, pfile
);
3931 cpp_skip_hspace (pfile
);
3936 parse_goto_mark (&start_mark
, pfile
);
3937 parse_clear_mark (&start_mark
);
3941 /* This #endif ends a #ifndef
3942 that contains all of the file (aside from whitespace).
3943 Arrange not to include the file again
3944 if the macro that was tested is defined. */
3945 struct cpp_buffer
*ip
;
3946 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3947 if (ip
->fname
!= NULL
)
3949 ip
->ihash
->control_macro
= temp
->control_macro
;
3953 output_line_command (pfile
, 1, same_file
);
3958 /* When an #else or #endif is found while skipping failed conditional,
3959 if -pedantic was specified, this is called to warn about text after
3960 the command name. P points to the first char after the command name. */
3963 validate_else (pfile
, directive
)
3968 cpp_skip_hspace (pfile
);
3970 if (c
!= EOF
&& c
!= '\n')
3972 "text following `%s' violates ANSI standard", directive
);
3975 /* Get the next token, and add it to the text in pfile->token_buffer.
3976 Return the kind of token we got. */
3979 cpp_get_token (pfile
)
3982 register int c
, c2
, c3
;
3984 long start_line
, start_column
;
3985 enum cpp_token token
;
3986 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
3987 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
3993 if (CPP_BUFFER (pfile
)->seen_eof
)
3995 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4002 cpp_buffer
*next_buf
4003 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4004 CPP_BUFFER (pfile
)->seen_eof
= 1;
4005 if (CPP_BUFFER (pfile
)->nominal_fname
4006 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4008 /* We're about to return from an #include file.
4009 Emit #line information now (as part of the CPP_POP) result.
4010 But the #line refers to the file we will pop to. */
4011 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4012 CPP_BUFFER (pfile
) = next_buf
;
4013 pfile
->input_stack_listing_current
= 0;
4014 output_line_command (pfile
, 0, leave_file
);
4015 CPP_BUFFER (pfile
) = cur_buffer
;
4025 struct parse_marker start_mark
;
4027 if (PEEKC () == '=')
4029 if (opts
->put_out_comments
)
4030 parse_set_mark (&start_mark
, pfile
);
4032 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4033 &start_line
, &start_column
);
4034 c
= skip_comment (pfile
, &newlines
);
4035 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4036 parse_clear_mark (&start_mark
);
4041 cpp_error_with_line (pfile
, start_line
, start_column
,
4042 "unterminated comment");
4045 c
= '/'; /* Initial letter of comment. */
4047 /* Comments are equivalent to spaces.
4048 For -traditional, a comment is equivalent to nothing. */
4049 if (opts
->put_out_comments
)
4051 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4052 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4053 int len
= pbuf
->cur
- start
;
4054 CPP_RESERVE(pfile
, 1 + len
);
4055 CPP_PUTC_Q (pfile
, c
);
4056 CPP_PUTS_Q (pfile
, start
, len
);
4057 pfile
->lineno
+= newlines
;
4058 parse_clear_mark (&start_mark
);
4061 else if (CPP_TRADITIONAL (pfile
))
4068 /* This may not work if cpp_get_token is called recursively,
4069 since many places look for horizontal space. */
4072 /* Copy the newlines into the output buffer, in order to
4073 avoid the pain of a #line every time a multiline comment
4075 CPP_RESERVE(pfile
, newlines
);
4076 while (--newlines
>= 0)
4078 CPP_PUTC_Q (pfile
, '\n');
4084 CPP_RESERVE(pfile
, 1);
4085 CPP_PUTC_Q (pfile
, ' ');
4089 if (opts
->for_lint
) {
4092 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4094 if (lintcmd
!= NULL
) {
4095 /* I believe it is always safe to emit this newline: */
4097 bcopy ("#pragma lint ", (char *) obp
, 13);
4099 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4104 bcopy (argbp
, (char *) obp
, arglen
);
4108 /* OK, now bring us back to the state we were in before we entered
4109 this branch. We need #line because the newline for the pragma
4110 could mess things up. */
4111 output_line_command (pfile
, 0, same_file
);
4112 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4120 /* If this is expanding a macro definition, don't recognize
4121 preprocessor directives. */
4124 /* If this is expand_into_temp_buffer, recognize them
4125 only after an actual newline at this level,
4126 not at the beginning of the input level. */
4127 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4133 if (!pfile
->only_seen_white
)
4135 if (handle_directive (pfile
))
4136 return CPP_DIRECTIVE
;
4137 pfile
->only_seen_white
= 0;
4142 /* A single quoted string is treated like a double -- some
4143 programs (e.g., troff) are perverse this way */
4144 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4145 &start_line
, &start_column
);
4146 old_written
= CPP_WRITTEN (pfile
);
4148 CPP_PUTC (pfile
, c
);
4154 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4156 /* try harder: this string crosses a macro expansion
4157 boundary. This can happen naturally if -traditional.
4158 Otherwise, only -D can make a macro with an unmatched
4160 cpp_buffer
*next_buf
4161 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4162 (*CPP_BUFFER (pfile
)->cleanup
)
4163 (CPP_BUFFER (pfile
), pfile
);
4164 CPP_BUFFER (pfile
) = next_buf
;
4167 if (!CPP_TRADITIONAL (pfile
))
4169 cpp_error_with_line (pfile
, start_line
, start_column
,
4170 "unterminated string or character constant");
4171 if (pfile
->multiline_string_line
!= start_line
4172 && pfile
->multiline_string_line
!= 0)
4173 cpp_error_with_line (pfile
,
4174 pfile
->multiline_string_line
, -1,
4175 "possible real start of unterminated constant");
4176 pfile
->multiline_string_line
= 0;
4180 CPP_PUTC (pfile
, cc
);
4184 /* Traditionally, end of line ends a string constant with
4185 no error. So exit the loop and record the new line. */
4186 if (CPP_TRADITIONAL (pfile
))
4190 cpp_error_with_line (pfile
, start_line
, start_column
,
4191 "unterminated character constant");
4194 if (CPP_PEDANTIC (pfile
)
4195 && pfile
->multiline_string_line
== 0)
4197 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4198 "string constant runs past end of line");
4200 if (pfile
->multiline_string_line
== 0)
4201 pfile
->multiline_string_line
= start_line
;
4208 /* Backslash newline is replaced by nothing at all. */
4209 CPP_ADJUST_WRITTEN (pfile
, -1);
4214 /* ANSI stupidly requires that in \\ the second \
4215 is *not* prevented from combining with a newline. */
4218 CPP_PUTC (pfile
, cc
);
4230 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4231 CPP_PWRITTEN (pfile
));
4232 pfile
->only_seen_white
= 0;
4233 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4236 if (!opts
->dollars_in_ident
)
4241 if (opts
->cplusplus
&& PEEKC () == ':')
4250 if (c2
== c
|| c2
== '=')
4260 if (PEEKC () == '=')
4267 if (c2
== '-' && opts
->chill
)
4269 /* Chill style comment */
4270 if (opts
->put_out_comments
)
4271 parse_set_mark (&start_mark
, pfile
);
4272 FORWARD(1); /* Skip second '-'. */
4280 /* Don't consider final '\n' to be part of comment. */
4286 goto return_comment
;
4288 if (c2
== '-' || c2
== '=' || c2
== '>')
4293 if (pfile
->parsing_include_directive
)
4297 CPP_PUTC (pfile
, c
);
4302 if (c
== '\n' || c
== EOF
)
4305 "missing '>' in `#include <FILENAME>'");
4311 /* else fall through */
4320 CPP_RESERVE (pfile
, 4);
4321 CPP_PUTC (pfile
, c
);
4322 CPP_PUTC (pfile
, c2
);
4326 CPP_PUTC_Q (pfile
, GETC ());
4327 CPP_NUL_TERMINATE_Q (pfile
);
4328 pfile
->only_seen_white
= 0;
4332 if (CPP_BUFFER (pfile
)->has_escapes
)
4337 if (pfile
->output_escapes
)
4338 CPP_PUTS (pfile
, "@-", 2);
4339 parse_name (pfile
, GETC ());
4342 else if (is_space
[c
])
4344 CPP_RESERVE (pfile
, 2);
4345 if (pfile
->output_escapes
)
4346 CPP_PUTC_Q (pfile
, '@');
4347 CPP_PUTC_Q (pfile
, c
);
4351 if (pfile
->output_escapes
)
4353 CPP_PUTS (pfile
, "@@", 2);
4363 CPP_RESERVE(pfile
, 2);
4364 CPP_PUTC_Q (pfile
, '.');
4368 /* FIXME - misses the case "..\\\n." */
4369 if (c2
== '.' && PEEKN(1) == '.')
4371 CPP_RESERVE(pfile
, 4);
4372 CPP_PUTC_Q (pfile
, '.');
4373 CPP_PUTC_Q (pfile
, '.');
4374 CPP_PUTC_Q (pfile
, '.');
4376 CPP_NUL_TERMINATE_Q (pfile
);
4377 pfile
->only_seen_white
= 0;
4384 pfile
->only_seen_white
= 0;
4386 CPP_RESERVE(pfile
, 3);
4387 CPP_PUTC_Q (pfile
, c
);
4388 CPP_PUTC_Q (pfile
, GETC ());
4389 CPP_NUL_TERMINATE_Q (pfile
);
4395 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4397 CPP_PUTC (pfile
, c
);
4403 case '0': case '1': case '2': case '3': case '4':
4404 case '5': case '6': case '7': case '8': case '9':
4409 CPP_RESERVE (pfile
, 2);
4410 CPP_PUTC_Q (pfile
, c
);
4415 if (!is_idchar
[c
] && c
!= '.'
4416 && ((c2
!= 'e' && c2
!= 'E'
4417 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
4418 || (c
!= '+' && c
!= '-')))
4423 CPP_NUL_TERMINATE_Q (pfile
);
4424 pfile
->only_seen_white
= 0;
4426 case 'b': case 'c': case 'd': case 'h': case 'o':
4427 case 'B': case 'C': case 'D': case 'H': case 'O':
4428 if (opts
->chill
&& PEEKC () == '\'')
4430 pfile
->only_seen_white
= 0;
4431 CPP_RESERVE (pfile
, 2);
4432 CPP_PUTC_Q (pfile
, c
);
4433 CPP_PUTC_Q (pfile
, '\'');
4439 goto chill_number_eof
;
4442 if (c
== '\\' && PEEKC() == '\n')
4449 CPP_PUTC (pfile
, c
);
4453 CPP_RESERVE (pfile
, 2);
4454 CPP_PUTC_Q (pfile
, c
);
4455 CPP_NUL_TERMINATE_Q (pfile
);
4462 CPP_NUL_TERMINATE (pfile
);
4469 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4470 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4471 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4472 case 'x': case 'y': case 'z':
4473 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4474 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4475 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4480 unsigned char *ident
;
4481 int before_name_written
= CPP_WRITTEN (pfile
);
4483 parse_name (pfile
, c
);
4484 pfile
->only_seen_white
= 0;
4485 if (pfile
->no_macro_expand
)
4487 ident
= pfile
->token_buffer
+ before_name_written
;
4488 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
4489 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
4492 if (hp
->type
== T_DISABLED
)
4494 if (pfile
->output_escapes
)
4495 { /* Return "@-IDENT", followed by '\0'. */
4497 CPP_RESERVE (pfile
, 3);
4498 ident
= pfile
->token_buffer
+ before_name_written
;
4499 CPP_ADJUST_WRITTEN (pfile
, 2);
4500 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
4507 /* If macro wants an arglist, verify that a '(' follows.
4508 first skip all whitespace, copying it to the output
4509 after the macro name. Then, if there is no '(',
4510 decide this is not a macro call and leave things that way. */
4511 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
4513 struct parse_marker macro_mark
;
4514 int is_macro_call
, macbuf_whitespace
= 0;
4516 parse_set_mark (¯o_mark
, pfile
);
4519 cpp_skip_hspace (pfile
);
4521 is_macro_call
= c
== '(';
4530 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4532 if (macro_mark
.position
!=
4533 (CPP_BUFFER (pfile
)->cur
4534 - CPP_BUFFER (pfile
)->buf
))
4535 macbuf_whitespace
= 1;
4537 parse_clear_mark (¯o_mark
);
4538 cpp_pop_buffer (pfile
);
4539 parse_set_mark (¯o_mark
, pfile
);
4547 parse_goto_mark (¯o_mark
, pfile
);
4548 if (macbuf_whitespace
)
4549 CPP_PUTC (pfile
, ' ');
4551 parse_clear_mark (¯o_mark
);
4555 /* This is now known to be a macro call. */
4557 /* it might not actually be a macro. */
4558 if (hp
->type
!= T_MACRO
) {
4559 int xbuf_len
; U_CHAR
*xbuf
;
4560 CPP_SET_WRITTEN (pfile
, before_name_written
);
4561 special_symbol (hp
, pfile
);
4562 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
4563 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
4564 CPP_SET_WRITTEN (pfile
, before_name_written
);
4565 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
4566 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
4570 /* Expand the macro, reading arguments as needed,
4571 and push the expansion on the input stack. */
4572 macroexpand (pfile
, hp
);
4573 CPP_SET_WRITTEN (pfile
, before_name_written
);
4576 /* An extra "@ " is added to the end of a macro expansion
4577 to prevent accidental token pasting. We prefer to avoid
4578 unneeded extra spaces (for the sake of cpp-using tools like
4579 imake). Here we remove the space if it is safe to do so. */
4580 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
4581 && pfile
->buffer
->rlimit
[-2] == '@'
4582 && pfile
->buffer
->rlimit
[-1] == ' ')
4584 int c1
= pfile
->buffer
->rlimit
[-3];
4585 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
4586 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
4587 pfile
->buffer
->rlimit
-= 2;
4592 case ' ': case '\t': case '\v': case '\r':
4595 CPP_PUTC (pfile
, c
);
4597 if (c
== EOF
|| !is_hor_space
[c
])
4611 CPP_PUTC (pfile
, c
);
4612 if (pfile
->only_seen_white
== 0)
4613 pfile
->only_seen_white
= 1;
4615 output_line_command (pfile
, 1, same_file
);
4618 case '(': token
= CPP_LPAREN
; goto char1
;
4619 case ')': token
= CPP_RPAREN
; goto char1
;
4620 case '{': token
= CPP_LBRACE
; goto char1
;
4621 case '}': token
= CPP_RBRACE
; goto char1
;
4622 case ',': token
= CPP_COMMA
; goto char1
;
4623 case ';': token
= CPP_SEMICOLON
; goto char1
;
4629 pfile
->only_seen_white
= 0;
4630 CPP_PUTC (pfile
, c
);
4636 /* Like cpp_get_token, but skip spaces and comments. */
4639 cpp_get_non_space_token (pfile
)
4642 int old_written
= CPP_WRITTEN (pfile
);
4645 enum cpp_token token
= cpp_get_token (pfile
);
4646 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
4647 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
4649 CPP_SET_WRITTEN (pfile
, old_written
);
4653 /* Parse an identifier starting with C. */
4656 parse_name (pfile
, c
)
4657 cpp_reader
*pfile
; int c
;
4663 if (c
== '\\' && PEEKC() == '\n')
4672 if (c
== '$' && CPP_PEDANTIC (pfile
))
4673 cpp_pedwarn (pfile
, "`$' in identifier");
4675 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
4676 CPP_PUTC_Q (pfile
, c
);
4681 CPP_NUL_TERMINATE_Q (pfile
);
4685 /* This is called after options have been processed.
4686 * Check options for consistency, and setup for processing input
4687 * from the file named FNAME. (Use standard input if FNAME==NULL.)
4688 * Return 1 on success, 0 on failure.
4692 cpp_start_read (pfile
, fname
)
4696 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4697 struct cpp_pending
*pend
;
4701 struct include_hash
*ih_fake
;
4703 /* The code looks at the defaults through this pointer, rather than through
4704 the constant structure above. This pointer gets changed if an environment
4705 variable specifies other defaults. */
4706 struct default_include
*include_defaults
= include_defaults_array
;
4708 /* Now that we know dollars_in_ident for real,
4709 reset is_idchar/is_idstart. */
4710 is_idchar
['$'] = opts
->dollars_in_ident
;
4711 is_idstart
['$'] = opts
->dollars_in_ident
;
4713 /* Add dirs from CPATH after dirs from -I. */
4714 /* There seems to be confusion about what CPATH should do,
4715 so for the moment it is not documented. */
4716 /* Some people say that CPATH should replace the standard include dirs,
4717 but that seems pointless: it comes before them, so it overrides them
4719 GET_ENV_PATH_LIST (p
, "CPATH");
4720 if (p
!= 0 && ! opts
->no_standard_includes
)
4721 path_include (pfile
, p
);
4723 /* Do partial setup of input buffer for the sake of generating
4724 early #line directives (when -g is in effect). */
4725 fp
= cpp_push_buffer (pfile
, NULL
, 0);
4728 if (opts
->in_fname
== NULL
|| *opts
->in_fname
== 0)
4730 opts
->in_fname
= fname
;
4731 if (opts
->in_fname
== NULL
)
4732 opts
->in_fname
= "";
4734 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
4737 /* Install __LINE__, etc. Must follow initialize_char_syntax
4738 and option processing. */
4739 initialize_builtins (pfile
);
4741 /* Do standard #defines and assertions
4742 that identify system and machine type. */
4744 if (!opts
->inhibit_predefs
) {
4745 char *p
= (char *) alloca (strlen (predefs
) + 1);
4746 strcpy (p
, predefs
);
4749 while (*p
== ' ' || *p
== '\t')
4751 /* Handle -D options. */
4752 if (p
[0] == '-' && p
[1] == 'D') {
4754 while (*p
&& *p
!= ' ' && *p
!= '\t')
4758 if (opts
->debug_output
)
4759 output_line_command (pfile
, 0, same_file
);
4760 cpp_define (pfile
, q
);
4761 while (*p
== ' ' || *p
== '\t')
4763 } else if (p
[0] == '-' && p
[1] == 'A') {
4764 /* Handle -A options (assertions). */
4773 past_name
= assertion
;
4774 /* Locate end of name. */
4775 while (*past_name
&& *past_name
!= ' '
4776 && *past_name
!= '\t' && *past_name
!= '(')
4778 /* Locate `(' at start of value. */
4780 while (*value
&& (*value
== ' ' || *value
== '\t'))
4782 if (*value
++ != '(')
4784 while (*value
&& (*value
== ' ' || *value
== '\t'))
4787 /* Locate end of value. */
4788 while (*past_value
&& *past_value
!= ' '
4789 && *past_value
!= '\t' && *past_value
!= ')')
4791 termination
= past_value
;
4792 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
4794 if (*termination
++ != ')')
4796 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
4798 /* Temporarily null-terminate the value. */
4799 save_char
= *termination
;
4800 *termination
= '\0';
4801 /* Install the assertion. */
4802 make_assertion (pfile
, "-A", assertion
);
4803 *termination
= (char) save_char
;
4805 while (*p
== ' ' || *p
== '\t')
4813 /* Now handle the command line options. */
4815 /* Do -U's, -D's and -A's in the order they were seen. */
4816 /* First reverse the list. */
4817 opts
->pending
= nreverse_pending (opts
->pending
);
4819 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
4821 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
4823 switch (pend
->cmd
[1])
4826 if (opts
->debug_output
)
4827 output_line_command (pfile
, 0, same_file
);
4828 cpp_undef (pfile
, pend
->arg
);
4831 if (opts
->debug_output
)
4832 output_line_command (pfile
, 0, same_file
);
4833 cpp_define (pfile
, pend
->arg
);
4836 make_assertion (pfile
, "-A", pend
->arg
);
4842 opts
->done_initializing
= 1;
4844 { /* Read the appropriate environment variable and if it exists
4845 replace include_defaults with the listed path. */
4847 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
4850 GET_ENV_PATH_LIST (epath
, "C_INCLUDE_PATH");
4853 GET_ENV_PATH_LIST (epath
, "CPLUS_INCLUDE_PATH");
4856 GET_ENV_PATH_LIST (epath
, "OBJC_INCLUDE_PATH");
4859 GET_ENV_PATH_LIST (epath
, "OBJCPLUS_INCLUDE_PATH");
4862 /* If the environment var for this language is set,
4863 add to the default list of include directories. */
4865 char *nstore
= (char *) alloca (strlen (epath
) + 2);
4867 char *startp
, *endp
;
4869 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
4870 if (*startp
== PATH_SEPARATOR
)
4873 = (struct default_include
*) xmalloc ((num_dirs
4874 * sizeof (struct default_include
))
4875 + sizeof (include_defaults_array
));
4876 startp
= endp
= epath
;
4879 /* Handle cases like c:/usr/lib:d:/gcc/lib */
4880 if ((*endp
== PATH_SEPARATOR
)
4882 strncpy (nstore
, startp
, endp
-startp
);
4884 strcpy (nstore
, ".");
4886 nstore
[endp
-startp
] = '\0';
4888 include_defaults
[num_dirs
].fname
= xstrdup (nstore
);
4889 include_defaults
[num_dirs
].component
= 0;
4890 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
4891 include_defaults
[num_dirs
].cxx_aware
= 1;
4895 endp
= startp
= endp
+ 1;
4899 /* Put the usual defaults back in at the end. */
4900 bcopy ((char *) include_defaults_array
,
4901 (char *) &include_defaults
[num_dirs
],
4902 sizeof (include_defaults_array
));
4906 /* Unless -fnostdinc,
4907 tack on the standard include file dirs to the specified list */
4908 if (!opts
->no_standard_includes
) {
4909 struct default_include
*p
= include_defaults
;
4910 char *specd_prefix
= opts
->include_prefix
;
4911 char *default_prefix
= xstrdup (GCC_INCLUDE_DIR
);
4912 int default_len
= 0;
4913 /* Remove the `include' from /usr/local/lib/gcc.../include. */
4914 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
4915 default_len
= strlen (default_prefix
) - 7;
4916 default_prefix
[default_len
] = 0;
4918 /* Search "translated" versions of GNU directories.
4919 These have /usr/local/lib/gcc... replaced by specd_prefix. */
4920 if (specd_prefix
!= 0 && default_len
!= 0)
4921 for (p
= include_defaults
; p
->fname
; p
++) {
4922 /* Some standard dirs are only for C++. */
4924 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
4925 /* Does this dir start with the prefix? */
4926 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
4927 /* Yes; change prefix and add to search list. */
4928 int this_len
= strlen (specd_prefix
)
4929 + strlen (p
->fname
) - default_len
;
4930 char *str
= (char *) xmalloc (this_len
+ 1);
4931 strcpy (str
, specd_prefix
);
4932 strcat (str
, p
->fname
+ default_len
);
4934 append_include_chain (pfile
, &opts
->system_include
,
4935 str
, !p
->cxx_aware
);
4939 /* Search ordinary names for GNU include directories. */
4940 for (p
= include_defaults
; p
->fname
; p
++) {
4941 /* Some standard dirs are only for C++. */
4943 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
4944 const char *str
= update_path (p
->fname
, p
->component
);
4945 append_include_chain (pfile
, &opts
->system_include
,
4946 str
, !p
->cxx_aware
);
4951 merge_include_chains (opts
);
4953 /* With -v, print the list of dirs to search. */
4954 if (opts
->verbose
) {
4955 struct file_name_list
*p
;
4956 fprintf (stderr
, "#include \"...\" search starts here:\n");
4957 for (p
= opts
->quote_include
; p
; p
= p
->next
) {
4958 if (p
== opts
->bracket_include
)
4959 fprintf (stderr
, "#include <...> search starts here:\n");
4960 fprintf (stderr
, " %s\n", p
->name
);
4962 fprintf (stderr
, "End of search list.\n");
4965 /* Copy the entire contents of the main input file into
4966 the stacked input buffer previously allocated for it. */
4967 if (fname
== NULL
|| *fname
== 0) {
4970 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
4971 cpp_pfatal_with_name (pfile
, fname
);
4973 /* -MG doesn't select the form of output and must be specified with one of
4974 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
4975 inhibit compilation. */
4976 if (opts
->print_deps_missing_files
4977 && (opts
->print_deps
== 0 || !opts
->no_output
))
4979 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
4983 /* Either of two environment variables can specify output of deps.
4984 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
4985 where OUTPUT_FILE is the file to write deps info to
4986 and DEPS_TARGET is the target to mention in the deps. */
4988 if (opts
->print_deps
== 0
4989 && (getenv ("SUNPRO_DEPENDENCIES") != 0
4990 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
4991 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
4997 spec
= getenv ("SUNPRO_DEPENDENCIES");
4998 opts
->print_deps
= 2;
5001 opts
->print_deps
= 1;
5004 /* Find the space before the DEPS_TARGET, if there is one. */
5005 /* This should use index. (mrs) */
5006 while (*s
!= 0 && *s
!= ' ') s
++;
5009 opts
->deps_target
= s
+ 1;
5010 output_file
= (char *) xmalloc (s
- spec
+ 1);
5011 bcopy (spec
, output_file
, s
- spec
);
5012 output_file
[s
- spec
] = 0;
5016 opts
->deps_target
= 0;
5020 opts
->deps_file
= output_file
;
5021 opts
->print_deps_append
= 1;
5024 /* For -M, print the expected object file name
5025 as the target of this Make-rule. */
5026 if (opts
->print_deps
)
5028 pfile
->deps_allocated_size
= 200;
5029 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
5030 pfile
->deps_buffer
[0] = 0;
5031 pfile
->deps_size
= 0;
5032 pfile
->deps_column
= 0;
5034 if (opts
->deps_target
)
5035 deps_output (pfile
, opts
->deps_target
, ':');
5036 else if (*opts
->in_fname
== 0)
5037 deps_output (pfile
, "-", ':');
5042 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
5043 ".cc", ".cxx", ".cpp", ".cp",
5047 /* Discard all directory prefixes from filename. */
5048 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
5049 #ifdef DIR_SEPARATOR
5050 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
5057 /* Copy remainder to mungable area. */
5058 p
= (char *) alloca (strlen(q
) + 8);
5061 /* Output P, but remove known suffixes. */
5064 /* Point to the filename suffix. */
5065 r
= rindex (p
, '.');
5066 /* Compare against the known suffixes. */
5068 while (known_suffixes
[x
] != 0)
5070 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
5072 /* Make q point to the bit we're going to overwrite
5073 with an object suffix. */
5080 /* Supply our own suffix. */
5087 deps_output (pfile
, p
, ':');
5088 deps_output (pfile
, opts
->in_fname
, ' ');
5093 /* Make sure data ends with a newline. And put a null after it. */
5095 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5096 /* Backslash-newline at end is not good enough. */
5097 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5098 fp
->buf
[fp
->length
++] = '\n';
5099 missing_newline
= 1;
5101 fp
->buf
[fp
->length
] = '\0';
5103 /* Unless inhibited, convert trigraphs in the input. */
5109 /* Must call finclude() on the main input before processing
5110 -include switches; otherwise the -included text winds up
5111 after the main input. */
5112 ih_fake
= (struct include_hash
*) xmalloc (sizeof (struct include_hash
));
5114 ih_fake
->next_this_file
= 0;
5115 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
5116 ih_fake
->name
= fname
;
5117 ih_fake
->control_macro
= 0;
5118 ih_fake
->buf
= (char *)-1;
5120 if (!finclude (pfile
, f
, ih_fake
))
5122 output_line_command (pfile
, 0, same_file
);
5123 pfile
->only_seen_white
= 2;
5125 /* The -imacros files can be scanned now, but the -include files
5126 have to be pushed onto the include stack and processed later,
5127 in the main loop calling cpp_get_token. That means the -include
5128 files have to be processed in reverse order of the pending list,
5129 which means the pending list has to be reversed again, which
5130 means the -imacros files have to be done separately and first. */
5132 pfile
->no_record_file
++;
5134 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5136 if (pend
->cmd
!= NULL
)
5138 if (strcmp (pend
->cmd
, "-imacros") == 0)
5140 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5143 cpp_perror_with_name (pfile
, pend
->arg
);
5146 if (!cpp_push_buffer (pfile
, NULL
, 0))
5149 ih_fake
= (struct include_hash
*)
5150 xmalloc (sizeof (struct include_hash
));
5152 ih_fake
->next_this_file
= 0;
5153 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
5154 ih_fake
->name
= pend
->arg
;
5155 ih_fake
->control_macro
= 0;
5156 ih_fake
->buf
= (char *)-1;
5158 if (!finclude (pfile
, fd
, ih_fake
))
5159 cpp_scan_buffer (pfile
);
5165 opts
->pending
= nreverse_pending (opts
->pending
);
5166 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5168 if (pend
->cmd
!= NULL
)
5170 if (strcmp (pend
->cmd
, "-include") == 0)
5172 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5175 cpp_perror_with_name (pfile
, pend
->arg
);
5178 if (!cpp_push_buffer (pfile
, NULL
, 0))
5181 ih_fake
= (struct include_hash
*)
5182 xmalloc (sizeof (struct include_hash
));
5184 ih_fake
->next_this_file
= 0;
5185 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
5186 ih_fake
->name
= pend
->arg
;
5187 ih_fake
->control_macro
= 0;
5188 ih_fake
->buf
= (char *)-1;
5190 if (finclude (pfile
, fd
, ih_fake
))
5191 output_line_command (pfile
, 0, enter_file
);
5195 pfile
->no_record_file
--;
5197 /* Free the pending list. */
5198 for (pend
= opts
->pending
; pend
; )
5200 struct cpp_pending
*next
= pend
->next
;
5204 opts
->pending
= NULL
;
5210 cpp_reader_init (pfile
)
5213 bzero ((char *) pfile
, sizeof (cpp_reader
));
5214 pfile
->get_token
= cpp_get_token
;
5216 pfile
->token_buffer_size
= 200;
5217 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
5218 CPP_SET_WRITTEN (pfile
, 0);
5220 pfile
->timebuf
= NULL
;
5221 pfile
->only_seen_white
= 1;
5222 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
5223 pfile
->actual_dirs
= NULL
;
5226 static struct cpp_pending
*
5227 nreverse_pending (list
)
5228 struct cpp_pending
*list
;
5231 register struct cpp_pending
*prev
= 0, *next
, *pend
;
5232 for (pend
= list
; pend
; pend
= next
)
5242 push_pending (pfile
, cmd
, arg
)
5247 struct cpp_pending
*pend
5248 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
5251 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
5252 CPP_OPTIONS (pfile
)->pending
= pend
;
5259 printf ("Usage: %s [switches] input output\n", progname
);
5260 printf ("Switches:\n");
5261 printf (" -include <file> Include the contents of <file> before other files\n");
5262 printf (" -imacros <file> Accept definition of marcos in <file>\n");
5263 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
5264 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
5265 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
5266 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
5267 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
5268 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
5269 printf (" -nostdinc Do not search the system include directories\n");
5270 printf (" -nostdinc++ Do not search the system include directories for C++\n");
5271 printf (" -o <file> Put output into <file>\n");
5272 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
5273 printf (" -traditional Follow K&R pre-processor behaviour\n");
5274 printf (" -trigraphs Support ANSI C trigraphs\n");
5275 printf (" -lang-c Assume that the input sources are in C\n");
5276 printf (" -lang-c89 Assume that the input sources are in C89\n");
5277 printf (" -lang-c++ Assume that the input sources are in C++\n");
5278 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
5279 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
5280 printf (" -lang-asm Assume that the input sources are in assembler\n");
5281 printf (" -lang-chill Assume that the input sources are in Chill\n");
5282 printf (" -+ Allow parsing of C++ style features\n");
5283 printf (" -w Inhibit warning messages\n");
5284 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
5285 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
5286 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
5287 printf (" -Wno-comment{s} Do not warn about comments\n");
5288 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
5289 printf (" a string if -traditional is specified\n");
5290 printf (" -Wno-traditional Do not warn about stringification\n");
5291 printf (" -Wundef Warn if an undefined macro is used by #if\n");
5292 printf (" -Wno-undef Do not warn about testing undefined macros\n");
5293 printf (" -Wimport Warn about the use of the #import directive\n");
5294 printf (" -Wno-import Do not warn about the use of #import\n");
5295 printf (" -Werror Treat all warnings as errors\n");
5296 printf (" -Wno-error Do not treat warnings as errors\n");
5297 printf (" -Wall Enable all preprocessor warnings\n");
5298 printf (" -M Generate make dependencies\n");
5299 printf (" -MM As -M, but ignore system header files\n");
5300 printf (" -MD As -M, but put output in a .d file\n");
5301 printf (" -MMD As -MD, but ignore system header files\n");
5302 printf (" -MG Treat missing header file as generated files\n");
5303 printf (" -g Include #define and #undef directives in the output\n");
5304 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
5305 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
5306 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
5307 printf (" -U<macro> Undefine <macro> \n");
5308 printf (" -u or -undef Do not predefine any macros\n");
5309 printf (" -v Display the version number\n");
5310 printf (" -H Print the name of header files as they are used\n");
5311 printf (" -C Do not discard comments\n");
5312 printf (" -dM Display a list of macro definitions active at end\n");
5313 printf (" -dD Preserve macro definitions in output\n");
5314 printf (" -dN As -dD except that only the names are preserved\n");
5315 printf (" -dI Include #include directives in the output\n");
5316 printf (" -ifoutput Describe skipped code blocks in output \n");
5317 printf (" -P Do not generate #line directives\n");
5318 printf (" -$ Do not include '$' in identifiers\n");
5319 printf (" -remap Remap file names when including files.\n");
5320 printf (" -h or --help Display this information\n");
5324 /* Handle one command-line option in (argc, argv).
5325 Can be called multiple times, to handle multiple sets of options.
5326 Returns number of strings consumed. */
5328 cpp_handle_option (pfile
, argc
, argv
)
5333 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5336 if (user_label_prefix
== NULL
)
5337 user_label_prefix
= USER_LABEL_PREFIX
;
5339 if (argv
[i
][0] != '-') {
5340 if (opts
->out_fname
!= NULL
)
5343 cpp_fatal (pfile
, "Too many arguments");
5345 else if (opts
->in_fname
!= NULL
)
5346 opts
->out_fname
= argv
[i
];
5348 opts
->in_fname
= argv
[i
];
5350 switch (argv
[i
][1]) {
5353 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
5356 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
5360 if (!strcmp (argv
[i
], "-fleading-underscore"))
5361 user_label_prefix
= "_";
5362 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
5363 user_label_prefix
= "";
5366 case 'I': /* Add directory to path for includes. */
5367 if (!strcmp (argv
[i
] + 2, "-"))
5369 if (! opts
->ignore_srcdir
)
5371 opts
->ignore_srcdir
= 1;
5372 /* Don't use any preceding -I directories for #include <...>. */
5373 opts
->quote_include
= opts
->bracket_include
;
5374 opts
->bracket_include
= 0;
5380 if (argv
[i
][2] != 0)
5381 fname
= argv
[i
] + 2;
5382 else if (i
+ 1 == argc
)
5383 goto missing_dirname
;
5386 append_include_chain (pfile
, &opts
->bracket_include
, fname
, 0);
5391 /* Add directory to beginning of system include path, as a system
5392 include directory. */
5393 if (!strcmp (argv
[i
], "-isystem"))
5396 goto missing_filename
;
5397 append_include_chain (pfile
, &opts
->system_include
, argv
[++i
], 1);
5399 /* Add directory to end of path for includes,
5400 with the default prefix at the front of its name. */
5401 else if (!strcmp (argv
[i
], "-iwithprefix"))
5405 goto missing_dirname
;
5408 if (opts
->include_prefix
!= 0)
5410 fname
= xmalloc (strlen (opts
->include_prefix
)
5411 + strlen (argv
[i
]) + 1);
5412 strcpy (fname
, opts
->include_prefix
);
5413 strcat (fname
, argv
[i
]);
5417 fname
= xmalloc (strlen (GCC_INCLUDE_DIR
)
5418 + strlen (argv
[i
]) + 1);
5419 strcpy (fname
, GCC_INCLUDE_DIR
);
5420 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5421 if (!strcmp (fname
+ strlen (fname
) - 8, "/include"))
5422 fname
[strlen (fname
) - 7] = 0;
5423 strcat (fname
, argv
[i
]);
5426 append_include_chain (pfile
, &opts
->system_include
, fname
, 0);
5428 /* Add directory to main path for includes,
5429 with the default prefix at the front of its name. */
5430 else if (!strcmp (argv
[i
], "-iwithprefix"))
5434 goto missing_dirname
;
5437 if (opts
->include_prefix
!= 0)
5439 fname
= xmalloc (strlen (opts
->include_prefix
)
5440 + strlen (argv
[i
]) + 1);
5441 strcpy (fname
, opts
->include_prefix
);
5442 strcat (fname
, argv
[i
]);
5446 fname
= xmalloc (strlen (GCC_INCLUDE_DIR
)
5447 + strlen (argv
[i
]) + 1);
5448 strcpy (fname
, GCC_INCLUDE_DIR
);
5449 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5450 if (!strcmp (fname
+ strlen (fname
) - 8, "/include"))
5451 fname
[strlen (fname
) - 7] = 0;
5452 strcat (fname
, argv
[i
]);
5455 append_include_chain (pfile
, &opts
->bracket_include
, fname
, 0);
5457 /* Add directory to end of path for includes. */
5458 else if (!strcmp (argv
[i
], "-idirafter"))
5461 goto missing_dirname
;
5462 append_include_chain (pfile
, &opts
->after_include
, argv
[++i
], 0);
5464 else if (!strcmp (argv
[i
], "-include") || !strcmp (argv
[i
], "-imacros"))
5467 goto missing_filename
;
5469 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
5471 else if (!strcmp (argv
[i
], "-iprefix"))
5474 goto missing_filename
;
5476 opts
->include_prefix
= argv
[++i
];
5478 else if (!strcmp (argv
[i
], "-ifoutput"))
5479 opts
->output_conditionals
= 1;
5484 if (opts
->out_fname
!= NULL
)
5486 cpp_fatal (pfile
, "Output filename specified twice");
5490 goto missing_filename
;
5491 opts
->out_fname
= argv
[++i
];
5492 if (!strcmp (opts
->out_fname
, "-"))
5493 opts
->out_fname
= "";
5497 if (!strcmp (argv
[i
], "-pedantic"))
5498 CPP_PEDANTIC (pfile
) = 1;
5499 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
5500 CPP_PEDANTIC (pfile
) = 1;
5501 opts
->pedantic_errors
= 1;
5504 else if (!strcmp (argv
[i
], "-pcp")) {
5505 char *pcp_fname
= argv
[++i
];
5506 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
5507 ? fopen (pcp_fname
, "w")
5508 : fdopen (dup (fileno (stdout
)), "w"));
5509 if (pcp_outfile
== 0)
5510 cpp_pfatal_with_name (pfile
, pcp_fname
);
5517 if (!strcmp (argv
[i
], "-traditional")) {
5518 opts
->traditional
= 1;
5519 opts
->cplusplus_comments
= 0;
5520 } else if (!strcmp (argv
[i
], "-trigraphs")) {
5522 opts
->no_trigraphs
= 0;
5527 if (! strcmp (argv
[i
], "-lang-c"))
5528 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5530 if (! strcmp (argv
[i
], "-lang-c89"))
5531 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
5533 if (! strcmp (argv
[i
], "-lang-c++"))
5534 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5536 if (! strcmp (argv
[i
], "-lang-objc"))
5537 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5539 if (! strcmp (argv
[i
], "-lang-objc++"))
5540 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5542 if (! strcmp (argv
[i
], "-lang-asm"))
5544 if (! strcmp (argv
[i
], "-lint"))
5546 if (! strcmp (argv
[i
], "-lang-chill"))
5547 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
5548 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
5552 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
5556 opts
->inhibit_warnings
= 1;
5560 if (!strcmp (argv
[i
], "-Wtrigraphs"))
5561 opts
->warn_trigraphs
= 1;
5562 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
5563 opts
->warn_trigraphs
= 0;
5564 else if (!strcmp (argv
[i
], "-Wcomment"))
5565 opts
->warn_comments
= 1;
5566 else if (!strcmp (argv
[i
], "-Wno-comment"))
5567 opts
->warn_comments
= 0;
5568 else if (!strcmp (argv
[i
], "-Wcomments"))
5569 opts
->warn_comments
= 1;
5570 else if (!strcmp (argv
[i
], "-Wno-comments"))
5571 opts
->warn_comments
= 0;
5572 else if (!strcmp (argv
[i
], "-Wtraditional"))
5573 opts
->warn_stringify
= 1;
5574 else if (!strcmp (argv
[i
], "-Wno-traditional"))
5575 opts
->warn_stringify
= 0;
5576 else if (!strcmp (argv
[i
], "-Wundef"))
5577 opts
->warn_undef
= 1;
5578 else if (!strcmp (argv
[i
], "-Wno-undef"))
5579 opts
->warn_undef
= 0;
5580 else if (!strcmp (argv
[i
], "-Wimport"))
5581 opts
->warn_import
= 1;
5582 else if (!strcmp (argv
[i
], "-Wno-import"))
5583 opts
->warn_import
= 0;
5584 else if (!strcmp (argv
[i
], "-Werror"))
5585 opts
->warnings_are_errors
= 1;
5586 else if (!strcmp (argv
[i
], "-Wno-error"))
5587 opts
->warnings_are_errors
= 0;
5588 else if (!strcmp (argv
[i
], "-Wall"))
5590 opts
->warn_trigraphs
= 1;
5591 opts
->warn_comments
= 1;
5596 /* The style of the choices here is a bit mixed.
5597 The chosen scheme is a hybrid of keeping all options in one string
5598 and specifying each option in a separate argument:
5599 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
5600 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5601 -M[M][G][D file]. This is awkward to handle in specs, and is not
5603 /* ??? -MG must be specified in addition to one of -M or -MM.
5604 This can be relaxed in the future without breaking anything.
5605 The converse isn't true. */
5607 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
5608 if (!strcmp (argv
[i
], "-MG"))
5610 opts
->print_deps_missing_files
= 1;
5613 if (!strcmp (argv
[i
], "-M"))
5614 opts
->print_deps
= 2;
5615 else if (!strcmp (argv
[i
], "-MM"))
5616 opts
->print_deps
= 1;
5617 else if (!strcmp (argv
[i
], "-MD"))
5618 opts
->print_deps
= 2;
5619 else if (!strcmp (argv
[i
], "-MMD"))
5620 opts
->print_deps
= 1;
5621 /* For -MD and -MMD options, write deps on file named by next arg. */
5622 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
5625 goto missing_filename
;
5626 opts
->deps_file
= argv
[++i
];
5630 /* For -M and -MM, write deps on standard output
5631 and suppress the usual output. */
5632 opts
->no_output
= 1;
5638 char *p
= argv
[i
] + 2;
5640 while ((c
= *p
++) != 0) {
5641 /* Arg to -d specifies what parts of macros to dump */
5644 opts
->dump_macros
= dump_only
;
5645 opts
->no_output
= 1;
5648 opts
->dump_macros
= dump_names
;
5651 opts
->dump_macros
= dump_definitions
;
5654 opts
->dump_includes
= 1;
5662 if (argv
[i
][2] == '3')
5663 opts
->debug_output
= 1;
5667 if (strcmp (argv
[i
], "--help") != 0)
5673 fprintf (stderr
, "GNU CPP version %s", version_string
);
5674 #ifdef TARGET_VERSION
5677 fprintf (stderr
, "\n");
5682 opts
->print_include_names
= 1;
5686 if (argv
[i
][2] != 0)
5687 push_pending (pfile
, "-D", argv
[i
] + 2);
5688 else if (i
+ 1 == argc
)
5690 cpp_fatal (pfile
, "Macro name missing after -D option");
5694 i
++, push_pending (pfile
, "-D", argv
[i
]);
5701 if (argv
[i
][2] != 0)
5703 else if (i
+ 1 == argc
)
5705 cpp_fatal (pfile
, "Assertion missing after -A option");
5711 if (!strcmp (p
, "-")) {
5712 struct cpp_pending
**ptr
;
5713 /* -A- eliminates all predefined macros and assertions.
5714 Let's include also any that were specified earlier
5715 on the command line. That way we can get rid of any
5716 that were passed automatically in from GCC. */
5717 opts
->inhibit_predefs
= 1;
5718 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
5720 struct cpp_pending
*pend
= *ptr
;
5721 if (pend
->cmd
&& pend
->cmd
[0] == '-'
5722 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
5731 push_pending (pfile
, "-A", p
);
5736 case 'U': /* JF #undef something */
5737 if (argv
[i
][2] != 0)
5738 push_pending (pfile
, "-U", argv
[i
] + 2);
5739 else if (i
+ 1 == argc
)
5741 cpp_fatal (pfile
, "Macro name missing after -U option");
5745 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
5749 opts
->put_out_comments
= 1;
5752 case 'E': /* -E comes from cc -E; ignore it. */
5756 opts
->no_line_commands
= 1;
5759 case '$': /* Don't include $ in identifiers. */
5760 opts
->dollars_in_ident
= 0;
5764 if (!strcmp (argv
[i
], "-nostdinc"))
5765 /* -nostdinc causes no default include directories.
5766 You must specify all include-file directories with -I. */
5767 opts
->no_standard_includes
= 1;
5768 else if (!strcmp (argv
[i
], "-nostdinc++"))
5769 /* -nostdinc++ causes no default C++-specific include directories. */
5770 opts
->no_standard_cplusplus_includes
= 1;
5772 else if (!strcmp (argv
[i
], "-noprecomp"))
5778 if (!strcmp (argv
[i
], "-remap"))
5783 /* Sun compiler passes undocumented switch "-undef".
5784 Let's assume it means to inhibit the predefined symbols. */
5785 opts
->inhibit_predefs
= 1;
5788 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
5789 if (opts
->in_fname
== NULL
) {
5790 opts
->in_fname
= "";
5792 } else if (opts
->out_fname
== NULL
) {
5793 opts
->out_fname
= "";
5795 } /* else fall through into error */
5805 /* Handle command-line options in (argc, argv).
5806 Can be called multiple times, to handle multiple sets of options.
5807 Returns if an unrecognized option is seen.
5808 Returns number of strings consumed. */
5811 cpp_handle_options (pfile
, argc
, argv
)
5817 int strings_processed
;
5818 for (i
= 0; i
< argc
; i
+= strings_processed
)
5820 strings_processed
= cpp_handle_option (pfile
, argc
- i
, argv
+ i
);
5821 if (strings_processed
== 0)
5831 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5833 if (opts
->print_deps
)
5835 /* Stream on which to print the dependency information. */
5838 /* Don't actually write the deps file if compilation has failed. */
5839 if (pfile
->errors
== 0)
5841 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
5842 if (opts
->deps_file
== 0)
5843 deps_stream
= stdout
;
5844 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
5845 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
5846 fputs (pfile
->deps_buffer
, deps_stream
);
5847 putc ('\n', deps_stream
);
5848 if (opts
->deps_file
)
5850 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
5851 cpp_fatal (pfile
, "I/O error on output");
5857 /* Debugging: dump statistics on the include hash table. */
5859 struct include_hash
*x
;
5862 for(i
= 0; i
< ALL_INCLUDE_HASHSIZE
; i
++)
5864 x
= pfile
->all_include_files
[i
];
5871 fprintf(stderr
, "%d/%d ", i
, j
);
5873 fputc('\n', stderr
);
5879 /* Free resources used by PFILE.
5880 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
5887 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
5888 cpp_pop_buffer (pfile
);
5890 if (pfile
->token_buffer
)
5892 free (pfile
->token_buffer
);
5893 pfile
->token_buffer
= NULL
;
5896 if (pfile
->deps_buffer
)
5898 free (pfile
->deps_buffer
);
5899 pfile
->deps_buffer
= NULL
;
5900 pfile
->deps_allocated_size
= 0;
5903 while (pfile
->if_stack
)
5905 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
5906 pfile
->if_stack
= temp
->next
;
5910 for (i
= ALL_INCLUDE_HASHSIZE
; --i
>= 0; )
5912 struct include_hash
*imp
= pfile
->all_include_files
[i
];
5915 struct include_hash
*next
= imp
->next
;
5917 /* This gets freed elsewhere - I think. */
5923 pfile
->all_include_files
[i
] = 0;
5926 cpp_hash_cleanup (pfile
);
5929 /* Read an assertion into the token buffer, converting to
5930 canonical form: `#predicate(a n swe r)' The next non-whitespace
5931 character to read should be the first letter of the predicate.
5932 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
5933 with answer (see callers for why). In case of 0, an error has been
5936 parse_assertion (pfile
)
5940 cpp_skip_hspace (pfile
);
5942 if (! is_idstart
[c
])
5944 cpp_error (pfile
, "assertion predicate is not an identifier");
5947 CPP_PUTC(pfile
, '#');
5949 parse_name(pfile
, c
);
5954 if (is_hor_space
[c
])
5955 cpp_skip_hspace (pfile
);
5961 CPP_PUTC(pfile
, '(');
5964 while ((c
= GETC()) != ')')
5966 if (is_hor_space
[c
])
5970 CPP_PUTC(pfile
, ' ');
5974 else if (c
== '\\' && PEEKC() == '\n')
5976 else if (c
== '\n' || c
== EOF
)
5978 if (c
== '\n') FORWARD(-1);
5979 cpp_error (pfile
, "un-terminated assertion answer");
5989 if (pfile
->limit
[-1] == ' ')
5990 pfile
->limit
[-1] = ')';
5991 else if (pfile
->limit
[-1] == '(')
5993 cpp_error (pfile
, "empty token sequence in assertion");
5997 CPP_PUTC(pfile
, ')');
5999 CPP_NUL_TERMINATE(pfile
);
6004 do_assert (pfile
, keyword
)
6006 struct directive
*keyword ATTRIBUTE_UNUSED
;
6010 HASHNODE
*base
, *this;
6011 int baselen
, thislen
;
6013 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6014 && !CPP_BUFFER (pfile
)->system_header_p
)
6015 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6017 cpp_skip_hspace (pfile
);
6018 sym
= CPP_PWRITTEN (pfile
); /* remember where it starts */
6019 ret
= parse_assertion (pfile
);
6024 cpp_error (pfile
, "missing token-sequence in `#assert'");
6028 cpp_skip_hspace (pfile
);
6030 if (c
!= EOF
&& c
!= '\n')
6032 cpp_error (pfile
, "junk at end of `#assert'");
6036 thislen
= strlen (sym
);
6037 baselen
= index (sym
, '(') - sym
;
6038 this = cpp_lookup (pfile
, sym
, thislen
, -1);
6041 cpp_warning (pfile
, "`%s' re-asserted", sym
);
6045 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
6047 base
= install (sym
, baselen
, T_ASSERT
, 0, 0, -1);
6048 else if (base
->type
!= T_ASSERT
)
6050 /* Token clash - but with what?! */
6052 "cpp internal error: base->type != T_ASSERT in do_assert");
6056 this = install (sym
, thislen
, T_ASSERT
, 0,
6057 (char *)base
->value
.aschain
, -1);
6058 base
->value
.aschain
= this;
6060 pfile
->limit
= sym
; /* Pop */
6064 pfile
->limit
= sym
; /* Pop */
6065 skip_rest_of_line (pfile
);
6070 do_unassert (pfile
, keyword
)
6072 struct directive
*keyword ATTRIBUTE_UNUSED
;
6076 long baselen
, thislen
;
6077 HASHNODE
*base
, *this, *next
;
6079 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6080 && !CPP_BUFFER (pfile
)->system_header_p
)
6081 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6083 cpp_skip_hspace (pfile
);
6085 sym
= CPP_PWRITTEN (pfile
); /* remember where it starts */
6086 ret
= parse_assertion (pfile
);
6090 cpp_skip_hspace (pfile
);
6092 if (c
!= EOF
&& c
!= '\n')
6093 cpp_error (pfile
, "junk at end of `#unassert'");
6095 thislen
= strlen (sym
);
6098 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
6100 goto error
; /* It isn't an error to #undef what isn't #defined,
6101 so it isn't an error to #unassert what isn't
6102 #asserted either. */
6104 for (this = base
->value
.aschain
; this; this = next
)
6106 next
= this->value
.aschain
;
6107 delete_macro (this);
6109 delete_macro (base
);
6113 baselen
= index (sym
, '(') - sym
;
6114 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
6115 if (! base
) goto error
;
6116 this = cpp_lookup (pfile
, sym
, thislen
, -1);
6117 if (! this) goto error
;
6120 while (next
->value
.aschain
!= this)
6121 next
= next
->value
.aschain
;
6123 next
->value
.aschain
= this->value
.aschain
;
6124 delete_macro (this);
6126 if (base
->value
.aschain
== NULL
)
6127 delete_macro (base
); /* Last answer for this predicate deleted. */
6130 pfile
->limit
= sym
; /* Pop */
6133 pfile
->limit
= sym
; /* Pop */
6134 skip_rest_of_line (pfile
);
6139 cpp_read_check_assertion (pfile
)
6142 char *name
= CPP_PWRITTEN (pfile
);
6146 FORWARD (1); /* Skip '#' */
6147 cpp_skip_hspace (pfile
);
6148 if (! parse_assertion (pfile
))
6152 hp
= cpp_lookup (pfile
, name
, (char *)CPP_PWRITTEN (pfile
) - name
, -1);
6156 pfile
->limit
= name
;
6160 /* Initialize PMARK to remember the current position of PFILE. */
6163 parse_set_mark (pmark
, pfile
)
6164 struct parse_marker
*pmark
;
6167 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
6168 pmark
->next
= pbuf
->marks
;
6169 pbuf
->marks
= pmark
;
6171 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
6174 /* Cleanup PMARK - we no longer need it. */
6177 parse_clear_mark (pmark
)
6178 struct parse_marker
*pmark
;
6180 struct parse_marker
**pp
= &pmark
->buf
->marks
;
6181 for (; ; pp
= &(*pp
)->next
) {
6182 if (*pp
== NULL
) abort ();
6183 if (*pp
== pmark
) break;
6188 /* Backup the current position of PFILE to that saved in PMARK. */
6191 parse_goto_mark (pmark
, pfile
)
6192 struct parse_marker
*pmark
;
6195 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
6196 if (pbuf
!= pmark
->buf
)
6197 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
6198 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
6201 /* Reset PMARK to point to the current position of PFILE. (Same
6202 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
6205 parse_move_mark (pmark
, pfile
)
6206 struct parse_marker
*pmark
;
6209 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
6210 if (pbuf
!= pmark
->buf
)
6211 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
6212 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
6217 cpp_print_file_and_line (pfile
)
6220 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
6225 cpp_buf_line_and_col (ip
, &line
, &col
);
6226 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
6227 line
, pfile
->show_column
? col
: -1);
6232 v_cpp_error (pfile
, msg
, ap
)
6237 cpp_print_containing_files (pfile
);
6238 cpp_print_file_and_line (pfile
);
6239 v_cpp_message (pfile
, 1, msg
, ap
);
6243 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msg
, ...))
6245 #ifndef ANSI_PROTOTYPES
6253 #ifndef ANSI_PROTOTYPES
6254 pfile
= va_arg (ap
, cpp_reader
*);
6255 msg
= va_arg (ap
, const char *);
6258 v_cpp_error (pfile
, msg
, ap
);
6262 /* Print error message but don't count it. */
6265 v_cpp_warning (pfile
, msg
, ap
)
6270 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
6273 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
6276 cpp_print_containing_files (pfile
);
6277 cpp_print_file_and_line (pfile
);
6278 v_cpp_message (pfile
, 0, msg
, ap
);
6282 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msg
, ...))
6284 #ifndef ANSI_PROTOTYPES
6292 #ifndef ANSI_PROTOTYPES
6293 pfile
= va_arg (ap
, cpp_reader
*);
6294 msg
= va_arg (ap
, const char *);
6297 v_cpp_warning (pfile
, msg
, ap
);
6301 /* Print an error message and maybe count it. */
6304 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msg
, ...))
6306 #ifndef ANSI_PROTOTYPES
6314 #ifndef ANSI_PROTOTYPES
6315 pfile
= va_arg (ap
, cpp_reader
*);
6316 msg
= va_arg (ap
, const char *);
6319 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
6320 v_cpp_error (pfile
, msg
, ap
);
6322 v_cpp_warning (pfile
, msg
, ap
);
6327 v_cpp_error_with_line (pfile
, line
, column
, msg
, ap
)
6334 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
6336 cpp_print_containing_files (pfile
);
6339 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
6341 v_cpp_message (pfile
, 1, msg
, ap
);
6345 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
, const char *msg
, ...))
6347 #ifndef ANSI_PROTOTYPES
6357 #ifndef ANSI_PROTOTYPES
6358 pfile
= va_arg (ap
, cpp_reader
*);
6359 line
= va_arg (ap
, int);
6360 column
= va_arg (ap
, int);
6361 msg
= va_arg (ap
, const char *);
6364 v_cpp_error_with_line(pfile
, line
, column
, msg
, ap
);
6369 v_cpp_warning_with_line (pfile
, line
, column
, msg
, ap
)
6378 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
6381 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
6384 cpp_print_containing_files (pfile
);
6386 ip
= cpp_file_buffer (pfile
);
6389 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
6391 v_cpp_message (pfile
, 0, msg
, ap
);
6396 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
, const char *msg
, ...))
6398 #ifndef ANSI_PROTOTYPES
6408 #ifndef ANSI_PROTOTYPES
6409 pfile
= va_arg (ap
, cpp_reader
*);
6410 line
= va_arg (ap
, int);
6411 column
= va_arg (ap
, int);
6412 msg
= va_arg (ap
, const char *);
6415 v_cpp_warning_with_line (pfile
, line
, column
, msg
, ap
);
6421 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
, const char *msg
, ...))
6423 #ifndef ANSI_PROTOTYPES
6433 #ifndef ANSI_PROTOTYPES
6434 pfile
= va_arg (ap
, cpp_reader
*);
6435 line
= va_arg (ap
, int);
6436 column
= va_arg (ap
, int);
6437 msg
= va_arg (ap
, const char *);
6440 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
6441 v_cpp_error_with_line (pfile
, column
, line
, msg
, ap
);
6443 v_cpp_warning_with_line (pfile
, line
, column
, msg
, ap
);
6447 /* Report a warning (or an error if pedantic_errors)
6448 giving specified file name and line number, not current. */
6451 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, char *file
, int line
, const char *msg
, ...))
6453 #ifndef ANSI_PROTOTYPES
6463 #ifndef ANSI_PROTOTYPES
6464 pfile
= va_arg (ap
, cpp_reader
*);
6465 file
= va_arg (ap
, char *);
6466 line
= va_arg (ap
, int);
6467 msg
= va_arg (ap
, const char *);
6470 if (!CPP_OPTIONS (pfile
)->pedantic_errors
6471 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
6474 cpp_file_line_for_message (pfile
, file
, line
, -1);
6475 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msg
, ap
);
6479 /* my_strerror - return the descriptive text associated with an
6483 my_strerror (errnum
)
6489 #ifndef HAVE_STRERROR
6490 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
6492 result
= strerror (errnum
);
6495 /* VAXCRTL's strerror() takes an optional second argument, which only
6496 matters when the first argument is EVMSERR. However, it's simplest
6497 just to pass it unconditionally. `vaxc$errno' is declared in
6498 <errno.h>, and maintained by the library in parallel with `errno'.
6499 We assume that caller's `errnum' either matches the last setting of
6500 `errno' by the library or else does not have the value `EVMSERR'. */
6502 result
= strerror (errnum
, vaxc$errno
);
6506 result
= "undocumented I/O error";
6511 /* Error including a message from `errno'. */
6514 cpp_error_from_errno (pfile
, name
)
6518 cpp_message_from_errno (pfile
, 1, name
);
6522 cpp_message_from_errno (pfile
, is_error
, name
)
6528 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
6530 cpp_print_containing_files (pfile
);
6533 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
6535 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
6539 cpp_perror_with_name (pfile
, name
)
6543 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
6547 * No pre-compiled header file support.
6549 * Possibly different enum token codes for each C/C++ token.
6551 * Find and cleanup remaining uses of static variables,
6553 * Support for trigraphs.
6555 * Support -dM flag (dump_all_macros).
6557 * Support for_lint flag.