1 /* Command line option handling. Code involving global state that
2 should not be shared with the driver.
3 Copyright (C) 2002-2014 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"
27 #include "tree.h" /* Required by langhooks.h. */
28 #include "basic-block.h"
29 #include "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "gimple-expr.h"
34 #include "langhooks.h"
35 #include "tm.h" /* Required by rtl.h. */
39 #include "lto-streamer.h"
43 #include "tree-pass.h"
49 #include "stringpool.h"
51 typedef const char *const_char_p
; /* For DEF_VEC_P. */
53 static vec
<const_char_p
> ignored_options
;
55 /* Input file names. */
56 const char **in_fnames
;
57 unsigned num_in_fnames
;
59 static struct reg_func_attr_patterns
62 const char *attribute
;
63 struct reg_func_attr_patterns
*next
;
64 } *reg_func_attr_patterns
;
66 /* Return a malloced slash-separated list of languages in MASK. */
69 write_langs (unsigned int mask
)
71 unsigned int n
= 0, len
= 0;
72 const char *lang_name
;
75 for (n
= 0; (lang_name
= lang_names
[n
]) != 0; n
++)
77 len
+= strlen (lang_name
) + 1;
79 result
= XNEWVEC (char, len
);
81 for (n
= 0; (lang_name
= lang_names
[n
]) != 0; n
++)
86 strcpy (result
+ len
, lang_name
);
87 len
+= strlen (lang_name
);
95 /* Add strings like attribute_str:pattern... to attribute pattern list. */
98 add_attribute_pattern (const char *arg
)
102 struct reg_func_attr_patterns
*one_pat
;
105 /* We never free this string. */
108 pattern_str
= strchr (tmp
, ':');
110 error ("invalid pattern in -ffunction-attribute-list option: %qs", tmp
);
115 one_pat
= XCNEW (struct reg_func_attr_patterns
);
116 one_pat
->next
= reg_func_attr_patterns
;
117 one_pat
->attribute
= tmp
;
118 reg_func_attr_patterns
= one_pat
;
119 if ((ec
= regcomp (&one_pat
->r
, pattern_str
, REG_EXTENDED
|REG_NOSUB
) != 0))
122 regerror (ec
, &one_pat
->r
, err
, 99);
123 error ("invalid pattern in -ffunction-attribute-list option: %qs: %qs",
128 /* Match FNDECL's name with user specified patterns, and add attributes
132 pattern_match_function_attributes (tree fndecl
)
135 struct reg_func_attr_patterns
*one_pat
;
140 if (!reg_func_attr_patterns
)
143 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl
));
145 for (one_pat
= reg_func_attr_patterns
; one_pat
; one_pat
= one_pat
->next
)
146 if (regexec (&one_pat
->r
, name
, 0, NULL
, 0) == 0)
147 decl_attributes (&fndecl
, tree_cons (
148 get_identifier (one_pat
->attribute
), NULL
, NULL
), 0);
151 /* Complain that switch DECODED does not apply to this front end (mask
155 complain_wrong_lang (const struct cl_decoded_option
*decoded
,
156 unsigned int lang_mask
)
158 const struct cl_option
*option
= &cl_options
[decoded
->opt_index
];
159 const char *text
= decoded
->orig_option_with_args_text
;
160 char *ok_langs
= NULL
, *bad_lang
= NULL
;
161 unsigned int opt_flags
= option
->flags
;
163 if (!lang_hooks
.complain_wrong_lang_p (option
))
166 opt_flags
&= ((1U << cl_lang_count
) - 1) | CL_DRIVER
;
167 if (opt_flags
!= CL_DRIVER
)
168 ok_langs
= write_langs (opt_flags
);
169 if (lang_mask
!= CL_DRIVER
)
170 bad_lang
= write_langs (lang_mask
);
172 if (opt_flags
== CL_DRIVER
)
173 error ("command line option %qs is valid for the driver but not for %s",
175 else if (lang_mask
== CL_DRIVER
)
178 /* Eventually this should become a hard error IMO. */
179 warning (0, "command line option %qs is valid for %s but not for %s",
180 text
, ok_langs
, bad_lang
);
186 /* Buffer the unknown option described by the string OPT. Currently,
187 we only complain about unknown -Wno-* options if they may have
188 prevented a diagnostic. Otherwise, we just ignore them. Note that
189 if we do complain, it is only as a warning, not an error; passing
190 the compiler an unrecognised -Wno-* option should never change
191 whether the compilation succeeds or fails. */
194 postpone_unknown_option_warning (const char *opt
)
196 ignored_options
.safe_push (opt
);
199 /* Produce a warning for each option previously buffered. */
202 print_ignored_options (void)
204 while (!ignored_options
.is_empty ())
208 opt
= ignored_options
.pop ();
209 warning_at (UNKNOWN_LOCATION
, 0,
210 "unrecognized command line option \"%s\"", opt
);
214 /* Handle an unknown option DECODED, returning true if an error should
218 unknown_option_callback (const struct cl_decoded_option
*decoded
)
220 const char *opt
= decoded
->arg
;
222 if (opt
[1] == 'W' && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-'
223 && !(decoded
->errors
& CL_ERR_NEGATIVE
))
225 /* We don't generate warnings for unknown -Wno-* options unless
226 we issue diagnostics. */
227 postpone_unknown_option_warning (opt
);
234 /* Handle a front-end option; arguments and return value as for
238 lang_handle_option (struct gcc_options
*opts
,
239 struct gcc_options
*opts_set
,
240 const struct cl_decoded_option
*decoded
,
241 unsigned int lang_mask ATTRIBUTE_UNUSED
, int kind
,
243 const struct cl_option_handlers
*handlers
,
244 diagnostic_context
*dc
)
246 gcc_assert (opts
== &global_options
);
247 gcc_assert (opts_set
== &global_options_set
);
248 gcc_assert (dc
== global_dc
);
249 gcc_assert (decoded
->canonical_option_num_elements
<= 2);
250 return lang_hooks
.handle_option (decoded
->opt_index
, decoded
->arg
,
251 decoded
->value
, kind
, loc
, handlers
);
254 /* Handle FILENAME from the command line. */
257 add_input_filename (const char *filename
)
260 in_fnames
= XRESIZEVEC (const char *, in_fnames
, num_in_fnames
);
261 in_fnames
[num_in_fnames
- 1] = filename
;
264 /* GCC command-line options saved to the LIPO profile data file.
265 See detailed comment in opts.h. */
266 const char **lipo_cl_args
;
267 unsigned num_lipo_cl_args
;
269 /* Inspect the given GCC command-line arguments, which are part of one GCC
270 switch, and decide whether or not to store these to the LIPO profile data
273 lipo_save_cl_args (struct cl_decoded_option
*decoded
)
275 const char *opt
= decoded
->orig_option_with_args_text
;
276 /* Store the following command-line flags to the lipo profile data file:
277 (1) -f... (except -frandom-seed...)
282 (6) -std=... (-std=c99 for restrict keyword)
285 && (opt
[1] == 'f' || opt
[1] == 'm' || opt
[1] == 'W' || opt
[1] == 'O'
286 || (strstr (opt
, "--param") == opt
)
287 || (strstr (opt
, "-std=")))
288 && !strstr(opt
, "-frandom-seed")
289 && !strstr(opt
, "-fripa-disallow-opt-mismatch")
290 && !strstr(opt
, "-Wripa-opt-mismatch"))
293 lipo_cl_args
= XRESIZEVEC (const char *, lipo_cl_args
, num_lipo_cl_args
);
294 lipo_cl_args
[num_lipo_cl_args
- 1] = opt
;
298 /* Handle the vector of command line options (located at LOC), storing
299 the results of processing DECODED_OPTIONS and DECODED_OPTIONS_COUNT
300 in OPTS and OPTS_SET and using DC for diagnostic state. LANG_MASK
301 contains has a single bit set representing the current language.
302 HANDLERS describes what functions to call for the options. */
305 read_cmdline_options (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
306 struct cl_decoded_option
*decoded_options
,
307 unsigned int decoded_options_count
,
309 unsigned int lang_mask
,
310 const struct cl_option_handlers
*handlers
,
311 diagnostic_context
*dc
)
314 int force_multi_module
= 0;
315 static int cur_mod_id
= 0;
317 force_multi_module
= PARAM_VALUE (PARAM_FORCE_LIPO_MODE
);
319 for (i
= 1; i
< decoded_options_count
; i
++)
321 if (decoded_options
[i
].opt_index
== OPT_SPECIAL_input_file
)
323 /* Input files should only ever appear on the main command
325 gcc_assert (opts
== &global_options
);
326 gcc_assert (opts_set
== &global_options_set
);
328 if (opts
->x_main_input_filename
== NULL
)
330 opts
->x_main_input_filename
= decoded_options
[i
].arg
;
331 opts
->x_main_input_baselength
332 = base_of_path (opts
->x_main_input_filename
,
333 &opts
->x_main_input_basename
);
335 add_input_filename (decoded_options
[i
].arg
);
336 if (force_multi_module
)
337 add_module_info (++cur_mod_id
, (num_in_fnames
== 1), num_in_fnames
- 1);
341 read_cmdline_option (opts
, opts_set
,
342 decoded_options
+ i
, loc
, lang_mask
, handlers
,
344 lipo_save_cl_args (decoded_options
+ i
);
348 /* Language mask determined at initialization. */
349 static unsigned int initial_lang_mask
;
351 /* Initialize global options-related settings at start-up. */
354 init_options_once (void)
356 /* Perform language-specific options initialization. */
357 initial_lang_mask
= lang_hooks
.option_lang_mask ();
359 lang_hooks
.initialize_diagnostics (global_dc
);
362 /* Decode command-line options to an array, like
363 decode_cmdline_options_to_array and with the same arguments but
364 using the default lang_mask. */
367 decode_cmdline_options_to_array_default_mask (unsigned int argc
,
369 struct cl_decoded_option
**decoded_options
,
370 unsigned int *decoded_options_count
)
372 decode_cmdline_options_to_array (argc
, argv
,
373 initial_lang_mask
| CL_COMMON
| CL_TARGET
,
374 decoded_options
, decoded_options_count
);
377 /* Set *HANDLERS to the default set of option handlers for use in the
378 compilers proper (not the driver). */
380 set_default_handlers (struct cl_option_handlers
*handlers
)
382 handlers
->unknown_option_callback
= unknown_option_callback
;
383 handlers
->wrong_lang_callback
= complain_wrong_lang
;
384 handlers
->num_handlers
= 3;
385 handlers
->handlers
[0].handler
= lang_handle_option
;
386 handlers
->handlers
[0].mask
= initial_lang_mask
;
387 handlers
->handlers
[1].handler
= common_handle_option
;
388 handlers
->handlers
[1].mask
= CL_COMMON
;
389 handlers
->handlers
[2].handler
= target_handle_option
;
390 handlers
->handlers
[2].mask
= CL_TARGET
;
393 /* Parse command line options and set default flag values. Do minimal
394 options processing. The decoded options are in *DECODED_OPTIONS
395 and *DECODED_OPTIONS_COUNT; settings go in OPTS, OPTS_SET and DC;
396 the options are located at LOC. */
398 decode_options (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
399 struct cl_decoded_option
*decoded_options
,
400 unsigned int decoded_options_count
,
401 location_t loc
, diagnostic_context
*dc
)
403 struct cl_option_handlers handlers
;
405 unsigned int lang_mask
;
407 lang_mask
= initial_lang_mask
;
409 set_default_handlers (&handlers
);
411 default_options_optimization (opts
, opts_set
,
412 decoded_options
, decoded_options_count
,
413 loc
, lang_mask
, &handlers
, dc
);
415 read_cmdline_options (opts
, opts_set
,
416 decoded_options
, decoded_options_count
,
420 finish_options (opts
, opts_set
, loc
);
423 /* Process common options that have been deferred until after the
424 handlers have been called for all options. */
427 handle_common_deferred_options (void)
430 cl_deferred_option
*opt
;
431 vec
<cl_deferred_option
> v
;
433 if (common_deferred_options
)
434 v
= *((vec
<cl_deferred_option
> *) common_deferred_options
);
438 if (flag_dump_all_passed
)
439 enable_rtl_dump_file ();
442 opt_info_switch_p (NULL
);
444 FOR_EACH_VEC_ELT (v
, i
, opt
)
446 switch (opt
->opt_index
)
448 case OPT_fcall_used_
:
449 fix_register (opt
->arg
, 0, 1);
452 case OPT_fcall_saved_
:
453 fix_register (opt
->arg
, 0, 0);
457 dbg_cnt_process_opt (opt
->arg
);
460 case OPT_fdbg_cnt_list
:
461 dbg_cnt_list_all_counters ();
464 case OPT_fdebug_prefix_map_
:
465 add_debug_prefix_map (opt
->arg
);
469 if (!g
->get_dumps ()->dump_switch_p (opt
->arg
))
470 error ("unrecognized command line option %<-fdump-%s%>", opt
->arg
);
474 if (!opt_info_switch_p (opt
->arg
))
475 error ("unrecognized command line option %<-fopt-info-%s%>",
481 if (opt
->opt_index
== OPT_fenable_
)
482 enable_pass (opt
->arg
);
484 disable_pass (opt
->arg
);
489 fix_register (opt
->arg
, 1, 1);
494 add_new_plugin (opt
->arg
);
496 error ("plugin support is disabled; configure with --enable-plugin");
500 case OPT_fplugin_arg_
:
502 parse_plugin_arg_opt (opt
->arg
);
504 error ("plugin support is disabled; configure with --enable-plugin");
508 case OPT_frandom_seed
:
509 /* The real switch is -fno-random-seed. */
511 set_random_seed (NULL
);
514 case OPT_frandom_seed_
:
515 set_random_seed (opt
->arg
);
518 case OPT_ffunction_attribute_list_
:
519 add_attribute_pattern (opt
->arg
);
522 case OPT_fstack_limit
:
523 /* The real switch is -fno-stack-limit. */
525 stack_limit_rtx
= NULL_RTX
;
528 case OPT_fstack_limit_register_
:
530 int reg
= decode_reg_name (opt
->arg
);
532 error ("unrecognized register name %qs", opt
->arg
);
534 stack_limit_rtx
= gen_rtx_REG (Pmode
, reg
);
538 case OPT_fstack_limit_symbol_
:
539 stack_limit_rtx
= gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (opt
->arg
));