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"
32 /* Predefined symbols, built-in macros, and the default include path. */
34 #ifndef GET_ENV_PATH_LIST
35 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
38 /* Windows does not natively support inodes, and neither does MSDOS.
39 Cygwin's emulation can generate non-unique inodes, so don't use it.
40 VMS has non-numeric inodes. */
42 # define INO_T_EQ(A, B) (!memcmp (&(A), &(B), sizeof (A)))
43 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
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))
50 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
53 /* Internal structures and prototypes. */
55 /* 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_read_main_file. 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 mark_named_operators PARAMS ((cpp_reader *));
102 static void append_include_chain PARAMS ((cpp_reader *,
104 static struct search_path * remove_dup_dir PARAMS ((cpp_reader *,
105 struct search_path *));
106 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
107 struct search_path *));
108 static void merge_include_chains PARAMS ((cpp_reader *));
109 static bool push_include PARAMS ((cpp_reader *,
110 struct pending_option *));
111 static void free_chain PARAMS ((struct pending_option *));
112 static void set_lang PARAMS ((cpp_reader *, enum c_lang));
113 static void init_dependency_output PARAMS ((cpp_reader *));
114 static void init_standard_includes PARAMS ((cpp_reader *));
115 static void read_original_filename PARAMS ((cpp_reader *));
116 static void new_pending_directive PARAMS ((struct cpp_pending *,
118 cl_directive_handler));
119 static void output_deps PARAMS ((cpp_reader *));
120 static int parse_option PARAMS ((const char *));
122 /* Fourth argument to append_include_chain: chain to use.
123 Note it's never asked to append to the quote chain. */
124 enum { BRACKET = 0, SYSTEM, AFTER };
126 /* If we have designated initializers (GCC >2.7) these tables can be
127 initialized, constant data. Otherwise, they have to be filled in at
129 #if HAVE_DESIGNATED_INITIALIZERS
131 #define init_trigraph_map() /* Nothing. */
132 #define TRIGRAPH_MAP \
133 __extension__ const uchar _cpp_trigraph_map[UCHAR_MAX + 1] = {
136 #define s(p, v) [p] = v,
140 #define TRIGRAPH_MAP uchar _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
141 static void init_trigraph_map PARAMS ((void)) { \
142 unsigned char *x = _cpp_trigraph_map;
145 #define s(p, v) x[p] = v;
150 s('=', '#') s(')', ']') s('!', '|')
151 s('(', '[') s('\'', '^') s('>', '}')
152 s('/', '\\') s('<', '{') s('-', '~')
159 /* Given a colon-separated list of file names PATH,
160 add all the names to the search path for include files. */
162 path_include (pfile, list, path)
173 /* Find the end of this name. */
175 while (*q != 0 && *q != PATH_SEPARATOR) q++;
178 /* An empty name in the path stands for the current directory. */
179 name = (char *) xmalloc (2);
185 /* Otherwise use the directory that is named. */
186 name = (char *) xmalloc (q - p + 1);
187 memcpy (name, p, q - p);
191 append_include_chain (pfile, name, path, 0);
193 /* Advance past this name. */
201 /* Append DIR to include path PATH. DIR must be allocated on the
202 heap; this routine takes responsibility for freeing it. CXX_AWARE
203 is non-zero if the header contains extern "C" guards for C++,
204 otherwise it is zero. */
206 append_include_chain (pfile, dir, path, cxx_aware)
212 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
213 struct search_path *new;
222 _cpp_simplify_pathname (dir);
226 /* Dirs that don't exist are silently ignored. */
228 cpp_errno (pfile, DL_ERROR, dir);
229 else if (CPP_OPTION (pfile, verbose))
230 fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
235 if (!S_ISDIR (st.st_mode))
237 cpp_error_with_line (pfile, DL_ERROR, 0, 0, "%s: Not a directory", dir);
243 if (len > pfile->max_include_len)
244 pfile->max_include_len = len;
246 new = (struct search_path *) xmalloc (sizeof (struct search_path));
249 INO_T_COPY (new->ino, st.st_ino);
250 new->dev = st.st_dev;
251 /* Both systm and after include file lists should be treated as system
252 include files since these two lists are really just a concatenation
253 of one "system" list. */
254 if (path == SYSTEM || path == AFTER)
255 new->sysp = cxx_aware ? 1 : 2;
258 new->name_map = NULL;
263 case BRACKET: APPEND (pend, brack, new); break;
264 case SYSTEM: APPEND (pend, systm, new); break;
265 case AFTER: APPEND (pend, after, new); break;
269 /* Handle a duplicated include path. PREV is the link in the chain
270 before the duplicate. The duplicate is removed from the chain and
271 freed. Returns PREV. */
272 static struct search_path *
273 remove_dup_dir (pfile, prev)
275 struct search_path *prev;
277 struct search_path *cur = prev->next;
279 if (CPP_OPTION (pfile, verbose))
280 fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
282 prev->next = cur->next;
283 free ((PTR) cur->name);
289 /* Remove duplicate directories from a chain. Returns the tail of the
290 chain, or NULL if the chain is empty. This algorithm is quadratic
291 in the number of -I switches, which is acceptable since there
292 aren't usually that many of them. */
293 static struct search_path *
294 remove_dup_dirs (pfile, head)
296 struct search_path *head;
298 struct search_path *prev = NULL, *cur, *other;
300 for (cur = head; cur; cur = cur->next)
302 for (other = head; other != cur; other = other->next)
303 if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
305 if (cur->sysp && !other->sysp)
307 cpp_error (pfile, DL_WARNING,
308 "changing search order for system directory \"%s\"",
310 if (strcmp (cur->name, other->name))
311 cpp_error (pfile, DL_WARNING,
312 " as it is the same as non-system directory \"%s\"",
315 cpp_error (pfile, DL_WARNING,
316 " as it has already been specified as a non-system directory");
318 cur = remove_dup_dir (pfile, prev);
327 /* Merge the four include chains together in the order quote, bracket,
328 system, after. Remove duplicate dirs (as determined by
329 INO_T_EQ()). The system_include and after_include chains are never
330 referred to again after this function; all access is through the
331 bracket_include path. */
333 merge_include_chains (pfile)
336 struct search_path *quote, *brack, *systm, *qtail;
338 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
340 quote = pend->quote_head;
341 brack = pend->brack_head;
342 systm = pend->systm_head;
343 qtail = pend->quote_tail;
345 /* Paste together bracket, system, and after include chains. */
347 pend->systm_tail->next = pend->after_head;
349 systm = pend->after_head;
352 pend->brack_tail->next = systm;
356 /* This is a bit tricky. First we drop dupes from the quote-include
357 list. Then we drop dupes from the bracket-include list.
358 Finally, if qtail and brack are the same directory, we cut out
359 brack and move brack up to point to qtail.
361 We can't just merge the lists and then uniquify them because
362 then we may lose directories from the <> search path that should
363 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
364 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
365 -Ibar -I- -Ifoo -Iquux. */
367 remove_dup_dirs (pfile, brack);
368 qtail = remove_dup_dirs (pfile, quote);
374 /* If brack == qtail, remove brack as it's simpler. */
375 if (brack && INO_T_EQ (qtail->ino, brack->ino)
376 && qtail->dev == brack->dev)
377 brack = remove_dup_dir (pfile, qtail);
382 CPP_OPTION (pfile, quote_include) = quote;
383 CPP_OPTION (pfile, bracket_include) = brack;
386 /* A set of booleans indicating what CPP features each source language
392 char extended_numbers;
394 char dollars_in_ident;
395 char cplusplus_comments;
399 /* ??? Enable $ in identifiers in assembly? */
400 static const struct lang_flags lang_defaults[] =
401 { /* c99 c++ xnum trig dollar c++comm digr */
402 /* GNUC89 */ { 0, 0, 1, 0, 1, 1, 1 },
403 /* GNUC99 */ { 1, 0, 1, 0, 1, 1, 1 },
404 /* STDC89 */ { 0, 0, 0, 1, 0, 0, 0 },
405 /* STDC94 */ { 0, 0, 0, 1, 0, 0, 1 },
406 /* STDC99 */ { 1, 0, 1, 1, 0, 1, 1 },
407 /* GNUCXX */ { 0, 1, 1, 0, 1, 1, 1 },
408 /* CXX98 */ { 0, 1, 1, 1, 0, 1, 1 },
409 /* ASM */ { 0, 0, 1, 0, 0, 1, 0 }
412 /* Sets internal flags correctly for a given language. */
414 set_lang (pfile, lang)
418 const struct lang_flags *l = &lang_defaults[(int) lang];
420 CPP_OPTION (pfile, lang) = lang;
422 CPP_OPTION (pfile, c99) = l->c99;
423 CPP_OPTION (pfile, cplusplus) = l->cplusplus;
424 CPP_OPTION (pfile, extended_numbers) = l->extended_numbers;
425 CPP_OPTION (pfile, trigraphs) = l->trigraphs;
426 CPP_OPTION (pfile, dollars_in_ident) = l->dollars_in_ident;
427 CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
428 CPP_OPTION (pfile, digraphs) = l->digraphs;
432 static int opt_comp PARAMS ((const void *, const void *));
434 /* Run-time sorting of options array. */
439 return strcmp (((struct cl_option *) p1)->opt_text,
440 ((struct cl_option *) p2)->opt_text);
444 /* init initializes library global state. It might not need to
445 do anything depending on the platform and compiler. */
449 static int initialized = 0;
456 /* For non-ASCII hosts, the cl_options array needs to be sorted at
458 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
461 /* Set up the trigraph map. This doesn't need to do anything if
462 we were compiled with a compiler that supports C99 designated
464 init_trigraph_map ();
468 /* Initialize a cpp_reader structure. */
470 cpp_create_reader (lang)
475 /* Initialise this instance of the library if it hasn't been already. */
478 pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
480 set_lang (pfile, lang);
481 CPP_OPTION (pfile, warn_import) = 1;
482 CPP_OPTION (pfile, warn_multichar) = 1;
483 CPP_OPTION (pfile, discard_comments) = 1;
484 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 1;
485 CPP_OPTION (pfile, show_column) = 1;
486 CPP_OPTION (pfile, tabstop) = 8;
487 CPP_OPTION (pfile, operator_names) = 1;
488 CPP_OPTION (pfile, warn_endif_labels) = 1;
490 CPP_OPTION (pfile, pending) =
491 (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
493 /* Default CPP arithmetic to something sensible for the host for the
494 benefit of dumb users like fix-header. */
495 CPP_OPTION (pfile, precision) = CHAR_BIT * sizeof (long);
496 CPP_OPTION (pfile, char_precision) = CHAR_BIT;
497 CPP_OPTION (pfile, wchar_precision) = CHAR_BIT * sizeof (int);
498 CPP_OPTION (pfile, int_precision) = CHAR_BIT * sizeof (int);
499 CPP_OPTION (pfile, unsigned_char) = 0;
500 CPP_OPTION (pfile, unsigned_wchar) = 1;
502 /* It's simplest to just create this struct whether or not it will
504 pfile->deps = deps_init ();
506 /* Initialise the line map. Start at logical line 1, so we can use
507 a line number of zero for special states. */
508 init_line_maps (&pfile->line_maps);
509 pfile->trad_line = pfile->line = 1;
511 /* Initialize lexer state. */
512 pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
514 /* Set up static tokens. */
515 pfile->date.type = CPP_EOF;
516 pfile->avoid_paste.type = CPP_PADDING;
517 pfile->avoid_paste.val.source = NULL;
518 pfile->eof.type = CPP_EOF;
519 pfile->eof.flags = 0;
521 /* Create a token buffer for the lexer. */
522 _cpp_init_tokenrun (&pfile->base_run, 250);
523 pfile->cur_run = &pfile->base_run;
524 pfile->cur_token = pfile->base_run.base;
526 /* Initialise the base context. */
527 pfile->context = &pfile->base_context;
528 pfile->base_context.macro = 0;
529 pfile->base_context.prev = pfile->base_context.next = 0;
531 /* Aligned and unaligned storage. */
532 pfile->a_buff = _cpp_get_buff (pfile, 0);
533 pfile->u_buff = _cpp_get_buff (pfile, 0);
535 /* The expression parser stack. */
536 _cpp_expand_op_stack (pfile);
538 /* Initialise the buffer obstack. */
539 gcc_obstack_init (&pfile->buffer_ob);
541 _cpp_init_includes (pfile);
546 /* Free resources used by PFILE. Accessing PFILE after this function
547 returns leads to undefined behaviour. Returns the error count. */
553 struct search_path *dir, *dirn;
554 cpp_context *context, *contextn;
555 tokenrun *run, *runn;
557 free_chain (CPP_OPTION (pfile, pending)->include_head);
558 free (CPP_OPTION (pfile, pending));
559 free (pfile->op_stack);
561 while (CPP_BUFFER (pfile) != NULL)
562 _cpp_pop_buffer (pfile);
564 if (pfile->trad_out_base)
565 free (pfile->trad_out_base);
567 if (pfile->macro_buffer)
569 free ((PTR) pfile->macro_buffer);
570 pfile->macro_buffer = NULL;
571 pfile->macro_buffer_len = 0;
574 deps_free (pfile->deps);
575 obstack_free (&pfile->buffer_ob, 0);
577 _cpp_destroy_hashtable (pfile);
578 _cpp_cleanup_includes (pfile);
580 _cpp_free_buff (pfile->a_buff);
581 _cpp_free_buff (pfile->u_buff);
582 _cpp_free_buff (pfile->free_buffs);
584 for (run = &pfile->base_run; run; run = runn)
588 if (run != &pfile->base_run)
592 for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
595 free ((PTR) dir->name);
599 for (context = pfile->base_context.next; context; context = contextn)
601 contextn = context->next;
605 free_line_maps (&pfile->line_maps);
607 result = pfile->errors;
613 /* This structure defines one built-in identifier. A node will be
614 entered in the hash table under the name NAME, with value VALUE.
616 There are two tables of these. builtin_array holds all the
617 "builtin" macros: these are handled by builtin_macro() in
618 cppmacro.c. Builtin is somewhat of a misnomer -- the property of
619 interest is that these macros require special code to compute their
620 expansions. The value is a "builtin_type" enumerator.
622 operator_array holds the C++ named operators. These are keywords
623 which act as aliases for punctuators. In C++, they cannot be
624 altered through #define, and #if recognizes them as operators. In
625 C, these are not entered into the hash table at all (but see
626 <iso646.h>). The value is a token-type enumerator. */
631 unsigned short value;
634 #define B(n, t) { DSC(n), t }
635 static const struct builtin builtin_array[] =
637 B("__TIME__", BT_TIME),
638 B("__DATE__", BT_DATE),
639 B("__FILE__", BT_FILE),
640 B("__BASE_FILE__", BT_BASE_FILE),
641 B("__LINE__", BT_SPECLINE),
642 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
643 B("_Pragma", BT_PRAGMA),
644 B("__STDC__", BT_STDC),
647 static const struct builtin operator_array[] =
649 B("and", CPP_AND_AND),
650 B("and_eq", CPP_AND_EQ),
651 B("bitand", CPP_AND),
653 B("compl", CPP_COMPL),
655 B("not_eq", CPP_NOT_EQ),
657 B("or_eq", CPP_OR_EQ),
659 B("xor_eq", CPP_XOR_EQ)
663 /* Mark the C++ named operators in the hash table. */
665 mark_named_operators (pfile)
668 const struct builtin *b;
670 for (b = operator_array;
671 b < (operator_array + ARRAY_SIZE (operator_array));
674 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
675 hp->flags |= NODE_OPERATOR;
676 hp->value.operator = b->value;
680 /* Subroutine of cpp_read_main_file; reads the builtins table above and
681 enters them, and language-specific macros, into the hash table. */
683 init_builtins (pfile)
686 const struct builtin *b;
688 for(b = builtin_array;
689 b < (builtin_array + ARRAY_SIZE (builtin_array));
692 cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
694 hp->flags |= NODE_BUILTIN | NODE_WARN;
695 hp->value.builtin = b->value;
698 if (CPP_OPTION (pfile, cplusplus))
699 _cpp_define_builtin (pfile, "__cplusplus 1");
700 else if (CPP_OPTION (pfile, objc))
701 _cpp_define_builtin (pfile, "__OBJC__ 1");
702 else if (CPP_OPTION (pfile, lang) == CLK_ASM)
703 _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
705 if (CPP_OPTION (pfile, lang) == CLK_STDC94)
706 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
707 else if (CPP_OPTION (pfile, c99))
708 _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
710 if (pfile->cb.register_builtins)
711 (*pfile->cb.register_builtins) (pfile);
714 /* And another subroutine. This one sets up the standard include path. */
716 init_standard_includes (pfile)
720 const struct default_include *p;
721 const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
723 /* Several environment variables may add to the include search path.
724 CPATH specifies an additional list of directories to be searched
725 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
726 etc. specify an additional list of directories to be searched as
727 if specified with -isystem, for the language indicated. */
729 GET_ENV_PATH_LIST (path, "CPATH");
730 if (path != 0 && *path != 0)
731 path_include (pfile, path, BRACKET);
733 switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
736 GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
739 GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
742 GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
745 GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
748 if (path != 0 && *path != 0)
749 path_include (pfile, path, SYSTEM);
751 /* Search "translated" versions of GNU directories.
752 These have /usr/local/lib/gcc... replaced by specd_prefix. */
753 if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
755 /* Remove the `include' from /usr/local/lib/gcc.../include.
756 GCC_INCLUDE_DIR will always end in /include. */
757 int default_len = cpp_GCC_INCLUDE_DIR_len;
758 char *default_prefix = (char *) alloca (default_len + 1);
759 int specd_len = strlen (specd_prefix);
761 memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
762 default_prefix[default_len] = '\0';
764 for (p = cpp_include_defaults; p->fname; p++)
766 /* Some standard dirs are only for C++. */
768 || (CPP_OPTION (pfile, cplusplus)
769 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
771 /* Does this dir start with the prefix? */
772 if (!memcmp (p->fname, default_prefix, default_len))
774 /* Yes; change prefix and add to search list. */
775 int flen = strlen (p->fname);
776 int this_len = specd_len + flen - default_len;
777 char *str = (char *) xmalloc (this_len + 1);
778 memcpy (str, specd_prefix, specd_len);
779 memcpy (str + specd_len,
780 p->fname + default_len,
781 flen - default_len + 1);
783 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
789 /* Search ordinary names for GNU include directories. */
790 for (p = cpp_include_defaults; p->fname; p++)
792 /* Some standard dirs are only for C++. */
794 || (CPP_OPTION (pfile, cplusplus)
795 && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
797 char *str = update_path (p->fname, p->component);
798 append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
803 /* Pushes a command line -imacro and -include file indicated by P onto
804 the buffer stack. Returns non-zero if successful. */
806 push_include (pfile, p)
808 struct pending_option *p;
812 /* Later: maybe update this to use the #include "" search path
813 if cpp_read_file fails. */
814 header.type = CPP_STRING;
815 header.val.str.text = (const unsigned char *) p->arg;
816 header.val.str.len = strlen (p->arg);
817 /* Make the command line directive take up a line. */
820 return _cpp_execute_include (pfile, &header, IT_CMDLINE);
823 /* Frees a pending_option chain. */
826 struct pending_option *head;
828 struct pending_option *next;
838 /* Sanity-checks are dependent on command-line options, so it is
839 called as a subroutine of cpp_read_main_file (). */
841 static void sanity_checks PARAMS ((cpp_reader *));
842 static void sanity_checks (pfile)
846 size_t max_precision = 2 * CHAR_BIT * sizeof (cpp_num_part);
848 /* Sanity checks for assumptions about CPP arithmetic and target
849 type precisions made by cpplib. */
852 cpp_error (pfile, DL_ICE, "cppchar_t must be an unsigned type");
854 if (CPP_OPTION (pfile, precision) > max_precision)
855 cpp_error (pfile, DL_ICE,
856 "preprocessor arithmetic has maximum precision of %lu bits; target requires %lu bits",
857 (unsigned long) max_precision,
858 (unsigned long) CPP_OPTION (pfile, precision));
860 if (CPP_OPTION (pfile, precision) < CPP_OPTION (pfile, int_precision))
861 cpp_error (pfile, DL_ICE,
862 "CPP arithmetic must be at least as precise as a target int");
864 if (CPP_OPTION (pfile, char_precision) < 8)
865 cpp_error (pfile, DL_ICE, "target char is less than 8 bits wide");
867 if (CPP_OPTION (pfile, wchar_precision) < CPP_OPTION (pfile, char_precision))
868 cpp_error (pfile, DL_ICE,
869 "target wchar_t is narrower than target char");
871 if (CPP_OPTION (pfile, int_precision) < CPP_OPTION (pfile, char_precision))
872 cpp_error (pfile, DL_ICE,
873 "target int is narrower than target char");
875 /* This is assumed in eval_token() and could be fixed if necessary. */
876 if (sizeof (cppchar_t) > sizeof (cpp_num_part))
877 cpp_error (pfile, DL_ICE, "CPP half-integer narrower than CPP character");
879 if (CPP_OPTION (pfile, wchar_precision) > BITS_PER_CPPCHAR_T)
880 cpp_error (pfile, DL_ICE,
881 "CPP on this host cannot handle wide character constants over %lu bits, but the target requires %lu bits",
882 (unsigned long) BITS_PER_CPPCHAR_T,
883 (unsigned long) CPP_OPTION (pfile, wchar_precision));
886 # define sanity_checks(PFILE)
889 /* This is called after options have been parsed, and partially
890 processed. Setup for processing input from the file named FNAME,
891 or stdin if it is the empty string. Return the original filename
892 on success (e.g. foo.i->foo.c), or NULL on failure. */
894 cpp_read_main_file (pfile, fname, table)
899 sanity_checks (pfile);
901 /* The front ends don't set up the hash table until they have
902 finished processing the command line options, so initializing the
903 hashtable is deferred until now. */
904 _cpp_init_hashtable (pfile, table);
906 /* Set up the include search path now. */
907 if (! CPP_OPTION (pfile, no_standard_includes))
908 init_standard_includes (pfile);
910 merge_include_chains (pfile);
912 /* With -v, print the list of dirs to search. */
913 if (CPP_OPTION (pfile, verbose))
915 struct search_path *l;
916 fprintf (stderr, _("#include \"...\" search starts here:\n"));
917 for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
919 if (l == CPP_OPTION (pfile, bracket_include))
920 fprintf (stderr, _("#include <...> search starts here:\n"));
921 fprintf (stderr, " %s\n", l->name);
923 fprintf (stderr, _("End of search list.\n"));
926 if (CPP_OPTION (pfile, print_deps))
927 /* Set the default target (if there is none already). */
928 deps_add_default_target (pfile->deps, fname);
930 /* Open the main input file. */
931 if (!_cpp_read_file (pfile, fname))
934 /* Set this after cpp_post_options so the client can change the
935 option if it wishes, and after stacking the main file so we don't
936 trace the main file. */
937 pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
939 /* For foo.i, read the original filename foo.c now, for the benefit
940 of the front ends. */
941 if (CPP_OPTION (pfile, preprocessed))
942 read_original_filename (pfile);
943 /* Overlay an empty buffer to seed traditional preprocessing. */
944 else if (CPP_OPTION (pfile, traditional))
945 _cpp_overlay_buffer (pfile, U"", 0);
947 return pfile->map->to_file;
950 /* For preprocessed files, if the first tokens are of the form # NUM.
951 handle the directive so we know the original file name. This will
952 generate file_change callbacks, which the front ends must handle
953 appropriately given their state of initialization. */
955 read_original_filename (pfile)
958 const cpp_token *token, *token1;
960 /* Lex ahead; if the first tokens are of the form # NUM, then
961 process the directive, otherwise back up. */
962 token = _cpp_lex_direct (pfile);
963 if (token->type == CPP_HASH)
965 token1 = _cpp_lex_direct (pfile);
966 _cpp_backup_tokens (pfile, 1);
968 /* If it's a #line directive, handle it. */
969 if (token1->type == CPP_NUMBER)
971 _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
976 /* Backup as if nothing happened. */
977 _cpp_backup_tokens (pfile, 1);
980 /* Handle pending command line options: -D, -U, -A, -imacros and
981 -include. This should be called after debugging has been properly
982 set up in the front ends. */
984 cpp_finish_options (pfile)
987 /* Mark named operators before handling command line macros. */
988 if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
989 mark_named_operators (pfile);
991 /* Install builtins and process command line macros etc. in the order
992 they appeared, but only if not already preprocessed. */
993 if (! CPP_OPTION (pfile, preprocessed))
995 struct pending_option *p;
997 _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
998 init_builtins (pfile);
999 _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1000 for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1001 (*p->handler) (pfile, p->arg);
1003 /* Scan -imacros files after -D, -U, but before -include.
1004 pfile->next_include_file is NULL, so _cpp_pop_buffer does not
1005 push -include files. */
1006 for (p = CPP_OPTION (pfile, pending)->imacros_head; p; p = p->next)
1007 if (push_include (pfile, p))
1008 cpp_scan_nooutput (pfile);
1010 pfile->next_include_file = &CPP_OPTION (pfile, pending)->include_head;
1011 _cpp_maybe_push_include_file (pfile);
1014 free_chain (CPP_OPTION (pfile, pending)->imacros_head);
1015 free_chain (CPP_OPTION (pfile, pending)->directive_head);
1018 /* Push the next buffer on the stack given by -include, if any. */
1020 _cpp_maybe_push_include_file (pfile)
1023 if (pfile->next_include_file)
1025 struct pending_option *head = *pfile->next_include_file;
1027 while (head && !push_include (pfile, head))
1031 pfile->next_include_file = &head->next;
1034 /* All done; restore the line map from <command line>. */
1035 _cpp_do_file_change (pfile, LC_RENAME,
1036 pfile->line_maps.maps[0].to_file, 1, 0);
1037 /* Don't come back here again. */
1038 pfile->next_include_file = NULL;
1043 /* Use mkdeps.c to output dependency information. */
1048 /* Stream on which to print the dependency information. */
1049 FILE *deps_stream = 0;
1050 const char *const deps_mode =
1051 CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1053 if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1054 deps_stream = stdout;
1057 deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1058 if (deps_stream == 0)
1060 cpp_errno (pfile, DL_ERROR, CPP_OPTION (pfile, deps_file));
1065 deps_write (pfile->deps, deps_stream, 72);
1067 if (CPP_OPTION (pfile, deps_phony_targets))
1068 deps_phony_targets (pfile->deps, deps_stream);
1070 /* Don't close stdout. */
1071 if (deps_stream != stdout)
1073 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1074 cpp_error (pfile, DL_ERROR, "I/O error on output");
1078 /* This is called at the end of preprocessing. It pops the
1079 last buffer and writes dependency output. It should also
1080 clear macro definitions, such that you could call cpp_start_read
1081 with a new filename to restart processing. */
1086 /* cpplex.c leaves the final buffer on the stack. This it so that
1087 it returns an unending stream of CPP_EOFs to the client. If we
1088 popped the buffer, we'd dereference a NULL buffer pointer and
1089 segfault. It's nice to allow the client to do worry-free excess
1090 cpp_get_token calls. */
1091 while (pfile->buffer)
1092 _cpp_pop_buffer (pfile);
1094 /* Don't write the deps file if preprocessing has failed. */
1095 if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1096 output_deps (pfile);
1098 /* Report on headers that could use multiple include guards. */
1099 if (CPP_OPTION (pfile, print_include_names))
1100 _cpp_report_missing_guards (pfile);
1103 /* Add a directive to be handled later in the initialization phase. */
1105 new_pending_directive (pend, text, handler)
1106 struct cpp_pending *pend;
1108 cl_directive_handler handler;
1110 struct pending_option *o = (struct pending_option *)
1111 xmalloc (sizeof (struct pending_option));
1115 o->handler = handler;
1116 APPEND (pend, directive, o);
1119 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1120 I.e. a const string initializer with parens around it. That is
1121 what N_("string") resolves to, so we make no_* be macros instead. */
1122 #define no_arg N_("argument missing after %s")
1123 #define no_ass N_("assertion missing after %s")
1124 #define no_dir N_("directory name missing after %s")
1125 #define no_fil N_("file name missing after %s")
1126 #define no_mac N_("macro name missing after %s")
1127 #define no_pth N_("path name missing after %s")
1128 #define no_num N_("number missing after %s")
1129 #define no_tgt N_("target missing after %s")
1131 /* This is the list of all command line options, with the leading
1132 "-" removed. It must be sorted in ASCII collating order. */
1133 #define COMMAND_LINE_OPTIONS \
1134 DEF_OPT("$", 0, OPT_dollar) \
1135 DEF_OPT("-help", 0, OPT__help) \
1136 DEF_OPT("-target-help", 0, OPT_target__help) \
1137 DEF_OPT("-version", 0, OPT__version) \
1138 DEF_OPT("A", no_ass, OPT_A) \
1139 DEF_OPT("C", 0, OPT_C) \
1140 DEF_OPT("CC", 0, OPT_CC) \
1141 DEF_OPT("D", no_mac, OPT_D) \
1142 DEF_OPT("H", 0, OPT_H) \
1143 DEF_OPT("I", no_dir, OPT_I) \
1144 DEF_OPT("M", 0, OPT_M) \
1145 DEF_OPT("MD", no_fil, OPT_MD) \
1146 DEF_OPT("MF", no_fil, OPT_MF) \
1147 DEF_OPT("MG", 0, OPT_MG) \
1148 DEF_OPT("MM", 0, OPT_MM) \
1149 DEF_OPT("MMD", no_fil, OPT_MMD) \
1150 DEF_OPT("MP", 0, OPT_MP) \
1151 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1152 DEF_OPT("MT", no_tgt, OPT_MT) \
1153 DEF_OPT("P", 0, OPT_P) \
1154 DEF_OPT("U", no_mac, OPT_U) \
1155 DEF_OPT("Wall", 0, OPT_Wall) \
1156 DEF_OPT("Wcomment", 0, OPT_Wcomment) \
1157 DEF_OPT("Wcomments", 0, OPT_Wcomments) \
1158 DEF_OPT("Wendif-labels", 0, OPT_Wendif_labels) \
1159 DEF_OPT("Werror", 0, OPT_Werror) \
1160 DEF_OPT("Wimport", 0, OPT_Wimport) \
1161 DEF_OPT("Wno-comment", 0, OPT_Wno_comment) \
1162 DEF_OPT("Wno-comments", 0, OPT_Wno_comments) \
1163 DEF_OPT("Wno-endif-labels", 0, OPT_Wno_endif_labels) \
1164 DEF_OPT("Wno-error", 0, OPT_Wno_error) \
1165 DEF_OPT("Wno-import", 0, OPT_Wno_import) \
1166 DEF_OPT("Wno-system-headers", 0, OPT_Wno_system_headers) \
1167 DEF_OPT("Wno-traditional", 0, OPT_Wno_traditional) \
1168 DEF_OPT("Wno-trigraphs", 0, OPT_Wno_trigraphs) \
1169 DEF_OPT("Wno-undef", 0, OPT_Wno_undef) \
1170 DEF_OPT("Wsystem-headers", 0, OPT_Wsystem_headers) \
1171 DEF_OPT("Wtraditional", 0, OPT_Wtraditional) \
1172 DEF_OPT("Wtrigraphs", 0, OPT_Wtrigraphs) \
1173 DEF_OPT("Wundef", 0, OPT_Wundef) \
1174 DEF_OPT("d", no_arg, OPT_d) \
1175 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1176 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1177 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1178 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1179 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1180 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1181 DEF_OPT("h", 0, OPT_h) \
1182 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1183 DEF_OPT("imacros", no_fil, OPT_imacros) \
1184 DEF_OPT("include", no_fil, OPT_include) \
1185 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1186 DEF_OPT("isystem", no_dir, OPT_isystem) \
1187 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1188 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1189 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1190 DEF_OPT("lang-c", 0, OPT_lang_c) \
1191 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1192 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1193 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1194 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1195 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1196 DEF_OPT("o", no_fil, OPT_o) \
1197 DEF_OPT("pedantic", 0, OPT_pedantic) \
1198 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1199 DEF_OPT("remap", 0, OPT_remap) \
1200 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1201 DEF_OPT("std=c89", 0, OPT_std_c89) \
1202 DEF_OPT("std=c99", 0, OPT_std_c99) \
1203 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1204 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1205 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1206 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1207 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1208 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1209 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1210 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1211 DEF_OPT("traditional-cpp", 0, OPT_traditional_cpp) \
1212 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1213 DEF_OPT("v", 0, OPT_v) \
1214 DEF_OPT("w", 0, OPT_w)
1216 #define DEF_OPT(text, msg, code) code,
1219 COMMAND_LINE_OPTIONS
1226 const char *opt_text;
1229 enum opt_code opt_code;
1232 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1234 static struct cl_option cl_options[] =
1236 static const struct cl_option cl_options[] =
1239 COMMAND_LINE_OPTIONS
1242 #undef COMMAND_LINE_OPTIONS
1244 /* Perform a binary search to find which, if any, option the given
1245 command-line matches. Returns its index in the option array,
1246 negative on failure. Complications arise since some options can be
1247 suffixed with an argument, and multiple complete matches can occur,
1248 e.g. -pedantic and -pedantic-errors. */
1250 parse_option (input)
1253 unsigned int md, mn, mx;
1264 opt_len = cl_options[md].opt_len;
1265 comp = memcmp (input, cl_options[md].opt_text, opt_len);
1273 if (input[opt_len] == '\0')
1275 /* We were passed more text. If the option takes an argument,
1276 we may match a later option or we may have been passed the
1277 argument. The longest possible option match succeeds.
1278 If the option takes no arguments we have not matched and
1279 continue the search (e.g. input="stdc++" match was "stdc"). */
1281 if (cl_options[md].msg)
1283 /* Scan forwards. If we get an exact match, return it.
1284 Otherwise, return the longest option-accepting match.
1285 This loops no more than twice with current options. */
1287 for (; mn < (unsigned int) N_OPTS; mn++)
1289 opt_len = cl_options[mn].opt_len;
1290 if (memcmp (input, cl_options[mn].opt_text, opt_len))
1292 if (input[opt_len] == '\0')
1294 if (cl_options[mn].msg)
1305 /* Handle one command-line option in (argc, argv).
1306 Can be called multiple times, to handle multiple sets of options.
1307 Returns number of strings consumed. */
1309 cpp_handle_option (pfile, argc, argv)
1315 struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1317 /* Interpret "-" or a non-option as a file name. */
1318 if (argv[i][0] != '-' || argv[i][1] == '\0')
1320 if (CPP_OPTION (pfile, in_fname) == NULL)
1321 CPP_OPTION (pfile, in_fname) = argv[i];
1322 else if (CPP_OPTION (pfile, out_fname) == NULL)
1323 CPP_OPTION (pfile, out_fname) = argv[i];
1325 cpp_error (pfile, DL_ERROR,
1326 "too many filenames. Type %s --help for usage info",
1331 enum opt_code opt_code;
1333 const char *arg = 0;
1335 /* Skip over '-'. */
1336 opt_index = parse_option (&argv[i][1]);
1340 opt_code = cl_options[opt_index].opt_code;
1341 if (cl_options[opt_index].msg)
1343 arg = &argv[i][cl_options[opt_index].opt_len + 1];
1349 cpp_error (pfile, DL_ERROR,
1350 cl_options[opt_index].msg, argv[i - 1]);
1358 case N_OPTS: /* Shut GCC up. */
1360 case OPT_fno_operator_names:
1361 CPP_OPTION (pfile, operator_names) = 0;
1363 case OPT_fpreprocessed:
1364 CPP_OPTION (pfile, preprocessed) = 1;
1366 case OPT_fno_preprocessed:
1367 CPP_OPTION (pfile, preprocessed) = 0;
1369 case OPT_fshow_column:
1370 CPP_OPTION (pfile, show_column) = 1;
1372 case OPT_fno_show_column:
1373 CPP_OPTION (pfile, show_column) = 0;
1376 /* Silently ignore empty string, non-longs and silly values. */
1380 long tabstop = strtol (arg, &endptr, 10);
1381 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1382 CPP_OPTION (pfile, tabstop) = tabstop;
1386 CPP_OPTION (pfile, inhibit_warnings) = 1;
1392 case OPT_target__help:
1394 /* Nothing to do for these cases, but we need to be sure
1395 help_only is set. */
1396 CPP_OPTION (pfile, help_only) = 1;
1399 CPP_OPTION (pfile, verbose) = 1;
1403 CPP_OPTION (pfile, discard_comments) = 0;
1406 CPP_OPTION (pfile, discard_comments) = 0;
1407 CPP_OPTION (pfile, discard_comments_in_macro_exp) = 0;
1410 CPP_OPTION (pfile, no_line_commands) = 1;
1412 case OPT_dollar: /* Don't include $ in identifiers. */
1413 CPP_OPTION (pfile, dollars_in_ident) = 0;
1416 CPP_OPTION (pfile, print_include_names) = 1;
1419 new_pending_directive (pend, arg, cpp_define);
1421 case OPT_pedantic_errors:
1422 CPP_OPTION (pfile, pedantic_errors) = 1;
1425 CPP_OPTION (pfile, pedantic) = 1;
1426 CPP_OPTION (pfile, warn_endif_labels) = 1;
1429 CPP_OPTION (pfile, trigraphs) = 1;
1432 CPP_OPTION (pfile, remap) = 1;
1434 case OPT_traditional_cpp:
1435 CPP_OPTION (pfile, traditional) = 1;
1438 CPP_OPTION (pfile, include_prefix) = arg;
1439 CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1442 set_lang (pfile, CLK_GNUC89);
1444 case OPT_lang_cplusplus:
1445 set_lang (pfile, CLK_GNUCXX);
1448 CPP_OPTION (pfile, objc) = 1;
1451 set_lang (pfile, CLK_ASM);
1453 case OPT_std_cplusplus98:
1454 set_lang (pfile, CLK_CXX98);
1457 set_lang (pfile, CLK_GNUC89);
1461 set_lang (pfile, CLK_GNUC99);
1463 case OPT_std_iso9899_199409:
1464 set_lang (pfile, CLK_STDC94);
1466 case OPT_std_iso9899_1990:
1469 set_lang (pfile, CLK_STDC89);
1471 case OPT_std_iso9899_199x:
1472 case OPT_std_iso9899_1999:
1475 set_lang (pfile, CLK_STDC99);
1478 /* -nostdinc causes no default include directories.
1479 You must specify all include-file directories with -I. */
1480 CPP_OPTION (pfile, no_standard_includes) = 1;
1482 case OPT_nostdincplusplus:
1483 /* -nostdinc++ causes no default C++-specific include directories. */
1484 CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1487 if (CPP_OPTION (pfile, out_fname) == NULL)
1488 CPP_OPTION (pfile, out_fname) = arg;
1491 cpp_error (pfile, DL_ERROR, "output filename specified twice");
1496 /* Args to -d specify what parts of macros to dump.
1497 Silently ignore unrecognised options; they may
1498 be aimed at the compiler proper. */
1502 while ((c = *arg++) != '\0')
1506 CPP_OPTION (pfile, dump_macros) = dump_only;
1509 CPP_OPTION (pfile, dump_macros) = dump_names;
1512 CPP_OPTION (pfile, dump_macros) = dump_definitions;
1515 CPP_OPTION (pfile, dump_includes) = 1;
1522 CPP_OPTION (pfile, print_deps_missing_files) = 1;
1525 /* When doing dependencies with -M or -MM, suppress normal
1526 preprocessed output, but still do -dM etc. as software
1527 depends on this. Preprocessed output occurs if -MD, -MMD
1528 or environment var dependency generation is used. */
1529 CPP_OPTION (pfile, print_deps) = 2;
1530 CPP_OPTION (pfile, no_output) = 1;
1533 CPP_OPTION (pfile, print_deps) = 1;
1534 CPP_OPTION (pfile, no_output) = 1;
1537 CPP_OPTION (pfile, deps_file) = arg;
1540 CPP_OPTION (pfile, deps_phony_targets) = 1;
1544 /* Add a target. -MQ quotes for Make. */
1545 deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1549 CPP_OPTION (pfile, print_deps) = 2;
1550 CPP_OPTION (pfile, deps_file) = arg;
1553 CPP_OPTION (pfile, print_deps) = 1;
1554 CPP_OPTION (pfile, deps_file) = arg;
1560 /* -A with an argument beginning with '-' acts as
1561 #unassert on whatever immediately follows the '-'.
1562 If "-" is the whole argument, we eliminate all
1563 predefined macros and assertions, including those
1564 that were specified earlier on the command line.
1565 That way we can get rid of any that were passed
1566 automatically in from GCC. */
1570 free_chain (pend->directive_head);
1571 pend->directive_head = NULL;
1572 pend->directive_tail = NULL;
1575 new_pending_directive (pend, arg + 1, cpp_unassert);
1578 new_pending_directive (pend, arg, cpp_assert);
1581 new_pending_directive (pend, arg, cpp_undef);
1583 case OPT_I: /* Add directory to path for includes. */
1584 if (!strcmp (arg, "-"))
1587 Use the preceding -I directories for #include "..."
1588 but not #include <...>.
1589 Don't search the directory of the present file
1590 for #include "...". (Note that -I. -I- is not the same as
1591 the default setup; -I. uses the compiler's working dir.) */
1592 if (! CPP_OPTION (pfile, ignore_srcdir))
1594 pend->quote_head = pend->brack_head;
1595 pend->quote_tail = pend->brack_tail;
1596 pend->brack_head = 0;
1597 pend->brack_tail = 0;
1598 CPP_OPTION (pfile, ignore_srcdir) = 1;
1602 cpp_error (pfile, DL_ERROR, "-I- specified twice");
1607 append_include_chain (pfile, xstrdup (arg), BRACKET, 0);
1610 /* Add directory to beginning of system include path, as a system
1611 include directory. */
1612 append_include_chain (pfile, xstrdup (arg), SYSTEM, 0);
1617 struct pending_option *o = (struct pending_option *)
1618 xmalloc (sizeof (struct pending_option));
1622 if (opt_code == OPT_include)
1623 APPEND (pend, include, o);
1625 APPEND (pend, imacros, o);
1628 case OPT_iwithprefix:
1629 /* Add directory to end of path for includes,
1630 with the default prefix at the front of its name. */
1632 case OPT_iwithprefixbefore:
1633 /* Add directory to main path for includes,
1634 with the default prefix at the front of its name. */
1641 if (CPP_OPTION (pfile, include_prefix) != 0)
1643 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1644 fname = xmalloc (ipl + len + 1);
1645 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1646 memcpy (fname + ipl, arg, len + 1);
1648 else if (cpp_GCC_INCLUDE_DIR_len)
1650 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1651 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1652 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1655 fname = xstrdup (arg);
1657 append_include_chain (pfile, fname,
1658 opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1662 /* Add directory to end of path for includes. */
1663 append_include_chain (pfile, xstrdup (arg), AFTER, 0);
1667 CPP_OPTION (pfile, warn_trigraphs) = 1;
1668 CPP_OPTION (pfile, warn_comments) = 1;
1671 case OPT_Wtraditional:
1672 CPP_OPTION (pfile, warn_traditional) = 1;
1674 case OPT_Wno_traditional:
1675 CPP_OPTION (pfile, warn_traditional) = 0;
1678 case OPT_Wtrigraphs:
1679 CPP_OPTION (pfile, warn_trigraphs) = 1;
1681 case OPT_Wno_trigraphs:
1682 CPP_OPTION (pfile, warn_trigraphs) = 0;
1687 CPP_OPTION (pfile, warn_comments) = 1;
1689 case OPT_Wno_comment:
1690 case OPT_Wno_comments:
1691 CPP_OPTION (pfile, warn_comments) = 0;
1695 CPP_OPTION (pfile, warn_undef) = 1;
1698 CPP_OPTION (pfile, warn_undef) = 0;
1702 CPP_OPTION (pfile, warn_import) = 1;
1704 case OPT_Wno_import:
1705 CPP_OPTION (pfile, warn_import) = 0;
1708 case OPT_Wendif_labels:
1709 CPP_OPTION (pfile, warn_endif_labels) = 1;
1711 case OPT_Wno_endif_labels:
1712 CPP_OPTION (pfile, warn_endif_labels) = 0;
1716 CPP_OPTION (pfile, warnings_are_errors) = 1;
1719 CPP_OPTION (pfile, warnings_are_errors) = 0;
1722 case OPT_Wsystem_headers:
1723 CPP_OPTION (pfile, warn_system_headers) = 1;
1725 case OPT_Wno_system_headers:
1726 CPP_OPTION (pfile, warn_system_headers) = 0;
1733 /* Handle command-line options in (argc, argv).
1734 Can be called multiple times, to handle multiple sets of options.
1735 Returns if an unrecognized option is seen.
1736 Returns number of strings consumed. */
1738 cpp_handle_options (pfile, argc, argv)
1744 int strings_processed;
1746 for (i = 0; i < argc; i += strings_processed)
1748 strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1749 if (strings_processed == 0)
1756 /* Extra processing when all options are parsed, after all calls to
1757 cpp_handle_option[s]. Consistency checks etc. */
1759 cpp_post_options (pfile)
1762 /* Canonicalize in_fname and out_fname. We guarantee they are not
1763 NULL, and that the empty string represents stdin / stdout. */
1764 if (CPP_OPTION (pfile, in_fname) == NULL
1765 || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1766 CPP_OPTION (pfile, in_fname) = "";
1768 if (CPP_OPTION (pfile, out_fname) == NULL
1769 || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1770 CPP_OPTION (pfile, out_fname) = "";
1772 /* -Wtraditional is not useful in C++ mode. */
1773 if (CPP_OPTION (pfile, cplusplus))
1774 CPP_OPTION (pfile, warn_traditional) = 0;
1776 /* The compiler front ends override this, but I think this is the
1777 appropriate setting for the library. */
1778 CPP_OPTION (pfile, warn_long_long) = (CPP_OPTION (pfile, pedantic)
1779 && !CPP_OPTION (pfile, c99));
1781 /* Permanently disable macro expansion if we are rescanning
1782 preprocessed text. Read preprocesed source in ISO mode. */
1783 if (CPP_OPTION (pfile, preprocessed))
1785 pfile->state.prevent_expansion = 1;
1786 CPP_OPTION (pfile, traditional) = 0;
1789 /* Traditional CPP does not accurately track column information. */
1790 if (CPP_OPTION (pfile, traditional))
1791 CPP_OPTION (pfile, show_column) = 0;
1793 /* -dM makes no normal output. This is set here so that -dM -dD
1794 works as expected. */
1795 if (CPP_OPTION (pfile, dump_macros) == dump_only)
1796 CPP_OPTION (pfile, no_output) = 1;
1798 /* Disable -dD, -dN and -dI if we should make no normal output
1799 (such as with -M). Allow -M -dM since some software relies on
1801 if (CPP_OPTION (pfile, no_output))
1803 if (CPP_OPTION (pfile, dump_macros) != dump_only)
1804 CPP_OPTION (pfile, dump_macros) = dump_none;
1805 CPP_OPTION (pfile, dump_includes) = 0;
1808 /* We need to do this after option processing and before
1809 cpp_start_read, as cppmain.c relies on the options->no_output to
1810 set its callbacks correctly before calling cpp_start_read. */
1811 init_dependency_output (pfile);
1813 /* After checking the environment variables, check if -M or -MM has
1814 not been specified, but other -M options have. */
1815 if (CPP_OPTION (pfile, print_deps) == 0 &&
1816 (CPP_OPTION (pfile, print_deps_missing_files)
1817 || CPP_OPTION (pfile, deps_file)
1818 || CPP_OPTION (pfile, deps_phony_targets)))
1819 cpp_error (pfile, DL_ERROR,
1820 "you must additionally specify either -M or -MM");
1823 /* Set up dependency-file output. On exit, if print_deps is non-zero
1824 then deps_file is not NULL; stdout is the empty string. */
1826 init_dependency_output (pfile)
1829 char *spec, *s, *output_file;
1831 /* Either of two environment variables can specify output of deps.
1832 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1833 where OUTPUT_FILE is the file to write deps info to
1834 and DEPS_TARGET is the target to mention in the deps. */
1836 if (CPP_OPTION (pfile, print_deps) == 0)
1838 spec = getenv ("DEPENDENCIES_OUTPUT");
1840 CPP_OPTION (pfile, print_deps) = 1;
1843 spec = getenv ("SUNPRO_DEPENDENCIES");
1845 CPP_OPTION (pfile, print_deps) = 2;
1850 /* Find the space before the DEPS_TARGET, if there is one. */
1851 s = strchr (spec, ' ');
1854 /* Let the caller perform MAKE quoting. */
1855 deps_add_target (pfile->deps, s + 1, 0);
1856 output_file = (char *) xmalloc (s - spec + 1);
1857 memcpy (output_file, spec, s - spec);
1858 output_file[s - spec] = 0;
1863 /* Command line -MF overrides environment variables and default. */
1864 if (CPP_OPTION (pfile, deps_file) == 0)
1865 CPP_OPTION (pfile, deps_file) = output_file;
1867 CPP_OPTION (pfile, print_deps_append) = 1;
1869 else if (CPP_OPTION (pfile, deps_file) == 0)
1870 /* If -M or -MM was seen without -MF, default output to wherever
1871 was specified with -o. out_fname is non-NULL here. */
1872 CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1875 /* Handle --help output. */
1879 /* To keep the lines from getting too long for some compilers, limit
1880 to about 500 characters (6 lines) per chunk. */
1883 -include <file> Include the contents of <file> before other files\n\
1884 -imacros <file> Accept definition of macros in <file>\n\
1885 -iprefix <path> Specify <path> as a prefix for next two options\n\
1886 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1887 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1888 -isystem <dir> Add <dir> to the start of the system include path\n\
1891 -idirafter <dir> Add <dir> to the end of the system include path\n\
1892 -I <dir> Add <dir> to the end of the main include path\n\
1893 -I- Fine-grained include path control; see info docs\n\
1894 -nostdinc Do not search system include directories\n\
1895 (dirs specified with -isystem will still be used)\n\
1896 -nostdinc++ Do not search system include directories for C++\n\
1897 -o <file> Put output into <file>\n\
1900 -pedantic Issue all warnings demanded by strict ISO C\n\
1901 -pedantic-errors Issue -pedantic warnings as errors instead\n\
1902 -trigraphs Support ISO C trigraphs\n\
1903 -lang-c Assume that the input sources are in C\n\
1904 -lang-c89 Assume that the input sources are in C89\n\
1907 -lang-c++ Assume that the input sources are in C++\n\
1908 -lang-objc Assume that the input sources are in ObjectiveC\n\
1909 -lang-asm Assume that the input sources are in assembler\n\
1912 -std=<std name> Specify the conformance standard; one of:\n\
1913 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1914 iso9899:199409, iso9899:1999\n\
1915 -w Inhibit warning messages\n\
1916 -Wtrigraphs Warn if trigraphs are encountered\n\
1917 -Wno-trigraphs Do not warn about trigraphs\n\
1918 -Wcomment{s} Warn if one comment starts inside another\n\
1921 -Wno-comment{s} Do not warn about comments\n\
1922 -Wtraditional Warn about features not present in traditional C\n\
1923 -Wno-traditional Do not warn about traditional C\n\
1924 -Wundef Warn if an undefined macro is used by #if\n\
1925 -Wno-undef Do not warn about testing undefined macros\n\
1926 -Wimport Warn about the use of the #import directive\n\
1929 -Wno-import Do not warn about the use of #import\n\
1930 -Werror Treat all warnings as errors\n\
1931 -Wno-error Do not treat warnings as errors\n\
1932 -Wsystem-headers Do not suppress warnings from system headers\n\
1933 -Wno-system-headers Suppress warnings from system headers\n\
1934 -Wall Enable all preprocessor warnings\n\
1937 -M Generate make dependencies\n\
1938 -MM As -M, but ignore system header files\n\
1939 -MD Generate make dependencies and compile\n\
1940 -MMD As -MD, but ignore system header files\n\
1941 -MF <file> Write dependency output to the given file\n\
1942 -MG Treat missing header file as generated files\n\
1945 -MP Generate phony targets for all headers\n\
1946 -MQ <target> Add a MAKE-quoted target\n\
1947 -MT <target> Add an unquoted target\n\
1950 -D<macro> Define a <macro> with string '1' as its value\n\
1951 -D<macro>=<val> Define a <macro> with <val> as its value\n\
1952 -A<question>=<answer> Assert the <answer> to <question>\n\
1953 -A-<question>=<answer> Disable the <answer> to <question>\n\
1954 -U<macro> Undefine <macro> \n\
1955 -v Display the version number\n\
1958 -H Print the name of header files as they are used\n\
1959 -C Do not discard comments\n\
1960 -dM Display a list of macro definitions active at end\n\
1961 -dD Preserve macro definitions in output\n\
1962 -dN As -dD except that only the names are preserved\n\
1963 -dI Include #include directives in the output\n\
1966 -fpreprocessed Treat the input file as already preprocessed\n\
1967 -ftabstop=<number> Distance between tab stops for column reporting\n\
1968 -P Do not generate #line directives\n\
1969 -$ Do not allow '$' in identifiers\n\
1970 -remap Remap file names when including files\n\
1971 --version Display version information\n\
1972 -h or --help Display this information\n\