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)))
44 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
46 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
47 # define INO_T_EQ(A, B) 0
49 # define INO_T_EQ(A, B) ((A) == (B))
51 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
54 /* Internal structures and prototypes. */
56 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
59 typedef void (* cl_directive_handler
) PARAMS ((cpp_reader
*, const char *));
62 struct pending_option
*next
;
64 cl_directive_handler handler
;
67 /* The `pending' structure accumulates all the options that are not
68 actually processed until we hit cpp_read_main_file. It consists of
69 several lists, one for each type of option. We keep both head and
70 tail pointers for quick insertion. */
73 struct pending_option
*directive_head
, *directive_tail
;
75 struct search_path
*quote_head
, *quote_tail
;
76 struct search_path
*brack_head
, *brack_tail
;
77 struct search_path
*systm_head
, *systm_tail
;
78 struct search_path
*after_head
, *after_tail
;
80 struct pending_option
*imacros_head
, *imacros_tail
;
81 struct pending_option
*include_head
, *include_tail
;
85 #define APPEND(pend, list, elt) \
86 do { if (!(pend)->list##_head) (pend)->list##_head = (elt); \
87 else (pend)->list##_tail->next = (elt); \
88 (pend)->list##_tail = (elt); \
91 #define APPEND(pend, list, elt) \
92 do { if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
93 else (pend)->list/**/_tail->next = (elt); \
94 (pend)->list/**/_tail = (elt); \
98 static void print_help
PARAMS ((void));
99 static void path_include
PARAMS ((cpp_reader
*,
101 static void init_library
PARAMS ((void));
102 static void init_builtins
PARAMS ((cpp_reader
*));
103 static void append_include_chain
PARAMS ((cpp_reader
*,
105 static struct search_path
* remove_dup_dir
PARAMS ((cpp_reader
*,
106 struct search_path
*));
107 static struct search_path
* remove_dup_dirs
PARAMS ((cpp_reader
*,
108 struct search_path
*));
109 static void merge_include_chains
PARAMS ((cpp_reader
*));
110 static bool push_include
PARAMS ((cpp_reader
*,
111 struct pending_option
*));
112 static void free_chain
PARAMS ((struct pending_option
*));
113 static void set_lang
PARAMS ((cpp_reader
*, enum c_lang
));
114 static void init_dependency_output
PARAMS ((cpp_reader
*));
115 static void init_standard_includes
PARAMS ((cpp_reader
*));
116 static void read_original_filename
PARAMS ((cpp_reader
*));
117 static void new_pending_directive
PARAMS ((struct cpp_pending
*,
119 cl_directive_handler
));
120 static void output_deps
PARAMS ((cpp_reader
*));
121 static int parse_option
PARAMS ((const char *));
123 /* Fourth argument to append_include_chain: chain to use.
124 Note it's never asked to append to the quote chain. */
125 enum { BRACKET
= 0, SYSTEM
, AFTER
};
127 /* If we have designated initializers (GCC >2.7) these tables can be
128 initialized, constant data. Otherwise, they have to be filled in at
130 #if HAVE_DESIGNATED_INITIALIZERS
132 #define init_trigraph_map() /* Nothing. */
133 #define TRIGRAPH_MAP \
134 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
137 #define s(p, v) [p] = v,
141 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
142 static void init_trigraph_map PARAMS ((void)) { \
143 unsigned char *x = _cpp_trigraph_map;
146 #define s(p, v) x[p] = v;
151 s('=', '#') s(')', ']') s('!', '|')
152 s('(', '[') s('\'', '^') s('>', '}')
153 s('/', '\\') s('<', '{') s('-', '~')
160 /* Given a colon-separated list of file names PATH,
161 add all the names to the search path for include files. */
164 path_include (pfile
, list
, path
)
175 /* Find the end of this name. */
177 while (*q
!= 0 && *q
!= PATH_SEPARATOR
) q
++;
180 /* An empty name in the path stands for the current directory. */
181 name
= (char *) xmalloc (2);
187 /* Otherwise use the directory that is named. */
188 name
= (char *) xmalloc (q
- p
+ 1);
189 memcpy (name
, p
, q
- p
);
193 append_include_chain (pfile
, name
, path
, 0);
195 /* Advance past this name. */
203 /* Append DIR to include path PATH. DIR must be allocated on the
204 heap; this routine takes responsibility for freeing it. */
206 append_include_chain (pfile
, dir
, path
, cxx_aware
)
210 int cxx_aware ATTRIBUTE_UNUSED
;
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_notice_from_errno (pfile
, 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_notice (pfile
, "%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 #ifdef NO_IMPLICIT_EXTERN_C
258 new->sysp
= cxx_aware
? 1 : 2;
262 new->name_map
= NULL
;
267 case BRACKET
: APPEND (pend
, brack
, new); break;
268 case SYSTEM
: APPEND (pend
, systm
, new); break;
269 case AFTER
: APPEND (pend
, after
, new); break;
273 /* Handle a duplicated include path. PREV is the link in the chain
274 before the duplicate. The duplicate is removed from the chain and
275 freed. Returns PREV. */
276 static struct search_path
*
277 remove_dup_dir (pfile
, prev
)
279 struct search_path
*prev
;
281 struct search_path
*cur
= prev
->next
;
283 if (CPP_OPTION (pfile
, verbose
))
284 fprintf (stderr
, _("ignoring duplicate directory \"%s\"\n"), cur
->name
);
286 prev
->next
= cur
->next
;
287 free ((PTR
) cur
->name
);
293 /* Remove duplicate directories from a chain. Returns the tail of the
294 chain, or NULL if the chain is empty. This algorithm is quadratic
295 in the number of -I switches, which is acceptable since there
296 aren't usually that many of them. */
297 static struct search_path
*
298 remove_dup_dirs (pfile
, head
)
300 struct search_path
*head
;
302 struct search_path
*prev
= NULL
, *cur
, *other
;
304 for (cur
= head
; cur
; cur
= cur
->next
)
306 for (other
= head
; other
!= cur
; other
= other
->next
)
307 if (INO_T_EQ (cur
->ino
, other
->ino
) && cur
->dev
== other
->dev
)
309 if (cur
->sysp
&& !other
->sysp
)
312 "changing search order for system directory \"%s\"",
314 if (strcmp (cur
->name
, other
->name
))
316 " as it is the same as non-system directory \"%s\"",
320 " as it has already been specified as a non-system directory");
322 cur
= remove_dup_dir (pfile
, prev
);
331 /* Merge the four include chains together in the order quote, bracket,
332 system, after. Remove duplicate dirs (as determined by
333 INO_T_EQ()). The system_include and after_include chains are never
334 referred to again after this function; all access is through the
335 bracket_include path.
337 For the future: Check if the directory is empty (but
338 how?) and possibly preload the include hash. */
341 merge_include_chains (pfile
)
344 struct search_path
*quote
, *brack
, *systm
, *qtail
;
346 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
348 quote
= pend
->quote_head
;
349 brack
= pend
->brack_head
;
350 systm
= pend
->systm_head
;
351 qtail
= pend
->quote_tail
;
353 /* Paste together bracket, system, and after include chains. */
355 pend
->systm_tail
->next
= pend
->after_head
;
357 systm
= pend
->after_head
;
360 pend
->brack_tail
->next
= systm
;
364 /* This is a bit tricky. First we drop dupes from the quote-include
365 list. Then we drop dupes from the bracket-include list.
366 Finally, if qtail and brack are the same directory, we cut out
367 brack and move brack up to point to qtail.
369 We can't just merge the lists and then uniquify them because
370 then we may lose directories from the <> search path that should
371 be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux. It is however
372 safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
373 -Ibar -I- -Ifoo -Iquux. */
375 remove_dup_dirs (pfile
, brack
);
376 qtail
= remove_dup_dirs (pfile
, quote
);
382 /* If brack == qtail, remove brack as it's simpler. */
383 if (INO_T_EQ (qtail
->ino
, brack
->ino
) && qtail
->dev
== brack
->dev
)
384 brack
= remove_dup_dir (pfile
, qtail
);
389 CPP_OPTION (pfile
, quote_include
) = quote
;
390 CPP_OPTION (pfile
, bracket_include
) = brack
;
393 /* Sets internal flags correctly for a given language, and defines
394 macros if necessary. */
401 char extended_numbers
;
403 char dollars_in_ident
;
404 char cplusplus_comments
;
408 /* ??? Enable $ in identifiers in assembly? */
409 static const struct lang_flags lang_defaults
[] =
410 { /* c99 objc c++ xnum trig dollar c++comm digr */
411 /* GNUC89 */ { 0, 0, 0, 1, 0, 1, 1, 1 },
412 /* GNUC99 */ { 1, 0, 0, 1, 0, 1, 1, 1 },
413 /* STDC89 */ { 0, 0, 0, 0, 1, 0, 0, 0 },
414 /* STDC94 */ { 0, 0, 0, 0, 1, 0, 0, 1 },
415 /* STDC99 */ { 1, 0, 0, 1, 1, 0, 1, 1 },
416 /* GNUCXX */ { 0, 0, 1, 1, 0, 1, 1, 1 },
417 /* CXX98 */ { 0, 0, 1, 1, 1, 0, 1, 1 },
418 /* OBJC */ { 0, 1, 0, 1, 0, 1, 1, 1 },
419 /* OBJCXX */ { 0, 1, 1, 1, 0, 1, 1, 1 },
420 /* ASM */ { 0, 0, 0, 1, 0, 0, 1, 0 }
424 set_lang (pfile
, lang
)
428 const struct lang_flags
*l
= &lang_defaults
[(int) lang
];
430 CPP_OPTION (pfile
, lang
) = lang
;
432 CPP_OPTION (pfile
, c99
) = l
->c99
;
433 CPP_OPTION (pfile
, objc
) = l
->objc
;
434 CPP_OPTION (pfile
, cplusplus
) = l
->cplusplus
;
435 CPP_OPTION (pfile
, extended_numbers
) = l
->extended_numbers
;
436 CPP_OPTION (pfile
, trigraphs
) = l
->trigraphs
;
437 CPP_OPTION (pfile
, dollars_in_ident
) = l
->dollars_in_ident
;
438 CPP_OPTION (pfile
, cplusplus_comments
) = l
->cplusplus_comments
;
439 CPP_OPTION (pfile
, digraphs
) = l
->digraphs
;
443 static int opt_comp
PARAMS ((const void *, const void *));
445 /* Run-time sorting of options array. */
450 return strcmp (((struct cl_option
*) p1
)->opt_text
,
451 ((struct cl_option
*) p2
)->opt_text
);
455 /* init initializes library global state. It might not need to
456 do anything depending on the platform and compiler. */
461 static int initialized
= 0;
468 /* For non-ASCII hosts, the cl_options array needs to be sorted at
470 qsort (cl_options
, N_OPTS
, sizeof (struct cl_option
), opt_comp
);
473 /* Set up the trigraph map. This doesn't need to do anything if
474 we were compiled with a compiler that supports C99 designated
476 init_trigraph_map ();
480 /* Initialize a cpp_reader structure. */
482 cpp_create_reader (lang
)
487 /* Initialise this instance of the library if it hasn't been already. */
490 pfile
= (cpp_reader
*) xcalloc (1, sizeof (cpp_reader
));
492 set_lang (pfile
, lang
);
493 CPP_OPTION (pfile
, warn_import
) = 1;
494 CPP_OPTION (pfile
, discard_comments
) = 1;
495 CPP_OPTION (pfile
, show_column
) = 1;
496 CPP_OPTION (pfile
, tabstop
) = 8;
497 CPP_OPTION (pfile
, operator_names
) = 1;
499 CPP_OPTION (pfile
, pending
) =
500 (struct cpp_pending
*) xcalloc (1, sizeof (struct cpp_pending
));
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
);
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 /* Initialise the buffer obstack. */
536 gcc_obstack_init (&pfile
->buffer_ob
);
538 _cpp_init_includes (pfile
);
543 /* Free resources used by PFILE. Accessing PFILE after this function
544 returns leads to undefined behaviour. */
550 struct search_path
*dir
, *dirn
;
551 cpp_context
*context
, *contextn
;
552 tokenrun
*run
, *runn
;
554 while (CPP_BUFFER (pfile
) != NULL
)
555 _cpp_pop_buffer (pfile
);
557 if (pfile
->macro_buffer
)
559 free ((PTR
) pfile
->macro_buffer
);
560 pfile
->macro_buffer
= NULL
;
561 pfile
->macro_buffer_len
= 0;
564 deps_free (pfile
->deps
);
565 obstack_free (&pfile
->buffer_ob
, 0);
567 _cpp_destroy_hashtable (pfile
);
568 _cpp_cleanup_includes (pfile
);
570 _cpp_free_buff (pfile
->a_buff
);
571 _cpp_free_buff (pfile
->u_buff
);
572 _cpp_free_buff (pfile
->free_buffs
);
574 for (run
= &pfile
->base_run
; run
; run
= runn
)
578 if (run
!= &pfile
->base_run
)
582 for (dir
= CPP_OPTION (pfile
, quote_include
); dir
; dir
= dirn
)
585 free ((PTR
) dir
->name
);
589 for (context
= pfile
->base_context
.next
; context
; context
= contextn
)
591 contextn
= context
->next
;
595 free_line_maps (&pfile
->line_maps
);
597 result
= pfile
->errors
;
604 /* This structure defines one built-in identifier. A node will be
605 entered in the hash table under the name NAME, with value VALUE (if
606 any). If flags has OPERATOR, the node's operator field is used; if
607 flags has BUILTIN the node's builtin field is used. Macros that are
608 known at build time should not be flagged BUILTIN, as then they do
609 not appear in macro dumps with e.g. -dM or -dD.
611 Two values are not compile time constants, so we tag
612 them in the FLAGS field instead:
613 VERS value is the global version_string, quoted
614 ULP value is the global user_label_prefix
616 Also, macros with CPLUS set in the flags field are entered only for C++. */
622 unsigned char builtin
;
623 unsigned char operator;
624 unsigned short flags
;
631 #define OPERATOR 0x10
633 #define B(n, t) { U n, 0, t, 0, BUILTIN, sizeof n - 1 }
634 #define C(n, v) { U n, v, 0, 0, 0, sizeof n - 1 }
635 #define X(n, f) { U n, 0, 0, 0, f, sizeof n - 1 }
636 #define O(n, c, f) { U n, 0, 0, c, OPERATOR | f, sizeof n - 1 }
637 static const struct builtin builtin_array
[] =
639 B("__TIME__", BT_TIME
),
640 B("__DATE__", BT_DATE
),
641 B("__FILE__", BT_FILE
),
642 B("__BASE_FILE__", BT_BASE_FILE
),
643 B("__LINE__", BT_SPECLINE
),
644 B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL
),
645 B("_Pragma", BT_PRAGMA
),
647 X("__VERSION__", VERS
),
648 X("__USER_LABEL_PREFIX__", ULP
),
649 C("__REGISTER_PREFIX__", REGISTER_PREFIX
),
650 C("__HAVE_BUILTIN_SETJMP__", "1"),
651 #ifndef NO_BUILTIN_SIZE_TYPE
652 C("__SIZE_TYPE__", SIZE_TYPE
),
654 #ifndef NO_BUILTIN_PTRDIFF_TYPE
655 C("__PTRDIFF_TYPE__", PTRDIFF_TYPE
),
657 #ifndef NO_BUILTIN_WCHAR_TYPE
658 C("__WCHAR_TYPE__", WCHAR_TYPE
),
660 #ifndef NO_BUILTIN_WINT_TYPE
661 C("__WINT_TYPE__", WINT_TYPE
),
663 #ifdef STDC_0_IN_SYSTEM_HEADERS
664 B("__STDC__", BT_STDC
),
669 /* Named operators known to the preprocessor. These cannot be #defined
670 and always have their stated meaning. They are treated like normal
671 identifiers except for the type code and the meaning. Most of them
672 are only for C++ (but see iso646.h). */
673 O("and", CPP_AND_AND
, CPLUS
),
674 O("and_eq", CPP_AND_EQ
, CPLUS
),
675 O("bitand", CPP_AND
, CPLUS
),
676 O("bitor", CPP_OR
, CPLUS
),
677 O("compl", CPP_COMPL
, CPLUS
),
678 O("not", CPP_NOT
, CPLUS
),
679 O("not_eq", CPP_NOT_EQ
, CPLUS
),
680 O("or", CPP_OR_OR
, CPLUS
),
681 O("or_eq", CPP_OR_EQ
, CPLUS
),
682 O("xor", CPP_XOR
, CPLUS
),
683 O("xor_eq", CPP_XOR_EQ
, CPLUS
)
689 #define builtin_array_end \
690 builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
692 /* Subroutine of cpp_read_main_file; reads the builtins table above and
693 enters the macros into the hash table. */
695 init_builtins (pfile
)
698 const struct builtin
*b
;
700 for(b
= builtin_array
; b
< builtin_array_end
; b
++)
702 if ((b
->flags
& CPLUS
) && ! CPP_OPTION (pfile
, cplusplus
))
705 if ((b
->flags
& OPERATOR
) && ! CPP_OPTION (pfile
, operator_names
))
708 if (b
->flags
& (OPERATOR
| BUILTIN
))
710 cpp_hashnode
*hp
= cpp_lookup (pfile
, b
->name
, b
->len
);
711 if (b
->flags
& OPERATOR
)
713 hp
->flags
|= NODE_OPERATOR
;
714 hp
->value
.operator = b
->operator;
719 hp
->flags
|= NODE_BUILTIN
| NODE_WARN
;
720 hp
->value
.builtin
= b
->builtin
;
723 else /* A standard macro of some kind. */
730 /* Allocate enough space for 'name "value"\n\0'. */
731 str
= alloca (b
->len
+ strlen (version_string
) + 5);
732 sprintf (str
, "%s \"%s\"\n", b
->name
, version_string
);
737 val
= CPP_OPTION (pfile
, user_label_prefix
);
741 /* Allocate enough space for "name value\n\0". */
742 str
= alloca (b
->len
+ strlen (val
) + 3);
743 sprintf(str
, "%s %s\n", b
->name
, val
);
746 _cpp_define_builtin (pfile
, str
);
750 if (CPP_OPTION (pfile
, cplusplus
))
752 _cpp_define_builtin (pfile
, "__cplusplus 1");
753 if (SUPPORTS_ONE_ONLY
)
754 _cpp_define_builtin (pfile
, "__GXX_WEAK__ 1");
756 _cpp_define_builtin (pfile
, "__GXX_WEAK__ 0");
758 if (CPP_OPTION (pfile
, objc
))
759 _cpp_define_builtin (pfile
, "__OBJC__ 1");
761 if (CPP_OPTION (pfile
, lang
) == CLK_STDC94
)
762 _cpp_define_builtin (pfile
, "__STDC_VERSION__ 199409L");
763 else if (CPP_OPTION (pfile
, c99
))
764 _cpp_define_builtin (pfile
, "__STDC_VERSION__ 199901L");
766 if (CPP_OPTION (pfile
, lang
) == CLK_STDC89
767 || CPP_OPTION (pfile
, lang
) == CLK_STDC94
768 || CPP_OPTION (pfile
, lang
) == CLK_STDC99
)
769 _cpp_define_builtin (pfile
, "__STRICT_ANSI__ 1");
770 else if (CPP_OPTION (pfile
, lang
) == CLK_ASM
)
771 _cpp_define_builtin (pfile
, "__ASSEMBLER__ 1");
778 #undef builtin_array_end
780 /* And another subroutine. This one sets up the standard include path. */
782 init_standard_includes (pfile
)
786 const struct default_include
*p
;
787 const char *specd_prefix
= CPP_OPTION (pfile
, include_prefix
);
789 /* Several environment variables may add to the include search path.
790 CPATH specifies an additional list of directories to be searched
791 as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
792 etc. specify an additional list of directories to be searched as
793 if specified with -isystem, for the language indicated. */
795 GET_ENV_PATH_LIST (path
, "CPATH");
796 if (path
!= 0 && *path
!= 0)
797 path_include (pfile
, path
, BRACKET
);
799 switch ((CPP_OPTION (pfile
, objc
) << 1) + CPP_OPTION (pfile
, cplusplus
))
802 GET_ENV_PATH_LIST (path
, "C_INCLUDE_PATH");
805 GET_ENV_PATH_LIST (path
, "CPLUS_INCLUDE_PATH");
808 GET_ENV_PATH_LIST (path
, "OBJC_INCLUDE_PATH");
811 GET_ENV_PATH_LIST (path
, "OBJCPLUS_INCLUDE_PATH");
814 if (path
!= 0 && *path
!= 0)
815 path_include (pfile
, path
, SYSTEM
);
817 /* Search "translated" versions of GNU directories.
818 These have /usr/local/lib/gcc... replaced by specd_prefix. */
819 if (specd_prefix
!= 0 && cpp_GCC_INCLUDE_DIR_len
)
821 /* Remove the `include' from /usr/local/lib/gcc.../include.
822 GCC_INCLUDE_DIR will always end in /include. */
823 int default_len
= cpp_GCC_INCLUDE_DIR_len
;
824 char *default_prefix
= (char *) alloca (default_len
+ 1);
825 int specd_len
= strlen (specd_prefix
);
827 memcpy (default_prefix
, cpp_GCC_INCLUDE_DIR
, default_len
);
828 default_prefix
[default_len
] = '\0';
830 for (p
= cpp_include_defaults
; p
->fname
; p
++)
832 /* Some standard dirs are only for C++. */
834 || (CPP_OPTION (pfile
, cplusplus
)
835 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
837 /* Does this dir start with the prefix? */
838 if (!memcmp (p
->fname
, default_prefix
, default_len
))
840 /* Yes; change prefix and add to search list. */
841 int flen
= strlen (p
->fname
);
842 int this_len
= specd_len
+ flen
- default_len
;
843 char *str
= (char *) xmalloc (this_len
+ 1);
844 memcpy (str
, specd_prefix
, specd_len
);
845 memcpy (str
+ specd_len
,
846 p
->fname
+ default_len
,
847 flen
- default_len
+ 1);
849 append_include_chain (pfile
, str
, SYSTEM
, p
->cxx_aware
);
855 /* Search ordinary names for GNU include directories. */
856 for (p
= cpp_include_defaults
; p
->fname
; p
++)
858 /* Some standard dirs are only for C++. */
860 || (CPP_OPTION (pfile
, cplusplus
)
861 && !CPP_OPTION (pfile
, no_standard_cplusplus_includes
)))
863 char *str
= update_path (p
->fname
, p
->component
);
864 append_include_chain (pfile
, str
, SYSTEM
, p
->cxx_aware
);
869 /* Pushes a -imacro and -include file given on the command line onto
870 the buffer stack. Returns non-zero if successful. */
872 push_include (pfile
, p
)
874 struct pending_option
*p
;
878 /* Later: maybe update this to use the #include "" search path
879 if cpp_read_file fails. */
880 header
.type
= CPP_STRING
;
881 header
.val
.str
.text
= (const unsigned char *) p
->arg
;
882 header
.val
.str
.len
= strlen (p
->arg
);
883 /* Make the command line directive take up a line. */
886 return _cpp_execute_include (pfile
, &header
, IT_CMDLINE
);
889 /* Frees a pending_option chain. */
892 struct pending_option
*head
;
894 struct pending_option
*next
;
904 /* This is called after options have been parsed, and partially
905 processed. Setup for processing input from the file named FNAME,
906 or stdin if it is the empty string. Return the original filename
907 on success (e.g. foo.i->foo.c), or NULL on failure. */
909 cpp_read_main_file (pfile
, fname
, table
)
914 /* The front ends don't set up the hash table until they have
915 finished processing the command line options, so initializing the
916 hashtable is deferred until now. */
917 _cpp_init_hashtable (pfile
, table
);
919 /* Set up the include search path now. */
920 if (! CPP_OPTION (pfile
, no_standard_includes
))
921 init_standard_includes (pfile
);
923 merge_include_chains (pfile
);
925 /* With -v, print the list of dirs to search. */
926 if (CPP_OPTION (pfile
, verbose
))
928 struct search_path
*l
;
929 fprintf (stderr
, _("#include \"...\" search starts here:\n"));
930 for (l
= CPP_OPTION (pfile
, quote_include
); l
; l
= l
->next
)
932 if (l
== CPP_OPTION (pfile
, bracket_include
))
933 fprintf (stderr
, _("#include <...> search starts here:\n"));
934 fprintf (stderr
, " %s\n", l
->name
);
936 fprintf (stderr
, _("End of search list.\n"));
939 if (CPP_OPTION (pfile
, print_deps
))
940 /* Set the default target (if there is none already). */
941 deps_add_default_target (pfile
->deps
, fname
);
943 /* Open the main input file. */
944 if (!_cpp_read_file (pfile
, fname
))
947 /* Set this after cpp_post_options so the client can change the
948 option if it wishes, and after stacking the main file so we don't
949 trace the main file. */
950 pfile
->line_maps
.trace_includes
= CPP_OPTION (pfile
, print_include_names
);
952 /* For foo.i, read the original filename foo.c now, for the benefit
953 of the front ends. */
954 if (CPP_OPTION (pfile
, preprocessed
))
955 read_original_filename (pfile
);
957 return pfile
->map
->to_file
;
960 /* For preprocessed files, if the first tokens are of the form # NUM.
961 handle the directive so we know the original file name. This will
962 generate file_change callbacks, which the front ends must handle
963 appropriately given their state of initialization. */
965 read_original_filename (pfile
)
968 const cpp_token
*token
, *token1
;
970 /* Lex ahead; if the first tokens are of the form # NUM, then
971 process the directive, otherwise back up. */
972 token
= _cpp_lex_direct (pfile
);
973 if (token
->type
== CPP_HASH
)
975 token1
= _cpp_lex_direct (pfile
);
976 _cpp_backup_tokens (pfile
, 1);
978 /* If it's a #line directive, handle it. */
979 if (token1
->type
== CPP_NUMBER
)
981 _cpp_handle_directive (pfile
, token
->flags
& PREV_WHITE
);
986 /* Backup as if nothing happened. */
987 _cpp_backup_tokens (pfile
, 1);
990 /* Handle pending command line options: -D, -U, -A, -imacros and
991 -include. This should be called after debugging has been properly
992 set up in the front ends. */
994 cpp_finish_options (pfile
)
997 /* Install builtins and process command line macros etc. in the order
998 they appeared, but only if not already preprocessed. */
999 if (! CPP_OPTION (pfile
, preprocessed
))
1001 struct pending_option
*p
;
1003 _cpp_do_file_change (pfile
, LC_RENAME
, _("<builtin>"), 1, 0);
1004 init_builtins (pfile
);
1005 _cpp_do_file_change (pfile
, LC_RENAME
, _("<command line>"), 1, 0);
1006 for (p
= CPP_OPTION (pfile
, pending
)->directive_head
; p
; p
= p
->next
)
1007 (*p
->handler
) (pfile
, p
->arg
);
1009 /* Scan -imacros files after command line defines, but before
1010 files given with -include. */
1011 while ((p
= CPP_OPTION (pfile
, pending
)->imacros_head
) != NULL
)
1013 if (push_include (pfile
, p
))
1015 pfile
->buffer
->return_at_eof
= true;
1016 cpp_scan_nooutput (pfile
);
1018 CPP_OPTION (pfile
, pending
)->imacros_head
= p
->next
;
1023 free_chain (CPP_OPTION (pfile
, pending
)->directive_head
);
1024 _cpp_push_next_buffer (pfile
);
1027 /* Called to push the next buffer on the stack given by -include. If
1028 there are none, free the pending structure and restore the line map
1029 for the main file. */
1031 _cpp_push_next_buffer (pfile
)
1034 bool pushed
= false;
1036 /* This is't pretty; we'd rather not be relying on this as a boolean
1037 for reverting the line map. Further, we only free the chains in
1038 this conditional, so an early call to cpp_finish / cpp_destroy
1039 will leak that memory. */
1040 if (CPP_OPTION (pfile
, pending
)
1041 && CPP_OPTION (pfile
, pending
)->imacros_head
== NULL
)
1045 struct pending_option
*p
= CPP_OPTION (pfile
, pending
)->include_head
;
1049 if (! CPP_OPTION (pfile
, preprocessed
))
1050 pushed
= push_include (pfile
, p
);
1051 CPP_OPTION (pfile
, pending
)->include_head
= p
->next
;
1057 free (CPP_OPTION (pfile
, pending
));
1058 CPP_OPTION (pfile
, pending
) = NULL
;
1060 /* Restore the line map for the main file. */
1061 if (! CPP_OPTION (pfile
, preprocessed
))
1062 _cpp_do_file_change (pfile
, LC_RENAME
,
1063 pfile
->line_maps
.maps
[0].to_file
, 1, 0);
1070 /* Use mkdeps.c to output dependency information. */
1075 /* Stream on which to print the dependency information. */
1076 FILE *deps_stream
= 0;
1077 const char *const deps_mode
=
1078 CPP_OPTION (pfile
, print_deps_append
) ? "a" : "w";
1080 if (CPP_OPTION (pfile
, deps_file
) == 0)
1081 deps_stream
= stdout
;
1084 deps_stream
= fopen (CPP_OPTION (pfile
, deps_file
), deps_mode
);
1085 if (deps_stream
== 0)
1087 cpp_notice_from_errno (pfile
, CPP_OPTION (pfile
, deps_file
));
1092 deps_write (pfile
->deps
, deps_stream
, 72);
1094 if (CPP_OPTION (pfile
, deps_phony_targets
))
1095 deps_phony_targets (pfile
->deps
, deps_stream
);
1097 /* Don't close stdout. */
1098 if (CPP_OPTION (pfile
, deps_file
))
1100 if (ferror (deps_stream
) || fclose (deps_stream
) != 0)
1101 cpp_fatal (pfile
, "I/O error on output");
1105 /* This is called at the end of preprocessing. It pops the
1106 last buffer and writes dependency output. It should also
1107 clear macro definitions, such that you could call cpp_start_read
1108 with a new filename to restart processing. */
1113 /* cpplex.c leaves the final buffer on the stack. This it so that
1114 it returns an unending stream of CPP_EOFs to the client. If we
1115 popped the buffer, we'd dereference a NULL buffer pointer and
1116 segfault. It's nice to allow the client to do worry-free excess
1117 cpp_get_token calls. */
1118 while (pfile
->buffer
)
1119 _cpp_pop_buffer (pfile
);
1121 /* Don't write the deps file if preprocessing has failed. */
1122 if (CPP_OPTION (pfile
, print_deps
) && pfile
->errors
== 0)
1123 output_deps (pfile
);
1125 /* Report on headers that could use multiple include guards. */
1126 if (CPP_OPTION (pfile
, print_include_names
))
1127 _cpp_report_missing_guards (pfile
);
1131 new_pending_directive (pend
, text
, handler
)
1132 struct cpp_pending
*pend
;
1134 cl_directive_handler handler
;
1136 struct pending_option
*o
= (struct pending_option
*)
1137 xmalloc (sizeof (struct pending_option
));
1141 o
->handler
= handler
;
1142 APPEND (pend
, directive
, o
);
1145 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1146 I.e. a const string initializer with parens around it. That is
1147 what N_("string") resolves to, so we make no_* be macros instead. */
1148 #define no_arg N_("argument missing after %s")
1149 #define no_ass N_("assertion missing after %s")
1150 #define no_dir N_("directory name missing after %s")
1151 #define no_fil N_("file name missing after %s")
1152 #define no_mac N_("macro name missing after %s")
1153 #define no_pth N_("path name missing after %s")
1154 #define no_num N_("number missing after %s")
1155 #define no_tgt N_("target missing after %s")
1157 /* This is the list of all command line options, with the leading
1158 "-" removed. It must be sorted in ASCII collating order. */
1159 #define COMMAND_LINE_OPTIONS \
1160 DEF_OPT("$", 0, OPT_dollar) \
1161 DEF_OPT("+", 0, OPT_plus) \
1162 DEF_OPT("-help", 0, OPT__help) \
1163 DEF_OPT("-target-help", 0, OPT_target__help) \
1164 DEF_OPT("-version", 0, OPT__version) \
1165 DEF_OPT("A", no_ass, OPT_A) \
1166 DEF_OPT("C", 0, OPT_C) \
1167 DEF_OPT("D", no_mac, OPT_D) \
1168 DEF_OPT("H", 0, OPT_H) \
1169 DEF_OPT("I", no_dir, OPT_I) \
1170 DEF_OPT("M", 0, OPT_M) \
1171 DEF_OPT("MD", no_fil, OPT_MD) \
1172 DEF_OPT("MF", no_fil, OPT_MF) \
1173 DEF_OPT("MG", 0, OPT_MG) \
1174 DEF_OPT("MM", 0, OPT_MM) \
1175 DEF_OPT("MMD", no_fil, OPT_MMD) \
1176 DEF_OPT("MP", 0, OPT_MP) \
1177 DEF_OPT("MQ", no_tgt, OPT_MQ) \
1178 DEF_OPT("MT", no_tgt, OPT_MT) \
1179 DEF_OPT("P", 0, OPT_P) \
1180 DEF_OPT("U", no_mac, OPT_U) \
1181 DEF_OPT("W", no_arg, OPT_W) /* arg optional */ \
1182 DEF_OPT("d", no_arg, OPT_d) \
1183 DEF_OPT("fleading-underscore", 0, OPT_fleading_underscore) \
1184 DEF_OPT("fno-leading-underscore", 0, OPT_fno_leading_underscore) \
1185 DEF_OPT("fno-operator-names", 0, OPT_fno_operator_names) \
1186 DEF_OPT("fno-preprocessed", 0, OPT_fno_preprocessed) \
1187 DEF_OPT("fno-show-column", 0, OPT_fno_show_column) \
1188 DEF_OPT("fpreprocessed", 0, OPT_fpreprocessed) \
1189 DEF_OPT("fshow-column", 0, OPT_fshow_column) \
1190 DEF_OPT("ftabstop=", no_num, OPT_ftabstop) \
1191 DEF_OPT("h", 0, OPT_h) \
1192 DEF_OPT("idirafter", no_dir, OPT_idirafter) \
1193 DEF_OPT("imacros", no_fil, OPT_imacros) \
1194 DEF_OPT("include", no_fil, OPT_include) \
1195 DEF_OPT("iprefix", no_pth, OPT_iprefix) \
1196 DEF_OPT("isystem", no_dir, OPT_isystem) \
1197 DEF_OPT("iwithprefix", no_dir, OPT_iwithprefix) \
1198 DEF_OPT("iwithprefixbefore", no_dir, OPT_iwithprefixbefore) \
1199 DEF_OPT("lang-asm", 0, OPT_lang_asm) \
1200 DEF_OPT("lang-c", 0, OPT_lang_c) \
1201 DEF_OPT("lang-c++", 0, OPT_lang_cplusplus) \
1202 DEF_OPT("lang-c89", 0, OPT_lang_c89) \
1203 DEF_OPT("lang-objc", 0, OPT_lang_objc) \
1204 DEF_OPT("lang-objc++", 0, OPT_lang_objcplusplus) \
1205 DEF_OPT("nostdinc", 0, OPT_nostdinc) \
1206 DEF_OPT("nostdinc++", 0, OPT_nostdincplusplus) \
1207 DEF_OPT("o", no_fil, OPT_o) \
1208 DEF_OPT("pedantic", 0, OPT_pedantic) \
1209 DEF_OPT("pedantic-errors", 0, OPT_pedantic_errors) \
1210 DEF_OPT("remap", 0, OPT_remap) \
1211 DEF_OPT("std=c++98", 0, OPT_std_cplusplus98) \
1212 DEF_OPT("std=c89", 0, OPT_std_c89) \
1213 DEF_OPT("std=c99", 0, OPT_std_c99) \
1214 DEF_OPT("std=c9x", 0, OPT_std_c9x) \
1215 DEF_OPT("std=gnu89", 0, OPT_std_gnu89) \
1216 DEF_OPT("std=gnu99", 0, OPT_std_gnu99) \
1217 DEF_OPT("std=gnu9x", 0, OPT_std_gnu9x) \
1218 DEF_OPT("std=iso9899:1990", 0, OPT_std_iso9899_1990) \
1219 DEF_OPT("std=iso9899:199409", 0, OPT_std_iso9899_199409) \
1220 DEF_OPT("std=iso9899:1999", 0, OPT_std_iso9899_1999) \
1221 DEF_OPT("std=iso9899:199x", 0, OPT_std_iso9899_199x) \
1222 DEF_OPT("trigraphs", 0, OPT_trigraphs) \
1223 DEF_OPT("v", 0, OPT_v) \
1224 DEF_OPT("version", 0, OPT_version) \
1225 DEF_OPT("w", 0, OPT_w)
1227 #define DEF_OPT(text, msg, code) code,
1230 COMMAND_LINE_OPTIONS
1237 const char *opt_text
;
1240 enum opt_code opt_code
;
1243 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1245 static struct cl_option cl_options
[] =
1247 static const struct cl_option cl_options
[] =
1250 COMMAND_LINE_OPTIONS
1253 #undef COMMAND_LINE_OPTIONS
1255 /* Perform a binary search to find which, if any, option the given
1256 command-line matches. Returns its index in the option array,
1257 negative on failure. Complications arise since some options can be
1258 suffixed with an argument, and multiple complete matches can occur,
1259 e.g. -iwithprefix and -iwithprefixbefore. Moreover, we need to
1260 accept options beginning with -W that we do not recognise, but not
1261 to swallow any subsequent command line argument; this is handled as
1262 special cases in cpp_handle_option. */
1264 parse_option (input
)
1267 unsigned int md
, mn
, mx
;
1278 opt_len
= cl_options
[md
].opt_len
;
1279 comp
= memcmp (input
, cl_options
[md
].opt_text
, opt_len
);
1287 if (input
[opt_len
] == '\0')
1289 /* We were passed more text. If the option takes an argument,
1290 we may match a later option or we may have been passed the
1291 argument. The longest possible option match succeeds.
1292 If the option takes no arguments we have not matched and
1293 continue the search (e.g. input="stdc++" match was "stdc"). */
1295 if (cl_options
[md
].msg
)
1297 /* Scan forwards. If we get an exact match, return it.
1298 Otherwise, return the longest option-accepting match.
1299 This loops no more than twice with current options. */
1301 for (; mn
< (unsigned int) N_OPTS
; mn
++)
1303 opt_len
= cl_options
[mn
].opt_len
;
1304 if (memcmp (input
, cl_options
[mn
].opt_text
, opt_len
))
1306 if (input
[opt_len
] == '\0')
1308 if (cl_options
[mn
].msg
)
1319 /* Handle one command-line option in (argc, argv).
1320 Can be called multiple times, to handle multiple sets of options.
1321 Returns number of strings consumed. */
1324 cpp_handle_option (pfile
, argc
, argv
)
1330 struct cpp_pending
*pend
= CPP_OPTION (pfile
, pending
);
1332 /* Interpret "-" or a non-option as a file name. */
1333 if (argv
[i
][0] != '-' || argv
[i
][1] == '\0')
1335 if (CPP_OPTION (pfile
, in_fname
) == NULL
)
1336 CPP_OPTION (pfile
, in_fname
) = argv
[i
];
1337 else if (CPP_OPTION (pfile
, out_fname
) == NULL
)
1338 CPP_OPTION (pfile
, out_fname
) = argv
[i
];
1340 cpp_fatal (pfile
, "too many filenames. Type %s --help for usage info",
1345 enum opt_code opt_code
;
1347 const char *arg
= 0;
1349 /* Skip over '-'. */
1350 opt_index
= parse_option (&argv
[i
][1]);
1354 opt_code
= cl_options
[opt_index
].opt_code
;
1355 if (cl_options
[opt_index
].msg
)
1357 arg
= &argv
[i
][cl_options
[opt_index
].opt_len
+ 1];
1359 /* Yuk. Special case for -W as it must not swallow
1360 up any following argument. If this becomes common, add
1361 another field to the cl_options table. */
1362 if (arg
[0] == '\0' && opt_code
!= OPT_W
)
1367 cpp_fatal (pfile
, cl_options
[opt_index
].msg
, argv
[i
- 1]);
1375 case N_OPTS
: /* Shut GCC up. */
1377 case OPT_fleading_underscore
:
1378 CPP_OPTION (pfile
, user_label_prefix
) = "_";
1380 case OPT_fno_leading_underscore
:
1381 CPP_OPTION (pfile
, user_label_prefix
) = "";
1383 case OPT_fno_operator_names
:
1384 CPP_OPTION (pfile
, operator_names
) = 0;
1386 case OPT_fpreprocessed
:
1387 CPP_OPTION (pfile
, preprocessed
) = 1;
1389 case OPT_fno_preprocessed
:
1390 CPP_OPTION (pfile
, preprocessed
) = 0;
1392 case OPT_fshow_column
:
1393 CPP_OPTION (pfile
, show_column
) = 1;
1395 case OPT_fno_show_column
:
1396 CPP_OPTION (pfile
, show_column
) = 0;
1399 /* Silently ignore empty string, non-longs and silly values. */
1403 long tabstop
= strtol (arg
, &endptr
, 10);
1404 if (*endptr
== '\0' && tabstop
>= 1 && tabstop
<= 100)
1405 CPP_OPTION (pfile
, tabstop
) = tabstop
;
1409 CPP_OPTION (pfile
, inhibit_warnings
) = 1;
1414 CPP_OPTION (pfile
, help_only
) = 1;
1416 case OPT_target__help
:
1417 /* Print if any target specific options. cpplib has none, but
1418 make sure help_only gets set. */
1419 CPP_OPTION (pfile
, help_only
) = 1;
1422 /* --version inhibits compilation, -version doesn't. -v means
1423 verbose and -version. Historical reasons, don't ask. */
1425 CPP_OPTION (pfile
, help_only
) = 1;
1426 pfile
->print_version
= 1;
1429 CPP_OPTION (pfile
, verbose
) = 1;
1430 pfile
->print_version
= 1;
1433 pfile
->print_version
= 1;
1437 CPP_OPTION (pfile
, discard_comments
) = 0;
1440 CPP_OPTION (pfile
, no_line_commands
) = 1;
1442 case OPT_dollar
: /* Don't include $ in identifiers. */
1443 CPP_OPTION (pfile
, dollars_in_ident
) = 0;
1446 CPP_OPTION (pfile
, print_include_names
) = 1;
1449 new_pending_directive (pend
, arg
, cpp_define
);
1451 case OPT_pedantic_errors
:
1452 CPP_OPTION (pfile
, pedantic_errors
) = 1;
1455 CPP_OPTION (pfile
, pedantic
) = 1;
1458 CPP_OPTION (pfile
, trigraphs
) = 1;
1461 CPP_OPTION (pfile
, cplusplus
) = 1;
1462 CPP_OPTION (pfile
, cplusplus_comments
) = 1;
1465 CPP_OPTION (pfile
, remap
) = 1;
1468 CPP_OPTION (pfile
, include_prefix
) = arg
;
1469 CPP_OPTION (pfile
, include_prefix_len
) = strlen (arg
);
1472 set_lang (pfile
, CLK_GNUC89
);
1474 case OPT_lang_cplusplus
:
1475 set_lang (pfile
, CLK_GNUCXX
);
1478 set_lang (pfile
, CLK_OBJC
);
1480 case OPT_lang_objcplusplus
:
1481 set_lang (pfile
, CLK_OBJCXX
);
1484 set_lang (pfile
, CLK_ASM
);
1486 case OPT_std_cplusplus98
:
1487 set_lang (pfile
, CLK_CXX98
);
1490 set_lang (pfile
, CLK_GNUC89
);
1494 set_lang (pfile
, CLK_GNUC99
);
1496 case OPT_std_iso9899_199409
:
1497 set_lang (pfile
, CLK_STDC94
);
1499 case OPT_std_iso9899_1990
:
1502 set_lang (pfile
, CLK_STDC89
);
1504 case OPT_std_iso9899_199x
:
1505 case OPT_std_iso9899_1999
:
1508 set_lang (pfile
, CLK_STDC99
);
1511 /* -nostdinc causes no default include directories.
1512 You must specify all include-file directories with -I. */
1513 CPP_OPTION (pfile
, no_standard_includes
) = 1;
1515 case OPT_nostdincplusplus
:
1516 /* -nostdinc++ causes no default C++-specific include directories. */
1517 CPP_OPTION (pfile
, no_standard_cplusplus_includes
) = 1;
1520 if (CPP_OPTION (pfile
, out_fname
) == NULL
)
1521 CPP_OPTION (pfile
, out_fname
) = arg
;
1524 cpp_fatal (pfile
, "output filename specified twice");
1529 /* Args to -d specify what parts of macros to dump.
1530 Silently ignore unrecognised options; they may
1531 be aimed at the compiler proper. */
1535 while ((c
= *arg
++) != '\0')
1539 CPP_OPTION (pfile
, dump_macros
) = dump_only
;
1540 CPP_OPTION (pfile
, no_output
) = 1;
1543 CPP_OPTION (pfile
, dump_macros
) = dump_names
;
1546 CPP_OPTION (pfile
, dump_macros
) = dump_definitions
;
1549 CPP_OPTION (pfile
, dump_includes
) = 1;
1556 CPP_OPTION (pfile
, print_deps_missing_files
) = 1;
1559 CPP_OPTION (pfile
, print_deps
) = 2;
1562 CPP_OPTION (pfile
, print_deps
) = 1;
1565 CPP_OPTION (pfile
, deps_file
) = arg
;
1568 CPP_OPTION (pfile
, deps_phony_targets
) = 1;
1572 /* Add a target. -MQ quotes for Make. */
1573 deps_add_target (pfile
->deps
, arg
, opt_code
== OPT_MQ
);
1576 /* -MD and -MMD for cpp0 are deprecated and undocumented
1577 (use -M or -MM with -MF instead), and probably should be
1578 removed with the next major GCC version. For the moment
1579 we allow these for the benefit of Automake 1.4, which
1580 uses these when dependency tracking is enabled. Automake
1581 1.5 will fix this. */
1583 CPP_OPTION (pfile
, print_deps
) = 2;
1584 CPP_OPTION (pfile
, deps_file
) = arg
;
1587 CPP_OPTION (pfile
, print_deps
) = 1;
1588 CPP_OPTION (pfile
, deps_file
) = arg
;
1594 /* -A with an argument beginning with '-' acts as
1595 #unassert on whatever immediately follows the '-'.
1596 If "-" is the whole argument, we eliminate all
1597 predefined macros and assertions, including those
1598 that were specified earlier on the command line.
1599 That way we can get rid of any that were passed
1600 automatically in from GCC. */
1604 free_chain (pend
->directive_head
);
1605 pend
->directive_head
= NULL
;
1606 pend
->directive_tail
= NULL
;
1609 new_pending_directive (pend
, arg
+ 1, cpp_unassert
);
1612 new_pending_directive (pend
, arg
, cpp_assert
);
1615 new_pending_directive (pend
, arg
, cpp_undef
);
1617 case OPT_I
: /* Add directory to path for includes. */
1618 if (!strcmp (arg
, "-"))
1621 Use the preceding -I directories for #include "..."
1622 but not #include <...>.
1623 Don't search the directory of the present file
1624 for #include "...". (Note that -I. -I- is not the same as
1625 the default setup; -I. uses the compiler's working dir.) */
1626 if (! CPP_OPTION (pfile
, ignore_srcdir
))
1628 pend
->quote_head
= pend
->brack_head
;
1629 pend
->quote_tail
= pend
->brack_tail
;
1630 pend
->brack_head
= 0;
1631 pend
->brack_tail
= 0;
1632 CPP_OPTION (pfile
, ignore_srcdir
) = 1;
1636 cpp_fatal (pfile
, "-I- specified twice");
1641 append_include_chain (pfile
, xstrdup (arg
), BRACKET
, 0);
1644 /* Add directory to beginning of system include path, as a system
1645 include directory. */
1646 append_include_chain (pfile
, xstrdup (arg
), SYSTEM
, 0);
1651 struct pending_option
*o
= (struct pending_option
*)
1652 xmalloc (sizeof (struct pending_option
));
1656 if (opt_code
== OPT_include
)
1657 APPEND (pend
, include
, o
);
1659 APPEND (pend
, imacros
, o
);
1662 case OPT_iwithprefix
:
1663 /* Add directory to end of path for includes,
1664 with the default prefix at the front of its name. */
1666 case OPT_iwithprefixbefore
:
1667 /* Add directory to main path for includes,
1668 with the default prefix at the front of its name. */
1675 if (CPP_OPTION (pfile
, include_prefix
) != 0)
1677 size_t ipl
= CPP_OPTION (pfile
, include_prefix_len
);
1678 fname
= xmalloc (ipl
+ len
+ 1);
1679 memcpy (fname
, CPP_OPTION (pfile
, include_prefix
), ipl
);
1680 memcpy (fname
+ ipl
, arg
, len
+ 1);
1682 else if (cpp_GCC_INCLUDE_DIR_len
)
1684 fname
= xmalloc (cpp_GCC_INCLUDE_DIR_len
+ len
+ 1);
1685 memcpy (fname
, cpp_GCC_INCLUDE_DIR
, cpp_GCC_INCLUDE_DIR_len
);
1686 memcpy (fname
+ cpp_GCC_INCLUDE_DIR_len
, arg
, len
+ 1);
1689 fname
= xstrdup (arg
);
1691 append_include_chain (pfile
, fname
,
1692 opt_code
== OPT_iwithprefix
? SYSTEM
: BRACKET
, 0);
1696 /* Add directory to end of path for includes. */
1697 append_include_chain (pfile
, xstrdup (arg
), AFTER
, 0);
1700 /* Silently ignore unrecognised options. */
1701 if (!strcmp (argv
[i
], "-Wall"))
1703 CPP_OPTION (pfile
, warn_trigraphs
) = 1;
1704 CPP_OPTION (pfile
, warn_comments
) = 1;
1706 else if (!strcmp (argv
[i
], "-Wtraditional"))
1707 CPP_OPTION (pfile
, warn_traditional
) = 1;
1708 else if (!strcmp (argv
[i
], "-Wtrigraphs"))
1709 CPP_OPTION (pfile
, warn_trigraphs
) = 1;
1710 else if (!strcmp (argv
[i
], "-Wcomment"))
1711 CPP_OPTION (pfile
, warn_comments
) = 1;
1712 else if (!strcmp (argv
[i
], "-Wcomments"))
1713 CPP_OPTION (pfile
, warn_comments
) = 1;
1714 else if (!strcmp (argv
[i
], "-Wundef"))
1715 CPP_OPTION (pfile
, warn_undef
) = 1;
1716 else if (!strcmp (argv
[i
], "-Wimport"))
1717 CPP_OPTION (pfile
, warn_import
) = 1;
1718 else if (!strcmp (argv
[i
], "-Werror"))
1719 CPP_OPTION (pfile
, warnings_are_errors
) = 1;
1720 else if (!strcmp (argv
[i
], "-Wsystem-headers"))
1721 CPP_OPTION (pfile
, warn_system_headers
) = 1;
1722 else if (!strcmp (argv
[i
], "-Wno-traditional"))
1723 CPP_OPTION (pfile
, warn_traditional
) = 0;
1724 else if (!strcmp (argv
[i
], "-Wno-trigraphs"))
1725 CPP_OPTION (pfile
, warn_trigraphs
) = 0;
1726 else if (!strcmp (argv
[i
], "-Wno-comment"))
1727 CPP_OPTION (pfile
, warn_comments
) = 0;
1728 else if (!strcmp (argv
[i
], "-Wno-comments"))
1729 CPP_OPTION (pfile
, warn_comments
) = 0;
1730 else if (!strcmp (argv
[i
], "-Wno-undef"))
1731 CPP_OPTION (pfile
, warn_undef
) = 0;
1732 else if (!strcmp (argv
[i
], "-Wno-import"))
1733 CPP_OPTION (pfile
, warn_import
) = 0;
1734 else if (!strcmp (argv
[i
], "-Wno-error"))
1735 CPP_OPTION (pfile
, warnings_are_errors
) = 0;
1736 else if (!strcmp (argv
[i
], "-Wno-system-headers"))
1737 CPP_OPTION (pfile
, warn_system_headers
) = 0;
1744 /* Handle command-line options in (argc, argv).
1745 Can be called multiple times, to handle multiple sets of options.
1746 Returns if an unrecognized option is seen.
1747 Returns number of strings consumed. */
1749 cpp_handle_options (pfile
, argc
, argv
)
1755 int strings_processed
;
1757 for (i
= 0; i
< argc
; i
+= strings_processed
)
1759 strings_processed
= cpp_handle_option (pfile
, argc
- i
, argv
+ i
);
1760 if (strings_processed
== 0)
1767 /* Extra processing when all options are parsed, after all calls to
1768 cpp_handle_option[s]. Consistency checks etc. */
1770 cpp_post_options (pfile
)
1773 if (pfile
->print_version
)
1775 fprintf (stderr
, _("GNU CPP version %s (cpplib)"), version_string
);
1776 #ifdef TARGET_VERSION
1779 fputc ('\n', stderr
);
1782 /* Canonicalize in_fname and out_fname. We guarantee they are not
1783 NULL, and that the empty string represents stdin / stdout. */
1784 if (CPP_OPTION (pfile
, in_fname
) == NULL
1785 || !strcmp (CPP_OPTION (pfile
, in_fname
), "-"))
1786 CPP_OPTION (pfile
, in_fname
) = "";
1788 if (CPP_OPTION (pfile
, out_fname
) == NULL
1789 || !strcmp (CPP_OPTION (pfile
, out_fname
), "-"))
1790 CPP_OPTION (pfile
, out_fname
) = "";
1792 /* -Wtraditional is not useful in C++ mode. */
1793 if (CPP_OPTION (pfile
, cplusplus
))
1794 CPP_OPTION (pfile
, warn_traditional
) = 0;
1796 /* Set this if it hasn't been set already. */
1797 if (CPP_OPTION (pfile
, user_label_prefix
) == NULL
)
1798 CPP_OPTION (pfile
, user_label_prefix
) = USER_LABEL_PREFIX
;
1800 /* Permanently disable macro expansion if we are rescanning
1801 preprocessed text. */
1802 if (CPP_OPTION (pfile
, preprocessed
))
1803 pfile
->state
.prevent_expansion
= 1;
1805 /* We need to do this after option processing and before
1806 cpp_start_read, as cppmain.c relies on the options->no_output to
1807 set its callbacks correctly before calling cpp_start_read. */
1808 init_dependency_output (pfile
);
1810 /* After checking the environment variables, check if -M or -MM has
1811 not been specified, but other -M options have. */
1812 if (CPP_OPTION (pfile
, print_deps
) == 0 &&
1813 (CPP_OPTION (pfile
, print_deps_missing_files
)
1814 || CPP_OPTION (pfile
, deps_file
)
1815 || CPP_OPTION (pfile
, deps_phony_targets
)))
1816 cpp_fatal (pfile
, "you must additionally specify either -M or -MM");
1819 /* Set up dependency-file output. */
1821 init_dependency_output (pfile
)
1824 char *spec
, *s
, *output_file
;
1826 /* Either of two environment variables can specify output of deps.
1827 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1828 where OUTPUT_FILE is the file to write deps info to
1829 and DEPS_TARGET is the target to mention in the deps. */
1831 if (CPP_OPTION (pfile
, print_deps
) == 0)
1833 spec
= getenv ("DEPENDENCIES_OUTPUT");
1835 CPP_OPTION (pfile
, print_deps
) = 1;
1838 spec
= getenv ("SUNPRO_DEPENDENCIES");
1840 CPP_OPTION (pfile
, print_deps
) = 2;
1845 /* Find the space before the DEPS_TARGET, if there is one. */
1846 s
= strchr (spec
, ' ');
1849 /* Let the caller perform MAKE quoting. */
1850 deps_add_target (pfile
->deps
, s
+ 1, 0);
1851 output_file
= (char *) xmalloc (s
- spec
+ 1);
1852 memcpy (output_file
, spec
, s
- spec
);
1853 output_file
[s
- spec
] = 0;
1858 /* Command line overrides environment variables. */
1859 if (CPP_OPTION (pfile
, deps_file
) == 0)
1860 CPP_OPTION (pfile
, deps_file
) = output_file
;
1861 CPP_OPTION (pfile
, print_deps_append
) = 1;
1864 /* If dependencies go to standard output, or -MG is used, we should
1865 suppress output, including -dM, -dI etc. */
1866 if (CPP_OPTION (pfile
, deps_file
) == 0
1867 || CPP_OPTION (pfile
, print_deps_missing_files
))
1869 CPP_OPTION (pfile
, no_output
) = 1;
1870 CPP_OPTION (pfile
, dump_macros
) = 0;
1871 CPP_OPTION (pfile
, dump_includes
) = 0;
1878 fprintf (stderr
, _("Usage: %s [switches] input output\n"), progname
);
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-objc++ Assume that the input sources are in ObjectiveC++\n\
1910 -lang-asm Assume that the input sources are in assembler\n\
1913 -std=<std name> Specify the conformance standard; one of:\n\
1914 gnu89, gnu99, c89, c99, iso9899:1990,\n\
1915 iso9899:199409, iso9899:1999\n\
1916 -+ Allow parsing of C++ style features\n\
1917 -w Inhibit warning messages\n\
1918 -Wtrigraphs Warn if trigraphs are encountered\n\
1919 -Wno-trigraphs Do not warn about trigraphs\n\
1920 -Wcomment{s} Warn if one comment starts inside another\n\
1923 -Wno-comment{s} Do not warn about comments\n\
1924 -Wtraditional Warn about features not present in traditional C\n\
1925 -Wno-traditional Do not warn about traditional C\n\
1926 -Wundef Warn if an undefined macro is used by #if\n\
1927 -Wno-undef Do not warn about testing undefined macros\n\
1928 -Wimport Warn about the use of the #import directive\n\
1931 -Wno-import Do not warn about the use of #import\n\
1932 -Werror Treat all warnings as errors\n\
1933 -Wno-error Do not treat warnings as errors\n\
1934 -Wsystem-headers Do not suppress warnings from system headers\n\
1935 -Wno-system-headers Suppress warnings from system headers\n\
1936 -Wall Enable all preprocessor warnings\n\
1939 -M Generate make dependencies\n\
1940 -MM As -M, 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\