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