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 mark_named_operators PARAMS ((cpp_reader *));
105 static void append_include_chain PARAMS ((cpp_reader *,
107 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
108 struct search_path *));
109 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
110 struct search_path *));
111 static void merge_include_chains PARAMS ((cpp_reader *));
112 static bool push_include PARAMS ((cpp_reader *,
113 struct pending_option *));
114 static void free_chain PARAMS ((struct pending_option *));
115 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
116 static void init_dependency_output PARAMS ((cpp_reader *));
117 static void init_standard_includes PARAMS ((cpp_reader *));
118 static void read_original_filename PARAMS ((cpp_reader *));
119 static void new_pending_directive PARAMS ((struct cpp_pending *,
121 cl_directive_handler));
122 static void output_deps PARAMS ((cpp_reader *));
123 static int parse_option PARAMS ((const char *));
125 /* Fourth argument to append_include_chain: chain to use.
126 Note it's never asked to append to the quote chain. */
127 enum { BRACKET = 0, SYSTEM, AFTER };
129 /* If we have designated initializers (GCC >2.7) these tables can be
130 initialized, constant data. Otherwise, they have to be filled in at
132 #if HAVE_DESIGNATED_INITIALIZERS
134 #define init_trigraph_map() /* Nothing. */
135 #define TRIGRAPH_MAP \
136 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
139 #define s(p, v) [p] = v,
143 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
144 static void init_trigraph_map PARAMS ((void)) { \
145 unsigned char *x = _cpp_trigraph_map;
148 #define s(p, v) x[p] = v;
153 s('=', '#') s(')', ']') s('!', '|')
154 s('(', '[') s('\'', '^') s('>', '}')
155 s('/', '\\') s('<', '{') s('-', '~')
162 /* Given a colon-separated list of file names PATH,
163 add all the names to the search path for include files. */
165 path_include (pfile, list, path)
176 /* Find the end of this name. */
178 while (*q != 0 && *q != PATH_SEPARATOR) q++;
181 /* An empty name in the path stands for the current directory. */
182 name = (char *) xmalloc (2);
188 /* Otherwise use the directory that is named. */
189 name = (char *) xmalloc (q - p + 1);
190 memcpy (name, p, q - p);
194 append_include_chain (pfile, name, path, path == SYSTEM);
196 /* Advance past this name. */
204 /* Append DIR to include path PATH. DIR must be allocated on the
205 heap; this routine takes responsibility for freeing it. CXX_AWARE
206 is non-zero if the header contains extern "C" guards for C++,
207 otherwise it is zero. */
209 append_include_chain (pfile, dir, path, cxx_aware)
213 int cxx_aware ATTRIBUTE_UNUSED;
215 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
216 struct search_path *new;
225 _cpp_simplify_pathname (dir);
229 /* Dirs that don't exist are silently ignored. */
231 cpp_notice_from_errno (pfile, dir);
232 else if (CPP_OPTION (pfile, verbose))
233 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
238 if (!S_ISDIR (st.st_mode))
240 cpp_notice (pfile, "%s: Not a directory", dir);
246 if (len > pfile->max_include_len)
247 pfile->max_include_len = len;
249 new = (struct search_path *) xmalloc (sizeof (struct search_path));
252 INO_T_COPY (new->ino, st.st_ino);
253 new->dev = st.st_dev;
254 /* Both systm and after include file lists should be treated as system
255 include files since these two lists are really just a concatenation
256 of one "system" list. */
257 if (path == SYSTEM || path == AFTER)
258 #ifdef NO_IMPLICIT_EXTERN_C
261 new->sysp = cxx_aware ? 1 : 2;
265 new->name_map = NULL;
270 case BRACKET: APPEND (pend, brack, new); break;
271 case SYSTEM: APPEND (pend, systm, new); break;
272 case AFTER: APPEND (pend, after, new); break;
276 /* Handle a duplicated include path. PREV is the link in the chain
277 before the duplicate. The duplicate is removed from the chain and
278 freed. Returns PREV. */
279 static struct search_path *
280 remove_dup_dir (pfile, prev)
282 struct search_path *prev;
284 struct search_path *cur = prev->next;
286 if (CPP_OPTION (pfile, verbose))
287 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
289 prev->next = cur->next;
290 free ((PTR) cur->name);
296 /* Remove duplicate directories from a chain. Returns the tail of the
297 chain, or NULL if the chain is empty. This algorithm is quadratic
298 in the number of -I switches, which is acceptable since there
299 aren't usually that many of them. */
300 static struct search_path *
301 remove_dup_dirs (pfile, head)
303 struct search_path *head;
305 struct search_path *prev = NULL, *cur, *other;
307 for (cur = head; cur; cur = cur->next)
309 for (other = head; other != cur; other = other->next)
310 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
312 if (cur->sysp && !other->sysp)
315 "changing search order for system directory \"%s\"",
317 if (strcmp (cur->name, other->name))
319 " as it is the same as non-system directory \"%s\"",
323 " as it has already been specified as a non-system directory");
325 cur = remove_dup_dir (pfile, prev);
334 /* Merge the four include chains together in the order quote, bracket,
335 system, after. Remove duplicate dirs (as determined by
336 INO_T_EQ()). The system_include and after_include chains are never
337 referred to again after this function; all access is through the
338 bracket_include path. */
340 merge_include_chains (pfile)
343 struct search_path *quote, *brack, *systm, *qtail;
345 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
347 quote = pend->quote_head;
348 brack = pend->brack_head;
349 systm = pend->systm_head;
350 qtail = pend->quote_tail;
352 /* Paste together bracket, system, and after include chains. */
354 pend->systm_tail->next = pend->after_head;
356 systm = pend->after_head;
359 pend->brack_tail->next = systm;
363 /* This is a bit tricky. First we drop dupes from the quote-include
364 list. Then we drop dupes from the bracket-include list.
365 Finally, if qtail and brack are the same directory, we cut out
366 brack and move brack up to point to qtail.
368 We can't just merge the lists and then uniquify them because
369 then we may lose directories from the <> search path that should
370 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
371 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
372 -Ibar -I- -Ifoo -Iquux. */
374 remove_dup_dirs (pfile, brack);
375 qtail = remove_dup_dirs (pfile, quote);
381 /* If brack == qtail, remove brack as it's simpler. */
382 if (brack && INO_T_EQ (qtail->ino, brack->ino)
383 && qtail->dev == brack->dev)
384 brack = remove_dup_dir (pfile, qtail);
389 CPP_OPTION (pfile, quote_include) = quote;
390 CPP_OPTION (pfile, bracket_include) = brack;
393 /* A set of booleans indicating what CPP features each source language
400 char extended_numbers;
402 char dollars_in_ident;
403 char cplusplus_comments;
407 /* ??? Enable $ in identifiers in assembly? */
408 static const struct lang_flags lang_defaults[] =
409 { /* c99 objc c++ xnum std dollar c++comm digr */
410 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
411 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
412 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
413 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
414 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
415 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
416 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
417 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
418 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
419 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
422 /* Sets internal flags correctly for a given language. */
424 set_lang (pfile, lang)
428 const struct lang_flags *l = &lang_defaults[(int) lang];
430 CPP_OPTION (pfile, lang) = lang;
432 CPP_OPTION (pfile, c99) = l->c99;
433 CPP_OPTION (pfile, objc) = l->objc;
434 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
435 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
436 CPP_OPTION (pfile, std) = l->std;
437 CPP_OPTION (pfile, trigraphs) = l->std;
438 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
439 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
440 CPP_OPTION (pfile, digraphs) = l->digraphs;
444 static int opt_comp PARAMS ((const void *, const void *));
446 /* Run-time sorting of options array. */
451 return strcmp (((struct cl_option *) p1)->opt_text,
452 ((struct cl_option *) p2)->opt_text);
456 /* init initializes library global state. It might not need to
457 do anything depending on the platform and compiler. */
461 static int initialized = 0;
468 /* For non-ASCII hosts, the cl_options array needs to be sorted at
470 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
473 /* Set up the trigraph map. This doesn't need to do anything if
474 we were compiled with a compiler that supports C99 designated
476 init_trigraph_map ();
480 /* Initialize a cpp_reader structure. */
482 cpp_create_reader (lang)
487 /* Initialise this instance of the library if it hasn't been already. */
490 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
492 set_lang (pfile, lang);
493 CPP_OPTION (pfile, warn_import) = 1;
494 CPP_OPTION (pfile, discard_comments) = 1;
495 CPP_OPTION (pfile, show_column) = 1;
496 CPP_OPTION (pfile, tabstop) = 8;
497 CPP_OPTION (pfile, operator_names) = 1;
498 #if DEFAULT_SIGNED_CHAR
499 CPP_OPTION (pfile, signed_char) = 1;
501 CPP_OPTION (pfile, signed_char) = 0;
504 CPP_OPTION (pfile, pending) =
505 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
507 /* It's simplest to just create this struct whether or not it will
509 pfile->deps = deps_init ();
511 /* Initialise the line map. Start at logical line 1, so we can use
512 a line number of zero for special states. */
513 init_line_maps (&pfile->line_maps);
516 /* Initialize lexer state. */
517 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
519 /* Set up static tokens. */
520 pfile->date.type = CPP_EOF;
521 pfile->avoid_paste.type = CPP_PADDING;
522 pfile->avoid_paste.val.source = NULL;
523 pfile->eof.type = CPP_EOF;
524 pfile->eof.flags = 0;
526 /* Create a token buffer for the lexer. */
527 _cpp_init_tokenrun (&pfile->base_run, 250);
528 pfile->cur_run = &pfile->base_run;
529 pfile->cur_token = pfile->base_run.base;
531 /* Initialise the base context. */
532 pfile->context = &pfile->base_context;
533 pfile->base_context.macro = 0;
534 pfile->base_context.prev = pfile->base_context.next = 0;
536 /* Aligned and unaligned storage. */
537 pfile->a_buff = _cpp_get_buff (pfile, 0);
538 pfile->u_buff = _cpp_get_buff (pfile, 0);
540 /* Initialise the buffer obstack. */
541 gcc_obstack_init (&pfile->buffer_ob);
543 _cpp_init_includes (pfile);
548 /* Free resources used by PFILE. Accessing PFILE after this function
549 returns leads to undefined behaviour. Returns the error count. */
555 struct search_path *dir, *dirn;
556 cpp_context *context, *contextn;
557 tokenrun *run, *runn;
559 while (CPP_BUFFER (pfile) != NULL)
560 _cpp_pop_buffer (pfile);
562 if (pfile->macro_buffer)
564 free ((PTR) pfile->macro_buffer);
565 pfile->macro_buffer = NULL;
566 pfile->macro_buffer_len = 0;
569 deps_free (pfile->deps);
570 obstack_free (&pfile->buffer_ob, 0);
572 _cpp_destroy_hashtable (pfile);
573 _cpp_cleanup_includes (pfile);
575 _cpp_free_buff (pfile->a_buff);
576 _cpp_free_buff (pfile->u_buff);
577 _cpp_free_buff (pfile->free_buffs);
579 for (run = &pfile->base_run; run; run = runn)
583 if (run != &pfile->base_run)
587 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
590 free ((PTR) dir->name);
594 for (context = pfile->base_context.next; context; context = contextn)
596 contextn = context->next;
600 free_line_maps (&pfile->line_maps);
602 result = pfile->errors;
609 /* This structure defines one built-in identifier. A node will be
610 entered in the hash table under the name NAME, with value VALUE (if
611 any). If flags has OPERATOR, the node's operator field is used; if
612 flags has BUILTIN the node's builtin field is used. Macros that are
613 known at build time should not be flagged BUILTIN, as then they do
614 not appear in macro dumps with e.g. -dM or -dD.
616 Two values are not compile time constants, so we tag
617 them in the FLAGS field instead:
618 VERS value is the global version_string, quoted
619 ULP value is the global user_label_prefix */
624 unsigned char builtin;
625 unsigned short flags;
632 #define B(n, t) { U n, 0, t, BUILTIN, sizeof n - 1 }
633 #define C(n, v) { U n, v, 0, 0, sizeof n - 1 }
634 #define X(n, f) { U n, 0, 0, f, sizeof n - 1 }
635 static const struct builtin builtin_array[] =
637 B("__TIME__", BT_TIME),
638 B("__DATE__", BT_DATE),
639 B("__FILE__", BT_FILE),
640 B("__BASE_FILE__", BT_BASE_FILE),
641 B("__LINE__", BT_SPECLINE),
642 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
643 B("_Pragma", BT_PRAGMA),
645 X("__VERSION__", VERS),
646 X("__USER_LABEL_PREFIX__", ULP),
647 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
648 C("__HAVE_BUILTIN_SETJMP__", "1"),
649 #if USING_SJLJ_EXCEPTIONS
650 /* libgcc needs to know this. */
651 C("__USING_SJLJ_EXCEPTIONS__","1"),
653 #ifndef NO_BUILTIN_SIZE_TYPE
654 C("__SIZE_TYPE__", SIZE_TYPE),
656 #ifndef NO_BUILTIN_PTRDIFF_TYPE
657 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
659 #ifndef NO_BUILTIN_WCHAR_TYPE
660 C("__WCHAR_TYPE__", WCHAR_TYPE),
662 #ifndef NO_BUILTIN_WINT_TYPE
663 C("__WINT_TYPE__", WINT_TYPE),
665 #ifdef STDC_0_IN_SYSTEM_HEADERS
666 B("__STDC__", BT_STDC),
674 #define builtin_array_end \
675 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
677 /* Named operators known to the preprocessor. These cannot be
678 #defined and always have their stated meaning. They are treated
679 like normal identifiers except for the type code and the meaning.
680 Most of them are only for C++ (but see iso646.h). */
681 #define B(n, t) { DSC(n), t }
682 static const struct named_op
686 enum cpp_ttype value;
687 } operator_array[] = {
688 B("and", CPP_AND_AND),
689 B("and_eq", CPP_AND_EQ),
690 B("bitand", CPP_AND),
692 B("compl", CPP_COMPL),
694 B("not_eq", CPP_NOT_EQ),
696 B("or_eq", CPP_OR_EQ),
698 B("xor_eq", CPP_XOR_EQ)
702 /* Mark the C++ named operators in the hash table. */
704 mark_named_operators (pfile)
707 const struct named_op *b;
709 for (b = operator_array;
710 b < (operator_array + ARRAY_SIZE (operator_array));
713 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
714 hp->flags |= NODE_OPERATOR;
715 hp->value.operator = b->value;
719 /* Subroutine of cpp_read_main_file; reads the builtins table above and
720 enters them, and language-specific macros, into the hash table. */
722 init_builtins (pfile)
725 const struct builtin *b;
727 for(b = builtin_array; b < builtin_array_end; b++)
729 if (b->flags & BUILTIN)
731 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
733 hp->flags |= NODE_BUILTIN | NODE_WARN;
734 hp->value.builtin = b->builtin;
736 else /* A standard macro of some kind. */
743 /* Allocate enough space for 'name "value"\n\0'. */
744 str = alloca (b->len + strlen (version_string) + 5);
745 sprintf (str, "%s \"%s\"\n", b->name, version_string);
750 val = CPP_OPTION (pfile, user_label_prefix);
754 /* Allocate enough space for "name value\n\0". */
755 str = alloca (b->len + strlen (val) + 3);
756 sprintf(str, "%s %s\n", b->name, val);
759 _cpp_define_builtin (pfile, str);
763 if (CPP_OPTION (pfile, cplusplus))
765 _cpp_define_builtin (pfile, "__cplusplus 1");
766 if (SUPPORTS_ONE_ONLY)
767 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
769 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
771 if (CPP_OPTION (pfile, objc))
772 _cpp_define_builtin (pfile, "__OBJC__ 1");
774 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
775 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
776 else if (CPP_OPTION (pfile, c99))
777 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
779 if (CPP_OPTION (pfile, signed_char) == 0)
780 _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
782 if (CPP_OPTION (pfile, lang) == CLK_STDC89
783 || CPP_OPTION (pfile, lang) == CLK_STDC94
784 || CPP_OPTION (pfile, lang) == CLK_STDC99)
785 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
786 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
787 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
793 #undef builtin_array_end
795 /* And another subroutine. This one sets up the standard include path. */
797 init_standard_includes (pfile)
801 const struct default_include *p;
802 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
804 /* Several environment variables may add to the include search path.
805 CPATH specifies an additional list of directories to be searched
806 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
807 etc. specify an additional list of directories to be searched as
808 if specified with -isystem, for the language indicated. */
810 GET_ENV_PATH_LIST (path, "CPATH");
811 if (path != 0 && *path != 0)
812 path_include (pfile, path, BRACKET);
814 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
817 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
820 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
823 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
826 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
829 if (path != 0 && *path != 0)
830 path_include (pfile, path, SYSTEM);
832 /* Search "translated" versions of GNU directories.
833 These have /usr/local/lib/gcc... replaced by specd_prefix. */
834 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
836 /* Remove the `include' from /usr/local/lib/gcc.../include.
837 GCC_INCLUDE_DIR will always end in /include. */
838 int default_len = cpp_GCC_INCLUDE_DIR_len;
839 char *default_prefix = (char *) alloca (default_len + 1);
840 int specd_len = strlen (specd_prefix);
842 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
843 default_prefix[default_len] = '\0';
845 for (p = cpp_include_defaults; p->fname; p++)
847 /* Some standard dirs are only for C++. */
849 || (CPP_OPTION (pfile, cplusplus)
850 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
852 /* Does this dir start with the prefix? */
853 if (!memcmp (p->fname, default_prefix, default_len))
855 /* Yes; change prefix and add to search list. */
856 int flen = strlen (p->fname);
857 int this_len = specd_len + flen - default_len;
858 char *str = (char *) xmalloc (this_len + 1);
859 memcpy (str, specd_prefix, specd_len);
860 memcpy (str + specd_len,
861 p->fname + default_len,
862 flen - default_len + 1);
864 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
870 /* Search ordinary names for GNU include directories. */
871 for (p = cpp_include_defaults; p->fname; p++)
873 /* Some standard dirs are only for C++. */
875 || (CPP_OPTION (pfile, cplusplus)
876 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
878 char *str = update_path (p->fname, p->component);
879 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
884 /* Pushes a command line -imacro and -include file indicated by P onto
885 the buffer stack. Returns non-zero if successful. */
887 push_include (pfile, p)
889 struct pending_option *p;
893 /* Later: maybe update this to use the #include "" search path
894 if cpp_read_file fails. */
895 header.type = CPP_STRING;
896 header.val.str.text = (const unsigned char *) p->arg;
897 header.val.str.len = strlen (p->arg);
898 /* Make the command line directive take up a line. */
901 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
904 /* Frees a pending_option chain. */
907 struct pending_option *head;
909 struct pending_option *next;
919 /* This is called after options have been parsed, and partially
920 processed. Setup for processing input from the file named FNAME,
921 or stdin if it is the empty string. Return the original filename
922 on success (e.g. foo.i->foo.c), or NULL on failure. */
924 cpp_read_main_file (pfile, fname, table)
929 /* The front ends don't set up the hash table until they have
930 finished processing the command line options, so initializing the
931 hashtable is deferred until now. */
932 _cpp_init_hashtable (pfile, table);
934 /* Set up the include search path now. */
935 if (! CPP_OPTION (pfile, no_standard_includes))
936 init_standard_includes (pfile);
938 merge_include_chains (pfile);
940 /* With -v, print the list of dirs to search. */
941 if (CPP_OPTION (pfile, verbose))
943 struct search_path *l;
944 fprintf (stderr, _("#include \"...\" search starts here:\n"));
945 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
947 if (l == CPP_OPTION (pfile, bracket_include))
948 fprintf (stderr, _("#include <...> search starts here:\n"));
949 fprintf (stderr, " %s\n", l->name);
951 fprintf (stderr, _("End of search list.\n"));
954 if (CPP_OPTION (pfile, print_deps))
955 /* Set the default target (if there is none already). */
956 deps_add_default_target (pfile->deps, fname);
958 /* Open the main input file. */
959 if (!_cpp_read_file (pfile, fname))
962 /* Set this after cpp_post_options so the client can change the
963 option if it wishes, and after stacking the main file so we don't
964 trace the main file. */
965 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
967 /* For foo.i, read the original filename foo.c now, for the benefit
968 of the front ends. */
969 if (CPP_OPTION (pfile, preprocessed))
970 read_original_filename (pfile);
972 return pfile->map->to_file;
975 /* For preprocessed files, if the first tokens are of the form # NUM.
976 handle the directive so we know the original file name. This will
977 generate file_change callbacks, which the front ends must handle
978 appropriately given their state of initialization. */
980 read_original_filename (pfile)
983 const cpp_token *token, *token1;
985 /* Lex ahead; if the first tokens are of the form # NUM, then
986 process the directive, otherwise back up. */
987 token = _cpp_lex_direct (pfile);
988 if (token->type == CPP_HASH)
990 token1 = _cpp_lex_direct (pfile);
991 _cpp_backup_tokens (pfile, 1);
993 /* If it's a #line directive, handle it. */
994 if (token1->type == CPP_NUMBER)
996 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
1001 /* Backup as if nothing happened. */
1002 _cpp_backup_tokens (pfile, 1);
1005 /* Handle pending command line options: -D, -U, -A, -imacros and
1006 -include. This should be called after debugging has been properly
1007 set up in the front ends. */
1009 cpp_finish_options (pfile)
1012 /* Mark named operators before handling command line macros. */
1013 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
1014 mark_named_operators (pfile);
1016 /* Install builtins and process command line macros etc. in the order
1017 they appeared, but only if not already preprocessed. */
1018 if (! CPP_OPTION (pfile, preprocessed))
1020 struct pending_option *p;
1022 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1023 init_builtins (pfile);
1024 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1025 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1026 (*p->handler) (pfile, p->arg);
1028 /* Scan -imacros files after command line defines, but before
1029 files given with -include. */
1030 while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
1032 if (push_include (pfile, p))
1034 pfile->buffer->return_at_eof = true;
1035 cpp_scan_nooutput (pfile);
1037 CPP_OPTION (pfile, pending)->imacros_head = p->next;
1042 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1043 _cpp_push_next_buffer (pfile);
1046 /* Called to push the next buffer on the stack given by -include. If
1047 there are none, free the pending structure and restore the line map
1048 for the main file. */
1050 _cpp_push_next_buffer (pfile)
1053 bool pushed = false;
1055 /* This is't pretty; we'd rather not be relying on this as a boolean
1056 for reverting the line map. Further, we only free the chains in
1057 this conditional, so an early call to cpp_finish / cpp_destroy
1058 will leak that memory. */
1059 if (CPP_OPTION (pfile, pending)
1060 && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1064 struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1068 if (! CPP_OPTION (pfile, preprocessed))
1069 pushed = push_include (pfile, p);
1070 CPP_OPTION (pfile, pending)->include_head = p->next;
1076 free (CPP_OPTION (pfile, pending));
1077 CPP_OPTION (pfile, pending) = NULL;
1079 /* Restore the line map for the main file. */
1080 if (! CPP_OPTION (pfile, preprocessed))
1081 _cpp_do_file_change (pfile, LC_RENAME,
1082 pfile->line_maps.maps[0].to_file, 1, 0);
1089 /* Use mkdeps.c to output dependency information. */
1094 /* Stream on which to print the dependency information. */
1095 FILE *deps_stream = 0;
1096 const char *const deps_mode =
1097 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1099 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1100 deps_stream = stdout;
1103 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1104 if (deps_stream == 0)
1106 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1111 deps_write (pfile->deps, deps_stream, 72);
1113 if (CPP_OPTION (pfile, deps_phony_targets))
1114 deps_phony_targets (pfile->deps, deps_stream);
1116 /* Don't close stdout. */
1117 if (deps_stream != stdout)
1119 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1120 cpp_fatal (pfile, "I/O error on output");
1124 /* This is called at the end of preprocessing. It pops the
1125 last buffer and writes dependency output. It should also
1126 clear macro definitions, such that you could call cpp_start_read
1127 with a new filename to restart processing. */
1132 /* cpplex.c leaves the final buffer on the stack. This it so that
1133 it returns an unending stream of CPP_EOFs to the client. If we
1134 popped the buffer, we'd dereference a NULL buffer pointer and
1135 segfault. It's nice to allow the client to do worry-free excess
1136 cpp_get_token calls. */
1137 while (pfile->buffer)
1138 _cpp_pop_buffer (pfile);
1140 /* Don't write the deps file if preprocessing has failed. */
1141 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1142 output_deps (pfile);
1144 /* Report on headers that could use multiple include guards. */
1145 if (CPP_OPTION (pfile, print_include_names))
1146 _cpp_report_missing_guards (pfile);
1149 /* Add a directive to be handled later in the initialization phase. */
1151 new_pending_directive (pend, text, handler)
1152 struct cpp_pending *pend;
1154 cl_directive_handler handler;
1156 struct pending_option *o = (struct pending_option *)
1157 xmalloc (sizeof (struct pending_option));
1161 o->handler = handler;
1162 APPEND (pend, directive, o);
1165 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1166 I.e. a const string initializer with parens around it. That is
1167 what N_("string") resolves to, so we make no_* be macros instead. */
1168 #define no_arg N_("argument missing after %s")
1169 #define no_ass N_("assertion missing after %s")
1170 #define no_dir N_("directory name missing after %s")
1171 #define no_fil N_("file name missing after %s")
1172 #define no_mac N_("macro name missing after %s")
1173 #define no_pth N_("path name missing after %s")
1174 #define no_num N_("number missing after %s")
1175 #define no_tgt N_("target missing after %s")
1177 /* This is the list of all command line options, with the leading
1178 "-" removed. It must be sorted in ASCII collating order. */
1179 #define COMMAND_LINE_OPTIONS \
1180 DEF_OPT("$", 0, OPT_dollar) \
1181 DEF_OPT("+", 0, OPT_plus) \
1182 DEF_OPT("-help", 0, OPT__help) \
1183 DEF_OPT("-target-help", 0, OPT_target__help) \
1184 DEF_OPT("-version", 0, OPT__version) \
1185 DEF_OPT("A", no_ass, OPT_A) \
1186 DEF_OPT("C", 0, OPT_C) \
1187 DEF_OPT("D", no_mac, OPT_D) \
1188 DEF_OPT("H", 0, OPT_H) \
1189 DEF_OPT("I", no_dir, OPT_I) \
1190 DEF_OPT("M", 0, OPT_M) \
1191 DEF_OPT("MD", no_fil, OPT_MD) \
1192 DEF_OPT("MF", no_fil, OPT_MF) \
1193 DEF_OPT("MG", 0, OPT_MG) \
1194 DEF_OPT("MM", 0, OPT_MM) \
1195 DEF_OPT("MMD", no_fil, OPT_MMD) \
1196 DEF_OPT("MP", 0, OPT_MP) \
1197 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1198 DEF_OPT("MT", no_tgt, OPT_MT) \
1199 DEF_OPT("P", 0, OPT_P) \
1200 DEF_OPT("U", no_mac, OPT_U) \
1201 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1202 DEF_OPT("d", no_arg, OPT_d) \
1203 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1204 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1205 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1206 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1207 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1208 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1209 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1210 DEF_OPT("fsigned-char", 0, OPT_fsigned_char) \
1211 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1212 DEF_OPT("funsigned-char", 0, OPT_funsigned_char) \
1213 DEF_OPT("h", 0, OPT_h) \
1214 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1215 DEF_OPT("imacros", no_fil, OPT_imacros) \
1216 DEF_OPT("include", no_fil, OPT_include) \
1217 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1218 DEF_OPT("isystem", no_dir, OPT_isystem) \
1219 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1220 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1221 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1222 DEF_OPT("lang-c", 0, OPT_lang_c) \
1223 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1224 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1225 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1226 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1227 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1228 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1229 DEF_OPT("o", no_fil, OPT_o) \
1230 DEF_OPT("pedantic", 0, OPT_pedantic) \
1231 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1232 DEF_OPT("remap", 0, OPT_remap) \
1233 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1234 DEF_OPT("std=c89", 0, OPT_std_c89) \
1235 DEF_OPT("std=c99", 0, OPT_std_c99) \
1236 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1237 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1238 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1239 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1240 DEF_OPT("std=bsd", 0, OPT_std_bsd) \
1241 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1242 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1243 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1244 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1245 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1246 DEF_OPT("v", 0, OPT_v) \
1247 DEF_OPT("version", 0, OPT_version) \
1248 DEF_OPT("w", 0, OPT_w)
1250 #define DEF_OPT(text, msg, code) code,
1253 COMMAND_LINE_OPTIONS
1260 const char *opt_text;
1263 enum opt_code opt_code;
1266 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1268 static struct cl_option cl_options[] =
1270 static const struct cl_option cl_options[] =
1273 COMMAND_LINE_OPTIONS
1276 #undef COMMAND_LINE_OPTIONS
1278 /* Perform a binary search to find which, if any, option the given
1279 command-line matches. Returns its index in the option array,
1280 negative on failure. Complications arise since some options can be
1281 suffixed with an argument, and multiple complete matches can occur,
1282 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1283 accept options beginning with -W that we do not recognise, but not
1284 to swallow any subsequent command line argument; this is handled as
1285 special cases in cpp_handle_option. */
1287 parse_option (input)
1290 unsigned int md, mn, mx;
1301 opt_len = cl_options[md].opt_len;
1302 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1310 if (input[opt_len] == '\0')
1312 /* We were passed more text. If the option takes an argument,
1313 we may match a later option or we may have been passed the
1314 argument. The longest possible option match succeeds.
1315 If the option takes no arguments we have not matched and
1316 continue the search (e.g. input="stdc++" match was "stdc"). */
1318 if (cl_options[md].msg)
1320 /* Scan forwards. If we get an exact match, return it.
1321 Otherwise, return the longest option-accepting match.
1322 This loops no more than twice with current options. */
1324 for (; mn < (unsigned int) N_OPTS; mn++)
1326 opt_len = cl_options[mn].opt_len;
1327 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1329 if (input[opt_len] == '\0')
1331 if (cl_options[mn].msg)
1342 /* Handle one command-line option in (argc, argv).
1343 Can be called multiple times, to handle multiple sets of options.
1344 If ignore is non-zero, this will ignore unrecognized -W* options.
1345 Returns number of strings consumed. */
1347 cpp_handle_option (pfile, argc, argv, ignore)
1354 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1356 /* Interpret "-" or a non-option as a file name. */
1357 if (argv[i][0] != '-' || argv[i][1] == '\0')
1359 if (CPP_OPTION (pfile, in_fname) == NULL)
1360 CPP_OPTION (pfile, in_fname) = argv[i];
1361 else if (CPP_OPTION (pfile, out_fname) == NULL)
1362 CPP_OPTION (pfile, out_fname) = argv[i];
1364 cpp_fatal (pfile, "too many filenames. Type %s --help for usage info",
1369 enum opt_code opt_code;
1371 const char *arg = 0;
1373 /* Skip over '-'. */
1374 opt_index = parse_option (&argv[i][1]);
1378 opt_code = cl_options[opt_index].opt_code;
1379 if (cl_options[opt_index].msg)
1381 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1383 /* Yuk. Special case for -W as it must not swallow
1384 up any following argument. If this becomes common, add
1385 another field to the cl_options table. */
1386 if (arg[0] == '\0' && opt_code != OPT_W)
1391 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1399 case N_OPTS: /* Shut GCC up. */
1401 case OPT_fleading_underscore:
1402 CPP_OPTION (pfile, user_label_prefix) = "_";
1404 case OPT_fno_leading_underscore:
1405 CPP_OPTION (pfile, user_label_prefix) = "";
1407 case OPT_fno_operator_names:
1408 CPP_OPTION (pfile, operator_names) = 0;
1410 case OPT_fpreprocessed:
1411 CPP_OPTION (pfile, preprocessed) = 1;
1413 case OPT_fno_preprocessed:
1414 CPP_OPTION (pfile, preprocessed) = 0;
1416 case OPT_fshow_column:
1417 CPP_OPTION (pfile, show_column) = 1;
1419 case OPT_fno_show_column:
1420 CPP_OPTION (pfile, show_column) = 0;
1422 case OPT_fsigned_char:
1423 CPP_OPTION (pfile, signed_char) = 1;
1425 case OPT_funsigned_char:
1426 CPP_OPTION (pfile, signed_char) = 0;
1429 /* Silently ignore empty string, non-longs and silly values. */
1433 long tabstop = strtol (arg, &endptr, 10);
1434 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1435 CPP_OPTION (pfile, tabstop) = tabstop;
1439 CPP_OPTION (pfile, inhibit_warnings) = 1;
1444 CPP_OPTION (pfile, help_only) = 1;
1446 case OPT_target__help:
1447 /* Print if any target specific options. cpplib has none, but
1448 make sure help_only gets set. */
1449 CPP_OPTION (pfile, help_only) = 1;
1452 /* --version inhibits compilation, -version doesn't. -v means
1453 verbose and -version. Historical reasons, don't ask. */
1455 CPP_OPTION (pfile, help_only) = 1;
1456 pfile->print_version = 1;
1459 CPP_OPTION (pfile, verbose) = 1;
1460 pfile->print_version = 1;
1463 pfile->print_version = 1;
1467 CPP_OPTION (pfile, discard_comments) = 0;
1470 CPP_OPTION (pfile, no_line_commands) = 1;
1472 case OPT_dollar: /* Don't include $ in identifiers. */
1473 CPP_OPTION (pfile, dollars_in_ident) = 0;
1476 CPP_OPTION (pfile, print_include_names) = 1;
1479 new_pending_directive (pend, arg, cpp_define);
1481 case OPT_pedantic_errors:
1482 CPP_OPTION (pfile, pedantic_errors) = 1;
1485 CPP_OPTION (pfile, pedantic) = 1;
1488 CPP_OPTION (pfile, trigraphs) = 1;
1491 CPP_OPTION (pfile, cplusplus) = 1;
1492 CPP_OPTION (pfile, cplusplus_comments) = 1;
1495 CPP_OPTION (pfile, remap) = 1;
1498 CPP_OPTION (pfile, include_prefix) = arg;
1499 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1502 set_lang (pfile, CLK_GNUC89);
1504 case OPT_lang_cplusplus:
1505 set_lang (pfile, CLK_GNUCXX);
1508 set_lang (pfile, CLK_OBJC);
1510 case OPT_lang_objcplusplus:
1511 set_lang (pfile, CLK_OBJCXX);
1514 set_lang (pfile, CLK_ASM);
1516 case OPT_std_cplusplus98:
1517 set_lang (pfile, CLK_CXX98);
1520 set_lang (pfile, CLK_GNUC89);
1524 set_lang (pfile, CLK_GNUC99);
1527 set_lang (pfile, CLK_GNUC89);
1529 case OPT_std_iso9899_199409:
1530 set_lang (pfile, CLK_STDC94);
1532 case OPT_std_iso9899_1990:
1535 set_lang (pfile, CLK_STDC89);
1537 case OPT_std_iso9899_199x:
1538 case OPT_std_iso9899_1999:
1541 set_lang (pfile, CLK_STDC99);
1544 /* -nostdinc causes no default include directories.
1545 You must specify all include-file directories with -I. */
1546 CPP_OPTION (pfile, no_standard_includes) = 1;
1548 case OPT_nostdincplusplus:
1549 /* -nostdinc++ causes no default C++-specific include directories. */
1550 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1553 if (CPP_OPTION (pfile, out_fname) == NULL)
1554 CPP_OPTION (pfile, out_fname) = arg;
1557 cpp_fatal (pfile, "output filename specified twice");
1562 /* Args to -d specify what parts of macros to dump.
1563 Silently ignore unrecognised options; they may
1564 be aimed at the compiler proper. */
1568 while ((c = *arg++) != '\0')
1572 CPP_OPTION (pfile, dump_macros) = dump_only;
1575 CPP_OPTION (pfile, dump_macros) = dump_names;
1578 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1581 CPP_OPTION (pfile, dump_includes) = 1;
1588 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1591 /* When doing dependencies with -M or -MM, suppress normal
1592 preprocessed output, but still do -dM etc. as software
1593 depends on this. Preprocessed output occurs if -MD, -MMD
1594 or environment var dependency generation is used. */
1595 CPP_OPTION (pfile, print_deps) = 2;
1596 CPP_OPTION (pfile, no_output) = 1;
1599 CPP_OPTION (pfile, print_deps) = 1;
1600 CPP_OPTION (pfile, no_output) = 1;
1603 CPP_OPTION (pfile, deps_file) = arg;
1606 CPP_OPTION (pfile, deps_phony_targets) = 1;
1610 /* Add a target. -MQ quotes for Make. */
1611 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1615 CPP_OPTION (pfile, print_deps) = 2;
1616 CPP_OPTION (pfile, deps_file) = arg;
1619 CPP_OPTION (pfile, print_deps) = 1;
1620 CPP_OPTION (pfile, deps_file) = arg;
1626 /* -A with an argument beginning with '-' acts as
1627 #unassert on whatever immediately follows the '-'.
1628 If "-" is the whole argument, we eliminate all
1629 predefined macros and assertions, including those
1630 that were specified earlier on the command line.
1631 That way we can get rid of any that were passed
1632 automatically in from GCC. */
1636 free_chain (pend->directive_head);
1637 pend->directive_head = NULL;
1638 pend->directive_tail = NULL;
1641 new_pending_directive (pend, arg + 1, cpp_unassert);
1644 new_pending_directive (pend, arg, cpp_assert);
1647 new_pending_directive (pend, arg, cpp_undef);
1649 case OPT_I: /* Add directory to path for includes. */
1650 if (!strcmp (arg, "-"))
1653 Use the preceding -I directories for #include "..."
1654 but not #include <...>.
1655 Don't search the directory of the present file
1656 for #include "...". (Note that -I. -I- is not the same as
1657 the default setup; -I. uses the compiler's working dir.) */
1658 if (! CPP_OPTION (pfile, ignore_srcdir))
1660 pend->quote_head = pend->brack_head;
1661 pend->quote_tail = pend->brack_tail;
1662 pend->brack_head = 0;
1663 pend->brack_tail = 0;
1664 CPP_OPTION (pfile, ignore_srcdir) = 1;
1668 cpp_fatal (pfile, "-I- specified twice");
1673 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1676 /* Add directory to beginning of system include path, as a system
1677 include directory. */
1678 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1683 struct pending_option *o = (struct pending_option *)
1684 xmalloc (sizeof (struct pending_option));
1688 if (opt_code == OPT_include)
1689 APPEND (pend, include, o);
1691 APPEND (pend, imacros, o);
1694 case OPT_iwithprefix:
1695 /* Add directory to end of path for includes,
1696 with the default prefix at the front of its name. */
1698 case OPT_iwithprefixbefore:
1699 /* Add directory to main path for includes,
1700 with the default prefix at the front of its name. */
1707 if (CPP_OPTION (pfile, include_prefix) != 0)
1709 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1710 fname = xmalloc (ipl + len + 1);
1711 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1712 memcpy (fname + ipl, arg, len + 1);
1714 else if (cpp_GCC_INCLUDE_DIR_len)
1716 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1717 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1718 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1721 fname = xstrdup (arg);
1723 append_include_chain (pfile, fname,
1724 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1728 /* Add directory to end of path for includes. */
1729 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1732 /* Silently ignore unrecognised options. */
1733 if (!strcmp (argv[i], "-Wall"))
1735 CPP_OPTION (pfile, warn_trigraphs) = 1;
1736 CPP_OPTION (pfile, warn_comments) = 1;
1738 else if (!strcmp (argv[i], "-Wtraditional"))
1739 CPP_OPTION (pfile, warn_traditional) = 1;
1740 else if (!strcmp (argv[i], "-Wtrigraphs"))
1741 CPP_OPTION (pfile, warn_trigraphs) = 1;
1742 else if (!strcmp (argv[i], "-Wcomment"))
1743 CPP_OPTION (pfile, warn_comments) = 1;
1744 else if (!strcmp (argv[i], "-Wcomments"))
1745 CPP_OPTION (pfile, warn_comments) = 1;
1746 else if (!strcmp (argv[i], "-Wundef"))
1747 CPP_OPTION (pfile, warn_undef) = 1;
1748 else if (!strcmp (argv[i], "-Wimport"))
1749 CPP_OPTION (pfile, warn_import) = 1;
1750 else if (!strcmp (argv[i], "-Werror"))
1751 CPP_OPTION (pfile, warnings_are_errors) = 1;
1752 else if (!strcmp (argv[i], "-Wsystem-headers"))
1753 CPP_OPTION (pfile, warn_system_headers) = 1;
1754 else if (!strcmp (argv[i], "-Wno-traditional"))
1755 CPP_OPTION (pfile, warn_traditional) = 0;
1756 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1757 CPP_OPTION (pfile, warn_trigraphs) = 0;
1758 else if (!strcmp (argv[i], "-Wno-comment"))
1759 CPP_OPTION (pfile, warn_comments) = 0;
1760 else if (!strcmp (argv[i], "-Wno-comments"))
1761 CPP_OPTION (pfile, warn_comments) = 0;
1762 else if (!strcmp (argv[i], "-Wno-undef"))
1763 CPP_OPTION (pfile, warn_undef) = 0;
1764 else if (!strcmp (argv[i], "-Wno-import"))
1765 CPP_OPTION (pfile, warn_import) = 0;
1766 else if (!strcmp (argv[i], "-Wno-error"))
1767 CPP_OPTION (pfile, warnings_are_errors) = 0;
1768 else if (!strcmp (argv[i], "-Wno-system-headers"))
1769 CPP_OPTION (pfile, warn_system_headers) = 0;
1778 /* Handle command-line options in (argc, argv).
1779 Can be called multiple times, to handle multiple sets of options.
1780 Returns if an unrecognized option is seen.
1781 Returns number of strings consumed. */
1783 cpp_handle_options (pfile, argc, argv)
1789 int strings_processed;
1791 for (i = 0; i < argc; i += strings_processed)
1793 strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
1794 if (strings_processed == 0)
1801 /* Extra processing when all options are parsed, after all calls to
1802 cpp_handle_option[s]. Consistency checks etc. */
1804 cpp_post_options (pfile)
1807 if (pfile->print_version)
1809 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1810 #ifdef TARGET_VERSION
1813 fputc ('\n', stderr);
1816 /* Canonicalize in_fname and out_fname. We guarantee they are not
1817 NULL, and that the empty string represents stdin / stdout. */
1818 if (CPP_OPTION (pfile, in_fname) == NULL
1819 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1820 CPP_OPTION (pfile, in_fname) = "";
1822 if (CPP_OPTION (pfile, out_fname) == NULL
1823 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1824 CPP_OPTION (pfile, out_fname) = "";
1826 /* -Wtraditional is not useful in C++ mode. */
1827 if (CPP_OPTION (pfile, cplusplus))
1828 CPP_OPTION (pfile, warn_traditional) = 0;
1830 /* Set this if it hasn't been set already. */
1831 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1832 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1834 /* Permanently disable macro expansion if we are rescanning
1835 preprocessed text. */
1836 if (CPP_OPTION (pfile, preprocessed))
1837 pfile->state.prevent_expansion = 1;
1839 /* -dM makes no normal output. This is set here so that -dM -dD
1840 works as expected. */
1841 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1842 CPP_OPTION (pfile, no_output) = 1;
1844 /* Disable -dD, -dN and -dI if we should make no normal output
1845 (such as with -M). Allow -M -dM since some software relies on
1847 if (CPP_OPTION (pfile, no_output))
1849 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1850 CPP_OPTION (pfile, dump_macros) = dump_none;
1851 CPP_OPTION (pfile, dump_includes) = 0;
1854 /* We need to do this after option processing and before
1855 cpp_start_read, as cppmain.c relies on the options->no_output to
1856 set its callbacks correctly before calling cpp_start_read. */
1857 init_dependency_output (pfile);
1859 /* After checking the environment variables, check if -M or -MM has
1860 not been specified, but other -M options have. */
1861 if (CPP_OPTION (pfile, print_deps) == 0 &&
1862 (CPP_OPTION (pfile, print_deps_missing_files)
1863 || CPP_OPTION (pfile, deps_file)
1864 || CPP_OPTION (pfile, deps_phony_targets)))
1865 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1868 /* Set up dependency-file output. On exit, if print_deps is non-zero
1869 then deps_file is not NULL; stdout is the empty string. */
1871 init_dependency_output (pfile)
1874 char *spec, *s, *output_file;
1876 /* Either of two environment variables can specify output of deps.
1877 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1878 where OUTPUT_FILE is the file to write deps info to
1879 and DEPS_TARGET is the target to mention in the deps. */
1881 if (CPP_OPTION (pfile, print_deps) == 0)
1883 spec = getenv ("DEPENDENCIES_OUTPUT");
1885 CPP_OPTION (pfile, print_deps) = 1;
1888 spec = getenv ("SUNPRO_DEPENDENCIES");
1891 CPP_OPTION (pfile, print_deps) = 2;
1892 CPP_OPTION (pfile, deps_ignore_main_file) = 1;
1898 /* Find the space before the DEPS_TARGET, if there is one. */
1899 s = strchr (spec, ' ');
1902 /* Let the caller perform MAKE quoting. */
1903 deps_add_target (pfile->deps, s + 1, 0);
1904 output_file = (char *) xmalloc (s - spec + 1);
1905 memcpy (output_file, spec, s - spec);
1906 output_file[s - spec] = 0;
1911 /* Command line -MF overrides environment variables and default. */
1912 if (CPP_OPTION (pfile, deps_file) == 0)
1913 CPP_OPTION (pfile, deps_file) = output_file;
1915 CPP_OPTION (pfile, print_deps_append) = 1;
1917 else if (CPP_OPTION (pfile, deps_file) == 0)
1918 /* If -M or -MM was seen without -MF, default output to wherever
1919 was specified with -o. out_fname is non-NULL here. */
1920 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1923 /* Handle --help output. */
1927 /* To keep the lines from getting too long for some compilers, limit
1928 to about 500 characters (6 lines) per chunk. */
1931 -include <file> Include the contents of <file> before other files\n\
1932 -imacros <file> Accept definition of macros in <file>\n\
1933 -iprefix <path> Specify <path> as a prefix for next two options\n\
1934 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1935 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1936 -isystem <dir> Add <dir> to the start of the system include path\n\
1939 -idirafter <dir> Add <dir> to the end of the system include path\n\
1940 -I <dir> Add <dir> to the end of the main include path\n\
1941 -I- Fine-grained include path control; see info docs\n\
1942 -nostdinc Do not search system include directories\n\
1943 (dirs specified with -isystem will still be used)\n\
1944 -nostdinc++ Do not search system include directories for C++\n\
1945 -o <file> Put output into <file>\n\
1948 -pedantic Issue all warnings demanded by strict ISO C\n\
1949 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1950 -trigraphs Support ISO C trigraphs\n\
1951 -lang-c Assume that the input sources are in C\n\
1952 -lang-c89 Assume that the input sources are in C89\n\
1955 -lang-c++ Assume that the input sources are in C++\n\
1956 -lang-objc Assume that the input sources are in ObjectiveC\n\
1957 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1958 -lang-asm Assume that the input sources are in assembler\n\
1961 -std=<std name> Specify the conformance standard; one of:\n\
1962 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1963 iso9899:199409, iso9899:1999\n\
1964 -+ Allow parsing of C++ style features\n\
1965 -w Inhibit warning messages\n\
1966 -Wtrigraphs Warn if trigraphs are encountered\n\
1967 -Wno-trigraphs Do not warn about trigraphs\n\
1968 -Wcomment{s} Warn if one comment starts inside another\n\
1971 -Wno-comment{s} Do not warn about comments\n\
1972 -Wtraditional Warn about features not present in traditional C\n\
1973 -Wno-traditional Do not warn about traditional C\n\
1974 -Wundef Warn if an undefined macro is used by #if\n\
1975 -Wno-undef Do not warn about testing undefined macros\n\
1976 -Wimport Warn about the use of the #import directive\n\
1979 -Wno-import Do not warn about the use of #import\n\
1980 -Werror Treat all warnings as errors\n\
1981 -Wno-error Do not treat warnings as errors\n\
1982 -Wsystem-headers Do not suppress warnings from system headers\n\
1983 -Wno-system-headers Suppress warnings from system headers\n\
1984 -Wall Enable all preprocessor warnings\n\
1987 -M Generate make dependencies\n\
1988 -MM As -M, but ignore system header files\n\
1989 -MD Generate make dependencies and compile\n\
1990 -MMD As -MD, but ignore system header files\n\
1991 -MF <file> Write dependency output to the given file\n\
1992 -MG Treat missing header file as generated files\n\
1995 -MP Generate phony targets for all headers\n\
1996 -MQ <target> Add a MAKE-quoted target\n\
1997 -MT <target> Add an unquoted target\n\
2000 -D<macro> Define a <macro> with string '1' as its value\n\
2001 -D<macro>=<val> Define a <macro> with <val> as its value\n\
2002 -A<question>=<answer> Assert the <answer> to <question>\n\
2003 -A-<question>=<answer> Disable the <answer> to <question>\n\
2004 -U<macro> Undefine <macro> \n\
2005 -v Display the version number\n\
2008 -H Print the name of header files as they are used\n\
2009 -C Do not discard comments\n\
2010 -dM Display a list of macro definitions active at end\n\
2011 -dD Preserve macro definitions in output\n\
2012 -dN As -dD except that only the names are preserved\n\
2013 -dI Include #include directives in the output\n\
2016 -fpreprocessed Treat the input file as already preprocessed\n\
2017 -ftabstop=<number> Distance between tab stops for column reporting\n\
2018 -P Do not generate #line directives\n\
2019 -$ Do not allow '$' in identifiers\n\
2020 -remap Remap file names when including files\n\
2021 --version Display version information\n\
2022 -h or --help Display this information\n\