Merge from trunk: 215733-215743
[official-gcc.git] / gcc-4_9 / gcc / opts-global.c
blob425c3c0b28f05249f8440a168d812a79489c251c
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
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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "diagnostic.h"
25 #include "opts.h"
26 #include "flags.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"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "langhooks.h"
35 #include "tm.h" /* Required by rtl.h. */
36 #include "rtl.h"
37 #include "dbgcnt.h"
38 #include "debug.h"
39 #include "lto-streamer.h"
40 #include "output.h"
41 #include "plugin.h"
42 #include "toplev.h"
43 #include "tree-pass.h"
44 #include "params.h"
45 #include "l-ipo.h"
46 #include "context.h"
47 #include "xregex.h"
48 #include "attribs.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
61 regex_t r;
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. */
68 static char *
69 write_langs (unsigned int mask)
71 unsigned int n = 0, len = 0;
72 const char *lang_name;
73 char *result;
75 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
76 if (mask & (1U << n))
77 len += strlen (lang_name) + 1;
79 result = XNEWVEC (char, len);
80 len = 0;
81 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
82 if (mask & (1U << n))
84 if (len)
85 result[len++] = '/';
86 strcpy (result + len, lang_name);
87 len += strlen (lang_name);
90 result[len] = 0;
92 return result;
95 /* Add strings like attribute_str:pattern... to attribute pattern list. */
97 static void
98 add_attribute_pattern (const char *arg)
100 char *tmp;
101 char *pattern_str;
102 struct reg_func_attr_patterns *one_pat;
103 int ec;
105 /* We never free this string. */
106 tmp = xstrdup (arg);
108 pattern_str = strchr (tmp, ':');
109 if (!pattern_str)
110 error ("invalid pattern in -ffunction-attribute-list option: %qs", tmp);
112 *pattern_str = '\0';
113 pattern_str ++;
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))
121 char err[100];
122 regerror (ec, &one_pat->r, err, 99);
123 error ("invalid pattern in -ffunction-attribute-list option: %qs: %qs",
124 pattern_str, err);
128 /* Match FNDECL's name with user specified patterns, and add attributes
129 to FNDECL. */
131 void
132 pattern_match_function_attributes (tree fndecl)
134 const char *name;
135 struct reg_func_attr_patterns *one_pat;
137 if (!fndecl)
138 return;
140 if (!reg_func_attr_patterns)
141 return;
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
152 LANG_MASK). */
154 static void
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))
164 return;
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",
174 text, bad_lang);
175 else if (lang_mask == CL_DRIVER)
176 gcc_unreachable ();
177 else
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);
182 free (ok_langs);
183 free (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. */
193 static void
194 postpone_unknown_option_warning (const char *opt)
196 ignored_options.safe_push (opt);
199 /* Produce a warning for each option previously buffered. */
201 void
202 print_ignored_options (void)
204 while (!ignored_options.is_empty ())
206 const char *opt;
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
215 be given. */
217 static bool
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);
228 return false;
230 else
231 return true;
234 /* Handle a front-end option; arguments and return value as for
235 handle_option. */
237 static bool
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,
242 location_t loc,
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. */
256 void
257 add_input_filename (const char *filename)
259 num_in_fnames++;
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
271 file. */
272 static void
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...)
278 (2) -m...
279 (3) -W...
280 (4) -O...
281 (5) --param...
282 (6) -std=... (-std=c99 for restrict keyword)
284 if (opt[0] == '-'
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"))
292 num_lipo_cl_args++;
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. */
304 static void
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,
308 location_t loc,
309 unsigned int lang_mask,
310 const struct cl_option_handlers *handlers,
311 diagnostic_context *dc)
313 unsigned int i;
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
324 line. */
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);
338 continue;
341 read_cmdline_option (opts, opts_set,
342 decoded_options + i, loc, lang_mask, handlers,
343 dc);
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. */
353 void
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. */
366 void
367 decode_cmdline_options_to_array_default_mask (unsigned int argc,
368 const char **argv,
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). */
379 void
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. */
397 void
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,
417 loc, lang_mask,
418 &handlers, dc);
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. */
426 void
427 handle_common_deferred_options (void)
429 unsigned int i;
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);
435 else
436 v = vNULL;
438 if (flag_dump_all_passed)
439 enable_rtl_dump_file ();
441 if (flag_opt_info)
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);
450 break;
452 case OPT_fcall_saved_:
453 fix_register (opt->arg, 0, 0);
454 break;
456 case OPT_fdbg_cnt_:
457 dbg_cnt_process_opt (opt->arg);
458 break;
460 case OPT_fdbg_cnt_list:
461 dbg_cnt_list_all_counters ();
462 break;
464 case OPT_fdebug_prefix_map_:
465 add_debug_prefix_map (opt->arg);
466 break;
468 case OPT_fdump_:
469 if (!g->get_dumps ()->dump_switch_p (opt->arg))
470 error ("unrecognized command line option %<-fdump-%s%>", opt->arg);
471 break;
473 case OPT_fopt_info_:
474 if (!opt_info_switch_p (opt->arg))
475 error ("unrecognized command line option %<-fopt-info-%s%>",
476 opt->arg);
477 break;
479 case OPT_fenable_:
480 case OPT_fdisable_:
481 if (opt->opt_index == OPT_fenable_)
482 enable_pass (opt->arg);
483 else
484 disable_pass (opt->arg);
485 break;
487 case OPT_ffixed_:
488 /* Deferred. */
489 fix_register (opt->arg, 1, 1);
490 break;
492 case OPT_fplugin_:
493 #ifdef ENABLE_PLUGIN
494 add_new_plugin (opt->arg);
495 #else
496 error ("plugin support is disabled; configure with --enable-plugin");
497 #endif
498 break;
500 case OPT_fplugin_arg_:
501 #ifdef ENABLE_PLUGIN
502 parse_plugin_arg_opt (opt->arg);
503 #else
504 error ("plugin support is disabled; configure with --enable-plugin");
505 #endif
506 break;
508 case OPT_frandom_seed:
509 /* The real switch is -fno-random-seed. */
510 if (!opt->value)
511 set_random_seed (NULL);
512 break;
514 case OPT_frandom_seed_:
515 set_random_seed (opt->arg);
516 break;
518 case OPT_ffunction_attribute_list_:
519 add_attribute_pattern (opt->arg);
520 break;
522 case OPT_fstack_limit:
523 /* The real switch is -fno-stack-limit. */
524 if (!opt->value)
525 stack_limit_rtx = NULL_RTX;
526 break;
528 case OPT_fstack_limit_register_:
530 int reg = decode_reg_name (opt->arg);
531 if (reg < 0)
532 error ("unrecognized register name %qs", opt->arg);
533 else
534 stack_limit_rtx = gen_rtx_REG (Pmode, reg);
536 break;
538 case OPT_fstack_limit_symbol_:
539 stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (opt->arg));
540 break;
542 default:
543 gcc_unreachable ();