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
)
2061 cpp_buffer
*ip
= NULL
;
2064 int paren
= 0; /* For special `defined' keyword */
2067 if (pcp_outfile
&& pcp_inside_if
2068 && hp
->type
!= T_SPEC_DEFINED
&& hp
->type
!= T_CONST
)
2070 "Predefined macro `%s' used inside `#if' during precompilation",
2074 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
2076 if (ip
== CPP_NULL_BUFFER (pfile
))
2078 cpp_error (pfile
, "cccp error: not in any file?!");
2079 return; /* the show must go on */
2081 if (ip
->fname
!= NULL
)
2091 if (hp
->type
== T_BASE_FILE
)
2093 while (CPP_PREV_BUFFER (ip
) != CPP_NULL_BUFFER (pfile
))
2094 ip
= CPP_PREV_BUFFER (ip
);
2096 string
= ip
->nominal_fname
;
2100 CPP_RESERVE (pfile
, 3 + 4 * strlen (string
));
2101 quote_string (pfile
, string
);
2105 case T_INCLUDE_LEVEL
:
2107 ip
= CPP_BUFFER (pfile
);
2108 for (; ip
!= CPP_NULL_BUFFER (pfile
); ip
= CPP_PREV_BUFFER (ip
))
2109 if (ip
->fname
!= NULL
)
2112 wbuf
= (char *) alloca (8); /* Eight bytes ought to be more than enough*/
2113 sprintf (wbuf
, "%d", true_indepth
- 1);
2118 wbuf
= (char *) alloca (3 + strlen (version_string
));
2119 sprintf (wbuf
, "\"%s\"", version_string
);
2123 #ifndef NO_BUILTIN_SIZE_TYPE
2129 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2130 case T_PTRDIFF_TYPE
:
2136 buf
= CPP_WCHAR_TYPE (pfile
);
2139 case T_USER_LABEL_PREFIX_TYPE
:
2140 buf
= user_label_prefix
;
2143 case T_REGISTER_PREFIX_TYPE
:
2144 buf
= REGISTER_PREFIX
;
2148 wbuf
= (char *) alloca (4 * sizeof (int));
2149 sprintf (wbuf
, "%d", hp
->value
.ival
);
2150 #ifdef STDC_0_IN_SYSTEM_HEADERS
2151 if (ip
->system_header_p
2152 && hp
->length
== 8 && bcmp (hp
->name
, "__STDC__", 8) == 0
2153 && ! cpp_lookup (pfile
, (U_CHAR
*) "__STRICT_ANSI__", -1, -1))
2157 if (pcp_inside_if
&& pcp_outfile
)
2158 /* Output a precondition for this macro use */
2159 fprintf (pcp_outfile
, "#define %s %d\n", hp
->name
, hp
->value
.ival
);
2166 long line
= ip
->lineno
;
2167 long col
= ip
->colno
;
2168 adjust_position (CPP_LINE_BASE (ip
), ip
->cur
, &line
, &col
);
2170 wbuf
= (char *) alloca (10);
2171 sprintf (wbuf
, "%ld", line
);
2178 wbuf
= (char *) alloca (20);
2179 timebuf
= timestamp (pfile
);
2180 if (hp
->type
== T_DATE
)
2181 sprintf (wbuf
, "\"%s %2d %4d\"", monthnames
[timebuf
->tm_mon
],
2182 timebuf
->tm_mday
, timebuf
->tm_year
+ 1900);
2184 sprintf (wbuf
, "\"%02d:%02d:%02d\"", timebuf
->tm_hour
, timebuf
->tm_min
,
2189 case T_SPEC_DEFINED
:
2190 buf
= " 0 "; /* Assume symbol is not defined */
2191 ip
= CPP_BUFFER (pfile
);
2192 SKIP_WHITE_SPACE (ip
->cur
);
2193 if (*ip
->cur
== '(')
2196 ip
->cur
++; /* Skip over the paren */
2197 SKIP_WHITE_SPACE (ip
->cur
);
2200 if (!is_idstart
[*ip
->cur
])
2202 if (ip
->cur
[0] == 'L' && (ip
->cur
[1] == '\'' || ip
->cur
[1] == '"'))
2204 if ((hp
= cpp_lookup (pfile
, ip
->cur
, -1, -1)))
2207 if (pcp_outfile
&& pcp_inside_if
2208 && (hp
->type
== T_CONST
2209 || (hp
->type
== T_MACRO
&& hp
->value
.defn
->predefined
)))
2210 /* Output a precondition for this macro use. */
2211 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
2217 if (pcp_outfile
&& pcp_inside_if
)
2219 /* Output a precondition for this macro use */
2220 U_CHAR
*cp
= ip
->bufp
;
2221 fprintf (pcp_outfile
, "#undef ");
2222 while (is_idchar
[*cp
]) /* Ick! */
2223 fputc (*cp
++, pcp_outfile
);
2224 putc ('\n', pcp_outfile
);
2227 while (is_idchar
[*ip
->cur
])
2229 SKIP_WHITE_SPACE (ip
->cur
);
2232 if (*ip
->cur
!= ')')
2240 cpp_error (pfile
, "`defined' without an identifier");
2244 cpp_error (pfile
, "cccp error: invalid special hash type"); /* time for gdb */
2248 CPP_RESERVE (pfile
, len
+ 1);
2249 CPP_PUTS_Q (pfile
, buf
, len
);
2250 CPP_NUL_TERMINATE_Q (pfile
);
2255 /* Write out a #define command for the special named MACRO_NAME
2256 to PFILE's token_buffer. */
2259 dump_special_to_buffer (pfile
, macro_name
)
2263 static char define_directive
[] = "#define ";
2264 int macro_name_length
= strlen (macro_name
);
2265 output_line_command (pfile
, 0, same_file
);
2266 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
2267 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
2268 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
2269 CPP_PUTC_Q (pfile
, ' ');
2270 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
2271 CPP_PUTC (pfile
, '\n');
2274 /* Initialize the built-in macros. */
2277 initialize_builtins (pfile
)
2280 install ((U_CHAR
*)"__LINE__", -1, T_SPECLINE
, 0, 0, -1);
2281 install ((U_CHAR
*)"__DATE__", -1, T_DATE
, 0, 0, -1);
2282 install ((U_CHAR
*)"__FILE__", -1, T_FILE
, 0, 0, -1);
2283 install ((U_CHAR
*)"__BASE_FILE__", -1, T_BASE_FILE
, 0, 0, -1);
2284 install ((U_CHAR
*)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL
, 0, 0, -1);
2285 install ((U_CHAR
*)"__VERSION__", -1, T_VERSION
, 0, 0, -1);
2286 #ifndef NO_BUILTIN_SIZE_TYPE
2287 install ((U_CHAR
*)"__SIZE_TYPE__", -1, T_SIZE_TYPE
, 0, 0, -1);
2289 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2290 install ((U_CHAR
*)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE
, 0, 0, -1);
2292 install ((U_CHAR
*)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE
, 0, 0, -1);
2293 install ((U_CHAR
*)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE
, 0, 0, -1);
2294 install ((U_CHAR
*)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE
, 0, 0, -1);
2295 install ((U_CHAR
*)"__TIME__", -1, T_TIME
, 0, 0, -1);
2296 if (!CPP_TRADITIONAL (pfile
))
2297 install ((U_CHAR
*)"__STDC__", -1, T_CONST
, STDC_VALUE
, 0, -1);
2298 if (CPP_OPTIONS (pfile
)->objc
)
2299 install ((U_CHAR
*)"__OBJC__", -1, T_CONST
, 1, 0, -1);
2300 /* This is supplied using a -D by the compiler driver
2301 so that it is present only when truly compiling with GNU C. */
2302 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2304 if (CPP_OPTIONS (pfile
)->debug_output
)
2306 dump_special_to_buffer (pfile
, "__BASE_FILE__");
2307 dump_special_to_buffer (pfile
, "__VERSION__");
2308 #ifndef NO_BUILTIN_SIZE_TYPE
2309 dump_special_to_buffer (pfile
, "__SIZE_TYPE__");
2311 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2312 dump_special_to_buffer (pfile
, "__PTRDIFF_TYPE__");
2314 dump_special_to_buffer (pfile
, "__WCHAR_TYPE__");
2315 dump_special_to_buffer (pfile
, "__DATE__");
2316 dump_special_to_buffer (pfile
, "__TIME__");
2317 if (!CPP_TRADITIONAL (pfile
))
2318 dump_special_to_buffer (pfile
, "__STDC__");
2319 if (CPP_OPTIONS (pfile
)->objc
)
2320 dump_special_to_buffer (pfile
, "__OBJC__");
2324 /* Return 1 iff a token ending in C1 followed directly by a token C2
2325 could cause mis-tokenization. */
2328 unsafe_chars (c1
, c2
)
2334 if (c2
== c1
|| c2
== '=')
2338 case '0': case '1': case '2': case '3': case '4':
2339 case '5': case '6': case '7': case '8': case '9':
2340 case 'e': case 'E': case 'p': case 'P':
2341 if (c2
== '-' || c2
== '+')
2342 return 1; /* could extend a pre-processing number */
2345 if (c2
== '\'' || c2
== '\"')
2346 return 1; /* Could turn into L"xxx" or L'xxx'. */
2350 case 'a': case 'b': case 'c': case 'd': case 'f':
2351 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2352 case 'm': case 'n': case 'o': case 'q': case 'r':
2353 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2355 case 'A': case 'B': case 'C': case 'D': case 'F':
2356 case 'G': case 'H': case 'I': case 'J': case 'K':
2357 case 'M': case 'N': case 'O': case 'Q': case 'R':
2358 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2360 /* We're in the middle of either a name or a pre-processing number. */
2361 return (is_idchar
[c2
] || c2
== '.');
2362 case '<': case '>': case '!': case '%': case '#': case ':':
2363 case '^': case '&': case '|': case '*': case '/': case '=':
2364 return (c2
== c1
|| c2
== '=');
2369 /* Expand a macro call.
2370 HP points to the symbol that is the macro being called.
2371 Put the result of expansion onto the input stack
2372 so that subsequent input by our caller will use it.
2374 If macro wants arguments, caller has already verified that
2375 an argument list follows; arguments come from the input stack. */
2378 macroexpand (pfile
, hp
)
2383 DEFINITION
*defn
= hp
->value
.defn
;
2384 register U_CHAR
*xbuf
;
2385 long start_line
, start_column
;
2387 struct argdata
*args
;
2388 long old_written
= CPP_WRITTEN (pfile
);
2390 int start_line
= instack
[indepth
].lineno
;
2392 int rest_args
, rest_zero
;
2396 CHECK_DEPTH (return;);
2400 /* This macro is being used inside a #if, which means it must be */
2401 /* recorded as a precondition. */
2402 if (pcp_inside_if
&& pcp_outfile
&& defn
->predefined
)
2403 dump_single_macro (hp
, pcp_outfile
);
2406 pfile
->output_escapes
++;
2407 cpp_buf_line_and_col (cpp_file_buffer (pfile
), &start_line
, &start_column
);
2409 nargs
= defn
->nargs
;
2413 enum cpp_token token
;
2415 args
= (struct argdata
*) alloca ((nargs
+ 1) * sizeof (struct argdata
));
2417 for (i
= 0; i
< nargs
; i
++)
2419 args
[i
].raw
= args
[i
].expanded
= 0;
2420 args
[i
].raw_length
= 0;
2421 args
[i
].expand_length
= args
[i
].stringified_length
= -1;
2422 args
[i
].use_count
= 0;
2425 /* Parse all the macro args that are supplied. I counts them.
2426 The first NARGS args are stored in ARGS.
2427 The rest are discarded. If rest_args is set then we assume
2428 macarg absorbed the rest of the args. */
2432 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2437 if (i
< nargs
|| (nargs
== 0 && i
== 0))
2439 /* if we are working on last arg which absorbs rest of args... */
2440 if (i
== nargs
- 1 && defn
->rest_args
)
2442 args
[i
].raw
= CPP_WRITTEN (pfile
);
2443 token
= macarg (pfile
, rest_args
);
2444 args
[i
].raw_length
= CPP_WRITTEN (pfile
) - args
[i
].raw
;
2445 args
[i
].newlines
= 0; /* FIXME */
2448 token
= macarg (pfile
, 0);
2449 if (token
== CPP_EOF
|| token
== CPP_POP
)
2451 cpp_error_with_line (pfile
, start_line
, start_column
,
2452 "unterminated macro call");
2456 } while (token
== CPP_COMMA
);
2458 /* If we got one arg but it was just whitespace, call that 0 args. */
2461 register U_CHAR
*bp
= ARG_BASE
+ args
[0].raw
;
2462 register U_CHAR
*lim
= bp
+ args
[0].raw_length
;
2463 /* cpp.texi says for foo ( ) we provide one argument.
2464 However, if foo wants just 0 arguments, treat this as 0. */
2466 while (bp
!= lim
&& is_space
[*bp
]) bp
++;
2471 /* Don't output an error message if we have already output one for
2472 a parse error above. */
2474 if (nargs
== 0 && i
> 0)
2476 cpp_error (pfile
, "arguments given to macro `%s'", hp
->name
);
2480 /* traditional C allows foo() if foo wants one argument. */
2481 if (nargs
== 1 && i
== 0 && CPP_TRADITIONAL (pfile
))
2483 /* the rest args token is allowed to absorb 0 tokens */
2484 else if (i
== nargs
- 1 && defn
->rest_args
)
2487 cpp_error (pfile
, "macro `%s' used without args", hp
->name
);
2489 cpp_error (pfile
, "macro `%s' used with just one arg", hp
->name
);
2491 cpp_error (pfile
, "macro `%s' used with only %d args",
2497 "macro `%s' used with too many (%d) args", hp
->name
, i
);
2501 /* If macro wants zero args, we parsed the arglist for checking only.
2502 Read directly from the macro definition. */
2505 xbuf
= defn
->expansion
;
2506 xbuf_len
= defn
->length
;
2510 register U_CHAR
*exp
= defn
->expansion
;
2511 register int offset
; /* offset in expansion,
2512 copied a piece at a time */
2513 register int totlen
; /* total amount of exp buffer filled so far */
2515 register struct reflist
*ap
, *last_ap
;
2517 /* Macro really takes args. Compute the expansion of this call. */
2519 /* Compute length in characters of the macro's expansion.
2520 Also count number of times each arg is used. */
2521 xbuf_len
= defn
->length
;
2522 for (ap
= defn
->pattern
; ap
!= NULL
; ap
= ap
->next
)
2526 register struct argdata
*arg
= &args
[ap
->argno
];
2527 /* Stringify if it hasn't already been */
2528 if (arg
->stringified_length
< 0)
2530 int arglen
= arg
->raw_length
;
2534 /* Initially need_space is -1. Otherwise, 1 means the
2535 previous character was a space, but we suppressed it;
2536 0 means the previous character was a non-space. */
2537 int need_space
= -1;
2539 arg
->stringified
= CPP_WRITTEN (pfile
);
2540 if (!CPP_TRADITIONAL (pfile
))
2541 CPP_PUTC (pfile
, '\"'); /* insert beginning quote */
2542 for (; i
< arglen
; i
++)
2544 c
= (ARG_BASE
+ arg
->raw
)[i
];
2548 /* Internal sequences of whitespace are replaced by
2549 one space except within an string or char token.*/
2552 if (CPP_WRITTEN (pfile
) > (unsigned)arg
->stringified
2553 && (CPP_PWRITTEN (pfile
))[-1] == '@')
2555 /* "@ " escape markers are removed */
2556 CPP_ADJUST_WRITTEN (pfile
, -1);
2559 if (need_space
== 0)
2563 else if (need_space
> 0)
2564 CPP_PUTC (pfile
, ' ');
2579 else if (c
== '\"' || c
== '\'')
2583 /* Escape these chars */
2584 if (c
== '\"' || (in_string
&& c
== '\\'))
2585 CPP_PUTC (pfile
, '\\');
2587 CPP_PUTC (pfile
, c
);
2590 CPP_RESERVE (pfile
, 4);
2591 sprintf ((char *)CPP_PWRITTEN (pfile
), "\\%03o",
2593 CPP_ADJUST_WRITTEN (pfile
, 4);
2596 if (!CPP_TRADITIONAL (pfile
))
2597 CPP_PUTC (pfile
, '\"'); /* insert ending quote */
2598 arg
->stringified_length
2599 = CPP_WRITTEN (pfile
) - arg
->stringified
;
2601 xbuf_len
+= args
[ap
->argno
].stringified_length
;
2603 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2604 /* Add 4 for two newline-space markers to prevent
2605 token concatenation. */
2606 xbuf_len
+= args
[ap
->argno
].raw_length
+ 4;
2609 /* We have an ordinary (expanded) occurrence of the arg.
2610 So compute its expansion, if we have not already. */
2611 if (args
[ap
->argno
].expand_length
< 0)
2613 args
[ap
->argno
].expanded
= CPP_WRITTEN (pfile
);
2614 cpp_expand_to_buffer (pfile
,
2615 ARG_BASE
+ args
[ap
->argno
].raw
,
2616 args
[ap
->argno
].raw_length
);
2618 args
[ap
->argno
].expand_length
2619 = CPP_WRITTEN (pfile
) - args
[ap
->argno
].expanded
;
2622 /* Add 4 for two newline-space markers to prevent
2623 token concatenation. */
2624 xbuf_len
+= args
[ap
->argno
].expand_length
+ 4;
2626 if (args
[ap
->argno
].use_count
< 10)
2627 args
[ap
->argno
].use_count
++;
2630 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
2632 /* Generate in XBUF the complete expansion
2633 with arguments substituted in.
2634 TOTLEN is the total size generated so far.
2635 OFFSET is the index in the definition
2636 of where we are copying from. */
2637 offset
= totlen
= 0;
2638 for (last_ap
= NULL
, ap
= defn
->pattern
; ap
!= NULL
;
2639 last_ap
= ap
, ap
= ap
->next
)
2641 register struct argdata
*arg
= &args
[ap
->argno
];
2642 int count_before
= totlen
;
2644 /* Add chars to XBUF. */
2645 for (i
= 0; i
< ap
->nchars
; i
++, offset
++)
2646 xbuf
[totlen
++] = exp
[offset
];
2648 /* If followed by an empty rest arg with concatenation,
2649 delete the last run of nonwhite chars. */
2650 if (rest_zero
&& totlen
> count_before
2651 && ((ap
->rest_args
&& ap
->raw_before
)
2652 || (last_ap
!= NULL
&& last_ap
->rest_args
2653 && last_ap
->raw_after
)))
2655 /* Delete final whitespace. */
2656 while (totlen
> count_before
&& is_space
[xbuf
[totlen
- 1]])
2659 /* Delete the nonwhites before them. */
2660 while (totlen
> count_before
&& ! is_space
[xbuf
[totlen
- 1]])
2664 if (ap
->stringify
!= 0)
2666 bcopy (ARG_BASE
+ arg
->stringified
,
2667 xbuf
+ totlen
, arg
->stringified_length
);
2668 totlen
+= arg
->stringified_length
;
2670 else if (ap
->raw_before
|| ap
->raw_after
|| CPP_TRADITIONAL (pfile
))
2672 U_CHAR
*p1
= ARG_BASE
+ arg
->raw
;
2673 U_CHAR
*l1
= p1
+ arg
->raw_length
;
2676 while (p1
!= l1
&& is_space
[*p1
]) p1
++;
2677 while (p1
!= l1
&& is_idchar
[*p1
])
2678 xbuf
[totlen
++] = *p1
++;
2682 /* Arg is concatenated after: delete trailing whitespace,
2683 whitespace markers, and no-reexpansion markers. */
2686 if (is_space
[l1
[-1]]) l1
--;
2687 else if (l1
[-1] == '@')
2689 U_CHAR
*p2
= l1
- 1;
2690 /* If whitespace is preceded by an odd number
2691 of `@' signs, the last `@' was a whitespace
2692 marker; drop it too. */
2693 while (p2
!= p1
&& p2
[0] == '@') p2
--;
2698 else if (l1
[-1] == '-')
2700 U_CHAR
*p2
= l1
- 1;
2701 /* If a `-' is preceded by an odd number of
2702 `@' signs then it and the last `@' are
2703 a no-reexpansion marker. */
2704 while (p2
!= p1
&& p2
[0] == '@') p2
--;
2714 /* Delete any no-reexpansion marker that precedes
2715 an identifier at the beginning of the argument. */
2716 if (p1
[0] == '@' && p1
[1] == '-')
2719 bcopy (p1
, xbuf
+ totlen
, l1
- p1
);
2724 U_CHAR
*expanded
= ARG_BASE
+ arg
->expanded
;
2725 if (!ap
->raw_before
&& totlen
> 0 && arg
->expand_length
2726 && !CPP_TRADITIONAL(pfile
)
2727 && unsafe_chars (xbuf
[totlen
-1], expanded
[0]))
2729 xbuf
[totlen
++] = '@';
2730 xbuf
[totlen
++] = ' ';
2733 bcopy (expanded
, xbuf
+ totlen
, arg
->expand_length
);
2734 totlen
+= arg
->expand_length
;
2736 if (!ap
->raw_after
&& totlen
> 0 && offset
< defn
->length
2737 && !CPP_TRADITIONAL(pfile
)
2738 && unsafe_chars (xbuf
[totlen
-1], exp
[offset
]))
2740 xbuf
[totlen
++] = '@';
2741 xbuf
[totlen
++] = ' ';
2744 /* If a macro argument with newlines is used multiple times,
2745 then only expand the newlines once. This avoids creating
2746 output lines which don't correspond to any input line,
2747 which confuses gdb and gcov. */
2748 if (arg
->use_count
> 1 && arg
->newlines
> 0)
2750 /* Don't bother doing change_newlines for subsequent
2754 = change_newlines (expanded
, arg
->expand_length
);
2758 if (totlen
> xbuf_len
)
2762 /* if there is anything left of the definition
2763 after handling the arg list, copy that in too. */
2765 for (i
= offset
; i
< defn
->length
; i
++)
2767 /* if we've reached the end of the macro */
2770 if (! (rest_zero
&& last_ap
!= NULL
&& last_ap
->rest_args
2771 && last_ap
->raw_after
))
2772 xbuf
[totlen
++] = exp
[i
];
2780 pfile
->output_escapes
--;
2782 /* Now put the expansion on the input stack
2783 so our caller will commence reading from it. */
2784 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
2785 CPP_BUFFER (pfile
)->has_escapes
= 1;
2787 /* Pop the space we've used in the token_buffer for argument expansion. */
2788 CPP_SET_WRITTEN (pfile
, old_written
);
2790 /* Recursive macro use sometimes works traditionally.
2791 #define foo(x,y) bar (x (y,0), y)
2794 if (!CPP_TRADITIONAL (pfile
))
2795 hp
->type
= T_DISABLED
;
2799 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
)
2801 register U_CHAR
*xbuf
;
2805 register cpp_buffer
*mbuf
= cpp_push_buffer (pfile
, xbuf
, xbuf_len
);
2808 mbuf
->cleanup
= macro_cleanup
;
2811 /* The first chars of the expansion should be a "@ " added by
2812 collect_expansion. This is to prevent accidental token-pasting
2813 between the text preceding the macro invocation, and the macro
2816 We would like to avoid adding unneeded spaces (for the sake of
2817 tools that use cpp, such as imake). In some common cases we can
2818 tell that it is safe to omit the space.
2820 The character before the macro invocation cannot have been an
2821 idchar (or else it would have been pasted with the idchars of
2822 the macro name). Therefore, if the first non-space character
2823 of the expansion is an idchar, we do not need the extra space
2824 to prevent token pasting.
2826 Also, we don't need the extra space if the first char is '(',
2827 or some other (less common) characters. */
2829 if (xbuf
[0] == '@' && xbuf
[1] == ' '
2830 && (is_idchar
[xbuf
[2]] || xbuf
[2] == '(' || xbuf
[2] == '\''
2831 || xbuf
[2] == '\"'))
2835 /* Like cpp_get_token, except that it does not read past end-of-line.
2836 Also, horizontal space is skipped, and macros are popped. */
2838 static enum cpp_token
2839 get_directive_token (pfile
)
2844 long old_written
= CPP_WRITTEN (pfile
);
2845 enum cpp_token token
;
2846 cpp_skip_hspace (pfile
);
2847 if (PEEKC () == '\n')
2849 token
= cpp_get_token (pfile
);
2853 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
2855 /* ... else fall though ... */
2856 case CPP_HSPACE
: case CPP_COMMENT
:
2857 CPP_SET_WRITTEN (pfile
, old_written
);
2865 /* Handle #include and #import.
2866 This function expects to see "fname" or <fname> on the input.
2868 The input is normally in part of the output_buffer following
2869 CPP_WRITTEN, and will get overwritten by output_line_command.
2870 I.e. in input file specification has been popped by handle_directive.
2874 do_include (pfile
, keyword
)
2876 struct directive
*keyword
;
2878 int importing
= (keyword
->type
== T_IMPORT
);
2879 int skip_dirs
= (keyword
->type
== T_INCLUDE_NEXT
);
2880 int angle_brackets
= 0; /* 0 for "...", 1 for <...> */
2881 int before
; /* included before? */
2886 enum cpp_token token
;
2888 /* Chain of dirs to search */
2889 struct include_hash
*ihash
;
2890 struct file_name_list
*search_start
;
2892 long old_written
= CPP_WRITTEN (pfile
);
2896 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
2899 cpp_pedwarn (pfile
, "ANSI C does not allow `#import'");
2901 cpp_pedwarn (pfile
, "ANSI C does not allow `#include_next'");
2904 if (importing
&& CPP_OPTIONS (pfile
)->warn_import
2905 && !CPP_OPTIONS (pfile
)->inhibit_warnings
2906 && !CPP_BUFFER (pfile
)->system_header_p
&& !pfile
->import_warning
)
2908 pfile
->import_warning
= 1;
2909 cpp_warning (pfile
, "`#import' is obsolete, use an #ifdef wrapper in the header file");
2912 pfile
->parsing_include_directive
++;
2913 token
= get_directive_token (pfile
);
2914 pfile
->parsing_include_directive
--;
2916 if (token
== CPP_STRING
)
2918 fbeg
= pfile
->token_buffer
+ old_written
+ 1;
2919 fend
= CPP_PWRITTEN (pfile
) - 1;
2921 if (fbeg
[-1] == '<')
2925 else if (token
== CPP_NAME
)
2927 /* Support '#include xyz' like VAX-C to allow for easy use of
2928 * all the decwindow include files. It defaults to '#include
2929 * <xyz.h>' and generates a warning. */
2931 "VAX-C-style include specification found, use '#include <filename.h>' !");
2934 /* Append the missing `.h' to the name. */
2935 CPP_PUTS (pfile
, ".h", 3)
2936 CPP_NUL_TERMINATE_Q (pfile
);
2938 fbeg
= pfile
->token_buffer
+ old_written
;
2939 fend
= CPP_PWRITTEN (pfile
);
2945 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword
->name
);
2946 CPP_SET_WRITTEN (pfile
, old_written
);
2947 skip_rest_of_line (pfile
);
2951 token
= get_directive_token (pfile
);
2952 if (token
!= CPP_VSPACE
)
2954 cpp_error (pfile
, "junk at end of `#include'");
2955 skip_rest_of_line (pfile
);
2958 CPP_SET_WRITTEN (pfile
, old_written
);
2964 cpp_error (pfile
, "empty file name in `#%s'", keyword
->name
);
2970 for (fp
= CPP_BUFFER (pfile
);
2971 fp
!= CPP_NULL_BUFFER (pfile
);
2972 fp
= CPP_PREV_BUFFER (fp
))
2973 if (fp
->fname
!= NULL
)
2976 if (fp
== CPP_NULL_BUFFER (pfile
))
2978 cpp_fatal (pfile
, "cpp internal error: fp == NULL_BUFFER in do_include");
2982 /* For #include_next, skip in the search path past the dir in which the
2983 containing file was found. Treat files specified using an absolute path
2984 as if there are no more directories to search. Treat the primary source
2985 file like any other included source, but generate a warning. */
2986 if (skip_dirs
&& CPP_PREV_BUFFER(fp
) != CPP_NULL_BUFFER (pfile
))
2988 if (fp
->ihash
->foundhere
!= ABSOLUTE_PATH
)
2989 search_start
= fp
->ihash
->foundhere
->next
;
2994 cpp_warning (pfile
, "#include_next in primary source file");
2997 search_start
= CPP_OPTIONS (pfile
)->bracket_include
;
3000 if (!CPP_OPTIONS (pfile
)->ignore_srcdir
)
3003 search_start
= fp
->actual_dir
;
3006 search_start
= CPP_OPTIONS (pfile
)->quote_include
;
3012 cpp_error (pfile
, "No include path in which to find %s", fbeg
);
3016 fd
= find_include_file (pfile
, fbeg
, search_start
, &ihash
, &before
);
3023 if (CPP_OPTIONS (pfile
)->print_deps_missing_files
3024 && CPP_PRINT_DEPS (pfile
) > (angle_brackets
||
3025 (pfile
->system_include_depth
> 0)))
3027 if (!angle_brackets
)
3028 deps_output (pfile
, fbeg
, ' ');
3032 struct file_name_list
*ptr
;
3033 /* If requested as a system header, assume it belongs in
3034 the first system header directory. */
3035 if (CPP_OPTIONS (pfile
)->bracket_include
)
3036 ptr
= CPP_OPTIONS (pfile
)->bracket_include
;
3038 ptr
= CPP_OPTIONS (pfile
)->quote_include
;
3040 p
= (char *) alloca (strlen (ptr
->name
)
3041 + strlen (fbeg
) + 2);
3042 if (*ptr
->name
!= '\0')
3044 strcpy (p
, ptr
->name
);
3048 deps_output (pfile
, p
, ' ');
3051 /* If -M was specified, and this header file won't be added to
3052 the dependency list, then don't count this as an error,
3053 because we can still produce correct output. Otherwise, we
3054 can't produce correct output, because there may be
3055 dependencies we need inside the missing file, and we don't
3056 know what directory this missing file exists in. */
3057 else if (CPP_PRINT_DEPS (pfile
)
3058 && (CPP_PRINT_DEPS (pfile
)
3059 <= (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3060 cpp_warning (pfile
, "No include path in which to find %s", fbeg
);
3062 cpp_error_from_errno (pfile
, fbeg
);
3067 /* For -M, add the file to the dependencies on its first inclusion. */
3068 if (!before
&& (CPP_PRINT_DEPS (pfile
)
3069 > (angle_brackets
|| (pfile
->system_include_depth
> 0))))
3070 deps_output (pfile
, ihash
->name
, ' ');
3072 /* Handle -H option. */
3073 if (CPP_OPTIONS(pfile
)->print_include_names
)
3075 fp
= CPP_BUFFER (pfile
);
3076 while ((fp
= CPP_PREV_BUFFER (fp
)) != CPP_NULL_BUFFER (pfile
))
3078 fprintf (stderr
, " %s\n", ihash
->name
);
3081 /* Actually process the file */
3084 ihash
->control_macro
= "";
3086 if (cpp_push_buffer (pfile
, NULL
, 0) == NULL
)
3093 pfile
->system_include_depth
++; /* Decremented in file_cleanup. */
3095 if (finclude (pfile
, fd
, ihash
))
3097 output_line_command (pfile
, 0, enter_file
);
3098 pfile
->only_seen_white
= 2;
3105 /* Convert a character string literal into a nul-terminated string.
3106 The input string is [IN ... LIMIT).
3107 The result is placed in RESULT. RESULT can be the same as IN.
3108 The value returned in the end of the string written to RESULT,
3109 or NULL on error. */
3112 convert_string (pfile
, result
, in
, limit
, handle_escapes
)
3114 register U_CHAR
*result
, *in
, *limit
;
3134 char *bpc
= (char *) in
;
3135 int i
= (U_CHAR
) cpp_parse_escape (pfile
, &bpc
, 0x00ff);
3136 in
= (U_CHAR
*) bpc
;
3138 *result
++ = (U_CHAR
)c
;
3141 /* else fall through */
3151 * interpret #line command. Remembers previously seen fnames
3152 * in its very own hash table.
3154 #define FNAME_HASHSIZE 37
3157 do_line (pfile
, keyword
)
3159 struct directive
*keyword ATTRIBUTE_UNUSED
;
3161 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3163 long old_written
= CPP_WRITTEN (pfile
);
3164 enum file_change_code file_change
= same_file
;
3165 enum cpp_token token
;
3167 token
= get_directive_token (pfile
);
3169 if (token
!= CPP_NUMBER
3170 || !ISDIGIT(pfile
->token_buffer
[old_written
]))
3172 cpp_error (pfile
, "invalid format `#line' command");
3173 goto bad_line_directive
;
3176 /* The Newline at the end of this line remains to be processed.
3177 To put the next line at the specified line number,
3178 we must store a line number now that is one less. */
3179 new_lineno
= atoi ((char *)(pfile
->token_buffer
+ old_written
)) - 1;
3180 CPP_SET_WRITTEN (pfile
, old_written
);
3182 /* NEW_LINENO is one less than the actual line number here. */
3183 if (CPP_PEDANTIC (pfile
) && new_lineno
< 0)
3184 cpp_pedwarn (pfile
, "line number out of range in `#line' command");
3186 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3187 if (PEEKC() && !is_space
[PEEKC()]) {
3188 cpp_error (pfile
, "invalid format `#line' command");
3189 goto bad_line_directive
;
3193 token
= get_directive_token (pfile
);
3195 if (token
== CPP_STRING
) {
3196 U_CHAR
*fname
= pfile
->token_buffer
+ old_written
;
3198 static HASHNODE
*fname_table
[FNAME_HASHSIZE
];
3199 HASHNODE
*hp
, **hash_bucket
;
3204 /* Turn the file name, which is a character string literal,
3205 into a null-terminated string. Do this in place. */
3206 end_name
= convert_string (pfile
, fname
, fname
, CPP_PWRITTEN (pfile
), 1);
3207 if (end_name
== NULL
)
3209 cpp_error (pfile
, "invalid format `#line' command");
3210 goto bad_line_directive
;
3213 fname_length
= end_name
- fname
;
3215 num_start
= CPP_WRITTEN (pfile
);
3216 token
= get_directive_token (pfile
);
3217 if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
&& token
!= CPP_POP
) {
3218 p
= pfile
->token_buffer
+ num_start
;
3219 if (CPP_PEDANTIC (pfile
))
3220 cpp_pedwarn (pfile
, "garbage at end of `#line' command");
3222 if (token
!= CPP_NUMBER
|| *p
< '0' || *p
> '4' || p
[1] != '\0')
3224 cpp_error (pfile
, "invalid format `#line' command");
3225 goto bad_line_directive
;
3228 file_change
= enter_file
;
3230 file_change
= leave_file
;
3232 ip
->system_header_p
= 1;
3233 else /* if (*p == '4') */
3234 ip
->system_header_p
= 2;
3236 CPP_SET_WRITTEN (pfile
, num_start
);
3237 token
= get_directive_token (pfile
);
3238 p
= pfile
->token_buffer
+ num_start
;
3239 if (token
== CPP_NUMBER
&& p
[1] == '\0' && (*p
== '3' || *p
== '4')) {
3240 ip
->system_header_p
= *p
== '3' ? 1 : 2;
3241 token
= get_directive_token (pfile
);
3243 if (token
!= CPP_VSPACE
) {
3244 cpp_error (pfile
, "invalid format `#line' command");
3245 goto bad_line_directive
;
3249 hash_bucket
= &fname_table
[hashf (fname
, fname_length
, FNAME_HASHSIZE
)];
3250 for (hp
= *hash_bucket
; hp
!= NULL
; hp
= hp
->next
)
3251 if (hp
->length
== fname_length
3252 && strncmp (hp
->value
.cpval
, fname
, fname_length
) == 0) {
3253 ip
->nominal_fname
= hp
->value
.cpval
;
3257 /* Didn't find it; cons up a new one. */
3258 hp
= (HASHNODE
*) xcalloc (1, sizeof (HASHNODE
) + fname_length
+ 1);
3259 hp
->next
= *hash_bucket
;
3262 hp
->length
= fname_length
;
3263 ip
->nominal_fname
= hp
->value
.cpval
= ((char *) hp
) + sizeof (HASHNODE
);
3264 bcopy (fname
, hp
->value
.cpval
, fname_length
);
3267 else if (token
!= CPP_VSPACE
&& token
!= CPP_EOF
) {
3268 cpp_error (pfile
, "invalid format `#line' command");
3269 goto bad_line_directive
;
3272 ip
->lineno
= new_lineno
;
3274 skip_rest_of_line (pfile
);
3275 CPP_SET_WRITTEN (pfile
, old_written
);
3276 output_line_command (pfile
, 0, file_change
);
3281 * remove the definition of a symbol from the symbol table.
3282 * according to un*x /lib/cpp, it is not an error to undef
3283 * something that has no definitions, so it isn't one here either.
3287 do_undef (pfile
, keyword
)
3289 struct directive
*keyword
;
3293 U_CHAR
*buf
, *name
, *limit
;
3295 long here
= CPP_WRITTEN (pfile
);
3296 enum cpp_token token
;
3298 cpp_skip_hspace (pfile
);
3300 if (! is_idstart
[c
])
3302 cpp_error (pfile
, "token after #undef is not an identifier");
3303 skip_rest_of_line (pfile
);
3307 parse_name (pfile
, c
);
3308 buf
= pfile
->token_buffer
+ here
;
3309 limit
= CPP_PWRITTEN(pfile
);
3311 /* Copy out the token so we can pop the token buffer. */
3312 name
= alloca (limit
- buf
+ 1);
3313 bcopy(buf
, name
, limit
- buf
);
3314 name
[limit
- buf
] = '\0';
3316 token
= get_directive_token (pfile
);
3317 if (token
!= CPP_VSPACE
&& token
!= CPP_POP
)
3319 cpp_pedwarn (pfile
, "junk on line after #undef");
3320 skip_rest_of_line (pfile
);
3323 CPP_SET_WRITTEN (pfile
, here
);
3326 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3327 if (pcp_outfile
&& keyword
)
3328 pass_thru_directive (buf
, limit
, pfile
, keyword
);
3331 sym_length
= check_macro_name (pfile
, name
, "macro");
3333 while ((hp
= cpp_lookup (pfile
, name
, sym_length
, -1)) != NULL
)
3335 /* If we are generating additional info for debugging (with -g) we
3336 need to pass through all effective #undef commands. */
3337 if (CPP_OPTIONS (pfile
)->debug_output
&& keyword
)
3338 pass_thru_directive (name
, name
+sym_length
, pfile
, keyword
);
3339 if (hp
->type
!= T_MACRO
)
3340 cpp_warning (pfile
, "undefining `%s'", hp
->name
);
3347 /* Wrap do_undef for -U processing. */
3349 cpp_undef (pfile
, macro
)
3353 if (cpp_push_buffer (pfile
, macro
, strlen(macro
)))
3355 do_undef (pfile
, NULL
);
3356 cpp_pop_buffer (pfile
);
3362 * Report an error detected by the program we are processing.
3363 * Use the text of the line in the error message.
3364 * (We use error because it prints the filename & line#.)
3368 do_error (pfile
, keyword
)
3370 struct directive
*keyword ATTRIBUTE_UNUSED
;
3372 long here
= CPP_WRITTEN (pfile
);
3374 copy_rest_of_line (pfile
);
3375 text
= pfile
->token_buffer
+ here
;
3376 SKIP_WHITE_SPACE(text
);
3378 cpp_error (pfile
, "#error %s", text
);
3379 CPP_SET_WRITTEN (pfile
, here
);
3384 * Report a warning detected by the program we are processing.
3385 * Use the text of the line in the warning message, then continue.
3389 do_warning (pfile
, keyword
)
3391 struct directive
*keyword ATTRIBUTE_UNUSED
;
3394 long here
= CPP_WRITTEN(pfile
);
3395 copy_rest_of_line (pfile
);
3396 text
= pfile
->token_buffer
+ here
;
3397 SKIP_WHITE_SPACE(text
);
3399 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3400 cpp_pedwarn (pfile
, "ANSI C does not allow `#warning'");
3402 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3403 if -pedantic-errors is given, #warning should cause an error. */
3404 cpp_pedwarn (pfile
, "#warning %s", text
);
3405 CPP_SET_WRITTEN (pfile
, here
);
3409 /* Report program identification. */
3412 do_ident (pfile
, keyword
)
3414 struct directive
*keyword ATTRIBUTE_UNUSED
;
3416 /* Allow #ident in system headers, since that's not user's fault. */
3417 if (CPP_PEDANTIC (pfile
) && !CPP_BUFFER (pfile
)->system_header_p
)
3418 cpp_pedwarn (pfile
, "ANSI C does not allow `#ident'");
3420 skip_rest_of_line (pfile
); /* Correct? Appears to match cccp. */
3425 /* Just check for some recognized pragmas that need validation here,
3426 and leave the text in the token buffer to be output. */
3429 do_pragma (pfile
, keyword
)
3431 struct directive
*keyword ATTRIBUTE_UNUSED
;
3433 long here
= CPP_WRITTEN (pfile
);
3436 copy_rest_of_line (pfile
);
3437 buf
= pfile
->token_buffer
+ here
;
3438 SKIP_WHITE_SPACE (buf
);
3440 if (!strncmp (buf
, "once", 4))
3442 cpp_buffer
*ip
= NULL
;
3444 /* Allow #pragma once in system headers, since that's not the user's
3446 if (!CPP_BUFFER (pfile
)->system_header_p
)
3447 cpp_warning (pfile
, "`#pragma once' is obsolete");
3449 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3451 if (ip
== CPP_NULL_BUFFER (pfile
))
3453 if (ip
->fname
!= NULL
)
3457 if (CPP_PREV_BUFFER (ip
) == CPP_NULL_BUFFER (pfile
))
3458 cpp_warning (pfile
, "`#pragma once' outside include file");
3460 ip
->ihash
->control_macro
= ""; /* never repeat */
3463 if (!strncmp (buf
, "implementation", 14))
3465 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3466 been included yet. */
3467 struct include_hash
*ptr
;
3468 U_CHAR
*p
= buf
+ 14, *fname
, *fcopy
;
3469 SKIP_WHITE_SPACE (p
);
3470 if (*p
== '\n' || *p
!= '\"')
3474 p
= (U_CHAR
*) index (fname
, '\"');
3476 fcopy
= alloca (p
- fname
+ 1);
3477 bcopy (fname
, fcopy
, p
- fname
);
3478 fcopy
[p
-fname
] = '\0';
3480 ptr
= include_hash (pfile
, fcopy
, 0);
3483 "`#pragma implementation' for `%s' appears after file is included",
3490 #ifdef SCCS_DIRECTIVE
3491 /* Just ignore #sccs, on systems where we define it at all. */
3494 do_sccs (pfile
, keyword
)
3496 struct directive
*keyword ATTRIBUTE_UNUSED
;
3498 if (CPP_PEDANTIC (pfile
))
3499 cpp_pedwarn (pfile
, "ANSI C does not allow `#sccs'");
3500 skip_rest_of_line (pfile
);
3506 * handle #if command by
3507 * 1) inserting special `defined' keyword into the hash table
3508 * that gets turned into 0 or 1 by special_symbol (thus,
3509 * if the luser has a symbol called `defined' already, it won't
3510 * work inside the #if command)
3511 * 2) rescan the input into a temporary output buffer
3512 * 3) pass the output buffer to the yacc parser and collect a value
3513 * 4) clean up the mess left from steps 1 and 2.
3514 * 5) call conditional_skip to skip til the next #endif (etc.),
3515 * or not, depending on the value from step 3.
3519 do_if (pfile
, keyword
)
3521 struct directive
*keyword ATTRIBUTE_UNUSED
;
3523 HOST_WIDE_INT value
= eval_if_expression (pfile
);
3524 conditional_skip (pfile
, value
== 0, T_IF
, NULL_PTR
);
3529 * handle a #elif directive by not changing if_stack either.
3530 * see the comment above do_else.
3534 do_elif (pfile
, keyword
)
3536 struct directive
*keyword ATTRIBUTE_UNUSED
;
3538 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
3539 cpp_error (pfile
, "`#elif' not within a conditional");
3542 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
3543 cpp_error (pfile
, "`#elif' after `#else'");
3545 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
3547 if (pfile
->if_stack
->fname
!= NULL
&& CPP_BUFFER (pfile
)->fname
!= NULL
3548 && strcmp (pfile
->if_stack
->fname
,
3549 CPP_BUFFER (pfile
)->nominal_fname
) != 0)
3550 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
3551 fprintf (stderr
, ")\n");
3553 pfile
->if_stack
->type
= T_ELIF
;
3556 if (pfile
->if_stack
->if_succeeded
)
3557 skip_if_group (pfile
);
3559 HOST_WIDE_INT value
= eval_if_expression (pfile
);
3561 skip_if_group (pfile
);
3563 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
3564 output_line_command (pfile
, 1, same_file
);
3571 * evaluate a #if expression in BUF, of length LENGTH,
3572 * then parse the result as a C expression and return the value as an int.
3575 static HOST_WIDE_INT
3576 eval_if_expression (pfile
)
3579 HASHNODE
*save_defined
;
3580 HOST_WIDE_INT value
;
3581 long old_written
= CPP_WRITTEN (pfile
);
3583 save_defined
= install ((U_CHAR
*)"defined", -1, T_SPEC_DEFINED
, 0, 0, -1);
3584 pfile
->pcp_inside_if
= 1;
3586 value
= cpp_parse_expr (pfile
);
3587 pfile
->pcp_inside_if
= 0;
3588 delete_macro (save_defined
); /* clean up special symbol */
3590 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
3596 * routine to handle ifdef/ifndef. Try to look up the symbol,
3597 * then do or don't skip to the #endif/#else/#elif depending
3598 * on what directive is actually being processed.
3602 do_xifdef (pfile
, keyword
)
3604 struct directive
*keyword
;
3607 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3610 enum cpp_token token
;
3611 int start_of_file
= 0;
3612 U_CHAR
*control_macro
= 0;
3613 int old_written
= CPP_WRITTEN (pfile
);
3615 /* Detect a #ifndef at start of file (not counting comments). */
3616 if (ip
->fname
!= 0 && keyword
->type
== T_IFNDEF
)
3617 start_of_file
= pfile
->only_seen_white
== 2;
3619 pfile
->no_macro_expand
++;
3620 token
= get_directive_token (pfile
);
3621 pfile
->no_macro_expand
--;
3623 ident
= pfile
->token_buffer
+ old_written
;
3624 ident_length
= CPP_WRITTEN (pfile
) - old_written
;
3625 CPP_SET_WRITTEN (pfile
, old_written
); /* Pop */
3627 if (token
== CPP_VSPACE
|| token
== CPP_POP
|| token
== CPP_EOF
)
3629 skip
= (keyword
->type
== T_IFDEF
);
3630 if (! CPP_TRADITIONAL (pfile
))
3631 cpp_pedwarn (pfile
, "`#%s' with no argument", keyword
->name
);
3633 else if (token
== CPP_NAME
)
3635 HASHNODE
*hp
= cpp_lookup (pfile
, ident
, ident_length
, -1);
3636 skip
= (hp
== NULL
) ^ (keyword
->type
== T_IFNDEF
);
3637 if (start_of_file
&& !skip
)
3639 control_macro
= (U_CHAR
*) xmalloc (ident_length
+ 1);
3640 bcopy (ident
, control_macro
, ident_length
+ 1);
3645 skip
= (keyword
->type
== T_IFDEF
);
3646 if (! CPP_TRADITIONAL (pfile
))
3647 cpp_error (pfile
, "`#%s' with invalid argument", keyword
->name
);
3650 if (!CPP_TRADITIONAL (pfile
))
3652 cpp_skip_hspace (pfile
);
3654 if (c
!= EOF
&& c
!= '\n')
3655 cpp_pedwarn (pfile
, "garbage at end of `#%s' argument", keyword
->name
);
3657 skip_rest_of_line (pfile
);
3661 /* Output a precondition for this macro. */
3662 if (hp
&& hp
->value
.defn
->predefined
)
3663 fprintf (pcp_outfile
, "#define %s\n", hp
->name
);
3666 fprintf (pcp_outfile
, "#undef ");
3667 while (is_idchar
[*cp
]) /* Ick! */
3668 fputc (*cp
++, pcp_outfile
);
3669 putc ('\n', pcp_outfile
);
3673 conditional_skip (pfile
, skip
, T_IF
, control_macro
);
3677 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3678 If this is a #ifndef starting at the beginning of a file,
3679 CONTROL_MACRO is the macro name tested by the #ifndef.
3680 Otherwise, CONTROL_MACRO is 0. */
3683 conditional_skip (pfile
, skip
, type
, control_macro
)
3686 enum node_type type
;
3687 U_CHAR
*control_macro
;
3689 IF_STACK_FRAME
*temp
;
3691 temp
= (IF_STACK_FRAME
*) xcalloc (1, sizeof (IF_STACK_FRAME
));
3692 temp
->fname
= CPP_BUFFER (pfile
)->nominal_fname
;
3694 temp
->lineno
= CPP_BUFFER (pfile
)->lineno
;
3696 temp
->next
= pfile
->if_stack
;
3697 temp
->control_macro
= control_macro
;
3698 pfile
->if_stack
= temp
;
3700 pfile
->if_stack
->type
= type
;
3703 skip_if_group (pfile
);
3706 ++pfile
->if_stack
->if_succeeded
;
3707 output_line_command (pfile
, 1, same_file
);
3711 /* Subroutine of skip_if_group. Examine one preprocessing directive and
3712 return 0 if skipping should continue, 1 if it should halt. Also
3713 adjusts the if_stack as appropriate.
3714 The `#' has been read, but not the identifier. */
3717 consider_directive_while_skipping (pfile
, stack
)
3719 IF_STACK_FRAME
*stack
;
3721 long ident_len
, ident
;
3722 struct directive
*kt
;
3723 IF_STACK_FRAME
*temp
;
3725 cpp_skip_hspace (pfile
);
3727 ident
= CPP_WRITTEN (pfile
);
3728 parse_name (pfile
, GETC());
3729 ident_len
= CPP_WRITTEN (pfile
) - ident
;
3731 CPP_SET_WRITTEN (pfile
, ident
);
3733 for (kt
= directive_table
; kt
->length
>= 0; kt
++)
3734 if (kt
->length
== ident_len
3735 && strncmp (pfile
->token_buffer
+ ident
, kt
->name
, kt
->length
) == 0)
3741 temp
= (IF_STACK_FRAME
*) xmalloc (sizeof (IF_STACK_FRAME
));
3742 temp
->next
= pfile
->if_stack
;
3743 pfile
->if_stack
= temp
;
3744 temp
->fname
= CPP_BUFFER(pfile
)->nominal_fname
;
3745 temp
->type
= kt
->type
;
3749 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
3750 validate_else (pfile
, "#else");
3753 if (pfile
->if_stack
->type
== T_ELSE
)
3754 cpp_error (pfile
, "`%s' after `#else'", kt
->name
);
3756 if (pfile
->if_stack
== stack
)
3760 pfile
->if_stack
->type
= kt
->type
;
3765 if (CPP_PEDANTIC (pfile
) && pfile
->if_stack
!= stack
)
3766 validate_else (pfile
, "#endif");
3768 if (pfile
->if_stack
== stack
)
3771 temp
= pfile
->if_stack
;
3772 pfile
->if_stack
= temp
->next
;
3780 /* Don't let erroneous code go by. */
3781 if (!CPP_OPTIONS (pfile
)->lang_asm
&& CPP_PEDANTIC (pfile
))
3782 cpp_pedwarn (pfile
, "invalid preprocessor directive name");
3786 /* skip to #endif, #else, or #elif. adjust line numbers, etc.
3787 * leaves input ptr at the sharp sign found.
3790 skip_if_group (pfile
)
3794 IF_STACK_FRAME
*save_if_stack
= pfile
->if_stack
; /* don't pop past here */
3795 U_CHAR
*beg_of_line
;
3798 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3800 CPP_PUTS (pfile
, "#failed\n", 8);
3802 output_line_command (pfile
, 1, same_file
);
3805 old_written
= CPP_WRITTEN (pfile
);
3809 beg_of_line
= CPP_BUFFER (pfile
)->cur
;
3811 if (! CPP_TRADITIONAL (pfile
))
3812 cpp_skip_hspace (pfile
);
3816 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3817 CPP_PUTC (pfile
, c
);
3822 if (consider_directive_while_skipping (pfile
, save_if_stack
))
3826 return; /* Caller will issue error. */
3829 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3831 CPP_PUTS (pfile
, beg_of_line
, CPP_BUFFER (pfile
)->cur
- beg_of_line
);
3832 copy_rest_of_line (pfile
);
3836 copy_rest_of_line (pfile
);
3837 CPP_SET_WRITTEN (pfile
, old_written
); /* discard it */
3842 return; /* Caller will issue error. */
3846 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3847 CPP_PUTC (pfile
, c
);
3851 /* Back up to the beginning of this line. Caller will process the
3853 CPP_BUFFER (pfile
)->cur
= beg_of_line
;
3854 pfile
->only_seen_white
= 1;
3855 if (CPP_OPTIONS (pfile
)->output_conditionals
)
3857 CPP_PUTS (pfile
, "#endfailed\n", 11);
3863 * handle a #else directive. Do this by just continuing processing
3864 * without changing if_stack ; this is so that the error message
3865 * for missing #endif's etc. will point to the original #if. It
3866 * is possible that something different would be better.
3870 do_else (pfile
, keyword
)
3872 struct directive
*keyword ATTRIBUTE_UNUSED
;
3874 cpp_buffer
*ip
= CPP_BUFFER (pfile
);
3876 if (CPP_PEDANTIC (pfile
))
3877 validate_else (pfile
, "#else");
3878 skip_rest_of_line (pfile
);
3880 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
) {
3881 cpp_error (pfile
, "`#else' not within a conditional");
3884 /* #ifndef can't have its special treatment for containing the whole file
3885 if it has a #else clause. */
3886 pfile
->if_stack
->control_macro
= 0;
3888 if (pfile
->if_stack
->type
!= T_IF
&& pfile
->if_stack
->type
!= T_ELIF
) {
3889 cpp_error (pfile
, "`#else' after `#else'");
3890 fprintf (stderr
, " (matches line %d", pfile
->if_stack
->lineno
);
3891 if (strcmp (pfile
->if_stack
->fname
, ip
->nominal_fname
) != 0)
3892 fprintf (stderr
, ", file %s", pfile
->if_stack
->fname
);
3893 fprintf (stderr
, ")\n");
3895 pfile
->if_stack
->type
= T_ELSE
;
3898 if (pfile
->if_stack
->if_succeeded
)
3899 skip_if_group (pfile
);
3901 ++pfile
->if_stack
->if_succeeded
; /* continue processing input */
3902 output_line_command (pfile
, 1, same_file
);
3908 * unstack after #endif command
3912 do_endif (pfile
, keyword
)
3914 struct directive
*keyword ATTRIBUTE_UNUSED
;
3916 if (CPP_PEDANTIC (pfile
))
3917 validate_else (pfile
, "#endif");
3918 skip_rest_of_line (pfile
);
3920 if (pfile
->if_stack
== CPP_BUFFER (pfile
)->if_stack
)
3921 cpp_error (pfile
, "unbalanced `#endif'");
3924 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
3925 pfile
->if_stack
= temp
->next
;
3926 if (temp
->control_macro
!= 0)
3928 /* This #endif matched a #ifndef at the start of the file.
3929 See if it is at the end of the file. */
3930 struct parse_marker start_mark
;
3933 parse_set_mark (&start_mark
, pfile
);
3937 cpp_skip_hspace (pfile
);
3942 parse_goto_mark (&start_mark
, pfile
);
3943 parse_clear_mark (&start_mark
);
3947 /* This #endif ends a #ifndef
3948 that contains all of the file (aside from whitespace).
3949 Arrange not to include the file again
3950 if the macro that was tested is defined. */
3951 struct cpp_buffer
*ip
;
3952 for (ip
= CPP_BUFFER (pfile
); ; ip
= CPP_PREV_BUFFER (ip
))
3953 if (ip
->fname
!= NULL
)
3955 ip
->ihash
->control_macro
= temp
->control_macro
;
3959 output_line_command (pfile
, 1, same_file
);
3964 /* When an #else or #endif is found while skipping failed conditional,
3965 if -pedantic was specified, this is called to warn about text after
3966 the command name. P points to the first char after the command name. */
3969 validate_else (pfile
, directive
)
3974 cpp_skip_hspace (pfile
);
3976 if (c
!= EOF
&& c
!= '\n')
3978 "text following `%s' violates ANSI standard", directive
);
3981 /* Get the next token, and add it to the text in pfile->token_buffer.
3982 Return the kind of token we got. */
3985 cpp_get_token (pfile
)
3988 register int c
, c2
, c3
;
3990 long start_line
, start_column
;
3991 enum cpp_token token
;
3992 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
3993 CPP_BUFFER (pfile
)->prev
= CPP_BUFFER (pfile
)->cur
;
3999 if (CPP_BUFFER (pfile
)->seen_eof
)
4001 if (cpp_pop_buffer (pfile
) != CPP_NULL_BUFFER (pfile
))
4008 cpp_buffer
*next_buf
4009 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4010 CPP_BUFFER (pfile
)->seen_eof
= 1;
4011 if (CPP_BUFFER (pfile
)->nominal_fname
4012 && next_buf
!= CPP_NULL_BUFFER (pfile
))
4014 /* We're about to return from an #include file.
4015 Emit #line information now (as part of the CPP_POP) result.
4016 But the #line refers to the file we will pop to. */
4017 cpp_buffer
*cur_buffer
= CPP_BUFFER (pfile
);
4018 CPP_BUFFER (pfile
) = next_buf
;
4019 pfile
->input_stack_listing_current
= 0;
4020 output_line_command (pfile
, 0, leave_file
);
4021 CPP_BUFFER (pfile
) = cur_buffer
;
4031 struct parse_marker start_mark
;
4033 if (PEEKC () == '=')
4035 if (opts
->put_out_comments
)
4036 parse_set_mark (&start_mark
, pfile
);
4038 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4039 &start_line
, &start_column
);
4040 c
= skip_comment (pfile
, &newlines
);
4041 if (opts
->put_out_comments
&& (c
== '/' || c
== EOF
))
4042 parse_clear_mark (&start_mark
);
4047 cpp_error_with_line (pfile
, start_line
, start_column
,
4048 "unterminated comment");
4051 c
= '/'; /* Initial letter of comment. */
4053 /* Comments are equivalent to spaces.
4054 For -traditional, a comment is equivalent to nothing. */
4055 if (opts
->put_out_comments
)
4057 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
4058 U_CHAR
*start
= pbuf
->buf
+ start_mark
.position
;
4059 int len
= pbuf
->cur
- start
;
4060 CPP_RESERVE(pfile
, 1 + len
);
4061 CPP_PUTC_Q (pfile
, c
);
4062 CPP_PUTS_Q (pfile
, start
, len
);
4063 pfile
->lineno
+= newlines
;
4064 parse_clear_mark (&start_mark
);
4067 else if (CPP_TRADITIONAL (pfile
))
4074 /* This may not work if cpp_get_token is called recursively,
4075 since many places look for horizontal space. */
4078 /* Copy the newlines into the output buffer, in order to
4079 avoid the pain of a #line every time a multiline comment
4081 CPP_RESERVE(pfile
, newlines
);
4082 while (--newlines
>= 0)
4084 CPP_PUTC_Q (pfile
, '\n');
4090 CPP_RESERVE(pfile
, 1);
4091 CPP_PUTC_Q (pfile
, ' ');
4095 if (opts
->for_lint
) {
4098 char *lintcmd
= get_lintcmd (ibp
, limit
, &argbp
, &arglen
, &cmdlen
);
4100 if (lintcmd
!= NULL
) {
4101 /* I believe it is always safe to emit this newline: */
4103 bcopy ("#pragma lint ", (char *) obp
, 13);
4105 bcopy (lintcmd
, (char *) obp
, cmdlen
);
4110 bcopy (argbp
, (char *) obp
, arglen
);
4114 /* OK, now bring us back to the state we were in before we entered
4115 this branch. We need #line because the newline for the pragma
4116 could mess things up. */
4117 output_line_command (pfile
, 0, same_file
);
4118 *(obp
++) = ' '; /* just in case, if comments are copied thru */
4126 /* If this is expanding a macro definition, don't recognize
4127 preprocessor directives. */
4130 /* If this is expand_into_temp_buffer, recognize them
4131 only after an actual newline at this level,
4132 not at the beginning of the input level. */
4133 if (ip
->fname
== 0 && beg_of_line
== ip
->buf
)
4139 if (!pfile
->only_seen_white
)
4141 if (handle_directive (pfile
))
4142 return CPP_DIRECTIVE
;
4143 pfile
->only_seen_white
= 0;
4148 /* A single quoted string is treated like a double -- some
4149 programs (e.g., troff) are perverse this way */
4150 cpp_buf_line_and_col (cpp_file_buffer (pfile
),
4151 &start_line
, &start_column
);
4152 old_written
= CPP_WRITTEN (pfile
);
4154 CPP_PUTC (pfile
, c
);
4160 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4162 /* try harder: this string crosses a macro expansion
4163 boundary. This can happen naturally if -traditional.
4164 Otherwise, only -D can make a macro with an unmatched
4166 cpp_buffer
*next_buf
4167 = CPP_PREV_BUFFER (CPP_BUFFER (pfile
));
4168 (*CPP_BUFFER (pfile
)->cleanup
)
4169 (CPP_BUFFER (pfile
), pfile
);
4170 CPP_BUFFER (pfile
) = next_buf
;
4173 if (!CPP_TRADITIONAL (pfile
))
4175 cpp_error_with_line (pfile
, start_line
, start_column
,
4176 "unterminated string or character constant");
4177 if (pfile
->multiline_string_line
!= start_line
4178 && pfile
->multiline_string_line
!= 0)
4179 cpp_error_with_line (pfile
,
4180 pfile
->multiline_string_line
, -1,
4181 "possible real start of unterminated constant");
4182 pfile
->multiline_string_line
= 0;
4186 CPP_PUTC (pfile
, cc
);
4190 /* Traditionally, end of line ends a string constant with
4191 no error. So exit the loop and record the new line. */
4192 if (CPP_TRADITIONAL (pfile
))
4196 cpp_error_with_line (pfile
, start_line
, start_column
,
4197 "unterminated character constant");
4200 if (CPP_PEDANTIC (pfile
)
4201 && pfile
->multiline_string_line
== 0)
4203 cpp_pedwarn_with_line (pfile
, start_line
, start_column
,
4204 "string constant runs past end of line");
4206 if (pfile
->multiline_string_line
== 0)
4207 pfile
->multiline_string_line
= start_line
;
4214 /* Backslash newline is replaced by nothing at all. */
4215 CPP_ADJUST_WRITTEN (pfile
, -1);
4220 /* ANSI stupidly requires that in \\ the second \
4221 is *not* prevented from combining with a newline. */
4224 CPP_PUTC (pfile
, cc
);
4236 pfile
->lineno
+= count_newlines (pfile
->token_buffer
+ old_written
,
4237 CPP_PWRITTEN (pfile
));
4238 pfile
->only_seen_white
= 0;
4239 return c
== '\'' ? CPP_CHAR
: CPP_STRING
;
4242 if (!opts
->dollars_in_ident
)
4247 if (opts
->cplusplus
&& PEEKC () == ':')
4256 if (c2
== c
|| c2
== '=')
4266 if (PEEKC () == '=')
4273 if (c2
== '-' && opts
->chill
)
4275 /* Chill style comment */
4276 if (opts
->put_out_comments
)
4277 parse_set_mark (&start_mark
, pfile
);
4278 FORWARD(1); /* Skip second '-'. */
4286 /* Don't consider final '\n' to be part of comment. */
4292 goto return_comment
;
4294 if (c2
== '-' || c2
== '=' || c2
== '>')
4299 if (pfile
->parsing_include_directive
)
4303 CPP_PUTC (pfile
, c
);
4308 if (c
== '\n' || c
== EOF
)
4311 "missing '>' in `#include <FILENAME>'");
4317 /* else fall through */
4326 CPP_RESERVE (pfile
, 4);
4327 CPP_PUTC (pfile
, c
);
4328 CPP_PUTC (pfile
, c2
);
4332 CPP_PUTC_Q (pfile
, GETC ());
4333 CPP_NUL_TERMINATE_Q (pfile
);
4334 pfile
->only_seen_white
= 0;
4338 if (CPP_BUFFER (pfile
)->has_escapes
)
4343 if (pfile
->output_escapes
)
4344 CPP_PUTS (pfile
, "@-", 2);
4345 parse_name (pfile
, GETC ());
4348 else if (is_space
[c
])
4350 CPP_RESERVE (pfile
, 2);
4351 if (pfile
->output_escapes
)
4352 CPP_PUTC_Q (pfile
, '@');
4353 CPP_PUTC_Q (pfile
, c
);
4357 if (pfile
->output_escapes
)
4359 CPP_PUTS (pfile
, "@@", 2);
4369 CPP_RESERVE(pfile
, 2);
4370 CPP_PUTC_Q (pfile
, '.');
4374 /* FIXME - misses the case "..\\\n." */
4375 if (c2
== '.' && PEEKN(1) == '.')
4377 CPP_RESERVE(pfile
, 4);
4378 CPP_PUTC_Q (pfile
, '.');
4379 CPP_PUTC_Q (pfile
, '.');
4380 CPP_PUTC_Q (pfile
, '.');
4382 CPP_NUL_TERMINATE_Q (pfile
);
4383 pfile
->only_seen_white
= 0;
4390 pfile
->only_seen_white
= 0;
4392 CPP_RESERVE(pfile
, 3);
4393 CPP_PUTC_Q (pfile
, c
);
4394 CPP_PUTC_Q (pfile
, GETC ());
4395 CPP_NUL_TERMINATE_Q (pfile
);
4401 if ((c2
== '\'' || c2
== '\"') && !CPP_TRADITIONAL (pfile
))
4403 CPP_PUTC (pfile
, c
);
4409 case '0': case '1': case '2': case '3': case '4':
4410 case '5': case '6': case '7': case '8': case '9':
4415 CPP_RESERVE (pfile
, 2);
4416 CPP_PUTC_Q (pfile
, c
);
4421 if (!is_idchar
[c
] && c
!= '.'
4422 && ((c2
!= 'e' && c2
!= 'E'
4423 && ((c2
!= 'p' && c2
!= 'P') || CPP_C89 (pfile
)))
4424 || (c
!= '+' && c
!= '-')))
4429 CPP_NUL_TERMINATE_Q (pfile
);
4430 pfile
->only_seen_white
= 0;
4432 case 'b': case 'c': case 'd': case 'h': case 'o':
4433 case 'B': case 'C': case 'D': case 'H': case 'O':
4434 if (opts
->chill
&& PEEKC () == '\'')
4436 pfile
->only_seen_white
= 0;
4437 CPP_RESERVE (pfile
, 2);
4438 CPP_PUTC_Q (pfile
, c
);
4439 CPP_PUTC_Q (pfile
, '\'');
4445 goto chill_number_eof
;
4448 if (c
== '\\' && PEEKC() == '\n')
4455 CPP_PUTC (pfile
, c
);
4459 CPP_RESERVE (pfile
, 2);
4460 CPP_PUTC_Q (pfile
, c
);
4461 CPP_NUL_TERMINATE_Q (pfile
);
4468 CPP_NUL_TERMINATE (pfile
);
4475 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4476 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4477 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4478 case 'x': case 'y': case 'z':
4479 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4480 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4481 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4486 unsigned char *ident
;
4487 int before_name_written
= CPP_WRITTEN (pfile
);
4489 parse_name (pfile
, c
);
4490 pfile
->only_seen_white
= 0;
4491 if (pfile
->no_macro_expand
)
4493 ident
= pfile
->token_buffer
+ before_name_written
;
4494 ident_len
= CPP_PWRITTEN (pfile
) - ident
;
4495 hp
= cpp_lookup (pfile
, ident
, ident_len
, -1);
4498 if (hp
->type
== T_DISABLED
)
4500 if (pfile
->output_escapes
)
4501 { /* Return "@-IDENT", followed by '\0'. */
4503 CPP_RESERVE (pfile
, 3);
4504 ident
= pfile
->token_buffer
+ before_name_written
;
4505 CPP_ADJUST_WRITTEN (pfile
, 2);
4506 for (i
= ident_len
; i
>= 0; i
--) ident
[i
+2] = ident
[i
];
4513 /* If macro wants an arglist, verify that a '(' follows.
4514 first skip all whitespace, copying it to the output
4515 after the macro name. Then, if there is no '(',
4516 decide this is not a macro call and leave things that way. */
4517 if (hp
->type
== T_MACRO
&& hp
->value
.defn
->nargs
>= 0)
4519 struct parse_marker macro_mark
;
4520 int is_macro_call
, macbuf_whitespace
= 0;
4522 parse_set_mark (¯o_mark
, pfile
);
4525 cpp_skip_hspace (pfile
);
4527 is_macro_call
= c
== '(';
4536 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile
)))
4538 if (macro_mark
.position
!=
4539 (CPP_BUFFER (pfile
)->cur
4540 - CPP_BUFFER (pfile
)->buf
))
4541 macbuf_whitespace
= 1;
4543 parse_clear_mark (¯o_mark
);
4544 cpp_pop_buffer (pfile
);
4545 parse_set_mark (¯o_mark
, pfile
);
4553 parse_goto_mark (¯o_mark
, pfile
);
4554 if (macbuf_whitespace
)
4555 CPP_PUTC (pfile
, ' ');
4557 parse_clear_mark (¯o_mark
);
4561 /* This is now known to be a macro call. */
4563 /* it might not actually be a macro. */
4564 if (hp
->type
!= T_MACRO
) {
4565 int xbuf_len
; U_CHAR
*xbuf
;
4566 CPP_SET_WRITTEN (pfile
, before_name_written
);
4567 special_symbol (hp
, pfile
);
4568 xbuf_len
= CPP_WRITTEN (pfile
) - before_name_written
;
4569 xbuf
= (U_CHAR
*) xmalloc (xbuf_len
+ 1);
4570 CPP_SET_WRITTEN (pfile
, before_name_written
);
4571 bcopy (CPP_PWRITTEN (pfile
), xbuf
, xbuf_len
+ 1);
4572 push_macro_expansion (pfile
, xbuf
, xbuf_len
, hp
);
4576 /* Expand the macro, reading arguments as needed,
4577 and push the expansion on the input stack. */
4578 macroexpand (pfile
, hp
);
4579 CPP_SET_WRITTEN (pfile
, before_name_written
);
4582 /* An extra "@ " is added to the end of a macro expansion
4583 to prevent accidental token pasting. We prefer to avoid
4584 unneeded extra spaces (for the sake of cpp-using tools like
4585 imake). Here we remove the space if it is safe to do so. */
4586 if (pfile
->buffer
->rlimit
- pfile
->buffer
->cur
>= 3
4587 && pfile
->buffer
->rlimit
[-2] == '@'
4588 && pfile
->buffer
->rlimit
[-1] == ' ')
4590 int c1
= pfile
->buffer
->rlimit
[-3];
4591 int c2
= CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)));
4592 if (c2
== EOF
|| ! unsafe_chars (c1
, c2
))
4593 pfile
->buffer
->rlimit
-= 2;
4598 case ' ': case '\t': case '\v': case '\r':
4601 CPP_PUTC (pfile
, c
);
4603 if (c
== EOF
|| !is_hor_space
[c
])
4617 CPP_PUTC (pfile
, c
);
4618 if (pfile
->only_seen_white
== 0)
4619 pfile
->only_seen_white
= 1;
4621 output_line_command (pfile
, 1, same_file
);
4624 case '(': token
= CPP_LPAREN
; goto char1
;
4625 case ')': token
= CPP_RPAREN
; goto char1
;
4626 case '{': token
= CPP_LBRACE
; goto char1
;
4627 case '}': token
= CPP_RBRACE
; goto char1
;
4628 case ',': token
= CPP_COMMA
; goto char1
;
4629 case ';': token
= CPP_SEMICOLON
; goto char1
;
4635 pfile
->only_seen_white
= 0;
4636 CPP_PUTC (pfile
, c
);
4642 /* Like cpp_get_token, but skip spaces and comments. */
4645 cpp_get_non_space_token (pfile
)
4648 int old_written
= CPP_WRITTEN (pfile
);
4651 enum cpp_token token
= cpp_get_token (pfile
);
4652 if (token
!= CPP_COMMENT
&& token
!= CPP_POP
4653 && token
!= CPP_HSPACE
&& token
!= CPP_VSPACE
)
4655 CPP_SET_WRITTEN (pfile
, old_written
);
4659 /* Parse an identifier starting with C. */
4662 parse_name (pfile
, c
)
4663 cpp_reader
*pfile
; int c
;
4669 if (c
== '\\' && PEEKC() == '\n')
4678 if (c
== '$' && CPP_PEDANTIC (pfile
))
4679 cpp_pedwarn (pfile
, "`$' in identifier");
4681 CPP_RESERVE(pfile
, 2); /* One more for final NUL. */
4682 CPP_PUTC_Q (pfile
, c
);
4687 CPP_NUL_TERMINATE_Q (pfile
);
4691 /* This is called after options have been processed.
4692 * Check options for consistency, and setup for processing input
4693 * from the file named FNAME. (Use standard input if FNAME==NULL.)
4694 * Return 1 on success, 0 on failure.
4698 cpp_start_read (pfile
, fname
)
4702 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
4703 struct cpp_pending
*pend
;
4707 struct include_hash
*ih_fake
;
4709 /* The code looks at the defaults through this pointer, rather than through
4710 the constant structure above. This pointer gets changed if an environment
4711 variable specifies other defaults. */
4712 struct default_include
*include_defaults
= include_defaults_array
;
4714 /* Now that we know dollars_in_ident for real,
4715 reset is_idchar/is_idstart. */
4716 is_idchar
['$'] = opts
->dollars_in_ident
;
4717 is_idstart
['$'] = opts
->dollars_in_ident
;
4719 /* Add dirs from CPATH after dirs from -I. */
4720 /* There seems to be confusion about what CPATH should do,
4721 so for the moment it is not documented. */
4722 /* Some people say that CPATH should replace the standard include dirs,
4723 but that seems pointless: it comes before them, so it overrides them
4725 GET_ENV_PATH_LIST (p
, "CPATH");
4726 if (p
!= 0 && ! opts
->no_standard_includes
)
4727 path_include (pfile
, p
);
4729 /* Do partial setup of input buffer for the sake of generating
4730 early #line directives (when -g is in effect). */
4731 fp
= cpp_push_buffer (pfile
, NULL
, 0);
4734 if (opts
->in_fname
== NULL
|| *opts
->in_fname
== 0)
4736 opts
->in_fname
= fname
;
4737 if (opts
->in_fname
== NULL
)
4738 opts
->in_fname
= "";
4740 fp
->nominal_fname
= fp
->fname
= opts
->in_fname
;
4743 /* Install __LINE__, etc. Must follow initialize_char_syntax
4744 and option processing. */
4745 initialize_builtins (pfile
);
4747 /* Do standard #defines and assertions
4748 that identify system and machine type. */
4750 if (!opts
->inhibit_predefs
) {
4751 char *p
= (char *) alloca (strlen (predefs
) + 1);
4752 strcpy (p
, predefs
);
4755 while (*p
== ' ' || *p
== '\t')
4757 /* Handle -D options. */
4758 if (p
[0] == '-' && p
[1] == 'D') {
4760 while (*p
&& *p
!= ' ' && *p
!= '\t')
4764 if (opts
->debug_output
)
4765 output_line_command (pfile
, 0, same_file
);
4766 cpp_define (pfile
, q
);
4767 while (*p
== ' ' || *p
== '\t')
4769 } else if (p
[0] == '-' && p
[1] == 'A') {
4770 /* Handle -A options (assertions). */
4779 past_name
= assertion
;
4780 /* Locate end of name. */
4781 while (*past_name
&& *past_name
!= ' '
4782 && *past_name
!= '\t' && *past_name
!= '(')
4784 /* Locate `(' at start of value. */
4786 while (*value
&& (*value
== ' ' || *value
== '\t'))
4788 if (*value
++ != '(')
4790 while (*value
&& (*value
== ' ' || *value
== '\t'))
4793 /* Locate end of value. */
4794 while (*past_value
&& *past_value
!= ' '
4795 && *past_value
!= '\t' && *past_value
!= ')')
4797 termination
= past_value
;
4798 while (*termination
&& (*termination
== ' ' || *termination
== '\t'))
4800 if (*termination
++ != ')')
4802 if (*termination
&& *termination
!= ' ' && *termination
!= '\t')
4804 /* Temporarily null-terminate the value. */
4805 save_char
= *termination
;
4806 *termination
= '\0';
4807 /* Install the assertion. */
4808 make_assertion (pfile
, "-A", assertion
);
4809 *termination
= (char) save_char
;
4811 while (*p
== ' ' || *p
== '\t')
4819 /* Now handle the command line options. */
4821 /* Do -U's, -D's and -A's in the order they were seen. */
4822 /* First reverse the list. */
4823 opts
->pending
= nreverse_pending (opts
->pending
);
4825 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
4827 if (pend
->cmd
!= NULL
&& pend
->cmd
[0] == '-')
4829 switch (pend
->cmd
[1])
4832 if (opts
->debug_output
)
4833 output_line_command (pfile
, 0, same_file
);
4834 cpp_undef (pfile
, pend
->arg
);
4837 if (opts
->debug_output
)
4838 output_line_command (pfile
, 0, same_file
);
4839 cpp_define (pfile
, pend
->arg
);
4842 make_assertion (pfile
, "-A", pend
->arg
);
4848 opts
->done_initializing
= 1;
4850 { /* Read the appropriate environment variable and if it exists
4851 replace include_defaults with the listed path. */
4853 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
4856 GET_ENV_PATH_LIST (epath
, "C_INCLUDE_PATH");
4859 GET_ENV_PATH_LIST (epath
, "CPLUS_INCLUDE_PATH");
4862 GET_ENV_PATH_LIST (epath
, "OBJC_INCLUDE_PATH");
4865 GET_ENV_PATH_LIST (epath
, "OBJCPLUS_INCLUDE_PATH");
4868 /* If the environment var for this language is set,
4869 add to the default list of include directories. */
4871 char *nstore
= (char *) alloca (strlen (epath
) + 2);
4873 char *startp
, *endp
;
4875 for (num_dirs
= 1, startp
= epath
; *startp
; startp
++)
4876 if (*startp
== PATH_SEPARATOR
)
4879 = (struct default_include
*) xmalloc ((num_dirs
4880 * sizeof (struct default_include
))
4881 + sizeof (include_defaults_array
));
4882 startp
= endp
= epath
;
4885 /* Handle cases like c:/usr/lib:d:/gcc/lib */
4886 if ((*endp
== PATH_SEPARATOR
)
4888 strncpy (nstore
, startp
, endp
-startp
);
4890 strcpy (nstore
, ".");
4892 nstore
[endp
-startp
] = '\0';
4894 include_defaults
[num_dirs
].fname
= xstrdup (nstore
);
4895 include_defaults
[num_dirs
].component
= 0;
4896 include_defaults
[num_dirs
].cplusplus
= opts
->cplusplus
;
4897 include_defaults
[num_dirs
].cxx_aware
= 1;
4901 endp
= startp
= endp
+ 1;
4905 /* Put the usual defaults back in at the end. */
4906 bcopy ((char *) include_defaults_array
,
4907 (char *) &include_defaults
[num_dirs
],
4908 sizeof (include_defaults_array
));
4912 /* Unless -fnostdinc,
4913 tack on the standard include file dirs to the specified list */
4914 if (!opts
->no_standard_includes
) {
4915 struct default_include
*p
= include_defaults
;
4916 char *specd_prefix
= opts
->include_prefix
;
4917 char *default_prefix
= xstrdup (GCC_INCLUDE_DIR
);
4918 int default_len
= 0;
4919 /* Remove the `include' from /usr/local/lib/gcc.../include. */
4920 if (!strcmp (default_prefix
+ strlen (default_prefix
) - 8, "/include")) {
4921 default_len
= strlen (default_prefix
) - 7;
4922 default_prefix
[default_len
] = 0;
4924 /* Search "translated" versions of GNU directories.
4925 These have /usr/local/lib/gcc... replaced by specd_prefix. */
4926 if (specd_prefix
!= 0 && default_len
!= 0)
4927 for (p
= include_defaults
; p
->fname
; p
++) {
4928 /* Some standard dirs are only for C++. */
4930 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
4931 /* Does this dir start with the prefix? */
4932 if (!strncmp (p
->fname
, default_prefix
, default_len
)) {
4933 /* Yes; change prefix and add to search list. */
4934 int this_len
= strlen (specd_prefix
)
4935 + strlen (p
->fname
) - default_len
;
4936 char *str
= (char *) xmalloc (this_len
+ 1);
4937 strcpy (str
, specd_prefix
);
4938 strcat (str
, p
->fname
+ default_len
);
4940 append_include_chain (pfile
, &opts
->system_include
,
4941 str
, !p
->cxx_aware
);
4945 /* Search ordinary names for GNU include directories. */
4946 for (p
= include_defaults
; p
->fname
; p
++) {
4947 /* Some standard dirs are only for C++. */
4949 || (opts
->cplusplus
&& !opts
->no_standard_cplusplus_includes
)) {
4950 const char *str
= update_path (p
->fname
, p
->component
);
4951 append_include_chain (pfile
, &opts
->system_include
,
4952 str
, !p
->cxx_aware
);
4957 merge_include_chains (opts
);
4959 /* With -v, print the list of dirs to search. */
4960 if (opts
->verbose
) {
4961 struct file_name_list
*p
;
4962 fprintf (stderr
, "#include \"...\" search starts here:\n");
4963 for (p
= opts
->quote_include
; p
; p
= p
->next
) {
4964 if (p
== opts
->bracket_include
)
4965 fprintf (stderr
, "#include <...> search starts here:\n");
4966 fprintf (stderr
, " %s\n", p
->name
);
4968 fprintf (stderr
, "End of search list.\n");
4971 /* Copy the entire contents of the main input file into
4972 the stacked input buffer previously allocated for it. */
4973 if (fname
== NULL
|| *fname
== 0) {
4976 } else if ((f
= open (fname
, O_RDONLY
, 0666)) < 0)
4977 cpp_pfatal_with_name (pfile
, fname
);
4979 /* -MG doesn't select the form of output and must be specified with one of
4980 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
4981 inhibit compilation. */
4982 if (opts
->print_deps_missing_files
4983 && (opts
->print_deps
== 0 || !opts
->no_output
))
4985 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
4989 /* Either of two environment variables can specify output of deps.
4990 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
4991 where OUTPUT_FILE is the file to write deps info to
4992 and DEPS_TARGET is the target to mention in the deps. */
4994 if (opts
->print_deps
== 0
4995 && (getenv ("SUNPRO_DEPENDENCIES") != 0
4996 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
4997 char *spec
= getenv ("DEPENDENCIES_OUTPUT");
5003 spec
= getenv ("SUNPRO_DEPENDENCIES");
5004 opts
->print_deps
= 2;
5007 opts
->print_deps
= 1;
5010 /* Find the space before the DEPS_TARGET, if there is one. */
5011 /* This should use index. (mrs) */
5012 while (*s
!= 0 && *s
!= ' ') s
++;
5015 opts
->deps_target
= s
+ 1;
5016 output_file
= (char *) xmalloc (s
- spec
+ 1);
5017 bcopy (spec
, output_file
, s
- spec
);
5018 output_file
[s
- spec
] = 0;
5022 opts
->deps_target
= 0;
5026 opts
->deps_file
= output_file
;
5027 opts
->print_deps_append
= 1;
5030 /* For -M, print the expected object file name
5031 as the target of this Make-rule. */
5032 if (opts
->print_deps
)
5034 pfile
->deps_allocated_size
= 200;
5035 pfile
->deps_buffer
= (char *) xmalloc (pfile
->deps_allocated_size
);
5036 pfile
->deps_buffer
[0] = 0;
5037 pfile
->deps_size
= 0;
5038 pfile
->deps_column
= 0;
5040 if (opts
->deps_target
)
5041 deps_output (pfile
, opts
->deps_target
, ':');
5042 else if (*opts
->in_fname
== 0)
5043 deps_output (pfile
, "-", ':');
5048 static char *known_suffixes
[] = { ".c", ".C", ".s", ".S", ".m",
5049 ".cc", ".cxx", ".cpp", ".cp",
5053 /* Discard all directory prefixes from filename. */
5054 if ((q
= rindex (opts
->in_fname
, '/')) != NULL
5055 #ifdef DIR_SEPARATOR
5056 && (q
= rindex (opts
->in_fname
, DIR_SEPARATOR
)) != NULL
5063 /* Copy remainder to mungable area. */
5064 p
= (char *) alloca (strlen(q
) + 8);
5067 /* Output P, but remove known suffixes. */
5070 /* Point to the filename suffix. */
5071 r
= rindex (p
, '.');
5072 /* Compare against the known suffixes. */
5074 while (known_suffixes
[x
] != 0)
5076 if (strncmp (known_suffixes
[x
], r
, q
- r
) == 0)
5078 /* Make q point to the bit we're going to overwrite
5079 with an object suffix. */
5086 /* Supply our own suffix. */
5093 deps_output (pfile
, p
, ':');
5094 deps_output (pfile
, opts
->in_fname
, ' ');
5099 /* Make sure data ends with a newline. And put a null after it. */
5101 if ((fp
->length
> 0 && fp
->buf
[fp
->length
- 1] != '\n')
5102 /* Backslash-newline at end is not good enough. */
5103 || (fp
->length
> 1 && fp
->buf
[fp
->length
- 2] == '\\')) {
5104 fp
->buf
[fp
->length
++] = '\n';
5105 missing_newline
= 1;
5107 fp
->buf
[fp
->length
] = '\0';
5109 /* Unless inhibited, convert trigraphs in the input. */
5115 /* Must call finclude() on the main input before processing
5116 -include switches; otherwise the -included text winds up
5117 after the main input. */
5118 ih_fake
= (struct include_hash
*) xmalloc (sizeof (struct include_hash
));
5120 ih_fake
->next_this_file
= 0;
5121 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
5122 ih_fake
->name
= fname
;
5123 ih_fake
->control_macro
= 0;
5124 ih_fake
->buf
= (char *)-1;
5126 if (!finclude (pfile
, f
, ih_fake
))
5128 output_line_command (pfile
, 0, same_file
);
5129 pfile
->only_seen_white
= 2;
5131 /* The -imacros files can be scanned now, but the -include files
5132 have to be pushed onto the include stack and processed later,
5133 in the main loop calling cpp_get_token. That means the -include
5134 files have to be processed in reverse order of the pending list,
5135 which means the pending list has to be reversed again, which
5136 means the -imacros files have to be done separately and first. */
5138 pfile
->no_record_file
++;
5140 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5142 if (pend
->cmd
!= NULL
)
5144 if (strcmp (pend
->cmd
, "-imacros") == 0)
5146 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5149 cpp_perror_with_name (pfile
, pend
->arg
);
5152 if (!cpp_push_buffer (pfile
, NULL
, 0))
5155 ih_fake
= (struct include_hash
*)
5156 xmalloc (sizeof (struct include_hash
));
5158 ih_fake
->next_this_file
= 0;
5159 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
5160 ih_fake
->name
= pend
->arg
;
5161 ih_fake
->control_macro
= 0;
5162 ih_fake
->buf
= (char *)-1;
5164 if (!finclude (pfile
, fd
, ih_fake
))
5165 cpp_scan_buffer (pfile
);
5171 opts
->pending
= nreverse_pending (opts
->pending
);
5172 for (pend
= opts
->pending
; pend
; pend
= pend
->next
)
5174 if (pend
->cmd
!= NULL
)
5176 if (strcmp (pend
->cmd
, "-include") == 0)
5178 int fd
= open (pend
->arg
, O_RDONLY
, 0666);
5181 cpp_perror_with_name (pfile
, pend
->arg
);
5184 if (!cpp_push_buffer (pfile
, NULL
, 0))
5187 ih_fake
= (struct include_hash
*)
5188 xmalloc (sizeof (struct include_hash
));
5190 ih_fake
->next_this_file
= 0;
5191 ih_fake
->foundhere
= ABSOLUTE_PATH
; /* well sort of ... */
5192 ih_fake
->name
= pend
->arg
;
5193 ih_fake
->control_macro
= 0;
5194 ih_fake
->buf
= (char *)-1;
5196 if (finclude (pfile
, fd
, ih_fake
))
5197 output_line_command (pfile
, 0, enter_file
);
5201 pfile
->no_record_file
--;
5203 /* Free the pending list. */
5204 for (pend
= opts
->pending
; pend
; )
5206 struct cpp_pending
*next
= pend
->next
;
5210 opts
->pending
= NULL
;
5216 cpp_reader_init (pfile
)
5219 bzero ((char *) pfile
, sizeof (cpp_reader
));
5220 pfile
->get_token
= cpp_get_token
;
5222 pfile
->token_buffer_size
= 200;
5223 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
5224 CPP_SET_WRITTEN (pfile
, 0);
5226 pfile
->timebuf
= NULL
;
5227 pfile
->only_seen_white
= 1;
5228 pfile
->buffer
= CPP_NULL_BUFFER(pfile
);
5229 pfile
->actual_dirs
= NULL
;
5232 static struct cpp_pending
*
5233 nreverse_pending (list
)
5234 struct cpp_pending
*list
;
5237 register struct cpp_pending
*prev
= 0, *next
, *pend
;
5238 for (pend
= list
; pend
; pend
= next
)
5248 push_pending (pfile
, cmd
, arg
)
5253 struct cpp_pending
*pend
5254 = (struct cpp_pending
*) xmalloc (sizeof (struct cpp_pending
));
5257 pend
->next
= CPP_OPTIONS (pfile
)->pending
;
5258 CPP_OPTIONS (pfile
)->pending
= pend
;
5265 printf ("Usage: %s [switches] input output\n", progname
);
5266 printf ("Switches:\n");
5267 printf (" -include <file> Include the contents of <file> before other files\n");
5268 printf (" -imacros <file> Accept definition of marcos in <file>\n");
5269 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
5270 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
5271 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
5272 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
5273 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
5274 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
5275 printf (" -nostdinc Do not search the system include directories\n");
5276 printf (" -nostdinc++ Do not search the system include directories for C++\n");
5277 printf (" -o <file> Put output into <file>\n");
5278 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
5279 printf (" -traditional Follow K&R pre-processor behaviour\n");
5280 printf (" -trigraphs Support ANSI C trigraphs\n");
5281 printf (" -lang-c Assume that the input sources are in C\n");
5282 printf (" -lang-c89 Assume that the input sources are in C89\n");
5283 printf (" -lang-c++ Assume that the input sources are in C++\n");
5284 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
5285 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
5286 printf (" -lang-asm Assume that the input sources are in assembler\n");
5287 printf (" -lang-chill Assume that the input sources are in Chill\n");
5288 printf (" -+ Allow parsing of C++ style features\n");
5289 printf (" -w Inhibit warning messages\n");
5290 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
5291 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
5292 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
5293 printf (" -Wno-comment{s} Do not warn about comments\n");
5294 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
5295 printf (" a string if -traditional is specified\n");
5296 printf (" -Wno-traditional Do not warn about stringification\n");
5297 printf (" -Wundef Warn if an undefined macro is used by #if\n");
5298 printf (" -Wno-undef Do not warn about testing undefined macros\n");
5299 printf (" -Wimport Warn about the use of the #import directive\n");
5300 printf (" -Wno-import Do not warn about the use of #import\n");
5301 printf (" -Werror Treat all warnings as errors\n");
5302 printf (" -Wno-error Do not treat warnings as errors\n");
5303 printf (" -Wall Enable all preprocessor warnings\n");
5304 printf (" -M Generate make dependencies\n");
5305 printf (" -MM As -M, but ignore system header files\n");
5306 printf (" -MD As -M, but put output in a .d file\n");
5307 printf (" -MMD As -MD, but ignore system header files\n");
5308 printf (" -MG Treat missing header file as generated files\n");
5309 printf (" -g Include #define and #undef directives in the output\n");
5310 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
5311 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
5312 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
5313 printf (" -U<macro> Undefine <macro> \n");
5314 printf (" -u or -undef Do not predefine any macros\n");
5315 printf (" -v Display the version number\n");
5316 printf (" -H Print the name of header files as they are used\n");
5317 printf (" -C Do not discard comments\n");
5318 printf (" -dM Display a list of macro definitions active at end\n");
5319 printf (" -dD Preserve macro definitions in output\n");
5320 printf (" -dN As -dD except that only the names are preserved\n");
5321 printf (" -dI Include #include directives in the output\n");
5322 printf (" -ifoutput Describe skipped code blocks in output \n");
5323 printf (" -P Do not generate #line directives\n");
5324 printf (" -$ Do not include '$' in identifiers\n");
5325 printf (" -remap Remap file names when including files.\n");
5326 printf (" -h or --help Display this information\n");
5330 /* Handle one command-line option in (argc, argv).
5331 Can be called multiple times, to handle multiple sets of options.
5332 Returns number of strings consumed. */
5334 cpp_handle_option (pfile
, argc
, argv
)
5339 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5342 if (user_label_prefix
== NULL
)
5343 user_label_prefix
= USER_LABEL_PREFIX
;
5345 if (argv
[i
][0] != '-') {
5346 if (opts
->out_fname
!= NULL
)
5349 cpp_fatal (pfile
, "Too many arguments");
5351 else if (opts
->in_fname
!= NULL
)
5352 opts
->out_fname
= argv
[i
];
5354 opts
->in_fname
= argv
[i
];
5356 switch (argv
[i
][1]) {
5359 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
5362 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
5366 if (!strcmp (argv
[i
], "-fleading-underscore"))
5367 user_label_prefix
= "_";
5368 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
5369 user_label_prefix
= "";
5372 case 'I': /* Add directory to path for includes. */
5373 if (!strcmp (argv
[i
] + 2, "-"))
5375 if (! opts
->ignore_srcdir
)
5377 opts
->ignore_srcdir
= 1;
5378 /* Don't use any preceding -I directories for #include <...>. */
5379 opts
->quote_include
= opts
->bracket_include
;
5380 opts
->bracket_include
= 0;
5386 if (argv
[i
][2] != 0)
5387 fname
= argv
[i
] + 2;
5388 else if (i
+ 1 == argc
)
5389 goto missing_dirname
;
5392 append_include_chain (pfile
, &opts
->bracket_include
, fname
, 0);
5397 /* Add directory to beginning of system include path, as a system
5398 include directory. */
5399 if (!strcmp (argv
[i
], "-isystem"))
5402 goto missing_filename
;
5403 append_include_chain (pfile
, &opts
->system_include
, argv
[++i
], 1);
5405 /* Add directory to end of path for includes,
5406 with the default prefix at the front of its name. */
5407 else if (!strcmp (argv
[i
], "-iwithprefix"))
5411 goto missing_dirname
;
5414 if (opts
->include_prefix
!= 0)
5416 fname
= xmalloc (strlen (opts
->include_prefix
)
5417 + strlen (argv
[i
]) + 1);
5418 strcpy (fname
, opts
->include_prefix
);
5419 strcat (fname
, argv
[i
]);
5423 fname
= xmalloc (strlen (GCC_INCLUDE_DIR
)
5424 + strlen (argv
[i
]) + 1);
5425 strcpy (fname
, GCC_INCLUDE_DIR
);
5426 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5427 if (!strcmp (fname
+ strlen (fname
) - 8, "/include"))
5428 fname
[strlen (fname
) - 7] = 0;
5429 strcat (fname
, argv
[i
]);
5432 append_include_chain (pfile
, &opts
->system_include
, fname
, 0);
5434 /* Add directory to main path for includes,
5435 with the default prefix at the front of its name. */
5436 else if (!strcmp (argv
[i
], "-iwithprefix"))
5440 goto missing_dirname
;
5443 if (opts
->include_prefix
!= 0)
5445 fname
= xmalloc (strlen (opts
->include_prefix
)
5446 + strlen (argv
[i
]) + 1);
5447 strcpy (fname
, opts
->include_prefix
);
5448 strcat (fname
, argv
[i
]);
5452 fname
= xmalloc (strlen (GCC_INCLUDE_DIR
)
5453 + strlen (argv
[i
]) + 1);
5454 strcpy (fname
, GCC_INCLUDE_DIR
);
5455 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5456 if (!strcmp (fname
+ strlen (fname
) - 8, "/include"))
5457 fname
[strlen (fname
) - 7] = 0;
5458 strcat (fname
, argv
[i
]);
5461 append_include_chain (pfile
, &opts
->bracket_include
, fname
, 0);
5463 /* Add directory to end of path for includes. */
5464 else if (!strcmp (argv
[i
], "-idirafter"))
5467 goto missing_dirname
;
5468 append_include_chain (pfile
, &opts
->after_include
, argv
[++i
], 0);
5470 else if (!strcmp (argv
[i
], "-include") || !strcmp (argv
[i
], "-imacros"))
5473 goto missing_filename
;
5475 push_pending (pfile
, argv
[i
], argv
[i
+1]), i
++;
5477 else if (!strcmp (argv
[i
], "-iprefix"))
5480 goto missing_filename
;
5482 opts
->include_prefix
= argv
[++i
];
5484 else if (!strcmp (argv
[i
], "-ifoutput"))
5485 opts
->output_conditionals
= 1;
5490 if (opts
->out_fname
!= NULL
)
5492 cpp_fatal (pfile
, "Output filename specified twice");
5496 goto missing_filename
;
5497 opts
->out_fname
= argv
[++i
];
5498 if (!strcmp (opts
->out_fname
, "-"))
5499 opts
->out_fname
= "";
5503 if (!strcmp (argv
[i
], "-pedantic"))
5504 CPP_PEDANTIC (pfile
) = 1;
5505 else if (!strcmp (argv
[i
], "-pedantic-errors")) {
5506 CPP_PEDANTIC (pfile
) = 1;
5507 opts
->pedantic_errors
= 1;
5510 else if (!strcmp (argv
[i
], "-pcp")) {
5511 char *pcp_fname
= argv
[++i
];
5512 pcp_outfile
= ((pcp_fname
[0] != '-' || pcp_fname
[1] != '\0')
5513 ? fopen (pcp_fname
, "w")
5514 : fdopen (dup (fileno (stdout
)), "w"));
5515 if (pcp_outfile
== 0)
5516 cpp_pfatal_with_name (pfile
, pcp_fname
);
5523 if (!strcmp (argv
[i
], "-traditional")) {
5524 opts
->traditional
= 1;
5525 opts
->cplusplus_comments
= 0;
5526 } else if (!strcmp (argv
[i
], "-trigraphs")) {
5528 opts
->no_trigraphs
= 0;
5533 if (! strcmp (argv
[i
], "-lang-c"))
5534 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5536 if (! strcmp (argv
[i
], "-lang-c89"))
5537 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0, opts
->c89
= 1,
5539 if (! strcmp (argv
[i
], "-lang-c++"))
5540 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5542 if (! strcmp (argv
[i
], "-lang-objc"))
5543 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5545 if (! strcmp (argv
[i
], "-lang-objc++"))
5546 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
5548 if (! strcmp (argv
[i
], "-lang-asm"))
5550 if (! strcmp (argv
[i
], "-lint"))
5552 if (! strcmp (argv
[i
], "-lang-chill"))
5553 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
5554 opts
->traditional
= 1, opts
->no_trigraphs
= 1;
5558 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
5562 opts
->inhibit_warnings
= 1;
5566 if (!strcmp (argv
[i
], "-Wtrigraphs"))
5567 opts
->warn_trigraphs
= 1;
5568 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
5569 opts
->warn_trigraphs
= 0;
5570 else if (!strcmp (argv
[i
], "-Wcomment"))
5571 opts
->warn_comments
= 1;
5572 else if (!strcmp (argv
[i
], "-Wno-comment"))
5573 opts
->warn_comments
= 0;
5574 else if (!strcmp (argv
[i
], "-Wcomments"))
5575 opts
->warn_comments
= 1;
5576 else if (!strcmp (argv
[i
], "-Wno-comments"))
5577 opts
->warn_comments
= 0;
5578 else if (!strcmp (argv
[i
], "-Wtraditional"))
5579 opts
->warn_stringify
= 1;
5580 else if (!strcmp (argv
[i
], "-Wno-traditional"))
5581 opts
->warn_stringify
= 0;
5582 else if (!strcmp (argv
[i
], "-Wundef"))
5583 opts
->warn_undef
= 1;
5584 else if (!strcmp (argv
[i
], "-Wno-undef"))
5585 opts
->warn_undef
= 0;
5586 else if (!strcmp (argv
[i
], "-Wimport"))
5587 opts
->warn_import
= 1;
5588 else if (!strcmp (argv
[i
], "-Wno-import"))
5589 opts
->warn_import
= 0;
5590 else if (!strcmp (argv
[i
], "-Werror"))
5591 opts
->warnings_are_errors
= 1;
5592 else if (!strcmp (argv
[i
], "-Wno-error"))
5593 opts
->warnings_are_errors
= 0;
5594 else if (!strcmp (argv
[i
], "-Wall"))
5596 opts
->warn_trigraphs
= 1;
5597 opts
->warn_comments
= 1;
5602 /* The style of the choices here is a bit mixed.
5603 The chosen scheme is a hybrid of keeping all options in one string
5604 and specifying each option in a separate argument:
5605 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
5606 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5607 -M[M][G][D file]. This is awkward to handle in specs, and is not
5609 /* ??? -MG must be specified in addition to one of -M or -MM.
5610 This can be relaxed in the future without breaking anything.
5611 The converse isn't true. */
5613 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
5614 if (!strcmp (argv
[i
], "-MG"))
5616 opts
->print_deps_missing_files
= 1;
5619 if (!strcmp (argv
[i
], "-M"))
5620 opts
->print_deps
= 2;
5621 else if (!strcmp (argv
[i
], "-MM"))
5622 opts
->print_deps
= 1;
5623 else if (!strcmp (argv
[i
], "-MD"))
5624 opts
->print_deps
= 2;
5625 else if (!strcmp (argv
[i
], "-MMD"))
5626 opts
->print_deps
= 1;
5627 /* For -MD and -MMD options, write deps on file named by next arg. */
5628 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
5631 goto missing_filename
;
5632 opts
->deps_file
= argv
[++i
];
5636 /* For -M and -MM, write deps on standard output
5637 and suppress the usual output. */
5638 opts
->no_output
= 1;
5644 char *p
= argv
[i
] + 2;
5646 while ((c
= *p
++) != 0) {
5647 /* Arg to -d specifies what parts of macros to dump */
5650 opts
->dump_macros
= dump_only
;
5651 opts
->no_output
= 1;
5654 opts
->dump_macros
= dump_names
;
5657 opts
->dump_macros
= dump_definitions
;
5660 opts
->dump_includes
= 1;
5668 if (argv
[i
][2] == '3')
5669 opts
->debug_output
= 1;
5673 if (strcmp (argv
[i
], "--help") != 0)
5679 fprintf (stderr
, "GNU CPP version %s", version_string
);
5680 #ifdef TARGET_VERSION
5683 fprintf (stderr
, "\n");
5688 opts
->print_include_names
= 1;
5692 if (argv
[i
][2] != 0)
5693 push_pending (pfile
, "-D", argv
[i
] + 2);
5694 else if (i
+ 1 == argc
)
5696 cpp_fatal (pfile
, "Macro name missing after -D option");
5700 i
++, push_pending (pfile
, "-D", argv
[i
]);
5707 if (argv
[i
][2] != 0)
5709 else if (i
+ 1 == argc
)
5711 cpp_fatal (pfile
, "Assertion missing after -A option");
5717 if (!strcmp (p
, "-")) {
5718 struct cpp_pending
**ptr
;
5719 /* -A- eliminates all predefined macros and assertions.
5720 Let's include also any that were specified earlier
5721 on the command line. That way we can get rid of any
5722 that were passed automatically in from GCC. */
5723 opts
->inhibit_predefs
= 1;
5724 for (ptr
= &opts
->pending
; *ptr
!= NULL
; )
5726 struct cpp_pending
*pend
= *ptr
;
5727 if (pend
->cmd
&& pend
->cmd
[0] == '-'
5728 && (pend
->cmd
[1] == 'D' || pend
->cmd
[1] == 'A'))
5737 push_pending (pfile
, "-A", p
);
5742 case 'U': /* JF #undef something */
5743 if (argv
[i
][2] != 0)
5744 push_pending (pfile
, "-U", argv
[i
] + 2);
5745 else if (i
+ 1 == argc
)
5747 cpp_fatal (pfile
, "Macro name missing after -U option");
5751 push_pending (pfile
, "-U", argv
[i
+1]), i
++;
5755 opts
->put_out_comments
= 1;
5758 case 'E': /* -E comes from cc -E; ignore it. */
5762 opts
->no_line_commands
= 1;
5765 case '$': /* Don't include $ in identifiers. */
5766 opts
->dollars_in_ident
= 0;
5770 if (!strcmp (argv
[i
], "-nostdinc"))
5771 /* -nostdinc causes no default include directories.
5772 You must specify all include-file directories with -I. */
5773 opts
->no_standard_includes
= 1;
5774 else if (!strcmp (argv
[i
], "-nostdinc++"))
5775 /* -nostdinc++ causes no default C++-specific include directories. */
5776 opts
->no_standard_cplusplus_includes
= 1;
5778 else if (!strcmp (argv
[i
], "-noprecomp"))
5784 if (!strcmp (argv
[i
], "-remap"))
5789 /* Sun compiler passes undocumented switch "-undef".
5790 Let's assume it means to inhibit the predefined symbols. */
5791 opts
->inhibit_predefs
= 1;
5794 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
5795 if (opts
->in_fname
== NULL
) {
5796 opts
->in_fname
= "";
5798 } else if (opts
->out_fname
== NULL
) {
5799 opts
->out_fname
= "";
5801 } /* else fall through into error */
5811 /* Handle command-line options in (argc, argv).
5812 Can be called multiple times, to handle multiple sets of options.
5813 Returns if an unrecognized option is seen.
5814 Returns number of strings consumed. */
5817 cpp_handle_options (pfile
, argc
, argv
)
5823 int strings_processed
;
5824 for (i
= 0; i
< argc
; i
+= strings_processed
)
5826 strings_processed
= cpp_handle_option (pfile
, argc
- i
, argv
+ i
);
5827 if (strings_processed
== 0)
5837 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
5839 if (opts
->print_deps
)
5841 /* Stream on which to print the dependency information. */
5844 /* Don't actually write the deps file if compilation has failed. */
5845 if (pfile
->errors
== 0)
5847 char *deps_mode
= opts
->print_deps_append
? "a" : "w";
5848 if (opts
->deps_file
== 0)
5849 deps_stream
= stdout
;
5850 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
5851 cpp_pfatal_with_name (pfile
, opts
->deps_file
);
5852 fputs (pfile
->deps_buffer
, deps_stream
);
5853 putc ('\n', deps_stream
);
5854 if (opts
->deps_file
)
5856 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
5857 cpp_fatal (pfile
, "I/O error on output");
5863 /* Debugging: dump statistics on the include hash table. */
5865 struct include_hash
*x
;
5868 for(i
= 0; i
< ALL_INCLUDE_HASHSIZE
; i
++)
5870 x
= pfile
->all_include_files
[i
];
5877 fprintf(stderr
, "%d/%d ", i
, j
);
5879 fputc('\n', stderr
);
5885 /* Free resources used by PFILE.
5886 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
5893 while ( CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
5894 cpp_pop_buffer (pfile
);
5896 if (pfile
->token_buffer
)
5898 free (pfile
->token_buffer
);
5899 pfile
->token_buffer
= NULL
;
5902 if (pfile
->deps_buffer
)
5904 free (pfile
->deps_buffer
);
5905 pfile
->deps_buffer
= NULL
;
5906 pfile
->deps_allocated_size
= 0;
5909 while (pfile
->if_stack
)
5911 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
5912 pfile
->if_stack
= temp
->next
;
5916 for (i
= ALL_INCLUDE_HASHSIZE
; --i
>= 0; )
5918 struct include_hash
*imp
= pfile
->all_include_files
[i
];
5921 struct include_hash
*next
= imp
->next
;
5923 /* This gets freed elsewhere - I think. */
5929 pfile
->all_include_files
[i
] = 0;
5932 cpp_hash_cleanup (pfile
);
5935 /* Read an assertion into the token buffer, converting to
5936 canonical form: `#predicate(a n swe r)' The next non-whitespace
5937 character to read should be the first letter of the predicate.
5938 Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
5939 with answer (see callers for why). In case of 0, an error has been
5942 parse_assertion (pfile
)
5946 cpp_skip_hspace (pfile
);
5948 if (! is_idstart
[c
])
5950 cpp_error (pfile
, "assertion predicate is not an identifier");
5953 CPP_PUTC(pfile
, '#');
5955 parse_name(pfile
, c
);
5960 if (is_hor_space
[c
])
5961 cpp_skip_hspace (pfile
);
5967 CPP_PUTC(pfile
, '(');
5970 while ((c
= GETC()) != ')')
5972 if (is_hor_space
[c
])
5976 CPP_PUTC(pfile
, ' ');
5980 else if (c
== '\\' && PEEKC() == '\n')
5982 else if (c
== '\n' || c
== EOF
)
5984 if (c
== '\n') FORWARD(-1);
5985 cpp_error (pfile
, "un-terminated assertion answer");
5995 if (pfile
->limit
[-1] == ' ')
5996 pfile
->limit
[-1] = ')';
5997 else if (pfile
->limit
[-1] == '(')
5999 cpp_error (pfile
, "empty token sequence in assertion");
6003 CPP_PUTC(pfile
, ')');
6005 CPP_NUL_TERMINATE(pfile
);
6010 do_assert (pfile
, keyword
)
6012 struct directive
*keyword ATTRIBUTE_UNUSED
;
6016 HASHNODE
*base
, *this;
6017 int baselen
, thislen
;
6019 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6020 && !CPP_BUFFER (pfile
)->system_header_p
)
6021 cpp_pedwarn (pfile
, "ANSI C does not allow `#assert'");
6023 cpp_skip_hspace (pfile
);
6024 sym
= CPP_PWRITTEN (pfile
); /* remember where it starts */
6025 ret
= parse_assertion (pfile
);
6030 cpp_error (pfile
, "missing token-sequence in `#assert'");
6034 cpp_skip_hspace (pfile
);
6036 if (c
!= EOF
&& c
!= '\n')
6038 cpp_error (pfile
, "junk at end of `#assert'");
6042 thislen
= strlen (sym
);
6043 baselen
= index (sym
, '(') - sym
;
6044 this = cpp_lookup (pfile
, sym
, thislen
, -1);
6047 cpp_warning (pfile
, "`%s' re-asserted", sym
);
6051 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
6053 base
= install (sym
, baselen
, T_ASSERT
, 0, 0, -1);
6054 else if (base
->type
!= T_ASSERT
)
6056 /* Token clash - but with what?! */
6058 "cpp internal error: base->type != T_ASSERT in do_assert");
6062 this = install (sym
, thislen
, T_ASSERT
, 0,
6063 (char *)base
->value
.aschain
, -1);
6064 base
->value
.aschain
= this;
6066 pfile
->limit
= sym
; /* Pop */
6070 pfile
->limit
= sym
; /* Pop */
6071 skip_rest_of_line (pfile
);
6076 do_unassert (pfile
, keyword
)
6078 struct directive
*keyword ATTRIBUTE_UNUSED
;
6082 long baselen
, thislen
;
6083 HASHNODE
*base
, *this, *next
;
6085 if (CPP_PEDANTIC (pfile
) && CPP_OPTIONS (pfile
)->done_initializing
6086 && !CPP_BUFFER (pfile
)->system_header_p
)
6087 cpp_pedwarn (pfile
, "ANSI C does not allow `#unassert'");
6089 cpp_skip_hspace (pfile
);
6091 sym
= CPP_PWRITTEN (pfile
); /* remember where it starts */
6092 ret
= parse_assertion (pfile
);
6096 cpp_skip_hspace (pfile
);
6098 if (c
!= EOF
&& c
!= '\n')
6099 cpp_error (pfile
, "junk at end of `#unassert'");
6101 thislen
= strlen (sym
);
6104 base
= cpp_lookup (pfile
, sym
, thislen
, -1);
6106 goto error
; /* It isn't an error to #undef what isn't #defined,
6107 so it isn't an error to #unassert what isn't
6108 #asserted either. */
6110 for (this = base
->value
.aschain
; this; this = next
)
6112 next
= this->value
.aschain
;
6113 delete_macro (this);
6115 delete_macro (base
);
6119 baselen
= index (sym
, '(') - sym
;
6120 base
= cpp_lookup (pfile
, sym
, baselen
, -1);
6121 if (! base
) goto error
;
6122 this = cpp_lookup (pfile
, sym
, thislen
, -1);
6123 if (! this) goto error
;
6126 while (next
->value
.aschain
!= this)
6127 next
= next
->value
.aschain
;
6129 next
->value
.aschain
= this->value
.aschain
;
6130 delete_macro (this);
6132 if (base
->value
.aschain
== NULL
)
6133 delete_macro (base
); /* Last answer for this predicate deleted. */
6136 pfile
->limit
= sym
; /* Pop */
6139 pfile
->limit
= sym
; /* Pop */
6140 skip_rest_of_line (pfile
);
6145 cpp_read_check_assertion (pfile
)
6148 char *name
= CPP_PWRITTEN (pfile
);
6152 FORWARD (1); /* Skip '#' */
6153 cpp_skip_hspace (pfile
);
6154 if (! parse_assertion (pfile
))
6158 hp
= cpp_lookup (pfile
, name
, (char *)CPP_PWRITTEN (pfile
) - name
, -1);
6162 pfile
->limit
= name
;
6166 /* Initialize PMARK to remember the current position of PFILE. */
6169 parse_set_mark (pmark
, pfile
)
6170 struct parse_marker
*pmark
;
6173 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
6174 pmark
->next
= pbuf
->marks
;
6175 pbuf
->marks
= pmark
;
6177 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
6180 /* Cleanup PMARK - we no longer need it. */
6183 parse_clear_mark (pmark
)
6184 struct parse_marker
*pmark
;
6186 struct parse_marker
**pp
= &pmark
->buf
->marks
;
6187 for (; ; pp
= &(*pp
)->next
) {
6188 if (*pp
== NULL
) abort ();
6189 if (*pp
== pmark
) break;
6194 /* Backup the current position of PFILE to that saved in PMARK. */
6197 parse_goto_mark (pmark
, pfile
)
6198 struct parse_marker
*pmark
;
6201 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
6202 if (pbuf
!= pmark
->buf
)
6203 cpp_fatal (pfile
, "internal error %s", "parse_goto_mark");
6204 pbuf
->cur
= pbuf
->buf
+ pmark
->position
;
6207 /* Reset PMARK to point to the current position of PFILE. (Same
6208 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
6211 parse_move_mark (pmark
, pfile
)
6212 struct parse_marker
*pmark
;
6215 cpp_buffer
*pbuf
= CPP_BUFFER (pfile
);
6216 if (pbuf
!= pmark
->buf
)
6217 cpp_fatal (pfile
, "internal error %s", "parse_move_mark");
6218 pmark
->position
= pbuf
->cur
- pbuf
->buf
;
6223 cpp_print_file_and_line (pfile
)
6226 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
6231 cpp_buf_line_and_col (ip
, &line
, &col
);
6232 cpp_file_line_for_message (pfile
, ip
->nominal_fname
,
6233 line
, pfile
->show_column
? col
: -1);
6238 v_cpp_error (pfile
, msg
, ap
)
6243 cpp_print_containing_files (pfile
);
6244 cpp_print_file_and_line (pfile
);
6245 v_cpp_message (pfile
, 1, msg
, ap
);
6249 cpp_error
VPROTO ((cpp_reader
* pfile
, const char *msg
, ...))
6251 #ifndef ANSI_PROTOTYPES
6259 #ifndef ANSI_PROTOTYPES
6260 pfile
= va_arg (ap
, cpp_reader
*);
6261 msg
= va_arg (ap
, const char *);
6264 v_cpp_error (pfile
, msg
, ap
);
6268 /* Print error message but don't count it. */
6271 v_cpp_warning (pfile
, msg
, ap
)
6276 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
6279 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
6282 cpp_print_containing_files (pfile
);
6283 cpp_print_file_and_line (pfile
);
6284 v_cpp_message (pfile
, 0, msg
, ap
);
6288 cpp_warning
VPROTO ((cpp_reader
* pfile
, const char *msg
, ...))
6290 #ifndef ANSI_PROTOTYPES
6298 #ifndef ANSI_PROTOTYPES
6299 pfile
= va_arg (ap
, cpp_reader
*);
6300 msg
= va_arg (ap
, const char *);
6303 v_cpp_warning (pfile
, msg
, ap
);
6307 /* Print an error message and maybe count it. */
6310 cpp_pedwarn
VPROTO ((cpp_reader
* pfile
, const char *msg
, ...))
6312 #ifndef ANSI_PROTOTYPES
6320 #ifndef ANSI_PROTOTYPES
6321 pfile
= va_arg (ap
, cpp_reader
*);
6322 msg
= va_arg (ap
, const char *);
6325 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
6326 v_cpp_error (pfile
, msg
, ap
);
6328 v_cpp_warning (pfile
, msg
, ap
);
6333 v_cpp_error_with_line (pfile
, line
, column
, msg
, ap
)
6340 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
6342 cpp_print_containing_files (pfile
);
6345 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
6347 v_cpp_message (pfile
, 1, msg
, ap
);
6351 cpp_error_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
, const char *msg
, ...))
6353 #ifndef ANSI_PROTOTYPES
6363 #ifndef ANSI_PROTOTYPES
6364 pfile
= va_arg (ap
, cpp_reader
*);
6365 line
= va_arg (ap
, int);
6366 column
= va_arg (ap
, int);
6367 msg
= va_arg (ap
, const char *);
6370 v_cpp_error_with_line(pfile
, line
, column
, msg
, ap
);
6375 v_cpp_warning_with_line (pfile
, line
, column
, msg
, ap
)
6384 if (CPP_OPTIONS (pfile
)->inhibit_warnings
)
6387 if (CPP_OPTIONS (pfile
)->warnings_are_errors
)
6390 cpp_print_containing_files (pfile
);
6392 ip
= cpp_file_buffer (pfile
);
6395 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, line
, column
);
6397 v_cpp_message (pfile
, 0, msg
, ap
);
6402 cpp_warning_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
, const char *msg
, ...))
6404 #ifndef ANSI_PROTOTYPES
6414 #ifndef ANSI_PROTOTYPES
6415 pfile
= va_arg (ap
, cpp_reader
*);
6416 line
= va_arg (ap
, int);
6417 column
= va_arg (ap
, int);
6418 msg
= va_arg (ap
, const char *);
6421 v_cpp_warning_with_line (pfile
, line
, column
, msg
, ap
);
6427 cpp_pedwarn_with_line
VPROTO ((cpp_reader
* pfile
, int line
, int column
, const char *msg
, ...))
6429 #ifndef ANSI_PROTOTYPES
6439 #ifndef ANSI_PROTOTYPES
6440 pfile
= va_arg (ap
, cpp_reader
*);
6441 line
= va_arg (ap
, int);
6442 column
= va_arg (ap
, int);
6443 msg
= va_arg (ap
, const char *);
6446 if (CPP_OPTIONS (pfile
)->pedantic_errors
)
6447 v_cpp_error_with_line (pfile
, column
, line
, msg
, ap
);
6449 v_cpp_warning_with_line (pfile
, line
, column
, msg
, ap
);
6453 /* Report a warning (or an error if pedantic_errors)
6454 giving specified file name and line number, not current. */
6457 cpp_pedwarn_with_file_and_line
VPROTO ((cpp_reader
*pfile
, char *file
, int line
, const char *msg
, ...))
6459 #ifndef ANSI_PROTOTYPES
6469 #ifndef ANSI_PROTOTYPES
6470 pfile
= va_arg (ap
, cpp_reader
*);
6471 file
= va_arg (ap
, char *);
6472 line
= va_arg (ap
, int);
6473 msg
= va_arg (ap
, const char *);
6476 if (!CPP_OPTIONS (pfile
)->pedantic_errors
6477 && CPP_OPTIONS (pfile
)->inhibit_warnings
)
6480 cpp_file_line_for_message (pfile
, file
, line
, -1);
6481 v_cpp_message (pfile
, CPP_OPTIONS (pfile
)->pedantic_errors
, msg
, ap
);
6485 /* my_strerror - return the descriptive text associated with an
6489 my_strerror (errnum
)
6495 #ifndef HAVE_STRERROR
6496 result
= (char *) ((errnum
< sys_nerr
) ? sys_errlist
[errnum
] : 0);
6498 result
= strerror (errnum
);
6501 /* VAXCRTL's strerror() takes an optional second argument, which only
6502 matters when the first argument is EVMSERR. However, it's simplest
6503 just to pass it unconditionally. `vaxc$errno' is declared in
6504 <errno.h>, and maintained by the library in parallel with `errno'.
6505 We assume that caller's `errnum' either matches the last setting of
6506 `errno' by the library or else does not have the value `EVMSERR'. */
6508 result
= strerror (errnum
, vaxc$errno
);
6512 result
= "undocumented I/O error";
6517 /* Error including a message from `errno'. */
6520 cpp_error_from_errno (pfile
, name
)
6524 cpp_message_from_errno (pfile
, 1, name
);
6528 cpp_message_from_errno (pfile
, is_error
, name
)
6534 cpp_buffer
*ip
= cpp_file_buffer (pfile
);
6536 cpp_print_containing_files (pfile
);
6539 cpp_file_line_for_message (pfile
, ip
->nominal_fname
, ip
->lineno
, -1);
6541 cpp_message (pfile
, is_error
, "%s: %s", name
, my_strerror (e
));
6545 cpp_perror_with_name (pfile
, name
)
6549 cpp_message (pfile
, 1, "%s: %s: %s", progname
, name
, my_strerror (errno
));
6553 * No pre-compiled header file support.
6555 * Possibly different enum token codes for each C/C++ token.
6557 * Find and cleanup remaining uses of static variables,
6559 * Support for trigraphs.
6561 * Support -dM flag (dump_all_macros).
6563 * Support for_lint flag.