coretypes.h: Include machmode.h...
[gcc.git] / gcc / opts-global.c
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.
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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
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 "hash-set.h"
28 #include "vec.h"
29 #include "input.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "inchash.h"
33 #include "tree.h" /* Required by langhooks.h. */
34 #include "fold-const.h"
35 #include "predict.h"
36 #include "tm.h"
37 #include "hard-reg-set.h"
38 #include "input.h"
39 #include "function.h"
40 #include "basic-block.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
44 #include "is-a.h"
45 #include "gimple.h"
46 #include "langhooks.h"
47 #include "rtl.h"
48 #include "dbgcnt.h"
49 #include "debug.h"
50 #include "hash-map.h"
51 #include "plugin-api.h"
52 #include "ipa-ref.h"
53 #include "cgraph.h"
54 #include "lto-streamer.h"
55 #include "output.h"
56 #include "plugin.h"
57 #include "toplev.h"
58 #include "tree-pass.h"
59 #include "context.h"
60 #include "asan.h"
61
62 typedef const char *const_char_p; /* For DEF_VEC_P. */
63
64 static vec<const_char_p> ignored_options;
65
66 /* Input file names. */
67 const char **in_fnames;
68 unsigned num_in_fnames;
69
70 /* Return a malloced slash-separated list of languages in MASK. */
71
72 static char *
73 write_langs (unsigned int mask)
74 {
75 unsigned int n = 0, len = 0;
76 const char *lang_name;
77 char *result;
78
79 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
80 if (mask & (1U << n))
81 len += strlen (lang_name) + 1;
82
83 result = XNEWVEC (char, len);
84 len = 0;
85 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
86 if (mask & (1U << n))
87 {
88 if (len)
89 result[len++] = '/';
90 strcpy (result + len, lang_name);
91 len += strlen (lang_name);
92 }
93
94 result[len] = 0;
95
96 return result;
97 }
98
99 /* Complain that switch DECODED does not apply to this front end (mask
100 LANG_MASK). */
101
102 static void
103 complain_wrong_lang (const struct cl_decoded_option *decoded,
104 unsigned int lang_mask)
105 {
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;
110
111 if (!lang_hooks.complain_wrong_lang_p (option))
112 return;
113
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);
119
120 if (opt_flags == CL_DRIVER)
121 error ("command line option %qs is valid for the driver but not for %s",
122 text, bad_lang);
123 else if (lang_mask == CL_DRIVER)
124 gcc_unreachable ();
125 else
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);
129
130 free (ok_langs);
131 free (bad_lang);
132 }
133
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. */
140
141 static void
142 postpone_unknown_option_warning (const char *opt)
143 {
144 ignored_options.safe_push (opt);
145 }
146
147 /* Produce a warning for each option previously buffered. */
148
149 void
150 print_ignored_options (void)
151 {
152 while (!ignored_options.is_empty ())
153 {
154 const char *opt;
155
156 opt = ignored_options.pop ();
157 warning_at (UNKNOWN_LOCATION, 0,
158 "unrecognized command line option %qs", opt);
159 }
160 }
161
162 /* Handle an unknown option DECODED, returning true if an error should
163 be given. */
164
165 static bool
166 unknown_option_callback (const struct cl_decoded_option *decoded)
167 {
168 const char *opt = decoded->arg;
169
170 if (opt[1] == 'W' && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-'
171 && !(decoded->errors & CL_ERR_NEGATIVE))
172 {
173 /* We don't generate warnings for unknown -Wno-* options unless
174 we issue diagnostics. */
175 postpone_unknown_option_warning (opt);
176 return false;
177 }
178 else
179 return true;
180 }
181
182 /* Handle a front-end option; arguments and return value as for
183 handle_option. */
184
185 static bool
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,
190 location_t loc,
191 const struct cl_option_handlers *handlers,
192 diagnostic_context *dc)
193 {
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);
200 }
201
202 /* Handle FILENAME from the command line. */
203
204 static void
205 add_input_filename (const char *filename)
206 {
207 num_in_fnames++;
208 in_fnames = XRESIZEVEC (const char *, in_fnames, num_in_fnames);
209 in_fnames[num_in_fnames - 1] = filename;
210 }
211
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. */
217
218 static void
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,
222 location_t loc,
223 unsigned int lang_mask,
224 const struct cl_option_handlers *handlers,
225 diagnostic_context *dc)
226 {
227 unsigned int i;
228
229 for (i = 1; i < decoded_options_count; i++)
230 {
231 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
232 {
233 /* Input files should only ever appear on the main command
234 line. */
235 gcc_assert (opts == &global_options);
236 gcc_assert (opts_set == &global_options_set);
237
238 if (opts->x_main_input_filename == NULL)
239 {
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);
244 }
245 add_input_filename (decoded_options[i].arg);
246 continue;
247 }
248
249 read_cmdline_option (opts, opts_set,
250 decoded_options + i, loc, lang_mask, handlers,
251 dc);
252 }
253 }
254
255 /* Language mask determined at initialization. */
256 static unsigned int initial_lang_mask;
257
258 /* Initialize global options-related settings at start-up. */
259
260 void
261 init_options_once (void)
262 {
263 /* Perform language-specific options initialization. */
264 initial_lang_mask = lang_hooks.option_lang_mask ();
265
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
270 are overriden. */
271 diagnostic_color_init (global_dc);
272 }
273
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. */
277
278 void
279 decode_cmdline_options_to_array_default_mask (unsigned int argc,
280 const char **argv,
281 struct cl_decoded_option **decoded_options,
282 unsigned int *decoded_options_count)
283 {
284 decode_cmdline_options_to_array (argc, argv,
285 initial_lang_mask | CL_COMMON | CL_TARGET,
286 decoded_options, decoded_options_count);
287 }
288
289 /* Set *HANDLERS to the default set of option handlers for use in the
290 compilers proper (not the driver). */
291 void
292 set_default_handlers (struct cl_option_handlers *handlers)
293 {
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;
303 }
304
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. */
309 void
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)
314 {
315 struct cl_option_handlers handlers;
316
317 unsigned int lang_mask;
318
319 lang_mask = initial_lang_mask;
320
321 set_default_handlers (&handlers);
322
323 default_options_optimization (opts, opts_set,
324 decoded_options, decoded_options_count,
325 loc, lang_mask, &handlers, dc);
326
327 read_cmdline_options (opts, opts_set,
328 decoded_options, decoded_options_count,
329 loc, lang_mask,
330 &handlers, dc);
331
332 finish_options (opts, opts_set, loc);
333 }
334
335 /* Process common options that have been deferred until after the
336 handlers have been called for all options. */
337
338 void
339 handle_common_deferred_options (void)
340 {
341 unsigned int i;
342 cl_deferred_option *opt;
343 vec<cl_deferred_option> v;
344
345 if (common_deferred_options)
346 v = *((vec<cl_deferred_option> *) common_deferred_options);
347 else
348 v = vNULL;
349
350 if (flag_dump_all_passed)
351 enable_rtl_dump_file ();
352
353 if (flag_opt_info)
354 opt_info_switch_p (NULL);
355
356 FOR_EACH_VEC_ELT (v, i, opt)
357 {
358 switch (opt->opt_index)
359 {
360 case OPT_fcall_used_:
361 fix_register (opt->arg, 0, 1);
362 break;
363
364 case OPT_fcall_saved_:
365 fix_register (opt->arg, 0, 0);
366 break;
367
368 case OPT_fdbg_cnt_:
369 dbg_cnt_process_opt (opt->arg);
370 break;
371
372 case OPT_fdbg_cnt_list:
373 dbg_cnt_list_all_counters ();
374 break;
375
376 case OPT_fdebug_prefix_map_:
377 add_debug_prefix_map (opt->arg);
378 break;
379
380 case OPT_fdump_:
381 if (!g->get_dumps ()->dump_switch_p (opt->arg))
382 error ("unrecognized command line option %<-fdump-%s%>", opt->arg);
383 break;
384
385 case OPT_fopt_info_:
386 if (!opt_info_switch_p (opt->arg))
387 error ("unrecognized command line option %<-fopt-info-%s%>",
388 opt->arg);
389 break;
390
391 case OPT_fenable_:
392 case OPT_fdisable_:
393 if (opt->opt_index == OPT_fenable_)
394 enable_pass (opt->arg);
395 else
396 disable_pass (opt->arg);
397 break;
398
399 case OPT_ffixed_:
400 /* Deferred. */
401 fix_register (opt->arg, 1, 1);
402 break;
403
404 case OPT_fplugin_:
405 #ifdef ENABLE_PLUGIN
406 add_new_plugin (opt->arg);
407 #else
408 error ("plugin support is disabled; configure with --enable-plugin");
409 #endif
410 break;
411
412 case OPT_fplugin_arg_:
413 #ifdef ENABLE_PLUGIN
414 parse_plugin_arg_opt (opt->arg);
415 #else
416 error ("plugin support is disabled; configure with --enable-plugin");
417 #endif
418 break;
419
420 case OPT_frandom_seed:
421 /* The real switch is -fno-random-seed. */
422 if (!opt->value)
423 set_random_seed (NULL);
424 break;
425
426 case OPT_frandom_seed_:
427 set_random_seed (opt->arg);
428 break;
429
430 case OPT_fstack_limit:
431 /* The real switch is -fno-stack-limit. */
432 if (!opt->value)
433 stack_limit_rtx = NULL_RTX;
434 break;
435
436 case OPT_fstack_limit_register_:
437 {
438 int reg = decode_reg_name (opt->arg);
439 if (reg < 0)
440 error ("unrecognized register name %qs", opt->arg);
441 else
442 stack_limit_rtx = gen_rtx_REG (Pmode, reg);
443 }
444 break;
445
446 case OPT_fstack_limit_symbol_:
447 stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (opt->arg));
448 break;
449
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);
456 break;
457
458 case OPT_fsanitize_sections_:
459 set_sanitized_sections (opt->arg);
460 break;
461
462 default:
463 gcc_unreachable ();
464 }
465 }
466 }