2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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. */
32 #include "cppdefault.h"
33 #include "except.h" /* for USING_SJLJ_EXCEPTIONS */
35 /* Predefined symbols, built-in macros, and the default include path. */
37 #ifndef GET_ENV_PATH_LIST
38 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
41 /* Windows does not natively support inodes, and neither does MSDOS.
42 Cygwin's emulation can generate non-unique inodes, so don't use it.
43 VMS has non-numeric inodes. */
45 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
46 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
48 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
49 # define INO_T_EQ(A, B) 0
51 # define INO_T_EQ(A, B) ((A) == (B))
53 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
56 /* Internal structures and prototypes. */
58 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
60 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
63 struct pending_option *next;
65 cl_directive_handler handler;
68 /* The `pending' structure accumulates all the options that are not
69 actually processed until we hit cpp_read_main_file. It consists of
70 several lists, one for each type of option. We keep both head and
71 tail pointers for quick insertion. */
74 struct pending_option *directive_head, *directive_tail;
76 struct search_path *quote_head, *quote_tail;
77 struct search_path *brack_head, *brack_tail;
78 struct search_path *systm_head, *systm_tail;
79 struct search_path *after_head, *after_tail;
81 struct pending_option *imacros_head, *imacros_tail;
82 struct pending_option *include_head, *include_tail;
86 #define APPEND(pend, list, elt) \
87 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
88 else (pend)->list##_tail->next = (elt); \
89 (pend)->list##_tail = (elt); \
92 #define APPEND(pend, list, elt) \
93 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
94 else (pend)->list/**/_tail->next = (elt); \
95 (pend)->list/**/_tail = (elt); \
99 static void print_help PARAMS ((void));
100 static void path_include PARAMS ((cpp_reader *,
102 static void init_library PARAMS ((void));
103 static void init_builtins PARAMS ((cpp_reader *));
104 static void append_include_chain PARAMS ((cpp_reader *,
106 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
107 struct search_path *));
108 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
109 struct search_path *));
110 static void merge_include_chains PARAMS ((cpp_reader *));
111 static bool push_include PARAMS ((cpp_reader *,
112 struct pending_option *));
113 static void free_chain PARAMS ((struct pending_option *));
114 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
115 static void init_dependency_output PARAMS ((cpp_reader *));
116 static void init_standard_includes PARAMS ((cpp_reader *));
117 static void read_original_filename PARAMS ((cpp_reader *));
118 static void new_pending_directive PARAMS ((struct cpp_pending *,
120 cl_directive_handler));
121 static void output_deps PARAMS ((cpp_reader *));
122 static int parse_option PARAMS ((const char *));
124 /* Fourth argument to append_include_chain: chain to use.
125 Note it's never asked to append to the quote chain. */
126 enum { BRACKET = 0, SYSTEM, AFTER };
128 /* If we have designated initializers (GCC >2.7) these tables can be
129 initialized, constant data. Otherwise, they have to be filled in at
131 #if HAVE_DESIGNATED_INITIALIZERS
133 #define init_trigraph_map() /* Nothing. */
134 #define TRIGRAPH_MAP \
135 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
138 #define s(p, v) [p] = v,
142 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
143 static void init_trigraph_map PARAMS ((void)) { \
144 unsigned char *x = _cpp_trigraph_map;
147 #define s(p, v) x[p] = v;
152 s('=', '#') s(')', ']') s('!', '|')
153 s('(', '[') s('\'', '^') s('>', '}')
154 s('/', '\\') s('<', '{') s('-', '~')
161 /* Given a colon-separated list of file names PATH,
162 add all the names to the search path for include files. */
164 path_include (pfile, list, path)
175 /* Find the end of this name. */
177 while (*q != 0 && *q != PATH_SEPARATOR) q++;
180 /* An empty name in the path stands for the current directory. */
181 name = (char *) xmalloc (2);
187 /* Otherwise use the directory that is named. */
188 name = (char *) xmalloc (q - p + 1);
189 memcpy (name, p, q - p);
193 append_include_chain (pfile, name, path, 0);
195 /* Advance past this name. */
203 /* Append DIR to include path PATH. DIR must be allocated on the
204 heap; this routine takes responsibility for freeing it. CXX_AWARE
205 is non-zero if the header contains extern "C" guards for C++,
206 otherwise it is zero. */
208 append_include_chain (pfile, dir, path, cxx_aware)
212 int cxx_aware ATTRIBUTE_UNUSED;
214 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
215 struct search_path *new;
224 _cpp_simplify_pathname (dir);
228 /* Dirs that don't exist are silently ignored. */
230 cpp_notice_from_errno (pfile, dir);
231 else if (CPP_OPTION (pfile, verbose))
232 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
237 if (!S_ISDIR (st.st_mode))
239 cpp_notice (pfile, "%s: Not a directory", dir);
245 if (len > pfile->max_include_len)
246 pfile->max_include_len = len;
248 new = (struct search_path *) xmalloc (sizeof (struct search_path));
251 INO_T_COPY (new->ino, st.st_ino);
252 new->dev = st.st_dev;
253 /* Both systm and after include file lists should be treated as system
254 include files since these two lists are really just a concatenation
255 of one "system" list. */
256 if (path == SYSTEM || path == AFTER)
257 #ifdef NO_IMPLICIT_EXTERN_C
260 new->sysp = cxx_aware ? 1 : 2;
264 new->name_map = NULL;
269 case BRACKET: APPEND (pend, brack, new); break;
270 case SYSTEM: APPEND (pend, systm, new); break;
271 case AFTER: APPEND (pend, after, new); break;
275 /* Handle a duplicated include path. PREV is the link in the chain
276 before the duplicate. The duplicate is removed from the chain and
277 freed. Returns PREV. */
278 static struct search_path *
279 remove_dup_dir (pfile, prev)
281 struct search_path *prev;
283 struct search_path *cur = prev->next;
285 if (CPP_OPTION (pfile, verbose))
286 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
288 prev->next = cur->next;
289 free ((PTR) cur->name);
295 /* Remove duplicate directories from a chain. Returns the tail of the
296 chain, or NULL if the chain is empty. This algorithm is quadratic
297 in the number of -I switches, which is acceptable since there
298 aren't usually that many of them. */
299 static struct search_path *
300 remove_dup_dirs (pfile, head)
302 struct search_path *head;
304 struct search_path *prev = NULL, *cur, *other;
306 for (cur = head; cur; cur = cur->next)
308 for (other = head; other != cur; other = other->next)
309 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
311 if (cur->sysp && !other->sysp)
314 "changing search order for system directory \"%s\"",
316 if (strcmp (cur->name, other->name))
318 " as it is the same as non-system directory \"%s\"",
322 " as it has already been specified as a non-system directory");
324 cur = remove_dup_dir (pfile, prev);
333 /* Merge the four include chains together in the order quote, bracket,
334 system, after. Remove duplicate dirs (as determined by
335 INO_T_EQ()). The system_include and after_include chains are never
336 referred to again after this function; all access is through the
337 bracket_include path. */
339 merge_include_chains (pfile)
342 struct search_path *quote, *brack, *systm, *qtail;
344 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
346 quote = pend->quote_head;
347 brack = pend->brack_head;
348 systm = pend->systm_head;
349 qtail = pend->quote_tail;
351 /* Paste together bracket, system, and after include chains. */
353 pend->systm_tail->next = pend->after_head;
355 systm = pend->after_head;
358 pend->brack_tail->next = systm;
362 /* This is a bit tricky. First we drop dupes from the quote-include
363 list. Then we drop dupes from the bracket-include list.
364 Finally, if qtail and brack are the same directory, we cut out
365 brack and move brack up to point to qtail.
367 We can't just merge the lists and then uniquify them because
368 then we may lose directories from the <> search path that should
369 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
370 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
371 -Ibar -I- -Ifoo -Iquux. */
373 remove_dup_dirs (pfile, brack);
374 qtail = remove_dup_dirs (pfile, quote);
380 /* If brack == qtail, remove brack as it's simpler. */
381 if (brack && INO_T_EQ (qtail->ino, brack->ino)
382 && qtail->dev == brack->dev)
383 brack = remove_dup_dir (pfile, qtail);
388 CPP_OPTION (pfile, quote_include) = quote;
389 CPP_OPTION (pfile, bracket_include) = brack;
392 /* A set of booleans indicating what CPP features each source language
399 char extended_numbers;
401 char dollars_in_ident;
402 char cplusplus_comments;
406 /* ??? Enable $ in identifiers in assembly? */
407 static const struct lang_flags lang_defaults[] =
408 { /* c99 objc c++ xnum trig dollar c++comm digr */
409 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
410 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
411 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
412 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
413 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
414 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
415 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
416 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
417 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
418 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
421 /* Sets internal flags correctly for a given language. */
423 set_lang (pfile, lang)
427 const struct lang_flags *l = &lang_defaults[(int) lang];
429 CPP_OPTION (pfile, lang) = lang;
431 CPP_OPTION (pfile, c99) = l->c99;
432 CPP_OPTION (pfile, objc) = l->objc;
433 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
434 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
435 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
436 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
437 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
438 CPP_OPTION (pfile, digraphs) = l->digraphs;
442 static int opt_comp PARAMS ((const void *, const void *));
444 /* Run-time sorting of options array. */
449 return strcmp (((struct cl_option *) p1)->opt_text,
450 ((struct cl_option *) p2)->opt_text);
454 /* init initializes library global state. It might not need to
455 do anything depending on the platform and compiler. */
459 static int initialized = 0;
466 /* For non-ASCII hosts, the cl_options array needs to be sorted at
468 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
471 /* Set up the trigraph map. This doesn't need to do anything if
472 we were compiled with a compiler that supports C99 designated
474 init_trigraph_map ();
478 /* Initialize a cpp_reader structure. */
480 cpp_create_reader (lang)
485 /* Initialise this instance of the library if it hasn't been already. */
488 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
490 set_lang (pfile, lang);
491 CPP_OPTION (pfile, warn_import) = 1;
492 CPP_OPTION (pfile, discard_comments) = 1;
493 CPP_OPTION (pfile, show_column) = 1;
494 CPP_OPTION (pfile, tabstop) = 8;
495 CPP_OPTION (pfile, operator_names) = 1;
496 #if DEFAULT_SIGNED_CHAR
497 CPP_OPTION (pfile, signed_char) = 1;
499 CPP_OPTION (pfile, signed_char) = 0;
502 CPP_OPTION (pfile, pending) =
503 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
505 /* It's simplest to just create this struct whether or not it will
507 pfile->deps = deps_init ();
509 /* Initialise the line map. Start at logical line 1, so we can use
510 a line number of zero for special states. */
511 init_line_maps (&pfile->line_maps);
514 /* Initialize lexer state. */
515 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
517 /* Set up static tokens. */
518 pfile->date.type = CPP_EOF;
519 pfile->avoid_paste.type = CPP_PADDING;
520 pfile->avoid_paste.val.source = NULL;
521 pfile->eof.type = CPP_EOF;
522 pfile->eof.flags = 0;
524 /* Create a token buffer for the lexer. */
525 _cpp_init_tokenrun (&pfile->base_run, 250);
526 pfile->cur_run = &pfile->base_run;
527 pfile->cur_token = pfile->base_run.base;
529 /* Initialise the base context. */
530 pfile->context = &pfile->base_context;
531 pfile->base_context.macro = 0;
532 pfile->base_context.prev = pfile->base_context.next = 0;
534 /* Aligned and unaligned storage. */
535 pfile->a_buff = _cpp_get_buff (pfile, 0);
536 pfile->u_buff = _cpp_get_buff (pfile, 0);
538 /* Initialise the buffer obstack. */
539 gcc_obstack_init (&pfile->buffer_ob);
541 _cpp_init_includes (pfile);
546 /* Free resources used by PFILE. Accessing PFILE after this function
547 returns leads to undefined behaviour. Returns the error count. */
553 struct search_path *dir, *dirn;
554 cpp_context *context, *contextn;
555 tokenrun *run, *runn;
557 while (CPP_BUFFER (pfile) != NULL)
558 _cpp_pop_buffer (pfile);
560 if (pfile->macro_buffer)
562 free ((PTR) pfile->macro_buffer);
563 pfile->macro_buffer = NULL;
564 pfile->macro_buffer_len = 0;
567 deps_free (pfile->deps);
568 obstack_free (&pfile->buffer_ob, 0);
570 _cpp_destroy_hashtable (pfile);
571 _cpp_cleanup_includes (pfile);
573 _cpp_free_buff (pfile->a_buff);
574 _cpp_free_buff (pfile->u_buff);
575 _cpp_free_buff (pfile->free_buffs);
577 for (run = &pfile->base_run; run; run = runn)
581 if (run != &pfile->base_run)
585 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
588 free ((PTR) dir->name);
592 for (context = pfile->base_context.next; context; context = contextn)
594 contextn = context->next;
598 free_line_maps (&pfile->line_maps);
600 result = pfile->errors;
607 /* This structure defines one built-in identifier. A node will be
608 entered in the hash table under the name NAME, with value VALUE (if
609 any). If flags has OPERATOR, the node's operator field is used; if
610 flags has BUILTIN the node's builtin field is used. Macros that are
611 known at build time should not be flagged BUILTIN, as then they do
612 not appear in macro dumps with e.g. -dM or -dD.
614 Two values are not compile time constants, so we tag
615 them in the FLAGS field instead:
616 VERS value is the global version_string, quoted
617 ULP value is the global user_label_prefix
619 Also, macros with CPLUS set in the flags field are entered only for C++. */
624 unsigned char builtin;
625 unsigned char operator;
626 unsigned short flags;
633 #define OPERATOR 0x10
635 #define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
636 #define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
637 #define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
638 #define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
639 static const struct builtin builtin_array[] =
641 B("__TIME__", BT_TIME),
642 B("__DATE__", BT_DATE),
643 B("__FILE__", BT_FILE),
644 B("__BASE_FILE__", BT_BASE_FILE),
645 B("__LINE__", BT_SPECLINE),
646 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
647 B("_Pragma", BT_PRAGMA),
649 X("__VERSION__", VERS),
650 X("__USER_LABEL_PREFIX__", ULP),
651 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
652 C("__HAVE_BUILTIN_SETJMP__", "1"),
653 #if USING_SJLJ_EXCEPTIONS
654 /* libgcc needs to know this. */
655 C("__USING_SJLJ_EXCEPTIONS__","1"),
657 #ifndef NO_BUILTIN_SIZE_TYPE
658 C("__SIZE_TYPE__", SIZE_TYPE),
660 #ifndef NO_BUILTIN_PTRDIFF_TYPE
661 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
663 #ifndef NO_BUILTIN_WCHAR_TYPE
664 C("__WCHAR_TYPE__", WCHAR_TYPE),
666 #ifndef NO_BUILTIN_WINT_TYPE
667 C("__WINT_TYPE__", WINT_TYPE),
669 #ifdef STDC_0_IN_SYSTEM_HEADERS
670 B("__STDC__", BT_STDC),
675 /* Named operators known to the preprocessor. These cannot be #defined
676 and always have their stated meaning. They are treated like normal
677 identifiers except for the type code and the meaning. Most of them
678 are only for C++ (but see iso646.h). */
679 O("and", CPP_AND_AND, CPLUS),
680 O("and_eq", CPP_AND_EQ, CPLUS),
681 O("bitand", CPP_AND, CPLUS),
682 O("bitor", CPP_OR, CPLUS),
683 O("compl", CPP_COMPL, CPLUS),
684 O("not", CPP_NOT, CPLUS),
685 O("not_eq", CPP_NOT_EQ, CPLUS),
686 O("or", CPP_OR_OR, CPLUS),
687 O("or_eq", CPP_OR_EQ, CPLUS),
688 O("xor", CPP_XOR, CPLUS),
689 O("xor_eq", CPP_XOR_EQ, CPLUS)
695 #define builtin_array_end \
696 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
698 /* Subroutine of cpp_read_main_file; reads the builtins table above and
699 enters them, and language-specific macros, into the hash table. */
701 init_builtins (pfile)
704 const struct builtin *b;
706 for(b = builtin_array; b < builtin_array_end; b++)
708 if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
711 if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
714 if (b->flags & (OPERATOR | BUILTIN))
716 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
717 if (b->flags & OPERATOR)
719 hp->flags |= NODE_OPERATOR;
720 hp->value.operator = b->operator;
725 hp->flags |= NODE_BUILTIN | NODE_WARN;
726 hp->value.builtin = b->builtin;
729 else /* A standard macro of some kind. */
736 /* Allocate enough space for 'name "value"\n\0'. */
737 str = alloca (b->len + strlen (version_string) + 5);
738 sprintf (str, "%s \"%s\"\n", b->name, version_string);
743 val = CPP_OPTION (pfile, user_label_prefix);
747 /* Allocate enough space for "name value\n\0". */
748 str = alloca (b->len + strlen (val) + 3);
749 sprintf(str, "%s %s\n", b->name, val);
752 _cpp_define_builtin (pfile, str);
756 if (CPP_OPTION (pfile, cplusplus))
758 _cpp_define_builtin (pfile, "__cplusplus 1");
759 if (SUPPORTS_ONE_ONLY)
760 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
762 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
764 if (CPP_OPTION (pfile, objc))
765 _cpp_define_builtin (pfile, "__OBJC__ 1");
767 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
768 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
769 else if (CPP_OPTION (pfile, c99))
770 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
772 if (CPP_OPTION (pfile, signed_char) == 0)
773 _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
775 if (CPP_OPTION (pfile, lang) == CLK_STDC89
776 || CPP_OPTION (pfile, lang) == CLK_STDC94
777 || CPP_OPTION (pfile, lang) == CLK_STDC99)
778 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
779 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
780 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
787 #undef builtin_array_end
789 /* And another subroutine. This one sets up the standard include path. */
791 init_standard_includes (pfile)
795 const struct default_include *p;
796 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
798 /* Several environment variables may add to the include search path.
799 CPATH specifies an additional list of directories to be searched
800 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
801 etc. specify an additional list of directories to be searched as
802 if specified with -isystem, for the language indicated. */
804 GET_ENV_PATH_LIST (path, "CPATH");
805 if (path != 0 && *path != 0)
806 path_include (pfile, path, BRACKET);
808 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
811 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
814 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
817 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
820 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
823 if (path != 0 && *path != 0)
824 path_include (pfile, path, SYSTEM);
826 /* Search "translated" versions of GNU directories.
827 These have /usr/local/lib/gcc... replaced by specd_prefix. */
828 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
830 /* Remove the `include' from /usr/local/lib/gcc.../include.
831 GCC_INCLUDE_DIR will always end in /include. */
832 int default_len = cpp_GCC_INCLUDE_DIR_len;
833 char *default_prefix = (char *) alloca (default_len + 1);
834 int specd_len = strlen (specd_prefix);
836 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
837 default_prefix[default_len] = '\0';
839 for (p = cpp_include_defaults; p->fname; p++)
841 /* Some standard dirs are only for C++. */
843 || (CPP_OPTION (pfile, cplusplus)
844 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
846 /* Does this dir start with the prefix? */
847 if (!memcmp (p->fname, default_prefix, default_len))
849 /* Yes; change prefix and add to search list. */
850 int flen = strlen (p->fname);
851 int this_len = specd_len + flen - default_len;
852 char *str = (char *) xmalloc (this_len + 1);
853 memcpy (str, specd_prefix, specd_len);
854 memcpy (str + specd_len,
855 p->fname + default_len,
856 flen - default_len + 1);
858 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
864 /* Search ordinary names for GNU include directories. */
865 for (p = cpp_include_defaults; p->fname; p++)
867 /* Some standard dirs are only for C++. */
869 || (CPP_OPTION (pfile, cplusplus)
870 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
872 char *str = update_path (p->fname, p->component);
873 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
878 /* Pushes a command line -imacro and -include file indicated by P onto
879 the buffer stack. Returns non-zero if successful. */
881 push_include (pfile, p)
883 struct pending_option *p;
887 /* Later: maybe update this to use the #include "" search path
888 if cpp_read_file fails. */
889 header.type = CPP_STRING;
890 header.val.str.text = (const unsigned char *) p->arg;
891 header.val.str.len = strlen (p->arg);
892 /* Make the command line directive take up a line. */
895 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
898 /* Frees a pending_option chain. */
901 struct pending_option *head;
903 struct pending_option *next;
913 /* This is called after options have been parsed, and partially
914 processed. Setup for processing input from the file named FNAME,
915 or stdin if it is the empty string. Return the original filename
916 on success (e.g. foo.i->foo.c), or NULL on failure. */
918 cpp_read_main_file (pfile, fname, table)
923 /* The front ends don't set up the hash table until they have
924 finished processing the command line options, so initializing the
925 hashtable is deferred until now. */
926 _cpp_init_hashtable (pfile, table);
928 /* Set up the include search path now. */
929 if (! CPP_OPTION (pfile, no_standard_includes))
930 init_standard_includes (pfile);
932 merge_include_chains (pfile);
934 /* With -v, print the list of dirs to search. */
935 if (CPP_OPTION (pfile, verbose))
937 struct search_path *l;
938 fprintf (stderr, _("#include \"...\" search starts here:\n"));
939 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
941 if (l == CPP_OPTION (pfile, bracket_include))
942 fprintf (stderr, _("#include <...> search starts here:\n"));
943 fprintf (stderr, " %s\n", l->name);
945 fprintf (stderr, _("End of search list.\n"));
948 if (CPP_OPTION (pfile, print_deps))
949 /* Set the default target (if there is none already). */
950 deps_add_default_target (pfile->deps, fname);
952 /* Open the main input file. */
953 if (!_cpp_read_file (pfile, fname))
956 /* Set this after cpp_post_options so the client can change the
957 option if it wishes, and after stacking the main file so we don't
958 trace the main file. */
959 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
961 /* For foo.i, read the original filename foo.c now, for the benefit
962 of the front ends. */
963 if (CPP_OPTION (pfile, preprocessed))
964 read_original_filename (pfile);
966 return pfile->map->to_file;
969 /* For preprocessed files, if the first tokens are of the form # NUM.
970 handle the directive so we know the original file name. This will
971 generate file_change callbacks, which the front ends must handle
972 appropriately given their state of initialization. */
974 read_original_filename (pfile)
977 const cpp_token *token, *token1;
979 /* Lex ahead; if the first tokens are of the form # NUM, then
980 process the directive, otherwise back up. */
981 token = _cpp_lex_direct (pfile);
982 if (token->type == CPP_HASH)
984 token1 = _cpp_lex_direct (pfile);
985 _cpp_backup_tokens (pfile, 1);
987 /* If it's a #line directive, handle it. */
988 if (token1->type == CPP_NUMBER)
990 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
995 /* Backup as if nothing happened. */
996 _cpp_backup_tokens (pfile, 1);
999 /* Handle pending command line options: -D, -U, -A, -imacros and
1000 -include. This should be called after debugging has been properly
1001 set up in the front ends. */
1003 cpp_finish_options (pfile)
1006 /* Install builtins and process command line macros etc. in the order
1007 they appeared, but only if not already preprocessed. */
1008 if (! CPP_OPTION (pfile, preprocessed))
1010 struct pending_option *p;
1012 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1013 init_builtins (pfile);
1014 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1015 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1016 (*p->handler) (pfile, p->arg);
1018 /* Scan -imacros files after command line defines, but before
1019 files given with -include. */
1020 while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
1022 if (push_include (pfile, p))
1024 pfile->buffer->return_at_eof = true;
1025 cpp_scan_nooutput (pfile);
1027 CPP_OPTION (pfile, pending)->imacros_head = p->next;
1032 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1033 _cpp_push_next_buffer (pfile);
1036 /* Called to push the next buffer on the stack given by -include. If
1037 there are none, free the pending structure and restore the line map
1038 for the main file. */
1040 _cpp_push_next_buffer (pfile)
1043 bool pushed = false;
1045 /* This is't pretty; we'd rather not be relying on this as a boolean
1046 for reverting the line map. Further, we only free the chains in
1047 this conditional, so an early call to cpp_finish / cpp_destroy
1048 will leak that memory. */
1049 if (CPP_OPTION (pfile, pending)
1050 && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1054 struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1058 if (! CPP_OPTION (pfile, preprocessed))
1059 pushed = push_include (pfile, p);
1060 CPP_OPTION (pfile, pending)->include_head = p->next;
1066 free (CPP_OPTION (pfile, pending));
1067 CPP_OPTION (pfile, pending) = NULL;
1069 /* Restore the line map for the main file. */
1070 if (! CPP_OPTION (pfile, preprocessed))
1071 _cpp_do_file_change (pfile, LC_RENAME,
1072 pfile->line_maps.maps[0].to_file, 1, 0);
1079 /* Use mkdeps.c to output dependency information. */
1084 /* Stream on which to print the dependency information. */
1085 FILE *deps_stream = 0;
1086 const char *const deps_mode =
1087 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1089 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1090 deps_stream = stdout;
1093 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1094 if (deps_stream == 0)
1096 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1101 deps_write (pfile->deps, deps_stream, 72);
1103 if (CPP_OPTION (pfile, deps_phony_targets))
1104 deps_phony_targets (pfile->deps, deps_stream);
1106 /* Don't close stdout. */
1107 if (deps_stream != stdout)
1109 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1110 cpp_fatal (pfile, "I/O error on output");
1114 /* This is called at the end of preprocessing. It pops the
1115 last buffer and writes dependency output. It should also
1116 clear macro definitions, such that you could call cpp_start_read
1117 with a new filename to restart processing. */
1122 /* cpplex.c leaves the final buffer on the stack. This it so that
1123 it returns an unending stream of CPP_EOFs to the client. If we
1124 popped the buffer, we'd dereference a NULL buffer pointer and
1125 segfault. It's nice to allow the client to do worry-free excess
1126 cpp_get_token calls. */
1127 while (pfile->buffer)
1128 _cpp_pop_buffer (pfile);
1130 /* Don't write the deps file if preprocessing has failed. */
1131 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1132 output_deps (pfile);
1134 /* Report on headers that could use multiple include guards. */
1135 if (CPP_OPTION (pfile, print_include_names))
1136 _cpp_report_missing_guards (pfile);
1139 /* Add a directive to be handled later in the initialization phase. */
1141 new_pending_directive (pend, text, handler)
1142 struct cpp_pending *pend;
1144 cl_directive_handler handler;
1146 struct pending_option *o = (struct pending_option *)
1147 xmalloc (sizeof (struct pending_option));
1151 o->handler = handler;
1152 APPEND (pend, directive, o);
1155 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1156 I.e. a const string initializer with parens around it. That is
1157 what N_("string") resolves to, so we make no_* be macros instead. */
1158 #define no_arg N_("argument missing after %s")
1159 #define no_ass N_("assertion missing after %s")
1160 #define no_dir N_("directory name missing after %s")
1161 #define no_fil N_("file name missing after %s")
1162 #define no_mac N_("macro name missing after %s")
1163 #define no_pth N_("path name missing after %s")
1164 #define no_num N_("number missing after %s")
1165 #define no_tgt N_("target missing after %s")
1167 /* This is the list of all command line options, with the leading
1168 "-" removed. It must be sorted in ASCII collating order. */
1169 #define COMMAND_LINE_OPTIONS \
1170 DEF_OPT("$", 0, OPT_dollar) \
1171 DEF_OPT("+", 0, OPT_plus) \
1172 DEF_OPT("-help", 0, OPT__help) \
1173 DEF_OPT("-target-help", 0, OPT_target__help) \
1174 DEF_OPT("-version", 0, OPT__version) \
1175 DEF_OPT("A", no_ass, OPT_A) \
1176 DEF_OPT("C", 0, OPT_C) \
1177 DEF_OPT("D", no_mac, OPT_D) \
1178 DEF_OPT("H", 0, OPT_H) \
1179 DEF_OPT("I", no_dir, OPT_I) \
1180 DEF_OPT("M", 0, OPT_M) \
1181 DEF_OPT("MD", no_fil, OPT_MD) \
1182 DEF_OPT("MF", no_fil, OPT_MF) \
1183 DEF_OPT("MG", 0, OPT_MG) \
1184 DEF_OPT("MM", 0, OPT_MM) \
1185 DEF_OPT("MMD", no_fil, OPT_MMD) \
1186 DEF_OPT("MP", 0, OPT_MP) \
1187 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1188 DEF_OPT("MT", no_tgt, OPT_MT) \
1189 DEF_OPT("P", 0, OPT_P) \
1190 DEF_OPT("U", no_mac, OPT_U) \
1191 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1192 DEF_OPT("d", no_arg, OPT_d) \
1193 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1194 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1195 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1196 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1197 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1198 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1199 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1200 DEF_OPT("fsigned-char", 0, OPT_fsigned_char) \
1201 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1202 DEF_OPT("funsigned-char", 0, OPT_funsigned_char) \
1203 DEF_OPT("h", 0, OPT_h) \
1204 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1205 DEF_OPT("imacros", no_fil, OPT_imacros) \
1206 DEF_OPT("include", no_fil, OPT_include) \
1207 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1208 DEF_OPT("isystem", no_dir, OPT_isystem) \
1209 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1210 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1211 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1212 DEF_OPT("lang-c", 0, OPT_lang_c) \
1213 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1214 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1215 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1216 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1217 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1218 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1219 DEF_OPT("o", no_fil, OPT_o) \
1220 DEF_OPT("pedantic", 0, OPT_pedantic) \
1221 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1222 DEF_OPT("remap", 0, OPT_remap) \
1223 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1224 DEF_OPT("std=c89", 0, OPT_std_c89) \
1225 DEF_OPT("std=c99", 0, OPT_std_c99) \
1226 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1227 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1228 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1229 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1230 DEF_OPT("std=bsd", 0, OPT_std_bsd) \
1231 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1232 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1233 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1234 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1235 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1236 DEF_OPT("v", 0, OPT_v) \
1237 DEF_OPT("version", 0, OPT_version) \
1238 DEF_OPT("w", 0, OPT_w)
1240 #define DEF_OPT(text, msg, code) code,
1243 COMMAND_LINE_OPTIONS
1250 const char *opt_text;
1253 enum opt_code opt_code;
1256 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1258 static struct cl_option cl_options[] =
1260 static const struct cl_option cl_options[] =
1263 COMMAND_LINE_OPTIONS
1266 #undef COMMAND_LINE_OPTIONS
1268 /* Perform a binary search to find which, if any, option the given
1269 command-line matches. Returns its index in the option array,
1270 negative on failure. Complications arise since some options can be
1271 suffixed with an argument, and multiple complete matches can occur,
1272 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1273 accept options beginning with -W that we do not recognise, but not
1274 to swallow any subsequent command line argument; this is handled as
1275 special cases in cpp_handle_option. */
1277 parse_option (input)
1280 unsigned int md, mn, mx;
1291 opt_len = cl_options[md].opt_len;
1292 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1300 if (input[opt_len] == '\0')
1302 /* We were passed more text. If the option takes an argument,
1303 we may match a later option or we may have been passed the
1304 argument. The longest possible option match succeeds.
1305 If the option takes no arguments we have not matched and
1306 continue the search (e.g. input="stdc++" match was "stdc"). */
1308 if (cl_options[md].msg)
1310 /* Scan forwards. If we get an exact match, return it.
1311 Otherwise, return the longest option-accepting match.
1312 This loops no more than twice with current options. */
1314 for (; mn < (unsigned int) N_OPTS; mn++)
1316 opt_len = cl_options[mn].opt_len;
1317 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1319 if (input[opt_len] == '\0')
1321 if (cl_options[mn].msg)
1332 /* Handle one command-line option in (argc, argv).
1333 Can be called multiple times, to handle multiple sets of options.
1334 If ignore is non-zero, this will ignore unrecognized -W* options.
1335 Returns number of strings consumed. */
1337 cpp_handle_option (pfile, argc, argv, ignore)
1344 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1346 /* Interpret "-" or a non-option as a file name. */
1347 if (argv[i][0] != '-' || argv[i][1] == '\0')
1349 if (CPP_OPTION (pfile, in_fname) == NULL)
1350 CPP_OPTION (pfile, in_fname) = argv[i];
1351 else if (CPP_OPTION (pfile, out_fname) == NULL)
1352 CPP_OPTION (pfile, out_fname) = argv[i];
1354 cpp_fatal (pfile, "too many filenames. Type %s --help for usage info",
1359 enum opt_code opt_code;
1361 const char *arg = 0;
1363 /* Skip over '-'. */
1364 opt_index = parse_option (&argv[i][1]);
1368 opt_code = cl_options[opt_index].opt_code;
1369 if (cl_options[opt_index].msg)
1371 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1373 /* Yuk. Special case for -W as it must not swallow
1374 up any following argument. If this becomes common, add
1375 another field to the cl_options table. */
1376 if (arg[0] == '\0' && opt_code != OPT_W)
1381 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1389 case N_OPTS: /* Shut GCC up. */
1391 case OPT_fleading_underscore:
1392 CPP_OPTION (pfile, user_label_prefix) = "_";
1394 case OPT_fno_leading_underscore:
1395 CPP_OPTION (pfile, user_label_prefix) = "";
1397 case OPT_fno_operator_names:
1398 CPP_OPTION (pfile, operator_names) = 0;
1400 case OPT_fpreprocessed:
1401 CPP_OPTION (pfile, preprocessed) = 1;
1403 case OPT_fno_preprocessed:
1404 CPP_OPTION (pfile, preprocessed) = 0;
1406 case OPT_fshow_column:
1407 CPP_OPTION (pfile, show_column) = 1;
1409 case OPT_fno_show_column:
1410 CPP_OPTION (pfile, show_column) = 0;
1412 case OPT_fsigned_char:
1413 CPP_OPTION (pfile, signed_char) = 1;
1415 case OPT_funsigned_char:
1416 CPP_OPTION (pfile, signed_char) = 0;
1419 /* Silently ignore empty string, non-longs and silly values. */
1423 long tabstop = strtol (arg, &endptr, 10);
1424 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1425 CPP_OPTION (pfile, tabstop) = tabstop;
1429 CPP_OPTION (pfile, inhibit_warnings) = 1;
1434 CPP_OPTION (pfile, help_only) = 1;
1436 case OPT_target__help:
1437 /* Print if any target specific options. cpplib has none, but
1438 make sure help_only gets set. */
1439 CPP_OPTION (pfile, help_only) = 1;
1442 /* --version inhibits compilation, -version doesn't. -v means
1443 verbose and -version. Historical reasons, don't ask. */
1445 CPP_OPTION (pfile, help_only) = 1;
1446 pfile->print_version = 1;
1449 CPP_OPTION (pfile, verbose) = 1;
1450 pfile->print_version = 1;
1453 pfile->print_version = 1;
1457 CPP_OPTION (pfile, discard_comments) = 0;
1460 CPP_OPTION (pfile, no_line_commands) = 1;
1462 case OPT_dollar: /* Don't include $ in identifiers. */
1463 CPP_OPTION (pfile, dollars_in_ident) = 0;
1466 CPP_OPTION (pfile, print_include_names) = 1;
1469 new_pending_directive (pend, arg, cpp_define);
1471 case OPT_pedantic_errors:
1472 CPP_OPTION (pfile, pedantic_errors) = 1;
1475 CPP_OPTION (pfile, pedantic) = 1;
1478 CPP_OPTION (pfile, trigraphs) = 1;
1481 CPP_OPTION (pfile, cplusplus) = 1;
1482 CPP_OPTION (pfile, cplusplus_comments) = 1;
1485 CPP_OPTION (pfile, remap) = 1;
1488 CPP_OPTION (pfile, include_prefix) = arg;
1489 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1492 set_lang (pfile, CLK_GNUC89);
1494 case OPT_lang_cplusplus:
1495 set_lang (pfile, CLK_GNUCXX);
1498 set_lang (pfile, CLK_OBJC);
1500 case OPT_lang_objcplusplus:
1501 set_lang (pfile, CLK_OBJCXX);
1504 set_lang (pfile, CLK_ASM);
1506 case OPT_std_cplusplus98:
1507 set_lang (pfile, CLK_CXX98);
1510 set_lang (pfile, CLK_GNUC89);
1514 set_lang (pfile, CLK_GNUC99);
1517 set_lang (pfile, CLK_GNUC89);
1519 case OPT_std_iso9899_199409:
1520 set_lang (pfile, CLK_STDC94);
1522 case OPT_std_iso9899_1990:
1525 set_lang (pfile, CLK_STDC89);
1527 case OPT_std_iso9899_199x:
1528 case OPT_std_iso9899_1999:
1531 set_lang (pfile, CLK_STDC99);
1534 /* -nostdinc causes no default include directories.
1535 You must specify all include-file directories with -I. */
1536 CPP_OPTION (pfile, no_standard_includes) = 1;
1538 case OPT_nostdincplusplus:
1539 /* -nostdinc++ causes no default C++-specific include directories. */
1540 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1543 if (CPP_OPTION (pfile, out_fname) == NULL)
1544 CPP_OPTION (pfile, out_fname) = arg;
1547 cpp_fatal (pfile, "output filename specified twice");
1552 /* Args to -d specify what parts of macros to dump.
1553 Silently ignore unrecognised options; they may
1554 be aimed at the compiler proper. */
1558 while ((c = *arg++) != '\0')
1562 CPP_OPTION (pfile, dump_macros) = dump_only;
1565 CPP_OPTION (pfile, dump_macros) = dump_names;
1568 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1571 CPP_OPTION (pfile, dump_includes) = 1;
1578 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1581 /* When doing dependencies with -M or -MM, suppress normal
1582 preprocessed output, but still do -dM etc. as software
1583 depends on this. Preprocessed output occurs if -MD, -MMD
1584 or environment var dependency generation is used. */
1585 CPP_OPTION (pfile, print_deps) = 2;
1586 CPP_OPTION (pfile, no_output) = 1;
1589 CPP_OPTION (pfile, print_deps) = 1;
1590 CPP_OPTION (pfile, no_output) = 1;
1593 CPP_OPTION (pfile, deps_file) = arg;
1596 CPP_OPTION (pfile, deps_phony_targets) = 1;
1600 /* Add a target. -MQ quotes for Make. */
1601 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1605 CPP_OPTION (pfile, print_deps) = 2;
1606 CPP_OPTION (pfile, deps_file) = arg;
1609 CPP_OPTION (pfile, print_deps) = 1;
1610 CPP_OPTION (pfile, deps_file) = arg;
1616 /* -A with an argument beginning with '-' acts as
1617 #unassert on whatever immediately follows the '-'.
1618 If "-" is the whole argument, we eliminate all
1619 predefined macros and assertions, including those
1620 that were specified earlier on the command line.
1621 That way we can get rid of any that were passed
1622 automatically in from GCC. */
1626 free_chain (pend->directive_head);
1627 pend->directive_head = NULL;
1628 pend->directive_tail = NULL;
1631 new_pending_directive (pend, arg + 1, cpp_unassert);
1634 new_pending_directive (pend, arg, cpp_assert);
1637 new_pending_directive (pend, arg, cpp_undef);
1639 case OPT_I: /* Add directory to path for includes. */
1640 if (!strcmp (arg, "-"))
1643 Use the preceding -I directories for #include "..."
1644 but not #include <...>.
1645 Don't search the directory of the present file
1646 for #include "...". (Note that -I. -I- is not the same as
1647 the default setup; -I. uses the compiler's working dir.) */
1648 if (! CPP_OPTION (pfile, ignore_srcdir))
1650 pend->quote_head = pend->brack_head;
1651 pend->quote_tail = pend->brack_tail;
1652 pend->brack_head = 0;
1653 pend->brack_tail = 0;
1654 CPP_OPTION (pfile, ignore_srcdir) = 1;
1658 cpp_fatal (pfile, "-I- specified twice");
1663 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1666 /* Add directory to beginning of system include path, as a system
1667 include directory. */
1668 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1673 struct pending_option *o = (struct pending_option *)
1674 xmalloc (sizeof (struct pending_option));
1678 if (opt_code == OPT_include)
1679 APPEND (pend, include, o);
1681 APPEND (pend, imacros, o);
1684 case OPT_iwithprefix:
1685 /* Add directory to end of path for includes,
1686 with the default prefix at the front of its name. */
1688 case OPT_iwithprefixbefore:
1689 /* Add directory to main path for includes,
1690 with the default prefix at the front of its name. */
1697 if (CPP_OPTION (pfile, include_prefix) != 0)
1699 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1700 fname = xmalloc (ipl + len + 1);
1701 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1702 memcpy (fname + ipl, arg, len + 1);
1704 else if (cpp_GCC_INCLUDE_DIR_len)
1706 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1707 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1708 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1711 fname = xstrdup (arg);
1713 append_include_chain (pfile, fname,
1714 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1718 /* Add directory to end of path for includes. */
1719 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1722 /* Silently ignore unrecognised options. */
1723 if (!strcmp (argv[i], "-Wall"))
1725 CPP_OPTION (pfile, warn_trigraphs) = 1;
1726 CPP_OPTION (pfile, warn_comments) = 1;
1728 else if (!strcmp (argv[i], "-Wtraditional"))
1729 CPP_OPTION (pfile, warn_traditional) = 1;
1730 else if (!strcmp (argv[i], "-Wtrigraphs"))
1731 CPP_OPTION (pfile, warn_trigraphs) = 1;
1732 else if (!strcmp (argv[i], "-Wcomment"))
1733 CPP_OPTION (pfile, warn_comments) = 1;
1734 else if (!strcmp (argv[i], "-Wcomments"))
1735 CPP_OPTION (pfile, warn_comments) = 1;
1736 else if (!strcmp (argv[i], "-Wundef"))
1737 CPP_OPTION (pfile, warn_undef) = 1;
1738 else if (!strcmp (argv[i], "-Wimport"))
1739 CPP_OPTION (pfile, warn_import) = 1;
1740 else if (!strcmp (argv[i], "-Werror"))
1741 CPP_OPTION (pfile, warnings_are_errors) = 1;
1742 else if (!strcmp (argv[i], "-Wsystem-headers"))
1743 CPP_OPTION (pfile, warn_system_headers) = 1;
1744 else if (!strcmp (argv[i], "-Wno-traditional"))
1745 CPP_OPTION (pfile, warn_traditional) = 0;
1746 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1747 CPP_OPTION (pfile, warn_trigraphs) = 0;
1748 else if (!strcmp (argv[i], "-Wno-comment"))
1749 CPP_OPTION (pfile, warn_comments) = 0;
1750 else if (!strcmp (argv[i], "-Wno-comments"))
1751 CPP_OPTION (pfile, warn_comments) = 0;
1752 else if (!strcmp (argv[i], "-Wno-undef"))
1753 CPP_OPTION (pfile, warn_undef) = 0;
1754 else if (!strcmp (argv[i], "-Wno-import"))
1755 CPP_OPTION (pfile, warn_import) = 0;
1756 else if (!strcmp (argv[i], "-Wno-error"))
1757 CPP_OPTION (pfile, warnings_are_errors) = 0;
1758 else if (!strcmp (argv[i], "-Wno-system-headers"))
1759 CPP_OPTION (pfile, warn_system_headers) = 0;
1768 /* Handle command-line options in (argc, argv).
1769 Can be called multiple times, to handle multiple sets of options.
1770 Returns if an unrecognized option is seen.
1771 Returns number of strings consumed. */
1773 cpp_handle_options (pfile, argc, argv)
1779 int strings_processed;
1781 for (i = 0; i < argc; i += strings_processed)
1783 strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
1784 if (strings_processed == 0)
1791 /* Extra processing when all options are parsed, after all calls to
1792 cpp_handle_option[s]. Consistency checks etc. */
1794 cpp_post_options (pfile)
1797 if (pfile->print_version)
1799 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1800 #ifdef TARGET_VERSION
1803 fputc ('\n', stderr);
1806 /* Canonicalize in_fname and out_fname. We guarantee they are not
1807 NULL, and that the empty string represents stdin / stdout. */
1808 if (CPP_OPTION (pfile, in_fname) == NULL
1809 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1810 CPP_OPTION (pfile, in_fname) = "";
1812 if (CPP_OPTION (pfile, out_fname) == NULL
1813 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1814 CPP_OPTION (pfile, out_fname) = "";
1816 /* -Wtraditional is not useful in C++ mode. */
1817 if (CPP_OPTION (pfile, cplusplus))
1818 CPP_OPTION (pfile, warn_traditional) = 0;
1820 /* Set this if it hasn't been set already. */
1821 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1822 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1824 /* Permanently disable macro expansion if we are rescanning
1825 preprocessed text. */
1826 if (CPP_OPTION (pfile, preprocessed))
1827 pfile->state.prevent_expansion = 1;
1829 /* -dM makes no normal output. This is set here so that -dM -dD
1830 works as expected. */
1831 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1832 CPP_OPTION (pfile, no_output) = 1;
1834 /* Disable -dD, -dN and -dI if we should make no normal output
1835 (such as with -M). Allow -M -dM since some software relies on
1837 if (CPP_OPTION (pfile, no_output))
1839 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1840 CPP_OPTION (pfile, dump_macros) = dump_none;
1841 CPP_OPTION (pfile, dump_includes) = 0;
1844 /* We need to do this after option processing and before
1845 cpp_start_read, as cppmain.c relies on the options->no_output to
1846 set its callbacks correctly before calling cpp_start_read. */
1847 init_dependency_output (pfile);
1849 /* After checking the environment variables, check if -M or -MM has
1850 not been specified, but other -M options have. */
1851 if (CPP_OPTION (pfile, print_deps) == 0 &&
1852 (CPP_OPTION (pfile, print_deps_missing_files)
1853 || CPP_OPTION (pfile, deps_file)
1854 || CPP_OPTION (pfile, deps_phony_targets)))
1855 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1858 /* Set up dependency-file output. On exit, if print_deps is non-zero
1859 then deps_file is not NULL; stdout is the empty string. */
1861 init_dependency_output (pfile)
1864 char *spec, *s, *output_file;
1866 /* Either of two environment variables can specify output of deps.
1867 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1868 where OUTPUT_FILE is the file to write deps info to
1869 and DEPS_TARGET is the target to mention in the deps. */
1871 if (CPP_OPTION (pfile, print_deps) == 0)
1873 spec = getenv ("DEPENDENCIES_OUTPUT");
1875 CPP_OPTION (pfile, print_deps) = 1;
1878 spec = getenv ("SUNPRO_DEPENDENCIES");
1880 CPP_OPTION (pfile, print_deps) = 2;
1885 /* Find the space before the DEPS_TARGET, if there is one. */
1886 s = strchr (spec, ' ');
1889 /* Let the caller perform MAKE quoting. */
1890 deps_add_target (pfile->deps, s + 1, 0);
1891 output_file = (char *) xmalloc (s - spec + 1);
1892 memcpy (output_file, spec, s - spec);
1893 output_file[s - spec] = 0;
1898 /* Command line -MF overrides environment variables and default. */
1899 if (CPP_OPTION (pfile, deps_file) == 0)
1900 CPP_OPTION (pfile, deps_file) = output_file;
1902 CPP_OPTION (pfile, print_deps_append) = 1;
1904 else if (CPP_OPTION (pfile, deps_file) == 0)
1905 /* If -M or -MM was seen without -MF, default output to wherever
1906 was specified with -o. out_fname is non-NULL here. */
1907 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1910 /* Handle --help output. */
1914 /* To keep the lines from getting too long for some compilers, limit
1915 to about 500 characters (6 lines) per chunk. */
1918 -include <file> Include the contents of <file> before other files\n\
1919 -imacros <file> Accept definition of macros in <file>\n\
1920 -iprefix <path> Specify <path> as a prefix for next two options\n\
1921 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1922 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1923 -isystem <dir> Add <dir> to the start of the system include path\n\
1926 -idirafter <dir> Add <dir> to the end of the system include path\n\
1927 -I <dir> Add <dir> to the end of the main include path\n\
1928 -I- Fine-grained include path control; see info docs\n\
1929 -nostdinc Do not search system include directories\n\
1930 (dirs specified with -isystem will still be used)\n\
1931 -nostdinc++ Do not search system include directories for C++\n\
1932 -o <file> Put output into <file>\n\
1935 -pedantic Issue all warnings demanded by strict ISO C\n\
1936 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1937 -trigraphs Support ISO C trigraphs\n\
1938 -lang-c Assume that the input sources are in C\n\
1939 -lang-c89 Assume that the input sources are in C89\n\
1942 -lang-c++ Assume that the input sources are in C++\n\
1943 -lang-objc Assume that the input sources are in ObjectiveC\n\
1944 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1945 -lang-asm Assume that the input sources are in assembler\n\
1948 -std=<std name> Specify the conformance standard; one of:\n\
1949 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1950 iso9899:199409, iso9899:1999\n\
1951 -+ Allow parsing of C++ style features\n\
1952 -w Inhibit warning messages\n\
1953 -Wtrigraphs Warn if trigraphs are encountered\n\
1954 -Wno-trigraphs Do not warn about trigraphs\n\
1955 -Wcomment{s} Warn if one comment starts inside another\n\
1958 -Wno-comment{s} Do not warn about comments\n\
1959 -Wtraditional Warn about features not present in traditional C\n\
1960 -Wno-traditional Do not warn about traditional C\n\
1961 -Wundef Warn if an undefined macro is used by #if\n\
1962 -Wno-undef Do not warn about testing undefined macros\n\
1963 -Wimport Warn about the use of the #import directive\n\
1966 -Wno-import Do not warn about the use of #import\n\
1967 -Werror Treat all warnings as errors\n\
1968 -Wno-error Do not treat warnings as errors\n\
1969 -Wsystem-headers Do not suppress warnings from system headers\n\
1970 -Wno-system-headers Suppress warnings from system headers\n\
1971 -Wall Enable all preprocessor warnings\n\
1974 -M Generate make dependencies\n\
1975 -MM As -M, but ignore system header files\n\
1976 -MD Generate make dependencies and compile\n\
1977 -MMD As -MD, but ignore system header files\n\
1978 -MF <file> Write dependency output to the given file\n\
1979 -MG Treat missing header file as generated files\n\
1982 -MP Generate phony targets for all headers\n\
1983 -MQ <target> Add a MAKE-quoted target\n\
1984 -MT <target> Add an unquoted target\n\
1987 -D<macro> Define a <macro> with string '1' as its value\n\
1988 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1989 -A<question>=<answer> Assert the <answer> to <question>\n\
1990 -A-<question>=<answer> Disable the <answer> to <question>\n\
1991 -U<macro> Undefine <macro> \n\
1992 -v Display the version number\n\
1995 -H Print the name of header files as they are used\n\
1996 -C Do not discard comments\n\
1997 -dM Display a list of macro definitions active at end\n\
1998 -dD Preserve macro definitions in output\n\
1999 -dN As -dD except that only the names are preserved\n\
2000 -dI Include #include directives in the output\n\
2003 -fpreprocessed Treat the input file as already preprocessed\n\
2004 -ftabstop=<number> Distance between tab stops for column reporting\n\
2005 -P Do not generate #line directives\n\
2006 -$ Do not allow '$' in identifiers\n\
2007 -remap Remap file names when including files\n\
2008 --version Display version information\n\
2009 -h or --help Display this information\n\