OSDN Git Service

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