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