2 Copyright (C) 1986, 87, 89, 92-97, 1998 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>
42 #ifndef GET_ENV_PATH_LIST
43 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
46 extern char *update_path PARAMS ((char *, char *));
50 #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
51 #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
53 /* Find the largest host integer type and set its size and type.
54 Watch out: on some crazy hosts `long' is shorter than `int'. */
58 # include <inttypes.h>
59 # define HOST_WIDE_INT intmax_t
61 # if (HOST_BITS_PER_LONG <= HOST_BITS_PER_INT \
62 && HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_INT)
63 # define HOST_WIDE_INT int
65 # if (HOST_BITS_PER_LONGLONG <= HOST_BITS_PER_LONG \
66 || ! (defined LONG_LONG_MAX || defined LLONG_MAX))
67 # define HOST_WIDE_INT long
69 # define HOST_WIDE_INT long long
75 /* By default, colon separates directories in a path. */
76 #ifndef PATH_SEPARATOR
77 #define PATH_SEPARATOR ':'
80 #ifndef STANDARD_INCLUDE_DIR
81 #define STANDARD_INCLUDE_DIR "/usr/include"
84 /* Symbols to predefine. */
87 static char *predefs = CPP_PREDEFINES;
89 static char *predefs = "";
92 /* We let tm.h override the types used here, to handle trivial differences
93 such as the choice of unsigned int or long unsigned int for size_t.
94 When machines start needing nontrivial differences in the size type,
95 it would be best to do something here to figure out automatically
96 from other information what type to use. */
98 /* The string value for __SIZE_TYPE__. */
101 #define SIZE_TYPE "long unsigned int"
104 /* The string value for __PTRDIFF_TYPE__. */
107 #define PTRDIFF_TYPE "long int"
110 /* The string value for __WCHAR_TYPE__. */
113 #define WCHAR_TYPE "int"
115 #define CPP_WCHAR_TYPE(PFILE) \
116 (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
118 /* The string value for __USER_LABEL_PREFIX__ */
120 #ifndef USER_LABEL_PREFIX
121 #define USER_LABEL_PREFIX ""
124 /* The string value for __REGISTER_PREFIX__ */
126 #ifndef REGISTER_PREFIX
127 #define REGISTER_PREFIX ""
130 /* In the definition of a #assert name, this structure forms
131 a list of the individual values asserted.
132 Each value is itself a list of "tokens".
133 These are strings that are compared by name. */
135 struct tokenlist_list {
136 struct tokenlist_list *next;
137 struct arglist *tokens;
140 struct assertion_hashnode {
141 struct assertion_hashnode *next; /* double links for easy deletion */
142 struct assertion_hashnode *prev;
143 /* also, a back pointer to this node's hash
144 chain is kept, in case the node is the head
145 of the chain and gets deleted. */
146 struct assertion_hashnode **bucket_hdr;
147 int length; /* length of token, for quick comparison */
148 U_CHAR *name; /* the actual name */
149 /* List of token-sequences. */
150 struct tokenlist_list *value;
153 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
154 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
156 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
157 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
158 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
159 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
160 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
161 (Note that it is false while we're expanding marco *arguments*.) */
162 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
164 /* Move all backslash-newline pairs out of embarrassing places.
165 Exchange all such pairs following BP
166 with any potentially-embarrassing characters that follow them.
167 Potentially-embarrassing characters are / and *
168 (because a backslash-newline inside a comment delimiter
169 would cause it not to be recognized). */
171 #define NEWLINE_FIX \
172 do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
174 /* Same, but assume we've already read the potential '\\' into C. */
175 #define NEWLINE_FIX1(C) do { \
176 while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
180 struct cpp_pending *next;
185 /* Forward declarations. */
188 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
190 static char *my_strerror PROTO ((int));
191 static void make_assertion PROTO ((cpp_reader *, char *, U_CHAR *));
192 static void path_include PROTO ((cpp_reader *, char *));
193 static void initialize_builtins PROTO ((cpp_reader *));
194 static void initialize_char_syntax PROTO ((struct cpp_options *));
196 static void trigraph_pcp ();
198 static void validate_else PROTO ((cpp_reader *, char *));
199 static int comp_def_part PROTO ((int, U_CHAR *, int, U_CHAR *,
202 extern void fancy_abort ();
204 static int check_macro_name PROTO ((cpp_reader *, U_CHAR *, char *));
205 static int compare_defs PROTO ((cpp_reader *,
206 DEFINITION *, DEFINITION *));
207 static int compare_token_lists PROTO ((struct arglist *,
209 static int is_system_include PROTO ((cpp_reader *, char *));
210 static HOST_WIDE_INT eval_if_expression PROTO ((cpp_reader *, U_CHAR *, int));
211 static int change_newlines PROTO ((U_CHAR *, int));
212 static struct arglist *read_token_list PROTO ((cpp_reader *, int *));
213 static void free_token_list PROTO ((struct arglist *));
214 static void push_macro_expansion PARAMS ((cpp_reader *,
215 U_CHAR *, int, HASHNODE *));
216 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
217 extern char *xrealloc ();
218 static char *xcalloc PROTO ((unsigned, unsigned));
220 static void conditional_skip PROTO ((cpp_reader *, int,
221 enum node_type, U_CHAR *));
222 static void skip_if_group PROTO ((cpp_reader *, int));
223 static int parse_name PARAMS ((cpp_reader *, int));
224 static void print_help PROTO ((void));
226 /* Last arg to output_line_command. */
227 enum file_change_code {same_file, enter_file, leave_file};
229 /* External declarations. */
231 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
233 extern char *version_string;
234 extern struct tm *localtime ();
237 /* #include "file" looks in source file dir, then stack. */
238 /* #include <file> just looks in the stack. */
239 /* -I directories are added to the end, then the defaults are added. */
241 static struct default_include {
242 char *fname; /* The name of the directory. */
243 char *component; /* The component containing the directory */
244 int cplusplus; /* Only look here if we're compiling C++. */
245 int cxx_aware; /* Includes in this directory don't need to
246 be wrapped in extern "C" when compiling
248 } include_defaults_array[]
249 #ifdef INCLUDE_DEFAULTS
253 /* Pick up GNU C++ specific include files. */
254 { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
256 /* This is the dir for fixincludes. Put it just before
257 the files that we fix. */
258 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
259 /* For cross-compilation, this dir name is generated
260 automatically in Makefile.in. */
261 { CROSS_INCLUDE_DIR, "GCC",0, 0 },
262 #ifdef TOOL_INCLUDE_DIR
263 /* This is another place that the target system's headers might be. */
264 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
266 #else /* not CROSS_COMPILE */
267 #ifdef LOCAL_INCLUDE_DIR
268 /* This should be /usr/local/include and should come before
269 the fixincludes-fixed header files. */
270 { LOCAL_INCLUDE_DIR, 0, 0, 1 },
272 #ifdef TOOL_INCLUDE_DIR
273 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
274 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
275 { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
277 /* This is the dir for fixincludes. Put it just before
278 the files that we fix. */
279 { GCC_INCLUDE_DIR, "GCC", 0, 0 },
280 /* Some systems have an extra dir of include files. */
281 #ifdef SYSTEM_INCLUDE_DIR
282 { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
284 #ifndef STANDARD_INCLUDE_COMPONENT
285 #define STANDARD_INCLUDE_COMPONENT 0
287 { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
288 #endif /* not CROSS_COMPILE */
291 #endif /* no INCLUDE_DEFAULTS */
293 /* `struct directive' defines one #-directive, including how to handle it. */
296 int length; /* Length of name */
297 int (*func) /* Function to handle directive */
298 PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
299 char *name; /* Name of directive */
300 enum node_type type; /* Code which describes which directive. */
301 char command_reads_line; /* One if rest of line is read by func. */
304 /* These functions are declared to return int instead of void since they
305 are going to be placed in a table and some old compilers have trouble with
306 pointers to functions returning void. */
308 static int do_define PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
309 static int do_line PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
310 static int do_include PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
311 static int do_undef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
312 static int do_error PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
313 static int do_pragma PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
314 static int do_ident PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
315 static int do_if PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
316 static int do_xifdef PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
317 static int do_else PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
318 static int do_elif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
319 static int do_endif PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
320 #ifdef SCCS_DIRECTIVE
321 static int do_sccs PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
323 static int do_assert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
324 static int do_unassert PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
325 static int do_warning PARAMS ((cpp_reader *, struct directive *, U_CHAR *, U_CHAR *));
327 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
328 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
330 /* Here is the actual list of #-directives, most-often-used first.
331 The initialize_builtins function assumes #define is the very first. */
333 static struct directive directive_table[] = {
334 { 6, do_define, "define", T_DEFINE, 0},
335 { 5, do_xifdef, "ifdef", T_IFDEF, 1},
336 { 6, do_xifdef, "ifndef", T_IFNDEF, 1},
337 { 7, do_include, "include", T_INCLUDE, 1},
338 { 12, do_include, "include_next", T_INCLUDE_NEXT, 1},
339 { 6, do_include, "import", T_IMPORT, 1},
340 { 5, do_endif, "endif", T_ENDIF, 1},
341 { 4, do_else, "else", T_ELSE, 1},
342 { 2, do_if, "if", T_IF, 1},
343 { 4, do_elif, "elif", T_ELIF, 1},
344 { 5, do_undef, "undef", T_UNDEF, 0},
345 { 5, do_error, "error", T_ERROR, 0},
346 { 7, do_warning, "warning", T_WARNING, 0},
347 { 6, do_pragma, "pragma", T_PRAGMA, 0},
348 { 4, do_line, "line", T_LINE, 1},
349 { 5, do_ident, "ident", T_IDENT, 1},
350 #ifdef SCCS_DIRECTIVE
351 { 4, do_sccs, "sccs", T_SCCS, 0},
353 { 6, do_assert, "assert", T_ASSERT, 1},
354 { 8, do_unassert, "unassert", T_UNASSERT, 1},
355 { -1, 0, "", T_UNUSED, 0},
358 /* table to tell if char can be part of a C identifier. */
359 U_CHAR is_idchar[256] = { 0 };
360 /* table to tell if char can be first char of a c identifier. */
361 U_CHAR is_idstart[256] = { 0 };
362 /* table to tell if c is horizontal space. */
363 U_CHAR is_hor_space[256] = { 0 };
364 /* table to tell if c is horizontal or vertical space. */
365 U_CHAR is_space[256] = { 0 };
367 /* Initialize syntactic classifications of characters. */
370 initialize_char_syntax (opts)
371 struct cpp_options *opts;
376 * Set up is_idchar and is_idstart tables. These should be
377 * faster than saying (is_alpha (c) || c == '_'), etc.
378 * Set up these things before calling any routines tthat
381 for (i = 'a'; i <= 'z'; i++) {
382 is_idchar[i - 'a' + 'A'] = 1;
384 is_idstart[i - 'a' + 'A'] = 1;
387 for (i = '0'; i <= '9'; i++)
391 is_idchar['$'] = opts->dollars_in_ident;
392 is_idstart['$'] = opts->dollars_in_ident;
394 /* horizontal space table */
395 is_hor_space[' '] = 1;
396 is_hor_space['\t'] = 1;
397 is_hor_space['\v'] = 1;
398 is_hor_space['\f'] = 1;
399 is_hor_space['\r'] = 1;
410 /* Place into PFILE a quoted string representing the string SRC.
411 Caller must reserve enough space in pfile->token_buffer. */
414 quote_string (pfile, src)
420 CPP_PUTC_Q (pfile, '\"');
422 switch ((c = *src++))
426 CPP_PUTC_Q (pfile, c);
429 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
430 CPP_ADJUST_WRITTEN (pfile, 4);
436 CPP_PUTC_Q (pfile, '\\');
437 CPP_PUTC_Q (pfile, c);
441 CPP_PUTC_Q (pfile, '\"');
442 CPP_NUL_TERMINATE_Q (pfile);
447 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars. */
450 cpp_grow_buffer (pfile, n)
454 long old_written = CPP_WRITTEN (pfile);
455 pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
456 pfile->token_buffer = (U_CHAR *)
457 xrealloc(pfile->token_buffer, pfile->token_buffer_size);
458 CPP_SET_WRITTEN (pfile, old_written);
463 * process a given definition string, for initialization
464 * If STR is just an identifier, define it with value 1.
465 * If STR has anything after the identifier, then it should
466 * be identifier=definition.
470 cpp_define (pfile, str)
480 cpp_error (pfile, "malformed option `-D %s'", str);
483 while (is_idchar[*++p])
486 while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
489 p = (U_CHAR *) str; /* Error */
493 buf = (U_CHAR *) alloca (p - buf + 4);
494 strcpy ((char *)buf, str);
495 strcat ((char *)buf, " 1");
499 cpp_error (pfile, "malformed option `-D %s'", str);
505 /* Copy the entire option so we can modify it. */
506 buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
507 strncpy (buf, str, p - str);
508 /* Change the = to a space. */
510 /* Scan for any backslash-newline and remove it. */
515 if (*p == '\\' && p[1] == '\n')
523 do_define (pfile, NULL, buf, buf + strlen (buf));
526 /* Process the string STR as if it appeared as the body of a #assert.
527 OPTION is the option name for which STR was the argument. */
530 make_assertion (pfile, option, str)
537 /* Copy the entire option so we can modify it. */
538 buf = (U_CHAR *) alloca (strlen (str) + 1);
539 strcpy ((char *) buf, str);
540 /* Scan for any backslash-newline and remove it. */
544 if (*p == '\\' && p[1] == '\n')
553 if (!is_idstart[*p]) {
554 cpp_error (pfile, "malformed option `%s %s'", option, str);
557 while (is_idchar[*++p])
559 while (*p == ' ' || *p == '\t') p++;
560 if (! (*p == 0 || *p == '(')) {
561 cpp_error (pfile, "malformed option `%s %s'", option, str);
565 if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
567 do_assert (pfile, NULL, NULL, NULL);
568 cpp_pop_buffer (pfile);
572 /* Given a colon-separated list of file names PATH,
573 add all the names to the search path for include files. */
576 path_include (pfile, path)
588 struct file_name_list *dirtmp;
590 /* Find the end of this name. */
591 while (*q != 0 && *q != PATH_SEPARATOR) q++;
593 /* An empty name in the path stands for the current directory. */
594 name = (char *) xmalloc (2);
598 /* Otherwise use the directory that is named. */
599 name = (char *) xmalloc (q - p + 1);
600 bcopy (p, name, q - p);
604 dirtmp = (struct file_name_list *)
605 xmalloc (sizeof (struct file_name_list));
606 dirtmp->next = 0; /* New one goes on the end */
607 dirtmp->control_macro = 0;
608 dirtmp->c_system_include_path = 0;
609 dirtmp->fname = name;
610 dirtmp->got_name_map = 0;
611 append_include_chain (pfile, dirtmp, dirtmp);
613 /* Advance past this name. */
617 /* Skip the colon. */
623 cpp_options_init (opts)
626 bzero ((char *) opts, sizeof *opts);
627 opts->in_fname = NULL;
628 opts->out_fname = NULL;
630 /* Initialize is_idchar to allow $. */
631 opts->dollars_in_ident = 1;
632 initialize_char_syntax (opts);
634 opts->no_line_commands = 0;
635 opts->no_trigraphs = 1;
636 opts->put_out_comments = 0;
637 opts->print_include_names = 0;
638 opts->dump_macros = dump_none;
642 opts->cplusplus_comments = 1;
649 opts->pedantic_errors = 0;
650 opts->inhibit_warnings = 0;
651 opts->warn_comments = 0;
652 opts->warn_import = 1;
653 opts->warnings_are_errors = 0;
657 null_underflow (pfile)
658 cpp_reader *pfile ATTRIBUTE_UNUSED;
664 null_cleanup (pbuf, pfile)
665 cpp_buffer *pbuf ATTRIBUTE_UNUSED;
666 cpp_reader *pfile ATTRIBUTE_UNUSED;
672 macro_cleanup (pbuf, pfile)
674 cpp_reader *pfile ATTRIBUTE_UNUSED;
676 HASHNODE *macro = (HASHNODE *) pbuf->data;
677 if (macro->type == T_DISABLED)
678 macro->type = T_MACRO;
679 if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
684 /* Assuming we have read '/'.
685 If this is the start of a comment (followed by '*' or '/'),
686 skip to the end of the comment, and return ' '.
687 Return EOF if we reached the end of file before the end of the comment.
688 If not the start of a comment, return '/'. */
691 skip_comment (pfile, linep)
696 while (PEEKC() == '\\' && PEEKN(1) == '\n')
711 while (c == '\\' && PEEKC() == '\n')
715 FORWARD(1), c = GETC();
717 if (prev_c == '*' && c == '/')
719 if (c == '\n' && linep)
723 else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
730 return ' '; /* Allow // to be terminated by EOF. */
731 while (c == '\\' && PEEKC() == '\n')
740 /* Don't consider final '\n' to be part of comment. */
750 /* Skip whitespace \-newline and comments. Does not macro-expand. */
753 cpp_skip_hspace (pfile)
763 if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
764 cpp_pedwarn (pfile, "%s in preprocessing directive",
765 c == '\f' ? "formfeed" : "vertical tab");
771 c = skip_comment (pfile, NULL);
774 if (c == EOF || c == '/')
777 else if (c == '\\' && PEEKN(1) == '\n') {
780 else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
781 && is_hor_space[PEEKN(1)])
787 /* Read the rest of the current line.
788 The line is appended to PFILE's output buffer. */
791 copy_rest_of_line (pfile)
794 struct cpp_options *opts = CPP_OPTIONS (pfile);
811 goto scan_directive_token;
815 if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
816 goto scan_directive_token;
820 if (CPP_PEDANTIC (pfile))
821 cpp_pedwarn (pfile, "%s in preprocessing directive",
822 c == '\f' ? "formfeed" : "vertical tab");
828 scan_directive_token:
830 cpp_get_token (pfile);
836 CPP_NUL_TERMINATE (pfile);
840 skip_rest_of_line (pfile)
843 long old = CPP_WRITTEN (pfile);
844 copy_rest_of_line (pfile);
845 CPP_SET_WRITTEN (pfile, old);
848 /* Handle a possible # directive.
849 '#' has already been read. */
852 handle_directive (pfile)
855 register struct directive *kt;
858 U_CHAR *ident, *line_end;
859 long old_written = CPP_WRITTEN (pfile);
861 cpp_skip_hspace (pfile);
864 if (c >= '0' && c <= '9')
866 /* Handle # followed by a line number. */
867 if (CPP_PEDANTIC (pfile))
868 cpp_pedwarn (pfile, "`#' followed by integer");
869 do_line (pfile, NULL, NULL, NULL);
870 goto done_a_directive;
873 /* Now find the directive name. */
874 CPP_PUTC (pfile, '#');
875 parse_name (pfile, GETC());
876 ident = pfile->token_buffer + old_written + 1;
877 ident_length = CPP_PWRITTEN (pfile) - ident;
878 if (ident_length == 0 && PEEKC() == '\n')
880 /* A line of just `#' becomes blank. */
881 goto done_a_directive;
885 if (ident_length == 0 || !is_idstart[*ident]) {
887 while (is_idchar[*p]) {
888 if (*p < '0' || *p > '9')
892 /* Avoid error for `###' and similar cases unless -pedantic. */
894 while (*p == '#' || is_hor_space[*p]) p++;
896 if (pedantic && !lang_asm)
897 cpp_warning (pfile, "invalid preprocessor directive");
903 cpp_error (pfile, "invalid preprocessor directive name");
909 * Decode the keyword and call the appropriate expansion
910 * routine, after moving the input pointer up to the next line.
912 for (kt = directive_table; ; kt++) {
914 goto not_a_directive;
915 if (kt->length == ident_length && !strncmp (kt->name, ident, ident_length))
919 if (kt->command_reads_line)
923 /* Nonzero means do not delete comments within the directive.
924 #define needs this when -traditional. */
925 int comments = CPP_TRADITIONAL (pfile) && kt->type == T_DEFINE;
926 int save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
927 CPP_OPTIONS (pfile)->put_out_comments = comments;
928 after_ident = CPP_WRITTEN (pfile);
929 copy_rest_of_line (pfile);
930 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
933 /* We may want to pass through #define, #pragma, and #include.
934 Other directives may create output, but we don't want the directive
935 itself out, so we pop it now. For example conditionals may emit
936 #failed ... #endfailed stuff. But note that popping the buffer
937 means the parameters to kt->func may point after pfile->limit
938 so these parameters are invalid as soon as something gets appended
939 to the token_buffer. */
941 line_end = CPP_PWRITTEN (pfile);
942 if (! (kt->type == T_DEFINE
943 || kt->type == T_PRAGMA
944 || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
945 && CPP_OPTIONS (pfile)->dump_includes)))
946 CPP_SET_WRITTEN (pfile, old_written);
948 (*kt->func) (pfile, kt, pfile->token_buffer + after_ident, line_end);
950 if (kt->type == T_DEFINE)
952 if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
954 /* Skip "#define". */
955 U_CHAR *p = pfile->token_buffer + old_written + 7;
957 SKIP_WHITE_SPACE (p);
958 while (is_idchar[*p]) p++;
960 CPP_PUTC (pfile, '\n');
962 else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
963 CPP_SET_WRITTEN (pfile, old_written);
973 /* Pass a directive through to the output file.
974 BUF points to the contents of the directive, as a contiguous string.
975 LIMIT points to the first character past the end of the directive.
976 KEYWORD is the keyword-table entry for the directive. */
979 pass_thru_directive (buf, limit, pfile, keyword)
982 struct directive *keyword;
984 register unsigned keyword_length = keyword->length;
986 CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
987 CPP_PUTC_Q (pfile, '#');
988 CPP_PUTS_Q (pfile, keyword->name, keyword_length);
989 if (limit != buf && buf[0] != ' ')
990 CPP_PUTC_Q (pfile, ' ');
991 CPP_PUTS_Q (pfile, buf, limit - buf);
993 CPP_PUTS_Q (pfile, '\n');
994 /* Count the line we have just made in the output,
995 to get in sync properly. */
1000 /* The arglist structure is built by do_define to tell
1001 collect_definition where the argument names begin. That
1002 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
1003 would contain pointers to the strings x, y, and z.
1004 Collect_definition would then build a DEFINITION node,
1005 with reflist nodes pointing to the places x, y, and z had
1006 appeared. So the arglist is just convenience data passed
1007 between these two routines. It is not kept around after
1008 the current #define has been processed and entered into the
1012 struct arglist *next;
1019 /* Read a replacement list for a macro with parameters.
1020 Build the DEFINITION structure.
1021 Reads characters of text starting at BUF until END.
1022 ARGLIST specifies the formal parameters to look for
1023 in the text of the definition; NARGS is the number of args
1024 in that list, or -1 for a macro name that wants no argument list.
1025 MACRONAME is the macro name itself (so we can avoid recursive expansion)
1026 and NAMELEN is its length in characters.
1028 Note that comments, backslash-newlines, and leading white space
1029 have already been deleted from the argument. */
1032 collect_expansion (pfile, buf, limit, nargs, arglist)
1034 U_CHAR *buf, *limit;
1036 struct arglist *arglist;
1039 register U_CHAR *p, *lastp, *exp_p;
1040 struct reflist *endpat = NULL;
1041 /* Pointer to first nonspace after last ## seen. */
1043 /* Pointer to first nonspace after last single-# seen. */
1044 U_CHAR *stringify = 0;
1046 int expected_delimiter = '\0';
1048 /* Scan thru the replacement list, ignoring comments and quoted
1049 strings, picking up on the macro calls. It does a linear search
1050 thru the arg list on every potential symbol. Profiling might say
1051 that something smarter should happen. */
1056 /* Find the beginning of the trailing whitespace. */
1058 while (p < limit && is_space[limit[-1]]) limit--;
1060 /* Allocate space for the text in the macro definition.
1061 Leading and trailing whitespace chars need 2 bytes each.
1062 Each other input char may or may not need 1 byte,
1063 so this is an upper bound. The extra 5 are for invented
1064 leading and trailing newline-marker and final null. */
1065 maxsize = (sizeof (DEFINITION)
1067 /* Occurrences of '@' get doubled, so allocate extra space for them. */
1071 defn = (DEFINITION *) xcalloc (1, maxsize);
1073 defn->nargs = nargs;
1074 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
1079 /* Add one initial space escape-marker to prevent accidental
1080 token-pasting (often removed by macroexpand). */
1084 if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1085 cpp_error (pfile, "`##' at start of macro definition");
1089 /* Process the main body of the definition. */
1091 int skipped_arg = 0;
1092 register U_CHAR c = *p++;
1096 if (!CPP_TRADITIONAL (pfile)) {
1100 if (expected_delimiter != '\0') {
1101 if (c == expected_delimiter)
1102 expected_delimiter = '\0';
1104 expected_delimiter = c;
1108 if (p < limit && expected_delimiter) {
1109 /* In a string, backslash goes through
1110 and makes next char ordinary. */
1116 /* An '@' in a string or character constant stands for itself,
1117 and does not need to be escaped. */
1118 if (!expected_delimiter)
1123 /* # is ordinary inside a string. */
1124 if (expected_delimiter)
1126 if (p < limit && *p == '#') {
1127 /* ##: concatenate preceding and following tokens. */
1128 /* Take out the first #, discard preceding whitespace. */
1130 while (exp_p > lastp && is_hor_space[exp_p[-1]])
1132 /* Skip the second #. */
1134 /* Discard following whitespace. */
1135 SKIP_WHITE_SPACE (p);
1138 cpp_error (pfile, "`##' at end of macro definition");
1139 } else if (nargs >= 0) {
1140 /* Single #: stringify following argument ref.
1141 Don't leave the # in the expansion. */
1143 SKIP_WHITE_SPACE (p);
1144 if (p == limit || ! is_idstart[*p]
1145 || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1147 "`#' operator is not followed by a macro argument name");
1154 /* In -traditional mode, recognize arguments inside strings and
1155 character constants, and ignore special properties of #.
1156 Arguments inside strings are considered "stringified", but no
1157 extra quote marks are supplied. */
1161 if (expected_delimiter != '\0') {
1162 if (c == expected_delimiter)
1163 expected_delimiter = '\0';
1165 expected_delimiter = c;
1169 /* Backslash quotes delimiters and itself, but not macro args. */
1170 if (expected_delimiter != 0 && p < limit
1171 && (*p == expected_delimiter || *p == '\\')) {
1178 if (expected_delimiter != '\0') /* No comments inside strings. */
1181 /* If we find a comment that wasn't removed by handle_directive,
1182 this must be -traditional. So replace the comment with
1186 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1189 /* Mark this as a concatenation-point, as if it had been ##. */
1197 /* Handle the start of a symbol. */
1198 if (is_idchar[c] && nargs > 0) {
1199 U_CHAR *id_beg = p - 1;
1203 while (p != limit && is_idchar[*p]) p++;
1204 id_len = p - id_beg;
1207 && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1208 register struct arglist *arg;
1210 for (arg = arglist; arg != NULL; arg = arg->next) {
1211 struct reflist *tpat;
1213 if (arg->name[0] == c
1214 && arg->length == id_len
1215 && strncmp (arg->name, id_beg, id_len) == 0) {
1216 if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1217 if (CPP_TRADITIONAL (pfile)) {
1218 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1222 "macro arg `%.*s' would be stringified with -traditional.",
1226 /* If ANSI, don't actually substitute inside a string. */
1227 if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1229 /* make a pat node for this arg and append it to the end of
1231 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1233 tpat->raw_before = concat == id_beg;
1234 tpat->raw_after = 0;
1235 tpat->rest_args = arg->rest_args;
1236 tpat->stringify = (CPP_TRADITIONAL (pfile)
1237 ? expected_delimiter != '\0'
1238 : stringify == id_beg);
1241 defn->pattern = tpat;
1243 endpat->next = tpat;
1246 tpat->argno = arg->argno;
1247 tpat->nchars = exp_p - lastp;
1249 register U_CHAR *p1 = p;
1250 SKIP_WHITE_SPACE (p1);
1251 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1252 tpat->raw_after = 1;
1254 lastp = exp_p; /* place to start copying from next time */
1261 /* If this was not a macro arg, copy it into the expansion. */
1262 if (! skipped_arg) {
1263 register U_CHAR *lim1 = p;
1267 if (stringify == id_beg)
1269 "`#' operator should be followed by a macro argument name");
1274 if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1276 /* If ANSI, put in a "@ " marker to prevent token pasting.
1277 But not if "inside a string" (which in ANSI mode
1278 happens only for -D option). */
1285 defn->length = exp_p - defn->expansion;
1287 /* Crash now if we overrun the allocated size. */
1288 if (defn->length + 1 > maxsize)
1292 /* This isn't worth the time it takes. */
1293 /* give back excess storage */
1294 defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1301 * special extension string that can be added to the last macro argument to
1302 * allow it to absorb the "rest" of the arguments when expanded. Ex:
1303 * #define wow(a, b...) process (b, a, b)
1304 * { wow (1, 2, 3); } -> { process (2, 3, 1, 2, 3); }
1305 * { wow (one, two); } -> { process (two, one, two); }
1306 * if this "rest_arg" is used with the concat token '##' and if it is not
1307 * supplied then the token attached to with ## will not be outputted. Ex:
1308 * #define wow (a, b...) process (b ## , a, ## b)
1309 * { wow (1, 2); } -> { process (2, 1, 2); }
1310 * { wow (one); } -> { process (one); {
1312 static char rest_extension[] = "...";
1313 #define REST_EXTENSION_LENGTH (sizeof (rest_extension) - 1)
1315 /* Create a DEFINITION node from a #define directive. Arguments are
1316 as for do_define. */
1319 create_definition (buf, limit, pfile, predefinition)
1320 U_CHAR *buf, *limit;
1324 U_CHAR *bp; /* temp ptr into input buffer */
1325 U_CHAR *symname; /* remember where symbol name starts */
1326 int sym_length; /* and how long it is */
1329 char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1331 int arglengths = 0; /* Accumulate lengths of arg names
1332 plus number of args. */
1334 cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1338 while (is_hor_space[*bp])
1341 symname = bp; /* remember where it starts */
1343 sym_length = check_macro_name (pfile, bp, "macro");
1346 /* Lossage will occur if identifiers or control keywords are broken
1347 across lines using backslash. This is not the right place to take
1351 struct arglist *arg_ptrs = NULL;
1354 bp++; /* skip '(' */
1355 SKIP_WHITE_SPACE (bp);
1357 /* Loop over macro argument names. */
1358 while (*bp != ')') {
1359 struct arglist *temp;
1361 temp = (struct arglist *) alloca (sizeof (struct arglist));
1363 temp->next = arg_ptrs;
1364 temp->argno = argno++;
1365 temp->rest_args = 0;
1369 cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1371 if (!is_idstart[*bp])
1372 cpp_pedwarn (pfile, "invalid character in macro parameter name");
1374 /* Find the end of the arg name. */
1375 while (is_idchar[*bp]) {
1377 /* do we have a "special" rest-args extension here? */
1378 if ((size_t)(limit - bp) > REST_EXTENSION_LENGTH
1379 && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1381 temp->rest_args = 1;
1385 temp->length = bp - temp->name;
1387 bp += REST_EXTENSION_LENGTH;
1388 arglengths += temp->length + 2;
1389 SKIP_WHITE_SPACE (bp);
1390 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1391 cpp_error (pfile, "badly punctuated parameter list in `#define'");
1396 SKIP_WHITE_SPACE (bp);
1399 cpp_error (pfile, "unterminated parameter list in `#define'");
1403 struct arglist *otemp;
1405 for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1406 if (temp->length == otemp->length
1407 && strncmp (temp->name, otemp->name, temp->length) == 0) {
1410 name = (U_CHAR *) alloca (temp->length + 1);
1411 (void) strncpy (name, temp->name, temp->length);
1412 name[temp->length] = '\0';
1414 "duplicate argument name `%s' in `#define'", name);
1420 ++bp; /* skip paren */
1421 SKIP_WHITE_SPACE (bp);
1422 /* now everything from bp before limit is the definition. */
1423 defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1424 defn->rest_args = rest_args;
1426 /* Now set defn->args.argnames to the result of concatenating
1427 the argument names in reverse order
1428 with comma-space between them. */
1429 defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1431 struct arglist *temp;
1433 for (temp = arg_ptrs; temp; temp = temp->next) {
1434 bcopy (temp->name, &defn->args.argnames[i], temp->length);
1436 if (temp->next != 0) {
1437 defn->args.argnames[i++] = ',';
1438 defn->args.argnames[i++] = ' ';
1441 defn->args.argnames[i] = 0;
1444 /* Simple expansion or empty definition. */
1448 if (is_hor_space[*bp]) {
1450 SKIP_WHITE_SPACE (bp);
1453 case '!': case '"': case '#': case '%': case '&': case '\'':
1454 case ')': case '*': case '+': case ',': case '-': case '.':
1455 case '/': case ':': case ';': case '<': case '=': case '>':
1456 case '?': case '[': case '\\': case ']': case '^': case '{':
1457 case '|': case '}': case '~':
1458 cpp_warning (pfile, "missing white space after `#define %.*s'",
1459 sym_length, symname);
1463 cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1464 sym_length, symname);
1469 /* now everything from bp before limit is the definition. */
1470 defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1471 defn->args.argnames = (U_CHAR *) "";
1477 /* OP is null if this is a predefinition */
1478 defn->predefined = predefinition;
1480 mdef.symnam = symname;
1481 mdef.symlen = sym_length;
1490 /* Check a purported macro name SYMNAME, and yield its length.
1491 USAGE is the kind of name this is intended for. */
1494 check_macro_name (pfile, symname, usage)
1502 for (p = symname; is_idchar[*p]; p++)
1504 sym_length = p - symname;
1506 || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1507 cpp_error (pfile, "invalid %s name", usage);
1508 else if (!is_idstart[*symname]) {
1509 U_CHAR *msg; /* what pain... */
1510 msg = (U_CHAR *) alloca (sym_length + 1);
1511 bcopy (symname, msg, sym_length);
1512 msg[sym_length] = 0;
1513 cpp_error (pfile, "invalid %s name `%s'", usage, msg);
1515 if (! strncmp (symname, "defined", 7) && sym_length == 7)
1516 cpp_error (pfile, "invalid %s name `defined'", usage);
1521 /* Return zero if two DEFINITIONs are isomorphic. */
1524 compare_defs (pfile, d1, d2)
1526 DEFINITION *d1, *d2;
1528 register struct reflist *a1, *a2;
1529 register U_CHAR *p1 = d1->expansion;
1530 register U_CHAR *p2 = d2->expansion;
1533 if (d1->nargs != d2->nargs)
1535 if (CPP_PEDANTIC (pfile)
1536 && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1538 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1539 a1 = a1->next, a2 = a2->next) {
1540 if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1541 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1542 || a1->argno != a2->argno
1543 || a1->stringify != a2->stringify
1544 || a1->raw_before != a2->raw_before
1545 || a1->raw_after != a2->raw_after)
1553 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1554 p2, d2->length - (p2 - d2->expansion), 1))
1559 /* Return 1 if two parts of two macro definitions are effectively different.
1560 One of the parts starts at BEG1 and has LEN1 chars;
1561 the other has LEN2 chars at BEG2.
1562 Any sequence of whitespace matches any other sequence of whitespace.
1563 FIRST means these parts are the first of a macro definition;
1564 so ignore leading whitespace entirely.
1565 LAST means these parts are the last of a macro definition;
1566 so ignore trailing whitespace entirely. */
1569 comp_def_part (first, beg1, len1, beg2, len2, last)
1571 U_CHAR *beg1, *beg2;
1575 register U_CHAR *end1 = beg1 + len1;
1576 register U_CHAR *end2 = beg2 + len2;
1578 while (beg1 != end1 && is_space[*beg1]) beg1++;
1579 while (beg2 != end2 && is_space[*beg2]) beg2++;
1582 while (beg1 != end1 && is_space[end1[-1]]) end1--;
1583 while (beg2 != end2 && is_space[end2[-1]]) end2--;
1585 while (beg1 != end1 && beg2 != end2) {
1586 if (is_space[*beg1] && is_space[*beg2]) {
1587 while (beg1 != end1 && is_space[*beg1]) beg1++;
1588 while (beg2 != end2 && is_space[*beg2]) beg2++;
1589 } else if (*beg1 == *beg2) {
1593 return (beg1 != end1) || (beg2 != end2);
1596 /* Process a #define command.
1597 BUF points to the contents of the #define command, as a contiguous string.
1598 LIMIT points to the first character past the end of the definition.
1599 KEYWORD is the keyword-table entry for #define,
1600 or NULL for a "predefined" macro. */
1603 do_define (pfile, keyword, buf, limit)
1605 struct directive *keyword;
1606 U_CHAR *buf, *limit;
1613 /* If this is a precompiler run (with -pcp) pass thru #define commands. */
1614 if (pcp_outfile && keyword)
1615 pass_thru_directive (buf, limit, pfile, keyword);
1618 mdef = create_definition (buf, limit, pfile, keyword == NULL);
1622 hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1624 if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1627 /* Redefining a precompiled key is ok. */
1628 if (hp->type == T_PCSTRING)
1630 /* Redefining a macro is ok if the definitions are the same. */
1631 else if (hp->type == T_MACRO)
1632 ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1633 /* Redefining a constant is ok with -D. */
1634 else if (hp->type == T_CONST)
1635 ok = ! CPP_OPTIONS (pfile)->done_initializing;
1636 /* Print the warning if it's not ok. */
1639 U_CHAR *msg; /* what pain... */
1641 /* If we are passing through #define and #undef directives, do
1642 that for this re-definition now. */
1643 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1644 pass_thru_directive (buf, limit, pfile, keyword);
1646 msg = (U_CHAR *) alloca (mdef.symlen + 22);
1648 bcopy (mdef.symnam, msg + 1, mdef.symlen);
1649 strcpy ((char *) (msg + mdef.symlen + 1), "' redefined");
1650 cpp_pedwarn (pfile, msg);
1651 if (hp->type == T_MACRO)
1652 cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1653 "this is the location of the previous definition");
1655 /* Replace the old definition. */
1657 hp->value.defn = mdef.defn;
1661 /* If we are passing through #define and #undef directives, do
1662 that for this new definition now. */
1663 if (CPP_OPTIONS (pfile)->debug_output && keyword)
1664 pass_thru_directive (buf, limit, pfile, keyword);
1665 install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1666 (char *) mdef.defn, hashcode);
1676 /* This structure represents one parsed argument in a macro call.
1677 `raw' points to the argument text as written (`raw_length' is its length).
1678 `expanded' points to the argument's macro-expansion
1679 (its length is `expand_length').
1680 `stringified_length' is the length the argument would have
1682 `use_count' is the number of times this macro arg is substituted
1683 into the macro. If the actual use count exceeds 10,
1684 the value stored is 10. */
1686 /* raw and expanded are relative to ARG_BASE */
1687 #define ARG_BASE ((pfile)->token_buffer)
1690 /* Strings relative to pfile->token_buffer */
1691 long raw, expanded, stringified;
1692 int raw_length, expand_length;
1693 int stringified_length;
1698 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1699 If BUFFER != NULL, then use the LENGTH characters in BUFFER
1700 as the new input buffer.
1701 Return the new buffer, or NULL on failure. */
1704 cpp_push_buffer (pfile, buffer, length)
1709 register cpp_buffer *buf = CPP_BUFFER (pfile);
1710 if (buf == pfile->buffer_stack)
1712 cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1717 bzero ((char *) buf, sizeof (cpp_buffer));
1718 CPP_BUFFER (pfile) = buf;
1719 buf->if_stack = pfile->if_stack;
1720 buf->cleanup = null_cleanup;
1721 buf->underflow = null_underflow;
1722 buf->buf = buf->cur = buffer;
1723 buf->alimit = buf->rlimit = buffer + length;
1729 cpp_pop_buffer (pfile)
1732 cpp_buffer *buf = CPP_BUFFER (pfile);
1733 (*buf->cleanup) (buf, pfile);
1734 return ++CPP_BUFFER (pfile);
1737 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1738 Pop the buffer when done. */
1741 cpp_scan_buffer (pfile)
1744 cpp_buffer *buffer = CPP_BUFFER (pfile);
1747 enum cpp_token token = cpp_get_token (pfile);
1748 if (token == CPP_EOF) /* Should not happen ... */
1750 if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1752 cpp_pop_buffer (pfile);
1759 * Rescan a string (which may have escape marks) into pfile's buffer.
1760 * Place the result in pfile->token_buffer.
1762 * The input is copied before it is scanned, so it is safe to pass
1763 * it something from the token_buffer that will get overwritten
1764 * (because it follows CPP_WRITTEN). This is used by do_include.
1768 cpp_expand_to_buffer (pfile, buf, length)
1773 register cpp_buffer *ip;
1777 U_CHAR *limit = buf + length;
1780 int odepth = indepth;
1786 /* Set up the input on the input stack. */
1788 buf1 = (U_CHAR *) alloca (length + 1);
1790 register U_CHAR *p1 = buf;
1791 register U_CHAR *p2 = buf1;
1798 ip = cpp_push_buffer (pfile, buf1, length);
1801 ip->has_escapes = 1;
1803 ip->lineno = obuf.lineno = 1;
1806 /* Scan the input, create the output. */
1807 cpp_scan_buffer (pfile);
1810 if (indepth != odepth)
1814 CPP_NUL_TERMINATE (pfile);
1819 adjust_position (buf, limit, linep, colp)
1829 (*linep)++, (*colp) = 1;
1835 /* Move line_base forward, updating lineno and colno. */
1838 update_position (pbuf)
1839 register cpp_buffer *pbuf;
1841 unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1842 unsigned char *new_pos = pbuf->cur;
1843 register struct parse_marker *mark;
1844 for (mark = pbuf->marks; mark != NULL; mark = mark->next)
1846 if (pbuf->buf + mark->position < new_pos)
1847 new_pos = pbuf->buf + mark->position;
1849 pbuf->line_base += new_pos - old_pos;
1850 adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1854 cpp_buf_line_and_col (pbuf, linep, colp)
1855 register cpp_buffer *pbuf;
1863 *linep = pbuf->lineno;
1864 *colp = pbuf->colno;
1865 adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
1874 /* Return the cpp_buffer that corresponds to a file (not a macro). */
1877 cpp_file_buffer (pfile)
1880 cpp_buffer *ip = CPP_BUFFER (pfile);
1882 for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
1883 if (ip->fname != NULL)
1889 count_newlines (buf, limit)
1890 register U_CHAR *buf;
1891 register U_CHAR *limit;
1893 register long count = 0;
1904 * write out a #line command, for instance, after an #include file.
1905 * If CONDITIONAL is nonzero, we can omit the #line if it would
1906 * appear to be a no-op, and we can output a few newlines instead
1907 * if we want to increase the line number by a small amount.
1908 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
1912 output_line_command (pfile, conditional, file_change)
1915 enum file_change_code file_change;
1918 cpp_buffer *ip = CPP_BUFFER (pfile);
1920 if (ip->fname == NULL)
1923 update_position (ip);
1925 if (CPP_OPTIONS (pfile)->no_line_commands
1926 || CPP_OPTIONS (pfile)->no_output)
1929 line = CPP_BUFFER (pfile)->lineno;
1930 col = CPP_BUFFER (pfile)->colno;
1931 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
1933 if (CPP_OPTIONS (pfile)->no_line_commands)
1937 if (line == pfile->lineno)
1940 /* If the inherited line number is a little too small,
1941 output some newlines instead of a #line command. */
1942 if (line > pfile->lineno && line < pfile->lineno + 8) {
1943 CPP_RESERVE (pfile, 20);
1944 while (line > pfile->lineno) {
1945 CPP_PUTC_Q (pfile, '\n');
1953 /* Don't output a line number of 0 if we can help it. */
1954 if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
1955 && *ip->bufp == '\n') {
1961 CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
1963 #ifdef OUTPUT_LINE_COMMANDS
1964 static char sharp_line[] = "#line ";
1966 static char sharp_line[] = "# ";
1968 CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
1971 sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
1972 CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
1974 quote_string (pfile, ip->nominal_fname);
1975 if (file_change != same_file) {
1976 CPP_PUTC_Q (pfile, ' ');
1977 CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
1979 /* Tell cc1 if following text comes from a system header file. */
1980 if (ip->system_header_p) {
1981 CPP_PUTC_Q (pfile, ' ');
1982 CPP_PUTC_Q (pfile, '3');
1984 #ifndef NO_IMPLICIT_EXTERN_C
1985 /* Tell cc1plus if following text should be treated as C. */
1986 if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
1987 CPP_PUTC_Q (pfile, ' ');
1988 CPP_PUTC_Q (pfile, '4');
1991 CPP_PUTC_Q (pfile, '\n');
1992 pfile->lineno = line;
1996 * Parse a macro argument and append the info on PFILE's token_buffer.
1997 * REST_ARGS means to absorb the rest of the args.
1998 * Return nonzero to indicate a syntax error.
2001 static enum cpp_token
2002 macarg (pfile, rest_args)
2007 enum cpp_token token;
2008 char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
2009 CPP_OPTIONS (pfile)->put_out_comments = 0;
2011 /* Try to parse as much of the argument as exists at this
2012 input stack level. */
2013 pfile->no_macro_expand++;
2016 token = cpp_get_token (pfile);
2022 /* If we've hit end of file, it's an error (reported by caller).
2023 Ditto if it's the end of cpp_expand_to_buffer text.
2024 If we've hit end of macro, just continue. */
2025 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2036 /* if we've returned to lowest level and
2037 we aren't absorbing all args */
2038 if (paren == 0 && rest_args == 0)
2042 /* Remove ',' or ')' from argument buffer. */
2043 CPP_ADJUST_WRITTEN (pfile, -1);
2050 CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
2051 pfile->no_macro_expand--;
2056 /* Turn newlines to spaces in the string of length LENGTH at START,
2057 except inside of string constants.
2058 The string is copied into itself with its beginning staying fixed. */
2061 change_newlines (start, length)
2065 register U_CHAR *ibp;
2066 register U_CHAR *obp;
2067 register U_CHAR *limit;
2071 limit = start + length;
2074 while (ibp < limit) {
2075 *obp++ = c = *ibp++;
2080 /* Notice and skip strings, so that we don't delete newlines in them. */
2083 while (ibp < limit) {
2084 *obp++ = c = *ibp++;
2087 if (c == '\n' && quotec == '\'')
2103 if (!pfile->timebuf) {
2104 time_t t = time ((time_t *) 0);
2105 pfile->timebuf = localtime (&t);
2107 return pfile->timebuf;
2110 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2111 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2115 * expand things like __FILE__. Place the expansion into the output
2116 * buffer *without* rescanning.
2120 special_symbol (hp, pfile)
2127 cpp_buffer *ip = NULL;
2130 int paren = 0; /* For special `defined' keyword */
2133 if (pcp_outfile && pcp_inside_if
2134 && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2136 "Predefined macro `%s' used inside `#if' during precompilation",
2140 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2142 if (ip == CPP_NULL_BUFFER (pfile))
2144 cpp_error (pfile, "cccp error: not in any file?!");
2145 return; /* the show must go on */
2147 if (ip->fname != NULL)
2157 if (hp->type == T_BASE_FILE)
2159 while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2160 ip = CPP_PREV_BUFFER (ip);
2162 string = ip->nominal_fname;
2166 CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2167 quote_string (pfile, string);
2171 case T_INCLUDE_LEVEL:
2173 ip = CPP_BUFFER (pfile);
2174 for (; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2175 if (ip->fname != NULL)
2178 buf = (char *) alloca (8); /* Eight bytes ought to be more than enough */
2179 sprintf (buf, "%d", true_indepth - 1);
2183 buf = (char *) alloca (3 + strlen (version_string));
2184 sprintf (buf, "\"%s\"", version_string);
2187 #ifndef NO_BUILTIN_SIZE_TYPE
2193 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2194 case T_PTRDIFF_TYPE:
2200 buf = CPP_WCHAR_TYPE (pfile);
2203 case T_USER_LABEL_PREFIX_TYPE:
2204 buf = user_label_prefix;
2207 case T_REGISTER_PREFIX_TYPE:
2208 buf = REGISTER_PREFIX;
2212 buf = (char *) alloca (4 * sizeof (int));
2213 sprintf (buf, "%d", hp->value.ival);
2214 #ifdef STDC_0_IN_SYSTEM_HEADERS
2215 if (ip->system_header_p
2216 && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2217 && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2221 if (pcp_inside_if && pcp_outfile)
2222 /* Output a precondition for this macro use */
2223 fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2229 long line = ip->lineno;
2230 long col = ip->colno;
2231 adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2233 buf = (char *) alloca (10);
2234 sprintf (buf, "%ld", line);
2240 buf = (char *) alloca (20);
2241 timebuf = timestamp (pfile);
2242 if (hp->type == T_DATE)
2243 sprintf (buf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2244 timebuf->tm_mday, timebuf->tm_year + 1900);
2246 sprintf (buf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2250 case T_SPEC_DEFINED:
2251 buf = " 0 "; /* Assume symbol is not defined */
2252 ip = CPP_BUFFER (pfile);
2253 SKIP_WHITE_SPACE (ip->cur);
2254 if (*ip->cur == '(')
2257 ip->cur++; /* Skip over the paren */
2258 SKIP_WHITE_SPACE (ip->cur);
2261 if (!is_idstart[*ip->cur])
2263 if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2265 if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2268 if (pcp_outfile && pcp_inside_if
2269 && (hp->type == T_CONST
2270 || (hp->type == T_MACRO && hp->value.defn->predefined)))
2271 /* Output a precondition for this macro use. */
2272 fprintf (pcp_outfile, "#define %s\n", hp->name);
2278 if (pcp_outfile && pcp_inside_if)
2280 /* Output a precondition for this macro use */
2281 U_CHAR *cp = ip->bufp;
2282 fprintf (pcp_outfile, "#undef ");
2283 while (is_idchar[*cp]) /* Ick! */
2284 fputc (*cp++, pcp_outfile);
2285 putc ('\n', pcp_outfile);
2288 while (is_idchar[*ip->cur])
2290 SKIP_WHITE_SPACE (ip->cur);
2293 if (*ip->cur != ')')
2301 cpp_error (pfile, "`defined' without an identifier");
2305 cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2309 CPP_RESERVE (pfile, len + 1);
2310 CPP_PUTS_Q (pfile, buf, len);
2311 CPP_NUL_TERMINATE_Q (pfile);
2316 /* Write out a #define command for the special named MACRO_NAME
2317 to PFILE's token_buffer. */
2320 dump_special_to_buffer (pfile, macro_name)
2324 static char define_directive[] = "#define ";
2325 int macro_name_length = strlen (macro_name);
2326 output_line_command (pfile, 0, same_file);
2327 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2328 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2329 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2330 CPP_PUTC_Q (pfile, ' ');
2331 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2332 CPP_PUTC (pfile, '\n');
2335 /* Initialize the built-in macros. */
2338 initialize_builtins (pfile)
2341 install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2342 install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2343 install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2344 install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2345 install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2346 install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2347 #ifndef NO_BUILTIN_SIZE_TYPE
2348 install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2350 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2351 install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2353 install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2354 install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2355 install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2356 install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2357 if (!CPP_TRADITIONAL (pfile))
2358 install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2359 if (CPP_OPTIONS (pfile)->objc)
2360 install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2361 /* This is supplied using a -D by the compiler driver
2362 so that it is present only when truly compiling with GNU C. */
2363 /* install ("__GNUC__", -1, T_CONST, 2, 0, -1); */
2365 if (CPP_OPTIONS (pfile)->debug_output)
2367 dump_special_to_buffer (pfile, "__BASE_FILE__");
2368 dump_special_to_buffer (pfile, "__VERSION__");
2369 #ifndef NO_BUILTIN_SIZE_TYPE
2370 dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2372 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2373 dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2375 dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2376 dump_special_to_buffer (pfile, "__DATE__");
2377 dump_special_to_buffer (pfile, "__TIME__");
2378 if (!CPP_TRADITIONAL (pfile))
2379 dump_special_to_buffer (pfile, "__STDC__");
2380 if (CPP_OPTIONS (pfile)->objc)
2381 dump_special_to_buffer (pfile, "__OBJC__");
2385 /* Return 1 iff a token ending in C1 followed directly by a token C2
2386 could cause mis-tokenization. */
2389 unsafe_chars (c1, c2)
2395 if (c2 == c1 || c2 == '=')
2399 case '0': case '1': case '2': case '3': case '4':
2400 case '5': case '6': case '7': case '8': case '9':
2401 case 'e': case 'E': case 'p': case 'P':
2402 if (c2 == '-' || c2 == '+')
2403 return 1; /* could extend a pre-processing number */
2406 if (c2 == '\'' || c2 == '\"')
2407 return 1; /* Could turn into L"xxx" or L'xxx'. */
2411 case 'a': case 'b': case 'c': case 'd': case 'f':
2412 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2413 case 'm': case 'n': case 'o': case 'q': case 'r':
2414 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2416 case 'A': case 'B': case 'C': case 'D': case 'F':
2417 case 'G': case 'H': case 'I': case 'J': case 'K':
2418 case 'M': case 'N': case 'O': case 'Q': case 'R':
2419 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2421 /* We're in the middle of either a name or a pre-processing number. */
2422 return (is_idchar[c2] || c2 == '.');
2423 case '<': case '>': case '!': case '%': case '#': case ':':
2424 case '^': case '&': case '|': case '*': case '/': case '=':
2425 return (c2 == c1 || c2 == '=');
2430 /* Expand a macro call.
2431 HP points to the symbol that is the macro being called.
2432 Put the result of expansion onto the input stack
2433 so that subsequent input by our caller will use it.
2435 If macro wants arguments, caller has already verified that
2436 an argument list follows; arguments come from the input stack. */
2439 macroexpand (pfile, hp)
2444 DEFINITION *defn = hp->value.defn;
2445 register U_CHAR *xbuf;
2446 long start_line, start_column;
2448 struct argdata *args;
2449 long old_written = CPP_WRITTEN (pfile);
2451 int start_line = instack[indepth].lineno;
2453 int rest_args, rest_zero;
2457 CHECK_DEPTH (return;);
2461 /* This macro is being used inside a #if, which means it must be */
2462 /* recorded as a precondition. */
2463 if (pcp_inside_if && pcp_outfile && defn->predefined)
2464 dump_single_macro (hp, pcp_outfile);
2467 pfile->output_escapes++;
2468 cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2470 nargs = defn->nargs;
2474 enum cpp_token token;
2476 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2478 for (i = 0; i < nargs; i++)
2480 args[i].raw = args[i].expanded = 0;
2481 args[i].raw_length = 0;
2482 args[i].expand_length = args[i].stringified_length = -1;
2483 args[i].use_count = 0;
2486 /* Parse all the macro args that are supplied. I counts them.
2487 The first NARGS args are stored in ARGS.
2488 The rest are discarded. If rest_args is set then we assume
2489 macarg absorbed the rest of the args. */
2493 FORWARD(1); /* Discard the open-parenthesis before the first arg. */
2498 if (i < nargs || (nargs == 0 && i == 0))
2500 /* if we are working on last arg which absorbs rest of args... */
2501 if (i == nargs - 1 && defn->rest_args)
2503 args[i].raw = CPP_WRITTEN (pfile);
2504 token = macarg (pfile, rest_args);
2505 args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2506 args[i].newlines = 0; /* FIXME */
2509 token = macarg (pfile, 0);
2510 if (token == CPP_EOF || token == CPP_POP)
2512 cpp_error_with_line (pfile, start_line, start_column,
2513 "unterminated macro call");
2517 } while (token == CPP_COMMA);
2519 /* If we got one arg but it was just whitespace, call that 0 args. */
2522 register U_CHAR *bp = ARG_BASE + args[0].raw;
2523 register U_CHAR *lim = bp + args[0].raw_length;
2524 /* cpp.texi says for foo ( ) we provide one argument.
2525 However, if foo wants just 0 arguments, treat this as 0. */
2527 while (bp != lim && is_space[*bp]) bp++;
2532 /* Don't output an error message if we have already output one for
2533 a parse error above. */
2535 if (nargs == 0 && i > 0)
2537 cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2541 /* traditional C allows foo() if foo wants one argument. */
2542 if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2544 /* the rest args token is allowed to absorb 0 tokens */
2545 else if (i == nargs - 1 && defn->rest_args)
2548 cpp_error (pfile, "macro `%s' used without args", hp->name);
2550 cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2552 cpp_error (pfile, "macro `%s' used with only %d args",
2558 "macro `%s' used with too many (%d) args", hp->name, i);
2562 /* If macro wants zero args, we parsed the arglist for checking only.
2563 Read directly from the macro definition. */
2566 xbuf = defn->expansion;
2567 xbuf_len = defn->length;
2571 register U_CHAR *exp = defn->expansion;
2572 register int offset; /* offset in expansion,
2573 copied a piece at a time */
2574 register int totlen; /* total amount of exp buffer filled so far */
2576 register struct reflist *ap, *last_ap;
2578 /* Macro really takes args. Compute the expansion of this call. */
2580 /* Compute length in characters of the macro's expansion.
2581 Also count number of times each arg is used. */
2582 xbuf_len = defn->length;
2583 for (ap = defn->pattern; ap != NULL; ap = ap->next)
2587 register struct argdata *arg = &args[ap->argno];
2588 /* Stringify if it hasn't already been */
2589 if (arg->stringified_length < 0)
2591 int arglen = arg->raw_length;
2595 /* Initially need_space is -1. Otherwise, 1 means the
2596 previous character was a space, but we suppressed it;
2597 0 means the previous character was a non-space. */
2598 int need_space = -1;
2600 arg->stringified = CPP_WRITTEN (pfile);
2601 if (!CPP_TRADITIONAL (pfile))
2602 CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2603 for (; i < arglen; i++)
2605 c = (ARG_BASE + arg->raw)[i];
2609 /* Internal sequences of whitespace are replaced by
2610 one space except within an string or char token.*/
2613 if (CPP_WRITTEN (pfile) > (unsigned)arg->stringified
2614 && (CPP_PWRITTEN (pfile))[-1] == '@')
2616 /* "@ " escape markers are removed */
2617 CPP_ADJUST_WRITTEN (pfile, -1);
2620 if (need_space == 0)
2624 else if (need_space > 0)
2625 CPP_PUTC (pfile, ' ');
2640 else if (c == '\"' || c == '\'')
2644 /* Escape these chars */
2645 if (c == '\"' || (in_string && c == '\\'))
2646 CPP_PUTC (pfile, '\\');
2648 CPP_PUTC (pfile, c);
2651 CPP_RESERVE (pfile, 4);
2652 sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2654 CPP_ADJUST_WRITTEN (pfile, 4);
2657 if (!CPP_TRADITIONAL (pfile))
2658 CPP_PUTC (pfile, '\"'); /* insert ending quote */
2659 arg->stringified_length
2660 = CPP_WRITTEN (pfile) - arg->stringified;
2662 xbuf_len += args[ap->argno].stringified_length;
2664 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2665 /* Add 4 for two newline-space markers to prevent
2666 token concatenation. */
2667 xbuf_len += args[ap->argno].raw_length + 4;
2670 /* We have an ordinary (expanded) occurrence of the arg.
2671 So compute its expansion, if we have not already. */
2672 if (args[ap->argno].expand_length < 0)
2674 args[ap->argno].expanded = CPP_WRITTEN (pfile);
2675 cpp_expand_to_buffer (pfile,
2676 ARG_BASE + args[ap->argno].raw,
2677 args[ap->argno].raw_length);
2679 args[ap->argno].expand_length
2680 = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2683 /* Add 4 for two newline-space markers to prevent
2684 token concatenation. */
2685 xbuf_len += args[ap->argno].expand_length + 4;
2687 if (args[ap->argno].use_count < 10)
2688 args[ap->argno].use_count++;
2691 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2693 /* Generate in XBUF the complete expansion
2694 with arguments substituted in.
2695 TOTLEN is the total size generated so far.
2696 OFFSET is the index in the definition
2697 of where we are copying from. */
2698 offset = totlen = 0;
2699 for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2700 last_ap = ap, ap = ap->next)
2702 register struct argdata *arg = &args[ap->argno];
2703 int count_before = totlen;
2705 /* Add chars to XBUF. */
2706 for (i = 0; i < ap->nchars; i++, offset++)
2707 xbuf[totlen++] = exp[offset];
2709 /* If followed by an empty rest arg with concatenation,
2710 delete the last run of nonwhite chars. */
2711 if (rest_zero && totlen > count_before
2712 && ((ap->rest_args && ap->raw_before)
2713 || (last_ap != NULL && last_ap->rest_args
2714 && last_ap->raw_after)))
2716 /* Delete final whitespace. */
2717 while (totlen > count_before && is_space[xbuf[totlen - 1]])
2720 /* Delete the nonwhites before them. */
2721 while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2725 if (ap->stringify != 0)
2727 bcopy (ARG_BASE + arg->stringified,
2728 xbuf + totlen, arg->stringified_length);
2729 totlen += arg->stringified_length;
2731 else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2733 U_CHAR *p1 = ARG_BASE + arg->raw;
2734 U_CHAR *l1 = p1 + arg->raw_length;
2737 while (p1 != l1 && is_space[*p1]) p1++;
2738 while (p1 != l1 && is_idchar[*p1])
2739 xbuf[totlen++] = *p1++;
2743 /* Arg is concatenated after: delete trailing whitespace,
2744 whitespace markers, and no-reexpansion markers. */
2747 if (is_space[l1[-1]]) l1--;
2748 else if (l1[-1] == '@')
2750 U_CHAR *p2 = l1 - 1;
2751 /* If whitespace is preceded by an odd number
2752 of `@' signs, the last `@' was a whitespace
2753 marker; drop it too. */
2754 while (p2 != p1 && p2[0] == '@') p2--;
2759 else if (l1[-1] == '-')
2761 U_CHAR *p2 = l1 - 1;
2762 /* If a `-' is preceded by an odd number of
2763 `@' signs then it and the last `@' are
2764 a no-reexpansion marker. */
2765 while (p2 != p1 && p2[0] == '@') p2--;
2775 /* Delete any no-reexpansion marker that precedes
2776 an identifier at the beginning of the argument. */
2777 if (p1[0] == '@' && p1[1] == '-')
2780 bcopy (p1, xbuf + totlen, l1 - p1);
2785 U_CHAR *expanded = ARG_BASE + arg->expanded;
2786 if (!ap->raw_before && totlen > 0 && arg->expand_length
2787 && !CPP_TRADITIONAL(pfile)
2788 && unsafe_chars (xbuf[totlen-1], expanded[0]))
2790 xbuf[totlen++] = '@';
2791 xbuf[totlen++] = ' ';
2794 bcopy (expanded, xbuf + totlen, arg->expand_length);
2795 totlen += arg->expand_length;
2797 if (!ap->raw_after && totlen > 0 && offset < defn->length
2798 && !CPP_TRADITIONAL(pfile)
2799 && unsafe_chars (xbuf[totlen-1], exp[offset]))
2801 xbuf[totlen++] = '@';
2802 xbuf[totlen++] = ' ';
2805 /* If a macro argument with newlines is used multiple times,
2806 then only expand the newlines once. This avoids creating
2807 output lines which don't correspond to any input line,
2808 which confuses gdb and gcov. */
2809 if (arg->use_count > 1 && arg->newlines > 0)
2811 /* Don't bother doing change_newlines for subsequent
2815 = change_newlines (expanded, arg->expand_length);
2819 if (totlen > xbuf_len)
2823 /* if there is anything left of the definition
2824 after handling the arg list, copy that in too. */
2826 for (i = offset; i < defn->length; i++)
2828 /* if we've reached the end of the macro */
2831 if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2832 && last_ap->raw_after))
2833 xbuf[totlen++] = exp[i];
2841 pfile->output_escapes--;
2843 /* Now put the expansion on the input stack
2844 so our caller will commence reading from it. */
2845 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2846 CPP_BUFFER (pfile)->has_escapes = 1;
2848 /* Pop the space we've used in the token_buffer for argument expansion. */
2849 CPP_SET_WRITTEN (pfile, old_written);
2851 /* Recursive macro use sometimes works traditionally.
2852 #define foo(x,y) bar (x (y,0), y)
2855 if (!CPP_TRADITIONAL (pfile))
2856 hp->type = T_DISABLED;
2860 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2862 register U_CHAR *xbuf;
2866 register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2869 mbuf->cleanup = macro_cleanup;
2872 /* The first chars of the expansion should be a "@ " added by
2873 collect_expansion. This is to prevent accidental token-pasting
2874 between the text preceding the macro invocation, and the macro
2877 We would like to avoid adding unneeded spaces (for the sake of
2878 tools that use cpp, such as imake). In some common cases we can
2879 tell that it is safe to omit the space.
2881 The character before the macro invocation cannot have been an
2882 idchar (or else it would have been pasted with the idchars of
2883 the macro name). Therefore, if the first non-space character
2884 of the expansion is an idchar, we do not need the extra space
2885 to prevent token pasting.
2887 Also, we don't need the extra space if the first char is '(',
2888 or some other (less common) characters. */
2890 if (xbuf[0] == '@' && xbuf[1] == ' '
2891 && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
2892 || xbuf[2] == '\"'))
2896 /* Like cpp_get_token, except that it does not read past end-of-line.
2897 Also, horizontal space is skipped, and macros are popped. */
2899 static enum cpp_token
2900 get_directive_token (pfile)
2905 long old_written = CPP_WRITTEN (pfile);
2906 enum cpp_token token;
2907 cpp_skip_hspace (pfile);
2908 if (PEEKC () == '\n')
2910 token = cpp_get_token (pfile);
2914 if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2916 /* ... else fall though ... */
2917 case CPP_HSPACE: case CPP_COMMENT:
2918 CPP_SET_WRITTEN (pfile, old_written);
2926 /* Handle #include and #import.
2927 This function expects to see "fname" or <fname> on the input.
2929 The input is normally in part of the output_buffer following
2930 CPP_WRITTEN, and will get overwritten by output_line_command.
2931 I.e. in input file specification has been popped by handle_directive.
2935 do_include (pfile, keyword, unused1, unused2)
2937 struct directive *keyword;
2938 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
2940 int importing = (keyword->type == T_IMPORT);
2941 int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
2942 int angle_brackets = 0; /* 0 for "...", 1 for <...> */
2943 char *fname; /* Dynamically allocated fname buffer */
2944 char *fbeg, *fend; /* Beginning and end of fname */
2947 enum cpp_token token;
2949 /* Chain of dirs to search */
2950 struct file_name_list *search_start = CPP_OPTIONS (pfile)->include;
2951 struct file_name_list dsp; /* First in chain, if #include "..." */
2952 struct file_name_list *foundhere, *ptr;
2954 long old_written = CPP_WRITTEN (pfile);
2958 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
2961 cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
2963 cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
2966 if (importing && CPP_OPTIONS (pfile)->warn_import
2967 && !CPP_OPTIONS (pfile)->inhibit_warnings
2968 && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
2970 pfile->import_warning = 1;
2971 cpp_warning (pfile, "using `#import' is not recommended");
2972 fprintf (stderr, "The fact that a certain header file need not be processed more than once\n");
2973 fprintf (stderr, "should be indicated in the header file, not where it is used.\n");
2974 fprintf (stderr, "The best way to do this is with a conditional of this form:\n\n");
2975 fprintf (stderr, " #ifndef _FOO_H_INCLUDED\n");
2976 fprintf (stderr, " #define _FOO_H_INCLUDED\n");
2977 fprintf (stderr, " ... <real contents of file> ...\n");
2978 fprintf (stderr, " #endif /* Not _FOO_H_INCLUDED */\n\n");
2979 fprintf (stderr, "Then users can use `#include' any number of times.\n");
2980 fprintf (stderr, "GNU C automatically avoids processing the file more than once\n");
2981 fprintf (stderr, "when it is equipped with such a conditional.\n");
2984 pfile->parsing_include_directive++;
2985 token = get_directive_token (pfile);
2986 pfile->parsing_include_directive--;
2988 if (token == CPP_STRING)
2990 fbeg = pfile->token_buffer + old_written + 1;
2991 fend = CPP_PWRITTEN (pfile) - 1;
2993 if (fbeg[-1] == '<')
2996 /* If -I-, start with the first -I dir after the -I-. */
2997 if (CPP_OPTIONS (pfile)->first_bracket_include)
2998 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3000 /* If -I- was specified, don't search current dir, only spec'd ones. */
3001 else if (! CPP_OPTIONS (pfile)->ignore_srcdir)
3003 cpp_buffer *fp = CPP_BUFFER (pfile);
3004 /* We have "filename". Figure out directory this source
3005 file is coming from and put it on the front of the list. */
3007 for ( ; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3012 if ((nam = fp->nominal_fname) != NULL)
3014 /* Found a named file. Figure out dir of the file,
3015 and put it in front of the search list. */
3016 dsp.next = search_start;
3017 search_start = &dsp;
3019 ep = rindex (nam, '/');
3021 ep = rindex (nam, ']');
3022 if (ep == NULL) ep = rindex (nam, '>');
3023 if (ep == NULL) ep = rindex (nam, ':');
3024 if (ep != NULL) ep++;
3029 dsp.fname = (char *) alloca (n + 1);
3030 strncpy (dsp.fname, nam, n);
3031 dsp.fname[n] = '\0';
3032 if (n + INCLUDE_LEN_FUDGE > pfile->max_include_len)
3033 pfile->max_include_len = n + INCLUDE_LEN_FUDGE;
3037 dsp.fname = 0; /* Current directory */
3039 dsp.got_name_map = 0;
3046 else if (token == CPP_NAME)
3049 * Support '#include xyz' like VAX-C to allow for easy use of all the
3050 * decwindow include files. It defaults to '#include <xyz.h>' (so the
3051 * code from case '<' is repeated here) and generates a warning.
3054 "VAX-C-style include specification found, use '#include <filename.h>' !");
3056 /* If -I-, start with the first -I dir after the -I-. */
3057 if (CPP_OPTIONS (pfile)->first_bracket_include)
3058 search_start = CPP_OPTIONS (pfile)->first_bracket_include;
3060 /* Append the missing `.h' to the name. */
3061 CPP_PUTS (pfile, ".h", 3)
3062 CPP_NUL_TERMINATE_Q (pfile);
3064 fbeg = pfile->token_buffer + old_written;
3065 fend = CPP_PWRITTEN (pfile);
3071 "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
3072 CPP_SET_WRITTEN (pfile, old_written);
3073 skip_rest_of_line (pfile);
3077 token = get_directive_token (pfile);
3078 if (token != CPP_VSPACE)
3080 cpp_error (pfile, "junk at end of `#include'");
3081 skip_rest_of_line (pfile);
3084 /* For #include_next, skip in the search path
3085 past the dir in which the containing file was found. */
3088 cpp_buffer *fp = CPP_BUFFER (pfile);
3089 for (; fp != CPP_NULL_BUFFER (pfile); fp = CPP_PREV_BUFFER (fp))
3090 if (fp->fname != NULL)
3092 /* fp->dir is null if the containing file was specified with
3093 an absolute file name. In that case, don't skip anything. */
3094 if (fp->dir == SELF_DIR_DUMMY)
3095 search_start = CPP_OPTIONS (pfile)->include;
3097 search_start = fp->dir->next;
3102 CPP_SET_WRITTEN (pfile, old_written);
3108 cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3112 /* Allocate this permanently, because it gets stored in the definitions
3114 /* + 2 above for slash and terminating null. */
3115 fname = (char *) xmalloc (pfile->max_include_len + flen + 2);
3117 fd = find_include_file (pfile, fbeg, flen, fname,
3118 importing, search_start, &foundhere);
3128 if (CPP_OPTIONS (pfile)->print_deps_missing_files
3129 && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
3130 (pfile->system_include_depth > 0)))
3132 if (!angle_brackets)
3133 deps_output (pfile, fbeg, ' ');
3136 /* If requested as a system header, assume it belongs in
3137 the first system header directory. */
3138 if (CPP_OPTIONS (pfile)->first_bracket_include)
3139 ptr = CPP_OPTIONS (pfile)->first_bracket_include;
3141 ptr = CPP_OPTIONS (pfile)->include;
3142 for (; ptr; ptr = ptr->next)
3147 if (ptr->fname[0] == 0)
3149 p = (char *) alloca (strlen (ptr->fname)
3150 + strlen (fname) + 2);
3151 strcpy (p, ptr->fname);
3154 deps_output (pfile, p, ' ');
3159 /* If -M was specified, and this header file won't be added to
3160 the dependency list, then don't count this as an error,
3161 because we can still produce correct output. Otherwise, we
3162 can't produce correct output, because there may be
3163 dependencies we need inside the missing file, and we don't
3164 know what directory this missing file exists in. */
3165 else if (CPP_PRINT_DEPS (pfile)
3166 && (CPP_PRINT_DEPS (pfile)
3167 <= (angle_brackets || (pfile->system_include_depth > 0))))
3168 cpp_warning (pfile, "No include path in which to find %s", fname);
3169 else if (search_start)
3170 cpp_error_from_errno (pfile, fname);
3172 cpp_error (pfile, "No include path in which to find %s", fname);
3178 /* If we get here, we have a file to process. */
3180 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next)
3181 if (!strcmp (ptr->fname, fname))
3182 break; /* This file was included before. */
3186 /* This is the first time for this file. */
3187 /* Add it to list of files included. */
3189 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3190 ptr->control_macro = 0;
3191 ptr->c_system_include_path = 0;
3192 ptr->next = pfile->all_include_files;
3193 pfile->all_include_files = ptr;
3194 ptr->fname = savestring (fname);
3195 ptr->got_name_map = 0;
3197 /* For -M, add this file to the dependencies. */
3198 if (CPP_PRINT_DEPS (pfile)
3199 > (angle_brackets || (pfile->system_include_depth > 0)))
3200 deps_output (pfile, fname, ' ');
3203 /* Handle -H option. */
3204 if (CPP_OPTIONS(pfile)->print_include_names)
3206 cpp_buffer *buf = CPP_BUFFER (pfile);
3207 while ((buf = CPP_PREV_BUFFER (buf)) != CPP_NULL_BUFFER (pfile))
3209 fprintf (stderr, "%s\n", fname);
3212 /* Actually process the file */
3213 if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3221 pfile->system_include_depth++;
3223 if (finclude (pfile, fd, fname, is_system_include (pfile, fname),
3224 foundhere != &dsp ? foundhere : SELF_DIR_DUMMY))
3226 output_line_command (pfile, 0, enter_file);
3227 pfile->only_seen_white = 2;
3231 pfile->system_include_depth--;
3236 /* Return nonzero if the given FILENAME is an absolute pathname which
3237 designates a file within one of the known "system" include file
3238 directories. We assume here that if the given FILENAME looks like
3239 it is the name of a file which resides either directly in a "system"
3240 include file directory, or within any subdirectory thereof, then the
3241 given file must be a "system" include file. This function tells us
3242 if we should suppress pedantic errors/warnings for the given FILENAME.
3244 The value is 2 if the file is a C-language system header file
3245 for which C++ should (on most systems) assume `extern "C"'. */
3248 is_system_include (pfile, filename)
3250 register char *filename;
3252 struct file_name_list *searchptr;
3254 for (searchptr = CPP_OPTIONS (pfile)->first_system_include; searchptr;
3255 searchptr = searchptr->next)
3256 if (searchptr->fname) {
3257 register char *sys_dir = searchptr->fname;
3258 register unsigned length = strlen (sys_dir);
3260 if (! strncmp (sys_dir, filename, length) && filename[length] == '/')
3262 if (searchptr->c_system_include_path)
3272 * Install a name in the assertion hash table.
3274 * If LEN is >= 0, it is the length of the name.
3275 * Otherwise, compute the length by scanning the entire name.
3277 * If HASH is >= 0, it is the precomputed hash code.
3278 * Otherwise, compute the hash code.
3281 static ASSERTION_HASHNODE *
3282 assertion_install (pfile, name, len, hash)
3288 register ASSERTION_HASHNODE *hp;
3289 register int i, bucket;
3290 register U_CHAR *p, *q;
3292 i = sizeof (ASSERTION_HASHNODE) + len + 1;
3293 hp = (ASSERTION_HASHNODE *) xmalloc (i);
3295 hp->bucket_hdr = &pfile->assertion_hashtab[bucket];
3296 hp->next = pfile->assertion_hashtab[bucket];
3297 pfile->assertion_hashtab[bucket] = hp;
3299 if (hp->next != NULL)
3300 hp->next->prev = hp;
3303 hp->name = ((U_CHAR *) hp) + sizeof (ASSERTION_HASHNODE);
3306 for (i = 0; i < len; i++)
3312 * find the most recent hash node for name "name" (ending with first
3313 * non-identifier char) installed by install
3315 * If LEN is >= 0, it is the length of the name.
3316 * Otherwise, compute the length by scanning the entire name.
3318 * If HASH is >= 0, it is the precomputed hash code.
3319 * Otherwise, compute the hash code.
3322 static ASSERTION_HASHNODE *
3323 assertion_lookup (pfile, name, len, hash)
3329 register ASSERTION_HASHNODE *bucket;
3331 bucket = pfile->assertion_hashtab[hash];
3333 if (bucket->length == len && strncmp (bucket->name, name, len) == 0)
3335 bucket = bucket->next;
3341 delete_assertion (hp)
3342 ASSERTION_HASHNODE *hp;
3344 struct tokenlist_list *tail;
3345 if (hp->prev != NULL)
3346 hp->prev->next = hp->next;
3347 if (hp->next != NULL)
3348 hp->next->prev = hp->prev;
3350 for (tail = hp->value; tail; )
3352 struct tokenlist_list *next = tail->next;
3353 free_token_list (tail->tokens);
3358 /* Make sure that the bucket chain header that
3359 the deleted guy was on points to the right thing afterwards. */
3360 if (hp == *hp->bucket_hdr)
3361 *hp->bucket_hdr = hp->next;
3366 /* Convert a character string literal into a nul-terminated string.
3367 The input string is [IN ... LIMIT).
3368 The result is placed in RESULT. RESULT can be the same as IN.
3369 The value returned in the end of the string written to RESULT,
3370 or NULL on error. */
3373 convert_string (pfile, result, in, limit, handle_escapes)
3375 register U_CHAR *result, *in, *limit;
3395 char *bpc = (char *) in;
3396 int i = (U_CHAR) cpp_parse_escape (pfile, &bpc);
3397 in = (U_CHAR *) bpc;
3399 *result++ = (U_CHAR)c;
3402 /* else fall through */
3412 * interpret #line command. Remembers previously seen fnames
3413 * in its very own hash table.
3415 #define FNAME_HASHSIZE 37
3418 do_line (pfile, keyword, unused1, unused2)
3420 struct directive *keyword ATTRIBUTE_UNUSED;
3421 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3423 cpp_buffer *ip = CPP_BUFFER (pfile);
3425 long old_written = CPP_WRITTEN (pfile);
3426 enum file_change_code file_change = same_file;
3427 enum cpp_token token;
3429 token = get_directive_token (pfile);
3431 if (token != CPP_NUMBER
3432 || !ISDIGIT(pfile->token_buffer[old_written]))
3434 cpp_error (pfile, "invalid format `#line' command");
3435 goto bad_line_directive;
3438 /* The Newline at the end of this line remains to be processed.
3439 To put the next line at the specified line number,
3440 we must store a line number now that is one less. */
3441 new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3442 CPP_SET_WRITTEN (pfile, old_written);
3444 /* NEW_LINENO is one less than the actual line number here. */
3445 if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3446 cpp_pedwarn (pfile, "line number out of range in `#line' command");
3448 #if 0 /* #line 10"foo.c" is supposed to be allowed. */
3449 if (PEEKC() && !is_space[PEEKC()]) {
3450 cpp_error (pfile, "invalid format `#line' command");
3451 goto bad_line_directive;
3455 token = get_directive_token (pfile);
3457 if (token == CPP_STRING) {
3458 U_CHAR *fname = pfile->token_buffer + old_written;
3460 static HASHNODE *fname_table[FNAME_HASHSIZE];
3461 HASHNODE *hp, **hash_bucket;
3466 /* Turn the file name, which is a character string literal,
3467 into a null-terminated string. Do this in place. */
3468 end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3469 if (end_name == NULL)
3471 cpp_error (pfile, "invalid format `#line' command");
3472 goto bad_line_directive;
3475 fname_length = end_name - fname;
3477 num_start = CPP_WRITTEN (pfile);
3478 token = get_directive_token (pfile);
3479 if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3480 p = pfile->token_buffer + num_start;
3481 if (CPP_PEDANTIC (pfile))
3482 cpp_pedwarn (pfile, "garbage at end of `#line' command");
3484 if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3486 cpp_error (pfile, "invalid format `#line' command");
3487 goto bad_line_directive;
3490 file_change = enter_file;
3492 file_change = leave_file;
3494 ip->system_header_p = 1;
3495 else /* if (*p == '4') */
3496 ip->system_header_p = 2;
3498 CPP_SET_WRITTEN (pfile, num_start);
3499 token = get_directive_token (pfile);
3500 p = pfile->token_buffer + num_start;
3501 if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3502 ip->system_header_p = *p == '3' ? 1 : 2;
3503 token = get_directive_token (pfile);
3505 if (token != CPP_VSPACE) {
3506 cpp_error (pfile, "invalid format `#line' command");
3507 goto bad_line_directive;
3511 hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3512 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3513 if (hp->length == fname_length
3514 && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3515 ip->nominal_fname = hp->value.cpval;
3519 /* Didn't find it; cons up a new one. */
3520 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3521 hp->next = *hash_bucket;
3524 hp->length = fname_length;
3525 ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3526 bcopy (fname, hp->value.cpval, fname_length);
3529 else if (token != CPP_VSPACE && token != CPP_EOF) {
3530 cpp_error (pfile, "invalid format `#line' command");
3531 goto bad_line_directive;
3534 ip->lineno = new_lineno;
3536 skip_rest_of_line (pfile);
3537 CPP_SET_WRITTEN (pfile, old_written);
3538 output_line_command (pfile, 0, file_change);
3543 * remove the definition of a symbol from the symbol table.
3544 * according to un*x /lib/cpp, it is not an error to undef
3545 * something that has no definitions, so it isn't one here either.
3549 do_undef (pfile, keyword, buf, limit)
3551 struct directive *keyword;
3552 U_CHAR *buf, *limit;
3556 U_CHAR *orig_buf = buf;
3559 /* If this is a precompiler run (with -pcp) pass thru #undef commands. */
3560 if (pcp_outfile && keyword)
3561 pass_thru_directive (buf, limit, pfile, keyword);
3564 SKIP_WHITE_SPACE (buf);
3565 sym_length = check_macro_name (pfile, buf, "macro");
3567 while ((hp = cpp_lookup (pfile, buf, sym_length, -1)) != NULL)
3569 /* If we are generating additional info for debugging (with -g) we
3570 need to pass through all effective #undef commands. */
3571 if (CPP_OPTIONS (pfile)->debug_output && keyword)
3572 pass_thru_directive (orig_buf, limit, pfile, keyword);
3573 if (hp->type != T_MACRO)
3574 cpp_warning (pfile, "undefining `%s'", hp->name);
3578 if (CPP_PEDANTIC (pfile)) {
3580 SKIP_WHITE_SPACE (buf);
3582 cpp_pedwarn (pfile, "garbage after `#undef' directive");
3588 * Report an error detected by the program we are processing.
3589 * Use the text of the line in the error message.
3590 * (We use error because it prints the filename & line#.)
3594 do_error (pfile, keyword, buf, limit)
3596 struct directive *keyword ATTRIBUTE_UNUSED;
3597 U_CHAR *buf, *limit;
3599 int length = limit - buf;
3600 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3601 bcopy (buf, copy, length);
3603 SKIP_WHITE_SPACE (copy);
3604 cpp_error (pfile, "#error %s", copy);
3609 * Report a warning detected by the program we are processing.
3610 * Use the text of the line in the warning message, then continue.
3611 * (We use error because it prints the filename & line#.)
3615 do_warning (pfile, keyword, buf, limit)
3617 struct directive *keyword ATTRIBUTE_UNUSED;
3618 U_CHAR *buf, *limit;
3620 int length = limit - buf;
3621 U_CHAR *copy = (U_CHAR *) alloca (length + 1);
3622 bcopy (buf, copy, length);
3624 SKIP_WHITE_SPACE (copy);
3626 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3627 cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3629 /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3630 if -pedantic-errors is given, #warning should cause an error. */
3631 cpp_pedwarn (pfile, "#warning %s", copy);
3635 /* Report program identification. */
3638 do_ident (pfile, keyword, buf, limit)
3640 struct directive *keyword ATTRIBUTE_UNUSED;
3641 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3643 /* long old_written = CPP_WRITTEN (pfile);*/
3645 /* Allow #ident in system headers, since that's not user's fault. */
3646 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3647 cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3649 /* Leave rest of line to be read by later calls to cpp_get_token. */
3654 /* #pragma and its argument line have already been copied to the output file.
3655 Just check for some recognized pragmas that need validation here. */
3658 do_pragma (pfile, keyword, buf, limit)
3660 struct directive *keyword ATTRIBUTE_UNUSED;
3661 U_CHAR *buf, *limit ATTRIBUTE_UNUSED;
3663 while (*buf == ' ' || *buf == '\t')
3665 if (!strncmp (buf, "once", 4))
3667 cpp_buffer *ip = NULL;
3668 struct file_name_list *new;
3670 /* Allow #pragma once in system headers, since that's not the user's
3672 if (!CPP_BUFFER (pfile)->system_header_p)
3673 cpp_warning (pfile, "`#pragma once' is obsolete");
3675 for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3677 if (ip == CPP_NULL_BUFFER (pfile))
3679 if (ip->fname != NULL)
3683 new = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
3684 new->next = pfile->dont_repeat_files;
3685 new->fname = savestring (ip->fname);
3686 new->control_macro = 0;
3687 new->got_name_map = 0;
3688 new->c_system_include_path = 0;
3689 pfile->dont_repeat_files = new;
3692 if (!strncmp (buf, "implementation", 14)) {
3693 /* Be quiet about `#pragma implementation' for a file only if it hasn't
3694 been included yet. */
3695 struct file_name_list *ptr;
3696 U_CHAR *p = buf + 14, *fname, *inc_fname;
3698 SKIP_WHITE_SPACE (p);
3699 if (*p == '\n' || *p != '\"')
3703 p = (U_CHAR *) index (fname, '\"');
3704 fname_len = p != NULL ? p - fname : strlen (fname);
3706 for (ptr = pfile->all_include_files; ptr; ptr = ptr->next) {
3707 inc_fname = (U_CHAR *) rindex (ptr->fname, '/');
3708 inc_fname = inc_fname ? inc_fname + 1 : (U_CHAR *) ptr->fname;
3709 if (inc_fname && !strncmp (inc_fname, fname, fname_len))
3711 "`#pragma implementation' for `%s' appears after file is included",
3720 /* This was a fun hack, but #pragma seems to start to be useful.
3721 By failing to recognize it, we pass it through unchanged to cc1. */
3724 * the behavior of the #pragma directive is implementation defined.
3725 * this implementation defines it as follows.
3732 if (open ("/dev/tty", O_RDONLY, 0666) != 0)
3735 if (open ("/dev/tty", O_WRONLY, 0666) != 1)
3737 execl ("/usr/games/hack", "#pragma", 0);
3738 execl ("/usr/games/rogue", "#pragma", 0);
3739 execl ("/usr/new/emacs", "-f", "hanoi", "9", "-kill", 0);
3740 execl ("/usr/local/emacs", "-f", "hanoi", "9", "-kill", 0);
3742 fatal ("You are in a maze of twisty compiler features, all different");
3746 #ifdef SCCS_DIRECTIVE
3747 /* Just ignore #sccs, on systems where we define it at all. */
3750 do_sccs (pfile, keyword, buf, limit)
3752 struct directive *keyword ATTRIBUTE_UNUSED;
3753 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
3755 if (CPP_PEDANTIC (pfile))
3756 cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
3762 * handle #if command by
3763 * 1) inserting special `defined' keyword into the hash table
3764 * that gets turned into 0 or 1 by special_symbol (thus,
3765 * if the luser has a symbol called `defined' already, it won't
3766 * work inside the #if command)
3767 * 2) rescan the input into a temporary output buffer
3768 * 3) pass the output buffer to the yacc parser and collect a value
3769 * 4) clean up the mess left from steps 1 and 2.
3770 * 5) call conditional_skip to skip til the next #endif (etc.),
3771 * or not, depending on the value from step 3.
3775 do_if (pfile, keyword, buf, limit)
3777 struct directive *keyword ATTRIBUTE_UNUSED;
3778 U_CHAR *buf, *limit;
3780 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
3781 conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
3786 * handle a #elif directive by not changing if_stack either.
3787 * see the comment above do_else.
3791 do_elif (pfile, keyword, buf, limit)
3793 struct directive *keyword ATTRIBUTE_UNUSED;
3794 U_CHAR *buf, *limit;
3796 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3797 cpp_error (pfile, "`#elif' not within a conditional");
3800 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3801 cpp_error (pfile, "`#elif' after `#else'");
3803 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3805 if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
3806 && strcmp (pfile->if_stack->fname,
3807 CPP_BUFFER (pfile)->nominal_fname) != 0)
3808 fprintf (stderr, ", file %s", pfile->if_stack->fname);
3809 fprintf (stderr, ")\n");
3811 pfile->if_stack->type = T_ELIF;
3814 if (pfile->if_stack->if_succeeded)
3815 skip_if_group (pfile, 0);
3817 HOST_WIDE_INT value = eval_if_expression (pfile, buf, limit - buf);
3819 skip_if_group (pfile, 0);
3821 ++pfile->if_stack->if_succeeded; /* continue processing input */
3822 output_line_command (pfile, 1, same_file);
3829 * evaluate a #if expression in BUF, of length LENGTH,
3830 * then parse the result as a C expression and return the value as an int.
3833 static HOST_WIDE_INT
3834 eval_if_expression (pfile, buf, length)
3836 U_CHAR *buf ATTRIBUTE_UNUSED;
3837 int length ATTRIBUTE_UNUSED;
3839 HASHNODE *save_defined;
3840 HOST_WIDE_INT value;
3841 long old_written = CPP_WRITTEN (pfile);
3843 save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
3844 pfile->pcp_inside_if = 1;
3846 value = cpp_parse_expr (pfile);
3847 pfile->pcp_inside_if = 0;
3848 delete_macro (save_defined); /* clean up special symbol */
3850 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3856 * routine to handle ifdef/ifndef. Try to look up the symbol,
3857 * then do or don't skip to the #endif/#else/#elif depending
3858 * on what directive is actually being processed.
3862 do_xifdef (pfile, keyword, unused1, unused2)
3864 struct directive *keyword;
3865 U_CHAR *unused1 ATTRIBUTE_UNUSED, *unused2 ATTRIBUTE_UNUSED;
3868 cpp_buffer *ip = CPP_BUFFER (pfile);
3871 enum cpp_token token;
3872 int start_of_file = 0;
3873 U_CHAR *control_macro = 0;
3874 int old_written = CPP_WRITTEN (pfile);
3876 /* Detect a #ifndef at start of file (not counting comments). */
3877 if (ip->fname != 0 && keyword->type == T_IFNDEF)
3878 start_of_file = pfile->only_seen_white == 2;
3880 pfile->no_macro_expand++;
3881 token = get_directive_token (pfile);
3882 pfile->no_macro_expand--;
3884 ident = pfile->token_buffer + old_written;
3885 ident_length = CPP_WRITTEN (pfile) - old_written;
3886 CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3888 if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
3890 skip = (keyword->type == T_IFDEF);
3891 if (! CPP_TRADITIONAL (pfile))
3892 cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
3894 else if (token == CPP_NAME)
3896 HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
3897 skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
3898 if (start_of_file && !skip)
3900 control_macro = (U_CHAR *) xmalloc (ident_length + 1);
3901 bcopy (ident, control_macro, ident_length + 1);
3906 skip = (keyword->type == T_IFDEF);
3907 if (! CPP_TRADITIONAL (pfile))
3908 cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
3911 if (!CPP_TRADITIONAL (pfile))
3913 cpp_skip_hspace (pfile);
3915 if (c != EOF && c != '\n')
3916 cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
3918 skip_rest_of_line (pfile);
3922 /* Output a precondition for this macro. */
3923 if (hp && hp->value.defn->predefined)
3924 fprintf (pcp_outfile, "#define %s\n", hp->name);
3927 fprintf (pcp_outfile, "#undef ");
3928 while (is_idchar[*cp]) /* Ick! */
3929 fputc (*cp++, pcp_outfile);
3930 putc ('\n', pcp_outfile);
3934 conditional_skip (pfile, skip, T_IF, control_macro);
3938 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3939 If this is a #ifndef starting at the beginning of a file,
3940 CONTROL_MACRO is the macro name tested by the #ifndef.
3941 Otherwise, CONTROL_MACRO is 0. */
3944 conditional_skip (pfile, skip, type, control_macro)
3947 enum node_type type;
3948 U_CHAR *control_macro;
3950 IF_STACK_FRAME *temp;
3952 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3953 temp->fname = CPP_BUFFER (pfile)->nominal_fname;
3955 temp->lineno = CPP_BUFFER (pfile)->lineno;
3957 temp->next = pfile->if_stack;
3958 temp->control_macro = control_macro;
3959 pfile->if_stack = temp;
3961 pfile->if_stack->type = type;
3964 skip_if_group (pfile, 0);
3967 ++pfile->if_stack->if_succeeded;
3968 output_line_command (pfile, 1, same_file);
3973 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3974 * leaves input ptr at the sharp sign found.
3975 * If ANY is nonzero, return at next directive of any sort.
3979 skip_if_group (pfile, any)
3984 struct directive *kt;
3985 IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
3987 U_CHAR *beg_of_line = bp;
3989 register int ident_length;
3991 struct parse_marker line_start_mark;
3993 parse_set_mark (&line_start_mark, pfile);
3995 if (CPP_OPTIONS (pfile)->output_conditionals) {
3996 static char failed[] = "#failed\n";
3997 CPP_PUTS (pfile, failed, sizeof(failed)-1);
3999 output_line_command (pfile, 1, same_file);
4003 if (CPP_OPTIONS (pfile)->output_conditionals)
4005 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4006 U_CHAR *start_line = pbuf->buf + line_start_mark.position;
4007 CPP_PUTS (pfile, start_line, pbuf->cur - start_line);
4009 parse_move_mark (&line_start_mark, pfile);
4010 if (!CPP_TRADITIONAL (pfile))
4011 cpp_skip_hspace (pfile);
4015 int old_written = CPP_WRITTEN (pfile);
4016 cpp_skip_hspace (pfile);
4018 parse_name (pfile, GETC());
4019 ident_length = CPP_WRITTEN (pfile) - old_written;
4020 ident = pfile->token_buffer + old_written;
4021 pfile->limit = ident;
4023 if (ident_length == 0)
4024 goto not_a_directive;
4026 /* Handle # followed by a line number. */
4028 /* Avoid error for `###' and similar cases unless -pedantic. */
4031 for (kt = directive_table; kt->length >= 0; kt++)
4033 IF_STACK_FRAME *temp;
4034 if (ident_length == kt->length
4035 && strncmp (ident, kt->name, kt->length) == 0)
4037 /* If we are asked to return on next directive, do so now. */
4047 = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
4048 temp->next = pfile->if_stack;
4049 pfile->if_stack = temp;
4051 temp->lineno = CPP_BUFFER(pfile)->lineno;
4053 temp->fname = CPP_BUFFER(pfile)->nominal_fname;
4054 temp->type = kt->type;
4058 if (CPP_PEDANTIC (pfile) && pfile->if_stack != save_if_stack)
4059 validate_else (pfile,
4060 kt->type == T_ELSE ? "#else" : "#endif");
4062 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4065 "`#%s' not within a conditional", kt->name);
4068 else if (pfile->if_stack == save_if_stack)
4069 goto done; /* found what we came for */
4071 if (kt->type != T_ENDIF)
4073 if (pfile->if_stack->type == T_ELSE)
4074 cpp_error (pfile, "`#else' or `#elif' after `#else'");
4075 pfile->if_stack->type = kt->type;
4079 temp = pfile->if_stack;
4080 pfile->if_stack = temp->next;
4087 /* Don't let erroneous code go by. */
4088 if (kt->length < 0 && !CPP_OPTIONS (pfile)->lang_asm
4089 && CPP_PEDANTIC (pfile))
4090 cpp_pedwarn (pfile, "invalid preprocessor directive name");
4094 /* We're in the middle of a line. Skip the rest of it. */
4101 case '/': /* possible comment */
4102 c = skip_comment (pfile, NULL);
4109 old = CPP_WRITTEN (pfile);
4110 cpp_get_token (pfile);
4111 CPP_SET_WRITTEN (pfile, old);
4114 /* Char after backslash loses its special meaning. */
4115 if (PEEKC() == '\n')
4125 if (CPP_OPTIONS (pfile)->output_conditionals) {
4126 static char end_failed[] = "#endfailed\n";
4127 CPP_PUTS (pfile, end_failed, sizeof(end_failed)-1);
4130 pfile->only_seen_white = 1;
4131 parse_goto_mark (&line_start_mark, pfile);
4132 parse_clear_mark (&line_start_mark);
4136 * handle a #else directive. Do this by just continuing processing
4137 * without changing if_stack ; this is so that the error message
4138 * for missing #endif's etc. will point to the original #if. It
4139 * is possible that something different would be better.
4143 do_else (pfile, keyword, buf, limit)
4145 struct directive *keyword ATTRIBUTE_UNUSED;
4146 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4148 cpp_buffer *ip = CPP_BUFFER (pfile);
4150 if (CPP_PEDANTIC (pfile))
4151 validate_else (pfile, "#else");
4152 skip_rest_of_line (pfile);
4154 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
4155 cpp_error (pfile, "`#else' not within a conditional");
4158 /* #ifndef can't have its special treatment for containing the whole file
4159 if it has a #else clause. */
4160 pfile->if_stack->control_macro = 0;
4162 if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
4163 cpp_error (pfile, "`#else' after `#else'");
4164 fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
4165 if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
4166 fprintf (stderr, ", file %s", pfile->if_stack->fname);
4167 fprintf (stderr, ")\n");
4169 pfile->if_stack->type = T_ELSE;
4172 if (pfile->if_stack->if_succeeded)
4173 skip_if_group (pfile, 0);
4175 ++pfile->if_stack->if_succeeded; /* continue processing input */
4176 output_line_command (pfile, 1, same_file);
4182 * unstack after #endif command
4186 do_endif (pfile, keyword, buf, limit)
4188 struct directive *keyword ATTRIBUTE_UNUSED;
4189 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
4191 if (CPP_PEDANTIC (pfile))
4192 validate_else (pfile, "#endif");
4193 skip_rest_of_line (pfile);
4195 if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
4196 cpp_error (pfile, "unbalanced `#endif'");
4199 IF_STACK_FRAME *temp = pfile->if_stack;
4200 pfile->if_stack = temp->next;
4201 if (temp->control_macro != 0)
4203 /* This #endif matched a #ifndef at the start of the file.
4204 See if it is at the end of the file. */
4205 struct parse_marker start_mark;
4208 parse_set_mark (&start_mark, pfile);
4212 cpp_skip_hspace (pfile);
4217 parse_goto_mark (&start_mark, pfile);
4218 parse_clear_mark (&start_mark);
4222 /* If we get here, this #endif ends a #ifndef
4223 that contains all of the file (aside from whitespace).
4224 Arrange not to include the file again
4225 if the macro that was tested is defined.
4227 Do not do this for the top-level file in a -include or any
4228 file in a -imacros. */
4232 && ! (indepth == 1 && pfile->no_record_file)
4233 && ! (pfile->no_record_file && no_output))
4236 struct file_name_list *ifile = pfile->all_include_files;
4238 for ( ; ifile != NULL; ifile = ifile->next)
4240 if (!strcmp (ifile->fname, CPP_BUFFER (pfile)->fname))
4242 ifile->control_macro = temp->control_macro;
4250 output_line_command (pfile, 1, same_file);
4255 /* When an #else or #endif is found while skipping failed conditional,
4256 if -pedantic was specified, this is called to warn about text after
4257 the command name. P points to the first char after the command name. */
4260 validate_else (pfile, directive)
4265 cpp_skip_hspace (pfile);
4267 if (c != EOF && c != '\n')
4269 "text following `%s' violates ANSI standard", directive);
4272 /* Get the next token, and add it to the text in pfile->token_buffer.
4273 Return the kind of token we got. */
4276 cpp_get_token (pfile)
4279 register int c, c2, c3;
4281 long start_line, start_column;
4282 enum cpp_token token;
4283 struct cpp_options *opts = CPP_OPTIONS (pfile);
4284 CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4290 if (CPP_BUFFER (pfile)->seen_eof)
4292 if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4299 cpp_buffer *next_buf
4300 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4301 CPP_BUFFER (pfile)->seen_eof = 1;
4302 if (CPP_BUFFER (pfile)->nominal_fname
4303 && next_buf != CPP_NULL_BUFFER (pfile))
4305 /* We're about to return from an #include file.
4306 Emit #line information now (as part of the CPP_POP) result.
4307 But the #line refers to the file we will pop to. */
4308 cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4309 CPP_BUFFER (pfile) = next_buf;
4310 pfile->input_stack_listing_current = 0;
4311 output_line_command (pfile, 0, leave_file);
4312 CPP_BUFFER (pfile) = cur_buffer;
4322 struct parse_marker start_mark;
4324 if (PEEKC () == '=')
4326 if (opts->put_out_comments)
4327 parse_set_mark (&start_mark, pfile);
4329 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4330 &start_line, &start_column);
4331 c = skip_comment (pfile, &newlines);
4332 if (opts->put_out_comments && (c == '/' || c == EOF))
4333 parse_clear_mark (&start_mark);
4338 cpp_error_with_line (pfile, start_line, start_column,
4339 "unterminated comment");
4342 c = '/'; /* Initial letter of comment. */
4344 /* Comments are equivalent to spaces.
4345 For -traditional, a comment is equivalent to nothing. */
4346 if (opts->put_out_comments)
4348 cpp_buffer *pbuf = CPP_BUFFER (pfile);
4349 U_CHAR *start = pbuf->buf + start_mark.position;
4350 int len = pbuf->cur - start;
4351 CPP_RESERVE(pfile, 1 + len);
4352 CPP_PUTC_Q (pfile, c);
4353 CPP_PUTS_Q (pfile, start, len);
4354 pfile->lineno += newlines;
4355 parse_clear_mark (&start_mark);
4358 else if (CPP_TRADITIONAL (pfile))
4365 /* This may not work if cpp_get_token is called recursively,
4366 since many places look for horizontal space. */
4369 /* Copy the newlines into the output buffer, in order to
4370 avoid the pain of a #line every time a multiline comment
4372 CPP_RESERVE(pfile, newlines);
4373 while (--newlines >= 0)
4375 CPP_PUTC_Q (pfile, '\n');
4381 CPP_RESERVE(pfile, 1);
4382 CPP_PUTC_Q (pfile, ' ');
4386 if (opts->for_lint) {
4389 char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4391 if (lintcmd != NULL) {
4392 /* I believe it is always safe to emit this newline: */
4394 bcopy ("#pragma lint ", (char *) obp, 13);
4396 bcopy (lintcmd, (char *) obp, cmdlen);
4401 bcopy (argbp, (char *) obp, arglen);
4405 /* OK, now bring us back to the state we were in before we entered
4406 this branch. We need #line because the newline for the pragma
4407 could mess things up. */
4408 output_line_command (pfile, 0, same_file);
4409 *(obp++) = ' '; /* just in case, if comments are copied thru */
4417 /* If this is expanding a macro definition, don't recognize
4418 preprocessor directives. */
4421 /* If this is expand_into_temp_buffer, recognize them
4422 only after an actual newline at this level,
4423 not at the beginning of the input level. */
4424 if (ip->fname == 0 && beg_of_line == ip->buf)
4430 if (!pfile->only_seen_white)
4432 if (handle_directive (pfile))
4433 return CPP_DIRECTIVE;
4434 pfile->only_seen_white = 0;
4439 /* A single quoted string is treated like a double -- some
4440 programs (e.g., troff) are perverse this way */
4441 cpp_buf_line_and_col (cpp_file_buffer (pfile),
4442 &start_line, &start_column);
4443 old_written = CPP_WRITTEN (pfile);
4445 CPP_PUTC (pfile, c);
4451 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4453 /* try harder: this string crosses a macro expansion
4454 boundary. This can happen naturally if -traditional.
4455 Otherwise, only -D can make a macro with an unmatched
4457 cpp_buffer *next_buf
4458 = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4459 (*CPP_BUFFER (pfile)->cleanup)
4460 (CPP_BUFFER (pfile), pfile);
4461 CPP_BUFFER (pfile) = next_buf;
4464 if (!CPP_TRADITIONAL (pfile))
4466 cpp_error_with_line (pfile, start_line, start_column,
4467 "unterminated string or character constant");
4468 if (pfile->multiline_string_line != start_line
4469 && pfile->multiline_string_line != 0)
4470 cpp_error_with_line (pfile,
4471 pfile->multiline_string_line, -1,
4472 "possible real start of unterminated constant");
4473 pfile->multiline_string_line = 0;
4477 CPP_PUTC (pfile, cc);
4481 /* Traditionally, end of line ends a string constant with
4482 no error. So exit the loop and record the new line. */
4483 if (CPP_TRADITIONAL (pfile))
4487 cpp_error_with_line (pfile, start_line, start_column,
4488 "unterminated character constant");
4491 if (CPP_PEDANTIC (pfile)
4492 && pfile->multiline_string_line == 0)
4494 cpp_pedwarn_with_line (pfile, start_line, start_column,
4495 "string constant runs past end of line");
4497 if (pfile->multiline_string_line == 0)
4498 pfile->multiline_string_line = start_line;
4505 /* Backslash newline is replaced by nothing at all. */
4506 CPP_ADJUST_WRITTEN (pfile, -1);
4511 /* ANSI stupidly requires that in \\ the second \
4512 is *not* prevented from combining with a newline. */
4515 CPP_PUTC (pfile, cc);
4527 pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4528 CPP_PWRITTEN (pfile));
4529 pfile->only_seen_white = 0;
4530 return c == '\'' ? CPP_CHAR : CPP_STRING;
4533 if (!opts->dollars_in_ident)
4538 if (opts->cplusplus && PEEKC () == ':')
4547 if (c2 == c || c2 == '=')
4557 if (PEEKC () == '=')
4564 if (c2 == '-' && opts->chill)
4566 /* Chill style comment */
4567 if (opts->put_out_comments)
4568 parse_set_mark (&start_mark, pfile);
4569 FORWARD(1); /* Skip second '-'. */
4577 /* Don't consider final '\n' to be part of comment. */
4583 goto return_comment;
4585 if (c2 == '-' || c2 == '=' || c2 == '>')
4590 if (pfile->parsing_include_directive)
4594 CPP_PUTC (pfile, c);
4599 if (c == '\n' || c == EOF)
4602 "missing '>' in `#include <FILENAME>'");
4608 /* else fall through */
4617 CPP_RESERVE (pfile, 4);
4618 CPP_PUTC (pfile, c);
4619 CPP_PUTC (pfile, c2);
4623 CPP_PUTC_Q (pfile, GETC ());
4624 CPP_NUL_TERMINATE_Q (pfile);
4625 pfile->only_seen_white = 0;
4629 if (CPP_BUFFER (pfile)->has_escapes)
4634 if (pfile->output_escapes)
4635 CPP_PUTS (pfile, "@-", 2);
4636 parse_name (pfile, GETC ());
4639 else if (is_space [c])
4641 CPP_RESERVE (pfile, 2);
4642 if (pfile->output_escapes)
4643 CPP_PUTC_Q (pfile, '@');
4644 CPP_PUTC_Q (pfile, c);
4648 if (pfile->output_escapes)
4650 CPP_PUTS (pfile, "@@", 2);
4660 CPP_RESERVE(pfile, 2);
4661 CPP_PUTC_Q (pfile, '.');
4665 /* FIXME - misses the case "..\\\n." */
4666 if (c2 == '.' && PEEKN(1) == '.')
4668 CPP_RESERVE(pfile, 4);
4669 CPP_PUTC_Q (pfile, '.');
4670 CPP_PUTC_Q (pfile, '.');
4671 CPP_PUTC_Q (pfile, '.');
4673 CPP_NUL_TERMINATE_Q (pfile);
4674 pfile->only_seen_white = 0;
4681 pfile->only_seen_white = 0;
4683 CPP_RESERVE(pfile, 3);
4684 CPP_PUTC_Q (pfile, c);
4685 CPP_PUTC_Q (pfile, GETC ());
4686 CPP_NUL_TERMINATE_Q (pfile);
4692 if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4694 CPP_PUTC (pfile, c);
4700 case '0': case '1': case '2': case '3': case '4':
4701 case '5': case '6': case '7': case '8': case '9':
4706 CPP_RESERVE (pfile, 2);
4707 CPP_PUTC_Q (pfile, c);
4712 if (!is_idchar[c] && c != '.'
4713 && ((c2 != 'e' && c2 != 'E'
4714 && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4715 || (c != '+' && c != '-')))
4720 CPP_NUL_TERMINATE_Q (pfile);
4721 pfile->only_seen_white = 0;
4723 case 'b': case 'c': case 'd': case 'h': case 'o':
4724 case 'B': case 'C': case 'D': case 'H': case 'O':
4725 if (opts->chill && PEEKC () == '\'')
4727 pfile->only_seen_white = 0;
4728 CPP_RESERVE (pfile, 2);
4729 CPP_PUTC_Q (pfile, c);
4730 CPP_PUTC_Q (pfile, '\'');
4736 goto chill_number_eof;
4739 if (c == '\\' && PEEKC() == '\n')
4746 CPP_PUTC (pfile, c);
4750 CPP_RESERVE (pfile, 2);
4751 CPP_PUTC_Q (pfile, c);
4752 CPP_NUL_TERMINATE_Q (pfile);
4759 CPP_NUL_TERMINATE (pfile);
4766 case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4767 case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4768 case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4769 case 'x': case 'y': case 'z':
4770 case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4771 case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4772 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4777 unsigned char *ident;
4778 int before_name_written = CPP_WRITTEN (pfile);
4780 parse_name (pfile, c);
4781 pfile->only_seen_white = 0;
4782 if (pfile->no_macro_expand)
4784 ident = pfile->token_buffer + before_name_written;
4785 ident_len = CPP_PWRITTEN (pfile) - ident;
4786 hp = cpp_lookup (pfile, ident, ident_len, -1);
4789 if (hp->type == T_DISABLED)
4791 if (pfile->output_escapes)
4792 { /* Return "@-IDENT", followed by '\0'. */
4794 CPP_RESERVE (pfile, 3);
4795 ident = pfile->token_buffer + before_name_written;
4796 CPP_ADJUST_WRITTEN (pfile, 2);
4797 for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
4804 /* If macro wants an arglist, verify that a '(' follows.
4805 first skip all whitespace, copying it to the output
4806 after the macro name. Then, if there is no '(',
4807 decide this is not a macro call and leave things that way. */
4808 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
4810 struct parse_marker macro_mark;
4811 int is_macro_call, macbuf_whitespace = 0;
4813 parse_set_mark (¯o_mark, pfile);
4816 cpp_skip_hspace (pfile);
4818 is_macro_call = c == '(';
4827 if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4829 if (macro_mark.position !=
4830 (CPP_BUFFER (pfile)->cur
4831 - CPP_BUFFER (pfile)->buf))
4832 macbuf_whitespace = 1;
4834 parse_clear_mark (¯o_mark);
4835 cpp_pop_buffer (pfile);
4836 parse_set_mark (¯o_mark, pfile);
4844 parse_goto_mark (¯o_mark, pfile);
4845 if (macbuf_whitespace)
4846 CPP_PUTC (pfile, ' ');
4848 parse_clear_mark (¯o_mark);
4852 /* This is now known to be a macro call. */
4854 /* it might not actually be a macro. */
4855 if (hp->type != T_MACRO) {
4856 int xbuf_len; U_CHAR *xbuf;
4857 CPP_SET_WRITTEN (pfile, before_name_written);
4858 special_symbol (hp, pfile);
4859 xbuf_len = CPP_WRITTEN (pfile) - before_name_written;
4860 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4861 CPP_SET_WRITTEN (pfile, before_name_written);
4862 bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
4863 push_macro_expansion (pfile, xbuf, xbuf_len, hp);
4867 /* Expand the macro, reading arguments as needed,
4868 and push the expansion on the input stack. */
4869 macroexpand (pfile, hp);
4870 CPP_SET_WRITTEN (pfile, before_name_written);
4873 /* An extra "@ " is added to the end of a macro expansion
4874 to prevent accidental token pasting. We prefer to avoid
4875 unneeded extra spaces (for the sake of cpp-using tools like
4876 imake). Here we remove the space if it is safe to do so. */
4877 if (pfile->buffer->rlimit - pfile->buffer->cur >= 3
4878 && pfile->buffer->rlimit[-2] == '@'
4879 && pfile->buffer->rlimit[-1] == ' ')
4881 int c1 = pfile->buffer->rlimit[-3];
4882 int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
4883 if (c2 == EOF || ! unsafe_chars (c1, c2))
4884 pfile->buffer->rlimit -= 2;
4889 case ' ': case '\t': case '\v': case '\r':
4892 CPP_PUTC (pfile, c);
4894 if (c == EOF || !is_hor_space[c])
4908 CPP_PUTC (pfile, c);
4909 if (pfile->only_seen_white == 0)
4910 pfile->only_seen_white = 1;
4912 output_line_command (pfile, 1, same_file);
4915 case '(': token = CPP_LPAREN; goto char1;
4916 case ')': token = CPP_RPAREN; goto char1;
4917 case '{': token = CPP_LBRACE; goto char1;
4918 case '}': token = CPP_RBRACE; goto char1;
4919 case ',': token = CPP_COMMA; goto char1;
4920 case ';': token = CPP_SEMICOLON; goto char1;
4926 pfile->only_seen_white = 0;
4927 CPP_PUTC (pfile, c);
4933 /* Like cpp_get_token, but skip spaces and comments. */
4936 cpp_get_non_space_token (pfile)
4939 int old_written = CPP_WRITTEN (pfile);
4942 enum cpp_token token = cpp_get_token (pfile);
4943 if (token != CPP_COMMENT && token != CPP_POP
4944 && token != CPP_HSPACE && token != CPP_VSPACE)
4946 CPP_SET_WRITTEN (pfile, old_written);
4950 /* Parse an identifier starting with C. */
4953 parse_name (pfile, c)
4954 cpp_reader *pfile; int c;
4960 if (c == '\\' && PEEKC() == '\n')
4969 if (c == '$' && CPP_PEDANTIC (pfile))
4970 cpp_pedwarn (pfile, "`$' in identifier");
4972 CPP_RESERVE(pfile, 2); /* One more for final NUL. */
4973 CPP_PUTC_Q (pfile, c);
4978 CPP_NUL_TERMINATE_Q (pfile);
4982 /* This is called after options have been processed.
4983 * Check options for consistency, and setup for processing input
4984 * from the file named FNAME. (Use standard input if FNAME==NULL.)
4985 * Return 1 on success, 0 on failure.
4989 cpp_start_read (pfile, fname)
4993 struct cpp_options *opts = CPP_OPTIONS (pfile);
4994 struct cpp_pending *pend;
4999 /* The code looks at the defaults through this pointer, rather than through
5000 the constant structure above. This pointer gets changed if an environment
5001 variable specifies other defaults. */
5002 struct default_include *include_defaults = include_defaults_array;
5004 /* Add dirs from CPATH after dirs from -I. */
5005 /* There seems to be confusion about what CPATH should do,
5006 so for the moment it is not documented. */
5007 /* Some people say that CPATH should replace the standard include dirs,
5008 but that seems pointless: it comes before them, so it overrides them
5010 GET_ENV_PATH_LIST (p, "CPATH");
5011 if (p != 0 && ! opts->no_standard_includes)
5012 path_include (pfile, p);
5014 /* Now that dollars_in_ident is known, initialize is_idchar. */
5015 initialize_char_syntax (opts);
5017 /* Do partial setup of input buffer for the sake of generating
5018 early #line directives (when -g is in effect). */
5019 fp = cpp_push_buffer (pfile, NULL, 0);
5022 if (opts->in_fname == NULL)
5023 opts->in_fname = "";
5024 fp->nominal_fname = fp->fname = opts->in_fname;
5027 /* Install __LINE__, etc. Must follow initialize_char_syntax
5028 and option processing. */
5029 initialize_builtins (pfile);
5031 /* Do standard #defines and assertions
5032 that identify system and machine type. */
5034 if (!opts->inhibit_predefs) {
5035 char *p = (char *) alloca (strlen (predefs) + 1);
5036 strcpy (p, predefs);
5039 while (*p == ' ' || *p == '\t')
5041 /* Handle -D options. */
5042 if (p[0] == '-' && p[1] == 'D') {
5044 while (*p && *p != ' ' && *p != '\t')
5048 if (opts->debug_output)
5049 output_line_command (pfile, 0, same_file);
5050 cpp_define (pfile, q);
5051 while (*p == ' ' || *p == '\t')
5053 } else if (p[0] == '-' && p[1] == 'A') {
5054 /* Handle -A options (assertions). */
5063 past_name = assertion;
5064 /* Locate end of name. */
5065 while (*past_name && *past_name != ' '
5066 && *past_name != '\t' && *past_name != '(')
5068 /* Locate `(' at start of value. */
5070 while (*value && (*value == ' ' || *value == '\t'))
5072 if (*value++ != '(')
5074 while (*value && (*value == ' ' || *value == '\t'))
5077 /* Locate end of value. */
5078 while (*past_value && *past_value != ' '
5079 && *past_value != '\t' && *past_value != ')')
5081 termination = past_value;
5082 while (*termination && (*termination == ' ' || *termination == '\t'))
5084 if (*termination++ != ')')
5086 if (*termination && *termination != ' ' && *termination != '\t')
5088 /* Temporarily null-terminate the value. */
5089 save_char = *termination;
5090 *termination = '\0';
5091 /* Install the assertion. */
5092 make_assertion (pfile, "-A", assertion);
5093 *termination = (char) save_char;
5095 while (*p == ' ' || *p == '\t')
5103 /* Now handle the command line options. */
5105 /* Do -U's, -D's and -A's in the order they were seen. */
5106 /* First reverse the list. */
5107 opts->pending = nreverse_pending (opts->pending);
5109 for (pend = opts->pending; pend; pend = pend->next)
5111 if (pend->cmd != NULL && pend->cmd[0] == '-')
5113 switch (pend->cmd[1])
5116 if (opts->debug_output)
5117 output_line_command (pfile, 0, same_file);
5118 do_undef (pfile, NULL, pend->arg, pend->arg + strlen (pend->arg));
5121 if (opts->debug_output)
5122 output_line_command (pfile, 0, same_file);
5123 cpp_define (pfile, pend->arg);
5126 make_assertion (pfile, "-A", pend->arg);
5132 opts->done_initializing = 1;
5134 { /* Read the appropriate environment variable and if it exists
5135 replace include_defaults with the listed path. */
5137 switch ((opts->objc << 1) + opts->cplusplus)
5140 GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
5143 GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
5146 GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
5149 GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
5152 /* If the environment var for this language is set,
5153 add to the default list of include directories. */
5155 char *nstore = (char *) alloca (strlen (epath) + 2);
5157 char *startp, *endp;
5159 for (num_dirs = 1, startp = epath; *startp; startp++)
5160 if (*startp == PATH_SEPARATOR)
5163 = (struct default_include *) xmalloc ((num_dirs
5164 * sizeof (struct default_include))
5165 + sizeof (include_defaults_array));
5166 startp = endp = epath;
5169 /* Handle cases like c:/usr/lib:d:/gcc/lib */
5170 if ((*endp == PATH_SEPARATOR)
5172 strncpy (nstore, startp, endp-startp);
5174 strcpy (nstore, ".");
5176 nstore[endp-startp] = '\0';
5178 include_defaults[num_dirs].fname = savestring (nstore);
5179 include_defaults[num_dirs].component = 0;
5180 include_defaults[num_dirs].cplusplus = opts->cplusplus;
5181 include_defaults[num_dirs].cxx_aware = 1;
5185 endp = startp = endp + 1;
5189 /* Put the usual defaults back in at the end. */
5190 bcopy ((char *) include_defaults_array,
5191 (char *) &include_defaults[num_dirs],
5192 sizeof (include_defaults_array));
5196 append_include_chain (pfile, opts->before_system, opts->last_before_system);
5197 opts->first_system_include = opts->before_system;
5199 /* Unless -fnostdinc,
5200 tack on the standard include file dirs to the specified list */
5201 if (!opts->no_standard_includes) {
5202 struct default_include *p = include_defaults;
5203 char *specd_prefix = opts->include_prefix;
5204 char *default_prefix = savestring (GCC_INCLUDE_DIR);
5205 int default_len = 0;
5206 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5207 if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
5208 default_len = strlen (default_prefix) - 7;
5209 default_prefix[default_len] = 0;
5211 /* Search "translated" versions of GNU directories.
5212 These have /usr/local/lib/gcc... replaced by specd_prefix. */
5213 if (specd_prefix != 0 && default_len != 0)
5214 for (p = include_defaults; p->fname; p++) {
5215 /* Some standard dirs are only for C++. */
5217 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5218 /* Does this dir start with the prefix? */
5219 if (!strncmp (p->fname, default_prefix, default_len)) {
5220 /* Yes; change prefix and add to search list. */
5221 struct file_name_list *new
5222 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5223 int this_len = strlen (specd_prefix) + strlen (p->fname) - default_len;
5224 char *str = (char *) xmalloc (this_len + 1);
5225 strcpy (str, specd_prefix);
5226 strcat (str, p->fname + default_len);
5228 new->control_macro = 0;
5229 new->c_system_include_path = !p->cxx_aware;
5230 new->got_name_map = 0;
5231 append_include_chain (pfile, new, new);
5232 if (opts->first_system_include == 0)
5233 opts->first_system_include = new;
5237 /* Search ordinary names for GNU include directories. */
5238 for (p = include_defaults; p->fname; p++) {
5239 /* Some standard dirs are only for C++. */
5241 || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
5242 struct file_name_list *new
5243 = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
5244 new->control_macro = 0;
5245 new->c_system_include_path = !p->cxx_aware;
5246 new->fname = update_path (p->fname, p->component);
5247 new->got_name_map = 0;
5248 append_include_chain (pfile, new, new);
5249 if (opts->first_system_include == 0)
5250 opts->first_system_include = new;
5255 /* Tack the after_include chain at the end of the include chain. */
5256 append_include_chain (pfile, opts->after_include, opts->last_after_include);
5257 if (opts->first_system_include == 0)
5258 opts->first_system_include = opts->after_include;
5260 /* With -v, print the list of dirs to search. */
5261 if (opts->verbose) {
5262 struct file_name_list *p;
5263 fprintf (stderr, "#include \"...\" search starts here:\n");
5264 for (p = opts->include; p; p = p->next) {
5265 if (p == opts->first_bracket_include)
5266 fprintf (stderr, "#include <...> search starts here:\n");
5267 fprintf (stderr, " %s\n", p->fname);
5269 fprintf (stderr, "End of search list.\n");
5272 /* Copy the entire contents of the main input file into
5273 the stacked input buffer previously allocated for it. */
5274 if (fname == NULL || *fname == 0) {
5277 } else if ((f = open (fname, O_RDONLY, 0666)) < 0)
5278 cpp_pfatal_with_name (pfile, fname);
5280 /* -MG doesn't select the form of output and must be specified with one of
5281 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
5282 inhibit compilation. */
5283 if (opts->print_deps_missing_files
5284 && (opts->print_deps == 0 || !opts->no_output))
5286 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
5290 /* Either of two environment variables can specify output of deps.
5291 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
5292 where OUTPUT_FILE is the file to write deps info to
5293 and DEPS_TARGET is the target to mention in the deps. */
5295 if (opts->print_deps == 0
5296 && (getenv ("SUNPRO_DEPENDENCIES") != 0
5297 || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
5298 char *spec = getenv ("DEPENDENCIES_OUTPUT");
5304 spec = getenv ("SUNPRO_DEPENDENCIES");
5305 opts->print_deps = 2;
5308 opts->print_deps = 1;
5311 /* Find the space before the DEPS_TARGET, if there is one. */
5312 /* This should use index. (mrs) */
5313 while (*s != 0 && *s != ' ') s++;
5316 opts->deps_target = s + 1;
5317 output_file = (char *) xmalloc (s - spec + 1);
5318 bcopy (spec, output_file, s - spec);
5319 output_file[s - spec] = 0;
5323 opts->deps_target = 0;
5327 opts->deps_file = output_file;
5328 opts->print_deps_append = 1;
5331 /* For -M, print the expected object file name
5332 as the target of this Make-rule. */
5333 if (opts->print_deps)
5335 pfile->deps_allocated_size = 200;
5336 pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
5337 pfile->deps_buffer[0] = 0;
5338 pfile->deps_size = 0;
5339 pfile->deps_column = 0;
5341 if (opts->deps_target)
5342 deps_output (pfile, opts->deps_target, ':');
5343 else if (*opts->in_fname == 0)
5344 deps_output (pfile, "-", ':');
5349 static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
5350 ".cc", ".cxx", ".cpp", ".cp",
5354 /* Discard all directory prefixes from filename. */
5355 if ((q = rindex (opts->in_fname, '/')) != NULL
5356 #ifdef DIR_SEPARATOR
5357 && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
5364 /* Copy remainder to mungable area. */
5365 p = (char *) alloca (strlen(q) + 8);
5368 /* Output P, but remove known suffixes. */
5371 /* Point to the filename suffix. */
5372 r = rindex (p, '.');
5373 /* Compare against the known suffixes. */
5375 while (known_suffixes[x] != 0)
5377 if (strncmp (known_suffixes[x], r, q - r) == 0)
5379 /* Make q point to the bit we're going to overwrite
5380 with an object suffix. */
5387 /* Supply our own suffix. */
5394 deps_output (pfile, p, ':');
5395 deps_output (pfile, opts->in_fname, ' ');
5400 /* Make sure data ends with a newline. And put a null after it. */
5402 if ((fp->length > 0 && fp->buf[fp->length - 1] != '\n')
5403 /* Backslash-newline at end is not good enough. */
5404 || (fp->length > 1 && fp->buf[fp->length - 2] == '\\')) {
5405 fp->buf[fp->length++] = '\n';
5406 missing_newline = 1;
5408 fp->buf[fp->length] = '\0';
5410 /* Unless inhibited, convert trigraphs in the input. */
5416 /* Must call finclude() on the main input before processing
5417 -include switches; otherwise the -included text winds up
5418 after the main input. */
5419 if (!finclude (pfile, f, fname, 0, NULL_PTR))
5421 output_line_command (pfile, 0, same_file);
5422 pfile->only_seen_white = 2;
5424 /* The -imacros files can be scanned now, but the -include files
5425 have to be pushed onto the include stack and processed later,
5426 in the main loop calling cpp_get_token. That means the -include
5427 files have to be processed in reverse order of the pending list,
5428 which means the pending list has to be reversed again, which
5429 means the -imacros files have to be done separately and first. */
5431 pfile->no_record_file++;
5432 for (pend = opts->pending; pend; pend = pend->next)
5434 if (pend->cmd != NULL)
5436 if (strcmp (pend->cmd, "-imacros") == 0)
5438 int fd = open (pend->arg, O_RDONLY, 0666);
5441 cpp_perror_with_name (pfile, pend->arg);
5444 if (!cpp_push_buffer (pfile, NULL, 0))
5447 if (finclude (pfile, fd, pend->arg, 0, NULL_PTR))
5448 cpp_scan_buffer (pfile);
5453 opts->pending = nreverse_pending (opts->pending);
5454 for (pend = opts->pending; pend; pend = pend->next)
5456 if (pend->cmd != NULL)
5458 if (strcmp (pend->cmd, "-include") == 0)
5460 int fd = open (pend->arg, O_RDONLY, 0666);
5463 cpp_perror_with_name (pfile, pend->arg);
5466 if (!cpp_push_buffer (pfile, NULL, 0))
5468 if (finclude (pfile, fd, pend->arg, 0, NULL_PTR))
5469 output_line_command (pfile, 0, enter_file);
5473 pfile->no_record_file--;
5475 /* Free the pending list. */
5476 for (pend = opts->pending; pend; )
5478 struct cpp_pending *next = pend->next;
5482 opts->pending = NULL;
5488 cpp_reader_init (pfile)
5491 bzero ((char *) pfile, sizeof (cpp_reader));
5492 pfile->get_token = cpp_get_token;
5494 pfile->token_buffer_size = 200;
5495 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
5496 CPP_SET_WRITTEN (pfile, 0);
5498 pfile->system_include_depth = 0;
5499 pfile->dont_repeat_files = 0;
5500 pfile->all_include_files = 0;
5501 pfile->max_include_len = 0;
5502 pfile->timebuf = NULL;
5503 pfile->only_seen_white = 1;
5504 pfile->buffer = CPP_NULL_BUFFER(pfile);
5507 static struct cpp_pending *
5508 nreverse_pending (list)
5509 struct cpp_pending *list;
5512 register struct cpp_pending *prev = 0, *next, *pend;
5513 for (pend = list; pend; pend = next)
5523 push_pending (pfile, cmd, arg)
5528 struct cpp_pending *pend
5529 = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
5532 pend->next = CPP_OPTIONS (pfile)->pending;
5533 CPP_OPTIONS (pfile)->pending = pend;
5540 printf ("Usage: %s [switches] input output\n", progname);
5541 printf ("Switches:\n");
5542 printf (" -include <file> Include the contents of <file> before other files\n");
5543 printf (" -imacros <file> Accept definition of marcos in <file>\n");
5544 printf (" -iprefix <path> Specify <path> as a prefix for next two options\n");
5545 printf (" -iwithprefix <dir> Add <dir> to the end of the system include paths\n");
5546 printf (" -iwithprefixbefore <dir> Add <dir> to the end of the main include paths\n");
5547 printf (" -isystem <dir> Add <dir> to the start of the system include paths\n");
5548 printf (" -idirafter <dir> Add <dir> to the end of the system include paths\n");
5549 printf (" -I <dir> Add <dir> to the end of the main include paths\n");
5550 printf (" -nostdinc Do not search the system include directories\n");
5551 printf (" -nostdinc++ Do not search the system include directories for C++\n");
5552 printf (" -o <file> Put output into <file>\n");
5553 printf (" -pedantic Issue all warnings demanded by strict ANSI C\n");
5554 printf (" -traditional Follow K&R pre-processor behaviour\n");
5555 printf (" -trigraphs Support ANSI C trigraphs\n");
5556 printf (" -lang-c Assume that the input sources are in C\n");
5557 printf (" -lang-c89 Assume that the input sources are in C89\n");
5558 printf (" -lang-c++ Assume that the input sources are in C++\n");
5559 printf (" -lang-objc Assume that the input sources are in ObjectiveC\n");
5560 printf (" -lang-objc++ Assume that the input sources are in ObjectiveC++\n");
5561 printf (" -lang-asm Assume that the input sources are in assembler\n");
5562 printf (" -lang-chill Assume that the input sources are in Chill\n");
5563 printf (" -+ Allow parsing of C++ style features\n");
5564 printf (" -w Inhibit warning messages\n");
5565 printf (" -Wtrigraphs Warn if trigraphs are encountered\n");
5566 printf (" -Wno-trigraphs Do not warn about trigraphs\n");
5567 printf (" -Wcomment{s} Warn if one comment starts inside another\n");
5568 printf (" -Wno-comment{s} Do not warn about comments\n");
5569 printf (" -Wtraditional Warn if a macro argument is/would be turned into\n");
5570 printf (" a string if -tradtional is specified\n");
5571 printf (" -Wno-traditional Do not warn about stringification\n");
5572 printf (" -Wundef Warn if an undefined macro is used by #if\n");
5573 printf (" -Wno-undef Do not warn about testing udefined macros\n");
5574 printf (" -Wimport Warn about the use of the #import directive\n");
5575 printf (" -Wno-import Do not warn about the use of #import\n");
5576 printf (" -Werror Treat all warnings as errors\n");
5577 printf (" -Wno-error Do not treat warnings as errors\n");
5578 printf (" -Wall Enable all preprocessor warnings\n");
5579 printf (" -M Generate make dependencies\n");
5580 printf (" -MM As -M, but ignore system header files\n");
5581 printf (" -MD As -M, but put output in a .d file\n");
5582 printf (" -MMD As -MD, but ignore system header files\n");
5583 printf (" -MG Treat missing header file as generated files\n");
5584 printf (" -g Include #define and #undef directives in the output\n");
5585 printf (" -D<macro> Define a <macro> with string '1' as its value\n");
5586 printf (" -D<macro>=<val> Define a <macro> with <val> as its value\n");
5587 printf (" -A<question> (<answer>) Assert the <answer> to <question>\n");
5588 printf (" -U<macro> Undefine <macro> \n");
5589 printf (" -u or -undef Do not predefine any macros\n");
5590 printf (" -v Display the version number\n");
5591 printf (" -H Print the name of header files as they are used\n");
5592 printf (" -C Do not discard comments\n");
5593 printf (" -dM Display a list of macro definitions active at end\n");
5594 printf (" -dD Preserve macro definitions in output\n");
5595 printf (" -dN As -dD except that only the names are preserved\n");
5596 printf (" -dI Include #include directives in the output\n");
5597 printf (" -ifoutput Describe skipped code blocks in output \n");
5598 printf (" -P Do not generate #line directives\n");
5599 printf (" -$ Do not include '$' in identifiers\n");
5600 printf (" -remap Remap file names when including files.\n");
5601 printf (" -h or --help Display this information\n");
5605 /* Handle one command-line option in (argc, argv).
5606 Can be called multiple times, to handle multiple sets of options.
5607 Returns number of strings consumed. */
5609 cpp_handle_option (pfile, argc, argv)
5614 struct cpp_options *opts = CPP_OPTIONS (pfile);
5617 if (user_label_prefix == NULL)
5618 user_label_prefix = USER_LABEL_PREFIX;
5620 if (argv[i][0] != '-') {
5621 if (opts->out_fname != NULL)
5624 cpp_fatal (pfile, "Too many arguments");
5626 else if (opts->in_fname != NULL)
5627 opts->out_fname = argv[i];
5629 opts->in_fname = argv[i];
5631 switch (argv[i][1]) {
5634 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
5637 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
5641 if (!strcmp (argv[i], "-fleading-underscore"))
5642 user_label_prefix = "_";
5643 else if (!strcmp (argv[i], "-fno-leading-underscore"))
5644 user_label_prefix = "";
5648 if (!strcmp (argv[i], "-include")
5649 || !strcmp (argv[i], "-imacros")) {
5651 goto missing_filename;
5653 push_pending (pfile, argv[i], argv[i+1]), i++;
5655 if (!strcmp (argv[i], "-iprefix")) {
5657 goto missing_filename;
5659 opts->include_prefix = argv[++i];
5661 if (!strcmp (argv[i], "-ifoutput")) {
5662 opts->output_conditionals = 1;
5664 if (!strcmp (argv[i], "-isystem")) {
5665 struct file_name_list *dirtmp;
5668 goto missing_filename;
5670 dirtmp = (struct file_name_list *)
5671 xmalloc (sizeof (struct file_name_list));
5673 dirtmp->control_macro = 0;
5674 dirtmp->c_system_include_path = 1;
5675 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1]) + 1);
5676 strcpy (dirtmp->fname, argv[++i]);
5677 dirtmp->got_name_map = 0;
5679 if (opts->before_system == 0)
5680 opts->before_system = dirtmp;
5682 opts->last_before_system->next = dirtmp;
5683 opts->last_before_system = dirtmp; /* Tail follows the last one */
5685 /* Add directory to end of path for includes,
5686 with the default prefix at the front of its name. */
5687 if (!strcmp (argv[i], "-iwithprefix")) {
5688 struct file_name_list *dirtmp;
5691 if (opts->include_prefix != 0)
5692 prefix = opts->include_prefix;
5694 prefix = savestring (GCC_INCLUDE_DIR);
5695 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5696 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
5697 prefix[strlen (prefix) - 7] = 0;
5700 dirtmp = (struct file_name_list *)
5701 xmalloc (sizeof (struct file_name_list));
5702 dirtmp->next = 0; /* New one goes on the end */
5703 dirtmp->control_macro = 0;
5704 dirtmp->c_system_include_path = 0;
5706 goto missing_dirname;
5708 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
5709 + strlen (prefix) + 1);
5710 strcpy (dirtmp->fname, prefix);
5711 strcat (dirtmp->fname, argv[++i]);
5712 dirtmp->got_name_map = 0;
5714 if (opts->after_include == 0)
5715 opts->after_include = dirtmp;
5717 opts->last_after_include->next = dirtmp;
5718 opts->last_after_include = dirtmp; /* Tail follows the last one */
5720 /* Add directory to main path for includes,
5721 with the default prefix at the front of its name. */
5722 if (!strcmp (argv[i], "-iwithprefixbefore")) {
5723 struct file_name_list *dirtmp;
5726 if (opts->include_prefix != 0)
5727 prefix = opts->include_prefix;
5729 prefix = savestring (GCC_INCLUDE_DIR);
5730 /* Remove the `include' from /usr/local/lib/gcc.../include. */
5731 if (!strcmp (prefix + strlen (prefix) - 8, "/include"))
5732 prefix[strlen (prefix) - 7] = 0;
5735 dirtmp = (struct file_name_list *)
5736 xmalloc (sizeof (struct file_name_list));
5737 dirtmp->next = 0; /* New one goes on the end */
5738 dirtmp->control_macro = 0;
5739 dirtmp->c_system_include_path = 0;
5741 goto missing_dirname;
5743 dirtmp->fname = (char *) xmalloc (strlen (argv[i+1])
5744 + strlen (prefix) + 1);
5745 strcpy (dirtmp->fname, prefix);
5746 strcat (dirtmp->fname, argv[++i]);
5747 dirtmp->got_name_map = 0;
5749 append_include_chain (pfile, dirtmp, dirtmp);
5751 /* Add directory to end of path for includes. */
5752 if (!strcmp (argv[i], "-idirafter")) {
5753 struct file_name_list *dirtmp;
5755 dirtmp = (struct file_name_list *)
5756 xmalloc (sizeof (struct file_name_list));
5757 dirtmp->next = 0; /* New one goes on the end */
5758 dirtmp->control_macro = 0;
5759 dirtmp->c_system_include_path = 0;
5761 goto missing_dirname;
5763 dirtmp->fname = argv[++i];
5764 dirtmp->got_name_map = 0;
5766 if (opts->after_include == 0)
5767 opts->after_include = dirtmp;
5769 opts->last_after_include->next = dirtmp;
5770 opts->last_after_include = dirtmp; /* Tail follows the last one */
5775 if (opts->out_fname != NULL)
5777 cpp_fatal (pfile, "Output filename specified twice");
5781 goto missing_filename;
5782 opts->out_fname = argv[++i];
5783 if (!strcmp (opts->out_fname, "-"))
5784 opts->out_fname = "";
5788 if (!strcmp (argv[i], "-pedantic"))
5789 CPP_PEDANTIC (pfile) = 1;
5790 else if (!strcmp (argv[i], "-pedantic-errors")) {
5791 CPP_PEDANTIC (pfile) = 1;
5792 opts->pedantic_errors = 1;
5795 else if (!strcmp (argv[i], "-pcp")) {
5796 char *pcp_fname = argv[++i];
5797 pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
5798 ? fopen (pcp_fname, "w")
5799 : fdopen (dup (fileno (stdout)), "w"));
5800 if (pcp_outfile == 0)
5801 cpp_pfatal_with_name (pfile, pcp_fname);
5808 if (!strcmp (argv[i], "-traditional")) {
5809 opts->traditional = 1;
5810 opts->cplusplus_comments = 0;
5811 } else if (!strcmp (argv[i], "-trigraphs")) {
5813 opts->no_trigraphs = 0;
5818 if (! strcmp (argv[i], "-lang-c"))
5819 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5821 if (! strcmp (argv[i], "-lang-c89"))
5822 opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
5824 if (! strcmp (argv[i], "-lang-c++"))
5825 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5827 if (! strcmp (argv[i], "-lang-objc"))
5828 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5830 if (! strcmp (argv[i], "-lang-objc++"))
5831 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5833 if (! strcmp (argv[i], "-lang-asm"))
5835 if (! strcmp (argv[i], "-lint"))
5837 if (! strcmp (argv[i], "-lang-chill"))
5838 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
5839 opts->traditional = 1, opts->no_trigraphs = 1;
5843 opts->cplusplus = 1, opts->cplusplus_comments = 1;
5847 opts->inhibit_warnings = 1;
5851 if (!strcmp (argv[i], "-Wtrigraphs"))
5852 opts->warn_trigraphs = 1;
5853 else if (!strcmp (argv[i], "-Wno-trigraphs"))
5854 opts->warn_trigraphs = 0;
5855 else if (!strcmp (argv[i], "-Wcomment"))
5856 opts->warn_comments = 1;
5857 else if (!strcmp (argv[i], "-Wno-comment"))
5858 opts->warn_comments = 0;
5859 else if (!strcmp (argv[i], "-Wcomments"))
5860 opts->warn_comments = 1;
5861 else if (!strcmp (argv[i], "-Wno-comments"))
5862 opts->warn_comments = 0;
5863 else if (!strcmp (argv[i], "-Wtraditional"))
5864 opts->warn_stringify = 1;
5865 else if (!strcmp (argv[i], "-Wno-traditional"))
5866 opts->warn_stringify = 0;
5867 else if (!strcmp (argv[i], "-Wundef"))
5868 opts->warn_undef = 1;
5869 else if (!strcmp (argv[i], "-Wno-undef"))
5870 opts->warn_undef = 0;
5871 else if (!strcmp (argv[i], "-Wimport"))
5872 opts->warn_import = 1;
5873 else if (!strcmp (argv[i], "-Wno-import"))
5874 opts->warn_import = 0;
5875 else if (!strcmp (argv[i], "-Werror"))
5876 opts->warnings_are_errors = 1;
5877 else if (!strcmp (argv[i], "-Wno-error"))
5878 opts->warnings_are_errors = 0;
5879 else if (!strcmp (argv[i], "-Wall"))
5881 opts->warn_trigraphs = 1;
5882 opts->warn_comments = 1;
5887 /* The style of the choices here is a bit mixed.
5888 The chosen scheme is a hybrid of keeping all options in one string
5889 and specifying each option in a separate argument:
5890 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
5891 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5892 -M[M][G][D file]. This is awkward to handle in specs, and is not
5894 /* ??? -MG must be specified in addition to one of -M or -MM.
5895 This can be relaxed in the future without breaking anything.
5896 The converse isn't true. */
5898 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
5899 if (!strcmp (argv[i], "-MG"))
5901 opts->print_deps_missing_files = 1;
5904 if (!strcmp (argv[i], "-M"))
5905 opts->print_deps = 2;
5906 else if (!strcmp (argv[i], "-MM"))
5907 opts->print_deps = 1;
5908 else if (!strcmp (argv[i], "-MD"))
5909 opts->print_deps = 2;
5910 else if (!strcmp (argv[i], "-MMD"))
5911 opts->print_deps = 1;
5912 /* For -MD and -MMD options, write deps on file named by next arg. */
5913 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
5916 goto missing_filename;
5917 opts->deps_file = argv[++i];
5921 /* For -M and -MM, write deps on standard output
5922 and suppress the usual output. */
5923 opts->no_output = 1;
5929 char *p = argv[i] + 2;
5931 while ((c = *p++) != 0) {
5932 /* Arg to -d specifies what parts of macros to dump */
5935 opts->dump_macros = dump_only;
5936 opts->no_output = 1;
5939 opts->dump_macros = dump_names;
5942 opts->dump_macros = dump_definitions;
5945 opts->dump_includes = 1;
5953 if (argv[i][2] == '3')
5954 opts->debug_output = 1;
5958 if (strcmp (argv[i], "--help") != 0)
5964 fprintf (stderr, "GNU CPP version %s", version_string);
5965 #ifdef TARGET_VERSION
5968 fprintf (stderr, "\n");
5973 opts->print_include_names = 1;
5977 if (argv[i][2] != 0)
5978 push_pending (pfile, "-D", argv[i] + 2);
5979 else if (i + 1 == argc)
5981 cpp_fatal (pfile, "Macro name missing after -D option");
5985 i++, push_pending (pfile, "-D", argv[i]);
5992 if (argv[i][2] != 0)
5994 else if (i + 1 == argc)
5996 cpp_fatal (pfile, "Assertion missing after -A option");
6002 if (!strcmp (p, "-")) {
6003 struct cpp_pending **ptr;
6004 /* -A- eliminates all predefined macros and assertions.
6005 Let's include also any that were specified earlier
6006 on the command line. That way we can get rid of any
6007 that were passed automatically in from GCC. */
6008 opts->inhibit_predefs = 1;
6009 for (ptr = &opts->pending; *ptr != NULL; )
6011 struct cpp_pending *pend = *ptr;
6012 if (pend->cmd && pend->cmd[0] == '-'
6013 && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
6022 push_pending (pfile, "-A", p);
6027 case 'U': /* JF #undef something */
6028 if (argv[i][2] != 0)
6029 push_pending (pfile, "-U", argv[i] + 2);
6030 else if (i + 1 == argc)
6032 cpp_fatal (pfile, "Macro name missing after -U option");
6036 push_pending (pfile, "-U", argv[i+1]), i++;
6040 opts->put_out_comments = 1;
6043 case 'E': /* -E comes from cc -E; ignore it. */
6047 opts->no_line_commands = 1;
6050 case '$': /* Don't include $ in identifiers. */
6051 opts->dollars_in_ident = 0;
6054 case 'I': /* Add directory to path for includes. */
6056 struct file_name_list *dirtmp;
6058 if (! CPP_OPTIONS(pfile)->ignore_srcdir
6059 && !strcmp (argv[i] + 2, "-")) {
6060 CPP_OPTIONS (pfile)->ignore_srcdir = 1;
6061 /* Don't use any preceding -I directories for #include <...>. */
6062 CPP_OPTIONS (pfile)->first_bracket_include = 0;
6065 dirtmp = (struct file_name_list *)
6066 xmalloc (sizeof (struct file_name_list));
6067 dirtmp->next = 0; /* New one goes on the end */
6068 dirtmp->control_macro = 0;
6069 dirtmp->c_system_include_path = 0;
6070 if (argv[i][2] != 0)
6071 dirtmp->fname = argv[i] + 2;
6072 else if (i + 1 == argc)
6073 goto missing_dirname;
6075 dirtmp->fname = argv[++i];
6076 dirtmp->got_name_map = 0;
6077 append_include_chain (pfile, dirtmp, dirtmp);
6083 if (!strcmp (argv[i], "-nostdinc"))
6084 /* -nostdinc causes no default include directories.
6085 You must specify all include-file directories with -I. */
6086 opts->no_standard_includes = 1;
6087 else if (!strcmp (argv[i], "-nostdinc++"))
6088 /* -nostdinc++ causes no default C++-specific include directories. */
6089 opts->no_standard_cplusplus_includes = 1;
6091 else if (!strcmp (argv[i], "-noprecomp"))
6097 if (!strcmp (argv[i], "-remap"))
6102 /* Sun compiler passes undocumented switch "-undef".
6103 Let's assume it means to inhibit the predefined symbols. */
6104 opts->inhibit_predefs = 1;
6107 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
6108 if (opts->in_fname == NULL) {
6109 opts->in_fname = "";
6111 } else if (opts->out_fname == NULL) {
6112 opts->out_fname = "";
6114 } /* else fall through into error */
6124 /* Handle command-line options in (argc, argv).
6125 Can be called multiple times, to handle multiple sets of options.
6126 Returns if an unrecognized option is seen.
6127 Returns number of strings consumed. */
6130 cpp_handle_options (pfile, argc, argv)
6136 int strings_processed;
6137 for (i = 0; i < argc; i += strings_processed)
6139 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
6140 if (strings_processed == 0)
6150 struct cpp_options *opts = CPP_OPTIONS (pfile);
6152 if (opts->print_deps)
6154 /* Stream on which to print the dependency information. */
6157 /* Don't actually write the deps file if compilation has failed. */
6158 if (pfile->errors == 0)
6160 char *deps_mode = opts->print_deps_append ? "a" : "w";
6161 if (opts->deps_file == 0)
6162 deps_stream = stdout;
6163 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
6164 cpp_pfatal_with_name (pfile, opts->deps_file);
6165 fputs (pfile->deps_buffer, deps_stream);
6166 putc ('\n', deps_stream);
6167 if (opts->deps_file)
6169 if (ferror (deps_stream) || fclose (deps_stream) != 0)
6170 cpp_fatal (pfile, "I/O error on output");
6176 /* Free resources used by PFILE.
6177 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
6184 while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
6185 cpp_pop_buffer (pfile);
6187 if (pfile->token_buffer)
6189 free (pfile->token_buffer);
6190 pfile->token_buffer = NULL;
6193 if (pfile->deps_buffer)
6195 free (pfile->deps_buffer);
6196 pfile->deps_buffer = NULL;
6197 pfile->deps_allocated_size = 0;
6200 while (pfile->if_stack)
6202 IF_STACK_FRAME *temp = pfile->if_stack;
6203 pfile->if_stack = temp->next;
6207 while (pfile->dont_repeat_files)
6209 struct file_name_list *temp = pfile->dont_repeat_files;
6210 pfile->dont_repeat_files = temp->next;
6215 while (pfile->all_include_files)
6217 struct file_name_list *temp = pfile->all_include_files;
6218 pfile->all_include_files = temp->next;
6223 for (i = IMPORT_HASH_SIZE; --i >= 0; )
6225 register struct import_file *imp = pfile->import_hash_table[i];
6228 struct import_file *next = imp->next;
6233 pfile->import_hash_table[i] = 0;
6236 for (i = ASSERTION_HASHSIZE; --i >= 0; )
6238 while (pfile->assertion_hashtab[i])
6239 delete_assertion (pfile->assertion_hashtab[i]);
6242 cpp_hash_cleanup (pfile);
6246 do_assert (pfile, keyword, buf, limit)
6248 struct directive *keyword ATTRIBUTE_UNUSED;
6249 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
6251 long symstart; /* remember where symbol name starts */
6253 int sym_length; /* and how long it is */
6254 struct arglist *tokens = NULL;
6256 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6257 && !CPP_BUFFER (pfile)->system_header_p)
6258 cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6260 cpp_skip_hspace (pfile);
6261 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6262 parse_name (pfile, GETC());
6263 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6266 cpp_skip_hspace (pfile);
6267 if (PEEKC() != '(') {
6268 cpp_error (pfile, "missing token-sequence in `#assert'");
6274 tokens = read_token_list (pfile, &error_flag);
6278 cpp_error (pfile, "empty token-sequence in `#assert'");
6281 cpp_skip_hspace (pfile);
6283 if (c != EOF && c != '\n')
6284 cpp_pedwarn (pfile, "junk at end of `#assert'");
6285 skip_rest_of_line (pfile);
6288 /* If this name isn't already an assertion name, make it one.
6289 Error if it was already in use in some other way. */
6292 ASSERTION_HASHNODE *hp;
6293 U_CHAR *symname = pfile->token_buffer + symstart;
6294 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6295 struct tokenlist_list *value
6296 = (struct tokenlist_list *) xmalloc (sizeof (struct tokenlist_list));
6298 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6300 if (sym_length == 7 && ! strncmp (symname, "defined", sym_length))
6301 cpp_error (pfile, "`defined' redefined as assertion");
6302 hp = assertion_install (pfile, symname, sym_length, hashcode);
6305 /* Add the spec'd token-sequence to the list of such. */
6306 value->tokens = tokens;
6307 value->next = hp->value;
6310 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6313 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6314 skip_rest_of_line (pfile);
6319 do_unassert (pfile, keyword, buf, limit)
6321 struct directive *keyword ATTRIBUTE_UNUSED;
6322 U_CHAR *buf ATTRIBUTE_UNUSED, *limit ATTRIBUTE_UNUSED;
6324 long symstart; /* remember where symbol name starts */
6325 int sym_length; /* and how long it is */
6328 struct arglist *tokens = NULL;
6329 int tokens_specified = 0;
6331 if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6332 && !CPP_BUFFER (pfile)->system_header_p)
6333 cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6335 cpp_skip_hspace (pfile);
6337 symstart = CPP_WRITTEN (pfile); /* remember where it starts */
6338 parse_name (pfile, GETC());
6339 sym_length = check_macro_name (pfile, pfile->token_buffer + symstart,
6342 cpp_skip_hspace (pfile);
6343 if (PEEKC() == '(') {
6346 tokens = read_token_list (pfile, &error_flag);
6350 cpp_error (pfile, "empty token list in `#unassert'");
6354 tokens_specified = 1;
6357 cpp_skip_hspace (pfile);
6359 if (c != EOF && c != '\n')
6360 cpp_error (pfile, "junk at end of `#unassert'");
6361 skip_rest_of_line (pfile);
6364 ASSERTION_HASHNODE *hp;
6365 U_CHAR *symname = pfile->token_buffer + symstart;
6366 int hashcode = hashf (symname, sym_length, ASSERTION_HASHSIZE);
6367 struct tokenlist_list *tail, *prev;
6369 hp = assertion_lookup (pfile, symname, sym_length, hashcode);
6373 /* If no token list was specified, then eliminate this assertion
6375 if (! tokens_specified)
6376 delete_assertion (hp);
6378 /* If a list of tokens was given, then delete any matching list. */
6383 struct tokenlist_list *next = tail->next;
6384 if (compare_token_lists (tail->tokens, tokens)) {
6388 hp->value = tail->next;
6389 free_token_list (tail->tokens);
6399 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6402 CPP_SET_WRITTEN (pfile, symstart); /* Pop */
6403 skip_rest_of_line (pfile);
6407 /* Test whether there is an assertion named NAME
6408 and optionally whether it has an asserted token list TOKENS.
6409 NAME is not null terminated; its length is SYM_LENGTH.
6410 If TOKENS_SPECIFIED is 0, then don't check for any token list. */
6413 check_assertion (pfile, name, sym_length, tokens_specified, tokens)
6417 int tokens_specified;
6418 struct arglist *tokens;
6420 ASSERTION_HASHNODE *hp;
6421 int hashcode = hashf (name, sym_length, ASSERTION_HASHSIZE);
6423 if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
6424 cpp_pedwarn (pfile, "ANSI C does not allow testing assertions");
6426 hp = assertion_lookup (pfile, name, sym_length, hashcode);
6428 /* It is not an assertion; just return false. */
6431 /* If no token list was specified, then value is 1. */
6432 if (! tokens_specified)
6436 struct tokenlist_list *tail;
6440 /* If a list of tokens was given,
6441 then succeed if the assertion records a matching list. */
6444 if (compare_token_lists (tail->tokens, tokens))
6449 /* Fail if the assertion has no matching list. */
6454 /* Compare two lists of tokens for equality including order of tokens. */
6457 compare_token_lists (l1, l2)
6458 struct arglist *l1, *l2;
6461 if (l1->length != l2->length)
6463 if (strncmp (l1->name, l2->name, l1->length))
6469 /* Succeed if both lists end at the same time. */
6474 reverse_token_list (tokens)
6475 struct arglist *tokens;
6477 register struct arglist *prev = 0, *this, *next;
6478 for (this = tokens; this; this = next)
6487 /* Read a space-separated list of tokens ending in a close parenthesis.
6488 Return a list of strings, in the order they were written.
6489 (In case of error, return 0 and store -1 in *ERROR_FLAG.) */
6491 static struct arglist *
6492 read_token_list (pfile, error_flag)
6496 struct arglist *token_ptrs = 0;
6501 FORWARD (1); /* Skip '(' */
6503 /* Loop over the assertion value tokens. */
6506 struct arglist *temp;
6507 long name_written = CPP_WRITTEN (pfile);
6510 cpp_skip_hspace (pfile);
6514 /* Find the end of the token. */
6517 CPP_PUTC (pfile, c);
6525 CPP_PUTC (pfile, c);
6527 else if (c == '"' || c == '\'')
6530 cpp_get_token (pfile);
6536 while (c != EOF && ! is_space[c] && c != '(' && c != ')'
6537 && c != '"' && c != '\'')
6539 CPP_PUTC (pfile, c);
6542 if (c != EOF) FORWARD(-1);
6545 length = CPP_WRITTEN (pfile) - name_written;
6546 temp = (struct arglist *)
6547 xmalloc (sizeof (struct arglist) + length + 1);
6548 temp->name = (U_CHAR *) (temp + 1);
6549 bcopy ((char *) (pfile->token_buffer + name_written),
6550 (char *) temp->name, length);
6551 temp->name[length] = 0;
6552 temp->next = token_ptrs;
6554 temp->length = length;
6556 CPP_ADJUST_WRITTEN (pfile, -length); /* pop */
6558 if (c == EOF || c == '\n')
6561 "unterminated token sequence following `#' operator");
6566 /* We accumulated the names in reverse order.
6567 Now reverse them to get the proper order. */
6568 return reverse_token_list (token_ptrs);
6572 free_token_list (tokens)
6573 struct arglist *tokens;
6576 struct arglist *next = tokens->next;
6577 free (tokens->name);
6583 /* FIXME: savestring() should be renamed strdup() and both should
6584 be moved into cppalloc.c. We can't do that right now because
6585 then we'd get multiple-symbol clashes with toplev.c and several
6588 xcalloc (number, size)
6589 unsigned number, size;
6591 register unsigned total = number * size;
6592 register char *ptr = (char *) xmalloc (total);
6601 unsigned size = strlen (input);
6602 char *output = xmalloc (size + 1);
6603 strcpy (output, input);
6607 /* Initialize PMARK to remember the current position of PFILE. */
6610 parse_set_mark (pmark, pfile)
6611 struct parse_marker *pmark;
6614 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6615 pmark->next = pbuf->marks;
6616 pbuf->marks = pmark;
6618 pmark->position = pbuf->cur - pbuf->buf;
6621 /* Cleanup PMARK - we no longer need it. */
6624 parse_clear_mark (pmark)
6625 struct parse_marker *pmark;
6627 struct parse_marker **pp = &pmark->buf->marks;
6628 for (; ; pp = &(*pp)->next) {
6629 if (*pp == NULL) abort ();
6630 if (*pp == pmark) break;
6635 /* Backup the current position of PFILE to that saved in PMARK. */
6638 parse_goto_mark (pmark, pfile)
6639 struct parse_marker *pmark;
6642 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6643 if (pbuf != pmark->buf)
6644 cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
6645 pbuf->cur = pbuf->buf + pmark->position;
6648 /* Reset PMARK to point to the current position of PFILE. (Same
6649 as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster. */
6652 parse_move_mark (pmark, pfile)
6653 struct parse_marker *pmark;
6656 cpp_buffer *pbuf = CPP_BUFFER (pfile);
6657 if (pbuf != pmark->buf)
6658 cpp_fatal (pfile, "internal error %s", "parse_move_mark");
6659 pmark->position = pbuf->cur - pbuf->buf;
6663 cpp_read_check_assertion (pfile)
6666 int name_start = CPP_WRITTEN (pfile);
6667 int name_length, name_written;
6669 FORWARD (1); /* Skip '#' */
6670 cpp_skip_hspace (pfile);
6671 parse_name (pfile, GETC ());
6672 name_written = CPP_WRITTEN (pfile);
6673 name_length = name_written - name_start;
6674 cpp_skip_hspace (pfile);
6675 if (CPP_BUF_PEEK (CPP_BUFFER (pfile)) == '(')
6678 struct arglist *token_ptrs = read_token_list (pfile, &error_flag);
6679 result = check_assertion (pfile,
6680 pfile->token_buffer + name_start, name_length,
6684 result = check_assertion (pfile,
6685 pfile->token_buffer + name_start, name_length,
6687 CPP_ADJUST_WRITTEN (pfile, - name_length); /* pop */
6692 cpp_print_file_and_line (pfile)
6695 cpp_buffer *ip = cpp_file_buffer (pfile);
6700 cpp_buf_line_and_col (ip, &line, &col);
6701 cpp_file_line_for_message (pfile, ip->nominal_fname,
6702 line, pfile->show_column ? col : -1);
6707 v_cpp_error (pfile, msg, ap)
6712 cpp_print_containing_files (pfile);
6713 cpp_print_file_and_line (pfile);
6714 v_cpp_message (pfile, 1, msg, ap);
6718 cpp_error VPROTO ((cpp_reader * pfile, const char *msg, ...))
6720 #ifndef ANSI_PROTOTYPES
6728 #ifndef ANSI_PROTOTYPES
6729 pfile = va_arg (ap, cpp_reader *);
6730 msg = va_arg (ap, const char *);
6733 v_cpp_error (pfile, msg, ap);
6737 /* Print error message but don't count it. */
6740 v_cpp_warning (pfile, msg, ap)
6745 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6748 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6751 cpp_print_containing_files (pfile);
6752 cpp_print_file_and_line (pfile);
6753 v_cpp_message (pfile, 0, msg, ap);
6757 cpp_warning VPROTO ((cpp_reader * pfile, const char *msg, ...))
6759 #ifndef ANSI_PROTOTYPES
6767 #ifndef ANSI_PROTOTYPES
6768 pfile = va_arg (ap, cpp_reader *);
6769 msg = va_arg (ap, const char *);
6772 v_cpp_warning (pfile, msg, ap);
6776 /* Print an error message and maybe count it. */
6779 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msg, ...))
6781 #ifndef ANSI_PROTOTYPES
6789 #ifndef ANSI_PROTOTYPES
6790 pfile = va_arg (ap, cpp_reader *);
6791 msg = va_arg (ap, const char *);
6794 if (CPP_OPTIONS (pfile)->pedantic_errors)
6795 v_cpp_error (pfile, msg, ap);
6797 v_cpp_warning (pfile, msg, ap);
6802 v_cpp_error_with_line (pfile, line, column, msg, ap)
6809 cpp_buffer *ip = cpp_file_buffer (pfile);
6811 cpp_print_containing_files (pfile);
6814 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6816 v_cpp_message (pfile, 1, msg, ap);
6820 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6822 #ifndef ANSI_PROTOTYPES
6832 #ifndef ANSI_PROTOTYPES
6833 pfile = va_arg (ap, cpp_reader *);
6834 line = va_arg (ap, int);
6835 column = va_arg (ap, int);
6836 msg = va_arg (ap, const char *);
6839 v_cpp_error_with_line(pfile, line, column, msg, ap);
6844 v_cpp_warning_with_line (pfile, line, column, msg, ap)
6853 if (CPP_OPTIONS (pfile)->inhibit_warnings)
6856 if (CPP_OPTIONS (pfile)->warnings_are_errors)
6859 cpp_print_containing_files (pfile);
6861 ip = cpp_file_buffer (pfile);
6864 cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6866 v_cpp_message (pfile, 0, msg, ap);
6871 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6873 #ifndef ANSI_PROTOTYPES
6883 #ifndef ANSI_PROTOTYPES
6884 pfile = va_arg (ap, cpp_reader *);
6885 line = va_arg (ap, int);
6886 column = va_arg (ap, int);
6887 msg = va_arg (ap, const char *);
6890 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6896 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column, const char *msg, ...))
6898 #ifndef ANSI_PROTOTYPES
6908 #ifndef ANSI_PROTOTYPES
6909 pfile = va_arg (ap, cpp_reader *);
6910 line = va_arg (ap, int);
6911 column = va_arg (ap, int);
6912 msg = va_arg (ap, const char *);
6915 if (CPP_OPTIONS (pfile)->pedantic_errors)
6916 v_cpp_error_with_line (pfile, column, line, msg, ap);
6918 v_cpp_warning_with_line (pfile, line, column, msg, ap);
6922 /* Report a warning (or an error if pedantic_errors)
6923 giving specified file name and line number, not current. */
6926 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line, const char *msg, ...))
6928 #ifndef ANSI_PROTOTYPES
6938 #ifndef ANSI_PROTOTYPES
6939 pfile = va_arg (ap, cpp_reader *);
6940 file = va_arg (ap, char *);
6941 line = va_arg (ap, int);
6942 msg = va_arg (ap, const char *);
6945 if (!CPP_OPTIONS (pfile)->pedantic_errors
6946 && CPP_OPTIONS (pfile)->inhibit_warnings)
6949 cpp_file_line_for_message (pfile, file, line, -1);
6950 v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msg, ap);
6954 /* my_strerror - return the descriptive text associated with an
6958 my_strerror (errnum)
6964 #ifndef HAVE_STRERROR
6965 result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
6967 result = strerror (errnum);
6970 /* VAXCRTL's strerror() takes an optional second argument, which only
6971 matters when the first argument is EVMSERR. However, it's simplest
6972 just to pass it unconditionally. `vaxc$errno' is declared in
6973 <errno.h>, and maintained by the library in parallel with `errno'.
6974 We assume that caller's `errnum' either matches the last setting of
6975 `errno' by the library or else does not have the value `EVMSERR'. */
6977 result = strerror (errnum, vaxc$errno);
6981 result = "undocumented I/O error";
6986 /* Error including a message from `errno'. */
6989 cpp_error_from_errno (pfile, name)
6993 cpp_message_from_errno (pfile, 1, name);
6997 cpp_message_from_errno (pfile, is_error, name)
7003 cpp_buffer *ip = cpp_file_buffer (pfile);
7005 cpp_print_containing_files (pfile);
7008 cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
7010 cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
7014 cpp_perror_with_name (pfile, name)
7018 cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
7022 * No pre-compiled header file support.
7024 * Possibly different enum token codes for each C/C++ token.
7026 * Should clean up remaining directives to that do_XXX functions
7027 * only take two arguments and all have command_reads_line.
7029 * Find and cleanup remaining uses of static variables,
7031 * Support for trigraphs.
7033 * Support -dM flag (dump_all_macros).
7035 * Support for_lint flag.