PR target/9164
[official-gcc.git] / gcc / c-opts.c
blobadab26d719590521745748afe3890d3fd0219bf5
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"
37 #include "debug.h" /* For debug_hooks. */
39 #ifndef TARGET_SYSTEM_ROOT
40 # define TARGET_SYSTEM_ROOT NULL
41 #endif
43 static int saved_lineno;
45 /* CPP's options. */
46 static cpp_options *cpp_opts;
48 /* Input filename. */
49 static const char *in_fname;
51 /* Filename and stream for preprocessed output. */
52 static const char *out_fname;
53 static FILE *out_stream;
55 /* Append dependencies to deps_file. */
56 static bool deps_append;
58 /* If dependency switches (-MF etc.) have been given. */
59 static bool deps_seen;
61 /* If -v seen. */
62 static bool verbose;
64 /* Dependency output file. */
65 static const char *deps_file;
67 /* The prefix given by -iprefix, if any. */
68 static const char *iprefix;
70 /* The system root, if any. Overridden by -isysroot. */
71 static const char *sysroot = TARGET_SYSTEM_ROOT;
73 /* Zero disables all standard directories for headers. */
74 static bool std_inc = true;
76 /* Zero disables the C++-specific standard directories for headers. */
77 static bool std_cxx_inc = true;
79 /* If the quote chain has been split by -I-. */
80 static bool quote_chain_split;
82 /* If -Wunused-macros. */
83 static bool warn_unused_macros;
85 /* Number of deferred options, deferred options array size. */
86 static size_t deferred_count, deferred_size;
88 /* Number of deferred options scanned for -include. */
89 static size_t include_cursor;
91 static void missing_arg PARAMS ((size_t));
92 static size_t find_opt PARAMS ((const char *, int));
93 static void set_Wimplicit PARAMS ((int));
94 static void complain_wrong_lang PARAMS ((size_t));
95 static void write_langs PARAMS ((char *, int));
96 static void print_help PARAMS ((void));
97 static void handle_OPT_d PARAMS ((const char *));
98 static void set_std_cxx98 PARAMS ((int));
99 static void set_std_c89 PARAMS ((int, int));
100 static void set_std_c99 PARAMS ((int));
101 static void check_deps_environment_vars PARAMS ((void));
102 static void handle_deferred_opts PARAMS ((void));
103 static void sanitize_cpp_opts PARAMS ((void));
104 static void add_prefixed_path PARAMS ((const char *, size_t));
105 static void push_command_line_include PARAMS ((void));
106 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
107 static void finish_options PARAMS ((void));
109 #ifndef STDC_0_IN_SYSTEM_HEADERS
110 #define STDC_0_IN_SYSTEM_HEADERS 0
111 #endif
113 #define CL_C_ONLY (1 << 0) /* Only C. */
114 #define CL_OBJC_ONLY (1 << 1) /* Only ObjC. */
115 #define CL_CXX_ONLY (1 << 2) /* Only C++. */
116 #define CL_OBJCXX_ONLY (1 << 3) /* Only ObjC++. */
117 #define CL_JOINED (1 << 4) /* If takes joined argument. */
118 #define CL_SEPARATE (1 << 5) /* If takes a separate argument. */
120 #define CL_ARG (CL_JOINED | CL_SEPARATE)
121 #define CL_C (CL_C_ONLY | CL_OBJC_ONLY)
122 #define CL_OBJC (CL_OBJC_ONLY | CL_OBJCXX_ONLY)
123 #define CL_CXX (CL_CXX_ONLY | CL_OBJCXX_ONLY)
124 #define CL_ALL (CL_C | CL_CXX)
126 /* This is the list of all command line options, with the leading "-"
127 removed. It must be sorted in ASCII collating order. All options
128 beginning with "f" or "W" are implicitly assumed to take a "no-"
129 form; this form should not be listed. The variable "on" is true if
130 the positive form is given, otherwise it is false. If you don't
131 want to allow a "no-" form, your handler should reject "on" being
132 false by returning zero. See, for example, the handling of
133 -ftabstop=.
135 If the user gives an option to a front end that doesn't support it,
136 an error is output, mentioning which front ends the option is valid
137 for. If you don't want this, you must accept it for all front
138 ends, and test for the front end in the option handler. See, for
139 example, the handling of -Wno-strict-prototypes for C++.
141 If you request an argument with CL_JOINED, CL_SEPARATE or their
142 combination CL_ARG, it is stored in the variable "arg", which is
143 guaranteed to be non-NULL and to not be an empty string. It points
144 to the argument either within the argv[] vector or within one of
145 that vector's strings, and so the text is permanent and copies need
146 not be made. Be sure to add an error message in missing_arg() if
147 the default is not appropriate. */
149 #define COMMAND_LINE_OPTIONS \
150 OPT("-help", CL_ALL, OPT__help) \
151 OPT("-output-pch=", CL_ALL | CL_ARG, OPT__output_pch) \
152 OPT("A", CL_ALL | CL_ARG, OPT_A) \
153 OPT("C", CL_ALL, OPT_C) \
154 OPT("CC", CL_ALL, OPT_CC) \
155 OPT("D", CL_ALL | CL_ARG, OPT_D) \
156 OPT("E", CL_ALL, OPT_E) \
157 OPT("H", CL_ALL, OPT_H) \
158 OPT("I", CL_ALL | CL_ARG, OPT_I) \
159 OPT("M", CL_ALL, OPT_M) \
160 OPT("MD", CL_ALL | CL_SEPARATE, OPT_MD) \
161 OPT("MF", CL_ALL | CL_ARG, OPT_MF) \
162 OPT("MG", CL_ALL, OPT_MG) \
163 OPT("MM", CL_ALL, OPT_MM) \
164 OPT("MMD", CL_ALL | CL_SEPARATE, OPT_MMD) \
165 OPT("MP", CL_ALL, OPT_MP) \
166 OPT("MQ", CL_ALL | CL_ARG, OPT_MQ) \
167 OPT("MT", CL_ALL | CL_ARG, OPT_MT) \
168 OPT("P", CL_ALL, OPT_P) \
169 OPT("U", CL_ALL | CL_ARG, OPT_U) \
170 OPT("Wabi", CL_CXX, OPT_Wabi) \
171 OPT("Wall", CL_ALL, OPT_Wall) \
172 OPT("Wbad-function-cast", CL_C, OPT_Wbad_function_cast) \
173 OPT("Wcast-qual", CL_ALL, OPT_Wcast_qual) \
174 OPT("Wchar-subscripts", CL_ALL, OPT_Wchar_subscripts) \
175 OPT("Wcomment", CL_ALL, OPT_Wcomment) \
176 OPT("Wcomments", CL_ALL, OPT_Wcomments) \
177 OPT("Wconversion", CL_ALL, OPT_Wconversion) \
178 OPT("Wctor-dtor-privacy", CL_CXX, OPT_Wctor_dtor_privacy) \
179 OPT("Wdeprecated", CL_CXX, OPT_Wdeprecated) \
180 OPT("Wdiv-by-zero", CL_C, OPT_Wdiv_by_zero) \
181 OPT("Weffc++", CL_CXX, OPT_Weffcxx) \
182 OPT("Wendif-labels", CL_ALL, OPT_Wendif_labels) \
183 OPT("Werror", CL_ALL, OPT_Werror) \
184 OPT("Werror-implicit-function-declaration", \
185 CL_C, OPT_Werror_implicit_function_decl) \
186 OPT("Wfloat-equal", CL_ALL, OPT_Wfloat_equal) \
187 OPT("Wformat", CL_ALL, OPT_Wformat) \
188 OPT("Wformat-extra-args", CL_ALL, OPT_Wformat_extra_args) \
189 OPT("Wformat-nonliteral", CL_ALL, OPT_Wformat_nonliteral) \
190 OPT("Wformat-security", CL_ALL, OPT_Wformat_security) \
191 OPT("Wformat-y2k", CL_ALL, OPT_Wformat_y2k) \
192 OPT("Wformat-zero-length", CL_C, OPT_Wformat_zero_length) \
193 OPT("Wformat=", CL_ALL | CL_JOINED, OPT_Wformat_eq) \
194 OPT("Wimplicit", CL_ALL, OPT_Wimplicit) \
195 OPT("Wimplicit-function-declaration", CL_C, OPT_Wimplicit_function_decl) \
196 OPT("Wimplicit-int", CL_C, OPT_Wimplicit_int) \
197 OPT("Wimport", CL_ALL, OPT_Wimport) \
198 OPT("Winvalid-pch", CL_ALL, OPT_Winvalid_pch) \
199 OPT("Wlong-long", CL_ALL, OPT_Wlong_long) \
200 OPT("Wmain", CL_C, OPT_Wmain) \
201 OPT("Wmissing-braces", CL_ALL, OPT_Wmissing_braces) \
202 OPT("Wmissing-declarations", CL_C, OPT_Wmissing_declarations) \
203 OPT("Wmissing-format-attribute",CL_ALL, OPT_Wmissing_format_attribute) \
204 OPT("Wmissing-prototypes", CL_ALL, OPT_Wmissing_prototypes) \
205 OPT("Wmultichar", CL_ALL, OPT_Wmultichar) \
206 OPT("Wnested-externs", CL_C, OPT_Wnested_externs) \
207 OPT("Wnon-template-friend", CL_CXX, OPT_Wnon_template_friend) \
208 OPT("Wnon-virtual-dtor", CL_CXX, OPT_Wnon_virtual_dtor) \
209 OPT("Wnonnull", CL_C, OPT_Wnonnull) \
210 OPT("Wold-style-cast", CL_CXX, OPT_Wold_style_cast) \
211 OPT("Woverloaded-virtual", CL_CXX, OPT_Woverloaded_virtual) \
212 OPT("Wparentheses", CL_ALL, OPT_Wparentheses) \
213 OPT("Wpmf-conversions", CL_CXX, OPT_Wpmf_conversions) \
214 OPT("Wpointer-arith", CL_ALL, OPT_Wpointer_arith) \
215 OPT("Wprotocol", CL_OBJC, OPT_Wprotocol) \
216 OPT("Wredundant-decls", CL_ALL, OPT_Wredundant_decls) \
217 OPT("Wreorder", CL_CXX, OPT_Wreorder) \
218 OPT("Wreturn-type", CL_ALL, OPT_Wreturn_type) \
219 OPT("Wselector", CL_OBJC, OPT_Wselector) \
220 OPT("Wsequence-point", CL_C, OPT_Wsequence_point) \
221 OPT("Wsign-compare", CL_ALL, OPT_Wsign_compare) \
222 OPT("Wsign-promo", CL_CXX, OPT_Wsign_promo) \
223 OPT("Wstrict-prototypes", CL_ALL, OPT_Wstrict_prototypes) \
224 OPT("Wsynth", CL_CXX, OPT_Wsynth) \
225 OPT("Wsystem-headers", CL_ALL, OPT_Wsystem_headers) \
226 OPT("Wtraditional", CL_C, OPT_Wtraditional) \
227 OPT("Wtrigraphs", CL_ALL, OPT_Wtrigraphs) \
228 OPT("Wundeclared-selector", CL_OBJC, OPT_Wundeclared_selector) \
229 OPT("Wundef", CL_ALL, OPT_Wundef) \
230 OPT("Wunknown-pragmas", CL_ALL, OPT_Wunknown_pragmas) \
231 OPT("Wunused-macros", CL_ALL, OPT_Wunused_macros) \
232 OPT("Wwrite-strings", CL_ALL, OPT_Wwrite_strings) \
233 OPT("ansi", CL_ALL, OPT_ansi) \
234 OPT("d", CL_ALL | CL_JOINED, OPT_d) \
235 OPT("fabi-version=", CL_CXX | CL_JOINED, OPT_fabi_version) \
236 OPT("faccess-control", CL_CXX, OPT_faccess_control) \
237 OPT("fall-virtual", CL_CXX, OPT_fall_virtual) \
238 OPT("falt-external-templates",CL_CXX, OPT_falt_external_templates) \
239 OPT("fasm", CL_ALL, OPT_fasm) \
240 OPT("fbuiltin", CL_ALL, OPT_fbuiltin) \
241 OPT("fbuiltin-", CL_ALL | CL_JOINED, OPT_fbuiltin_) \
242 OPT("fcheck-new", CL_CXX, OPT_fcheck_new) \
243 OPT("fcond-mismatch", CL_ALL, OPT_fcond_mismatch) \
244 OPT("fconserve-space", CL_CXX, OPT_fconserve_space) \
245 OPT("fconst-strings", CL_CXX, OPT_fconst_strings) \
246 OPT("fconstant-string-class=", CL_OBJC | CL_JOINED, \
247 OPT_fconstant_string_class) \
248 OPT("fdefault-inline", CL_CXX, OPT_fdefault_inline) \
249 OPT("fdollars-in-identifiers",CL_ALL, OPT_fdollars_in_identifiers) \
250 OPT("fdump-", CL_ALL | CL_JOINED, OPT_fdump) \
251 OPT("felide-constructors", CL_CXX, OPT_felide_constructors) \
252 OPT("fenforce-eh-specs", CL_CXX, OPT_fenforce_eh_specs) \
253 OPT("fenum-int-equiv", CL_CXX, OPT_fenum_int_equiv) \
254 OPT("fexternal-templates", CL_CXX, OPT_fexternal_templates) \
255 OPT("ffixed-form", CL_C, OPT_ffixed_form) \
256 OPT("ffixed-line-length-", CL_C | CL_JOINED, OPT_ffixed_line_length) \
257 OPT("ffor-scope", CL_CXX, OPT_ffor_scope) \
258 OPT("ffreestanding", CL_C, OPT_ffreestanding) \
259 OPT("fgnu-keywords", CL_CXX, OPT_fgnu_keywords) \
260 OPT("fgnu-runtime", CL_OBJC, OPT_fgnu_runtime) \
261 OPT("fguiding-decls", CL_CXX, OPT_fguiding_decls) \
262 OPT("fhandle-exceptions", CL_CXX, OPT_fhandle_exceptions) \
263 OPT("fhonor-std", CL_CXX, OPT_fhonor_std) \
264 OPT("fhosted", CL_C, OPT_fhosted) \
265 OPT("fhuge-objects", CL_CXX, OPT_fhuge_objects) \
266 OPT("fimplement-inlines", CL_CXX, OPT_fimplement_inlines) \
267 OPT("fimplicit-inline-templates", CL_CXX, OPT_fimplicit_inline_templates) \
268 OPT("fimplicit-templates", CL_CXX, OPT_fimplicit_templates) \
269 OPT("flabels-ok", CL_CXX, OPT_flabels_ok) \
270 OPT("fms-extensions", CL_ALL, OPT_fms_extensions) \
271 OPT("fname-mangling-version-",CL_CXX | CL_JOINED, OPT_fname_mangling) \
272 OPT("fnew-abi", CL_CXX, OPT_fnew_abi) \
273 OPT("fnext-runtime", CL_OBJC, OPT_fnext_runtime) \
274 OPT("fnonansi-builtins", CL_CXX, OPT_fnonansi_builtins) \
275 OPT("fnonnull-objects", CL_CXX, OPT_fnonnull_objects) \
276 OPT("foperator-names", CL_CXX, OPT_foperator_names) \
277 OPT("foptional-diags", CL_CXX, OPT_foptional_diags) \
278 OPT("fpch-deps", CL_ALL, OPT_fpch_deps) \
279 OPT("fpermissive", CL_CXX, OPT_fpermissive) \
280 OPT("fpreprocessed", CL_ALL, OPT_fpreprocessed) \
281 OPT("frepo", CL_CXX, OPT_frepo) \
282 OPT("frtti", CL_CXX, OPT_frtti) \
283 OPT("fshort-double", CL_ALL, OPT_fshort_double) \
284 OPT("fshort-enums", CL_ALL, OPT_fshort_enums) \
285 OPT("fshort-wchar", CL_ALL, OPT_fshort_wchar) \
286 OPT("fshow-column", CL_ALL, OPT_fshow_column) \
287 OPT("fsigned-bitfields", CL_ALL, OPT_fsigned_bitfields) \
288 OPT("fsigned-char", CL_ALL, OPT_fsigned_char) \
289 OPT("fsquangle", CL_CXX, OPT_fsquangle) \
290 OPT("fstats", CL_CXX, OPT_fstats) \
291 OPT("fstrict-prototype", CL_CXX, OPT_fstrict_prototype) \
292 OPT("ftabstop=", CL_ALL | CL_JOINED, OPT_ftabstop) \
293 OPT("ftemplate-depth-", CL_CXX | CL_JOINED, OPT_ftemplate_depth) \
294 OPT("fthis-is-variable", CL_CXX, OPT_fthis_is_variable) \
295 OPT("funsigned-bitfields", CL_ALL, OPT_funsigned_bitfields) \
296 OPT("funsigned-char", CL_ALL, OPT_funsigned_char) \
297 OPT("fuse-cxa-atexit", CL_CXX, OPT_fuse_cxa_atexit) \
298 OPT("fvtable-gc", CL_CXX, OPT_fvtable_gc) \
299 OPT("fvtable-thunks", CL_CXX, OPT_fvtable_thunks) \
300 OPT("fweak", CL_CXX, OPT_fweak) \
301 OPT("fxref", CL_CXX, OPT_fxref) \
302 OPT("gen-decls", CL_OBJC, OPT_gen_decls) \
303 OPT("idirafter", CL_ALL | CL_ARG, OPT_idirafter) \
304 OPT("imacros", CL_ALL | CL_ARG, OPT_imacros) \
305 OPT("include", CL_ALL | CL_ARG, OPT_include) \
306 OPT("iprefix", CL_ALL | CL_ARG, OPT_iprefix) \
307 OPT("isysroot", CL_ALL | CL_ARG, OPT_isysroot) \
308 OPT("isystem", CL_ALL | CL_ARG, OPT_isystem) \
309 OPT("iwithprefix", CL_ALL | CL_ARG, OPT_iwithprefix) \
310 OPT("iwithprefixbefore", CL_ALL | CL_ARG, OPT_iwithprefixbefore) \
311 OPT("lang-asm", CL_C_ONLY, OPT_lang_asm) \
312 OPT("lang-objc", CL_ALL, OPT_lang_objc) \
313 OPT("nostdinc", CL_ALL, OPT_nostdinc) \
314 OPT("nostdinc++", CL_ALL, OPT_nostdincplusplus) \
315 OPT("o", CL_ALL | CL_ARG, OPT_o) \
316 OPT("pedantic", CL_ALL, OPT_pedantic) \
317 OPT("pedantic-errors", CL_ALL, OPT_pedantic_errors) \
318 OPT("print-objc-runtime-info", CL_OBJC, OPT_print_objc_runtime_info) \
319 OPT("remap", CL_ALL, OPT_remap) \
320 OPT("std=c++98", CL_CXX, OPT_std_cplusplus98) \
321 OPT("std=c89", CL_C, OPT_std_c89) \
322 OPT("std=c99", CL_C, OPT_std_c99) \
323 OPT("std=c9x", CL_C, OPT_std_c9x) \
324 OPT("std=gnu++98", CL_CXX, OPT_std_gnuplusplus98) \
325 OPT("std=gnu89", CL_C, OPT_std_gnu89) \
326 OPT("std=gnu99", CL_C, OPT_std_gnu99) \
327 OPT("std=gnu9x", CL_C, OPT_std_gnu9x) \
328 OPT("std=iso9899:1990", CL_C, OPT_std_iso9899_1990) \
329 OPT("std=iso9899:199409", CL_C, OPT_std_iso9899_199409) \
330 OPT("std=iso9899:1999", CL_C, OPT_std_iso9899_1999) \
331 OPT("std=iso9899:199x", CL_C, OPT_std_iso9899_199x) \
332 OPT("traditional-cpp", CL_ALL, OPT_traditional_cpp) \
333 OPT("trigraphs", CL_ALL, OPT_trigraphs) \
334 OPT("undef", CL_ALL, OPT_undef) \
335 OPT("v", CL_ALL, OPT_v) \
336 OPT("w", CL_ALL, OPT_w)
338 #define OPT(text, flags, code) code,
339 enum opt_code
341 COMMAND_LINE_OPTIONS
342 N_OPTS
344 #undef OPT
346 struct cl_option
348 const char *opt_text;
349 unsigned char opt_len;
350 unsigned char flags;
351 ENUM_BITFIELD (opt_code) opt_code : 2 * CHAR_BIT;
354 #define OPT(text, flags, code) { text, sizeof(text) - 1, flags, code },
355 #ifdef HOST_EBCDIC
356 static struct cl_option cl_options[] =
357 #else
358 static const struct cl_option cl_options[] =
359 #endif
361 COMMAND_LINE_OPTIONS
363 #undef OPT
364 #undef COMMAND_LINE_OPTIONS
366 /* Holds switches parsed by c_common_decode_option (), but whose
367 handling is deferred to c_common_post_options (). */
368 static void defer_opt PARAMS ((enum opt_code, const char *));
369 static struct deferred_opt
371 enum opt_code code;
372 const char *arg;
373 } *deferred_opts;
376 #ifdef HOST_EBCDIC
377 static int opt_comp PARAMS ((const void *, const void *));
379 /* Run-time sorting of options array. */
380 static int
381 opt_comp (p1, p2)
382 const void *p1, *p2;
384 return strcmp (((struct cl_option *) p1)->opt_text,
385 ((struct cl_option *) p2)->opt_text);
387 #endif
389 /* Complain that switch OPT_INDEX expects an argument but none was
390 provided. */
391 static void
392 missing_arg (opt_index)
393 size_t opt_index;
395 const char *opt_text = cl_options[opt_index].opt_text;
397 switch (cl_options[opt_index].opt_code)
399 case OPT__output_pch:
400 case OPT_Wformat_eq:
401 case OPT_d:
402 case OPT_fabi_version:
403 case OPT_fbuiltin_:
404 case OPT_fdump:
405 case OPT_fname_mangling:
406 case OPT_ftabstop:
407 case OPT_ftemplate_depth:
408 case OPT_iprefix:
409 case OPT_iwithprefix:
410 case OPT_iwithprefixbefore:
411 default:
412 error ("missing argument to \"-%s\"", opt_text);
413 break;
415 case OPT_fconstant_string_class:
416 error ("no class name specified with \"-%s\"", opt_text);
417 break;
419 case OPT_A:
420 error ("assertion missing after \"-%s\"", opt_text);
421 break;
423 case OPT_D:
424 case OPT_U:
425 error ("macro name missing after \"-%s\"", opt_text);
426 break;
428 case OPT_I:
429 case OPT_idirafter:
430 case OPT_isysroot:
431 case OPT_isystem:
432 error ("missing path after \"-%s\"", opt_text);
433 break;
435 case OPT_MF:
436 case OPT_MD:
437 case OPT_MMD:
438 case OPT_include:
439 case OPT_imacros:
440 case OPT_o:
441 error ("missing filename after \"-%s\"", opt_text);
442 break;
444 case OPT_MQ:
445 case OPT_MT:
446 error ("missing target after \"-%s\"", opt_text);
447 break;
451 /* Perform a binary search to find which option the command-line INPUT
452 matches. Returns its index in the option array, and N_OPTS on
453 failure.
455 Complications arise since some options can be suffixed with an
456 argument, and multiple complete matches can occur, e.g. -pedantic
457 and -pedantic-errors. Also, some options are only accepted by some
458 languages. If a switch matches for a different language and
459 doesn't match any alternatives for the true front end, the index of
460 the matched switch is returned anyway. The caller should check for
461 this case. */
462 static size_t
463 find_opt (input, lang_flag)
464 const char *input;
465 int lang_flag;
467 size_t md, mn, mx;
468 size_t opt_len;
469 size_t result = N_OPTS;
470 int comp;
472 mn = 0;
473 mx = N_OPTS;
475 while (mx > mn)
477 md = (mn + mx) / 2;
479 opt_len = cl_options[md].opt_len;
480 comp = strncmp (input, cl_options[md].opt_text, opt_len);
482 if (comp < 0)
483 mx = md;
484 else if (comp > 0)
485 mn = md + 1;
486 else
488 /* The switch matches. It it an exact match? */
489 if (input[opt_len] == '\0')
490 return md;
491 else
493 mn = md + 1;
495 /* If the switch takes no arguments this is not a proper
496 match, so we continue the search (e.g. input="stdc++"
497 match was "stdc"). */
498 if (!(cl_options[md].flags & CL_JOINED))
499 continue;
501 /* Is this switch valid for this front end? */
502 if (!(cl_options[md].flags & lang_flag))
504 /* If subsequently we don't find a better match,
505 return this and let the caller report it as a bad
506 match. */
507 result = md;
508 continue;
511 /* Two scenarios remain: we have the switch's argument,
512 or we match a longer option. This can happen with
513 -iwithprefix and -withprefixbefore. The longest
514 possible option match succeeds.
516 Scan forwards, and return an exact match. Otherwise
517 return the longest valid option-accepting match (mx).
518 This loops at most twice with current options. */
519 mx = md;
520 for (md = md + 1; md < (size_t) N_OPTS; md++)
522 opt_len = cl_options[md].opt_len;
523 if (strncmp (input, cl_options[md].opt_text, opt_len))
524 break;
525 if (input[opt_len] == '\0')
526 return md;
527 if (cl_options[md].flags & lang_flag
528 && cl_options[md].flags & CL_JOINED)
529 mx = md;
532 return mx;
537 return result;
540 /* Defer option CODE with argument ARG. */
541 static void
542 defer_opt (code, arg)
543 enum opt_code code;
544 const char *arg;
546 /* FIXME: this should be in c_common_init_options, which should take
547 argc and argv. */
548 if (!deferred_opts)
550 extern int save_argc;
551 deferred_size = save_argc;
552 deferred_opts = (struct deferred_opt *)
553 xmalloc (deferred_size * sizeof (struct deferred_opt));
556 if (deferred_count == deferred_size)
557 abort ();
559 deferred_opts[deferred_count].code = code;
560 deferred_opts[deferred_count].arg = arg;
561 deferred_count++;
564 /* Common initialization before parsing options. */
565 void
566 c_common_init_options (lang)
567 enum c_language_kind lang;
569 #ifdef HOST_EBCDIC
570 /* For non-ASCII hosts, the cl_options array needs to be sorted at
571 runtime. */
572 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
573 #endif
574 #if ENABLE_CHECKING
576 size_t i;
578 for (i = 1; i < N_OPTS; i++)
579 if (strcmp (cl_options[i - 1].opt_text, cl_options[i].opt_text) >= 0)
580 error ("options array incorrectly sorted: %s is before %s",
581 cl_options[i - 1].opt_text, cl_options[i].opt_text);
583 #endif
585 c_language = lang;
586 parse_in = cpp_create_reader (lang == clk_c ? CLK_GNUC89 : CLK_GNUCXX);
587 cpp_opts = cpp_get_options (parse_in);
588 if (flag_objc)
589 cpp_opts->objc = 1;
591 flag_const_strings = (lang == clk_cplusplus);
592 warn_pointer_arith = (lang == clk_cplusplus);
593 if (lang == clk_c)
594 warn_sign_compare = -1;
597 /* Handle one command-line option in (argc, argv).
598 Can be called multiple times, to handle multiple sets of options.
599 Returns number of strings consumed. */
601 c_common_decode_option (argc, argv)
602 int argc;
603 char **argv;
605 static const int lang_flags[] = {CL_C_ONLY, CL_C, CL_CXX_ONLY, CL_CXX};
606 size_t opt_index;
607 const char *opt, *arg = 0;
608 char *dup = 0;
609 bool on = true;
610 int result = 0, lang_flag;
611 const struct cl_option *option;
612 enum opt_code code;
614 opt = argv[0];
616 /* Interpret "-" or a non-switch as a file name. */
617 if (opt[0] != '-' || opt[1] == '\0')
619 if (!in_fname)
620 in_fname = opt;
621 else if (!out_fname)
622 out_fname = opt;
623 else
625 error ("too many filenames given. Type %s --help for usage",
626 progname);
627 return argc;
630 return 1;
633 /* Drop the "no-" from negative switches. */
634 if ((opt[1] == 'W' || opt[1] == 'f')
635 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
637 size_t len = strlen (opt) - 3;
639 dup = xmalloc (len + 1);
640 dup[0] = '-';
641 dup[1] = opt[1];
642 memcpy (dup + 2, opt + 5, len - 2 + 1);
643 opt = dup;
644 on = false;
647 /* Skip over '-'. */
648 lang_flag = lang_flags[(c_language << 1) + flag_objc];
649 opt_index = find_opt (opt + 1, lang_flag);
650 if (opt_index == N_OPTS)
651 goto done;
653 result = 1;
654 option = &cl_options[opt_index];
656 /* Sort out any argument the switch takes. */
657 if (option->flags & CL_ARG)
659 if (option->flags & CL_JOINED)
661 /* Have arg point to the original switch. This is because
662 some code, such as disable_builtin_function, expects its
663 argument to be persistent until the program exits. */
664 arg = argv[0] + cl_options[opt_index].opt_len + 1;
665 if (!on)
666 arg += strlen ("no-");
669 /* If we don't have an argument, and CL_SEPARATE, try the next
670 argument in the vector. */
671 if (!arg || (*arg == '\0' && option->flags & CL_SEPARATE))
673 arg = argv[1];
674 result = 2;
677 if (!arg || *arg == '\0')
679 missing_arg (opt_index);
680 result = argc;
681 goto done;
685 /* Complain about the wrong language after we've swallowed any
686 necessary extra argument. Eventually make this a hard error
687 after the call to find_opt, and return argc. */
688 if (!(cl_options[opt_index].flags & lang_flag))
690 complain_wrong_lang (opt_index);
691 goto done;
694 switch (code = option->opt_code)
696 case N_OPTS: /* Shut GCC up. */
697 break;
699 case OPT__help:
700 print_help ();
701 break;
703 case OPT__output_pch:
704 pch_file = arg;
705 break;
707 case OPT_A:
708 defer_opt (code, arg);
709 break;
711 case OPT_C:
712 cpp_opts->discard_comments = 0;
713 break;
715 case OPT_CC:
716 cpp_opts->discard_comments = 0;
717 cpp_opts->discard_comments_in_macro_exp = 0;
718 break;
720 case OPT_D:
721 defer_opt (code, arg);
722 break;
724 case OPT_E:
725 flag_preprocess_only = 1;
726 break;
728 case OPT_H:
729 cpp_opts->print_include_names = 1;
730 break;
732 case OPT_I:
733 if (strcmp (arg, "-"))
734 add_path (xstrdup (arg), BRACKET, 0);
735 else
737 if (quote_chain_split)
738 error ("-I- specified twice");
739 quote_chain_split = true;
740 split_quote_chain ();
742 break;
744 case OPT_M:
745 case OPT_MM:
746 /* When doing dependencies with -M or -MM, suppress normal
747 preprocessed output, but still do -dM etc. as software
748 depends on this. Preprocessed output does occur if -MD, -MMD
749 or environment var dependency generation is used. */
750 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
751 flag_no_output = 1;
752 cpp_opts->inhibit_warnings = 1;
753 break;
755 case OPT_MD:
756 case OPT_MMD:
757 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
758 deps_file = arg;
759 break;
761 case OPT_MF:
762 deps_seen = true;
763 deps_file = arg;
764 break;
766 case OPT_MG:
767 deps_seen = true;
768 cpp_opts->deps.missing_files = true;
769 break;
771 case OPT_MP:
772 deps_seen = true;
773 cpp_opts->deps.phony_targets = true;
774 break;
776 case OPT_MQ:
777 case OPT_MT:
778 deps_seen = true;
779 defer_opt (code, arg);
780 break;
782 case OPT_P:
783 flag_no_line_commands = 1;
784 break;
786 case OPT_U:
787 defer_opt (code, arg);
788 break;
790 case OPT_Wabi:
791 warn_abi = on;
792 break;
794 case OPT_Wall:
795 set_Wunused (on);
796 set_Wformat (on);
797 set_Wimplicit (on);
798 warn_char_subscripts = on;
799 warn_missing_braces = on;
800 warn_parentheses = on;
801 warn_return_type = on;
802 warn_sequence_point = on; /* Was C only. */
803 warn_sign_compare = on; /* Was C++ only. */
804 warn_switch = on;
805 warn_strict_aliasing = on;
807 /* Only warn about unknown pragmas that are not in system
808 headers. */
809 warn_unknown_pragmas = on;
811 /* We save the value of warn_uninitialized, since if they put
812 -Wuninitialized on the command line, we need to generate a
813 warning about not using it without also specifying -O. */
814 if (warn_uninitialized != 1)
815 warn_uninitialized = (on ? 2 : 0);
817 if (c_language == clk_c)
818 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
819 can turn it off only if it's not explicit. */
820 warn_main = on * 2;
821 else
823 /* C++-specific warnings. */
824 warn_ctor_dtor_privacy = on;
825 warn_nonvdtor = on;
826 warn_reorder = on;
827 warn_nontemplate_friend = on;
830 cpp_opts->warn_trigraphs = on;
831 cpp_opts->warn_comments = on;
832 cpp_opts->warn_num_sign_change = on;
833 cpp_opts->warn_multichar = on; /* Was C++ only. */
834 break;
836 case OPT_Wbad_function_cast:
837 warn_bad_function_cast = on;
838 break;
840 case OPT_Wcast_qual:
841 warn_cast_qual = on;
842 break;
844 case OPT_Wchar_subscripts:
845 warn_char_subscripts = on;
846 break;
848 case OPT_Wcomment:
849 case OPT_Wcomments:
850 cpp_opts->warn_comments = on;
851 break;
853 case OPT_Wconversion:
854 warn_conversion = on;
855 break;
857 case OPT_Wctor_dtor_privacy:
858 warn_ctor_dtor_privacy = on;
859 break;
861 case OPT_Wdeprecated:
862 warn_deprecated = on;
863 cpp_opts->warn_deprecated = on;
864 break;
866 case OPT_Wdiv_by_zero:
867 warn_div_by_zero = on;
868 break;
870 case OPT_Weffcxx:
871 warn_ecpp = on;
872 break;
874 case OPT_Wendif_labels:
875 cpp_opts->warn_endif_labels = on;
876 break;
878 case OPT_Werror:
879 cpp_opts->warnings_are_errors = on;
880 break;
882 case OPT_Werror_implicit_function_decl:
883 if (!on)
884 result = 0;
885 else
886 mesg_implicit_function_declaration = 2;
887 break;
889 case OPT_Wfloat_equal:
890 warn_float_equal = on;
891 break;
893 case OPT_Wformat:
894 set_Wformat (on);
895 break;
897 case OPT_Wformat_eq:
898 set_Wformat (atoi (arg));
899 break;
901 case OPT_Wformat_extra_args:
902 warn_format_extra_args = on;
903 break;
905 case OPT_Wformat_nonliteral:
906 warn_format_nonliteral = on;
907 break;
909 case OPT_Wformat_security:
910 warn_format_security = on;
911 break;
913 case OPT_Wformat_y2k:
914 warn_format_y2k = on;
915 break;
917 case OPT_Wformat_zero_length:
918 warn_format_zero_length = on;
919 break;
921 case OPT_Wimplicit:
922 set_Wimplicit (on);
923 break;
925 case OPT_Wimplicit_function_decl:
926 mesg_implicit_function_declaration = on;
927 break;
929 case OPT_Wimplicit_int:
930 warn_implicit_int = on;
931 break;
933 case OPT_Wimport:
934 cpp_opts->warn_import = on;
935 break;
937 case OPT_Winvalid_pch:
938 cpp_opts->warn_invalid_pch = on;
939 break;
941 case OPT_Wlong_long:
942 warn_long_long = on;
943 break;
945 case OPT_Wmain:
946 if (on)
947 warn_main = 1;
948 else
949 warn_main = -1;
950 break;
952 case OPT_Wmissing_braces:
953 warn_missing_braces = on;
954 break;
956 case OPT_Wmissing_declarations:
957 warn_missing_declarations = on;
958 break;
960 case OPT_Wmissing_format_attribute:
961 warn_missing_format_attribute = on;
962 break;
964 case OPT_Wmissing_prototypes:
965 warn_missing_prototypes = on;
966 break;
968 case OPT_Wmultichar:
969 cpp_opts->warn_multichar = on;
970 break;
972 case OPT_Wnested_externs:
973 warn_nested_externs = on;
974 break;
976 case OPT_Wnon_template_friend:
977 warn_nontemplate_friend = on;
978 break;
980 case OPT_Wnon_virtual_dtor:
981 warn_nonvdtor = on;
982 break;
984 case OPT_Wnonnull:
985 warn_nonnull = on;
986 break;
988 case OPT_Wold_style_cast:
989 warn_old_style_cast = on;
990 break;
992 case OPT_Woverloaded_virtual:
993 warn_overloaded_virtual = on;
994 break;
996 case OPT_Wparentheses:
997 warn_parentheses = on;
998 break;
1000 case OPT_Wpmf_conversions:
1001 warn_pmf2ptr = on;
1002 break;
1004 case OPT_Wpointer_arith:
1005 warn_pointer_arith = on;
1006 break;
1008 case OPT_Wprotocol:
1009 warn_protocol = on;
1010 break;
1012 case OPT_Wselector:
1013 warn_selector = on;
1014 break;
1016 case OPT_Wredundant_decls:
1017 warn_redundant_decls = on;
1018 break;
1020 case OPT_Wreorder:
1021 warn_reorder = on;
1022 break;
1024 case OPT_Wreturn_type:
1025 warn_return_type = on;
1026 break;
1028 case OPT_Wsequence_point:
1029 warn_sequence_point = on;
1030 break;
1032 case OPT_Wsign_compare:
1033 warn_sign_compare = on;
1034 break;
1036 case OPT_Wsign_promo:
1037 warn_sign_promo = on;
1038 break;
1040 case OPT_Wstrict_prototypes:
1041 if (!on && c_language == clk_cplusplus)
1042 warning ("-Wno-strict-prototypes is not supported in C++");
1043 else
1044 warn_strict_prototypes = on;
1045 break;
1047 case OPT_Wsynth:
1048 warn_synth = on;
1049 break;
1051 case OPT_Wsystem_headers:
1052 cpp_opts->warn_system_headers = on;
1053 break;
1055 case OPT_Wtraditional:
1056 warn_traditional = on;
1057 cpp_opts->warn_traditional = on;
1058 break;
1060 case OPT_Wtrigraphs:
1061 cpp_opts->warn_trigraphs = on;
1062 break;
1064 case OPT_Wundeclared_selector:
1065 warn_undeclared_selector = on;
1066 break;
1068 case OPT_Wundef:
1069 cpp_opts->warn_undef = on;
1070 break;
1072 case OPT_Wunknown_pragmas:
1073 /* Set to greater than 1, so that even unknown pragmas in
1074 system headers will be warned about. */
1075 warn_unknown_pragmas = on * 2;
1076 break;
1078 case OPT_Wunused_macros:
1079 warn_unused_macros = on;
1080 break;
1082 case OPT_Wwrite_strings:
1083 if (c_language == clk_c)
1084 flag_const_strings = on;
1085 else
1086 warn_write_strings = on;
1087 break;
1089 case OPT_ansi:
1090 if (c_language == clk_c)
1091 set_std_c89 (false, true);
1092 else
1093 set_std_cxx98 (true);
1094 break;
1096 case OPT_d:
1097 handle_OPT_d (arg);
1098 break;
1100 case OPT_fcond_mismatch:
1101 if (c_language == clk_c)
1103 flag_cond_mismatch = on;
1104 break;
1106 /* Fall through. */
1108 case OPT_fall_virtual:
1109 case OPT_fenum_int_equiv:
1110 case OPT_fguiding_decls:
1111 case OPT_fhonor_std:
1112 case OPT_fhuge_objects:
1113 case OPT_flabels_ok:
1114 case OPT_fname_mangling:
1115 case OPT_fnew_abi:
1116 case OPT_fnonnull_objects:
1117 case OPT_fsquangle:
1118 case OPT_fstrict_prototype:
1119 case OPT_fthis_is_variable:
1120 case OPT_fvtable_thunks:
1121 case OPT_fxref:
1122 warning ("switch \"%s\" is no longer supported", argv[0]);
1123 break;
1125 case OPT_fabi_version:
1126 flag_abi_version = read_integral_parameter (arg, argv[0], 1);
1127 break;
1129 case OPT_faccess_control:
1130 flag_access_control = on;
1131 break;
1133 case OPT_falt_external_templates:
1134 flag_alt_external_templates = on;
1135 if (on)
1136 flag_external_templates = true;
1137 cp_deprecated:
1138 warning ("switch \"%s\" is deprecated, please see documentation for details", argv[0]);
1139 break;
1141 case OPT_fasm:
1142 flag_no_asm = !on;
1143 break;
1145 case OPT_fbuiltin:
1146 flag_no_builtin = !on;
1147 break;
1149 case OPT_fbuiltin_:
1150 if (on)
1151 result = 0;
1152 else
1153 disable_builtin_function (arg);
1154 break;
1156 case OPT_fdollars_in_identifiers:
1157 dollars_in_ident = on;
1158 break;
1160 case OPT_fdump:
1161 if (!on || !dump_switch_p (argv[0] + strlen ("-f")))
1162 result = 0;
1163 break;
1165 case OPT_ffreestanding:
1166 on = !on;
1167 /* Fall through... */
1168 case OPT_fhosted:
1169 flag_hosted = on;
1170 flag_no_builtin = !on;
1171 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
1172 if (!on && warn_main == 2)
1173 warn_main = 0;
1174 break;
1176 case OPT_fshort_double:
1177 flag_short_double = on;
1178 break;
1180 case OPT_fshort_enums:
1181 flag_short_enums = on;
1182 break;
1184 case OPT_fshort_wchar:
1185 flag_short_wchar = on;
1186 break;
1188 case OPT_fsigned_bitfields:
1189 flag_signed_bitfields = on;
1190 explicit_flag_signed_bitfields = 1;
1191 break;
1193 case OPT_fsigned_char:
1194 flag_signed_char = on;
1195 break;
1197 case OPT_funsigned_bitfields:
1198 flag_signed_bitfields = !on;
1199 explicit_flag_signed_bitfields = 1;
1200 break;
1202 case OPT_funsigned_char:
1203 flag_signed_char = !on;
1204 break;
1206 case OPT_fcheck_new:
1207 flag_check_new = on;
1208 break;
1210 case OPT_fconserve_space:
1211 flag_conserve_space = on;
1212 break;
1214 case OPT_fconst_strings:
1215 flag_const_strings = on;
1216 break;
1218 case OPT_fconstant_string_class:
1219 constant_string_class_name = arg;
1220 break;
1222 case OPT_fdefault_inline:
1223 flag_default_inline = on;
1224 break;
1226 case OPT_felide_constructors:
1227 flag_elide_constructors = on;
1228 break;
1230 case OPT_fenforce_eh_specs:
1231 flag_enforce_eh_specs = on;
1232 break;
1234 case OPT_fexternal_templates:
1235 flag_external_templates = on;
1236 goto cp_deprecated;
1238 case OPT_ffixed_form:
1239 case OPT_ffixed_line_length:
1240 /* Fortran front end options ignored when preprocessing only. */
1241 if (flag_preprocess_only)
1242 result = -1;
1243 break;
1245 case OPT_ffor_scope:
1246 flag_new_for_scope = on;
1247 break;
1249 case OPT_fgnu_keywords:
1250 flag_no_gnu_keywords = !on;
1251 break;
1253 case OPT_fgnu_runtime:
1254 flag_next_runtime = !on;
1255 break;
1257 case OPT_fhandle_exceptions:
1258 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
1259 flag_exceptions = on;
1260 break;
1262 case OPT_fimplement_inlines:
1263 flag_implement_inlines = on;
1264 break;
1266 case OPT_fimplicit_inline_templates:
1267 flag_implicit_inline_templates = on;
1268 break;
1270 case OPT_fimplicit_templates:
1271 flag_implicit_templates = on;
1272 break;
1274 case OPT_fms_extensions:
1275 flag_ms_extensions = on;
1276 break;
1278 case OPT_fnext_runtime:
1279 flag_next_runtime = on;
1280 break;
1282 case OPT_fnonansi_builtins:
1283 flag_no_nonansi_builtin = !on;
1284 break;
1286 case OPT_foperator_names:
1287 cpp_opts->operator_names = on;
1288 break;
1290 case OPT_foptional_diags:
1291 flag_optional_diags = on;
1292 break;
1294 case OPT_fpch_deps:
1295 cpp_opts->restore_pch_deps = on;
1296 break;
1298 case OPT_fpermissive:
1299 flag_permissive = on;
1300 break;
1302 case OPT_fpreprocessed:
1303 cpp_opts->preprocessed = on;
1304 break;
1306 case OPT_frepo:
1307 flag_use_repository = on;
1308 if (on)
1309 flag_implicit_templates = 0;
1310 break;
1312 case OPT_frtti:
1313 flag_rtti = on;
1314 break;
1316 case OPT_fshow_column:
1317 cpp_opts->show_column = on;
1318 break;
1320 case OPT_fstats:
1321 flag_detailed_statistics = on;
1322 break;
1324 case OPT_ftabstop:
1325 /* Don't recognize -fno-tabstop=. */
1326 if (!on)
1327 return 0;
1329 /* It is documented that we silently ignore silly values. */
1331 char *endptr;
1332 long tabstop = strtol (arg, &endptr, 10);
1333 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1334 cpp_opts->tabstop = tabstop;
1336 break;
1338 case OPT_ftemplate_depth:
1339 max_tinst_depth = read_integral_parameter (arg, argv[0], 0);
1340 break;
1342 case OPT_fvtable_gc:
1343 flag_vtable_gc = on;
1344 break;
1346 case OPT_fuse_cxa_atexit:
1347 flag_use_cxa_atexit = on;
1348 break;
1350 case OPT_fweak:
1351 flag_weak = on;
1352 break;
1354 case OPT_gen_decls:
1355 flag_gen_declaration = 1;
1356 break;
1358 case OPT_idirafter:
1359 add_path (xstrdup (arg), AFTER, 0);
1360 break;
1362 case OPT_imacros:
1363 case OPT_include:
1364 defer_opt (code, arg);
1365 break;
1367 case OPT_iprefix:
1368 iprefix = arg;
1369 break;
1371 case OPT_isysroot:
1372 sysroot = arg;
1373 break;
1375 case OPT_isystem:
1376 add_path (xstrdup (arg), SYSTEM, 0);
1377 break;
1379 case OPT_iwithprefix:
1380 add_prefixed_path (arg, SYSTEM);
1381 break;
1383 case OPT_iwithprefixbefore:
1384 add_prefixed_path (arg, BRACKET);
1385 break;
1387 case OPT_lang_asm:
1388 cpp_set_lang (parse_in, CLK_ASM);
1389 break;
1391 case OPT_lang_objc:
1392 cpp_opts->objc = 1;
1393 break;
1395 case OPT_nostdinc:
1396 std_inc = false;
1397 break;
1399 case OPT_nostdincplusplus:
1400 std_cxx_inc = false;
1401 break;
1403 case OPT_o:
1404 if (!out_fname)
1405 out_fname = arg;
1406 else
1408 error ("output filename specified twice");
1409 result = argc;
1411 break;
1413 /* We need to handle the -pedantic switches here, rather than in
1414 c_common_post_options, so that a subsequent -Wno-endif-labels
1415 is not overridden. */
1416 case OPT_pedantic_errors:
1417 cpp_opts->pedantic_errors = 1;
1418 /* fall through */
1419 case OPT_pedantic:
1420 cpp_opts->pedantic = 1;
1421 cpp_opts->warn_endif_labels = 1;
1422 break;
1424 case OPT_print_objc_runtime_info:
1425 print_struct_values = 1;
1426 break;
1428 case OPT_remap:
1429 cpp_opts->remap = 1;
1430 break;
1432 case OPT_std_cplusplus98:
1433 case OPT_std_gnuplusplus98:
1434 set_std_cxx98 (code == OPT_std_cplusplus98 /* ISO */);
1435 break;
1437 case OPT_std_c89:
1438 case OPT_std_iso9899_1990:
1439 case OPT_std_iso9899_199409:
1440 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1441 break;
1443 case OPT_std_gnu89:
1444 set_std_c89 (false /* c94 */, false /* ISO */);
1445 break;
1447 case OPT_std_c99:
1448 case OPT_std_c9x:
1449 case OPT_std_iso9899_1999:
1450 case OPT_std_iso9899_199x:
1451 set_std_c99 (true /* ISO */);
1452 break;
1454 case OPT_std_gnu99:
1455 case OPT_std_gnu9x:
1456 set_std_c99 (false /* ISO */);
1457 break;
1459 case OPT_trigraphs:
1460 cpp_opts->trigraphs = 1;
1461 break;
1463 case OPT_traditional_cpp:
1464 cpp_opts->traditional = 1;
1465 break;
1467 case OPT_undef:
1468 flag_undef = 1;
1469 break;
1471 case OPT_w:
1472 cpp_opts->inhibit_warnings = 1;
1473 break;
1475 case OPT_v:
1476 verbose = true;
1477 break;
1480 done:
1481 if (dup)
1482 free (dup);
1483 return result;
1486 /* Post-switch processing. */
1487 bool
1488 c_common_post_options (pfilename)
1489 const char **pfilename;
1491 /* Canonicalize the input and output filenames. */
1492 if (in_fname == NULL || !strcmp (in_fname, "-"))
1493 in_fname = "";
1495 if (out_fname == NULL || !strcmp (out_fname, "-"))
1496 out_fname = "";
1498 if (cpp_opts->deps.style == DEPS_NONE)
1499 check_deps_environment_vars ();
1501 handle_deferred_opts ();
1503 sanitize_cpp_opts ();
1505 register_include_chains (parse_in, sysroot, iprefix,
1506 std_inc, std_cxx_inc && c_language == clk_cplusplus,
1507 verbose);
1509 flag_inline_trees = 1;
1511 /* Use tree inlining if possible. Function instrumentation is only
1512 done in the RTL level, so we disable tree inlining. */
1513 if (! flag_instrument_function_entry_exit)
1515 if (!flag_no_inline)
1516 flag_no_inline = 1;
1517 if (flag_inline_functions)
1519 flag_inline_trees = 2;
1520 flag_inline_functions = 0;
1524 /* Special format checking options don't work without -Wformat; warn if
1525 they are used. */
1526 if (warn_format_y2k && !warn_format)
1527 warning ("-Wformat-y2k ignored without -Wformat");
1528 if (warn_format_extra_args && !warn_format)
1529 warning ("-Wformat-extra-args ignored without -Wformat");
1530 if (warn_format_zero_length && !warn_format)
1531 warning ("-Wformat-zero-length ignored without -Wformat");
1532 if (warn_format_nonliteral && !warn_format)
1533 warning ("-Wformat-nonliteral ignored without -Wformat");
1534 if (warn_format_security && !warn_format)
1535 warning ("-Wformat-security ignored without -Wformat");
1536 if (warn_missing_format_attribute && !warn_format)
1537 warning ("-Wmissing-format-attribute ignored without -Wformat");
1539 if (flag_preprocess_only)
1541 /* Open the output now. We must do so even if flag_no_output is
1542 on, because there may be other output than from the actual
1543 preprocessing (e.g. from -dM). */
1544 if (out_fname[0] == '\0')
1545 out_stream = stdout;
1546 else
1547 out_stream = fopen (out_fname, "w");
1549 if (out_stream == NULL)
1551 fatal_io_error ("opening output file %s", out_fname);
1552 return false;
1555 init_pp_output (out_stream);
1557 else
1559 init_c_lex ();
1561 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1562 lineno = 0;
1565 cpp_get_callbacks (parse_in)->file_change = cb_file_change;
1567 /* NOTE: we use in_fname here, not the one supplied. */
1568 *pfilename = cpp_read_main_file (parse_in, in_fname, ident_hash);
1570 saved_lineno = lineno;
1571 lineno = 0;
1573 /* If an error has occurred in cpplib, note it so we fail
1574 immediately. */
1575 errorcount += cpp_errors (parse_in);
1577 return flag_preprocess_only;
1580 /* Front end initialization common to C, ObjC and C++. */
1581 bool
1582 c_common_init ()
1584 lineno = saved_lineno;
1586 /* Set up preprocessor arithmetic. Must be done after call to
1587 c_common_nodes_and_builtins for type nodes to be good. */
1588 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1589 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1590 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1591 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1592 cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1594 if (flag_preprocess_only)
1596 finish_options ();
1597 preprocess_file (parse_in);
1598 return false;
1601 /* Has to wait until now so that cpplib has its hash table. */
1602 init_pragma ();
1604 return true;
1607 /* A thin wrapper around the real parser that initializes the
1608 integrated preprocessor after debug output has been initialized.
1609 Also, make sure the start_source_file debug hook gets called for
1610 the primary source file. */
1611 void
1612 c_common_parse_file (set_yydebug)
1613 int set_yydebug ATTRIBUTE_UNUSED;
1615 #if YYDEBUG != 0
1616 yydebug = set_yydebug;
1617 #else
1618 warning ("YYDEBUG not defined");
1619 #endif
1621 (*debug_hooks->start_source_file) (lineno, input_filename);
1622 finish_options();
1623 pch_init();
1624 yyparse ();
1625 free_parser_stacks ();
1628 /* Common finish hook for the C, ObjC and C++ front ends. */
1629 void
1630 c_common_finish ()
1632 FILE *deps_stream = NULL;
1634 if (cpp_opts->deps.style != DEPS_NONE)
1636 /* If -M or -MM was seen without -MF, default output to the
1637 output stream. */
1638 if (!deps_file)
1639 deps_stream = out_stream;
1640 else
1642 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1643 if (!deps_stream)
1644 fatal_io_error ("opening dependency file %s", deps_file);
1648 /* For performance, avoid tearing down cpplib's internal structures
1649 with cpp_destroy (). */
1650 errorcount += cpp_finish (parse_in, deps_stream);
1652 if (deps_stream && deps_stream != out_stream
1653 && (ferror (deps_stream) || fclose (deps_stream)))
1654 fatal_io_error ("closing dependency file %s", deps_file);
1656 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1657 fatal_io_error ("when writing output to %s", out_fname);
1660 /* Either of two environment variables can specify output of
1661 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1662 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1663 and DEPS_TARGET is the target to mention in the deps. They also
1664 result in dependency information being appended to the output file
1665 rather than overwriting it, and like Sun's compiler
1666 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1667 static void
1668 check_deps_environment_vars ()
1670 char *spec;
1672 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1673 if (spec)
1674 cpp_opts->deps.style = DEPS_USER;
1675 else
1677 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1678 if (spec)
1680 cpp_opts->deps.style = DEPS_SYSTEM;
1681 cpp_opts->deps.ignore_main_file = true;
1685 if (spec)
1687 /* Find the space before the DEPS_TARGET, if there is one. */
1688 char *s = strchr (spec, ' ');
1689 if (s)
1691 /* Let the caller perform MAKE quoting. */
1692 defer_opt (OPT_MT, s + 1);
1693 *s = '\0';
1696 /* Command line -MF overrides environment variables and default. */
1697 if (!deps_file)
1698 deps_file = spec;
1700 deps_append = 1;
1704 /* Handle deferred command line switches. */
1705 static void
1706 handle_deferred_opts ()
1708 size_t i;
1710 for (i = 0; i < deferred_count; i++)
1712 struct deferred_opt *opt = &deferred_opts[i];
1714 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1715 cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1719 /* These settings are appropriate for GCC, but not necessarily so for
1720 cpplib as a library. */
1721 static void
1722 sanitize_cpp_opts ()
1724 /* If we don't know what style of dependencies to output, complain
1725 if any other dependency switches have been given. */
1726 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1727 error ("to generate dependencies you must specify either -M or -MM");
1729 /* -dM and dependencies suppress normal output; do it here so that
1730 the last -d[MDN] switch overrides earlier ones. */
1731 if (flag_dump_macros == 'M')
1732 flag_no_output = 1;
1734 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1735 -dM since at least glibc relies on -M -dM to work. */
1736 if (flag_no_output)
1738 if (flag_dump_macros != 'M')
1739 flag_dump_macros = 0;
1740 flag_dump_includes = 0;
1743 cpp_opts->unsigned_char = !flag_signed_char;
1744 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1746 /* We want -Wno-long-long to override -pedantic -std=non-c99
1747 and/or -Wtraditional, whatever the ordering. */
1748 cpp_opts->warn_long_long
1749 = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1752 /* Add include path with a prefix at the front of its name. */
1753 static void
1754 add_prefixed_path (suffix, chain)
1755 const char *suffix;
1756 size_t chain;
1758 char *path;
1759 const char *prefix;
1760 size_t prefix_len, suffix_len;
1762 suffix_len = strlen (suffix);
1763 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1764 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1766 path = xmalloc (prefix_len + suffix_len + 1);
1767 memcpy (path, prefix, prefix_len);
1768 memcpy (path + prefix_len, suffix, suffix_len);
1769 path[prefix_len + suffix_len] = '\0';
1771 add_path (path, chain, 0);
1774 /* Handle -D, -U, -A, -imacros, and the first -include. */
1775 static void
1776 finish_options ()
1778 if (!cpp_opts->preprocessed)
1780 size_t i;
1782 cpp_rename_file (parse_in, _("<built-in>"));
1783 cpp_init_builtins (parse_in);
1784 c_cpp_builtins (parse_in);
1785 cpp_rename_file (parse_in, _("<command line>"));
1786 for (i = 0; i < deferred_count; i++)
1788 struct deferred_opt *opt = &deferred_opts[i];
1790 if (opt->code == OPT_D)
1791 cpp_define (parse_in, opt->arg);
1792 else if (opt->code == OPT_U)
1793 cpp_undef (parse_in, opt->arg);
1794 else if (opt->code == OPT_A)
1796 if (opt->arg[0] == '-')
1797 cpp_unassert (parse_in, opt->arg + 1);
1798 else
1799 cpp_assert (parse_in, opt->arg);
1803 /* Handle -imacros after -D and -U. */
1804 for (i = 0; i < deferred_count; i++)
1806 struct deferred_opt *opt = &deferred_opts[i];
1808 if (opt->code == OPT_imacros
1809 && cpp_push_include (parse_in, opt->arg))
1810 cpp_scan_nooutput (parse_in);
1814 push_command_line_include ();
1817 /* Give CPP the next file given by -include, if any. */
1818 static void
1819 push_command_line_include ()
1821 if (cpp_opts->preprocessed)
1822 return;
1824 while (include_cursor < deferred_count)
1826 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1828 if (opt->code == OPT_include && cpp_push_include (parse_in, opt->arg))
1829 return;
1832 if (include_cursor == deferred_count)
1834 /* Restore the line map from <command line>. */
1835 cpp_rename_file (parse_in, main_input_filename);
1836 /* -Wunused-macros should only warn about macros defined hereafter. */
1837 cpp_opts->warn_unused_macros = warn_unused_macros;
1838 include_cursor++;
1842 /* File change callback. Has to handle -include files. */
1843 static void
1844 cb_file_change (pfile, new_map)
1845 cpp_reader *pfile ATTRIBUTE_UNUSED;
1846 const struct line_map *new_map;
1848 if (flag_preprocess_only)
1849 pp_file_change (new_map);
1850 else
1851 fe_file_change (new_map);
1853 if (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map))
1854 push_command_line_include ();
1857 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1858 extensions if ISO). There is no concept of gnu94. */
1859 static void
1860 set_std_c89 (c94, iso)
1861 int c94, iso;
1863 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1864 flag_iso = iso;
1865 flag_no_asm = iso;
1866 flag_no_gnu_keywords = iso;
1867 flag_no_nonansi_builtin = iso;
1868 flag_noniso_default_format_attributes = !iso;
1869 flag_isoc94 = c94;
1870 flag_isoc99 = 0;
1871 flag_writable_strings = 0;
1874 /* Set the C 99 standard (without GNU extensions if ISO). */
1875 static void
1876 set_std_c99 (iso)
1877 int iso;
1879 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1880 flag_no_asm = iso;
1881 flag_no_nonansi_builtin = iso;
1882 flag_noniso_default_format_attributes = !iso;
1883 flag_iso = iso;
1884 flag_isoc99 = 1;
1885 flag_isoc94 = 1;
1886 flag_writable_strings = 0;
1889 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1890 static void
1891 set_std_cxx98 (iso)
1892 int iso;
1894 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1895 flag_no_gnu_keywords = iso;
1896 flag_no_nonansi_builtin = iso;
1897 flag_noniso_default_format_attributes = !iso;
1898 flag_iso = iso;
1901 /* Handle setting implicit to ON. */
1902 static void
1903 set_Wimplicit (on)
1904 int on;
1906 warn_implicit = on;
1907 warn_implicit_int = on;
1908 if (on)
1910 if (mesg_implicit_function_declaration != 2)
1911 mesg_implicit_function_declaration = 1;
1913 else
1914 mesg_implicit_function_declaration = 0;
1917 /* Args to -d specify what to dump. Silently ignore
1918 unrecognized options; they may be aimed at toplev.c. */
1919 static void
1920 handle_OPT_d (arg)
1921 const char *arg;
1923 char c;
1925 while ((c = *arg++) != '\0')
1926 switch (c)
1928 case 'M': /* Dump macros only. */
1929 case 'N': /* Dump names. */
1930 case 'D': /* Dump definitions. */
1931 flag_dump_macros = c;
1932 break;
1934 case 'I':
1935 flag_dump_includes = 1;
1936 break;
1940 /* Write a slash-separated list of languages in FLAGS to BUF. */
1941 static void
1942 write_langs (buf, flags)
1943 char *buf;
1944 int flags;
1946 *buf = '\0';
1947 if (flags & CL_C_ONLY)
1948 strcat (buf, "C");
1949 if (flags & CL_OBJC_ONLY)
1951 if (*buf)
1952 strcat (buf, "/");
1953 strcat (buf, "ObjC");
1955 if (flags & CL_CXX_ONLY)
1957 if (*buf)
1958 strcat (buf, "/");
1959 strcat (buf, "C++");
1963 /* Complain that switch OPT_INDEX does not apply to this front end. */
1964 static void
1965 complain_wrong_lang (opt_index)
1966 size_t opt_index;
1968 char ok_langs[60], bad_langs[60];
1969 int ok_flags = cl_options[opt_index].flags;
1971 write_langs (ok_langs, ok_flags);
1972 write_langs (bad_langs, ~ok_flags);
1973 warning ("\"-%s\" is valid for %s but not for %s",
1974 cl_options[opt_index].opt_text, ok_langs, bad_langs);
1977 /* Handle --help output. */
1978 static void
1979 print_help ()
1981 /* To keep the lines from getting too long for some compilers, limit
1982 to about 500 characters (6 lines) per chunk. */
1983 fputs (_("\
1984 Switches:\n\
1985 -include <file> Include the contents of <file> before other files\n\
1986 -imacros <file> Accept definition of macros in <file>\n\
1987 -iprefix <path> Specify <path> as a prefix for next two options\n\
1988 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1989 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1990 -isystem <dir> Add <dir> to the start of the system include path\n\
1991 "), stdout);
1992 fputs (_("\
1993 -idirafter <dir> Add <dir> to the end of the system include path\n\
1994 -I <dir> Add <dir> to the end of the main include path\n\
1995 -I- Fine-grained include path control; see info docs\n\
1996 -nostdinc Do not search system include directories\n\
1997 (dirs specified with -isystem will still be used)\n\
1998 -nostdinc++ Do not search system include directories for C++\n\
1999 -o <file> Put output into <file>\n\
2000 "), stdout);
2001 fputs (_("\
2002 -trigraphs Support ISO C trigraphs\n\
2003 -std=<std name> Specify the conformance standard; one of:\n\
2004 gnu89, gnu99, c89, c99, iso9899:1990,\n\
2005 iso9899:199409, iso9899:1999, c++98\n\
2006 -w Inhibit warning messages\n\
2007 -W[no-]trigraphs Warn if trigraphs are encountered\n\
2008 -W[no-]comment{s} Warn if one comment starts inside another\n\
2009 "), stdout);
2010 fputs (_("\
2011 -W[no-]traditional Warn about features not present in traditional C\n\
2012 -W[no-]undef Warn if an undefined macro is used by #if\n\
2013 -W[no-]import Warn about the use of the #import directive\n\
2014 "), stdout);
2015 fputs (_("\
2016 -W[no-]error Treat all warnings as errors\n\
2017 -W[no-]system-headers Do not suppress warnings from system headers\n\
2018 -W[no-]all Enable most preprocessor warnings\n\
2019 "), stdout);
2020 fputs (_("\
2021 -M Generate make dependencies\n\
2022 -MM As -M, but ignore system header files\n\
2023 -MD Generate make dependencies and compile\n\
2024 -MMD As -MD, but ignore system header files\n\
2025 -MF <file> Write dependency output to the given file\n\
2026 -MG Treat missing header file as generated files\n\
2027 "), stdout);
2028 fputs (_("\
2029 -MP Generate phony targets for all headers\n\
2030 -MQ <target> Add a MAKE-quoted target\n\
2031 -MT <target> Add an unquoted target\n\
2032 "), stdout);
2033 fputs (_("\
2034 -D<macro> Define a <macro> with string '1' as its value\n\
2035 -D<macro>=<val> Define a <macro> with <val> as its value\n\
2036 -A<question>=<answer> Assert the <answer> to <question>\n\
2037 -A-<question>=<answer> Disable the <answer> to <question>\n\
2038 -U<macro> Undefine <macro> \n\
2039 -v Display the version number\n\
2040 "), stdout);
2041 fputs (_("\
2042 -H Print the name of header files as they are used\n\
2043 -C Do not discard comments\n\
2044 -dM Display a list of macro definitions active at end\n\
2045 -dD Preserve macro definitions in output\n\
2046 -dN As -dD except that only the names are preserved\n\
2047 -dI Include #include directives in the output\n\
2048 "), stdout);
2049 fputs (_("\
2050 -f[no-]preprocessed Treat the input file as already preprocessed\n\
2051 -ftabstop=<number> Distance between tab stops for column reporting\n\
2052 -isysroot <dir> Set <dir> to be the system root directory\n\
2053 -P Do not generate #line directives\n\
2054 -remap Remap file names when including files\n\
2055 --help Display this information\n\
2056 "), stdout);