2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Per Bothner, 1994-95.
5 Based on CCCP program by Paul Rubin, June 1986
6 Adapted to ANSI C, Richard Stallman, Jan 1987
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 /* Predefined symbols, built-in macros, and the default include path. */
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
39 #ifndef STANDARD_INCLUDE_DIR
40 #define STANDARD_INCLUDE_DIR "/usr/include"
43 /* We let tm.h override the types used here, to handle trivial differences
44 such as the choice of unsigned int or long unsigned int for size_t.
45 When machines start needing nontrivial differences in the size type,
46 it would be best to do something here to figure out automatically
47 from other information what type to use. */
49 /* The string value for __SIZE_TYPE__. */
52 #define SIZE_TYPE "long unsigned int"
55 /* The string value for __PTRDIFF_TYPE__. */
58 #define PTRDIFF_TYPE "long int"
61 /* The string value for __WCHAR_TYPE__. */
64 #define WCHAR_TYPE "int"
67 /* The string value for __USER_LABEL_PREFIX__ */
69 #ifndef USER_LABEL_PREFIX
70 #define USER_LABEL_PREFIX ""
73 /* The string value for __REGISTER_PREFIX__ */
75 #ifndef REGISTER_PREFIX
76 #define REGISTER_PREFIX ""
79 /* This is the default list of directories to search for include files.
80 It may be overridden by the various -I and -ixxx options.
82 #include "file" looks in the same directory as the current file,
84 #include <file> just looks in this list.
86 All these directories are treated as `system' include directories
87 (they are not subject to pedantic warnings in some cases). */
89 struct default_include
91 const char *fname
; /* The name of the directory. */
92 const char *component
; /* The component containing the directory
93 (see update_path in prefix.c) */
94 int cplusplus
; /* Only look here if we're compiling C++. */
95 int cxx_aware
; /* Includes in this directory don't need to
96 be wrapped in extern "C" when compiling
100 static const struct default_include include_defaults_array
[]
101 #ifdef INCLUDE_DEFAULTS
105 /* Pick up GNU C++ specific include files. */
106 { GPLUSPLUS_INCLUDE_DIR
, "G++", 1, 1 },
108 /* This is the dir for fixincludes. Put it just before
109 the files that we fix. */
110 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
111 /* For cross-compilation, this dir name is generated
112 automatically in Makefile.in. */
113 { CROSS_INCLUDE_DIR
, "GCC", 0, 0 },
114 #ifdef TOOL_INCLUDE_DIR
115 /* This is another place that the target system's headers might be. */
116 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
118 #else /* not CROSS_COMPILE */
119 #ifdef LOCAL_INCLUDE_DIR
120 /* This should be /usr/local/include and should come before
121 the fixincludes-fixed header files. */
122 { LOCAL_INCLUDE_DIR
, 0, 0, 1 },
124 #ifdef TOOL_INCLUDE_DIR
125 /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
126 Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h. */
127 { TOOL_INCLUDE_DIR
, "BINUTILS", 0, 1 },
129 /* This is the dir for fixincludes. Put it just before
130 the files that we fix. */
131 { GCC_INCLUDE_DIR
, "GCC", 0, 0 },
132 /* Some systems have an extra dir of include files. */
133 #ifdef SYSTEM_INCLUDE_DIR
134 { SYSTEM_INCLUDE_DIR
, 0, 0, 0 },
136 #ifndef STANDARD_INCLUDE_COMPONENT
137 #define STANDARD_INCLUDE_COMPONENT 0
139 { STANDARD_INCLUDE_DIR
, STANDARD_INCLUDE_COMPONENT
, 0, 0 },
140 #endif /* not CROSS_COMPILE */
143 #endif /* no INCLUDE_DEFAULTS */
145 /* Internal structures and prototypes. */
147 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
148 switch. There are four lists: one for -D and -U, one for -A, one
149 for -include, one for -imacros. `undef' is set for -U, clear for
150 -D, ignored for the others.
151 (Future: add an equivalent of -U for -A) */
152 struct pending_option
154 struct pending_option
*next
;
160 #define APPEND(pend, list, elt) \
161 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
162 else (pend)->list##_tail->next = (elt); \
163 (pend)->list##_tail = (elt); \
166 #define APPEND(pend, list, elt) \
167 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
168 else (pend)->list/**/_tail->next = (elt); \
169 (pend)->list/**/_tail = (elt); \
173 static void print_help
PARAMS ((void));
174 static void path_include
PARAMS ((cpp_reader
*,
175 struct cpp_pending
*,
177 static void initialize_builtins
PARAMS ((cpp_reader
*));
178 static void append_include_chain
PARAMS ((cpp_reader
*,
179 struct cpp_pending
*,
181 static void dump_special_to_buffer
PARAMS ((cpp_reader
*, const char *));
182 static void initialize_dependency_output
PARAMS ((cpp_reader
*));
183 static void initialize_standard_includes
PARAMS ((cpp_reader
*));
184 static void new_pending_define
PARAMS ((struct cpp_options
*,
187 /* Fourth argument to append_include_chain: chain to use */
188 enum { QUOTE
= 0, BRACKET
, SYSTEM
, AFTER
};
190 /* If we have designated initializers (GCC >2.7, or C99) this table
191 can be initialized, constant data. Otherwise, it has to be filled
194 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
195 #define init_IStable() /* nothing */
196 #define ISTABLE const unsigned char _cpp_IStable[256] = {
198 #define s(p, v) [p] = v,
200 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
201 static void init_IStable PARAMS ((void)) { \
202 unsigned char *x = _cpp_IStable;
204 #define s(p, v) x[p] = v;
207 #define A(x) s(x, ISidnum|ISidstart)
208 #define N(x) s(x, ISidnum|ISnumstart)
209 #define H(x) s(x, IShspace|ISspace)
210 #define S(x) s(x, ISspace)
215 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
216 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
217 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
219 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
220 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
221 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
223 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
225 H(' ') H('\t') H('\v') H('\f')
238 /* Given a colon-separated list of file names PATH,
239 add all the names to the search path for include files. */
242 path_include (pfile
, pend
, list
, path
)
244 struct cpp_pending
*pend
;
254 /* Find the end of this name. */
256 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
259 /* An empty name in the path stands for the current directory. */
260 name
= (char *) xmalloc (2);
266 /* Otherwise use the directory that is named. */
267 name
= (char *) xmalloc (q
- p
+ 1);
268 memcpy (name
, p
, q
- p
);
272 append_include_chain (pfile
, pend
, name
, path
, 0);
274 /* Advance past this name. */
282 /* Append DIR to include path PATH. DIR must be permanently allocated
285 append_include_chain (pfile
, pend
, dir
, path
, cxx_aware
)
287 struct cpp_pending
*pend
;
292 struct file_name_list
*new;
296 simplify_pathname (dir
);
299 /* Dirs that don't exist are silently ignored. */
301 cpp_notice_from_errno (pfile
, dir
);
302 else if (CPP_OPTIONS (pfile
)->verbose
)
303 fprintf (stderr
, _("ignoring nonexistent directory `%s'\n"), dir
);
307 if (!S_ISDIR (st
.st_mode
))
309 cpp_notice (pfile
, "%s: Not a directory", dir
);
314 if (len
> pfile
->max_include_len
)
315 pfile
->max_include_len
= len
;
317 new = (struct file_name_list
*)xmalloc (sizeof (struct file_name_list
));
320 new->ino
= st
.st_ino
;
321 new->dev
= st
.st_dev
;
323 new->sysp
= cxx_aware
? 1 : 2;
326 new->name_map
= NULL
;
332 case QUOTE
: APPEND (pend
, quote
, new); break;
333 case BRACKET
: APPEND (pend
, brack
, new); break;
334 case SYSTEM
: APPEND (pend
, systm
, new); break;
335 case AFTER
: APPEND (pend
, after
, new); break;
340 /* Write out a #define command for the special named MACRO_NAME
341 to PFILE's token_buffer. */
344 dump_special_to_buffer (pfile
, macro_name
)
346 const char *macro_name
;
348 static const char define_directive
[] = "#define ";
349 int macro_name_length
= strlen (macro_name
);
350 output_line_command (pfile
, same_file
);
351 CPP_RESERVE (pfile
, sizeof(define_directive
) + macro_name_length
);
352 CPP_PUTS_Q (pfile
, define_directive
, sizeof(define_directive
)-1);
353 CPP_PUTS_Q (pfile
, macro_name
, macro_name_length
);
354 CPP_PUTC_Q (pfile
, ' ');
355 cpp_expand_to_buffer (pfile
, macro_name
, macro_name_length
);
356 CPP_PUTC (pfile
, '\n');
359 /* Initialize a cpp_options structure. */
361 cpp_options_init (opts
)
364 bzero ((char *) opts
, sizeof (struct cpp_options
));
366 opts
->dollars_in_ident
= 1;
367 opts
->cplusplus_comments
= 1;
368 opts
->warn_import
= 1;
369 opts
->discard_comments
= 1;
372 (struct cpp_pending
*) xcalloc (1, sizeof (struct cpp_pending
));
375 /* Initialize a cpp_reader structure. */
377 cpp_reader_init (pfile
)
380 bzero ((char *) pfile
, sizeof (cpp_reader
));
382 pfile
->token_buffer_size
= 200;
383 pfile
->token_buffer
= (U_CHAR
*) xmalloc (pfile
->token_buffer_size
);
384 CPP_SET_WRITTEN (pfile
, 0);
386 pfile
->hashtab
= (HASHNODE
**) xcalloc (HASHSIZE
, sizeof (HASHNODE
*));
389 /* Free resources used by PFILE.
390 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
396 while (CPP_BUFFER (pfile
) != CPP_NULL_BUFFER (pfile
))
397 cpp_pop_buffer (pfile
);
399 if (pfile
->token_buffer
)
401 free (pfile
->token_buffer
);
402 pfile
->token_buffer
= NULL
;
405 if (pfile
->input_buffer
)
407 free (pfile
->input_buffer
);
408 free (pfile
->input_speccase
);
409 pfile
->input_buffer
= pfile
->input_speccase
= NULL
;
410 pfile
->input_buffer_len
= 0;
414 deps_free (pfile
->deps
);
416 while (pfile
->if_stack
)
418 IF_STACK_FRAME
*temp
= pfile
->if_stack
;
419 pfile
->if_stack
= temp
->next
;
423 for (i
= ALL_INCLUDE_HASHSIZE
; --i
>= 0; )
425 struct include_hash
*imp
= pfile
->all_include_files
[i
];
428 struct include_hash
*next
= imp
->next
;
430 free ((PTR
) imp
->name
);
434 pfile
->all_include_files
[i
] = 0;
437 for (i
= HASHSIZE
; --i
>= 0;)
439 while (pfile
->hashtab
[i
])
440 delete_macro (pfile
->hashtab
[i
]);
442 free (pfile
->hashtab
);
446 /* This structure defines one built-in macro. A node of type TYPE will
447 be entered in the macro hash table under the name NAME, with value
448 VALUE (if any). FLAGS tweaks the behavior a little:
449 DUMP write debug info for this macro
450 STDC define only if not -traditional
451 ULP value is the global user_label_prefix (which can't be
452 put directly into the table).
460 unsigned short flags
;
466 static const struct builtin builtin_array
[] =
468 { "__TIME__", 0, T_TIME
, DUMP
},
469 { "__DATE__", 0, T_DATE
, DUMP
},
470 { "__FILE__", 0, T_FILE
, 0 },
471 { "__BASE_FILE__", 0, T_BASE_FILE
, 0 },
472 { "__LINE__", 0, T_SPECLINE
, 0 },
473 { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL
, 0 },
474 { "__VERSION__", 0, T_VERSION
, DUMP
},
475 { "__STDC__", 0, T_STDC
, DUMP
|STDC
},
477 { "__USER_LABEL_PREFIX__", 0, T_CONST
, ULP
},
478 { "__REGISTER_PREFIX__", REGISTER_PREFIX
, T_CONST
, 0 },
479 { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST
, 0 },
480 #ifndef NO_BUILTIN_SIZE_TYPE
481 { "__SIZE_TYPE__", SIZE_TYPE
, T_CONST
, DUMP
},
483 #ifndef NO_BUILTIN_PTRDIFF_TYPE
484 { "__PTRDIFF_TYPE__", PTRDIFF_TYPE
, T_CONST
, DUMP
},
486 #ifndef NO_BUILTIN_WCHAR_TYPE
487 { "__WCHAR_TYPE__", WCHAR_TYPE
, T_CONST
, DUMP
},
492 /* Subroutine of cpp_start_read; reads the builtins table above and
493 enters the macros into the hash table. */
496 initialize_builtins (pfile
)
500 const struct builtin
*b
;
502 for(b
= builtin_array
; b
->name
; b
++)
504 if ((b
->flags
& STDC
) && CPP_TRADITIONAL (pfile
))
507 val
= (b
->flags
& ULP
) ? user_label_prefix
: b
->value
;
508 len
= strlen (b
->name
);
510 cpp_install (pfile
, b
->name
, len
, b
->type
, val
);
511 if ((b
->flags
& DUMP
) && CPP_OPTIONS (pfile
)->debug_output
)
512 dump_special_to_buffer (pfile
, b
->name
);
520 /* Another subroutine of cpp_start_read. This one sets up to do
521 dependency-file output. */
523 initialize_dependency_output (pfile
)
526 cpp_options
*opts
= CPP_OPTIONS (pfile
);
527 char *spec
, *s
, *output_file
;
529 /* Either of two environment variables can specify output of deps.
530 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
531 where OUTPUT_FILE is the file to write deps info to
532 and DEPS_TARGET is the target to mention in the deps. */
534 if (opts
->print_deps
== 0)
536 spec
= getenv ("DEPENDENCIES_OUTPUT");
538 opts
->print_deps
= 1;
541 spec
= getenv ("SUNPRO_DEPENDENCIES");
543 opts
->print_deps
= 2;
548 /* Find the space before the DEPS_TARGET, if there is one. */
549 s
= strchr (spec
, ' ');
552 opts
->deps_target
= s
+ 1;
553 output_file
= (char *) xmalloc (s
- spec
+ 1);
554 memcpy (output_file
, spec
, s
- spec
);
555 output_file
[s
- spec
] = 0;
559 opts
->deps_target
= 0;
563 opts
->deps_file
= output_file
;
564 opts
->print_deps_append
= 1;
567 pfile
->deps
= deps_init ();
569 /* Print the expected object file name as the target of this Make-rule. */
570 if (opts
->deps_target
)
571 deps_add_target (pfile
->deps
, opts
->deps_target
);
572 else if (*opts
->in_fname
== 0)
573 deps_add_target (pfile
->deps
, "-");
575 deps_calc_target (pfile
->deps
, opts
->in_fname
);
578 deps_add_dep (pfile
->deps
, opts
->in_fname
);
581 /* And another subroutine. This one sets up the standard include path. */
583 initialize_standard_includes (pfile
)
586 cpp_options
*opts
= CPP_OPTIONS (pfile
);
588 const struct default_include
*p
;
589 char *specd_prefix
= opts
->include_prefix
;
591 /* Several environment variables may add to the include search path.
592 CPATH specifies an additional list of directories to be searched
593 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
594 etc. specify an additional list of directories to be searched as
595 if specified with -isystem, for the language indicated. */
597 GET_ENV_PATH_LIST (path
, "CPATH");
598 if (path
!= 0 && *path
!= 0)
599 path_include (pfile
, opts
->pending
, path
, BRACKET
);
601 switch ((opts
->objc
<< 1) + opts
->cplusplus
)
604 GET_ENV_PATH_LIST (path
, "C_INCLUDE_PATH");
607 GET_ENV_PATH_LIST (path
, "CPLUS_INCLUDE_PATH");
610 GET_ENV_PATH_LIST (path
, "OBJC_INCLUDE_PATH");
613 GET_ENV_PATH_LIST (path
, "OBJCPLUS_INCLUDE_PATH");
616 if (path
!= 0 && *path
!= 0)
617 path_include (pfile
, opts
->pending
, path
, SYSTEM
);
619 /* Search "translated" versions of GNU directories.
620 These have /usr/local/lib/gcc... replaced by specd_prefix. */
621 if (specd_prefix
!= 0)
623 char *default_prefix
= alloca (sizeof GCC_INCLUDE_DIR
- 7);
624 /* Remove the `include' from /usr/local/lib/gcc.../include.
625 GCC_INCLUDE_DIR will always end in /include. */
626 int default_len
= sizeof GCC_INCLUDE_DIR
- 8;
627 int specd_len
= strlen (specd_prefix
);
629 memcpy (default_prefix
, GCC_INCLUDE_DIR
, default_len
);
630 default_prefix
[default_len
] = '\0';
632 for (p
= include_defaults_array
; p
->fname
; p
++)
634 /* Some standard dirs are only for C++. */
637 && !opts
->no_standard_cplusplus_includes
))
639 /* Does this dir start with the prefix? */
640 if (!strncmp (p
->fname
, default_prefix
, default_len
))
642 /* Yes; change prefix and add to search list. */
643 int flen
= strlen (p
->fname
);
644 int this_len
= specd_len
+ flen
- default_len
;
645 char *str
= (char *) xmalloc (this_len
+ 1);
646 memcpy (str
, specd_prefix
, specd_len
);
647 memcpy (str
+ specd_len
,
648 p
->fname
+ default_len
,
649 flen
- default_len
+ 1);
651 append_include_chain (pfile
, opts
->pending
,
652 str
, SYSTEM
, p
->cxx_aware
);
658 /* Search ordinary names for GNU include directories. */
659 for (p
= include_defaults_array
; p
->fname
; p
++)
661 /* Some standard dirs are only for C++. */
664 && !opts
->no_standard_cplusplus_includes
))
666 /* XXX Potential memory leak! */
667 char *str
= xstrdup (update_path (p
->fname
, p
->component
));
668 append_include_chain (pfile
, opts
->pending
, str
, SYSTEM
,
674 /* This is called after options have been processed.
675 * Check options for consistency, and setup for processing input
676 * from the file named FNAME. (Use standard input if FNAME==NULL.)
677 * Return 1 on success, 0 on failure.
681 cpp_start_read (pfile
, fname
)
685 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
686 struct pending_option
*p
, *q
;
688 /* -MG doesn't select the form of output and must be specified with one of
689 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
690 inhibit compilation. */
691 if (opts
->print_deps_missing_files
692 && (opts
->print_deps
== 0 || !opts
->no_output
))
694 cpp_fatal (pfile
, "-MG must be specified with one of -M or -MM");
698 /* Chill should not be used with -trigraphs. */
699 if (opts
->chill
&& opts
->trigraphs
)
701 cpp_warning (pfile
, "-lang-chill and -trigraphs are mutually exclusive");
705 /* Set this if it hasn't been set already. */
706 if (user_label_prefix
== NULL
)
707 user_label_prefix
= USER_LABEL_PREFIX
;
709 /* Don't bother trying to do macro expansion if we've already done
711 if (opts
->preprocessed
)
712 pfile
->no_macro_expand
++;
714 /* Set up the IStable. This doesn't do anything if we were compiled
715 with a compiler that supports C99 designated initializers. */
718 /* Set up the include search path now. */
719 if (! opts
->no_standard_includes
)
720 initialize_standard_includes (pfile
);
722 merge_include_chains (opts
);
724 /* With -v, print the list of dirs to search. */
727 struct file_name_list
*l
;
728 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
729 for (l
= opts
->quote_include
; l
; l
= l
->next
)
731 if (l
== opts
->bracket_include
)
732 fprintf (stderr
, _("#include <...> search starts here:\n"));
733 fprintf (stderr
, " %s\n", l
->name
);
735 fprintf (stderr
, _("End of search list.\n"));
738 initialize_dependency_output (pfile
);
740 /* Open the main input file. This must be done before -D processing
741 so we have a buffer to stand on. */
742 if (opts
->in_fname
== NULL
|| *opts
->in_fname
== 0)
744 opts
->in_fname
= fname
;
745 if (opts
->in_fname
== NULL
)
749 if (!cpp_read_file (pfile
, fname
))
752 /* -D and friends may produce output, which should be identified
755 CPP_BUFFER (pfile
)->lineno
= 0;
757 /* Install __LINE__, etc. */
758 initialize_builtins (pfile
);
760 /* Do -U's, -D's and -A's in the order they were seen. */
761 p
= opts
->pending
->define_head
;
765 cpp_undef (pfile
, p
->arg
);
767 cpp_define (pfile
, p
->arg
);
774 p
= opts
->pending
->assert_head
;
778 cpp_unassert (pfile
, p
->arg
);
780 cpp_assert (pfile
, p
->arg
);
787 opts
->done_initializing
= 1;
788 CPP_BUFFER (pfile
)->lineno
= 1;
790 if (opts
->preprocessed
)
791 /* If we've already processed this code, we want to trust the #line
792 directives in the input. But we still need to update our line
793 counter accordingly. */
794 pfile
->lineno
= CPP_BUFFER (pfile
)->lineno
;
796 output_line_command (pfile
, same_file
);
797 pfile
->only_seen_white
= 2;
799 /* The -imacros files can be scanned now, but the -include files
800 have to be pushed onto the include stack and processed later,
801 in the main loop calling cpp_get_token. */
804 p
= opts
->pending
->imacros_head
;
807 if (cpp_read_file (pfile
, p
->arg
))
808 cpp_scan_buffer (pfile
);
816 p
= opts
->pending
->include_head
;
819 if (cpp_read_file (pfile
, p
->arg
))
820 output_line_command (pfile
, enter_file
);
827 free (opts
->pending
);
828 opts
->pending
= NULL
;
833 /* This is called at the end of preprocessing. It pops the
834 last buffer and writes dependency output. It should also
835 clear macro definitions, such that you could call cpp_start_read
836 with a new filename to restart processing. */
841 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
843 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile
)) != CPP_NULL_BUFFER (pfile
))
844 cpp_ice (pfile
, "buffers still stacked in cpp_finish");
845 cpp_pop_buffer (pfile
);
847 /* Don't write the deps file if preprocessing has failed. */
848 if (opts
->print_deps
&& pfile
->errors
== 0)
850 /* Stream on which to print the dependency information. */
851 FILE *deps_stream
= 0;
853 const char *deps_mode
= opts
->print_deps_append
? "a" : "w";
854 if (opts
->deps_file
== 0)
855 deps_stream
= stdout
;
856 else if ((deps_stream
= fopen (opts
->deps_file
, deps_mode
)) == 0)
857 cpp_notice_from_errno (pfile
, opts
->deps_file
);
861 deps_write (pfile
->deps
, deps_stream
, 72);
864 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
865 cpp_fatal (pfile
, "I/O error on output");
870 if (opts
->dump_macros
== dump_only
)
874 for (i
= HASHSIZE
; --i
>= 0;)
876 for (h
= pfile
->hashtab
[i
]; h
; h
= h
->next
)
877 if (h
->type
== T_MACRO
)
879 dump_definition (pfile
, h
->name
, h
->length
, h
->value
.defn
);
880 CPP_PUTC (pfile
, '\n');
887 new_pending_define (opts
, text
)
888 struct cpp_options
*opts
;
891 struct pending_option
*o
= (struct pending_option
*)
892 xmalloc (sizeof (struct pending_option
));
894 o
->arg
= (char *) text
;
897 APPEND (opts
->pending
, define
, o
);
900 /* Handle one command-line option in (argc, argv).
901 Can be called multiple times, to handle multiple sets of options.
902 Returns number of strings consumed. */
905 cpp_handle_option (pfile
, argc
, argv
)
910 struct cpp_options
*opts
= CPP_OPTIONS (pfile
);
913 if (argv
[i
][0] != '-')
915 if (opts
->out_fname
!= NULL
)
918 cpp_fatal (pfile
, "Too many arguments");
920 else if (opts
->in_fname
!= NULL
)
921 opts
->out_fname
= argv
[i
];
923 opts
->in_fname
= argv
[i
];
929 if (!strcmp (argv
[i
], "-fleading-underscore"))
930 user_label_prefix
= "_";
931 else if (!strcmp (argv
[i
], "-fno-leading-underscore"))
932 user_label_prefix
= "";
933 else if (!strcmp (argv
[i
], "-fpreprocessed"))
934 opts
->preprocessed
= 1;
935 else if (!strcmp (argv
[i
], "-fno-preprocessed"))
936 opts
->preprocessed
= 0;
943 case 'I': /* Add directory to path for includes. */
944 if (!strcmp (argv
[i
] + 2, "-"))
947 Use the preceding -I directories for #include "..."
948 but not #include <...>.
949 Don't search the directory of the present file
950 for #include "...". (Note that -I. -I- is not the same as
951 the default setup; -I. uses the compiler's working dir.) */
952 if (! opts
->ignore_srcdir
)
954 opts
->ignore_srcdir
= 1;
955 opts
->pending
->quote_head
= opts
->pending
->brack_head
;
956 opts
->pending
->quote_tail
= opts
->pending
->brack_tail
;
957 opts
->pending
->brack_head
= 0;
958 opts
->pending
->brack_tail
= 0;
962 cpp_fatal (pfile
, "-I- specified twice");
971 else if (i
+ 1 == argc
)
972 goto missing_dirname
;
975 append_include_chain (pfile
, opts
->pending
,
976 xstrdup (fname
), BRACKET
, 0);
981 /* Add directory to beginning of system include path, as a system
982 include directory. */
983 if (!strcmp (argv
[i
], "-isystem"))
986 goto missing_filename
;
987 append_include_chain (pfile
, opts
->pending
,
988 xstrdup (argv
[++i
]), SYSTEM
, 0);
990 else if (!strcmp (argv
[i
], "-include"))
993 goto missing_filename
;
996 struct pending_option
*o
= (struct pending_option
*)
997 xmalloc (sizeof (struct pending_option
));
1000 /* This list has to be built in reverse order so that
1001 when cpp_start_read pushes all the -include files onto
1002 the buffer stack, they will be scanned in forward order. */
1003 o
->next
= opts
->pending
->include_head
;
1004 opts
->pending
->include_head
= o
;
1007 else if (!strcmp (argv
[i
], "-imacros"))
1010 goto missing_filename
;
1013 struct pending_option
*o
= (struct pending_option
*)
1014 xmalloc (sizeof (struct pending_option
));
1018 APPEND (opts
->pending
, imacros
, o
);
1021 /* Add directory to end of path for includes,
1022 with the default prefix at the front of its name. */
1023 else if (!strcmp (argv
[i
], "-iwithprefix"))
1028 goto missing_dirname
;
1030 len
= strlen (argv
[i
]);
1032 if (opts
->include_prefix
!= 0)
1034 fname
= xmalloc (opts
->include_prefix_len
+ len
+ 1);
1035 memcpy (fname
, opts
->include_prefix
, opts
->include_prefix_len
);
1036 memcpy (fname
+ opts
->include_prefix_len
, argv
[i
], len
+ 1);
1040 fname
= xmalloc (sizeof GCC_INCLUDE_DIR
- 8 + len
);
1041 memcpy (fname
, GCC_INCLUDE_DIR
, sizeof GCC_INCLUDE_DIR
- 9);
1042 memcpy (fname
+ sizeof GCC_INCLUDE_DIR
- 9, argv
[i
], len
+ 1);
1045 append_include_chain (pfile
, opts
->pending
, fname
, SYSTEM
, 0);
1047 /* Add directory to main path for includes,
1048 with the default prefix at the front of its name. */
1049 else if (!strcmp (argv
[i
], "-iwithprefixbefore"))
1054 goto missing_dirname
;
1056 len
= strlen (argv
[i
]);
1058 if (opts
->include_prefix
!= 0)
1060 fname
= xmalloc (opts
->include_prefix_len
+ len
+ 1);
1061 memcpy (fname
, opts
->include_prefix
, opts
->include_prefix_len
);
1062 memcpy (fname
+ opts
->include_prefix_len
, argv
[i
], len
+ 1);
1066 fname
= xmalloc (sizeof GCC_INCLUDE_DIR
- 8 + len
);
1067 memcpy (fname
, GCC_INCLUDE_DIR
, sizeof GCC_INCLUDE_DIR
- 9);
1068 memcpy (fname
+ sizeof GCC_INCLUDE_DIR
- 9, argv
[i
], len
+ 1);
1071 append_include_chain (pfile
, opts
->pending
, fname
, BRACKET
, 0);
1073 /* Add directory to end of path for includes. */
1074 else if (!strcmp (argv
[i
], "-idirafter"))
1077 goto missing_dirname
;
1078 append_include_chain (pfile
, opts
->pending
,
1079 xstrdup (argv
[++i
]), AFTER
, 0);
1081 else if (!strcmp (argv
[i
], "-iprefix"))
1084 goto missing_filename
;
1087 opts
->include_prefix
= argv
[++i
];
1088 opts
->include_prefix_len
= strlen (argv
[i
]);
1094 if (opts
->out_fname
!= NULL
)
1096 cpp_fatal (pfile
, "Output filename specified twice");
1100 goto missing_filename
;
1101 opts
->out_fname
= argv
[++i
];
1102 if (!strcmp (opts
->out_fname
, "-"))
1103 opts
->out_fname
= "";
1107 if (!strcmp (argv
[i
], "-pedantic"))
1109 else if (!strcmp (argv
[i
], "-pedantic-errors"))
1112 opts
->pedantic_errors
= 1;
1117 if (!strcmp (argv
[i
], "-traditional"))
1119 opts
->traditional
= 1;
1120 opts
->cplusplus_comments
= 0;
1121 opts
->trigraphs
= 0;
1122 opts
->warn_trigraphs
= 0;
1124 else if (!strcmp (argv
[i
], "-trigraphs"))
1125 opts
->trigraphs
= 1;
1129 if (! strcmp (argv
[i
], "-lang-c"))
1130 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
1131 opts
->c99
= 1, opts
->objc
= 0;
1132 if (! strcmp (argv
[i
], "-lang-c89"))
1134 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0;
1135 opts
->c89
= 1, opts
->c99
= 0, opts
->objc
= 0;
1136 opts
->trigraphs
= 1;
1137 new_pending_define (opts
, "__STRICT_ANSI__");
1139 if (! strcmp (argv
[i
], "-lang-c++"))
1140 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
1141 opts
->c99
= 0, opts
->objc
= 0;
1142 if (! strcmp (argv
[i
], "-lang-objc"))
1143 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
1144 opts
->c99
= 0, opts
->objc
= 1;
1145 if (! strcmp (argv
[i
], "-lang-objc++"))
1146 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1, opts
->c89
= 0,
1147 opts
->c99
= 0, opts
->objc
= 1;
1148 if (! strcmp (argv
[i
], "-lang-asm"))
1150 if (! strcmp (argv
[i
], "-lang-fortran"))
1151 opts
->lang_fortran
= 1, opts
->cplusplus_comments
= 0;
1152 if (! strcmp (argv
[i
], "-lang-chill"))
1153 opts
->objc
= 0, opts
->cplusplus
= 0, opts
->chill
= 1,
1154 opts
->traditional
= 1;
1158 opts
->cplusplus
= 1, opts
->cplusplus_comments
= 1;
1162 if (!strcmp (argv
[i
], "-std=gnu89"))
1164 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1;
1165 opts
->c89
= 1, opts
->c99
= 0, opts
->objc
= 0;
1167 else if (!strcmp (argv
[i
], "-std=gnu9x")
1168 || !strcmp (argv
[i
], "-std=gnu99"))
1170 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1;
1171 opts
->c89
= 0, opts
->c99
= 1, opts
->objc
= 0;
1172 new_pending_define (opts
, "__STDC_VERSION__=199901L");
1174 else if (!strcmp (argv
[i
], "-std=iso9899:1990")
1175 || !strcmp (argv
[i
], "-std=c89"))
1177 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0;
1178 opts
->c89
= 1, opts
->c99
= 0, opts
->objc
= 0;
1179 opts
->trigraphs
= 1;
1180 new_pending_define (opts
, "__STRICT_ANSI__");
1182 else if (!strcmp (argv
[i
], "-std=iso9899:199409"))
1184 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 0;
1185 opts
->c89
= 1, opts
->c99
= 0, opts
->objc
= 0;
1186 opts
->trigraphs
= 1;
1187 new_pending_define (opts
, "__STRICT_ANSI__");
1188 new_pending_define (opts
, "__STDC_VERSION__=199409L");
1190 else if (!strcmp (argv
[i
], "-std=iso9899:199x")
1191 || !strcmp (argv
[i
], "-std=iso9899:1999")
1192 || !strcmp (argv
[i
], "-std=c9x")
1193 || !strcmp (argv
[i
], "-std=c99"))
1195 opts
->cplusplus
= 0, opts
->cplusplus_comments
= 1;
1196 opts
->c89
= 0, opts
->c99
= 1, opts
->objc
= 0;
1197 opts
->trigraphs
= 1;
1198 new_pending_define (opts
, "__STRICT_ANSI__");
1199 new_pending_define (opts
, "__STDC_VERSION__=199901L");
1204 opts
->inhibit_warnings
= 1;
1208 if (!strcmp (argv
[i
], "-Wtrigraphs"))
1209 opts
->warn_trigraphs
= 1;
1210 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1211 opts
->warn_trigraphs
= 0;
1212 else if (!strcmp (argv
[i
], "-Wcomment"))
1213 opts
->warn_comments
= 1;
1214 else if (!strcmp (argv
[i
], "-Wno-comment"))
1215 opts
->warn_comments
= 0;
1216 else if (!strcmp (argv
[i
], "-Wcomments"))
1217 opts
->warn_comments
= 1;
1218 else if (!strcmp (argv
[i
], "-Wno-comments"))
1219 opts
->warn_comments
= 0;
1220 else if (!strcmp (argv
[i
], "-Wtraditional"))
1221 opts
->warn_stringify
= 1;
1222 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1223 opts
->warn_stringify
= 0;
1224 else if (!strcmp (argv
[i
], "-Wundef"))
1225 opts
->warn_undef
= 1;
1226 else if (!strcmp (argv
[i
], "-Wno-undef"))
1227 opts
->warn_undef
= 0;
1228 else if (!strcmp (argv
[i
], "-Wimport"))
1229 opts
->warn_import
= 1;
1230 else if (!strcmp (argv
[i
], "-Wno-import"))
1231 opts
->warn_import
= 0;
1232 else if (!strcmp (argv
[i
], "-Werror"))
1233 opts
->warnings_are_errors
= 1;
1234 else if (!strcmp (argv
[i
], "-Wno-error"))
1235 opts
->warnings_are_errors
= 0;
1236 else if (!strcmp (argv
[i
], "-Wall"))
1238 opts
->warn_trigraphs
= 1;
1239 opts
->warn_comments
= 1;
1244 /* The style of the choices here is a bit mixed.
1245 The chosen scheme is a hybrid of keeping all options in one string
1246 and specifying each option in a separate argument:
1247 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1248 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1249 -M[M][G][D file]. This is awkward to handle in specs, and is not
1251 /* ??? -MG must be specified in addition to one of -M or -MM.
1252 This can be relaxed in the future without breaking anything.
1253 The converse isn't true. */
1255 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1256 if (!strcmp (argv
[i
], "-MG"))
1258 opts
->print_deps_missing_files
= 1;
1261 if (!strcmp (argv
[i
], "-M"))
1262 opts
->print_deps
= 2;
1263 else if (!strcmp (argv
[i
], "-MM"))
1264 opts
->print_deps
= 1;
1265 else if (!strcmp (argv
[i
], "-MD"))
1266 opts
->print_deps
= 2;
1267 else if (!strcmp (argv
[i
], "-MMD"))
1268 opts
->print_deps
= 1;
1269 /* For -MD and -MMD options, write deps on file named by next arg. */
1270 if (!strcmp (argv
[i
], "-MD") || !strcmp (argv
[i
], "-MMD"))
1273 goto missing_filename
;
1274 opts
->deps_file
= argv
[++i
];
1278 /* For -M and -MM, write deps on standard output
1279 and suppress the usual output. */
1280 opts
->no_output
= 1;
1286 char *p
= argv
[i
] + 2;
1288 while ((c
= *p
++) != 0)
1290 /* Arg to -d specifies what parts of macros to dump */
1294 opts
->dump_macros
= dump_only
;
1295 opts
->no_output
= 1;
1298 opts
->dump_macros
= dump_names
;
1301 opts
->dump_macros
= dump_definitions
;
1304 opts
->dump_includes
= 1;
1312 if (argv
[i
][2] == '3')
1313 opts
->debug_output
= 1;
1317 if (!strcmp (argv
[i
], "--help"))
1319 else if (!strcmp (argv
[i
], "--version"))
1320 fprintf (stderr
, _("GNU CPP version %s (cpplib)\n"), version_string
);
1325 fprintf (stderr
, _("GNU CPP version %s (cpplib)\n"), version_string
);
1326 #ifdef TARGET_VERSION
1329 fputc ('\n', stderr
);
1334 opts
->print_include_names
= 1;
1340 if (argv
[i
][2] != 0)
1342 else if (i
+ 1 == argc
)
1344 cpp_fatal (pfile
, "Macro name missing after -D option");
1349 new_pending_define (opts
, text
);
1357 if (argv
[i
][2] != 0)
1359 else if (i
+ 1 == argc
)
1361 cpp_fatal (pfile
, "Assertion missing after -A option");
1367 if (strcmp (p
, "-"))
1369 struct pending_option
*o
= (struct pending_option
*)
1370 xmalloc (sizeof (struct pending_option
));
1375 APPEND (opts
->pending
, assert, o
);
1379 /* -A- eliminates all predefined macros and assertions.
1380 Let's include also any that were specified earlier
1381 on the command line. That way we can get rid of any
1382 that were passed automatically in from GCC. */
1383 struct pending_option
*o1
, *o2
;
1385 o1
= opts
->pending
->define_head
;
1392 o1
= opts
->pending
->assert_head
;
1399 opts
->pending
->assert_head
= NULL
;
1400 opts
->pending
->assert_tail
= NULL
;
1401 opts
->pending
->define_head
= NULL
;
1402 opts
->pending
->define_tail
= NULL
;
1409 struct pending_option
*o
= (struct pending_option
*)
1410 xmalloc (sizeof (struct pending_option
));
1412 if (argv
[i
][2] != 0)
1413 o
->arg
= argv
[i
] + 2;
1414 else if (i
+ 1 == argc
)
1416 cpp_fatal (pfile
, "Macro name missing after -U option");
1424 APPEND (opts
->pending
, define
, o
);
1429 opts
->discard_comments
= 0;
1432 case 'E': /* -E comes from cc -E; ignore it. */
1436 opts
->no_line_commands
= 1;
1439 case '$': /* Don't include $ in identifiers. */
1440 opts
->dollars_in_ident
= 0;
1444 if (!strcmp (argv
[i
], "-nostdinc"))
1445 /* -nostdinc causes no default include directories.
1446 You must specify all include-file directories with -I. */
1447 opts
->no_standard_includes
= 1;
1448 else if (!strcmp (argv
[i
], "-nostdinc++"))
1449 /* -nostdinc++ causes no default C++-specific include directories. */
1450 opts
->no_standard_cplusplus_includes
= 1;
1454 if (!strcmp (argv
[i
], "-remap"))
1458 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1459 if (opts
->in_fname
== NULL
)
1460 opts
->in_fname
= "";
1461 else if (opts
->out_fname
== NULL
)
1462 opts
->out_fname
= "";
1464 return i
; /* error */
1474 cpp_fatal (pfile
, "Filename missing after `%s' option", argv
[i
]);
1477 cpp_fatal (pfile
, "Directory name missing after `%s' option", argv
[i
]);
1481 /* Handle command-line options in (argc, argv).
1482 Can be called multiple times, to handle multiple sets of options.
1483 Returns if an unrecognized option is seen.
1484 Returns number of strings consumed. */
1487 cpp_handle_options (pfile
, argc
, argv
)
1493 int strings_processed
;
1494 for (i
= 0; i
< argc
; i
+= strings_processed
)
1496 strings_processed
= cpp_handle_option (pfile
, argc
- i
, argv
+ i
);
1497 if (strings_processed
== 0)
1506 fprintf (stderr
, _("Usage: %s [switches] input output\n"), progname
);
1509 -include <file> Include the contents of <file> before other files\n\
1510 -imacros <file> Accept definition of macros in <file>\n\
1511 -iprefix <path> Specify <path> as a prefix for next two options\n\
1512 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1513 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1514 -isystem <dir> Add <dir> to the start of the system include path\n\
1515 -idirafter <dir> Add <dir> to the end of the system include path\n\
1516 -I <dir> Add <dir> to the end of the main include path\n\
1517 -nostdinc Do not search system include directories\n\
1518 (dirs specified with -isystem will still be used)\n\
1519 -nostdinc++ Do not search system include directories for C++\n\
1520 -o <file> Put output into <file>\n\
1521 -pedantic Issue all warnings demanded by strict ANSI C\n\
1522 -traditional Follow K&R pre-processor behaviour\n\
1523 -trigraphs Support ANSI C trigraphs\n\
1524 -lang-c Assume that the input sources are in C\n\
1525 -lang-c89 Assume that the input sources are in C89\n\
1526 -lang-c++ Assume that the input sources are in C++\n\
1527 -lang-objc Assume that the input sources are in ObjectiveC\n\
1528 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1529 -lang-asm Assume that the input sources are in assembler\n\
1530 -lang-fortran Assume that the input sources are in Fortran\n\
1531 -lang-chill Assume that the input sources are in Chill\n\
1532 -std=<std name> Specify the conformance standard; one of:\n\
1533 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1534 iso9899:199409, iso9899:1999\n\
1535 -+ Allow parsing of C++ style features\n\
1536 -w Inhibit warning messages\n\
1537 -Wtrigraphs Warn if trigraphs are encountered\n\
1538 -Wno-trigraphs Do not warn about trigraphs\n\
1539 -Wcomment{s} Warn if one comment starts inside another\n\
1540 -Wno-comment{s} Do not warn about comments\n\
1541 -Wtraditional Warn if a macro argument is/would be turned into\n\
1542 a string if -traditional is specified\n\
1543 -Wno-traditional Do not warn about stringification\n\
1544 -Wundef Warn if an undefined macro is used by #if\n\
1545 -Wno-undef Do not warn about testing undefined macros\n\
1546 -Wimport Warn about the use of the #import directive\n\
1547 -Wno-import Do not warn about the use of #import\n\
1548 -Werror Treat all warnings as errors\n\
1549 -Wno-error Do not treat warnings as errors\n\
1550 -Wall Enable all preprocessor warnings\n\
1551 -M Generate make dependencies\n\
1552 -MM As -M, but ignore system header files\n\
1553 -MD As -M, but put output in a .d file\n\
1554 -MMD As -MD, but ignore system header files\n\
1555 -MG Treat missing header file as generated files\n\
1556 -g Include #define and #undef directives in the output\n\
1557 -D<macro> Define a <macro> with string '1' as its value\n\
1558 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1559 -A<question> (<answer>) Assert the <answer> to <question>\n\
1560 -U<macro> Undefine <macro> \n\
1561 -v Display the version number\n\
1562 -H Print the name of header files as they are used\n\
1563 -C Do not discard comments\n\
1564 -dM Display a list of macro definitions active at end\n\
1565 -dD Preserve macro definitions in output\n\
1566 -dN As -dD except that only the names are preserved\n\
1567 -dI Include #include directives in the output\n\
1568 -P Do not generate #line directives\n\
1569 -$ Do not allow '$' in identifiers\n\
1570 -remap Remap file names when including files.\n\
1571 -h or --help Display this information\n\