* config/arm/elf.h (ASM_OUTPUT_ALIGNED_COMMON): Remove definition.
[official-gcc.git] / gcc / c-opts.c
blob5de5019499b713775f64005d58fe4083f7f61a9e
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 #ifndef TARGET_EBCDIC
44 # define TARGET_EBCDIC 0
45 #endif
47 static int saved_lineno;
49 /* CPP's options. */
50 static cpp_options *cpp_opts;
52 /* Input filename. */
53 static const char *in_fname;
55 /* Filename and stream for preprocessed output. */
56 static const char *out_fname;
57 static FILE *out_stream;
59 /* Append dependencies to deps_file. */
60 static bool deps_append;
62 /* If dependency switches (-MF etc.) have been given. */
63 static bool deps_seen;
65 /* If -v seen. */
66 static bool verbose;
68 /* Dependency output file. */
69 static const char *deps_file;
71 /* The prefix given by -iprefix, if any. */
72 static const char *iprefix;
74 /* The system root, if any. Overridden by -isysroot. */
75 static const char *sysroot = TARGET_SYSTEM_ROOT;
77 /* Zero disables all standard directories for headers. */
78 static bool std_inc = true;
80 /* Zero disables the C++-specific standard directories for headers. */
81 static bool std_cxx_inc = true;
83 /* If the quote chain has been split by -I-. */
84 static bool quote_chain_split;
86 /* If -Wunused-macros. */
87 static bool warn_unused_macros;
89 /* Number of deferred options, deferred options array size. */
90 static size_t deferred_count, deferred_size;
92 /* Number of deferred options scanned for -include. */
93 static size_t include_cursor;
95 static void missing_arg PARAMS ((size_t));
96 static size_t find_opt PARAMS ((const char *, int));
97 static void set_Wimplicit PARAMS ((int));
98 static void complain_wrong_lang PARAMS ((size_t));
99 static void write_langs PARAMS ((char *, int));
100 static void print_help PARAMS ((void));
101 static void handle_OPT_d PARAMS ((const char *));
102 static void set_std_cxx98 PARAMS ((int));
103 static void set_std_c89 PARAMS ((int, int));
104 static void set_std_c99 PARAMS ((int));
105 static void check_deps_environment_vars PARAMS ((void));
106 static void handle_deferred_opts PARAMS ((void));
107 static void sanitize_cpp_opts PARAMS ((void));
108 static void add_prefixed_path PARAMS ((const char *, size_t));
109 static void push_command_line_include PARAMS ((void));
110 static void cb_file_change PARAMS ((cpp_reader *, const struct line_map *));
111 static void finish_options PARAMS ((void));
113 #ifndef STDC_0_IN_SYSTEM_HEADERS
114 #define STDC_0_IN_SYSTEM_HEADERS 0
115 #endif
117 #define CL_C_ONLY (1 << 0) /* Only C. */
118 #define CL_OBJC_ONLY (1 << 1) /* Only ObjC. */
119 #define CL_CXX_ONLY (1 << 2) /* Only C++. */
120 #define CL_OBJCXX_ONLY (1 << 3) /* Only ObjC++. */
121 #define CL_JOINED (1 << 4) /* If takes joined argument. */
122 #define CL_SEPARATE (1 << 5) /* If takes a separate argument. */
124 #define CL_ARG (CL_JOINED | CL_SEPARATE)
125 #define CL_C (CL_C_ONLY | CL_OBJC_ONLY)
126 #define CL_OBJC (CL_OBJC_ONLY | CL_OBJCXX_ONLY)
127 #define CL_CXX (CL_CXX_ONLY | CL_OBJCXX_ONLY)
128 #define CL_ALL (CL_C | CL_CXX)
130 /* This is the list of all command line options, with the leading "-"
131 removed. It must be sorted in ASCII collating order. All options
132 beginning with "f" or "W" are implicitly assumed to take a "no-"
133 form; this form should not be listed. The variable "on" is true if
134 the positive form is given, otherwise it is false. If you don't
135 want to allow a "no-" form, your handler should reject "on" being
136 false by returning zero. See, for example, the handling of
137 -ftabstop=.
139 If the user gives an option to a front end that doesn't support it,
140 an error is output, mentioning which front ends the option is valid
141 for. If you don't want this, you must accept it for all front
142 ends, and test for the front end in the option handler. See, for
143 example, the handling of -Wno-strict-prototypes for C++.
145 If you request an argument with CL_JOINED, CL_SEPARATE or their
146 combination CL_ARG, it is stored in the variable "arg", which is
147 guaranteed to be non-NULL and to not be an empty string. It points
148 to the argument either within the argv[] vector or within one of
149 that vector's strings, and so the text is permanent and copies need
150 not be made. Be sure to add an error message in missing_arg() if
151 the default is not appropriate. */
153 #define COMMAND_LINE_OPTIONS \
154 OPT("-help", CL_ALL, OPT__help) \
155 OPT("-output-pch=", CL_ALL | CL_ARG, OPT__output_pch) \
156 OPT("A", CL_ALL | CL_ARG, OPT_A) \
157 OPT("C", CL_ALL, OPT_C) \
158 OPT("CC", CL_ALL, OPT_CC) \
159 OPT("D", CL_ALL | CL_ARG, OPT_D) \
160 OPT("E", CL_ALL, OPT_E) \
161 OPT("H", CL_ALL, OPT_H) \
162 OPT("I", CL_ALL | CL_ARG, OPT_I) \
163 OPT("M", CL_ALL, OPT_M) \
164 OPT("MD", CL_ALL | CL_SEPARATE, OPT_MD) \
165 OPT("MF", CL_ALL | CL_ARG, OPT_MF) \
166 OPT("MG", CL_ALL, OPT_MG) \
167 OPT("MM", CL_ALL, OPT_MM) \
168 OPT("MMD", CL_ALL | CL_SEPARATE, OPT_MMD) \
169 OPT("MP", CL_ALL, OPT_MP) \
170 OPT("MQ", CL_ALL | CL_ARG, OPT_MQ) \
171 OPT("MT", CL_ALL | CL_ARG, OPT_MT) \
172 OPT("P", CL_ALL, OPT_P) \
173 OPT("U", CL_ALL | CL_ARG, OPT_U) \
174 OPT("Wabi", CL_CXX, OPT_Wabi) \
175 OPT("Wall", CL_ALL, OPT_Wall) \
176 OPT("Wbad-function-cast", CL_C, OPT_Wbad_function_cast) \
177 OPT("Wcast-qual", CL_ALL, OPT_Wcast_qual) \
178 OPT("Wchar-subscripts", CL_ALL, OPT_Wchar_subscripts) \
179 OPT("Wcomment", CL_ALL, OPT_Wcomment) \
180 OPT("Wcomments", CL_ALL, OPT_Wcomments) \
181 OPT("Wconversion", CL_ALL, OPT_Wconversion) \
182 OPT("Wctor-dtor-privacy", CL_CXX, OPT_Wctor_dtor_privacy) \
183 OPT("Wdeprecated", CL_CXX, OPT_Wdeprecated) \
184 OPT("Wdiv-by-zero", CL_C, OPT_Wdiv_by_zero) \
185 OPT("Weffc++", CL_CXX, OPT_Weffcxx) \
186 OPT("Wendif-labels", CL_ALL, OPT_Wendif_labels) \
187 OPT("Werror", CL_ALL, OPT_Werror) \
188 OPT("Werror-implicit-function-declaration", \
189 CL_C, OPT_Werror_implicit_function_decl) \
190 OPT("Wfloat-equal", CL_ALL, OPT_Wfloat_equal) \
191 OPT("Wformat", CL_ALL, OPT_Wformat) \
192 OPT("Wformat-extra-args", CL_ALL, OPT_Wformat_extra_args) \
193 OPT("Wformat-nonliteral", CL_ALL, OPT_Wformat_nonliteral) \
194 OPT("Wformat-security", CL_ALL, OPT_Wformat_security) \
195 OPT("Wformat-y2k", CL_ALL, OPT_Wformat_y2k) \
196 OPT("Wformat-zero-length", CL_C, OPT_Wformat_zero_length) \
197 OPT("Wformat=", CL_ALL | CL_JOINED, OPT_Wformat_eq) \
198 OPT("Wimplicit", CL_ALL, OPT_Wimplicit) \
199 OPT("Wimplicit-function-declaration", CL_C, OPT_Wimplicit_function_decl) \
200 OPT("Wimplicit-int", CL_C, OPT_Wimplicit_int) \
201 OPT("Wimport", CL_ALL, OPT_Wimport) \
202 OPT("Winvalid-pch", CL_ALL, OPT_Winvalid_pch) \
203 OPT("Wlong-long", CL_ALL, OPT_Wlong_long) \
204 OPT("Wmain", CL_C, OPT_Wmain) \
205 OPT("Wmissing-braces", CL_ALL, OPT_Wmissing_braces) \
206 OPT("Wmissing-declarations", CL_C, OPT_Wmissing_declarations) \
207 OPT("Wmissing-format-attribute",CL_ALL, OPT_Wmissing_format_attribute) \
208 OPT("Wmissing-prototypes", CL_ALL, OPT_Wmissing_prototypes) \
209 OPT("Wmultichar", CL_ALL, OPT_Wmultichar) \
210 OPT("Wnested-externs", CL_C, OPT_Wnested_externs) \
211 OPT("Wnon-template-friend", CL_CXX, OPT_Wnon_template_friend) \
212 OPT("Wnon-virtual-dtor", CL_CXX, OPT_Wnon_virtual_dtor) \
213 OPT("Wnonnull", CL_C, OPT_Wnonnull) \
214 OPT("Wold-style-cast", CL_CXX, OPT_Wold_style_cast) \
215 OPT("Woverloaded-virtual", CL_CXX, OPT_Woverloaded_virtual) \
216 OPT("Wparentheses", CL_ALL, OPT_Wparentheses) \
217 OPT("Wpmf-conversions", CL_CXX, OPT_Wpmf_conversions) \
218 OPT("Wpointer-arith", CL_ALL, OPT_Wpointer_arith) \
219 OPT("Wprotocol", CL_OBJC, OPT_Wprotocol) \
220 OPT("Wredundant-decls", CL_ALL, OPT_Wredundant_decls) \
221 OPT("Wreorder", CL_CXX, OPT_Wreorder) \
222 OPT("Wreturn-type", CL_ALL, OPT_Wreturn_type) \
223 OPT("Wselector", CL_OBJC, OPT_Wselector) \
224 OPT("Wsequence-point", CL_C, OPT_Wsequence_point) \
225 OPT("Wsign-compare", CL_ALL, OPT_Wsign_compare) \
226 OPT("Wsign-promo", CL_CXX, OPT_Wsign_promo) \
227 OPT("Wstrict-prototypes", CL_ALL, OPT_Wstrict_prototypes) \
228 OPT("Wsynth", CL_CXX, OPT_Wsynth) \
229 OPT("Wsystem-headers", CL_ALL, OPT_Wsystem_headers) \
230 OPT("Wtraditional", CL_C, OPT_Wtraditional) \
231 OPT("Wtrigraphs", CL_ALL, OPT_Wtrigraphs) \
232 OPT("Wundeclared-selector", CL_OBJC, OPT_Wundeclared_selector) \
233 OPT("Wundef", CL_ALL, OPT_Wundef) \
234 OPT("Wunknown-pragmas", CL_ALL, OPT_Wunknown_pragmas) \
235 OPT("Wunused-macros", CL_ALL, OPT_Wunused_macros) \
236 OPT("Wwrite-strings", CL_ALL, OPT_Wwrite_strings) \
237 OPT("ansi", CL_ALL, OPT_ansi) \
238 OPT("d", CL_ALL | CL_JOINED, OPT_d) \
239 OPT("fabi-version=", CL_CXX | CL_JOINED, OPT_fabi_version) \
240 OPT("faccess-control", CL_CXX, OPT_faccess_control) \
241 OPT("fall-virtual", CL_CXX, OPT_fall_virtual) \
242 OPT("falt-external-templates",CL_CXX, OPT_falt_external_templates) \
243 OPT("fasm", CL_ALL, OPT_fasm) \
244 OPT("fbuiltin", CL_ALL, OPT_fbuiltin) \
245 OPT("fbuiltin-", CL_ALL | CL_JOINED, OPT_fbuiltin_) \
246 OPT("fcheck-new", CL_CXX, OPT_fcheck_new) \
247 OPT("fcond-mismatch", CL_ALL, OPT_fcond_mismatch) \
248 OPT("fconserve-space", CL_CXX, OPT_fconserve_space) \
249 OPT("fconst-strings", CL_CXX, OPT_fconst_strings) \
250 OPT("fconstant-string-class=", CL_OBJC | CL_JOINED, \
251 OPT_fconstant_string_class) \
252 OPT("fdefault-inline", CL_CXX, OPT_fdefault_inline) \
253 OPT("fdollars-in-identifiers",CL_ALL, OPT_fdollars_in_identifiers) \
254 OPT("fdump-", CL_ALL | CL_JOINED, OPT_fdump) \
255 OPT("felide-constructors", CL_CXX, OPT_felide_constructors) \
256 OPT("fenforce-eh-specs", CL_CXX, OPT_fenforce_eh_specs) \
257 OPT("fenum-int-equiv", CL_CXX, OPT_fenum_int_equiv) \
258 OPT("fexternal-templates", CL_CXX, OPT_fexternal_templates) \
259 OPT("ffixed-form", CL_C, OPT_ffixed_form) \
260 OPT("ffixed-line-length-", CL_C | CL_JOINED, OPT_ffixed_line_length) \
261 OPT("ffor-scope", CL_CXX, OPT_ffor_scope) \
262 OPT("ffreestanding", CL_C, OPT_ffreestanding) \
263 OPT("fgnu-keywords", CL_CXX, OPT_fgnu_keywords) \
264 OPT("fgnu-runtime", CL_OBJC, OPT_fgnu_runtime) \
265 OPT("fguiding-decls", CL_CXX, OPT_fguiding_decls) \
266 OPT("fhandle-exceptions", CL_CXX, OPT_fhandle_exceptions) \
267 OPT("fhonor-std", CL_CXX, OPT_fhonor_std) \
268 OPT("fhosted", CL_C, OPT_fhosted) \
269 OPT("fhuge-objects", CL_CXX, OPT_fhuge_objects) \
270 OPT("fimplement-inlines", CL_CXX, OPT_fimplement_inlines) \
271 OPT("fimplicit-inline-templates", CL_CXX, OPT_fimplicit_inline_templates) \
272 OPT("fimplicit-templates", CL_CXX, OPT_fimplicit_templates) \
273 OPT("flabels-ok", CL_CXX, OPT_flabels_ok) \
274 OPT("fms-extensions", CL_ALL, OPT_fms_extensions) \
275 OPT("fname-mangling-version-",CL_CXX | CL_JOINED, OPT_fname_mangling) \
276 OPT("fnew-abi", CL_CXX, OPT_fnew_abi) \
277 OPT("fnext-runtime", CL_OBJC, OPT_fnext_runtime) \
278 OPT("fnonansi-builtins", CL_CXX, OPT_fnonansi_builtins) \
279 OPT("fnonnull-objects", CL_CXX, OPT_fnonnull_objects) \
280 OPT("foperator-names", CL_CXX, OPT_foperator_names) \
281 OPT("foptional-diags", CL_CXX, OPT_foptional_diags) \
282 OPT("fpch-deps", CL_ALL, OPT_fpch_deps) \
283 OPT("fpermissive", CL_CXX, OPT_fpermissive) \
284 OPT("fpreprocessed", CL_ALL, OPT_fpreprocessed) \
285 OPT("frepo", CL_CXX, OPT_frepo) \
286 OPT("frtti", CL_CXX, OPT_frtti) \
287 OPT("fshort-double", CL_ALL, OPT_fshort_double) \
288 OPT("fshort-enums", CL_ALL, OPT_fshort_enums) \
289 OPT("fshort-wchar", CL_ALL, OPT_fshort_wchar) \
290 OPT("fshow-column", CL_ALL, OPT_fshow_column) \
291 OPT("fsigned-bitfields", CL_ALL, OPT_fsigned_bitfields) \
292 OPT("fsigned-char", CL_ALL, OPT_fsigned_char) \
293 OPT("fsquangle", CL_CXX, OPT_fsquangle) \
294 OPT("fstats", CL_CXX, OPT_fstats) \
295 OPT("fstrict-prototype", CL_CXX, OPT_fstrict_prototype) \
296 OPT("ftabstop=", CL_ALL | CL_JOINED, OPT_ftabstop) \
297 OPT("ftemplate-depth-", CL_CXX | CL_JOINED, OPT_ftemplate_depth) \
298 OPT("fthis-is-variable", CL_CXX, OPT_fthis_is_variable) \
299 OPT("funsigned-bitfields", CL_ALL, OPT_funsigned_bitfields) \
300 OPT("funsigned-char", CL_ALL, OPT_funsigned_char) \
301 OPT("fuse-cxa-atexit", CL_CXX, OPT_fuse_cxa_atexit) \
302 OPT("fvtable-gc", CL_CXX, OPT_fvtable_gc) \
303 OPT("fvtable-thunks", CL_CXX, OPT_fvtable_thunks) \
304 OPT("fweak", CL_CXX, OPT_fweak) \
305 OPT("fxref", CL_CXX, OPT_fxref) \
306 OPT("gen-decls", CL_OBJC, OPT_gen_decls) \
307 OPT("idirafter", CL_ALL | CL_ARG, OPT_idirafter) \
308 OPT("imacros", CL_ALL | CL_ARG, OPT_imacros) \
309 OPT("include", CL_ALL | CL_ARG, OPT_include) \
310 OPT("iprefix", CL_ALL | CL_ARG, OPT_iprefix) \
311 OPT("isysroot", CL_ALL | CL_ARG, OPT_isysroot) \
312 OPT("isystem", CL_ALL | CL_ARG, OPT_isystem) \
313 OPT("iwithprefix", CL_ALL | CL_ARG, OPT_iwithprefix) \
314 OPT("iwithprefixbefore", CL_ALL | CL_ARG, OPT_iwithprefixbefore) \
315 OPT("lang-asm", CL_C_ONLY, OPT_lang_asm) \
316 OPT("lang-objc", CL_ALL, OPT_lang_objc) \
317 OPT("nostdinc", CL_ALL, OPT_nostdinc) \
318 OPT("nostdinc++", CL_ALL, OPT_nostdincplusplus) \
319 OPT("o", CL_ALL | CL_ARG, OPT_o) \
320 OPT("pedantic", CL_ALL, OPT_pedantic) \
321 OPT("pedantic-errors", CL_ALL, OPT_pedantic_errors) \
322 OPT("print-objc-runtime-info", CL_OBJC, OPT_print_objc_runtime_info) \
323 OPT("remap", CL_ALL, OPT_remap) \
324 OPT("std=c++98", CL_CXX, OPT_std_cplusplus98) \
325 OPT("std=c89", CL_C, OPT_std_c89) \
326 OPT("std=c99", CL_C, OPT_std_c99) \
327 OPT("std=c9x", CL_C, OPT_std_c9x) \
328 OPT("std=gnu++98", CL_CXX, OPT_std_gnuplusplus98) \
329 OPT("std=gnu89", CL_C, OPT_std_gnu89) \
330 OPT("std=gnu99", CL_C, OPT_std_gnu99) \
331 OPT("std=gnu9x", CL_C, OPT_std_gnu9x) \
332 OPT("std=iso9899:1990", CL_C, OPT_std_iso9899_1990) \
333 OPT("std=iso9899:199409", CL_C, OPT_std_iso9899_199409) \
334 OPT("std=iso9899:1999", CL_C, OPT_std_iso9899_1999) \
335 OPT("std=iso9899:199x", CL_C, OPT_std_iso9899_199x) \
336 OPT("traditional-cpp", CL_ALL, OPT_traditional_cpp) \
337 OPT("trigraphs", CL_ALL, OPT_trigraphs) \
338 OPT("undef", CL_ALL, OPT_undef) \
339 OPT("v", CL_ALL, OPT_v) \
340 OPT("w", CL_ALL, OPT_w)
342 #define OPT(text, flags, code) code,
343 enum opt_code
345 COMMAND_LINE_OPTIONS
346 N_OPTS
348 #undef OPT
350 struct cl_option
352 const char *opt_text;
353 unsigned char opt_len;
354 unsigned char flags;
355 ENUM_BITFIELD (opt_code) opt_code : 2 * CHAR_BIT;
358 #define OPT(text, flags, code) { text, sizeof(text) - 1, flags, code },
359 #ifdef HOST_EBCDIC
360 static struct cl_option cl_options[] =
361 #else
362 static const struct cl_option cl_options[] =
363 #endif
365 COMMAND_LINE_OPTIONS
367 #undef OPT
368 #undef COMMAND_LINE_OPTIONS
370 /* Holds switches parsed by c_common_decode_option (), but whose
371 handling is deferred to c_common_post_options (). */
372 static void defer_opt PARAMS ((enum opt_code, const char *));
373 static struct deferred_opt
375 enum opt_code code;
376 const char *arg;
377 } *deferred_opts;
380 #ifdef HOST_EBCDIC
381 static int opt_comp PARAMS ((const void *, const void *));
383 /* Run-time sorting of options array. */
384 static int
385 opt_comp (p1, p2)
386 const void *p1, *p2;
388 return strcmp (((struct cl_option *) p1)->opt_text,
389 ((struct cl_option *) p2)->opt_text);
391 #endif
393 /* Complain that switch OPT_INDEX expects an argument but none was
394 provided. */
395 static void
396 missing_arg (opt_index)
397 size_t opt_index;
399 const char *opt_text = cl_options[opt_index].opt_text;
401 switch (cl_options[opt_index].opt_code)
403 case OPT__output_pch:
404 case OPT_Wformat_eq:
405 case OPT_d:
406 case OPT_fabi_version:
407 case OPT_fbuiltin_:
408 case OPT_fdump:
409 case OPT_fname_mangling:
410 case OPT_ftabstop:
411 case OPT_ftemplate_depth:
412 case OPT_iprefix:
413 case OPT_iwithprefix:
414 case OPT_iwithprefixbefore:
415 default:
416 error ("missing argument to \"-%s\"", opt_text);
417 break;
419 case OPT_fconstant_string_class:
420 error ("no class name specified with \"-%s\"", opt_text);
421 break;
423 case OPT_A:
424 error ("assertion missing after \"-%s\"", opt_text);
425 break;
427 case OPT_D:
428 case OPT_U:
429 error ("macro name missing after \"-%s\"", opt_text);
430 break;
432 case OPT_I:
433 case OPT_idirafter:
434 case OPT_isysroot:
435 case OPT_isystem:
436 error ("missing path after \"-%s\"", opt_text);
437 break;
439 case OPT_MF:
440 case OPT_MD:
441 case OPT_MMD:
442 case OPT_include:
443 case OPT_imacros:
444 case OPT_o:
445 error ("missing filename after \"-%s\"", opt_text);
446 break;
448 case OPT_MQ:
449 case OPT_MT:
450 error ("missing target after \"-%s\"", opt_text);
451 break;
455 /* Perform a binary search to find which option the command-line INPUT
456 matches. Returns its index in the option array, and N_OPTS on
457 failure.
459 Complications arise since some options can be suffixed with an
460 argument, and multiple complete matches can occur, e.g. -pedantic
461 and -pedantic-errors. Also, some options are only accepted by some
462 languages. If a switch matches for a different language and
463 doesn't match any alternatives for the true front end, the index of
464 the matched switch is returned anyway. The caller should check for
465 this case. */
466 static size_t
467 find_opt (input, lang_flag)
468 const char *input;
469 int lang_flag;
471 size_t md, mn, mx;
472 size_t opt_len;
473 size_t result = N_OPTS;
474 int comp;
476 mn = 0;
477 mx = N_OPTS;
479 while (mx > mn)
481 md = (mn + mx) / 2;
483 opt_len = cl_options[md].opt_len;
484 comp = strncmp (input, cl_options[md].opt_text, opt_len);
486 if (comp < 0)
487 mx = md;
488 else if (comp > 0)
489 mn = md + 1;
490 else
492 /* The switch matches. It it an exact match? */
493 if (input[opt_len] == '\0')
494 return md;
495 else
497 mn = md + 1;
499 /* If the switch takes no arguments this is not a proper
500 match, so we continue the search (e.g. input="stdc++"
501 match was "stdc"). */
502 if (!(cl_options[md].flags & CL_JOINED))
503 continue;
505 /* Is this switch valid for this front end? */
506 if (!(cl_options[md].flags & lang_flag))
508 /* If subsequently we don't find a better match,
509 return this and let the caller report it as a bad
510 match. */
511 result = md;
512 continue;
515 /* Two scenarios remain: we have the switch's argument,
516 or we match a longer option. This can happen with
517 -iwithprefix and -withprefixbefore. The longest
518 possible option match succeeds.
520 Scan forwards, and return an exact match. Otherwise
521 return the longest valid option-accepting match (mx).
522 This loops at most twice with current options. */
523 mx = md;
524 for (md = md + 1; md < (size_t) N_OPTS; md++)
526 opt_len = cl_options[md].opt_len;
527 if (strncmp (input, cl_options[md].opt_text, opt_len))
528 break;
529 if (input[opt_len] == '\0')
530 return md;
531 if (cl_options[md].flags & lang_flag
532 && cl_options[md].flags & CL_JOINED)
533 mx = md;
536 return mx;
541 return result;
544 /* Defer option CODE with argument ARG. */
545 static void
546 defer_opt (code, arg)
547 enum opt_code code;
548 const char *arg;
550 /* FIXME: this should be in c_common_init_options, which should take
551 argc and argv. */
552 if (!deferred_opts)
554 extern int save_argc;
555 deferred_size = save_argc;
556 deferred_opts = (struct deferred_opt *)
557 xmalloc (deferred_size * sizeof (struct deferred_opt));
560 if (deferred_count == deferred_size)
561 abort ();
563 deferred_opts[deferred_count].code = code;
564 deferred_opts[deferred_count].arg = arg;
565 deferred_count++;
568 /* Common initialization before parsing options. */
569 void
570 c_common_init_options (lang)
571 enum c_language_kind lang;
573 #ifdef HOST_EBCDIC
574 /* For non-ASCII hosts, the cl_options array needs to be sorted at
575 runtime. */
576 qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
577 #endif
578 #if ENABLE_CHECKING
580 size_t i;
582 for (i = 1; i < N_OPTS; i++)
583 if (strcmp (cl_options[i - 1].opt_text, cl_options[i].opt_text) >= 0)
584 error ("options array incorrectly sorted: %s is before %s",
585 cl_options[i - 1].opt_text, cl_options[i].opt_text);
587 #endif
589 c_language = lang;
590 parse_in = cpp_create_reader (lang == clk_c ? CLK_GNUC89 : CLK_GNUCXX,
591 ident_hash);
592 cpp_opts = cpp_get_options (parse_in);
593 if (flag_objc)
594 cpp_opts->objc = 1;
596 flag_const_strings = (lang == clk_cplusplus);
597 warn_pointer_arith = (lang == clk_cplusplus);
598 if (lang == clk_c)
599 warn_sign_compare = -1;
602 /* Handle one command-line option in (argc, argv).
603 Can be called multiple times, to handle multiple sets of options.
604 Returns number of strings consumed. */
606 c_common_decode_option (argc, argv)
607 int argc;
608 char **argv;
610 static const int lang_flags[] = {CL_C_ONLY, CL_C, CL_CXX_ONLY, CL_CXX};
611 size_t opt_index;
612 const char *opt, *arg = 0;
613 char *dup = 0;
614 bool on = true;
615 int result = 0, lang_flag;
616 const struct cl_option *option;
617 enum opt_code code;
619 opt = argv[0];
621 /* Interpret "-" or a non-switch as a file name. */
622 if (opt[0] != '-' || opt[1] == '\0')
624 if (!in_fname)
625 in_fname = opt;
626 else if (!out_fname)
627 out_fname = opt;
628 else
630 error ("too many filenames given. Type %s --help for usage",
631 progname);
632 return argc;
635 return 1;
638 /* Drop the "no-" from negative switches. */
639 if ((opt[1] == 'W' || opt[1] == 'f')
640 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
642 size_t len = strlen (opt) - 3;
644 dup = xmalloc (len + 1);
645 dup[0] = '-';
646 dup[1] = opt[1];
647 memcpy (dup + 2, opt + 5, len - 2 + 1);
648 opt = dup;
649 on = false;
652 /* Skip over '-'. */
653 lang_flag = lang_flags[(c_language << 1) + flag_objc];
654 opt_index = find_opt (opt + 1, lang_flag);
655 if (opt_index == N_OPTS)
656 goto done;
658 result = 1;
659 option = &cl_options[opt_index];
661 /* Sort out any argument the switch takes. */
662 if (option->flags & CL_ARG)
664 if (option->flags & CL_JOINED)
666 /* Have arg point to the original switch. This is because
667 some code, such as disable_builtin_function, expects its
668 argument to be persistent until the program exits. */
669 arg = argv[0] + cl_options[opt_index].opt_len + 1;
670 if (!on)
671 arg += strlen ("no-");
674 /* If we don't have an argument, and CL_SEPARATE, try the next
675 argument in the vector. */
676 if (!arg || (*arg == '\0' && option->flags & CL_SEPARATE))
678 arg = argv[1];
679 result = 2;
682 if (!arg || *arg == '\0')
684 missing_arg (opt_index);
685 result = argc;
686 goto done;
690 /* Complain about the wrong language after we've swallowed any
691 necessary extra argument. Eventually make this a hard error
692 after the call to find_opt, and return argc. */
693 if (!(cl_options[opt_index].flags & lang_flag))
695 complain_wrong_lang (opt_index);
696 goto done;
699 switch (code = option->opt_code)
701 case N_OPTS: /* Shut GCC up. */
702 break;
704 case OPT__help:
705 print_help ();
706 break;
708 case OPT__output_pch:
709 pch_file = arg;
710 break;
712 case OPT_A:
713 defer_opt (code, arg);
714 break;
716 case OPT_C:
717 cpp_opts->discard_comments = 0;
718 break;
720 case OPT_CC:
721 cpp_opts->discard_comments = 0;
722 cpp_opts->discard_comments_in_macro_exp = 0;
723 break;
725 case OPT_D:
726 defer_opt (code, arg);
727 break;
729 case OPT_E:
730 flag_preprocess_only = 1;
731 break;
733 case OPT_H:
734 cpp_opts->print_include_names = 1;
735 break;
737 case OPT_I:
738 if (strcmp (arg, "-"))
739 add_path (xstrdup (arg), BRACKET, 0);
740 else
742 if (quote_chain_split)
743 error ("-I- specified twice");
744 quote_chain_split = true;
745 split_quote_chain ();
747 break;
749 case OPT_M:
750 case OPT_MM:
751 /* When doing dependencies with -M or -MM, suppress normal
752 preprocessed output, but still do -dM etc. as software
753 depends on this. Preprocessed output does occur if -MD, -MMD
754 or environment var dependency generation is used. */
755 cpp_opts->deps.style = (code == OPT_M ? DEPS_SYSTEM: DEPS_USER);
756 flag_no_output = 1;
757 cpp_opts->inhibit_warnings = 1;
758 break;
760 case OPT_MD:
761 case OPT_MMD:
762 cpp_opts->deps.style = (code == OPT_MD ? DEPS_SYSTEM: DEPS_USER);
763 deps_file = arg;
764 break;
766 case OPT_MF:
767 deps_seen = true;
768 deps_file = arg;
769 break;
771 case OPT_MG:
772 deps_seen = true;
773 cpp_opts->deps.missing_files = true;
774 break;
776 case OPT_MP:
777 deps_seen = true;
778 cpp_opts->deps.phony_targets = true;
779 break;
781 case OPT_MQ:
782 case OPT_MT:
783 deps_seen = true;
784 defer_opt (code, arg);
785 break;
787 case OPT_P:
788 flag_no_line_commands = 1;
789 break;
791 case OPT_U:
792 defer_opt (code, arg);
793 break;
795 case OPT_Wabi:
796 warn_abi = on;
797 break;
799 case OPT_Wall:
800 set_Wunused (on);
801 set_Wformat (on);
802 set_Wimplicit (on);
803 warn_char_subscripts = on;
804 warn_missing_braces = on;
805 warn_parentheses = on;
806 warn_return_type = on;
807 warn_sequence_point = on; /* Was C only. */
808 warn_sign_compare = on; /* Was C++ only. */
809 warn_switch = on;
810 warn_strict_aliasing = on;
812 /* Only warn about unknown pragmas that are not in system
813 headers. */
814 warn_unknown_pragmas = on;
816 /* We save the value of warn_uninitialized, since if they put
817 -Wuninitialized on the command line, we need to generate a
818 warning about not using it without also specifying -O. */
819 if (warn_uninitialized != 1)
820 warn_uninitialized = (on ? 2 : 0);
822 if (c_language == clk_c)
823 /* We set this to 2 here, but 1 in -Wmain, so -ffreestanding
824 can turn it off only if it's not explicit. */
825 warn_main = on * 2;
826 else
828 /* C++-specific warnings. */
829 warn_ctor_dtor_privacy = on;
830 warn_nonvdtor = on;
831 warn_reorder = on;
832 warn_nontemplate_friend = on;
835 cpp_opts->warn_trigraphs = on;
836 cpp_opts->warn_comments = on;
837 cpp_opts->warn_num_sign_change = on;
838 cpp_opts->warn_multichar = on; /* Was C++ only. */
839 break;
841 case OPT_Wbad_function_cast:
842 warn_bad_function_cast = on;
843 break;
845 case OPT_Wcast_qual:
846 warn_cast_qual = on;
847 break;
849 case OPT_Wchar_subscripts:
850 warn_char_subscripts = on;
851 break;
853 case OPT_Wcomment:
854 case OPT_Wcomments:
855 cpp_opts->warn_comments = on;
856 break;
858 case OPT_Wconversion:
859 warn_conversion = on;
860 break;
862 case OPT_Wctor_dtor_privacy:
863 warn_ctor_dtor_privacy = on;
864 break;
866 case OPT_Wdeprecated:
867 warn_deprecated = on;
868 cpp_opts->warn_deprecated = on;
869 break;
871 case OPT_Wdiv_by_zero:
872 warn_div_by_zero = on;
873 break;
875 case OPT_Weffcxx:
876 warn_ecpp = on;
877 break;
879 case OPT_Wendif_labels:
880 cpp_opts->warn_endif_labels = on;
881 break;
883 case OPT_Werror:
884 cpp_opts->warnings_are_errors = on;
885 break;
887 case OPT_Werror_implicit_function_decl:
888 if (!on)
889 result = 0;
890 else
891 mesg_implicit_function_declaration = 2;
892 break;
894 case OPT_Wfloat_equal:
895 warn_float_equal = on;
896 break;
898 case OPT_Wformat:
899 set_Wformat (on);
900 break;
902 case OPT_Wformat_eq:
903 set_Wformat (atoi (arg));
904 break;
906 case OPT_Wformat_extra_args:
907 warn_format_extra_args = on;
908 break;
910 case OPT_Wformat_nonliteral:
911 warn_format_nonliteral = on;
912 break;
914 case OPT_Wformat_security:
915 warn_format_security = on;
916 break;
918 case OPT_Wformat_y2k:
919 warn_format_y2k = on;
920 break;
922 case OPT_Wformat_zero_length:
923 warn_format_zero_length = on;
924 break;
926 case OPT_Wimplicit:
927 set_Wimplicit (on);
928 break;
930 case OPT_Wimplicit_function_decl:
931 mesg_implicit_function_declaration = on;
932 break;
934 case OPT_Wimplicit_int:
935 warn_implicit_int = on;
936 break;
938 case OPT_Wimport:
939 cpp_opts->warn_import = on;
940 break;
942 case OPT_Winvalid_pch:
943 cpp_opts->warn_invalid_pch = on;
944 break;
946 case OPT_Wlong_long:
947 warn_long_long = on;
948 break;
950 case OPT_Wmain:
951 if (on)
952 warn_main = 1;
953 else
954 warn_main = -1;
955 break;
957 case OPT_Wmissing_braces:
958 warn_missing_braces = on;
959 break;
961 case OPT_Wmissing_declarations:
962 warn_missing_declarations = on;
963 break;
965 case OPT_Wmissing_format_attribute:
966 warn_missing_format_attribute = on;
967 break;
969 case OPT_Wmissing_prototypes:
970 warn_missing_prototypes = on;
971 break;
973 case OPT_Wmultichar:
974 cpp_opts->warn_multichar = on;
975 break;
977 case OPT_Wnested_externs:
978 warn_nested_externs = on;
979 break;
981 case OPT_Wnon_template_friend:
982 warn_nontemplate_friend = on;
983 break;
985 case OPT_Wnon_virtual_dtor:
986 warn_nonvdtor = on;
987 break;
989 case OPT_Wnonnull:
990 warn_nonnull = on;
991 break;
993 case OPT_Wold_style_cast:
994 warn_old_style_cast = on;
995 break;
997 case OPT_Woverloaded_virtual:
998 warn_overloaded_virtual = on;
999 break;
1001 case OPT_Wparentheses:
1002 warn_parentheses = on;
1003 break;
1005 case OPT_Wpmf_conversions:
1006 warn_pmf2ptr = on;
1007 break;
1009 case OPT_Wpointer_arith:
1010 warn_pointer_arith = on;
1011 break;
1013 case OPT_Wprotocol:
1014 warn_protocol = on;
1015 break;
1017 case OPT_Wselector:
1018 warn_selector = on;
1019 break;
1021 case OPT_Wredundant_decls:
1022 warn_redundant_decls = on;
1023 break;
1025 case OPT_Wreorder:
1026 warn_reorder = on;
1027 break;
1029 case OPT_Wreturn_type:
1030 warn_return_type = on;
1031 break;
1033 case OPT_Wsequence_point:
1034 warn_sequence_point = on;
1035 break;
1037 case OPT_Wsign_compare:
1038 warn_sign_compare = on;
1039 break;
1041 case OPT_Wsign_promo:
1042 warn_sign_promo = on;
1043 break;
1045 case OPT_Wstrict_prototypes:
1046 if (!on && c_language == clk_cplusplus)
1047 warning ("-Wno-strict-prototypes is not supported in C++");
1048 else
1049 warn_strict_prototypes = on;
1050 break;
1052 case OPT_Wsynth:
1053 warn_synth = on;
1054 break;
1056 case OPT_Wsystem_headers:
1057 cpp_opts->warn_system_headers = on;
1058 break;
1060 case OPT_Wtraditional:
1061 warn_traditional = on;
1062 cpp_opts->warn_traditional = on;
1063 break;
1065 case OPT_Wtrigraphs:
1066 cpp_opts->warn_trigraphs = on;
1067 break;
1069 case OPT_Wundeclared_selector:
1070 warn_undeclared_selector = on;
1071 break;
1073 case OPT_Wundef:
1074 cpp_opts->warn_undef = on;
1075 break;
1077 case OPT_Wunknown_pragmas:
1078 /* Set to greater than 1, so that even unknown pragmas in
1079 system headers will be warned about. */
1080 warn_unknown_pragmas = on * 2;
1081 break;
1083 case OPT_Wunused_macros:
1084 warn_unused_macros = on;
1085 break;
1087 case OPT_Wwrite_strings:
1088 if (c_language == clk_c)
1089 flag_const_strings = on;
1090 else
1091 warn_write_strings = on;
1092 break;
1094 case OPT_ansi:
1095 if (c_language == clk_c)
1096 set_std_c89 (false, true);
1097 else
1098 set_std_cxx98 (true);
1099 break;
1101 case OPT_d:
1102 handle_OPT_d (arg);
1103 break;
1105 case OPT_fcond_mismatch:
1106 if (c_language == clk_c)
1108 flag_cond_mismatch = on;
1109 break;
1111 /* Fall through. */
1113 case OPT_fall_virtual:
1114 case OPT_fenum_int_equiv:
1115 case OPT_fguiding_decls:
1116 case OPT_fhonor_std:
1117 case OPT_fhuge_objects:
1118 case OPT_flabels_ok:
1119 case OPT_fname_mangling:
1120 case OPT_fnew_abi:
1121 case OPT_fnonnull_objects:
1122 case OPT_fsquangle:
1123 case OPT_fstrict_prototype:
1124 case OPT_fthis_is_variable:
1125 case OPT_fvtable_thunks:
1126 case OPT_fxref:
1127 warning ("switch \"%s\" is no longer supported", argv[0]);
1128 break;
1130 case OPT_fabi_version:
1131 flag_abi_version = read_integral_parameter (arg, argv[0], 1);
1132 break;
1134 case OPT_faccess_control:
1135 flag_access_control = on;
1136 break;
1138 case OPT_falt_external_templates:
1139 flag_alt_external_templates = on;
1140 if (on)
1141 flag_external_templates = true;
1142 cp_deprecated:
1143 warning ("switch \"%s\" is deprecated, please see documentation for details", argv[0]);
1144 break;
1146 case OPT_fasm:
1147 flag_no_asm = !on;
1148 break;
1150 case OPT_fbuiltin:
1151 flag_no_builtin = !on;
1152 break;
1154 case OPT_fbuiltin_:
1155 if (on)
1156 result = 0;
1157 else
1158 disable_builtin_function (arg);
1159 break;
1161 case OPT_fdollars_in_identifiers:
1162 dollars_in_ident = on;
1163 break;
1165 case OPT_fdump:
1166 if (!on || !dump_switch_p (argv[0] + strlen ("-f")))
1167 result = 0;
1168 break;
1170 case OPT_ffreestanding:
1171 on = !on;
1172 /* Fall through... */
1173 case OPT_fhosted:
1174 flag_hosted = on;
1175 flag_no_builtin = !on;
1176 /* warn_main will be 2 if set by -Wall, 1 if set by -Wmain */
1177 if (!on && warn_main == 2)
1178 warn_main = 0;
1179 break;
1181 case OPT_fshort_double:
1182 flag_short_double = on;
1183 break;
1185 case OPT_fshort_enums:
1186 flag_short_enums = on;
1187 break;
1189 case OPT_fshort_wchar:
1190 flag_short_wchar = on;
1191 break;
1193 case OPT_fsigned_bitfields:
1194 flag_signed_bitfields = on;
1195 explicit_flag_signed_bitfields = 1;
1196 break;
1198 case OPT_fsigned_char:
1199 flag_signed_char = on;
1200 break;
1202 case OPT_funsigned_bitfields:
1203 flag_signed_bitfields = !on;
1204 explicit_flag_signed_bitfields = 1;
1205 break;
1207 case OPT_funsigned_char:
1208 flag_signed_char = !on;
1209 break;
1211 case OPT_fcheck_new:
1212 flag_check_new = on;
1213 break;
1215 case OPT_fconserve_space:
1216 flag_conserve_space = on;
1217 break;
1219 case OPT_fconst_strings:
1220 flag_const_strings = on;
1221 break;
1223 case OPT_fconstant_string_class:
1224 constant_string_class_name = arg;
1225 break;
1227 case OPT_fdefault_inline:
1228 flag_default_inline = on;
1229 break;
1231 case OPT_felide_constructors:
1232 flag_elide_constructors = on;
1233 break;
1235 case OPT_fenforce_eh_specs:
1236 flag_enforce_eh_specs = on;
1237 break;
1239 case OPT_fexternal_templates:
1240 flag_external_templates = on;
1241 goto cp_deprecated;
1243 case OPT_ffixed_form:
1244 case OPT_ffixed_line_length:
1245 /* Fortran front end options ignored when preprocessing only. */
1246 if (flag_preprocess_only)
1247 result = -1;
1248 break;
1250 case OPT_ffor_scope:
1251 flag_new_for_scope = on;
1252 break;
1254 case OPT_fgnu_keywords:
1255 flag_no_gnu_keywords = !on;
1256 break;
1258 case OPT_fgnu_runtime:
1259 flag_next_runtime = !on;
1260 break;
1262 case OPT_fhandle_exceptions:
1263 warning ("-fhandle-exceptions has been renamed to -fexceptions (and is now on by default)");
1264 flag_exceptions = on;
1265 break;
1267 case OPT_fimplement_inlines:
1268 flag_implement_inlines = on;
1269 break;
1271 case OPT_fimplicit_inline_templates:
1272 flag_implicit_inline_templates = on;
1273 break;
1275 case OPT_fimplicit_templates:
1276 flag_implicit_templates = on;
1277 break;
1279 case OPT_fms_extensions:
1280 flag_ms_extensions = on;
1281 break;
1283 case OPT_fnext_runtime:
1284 flag_next_runtime = on;
1285 break;
1287 case OPT_fnonansi_builtins:
1288 flag_no_nonansi_builtin = !on;
1289 break;
1291 case OPT_foperator_names:
1292 cpp_opts->operator_names = on;
1293 break;
1295 case OPT_foptional_diags:
1296 flag_optional_diags = on;
1297 break;
1299 case OPT_fpch_deps:
1300 cpp_opts->restore_pch_deps = on;
1301 break;
1303 case OPT_fpermissive:
1304 flag_permissive = on;
1305 break;
1307 case OPT_fpreprocessed:
1308 cpp_opts->preprocessed = on;
1309 break;
1311 case OPT_frepo:
1312 flag_use_repository = on;
1313 if (on)
1314 flag_implicit_templates = 0;
1315 break;
1317 case OPT_frtti:
1318 flag_rtti = on;
1319 break;
1321 case OPT_fshow_column:
1322 cpp_opts->show_column = on;
1323 break;
1325 case OPT_fstats:
1326 flag_detailed_statistics = on;
1327 break;
1329 case OPT_ftabstop:
1330 /* Don't recognize -fno-tabstop=. */
1331 if (!on)
1332 return 0;
1334 /* It is documented that we silently ignore silly values. */
1336 char *endptr;
1337 long tabstop = strtol (arg, &endptr, 10);
1338 if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1339 cpp_opts->tabstop = tabstop;
1341 break;
1343 case OPT_ftemplate_depth:
1344 max_tinst_depth = read_integral_parameter (arg, argv[0], 0);
1345 break;
1347 case OPT_fvtable_gc:
1348 flag_vtable_gc = on;
1349 break;
1351 case OPT_fuse_cxa_atexit:
1352 flag_use_cxa_atexit = on;
1353 break;
1355 case OPT_fweak:
1356 flag_weak = on;
1357 break;
1359 case OPT_gen_decls:
1360 flag_gen_declaration = 1;
1361 break;
1363 case OPT_idirafter:
1364 add_path (xstrdup (arg), AFTER, 0);
1365 break;
1367 case OPT_imacros:
1368 case OPT_include:
1369 defer_opt (code, arg);
1370 break;
1372 case OPT_iprefix:
1373 iprefix = arg;
1374 break;
1376 case OPT_isysroot:
1377 sysroot = arg;
1378 break;
1380 case OPT_isystem:
1381 add_path (xstrdup (arg), SYSTEM, 0);
1382 break;
1384 case OPT_iwithprefix:
1385 add_prefixed_path (arg, SYSTEM);
1386 break;
1388 case OPT_iwithprefixbefore:
1389 add_prefixed_path (arg, BRACKET);
1390 break;
1392 case OPT_lang_asm:
1393 cpp_set_lang (parse_in, CLK_ASM);
1394 break;
1396 case OPT_lang_objc:
1397 cpp_opts->objc = 1;
1398 break;
1400 case OPT_nostdinc:
1401 std_inc = false;
1402 break;
1404 case OPT_nostdincplusplus:
1405 std_cxx_inc = false;
1406 break;
1408 case OPT_o:
1409 if (!out_fname)
1410 out_fname = arg;
1411 else
1413 error ("output filename specified twice");
1414 result = argc;
1416 break;
1418 /* We need to handle the -pedantic switches here, rather than in
1419 c_common_post_options, so that a subsequent -Wno-endif-labels
1420 is not overridden. */
1421 case OPT_pedantic_errors:
1422 cpp_opts->pedantic_errors = 1;
1423 /* fall through */
1424 case OPT_pedantic:
1425 cpp_opts->pedantic = 1;
1426 cpp_opts->warn_endif_labels = 1;
1427 break;
1429 case OPT_print_objc_runtime_info:
1430 print_struct_values = 1;
1431 break;
1433 case OPT_remap:
1434 cpp_opts->remap = 1;
1435 break;
1437 case OPT_std_cplusplus98:
1438 case OPT_std_gnuplusplus98:
1439 set_std_cxx98 (code == OPT_std_cplusplus98 /* ISO */);
1440 break;
1442 case OPT_std_c89:
1443 case OPT_std_iso9899_1990:
1444 case OPT_std_iso9899_199409:
1445 set_std_c89 (code == OPT_std_iso9899_199409 /* c94 */, true /* ISO */);
1446 break;
1448 case OPT_std_gnu89:
1449 set_std_c89 (false /* c94 */, false /* ISO */);
1450 break;
1452 case OPT_std_c99:
1453 case OPT_std_c9x:
1454 case OPT_std_iso9899_1999:
1455 case OPT_std_iso9899_199x:
1456 set_std_c99 (true /* ISO */);
1457 break;
1459 case OPT_std_gnu99:
1460 case OPT_std_gnu9x:
1461 set_std_c99 (false /* ISO */);
1462 break;
1464 case OPT_trigraphs:
1465 cpp_opts->trigraphs = 1;
1466 break;
1468 case OPT_traditional_cpp:
1469 cpp_opts->traditional = 1;
1470 break;
1472 case OPT_undef:
1473 flag_undef = 1;
1474 break;
1476 case OPT_w:
1477 cpp_opts->inhibit_warnings = 1;
1478 break;
1480 case OPT_v:
1481 verbose = true;
1482 break;
1485 done:
1486 if (dup)
1487 free (dup);
1488 return result;
1491 /* Post-switch processing. */
1492 bool
1493 c_common_post_options (pfilename)
1494 const char **pfilename;
1496 /* Canonicalize the input and output filenames. */
1497 if (in_fname == NULL || !strcmp (in_fname, "-"))
1498 in_fname = "";
1500 if (out_fname == NULL || !strcmp (out_fname, "-"))
1501 out_fname = "";
1503 if (cpp_opts->deps.style == DEPS_NONE)
1504 check_deps_environment_vars ();
1506 handle_deferred_opts ();
1508 sanitize_cpp_opts ();
1510 register_include_chains (parse_in, sysroot, iprefix,
1511 std_inc, std_cxx_inc && c_language == clk_cplusplus,
1512 verbose);
1514 flag_inline_trees = 1;
1516 /* Use tree inlining if possible. Function instrumentation is only
1517 done in the RTL level, so we disable tree inlining. */
1518 if (! flag_instrument_function_entry_exit)
1520 if (!flag_no_inline)
1521 flag_no_inline = 1;
1522 if (flag_inline_functions)
1524 flag_inline_trees = 2;
1525 flag_inline_functions = 0;
1529 /* Special format checking options don't work without -Wformat; warn if
1530 they are used. */
1531 if (warn_format_y2k && !warn_format)
1532 warning ("-Wformat-y2k ignored without -Wformat");
1533 if (warn_format_extra_args && !warn_format)
1534 warning ("-Wformat-extra-args ignored without -Wformat");
1535 if (warn_format_zero_length && !warn_format)
1536 warning ("-Wformat-zero-length ignored without -Wformat");
1537 if (warn_format_nonliteral && !warn_format)
1538 warning ("-Wformat-nonliteral ignored without -Wformat");
1539 if (warn_format_security && !warn_format)
1540 warning ("-Wformat-security ignored without -Wformat");
1541 if (warn_missing_format_attribute && !warn_format)
1542 warning ("-Wmissing-format-attribute ignored without -Wformat");
1544 if (flag_preprocess_only)
1546 /* Open the output now. We must do so even if flag_no_output is
1547 on, because there may be other output than from the actual
1548 preprocessing (e.g. from -dM). */
1549 if (out_fname[0] == '\0')
1550 out_stream = stdout;
1551 else
1552 out_stream = fopen (out_fname, "w");
1554 if (out_stream == NULL)
1556 fatal_io_error ("opening output file %s", out_fname);
1557 return false;
1560 init_pp_output (out_stream);
1562 else
1564 init_c_lex ();
1566 /* Yuk. WTF is this? I do know ObjC relies on it somewhere. */
1567 lineno = 0;
1570 cpp_get_callbacks (parse_in)->file_change = cb_file_change;
1572 /* NOTE: we use in_fname here, not the one supplied. */
1573 *pfilename = cpp_read_main_file (parse_in, in_fname);
1575 saved_lineno = lineno;
1576 lineno = 0;
1578 /* If an error has occurred in cpplib, note it so we fail
1579 immediately. */
1580 errorcount += cpp_errors (parse_in);
1582 return flag_preprocess_only;
1585 /* Front end initialization common to C, ObjC and C++. */
1586 bool
1587 c_common_init ()
1589 lineno = saved_lineno;
1591 /* Set up preprocessor arithmetic. Must be done after call to
1592 c_common_nodes_and_builtins for type nodes to be good. */
1593 cpp_opts->precision = TYPE_PRECISION (intmax_type_node);
1594 cpp_opts->char_precision = TYPE_PRECISION (char_type_node);
1595 cpp_opts->int_precision = TYPE_PRECISION (integer_type_node);
1596 cpp_opts->wchar_precision = TYPE_PRECISION (wchar_type_node);
1597 cpp_opts->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
1598 cpp_opts->EBCDIC = TARGET_EBCDIC;
1600 if (flag_preprocess_only)
1602 finish_options ();
1603 preprocess_file (parse_in);
1604 return false;
1607 /* Has to wait until now so that cpplib has its hash table. */
1608 init_pragma ();
1610 return true;
1613 /* A thin wrapper around the real parser that initializes the
1614 integrated preprocessor after debug output has been initialized.
1615 Also, make sure the start_source_file debug hook gets called for
1616 the primary source file. */
1617 void
1618 c_common_parse_file (set_yydebug)
1619 int set_yydebug ATTRIBUTE_UNUSED;
1621 #if YYDEBUG != 0
1622 yydebug = set_yydebug;
1623 #else
1624 warning ("YYDEBUG not defined");
1625 #endif
1627 (*debug_hooks->start_source_file) (lineno, input_filename);
1628 finish_options();
1629 pch_init();
1630 yyparse ();
1631 free_parser_stacks ();
1634 /* Common finish hook for the C, ObjC and C++ front ends. */
1635 void
1636 c_common_finish ()
1638 FILE *deps_stream = NULL;
1640 if (cpp_opts->deps.style != DEPS_NONE)
1642 /* If -M or -MM was seen without -MF, default output to the
1643 output stream. */
1644 if (!deps_file)
1645 deps_stream = out_stream;
1646 else
1648 deps_stream = fopen (deps_file, deps_append ? "a": "w");
1649 if (!deps_stream)
1650 fatal_io_error ("opening dependency file %s", deps_file);
1654 /* For performance, avoid tearing down cpplib's internal structures
1655 with cpp_destroy (). */
1656 errorcount += cpp_finish (parse_in, deps_stream);
1658 if (deps_stream && deps_stream != out_stream
1659 && (ferror (deps_stream) || fclose (deps_stream)))
1660 fatal_io_error ("closing dependency file %s", deps_file);
1662 if (out_stream && (ferror (out_stream) || fclose (out_stream)))
1663 fatal_io_error ("when writing output to %s", out_fname);
1666 /* Either of two environment variables can specify output of
1667 dependencies. Their value is either "OUTPUT_FILE" or "OUTPUT_FILE
1668 DEPS_TARGET", where OUTPUT_FILE is the file to write deps info to
1669 and DEPS_TARGET is the target to mention in the deps. They also
1670 result in dependency information being appended to the output file
1671 rather than overwriting it, and like Sun's compiler
1672 SUNPRO_DEPENDENCIES suppresses the dependency on the main file. */
1673 static void
1674 check_deps_environment_vars ()
1676 char *spec;
1678 GET_ENVIRONMENT (spec, "DEPENDENCIES_OUTPUT");
1679 if (spec)
1680 cpp_opts->deps.style = DEPS_USER;
1681 else
1683 GET_ENVIRONMENT (spec, "SUNPRO_DEPENDENCIES");
1684 if (spec)
1686 cpp_opts->deps.style = DEPS_SYSTEM;
1687 cpp_opts->deps.ignore_main_file = true;
1691 if (spec)
1693 /* Find the space before the DEPS_TARGET, if there is one. */
1694 char *s = strchr (spec, ' ');
1695 if (s)
1697 /* Let the caller perform MAKE quoting. */
1698 defer_opt (OPT_MT, s + 1);
1699 *s = '\0';
1702 /* Command line -MF overrides environment variables and default. */
1703 if (!deps_file)
1704 deps_file = spec;
1706 deps_append = 1;
1710 /* Handle deferred command line switches. */
1711 static void
1712 handle_deferred_opts ()
1714 size_t i;
1716 for (i = 0; i < deferred_count; i++)
1718 struct deferred_opt *opt = &deferred_opts[i];
1720 if (opt->code == OPT_MT || opt->code == OPT_MQ)
1721 cpp_add_dependency_target (parse_in, opt->arg, opt->code == OPT_MQ);
1725 /* These settings are appropriate for GCC, but not necessarily so for
1726 cpplib as a library. */
1727 static void
1728 sanitize_cpp_opts ()
1730 /* If we don't know what style of dependencies to output, complain
1731 if any other dependency switches have been given. */
1732 if (deps_seen && cpp_opts->deps.style == DEPS_NONE)
1733 error ("to generate dependencies you must specify either -M or -MM");
1735 /* -dM and dependencies suppress normal output; do it here so that
1736 the last -d[MDN] switch overrides earlier ones. */
1737 if (flag_dump_macros == 'M')
1738 flag_no_output = 1;
1740 /* Disable -dD, -dN and -dI if normal output is suppressed. Allow
1741 -dM since at least glibc relies on -M -dM to work. */
1742 if (flag_no_output)
1744 if (flag_dump_macros != 'M')
1745 flag_dump_macros = 0;
1746 flag_dump_includes = 0;
1749 cpp_opts->unsigned_char = !flag_signed_char;
1750 cpp_opts->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
1752 /* We want -Wno-long-long to override -pedantic -std=non-c99
1753 and/or -Wtraditional, whatever the ordering. */
1754 cpp_opts->warn_long_long
1755 = warn_long_long && ((!flag_isoc99 && pedantic) || warn_traditional);
1758 /* Add include path with a prefix at the front of its name. */
1759 static void
1760 add_prefixed_path (suffix, chain)
1761 const char *suffix;
1762 size_t chain;
1764 char *path;
1765 const char *prefix;
1766 size_t prefix_len, suffix_len;
1768 suffix_len = strlen (suffix);
1769 prefix = iprefix ? iprefix : cpp_GCC_INCLUDE_DIR;
1770 prefix_len = iprefix ? strlen (iprefix) : cpp_GCC_INCLUDE_DIR_len;
1772 path = xmalloc (prefix_len + suffix_len + 1);
1773 memcpy (path, prefix, prefix_len);
1774 memcpy (path + prefix_len, suffix, suffix_len);
1775 path[prefix_len + suffix_len] = '\0';
1777 add_path (path, chain, 0);
1780 /* Handle -D, -U, -A, -imacros, and the first -include. */
1781 static void
1782 finish_options ()
1784 if (!cpp_opts->preprocessed)
1786 size_t i;
1788 cpp_change_file (parse_in, LC_RENAME, _("<built-in>"));
1789 cpp_init_builtins (parse_in);
1790 c_cpp_builtins (parse_in);
1791 cpp_change_file (parse_in, LC_RENAME, _("<command line>"));
1792 for (i = 0; i < deferred_count; i++)
1794 struct deferred_opt *opt = &deferred_opts[i];
1796 if (opt->code == OPT_D)
1797 cpp_define (parse_in, opt->arg);
1798 else if (opt->code == OPT_U)
1799 cpp_undef (parse_in, opt->arg);
1800 else if (opt->code == OPT_A)
1802 if (opt->arg[0] == '-')
1803 cpp_unassert (parse_in, opt->arg + 1);
1804 else
1805 cpp_assert (parse_in, opt->arg);
1809 /* Handle -imacros after -D and -U. */
1810 for (i = 0; i < deferred_count; i++)
1812 struct deferred_opt *opt = &deferred_opts[i];
1814 if (opt->code == OPT_imacros
1815 && cpp_push_include (parse_in, opt->arg))
1816 cpp_scan_nooutput (parse_in);
1820 push_command_line_include ();
1823 /* Give CPP the next file given by -include, if any. */
1824 static void
1825 push_command_line_include ()
1827 if (cpp_opts->preprocessed)
1828 return;
1830 while (include_cursor < deferred_count)
1832 struct deferred_opt *opt = &deferred_opts[include_cursor++];
1834 if (opt->code == OPT_include && cpp_push_include (parse_in, opt->arg))
1835 return;
1838 if (include_cursor == deferred_count)
1840 /* Restore the line map from <command line>. */
1841 cpp_change_file (parse_in, LC_RENAME, main_input_filename);
1842 /* -Wunused-macros should only warn about macros defined hereafter. */
1843 cpp_opts->warn_unused_macros = warn_unused_macros;
1844 include_cursor++;
1848 /* File change callback. Has to handle -include files. */
1849 static void
1850 cb_file_change (pfile, new_map)
1851 cpp_reader *pfile ATTRIBUTE_UNUSED;
1852 const struct line_map *new_map;
1854 if (flag_preprocess_only)
1855 pp_file_change (new_map);
1856 else
1857 fe_file_change (new_map);
1859 if (new_map->reason == LC_LEAVE && MAIN_FILE_P (new_map))
1860 push_command_line_include ();
1863 /* Set the C 89 standard (with 1994 amendments if C94, without GNU
1864 extensions if ISO). There is no concept of gnu94. */
1865 static void
1866 set_std_c89 (c94, iso)
1867 int c94, iso;
1869 cpp_set_lang (parse_in, c94 ? CLK_STDC94: iso ? CLK_STDC89: CLK_GNUC89);
1870 flag_iso = iso;
1871 flag_no_asm = iso;
1872 flag_no_gnu_keywords = iso;
1873 flag_no_nonansi_builtin = iso;
1874 flag_noniso_default_format_attributes = !iso;
1875 flag_isoc94 = c94;
1876 flag_isoc99 = 0;
1877 flag_writable_strings = 0;
1880 /* Set the C 99 standard (without GNU extensions if ISO). */
1881 static void
1882 set_std_c99 (iso)
1883 int iso;
1885 cpp_set_lang (parse_in, iso ? CLK_STDC99: CLK_GNUC99);
1886 flag_no_asm = iso;
1887 flag_no_nonansi_builtin = iso;
1888 flag_noniso_default_format_attributes = !iso;
1889 flag_iso = iso;
1890 flag_isoc99 = 1;
1891 flag_isoc94 = 1;
1892 flag_writable_strings = 0;
1895 /* Set the C++ 98 standard (without GNU extensions if ISO). */
1896 static void
1897 set_std_cxx98 (iso)
1898 int iso;
1900 cpp_set_lang (parse_in, iso ? CLK_CXX98: CLK_GNUCXX);
1901 flag_no_gnu_keywords = iso;
1902 flag_no_nonansi_builtin = iso;
1903 flag_noniso_default_format_attributes = !iso;
1904 flag_iso = iso;
1907 /* Handle setting implicit to ON. */
1908 static void
1909 set_Wimplicit (on)
1910 int on;
1912 warn_implicit = on;
1913 warn_implicit_int = on;
1914 if (on)
1916 if (mesg_implicit_function_declaration != 2)
1917 mesg_implicit_function_declaration = 1;
1919 else
1920 mesg_implicit_function_declaration = 0;
1923 /* Args to -d specify what to dump. Silently ignore
1924 unrecognized options; they may be aimed at toplev.c. */
1925 static void
1926 handle_OPT_d (arg)
1927 const char *arg;
1929 char c;
1931 while ((c = *arg++) != '\0')
1932 switch (c)
1934 case 'M': /* Dump macros only. */
1935 case 'N': /* Dump names. */
1936 case 'D': /* Dump definitions. */
1937 flag_dump_macros = c;
1938 break;
1940 case 'I':
1941 flag_dump_includes = 1;
1942 break;
1946 /* Write a slash-separated list of languages in FLAGS to BUF. */
1947 static void
1948 write_langs (buf, flags)
1949 char *buf;
1950 int flags;
1952 *buf = '\0';
1953 if (flags & CL_C_ONLY)
1954 strcat (buf, "C");
1955 if (flags & CL_OBJC_ONLY)
1957 if (*buf)
1958 strcat (buf, "/");
1959 strcat (buf, "ObjC");
1961 if (flags & CL_CXX_ONLY)
1963 if (*buf)
1964 strcat (buf, "/");
1965 strcat (buf, "C++");
1969 /* Complain that switch OPT_INDEX does not apply to this front end. */
1970 static void
1971 complain_wrong_lang (opt_index)
1972 size_t opt_index;
1974 char ok_langs[60], bad_langs[60];
1975 int ok_flags = cl_options[opt_index].flags;
1977 write_langs (ok_langs, ok_flags);
1978 write_langs (bad_langs, ~ok_flags);
1979 warning ("\"-%s\" is valid for %s but not for %s",
1980 cl_options[opt_index].opt_text, ok_langs, bad_langs);
1983 /* Handle --help output. */
1984 static void
1985 print_help ()
1987 /* To keep the lines from getting too long for some compilers, limit
1988 to about 500 characters (6 lines) per chunk. */
1989 fputs (_("\
1990 Switches:\n\
1991 -include <file> Include the contents of <file> before other files\n\
1992 -imacros <file> Accept definition of macros in <file>\n\
1993 -iprefix <path> Specify <path> as a prefix for next two options\n\
1994 -iwithprefix <dir> Add <dir> to the end of the system include path\n\
1995 -iwithprefixbefore <dir> Add <dir> to the end of the main include path\n\
1996 -isystem <dir> Add <dir> to the start of the system include path\n\
1997 "), stdout);
1998 fputs (_("\
1999 -idirafter <dir> Add <dir> to the end of the system include path\n\
2000 -I <dir> Add <dir> to the end of the main include path\n\
2001 -I- Fine-grained include path control; see info docs\n\
2002 -nostdinc Do not search system include directories\n\
2003 (dirs specified with -isystem will still be used)\n\
2004 -nostdinc++ Do not search system include directories for C++\n\
2005 -o <file> Put output into <file>\n\
2006 "), stdout);
2007 fputs (_("\
2008 -trigraphs Support ISO C trigraphs\n\
2009 -std=<std name> Specify the conformance standard; one of:\n\
2010 gnu89, gnu99, c89, c99, iso9899:1990,\n\
2011 iso9899:199409, iso9899:1999, c++98\n\
2012 -w Inhibit warning messages\n\
2013 -W[no-]trigraphs Warn if trigraphs are encountered\n\
2014 -W[no-]comment{s} Warn if one comment starts inside another\n\
2015 "), stdout);
2016 fputs (_("\
2017 -W[no-]traditional Warn about features not present in traditional C\n\
2018 -W[no-]undef Warn if an undefined macro is used by #if\n\
2019 -W[no-]import Warn about the use of the #import directive\n\
2020 "), stdout);
2021 fputs (_("\
2022 -W[no-]error Treat all warnings as errors\n\
2023 -W[no-]system-headers Do not suppress warnings from system headers\n\
2024 -W[no-]all Enable most preprocessor warnings\n\
2025 "), stdout);
2026 fputs (_("\
2027 -M Generate make dependencies\n\
2028 -MM As -M, but ignore system header files\n\
2029 -MD Generate make dependencies and compile\n\
2030 -MMD As -MD, but ignore system header files\n\
2031 -MF <file> Write dependency output to the given file\n\
2032 -MG Treat missing header file as generated files\n\
2033 "), stdout);
2034 fputs (_("\
2035 -MP Generate phony targets for all headers\n\
2036 -MQ <target> Add a MAKE-quoted target\n\
2037 -MT <target> Add an unquoted target\n\
2038 "), stdout);
2039 fputs (_("\
2040 -D<macro> Define a <macro> with string '1' as its value\n\
2041 -D<macro>=<val> Define a <macro> with <val> as its value\n\
2042 -A<question>=<answer> Assert the <answer> to <question>\n\
2043 -A-<question>=<answer> Disable the <answer> to <question>\n\
2044 -U<macro> Undefine <macro> \n\
2045 -v Display the version number\n\
2046 "), stdout);
2047 fputs (_("\
2048 -H Print the name of header files as they are used\n\
2049 -C Do not discard comments\n\
2050 -dM Display a list of macro definitions active at end\n\
2051 -dD Preserve macro definitions in output\n\
2052 -dN As -dD except that only the names are preserved\n\
2053 -dI Include #include directives in the output\n\
2054 "), stdout);
2055 fputs (_("\
2056 -f[no-]preprocessed Treat the input file as already preprocessed\n\
2057 -ftabstop=<number> Distance between tab stops for column reporting\n\
2058 -isysroot <dir> Set <dir> to be the system root directory\n\
2059 -P Do not generate #line directives\n\
2060 -remap Remap file names when including files\n\
2061 --help Display this information\n\
2062 "), stdout);