OSDN Git Service

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