2 Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 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. */
31 #include "cppdefault.h"
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)))
45 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
46 # define INO_T_EQ(a, b) 0
48 # define INO_T_EQ(a, b) ((a) == (b))
52 /* Internal structures and prototypes. */
54 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
57 typedef void (* cl_directive_handler) PARAMS ((cpp_reader *, const char *));
60 struct pending_option *next;
62 cl_directive_handler handler;
65 /* The `pending' structure accumulates all the options that are not
66 actually processed until we hit cpp_start_read. It consists of
67 several lists, one for each type of option. We keep both head and
68 tail pointers for quick insertion. */
71 struct pending_option *directive_head, *directive_tail;
73 struct search_path *quote_head, *quote_tail;
74 struct search_path *brack_head, *brack_tail;
75 struct search_path *systm_head, *systm_tail;
76 struct search_path *after_head, *after_tail;
78 struct pending_option *imacros_head, *imacros_tail;
79 struct pending_option *include_head, *include_tail;
83 #define APPEND(pend, list, elt) \
84 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
85 else (pend)->list##_tail->next = (elt); \
86 (pend)->list##_tail = (elt); \
89 #define APPEND(pend, list, elt) \
90 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
91 else (pend)->list/**/_tail->next = (elt); \
92 (pend)->list/**/_tail = (elt); \
96 static void print_help PARAMS ((void));
97 static void path_include PARAMS ((cpp_reader *,
99 static void init_library PARAMS ((void));
100 static void init_builtins PARAMS ((cpp_reader *));
101 static void append_include_chain PARAMS ((cpp_reader *,
103 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
104 struct search_path *));
105 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
106 struct search_path *));
107 static void merge_include_chains PARAMS ((cpp_reader *));
108 static bool push_include PARAMS ((cpp_reader *,
109 struct pending_option *));
110 static void free_chain PARAMS ((struct pending_option *));
111 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
112 static void init_dependency_output PARAMS ((cpp_reader *));
113 static void init_standard_includes PARAMS ((cpp_reader *));
114 static void new_pending_directive PARAMS ((struct cpp_pending *,
116 cl_directive_handler));
117 static void output_deps PARAMS ((cpp_reader *));
118 static int parse_option PARAMS ((const char *));
120 /* Fourth argument to append_include_chain: chain to use.
121 Note it's never asked to append to the quote chain. */
122 enum { BRACKET = 0, SYSTEM, AFTER };
124 /* If we have designated initializers (GCC >2.7) these tables can be
125 initialized, constant data. Otherwise, they have to be filled in at
127 #if HAVE_DESIGNATED_INITIALIZERS
129 #define init_trigraph_map() /* Nothing. */
130 #define TRIGRAPH_MAP \
131 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
134 #define s(p, v) [p] = v,
138 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
139 static void init_trigraph_map PARAMS ((void)) { \
140 unsigned char *x = _cpp_trigraph_map;
143 #define s(p, v) x[p] = v;
148 s('=', '#') s(')', ']') s('!', '|')
149 s('(', '[') s('\'', '^') s('>', '}')
150 s('/', '\\') s('<', '{') s('-', '~')
157 /* Given a colon-separated list of file names PATH,
158 add all the names to the search path for include files. */
161 path_include (pfile, list, path)
172 /* Find the end of this name. */
174 while (*q != 0 && *q != PATH_SEPARATOR) q++;
177 /* An empty name in the path stands for the current directory. */
178 name = (char *) xmalloc (2);
184 /* Otherwise use the directory that is named. */
185 name = (char *) xmalloc (q - p + 1);
186 memcpy (name, p, q - p);
190 append_include_chain (pfile, name, path, 0);
192 /* Advance past this name. */
200 /* Append DIR to include path PATH. DIR must be allocated on the
201 heap; this routine takes responsibility for freeing it. */
203 append_include_chain (pfile, dir, path, cxx_aware)
207 int cxx_aware ATTRIBUTE_UNUSED;
209 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
210 struct search_path *new;
219 _cpp_simplify_pathname (dir);
223 /* Dirs that don't exist are silently ignored. */
225 cpp_notice_from_errno (pfile, dir);
226 else if (CPP_OPTION (pfile, verbose))
227 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
232 if (!S_ISDIR (st.st_mode))
234 cpp_notice (pfile, "%s: Not a directory", dir);
240 if (len > pfile->max_include_len)
241 pfile->max_include_len = len;
243 new = (struct search_path *) xmalloc (sizeof (struct search_path));
246 new->ino = st.st_ino;
247 new->dev = st.st_dev;
248 /* Both systm and after include file lists should be treated as system
249 include files since these two lists are really just a concatenation
250 of one "system" list. */
251 if (path == SYSTEM || path == AFTER)
252 #ifdef NO_IMPLICIT_EXTERN_C
255 new->sysp = cxx_aware ? 1 : 2;
259 new->name_map = NULL;
264 case BRACKET: APPEND (pend, brack, new); break;
265 case SYSTEM: APPEND (pend, systm, new); break;
266 case AFTER: APPEND (pend, after, new); break;
270 /* Handle a duplicated include path. PREV is the link in the chain
271 before the duplicate. The duplicate is removed from the chain and
272 freed. Returns PREV. */
273 static struct search_path *
274 remove_dup_dir (pfile, prev)
276 struct search_path *prev;
278 struct search_path *cur = prev->next;
280 if (CPP_OPTION (pfile, verbose))
281 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
283 prev->next = cur->next;
284 free ((PTR) cur->name);
290 /* Remove duplicate directories from a chain. Returns the tail of the
291 chain, or NULL if the chain is empty. This algorithm is quadratic
292 in the number of -I switches, which is acceptable since there
293 aren't usually that many of them. */
294 static struct search_path *
295 remove_dup_dirs (pfile, head)
297 struct search_path *head;
299 struct search_path *prev = NULL, *cur, *other;
301 for (cur = head; cur; cur = cur->next)
303 for (other = head; other != cur; other = other->next)
304 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
306 if (cur->sysp && !other->sysp)
309 "changing search order for system directory \"%s\"",
311 if (strcmp (cur->name, other->name))
313 " as it is the same as non-system directory \"%s\"",
317 " as it has already been specified as a non-system directory");
319 cur = remove_dup_dir (pfile, prev);
328 /* Merge the four include chains together in the order quote, bracket,
329 system, after. Remove duplicate dirs (as determined by
330 INO_T_EQ()). The system_include and after_include chains are never
331 referred to again after this function; all access is through the
332 bracket_include path.
334 For the future: Check if the directory is empty (but
335 how?) and possibly preload the include hash. */
338 merge_include_chains (pfile)
341 struct search_path *quote, *brack, *systm, *qtail;
343 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
345 quote = pend->quote_head;
346 brack = pend->brack_head;
347 systm = pend->systm_head;
348 qtail = pend->quote_tail;
350 /* Paste together bracket, system, and after include chains. */
352 pend->systm_tail->next = pend->after_head;
354 systm = pend->after_head;
357 pend->brack_tail->next = systm;
361 /* This is a bit tricky. First we drop dupes from the quote-include
362 list. Then we drop dupes from the bracket-include list.
363 Finally, if qtail and brack are the same directory, we cut out
364 brack and move brack up to point to qtail.
366 We can't just merge the lists and then uniquify them because
367 then we may lose directories from the <> search path that should
368 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
369 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
370 -Ibar -I- -Ifoo -Iquux. */
372 remove_dup_dirs (pfile, brack);
373 qtail = remove_dup_dirs (pfile, quote);
379 /* If brack == qtail, remove brack as it's simpler. */
380 if (INO_T_EQ (qtail->ino, brack->ino) && qtail->dev == brack->dev)
381 brack = remove_dup_dir (pfile, qtail);
386 CPP_OPTION (pfile, quote_include) = quote;
387 CPP_OPTION (pfile, bracket_include) = brack;
390 /* Sets internal flags correctly for a given language, and defines
391 macros if necessary. */
398 char extended_numbers;
400 char dollars_in_ident;
401 char cplusplus_comments;
405 /* ??? Enable $ in identifiers in assembly? */
406 static const struct lang_flags lang_defaults[] =
407 { /* c99 objc c++ xnum trig dollar c++comm digr */
408 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
409 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
410 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
411 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
412 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
413 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
414 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
415 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
416 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
417 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
421 set_lang (pfile, lang)
425 const struct lang_flags *l = &lang_defaults[(int) lang];
427 CPP_OPTION (pfile, lang) = lang;
429 CPP_OPTION (pfile, c99) = l->c99;
430 CPP_OPTION (pfile, objc) = l->objc;
431 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
432 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
433 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
434 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
435 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
436 CPP_OPTION (pfile, digraphs) = l->digraphs;
440 static int opt_comp PARAMS ((const void *, const void *));
442 /* Run-time sorting of options array. */
447 return strcmp (((struct cl_option *) p1)->opt_text,
448 ((struct cl_option *) p2)->opt_text);
452 /* init initializes library global state. It might not need to
453 do anything depending on the platform and compiler. */
458 static int initialized = 0;
465 /* For non-ASCII hosts, the cl_options array needs to be sorted at
467 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
470 /* Set up the trigraph map. This doesn't need to do anything if
471 we were compiled with a compiler that supports C99 designated
473 init_trigraph_map ();
477 /* Initialize a cpp_reader structure. */
479 cpp_create_reader (table, lang)
483 struct spec_nodes *s;
486 /* Initialise this instance of the library if it hasn't been already. */
489 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
491 set_lang (pfile, lang);
492 CPP_OPTION (pfile, warn_import) = 1;
493 CPP_OPTION (pfile, discard_comments) = 1;
494 CPP_OPTION (pfile, show_column) = 1;
495 CPP_OPTION (pfile, tabstop) = 8;
496 CPP_OPTION (pfile, operator_names) = 1;
498 CPP_OPTION (pfile, pending) =
499 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
501 /* It's simplest to just create this struct whether or not it will
503 pfile->deps = deps_init ();
505 /* Initialise the line map. */
506 init_line_maps (&pfile->line_maps);
508 /* Initialize lexer state. */
509 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
511 /* Indicate date and time not yet calculated. */
512 pfile->date.type = CPP_EOF;
514 /* Create a token buffer for the lexer. */
515 _cpp_init_tokenrun (&pfile->base_run, 250);
516 pfile->cur_run = &pfile->base_run;
517 pfile->cur_token = pfile->base_run.base;
519 /* Initialise the base context. */
520 pfile->context = &pfile->base_context;
521 pfile->base_context.macro = 0;
522 pfile->base_context.prev = pfile->base_context.next = 0;
524 /* Identifier pool initially 8K. Unaligned, permanent pool. */
525 _cpp_init_pool (&pfile->ident_pool, 8 * 1024, 1, 0);
527 /* Argument pool initially 8K. Aligned, temporary pool. */
528 _cpp_init_pool (&pfile->argument_pool, 8 * 1024, 0, 1);
530 /* Macro pool initially 8K. Aligned, permanent pool. */
531 _cpp_init_pool (&pfile->macro_pool, 8 * 1024, 0, 0);
533 /* Initialise the buffer obstack. */
534 gcc_obstack_init (&pfile->buffer_ob);
536 /* Initialise the hashtable. */
537 _cpp_init_hashtable (pfile, table);
539 _cpp_init_directives (pfile);
540 _cpp_init_includes (pfile);
541 _cpp_init_internal_pragmas (pfile);
543 /* Initialize the special nodes. */
544 s = &pfile->spec_nodes;
545 s->n_L = cpp_lookup (pfile, DSC("L"));
546 s->n_defined = cpp_lookup (pfile, DSC("defined"));
547 s->n_true = cpp_lookup (pfile, DSC("true"));
548 s->n_false = cpp_lookup (pfile, DSC("false"));
549 s->n__Pragma = cpp_lookup (pfile, DSC("_Pragma"));
550 s->n__STRICT_ANSI__ = cpp_lookup (pfile, DSC("__STRICT_ANSI__"));
551 s->n__CHAR_UNSIGNED__ = cpp_lookup (pfile, DSC("__CHAR_UNSIGNED__"));
552 s->n__VA_ARGS__ = cpp_lookup (pfile, DSC("__VA_ARGS__"));
553 s->n__VA_ARGS__->flags |= NODE_DIAGNOSTIC;
558 /* Free resources used by PFILE. Accessing PFILE after this function
559 returns leads to undefined behaviour. */
565 struct search_path *dir, *dirn;
566 cpp_context *context, *contextn;
568 while (CPP_BUFFER (pfile) != NULL)
569 _cpp_pop_buffer (pfile);
571 if (pfile->macro_buffer)
573 free ((PTR) pfile->macro_buffer);
574 pfile->macro_buffer = NULL;
575 pfile->macro_buffer_len = 0;
578 deps_free (pfile->deps);
579 obstack_free (&pfile->buffer_ob, 0);
581 _cpp_destroy_hashtable (pfile);
582 _cpp_cleanup_includes (pfile);
584 _cpp_free_pool (&pfile->ident_pool);
585 _cpp_free_pool (&pfile->macro_pool);
586 _cpp_free_pool (&pfile->argument_pool);
588 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
591 free ((PTR) dir->name);
595 for (context = pfile->base_context.next; context; context = contextn)
597 contextn = context->next;
601 free_line_maps (&pfile->line_maps);
603 result = pfile->errors;
610 /* This structure defines one built-in identifier. A node will be
611 entered in the hash table under the name NAME, with value VALUE (if
612 any). If flags has OPERATOR, the node's operator field is used; if
613 flags has BUILTIN the node's builtin field is used. Macros that are
614 known at build time should not be flagged BUILTIN, as then they do
615 not appear in macro dumps with e.g. -dM or -dD.
617 Two values are not compile time constants, so we tag
618 them in the FLAGS field instead:
619 VERS value is the global version_string, quoted
620 ULP value is the global user_label_prefix
622 Also, macros with CPLUS set in the flags field are entered only for C++. */
628 unsigned char builtin;
629 unsigned char operator;
630 unsigned short flags;
637 #define OPERATOR 0x10
639 #define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
640 #define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
641 #define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
642 #define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
643 static const struct builtin builtin_array[] =
645 B("__TIME__", BT_TIME),
646 B("__DATE__", BT_DATE),
647 B("__FILE__", BT_FILE),
648 B("__BASE_FILE__", BT_BASE_FILE),
649 B("__LINE__", BT_SPECLINE),
650 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
652 X("__VERSION__", VERS),
653 X("__USER_LABEL_PREFIX__", ULP),
654 C("__REGISTER_PREFIX__", REGISTER_PREFIX),
655 C("__HAVE_BUILTIN_SETJMP__", "1"),
656 #ifndef NO_BUILTIN_SIZE_TYPE
657 C("__SIZE_TYPE__", SIZE_TYPE),
659 #ifndef NO_BUILTIN_PTRDIFF_TYPE
660 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE),
662 #ifndef NO_BUILTIN_WCHAR_TYPE
663 C("__WCHAR_TYPE__", WCHAR_TYPE),
665 #ifndef NO_BUILTIN_WINT_TYPE
666 C("__WINT_TYPE__", WINT_TYPE),
668 #ifdef STDC_0_IN_SYSTEM_HEADERS
669 B("__STDC__", BT_STDC),
674 /* Named operators known to the preprocessor. These cannot be #defined
675 and always have their stated meaning. They are treated like normal
676 identifiers except for the type code and the meaning. Most of them
677 are only for C++ (but see iso646.h). */
678 O("and", CPP_AND_AND, CPLUS),
679 O("and_eq", CPP_AND_EQ, CPLUS),
680 O("bitand", CPP_AND, CPLUS),
681 O("bitor", CPP_OR, CPLUS),
682 O("compl", CPP_COMPL, CPLUS),
683 O("not", CPP_NOT, CPLUS),
684 O("not_eq", CPP_NOT_EQ, CPLUS),
685 O("or", CPP_OR_OR, CPLUS),
686 O("or_eq", CPP_OR_EQ, CPLUS),
687 O("xor", CPP_XOR, CPLUS),
688 O("xor_eq", CPP_XOR_EQ, CPLUS)
694 #define builtin_array_end \
695 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
697 /* Subroutine of cpp_start_read; reads the builtins table above and
698 enters the macros into the hash table. */
700 init_builtins (pfile)
703 const struct builtin *b;
705 for(b = builtin_array; b < builtin_array_end; b++)
707 if ((b->flags & CPLUS) && ! CPP_OPTION (pfile, cplusplus))
710 if ((b->flags & OPERATOR) && ! CPP_OPTION (pfile, operator_names))
713 if (b->flags & (OPERATOR | BUILTIN))
715 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
716 if (b->flags & OPERATOR)
718 hp->flags |= NODE_OPERATOR;
719 hp->value.operator = b->operator;
724 hp->flags |= NODE_BUILTIN | NODE_WARN;
725 hp->value.builtin = b->builtin;
728 else /* A standard macro of some kind. */
735 /* Allocate enough space for 'name "value"\n\0'. */
736 str = alloca (b->len + strlen (version_string) + 5);
737 sprintf (str, "%s \"%s\"\n", b->name, version_string);
742 val = CPP_OPTION (pfile, user_label_prefix);
746 /* Allocate enough space for "name value\n\0". */
747 str = alloca (b->len + strlen (val) + 3);
748 sprintf(str, "%s %s\n", b->name, val);
751 _cpp_define_builtin (pfile, str);
755 if (CPP_OPTION (pfile, cplusplus))
757 _cpp_define_builtin (pfile, "__cplusplus 1");
758 if (SUPPORTS_ONE_ONLY)
759 _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
761 _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
763 if (CPP_OPTION (pfile, objc))
764 _cpp_define_builtin (pfile, "__OBJC__ 1");
766 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
767 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
768 else if (CPP_OPTION (pfile, c99))
769 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
771 if (CPP_OPTION (pfile, lang) == CLK_STDC89
772 || CPP_OPTION (pfile, lang) == CLK_STDC94
773 || CPP_OPTION (pfile, lang) == CLK_STDC99)
774 _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
775 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
776 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
783 #undef builtin_array_end
785 /* And another subroutine. This one sets up the standard include path. */
787 init_standard_includes (pfile)
791 const struct default_include *p;
792 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
794 /* Several environment variables may add to the include search path.
795 CPATH specifies an additional list of directories to be searched
796 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
797 etc. specify an additional list of directories to be searched as
798 if specified with -isystem, for the language indicated. */
800 GET_ENV_PATH_LIST (path, "CPATH");
801 if (path != 0 && *path != 0)
802 path_include (pfile, path, BRACKET);
804 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
807 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
810 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
813 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
816 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
819 if (path != 0 && *path != 0)
820 path_include (pfile, path, SYSTEM);
822 /* Search "translated" versions of GNU directories.
823 These have /usr/local/lib/gcc... replaced by specd_prefix. */
824 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
826 /* Remove the `include' from /usr/local/lib/gcc.../include.
827 GCC_INCLUDE_DIR will always end in /include. */
828 int default_len = cpp_GCC_INCLUDE_DIR_len;
829 char *default_prefix = (char *) alloca (default_len + 1);
830 int specd_len = strlen (specd_prefix);
832 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
833 default_prefix[default_len] = '\0';
835 for (p = cpp_include_defaults; p->fname; p++)
837 /* Some standard dirs are only for C++. */
839 || (CPP_OPTION (pfile, cplusplus)
840 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
842 /* Does this dir start with the prefix? */
843 if (!memcmp (p->fname, default_prefix, default_len))
845 /* Yes; change prefix and add to search list. */
846 int flen = strlen (p->fname);
847 int this_len = specd_len + flen - default_len;
848 char *str = (char *) xmalloc (this_len + 1);
849 memcpy (str, specd_prefix, specd_len);
850 memcpy (str + specd_len,
851 p->fname + default_len,
852 flen - default_len + 1);
854 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
860 /* Search ordinary names for GNU include directories. */
861 for (p = cpp_include_defaults; p->fname; p++)
863 /* Some standard dirs are only for C++. */
865 || (CPP_OPTION (pfile, cplusplus)
866 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
868 char *str = update_path (p->fname, p->component);
869 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
874 /* Pushes a -imacro and -include file given on the command line onto
875 the buffer stack. Returns non-zero if successful. */
877 push_include (pfile, p)
879 struct pending_option *p;
883 /* Later: maybe update this to use the #include "" search path
884 if cpp_read_file fails. */
885 header.type = CPP_STRING;
886 header.val.str.text = (const unsigned char *) p->arg;
887 header.val.str.len = strlen (p->arg);
888 /* Make the command line directive take up a line. */
889 pfile->lexer_pos.line = ++pfile->line;
891 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
894 /* Frees a pending_option chain. */
897 struct pending_option *head;
899 struct pending_option *next;
909 /* This is called after options have been processed. Setup for
910 processing input from the file named FNAME, or stdin if it is the
911 empty string. Return 1 on success, 0 on failure. */
913 cpp_start_read (pfile, fname)
917 /* Set up the include search path now. */
918 if (! CPP_OPTION (pfile, no_standard_includes))
919 init_standard_includes (pfile);
921 merge_include_chains (pfile);
923 /* With -v, print the list of dirs to search. */
924 if (CPP_OPTION (pfile, verbose))
926 struct search_path *l;
927 fprintf (stderr, _("#include \"...\" search starts here:\n"));
928 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
930 if (l == CPP_OPTION (pfile, bracket_include))
931 fprintf (stderr, _("#include <...> search starts here:\n"));
932 fprintf (stderr, " %s\n", l->name);
934 fprintf (stderr, _("End of search list.\n"));
937 if (CPP_OPTION (pfile, print_deps))
938 /* Set the default target (if there is none already). */
939 deps_add_default_target (pfile->deps, fname);
941 /* Open the main input file. This must be done early, so we have a
942 buffer to stand on. */
943 if (!_cpp_read_file (pfile, fname))
946 /* Set this after cpp_post_options so the client can change the
947 option if it wishes, and after stacking the main file so we don't
948 trace the main file. */
949 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
951 /* Install builtins and process command line macros etc. in the order
952 they appeared, but only if not already preprocessed. */
953 if (! CPP_OPTION (pfile, preprocessed))
955 struct pending_option *p;
957 _cpp_do_file_change (pfile, LC_RENAME, _("<builtin>"), 1, 0);
958 init_builtins (pfile);
959 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
960 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
961 (*p->handler) (pfile, p->arg);
963 /* Scan -imacros files after command line defines, but before
964 files given with -include. */
965 while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
967 if (push_include (pfile, p))
969 pfile->buffer->return_at_eof = true;
970 cpp_scan_nooutput (pfile);
972 CPP_OPTION (pfile, pending)->imacros_head = p->next;
977 free_chain (CPP_OPTION (pfile, pending)->directive_head);
978 _cpp_push_next_buffer (pfile);
983 /* Called to push the next buffer on the stack given by -include. If
984 there are none, free the pending structure and restore the line map
985 for the main file. */
987 _cpp_push_next_buffer (pfile)
992 /* This is't pretty; we'd rather not be relying on this as a boolean
993 for reverting the line map. Further, we only free the chains in
994 this conditional, so an early call to cpp_finish / cpp_destroy
995 will leak that memory. */
996 if (CPP_OPTION (pfile, pending)
997 && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1001 struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1005 if (! CPP_OPTION (pfile, preprocessed))
1006 pushed = push_include (pfile, p);
1007 CPP_OPTION (pfile, pending)->include_head = p->next;
1013 free (CPP_OPTION (pfile, pending));
1014 CPP_OPTION (pfile, pending) = NULL;
1016 /* Restore the line map for the main file. */
1017 if (! CPP_OPTION (pfile, preprocessed))
1018 _cpp_do_file_change (pfile, LC_RENAME,
1019 pfile->line_maps.maps[0].to_file, 1, 0);
1026 /* Use mkdeps.c to output dependency information. */
1031 /* Stream on which to print the dependency information. */
1032 FILE *deps_stream = 0;
1033 const char *const deps_mode =
1034 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1036 if (CPP_OPTION (pfile, deps_file) == 0)
1037 deps_stream = stdout;
1040 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1041 if (deps_stream == 0)
1043 cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1048 deps_write (pfile->deps, deps_stream, 72);
1050 if (CPP_OPTION (pfile, deps_phony_targets))
1051 deps_phony_targets (pfile->deps, deps_stream);
1053 /* Don't close stdout. */
1054 if (CPP_OPTION (pfile, deps_file))
1056 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1057 cpp_fatal (pfile, "I/O error on output");
1061 /* This is called at the end of preprocessing. It pops the
1062 last buffer and writes dependency output. It should also
1063 clear macro definitions, such that you could call cpp_start_read
1064 with a new filename to restart processing. */
1069 /* cpplex.c leaves the final buffer on the stack. This it so that
1070 it returns an unending stream of CPP_EOFs to the client. If we
1071 popped the buffer, we'd derefence a NULL buffer pointer and
1072 segfault. It's nice to allow the client to do worry-free excess
1073 cpp_get_token calls. */
1074 while (pfile->buffer)
1075 _cpp_pop_buffer (pfile);
1077 /* Don't write the deps file if preprocessing has failed. */
1078 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1079 output_deps (pfile);
1081 /* Report on headers that could use multiple include guards. */
1082 if (CPP_OPTION (pfile, print_include_names))
1083 _cpp_report_missing_guards (pfile);
1087 new_pending_directive (pend, text, handler)
1088 struct cpp_pending *pend;
1090 cl_directive_handler handler;
1092 struct pending_option *o = (struct pending_option *)
1093 xmalloc (sizeof (struct pending_option));
1097 o->handler = handler;
1098 APPEND (pend, directive, o);
1101 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1102 I.e. a const string initializer with parens around it. That is
1103 what N_("string") resolves to, so we make no_* be macros instead. */
1104 #define no_arg N_("Argument missing after %s")
1105 #define no_ass N_("Assertion missing after %s")
1106 #define no_dir N_("Directory name missing after %s")
1107 #define no_fil N_("File name missing after %s")
1108 #define no_mac N_("Macro name missing after %s")
1109 #define no_pth N_("Path name missing after %s")
1110 #define no_num N_("Number missing after %s")
1111 #define no_tgt N_("Target missing after %s")
1113 /* This is the list of all command line options, with the leading
1114 "-" removed. It must be sorted in ASCII collating order. */
1115 #define COMMAND_LINE_OPTIONS \
1116 DEF_OPT("$", 0, OPT_dollar) \
1117 DEF_OPT("+", 0, OPT_plus) \
1118 DEF_OPT("-help", 0, OPT__help) \
1119 DEF_OPT("-target-help", 0, OPT_target__help) \
1120 DEF_OPT("-version", 0, OPT__version) \
1121 DEF_OPT("A", no_ass, OPT_A) \
1122 DEF_OPT("C", 0, OPT_C) \
1123 DEF_OPT("D", no_mac, OPT_D) \
1124 DEF_OPT("H", 0, OPT_H) \
1125 DEF_OPT("I", no_dir, OPT_I) \
1126 DEF_OPT("M", 0, OPT_M) \
1127 DEF_OPT("MD", no_fil, OPT_MD) \
1128 DEF_OPT("MF", no_fil, OPT_MF) \
1129 DEF_OPT("MG", 0, OPT_MG) \
1130 DEF_OPT("MM", 0, OPT_MM) \
1131 DEF_OPT("MMD", no_fil, OPT_MMD) \
1132 DEF_OPT("MP", 0, OPT_MP) \
1133 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1134 DEF_OPT("MT", no_tgt, OPT_MT) \
1135 DEF_OPT("P", 0, OPT_P) \
1136 DEF_OPT("U", no_mac, OPT_U) \
1137 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1138 DEF_OPT("d", no_arg, OPT_d) \
1139 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1140 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1141 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1142 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1143 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1144 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1145 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1146 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1147 DEF_OPT("h", 0, OPT_h) \
1148 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1149 DEF_OPT("imacros", no_fil, OPT_imacros) \
1150 DEF_OPT("include", no_fil, OPT_include) \
1151 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1152 DEF_OPT("isystem", no_dir, OPT_isystem) \
1153 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1154 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1155 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1156 DEF_OPT("lang-c", 0, OPT_lang_c) \
1157 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1158 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1159 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1160 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1161 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1162 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1163 DEF_OPT("o", no_fil, OPT_o) \
1164 DEF_OPT("pedantic", 0, OPT_pedantic) \
1165 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1166 DEF_OPT("remap", 0, OPT_remap) \
1167 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1168 DEF_OPT("std=c89", 0, OPT_std_c89) \
1169 DEF_OPT("std=c99", 0, OPT_std_c99) \
1170 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1171 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1172 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1173 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1174 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1175 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1176 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1177 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1178 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1179 DEF_OPT("v", 0, OPT_v) \
1180 DEF_OPT("version", 0, OPT_version) \
1181 DEF_OPT("w", 0, OPT_w)
1183 #define DEF_OPT(text, msg, code) code,
1186 COMMAND_LINE_OPTIONS
1193 const char *opt_text;
1196 enum opt_code opt_code;
1199 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1201 static struct cl_option cl_options[] =
1203 static const struct cl_option cl_options[] =
1206 COMMAND_LINE_OPTIONS
1209 #undef COMMAND_LINE_OPTIONS
1211 /* Perform a binary search to find which, if any, option the given
1212 command-line matches. Returns its index in the option array,
1213 negative on failure. Complications arise since some options can be
1214 suffixed with an argument, and multiple complete matches can occur,
1215 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1216 accept options beginning with -W that we do not recognise, but not
1217 to swallow any subsequent command line argument; this is handled as
1218 special cases in cpp_handle_option. */
1220 parse_option (input)
1223 unsigned int md, mn, mx;
1234 opt_len = cl_options[md].opt_len;
1235 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1243 if (input[opt_len] == '\0')
1245 /* We were passed more text. If the option takes an argument,
1246 we may match a later option or we may have been passed the
1247 argument. The longest possible option match succeeds.
1248 If the option takes no arguments we have not matched and
1249 continue the search (e.g. input="stdc++" match was "stdc"). */
1251 if (cl_options[md].msg)
1253 /* Scan forwards. If we get an exact match, return it.
1254 Otherwise, return the longest option-accepting match.
1255 This loops no more than twice with current options. */
1257 for (; mn < (unsigned int) N_OPTS; mn++)
1259 opt_len = cl_options[mn].opt_len;
1260 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1262 if (input[opt_len] == '\0')
1264 if (cl_options[mn].msg)
1275 /* Handle one command-line option in (argc, argv).
1276 Can be called multiple times, to handle multiple sets of options.
1277 Returns number of strings consumed. */
1280 cpp_handle_option (pfile, argc, argv)
1286 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1288 /* Interpret "-" or a non-option as a file name. */
1289 if (argv[i][0] != '-' || argv[i][1] == '\0')
1291 if (CPP_OPTION (pfile, in_fname) == NULL)
1292 CPP_OPTION (pfile, in_fname) = argv[i];
1293 else if (CPP_OPTION (pfile, out_fname) == NULL)
1294 CPP_OPTION (pfile, out_fname) = argv[i];
1296 cpp_fatal (pfile, "Too many filenames. Type %s --help for usage info",
1301 enum opt_code opt_code;
1303 const char *arg = 0;
1305 /* Skip over '-'. */
1306 opt_index = parse_option (&argv[i][1]);
1310 opt_code = cl_options[opt_index].opt_code;
1311 if (cl_options[opt_index].msg)
1313 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1315 /* Yuk. Special case for -W as it must not swallow
1316 up any following argument. If this becomes common, add
1317 another field to the cl_options table. */
1318 if (arg[0] == '\0' && opt_code != OPT_W)
1323 cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1331 case N_OPTS: /* Shut GCC up. */
1333 case OPT_fleading_underscore:
1334 CPP_OPTION (pfile, user_label_prefix) = "_";
1336 case OPT_fno_leading_underscore:
1337 CPP_OPTION (pfile, user_label_prefix) = "";
1339 case OPT_fno_operator_names:
1340 CPP_OPTION (pfile, operator_names) = 0;
1342 case OPT_fpreprocessed:
1343 CPP_OPTION (pfile, preprocessed) = 1;
1345 case OPT_fno_preprocessed:
1346 CPP_OPTION (pfile, preprocessed) = 0;
1348 case OPT_fshow_column:
1349 CPP_OPTION (pfile, show_column) = 1;
1351 case OPT_fno_show_column:
1352 CPP_OPTION (pfile, show_column) = 0;
1355 /* Silently ignore empty string, non-longs and silly values. */
1359 long tabstop = strtol (arg, &endptr, 10);
1360 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1361 CPP_OPTION (pfile, tabstop) = tabstop;
1365 CPP_OPTION (pfile, inhibit_warnings) = 1;
1370 CPP_OPTION (pfile, help_only) = 1;
1372 case OPT_target__help:
1373 /* Print if any target specific options. cpplib has none, but
1374 make sure help_only gets set. */
1375 CPP_OPTION (pfile, help_only) = 1;
1378 /* --version inhibits compilation, -version doesn't. -v means
1379 verbose and -version. Historical reasons, don't ask. */
1381 CPP_OPTION (pfile, help_only) = 1;
1382 pfile->print_version = 1;
1385 CPP_OPTION (pfile, verbose) = 1;
1386 pfile->print_version = 1;
1389 pfile->print_version = 1;
1393 CPP_OPTION (pfile, discard_comments) = 0;
1396 CPP_OPTION (pfile, no_line_commands) = 1;
1398 case OPT_dollar: /* Don't include $ in identifiers. */
1399 CPP_OPTION (pfile, dollars_in_ident) = 0;
1402 CPP_OPTION (pfile, print_include_names) = 1;
1405 new_pending_directive (pend, arg, cpp_define);
1407 case OPT_pedantic_errors:
1408 CPP_OPTION (pfile, pedantic_errors) = 1;
1411 CPP_OPTION (pfile, pedantic) = 1;
1414 CPP_OPTION (pfile, trigraphs) = 1;
1417 CPP_OPTION (pfile, cplusplus) = 1;
1418 CPP_OPTION (pfile, cplusplus_comments) = 1;
1421 CPP_OPTION (pfile, remap) = 1;
1424 CPP_OPTION (pfile, include_prefix) = arg;
1425 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1428 set_lang (pfile, CLK_GNUC89);
1430 case OPT_lang_cplusplus:
1431 set_lang (pfile, CLK_GNUCXX);
1434 set_lang (pfile, CLK_OBJC);
1436 case OPT_lang_objcplusplus:
1437 set_lang (pfile, CLK_OBJCXX);
1440 set_lang (pfile, CLK_ASM);
1442 case OPT_std_cplusplus98:
1443 set_lang (pfile, CLK_CXX98);
1446 set_lang (pfile, CLK_GNUC89);
1450 set_lang (pfile, CLK_GNUC99);
1452 case OPT_std_iso9899_199409:
1453 set_lang (pfile, CLK_STDC94);
1455 case OPT_std_iso9899_1990:
1458 set_lang (pfile, CLK_STDC89);
1460 case OPT_std_iso9899_199x:
1461 case OPT_std_iso9899_1999:
1464 set_lang (pfile, CLK_STDC99);
1467 /* -nostdinc causes no default include directories.
1468 You must specify all include-file directories with -I. */
1469 CPP_OPTION (pfile, no_standard_includes) = 1;
1471 case OPT_nostdincplusplus:
1472 /* -nostdinc++ causes no default C++-specific include directories. */
1473 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1476 if (CPP_OPTION (pfile, out_fname) == NULL)
1477 CPP_OPTION (pfile, out_fname) = arg;
1480 cpp_fatal (pfile, "Output filename specified twice");
1485 /* Args to -d specify what parts of macros to dump.
1486 Silently ignore unrecognised options; they may
1487 be aimed at the compiler proper. */
1491 while ((c = *arg++) != '\0')
1495 CPP_OPTION (pfile, dump_macros) = dump_only;
1496 CPP_OPTION (pfile, no_output) = 1;
1499 CPP_OPTION (pfile, dump_macros) = dump_names;
1502 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1505 CPP_OPTION (pfile, dump_includes) = 1;
1512 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1515 CPP_OPTION (pfile, print_deps) = 2;
1518 CPP_OPTION (pfile, print_deps) = 1;
1521 CPP_OPTION (pfile, deps_file) = arg;
1524 CPP_OPTION (pfile, deps_phony_targets) = 1;
1528 /* Add a target. -MQ quotes for Make. */
1529 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1532 /* -MD and -MMD for cpp0 are deprecated and undocumented
1533 (use -M or -MM with -MF instead), and probably should be
1534 removed with the next major GCC version. For the moment
1535 we allow these for the benefit of Automake 1.4, which
1536 uses these when dependency tracking is enabled. Automake
1537 1.5 will fix this. */
1539 CPP_OPTION (pfile, print_deps) = 2;
1540 CPP_OPTION (pfile, deps_file) = arg;
1543 CPP_OPTION (pfile, print_deps) = 1;
1544 CPP_OPTION (pfile, deps_file) = arg;
1550 /* -A with an argument beginning with '-' acts as
1551 #unassert on whatever immediately follows the '-'.
1552 If "-" is the whole argument, we eliminate all
1553 predefined macros and assertions, including those
1554 that were specified earlier on the command line.
1555 That way we can get rid of any that were passed
1556 automatically in from GCC. */
1560 free_chain (pend->directive_head);
1561 pend->directive_head = NULL;
1562 pend->directive_tail = NULL;
1565 new_pending_directive (pend, arg + 1, cpp_unassert);
1568 new_pending_directive (pend, arg, cpp_assert);
1571 new_pending_directive (pend, arg, cpp_undef);
1573 case OPT_I: /* Add directory to path for includes. */
1574 if (!strcmp (arg, "-"))
1577 Use the preceding -I directories for #include "..."
1578 but not #include <...>.
1579 Don't search the directory of the present file
1580 for #include "...". (Note that -I. -I- is not the same as
1581 the default setup; -I. uses the compiler's working dir.) */
1582 if (! CPP_OPTION (pfile, ignore_srcdir))
1584 pend->quote_head = pend->brack_head;
1585 pend->quote_tail = pend->brack_tail;
1586 pend->brack_head = 0;
1587 pend->brack_tail = 0;
1588 CPP_OPTION (pfile, ignore_srcdir) = 1;
1592 cpp_fatal (pfile, "-I- specified twice");
1597 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1600 /* Add directory to beginning of system include path, as a system
1601 include directory. */
1602 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1607 struct pending_option *o = (struct pending_option *)
1608 xmalloc (sizeof (struct pending_option));
1612 if (opt_code == OPT_include)
1613 APPEND (pend, include, o);
1615 APPEND (pend, imacros, o);
1618 case OPT_iwithprefix:
1619 /* Add directory to end of path for includes,
1620 with the default prefix at the front of its name. */
1622 case OPT_iwithprefixbefore:
1623 /* Add directory to main path for includes,
1624 with the default prefix at the front of its name. */
1631 if (CPP_OPTION (pfile, include_prefix) != 0)
1633 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1634 fname = xmalloc (ipl + len + 1);
1635 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1636 memcpy (fname + ipl, arg, len + 1);
1638 else if (cpp_GCC_INCLUDE_DIR_len)
1640 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1641 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1642 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1645 fname = xstrdup (arg);
1647 append_include_chain (pfile, fname,
1648 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1652 /* Add directory to end of path for includes. */
1653 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1656 /* Silently ignore unrecognised options. */
1657 if (!strcmp (argv[i], "-Wall"))
1659 CPP_OPTION (pfile, warn_trigraphs) = 1;
1660 CPP_OPTION (pfile, warn_comments) = 1;
1662 else if (!strcmp (argv[i], "-Wtraditional"))
1663 CPP_OPTION (pfile, warn_traditional) = 1;
1664 else if (!strcmp (argv[i], "-Wtrigraphs"))
1665 CPP_OPTION (pfile, warn_trigraphs) = 1;
1666 else if (!strcmp (argv[i], "-Wcomment"))
1667 CPP_OPTION (pfile, warn_comments) = 1;
1668 else if (!strcmp (argv[i], "-Wcomments"))
1669 CPP_OPTION (pfile, warn_comments) = 1;
1670 else if (!strcmp (argv[i], "-Wundef"))
1671 CPP_OPTION (pfile, warn_undef) = 1;
1672 else if (!strcmp (argv[i], "-Wimport"))
1673 CPP_OPTION (pfile, warn_import) = 1;
1674 else if (!strcmp (argv[i], "-Werror"))
1675 CPP_OPTION (pfile, warnings_are_errors) = 1;
1676 else if (!strcmp (argv[i], "-Wsystem-headers"))
1677 CPP_OPTION (pfile, warn_system_headers) = 1;
1678 else if (!strcmp (argv[i], "-Wno-traditional"))
1679 CPP_OPTION (pfile, warn_traditional) = 0;
1680 else if (!strcmp (argv[i], "-Wno-trigraphs"))
1681 CPP_OPTION (pfile, warn_trigraphs) = 0;
1682 else if (!strcmp (argv[i], "-Wno-comment"))
1683 CPP_OPTION (pfile, warn_comments) = 0;
1684 else if (!strcmp (argv[i], "-Wno-comments"))
1685 CPP_OPTION (pfile, warn_comments) = 0;
1686 else if (!strcmp (argv[i], "-Wno-undef"))
1687 CPP_OPTION (pfile, warn_undef) = 0;
1688 else if (!strcmp (argv[i], "-Wno-import"))
1689 CPP_OPTION (pfile, warn_import) = 0;
1690 else if (!strcmp (argv[i], "-Wno-error"))
1691 CPP_OPTION (pfile, warnings_are_errors) = 0;
1692 else if (!strcmp (argv[i], "-Wno-system-headers"))
1693 CPP_OPTION (pfile, warn_system_headers) = 0;
1700 /* Handle command-line options in (argc, argv).
1701 Can be called multiple times, to handle multiple sets of options.
1702 Returns if an unrecognized option is seen.
1703 Returns number of strings consumed. */
1705 cpp_handle_options (pfile, argc, argv)
1711 int strings_processed;
1713 for (i = 0; i < argc; i += strings_processed)
1715 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1716 if (strings_processed == 0)
1723 /* Extra processing when all options are parsed, after all calls to
1724 cpp_handle_option[s]. Consistency checks etc. */
1726 cpp_post_options (pfile)
1729 if (pfile->print_version)
1731 fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1732 #ifdef TARGET_VERSION
1735 fputc ('\n', stderr);
1738 /* Canonicalize in_fname and out_fname. We guarantee they are not
1739 NULL, and that the empty string represents stdin / stdout. */
1740 if (CPP_OPTION (pfile, in_fname) == NULL
1741 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1742 CPP_OPTION (pfile, in_fname) = "";
1744 if (CPP_OPTION (pfile, out_fname) == NULL
1745 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1746 CPP_OPTION (pfile, out_fname) = "";
1748 /* -Wtraditional is not useful in C++ mode. */
1749 if (CPP_OPTION (pfile, cplusplus))
1750 CPP_OPTION (pfile, warn_traditional) = 0;
1752 /* Set this if it hasn't been set already. */
1753 if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1754 CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1756 /* Permanently disable macro expansion if we are rescanning
1757 preprocessed text. */
1758 if (CPP_OPTION (pfile, preprocessed))
1759 pfile->state.prevent_expansion = 1;
1761 /* We need to do this after option processing and before
1762 cpp_start_read, as cppmain.c relies on the options->no_output to
1763 set its callbacks correctly before calling cpp_start_read. */
1764 init_dependency_output (pfile);
1766 /* After checking the environment variables, check if -M or -MM has
1767 not been specified, but other -M options have. */
1768 if (CPP_OPTION (pfile, print_deps) == 0 &&
1769 (CPP_OPTION (pfile, print_deps_missing_files)
1770 || CPP_OPTION (pfile, deps_file)
1771 || CPP_OPTION (pfile, deps_phony_targets)))
1772 cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1775 /* Set up dependency-file output. */
1777 init_dependency_output (pfile)
1780 char *spec, *s, *output_file;
1782 /* Either of two environment variables can specify output of deps.
1783 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1784 where OUTPUT_FILE is the file to write deps info to
1785 and DEPS_TARGET is the target to mention in the deps. */
1787 if (CPP_OPTION (pfile, print_deps) == 0)
1789 spec = getenv ("DEPENDENCIES_OUTPUT");
1791 CPP_OPTION (pfile, print_deps) = 1;
1794 spec = getenv ("SUNPRO_DEPENDENCIES");
1796 CPP_OPTION (pfile, print_deps) = 2;
1801 /* Find the space before the DEPS_TARGET, if there is one. */
1802 s = strchr (spec, ' ');
1805 /* Let the caller perform MAKE quoting. */
1806 deps_add_target (pfile->deps, s + 1, 0);
1807 output_file = (char *) xmalloc (s - spec + 1);
1808 memcpy (output_file, spec, s - spec);
1809 output_file[s - spec] = 0;
1814 /* Command line overrides environment variables. */
1815 if (CPP_OPTION (pfile, deps_file) == 0)
1816 CPP_OPTION (pfile, deps_file) = output_file;
1817 CPP_OPTION (pfile, print_deps_append) = 1;
1820 /* If dependencies go to standard output, or -MG is used, we should
1821 suppress output, including -dM, -dI etc. */
1822 if (CPP_OPTION (pfile, deps_file) == 0
1823 || CPP_OPTION (pfile, print_deps_missing_files))
1825 CPP_OPTION (pfile, no_output) = 1;
1826 CPP_OPTION (pfile, dump_macros) = 0;
1827 CPP_OPTION (pfile, dump_includes) = 0;
1834 fprintf (stderr, _("Usage: %s [switches] input output\n"), progname);
1835 /* To keep the lines from getting too long for some compilers, limit
1836 to about 500 characters (6 lines) per chunk. */
1839 -include <file> Include the contents of <file> before other files\n\
1840 -imacros <file> Accept definition of macros in <file>\n\
1841 -iprefix <path> Specify <path> as a prefix for next two options\n\
1842 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1843 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1844 -isystem <dir> Add <dir> to the start of the system include path\n\
1847 -idirafter <dir> Add <dir> to the end of the system include path\n\
1848 -I <dir> Add <dir> to the end of the main include path\n\
1849 -I- Fine-grained include path control; see info docs\n\
1850 -nostdinc Do not search system include directories\n\
1851 (dirs specified with -isystem will still be used)\n\
1852 -nostdinc++ Do not search system include directories for C++\n\
1853 -o <file> Put output into <file>\n\
1856 -pedantic Issue all warnings demanded by strict ISO C\n\
1857 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1858 -trigraphs Support ISO C trigraphs\n\
1859 -lang-c Assume that the input sources are in C\n\
1860 -lang-c89 Assume that the input sources are in C89\n\
1863 -lang-c++ Assume that the input sources are in C++\n\
1864 -lang-objc Assume that the input sources are in ObjectiveC\n\
1865 -lang-objc++ Assume that the input sources are in ObjectiveC++\n\
1866 -lang-asm Assume that the input sources are in assembler\n\
1869 -std=<std name> Specify the conformance standard; one of:\n\
1870 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1871 iso9899:199409, iso9899:1999\n\
1872 -+ Allow parsing of C++ style features\n\
1873 -w Inhibit warning messages\n\
1874 -Wtrigraphs Warn if trigraphs are encountered\n\
1875 -Wno-trigraphs Do not warn about trigraphs\n\
1876 -Wcomment{s} Warn if one comment starts inside another\n\
1879 -Wno-comment{s} Do not warn about comments\n\
1880 -Wtraditional Warn about features not present in traditional C\n\
1881 -Wno-traditional Do not warn about traditional C\n\
1882 -Wundef Warn if an undefined macro is used by #if\n\
1883 -Wno-undef Do not warn about testing undefined macros\n\
1884 -Wimport Warn about the use of the #import directive\n\
1887 -Wno-import Do not warn about the use of #import\n\
1888 -Werror Treat all warnings as errors\n\
1889 -Wno-error Do not treat warnings as errors\n\
1890 -Wsystem-headers Do not suppress warnings from system headers\n\
1891 -Wno-system-headers Suppress warnings from system headers\n\
1892 -Wall Enable all preprocessor warnings\n\
1895 -M Generate make dependencies\n\
1896 -MM As -M, but ignore system header files\n\
1897 -MF <file> Write dependency output to the given file\n\
1898 -MG Treat missing header file as generated files\n\
1901 -MP Generate phony targets for all headers\n\
1902 -MQ <target> Add a MAKE-quoted target\n\
1903 -MT <target> Add an unquoted target\n\
1906 -D<macro> Define a <macro> with string '1' as its value\n\
1907 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1908 -A<question> (<answer>) Assert the <answer> to <question>\n\
1909 -A-<question> (<answer>) Disable the <answer> to <question>\n\
1910 -U<macro> Undefine <macro> \n\
1911 -v Display the version number\n\
1914 -H Print the name of header files as they are used\n\
1915 -C Do not discard comments\n\
1916 -dM Display a list of macro definitions active at end\n\
1917 -dD Preserve macro definitions in output\n\
1918 -dN As -dD except that only the names are preserved\n\
1919 -dI Include #include directives in the output\n\
1922 -fpreprocessed Treat the input file as already preprocessed\n\
1923 -ftabstop=<number> Distance between tab stops for column reporting\n\
1924 -P Do not generate #line directives\n\
1925 -$ Do not allow '$' in identifiers\n\
1926 -remap Remap file names when including files.\n\
1927 --version Display version information\n\
1928 -h or --help Display this information\n\