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