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. */
30 #include "cppdefault.h"
31 #include "except.h" /* for USING_SJLJ_EXCEPTIONS */
33 /* Predefined symbols, built-in macros, and the default include path. */
35 #ifndef GET_ENV_PATH_LIST
36 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
39 /* Windows does not natively support inodes, and neither does MSDOS.
40 Cygwin's emulation can generate non-unique inodes, so don't use it.
41 VMS has non-numeric inodes. */
43 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
44 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
46 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
47 # define INO_T_EQ(A, B) 0
49 # define INO_T_EQ(A, B) ((A) == (B))
51 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
54 /* Internal structures and prototypes. */
56 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
58 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
61 struct pending_option *next;
63 cl_directive_handler handler;
66 /* The `pending' structure accumulates all the options that are not
67 actually processed until we hit cpp_read_main_file. It consists of
68 several lists, one for each type of option. We keep both head and
69 tail pointers for quick insertion. */
72 struct pending_option *directive_head, *directive_tail;
74 struct search_path *quote_head, *quote_tail;
75 struct search_path *brack_head, *brack_tail;
76 struct search_path *systm_head, *systm_tail;
77 struct search_path *after_head, *after_tail;
79 struct pending_option *imacros_head, *imacros_tail;
80 struct pending_option *include_head, *include_tail;
84 #define APPEND(pend, list, elt) \
85 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
86 else (pend)->list##_tail->next = (elt); \
87 (pend)->list##_tail = (elt); \
90 #define APPEND(pend, list, elt) \
91 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
92 else (pend)->list/**/_tail->next = (elt); \
93 (pend)->list/**/_tail = (elt); \
97 static void print_help PARAMS ((void));
98 static void path_include PARAMS ((cpp_reader *,
100 static void init_library PARAMS ((void));
101 static void init_builtins PARAMS ((cpp_reader *));
102 static void mark_named_operators PARAMS ((cpp_reader *));
103 static void append_include_chain PARAMS ((cpp_reader *,
105 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
106 struct search_path *,
107 struct search_path **));
108 static struct search_path * remove_dup_nonsys_dirs PARAMS ((cpp_reader *,
109 struct search_path **,
110 struct search_path *));
111 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
112 struct search_path **));
113 static void merge_include_chains PARAMS ((cpp_reader *));
114 static bool push_include PARAMS ((cpp_reader *,
115 struct pending_option *));
116 static void free_chain PARAMS ((struct pending_option *));
117 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
118 static void init_dependency_output PARAMS ((cpp_reader *));
119 static void init_standard_includes PARAMS ((cpp_reader *));
120 static void read_original_filename PARAMS ((cpp_reader *));
121 static void new_pending_directive PARAMS ((struct cpp_pending *,
123 cl_directive_handler));
124 static void output_deps PARAMS ((cpp_reader *));
125 static int parse_option PARAMS ((const char *));
127 /* Fourth argument to append_include_chain: chain to use.
128 Note it's never asked to append to the quote chain. */
129 enum { BRACKET = 0, SYSTEM, AFTER };
131 /* If we have designated initializers (GCC >2.7) these tables can be
132 initialized, constant data. Otherwise, they have to be filled in at
134 #if HAVE_DESIGNATED_INITIALIZERS
136 #define init_trigraph_map() /* Nothing. */
137 #define TRIGRAPH_MAP \
138 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
141 #define s(p, v) [p] = v,
145 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
146 static void init_trigraph_map PARAMS ((void)) { \
147 unsigned char *x = _cpp_trigraph_map;
150 #define s(p, v) x[p] = v;
155 s('=', '#') s(')', ']') s('!', '|')
156 s('(', '[') s('\'', '^') s('>', '}')
157 s('/', '\\') s('<', '{') s('-', '~')
164 /* Given a colon-separated list of file names PATH,
165 add all the names to the search path for include files. */
167 path_include (pfile, list, path)
178 /* Find the end of this name. */
180 while (*q != 0 && *q != PATH_SEPARATOR) q++;
183 /* An empty name in the path stands for the current directory. */
184 name = (char *) xmalloc (2);
190 /* Otherwise use the directory that is named. */
191 name = (char *) xmalloc (q - p + 1);
192 memcpy (name, p, q - p);
196 append_include_chain (pfile, name, path, path == SYSTEM);
198 /* Advance past this name. */
206 /* Append DIR to include path PATH. DIR must be allocated on the
207 heap; this routine takes responsibility for freeing it. CXX_AWARE
208 is non-zero if the header contains extern "C" guards for C++,
209 otherwise it is zero. */
211 append_include_chain (pfile, dir, path, cxx_aware)
215 int cxx_aware ATTRIBUTE_UNUSED;
217 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
218 struct search_path *new;
227 _cpp_simplify_pathname (dir);
231 /* Dirs that don't exist are silently ignored. */
233 cpp_notice_from_errno (pfile, dir);
234 else if (CPP_OPTION (pfile, verbose))
235 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
240 if (!S_ISDIR (st.st_mode))
242 cpp_notice (pfile, "%s: Not a directory", dir);
248 if (len > pfile->max_include_len)
249 pfile->max_include_len = len;
251 new = (struct search_path *) xmalloc (sizeof (struct search_path));
254 INO_T_COPY (new->ino, st.st_ino);
255 new->dev = st.st_dev;
256 /* Both systm and after include file lists should be treated as system
257 include files since these two lists are really just a concatenation
258 of one "system" list. */
259 if (path == SYSTEM || path == AFTER)
260 #ifdef NO_IMPLICIT_EXTERN_C
263 new->sysp = cxx_aware ? 1 : 2;
267 new->name_map = NULL;
272 case BRACKET: APPEND (pend, brack, new); break;
273 case SYSTEM: APPEND (pend, systm, new); break;
274 case AFTER: APPEND (pend, after, new); break;
278 /* Handle a duplicated include path. PREV is the link in the chain
279 before the duplicate, or NULL if the duplicate is at the head of
280 the chain. The duplicate is removed from the chain and freed.
282 static struct search_path *
283 remove_dup_dir (pfile, prev, head_ptr)
285 struct search_path *prev;
286 struct search_path **head_ptr;
288 struct search_path *cur;
293 prev->next = cur->next;
298 *head_ptr = cur->next;
301 if (CPP_OPTION (pfile, verbose))
302 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
304 free ((PTR) cur->name);
310 /* Remove duplicate non-system directories for which there is an equivalent
311 system directory later in the chain. The range for removal is between
312 *HEAD_PTR and END. Returns the directory before END, or NULL if none.
313 This algorithm is quadratic in the number of system directories, which is
314 acceptable since there aren't usually that many of them. */
315 static struct search_path *
316 remove_dup_nonsys_dirs (pfile, head_ptr, end)
318 struct search_path **head_ptr;
319 struct search_path *end;
322 struct search_path *prev = NULL, *cur, *other;
324 for (cur = *head_ptr; cur; cur = cur->next)
329 for (other = *head_ptr, prev = NULL;
331 other = other ? other->next : *head_ptr)
334 && INO_T_EQ (cur->ino, other->ino)
335 && cur->dev == other->dev)
337 other = remove_dup_dir (pfile, prev, head_ptr);
338 if (CPP_OPTION (pfile, verbose))
340 _(" as it is a non-system directory that duplicates a system directory\n"));
348 for (cur = *head_ptr; cur != end; cur = cur->next)
354 /* Remove duplicate directories from a chain. Returns the tail of the
355 chain, or NULL if the chain is empty. This algorithm is quadratic
356 in the number of -I switches, which is acceptable since there
357 aren't usually that many of them. */
358 static struct search_path *
359 remove_dup_dirs (pfile, head_ptr)
361 struct search_path **head_ptr;
363 struct search_path *prev = NULL, *cur, *other;
365 for (cur = *head_ptr; cur; cur = cur->next)
367 for (other = *head_ptr; other != cur; other = other->next)
368 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
370 cur = remove_dup_dir (pfile, prev, head_ptr);
379 /* Merge the four include chains together in the order quote, bracket,
380 system, after. Remove duplicate dirs (as determined by
381 INO_T_EQ()). The system_include and after_include chains are never
382 referred to again after this function; all access is through the
383 bracket_include path. */
385 merge_include_chains (pfile)
388 struct search_path *quote, *brack, *systm, *qtail;
390 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
392 quote = pend->quote_head;
393 brack = pend->brack_head;
394 systm = pend->systm_head;
395 qtail = pend->quote_tail;
397 /* Paste together bracket, system, and after include chains. */
399 pend->systm_tail->next = pend->after_head;
401 systm = pend->after_head;
404 pend->brack_tail->next = systm;
408 /* This is a bit tricky. First we drop non-system dupes of system
409 directories from the merged bracket-include list. Next we drop
410 dupes from the bracket and quote include lists. Then we drop
411 non-system dupes from the merged quote-include list. Finally,
412 if qtail and brack are the same directory, we cut out brack and
413 move brack up to point to qtail.
415 We can't just merge the lists and then uniquify them because
416 then we may lose directories from the <> search path that should
417 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
418 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
419 -Ibar -I- -Ifoo -Iquux. */
421 remove_dup_nonsys_dirs (pfile, &brack, systm);
422 remove_dup_dirs (pfile, &brack);
426 qtail = remove_dup_dirs (pfile, "e);
429 qtail = remove_dup_nonsys_dirs (pfile, "e, brack);
431 /* If brack == qtail, remove brack as it's simpler. */
432 if (qtail && brack && INO_T_EQ (qtail->ino, brack->ino)
433 && qtail->dev == brack->dev)
434 brack = remove_dup_dir (pfile, qtail, "e);
439 CPP_OPTION (pfile, quote_include) = quote;
440 CPP_OPTION (pfile, bracket_include) = brack;
443 /* A set of booleans indicating what CPP features each source language
450 char extended_numbers;
452 char dollars_in_ident;
453 char cplusplus_comments;
457 /* ??? Enable $ in identifiers in assembly? */
458 static const struct lang_flags lang_defaults[] =
459 { /* c99 objc c++ xnum std dollar c++comm digr */
460 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
461 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
462 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
463 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
464 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
465 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
466 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
467 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
468 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
469 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
472 /* Sets internal flags correctly for a given language. */
474 set_lang (pfile, lang)
478 const struct lang_flags *l = &lang_defaults[(int) lang];
480 CPP_OPTION (pfile, lang) = lang;
482 CPP_OPTION (pfile, c99) = l->c99;
483 CPP_OPTION (pfile, objc) = l->objc;
484 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
485 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
486 CPP_OPTION (pfile, std) = l->std;
487 CPP_OPTION (pfile, trigraphs) = l->std;
488 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
489 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
490 CPP_OPTION (pfile, digraphs) = l->digraphs;
494 static int opt_comp PARAMS ((const void *, const void *));
496 /* Run-time sorting of options array. */
501 return strcmp (((struct cl_option *) p1)->opt_text,
502 ((struct cl_option *) p2)->opt_text);
506 /* init initializes library global state. It might not need to
507 do anything depending on the platform and compiler. */
511 static int initialized = 0;
518 /* For non-ASCII hosts, the cl_options array needs to be sorted at
520 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
523 /* Set up the trigraph map. This doesn't need to do anything if
524 we were compiled with a compiler that supports C99 designated
526 init_trigraph_map ();
530 /* Initialize a cpp_reader structure. */
532 cpp_create_reader (lang)
537 /* Initialise this instance of the library if it hasn't been already. */
540 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
542 set_lang (pfile, lang);
543 CPP_OPTION (pfile, warn_import) = 1;
544 CPP_OPTION (pfile, discard_comments) = 1;
545 CPP_OPTION (pfile, show_column) = 1;
546 CPP_OPTION (pfile, tabstop) = 8;
547 CPP_OPTION (pfile, operator_names) = 1;
548 #if DEFAULT_SIGNED_CHAR
549 CPP_OPTION (pfile, signed_char) = 1;
551 CPP_OPTION (pfile, signed_char) = 0;
553 /* SDCC _asm specific */
554 CPP_OPTION (pfile, preproc_asm) = 1;
556 CPP_OPTION (pfile, pending) =
557 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
559 /* It's simplest to just create this struct whether or not it will
561 pfile->deps = deps_init ();
563 /* Initialise the line map. Start at logical line 1, so we can use
564 a line number of zero for special states. */
565 init_line_maps (&pfile->line_maps);
568 /* Initialize lexer state. */
569 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
571 /* Set up static tokens. */
572 pfile->date.type = CPP_EOF;
573 pfile->avoid_paste.type = CPP_PADDING;
574 pfile->avoid_paste.val.source = NULL;
575 pfile->eof.type = CPP_EOF;
576 pfile->eof.flags = 0;
578 /* Create a token buffer for the lexer. */
579 _cpp_init_tokenrun (&pfile->base_run, 250);
580 pfile->cur_run = &pfile->base_run;
581 pfile->cur_token = pfile->base_run.base;
583 /* Initialise the base context. */
584 pfile->context = &pfile->base_context;
585 pfile->base_context.macro = 0;
586 pfile->base_context.prev = pfile->base_context.next = 0;
588 /* Aligned and unaligned storage. */
589 pfile->a_buff = _cpp_get_buff (pfile, 0);
590 pfile->u_buff = _cpp_get_buff (pfile, 0);
592 /* Initialise the buffer obstack. */
593 gcc_obstack_init (&pfile->buffer_ob);
595 _cpp_init_includes (pfile);
600 /* Free resources used by PFILE. Accessing PFILE after this function
601 returns leads to undefined behaviour. Returns the error count. */
607 struct search_path *dir, *dirn;
608 cpp_context *context, *contextn;
609 tokenrun *run, *runn;
611 while (CPP_BUFFER (pfile) != NULL)
612 _cpp_pop_buffer (pfile);
614 if (pfile->macro_buffer)
616 free ((PTR) pfile->macro_buffer);
617 pfile->macro_buffer = NULL;
618 pfile->macro_buffer_len = 0;
621 deps_free (pfile->deps);
622 obstack_free (&pfile->buffer_ob, 0);
624 _cpp_destroy_hashtable (pfile);
625 _cpp_cleanup_includes (pfile);
627 _cpp_free_buff (pfile->a_buff);
628 _cpp_free_buff (pfile->u_buff);
629 _cpp_free_buff (pfile->free_buffs);
631 for (run = &pfile->base_run; run; run = runn)
635 if (run != &pfile->base_run)
639 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
642 free ((PTR) dir->name);
646 for (context = pfile->base_context.next; context; context = contextn)
648 contextn = context->next;
652 free_line_maps (&pfile->line_maps);
654 result = pfile->errors;
661 /* This structure defines one built-in identifier. A node will be
662 entered in the hash table under the name NAME, with value VALUE (if
663 any). If flags has OPERATOR, the node's operator field is used; if
664 flags has BUILTIN the node's builtin field is used. Macros that are
665 known at build time should not be flagged BUILTIN, as then they do
666 not appear in macro dumps with e.g. -dM or -dD.
668 Two values are not compile time constants, so we tag
669 them in the FLAGS field instead:
670 VERS value is the global version_string, quoted
671 ULP value is the global user_label_prefix */
676 unsigned char builtin;
677 unsigned short flags;
684 #define B(n, t) { U n, 0, t, BUILTIN, sizeof n - 1 }
685 #define C(n, v) { U n, v, 0, 0, sizeof n - 1 }
686 #define X(n, f) { U n, 0, 0, f, sizeof n - 1 }
687 static const struct builtin builtin_array[] =
689 B("__TIME__", BT_TIME),
690 B("__DATE__", BT_DATE),
691 B("__FILE__", BT_FILE),
692 B("__BASE_FILE__", BT_BASE_FILE),
693 B("__LINE__", BT_SPECLINE),
694 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
695 B("_Pragma", BT_PRAGMA),
697 X("__VERSION__", VERS),
698 X("__USER_LABEL_PREFIX__", ULP),
699 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
700 C("__HAVE_BUILTIN_SETJMP__", "1"),
701 #if USING_SJLJ_EXCEPTIONS
702 /* libgcc needs to know this. */
703 C("__USING_SJLJ_EXCEPTIONS__","1"),
705 #ifndef NO_BUILTIN_SIZE_TYPE
706 C("__SIZE_TYPE__", SIZE_TYPE),
708 #ifndef NO_BUILTIN_PTRDIFF_TYPE
709 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
711 #ifndef NO_BUILTIN_WCHAR_TYPE
712 C("__WCHAR_TYPE__", WCHAR_TYPE),
714 #ifndef NO_BUILTIN_WINT_TYPE
715 C("__WINT_TYPE__", WINT_TYPE),
717 #ifdef STDC_0_IN_SYSTEM_HEADERS
718 B("__STDC__", BT_STDC),
726 #define builtin_array_end \
727 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
729 /* Named operators known to the preprocessor. These cannot be
730 #defined and always have their stated meaning. They are treated
731 like normal identifiers except for the type code and the meaning.
732 Most of them are only for C++ (but see iso646.h). */
733 #define B(n, t) { DSC(n), t }
734 static const struct named_op
738 enum cpp_ttype value;
739 } operator_array[] = {
740 B("and", CPP_AND_AND),
741 B("and_eq", CPP_AND_EQ),
742 B("bitand", CPP_AND),
744 B("compl", CPP_COMPL),
746 B("not_eq", CPP_NOT_EQ),
748 B("or_eq", CPP_OR_EQ),
750 B("xor_eq", CPP_XOR_EQ)
754 /* Mark the C++ named operators in the hash table. */
756 mark_named_operators (pfile)
759 const struct named_op *b;
761 for (b = operator_array;
762 b < (operator_array + ARRAY_SIZE (operator_array));
765 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
766 hp->flags |= NODE_OPERATOR;
767 hp->value.operator = b->value;
771 /* Subroutine of cpp_read_main_file; reads the builtins table above and
772 enters them, and language-specific macros, into the hash table. */
774 init_builtins (pfile)
777 const struct builtin *b;
779 for(b = builtin_array; b < builtin_array_end; b++)
781 if (b->flags & BUILTIN)
783 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
785 hp->flags |= NODE_BUILTIN | NODE_WARN;
786 hp->value.builtin = b->builtin;
788 else /* A standard macro of some kind. */
795 /* Allocate enough space for 'name "value"\n\0'. */
796 str = alloca (b->len + strlen (version_string) + 5);
797 sprintf (str, "%s \"%s\"\n", b->name, version_string);
802 val = CPP_OPTION (pfile, user_label_prefix);
806 /* Allocate enough space for "name value\n\0". */
807 str = alloca (b->len + strlen (val) + 3);
808 sprintf(str, "%s %s\n", b->name, val);
811 _cpp_define_builtin (pfile, str);
815 if (CPP_OPTION (pfile, cplusplus))
817 _cpp_define_builtin (pfile, "__cplusplus 1");
818 if (SUPPORTS_ONE_ONLY)
819 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
821 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
823 if (CPP_OPTION (pfile, objc))
824 _cpp_define_builtin (pfile, "__OBJC__ 1");
826 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
827 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
828 else if (CPP_OPTION (pfile, c99))
829 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
831 if (CPP_OPTION (pfile, signed_char) == 0)
832 _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
834 if (CPP_OPTION (pfile, lang) == CLK_STDC89
835 || CPP_OPTION (pfile, lang) == CLK_STDC94
836 || CPP_OPTION (pfile, lang) == CLK_STDC99)
837 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
838 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
839 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
845 #undef builtin_array_end
847 /* And another subroutine. This one sets up the standard include path. */
849 init_standard_includes (pfile)
853 const struct default_include *p;
854 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
856 /* Several environment variables may add to the include search path.
857 CPATH specifies an additional list of directories to be searched
858 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
859 etc. specify an additional list of directories to be searched as
860 if specified with -isystem, for the language indicated. */
862 GET_ENV_PATH_LIST (path, "CPATH");
863 if (path != 0 && *path != 0)
864 path_include (pfile, path, BRACKET);
866 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
869 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
872 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
875 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
878 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
881 if (path != 0 && *path != 0)
882 path_include (pfile, path, SYSTEM);
884 /* Search "translated" versions of GNU directories.
885 These have /usr/local/lib/gcc... replaced by specd_prefix. */
886 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
888 /* Remove the `include' from /usr/local/lib/gcc.../include.
889 GCC_INCLUDE_DIR will always end in /include. */
890 int default_len = cpp_GCC_INCLUDE_DIR_len;
891 char *default_prefix = (char *) alloca (default_len + 1);
892 int specd_len = strlen (specd_prefix);
894 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
895 default_prefix[default_len] = '\0';
897 for (p = cpp_include_defaults; p->fname; p++)
899 /* Some standard dirs are only for C++. */
901 || (CPP_OPTION (pfile, cplusplus)
902 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
904 /* Does this dir start with the prefix? */
905 if (!strncmp (p->fname, default_prefix, default_len))
907 /* Yes; change prefix and add to search list. */
908 int flen = strlen (p->fname);
909 int this_len = specd_len + flen - default_len;
910 char *str = (char *) xmalloc (this_len + 1);
911 memcpy (str, specd_prefix, specd_len);
912 memcpy (str + specd_len,
913 p->fname + default_len,
914 flen - default_len + 1);
916 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
922 /* Search ordinary names for GNU include directories. */
923 for (p = cpp_include_defaults; p->fname; p++)
925 /* Some standard dirs are only for C++. */
927 || (CPP_OPTION (pfile, cplusplus)
928 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
930 char *str = update_path (p->fname, p->component);
931 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
936 /* Pushes a command line -imacro and -include file indicated by P onto
937 the buffer stack. Returns non-zero if successful. */
939 push_include (pfile, p)
941 struct pending_option *p;
945 /* Later: maybe update this to use the #include "" search path
946 if cpp_read_file fails. */
947 header.type = CPP_STRING;
948 header.val.str.text = (const unsigned char *) p->arg;
949 header.val.str.len = strlen (p->arg);
950 /* Make the command line directive take up a line. */
953 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
956 /* Frees a pending_option chain. */
959 struct pending_option *head;
961 struct pending_option *next;
971 /* This is called after options have been parsed, and partially
972 processed. Setup for processing input from the file named FNAME,
973 or stdin if it is the empty string. Return the original filename
974 on success (e.g. foo.i->foo.c), or NULL on failure. */
976 cpp_read_main_file (pfile, fname, table)
981 /* The front ends don't set up the hash table until they have
982 finished processing the command line options, so initializing the
983 hashtable is deferred until now. */
984 _cpp_init_hashtable (pfile, table);
986 /* Set up the include search path now. */
987 if (! CPP_OPTION (pfile, no_standard_includes))
988 init_standard_includes (pfile);
990 merge_include_chains (pfile);
992 /* With -v, print the list of dirs to search. */
993 if (CPP_OPTION (pfile, verbose))
995 struct search_path *l;
996 fprintf (stderr, _("#include \"...\" search starts here:\n"));
997 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
999 if (l == CPP_OPTION (pfile, bracket_include))
1000 fprintf (stderr, _("#include <...> search starts here:\n"));
1001 fprintf (stderr, " %s\n", l->name);
1003 fprintf (stderr, _("End of search list.\n"));
1006 if (CPP_OPTION (pfile, print_deps))
1007 /* Set the default target (if there is none already). */
1008 deps_add_default_target (pfile, fname);
1010 /* Open the main input file. */
1011 if (!_cpp_read_file (pfile, fname))
1014 /* Set this after cpp_post_options so the client can change the
1015 option if it wishes, and after stacking the main file so we don't
1016 trace the main file. */
1017 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
1019 /* For foo.i, read the original filename foo.c now, for the benefit
1020 of the front ends. */
1021 if (CPP_OPTION (pfile, preprocessed))
1022 read_original_filename (pfile);
1024 return pfile->map->to_file;
1027 /* For preprocessed files, if the first tokens are of the form # NUM.
1028 handle the directive so we know the original file name. This will
1029 generate file_change callbacks, which the front ends must handle
1030 appropriately given their state of initialization. */
1032 read_original_filename (pfile)
1035 const cpp_token *token, *token1;
1037 /* Lex ahead; if the first tokens are of the form # NUM, then
1038 process the directive, otherwise back up. */
1039 token = _cpp_lex_direct (pfile);
1040 if (token->type == CPP_HASH)
1042 token1 = _cpp_lex_direct (pfile);
1043 _cpp_backup_tokens (pfile, 1);
1045 /* If it's a #line directive, handle it. */
1046 if (token1->type == CPP_NUMBER)
1048 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
1053 /* Backup as if nothing happened. */
1054 _cpp_backup_tokens (pfile, 1);
1057 /* Handle pending command line options: -D, -U, -A, -imacros and
1058 -include. This should be called after debugging has been properly
1059 set up in the front ends. */
1061 cpp_finish_options (pfile)
1064 /* Mark named operators before handling command line macros. */
1065 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
1066 mark_named_operators (pfile);
1068 /* Install builtins and process command line macros etc. in the order
1069 they appeared, but only if not already preprocessed. */
1070 if (! CPP_OPTION (pfile, preprocessed))
1072 struct pending_option *p;
1074 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1075 init_builtins (pfile);
1076 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1077 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1078 (*p->handler) (pfile, p->arg);
1080 /* Scan -imacros files after command line defines, but before
1081 files given with -include. */
1082 while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
1084 if (push_include (pfile, p))
1086 pfile->buffer->return_at_eof = true;
1087 cpp_scan_nooutput (pfile);
1089 CPP_OPTION (pfile, pending)->imacros_head = p->next;
1094 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1095 _cpp_push_next_buffer (pfile);
1098 /* Called to push the next buffer on the stack given by -include. If
1099 there are none, free the pending structure and restore the line map
1100 for the main file. */
1102 _cpp_push_next_buffer (pfile)
1105 bool pushed = false;
1107 /* This is't pretty; we'd rather not be relying on this as a boolean
1108 for reverting the line map. Further, we only free the chains in
1109 this conditional, so an early call to cpp_finish / cpp_destroy
1110 will leak that memory. */
1111 if (CPP_OPTION (pfile, pending)
1112 && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1116 struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1120 if (! CPP_OPTION (pfile, preprocessed))
1121 pushed = push_include (pfile, p);
1122 CPP_OPTION (pfile, pending)->include_head = p->next;
1128 free (CPP_OPTION (pfile, pending));
1129 CPP_OPTION (pfile, pending) = NULL;
1131 /* Restore the line map for the main file. */
1132 if (! CPP_OPTION (pfile, preprocessed))
1133 _cpp_do_file_change (pfile, LC_RENAME,
1134 pfile->line_maps.maps[0].to_file, 1, 0);
1141 /* Use mkdeps.c to output dependency information. */
1146 /* Stream on which to print the dependency information. */
1147 FILE *deps_stream = 0;
1148 const char *const deps_mode =
1149 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1151 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1152 deps_stream = stdout;
1155 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1156 if (deps_stream == 0)
1158 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1163 deps_write (pfile->deps, deps_stream, 72);
1165 if (CPP_OPTION (pfile, deps_phony_targets))
1166 deps_phony_targets (pfile->deps, deps_stream);
1168 /* Don't close stdout. */
1169 if (deps_stream != stdout)
1171 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1172 cpp_fatal (pfile, "I/O error on output");
1176 /* This is called at the end of preprocessing. It pops the
1177 last buffer and writes dependency output. It should also
1178 clear macro definitions, such that you could call cpp_start_read
1179 with a new filename to restart processing. */
1184 /* cpplex.c leaves the final buffer on the stack. This it so that
1185 it returns an unending stream of CPP_EOFs to the client. If we
1186 popped the buffer, we'd dereference a NULL buffer pointer and
1187 segfault. It's nice to allow the client to do worry-free excess
1188 cpp_get_token calls. */
1189 while (pfile->buffer)
1190 _cpp_pop_buffer (pfile);
1192 /* Don't write the deps file if preprocessing has failed. */
1193 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1194 output_deps (pfile);
1196 /* Report on headers that could use multiple include guards. */
1197 if (CPP_OPTION (pfile, print_include_names))
1198 _cpp_report_missing_guards (pfile);
1201 /* Add a directive to be handled later in the initialization phase. */
1203 new_pending_directive (pend, text, handler)
1204 struct cpp_pending *pend;
1206 cl_directive_handler handler;
1208 struct pending_option *o = (struct pending_option *)
1209 xmalloc (sizeof (struct pending_option));
1213 o->handler = handler;
1214 APPEND (pend, directive, o);
1217 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1218 I.e. a const string initializer with parens around it. That is
1219 what N_("string") resolves to, so we make no_* be macros instead. */
1220 #define no_arg N_("argument missing after %s")
1221 #define no_ass N_("assertion missing after %s")
1222 #define no_dir N_("directory name missing after %s")
1223 #define no_fil N_("file name missing after %s")
1224 #define no_mac N_("macro name missing after %s")
1225 #define no_pth N_("path name missing after %s")
1226 #define no_num N_("number missing after %s")
1227 #define no_tgt N_("target missing after %s")
1229 /* This is the list of all command line options, with the leading
1230 "-" removed. It must be sorted in ASCII collating order. */
1231 #define COMMAND_LINE_OPTIONS \
1232 DEF_OPT("$", 0, OPT_dollar) \
1233 DEF_OPT("+", 0, OPT_plus) \
1234 DEF_OPT("-help", 0, OPT__help) \
1235 DEF_OPT("-target-help", 0, OPT_target__help) \
1236 DEF_OPT("-version", 0, OPT__version) \
1237 DEF_OPT("A", no_ass, OPT_A) \
1238 DEF_OPT("C", 0, OPT_C) \
1239 DEF_OPT("D", no_mac, OPT_D) \
1240 DEF_OPT("H", 0, OPT_H) \
1241 DEF_OPT("I", no_dir, OPT_I) \
1242 DEF_OPT("M", 0, OPT_M) \
1243 DEF_OPT("MD", no_fil, OPT_MD) \
1244 DEF_OPT("MF", no_fil, OPT_MF) \
1245 DEF_OPT("MG", 0, OPT_MG) \
1246 DEF_OPT("MM", 0, OPT_MM) \
1247 DEF_OPT("MMD", no_fil, OPT_MMD) \
1248 DEF_OPT("MP", 0, OPT_MP) \
1249 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1250 DEF_OPT("MT", no_tgt, OPT_MT) \
1251 DEF_OPT("P", 0, OPT_P) \
1252 DEF_OPT("U", no_mac, OPT_U) \
1253 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1254 DEF_OPT("d", no_arg, OPT_d) \
1255 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1256 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1257 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1258 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1259 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1260 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1261 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1262 DEF_OPT("fsigned-char", 0, OPT_fsigned_char) \
1263 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1264 DEF_OPT("funsigned-char", 0, OPT_funsigned_char) \
1265 DEF_OPT("h", 0, OPT_h) \
1266 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1267 DEF_OPT("imacros", no_fil, OPT_imacros) \
1268 DEF_OPT("include", no_fil, OPT_include) \
1269 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1270 DEF_OPT("isystem", no_dir, OPT_isystem) \
1271 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1272 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1273 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1274 DEF_OPT("lang-c", 0, OPT_lang_c) \
1275 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1276 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1277 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1278 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1279 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1280 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1281 DEF_OPT("o", no_fil, OPT_o) \
1282 /* SDCC specific */ \
1283 DEF_OPT("obj-ext=", no_arg, OPT_obj_ext) \
1284 DEF_OPT("pedantic", 0, OPT_pedantic) \
1285 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1286 DEF_OPT("remap", 0, OPT_remap) \
1287 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1288 DEF_OPT("std=c89", 0, OPT_std_c89) \
1289 DEF_OPT("std=c99", 0, OPT_std_c99) \
1290 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1291 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1292 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1293 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1294 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1295 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1296 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1297 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1298 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1299 DEF_OPT("v", 0, OPT_v) \
1300 DEF_OPT("version", 0, OPT_version) \
1301 DEF_OPT("w", 0, OPT_w)
1303 #define DEF_OPT(text, msg, code) code,
1306 COMMAND_LINE_OPTIONS
1313 const char *opt_text;
1316 enum opt_code opt_code;
1319 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1321 static struct cl_option cl_options[] =
1323 static const struct cl_option cl_options[] =
1326 COMMAND_LINE_OPTIONS
1329 #undef COMMAND_LINE_OPTIONS
1331 /* Perform a binary search to find which, if any, option the given
1332 command-line matches. Returns its index in the option array,
1333 negative on failure. Complications arise since some options can be
1334 suffixed with an argument, and multiple complete matches can occur,
1335 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1336 accept options beginning with -W that we do not recognise, but not
1337 to swallow any subsequent command line argument; this is handled as
1338 special cases in cpp_handle_option. */
1340 parse_option (input)
1343 unsigned int md, mn, mx;
1354 opt_len = cl_options[md].opt_len;
1355 comp = strncmp (input, cl_options[md].opt_text, opt_len);
1363 if (input[opt_len] == '\0')
1365 /* We were passed more text. If the option takes an argument,
1366 we may match a later option or we may have been passed the
1367 argument. The longest possible option match succeeds.
1368 If the option takes no arguments we have not matched and
1369 continue the search (e.g. input="stdc++" match was "stdc"). */
1371 if (cl_options[md].msg)
1373 /* Scan forwards. If we get an exact match, return it.
1374 Otherwise, return the longest option-accepting match.
1375 This loops no more than twice with current options. */
1377 for (; mn < (unsigned int) N_OPTS; mn++)
1379 opt_len = cl_options[mn].opt_len;
1380 if (strncmp (input, cl_options[mn].opt_text, opt_len))
1382 if (input[opt_len] == '\0')
1384 if (cl_options[mn].msg)
1395 /* Handle one command-line option in (argc, argv).
1396 Can be called multiple times, to handle multiple sets of options.
1397 If ignore is non-zero, this will ignore unrecognized -W* options.
1398 Returns number of strings consumed. */
1400 cpp_handle_option (pfile, argc, argv, ignore)
1407 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1409 /* Interpret "-" or a non-option as a file name. */
1410 if (argv[i][0] != '-' || argv[i][1] == '\0')
1412 if (CPP_OPTION (pfile, in_fname) == NULL)
1413 CPP_OPTION (pfile, in_fname) = argv[i];
1414 else if (CPP_OPTION (pfile, out_fname) == NULL)
1415 CPP_OPTION (pfile, out_fname) = argv[i];
1417 cpp_fatal (pfile, "too many filenames. Type %s --help for usage info",
1422 enum opt_code opt_code;
1424 const char *arg = 0;
1426 /* Skip over '-'. */
1427 opt_index = parse_option (&argv[i][1]);
1431 opt_code = cl_options[opt_index].opt_code;
1432 if (cl_options[opt_index].msg)
1434 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1436 /* Yuk. Special case for -W as it must not swallow
1437 up any following argument. If this becomes common, add
1438 another field to the cl_options table. */
1439 if (arg[0] == '\0' && opt_code != OPT_W)
1444 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1452 case N_OPTS: /* Shut GCC up. */
1454 case OPT_fleading_underscore:
1455 CPP_OPTION (pfile, user_label_prefix) = "_";
1457 case OPT_fno_leading_underscore:
1458 CPP_OPTION (pfile, user_label_prefix) = "";
1460 case OPT_fno_operator_names:
1461 CPP_OPTION (pfile, operator_names) = 0;
1463 case OPT_fpreprocessed:
1464 CPP_OPTION (pfile, preprocessed) = 1;
1466 case OPT_fno_preprocessed:
1467 CPP_OPTION (pfile, preprocessed) = 0;
1469 case OPT_fshow_column:
1470 CPP_OPTION (pfile, show_column) = 1;
1472 case OPT_fno_show_column:
1473 CPP_OPTION (pfile, show_column) = 0;
1475 case OPT_fsigned_char:
1476 CPP_OPTION (pfile, signed_char) = 1;
1478 case OPT_funsigned_char:
1479 CPP_OPTION (pfile, signed_char) = 0;
1482 /* Silently ignore empty string, non-longs and silly values. */
1486 long tabstop = strtol (arg, &endptr, 10);
1487 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1488 CPP_OPTION (pfile, tabstop) = tabstop;
1492 CPP_OPTION (pfile, inhibit_warnings) = 1;
1497 CPP_OPTION (pfile, help_only) = 1;
1499 case OPT_target__help:
1500 /* Print if any target specific options. cpplib has none, but
1501 make sure help_only gets set. */
1502 CPP_OPTION (pfile, help_only) = 1;
1505 /* --version inhibits compilation, -version doesn't. -v means
1506 verbose and -version. Historical reasons, don't ask. */
1508 CPP_OPTION (pfile, help_only) = 1;
1509 pfile->print_version = 1;
1512 CPP_OPTION (pfile, verbose) = 1;
1513 pfile->print_version = 1;
1516 pfile->print_version = 1;
1520 CPP_OPTION (pfile, discard_comments) = 0;
1523 CPP_OPTION (pfile, no_line_commands) = 1;
1525 case OPT_dollar: /* Don't include $ in identifiers. */
1526 CPP_OPTION (pfile, dollars_in_ident) = 0;
1529 CPP_OPTION (pfile, print_include_names) = 1;
1532 new_pending_directive (pend, arg, cpp_define);
1534 case OPT_pedantic_errors:
1535 CPP_OPTION (pfile, pedantic_errors) = 1;
1538 CPP_OPTION (pfile, pedantic) = 1;
1541 CPP_OPTION (pfile, trigraphs) = 1;
1544 CPP_OPTION (pfile, cplusplus) = 1;
1545 CPP_OPTION (pfile, cplusplus_comments) = 1;
1548 CPP_OPTION (pfile, remap) = 1;
1551 CPP_OPTION (pfile, include_prefix) = arg;
1552 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1555 set_lang (pfile, CLK_GNUC89);
1557 case OPT_lang_cplusplus:
1558 set_lang (pfile, CLK_GNUCXX);
1561 set_lang (pfile, CLK_OBJC);
1563 case OPT_lang_objcplusplus:
1564 set_lang (pfile, CLK_OBJCXX);
1567 set_lang (pfile, CLK_ASM);
1569 case OPT_std_cplusplus98:
1570 set_lang (pfile, CLK_CXX98);
1573 set_lang (pfile, CLK_GNUC89);
1577 set_lang (pfile, CLK_GNUC99);
1579 case OPT_std_iso9899_199409:
1580 set_lang (pfile, CLK_STDC94);
1582 case OPT_std_iso9899_1990:
1585 set_lang (pfile, CLK_STDC89);
1587 case OPT_std_iso9899_199x:
1588 case OPT_std_iso9899_1999:
1591 set_lang (pfile, CLK_STDC99);
1594 /* -nostdinc causes no default include directories.
1595 You must specify all include-file directories with -I. */
1596 CPP_OPTION (pfile, no_standard_includes) = 1;
1598 case OPT_nostdincplusplus:
1599 /* -nostdinc++ causes no default C++-specific include directories. */
1600 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1603 if (CPP_OPTION (pfile, out_fname) == NULL)
1604 CPP_OPTION (pfile, out_fname) = arg;
1607 cpp_fatal (pfile, "output filename specified twice");
1612 /* Args to -d specify what parts of macros to dump.
1613 Silently ignore unrecognised options; they may
1614 be aimed at the compiler proper. */
1618 while ((c = *arg++) != '\0')
1622 CPP_OPTION (pfile, dump_macros) = dump_only;
1625 CPP_OPTION (pfile, dump_macros) = dump_names;
1628 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1631 CPP_OPTION (pfile, dump_includes) = 1;
1638 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1641 /* When doing dependencies with -M or -MM, suppress normal
1642 preprocessed output, but still do -dM etc. as software
1643 depends on this. Preprocessed output occurs if -MD, -MMD
1644 or environment var dependency generation is used. */
1645 CPP_OPTION (pfile, print_deps) = 2;
1646 CPP_OPTION (pfile, no_output) = 1;
1649 CPP_OPTION (pfile, print_deps) = 1;
1650 CPP_OPTION (pfile, no_output) = 1;
1653 CPP_OPTION (pfile, deps_file) = arg;
1656 CPP_OPTION (pfile, deps_phony_targets) = 1;
1660 /* Add a target. -MQ quotes for Make. */
1661 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1665 CPP_OPTION (pfile, print_deps) = 2;
1666 CPP_OPTION (pfile, deps_file) = arg;
1669 CPP_OPTION (pfile, print_deps) = 1;
1670 CPP_OPTION (pfile, deps_file) = arg;
1676 /* -A with an argument beginning with '-' acts as
1677 #unassert on whatever immediately follows the '-'.
1678 If "-" is the whole argument, we eliminate all
1679 predefined macros and assertions, including those
1680 that were specified earlier on the command line.
1681 That way we can get rid of any that were passed
1682 automatically in from GCC. */
1686 free_chain (pend->directive_head);
1687 pend->directive_head = NULL;
1688 pend->directive_tail = NULL;
1691 new_pending_directive (pend, arg + 1, cpp_unassert);
1694 new_pending_directive (pend, arg, cpp_assert);
1697 new_pending_directive (pend, arg, cpp_undef);
1699 case OPT_I: /* Add directory to path for includes. */
1700 if (!strcmp (arg, "-"))
1703 Use the preceding -I directories for #include "..."
1704 but not #include <...>.
1705 Don't search the directory of the present file
1706 for #include "...". (Note that -I. -I- is not the same as
1707 the default setup; -I. uses the compiler's working dir.) */
1708 if (! CPP_OPTION (pfile, ignore_srcdir))
1710 pend->quote_head = pend->brack_head;
1711 pend->quote_tail = pend->brack_tail;
1712 pend->brack_head = 0;
1713 pend->brack_tail = 0;
1714 CPP_OPTION (pfile, ignore_srcdir) = 1;
1718 cpp_fatal (pfile, "-I- specified twice");
1723 append_include_chain (pfile, (char *)xstrdup(arg), BRACKET, 0);
1726 /* Add directory to beginning of system include path, as a system
1727 include directory. */
1728 append_include_chain (pfile, (char *)xstrdup(arg), SYSTEM, 0);
1733 struct pending_option *o = (struct pending_option *)
1734 xmalloc (sizeof (struct pending_option));
1738 if (opt_code == OPT_include)
1739 APPEND (pend, include, o);
1741 APPEND (pend, imacros, o);
1744 case OPT_iwithprefix:
1745 /* Add directory to end of path for includes,
1746 with the default prefix at the front of its name. */
1748 case OPT_iwithprefixbefore:
1749 /* Add directory to main path for includes,
1750 with the default prefix at the front of its name. */
1757 if (CPP_OPTION (pfile, include_prefix) != 0)
1759 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1760 fname = xmalloc (ipl + len + 1);
1761 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1762 memcpy (fname + ipl, arg, len + 1);
1764 else if (cpp_GCC_INCLUDE_DIR_len)
1766 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1767 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1768 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1771 fname = xstrdup (arg);
1773 append_include_chain (pfile, fname,
1774 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1778 /* Add directory to end of path for includes. */
1779 append_include_chain (pfile, (char *)xstrdup (arg), AFTER, 0);
1782 /* Silently ignore unrecognised options. */
1783 if (!strcmp (argv[i], "-Wall"))
1785 CPP_OPTION (pfile, warn_trigraphs) = 1;
1786 CPP_OPTION (pfile, warn_comments) = 1;
1788 else if (!strcmp (argv[i], "-Wtraditional"))
1789 CPP_OPTION (pfile, warn_traditional) = 1;
1790 else if (!strcmp (argv[i], "-Wtrigraphs"))
1791 CPP_OPTION (pfile, warn_trigraphs) = 1;
1792 else if (!strcmp (argv[i], "-Wcomment"))
1793 CPP_OPTION (pfile, warn_comments) = 1;
1794 else if (!strcmp (argv[i], "-Wcomments"))
1795 CPP_OPTION (pfile, warn_comments) = 1;
1796 else if (!strcmp (argv[i], "-Wundef"))
1797 CPP_OPTION (pfile, warn_undef) = 1;
1798 else if (!strcmp (argv[i], "-Wimport"))
1799 CPP_OPTION (pfile, warn_import) = 1;
1800 else if (!strcmp (argv[i], "-Werror"))
1801 CPP_OPTION (pfile, warnings_are_errors) = 1;
1802 else if (!strcmp (argv[i], "-Wsystem-headers"))
1803 CPP_OPTION (pfile, warn_system_headers) = 1;
1804 else if (!strcmp (argv[i], "-Wno-traditional"))
1805 CPP_OPTION (pfile, warn_traditional) = 0;
1806 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1807 CPP_OPTION (pfile, warn_trigraphs) = 0;
1808 else if (!strcmp (argv[i], "-Wno-comment"))
1809 CPP_OPTION (pfile, warn_comments) = 0;
1810 else if (!strcmp (argv[i], "-Wno-comments"))
1811 CPP_OPTION (pfile, warn_comments) = 0;
1812 else if (!strcmp (argv[i], "-Wno-undef"))
1813 CPP_OPTION (pfile, warn_undef) = 0;
1814 else if (!strcmp (argv[i], "-Wno-import"))
1815 CPP_OPTION (pfile, warn_import) = 0;
1816 else if (!strcmp (argv[i], "-Wno-error"))
1817 CPP_OPTION (pfile, warnings_are_errors) = 0;
1818 else if (!strcmp (argv[i], "-Wno-system-headers"))
1819 CPP_OPTION (pfile, warn_system_headers) = 0;
1825 CPP_OPTION (pfile, obj_ext) = arg;
1832 /* Handle command-line options in (argc, argv).
1833 Can be called multiple times, to handle multiple sets of options.
1834 Returns if an unrecognized option is seen.
1835 Returns number of strings consumed. */
1837 cpp_handle_options (pfile, argc, argv)
1843 int strings_processed;
1845 for (i = 0; i < argc; i += strings_processed)
1847 strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
1848 if (strings_processed == 0)
1855 /* Extra processing when all options are parsed, after all calls to
1856 cpp_handle_option[s]. Consistency checks etc. */
1858 cpp_post_options (pfile)
1861 if (pfile->print_version)
1863 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1864 #ifdef TARGET_VERSION
1867 fputc ('\n', stderr);
1870 /* Canonicalize in_fname and out_fname. We guarantee they are not
1871 NULL, and that the empty string represents stdin / stdout. */
1872 if (CPP_OPTION (pfile, in_fname) == NULL
1873 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1874 CPP_OPTION (pfile, in_fname) = "";
1876 if (CPP_OPTION (pfile, out_fname) == NULL
1877 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1878 CPP_OPTION (pfile, out_fname) = "";
1880 /* -Wtraditional is not useful in C++ mode. */
1881 if (CPP_OPTION (pfile, cplusplus))
1882 CPP_OPTION (pfile, warn_traditional) = 0;
1884 /* Set this if it hasn't been set already. */
1885 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1886 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1888 /* Permanently disable macro expansion if we are rescanning
1889 preprocessed text. */
1890 if (CPP_OPTION (pfile, preprocessed))
1891 pfile->state.prevent_expansion = 1;
1893 /* -dM makes no normal output. This is set here so that -dM -dD
1894 works as expected. */
1895 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1896 CPP_OPTION (pfile, no_output) = 1;
1898 /* Disable -dD, -dN and -dI if we should make no normal output
1899 (such as with -M). Allow -M -dM since some software relies on
1901 if (CPP_OPTION (pfile, no_output))
1903 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1904 CPP_OPTION (pfile, dump_macros) = dump_none;
1905 CPP_OPTION (pfile, dump_includes) = 0;
1908 /* We need to do this after option processing and before
1909 cpp_start_read, as cppmain.c relies on the options->no_output to
1910 set its callbacks correctly before calling cpp_start_read. */
1911 init_dependency_output (pfile);
1913 /* After checking the environment variables, check if -M or -MM has
1914 not been specified, but other -M options have. */
1915 if (CPP_OPTION (pfile, print_deps) == 0 &&
1916 (CPP_OPTION (pfile, print_deps_missing_files)
1917 || CPP_OPTION (pfile, deps_file)
1918 || CPP_OPTION (pfile, deps_phony_targets)))
1919 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1922 /* Set up dependency-file output. On exit, if print_deps is non-zero
1923 then deps_file is not NULL; stdout is the empty string. */
1925 init_dependency_output (pfile)
1928 char *spec, *s, *output_file;
1930 /* Either of two environment variables can specify output of deps.
1931 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1932 where OUTPUT_FILE is the file to write deps info to
1933 and DEPS_TARGET is the target to mention in the deps. */
1935 if (CPP_OPTION (pfile, print_deps) == 0)
1937 spec = getenv ("DEPENDENCIES_OUTPUT");
1939 CPP_OPTION (pfile, print_deps) = 1;
1942 spec = getenv ("SUNPRO_DEPENDENCIES");
1945 CPP_OPTION (pfile, print_deps) = 2;
1946 CPP_OPTION (pfile, deps_ignore_main_file) = 1;
1952 /* Find the space before the DEPS_TARGET, if there is one. */
1953 s = strchr (spec, ' ');
1956 /* Let the caller perform MAKE quoting. */
1957 deps_add_target (pfile->deps, s + 1, 0);
1958 output_file = (char *) xmalloc (s - spec + 1);
1959 memcpy (output_file, spec, s - spec);
1960 output_file[s - spec] = 0;
1965 /* Command line -MF overrides environment variables and default. */
1966 if (CPP_OPTION (pfile, deps_file) == 0)
1967 CPP_OPTION (pfile, deps_file) = output_file;
1969 CPP_OPTION (pfile, print_deps_append) = 1;
1971 else if (CPP_OPTION (pfile, deps_file) == 0)
1972 /* If -M or -MM was seen without -MF, default output to wherever
1973 was specified with -o. out_fname is non-NULL here. */
1974 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1977 /* Handle --help output. */
1981 /* To keep the lines from getting too long for some compilers, limit
1982 to about 500 characters (6 lines) per chunk. */
1985 -include <file> Include the contents of <file> before other files\n\
1986 -imacros <file> Accept definition of macros in <file>\n\
1987 -iprefix <path> Specify <path> as a prefix for next two options\n\
1988 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1989 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1990 -isystem <dir> Add <dir> to the start of the system include path\n\
1993 -idirafter <dir> Add <dir> to the end of the system include path\n\
1994 -I <dir> Add <dir> to the end of the main include path\n\
1995 -I- Fine-grained include path control; see info docs\n\
1996 -nostdinc Do not search system include directories\n\
1997 (dirs specified with -isystem will still be used)\n\
1998 -nostdinc++ Do not search system include directories for C++\n\
1999 -o <file> Put output into <file>\n\
2002 -pedantic Issue all warnings demanded by strict ISO C\n\
2003 -pedantic-errors Issue -pedantic warnings as errors instead\n\
2004 -trigraphs Support ISO C trigraphs\n\
2005 -lang-c Assume that the input sources are in C\n\
2006 -lang-c89 Assume that the input sources are in C89\n\
2009 -lang-c++ Assume that the input sources are in C++\n\
2010 -lang-objc Assume that the input sources are in ObjectiveC\n\
2011 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
2012 -lang-asm Assume that the input sources are in assembler\n\
2015 -std=<std name> Specify the conformance standard; one of:\n\
2016 gnu89, gnu99, c89, c99, iso9899:1990,\n\
2017 iso9899:199409, iso9899:1999\n\
2018 -+ Allow parsing of C++ style features\n\
2019 -w Inhibit warning messages\n\
2020 -Wtrigraphs Warn if trigraphs are encountered\n\
2021 -Wno-trigraphs Do not warn about trigraphs\n\
2022 -Wcomment{s} Warn if one comment starts inside another\n\
2025 -Wno-comment{s} Do not warn about comments\n\
2026 -Wtraditional Warn about features not present in traditional C\n\
2027 -Wno-traditional Do not warn about traditional C\n\
2028 -Wundef Warn if an undefined macro is used by #if\n\
2029 -Wno-undef Do not warn about testing undefined macros\n\
2030 -Wimport Warn about the use of the #import directive\n\
2033 -Wno-import Do not warn about the use of #import\n\
2034 -Werror Treat all warnings as errors\n\
2035 -Wno-error Do not treat warnings as errors\n\
2036 -Wsystem-headers Do not suppress warnings from system headers\n\
2037 -Wno-system-headers Suppress warnings from system headers\n\
2038 -Wall Enable all preprocessor warnings\n\
2041 -M Generate make dependencies\n\
2042 -MM As -M, but ignore system header files\n\
2043 -MD Generate make dependencies and compile\n\
2044 -MMD As -MD, but ignore system header files\n\
2045 -MF <file> Write dependency output to the given file\n\
2046 -MG Treat missing header file as generated files\n\
2049 -MP Generate phony targets for all headers\n\
2050 -MQ <target> Add a MAKE-quoted target\n\
2051 -MT <target> Add an unquoted target\n\
2055 -obj-ext=<extension> Define object file extension, used for generation\n\
2056 of make dependencies\n\
2059 -D<macro> Define a <macro> with string '1' as its value\n\
2060 -D<macro>=<val> Define a <macro> with <val> as its value\n\
2061 -A<question>=<answer> Assert the <answer> to <question>\n\
2062 -A-<question>=<answer> Disable the <answer> to <question>\n\
2063 -U<macro> Undefine <macro> \n\
2064 -v Display the version number\n\
2067 -H Print the name of header files as they are used\n\
2068 -C Do not discard comments\n\
2069 -dM Display a list of macro definitions active at end\n\
2070 -dD Preserve macro definitions in output\n\
2071 -dN As -dD except that only the names are preserved\n\
2072 -dI Include #include directives in the output\n\
2075 -fpreprocessed Treat the input file as already preprocessed\n\
2076 -ftabstop=<number> Distance between tab stops for column reporting\n\
2077 -P Do not generate #line directives\n\
2078 -$ Do not allow '$' in identifiers\n\
2079 -remap Remap file names when including files\n\
2080 --version Display version information\n\
2081 -h or --help Display this information\n\