OSDN Git Service

* configure.in: Delete three unused variables. Move a variable
[pf3gnuchains/gcc-fork.git] / gcc / c-opts.c
1 /* C/ObjC/C++ command line option handling.
2    Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3    Contributed by Neil Booth.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "c-common.h"
28 #include "c-pragma.h"
29 #include "flags.h"
30 #include "toplev.h"
31 #include "langhooks.h"
32 #include "tree-inline.h"
33 #include "diagnostic.h"
34 #include "intl.h"
35 #include "cppdefault.h"
36 #include "c-incpath.h"
37
38 #ifndef TARGET_SYSTEM_ROOT
39 # define TARGET_SYSTEM_ROOT NULL
40 #endif
41
42 /* CPP's options.  */
43 static cpp_options *cpp_opts;
44
45 /* Input filename.  */
46 static const char *in_fname;
47
48 /* Filename and stream for preprocessed output.  */
49 static const char *out_fname;
50 static FILE *out_stream;
51
52 /* Append dependencies to deps_file.  */
53 static bool deps_append;
54
55 /* If dependency switches (-MF etc.) have been given.  */
56 static bool deps_seen;
57
58 /* If -v seen.  */
59 static bool verbose;
60
61 /* Dependency output file.  */
62 static const char *deps_file;
63
64 /* The prefix given by -iprefix, if any.  */
65 static const char *iprefix;
66
67 /* The system root, if any.  Overridden by -isysroot.  */
68 static const char *sysroot = TARGET_SYSTEM_ROOT;
69
70 /* Zero disables all standard directories for headers.  */
71 static bool std_inc = true;
72
73 /* Zero disables the C++-specific standard directories for headers.  */
74 static bool std_cxx_inc = true;
75
76 /* If the quote chain has been split by -I-.  */
77 static bool quote_chain_split;
78
79 /* Number of deferred options, deferred options array size.  */
80 static size_t deferred_count, deferred_size;
81
82 static void missing_arg PARAMS ((size_t));
83 static size_t find_opt PARAMS ((const char *, int));
84 static void set_Wimplicit PARAMS ((int));
85 static void complain_wrong_lang PARAMS ((size_t));
86 static void write_langs PARAMS ((char *, int));
87 static void print_help PARAMS ((void));
88 static void handle_OPT_d PARAMS ((const char *));
89 static void set_std_cxx98 PARAMS ((int));
90 static void set_std_c89 PARAMS ((int, int));
91 static void set_std_c99 PARAMS ((int));
92 static void check_deps_environment_vars PARAMS ((void));
93 static void handle_deferred_opts PARAMS ((void));
94 static void sanitize_cpp_opts PARAMS ((void));
95 static void add_prefixed_path PARAMS ((const char *, size_t));
96
97 #ifndef STDC_0_IN_SYSTEM_HEADERS
98 #define STDC_0_IN_SYSTEM_HEADERS 0
99 #endif
100
101 #define CL_C_ONLY       (1 << 0) /* Only C.  */
102 #define CL_OBJC_ONLY    (1 << 1) /* Only ObjC.  */
103 #define CL_CXX_ONLY     (1 << 2) /* Only C++.  */
104 #define CL_OBJCXX_ONLY  (1 << 3) /* Only ObjC++.  */
105 #define CL_JOINED       (1 << 4) /* If takes joined argument.  */
106 #define CL_SEPARATE     (1 << 5) /* If takes a separate argument.  */
107
108 #define CL_ARG          (CL_JOINED | CL_SEPARATE)
109 #define CL_C            (CL_C_ONLY | CL_OBJC_ONLY)
110 #define CL_OBJC         (CL_OBJC_ONLY | CL_OBJCXX_ONLY)
111 #define CL_CXX          (CL_CXX_ONLY | CL_OBJCXX_ONLY)
112 #define CL_ALL          (CL_C | CL_CXX)
113
114 /* This is the list of all command line options, with the leading "-"
115    removed.  It must be sorted in ASCII collating order.  All options
116    beginning with "f" or "W" are implicitly assumed to take a "no-"
117    form; this form should not be listed.  The variable "on" is true if
118    the positive form is given, otherwise it is false.  If you don't
119    want to allow a "no-" form, your handler should reject "on" being
120    false by returning zero.  See, for example, the handling of
121    -ftabstop=.
122
123    If the user gives an option to a front end that doesn't support it,
124    an error is output, mentioning which front ends the option is valid
125    for.  If you don't want this, you must accept it for all front
126    ends, and test for the front end in the option handler.  See, for
127    example, the handling of -Wno-strict-prototypes for C++.
128
129    If you request an argument with CL_JOINED, CL_SEPARATE or their
130    combination CL_ARG, it is stored in the variable "arg", which is
131    guaranteed to be non-NULL and to not be an empty string.  It points
132    to the argument either within the argv[] vector or within one of
133    that vector's strings, and so the text is permanent and copies need
134    not be made.  Be sure to add an error message in missing_arg() if
135    the default is not appropriate.  */
136
137 #define COMMAND_LINE_OPTIONS                                                 \
138   OPT("-help",                  CL_ALL,   OPT__help)                         \
139   OPT("-output-pch=",           CL_ALL | CL_ARG, OPT__output_pch)            \
140   OPT("C",                      CL_ALL,   OPT_C)                             \
141   OPT("CC",                     CL_ALL,   OPT_CC)                            \
142   OPT("E",                      CL_ALL,   OPT_E)                             \
143   OPT("H",                      CL_ALL,   OPT_H)                             \
144   OPT("I",                      CL_ALL | CL_ARG, OPT_I)                      \
145   OPT("M",                      CL_ALL,   OPT_M)                             \
146   OPT("MD",                     CL_ALL | CL_SEPARATE, OPT_MD)                \
147   OPT("MF",                     CL_ALL | CL_ARG, OPT_MF)                     \
148   OPT("MG",                     CL_ALL,   OPT_MG)                            \
149   OPT("MM",                     CL_ALL,   OPT_MM)                            \
150   OPT("MMD",                    CL_ALL | CL_SEPARATE, OPT_MMD)               \
151   OPT("MP",                     CL_ALL,   OPT_MP)                            \
152   OPT("MQ",                     CL_ALL | CL_ARG, OPT_MQ)                     \
153   OPT("MT",                     CL_ALL | CL_ARG, OPT_MT)                     \
154   OPT("P",                      CL_ALL,   OPT_P)                             \
155   OPT("Wabi",                   CL_CXX,   OPT_Wabi)                          \
156   OPT("Wall",                   CL_ALL,   OPT_Wall)                          \
157   OPT("Wbad-function-cast",     CL_C,     OPT_Wbad_function_cast)            \
158   OPT("Wcast-qual",             CL_ALL,   OPT_Wcast_qual)                    \
159   OPT("Wchar-subscripts",       CL_ALL,   OPT_Wchar_subscripts)              \
160   OPT("Wcomment",               CL_ALL,   OPT_Wcomment)                      \
161   OPT("Wcomments",              CL_ALL,   OPT_Wcomments)                     \
162   OPT("Wconversion",            CL_ALL,   OPT_Wconversion)                   \
163   OPT("Wctor-dtor-privacy",     CL_CXX,   OPT_Wctor_dtor_privacy)            \
164   OPT("Wdeprecated",            CL_CXX,   OPT_Wdeprecated)                   \
165   OPT("Wdiv-by-zero",           CL_C,     OPT_Wdiv_by_zero)                  \
166   OPT("Weffc++",                CL_CXX,   OPT_Weffcxx)                       \
167   OPT("Wendif-labels",          CL_ALL,   OPT_Wendif_labels)                 \
168   OPT("Werror",                 CL_ALL,   OPT_Werror)                        \
169   OPT("Werror-implicit-function-declaration",                                \
170                                 CL_C,     OPT_Werror_implicit_function_decl) \
171   OPT("Wfloat-equal",           CL_ALL,   OPT_Wfloat_equal)                  \
172   OPT("Wformat",                CL_ALL,   OPT_Wformat)                       \
173   OPT("Wformat-extra-args",     CL_ALL,   OPT_Wformat_extra_args)            \
174   OPT("Wformat-nonliteral",     CL_ALL,   OPT_Wformat_nonliteral)            \
175   OPT("Wformat-security",       CL_ALL,   OPT_Wformat_security)              \
176   OPT("Wformat-y2k",            CL_ALL,   OPT_Wformat_y2k)                   \
177   OPT("Wformat-zero-length",    CL_C,     OPT_Wformat_zero_length)           \
178   OPT("Wformat=",               CL_ALL | CL_JOINED, OPT_Wformat_eq)          \
179   OPT("Wimplicit",              CL_ALL,   OPT_Wimplicit)                     \
180   OPT("Wimplicit-function-declaration", CL_C, OPT_Wimplicit_function_decl)   \
181   OPT("Wimplicit-int",          CL_C,     OPT_Wimplicit_int)                 \
182   OPT("Wimport",                CL_ALL,   OPT_Wimport)                       \
183   OPT("Winvalid-pch",           CL_ALL,   OPT_Winvalid_pch)                  \
184   OPT("Wlong-long",             CL_ALL,   OPT_Wlong_long)                    \
185   OPT("Wmain",                  CL_C,     OPT_Wmain)                         \
186   OPT("Wmissing-braces",        CL_ALL,   OPT_Wmissing_braces)               \
187   OPT("Wmissing-declarations",  CL_C,     OPT_Wmissing_declarations)         \
188   OPT("Wmissing-format-attribute",CL_ALL, OPT_Wmissing_format_attribute)     \
189   OPT("Wmissing-prototypes",    CL_ALL,   OPT_Wmissing_prototypes)           \
190   OPT("Wmultichar",             CL_ALL,   OPT_Wmultichar)                    \
191   OPT("Wnested-externs",        CL_C,     OPT_Wnested_externs)               \
192   OPT("Wnon-template-friend",   CL_CXX,   OPT_Wnon_template_friend)          \
193   OPT("Wnon-virtual-dtor",      CL_CXX,   OPT_Wnon_virtual_dtor)             \
194   OPT("Wnonnull",               CL_C,     OPT_Wnonnull)                      \
195   OPT("Wold-style-cast",        CL_CXX,   OPT_Wold_style_cast)               \
196   OPT("Woverloaded-virtual",    CL_CXX,   OPT_Woverloaded_virtual)           \
197   OPT("Wparentheses",           CL_ALL,   OPT_Wparentheses)                  \
198   OPT("Wpmf-conversions",       CL_CXX,   OPT_Wpmf_conversions)              \
199   OPT("Wpointer-arith",         CL_ALL,   OPT_Wpointer_arith)                \
200   OPT("Wprotocol",              CL_OBJC,  OPT_Wprotocol)                     \
201   OPT("Wredundant-decls",       CL_ALL,   OPT_Wredundant_decls)              \
202   OPT("Wreorder",               CL_CXX,   OPT_Wreorder)                      \
203   OPT("Wreturn-type",           CL_ALL,   OPT_Wreturn_type)                  \
204   OPT("Wselector",              CL_OBJC,  OPT_Wselector)                     \
205   OPT("Wsequence-point",        CL_C,     OPT_Wsequence_point)               \
206   OPT("Wsign-compare",          CL_ALL,   OPT_Wsign_compare)                 \
207   OPT("Wsign-promo",            CL_CXX,   OPT_Wsign_promo)                   \
208   OPT("Wstrict-prototypes",     CL_ALL,   OPT_Wstrict_prototypes)            \
209   OPT("Wsynth",                 CL_CXX,   OPT_Wsynth)                        \
210   OPT("Wsystem-headers",        CL_ALL,   OPT_Wsystem_headers)               \
211   OPT("Wtraditional",           CL_C,     OPT_Wtraditional)                  \
212   OPT("Wtrigraphs",             CL_ALL,   OPT_Wtrigraphs)                    \
213   OPT("Wundeclared-selector",   CL_OBJC,  OPT_Wundeclared_selector)          \
214   OPT("Wundef",                 CL_ALL,   OPT_Wundef)                        \
215   OPT("Wunknown-pragmas",       CL_ALL,   OPT_Wunknown_pragmas)              \
216   OPT("Wunused-macros",         CL_ALL,   OPT_Wunused_macros)                \
217   OPT("Wwrite-strings",         CL_ALL,   OPT_Wwrite_strings)                \
218   OPT("ansi",                   CL_ALL,   OPT_ansi)                          \
219   OPT("d",                      CL_ALL | CL_JOINED, OPT_d)                   \
220   OPT("fabi-version=",          CL_CXX | CL_JOINED, OPT_fabi_version)        \
221   OPT("faccess-control",        CL_CXX,   OPT_faccess_control)               \
222   OPT("fall-virtual",           CL_CXX,   OPT_fall_virtual)                  \
223   OPT("falt-external-templates",CL_CXX,   OPT_falt_external_templates)       \
224   OPT("fasm",                   CL_ALL,   OPT_fasm)                          \
225   OPT("fbuiltin",               CL_ALL,   OPT_fbuiltin)                      \
226   OPT("fbuiltin-",              CL_ALL | CL_JOINED, OPT_fbuiltin_)           \
227   OPT("fcheck-new",             CL_CXX,   OPT_fcheck_new)                    \
228   OPT("fcond-mismatch",         CL_ALL,   OPT_fcond_mismatch)                \
229   OPT("fconserve-space",        CL_CXX,   OPT_fconserve_space)               \
230   OPT("fconst-strings",         CL_CXX,   OPT_fconst_strings)                \
231   OPT("fconstant-string-class=", CL_OBJC | CL_JOINED,                        \
232                                           OPT_fconstant_string_class)        \
233   OPT("fdefault-inline",        CL_CXX,   OPT_fdefault_inline)               \
234   OPT("fdollars-in-identifiers",CL_ALL,   OPT_fdollars_in_identifiers)       \
235   OPT("fdump-",                 CL_ALL | CL_JOINED, OPT_fdump)               \
236   OPT("felide-constructors",    CL_CXX,   OPT_felide_constructors)           \
237   OPT("fenforce-eh-specs",      CL_CXX,   OPT_fenforce_eh_specs)             \
238   OPT("fenum-int-equiv",        CL_CXX,   OPT_fenum_int_equiv)               \
239   OPT("fexternal-templates",    CL_CXX,   OPT_fexternal_templates)           \
240   OPT("ffixed-form",            CL_C,     OPT_ffixed_form)                   \
241   OPT("ffixed-line-length-",    CL_C | CL_JOINED, OPT_ffixed_line_length)    \
242   OPT("ffor-scope",             CL_CXX,   OPT_ffor_scope)                    \
243   OPT("ffreestanding",          CL_C,     OPT_ffreestanding)                 \
244   OPT("fgnu-keywords",          CL_CXX,   OPT_fgnu_keywords)                 \
245   OPT("fgnu-runtime",           CL_OBJC,  OPT_fgnu_runtime)                  \
246   OPT("fguiding-decls",         CL_CXX,   OPT_fguiding_decls)                \
247   OPT("fhandle-exceptions",     CL_CXX,   OPT_fhandle_exceptions)            \
248   OPT("fhonor-std",             CL_CXX,   OPT_fhonor_std)                    \
249   OPT("fhosted",                CL_C,     OPT_fhosted)                       \
250   OPT("fhuge-objects",          CL_CXX,   OPT_fhuge_objects)                 \
251   OPT("fimplement-inlines",     CL_CXX,   OPT_fimplement_inlines)            \
252   OPT("fimplicit-inline-templates", CL_CXX, OPT_fimplicit_inline_templates)  \
253   OPT("fimplicit-templates",    CL_CXX,   OPT_fimplicit_templates)           \
254   OPT("flabels-ok",             CL_CXX,   OPT_flabels_ok)                    \
255   OPT("fms-extensions",         CL_ALL,   OPT_fms_extensions)                \
256   OPT("fname-mangling-version-",CL_CXX | CL_JOINED, OPT_fname_mangling)      \
257   OPT("fnew-abi",               CL_CXX,   OPT_fnew_abi)                      \
258   OPT("fnext-runtime",          CL_OBJC,  OPT_fnext_runtime)                 \
259   OPT("fnonansi-builtins",      CL_CXX,   OPT_fnonansi_builtins)             \
260   OPT("fnonnull-objects",       CL_CXX,   OPT_fnonnull_objects)              \
261   OPT("foperator-names",        CL_CXX,   OPT_foperator_names)               \
262   OPT("foptional-diags",        CL_CXX,   OPT_foptional_diags)               \
263   OPT("fpch-deps",              CL_ALL,   OPT_fpch_deps)                     \
264   OPT("fpermissive",            CL_CXX,   OPT_fpermissive)                   \
265   OPT("fpreprocessed",          CL_ALL,   OPT_fpreprocessed)                 \
266   OPT("frepo",                  CL_CXX,   OPT_frepo)                         \
267   OPT("frtti",                  CL_CXX,   OPT_frtti)                         \
268   OPT("fshort-double",          CL_ALL,   OPT_fshort_double)                 \
269   OPT("fshort-enums",           CL_ALL,   OPT_fshort_enums)                  \
270   OPT("fshort-wchar",           CL_ALL,   OPT_fshort_wchar)                  \
271   OPT("fshow-column",           CL_ALL,   OPT_fshow_column)                  \
272   OPT("fsigned-bitfields",      CL_ALL,   OPT_fsigned_bitfields)             \
273   OPT("fsigned-char",           CL_ALL,   OPT_fsigned_char)                  \
274   OPT("fsquangle",              CL_CXX,   OPT_fsquangle)                     \
275   OPT("fstats",                 CL_CXX,   OPT_fstats)                        \
276   OPT("fstrict-prototype",      CL_CXX,   OPT_fstrict_prototype)             \
277   OPT("ftabstop=",              CL_ALL | CL_JOINED, OPT_ftabstop)            \
278   OPT("ftemplate-depth-",       CL_CXX | CL_JOINED, OPT_ftemplate_depth)     \
279   OPT("fthis-is-variable",      CL_CXX,   OPT_fthis_is_variable)             \
280   OPT("funsigned-bitfields",    CL_ALL,   OPT_funsigned_bitfields)           \
281   OPT("funsigned-char",         CL_ALL,   OPT_funsigned_char)                \
282   OPT("fuse-cxa-atexit",        CL_CXX,   OPT_fuse_cxa_atexit)               \
283   OPT("fvtable-gc",             CL_CXX,   OPT_fvtable_gc)                    \
284   OPT("fvtable-thunks",         CL_CXX,   OPT_fvtable_thunks)                \
285   OPT("fweak",                  CL_CXX,   OPT_fweak)                         \
286   OPT("fxref",                  CL_CXX,   OPT_fxref)                         \
287   OPT("gen-decls",              CL_OBJC,  OPT_gen_decls)                     \
288   OPT("idirafter",              CL_ALL | CL_ARG, OPT_idirafter)              \
289   OPT("iprefix",                CL_ALL | CL_ARG, OPT_iprefix)                \
290   OPT("isysroot",               CL_ALL | CL_ARG, OPT_isysroot)               \
291   OPT("isystem",                CL_ALL | CL_ARG, OPT_isystem)                \
292   OPT("iwithprefix",            CL_ALL | CL_ARG, OPT_iwithprefix)            \
293   OPT("iwithprefixbefore",      CL_ALL | CL_ARG, OPT_iwithprefixbefore)      \
294   OPT("lang-asm",               CL_C_ONLY, OPT_lang_asm)                     \
295   OPT("lang-objc",              CL_ALL,   OPT_lang_objc)                     \
296   OPT("nostdinc",               CL_ALL,   OPT_nostdinc)                      \
297   OPT("nostdinc++",             CL_ALL,   OPT_nostdincplusplus)              \
298   OPT("o",                      CL_ALL | CL_ARG, OPT_o)                      \
299   OPT("pedantic",               CL_ALL,   OPT_pedantic)                      \
300   OPT("pedantic-errors",        CL_ALL,   OPT_pedantic_errors)               \
301   OPT("print-objc-runtime-info", CL_OBJC, OPT_print_objc_runtime_info)       \
302   OPT("remap",                  CL_ALL,   OPT_remap)                         \
303   OPT("std=c++98",              CL_CXX,   OPT_std_cplusplus98)               \
304   OPT("std=c89",                CL_C,     OPT_std_c89)                       \
305   OPT("std=c99",                CL_C,     OPT_std_c99)                       \
306   OPT("std=c9x",                CL_C,     OPT_std_c9x)                       \
307   OPT("std=gnu++98",            CL_CXX,   OPT_std_gnuplusplus98)             \
308   OPT("std=gnu89",              CL_C,     OPT_std_gnu89)                     \
309   OPT("std=gnu99",              CL_C,     OPT_std_gnu99)                     \
310   OPT("std=gnu9x",              CL_C,     OPT_std_gnu9x)                     \
311   OPT("std=iso9899:1990",       CL_C,     OPT_std_iso9899_1990)              \
312   OPT("std=iso9899:199409",     CL_C,     OPT_std_iso9899_199409)            \
313   OPT("std=iso9899:1999",       CL_C,     OPT_std_iso9899_1999)              \
314   OPT("std=iso9899:199x",       CL_C,     OPT_std_iso9899_199x)              \
315   OPT("traditional-cpp",        CL_ALL,   OPT_traditional_cpp)               \
316   OPT("trigraphs",              CL_ALL,   OPT_trigraphs)                     \
317   OPT("undef",                  CL_ALL,   OPT_undef)                         \
318   OPT("v",                      CL_ALL,   OPT_v)                             \
319   OPT("w",                      CL_ALL,   OPT_w)
320
321 #define OPT(text, flags, code) code,
322 enum opt_code
323 {
324   COMMAND_LINE_OPTIONS
325   N_OPTS
326 };
327 #undef OPT
328
329 struct cl_option
330 {
331   const char *opt_text;
332   unsigned char opt_len;
333   unsigned char flags;
334   ENUM_BITFIELD (opt_code) opt_code : 2 * CHAR_BIT;
335 };
336
337 #define OPT(text, flags, code) { text, sizeof(text) - 1, flags, code },
338 #ifdef HOST_EBCDIC
339 static struct cl_option cl_options[] =
340 #else
341 static const struct cl_option cl_options[] =
342 #endif
343 {
344   COMMAND_LINE_OPTIONS
345 };
346 #undef OPT
347 #undef COMMAND_LINE_OPTIONS
348
349 /* Holds switches parsed by c_common_decode_option (), but whose
350    handling is deferred to c_common_post_options ().  */
351 static void defer_opt PARAMS ((enum opt_code, const char *));
352 static struct deferred_opt
353 {
354   enum opt_code code;
355   const char *arg;
356 } *deferred_opts;
357
358
359 #ifdef HOST_EBCDIC
360 static int opt_comp PARAMS ((const void *, const void *));
361
362 /* Run-time sorting of options array.  */
363 static int
364 opt_comp (p1, p2)
365      const void *p1, *p2;
366 {
367   return strcmp (((struct cl_option *) p1)->opt_text,
368                  ((struct cl_option *) p2)->opt_text);
369 }
370 #endif
371
372 /* Complain that switch OPT_INDEX expects an argument but none was
373    provided.  */
374 static void
375 missing_arg (opt_index)
376      size_t opt_index;
377 {
378   const char *opt_text = cl_options[opt_index].opt_text;
379
380   switch (cl_options[opt_index].opt_code)
381     {
382     case OPT__output_pch:
383     case OPT_Wformat_eq:
384     case OPT_d:
385     case OPT_fabi_version:
386     case OPT_fbuiltin_:
387     case OPT_fdump:
388     case OPT_fname_mangling:
389     case OPT_ftabstop:
390     case OPT_ftemplate_depth:
391     case OPT_iprefix:
392     case OPT_iwithprefix:
393     case OPT_iwithprefixbefore:
394     default:
395       error ("missing argument to \"-%s\"", opt_text);
396       break;
397
398     case OPT_fconstant_string_class:
399       error ("no class name specified with \"-%s\"", opt_text);
400       break;
401
402     case OPT_I:
403     case OPT_idirafter:
404     case OPT_isysroot:
405     case OPT_isystem:
406       error ("missing path after \"-%s\"", opt_text);
407       break;
408
409     case OPT_MF:
410     case OPT_MD:
411     case OPT_MMD:
412     case OPT_o:
413       error ("missing filename after \"-%s\"", opt_text);
414       break;
415
416     case OPT_MQ:
417     case OPT_MT:
418       error ("missing target after \"-%s\"", opt_text);
419       break;
420     }
421 }
422
423 /* Perform a binary search to find which option the command-line INPUT
424    matches.  Returns its index in the option array, and N_OPTS on
425    failure.
426
427    Complications arise since some options can be suffixed with an
428    argument, and multiple complete matches can occur, e.g. -pedantic
429    and -pedantic-errors.  Also, some options are only accepted by some
430    languages.  If a switch matches for a different language and
431    doesn't match any alternatives for the true front end, the index of
432    the matched switch is returned anyway.  The caller should check for
433    this case.  */
434 static size_t
435 find_opt (input, lang_flag)
436      const char *input;
437      int lang_flag;
438 {
439   size_t md, mn, mx;
440   size_t opt_len;
441   size_t result = N_OPTS;
442   int comp;
443
444   mn = 0;
445   mx = N_OPTS;
446
447   while (mx > mn)
448     {
449       md = (mn + mx) / 2;
450
451       opt_len = cl_options[md].opt_len;
452       comp = strncmp (input, cl_options[md].opt_text, opt_len);
453
454       if (comp < 0)
455         mx = md;
456       else if (comp > 0)
457         mn = md + 1;
458       else
459         {
460           /* The switch matches.  It it an exact match?  */
461           if (input[opt_len] == '\0')
462             return md;
463           else
464             {
465               mn = md + 1;
466
467               /* If the switch takes no arguments this is not a proper
468                  match, so we continue the search (e.g. input="stdc++"
469                  match was "stdc").  */
470               if (!(cl_options[md].flags & CL_JOINED))
471                 continue;
472
473               /* Is this switch valid for this front end?  */
474               if (!(cl_options[md].flags & lang_flag))
475                 {
476                   /* If subsequently we don't find a better match,
477                      return this and let the caller report it as a bad
478                      match.  */
479                   result = md;
480                   continue;
481                 }
482
483               /* Two scenarios remain: we have the switch's argument,
484                  or we match a longer option.  This can happen with
485                  -iwithprefix and -withprefixbefore.  The longest
486                  possible option match succeeds.
487
488                  Scan forwards, and return an exact match.  Otherwise
489                  return the longest valid option-accepting match (mx).
490                  This loops at most twice with current options.  */
491               mx = md;
492               for (md = md + 1; md < (size_t) N_OPTS; md++)
493                 {
494                   opt_len = cl_options[md].opt_len;
495                   if (strncmp (input, cl_options[md].opt_text, opt_len))
496                     break;
497                   if (input[opt_len] == '\0')
498                     return md;
499                   if (cl_options[md].flags & lang_flag
500                       && cl_options[md].flags & CL_JOINED)
501                     mx = md;
502                 }
503
504               return mx;
505             }
506         }
507     }
508
509   return result;
510 }
511
512 /* Defer option CODE with argument ARG.  */
513 static void
514 defer_opt (code, arg)
515      enum opt_code code;
516      const char *arg;
517 {
518   /* FIXME: this should be in c_common_init_options, which should take
519      argc and argv.  */
520   if (!deferred_opts)
521     {
522       extern int save_argc;
523       deferred_size = save_argc;
524       deferred_opts = (struct deferred_opt *)
525         xmalloc (deferred_size * sizeof (struct deferred_opt));
526     }
527
528   if (deferred_count == deferred_size)
529     abort ();
530
531   deferred_opts[deferred_count].code = code;
532   deferred_opts[deferred_count].arg = arg;
533   deferred_count++;
534 }
535
536 /* Common initialization before parsing options.  */
537 void
538 c_common_init_options (lang)
539      enum c_language_kind lang;
540 {
541 #ifdef HOST_EBCDIC
542   /* For non-ASCII hosts, the cl_options array needs to be sorted at
543      runtime.  */
544   qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
545 #endif
546 #if ENABLE_CHECKING
547  {
548   size_t i;
549
550   for (i = 1; i < N_OPTS; i++)
551     if (strcmp (cl_options[i - 1].opt_text, cl_options[i].opt_text) >= 0)
552       error ("options array incorrectly sorted: %s is before %s",
553              cl_options[i - 1].opt_text, cl_options[i].opt_text);
554  }
555 #endif
556
557   c_language = lang;
558   parse_in = cpp_create_reader (lang == clk_c ? CLK_GNUC89 : CLK_GNUCXX);
559   cpp_opts = cpp_get_options (parse_in);
560   if (flag_objc)
561     cpp_opts->objc = 1;
562
563   flag_const_strings = (lang == clk_cplusplus);
564   warn_pointer_arith = (lang == clk_cplusplus);
565   if (lang == clk_c)
566     warn_sign_compare = -1;
567 }
568
569 /* Handle one command-line option in (argc, argv).
570    Can be called multiple times, to handle multiple sets of options.
571    Returns number of strings consumed.  */
572 int
573 c_common_decode_option (argc, argv)
574      int argc;
575      char **argv;
576 {
577   static const int lang_flags[] = {CL_C_ONLY, CL_C, CL_CXX_ONLY, CL_CXX};
578   size_t opt_index;
579   const char *opt, *arg = 0;
580   char *dup = 0;
581   bool on = true;
582   int result, lang_flag;
583   const struct cl_option *option;
584   enum opt_code code;
585
586   opt = argv[0];
587
588   /* Interpret "-" or a non-switch as a file name.  */
589   if (opt[0] != '-' || opt[1] == '\0')
590     {
591       if (!in_fname)
592         in_fname = opt;
593       else if (!out_fname)
594         out_fname = opt;
595       else
596         {
597           error ("too many filenames given.  Type %s --help for usage",
598                  progname);
599           return argc;
600         }
601
602       return 1;
603     }
604
605   /* Drop the "no-" from negative switches.  */
606   if ((opt[1] == 'W' || opt[1] == 'f')
607       && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
608     {
609       size_t len = strlen (opt) - 3;
610
611       dup = xmalloc (len + 1);
612       dup[0] = '-';
613       dup[1] = opt[1];
614       memcpy (dup + 2, opt + 5, len - 2 + 1);
615       opt = dup;
616       on = false;
617     }
618
619   result = cpp_handle_option (parse_in, argc, argv);
620
621   /* Skip over '-'.  */
622   lang_flag = lang_flags[(c_language << 1) + flag_objc];
623   opt_index = find_opt (opt + 1, lang_flag);
624   if (opt_index == N_OPTS)
625     goto done;
626
627   result = 1;
628   option = &cl_options[opt_index];
629
630   /* Sort out any argument the switch takes.  */
631   if (option->flags & CL_ARG)
632     {
633       if (option->flags & CL_JOINED)
634         {
635           /* Have arg point to the original switch.  This is because
636              some code, such as disable_builtin_function, expects its
637              argument to be persistent until the program exits.  */
638           arg = argv[0] + cl_options[opt_index].opt_len + 1;
639           if (!on)
640             arg += strlen ("no-");
641         }
642
643       /* If we don't have an argument, and CL_SEPARATE, try the next
644          argument in the vector.  */
645       if (!arg || (*arg == '\0' && option->flags & CL_SEPARATE))
646         {
647           arg = argv[1];
648           result = 2;
649         }
650
651       if (!arg || *arg == '\0')
652         {
653           missing_arg (opt_index);
654           result = argc;
655           goto done;
656         }
657     }
658
659   /* Complain about the wrong language after we've swallowed any
660      necessary extra argument.  Eventually make this a hard error
661      after the call to find_opt, and return argc.  */
662   if (!(cl_options[opt_index].flags & lang_flag))
663     {
664       complain_wrong_lang (opt_index);
665       goto done;
666     }
667
668   switch (code = option->opt_code)
669     {
670     case N_OPTS: /* Shut GCC up.  */
671       break;
672
673     case OPT__help:
674       print_help ();
675       break;
676
677     case OPT__output_pch:
678       pch_file = arg;
679       break;
680
681     case OPT_C:
682       cpp_opts->discard_comments = 0;
683       break;
684
685     case OPT_CC:
686       cpp_opts->discard_comments = 0;
687       cpp_opts->discard_comments_in_macro_exp = 0;
688       break;
689
690     case OPT_E:
691       flag_preprocess_only = 1;
692       break;
693
694     case OPT_H:
695       cpp_opts->print_include_names = 1;
696       break;
697
698     case OPT_I:
699       if (strcmp (arg, "-"))
700         add_path (xstrdup (arg), BRACKET, 0);
701       else
702         {
703           if (quote_chain_split)
704             error ("-I- specified twice");
705           quote_chain_split = true;
706           split_quote_chain ();
707         }
708       break;
709
710     case OPT_M:
711     case OPT_MM:
712       /* When doing dependencies with -M or -MM, suppress normal
713          preprocessed output, but still do -dM etc. as software
714          depends on this.  Preprocessed output does occur if -MD, -MMD
715          or environment var dependency generation is used.  */
716       cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
717       flag_no_output = 1;
718       cpp_opts->inhibit_warnings = 1;
719       break;
720
721     case OPT_MD:
722     case OPT_MMD:
723       cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
724       deps_file = arg;
725       break;
726
727     case OPT_MF:
728       deps_seen = true;
729       deps_file = arg;
730       break;
731
732     case OPT_MG:
733       deps_seen = true;
734       cpp_opts->deps.missing_files = true;
735       break;
736
737     case OPT_MP:
738       deps_seen = true;
739       cpp_opts->deps.phony_targets = true;
740       break;
741
742     case OPT_MQ:
743     case OPT_MT:
744       deps_seen = true;
745       defer_opt (code, arg);
746       break;
747
748     case OPT_P:
749       flag_no_line_commands = 1;
750       break;
751
752     case OPT_Wabi:
753       warn_abi = on;
754       break;
755
756     case OPT_Wall:
757       set_Wunused (on);
758       set_Wformat (on);
759       set_Wimplicit (on);
760       warn_char_subscripts = on;
761       warn_missing_braces = on;
762       warn_parentheses = on;
763       warn_return_type = on;
764       warn_sequence_point = on; /* Was C only.  */
765       warn_sign_compare = on;   /* Was C++ only.  */
766       warn_switch = on;
767       warn_strict_aliasing = on;
768       
769       /* Only warn about unknown pragmas that are not in system
770          headers.  */                                        
771       warn_unknown_pragmas = on;
772
773       /* We save the value of warn_uninitialized, since if they put
774          -Wuninitialized on the command line, we need to generate a
775          warning about not using it without also specifying -O.  */
776       if (warn_uninitialized != 1)
777         warn_uninitialized = (on ? 2 : 0);
778
779       if (c_language == clk_c)
780         /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
781            can turn it off only if it's not explicit.  */
782         warn_main = on * 2;
783       else
784         {
785           /* C++-specific warnings.  */
786           warn_ctor_dtor_privacy = on;
787           warn_nonvdtor = on;
788           warn_reorder = on;
789           warn_nontemplate_friend = on;
790         }
791
792       cpp_opts->warn_trigraphs = on;
793       cpp_opts->warn_comments = on;
794       cpp_opts->warn_num_sign_change = on;
795       cpp_opts->warn_multichar = on;    /* Was C++ only.  */
796       break;
797
798     case OPT_Wbad_function_cast:
799       warn_bad_function_cast = on;
800       break;
801
802     case OPT_Wcast_qual:
803       warn_cast_qual = on;
804       break;
805
806     case OPT_Wchar_subscripts:
807       warn_char_subscripts = on;
808       break;
809
810     case OPT_Wcomment:
811     case OPT_Wcomments:
812       cpp_opts->warn_comments = on;
813       break;
814
815     case OPT_Wconversion:
816       warn_conversion = on;
817       break;
818
819     case OPT_Wctor_dtor_privacy:
820       warn_ctor_dtor_privacy = on;
821       break;
822
823     case OPT_Wdeprecated:
824       warn_deprecated = on;
825       cpp_opts->warn_deprecated = on;
826       break;
827
828     case OPT_Wdiv_by_zero:
829       warn_div_by_zero = on;
830       break;
831
832     case OPT_Weffcxx:
833       warn_ecpp = on;
834       break;
835
836     case OPT_Wendif_labels:
837       cpp_opts->warn_endif_labels = on;
838       break;
839
840     case OPT_Werror:
841       cpp_opts->warnings_are_errors = on;
842       break;
843
844     case OPT_Werror_implicit_function_decl:
845       if (!on)
846         result = 0;
847       else
848         mesg_implicit_function_declaration = 2;
849       break;
850
851     case OPT_Wfloat_equal:
852       warn_float_equal = on;
853       break;
854
855     case OPT_Wformat:
856       set_Wformat (on);
857       break;
858
859     case OPT_Wformat_eq:
860       set_Wformat (atoi (arg));
861       break;
862
863     case OPT_Wformat_extra_args:
864       warn_format_extra_args = on;
865       break;
866
867     case OPT_Wformat_nonliteral:
868       warn_format_nonliteral = on;
869       break;
870
871     case OPT_Wformat_security:
872       warn_format_security = on;
873       break;
874
875     case OPT_Wformat_y2k:
876       warn_format_y2k = on;
877       break;
878
879     case OPT_Wformat_zero_length:
880       warn_format_zero_length = on;
881       break;
882
883     case OPT_Wimplicit:
884       set_Wimplicit (on);
885       break;
886
887     case OPT_Wimplicit_function_decl:
888       mesg_implicit_function_declaration = on;
889       break;
890
891     case OPT_Wimplicit_int:
892       warn_implicit_int = on;
893       break;
894
895     case OPT_Wimport:
896       cpp_opts->warn_import = on;
897       break;
898
899     case OPT_Winvalid_pch:
900       cpp_opts->warn_invalid_pch = on;
901       break;
902
903     case OPT_Wlong_long:
904       warn_long_long = on;
905       break;
906
907     case OPT_Wmain:
908       if (on)
909         warn_main = 1;
910       else
911         warn_main = -1;
912       break;
913
914     case OPT_Wmissing_braces:
915       warn_missing_braces = on;
916       break;
917
918     case OPT_Wmissing_declarations:
919       warn_missing_declarations = on;
920       break;
921
922     case OPT_Wmissing_format_attribute:
923       warn_missing_format_attribute = on;
924       break;
925
926     case OPT_Wmissing_prototypes:
927       warn_missing_prototypes = on;
928       break;
929
930     case OPT_Wmultichar:
931       cpp_opts->warn_multichar = on;
932       break;
933
934     case OPT_Wnested_externs:
935       warn_nested_externs = on;
936       break;
937
938     case OPT_Wnon_template_friend:
939       warn_nontemplate_friend = on;
940       break;
941
942     case OPT_Wnon_virtual_dtor:
943       warn_nonvdtor = on;
944       break;
945
946     case OPT_Wnonnull:
947       warn_nonnull = on;
948       break;
949
950     case OPT_Wold_style_cast:
951       warn_old_style_cast = on;
952       break;
953
954     case OPT_Woverloaded_virtual:
955       warn_overloaded_virtual = on;
956       break;
957
958     case OPT_Wparentheses:
959       warn_parentheses = on;
960       break;
961
962     case OPT_Wpmf_conversions:
963       warn_pmf2ptr = on;
964       break;
965
966     case OPT_Wpointer_arith:
967       warn_pointer_arith = on;
968       break;
969
970     case OPT_Wprotocol:
971       warn_protocol = on;
972       break;
973
974     case OPT_Wselector:
975       warn_selector = on;
976       break;
977
978     case OPT_Wredundant_decls:
979       warn_redundant_decls = on;
980       break;
981
982     case OPT_Wreorder:
983       warn_reorder = on;
984       break;
985
986     case OPT_Wreturn_type:
987       warn_return_type = on;
988       break;
989
990     case OPT_Wsequence_point:
991       warn_sequence_point = on;
992       break;
993
994     case OPT_Wsign_compare:
995       warn_sign_compare = on;
996       break;
997
998     case OPT_Wsign_promo:
999       warn_sign_promo = on;
1000       break;
1001
1002     case OPT_Wstrict_prototypes:
1003       if (!on && c_language == clk_cplusplus)
1004         warning ("-Wno-strict-prototypes is not supported in C++");
1005       else
1006         warn_strict_prototypes = on;
1007       break;
1008
1009     case OPT_Wsynth:
1010       warn_synth = on;
1011       break;
1012
1013     case OPT_Wsystem_headers:
1014       cpp_opts->warn_system_headers = on;
1015       break;
1016
1017     case OPT_Wtraditional:
1018       warn_traditional = on;
1019       cpp_opts->warn_traditional = on;
1020       break;
1021
1022     case OPT_Wtrigraphs:
1023       cpp_opts->warn_trigraphs = on;
1024       break;
1025
1026     case OPT_Wundeclared_selector:
1027       warn_undeclared_selector = on;
1028       break;
1029
1030     case OPT_Wundef:
1031       cpp_opts->warn_undef = on;
1032       break;
1033
1034     case OPT_Wunknown_pragmas:
1035       /* Set to greater than 1, so that even unknown pragmas in
1036          system headers will be warned about.  */  
1037       warn_unknown_pragmas = on * 2;
1038       break;
1039
1040     case OPT_Wunused_macros:
1041       cpp_opts->warn_unused_macros = on;
1042       break;
1043
1044     case OPT_Wwrite_strings:
1045       if (c_language == clk_c)
1046         flag_const_strings = on;
1047       else
1048         warn_write_strings = on;
1049       break;
1050       
1051     case OPT_ansi:
1052       if (c_language == clk_c)
1053         set_std_c89 (false, true);
1054       else
1055         set_std_cxx98 (true);
1056       break;
1057
1058     case OPT_d:
1059       handle_OPT_d (arg);
1060       break;
1061
1062     case OPT_fcond_mismatch:
1063       if (c_language == clk_c)
1064         {
1065           flag_cond_mismatch = on;
1066           break;
1067         }
1068       /* Fall through.  */
1069
1070     case OPT_fall_virtual:
1071     case OPT_fenum_int_equiv:
1072     case OPT_fguiding_decls:
1073     case OPT_fhonor_std:
1074     case OPT_fhuge_objects:
1075     case OPT_flabels_ok:
1076     case OPT_fname_mangling:
1077     case OPT_fnew_abi:
1078     case OPT_fnonnull_objects:
1079     case OPT_fsquangle:
1080     case OPT_fstrict_prototype:
1081     case OPT_fthis_is_variable:
1082     case OPT_fvtable_thunks:
1083     case OPT_fxref:
1084       warning ("switch \"%s\" is no longer supported", argv[0]);
1085       break;
1086
1087     case OPT_fabi_version:
1088       flag_abi_version = read_integral_parameter (arg, argv[0], 1);
1089       break;
1090
1091     case OPT_faccess_control:
1092       flag_access_control = on;
1093       break;
1094
1095     case OPT_falt_external_templates:
1096       flag_alt_external_templates = on;
1097       if (on)
1098         flag_external_templates = true;
1099     cp_deprecated:
1100       warning ("switch \"%s\" is deprecated, please see documentation for details", argv[0]);
1101       break;
1102
1103     case OPT_fasm:
1104       flag_no_asm = !on;
1105       break;
1106
1107     case OPT_fbuiltin:
1108       flag_no_builtin = !on;
1109       break;
1110
1111     case OPT_fbuiltin_:
1112       if (on)
1113         result = 0;
1114       else
1115         disable_builtin_function (arg);
1116       break;
1117
1118     case OPT_fdollars_in_identifiers:
1119       dollars_in_ident = on;
1120       break;
1121
1122     case OPT_fdump:
1123       if (!on || !dump_switch_p (argv[0] + strlen ("-f")))
1124         result = 0;
1125       break;
1126
1127     case OPT_ffreestanding:
1128       on = !on;
1129       /* Fall through...  */
1130     case OPT_fhosted:
1131       flag_hosted = on;
1132       flag_no_builtin = !on;
1133       /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
1134       if (!on && warn_main == 2)
1135         warn_main = 0;
1136       break;
1137
1138     case OPT_fshort_double:
1139       flag_short_double = on;
1140       break;
1141
1142     case OPT_fshort_enums:
1143       flag_short_enums = on;
1144       break;
1145
1146     case OPT_fshort_wchar:
1147       flag_short_wchar = on;
1148       break;
1149
1150     case OPT_fsigned_bitfields:
1151       flag_signed_bitfields = on;
1152       explicit_flag_signed_bitfields = 1;
1153       break;
1154
1155     case OPT_fsigned_char:
1156       flag_signed_char = on;
1157       break;
1158
1159     case OPT_funsigned_bitfields:
1160       flag_signed_bitfields = !on;
1161       explicit_flag_signed_bitfields = 1;
1162       break;
1163
1164     case OPT_funsigned_char:
1165       flag_signed_char = !on;
1166       break;
1167
1168     case OPT_fcheck_new:
1169       flag_check_new = on;
1170       break;
1171
1172     case OPT_fconserve_space:
1173       flag_conserve_space = on;
1174       break;
1175
1176     case OPT_fconst_strings:
1177       flag_const_strings = on;
1178       break;
1179
1180     case OPT_fconstant_string_class:
1181       constant_string_class_name = arg;
1182       break;
1183
1184     case OPT_fdefault_inline:
1185       flag_default_inline = on;
1186       break;
1187
1188     case OPT_felide_constructors:
1189       flag_elide_constructors = on;
1190       break;
1191
1192     case OPT_fenforce_eh_specs:
1193       flag_enforce_eh_specs = on;
1194       break;
1195
1196     case OPT_fexternal_templates:
1197       flag_external_templates = on;
1198       goto cp_deprecated;
1199
1200     case OPT_ffixed_form:
1201     case OPT_ffixed_line_length:
1202       /* Fortran front end options ignored when preprocessing only.  */
1203       if (flag_preprocess_only)
1204         result = -1;
1205       break;
1206
1207     case OPT_ffor_scope:
1208       flag_new_for_scope = on;
1209       break;
1210
1211     case OPT_fgnu_keywords:
1212       flag_no_gnu_keywords = !on;
1213       break;
1214
1215     case OPT_fgnu_runtime:
1216       flag_next_runtime = !on;
1217       break;
1218
1219     case OPT_fhandle_exceptions:
1220       warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
1221       flag_exceptions = on;
1222       break;
1223
1224     case OPT_fimplement_inlines:
1225       flag_implement_inlines = on;
1226       break;
1227
1228     case OPT_fimplicit_inline_templates:
1229       flag_implicit_inline_templates = on;
1230       break;
1231
1232     case OPT_fimplicit_templates:
1233       flag_implicit_templates = on;
1234       break;
1235
1236     case OPT_fms_extensions:
1237       flag_ms_extensions = on;
1238       break;
1239
1240     case OPT_fnext_runtime:
1241       flag_next_runtime = on;
1242       break;
1243
1244     case OPT_fnonansi_builtins:
1245       flag_no_nonansi_builtin = !on;
1246       break;
1247
1248     case OPT_foperator_names:
1249       cpp_opts->operator_names = on;
1250       break;
1251
1252     case OPT_foptional_diags:
1253       flag_optional_diags = on;
1254       break;
1255
1256     case OPT_fpch_deps:
1257       cpp_opts->restore_pch_deps = on;
1258       break;
1259
1260     case OPT_fpermissive:
1261       flag_permissive = on;
1262       break;
1263
1264     case OPT_fpreprocessed:
1265       cpp_opts->preprocessed = on;
1266       break;
1267
1268     case OPT_frepo:
1269       flag_use_repository = on;
1270       if (on)
1271         flag_implicit_templates = 0;
1272       break;
1273
1274     case OPT_frtti:
1275       flag_rtti = on;
1276       break;
1277
1278     case OPT_fshow_column:
1279       cpp_opts->show_column = on;
1280       break;
1281
1282     case OPT_fstats:
1283       flag_detailed_statistics = on;
1284       break;
1285
1286     case OPT_ftabstop:
1287       /* Don't recognize -fno-tabstop=.  */
1288       if (!on)
1289         return 0;
1290
1291       /* It is documented that we silently ignore silly values.  */
1292         {
1293           char *endptr;
1294           long tabstop = strtol (arg, &endptr, 10);
1295           if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1296             cpp_opts->tabstop = tabstop;
1297         }
1298       break;
1299
1300     case OPT_ftemplate_depth:
1301       max_tinst_depth = read_integral_parameter (arg, argv[0], 0);
1302       break;
1303
1304     case OPT_fvtable_gc:
1305       flag_vtable_gc = on;
1306       break;
1307
1308     case OPT_fuse_cxa_atexit:
1309       flag_use_cxa_atexit = on;
1310       break;
1311
1312     case OPT_fweak:
1313       flag_weak = on;
1314       break;
1315
1316     case OPT_gen_decls:
1317       flag_gen_declaration = 1;
1318       break;
1319
1320     case OPT_idirafter:
1321       add_path (xstrdup (arg), AFTER, 0);
1322       break;
1323
1324     case OPT_iprefix:
1325       iprefix = arg;
1326       break;
1327
1328     case OPT_isysroot:
1329       sysroot = arg;
1330       break;
1331
1332     case OPT_isystem:
1333       add_path (xstrdup (arg), SYSTEM, 0);
1334       break;
1335
1336     case OPT_iwithprefix:
1337       add_prefixed_path (arg, SYSTEM);
1338       break;
1339
1340     case OPT_iwithprefixbefore:
1341       add_prefixed_path (arg, BRACKET);
1342       break;
1343
1344     case OPT_lang_asm:
1345       cpp_set_lang (parse_in, CLK_ASM);
1346       break;
1347
1348     case OPT_lang_objc:
1349       cpp_opts->objc = 1;
1350       break;
1351
1352     case OPT_nostdinc:
1353       std_inc = false;
1354       break;
1355
1356     case OPT_nostdincplusplus:
1357       std_cxx_inc = false;
1358       break;
1359
1360     case OPT_o:
1361       if (!out_fname)
1362         out_fname = arg;
1363       else
1364         {
1365           error ("output filename specified twice");
1366           result = argc;
1367         }
1368       break;
1369
1370       /* We need to handle the -pedantic switches here, rather than in
1371          c_common_post_options, so that a subsequent -Wno-endif-labels
1372          is not overridden.  */
1373     case OPT_pedantic_errors:
1374       cpp_opts->pedantic_errors = 1;
1375       /* fall through */
1376     case OPT_pedantic:
1377       cpp_opts->pedantic = 1;
1378       cpp_opts->warn_endif_labels = 1;
1379       break;
1380
1381     case OPT_print_objc_runtime_info:
1382       print_struct_values = 1;
1383       break;
1384
1385     case OPT_remap:
1386       cpp_opts->remap = 1;
1387       break;
1388
1389     case OPT_std_cplusplus98:
1390     case OPT_std_gnuplusplus98:
1391       set_std_cxx98 (code == OPT_std_cplusplus98 /* ISO */);
1392       break;
1393
1394     case OPT_std_c89:
1395     case OPT_std_iso9899_1990:
1396     case OPT_std_iso9899_199409:
1397       set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1398       break;
1399
1400     case OPT_std_gnu89:
1401       set_std_c89 (false /* c94 */, false /* ISO */);
1402       break;
1403
1404     case OPT_std_c99:
1405     case OPT_std_c9x:
1406     case OPT_std_iso9899_1999:
1407     case OPT_std_iso9899_199x:
1408       set_std_c99 (true /* ISO */);
1409       break;
1410
1411     case OPT_std_gnu99:
1412     case OPT_std_gnu9x:
1413       set_std_c99 (false /* ISO */);
1414       break;
1415
1416     case OPT_trigraphs:
1417       cpp_opts->trigraphs = 1;
1418       break;
1419
1420     case OPT_traditional_cpp:
1421       cpp_opts->traditional = 1;
1422       break;
1423
1424     case OPT_undef:
1425       flag_undef = 1;
1426       break;
1427
1428     case OPT_w:
1429       cpp_opts->inhibit_warnings = 1;
1430       break;
1431
1432     case OPT_v:
1433       verbose = true;
1434       break;
1435     }
1436
1437  done:
1438   if (dup)
1439     free (dup);
1440   return result;
1441 }
1442
1443 /* Post-switch processing.  */
1444 bool
1445 c_common_post_options ()
1446 {
1447   /* Canonicalize the input and output filenames.  */
1448   if (in_fname == NULL || !strcmp (in_fname, "-"))
1449     in_fname = "";
1450
1451   if (out_fname == NULL || !strcmp (out_fname, "-"))
1452     out_fname = "";
1453
1454   if (cpp_opts->deps.style == DEPS_NONE)
1455     check_deps_environment_vars ();
1456
1457   handle_deferred_opts ();
1458
1459   sanitize_cpp_opts ();
1460
1461   register_include_chains (parse_in, sysroot, iprefix,
1462                            std_inc, std_cxx_inc && c_language == clk_cplusplus,
1463                            verbose);
1464
1465   flag_inline_trees = 1;
1466
1467   /* Use tree inlining if possible.  Function instrumentation is only
1468      done in the RTL level, so we disable tree inlining.  */
1469   if (! flag_instrument_function_entry_exit)
1470     {
1471       if (!flag_no_inline)
1472         flag_no_inline = 1;
1473       if (flag_inline_functions)
1474         {
1475           flag_inline_trees = 2;
1476           flag_inline_functions = 0;
1477         }
1478     }
1479
1480   /* Special format checking options don't work without -Wformat; warn if
1481      they are used.  */
1482   if (warn_format_y2k && !warn_format)
1483     warning ("-Wformat-y2k ignored without -Wformat");
1484   if (warn_format_extra_args && !warn_format)
1485     warning ("-Wformat-extra-args ignored without -Wformat");
1486   if (warn_format_zero_length && !warn_format)
1487     warning ("-Wformat-zero-length ignored without -Wformat");
1488   if (warn_format_nonliteral && !warn_format)
1489     warning ("-Wformat-nonliteral ignored without -Wformat");
1490   if (warn_format_security && !warn_format)
1491     warning ("-Wformat-security ignored without -Wformat");
1492   if (warn_missing_format_attribute && !warn_format)
1493     warning ("-Wmissing-format-attribute ignored without -Wformat");
1494
1495   /* If an error has occurred in cpplib, note it so we fail
1496      immediately.  */
1497   errorcount += cpp_errors (parse_in);
1498
1499   return flag_preprocess_only;
1500 }
1501
1502 /* Front end initialization common to C, ObjC and C++.  */
1503 const char *
1504 c_common_init (filename)
1505      const char *filename;
1506 {
1507   /* Set up preprocessor arithmetic.  Must be done after call to
1508      c_common_nodes_and_builtins for type nodes to be good.  */
1509   cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1510   cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1511   cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1512   cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1513   cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1514
1515   /* Register preprocessor built-ins before calls to
1516      cpp_main_file.  */
1517   cpp_get_callbacks (parse_in)->register_builtins = cb_register_builtins;
1518
1519   if (flag_preprocess_only)
1520     {
1521       /* Open the output now.  We must do so even if flag_no_output is
1522          on, because there may be other output than from the actual
1523          preprocessing (e.g. from -dM).  */
1524       if (out_fname[0] == '\0')
1525         out_stream = stdout;
1526       else
1527         out_stream = fopen (out_fname, "w");
1528
1529       if (out_stream == NULL)
1530         fatal_io_error ("opening output file %s", out_fname);
1531       else
1532         /* Preprocess the input file to out_stream.  */
1533         preprocess_file (parse_in, in_fname, out_stream);
1534
1535       /* Exit quickly in toplev.c.  */
1536       return NULL;
1537     }
1538
1539   init_c_lex ();
1540
1541   /* Start it at 0.  */
1542   lineno = 0;
1543
1544   /* NOTE: we use in_fname here, not the one supplied.  */
1545   filename = cpp_read_main_file (parse_in, in_fname, ident_hash);
1546
1547   /* Has to wait until now so that cpplib has its hash table.  */
1548   init_pragma ();
1549
1550   return filename;
1551 }
1552
1553 /* Common finish hook for the C, ObjC and C++ front ends.  */
1554 void
1555 c_common_finish ()
1556 {
1557   FILE *deps_stream = NULL;
1558
1559   if (cpp_opts->deps.style != DEPS_NONE)
1560     {
1561       /* If -M or -MM was seen without -MF, default output to the
1562          output stream.  */
1563       if (!deps_file)
1564         deps_stream = out_stream;
1565       else
1566         {
1567           deps_stream = fopen (deps_file, deps_append ? "a": "w");
1568           if (!deps_stream)
1569             fatal_io_error ("opening dependency file %s", deps_file);
1570         }
1571     }
1572
1573   /* For performance, avoid tearing down cpplib's internal structures
1574      with cpp_destroy ().  */
1575   errorcount += cpp_finish (parse_in, deps_stream);
1576
1577   if (deps_stream && deps_stream != out_stream
1578       && (ferror (deps_stream) || fclose (deps_stream)))
1579     fatal_io_error ("closing dependency file %s", deps_file);
1580
1581   if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1582     fatal_io_error ("when writing output to %s", out_fname);
1583 }
1584
1585 /* Either of two environment variables can specify output of
1586    dependencies.  Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1587    DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1588    and DEPS_TARGET is the target to mention in the deps.  They also
1589    result in dependency information being appended to the output file
1590    rather than overwriting it, and like Sun's compiler
1591    SUNPRO_DEPENDENCIES suppresses the dependency on the main file.  */
1592 static void
1593 check_deps_environment_vars ()
1594 {
1595   char *spec;
1596
1597   GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1598   if (spec)
1599     cpp_opts->deps.style = DEPS_USER;
1600   else
1601     {
1602       GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1603       if (spec)
1604         {
1605           cpp_opts->deps.style = DEPS_SYSTEM;
1606           cpp_opts->deps.ignore_main_file = true;
1607         }
1608     }
1609
1610   if (spec)
1611     {
1612       /* Find the space before the DEPS_TARGET, if there is one.  */
1613       char *s = strchr (spec, ' ');
1614       if (s)
1615         {
1616           /* Let the caller perform MAKE quoting.  */
1617           defer_opt (OPT_MT, s + 1);
1618           *s = '\0';
1619         }
1620
1621       /* Command line -MF overrides environment variables and default.  */
1622       if (!deps_file)
1623         deps_file = spec;
1624
1625       deps_append = 1;
1626     }
1627 }
1628
1629 /* Handle deferred command line switches.  */
1630 static void
1631 handle_deferred_opts ()
1632 {
1633   size_t i;
1634
1635   for (i = 0; i < deferred_count; i++)
1636     {
1637       struct deferred_opt *opt = &deferred_opts[i];
1638
1639       switch (opt->code)
1640         {
1641         case OPT_MT:
1642         case OPT_MQ:
1643           cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1644           break;
1645
1646         default:
1647           abort ();
1648         }
1649     }
1650
1651   free (deferred_opts);
1652 }
1653
1654 /* These settings are appropriate for GCC, but not necessarily so for
1655    cpplib as a library.  */
1656 static void
1657 sanitize_cpp_opts ()
1658 {
1659   /* If we don't know what style of dependencies to output, complain
1660      if any other dependency switches have been given.  */
1661   if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1662     error ("to generate dependencies you must specify either -M or -MM");
1663
1664   /* -dM and dependencies suppress normal output; do it here so that
1665      the last -d[MDN] switch overrides earlier ones.  */
1666   if (flag_dump_macros == 'M')
1667     flag_no_output = 1;
1668
1669   /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
1670      -dM since at least glibc relies on -M -dM to work.  */
1671   if (flag_no_output)
1672     {
1673       if (flag_dump_macros != 'M')
1674         flag_dump_macros = 0;
1675       flag_dump_includes = 0;
1676     }
1677
1678   cpp_opts->unsigned_char = !flag_signed_char;
1679   cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1680
1681   /* We want -Wno-long-long to override -pedantic -std=non-c99
1682      and/or -Wtraditional, whatever the ordering.  */
1683   cpp_opts->warn_long_long
1684     = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1685 }
1686
1687 /* Add include path with a prefix at the front of its name.  */
1688 static void
1689 add_prefixed_path (suffix, chain)
1690      const char *suffix;
1691      size_t chain;
1692 {
1693   const char *prefix;
1694
1695   prefix = iprefix ? iprefix: cpp_GCC_INCLUDE_DIR;
1696   add_path (concat (prefix, suffix), chain, 0);
1697 }
1698
1699 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1700    extensions if ISO).  There is no concept of gnu94.  */
1701 static void
1702 set_std_c89 (c94, iso)
1703      int c94, iso;
1704 {
1705   cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1706   flag_iso = iso;
1707   flag_no_asm = iso;
1708   flag_no_gnu_keywords = iso;
1709   flag_no_nonansi_builtin = iso;
1710   flag_noniso_default_format_attributes = !iso;
1711   flag_isoc94 = c94;
1712   flag_isoc99 = 0;
1713   flag_writable_strings = 0;
1714 }
1715
1716 /* Set the C 99 standard (without GNU extensions if ISO).  */
1717 static void
1718 set_std_c99 (iso)
1719      int iso;
1720 {
1721   cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1722   flag_no_asm = iso;
1723   flag_no_nonansi_builtin = iso;
1724   flag_noniso_default_format_attributes = !iso;
1725   flag_iso = iso;
1726   flag_isoc99 = 1;
1727   flag_isoc94 = 1;
1728   flag_writable_strings = 0;
1729 }
1730
1731 /* Set the C++ 98 standard (without GNU extensions if ISO).  */
1732 static void
1733 set_std_cxx98 (iso)
1734      int iso;
1735 {
1736   cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1737   flag_no_gnu_keywords = iso;
1738   flag_no_nonansi_builtin = iso;
1739   flag_noniso_default_format_attributes = !iso;
1740   flag_iso = iso;
1741 }
1742
1743 /* Handle setting implicit to ON.  */
1744 static void
1745 set_Wimplicit (on)
1746      int on;
1747 {
1748   warn_implicit = on;
1749   warn_implicit_int = on;
1750   if (on)
1751     {
1752       if (mesg_implicit_function_declaration != 2)
1753         mesg_implicit_function_declaration = 1;
1754     }
1755   else
1756     mesg_implicit_function_declaration = 0;
1757 }
1758
1759 /* Args to -d specify what to dump.  Silently ignore
1760    unrecognized options; they may be aimed at toplev.c.  */
1761 static void
1762 handle_OPT_d (arg)
1763      const char *arg;
1764 {
1765   char c;
1766
1767   while ((c = *arg++) != '\0')
1768     switch (c)
1769       {
1770       case 'M':                 /* Dump macros only.  */
1771       case 'N':                 /* Dump names.  */
1772       case 'D':                 /* Dump definitions.  */
1773         flag_dump_macros = c;
1774         break;
1775
1776       case 'I':
1777         flag_dump_includes = 1;
1778         break;
1779       }
1780 }
1781
1782 /* Write a slash-separated list of languages in FLAGS to BUF.  */
1783 static void
1784 write_langs (buf, flags)
1785      char *buf;
1786      int flags;
1787 {
1788   *buf = '\0';
1789   if (flags & CL_C_ONLY)
1790     strcat (buf, "C");
1791   if (flags & CL_OBJC_ONLY)
1792     {
1793       if (*buf)
1794         strcat (buf, "/");
1795       strcat (buf, "ObjC");
1796     }
1797   if (flags & CL_CXX_ONLY)
1798     {
1799       if (*buf)
1800         strcat (buf, "/");
1801       strcat (buf, "C++");
1802     }
1803 }
1804
1805 /* Complain that switch OPT_INDEX does not apply to this front end.  */
1806 static void
1807 complain_wrong_lang (opt_index)
1808      size_t opt_index;
1809 {
1810   char ok_langs[60], bad_langs[60];
1811   int ok_flags = cl_options[opt_index].flags;
1812
1813   write_langs (ok_langs, ok_flags);
1814   write_langs (bad_langs, ~ok_flags);
1815   warning ("\"-%s\" is valid for %s but not for %s",
1816            cl_options[opt_index].opt_text, ok_langs, bad_langs);
1817 }
1818
1819 /* Handle --help output.  */
1820 static void
1821 print_help ()
1822 {
1823   /* To keep the lines from getting too long for some compilers, limit
1824      to about 500 characters (6 lines) per chunk.  */
1825   fputs (_("\
1826 Switches:\n\
1827   -include <file>           Include the contents of <file> before other files\n\
1828   -imacros <file>           Accept definition of macros in <file>\n\
1829   -iprefix <path>           Specify <path> as a prefix for next two options\n\
1830   -iwithprefix <dir>        Add <dir> to the end of the system include path\n\
1831   -iwithprefixbefore <dir>  Add <dir> to the end of the main include path\n\
1832   -isystem <dir>            Add <dir> to the start of the system include path\n\
1833 "), stdout);
1834   fputs (_("\
1835   -idirafter <dir>          Add <dir> to the end of the system include path\n\
1836   -I <dir>                  Add <dir> to the end of the main include path\n\
1837   -I-                       Fine-grained include path control; see info docs\n\
1838   -nostdinc                 Do not search system include directories\n\
1839                              (dirs specified with -isystem will still be used)\n\
1840   -nostdinc++               Do not search system include directories for C++\n\
1841   -o <file>                 Put output into <file>\n\
1842 "), stdout);
1843   fputs (_("\
1844   -trigraphs                Support ISO C trigraphs\n\
1845   -std=<std name>           Specify the conformance standard; one of:\n\
1846                             gnu89, gnu99, c89, c99, iso9899:1990,\n\
1847                             iso9899:199409, iso9899:1999, c++98\n\
1848   -w                        Inhibit warning messages\n\
1849   -W[no-]trigraphs          Warn if trigraphs are encountered\n\
1850   -W[no-]comment{s}         Warn if one comment starts inside another\n\
1851 "), stdout);
1852   fputs (_("\
1853   -W[no-]traditional        Warn about features not present in traditional C\n\
1854   -W[no-]undef              Warn if an undefined macro is used by #if\n\
1855   -W[no-]import             Warn about the use of the #import directive\n\
1856 "), stdout);
1857   fputs (_("\
1858   -W[no-]error              Treat all warnings as errors\n\
1859   -W[no-]system-headers     Do not suppress warnings from system headers\n\
1860   -W[no-]all                Enable most preprocessor warnings\n\
1861 "), stdout);
1862   fputs (_("\
1863   -M                        Generate make dependencies\n\
1864   -MM                       As -M, but ignore system header files\n\
1865   -MD                       Generate make dependencies and compile\n\
1866   -MMD                      As -MD, but ignore system header files\n\
1867   -MF <file>                Write dependency output to the given file\n\
1868   -MG                       Treat missing header file as generated files\n\
1869 "), stdout);
1870   fputs (_("\
1871   -MP                       Generate phony targets for all headers\n\
1872   -MQ <target>              Add a MAKE-quoted target\n\
1873   -MT <target>              Add an unquoted target\n\
1874 "), stdout);
1875   fputs (_("\
1876   -D<macro>                 Define a <macro> with string '1' as its value\n\
1877   -D<macro>=<val>           Define a <macro> with <val> as its value\n\
1878   -A<question>=<answer>     Assert the <answer> to <question>\n\
1879   -A-<question>=<answer>    Disable the <answer> to <question>\n\
1880   -U<macro>                 Undefine <macro> \n\
1881   -v                        Display the version number\n\
1882 "), stdout);
1883   fputs (_("\
1884   -H                        Print the name of header files as they are used\n\
1885   -C                        Do not discard comments\n\
1886   -dM                       Display a list of macro definitions active at end\n\
1887   -dD                       Preserve macro definitions in output\n\
1888   -dN                       As -dD except that only the names are preserved\n\
1889   -dI                       Include #include directives in the output\n\
1890 "), stdout);
1891   fputs (_("\
1892   -f[no-]preprocessed       Treat the input file as already preprocessed\n\
1893   -ftabstop=<number>        Distance between tab stops for column reporting\n\
1894   -isysroot <dir>           Set <dir> to be the system root directory\n\
1895   -P                        Do not generate #line directives\n\
1896   -remap                    Remap file names when including files\n\
1897   --help                    Display this information\n\
1898 "), stdout);
1899 }