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 char *base_name PARAMS ((const char *));
182 static void dump_special_to_buffer PARAMS ((cpp_reader *, const char *));
183 static void initialize_dependency_output PARAMS ((cpp_reader *));
184 static void initialize_standard_includes PARAMS ((cpp_reader *));
185 static void new_pending_define PARAMS ((struct cpp_options *,
188 /* Fourth argument to append_include_chain: chain to use */
189 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
191 /* If we have designated initializers (GCC >2.7, or C99) this table
192 can be initialized, constant data. Otherwise, it has to be filled
195 #if (GCC_VERSION >= 2007) || (__STDC_VERSION__ >= 199901L)
196 #define init_IStable() /* nothing */
197 #define ISTABLE const unsigned char _cpp_IStable[256] = {
199 #define s(p, v) [p] = v,
201 #define ISTABLE unsigned char _cpp_IStable[256] = { 0 }; \
202 static void init_IStable PARAMS ((void)) { \
203 unsigned char *x = _cpp_IStable;
205 #define s(p, v) x[p] = v;
208 #define A(x) s(x, ISidnum|ISidstart)
209 #define N(x) s(x, ISidnum|ISnumstart)
210 #define H(x) s(x, IShspace|ISspace)
211 #define S(x) s(x, ISspace)
216 A('a') A('b') A('c') A('d') A('e') A('f') A('g') A('h') A('i')
217 A('j') A('k') A('l') A('m') A('n') A('o') A('p') A('q') A('r')
218 A('s') A('t') A('u') A('v') A('w') A('x') A('y') A('z')
220 A('A') A('B') A('C') A('D') A('E') A('F') A('G') A('H') A('I')
221 A('J') A('K') A('L') A('M') A('N') A('O') A('P') A('Q') A('R')
222 A('S') A('T') A('U') A('V') A('W') A('X') A('Y') A('Z')
224 N('1') N('2') N('3') N('4') N('5') N('6') N('7') N('8') N('9') N('0')
226 H(' ') H('\t') H('\v') H('\f')
239 /* Given a colon-separated list of file names PATH,
240 add all the names to the search path for include files. */
243 path_include (pfile, pend, list, path)
245 struct cpp_pending *pend;
255 /* Find the end of this name. */
257 while (*q != 0 && *q != PATH_SEPARATOR) q++;
260 /* An empty name in the path stands for the current directory. */
261 name = (char *) xmalloc (2);
267 /* Otherwise use the directory that is named. */
268 name = (char *) xmalloc (q - p + 1);
269 memcpy (name, p, q - p);
273 append_include_chain (pfile, pend, name, path, 0);
275 /* Advance past this name. */
283 /* Append DIR to include path PATH. DIR must be permanently allocated
286 append_include_chain (pfile, pend, dir, path, cxx_aware)
288 struct cpp_pending *pend;
293 struct file_name_list *new;
297 simplify_pathname (dir);
300 /* Dirs that don't exist are silently ignored. */
302 cpp_notice_from_errno (pfile, dir);
303 else if (CPP_OPTIONS (pfile)->verbose)
304 fprintf (stderr, _("ignoring nonexistent directory `%s'\n"), dir);
308 if (!S_ISDIR (st.st_mode))
310 cpp_notice (pfile, "%s: Not a directory", dir);
315 if (len > pfile->max_include_len)
316 pfile->max_include_len = len;
318 new = (struct file_name_list *)xmalloc (sizeof (struct file_name_list));
321 new->ino = st.st_ino;
322 new->dev = st.st_dev;
324 new->sysp = cxx_aware ? 1 : 2;
327 new->name_map = NULL;
333 case QUOTE: APPEND (pend, quote, new); break;
334 case BRACKET: APPEND (pend, brack, new); break;
335 case SYSTEM: APPEND (pend, systm, new); break;
336 case AFTER: APPEND (pend, after, new); break;
341 /* Write out a #define command for the special named MACRO_NAME
342 to PFILE's token_buffer. */
345 dump_special_to_buffer (pfile, macro_name)
347 const char *macro_name;
349 static const char define_directive[] = "#define ";
350 int macro_name_length = strlen (macro_name);
351 output_line_command (pfile, same_file);
352 CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
353 CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
354 CPP_PUTS_Q (pfile, macro_name, macro_name_length);
355 CPP_PUTC_Q (pfile, ' ');
356 cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
357 CPP_PUTC (pfile, '\n');
360 /* Initialize a cpp_options structure. */
362 cpp_options_init (opts)
365 bzero ((char *) opts, sizeof (struct cpp_options));
367 opts->dollars_in_ident = 1;
368 opts->cplusplus_comments = 1;
369 opts->warn_import = 1;
370 opts->discard_comments = 1;
373 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
376 /* Initialize a cpp_reader structure. */
378 cpp_reader_init (pfile)
381 bzero ((char *) pfile, sizeof (cpp_reader));
383 pfile->token_buffer_size = 200;
384 pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
385 CPP_SET_WRITTEN (pfile, 0);
387 pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *));
390 /* Free resources used by PFILE.
391 This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology). */
397 while (CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
398 cpp_pop_buffer (pfile);
400 if (pfile->token_buffer)
402 free (pfile->token_buffer);
403 pfile->token_buffer = NULL;
406 if (pfile->input_buffer)
408 free (pfile->input_buffer);
409 free (pfile->input_speccase);
410 pfile->input_buffer = pfile->input_speccase = NULL;
411 pfile->input_buffer_len = 0;
415 deps_free (pfile->deps);
417 while (pfile->if_stack)
419 IF_STACK_FRAME *temp = pfile->if_stack;
420 pfile->if_stack = temp->next;
424 for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
426 struct include_hash *imp = pfile->all_include_files[i];
429 struct include_hash *next = imp->next;
431 free ((PTR) imp->name);
435 pfile->all_include_files[i] = 0;
438 for (i = HASHSIZE; --i >= 0;)
440 while (pfile->hashtab[i])
441 delete_macro (pfile->hashtab[i]);
443 free (pfile->hashtab);
447 /* This structure defines one built-in macro. A node of type TYPE will
448 be entered in the macro hash table under the name NAME, with value
449 VALUE (if any). FLAGS tweaks the behavior a little:
450 DUMP write debug info for this macro
451 STDC define only if not -traditional
452 ULP value is the global user_label_prefix (which can't be
453 put directly into the table).
461 unsigned short flags;
467 static const struct builtin builtin_array[] =
469 { "__TIME__", 0, T_TIME, DUMP },
470 { "__DATE__", 0, T_DATE, DUMP },
471 { "__FILE__", 0, T_FILE, 0 },
472 { "__BASE_FILE__", 0, T_BASE_FILE, 0 },
473 { "__LINE__", 0, T_SPECLINE, 0 },
474 { "__INCLUDE_LEVEL__", 0, T_INCLUDE_LEVEL, 0 },
475 { "__VERSION__", 0, T_VERSION, DUMP },
476 { "__STDC__", 0, T_STDC, DUMP|STDC },
478 { "__USER_LABEL_PREFIX__", 0, T_CONST, ULP },
479 { "__REGISTER_PREFIX__", REGISTER_PREFIX, T_CONST, 0 },
480 { "__HAVE_BUILTIN_SETJMP__", "1", T_CONST, 0 },
481 #ifndef NO_BUILTIN_SIZE_TYPE
482 { "__SIZE_TYPE__", SIZE_TYPE, T_CONST, DUMP },
484 #ifndef NO_BUILTIN_PTRDIFF_TYPE
485 { "__PTRDIFF_TYPE__", PTRDIFF_TYPE, T_CONST, DUMP },
487 #ifndef NO_BUILTIN_WCHAR_TYPE
488 { "__WCHAR_TYPE__", WCHAR_TYPE, T_CONST, DUMP },
493 /* Subroutine of cpp_start_read; reads the builtins table above and
494 enters the macros into the hash table. */
497 initialize_builtins (pfile)
501 const struct builtin *b;
503 for(b = builtin_array; b->name; b++)
505 if ((b->flags & STDC) && CPP_TRADITIONAL (pfile))
508 val = (b->flags & ULP) ? user_label_prefix : b->value;
509 len = strlen (b->name);
511 cpp_install (pfile, b->name, len, b->type, val);
512 if ((b->flags & DUMP) && CPP_OPTIONS (pfile)->debug_output)
513 dump_special_to_buffer (pfile, b->name);
521 /* Another subroutine of cpp_start_read. This one sets up to do
522 dependency-file output. */
524 initialize_dependency_output (pfile)
527 cpp_options *opts = CPP_OPTIONS (pfile);
528 char *spec, *s, *output_file;
530 /* Either of two environment variables can specify output of deps.
531 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
532 where OUTPUT_FILE is the file to write deps info to
533 and DEPS_TARGET is the target to mention in the deps. */
535 if (opts->print_deps == 0)
537 spec = getenv ("DEPENDENCIES_OUTPUT");
539 opts->print_deps = 1;
542 spec = getenv ("SUNPRO_DEPENDENCIES");
544 opts->print_deps = 2;
549 /* Find the space before the DEPS_TARGET, if there is one. */
550 s = strchr (spec, ' ');
553 opts->deps_target = s + 1;
554 output_file = (char *) xmalloc (s - spec + 1);
555 memcpy (output_file, spec, s - spec);
556 output_file[s - spec] = 0;
560 opts->deps_target = 0;
564 opts->deps_file = output_file;
565 opts->print_deps_append = 1;
568 pfile->deps = deps_init ();
570 /* Print the expected object file name as the target of this Make-rule. */
571 if (opts->deps_target)
572 deps_add_target (pfile->deps, opts->deps_target);
573 else if (*opts->in_fname == 0)
574 deps_add_target (pfile->deps, "-");
576 deps_calc_target (pfile->deps, opts->in_fname);
579 deps_add_dep (pfile->deps, opts->in_fname);
582 /* And another subroutine. This one sets up the standard include path. */
584 initialize_standard_includes (pfile)
587 cpp_options *opts = CPP_OPTIONS (pfile);
589 const struct default_include *p;
590 char *specd_prefix = opts->include_prefix;
592 /* Several environment variables may add to the include search path.
593 CPATH specifies an additional list of directories to be searched
594 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
595 etc. specify an additional list of directories to be searched as
596 if specified with -isystem, for the language indicated. */
598 GET_ENV_PATH_LIST (path, "CPATH");
599 if (path != 0 && *path != 0)
600 path_include (pfile, opts->pending, path, BRACKET);
602 switch ((opts->objc << 1) + opts->cplusplus)
605 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
608 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
611 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
614 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
617 if (path != 0 && *path != 0)
618 path_include (pfile, opts->pending, path, SYSTEM);
620 /* Search "translated" versions of GNU directories.
621 These have /usr/local/lib/gcc... replaced by specd_prefix. */
622 if (specd_prefix != 0)
624 char *default_prefix = alloca (sizeof GCC_INCLUDE_DIR - 7);
625 /* Remove the `include' from /usr/local/lib/gcc.../include.
626 GCC_INCLUDE_DIR will always end in /include. */
627 int default_len = sizeof GCC_INCLUDE_DIR - 8;
628 int specd_len = strlen (specd_prefix);
630 memcpy (default_prefix, GCC_INCLUDE_DIR, default_len);
631 default_prefix[default_len] = '\0';
633 for (p = include_defaults_array; p->fname; p++)
635 /* Some standard dirs are only for C++. */
638 && !opts->no_standard_cplusplus_includes))
640 /* Does this dir start with the prefix? */
641 if (!strncmp (p->fname, default_prefix, default_len))
643 /* Yes; change prefix and add to search list. */
644 int flen = strlen (p->fname);
645 int this_len = specd_len + flen - default_len;
646 char *str = (char *) xmalloc (this_len + 1);
647 memcpy (str, specd_prefix, specd_len);
648 memcpy (str + specd_len,
649 p->fname + default_len,
650 flen - default_len + 1);
652 append_include_chain (pfile, opts->pending,
653 str, SYSTEM, p->cxx_aware);
659 /* Search ordinary names for GNU include directories. */
660 for (p = include_defaults_array; p->fname; p++)
662 /* Some standard dirs are only for C++. */
665 && !opts->no_standard_cplusplus_includes))
667 /* XXX Potential memory leak! */
668 char *str = xstrdup (update_path (p->fname, p->component));
669 append_include_chain (pfile, opts->pending, str, SYSTEM,
675 /* This is called after options have been processed.
676 * Check options for consistency, and setup for processing input
677 * from the file named FNAME. (Use standard input if FNAME==NULL.)
678 * Return 1 on success, 0 on failure.
682 cpp_start_read (pfile, fname)
686 struct cpp_options *opts = CPP_OPTIONS (pfile);
687 struct pending_option *p, *q;
689 /* -MG doesn't select the form of output and must be specified with one of
690 -M or -MM. -MG doesn't make sense with -MD or -MMD since they don't
691 inhibit compilation. */
692 if (opts->print_deps_missing_files
693 && (opts->print_deps == 0 || !opts->no_output))
695 cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
699 /* Chill should not be used with -trigraphs. */
700 if (opts->chill && opts->trigraphs)
702 cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
706 /* Set this if it hasn't been set already. */
707 if (user_label_prefix == NULL)
708 user_label_prefix = USER_LABEL_PREFIX;
710 /* Don't bother trying to do macro expansion if we've already done
712 if (opts->preprocessed)
713 pfile->no_macro_expand++;
715 /* Set up the IStable. This doesn't do anything if we were compiled
716 with a compiler that supports C99 designated initializers. */
719 /* Set up the include search path now. */
720 if (! opts->no_standard_includes)
721 initialize_standard_includes (pfile);
723 merge_include_chains (opts);
725 /* With -v, print the list of dirs to search. */
728 struct file_name_list *l;
729 fprintf (stderr, _("#include \"...\" search starts here:\n"));
730 for (l = opts->quote_include; l; l = l->next)
732 if (l == opts->bracket_include)
733 fprintf (stderr, _("#include <...> search starts here:\n"));
734 fprintf (stderr, " %s\n", l->name);
736 fprintf (stderr, _("End of search list.\n"));
739 initialize_dependency_output (pfile);
741 /* Open the main input file. This must be done before -D processing
742 so we have a buffer to stand on. */
743 if (opts->in_fname == NULL || *opts->in_fname == 0)
745 opts->in_fname = fname;
746 if (opts->in_fname == NULL)
750 if (!cpp_read_file (pfile, fname))
753 /* -D and friends may produce output, which should be identified
756 CPP_BUFFER (pfile)->lineno = 0;
758 /* Install __LINE__, etc. */
759 initialize_builtins (pfile);
761 /* Do -U's, -D's and -A's in the order they were seen. */
762 p = opts->pending->define_head;
766 cpp_undef (pfile, p->arg);
768 cpp_define (pfile, p->arg);
775 p = opts->pending->assert_head;
779 cpp_unassert (pfile, p->arg);
781 cpp_assert (pfile, p->arg);
788 opts->done_initializing = 1;
789 CPP_BUFFER (pfile)->lineno = 1;
791 if (opts->preprocessed)
792 /* If we've already processed this code, we want to trust the #line
793 directives in the input. But we still need to update our line
794 counter accordingly. */
795 pfile->lineno = CPP_BUFFER (pfile)->lineno;
797 output_line_command (pfile, same_file);
798 pfile->only_seen_white = 2;
800 /* The -imacros files can be scanned now, but the -include files
801 have to be pushed onto the include stack and processed later,
802 in the main loop calling cpp_get_token. */
805 p = opts->pending->imacros_head;
808 if (cpp_read_file (pfile, p->arg))
809 cpp_scan_buffer (pfile);
817 p = opts->pending->include_head;
820 if (cpp_read_file (pfile, p->arg))
821 output_line_command (pfile, enter_file);
828 free (opts->pending);
829 opts->pending = NULL;
834 /* This is called at the end of preprocessing. It pops the
835 last buffer and writes dependency output. It should also
836 clear macro definitions, such that you could call cpp_start_read
837 with a new filename to restart processing. */
842 struct cpp_options *opts = CPP_OPTIONS (pfile);
844 if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != CPP_NULL_BUFFER (pfile))
845 cpp_ice (pfile, "buffers still stacked in cpp_finish");
846 cpp_pop_buffer (pfile);
848 /* Don't write the deps file if preprocessing has failed. */
849 if (opts->print_deps && pfile->errors == 0)
851 /* Stream on which to print the dependency information. */
852 FILE *deps_stream = 0;
854 const char *deps_mode = opts->print_deps_append ? "a" : "w";
855 if (opts->deps_file == 0)
856 deps_stream = stdout;
857 else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
858 cpp_notice_from_errno (pfile, opts->deps_file);
862 deps_write (pfile->deps, deps_stream, 72);
865 if (ferror (deps_stream) || fclose (deps_stream) != 0)
866 cpp_fatal (pfile, "I/O error on output");
871 if (opts->dump_macros == dump_only)
875 for (i = HASHSIZE; --i >= 0;)
877 for (h = pfile->hashtab[i]; h; h = h->next)
878 if (h->type == T_MACRO)
880 dump_definition (pfile, h->name, h->length, h->value.defn);
881 CPP_PUTC (pfile, '\n');
888 new_pending_define (opts, text)
889 struct cpp_options *opts;
892 struct pending_option *o = (struct pending_option *)
893 xmalloc (sizeof (struct pending_option));
895 o->arg = (char *) text;
898 APPEND (opts->pending, define, o);
901 /* Handle one command-line option in (argc, argv).
902 Can be called multiple times, to handle multiple sets of options.
903 Returns number of strings consumed. */
906 cpp_handle_option (pfile, argc, argv)
911 struct cpp_options *opts = CPP_OPTIONS (pfile);
914 if (argv[i][0] != '-')
916 if (opts->out_fname != NULL)
919 cpp_fatal (pfile, "Too many arguments");
921 else if (opts->in_fname != NULL)
922 opts->out_fname = argv[i];
924 opts->in_fname = argv[i];
930 if (!strcmp (argv[i], "-fleading-underscore"))
931 user_label_prefix = "_";
932 else if (!strcmp (argv[i], "-fno-leading-underscore"))
933 user_label_prefix = "";
934 else if (!strcmp (argv[i], "-fpreprocessed"))
935 opts->preprocessed = 1;
936 else if (!strcmp (argv[i], "-fno-preprocessed"))
937 opts->preprocessed = 0;
944 case 'I': /* Add directory to path for includes. */
945 if (!strcmp (argv[i] + 2, "-"))
948 Use the preceding -I directories for #include "..."
949 but not #include <...>.
950 Don't search the directory of the present file
951 for #include "...". (Note that -I. -I- is not the same as
952 the default setup; -I. uses the compiler's working dir.) */
953 if (! opts->ignore_srcdir)
955 opts->ignore_srcdir = 1;
956 opts->pending->quote_head = opts->pending->brack_head;
957 opts->pending->quote_tail = opts->pending->brack_tail;
958 opts->pending->brack_head = 0;
959 opts->pending->brack_tail = 0;
963 cpp_fatal (pfile, "-I- specified twice");
972 else if (i + 1 == argc)
973 goto missing_dirname;
976 append_include_chain (pfile, opts->pending,
977 xstrdup (fname), BRACKET, 0);
982 /* Add directory to beginning of system include path, as a system
983 include directory. */
984 if (!strcmp (argv[i], "-isystem"))
987 goto missing_filename;
988 append_include_chain (pfile, opts->pending,
989 xstrdup (argv[++i]), SYSTEM, 0);
991 else if (!strcmp (argv[i], "-include"))
994 goto missing_filename;
997 struct pending_option *o = (struct pending_option *)
998 xmalloc (sizeof (struct pending_option));
1001 /* This list has to be built in reverse order so that
1002 when cpp_start_read pushes all the -include files onto
1003 the buffer stack, they will be scanned in forward order. */
1004 o->next = opts->pending->include_head;
1005 opts->pending->include_head = o;
1008 else if (!strcmp (argv[i], "-imacros"))
1011 goto missing_filename;
1014 struct pending_option *o = (struct pending_option *)
1015 xmalloc (sizeof (struct pending_option));
1019 APPEND (opts->pending, imacros, o);
1022 /* Add directory to end of path for includes,
1023 with the default prefix at the front of its name. */
1024 else if (!strcmp (argv[i], "-iwithprefix"))
1029 goto missing_dirname;
1031 len = strlen (argv[i]);
1033 if (opts->include_prefix != 0)
1035 fname = xmalloc (opts->include_prefix_len + len + 1);
1036 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1037 memcpy (fname + opts->include_prefix_len, argv[i], len + 1);
1041 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1042 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1043 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, argv[i], len + 1);
1046 append_include_chain (pfile, opts->pending, fname, SYSTEM, 0);
1048 /* Add directory to main path for includes,
1049 with the default prefix at the front of its name. */
1050 else if (!strcmp (argv[i], "-iwithprefixbefore"))
1055 goto missing_dirname;
1057 len = strlen (argv[i]);
1059 if (opts->include_prefix != 0)
1061 fname = xmalloc (opts->include_prefix_len + len + 1);
1062 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1063 memcpy (fname + opts->include_prefix_len, argv[i], len + 1);
1067 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1068 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1069 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, argv[i], len + 1);
1072 append_include_chain (pfile, opts->pending, fname, BRACKET, 0);
1074 /* Add directory to end of path for includes. */
1075 else if (!strcmp (argv[i], "-idirafter"))
1078 goto missing_dirname;
1079 append_include_chain (pfile, opts->pending,
1080 xstrdup (argv[++i]), AFTER, 0);
1082 else if (!strcmp (argv[i], "-iprefix"))
1085 goto missing_filename;
1088 opts->include_prefix = argv[++i];
1089 opts->include_prefix_len = strlen (argv[i]);
1095 if (opts->out_fname != NULL)
1097 cpp_fatal (pfile, "Output filename specified twice");
1101 goto missing_filename;
1102 opts->out_fname = argv[++i];
1103 if (!strcmp (opts->out_fname, "-"))
1104 opts->out_fname = "";
1108 if (!strcmp (argv[i], "-pedantic"))
1110 else if (!strcmp (argv[i], "-pedantic-errors"))
1113 opts->pedantic_errors = 1;
1118 if (!strcmp (argv[i], "-traditional"))
1120 opts->traditional = 1;
1121 opts->cplusplus_comments = 0;
1122 opts->trigraphs = 0;
1123 opts->warn_trigraphs = 0;
1125 else if (!strcmp (argv[i], "-trigraphs"))
1126 opts->trigraphs = 1;
1130 if (! strcmp (argv[i], "-lang-c"))
1131 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1132 opts->c99 = 1, opts->objc = 0;
1133 if (! strcmp (argv[i], "-lang-c89"))
1135 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1136 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1137 opts->trigraphs = 1;
1138 new_pending_define (opts, "__STRICT_ANSI__");
1140 if (! strcmp (argv[i], "-lang-c++"))
1141 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1142 opts->c99 = 0, opts->objc = 0;
1143 if (! strcmp (argv[i], "-lang-objc"))
1144 opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1145 opts->c99 = 0, opts->objc = 1;
1146 if (! strcmp (argv[i], "-lang-objc++"))
1147 opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1148 opts->c99 = 0, opts->objc = 1;
1149 if (! strcmp (argv[i], "-lang-asm"))
1151 if (! strcmp (argv[i], "-lang-fortran"))
1152 opts->lang_fortran = 1, opts->cplusplus_comments = 0;
1153 if (! strcmp (argv[i], "-lang-chill"))
1154 opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
1155 opts->traditional = 1;
1159 opts->cplusplus = 1, opts->cplusplus_comments = 1;
1163 if (!strcmp (argv[i], "-std=gnu89"))
1165 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1166 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1168 else if (!strcmp (argv[i], "-std=gnu9x")
1169 || !strcmp (argv[i], "-std=gnu99"))
1171 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1172 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1173 new_pending_define (opts, "__STDC_VERSION__=199901L");
1175 else if (!strcmp (argv[i], "-std=iso9899:1990")
1176 || !strcmp (argv[i], "-std=c89"))
1178 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1179 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1180 opts->trigraphs = 1;
1181 new_pending_define (opts, "__STRICT_ANSI__");
1183 else if (!strcmp (argv[i], "-std=iso9899:199409"))
1185 opts->cplusplus = 0, opts->cplusplus_comments = 0;
1186 opts->c89 = 1, opts->c99 = 0, opts->objc = 0;
1187 opts->trigraphs = 1;
1188 new_pending_define (opts, "__STRICT_ANSI__");
1189 new_pending_define (opts, "__STDC_VERSION__=199409L");
1191 else if (!strcmp (argv[i], "-std=iso9899:199x")
1192 || !strcmp (argv[i], "-std=iso9899:1999")
1193 || !strcmp (argv[i], "-std=c9x")
1194 || !strcmp (argv[i], "-std=c99"))
1196 opts->cplusplus = 0, opts->cplusplus_comments = 1;
1197 opts->c89 = 0, opts->c99 = 1, opts->objc = 0;
1198 opts->trigraphs = 1;
1199 new_pending_define (opts, "__STRICT_ANSI__");
1200 new_pending_define (opts, "__STDC_VERSION__=199901L");
1205 opts->inhibit_warnings = 1;
1209 if (!strcmp (argv[i], "-Wtrigraphs"))
1210 opts->warn_trigraphs = 1;
1211 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1212 opts->warn_trigraphs = 0;
1213 else if (!strcmp (argv[i], "-Wcomment"))
1214 opts->warn_comments = 1;
1215 else if (!strcmp (argv[i], "-Wno-comment"))
1216 opts->warn_comments = 0;
1217 else if (!strcmp (argv[i], "-Wcomments"))
1218 opts->warn_comments = 1;
1219 else if (!strcmp (argv[i], "-Wno-comments"))
1220 opts->warn_comments = 0;
1221 else if (!strcmp (argv[i], "-Wtraditional"))
1222 opts->warn_stringify = 1;
1223 else if (!strcmp (argv[i], "-Wno-traditional"))
1224 opts->warn_stringify = 0;
1225 else if (!strcmp (argv[i], "-Wundef"))
1226 opts->warn_undef = 1;
1227 else if (!strcmp (argv[i], "-Wno-undef"))
1228 opts->warn_undef = 0;
1229 else if (!strcmp (argv[i], "-Wimport"))
1230 opts->warn_import = 1;
1231 else if (!strcmp (argv[i], "-Wno-import"))
1232 opts->warn_import = 0;
1233 else if (!strcmp (argv[i], "-Werror"))
1234 opts->warnings_are_errors = 1;
1235 else if (!strcmp (argv[i], "-Wno-error"))
1236 opts->warnings_are_errors = 0;
1237 else if (!strcmp (argv[i], "-Wall"))
1239 opts->warn_trigraphs = 1;
1240 opts->warn_comments = 1;
1245 /* The style of the choices here is a bit mixed.
1246 The chosen scheme is a hybrid of keeping all options in one string
1247 and specifying each option in a separate argument:
1248 -M|-MM|-MD file|-MMD file [-MG]. An alternative is:
1249 -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1250 -M[M][G][D file]. This is awkward to handle in specs, and is not
1252 /* ??? -MG must be specified in addition to one of -M or -MM.
1253 This can be relaxed in the future without breaking anything.
1254 The converse isn't true. */
1256 /* -MG isn't valid with -MD or -MMD. This is checked for later. */
1257 if (!strcmp (argv[i], "-MG"))
1259 opts->print_deps_missing_files = 1;
1262 if (!strcmp (argv[i], "-M"))
1263 opts->print_deps = 2;
1264 else if (!strcmp (argv[i], "-MM"))
1265 opts->print_deps = 1;
1266 else if (!strcmp (argv[i], "-MD"))
1267 opts->print_deps = 2;
1268 else if (!strcmp (argv[i], "-MMD"))
1269 opts->print_deps = 1;
1270 /* For -MD and -MMD options, write deps on file named by next arg. */
1271 if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
1274 goto missing_filename;
1275 opts->deps_file = argv[++i];
1279 /* For -M and -MM, write deps on standard output
1280 and suppress the usual output. */
1281 opts->no_output = 1;
1287 char *p = argv[i] + 2;
1289 while ((c = *p++) != 0)
1291 /* Arg to -d specifies what parts of macros to dump */
1295 opts->dump_macros = dump_only;
1296 opts->no_output = 1;
1299 opts->dump_macros = dump_names;
1302 opts->dump_macros = dump_definitions;
1305 opts->dump_includes = 1;
1313 if (argv[i][2] == '3')
1314 opts->debug_output = 1;
1318 if (!strcmp (argv[i], "--help"))
1320 else if (!strcmp (argv[i], "--version"))
1321 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1326 fprintf (stderr, _("GNU CPP version %s (cpplib)\n"), version_string);
1327 #ifdef TARGET_VERSION
1330 fputc ('\n', stderr);
1335 opts->print_include_names = 1;
1341 if (argv[i][2] != 0)
1343 else if (i + 1 == argc)
1345 cpp_fatal (pfile, "Macro name missing after -D option");
1350 new_pending_define (opts, text);
1358 if (argv[i][2] != 0)
1360 else if (i + 1 == argc)
1362 cpp_fatal (pfile, "Assertion missing after -A option");
1368 if (strcmp (p, "-"))
1370 struct pending_option *o = (struct pending_option *)
1371 xmalloc (sizeof (struct pending_option));
1376 APPEND (opts->pending, assert, o);
1380 /* -A- eliminates all predefined macros and assertions.
1381 Let's include also any that were specified earlier
1382 on the command line. That way we can get rid of any
1383 that were passed automatically in from GCC. */
1384 struct pending_option *o1, *o2;
1386 o1 = opts->pending->define_head;
1393 o1 = opts->pending->assert_head;
1400 opts->pending->assert_head = NULL;
1401 opts->pending->assert_tail = NULL;
1402 opts->pending->define_head = NULL;
1403 opts->pending->define_tail = NULL;
1410 struct pending_option *o = (struct pending_option *)
1411 xmalloc (sizeof (struct pending_option));
1413 if (argv[i][2] != 0)
1414 o->arg = argv[i] + 2;
1415 else if (i + 1 == argc)
1417 cpp_fatal (pfile, "Macro name missing after -U option");
1425 APPEND (opts->pending, define, o);
1430 opts->discard_comments = 0;
1433 case 'E': /* -E comes from cc -E; ignore it. */
1437 opts->no_line_commands = 1;
1440 case '$': /* Don't include $ in identifiers. */
1441 opts->dollars_in_ident = 0;
1445 if (!strcmp (argv[i], "-nostdinc"))
1446 /* -nostdinc causes no default include directories.
1447 You must specify all include-file directories with -I. */
1448 opts->no_standard_includes = 1;
1449 else if (!strcmp (argv[i], "-nostdinc++"))
1450 /* -nostdinc++ causes no default C++-specific include directories. */
1451 opts->no_standard_cplusplus_includes = 1;
1455 if (!strcmp (argv[i], "-remap"))
1459 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1460 if (opts->in_fname == NULL)
1461 opts->in_fname = "";
1462 else if (opts->out_fname == NULL)
1463 opts->out_fname = "";
1465 return i; /* error */
1475 cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
1478 cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
1482 /* Handle command-line options in (argc, argv).
1483 Can be called multiple times, to handle multiple sets of options.
1484 Returns if an unrecognized option is seen.
1485 Returns number of strings consumed. */
1488 cpp_handle_options (pfile, argc, argv)
1494 int strings_processed;
1495 for (i = 0; i < argc; i += strings_processed)
1497 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1498 if (strings_processed == 0)
1507 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1510 -include <file> Include the contents of <file> before other files\n\
1511 -imacros <file> Accept definition of macros in <file>\n\
1512 -iprefix <path> Specify <path> as a prefix for next two options\n\
1513 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1514 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1515 -isystem <dir> Add <dir> to the start of the system include path\n\
1516 -idirafter <dir> Add <dir> to the end of the system include path\n\
1517 -I <dir> Add <dir> to the end of the main include path\n\
1518 -nostdinc Do not search system include directories\n\
1519 (dirs specified with -isystem will still be used)\n\
1520 -nostdinc++ Do not search system include directories for C++\n\
1521 -o <file> Put output into <file>\n\
1522 -pedantic Issue all warnings demanded by strict ANSI C\n\
1523 -traditional Follow K&R pre-processor behaviour\n\
1524 -trigraphs Support ANSI C trigraphs\n\
1525 -lang-c Assume that the input sources are in C\n\
1526 -lang-c89 Assume that the input sources are in C89\n\
1527 -lang-c++ Assume that the input sources are in C++\n\
1528 -lang-objc Assume that the input sources are in ObjectiveC\n\
1529 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1530 -lang-asm Assume that the input sources are in assembler\n\
1531 -lang-fortran Assume that the input sources are in Fortran\n\
1532 -lang-chill Assume that the input sources are in Chill\n\
1533 -std=<std name> Specify the conformance standard; one of:\n\
1534 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1535 iso9899:199409, iso9899:1999\n\
1536 -+ Allow parsing of C++ style features\n\
1537 -w Inhibit warning messages\n\
1538 -Wtrigraphs Warn if trigraphs are encountered\n\
1539 -Wno-trigraphs Do not warn about trigraphs\n\
1540 -Wcomment{s} Warn if one comment starts inside another\n\
1541 -Wno-comment{s} Do not warn about comments\n\
1542 -Wtraditional Warn if a macro argument is/would be turned into\n\
1543 a string if -traditional is specified\n\
1544 -Wno-traditional Do not warn about stringification\n\
1545 -Wundef Warn if an undefined macro is used by #if\n\
1546 -Wno-undef Do not warn about testing undefined macros\n\
1547 -Wimport Warn about the use of the #import directive\n\
1548 -Wno-import Do not warn about the use of #import\n\
1549 -Werror Treat all warnings as errors\n\
1550 -Wno-error Do not treat warnings as errors\n\
1551 -Wall Enable all preprocessor warnings\n\
1552 -M Generate make dependencies\n\
1553 -MM As -M, but ignore system header files\n\
1554 -MD As -M, but put output in a .d file\n\
1555 -MMD As -MD, but ignore system header files\n\
1556 -MG Treat missing header file as generated files\n\
1557 -g Include #define and #undef directives in the output\n\
1558 -D<macro> Define a <macro> with string '1' as its value\n\
1559 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1560 -A<question> (<answer>) Assert the <answer> to <question>\n\
1561 -U<macro> Undefine <macro> \n\
1562 -v Display the version number\n\
1563 -H Print the name of header files as they are used\n\
1564 -C Do not discard comments\n\
1565 -dM Display a list of macro definitions active at end\n\
1566 -dD Preserve macro definitions in output\n\
1567 -dN As -dD except that only the names are preserved\n\
1568 -dI Include #include directives in the output\n\
1569 -P Do not generate #line directives\n\
1570 -$ Do not allow '$' in identifiers\n\
1571 -remap Remap file names when including files.\n\
1572 -h or --help Display this information\n\