c-opts.c revision 117395
1/* C/ObjC/C++ command line option handling.
2   Copyright (C) 2002 Free Software Foundation, Inc.
3   Contributed by Neil Booth.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA.  */
21
22#include "config.h"
23#include "system.h"
24#include "tree.h"
25#include "c-common.h"
26#include "c-pragma.h"
27#include "flags.h"
28#include "toplev.h"
29#include "langhooks.h"
30#include "tree-inline.h"
31#include "diagnostic.h"
32#include "intl.h"
33
34/* CPP's options.  */
35static cpp_options *cpp_opts;
36
37/* Input filename.  */
38static const char *in_fname;
39
40/* Filename and stream for preprocessed output.  */
41static const char *out_fname;
42static FILE *out_stream;
43
44/* Append dependencies to deps_file.  */
45static bool deps_append;
46
47/* If dependency switches (-MF etc.) have been given.  */
48static bool deps_seen;
49
50/* Dependency output file.  */
51static const char *deps_file;
52
53/* Number of deferred options, deferred options array size.  */
54static size_t deferred_count, deferred_size;
55
56static void missing_arg PARAMS ((size_t));
57static size_t find_opt PARAMS ((const char *, int));
58static void set_Wimplicit PARAMS ((int));
59static void complain_wrong_lang PARAMS ((size_t, int));
60static void write_langs PARAMS ((char *, int));
61static void print_help PARAMS ((void));
62static void handle_OPT_d PARAMS ((const char *));
63static void set_std_cxx98 PARAMS ((int));
64static void set_std_c89 PARAMS ((int, int));
65static void set_std_c99 PARAMS ((int));
66static void check_deps_environment_vars PARAMS ((void));
67static void preprocess_file PARAMS ((void));
68static void handle_deferred_opts PARAMS ((void));
69static void sanitize_cpp_opts PARAMS ((void));
70
71#ifndef STDC_0_IN_SYSTEM_HEADERS
72#define STDC_0_IN_SYSTEM_HEADERS 0
73#endif
74
75#define CL_C_ONLY	(1 << 0) /* Only C.  */
76#define CL_OBJC_ONLY	(1 << 1) /* Only ObjC.  */
77#define CL_CXX_ONLY	(1 << 2) /* Only C++.  */
78#define CL_OBJCXX_ONLY	(1 << 3) /* Only ObjC++.  */
79#define CL_JOINED	(1 << 4) /* If takes joined argument.  */
80#define CL_SEPARATE	(1 << 5) /* If takes a separate argument.  */
81
82#define CL_ARG		(CL_JOINED | CL_SEPARATE)
83#define CL_C		(CL_C_ONLY | CL_OBJC_ONLY)
84#define CL_OBJC		(CL_OBJC_ONLY | CL_OBJCXX_ONLY)
85#define CL_CXX		(CL_CXX_ONLY | CL_OBJCXX_ONLY)
86#define CL_ALL		(CL_C | CL_CXX)
87
88/* This is the list of all command line options, with the leading "-"
89   removed.  It must be sorted in ASCII collating order.  All options
90   beginning with "f" or "W" are implicitly assumed to take a "no-"
91   form; this form should not be listed.  The variable "on" is true if
92   the positive form is given, otherwise it is false.  If you don't
93   want to allow a "no-" form, your handler should reject "on" being
94   false by returning zero.  See, for example, the handling of
95   -ftabstop=.
96
97   If the user gives an option to a front end that doesn't support it,
98   an error is output, mentioning which front ends the option is valid
99   for.  If you don't want this, you must accept it for all front
100   ends, and test for the front end in the option handler.  See, for
101   example, the handling of -Wno-strict-prototypes for C++.
102
103   If you request an argument with CL_JOINED, CL_SEPARATE or their
104   combination CL_ARG, it is stored in the variable "arg", which is
105   guaranteed to be non-NULL and to not be an empty string.  It points
106   to the argument either within the argv[] vector or within one of
107   that vector's strings, and so the text is permanent and copies need
108   not be made.  Be sure to add an error message in missing_arg() if
109   the default is not appropriate.  */
110
111#define COMMAND_LINE_OPTIONS						     \
112  OPT("-help",                  CL_ALL,   OPT__help)			     \
113  OPT("C",                      CL_ALL,   OPT_C)			     \
114  OPT("CC",                     CL_ALL,   OPT_CC)			     \
115  OPT("E",			CL_ALL,   OPT_E)			     \
116  OPT("H",                      CL_ALL,   OPT_H)			     \
117  OPT("M",                      CL_ALL,   OPT_M)			     \
118  OPT("MD",                     CL_ALL | CL_SEPARATE, OPT_MD)		     \
119  OPT("MF",                     CL_ALL | CL_ARG, OPT_MF)		     \
120  OPT("MG",                     CL_ALL,   OPT_MG)			     \
121  OPT("MM",                     CL_ALL,   OPT_MM)			     \
122  OPT("MMD",                    CL_ALL | CL_SEPARATE, OPT_MMD)		     \
123  OPT("MP",                     CL_ALL,   OPT_MP)			     \
124  OPT("MQ",                     CL_ALL | CL_ARG, OPT_MQ)		     \
125  OPT("MT",                     CL_ALL | CL_ARG, OPT_MT)		     \
126  OPT("P",                      CL_ALL,   OPT_P)			     \
127  OPT("Wabi",                   CL_CXX,   OPT_Wabi)                          \
128  OPT("Wall",			CL_ALL,   OPT_Wall)			     \
129  OPT("Wbad-function-cast",	CL_C,     OPT_Wbad_function_cast)	     \
130  OPT("Wcast-qual",		CL_ALL,   OPT_Wcast_qual)		     \
131  OPT("Wchar-subscripts",	CL_ALL,   OPT_Wchar_subscripts)		     \
132  OPT("Wcomment",		CL_ALL,   OPT_Wcomment)			     \
133  OPT("Wcomments",              CL_ALL,   OPT_Wcomments)		     \
134  OPT("Wconversion",		CL_ALL,   OPT_Wconversion)		     \
135  OPT("Wctor-dtor-privacy",	CL_CXX,   OPT_Wctor_dtor_privacy)	     \
136  OPT("Wdeprecated",		CL_CXX,   OPT_Wdeprecated)		     \
137  OPT("Wdiv-by-zero",		CL_C,     OPT_Wdiv_by_zero)		     \
138  OPT("Weffc++",		CL_CXX,   OPT_Weffcxx)			     \
139  OPT("Wendif-labels",		CL_ALL,   OPT_Wendif_labels)		     \
140  OPT("Werror",                 CL_ALL,   OPT_Werror)			     \
141  OPT("Werror-implicit-function-declaration",				     \
142	     			CL_C,     OPT_Werror_implicit_function_decl) \
143  OPT("Wfloat-equal",		CL_ALL,   OPT_Wfloat_equal)		     \
144  OPT("Wformat",		CL_ALL,   OPT_Wformat)			     \
145  OPT("Wformat-extra-args",	CL_ALL,   OPT_Wformat_extra_args)	     \
146  OPT("Wformat-nonliteral",	CL_ALL,   OPT_Wformat_nonliteral)	     \
147  OPT("Wformat-security",	CL_ALL,   OPT_Wformat_security)		     \
148  OPT("Wformat-y2k",		CL_ALL,   OPT_Wformat_y2k)		     \
149  OPT("Wformat-zero-length",	CL_C,     OPT_Wformat_zero_length)	     \
150  OPT("Wformat=",		CL_ALL | CL_JOINED, OPT_Wformat_eq)	     \
151  OPT("Wimplicit",		CL_ALL,   OPT_Wimplicit)		     \
152  OPT("Wimplicit-function-declaration",	CL_C, OPT_Wimplicit_function_decl)   \
153  OPT("Wimplicit-int",		CL_C,	  OPT_Wimplicit_int)		     \
154  OPT("Wimport",                CL_ALL,   OPT_Wimport)			     \
155  OPT("Wlong-long",		CL_ALL,   OPT_Wlong_long)		     \
156  OPT("Wmain",			CL_C,     OPT_Wmain)			     \
157  OPT("Wmissing-braces",	CL_ALL,   OPT_Wmissing_braces)		     \
158  OPT("Wmissing-declarations",	CL_C,     OPT_Wmissing_declarations)	     \
159  OPT("Wmissing-format-attribute",CL_ALL, OPT_Wmissing_format_attribute)     \
160  OPT("Wmissing-prototypes",	CL_ALL,   OPT_Wmissing_prototypes)	     \
161  OPT("Wmultichar",		CL_ALL,   OPT_Wmultichar)		     \
162  OPT("Wnested-externs",	CL_C,     OPT_Wnested_externs)		     \
163  OPT("Wnon-template-friend",	CL_CXX,   OPT_Wnon_template_friend)	     \
164  OPT("Wnon-virtual-dtor",	CL_CXX,   OPT_Wnon_virtual_dtor)	     \
165  OPT("Wnonnull",		CL_C,     OPT_Wnonnull)			     \
166  OPT("Wold-style-cast",	CL_CXX,   OPT_Wold_style_cast)		     \
167  OPT("Woverloaded-virtual",	CL_CXX,   OPT_Woverloaded_virtual)	     \
168  OPT("Wparentheses",		CL_ALL,   OPT_Wparentheses)		     \
169  OPT("Wpmf-conversions",	CL_CXX,   OPT_Wpmf_conversions)		     \
170  OPT("Wpointer-arith",		CL_ALL,   OPT_Wpointer_arith)		     \
171  OPT("Wprotocol",		CL_OBJC,  OPT_Wprotocol)		     \
172  OPT("Wredundant-decls",	CL_ALL,   OPT_Wredundant_decls)		     \
173  OPT("Wreorder",		CL_CXX,   OPT_Wreorder)			     \
174  OPT("Wreturn-type",		CL_ALL,   OPT_Wreturn_type)		     \
175  OPT("Wselector",		CL_OBJC,  OPT_Wselector)		     \
176  OPT("Wsequence-point",	CL_C,     OPT_Wsequence_point)		     \
177  OPT("Wsign-compare",		CL_ALL,   OPT_Wsign_compare)		     \
178  OPT("Wsign-promo",		CL_CXX,   OPT_Wsign_promo)		     \
179  OPT("Wstrict-prototypes",	CL_ALL,   OPT_Wstrict_prototypes)	     \
180  OPT("Wsynth",			CL_CXX,   OPT_Wsynth)			     \
181  OPT("Wsystem-headers",	CL_ALL,   OPT_Wsystem_headers)		     \
182  OPT("Wtraditional",		CL_C,     OPT_Wtraditional)		     \
183  OPT("Wtrigraphs",		CL_ALL,   OPT_Wtrigraphs)		     \
184  OPT("Wundeclared-selector",	CL_OBJC,  OPT_Wundeclared_selector)	     \
185  OPT("Wundef",			CL_ALL,   OPT_Wundef)			     \
186  OPT("Wunknown-pragmas",	CL_ALL,   OPT_Wunknown_pragmas)		     \
187  OPT("Wunused-macros",		CL_ALL,   OPT_Wunused_macros)		     \
188  OPT("Wwrite-strings",		CL_ALL,   OPT_Wwrite_strings)		     \
189  OPT("ansi",			CL_ALL,   OPT_ansi)			     \
190  OPT("d",                      CL_ALL | CL_JOINED, OPT_d)		     \
191  OPT("fabi-version=",          CL_CXX | CL_JOINED, OPT_fabi_version)        \
192  OPT("faccess-control",	CL_CXX,   OPT_faccess_control)		     \
193  OPT("fall-virtual",		CL_CXX,   OPT_fall_virtual)		     \
194  OPT("falt-external-templates",CL_CXX,   OPT_falt_external_templates)	     \
195  OPT("fasm",			CL_ALL,   OPT_fasm)			     \
196  OPT("fbuiltin",		CL_ALL,   OPT_fbuiltin)			     \
197  OPT("fbuiltin-",		CL_ALL | CL_JOINED, OPT_fbuiltin_)	     \
198  OPT("fcheck-new",		CL_CXX,   OPT_fcheck_new)		     \
199  OPT("fcond-mismatch",		CL_ALL,   OPT_fcond_mismatch)		     \
200  OPT("fconserve-space",	CL_CXX,   OPT_fconserve_space)		     \
201  OPT("fconst-strings",		CL_CXX,   OPT_fconst_strings)		     \
202  OPT("fconstant-string-class=", CL_OBJC | CL_JOINED,			     \
203					  OPT_fconstant_string_class)	     \
204  OPT("fdefault-inline",	CL_CXX,   OPT_fdefault_inline)		     \
205  OPT("fdollars-in-identifiers",CL_ALL,   OPT_fdollars_in_identifiers)	     \
206  OPT("fdump-",			CL_ALL | CL_JOINED, OPT_fdump)		     \
207  OPT("felide-constructors",	CL_CXX,   OPT_felide_constructors)	     \
208  OPT("fenforce-eh-specs",	CL_CXX,   OPT_fenforce_eh_specs)	     \
209  OPT("fenum-int-equiv",	CL_CXX,   OPT_fenum_int_equiv)		     \
210  OPT("fexternal-templates",	CL_CXX,   OPT_fexternal_templates)	     \
211  OPT("ffixed-form",		CL_C,	  OPT_ffixed_form)		     \
212  OPT("ffixed-line-length-",	CL_C | CL_JOINED, OPT_ffixed_line_length)    \
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,
286enum opt_code
287{
288  COMMAND_LINE_OPTIONS
289  N_OPTS
290};
291#undef OPT
292
293struct 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
303static struct cl_option cl_options[] =
304#else
305static 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 ().  */
315static void defer_opt PARAMS ((enum opt_code, const char *));
316static struct deferred_opt
317{
318  enum opt_code code;
319  const char *arg;
320} *deferred_opts;
321
322
323#ifdef HOST_EBCDIC
324static int opt_comp PARAMS ((const void *, const void *));
325
326/* Run-time sorting of options array.  */
327static int
328opt_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.  */
338static void
339missing_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.  */
387static size_t
388find_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.  */
466static void
467defer_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.  */
490void
491c_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.  */
525int
526c_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, on);
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      if (c_language == clk_cplusplus)
703	warn_sign_compare = on;
704      warn_switch = on;
705      warn_strict_aliasing = on;
706
707      /* Only warn about unknown pragmas that are not in system
708	 headers.  */
709      warn_unknown_pragmas = on;
710
711      /* We save the value of warn_uninitialized, since if they put
712	 -Wuninitialized on the command line, we need to generate a
713	 warning about not using it without also specifying -O.  */
714      if (warn_uninitialized != 1)
715	warn_uninitialized = (on ? 2 : 0);
716
717      if (c_language == clk_c)
718	/* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
719	   can turn it off only if it's not explicit.  */
720	warn_main = on * 2;
721      else
722	{
723	  /* C++-specific warnings.  */
724	  warn_ctor_dtor_privacy = on;
725	  warn_nonvdtor = on;
726	  warn_reorder = on;
727	  warn_nontemplate_friend = on;
728	}
729
730      cpp_opts->warn_trigraphs = on;
731      cpp_opts->warn_comments = on;
732      cpp_opts->warn_num_sign_change = on;
733      cpp_opts->warn_multichar = on;	/* Was C++ only.  */
734      break;
735
736    case OPT_Wbad_function_cast:
737      warn_bad_function_cast = on;
738      break;
739
740    case OPT_Wcast_qual:
741      warn_cast_qual = on;
742      break;
743
744    case OPT_Wchar_subscripts:
745      warn_char_subscripts = on;
746      break;
747
748    case OPT_Wcomment:
749    case OPT_Wcomments:
750      cpp_opts->warn_comments = on;
751      break;
752
753    case OPT_Wconversion:
754      warn_conversion = on;
755      break;
756
757    case OPT_Wctor_dtor_privacy:
758      warn_ctor_dtor_privacy = on;
759      break;
760
761    case OPT_Wdeprecated:
762      warn_deprecated = on;
763      break;
764
765    case OPT_Wdiv_by_zero:
766      warn_div_by_zero = on;
767      break;
768
769    case OPT_Weffcxx:
770      warn_ecpp = on;
771      break;
772
773    case OPT_Wendif_labels:
774      cpp_opts->warn_endif_labels = on;
775      break;
776
777    case OPT_Werror:
778      cpp_opts->warnings_are_errors = on;
779      break;
780
781    case OPT_Werror_implicit_function_decl:
782      if (!on)
783	result = 0;
784      else
785	mesg_implicit_function_declaration = 2;
786      break;
787
788    case OPT_Wfloat_equal:
789      warn_float_equal = on;
790      break;
791
792    case OPT_Wformat:
793      set_Wformat (on);
794      break;
795
796    case OPT_Wformat_eq:
797      set_Wformat (atoi (arg));
798      break;
799
800    case OPT_Wformat_extra_args:
801      warn_format_extra_args = on;
802      break;
803
804    case OPT_Wformat_nonliteral:
805      warn_format_nonliteral = on;
806      break;
807
808    case OPT_Wformat_security:
809      warn_format_security = on;
810      break;
811
812    case OPT_Wformat_y2k:
813      warn_format_y2k = on;
814      break;
815
816    case OPT_Wformat_zero_length:
817      warn_format_zero_length = on;
818      break;
819
820    case OPT_Wimplicit:
821      set_Wimplicit (on);
822      break;
823
824    case OPT_Wimplicit_function_decl:
825      mesg_implicit_function_declaration = on;
826      break;
827
828    case OPT_Wimplicit_int:
829      warn_implicit_int = on;
830      break;
831
832    case OPT_Wimport:
833      cpp_opts->warn_import = on;
834      break;
835
836    case OPT_Wlong_long:
837      warn_long_long = on;
838      break;
839
840    case OPT_Wmain:
841      if (on)
842	warn_main = 1;
843      else
844	warn_main = -1;
845      break;
846
847    case OPT_Wmissing_braces:
848      warn_missing_braces = on;
849      break;
850
851    case OPT_Wmissing_declarations:
852      warn_missing_declarations = on;
853      break;
854
855    case OPT_Wmissing_format_attribute:
856      warn_missing_format_attribute = on;
857      break;
858
859    case OPT_Wmissing_prototypes:
860      warn_missing_prototypes = on;
861      break;
862
863    case OPT_Wmultichar:
864      cpp_opts->warn_multichar = on;
865      break;
866
867    case OPT_Wnested_externs:
868      warn_nested_externs = on;
869      break;
870
871    case OPT_Wnon_template_friend:
872      warn_nontemplate_friend = on;
873      break;
874
875    case OPT_Wnon_virtual_dtor:
876      warn_nonvdtor = on;
877      break;
878
879    case OPT_Wnonnull:
880      warn_nonnull = on;
881      break;
882
883    case OPT_Wold_style_cast:
884      warn_old_style_cast = on;
885      break;
886
887    case OPT_Woverloaded_virtual:
888      warn_overloaded_virtual = on;
889      break;
890
891    case OPT_Wparentheses:
892      warn_parentheses = on;
893      break;
894
895    case OPT_Wpmf_conversions:
896      warn_pmf2ptr = on;
897      break;
898
899    case OPT_Wpointer_arith:
900      warn_pointer_arith = on;
901      break;
902
903    case OPT_Wprotocol:
904      warn_protocol = on;
905      break;
906
907    case OPT_Wselector:
908      warn_selector = on;
909      break;
910
911    case OPT_Wredundant_decls:
912      warn_redundant_decls = on;
913      break;
914
915    case OPT_Wreorder:
916      warn_reorder = on;
917      break;
918
919    case OPT_Wreturn_type:
920      warn_return_type = on;
921      break;
922
923    case OPT_Wsequence_point:
924      warn_sequence_point = on;
925      break;
926
927    case OPT_Wsign_compare:
928      warn_sign_compare = on;
929      break;
930
931    case OPT_Wsign_promo:
932      warn_sign_promo = on;
933      break;
934
935    case OPT_Wstrict_prototypes:
936      if (!on && c_language == clk_cplusplus)
937	warning ("-Wno-strict-prototypes is not supported in C++");
938      else
939	warn_strict_prototypes = on;
940      break;
941
942    case OPT_Wsynth:
943      warn_synth = on;
944      break;
945
946    case OPT_Wsystem_headers:
947      cpp_opts->warn_system_headers = on;
948      break;
949
950    case OPT_Wtraditional:
951      warn_traditional = on;
952      cpp_opts->warn_traditional = on;
953      break;
954
955    case OPT_Wtrigraphs:
956      cpp_opts->warn_trigraphs = on;
957      break;
958
959    case OPT_Wundeclared_selector:
960      warn_undeclared_selector = on;
961      break;
962
963    case OPT_Wundef:
964      cpp_opts->warn_undef = on;
965      break;
966
967    case OPT_Wunknown_pragmas:
968      /* Set to greater than 1, so that even unknown pragmas in
969	 system headers will be warned about.  */
970      warn_unknown_pragmas = on * 2;
971      break;
972
973    case OPT_Wunused_macros:
974      cpp_opts->warn_unused_macros = on;
975      break;
976
977    case OPT_Wwrite_strings:
978      if (c_language == clk_c)
979	flag_const_strings = on;
980      else
981	warn_write_strings = on;
982      break;
983
984    case OPT_ansi:
985      if (c_language == clk_c)
986	set_std_c89 (false, true);
987      else
988	set_std_cxx98 (true);
989      break;
990
991    case OPT_d:
992      handle_OPT_d (arg);
993      break;
994
995    case OPT_fcond_mismatch:
996      if (c_language == clk_c)
997	{
998	  flag_cond_mismatch = on;
999	  break;
1000	}
1001      /* Fall through.  */
1002
1003    case OPT_fall_virtual:
1004    case OPT_fenum_int_equiv:
1005    case OPT_fguiding_decls:
1006    case OPT_fhonor_std:
1007    case OPT_fhuge_objects:
1008    case OPT_flabels_ok:
1009    case OPT_fname_mangling:
1010    case OPT_fnew_abi:
1011    case OPT_fnonnull_objects:
1012    case OPT_fsquangle:
1013    case OPT_fstrict_prototype:
1014    case OPT_fthis_is_variable:
1015    case OPT_fvtable_thunks:
1016    case OPT_fxref:
1017      warning ("switch \"%s\" is no longer supported", argv[0]);
1018      break;
1019
1020    case OPT_fabi_version:
1021      flag_abi_version = read_integral_parameter (arg, argv[0], 1);
1022      break;
1023
1024    case OPT_faccess_control:
1025      flag_access_control = on;
1026      break;
1027
1028    case OPT_falt_external_templates:
1029      flag_alt_external_templates = on;
1030      if (on)
1031	flag_external_templates = true;
1032    cp_deprecated:
1033      warning ("switch \"%s\" is deprecated, please see documentation for details", argv[0]);
1034      break;
1035
1036    case OPT_fasm:
1037      flag_no_asm = !on;
1038      break;
1039
1040    case OPT_fbuiltin:
1041      flag_no_builtin = !on;
1042      break;
1043
1044    case OPT_fbuiltin_:
1045      if (on)
1046	result = 0;
1047      else
1048	disable_builtin_function (arg);
1049      break;
1050
1051    case OPT_fdollars_in_identifiers:
1052      dollars_in_ident = on;
1053      break;
1054
1055    case OPT_fdump:
1056      if (!on || !dump_switch_p (argv[0] + strlen ("-f")))
1057	result = 0;
1058      break;
1059
1060    case OPT_ffreestanding:
1061      on = !on;
1062      /* Fall through...  */
1063    case OPT_fhosted:
1064      flag_hosted = on;
1065      flag_no_builtin = !on;
1066      /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
1067      if (!on && warn_main == 2)
1068	warn_main = 0;
1069      break;
1070
1071    case OPT_fshort_double:
1072      flag_short_double = on;
1073      break;
1074
1075    case OPT_fshort_enums:
1076      flag_short_enums = on;
1077      break;
1078
1079    case OPT_fshort_wchar:
1080      flag_short_wchar = on;
1081      break;
1082
1083    case OPT_fsigned_bitfields:
1084      flag_signed_bitfields = on;
1085      explicit_flag_signed_bitfields = 1;
1086      break;
1087
1088    case OPT_fsigned_char:
1089      flag_signed_char = on;
1090      break;
1091
1092    case OPT_funsigned_bitfields:
1093      flag_signed_bitfields = !on;
1094      explicit_flag_signed_bitfields = 1;
1095      break;
1096
1097    case OPT_funsigned_char:
1098      flag_signed_char = !on;
1099      break;
1100
1101    case OPT_fcheck_new:
1102      flag_check_new = on;
1103      break;
1104
1105    case OPT_fconserve_space:
1106      flag_conserve_space = on;
1107      break;
1108
1109    case OPT_fconst_strings:
1110      flag_const_strings = on;
1111      break;
1112
1113    case OPT_fconstant_string_class:
1114      constant_string_class_name = arg;
1115      break;
1116
1117    case OPT_fdefault_inline:
1118      flag_default_inline = on;
1119      break;
1120
1121    case OPT_felide_constructors:
1122      flag_elide_constructors = on;
1123      break;
1124
1125    case OPT_fenforce_eh_specs:
1126      flag_enforce_eh_specs = on;
1127      break;
1128
1129    case OPT_fexternal_templates:
1130      flag_external_templates = on;
1131      goto cp_deprecated;
1132
1133    case OPT_ffixed_form:
1134    case OPT_ffixed_line_length:
1135      /* Fortran front end options ignored when preprocessing only.  */
1136      if (flag_preprocess_only)
1137        result = -1;
1138      break;
1139
1140    case OPT_ffor_scope:
1141      flag_new_for_scope = on;
1142      break;
1143
1144    case OPT_fgnu_keywords:
1145      flag_no_gnu_keywords = !on;
1146      break;
1147
1148    case OPT_fgnu_runtime:
1149      flag_next_runtime = !on;
1150      break;
1151
1152    case OPT_fhandle_exceptions:
1153      warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
1154      flag_exceptions = on;
1155      break;
1156
1157    case OPT_fimplement_inlines:
1158      flag_implement_inlines = on;
1159      break;
1160
1161    case OPT_fimplicit_inline_templates:
1162      flag_implicit_inline_templates = on;
1163      break;
1164
1165    case OPT_fimplicit_templates:
1166      flag_implicit_templates = on;
1167      break;
1168
1169    case OPT_fms_extensions:
1170      flag_ms_extensions = on;
1171      break;
1172
1173    case OPT_fnext_runtime:
1174      flag_next_runtime = on;
1175      break;
1176
1177    case OPT_fnonansi_builtins:
1178      flag_no_nonansi_builtin = !on;
1179      break;
1180
1181    case OPT_foperator_names:
1182      cpp_opts->operator_names = on;
1183      break;
1184
1185    case OPT_foptional_diags:
1186      flag_optional_diags = on;
1187      break;
1188
1189    case OPT_fpermissive:
1190      flag_permissive = on;
1191      break;
1192
1193    case OPT_fpreprocessed:
1194      cpp_opts->preprocessed = on;
1195      break;
1196
1197    case OPT_frepo:
1198      flag_use_repository = on;
1199      if (on)
1200	flag_implicit_templates = 0;
1201      break;
1202
1203    case OPT_frtti:
1204      flag_rtti = on;
1205      break;
1206
1207    case OPT_fshow_column:
1208      cpp_opts->show_column = on;
1209      break;
1210
1211    case OPT_fstats:
1212      flag_detailed_statistics = on;
1213      break;
1214
1215    case OPT_ftabstop:
1216      /* Don't recognize -fno-tabstop=.  */
1217      if (!on)
1218	return 0;
1219
1220      /* It is documented that we silently ignore silly values.  */
1221	{
1222	  char *endptr;
1223	  long tabstop = strtol (arg, &endptr, 10);
1224	  if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1225	    cpp_opts->tabstop = tabstop;
1226	}
1227      break;
1228
1229    case OPT_ftemplate_depth:
1230      max_tinst_depth = read_integral_parameter (arg, argv[0], 0);
1231      break;
1232
1233    case OPT_fvtable_gc:
1234      flag_vtable_gc = on;
1235      break;
1236
1237    case OPT_fuse_cxa_atexit:
1238      flag_use_cxa_atexit = on;
1239      break;
1240
1241    case OPT_fweak:
1242      flag_weak = on;
1243      break;
1244
1245    case OPT_gen_decls:
1246      flag_gen_declaration = 1;
1247      break;
1248
1249    case OPT_lang_asm:
1250      cpp_set_lang (parse_in, CLK_ASM);
1251      break;
1252
1253    case OPT_lang_objc:
1254      cpp_opts->objc = 1;
1255      break;
1256
1257    case OPT_nostdinc:
1258      /* No default include directories.  You must specify all
1259	 include-file directories with -I.  */
1260      cpp_opts->no_standard_includes = 1;
1261      break;
1262
1263    case OPT_nostdincplusplus:
1264      /* No default C++-specific include directories.  */
1265      cpp_opts->no_standard_cplusplus_includes = 1;
1266      break;
1267
1268    case OPT_o:
1269      if (!out_fname)
1270	out_fname = arg;
1271      else
1272	{
1273	  error ("output filename specified twice");
1274	  result = argc;
1275	}
1276      break;
1277
1278      /* We need to handle the -pedantic switches here, rather than in
1279	 c_common_post_options, so that a subsequent -Wno-endif-labels
1280	 is not overridden.  */
1281    case OPT_pedantic_errors:
1282      cpp_opts->pedantic_errors = 1;
1283      /* fall through */
1284    case OPT_pedantic:
1285      cpp_opts->pedantic = 1;
1286      cpp_opts->warn_endif_labels = 1;
1287      break;
1288
1289    case OPT_print_objc_runtime_info:
1290      print_struct_values = 1;
1291      break;
1292
1293    case OPT_remap:
1294      cpp_opts->remap = 1;
1295      break;
1296
1297    case OPT_std_cplusplus98:
1298    case OPT_std_gnuplusplus98:
1299      set_std_cxx98 (code == OPT_std_cplusplus98 /* ISO */);
1300      break;
1301
1302    case OPT_std_c89:
1303    case OPT_std_iso9899_1990:
1304    case OPT_std_iso9899_199409:
1305      set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1306      break;
1307
1308    case OPT_std_gnu89:
1309      set_std_c89 (false /* c94 */, false /* ISO */);
1310      break;
1311
1312    case OPT_std_c99:
1313    case OPT_std_c9x:
1314    case OPT_std_iso9899_1999:
1315    case OPT_std_iso9899_199x:
1316      set_std_c99 (true /* ISO */);
1317      break;
1318
1319    case OPT_std_gnu99:
1320    case OPT_std_gnu9x:
1321      set_std_c99 (false /* ISO */);
1322      break;
1323
1324    case OPT_trigraphs:
1325      cpp_opts->trigraphs = 1;
1326      break;
1327
1328    case OPT_traditional_cpp:
1329      cpp_opts->traditional = 1;
1330      break;
1331
1332    case OPT_undef:
1333      flag_undef = 1;
1334      break;
1335
1336    case OPT_w:
1337      cpp_opts->inhibit_warnings = 1;
1338      break;
1339
1340    case OPT_v:
1341      cpp_opts->verbose = 1;
1342      break;
1343    }
1344
1345 done:
1346  if (dup)
1347    free (dup);
1348  return result;
1349}
1350
1351/* Post-switch processing.  */
1352bool
1353c_common_post_options ()
1354{
1355  /* Canonicalize the input and output filenames.  */
1356  if (in_fname == NULL || !strcmp (in_fname, "-"))
1357    in_fname = "";
1358
1359  if (out_fname == NULL || !strcmp (out_fname, "-"))
1360    out_fname = "";
1361
1362  if (cpp_opts->deps.style == DEPS_NONE)
1363    check_deps_environment_vars ();
1364
1365  handle_deferred_opts ();
1366
1367  sanitize_cpp_opts ();
1368
1369  flag_inline_trees = 1;
1370
1371  /* Use tree inlining if possible.  Function instrumentation is only
1372     done in the RTL level, so we disable tree inlining.  */
1373  if (! flag_instrument_function_entry_exit)
1374    {
1375      if (!flag_no_inline)
1376	flag_no_inline = 1;
1377      if (flag_inline_functions)
1378	{
1379	  flag_inline_trees = 2;
1380	  flag_inline_functions = 0;
1381	}
1382    }
1383
1384  /* Special format checking options don't work without -Wformat; warn if
1385     they are used.  */
1386  if (warn_format_y2k && !warn_format)
1387    warning ("-Wformat-y2k ignored without -Wformat");
1388  if (warn_format_extra_args && !warn_format)
1389    warning ("-Wformat-extra-args ignored without -Wformat");
1390  if (warn_format_zero_length && !warn_format)
1391    warning ("-Wformat-zero-length ignored without -Wformat");
1392  if (warn_format_nonliteral && !warn_format)
1393    warning ("-Wformat-nonliteral ignored without -Wformat");
1394  if (warn_format_security && !warn_format)
1395    warning ("-Wformat-security ignored without -Wformat");
1396  if (warn_missing_format_attribute && !warn_format)
1397    warning ("-Wmissing-format-attribute ignored without -Wformat");
1398
1399  /* If an error has occurred in cpplib, note it so we fail
1400     immediately.  */
1401  errorcount += cpp_errors (parse_in);
1402
1403  return flag_preprocess_only;
1404}
1405
1406/* Preprocess the input file to out_stream.  */
1407static void
1408preprocess_file ()
1409{
1410  /* Open the output now.  We must do so even if no_output is on,
1411     because there may be other output than from the actual
1412     preprocessing (e.g. from -dM).  */
1413  if (out_fname[0] == '\0')
1414    out_stream = stdout;
1415  else
1416    out_stream = fopen (out_fname, "w");
1417
1418  if (out_stream == NULL)
1419    fatal_io_error ("opening output file %s", out_fname);
1420  else
1421    cpp_preprocess_file (parse_in, in_fname, out_stream);
1422}
1423
1424/* Front end initialization common to C, ObjC and C++.  */
1425const char *
1426c_common_init (filename)
1427     const char *filename;
1428{
1429  /* Set up preprocessor arithmetic.  Must be done after call to
1430     c_common_nodes_and_builtins for type nodes to be good.  */
1431  cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1432  cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1433  cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1434  cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1435  cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1436
1437  /* Register preprocessor built-ins before calls to
1438     cpp_main_file.  */
1439  cpp_get_callbacks (parse_in)->register_builtins = cb_register_builtins;
1440
1441  /* NULL is passed up to toplev.c and we exit quickly.  */
1442  if (flag_preprocess_only)
1443    {
1444      preprocess_file ();
1445      return NULL;
1446    }
1447
1448  /* Do this before initializing pragmas, as then cpplib's hash table
1449     has been set up.  NOTE: we are using our own file name here, not
1450     the one supplied.  */
1451  filename = init_c_lex (in_fname);
1452
1453  init_pragma ();
1454
1455  return filename;
1456}
1457
1458/* Common finish hook for the C, ObjC and C++ front ends.  */
1459void
1460c_common_finish ()
1461{
1462  FILE *deps_stream = NULL;
1463
1464  if (cpp_opts->deps.style != DEPS_NONE)
1465    {
1466      /* If -M or -MM was seen without -MF, default output to the
1467	 output stream.  */
1468      if (!deps_file)
1469	deps_stream = out_stream;
1470      else
1471	{
1472	  deps_stream = fopen (deps_file, deps_append ? "a": "w");
1473	  if (!deps_stream)
1474	    fatal_io_error ("opening dependency file %s", deps_file);
1475	}
1476    }
1477
1478  /* For performance, avoid tearing down cpplib's internal structures
1479     with cpp_destroy ().  */
1480  errorcount += cpp_finish (parse_in, deps_stream);
1481
1482  if (deps_stream && deps_stream != out_stream
1483      && (ferror (deps_stream) || fclose (deps_stream)))
1484    fatal_io_error ("closing dependency file %s", deps_file);
1485
1486  if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1487    fatal_io_error ("when writing output to %s", out_fname);
1488}
1489
1490/* Either of two environment variables can specify output of
1491   dependencies.  Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1492   DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1493   and DEPS_TARGET is the target to mention in the deps.  They also
1494   result in dependency information being appended to the output file
1495   rather than overwriting it, and like Sun's compiler
1496   SUNPRO_DEPENDENCIES suppresses the dependency on the main file.  */
1497static void
1498check_deps_environment_vars ()
1499{
1500  char *spec;
1501
1502  GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1503  if (spec)
1504    cpp_opts->deps.style = DEPS_USER;
1505  else
1506    {
1507      GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1508      if (spec)
1509	{
1510	  cpp_opts->deps.style = DEPS_SYSTEM;
1511	  cpp_opts->deps.ignore_main_file = true;
1512	}
1513    }
1514
1515  if (spec)
1516    {
1517      /* Find the space before the DEPS_TARGET, if there is one.  */
1518      char *s = strchr (spec, ' ');
1519      if (s)
1520	{
1521	  /* Let the caller perform MAKE quoting.  */
1522	  defer_opt (OPT_MT, s + 1);
1523	  *s = '\0';
1524	}
1525
1526      /* Command line -MF overrides environment variables and default.  */
1527      if (!deps_file)
1528	deps_file = spec;
1529
1530      deps_append = 1;
1531    }
1532}
1533
1534/* Handle deferred command line switches.  */
1535static void
1536handle_deferred_opts ()
1537{
1538  size_t i;
1539
1540  for (i = 0; i < deferred_count; i++)
1541    {
1542      struct deferred_opt *opt = &deferred_opts[i];
1543
1544      switch (opt->code)
1545	{
1546	case OPT_MT:
1547	case OPT_MQ:
1548	  cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1549	  break;
1550
1551	default:
1552	  abort ();
1553	}
1554    }
1555
1556  free (deferred_opts);
1557}
1558
1559/* These settings are appropriate for GCC, but not necessarily so for
1560   cpplib as a library.  */
1561static void
1562sanitize_cpp_opts ()
1563{
1564  /* If we don't know what style of dependencies to output, complain
1565     if any other dependency switches have been given.  */
1566  if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1567    error ("to generate dependencies you must specify either -M or -MM");
1568
1569  /* -dM and dependencies suppress normal output; do it here so that
1570     the last -d[MDN] switch overrides earlier ones.  */
1571  if (cpp_opts->dump_macros == dump_only)
1572    cpp_opts->no_output = 1;
1573
1574  /* Disable -dD, -dN and -dI if normal output is suppressed.  Allow
1575     -dM since at least glibc relies on -M -dM to work.  */
1576  if (cpp_opts->no_output)
1577    {
1578      if (cpp_opts->dump_macros != dump_only)
1579	cpp_opts->dump_macros = dump_none;
1580      cpp_opts->dump_includes = 0;
1581    }
1582
1583  cpp_opts->unsigned_char = !flag_signed_char;
1584  cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1585
1586  /* We want -Wno-long-long to override -pedantic -std=non-c99
1587     and/or -Wtraditional, whatever the ordering.  */
1588  cpp_opts->warn_long_long
1589    = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1590}
1591
1592/* Set the C 89 standard (with 1994 amendments if C94, without GNU
1593   extensions if ISO).  There is no concept of gnu94.  */
1594static void
1595set_std_c89 (c94, iso)
1596     int c94, iso;
1597{
1598  cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1599  flag_iso = iso;
1600  flag_no_asm = iso;
1601  flag_no_gnu_keywords = iso;
1602  flag_no_nonansi_builtin = iso;
1603  flag_noniso_default_format_attributes = !iso;
1604  flag_isoc94 = c94;
1605  flag_isoc99 = 0;
1606  flag_writable_strings = 0;
1607}
1608
1609/* Set the C 99 standard (without GNU extensions if ISO).  */
1610static void
1611set_std_c99 (iso)
1612     int iso;
1613{
1614  cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1615  flag_no_asm = iso;
1616  flag_no_nonansi_builtin = iso;
1617  flag_noniso_default_format_attributes = !iso;
1618  flag_iso = iso;
1619  flag_isoc99 = 1;
1620  flag_isoc94 = 1;
1621  flag_writable_strings = 0;
1622}
1623
1624/* Set the C++ 98 standard (without GNU extensions if ISO).  */
1625static void
1626set_std_cxx98 (iso)
1627     int iso;
1628{
1629  cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1630  flag_no_gnu_keywords = iso;
1631  flag_no_nonansi_builtin = iso;
1632  flag_noniso_default_format_attributes = !iso;
1633  flag_iso = iso;
1634}
1635
1636/* Handle setting implicit to ON.  */
1637static void
1638set_Wimplicit (on)
1639     int on;
1640{
1641  warn_implicit = on;
1642  warn_implicit_int = on;
1643  if (on)
1644    {
1645      if (mesg_implicit_function_declaration != 2)
1646	mesg_implicit_function_declaration = 1;
1647    }
1648  else
1649    mesg_implicit_function_declaration = 0;
1650}
1651
1652/* Args to -d specify what to dump.  Silently ignore
1653   unrecognized options; they may be aimed at toplev.c.  */
1654static void
1655handle_OPT_d (arg)
1656     const char *arg;
1657{
1658  char c;
1659
1660  while ((c = *arg++) != '\0')
1661    switch (c)
1662      {
1663      case 'M':
1664	cpp_opts->dump_macros = dump_only;
1665	break;
1666
1667      case 'N':
1668	cpp_opts->dump_macros = dump_names;
1669	break;
1670
1671      case 'D':
1672	cpp_opts->dump_macros = dump_definitions;
1673	break;
1674
1675      case 'I':
1676	cpp_opts->dump_includes = 1;
1677	break;
1678      }
1679}
1680
1681/* Write a slash-separated list of languages in FLAGS to BUF.  */
1682static void
1683write_langs (buf, flags)
1684     char *buf;
1685     int flags;
1686{
1687  *buf = '\0';
1688  if (flags & CL_C_ONLY)
1689    strcat (buf, "C");
1690  if (flags & CL_OBJC_ONLY)
1691    {
1692      if (*buf)
1693	strcat (buf, "/");
1694      strcat (buf, "ObjC");
1695    }
1696  if (flags & CL_CXX_ONLY)
1697    {
1698      if (*buf)
1699	strcat (buf, "/");
1700      strcat (buf, "C++");
1701    }
1702}
1703
1704/* Complain that switch OPT_INDEX does not apply to this front end.  */
1705static void
1706complain_wrong_lang (opt_index, on)
1707     size_t opt_index;
1708     int on;
1709{
1710  char ok_langs[60], bad_langs[60];
1711  int ok_flags = cl_options[opt_index].flags;
1712
1713  write_langs (ok_langs, ok_flags);
1714  write_langs (bad_langs, ~ok_flags);
1715  warning ("\"-%c%s%s\" is valid for %s but not for %s",
1716	   cl_options[opt_index].opt_text[0], on ? "" : "no-",
1717	   cl_options[opt_index].opt_text + 1, ok_langs, bad_langs);
1718}
1719
1720/* Handle --help output.  */
1721static void
1722print_help ()
1723{
1724  /* To keep the lines from getting too long for some compilers, limit
1725     to about 500 characters (6 lines) per chunk.  */
1726  fputs (_("\
1727Switches:\n\
1728  -include <file>           Include the contents of <file> before other files\n\
1729  -imacros <file>           Accept definition of macros in <file>\n\
1730  -iprefix <path>           Specify <path> as a prefix for next two options\n\
1731  -iwithprefix <dir>        Add <dir> to the end of the system include path\n\
1732  -iwithprefixbefore <dir>  Add <dir> to the end of the main include path\n\
1733  -isystem <dir>            Add <dir> to the start of the system include path\n\
1734"), stdout);
1735  fputs (_("\
1736  -idirafter <dir>          Add <dir> to the end of the system include path\n\
1737  -I <dir>                  Add <dir> to the end of the main include path\n\
1738  -I-                       Fine-grained include path control; see info docs\n\
1739  -nostdinc                 Do not search system include directories\n\
1740                             (dirs specified with -isystem will still be used)\n\
1741  -nostdinc++               Do not search system include directories for C++\n\
1742  -o <file>                 Put output into <file>\n\
1743"), stdout);
1744  fputs (_("\
1745  -trigraphs                Support ISO C trigraphs\n\
1746  -std=<std name>           Specify the conformance standard; one of:\n\
1747                            gnu89, gnu99, c89, c99, iso9899:1990,\n\
1748                            iso9899:199409, iso9899:1999, c++98\n\
1749  -w                        Inhibit warning messages\n\
1750  -W[no-]trigraphs          Warn if trigraphs are encountered\n\
1751  -W[no-]comment{s}         Warn if one comment starts inside another\n\
1752"), stdout);
1753  fputs (_("\
1754  -W[no-]traditional        Warn about features not present in traditional C\n\
1755  -W[no-]undef              Warn if an undefined macro is used by #if\n\
1756  -W[no-]import             Warn about the use of the #import directive\n\
1757"), stdout);
1758  fputs (_("\
1759  -W[no-]error              Treat all warnings as errors\n\
1760  -W[no-]system-headers     Do not suppress warnings from system headers\n\
1761  -W[no-]all                Enable most preprocessor warnings\n\
1762"), stdout);
1763  fputs (_("\
1764  -M                        Generate make dependencies\n\
1765  -MM                       As -M, but ignore system header files\n\
1766  -MD                       Generate make dependencies and compile\n\
1767  -MMD                      As -MD, but ignore system header files\n\
1768  -MF <file>                Write dependency output to the given file\n\
1769  -MG                       Treat missing header file as generated files\n\
1770"), stdout);
1771  fputs (_("\
1772  -MP			    Generate phony targets for all headers\n\
1773  -MQ <target>              Add a MAKE-quoted target\n\
1774  -MT <target>              Add an unquoted target\n\
1775"), stdout);
1776  fputs (_("\
1777  -D<macro>                 Define a <macro> with string '1' as its value\n\
1778  -D<macro>=<val>           Define a <macro> with <val> as its value\n\
1779  -A<question>=<answer>     Assert the <answer> to <question>\n\
1780  -A-<question>=<answer>    Disable the <answer> to <question>\n\
1781  -U<macro>                 Undefine <macro> \n\
1782  -v                        Display the version number\n\
1783"), stdout);
1784  fputs (_("\
1785  -H                        Print the name of header files as they are used\n\
1786  -C                        Do not discard comments\n\
1787  -dM                       Display a list of macro definitions active at end\n\
1788  -dD                       Preserve macro definitions in output\n\
1789  -dN                       As -dD except that only the names are preserved\n\
1790  -dI                       Include #include directives in the output\n\
1791"), stdout);
1792  fputs (_("\
1793  -f[no-]preprocessed       Treat the input file as already preprocessed\n\
1794  -ftabstop=<number>        Distance between tab stops for column reporting\n\
1795  -P                        Do not generate #line directives\n\
1796  -remap                    Remap file names when including files\n\
1797  --help                    Display this information\n\
1798"), stdout);
1799}
1800