1 /* Command line option handling. Code involving global state that
2 should not be shared with the driver.
3 Copyright (C) 2002-2015 Free Software Foundation, Inc.
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 3, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
24 #include "diagnostic.h"
33 #include "tree.h" /* Required by langhooks.h. */
34 #include "fold-const.h"
37 #include "hard-reg-set.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
46 #include "langhooks.h"
51 #include "plugin-api.h"
54 #include "lto-streamer.h"
58 #include "tree-pass.h"
62 typedef const char *const_char_p
; /* For DEF_VEC_P. */
64 static vec
<const_char_p
> ignored_options
;
66 /* Input file names. */
67 const char **in_fnames
;
68 unsigned num_in_fnames
;
70 /* Return a malloced slash-separated list of languages in MASK. */
73 write_langs (unsigned int mask
)
75 unsigned int n
= 0, len
= 0;
76 const char *lang_name
;
79 for (n
= 0; (lang_name
= lang_names
[n
]) != 0; n
++)
81 len
+= strlen (lang_name
) + 1;
83 result
= XNEWVEC (char, len
);
85 for (n
= 0; (lang_name
= lang_names
[n
]) != 0; n
++)
90 strcpy (result
+ len
, lang_name
);
91 len
+= strlen (lang_name
);
99 /* Complain that switch DECODED does not apply to this front end (mask
103 complain_wrong_lang (const struct cl_decoded_option
*decoded
,
104 unsigned int lang_mask
)
106 const struct cl_option
*option
= &cl_options
[decoded
->opt_index
];
107 const char *text
= decoded
->orig_option_with_args_text
;
108 char *ok_langs
= NULL
, *bad_lang
= NULL
;
109 unsigned int opt_flags
= option
->flags
;
111 if (!lang_hooks
.complain_wrong_lang_p (option
))
114 opt_flags
&= ((1U << cl_lang_count
) - 1) | CL_DRIVER
;
115 if (opt_flags
!= CL_DRIVER
)
116 ok_langs
= write_langs (opt_flags
);
117 if (lang_mask
!= CL_DRIVER
)
118 bad_lang
= write_langs (lang_mask
);
120 if (opt_flags
== CL_DRIVER
)
121 error ("command line option %qs is valid for the driver but not for %s",
123 else if (lang_mask
== CL_DRIVER
)
126 /* Eventually this should become a hard error IMO. */
127 warning (0, "command line option %qs is valid for %s but not for %s",
128 text
, ok_langs
, bad_lang
);
134 /* Buffer the unknown option described by the string OPT. Currently,
135 we only complain about unknown -Wno-* options if they may have
136 prevented a diagnostic. Otherwise, we just ignore them. Note that
137 if we do complain, it is only as a warning, not an error; passing
138 the compiler an unrecognized -Wno-* option should never change
139 whether the compilation succeeds or fails. */
142 postpone_unknown_option_warning (const char *opt
)
144 ignored_options
.safe_push (opt
);
147 /* Produce a warning for each option previously buffered. */
150 print_ignored_options (void)
152 while (!ignored_options
.is_empty ())
156 opt
= ignored_options
.pop ();
157 warning_at (UNKNOWN_LOCATION
, 0,
158 "unrecognized command line option %qs", opt
);
162 /* Handle an unknown option DECODED, returning true if an error should
166 unknown_option_callback (const struct cl_decoded_option
*decoded
)
168 const char *opt
= decoded
->arg
;
170 if (opt
[1] == 'W' && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-'
171 && !(decoded
->errors
& CL_ERR_NEGATIVE
))
173 /* We don't generate warnings for unknown -Wno-* options unless
174 we issue diagnostics. */
175 postpone_unknown_option_warning (opt
);
182 /* Handle a front-end option; arguments and return value as for
186 lang_handle_option (struct gcc_options
*opts
,
187 struct gcc_options
*opts_set
,
188 const struct cl_decoded_option
*decoded
,
189 unsigned int lang_mask ATTRIBUTE_UNUSED
, int kind
,
191 const struct cl_option_handlers
*handlers
,
192 diagnostic_context
*dc
)
194 gcc_assert (opts
== &global_options
);
195 gcc_assert (opts_set
== &global_options_set
);
196 gcc_assert (dc
== global_dc
);
197 gcc_assert (decoded
->canonical_option_num_elements
<= 2);
198 return lang_hooks
.handle_option (decoded
->opt_index
, decoded
->arg
,
199 decoded
->value
, kind
, loc
, handlers
);
202 /* Handle FILENAME from the command line. */
205 add_input_filename (const char *filename
)
208 in_fnames
= XRESIZEVEC (const char *, in_fnames
, num_in_fnames
);
209 in_fnames
[num_in_fnames
- 1] = filename
;
212 /* Handle the vector of command line options (located at LOC), storing
213 the results of processing DECODED_OPTIONS and DECODED_OPTIONS_COUNT
214 in OPTS and OPTS_SET and using DC for diagnostic state. LANG_MASK
215 contains has a single bit set representing the current language.
216 HANDLERS describes what functions to call for the options. */
219 read_cmdline_options (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
220 struct cl_decoded_option
*decoded_options
,
221 unsigned int decoded_options_count
,
223 unsigned int lang_mask
,
224 const struct cl_option_handlers
*handlers
,
225 diagnostic_context
*dc
)
229 for (i
= 1; i
< decoded_options_count
; i
++)
231 if (decoded_options
[i
].opt_index
== OPT_SPECIAL_input_file
)
233 /* Input files should only ever appear on the main command
235 gcc_assert (opts
== &global_options
);
236 gcc_assert (opts_set
== &global_options_set
);
238 if (opts
->x_main_input_filename
== NULL
)
240 opts
->x_main_input_filename
= decoded_options
[i
].arg
;
241 opts
->x_main_input_baselength
242 = base_of_path (opts
->x_main_input_filename
,
243 &opts
->x_main_input_basename
);
245 add_input_filename (decoded_options
[i
].arg
);
249 read_cmdline_option (opts
, opts_set
,
250 decoded_options
+ i
, loc
, lang_mask
, handlers
,
255 /* Language mask determined at initialization. */
256 static unsigned int initial_lang_mask
;
258 /* Initialize global options-related settings at start-up. */
261 init_options_once (void)
263 /* Perform language-specific options initialization. */
264 initial_lang_mask
= lang_hooks
.option_lang_mask ();
266 lang_hooks
.initialize_diagnostics (global_dc
);
267 /* ??? Ideally, we should do this earlier and the FEs will override
268 it if desired (none do it so far). However, the way the FEs
269 construct their pretty-printers means that all previous settings
271 diagnostic_color_init (global_dc
);
274 /* Decode command-line options to an array, like
275 decode_cmdline_options_to_array and with the same arguments but
276 using the default lang_mask. */
279 decode_cmdline_options_to_array_default_mask (unsigned int argc
,
281 struct cl_decoded_option
**decoded_options
,
282 unsigned int *decoded_options_count
)
284 decode_cmdline_options_to_array (argc
, argv
,
285 initial_lang_mask
| CL_COMMON
| CL_TARGET
,
286 decoded_options
, decoded_options_count
);
289 /* Set *HANDLERS to the default set of option handlers for use in the
290 compilers proper (not the driver). */
292 set_default_handlers (struct cl_option_handlers
*handlers
)
294 handlers
->unknown_option_callback
= unknown_option_callback
;
295 handlers
->wrong_lang_callback
= complain_wrong_lang
;
296 handlers
->num_handlers
= 3;
297 handlers
->handlers
[0].handler
= lang_handle_option
;
298 handlers
->handlers
[0].mask
= initial_lang_mask
;
299 handlers
->handlers
[1].handler
= common_handle_option
;
300 handlers
->handlers
[1].mask
= CL_COMMON
;
301 handlers
->handlers
[2].handler
= target_handle_option
;
302 handlers
->handlers
[2].mask
= CL_TARGET
;
305 /* Parse command line options and set default flag values. Do minimal
306 options processing. The decoded options are in *DECODED_OPTIONS
307 and *DECODED_OPTIONS_COUNT; settings go in OPTS, OPTS_SET and DC;
308 the options are located at LOC. */
310 decode_options (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
311 struct cl_decoded_option
*decoded_options
,
312 unsigned int decoded_options_count
,
313 location_t loc
, diagnostic_context
*dc
)
315 struct cl_option_handlers handlers
;
317 unsigned int lang_mask
;
319 lang_mask
= initial_lang_mask
;
321 set_default_handlers (&handlers
);
323 default_options_optimization (opts
, opts_set
,
324 decoded_options
, decoded_options_count
,
325 loc
, lang_mask
, &handlers
, dc
);
327 read_cmdline_options (opts
, opts_set
,
328 decoded_options
, decoded_options_count
,
332 finish_options (opts
, opts_set
, loc
);
335 /* Process common options that have been deferred until after the
336 handlers have been called for all options. */
339 handle_common_deferred_options (void)
342 cl_deferred_option
*opt
;
343 vec
<cl_deferred_option
> v
;
345 if (common_deferred_options
)
346 v
= *((vec
<cl_deferred_option
> *) common_deferred_options
);
350 if (flag_dump_all_passed
)
351 enable_rtl_dump_file ();
354 opt_info_switch_p (NULL
);
356 FOR_EACH_VEC_ELT (v
, i
, opt
)
358 switch (opt
->opt_index
)
360 case OPT_fcall_used_
:
361 fix_register (opt
->arg
, 0, 1);
364 case OPT_fcall_saved_
:
365 fix_register (opt
->arg
, 0, 0);
369 dbg_cnt_process_opt (opt
->arg
);
372 case OPT_fdbg_cnt_list
:
373 dbg_cnt_list_all_counters ();
376 case OPT_fdebug_prefix_map_
:
377 add_debug_prefix_map (opt
->arg
);
381 if (!g
->get_dumps ()->dump_switch_p (opt
->arg
))
382 error ("unrecognized command line option %<-fdump-%s%>", opt
->arg
);
386 if (!opt_info_switch_p (opt
->arg
))
387 error ("unrecognized command line option %<-fopt-info-%s%>",
393 if (opt
->opt_index
== OPT_fenable_
)
394 enable_pass (opt
->arg
);
396 disable_pass (opt
->arg
);
401 fix_register (opt
->arg
, 1, 1);
406 add_new_plugin (opt
->arg
);
408 error ("plugin support is disabled; configure with --enable-plugin");
412 case OPT_fplugin_arg_
:
414 parse_plugin_arg_opt (opt
->arg
);
416 error ("plugin support is disabled; configure with --enable-plugin");
420 case OPT_frandom_seed
:
421 /* The real switch is -fno-random-seed. */
423 set_random_seed (NULL
);
426 case OPT_frandom_seed_
:
427 set_random_seed (opt
->arg
);
430 case OPT_fstack_limit
:
431 /* The real switch is -fno-stack-limit. */
433 stack_limit_rtx
= NULL_RTX
;
436 case OPT_fstack_limit_register_
:
438 int reg
= decode_reg_name (opt
->arg
);
440 error ("unrecognized register name %qs", opt
->arg
);
442 stack_limit_rtx
= gen_rtx_REG (Pmode
, reg
);
446 case OPT_fstack_limit_symbol_
:
447 stack_limit_rtx
= gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (opt
->arg
));
450 case OPT_fasan_shadow_offset_
:
451 if (!(flag_sanitize
& SANITIZE_KERNEL_ADDRESS
))
452 error ("-fasan-shadow-offset should only be used "
453 "with -fsanitize=kernel-address");
454 if (!set_asan_shadow_offset (opt
->arg
))
455 error ("unrecognized shadow offset %qs", opt
->arg
);
458 case OPT_fsanitize_sections_
:
459 set_sanitized_sections (opt
->arg
);