1 /* Command line option handling. Code involving global state that
2 should not be shared with the driver.
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
25 #include "diagnostic.h"
29 #include "tree.h" /* Required by langhooks.h. */
30 #include "langhooks.h"
31 #include "tm.h" /* Required by rtl.h. */
35 #include "lto-streamer.h"
39 #include "tree-pass.h"
44 typedef const char *const_char_p
; /* For DEF_VEC_P. */
45 DEF_VEC_P(const_char_p
);
46 DEF_VEC_ALLOC_P(const_char_p
,heap
);
48 static VEC(const_char_p
,heap
) *ignored_options
;
50 /* Input file names. */
51 const char **in_fnames
;
52 unsigned num_in_fnames
;
54 static struct reg_func_attr_patterns
57 const char *attribute
;
58 struct reg_func_attr_patterns
*next
;
59 } *reg_func_attr_patterns
;
61 /* Return a malloced slash-separated list of languages in MASK. */
64 write_langs (unsigned int mask
)
66 unsigned int n
= 0, len
= 0;
67 const char *lang_name
;
70 for (n
= 0; (lang_name
= lang_names
[n
]) != 0; n
++)
72 len
+= strlen (lang_name
) + 1;
74 result
= XNEWVEC (char, len
);
76 for (n
= 0; (lang_name
= lang_names
[n
]) != 0; n
++)
81 strcpy (result
+ len
, lang_name
);
82 len
+= strlen (lang_name
);
90 /* Add strings like attribute_str:pattern... to attribute pattern list. */
93 add_attribute_pattern (const char *arg
)
97 struct reg_func_attr_patterns
*one_pat
;
100 /* We never free this string. */
103 pattern_str
= strchr (tmp
, ':');
105 error ("invalid pattern in -ffunction-attribute-list option: %qs", tmp
);
110 one_pat
= XCNEW (struct reg_func_attr_patterns
);
111 one_pat
->next
= reg_func_attr_patterns
;
112 one_pat
->attribute
= tmp
;
113 reg_func_attr_patterns
= one_pat
;
114 if ((ec
= regcomp (&one_pat
->r
, pattern_str
, REG_EXTENDED
|REG_NOSUB
) != 0))
117 regerror (ec
, &one_pat
->r
, err
, 99);
118 error ("invalid pattern in -ffunction-attribute-list option: %qs: %qs",
123 /* Match FNDECL's name with user specified patterns, and add attributes
127 pattern_match_function_attributes (tree fndecl
)
130 struct reg_func_attr_patterns
*one_pat
;
135 if (!reg_func_attr_patterns
)
138 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (fndecl
));
140 for (one_pat
= reg_func_attr_patterns
; one_pat
; one_pat
= one_pat
->next
)
141 if (regexec (&one_pat
->r
, name
, 0, NULL
, 0) == 0)
142 decl_attributes (&fndecl
, tree_cons (
143 get_identifier (one_pat
->attribute
), NULL
, NULL
), 0);
146 /* Complain that switch DECODED does not apply to this front end (mask
150 complain_wrong_lang (const struct cl_decoded_option
*decoded
,
151 unsigned int lang_mask
)
153 const struct cl_option
*option
= &cl_options
[decoded
->opt_index
];
154 const char *text
= decoded
->orig_option_with_args_text
;
155 char *ok_langs
= NULL
, *bad_lang
= NULL
;
156 unsigned int opt_flags
= option
->flags
;
158 if (!lang_hooks
.complain_wrong_lang_p (option
))
161 opt_flags
&= ((1U << cl_lang_count
) - 1) | CL_DRIVER
;
162 if (opt_flags
!= CL_DRIVER
)
163 ok_langs
= write_langs (opt_flags
);
164 if (lang_mask
!= CL_DRIVER
)
165 bad_lang
= write_langs (lang_mask
);
167 if (opt_flags
== CL_DRIVER
)
168 error ("command line option %qs is valid for the driver but not for %s",
170 else if (lang_mask
== CL_DRIVER
)
173 /* Eventually this should become a hard error IMO. */
174 warning (0, "command line option %qs is valid for %s but not for %s",
175 text
, ok_langs
, bad_lang
);
181 /* Buffer the unknown option described by the string OPT. Currently,
182 we only complain about unknown -Wno-* options if they may have
183 prevented a diagnostic. Otherwise, we just ignore them. Note that
184 if we do complain, it is only as a warning, not an error; passing
185 the compiler an unrecognised -Wno-* option should never change
186 whether the compilation succeeds or fails. */
189 postpone_unknown_option_warning (const char *opt
)
191 VEC_safe_push (const_char_p
, heap
, ignored_options
, opt
);
194 /* Produce a warning for each option previously buffered. */
197 print_ignored_options (void)
199 while (!VEC_empty (const_char_p
, ignored_options
))
203 opt
= VEC_pop (const_char_p
, ignored_options
);
204 warning_at (UNKNOWN_LOCATION
, 0,
205 "unrecognized command line option \"%s\"", opt
);
209 /* Handle an unknown option DECODED, returning true if an error should
213 unknown_option_callback (const struct cl_decoded_option
*decoded
)
215 const char *opt
= decoded
->arg
;
217 if (opt
[1] == 'W' && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-'
218 && !(decoded
->errors
& CL_ERR_NEGATIVE
))
220 /* We don't generate warnings for unknown -Wno-* options unless
221 we issue diagnostics. */
222 postpone_unknown_option_warning (opt
);
229 /* Handle a front-end option; arguments and return value as for
233 lang_handle_option (struct gcc_options
*opts
,
234 struct gcc_options
*opts_set
,
235 const struct cl_decoded_option
*decoded
,
236 unsigned int lang_mask ATTRIBUTE_UNUSED
, int kind
,
238 const struct cl_option_handlers
*handlers
,
239 diagnostic_context
*dc
)
241 gcc_assert (opts
== &global_options
);
242 gcc_assert (opts_set
== &global_options_set
);
243 gcc_assert (dc
== global_dc
);
244 gcc_assert (decoded
->canonical_option_num_elements
<= 2);
245 return lang_hooks
.handle_option (decoded
->opt_index
, decoded
->arg
,
246 decoded
->value
, kind
, loc
, handlers
);
249 /* Handle FILENAME from the command line. */
252 add_input_filename (const char *filename
)
255 in_fnames
= XRESIZEVEC (const char *, in_fnames
, num_in_fnames
);
256 in_fnames
[num_in_fnames
- 1] = filename
;
259 /* GCC command-line options saved to the LIPO profile data file.
260 See detailed comment in opts.h. */
261 const char **lipo_cl_args
;
262 unsigned num_lipo_cl_args
;
264 /* Inspect the given GCC command-line arguments, which are part of one GCC
265 switch, and decide whether or not to store these to the LIPO profile data
268 lipo_save_cl_args (struct cl_decoded_option
*decoded
)
270 const char *opt
= decoded
->orig_option_with_args_text
;
271 /* Store the following command-line flags to the lipo profile data file:
272 (1) -f... (except -frandom-seed...)
279 && (opt
[1] == 'f' || opt
[1] == 'm' || opt
[1] == 'W' || opt
[1] == 'O'
280 || (strstr (opt
, "--param") == opt
) || (strstr (opt
, "-std=")))
281 && !strstr(opt
, "-frandom-seed")
282 && !strstr(opt
, "-fripa-disallow-opt-mismatch")
283 && !strstr(opt
, "-Wripa-opt-mismatch"))
286 lipo_cl_args
= XRESIZEVEC (const char *, lipo_cl_args
, num_lipo_cl_args
);
287 lipo_cl_args
[num_lipo_cl_args
- 1] = opt
;
291 /* Handle the vector of command line options (located at LOC), storing
292 the results of processing DECODED_OPTIONS and DECODED_OPTIONS_COUNT
293 in OPTS and OPTS_SET and using DC for diagnostic state. LANG_MASK
294 contains has a single bit set representing the current language.
295 HANDLERS describes what functions to call for the options. */
298 read_cmdline_options (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
299 struct cl_decoded_option
*decoded_options
,
300 unsigned int decoded_options_count
,
302 unsigned int lang_mask
,
303 const struct cl_option_handlers
*handlers
,
304 diagnostic_context
*dc
)
307 int force_multi_module
= 0;
308 static int cur_mod_id
= 0;
310 force_multi_module
= PARAM_VALUE (PARAM_FORCE_LIPO_MODE
);
312 for (i
= 1; i
< decoded_options_count
; i
++)
314 if (decoded_options
[i
].opt_index
== OPT_SPECIAL_input_file
)
316 /* Input files should only ever appear on the main command
318 gcc_assert (opts
== &global_options
);
319 gcc_assert (opts_set
== &global_options_set
);
321 if (opts
->x_main_input_filename
== NULL
)
323 opts
->x_main_input_filename
= decoded_options
[i
].arg
;
324 opts
->x_main_input_baselength
325 = base_of_path (opts
->x_main_input_filename
,
326 &opts
->x_main_input_basename
);
328 add_input_filename (decoded_options
[i
].arg
);
329 if (force_multi_module
)
330 add_module_info (++cur_mod_id
, (num_in_fnames
== 1), num_in_fnames
- 1);
334 read_cmdline_option (opts
, opts_set
,
335 decoded_options
+ i
, loc
, lang_mask
, handlers
,
337 lipo_save_cl_args (decoded_options
+ i
);
341 /* Language mask determined at initialization. */
342 static unsigned int initial_lang_mask
;
344 /* Initialize global options-related settings at start-up. */
347 init_options_once (void)
349 /* Perform language-specific options initialization. */
350 initial_lang_mask
= lang_hooks
.option_lang_mask ();
352 lang_hooks
.initialize_diagnostics (global_dc
);
355 /* Decode command-line options to an array, like
356 decode_cmdline_options_to_array and with the same arguments but
357 using the default lang_mask. */
360 decode_cmdline_options_to_array_default_mask (unsigned int argc
,
362 struct cl_decoded_option
**decoded_options
,
363 unsigned int *decoded_options_count
)
365 decode_cmdline_options_to_array (argc
, argv
,
366 initial_lang_mask
| CL_COMMON
| CL_TARGET
,
367 decoded_options
, decoded_options_count
);
370 /* Set *HANDLERS to the default set of option handlers for use in the
371 compilers proper (not the driver). */
373 set_default_handlers (struct cl_option_handlers
*handlers
)
375 handlers
->unknown_option_callback
= unknown_option_callback
;
376 handlers
->wrong_lang_callback
= complain_wrong_lang
;
377 handlers
->num_handlers
= 3;
378 handlers
->handlers
[0].handler
= lang_handle_option
;
379 handlers
->handlers
[0].mask
= initial_lang_mask
;
380 handlers
->handlers
[1].handler
= common_handle_option
;
381 handlers
->handlers
[1].mask
= CL_COMMON
;
382 handlers
->handlers
[2].handler
= target_handle_option
;
383 handlers
->handlers
[2].mask
= CL_TARGET
;
386 /* Parse command line options and set default flag values. Do minimal
387 options processing. The decoded options are in *DECODED_OPTIONS
388 and *DECODED_OPTIONS_COUNT; settings go in OPTS, OPTS_SET and DC;
389 the options are located at LOC. */
391 decode_options (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
392 struct cl_decoded_option
*decoded_options
,
393 unsigned int decoded_options_count
,
394 location_t loc
, diagnostic_context
*dc
)
396 struct cl_option_handlers handlers
;
398 unsigned int lang_mask
;
400 lang_mask
= initial_lang_mask
;
402 set_default_handlers (&handlers
);
404 default_options_optimization (opts
, opts_set
,
405 decoded_options
, decoded_options_count
,
406 loc
, lang_mask
, &handlers
, dc
);
408 read_cmdline_options (opts
, opts_set
,
409 decoded_options
, decoded_options_count
,
413 finish_options (opts
, opts_set
, loc
);
416 /* Process common options that have been deferred until after the
417 handlers have been called for all options. */
420 handle_common_deferred_options (void)
423 cl_deferred_option
*opt
;
424 VEC(cl_deferred_option
,heap
) *vec
425 = (VEC(cl_deferred_option
,heap
) *) common_deferred_options
;
427 if (flag_dump_all_passed
)
428 enable_rtl_dump_file ();
430 FOR_EACH_VEC_ELT (cl_deferred_option
, vec
, i
, opt
)
432 switch (opt
->opt_index
)
434 case OPT_fcall_used_
:
435 fix_register (opt
->arg
, 0, 1);
438 case OPT_fcall_saved_
:
439 fix_register (opt
->arg
, 0, 0);
443 dbg_cnt_process_opt (opt
->arg
);
446 case OPT_fdbg_cnt_list
:
447 dbg_cnt_list_all_counters ();
450 case OPT_fdebug_prefix_map_
:
451 add_debug_prefix_map (opt
->arg
);
455 if (!dump_switch_p (opt
->arg
))
456 error ("unrecognized command line option %<-fdump-%s%>", opt
->arg
);
461 if (opt
->opt_index
== OPT_fenable_
)
462 enable_pass (opt
->arg
);
464 disable_pass (opt
->arg
);
469 fix_register (opt
->arg
, 1, 1);
474 add_new_plugin (opt
->arg
);
476 error ("plugin support is disabled; configure with --enable-plugin");
480 case OPT_fplugin_arg_
:
482 parse_plugin_arg_opt (opt
->arg
);
484 error ("plugin support is disabled; configure with --enable-plugin");
488 case OPT_frandom_seed
:
489 /* The real switch is -fno-random-seed. */
491 set_random_seed (NULL
);
494 case OPT_frandom_seed_
:
495 set_random_seed (opt
->arg
);
498 case OPT_ffunction_attribute_list_
:
499 add_attribute_pattern (opt
->arg
);
502 case OPT_fstack_limit
:
503 /* The real switch is -fno-stack-limit. */
505 stack_limit_rtx
= NULL_RTX
;
508 case OPT_fstack_limit_register_
:
510 int reg
= decode_reg_name (opt
->arg
);
512 error ("unrecognized register name %qs", opt
->arg
);
514 stack_limit_rtx
= gen_rtx_REG (Pmode
, reg
);
518 case OPT_fstack_limit_symbol_
:
519 stack_limit_rtx
= gen_rtx_SYMBOL_REF (Pmode
, ggc_strdup (opt
->arg
));