Wattributes-10.c: Add -fno-common option on hppa*-*-hpux*.
[official-gcc.git] / gcc / opts.c
blob3161e0b67533545436116a79e40469aa81bfc4bd
1 /* Command line option handling.
2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
3 Contributed by Neil Booth.
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 "intl.h"
24 #include "coretypes.h"
25 #include "opts.h"
26 #include "tm.h"
27 #include "flags.h"
28 #include "params.h"
29 #include "diagnostic.h"
30 #include "opts-diagnostic.h"
31 #include "insn-attr-common.h"
32 #include "common/common-target.h"
33 #include "spellcheck.h"
35 static void set_Wstrict_aliasing (struct gcc_options *opts, int onoff);
37 /* Indexed by enum debug_info_type. */
38 const char *const debug_type_names[] =
40 "none", "stabs", "dwarf-2", "xcoff", "vms"
43 /* Parse the -femit-struct-debug-detailed option value
44 and set the flag variables. */
46 #define MATCH( prefix, string ) \
47 ((strncmp (prefix, string, sizeof prefix - 1) == 0) \
48 ? ((string += sizeof prefix - 1), 1) : 0)
50 void
51 set_struct_debug_option (struct gcc_options *opts, location_t loc,
52 const char *spec)
54 /* various labels for comparison */
55 static const char dfn_lbl[] = "dfn:", dir_lbl[] = "dir:", ind_lbl[] = "ind:";
56 static const char ord_lbl[] = "ord:", gen_lbl[] = "gen:";
57 static const char none_lbl[] = "none", any_lbl[] = "any";
58 static const char base_lbl[] = "base", sys_lbl[] = "sys";
60 enum debug_struct_file files = DINFO_STRUCT_FILE_ANY;
61 /* Default is to apply to as much as possible. */
62 enum debug_info_usage usage = DINFO_USAGE_NUM_ENUMS;
63 int ord = 1, gen = 1;
65 /* What usage? */
66 if (MATCH (dfn_lbl, spec))
67 usage = DINFO_USAGE_DFN;
68 else if (MATCH (dir_lbl, spec))
69 usage = DINFO_USAGE_DIR_USE;
70 else if (MATCH (ind_lbl, spec))
71 usage = DINFO_USAGE_IND_USE;
73 /* Generics or not? */
74 if (MATCH (ord_lbl, spec))
75 gen = 0;
76 else if (MATCH (gen_lbl, spec))
77 ord = 0;
79 /* What allowable environment? */
80 if (MATCH (none_lbl, spec))
81 files = DINFO_STRUCT_FILE_NONE;
82 else if (MATCH (any_lbl, spec))
83 files = DINFO_STRUCT_FILE_ANY;
84 else if (MATCH (sys_lbl, spec))
85 files = DINFO_STRUCT_FILE_SYS;
86 else if (MATCH (base_lbl, spec))
87 files = DINFO_STRUCT_FILE_BASE;
88 else
89 error_at (loc,
90 "argument %qs to %<-femit-struct-debug-detailed%> "
91 "not recognized",
92 spec);
94 /* Effect the specification. */
95 if (usage == DINFO_USAGE_NUM_ENUMS)
97 if (ord)
99 opts->x_debug_struct_ordinary[DINFO_USAGE_DFN] = files;
100 opts->x_debug_struct_ordinary[DINFO_USAGE_DIR_USE] = files;
101 opts->x_debug_struct_ordinary[DINFO_USAGE_IND_USE] = files;
103 if (gen)
105 opts->x_debug_struct_generic[DINFO_USAGE_DFN] = files;
106 opts->x_debug_struct_generic[DINFO_USAGE_DIR_USE] = files;
107 opts->x_debug_struct_generic[DINFO_USAGE_IND_USE] = files;
110 else
112 if (ord)
113 opts->x_debug_struct_ordinary[usage] = files;
114 if (gen)
115 opts->x_debug_struct_generic[usage] = files;
118 if (*spec == ',')
119 set_struct_debug_option (opts, loc, spec+1);
120 else
122 /* No more -femit-struct-debug-detailed specifications.
123 Do final checks. */
124 if (*spec != '\0')
125 error_at (loc,
126 "argument %qs to %<-femit-struct-debug-detailed%> unknown",
127 spec);
128 if (opts->x_debug_struct_ordinary[DINFO_USAGE_DIR_USE]
129 < opts->x_debug_struct_ordinary[DINFO_USAGE_IND_USE]
130 || opts->x_debug_struct_generic[DINFO_USAGE_DIR_USE]
131 < opts->x_debug_struct_generic[DINFO_USAGE_IND_USE])
132 error_at (loc,
133 "%<-femit-struct-debug-detailed=dir:...%> must allow "
134 "at least as much as "
135 "%<-femit-struct-debug-detailed=ind:...%>");
139 /* Strip off a legitimate source ending from the input string NAME of
140 length LEN. Rather than having to know the names used by all of
141 our front ends, we strip off an ending of a period followed by
142 up to fource characters. (C++ uses ".cpp".) */
144 void
145 strip_off_ending (char *name, int len)
147 int i;
148 for (i = 2; i < 5 && len > i; i++)
150 if (name[len - i] == '.')
152 name[len - i] = '\0';
153 break;
158 /* Find the base name of a path, stripping off both directories and
159 a single final extension. */
161 base_of_path (const char *path, const char **base_out)
163 const char *base = path;
164 const char *dot = 0;
165 const char *p = path;
166 char c = *p;
167 while (c)
169 if (IS_DIR_SEPARATOR (c))
171 base = p + 1;
172 dot = 0;
174 else if (c == '.')
175 dot = p;
176 c = *++p;
178 if (!dot)
179 dot = p;
180 *base_out = base;
181 return dot - base;
184 /* What to print when a switch has no documentation. */
185 static const char undocumented_msg[] = N_("This option lacks documentation.");
186 static const char use_diagnosed_msg[] = N_("Uses of this option are diagnosed.");
188 typedef char *char_p; /* For DEF_VEC_P. */
190 static void handle_param (struct gcc_options *opts,
191 struct gcc_options *opts_set, location_t loc,
192 const char *carg);
193 static void set_debug_level (enum debug_info_type type, int extended,
194 const char *arg, struct gcc_options *opts,
195 struct gcc_options *opts_set,
196 location_t loc);
197 static void set_fast_math_flags (struct gcc_options *opts, int set);
198 static void decode_d_option (const char *arg, struct gcc_options *opts,
199 location_t loc, diagnostic_context *dc);
200 static void set_unsafe_math_optimizations_flags (struct gcc_options *opts,
201 int set);
202 static void enable_warning_as_error (const char *arg, int value,
203 unsigned int lang_mask,
204 const struct cl_option_handlers *handlers,
205 struct gcc_options *opts,
206 struct gcc_options *opts_set,
207 location_t loc,
208 diagnostic_context *dc);
210 /* Handle a back-end option; arguments and return value as for
211 handle_option. */
213 bool
214 target_handle_option (struct gcc_options *opts,
215 struct gcc_options *opts_set,
216 const struct cl_decoded_option *decoded,
217 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
218 location_t loc,
219 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
220 diagnostic_context *dc, void (*) (void))
222 gcc_assert (dc == global_dc);
223 gcc_assert (kind == DK_UNSPECIFIED);
224 return targetm_common.handle_option (opts, opts_set, decoded, loc);
227 /* Add comma-separated strings to a char_p vector. */
229 static void
230 add_comma_separated_to_vector (void **pvec, const char *arg)
232 char *tmp;
233 char *r;
234 char *w;
235 char *token_start;
236 vec<char_p> *v = (vec<char_p> *) *pvec;
238 vec_check_alloc (v, 1);
240 /* We never free this string. */
241 tmp = xstrdup (arg);
243 r = tmp;
244 w = tmp;
245 token_start = tmp;
247 while (*r != '\0')
249 if (*r == ',')
251 *w++ = '\0';
252 ++r;
253 v->safe_push (token_start);
254 token_start = w;
256 if (*r == '\\' && r[1] == ',')
258 *w++ = ',';
259 r += 2;
261 else
262 *w++ = *r++;
264 if (*token_start != '\0')
265 v->safe_push (token_start);
267 *pvec = v;
270 /* Initialize opts_obstack. */
272 void
273 init_opts_obstack (void)
275 gcc_obstack_init (&opts_obstack);
278 /* Initialize OPTS and OPTS_SET before using them in parsing options. */
280 void
281 init_options_struct (struct gcc_options *opts, struct gcc_options *opts_set)
283 size_t num_params = get_num_compiler_params ();
285 /* Ensure that opts_obstack has already been initialized by the time
286 that we initialize any gcc_options instances (PR jit/68446). */
287 gcc_assert (opts_obstack.chunk_size > 0);
289 *opts = global_options_init;
291 if (opts_set)
292 memset (opts_set, 0, sizeof (*opts_set));
294 opts->x_param_values = XNEWVEC (int, num_params);
296 if (opts_set)
297 opts_set->x_param_values = XCNEWVEC (int, num_params);
299 init_param_values (opts->x_param_values);
301 /* Initialize whether `char' is signed. */
302 opts->x_flag_signed_char = DEFAULT_SIGNED_CHAR;
303 /* Set this to a special "uninitialized" value. The actual default
304 is set after target options have been processed. */
305 opts->x_flag_short_enums = 2;
307 /* Initialize target_flags before default_options_optimization
308 so the latter can modify it. */
309 opts->x_target_flags = targetm_common.default_target_flags;
311 /* Some targets have ABI-specified unwind tables. */
312 opts->x_flag_unwind_tables = targetm_common.unwind_tables_default;
314 /* Some targets have other target-specific initialization. */
315 targetm_common.option_init_struct (opts);
318 /* Release any allocations owned by OPTS. */
320 void
321 finalize_options_struct (struct gcc_options *opts)
323 XDELETEVEC (opts->x_param_values);
326 /* If indicated by the optimization level LEVEL (-Os if SIZE is set,
327 -Ofast if FAST is set, -Og if DEBUG is set), apply the option DEFAULT_OPT
328 to OPTS and OPTS_SET, diagnostic context DC, location LOC, with language
329 mask LANG_MASK and option handlers HANDLERS. */
331 static void
332 maybe_default_option (struct gcc_options *opts,
333 struct gcc_options *opts_set,
334 const struct default_options *default_opt,
335 int level, bool size, bool fast, bool debug,
336 unsigned int lang_mask,
337 const struct cl_option_handlers *handlers,
338 location_t loc,
339 diagnostic_context *dc)
341 const struct cl_option *option = &cl_options[default_opt->opt_index];
342 bool enabled;
344 if (size)
345 gcc_assert (level == 2);
346 if (fast)
347 gcc_assert (level == 3);
348 if (debug)
349 gcc_assert (level == 1);
351 switch (default_opt->levels)
353 case OPT_LEVELS_ALL:
354 enabled = true;
355 break;
357 case OPT_LEVELS_0_ONLY:
358 enabled = (level == 0);
359 break;
361 case OPT_LEVELS_1_PLUS:
362 enabled = (level >= 1);
363 break;
365 case OPT_LEVELS_1_PLUS_SPEED_ONLY:
366 enabled = (level >= 1 && !size && !debug);
367 break;
369 case OPT_LEVELS_1_PLUS_NOT_DEBUG:
370 enabled = (level >= 1 && !debug);
371 break;
373 case OPT_LEVELS_2_PLUS:
374 enabled = (level >= 2);
375 break;
377 case OPT_LEVELS_2_PLUS_SPEED_ONLY:
378 enabled = (level >= 2 && !size && !debug);
379 break;
381 case OPT_LEVELS_3_PLUS:
382 enabled = (level >= 3);
383 break;
385 case OPT_LEVELS_3_PLUS_AND_SIZE:
386 enabled = (level >= 3 || size);
387 break;
389 case OPT_LEVELS_SIZE:
390 enabled = size;
391 break;
393 case OPT_LEVELS_FAST:
394 enabled = fast;
395 break;
397 case OPT_LEVELS_NONE:
398 default:
399 gcc_unreachable ();
402 if (enabled)
403 handle_generated_option (opts, opts_set, default_opt->opt_index,
404 default_opt->arg, default_opt->value,
405 lang_mask, DK_UNSPECIFIED, loc,
406 handlers, true, dc);
407 else if (default_opt->arg == NULL
408 && !option->cl_reject_negative)
409 handle_generated_option (opts, opts_set, default_opt->opt_index,
410 default_opt->arg, !default_opt->value,
411 lang_mask, DK_UNSPECIFIED, loc,
412 handlers, true, dc);
415 /* As indicated by the optimization level LEVEL (-Os if SIZE is set,
416 -Ofast if FAST is set), apply the options in array DEFAULT_OPTS to
417 OPTS and OPTS_SET, diagnostic context DC, location LOC, with
418 language mask LANG_MASK and option handlers HANDLERS. */
420 static void
421 maybe_default_options (struct gcc_options *opts,
422 struct gcc_options *opts_set,
423 const struct default_options *default_opts,
424 int level, bool size, bool fast, bool debug,
425 unsigned int lang_mask,
426 const struct cl_option_handlers *handlers,
427 location_t loc,
428 diagnostic_context *dc)
430 size_t i;
432 for (i = 0; default_opts[i].levels != OPT_LEVELS_NONE; i++)
433 maybe_default_option (opts, opts_set, &default_opts[i],
434 level, size, fast, debug,
435 lang_mask, handlers, loc, dc);
438 /* Table of options enabled by default at different levels.
439 Please keep this list sorted by level and alphabetized within
440 each level; this makes it easier to keep the documentation
441 in sync. */
443 static const struct default_options default_options_table[] =
445 /* -O1 and -Og optimizations. */
446 { OPT_LEVELS_1_PLUS, OPT_fcombine_stack_adjustments, NULL, 1 },
447 { OPT_LEVELS_1_PLUS, OPT_fcompare_elim, NULL, 1 },
448 { OPT_LEVELS_1_PLUS, OPT_fcprop_registers, NULL, 1 },
449 { OPT_LEVELS_1_PLUS, OPT_fdefer_pop, NULL, 1 },
450 { OPT_LEVELS_1_PLUS, OPT_fforward_propagate, NULL, 1 },
451 { OPT_LEVELS_1_PLUS, OPT_fguess_branch_probability, NULL, 1 },
452 { OPT_LEVELS_1_PLUS, OPT_fipa_profile, NULL, 1 },
453 { OPT_LEVELS_1_PLUS, OPT_fipa_pure_const, NULL, 1 },
454 { OPT_LEVELS_1_PLUS, OPT_fipa_reference, NULL, 1 },
455 { OPT_LEVELS_1_PLUS, OPT_fipa_reference_addressable, NULL, 1 },
456 { OPT_LEVELS_1_PLUS, OPT_fmerge_constants, NULL, 1 },
457 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
458 { OPT_LEVELS_1_PLUS, OPT_freorder_blocks, NULL, 1 },
459 { OPT_LEVELS_1_PLUS, OPT_fshrink_wrap, NULL, 1 },
460 { OPT_LEVELS_1_PLUS, OPT_fsplit_wide_types, NULL, 1 },
461 { OPT_LEVELS_1_PLUS, OPT_ftree_builtin_call_dce, NULL, 1 },
462 { OPT_LEVELS_1_PLUS, OPT_ftree_ccp, NULL, 1 },
463 { OPT_LEVELS_1_PLUS, OPT_ftree_ch, NULL, 1 },
464 { OPT_LEVELS_1_PLUS, OPT_ftree_coalesce_vars, NULL, 1 },
465 { OPT_LEVELS_1_PLUS, OPT_ftree_copy_prop, NULL, 1 },
466 { OPT_LEVELS_1_PLUS, OPT_ftree_dce, NULL, 1 },
467 { OPT_LEVELS_1_PLUS, OPT_ftree_dominator_opts, NULL, 1 },
468 { OPT_LEVELS_1_PLUS, OPT_ftree_dse, NULL, 1 },
469 { OPT_LEVELS_1_PLUS, OPT_ftree_fre, NULL, 1 },
470 { OPT_LEVELS_1_PLUS, OPT_ftree_sink, NULL, 1 },
471 { OPT_LEVELS_1_PLUS, OPT_ftree_slsr, NULL, 1 },
472 { OPT_LEVELS_1_PLUS, OPT_ftree_ter, NULL, 1 },
474 /* -O1 (and not -Og) optimizations. */
475 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fbranch_count_reg, NULL, 1 },
476 #if DELAY_SLOTS
477 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fdelayed_branch, NULL, 1 },
478 #endif
479 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fif_conversion, NULL, 1 },
480 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fif_conversion2, NULL, 1 },
481 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_finline_functions_called_once, NULL, 1 },
482 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fmove_loop_invariants, NULL, 1 },
483 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fssa_phiopt, NULL, 1 },
484 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_bit_ccp, NULL, 1 },
485 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_sra, NULL, 1 },
486 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_pta, NULL, 1 },
488 /* -O2 and -Os optimizations. */
489 { OPT_LEVELS_2_PLUS, OPT_fcaller_saves, NULL, 1 },
490 { OPT_LEVELS_2_PLUS, OPT_fcode_hoisting, NULL, 1 },
491 { OPT_LEVELS_2_PLUS, OPT_fcrossjumping, NULL, 1 },
492 { OPT_LEVELS_2_PLUS, OPT_fcse_follow_jumps, NULL, 1 },
493 { OPT_LEVELS_2_PLUS, OPT_fdevirtualize, NULL, 1 },
494 { OPT_LEVELS_2_PLUS, OPT_fdevirtualize_speculatively, NULL, 1 },
495 { OPT_LEVELS_2_PLUS, OPT_fexpensive_optimizations, NULL, 1 },
496 { OPT_LEVELS_2_PLUS, OPT_fgcse, NULL, 1 },
497 { OPT_LEVELS_2_PLUS, OPT_fhoist_adjacent_loads, NULL, 1 },
498 { OPT_LEVELS_2_PLUS, OPT_findirect_inlining, NULL, 1 },
499 { OPT_LEVELS_2_PLUS, OPT_finline_small_functions, NULL, 1 },
500 { OPT_LEVELS_2_PLUS, OPT_fipa_bit_cp, NULL, 1 },
501 { OPT_LEVELS_2_PLUS, OPT_fipa_cp, NULL, 1 },
502 { OPT_LEVELS_2_PLUS, OPT_fipa_icf, NULL, 1 },
503 { OPT_LEVELS_2_PLUS, OPT_fipa_ra, NULL, 1 },
504 { OPT_LEVELS_2_PLUS, OPT_fipa_sra, NULL, 1 },
505 { OPT_LEVELS_2_PLUS, OPT_fipa_vrp, NULL, 1 },
506 { OPT_LEVELS_2_PLUS, OPT_fisolate_erroneous_paths_dereference, NULL, 1 },
507 { OPT_LEVELS_2_PLUS, OPT_flra_remat, NULL, 1 },
508 { OPT_LEVELS_2_PLUS, OPT_foptimize_sibling_calls, NULL, 1 },
509 { OPT_LEVELS_2_PLUS, OPT_fpartial_inlining, NULL, 1 },
510 { OPT_LEVELS_2_PLUS, OPT_fpeephole2, NULL, 1 },
511 { OPT_LEVELS_2_PLUS, OPT_freorder_functions, NULL, 1 },
512 { OPT_LEVELS_2_PLUS, OPT_frerun_cse_after_loop, NULL, 1 },
513 #ifdef INSN_SCHEDULING
514 { OPT_LEVELS_2_PLUS, OPT_fschedule_insns2, NULL, 1 },
515 #endif
516 { OPT_LEVELS_2_PLUS, OPT_fstrict_aliasing, NULL, 1 },
517 { OPT_LEVELS_2_PLUS, OPT_fstore_merging, NULL, 1 },
518 { OPT_LEVELS_2_PLUS, OPT_fthread_jumps, NULL, 1 },
519 { OPT_LEVELS_2_PLUS, OPT_ftree_pre, NULL, 1 },
520 { OPT_LEVELS_2_PLUS, OPT_ftree_switch_conversion, NULL, 1 },
521 { OPT_LEVELS_2_PLUS, OPT_ftree_tail_merge, NULL, 1 },
522 { OPT_LEVELS_2_PLUS, OPT_ftree_vrp, NULL, 1 },
523 { OPT_LEVELS_2_PLUS, OPT_fvect_cost_model_, NULL, VECT_COST_MODEL_CHEAP },
525 /* -O2 and -Os optimizations. */
526 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_functions, NULL, 1 },
527 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_jumps, NULL, 1 },
528 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_labels, NULL, 1 },
529 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_loops, NULL, 1 },
530 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_foptimize_strlen, NULL, 1 },
531 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_freorder_blocks_algorithm_, NULL,
532 REORDER_BLOCKS_ALGORITHM_STC },
533 #ifdef INSN_SCHEDULING
534 /* Only run the pre-regalloc scheduling pass if optimizing for speed. */
535 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_fschedule_insns, NULL, 1 },
536 #endif
538 /* -O3 and -Os optimizations. */
539 /* Inlining of functions reducing size is a good idea with -Os
540 regardless of them being declared inline. */
541 { OPT_LEVELS_3_PLUS_AND_SIZE, OPT_finline_functions, NULL, 1 },
543 /* -O3 optimizations. */
544 { OPT_LEVELS_3_PLUS, OPT_fgcse_after_reload, NULL, 1 },
545 { OPT_LEVELS_3_PLUS, OPT_fipa_cp_clone, NULL, 1 },
546 { OPT_LEVELS_3_PLUS, OPT_floop_interchange, NULL, 1 },
547 { OPT_LEVELS_3_PLUS, OPT_floop_unroll_and_jam, NULL, 1 },
548 { OPT_LEVELS_3_PLUS, OPT_fpeel_loops, NULL, 1 },
549 { OPT_LEVELS_3_PLUS, OPT_fpredictive_commoning, NULL, 1 },
550 { OPT_LEVELS_3_PLUS, OPT_fsplit_loops, NULL, 1 },
551 { OPT_LEVELS_3_PLUS, OPT_fsplit_paths, NULL, 1 },
552 { OPT_LEVELS_3_PLUS, OPT_ftree_loop_distribute_patterns, NULL, 1 },
553 { OPT_LEVELS_3_PLUS, OPT_ftree_loop_distribution, NULL, 1 },
554 { OPT_LEVELS_3_PLUS, OPT_ftree_loop_vectorize, NULL, 1 },
555 { OPT_LEVELS_3_PLUS, OPT_ftree_partial_pre, NULL, 1 },
556 { OPT_LEVELS_3_PLUS, OPT_ftree_slp_vectorize, NULL, 1 },
557 { OPT_LEVELS_3_PLUS, OPT_funswitch_loops, NULL, 1 },
558 { OPT_LEVELS_3_PLUS, OPT_fvect_cost_model_, NULL, VECT_COST_MODEL_DYNAMIC },
559 { OPT_LEVELS_3_PLUS, OPT_fversion_loops_for_strides, NULL, 1 },
561 /* -Ofast adds optimizations to -O3. */
562 { OPT_LEVELS_FAST, OPT_ffast_math, NULL, 1 },
564 { OPT_LEVELS_NONE, 0, NULL, 0 }
567 /* Default the options in OPTS and OPTS_SET based on the optimization
568 settings in DECODED_OPTIONS and DECODED_OPTIONS_COUNT. */
569 void
570 default_options_optimization (struct gcc_options *opts,
571 struct gcc_options *opts_set,
572 struct cl_decoded_option *decoded_options,
573 unsigned int decoded_options_count,
574 location_t loc,
575 unsigned int lang_mask,
576 const struct cl_option_handlers *handlers,
577 diagnostic_context *dc)
579 unsigned int i;
580 int opt2;
581 bool openacc_mode = false;
583 /* Scan to see what optimization level has been specified. That will
584 determine the default value of many flags. */
585 for (i = 1; i < decoded_options_count; i++)
587 struct cl_decoded_option *opt = &decoded_options[i];
588 switch (opt->opt_index)
590 case OPT_O:
591 if (*opt->arg == '\0')
593 opts->x_optimize = 1;
594 opts->x_optimize_size = 0;
595 opts->x_optimize_fast = 0;
596 opts->x_optimize_debug = 0;
598 else
600 const int optimize_val = integral_argument (opt->arg);
601 if (optimize_val == -1)
602 error_at (loc, "argument to %<-O%> should be a non-negative "
603 "integer, %<g%>, %<s%> or %<fast%>");
604 else
606 opts->x_optimize = optimize_val;
607 if ((unsigned int) opts->x_optimize > 255)
608 opts->x_optimize = 255;
609 opts->x_optimize_size = 0;
610 opts->x_optimize_fast = 0;
611 opts->x_optimize_debug = 0;
614 break;
616 case OPT_Os:
617 opts->x_optimize_size = 1;
619 /* Optimizing for size forces optimize to be 2. */
620 opts->x_optimize = 2;
621 opts->x_optimize_fast = 0;
622 opts->x_optimize_debug = 0;
623 break;
625 case OPT_Ofast:
626 /* -Ofast only adds flags to -O3. */
627 opts->x_optimize_size = 0;
628 opts->x_optimize = 3;
629 opts->x_optimize_fast = 1;
630 opts->x_optimize_debug = 0;
631 break;
633 case OPT_Og:
634 /* -Og selects optimization level 1. */
635 opts->x_optimize_size = 0;
636 opts->x_optimize = 1;
637 opts->x_optimize_fast = 0;
638 opts->x_optimize_debug = 1;
639 break;
641 case OPT_fopenacc:
642 if (opt->value)
643 openacc_mode = true;
644 break;
646 default:
647 /* Ignore other options in this prescan. */
648 break;
652 maybe_default_options (opts, opts_set, default_options_table,
653 opts->x_optimize, opts->x_optimize_size,
654 opts->x_optimize_fast, opts->x_optimize_debug,
655 lang_mask, handlers, loc, dc);
657 /* -O2 param settings. */
658 opt2 = (opts->x_optimize >= 2);
660 if (openacc_mode
661 && !opts_set->x_flag_ipa_pta)
662 opts->x_flag_ipa_pta = true;
664 /* Track fields in field-sensitive alias analysis. */
665 maybe_set_param_value
666 (PARAM_MAX_FIELDS_FOR_FIELD_SENSITIVE,
667 opt2 ? 100 : default_param_value (PARAM_MAX_FIELDS_FOR_FIELD_SENSITIVE),
668 opts->x_param_values, opts_set->x_param_values);
670 /* For -O1 only do loop invariant motion for very small loops. */
671 maybe_set_param_value
672 (PARAM_LOOP_INVARIANT_MAX_BBS_IN_LOOP,
673 opt2 ? default_param_value (PARAM_LOOP_INVARIANT_MAX_BBS_IN_LOOP)
674 : default_param_value (PARAM_LOOP_INVARIANT_MAX_BBS_IN_LOOP) / 10,
675 opts->x_param_values, opts_set->x_param_values);
677 /* For -O1 reduce the maximum number of active local stores for RTL DSE
678 since this can consume huge amounts of memory (PR89115). */
679 maybe_set_param_value
680 (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES,
681 opt2 ? default_param_value (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES)
682 : default_param_value (PARAM_MAX_DSE_ACTIVE_LOCAL_STORES) / 10,
683 opts->x_param_values, opts_set->x_param_values);
685 /* At -Ofast, allow store motion to introduce potential race conditions. */
686 maybe_set_param_value
687 (PARAM_ALLOW_STORE_DATA_RACES,
688 opts->x_optimize_fast ? 1
689 : default_param_value (PARAM_ALLOW_STORE_DATA_RACES),
690 opts->x_param_values, opts_set->x_param_values);
692 if (opts->x_optimize_size)
693 /* We want to crossjump as much as possible. */
694 maybe_set_param_value (PARAM_MIN_CROSSJUMP_INSNS, 1,
695 opts->x_param_values, opts_set->x_param_values);
696 else
697 maybe_set_param_value (PARAM_MIN_CROSSJUMP_INSNS,
698 default_param_value (PARAM_MIN_CROSSJUMP_INSNS),
699 opts->x_param_values, opts_set->x_param_values);
701 /* Restrict the amount of work combine does at -Og while retaining
702 most of its useful transforms. */
703 if (opts->x_optimize_debug)
704 maybe_set_param_value (PARAM_MAX_COMBINE_INSNS, 2,
705 opts->x_param_values, opts_set->x_param_values);
707 /* Allow default optimizations to be specified on a per-machine basis. */
708 maybe_default_options (opts, opts_set,
709 targetm_common.option_optimization_table,
710 opts->x_optimize, opts->x_optimize_size,
711 opts->x_optimize_fast, opts->x_optimize_debug,
712 lang_mask, handlers, loc, dc);
715 /* Control IPA optimizations based on different live patching LEVEL. */
716 static void
717 control_options_for_live_patching (struct gcc_options *opts,
718 struct gcc_options *opts_set,
719 enum live_patching_level level,
720 location_t loc)
722 gcc_assert (level > LIVE_PATCHING_NONE);
724 switch (level)
726 case LIVE_PATCHING_INLINE_ONLY_STATIC:
727 if (opts_set->x_flag_ipa_cp_clone && opts->x_flag_ipa_cp_clone)
728 error_at (loc,
729 "%<-fipa-cp-clone%> is incompatible with "
730 "%<-flive-patching=inline-only-static%>");
731 else
732 opts->x_flag_ipa_cp_clone = 0;
734 if (opts_set->x_flag_ipa_sra && opts->x_flag_ipa_sra)
735 error_at (loc,
736 "%<-fipa-sra%> is incompatible with "
737 "%<-flive-patching=inline-only-static%>");
738 else
739 opts->x_flag_ipa_sra = 0;
741 if (opts_set->x_flag_partial_inlining && opts->x_flag_partial_inlining)
742 error_at (loc,
743 "%<-fpartial-inlining%> is incompatible with "
744 "%<-flive-patching=inline-only-static%>");
745 else
746 opts->x_flag_partial_inlining = 0;
748 if (opts_set->x_flag_ipa_cp && opts->x_flag_ipa_cp)
749 error_at (loc,
750 "%<-fipa-cp%> is incompatible with "
751 "%<-flive-patching=inline-only-static%>");
752 else
753 opts->x_flag_ipa_cp = 0;
755 /* FALLTHROUGH. */
756 case LIVE_PATCHING_INLINE_CLONE:
757 /* live patching should disable whole-program optimization. */
758 if (opts_set->x_flag_whole_program && opts->x_flag_whole_program)
759 error_at (loc,
760 "%<-fwhole-program%> is incompatible with "
761 "%<-flive-patching=inline-only-static|inline-clone%>");
762 else
763 opts->x_flag_whole_program = 0;
765 /* visibility change should be excluded by !flag_whole_program
766 && !in_lto_p && !flag_ipa_cp_clone && !flag_ipa_sra
767 && !flag_partial_inlining. */
769 if (opts_set->x_flag_ipa_pta && opts->x_flag_ipa_pta)
770 error_at (loc,
771 "%<-fipa-pta%> is incompatible with "
772 "%<-flive-patching=inline-only-static|inline-clone%>");
773 else
774 opts->x_flag_ipa_pta = 0;
776 if (opts_set->x_flag_ipa_reference && opts->x_flag_ipa_reference)
777 error_at (loc,
778 "%<-fipa-reference%> is incompatible with "
779 "%<-flive-patching=inline-only-static|inline-clone%>");
780 else
781 opts->x_flag_ipa_reference = 0;
783 if (opts_set->x_flag_ipa_ra && opts->x_flag_ipa_ra)
784 error_at (loc,
785 "%<-fipa-ra%> is incompatible with "
786 "%<-flive-patching=inline-only-static|inline-clone%>");
787 else
788 opts->x_flag_ipa_ra = 0;
790 if (opts_set->x_flag_ipa_icf && opts->x_flag_ipa_icf)
791 error_at (loc,
792 "%<-fipa-icf%> is incompatible with "
793 "%<-flive-patching=inline-only-static|inline-clone%>");
794 else
795 opts->x_flag_ipa_icf = 0;
797 if (opts_set->x_flag_ipa_icf_functions && opts->x_flag_ipa_icf_functions)
798 error_at (loc,
799 "%<-fipa-icf-functions%> is incompatible with "
800 "%<-flive-patching=inline-only-static|inline-clone%>");
801 else
802 opts->x_flag_ipa_icf_functions = 0;
804 if (opts_set->x_flag_ipa_icf_variables && opts->x_flag_ipa_icf_variables)
805 error_at (loc,
806 "%<-fipa-icf-variables%> is incompatible with "
807 "%<-flive-patching=inline-only-static|inline-clone%>");
808 else
809 opts->x_flag_ipa_icf_variables = 0;
811 if (opts_set->x_flag_ipa_bit_cp && opts->x_flag_ipa_bit_cp)
812 error_at (loc,
813 "%<-fipa-bit-cp%> is incompatible with "
814 "%<-flive-patching=inline-only-static|inline-clone%>");
815 else
816 opts->x_flag_ipa_bit_cp = 0;
818 if (opts_set->x_flag_ipa_vrp && opts->x_flag_ipa_vrp)
819 error_at (loc,
820 "%<-fipa-vrp%> is incompatible with "
821 "%<-flive-patching=inline-only-static|inline-clone%>");
822 else
823 opts->x_flag_ipa_vrp = 0;
825 if (opts_set->x_flag_ipa_pure_const && opts->x_flag_ipa_pure_const)
826 error_at (loc,
827 "%<-fipa-pure-const%> is incompatible with "
828 "%<-flive-patching=inline-only-static|inline-clone%>");
829 else
830 opts->x_flag_ipa_pure_const = 0;
832 /* FIXME: disable unreachable code removal. */
834 /* discovery of functions/variables with no address taken. */
835 if (opts_set->x_flag_ipa_reference_addressable
836 && opts->x_flag_ipa_reference_addressable)
837 error_at (loc,
838 "%<-fipa-reference-addressable%> is incompatible with "
839 "%<-flive-patching=inline-only-static|inline-clone%>");
840 else
841 opts->x_flag_ipa_reference_addressable = 0;
843 /* ipa stack alignment propagation. */
844 if (opts_set->x_flag_ipa_stack_alignment
845 && opts->x_flag_ipa_stack_alignment)
846 error_at (loc,
847 "%<-fipa-stack-alignment%> is incompatible with "
848 "%<-flive-patching=inline-only-static|inline-clone%>");
849 else
850 opts->x_flag_ipa_stack_alignment = 0;
851 break;
852 default:
853 gcc_unreachable ();
857 /* After all options at LOC have been read into OPTS and OPTS_SET,
858 finalize settings of those options and diagnose incompatible
859 combinations. */
860 void
861 finish_options (struct gcc_options *opts, struct gcc_options *opts_set,
862 location_t loc)
864 enum unwind_info_type ui_except;
866 if (opts->x_dump_base_name
867 && ! opts->x_dump_base_name_prefixed)
869 const char *sep = opts->x_dump_base_name;
871 for (; *sep; sep++)
872 if (IS_DIR_SEPARATOR (*sep))
873 break;
875 if (*sep)
876 /* If dump_base_path contains subdirectories, don't prepend
877 anything. */;
878 else if (opts->x_dump_dir_name)
879 /* We have a DUMP_DIR_NAME, prepend that. */
880 opts->x_dump_base_name = opts_concat (opts->x_dump_dir_name,
881 opts->x_dump_base_name, NULL);
882 else if (opts->x_aux_base_name
883 && strcmp (opts->x_aux_base_name, HOST_BIT_BUCKET) != 0)
884 /* AUX_BASE_NAME is set and is not the bit bucket. If it
885 contains a directory component, prepend those directories.
886 Typically this places things in the same directory as the
887 object file. */
889 const char *aux_base;
891 base_of_path (opts->x_aux_base_name, &aux_base);
892 if (opts->x_aux_base_name != aux_base)
894 int dir_len = aux_base - opts->x_aux_base_name;
895 char *new_dump_base_name
896 = XOBNEWVEC (&opts_obstack, char,
897 strlen (opts->x_dump_base_name) + dir_len + 1);
899 /* Copy directory component from OPTS->X_AUX_BASE_NAME. */
900 memcpy (new_dump_base_name, opts->x_aux_base_name, dir_len);
901 /* Append existing OPTS->X_DUMP_BASE_NAME. */
902 strcpy (new_dump_base_name + dir_len, opts->x_dump_base_name);
903 opts->x_dump_base_name = new_dump_base_name;
907 /* It is definitely prefixed now. */
908 opts->x_dump_base_name_prefixed = true;
911 /* Handle related options for unit-at-a-time, toplevel-reorder, and
912 section-anchors. */
913 if (!opts->x_flag_unit_at_a_time)
915 if (opts->x_flag_section_anchors && opts_set->x_flag_section_anchors)
916 error_at (loc, "section anchors must be disabled when unit-at-a-time "
917 "is disabled");
918 opts->x_flag_section_anchors = 0;
919 if (opts->x_flag_toplevel_reorder == 1)
920 error_at (loc, "toplevel reorder must be disabled when unit-at-a-time "
921 "is disabled");
922 opts->x_flag_toplevel_reorder = 0;
925 /* -fself-test depends on the state of the compiler prior to
926 compiling anything. Ideally it should be run on an empty source
927 file. However, in case we get run with actual source, assume
928 -fsyntax-only which will inhibit any compiler initialization
929 which may confuse the self tests. */
930 if (opts->x_flag_self_test)
931 opts->x_flag_syntax_only = 1;
933 if (opts->x_flag_tm && opts->x_flag_non_call_exceptions)
934 sorry ("transactional memory is not supported with non-call exceptions");
936 /* Unless the user has asked for section anchors, we disable toplevel
937 reordering at -O0 to disable transformations that might be surprising
938 to end users and to get -fno-toplevel-reorder tested. */
939 if (!opts->x_optimize
940 && opts->x_flag_toplevel_reorder == 2
941 && !(opts->x_flag_section_anchors && opts_set->x_flag_section_anchors))
943 opts->x_flag_toplevel_reorder = 0;
944 opts->x_flag_section_anchors = 0;
946 if (!opts->x_flag_toplevel_reorder)
948 if (opts->x_flag_section_anchors && opts_set->x_flag_section_anchors)
949 error_at (loc, "section anchors must be disabled when toplevel reorder"
950 " is disabled");
951 opts->x_flag_section_anchors = 0;
954 if (!opts->x_flag_opts_finished)
956 /* We initialize opts->x_flag_pie to -1 so that targets can set a
957 default value. */
958 if (opts->x_flag_pie == -1)
960 /* We initialize opts->x_flag_pic to -1 so that we can tell if
961 -fpic, -fPIC, -fno-pic or -fno-PIC is used. */
962 if (opts->x_flag_pic == -1)
963 opts->x_flag_pie = DEFAULT_FLAG_PIE;
964 else
965 opts->x_flag_pie = 0;
967 /* If -fPIE or -fpie is used, turn on PIC. */
968 if (opts->x_flag_pie)
969 opts->x_flag_pic = opts->x_flag_pie;
970 else if (opts->x_flag_pic == -1)
971 opts->x_flag_pic = 0;
972 if (opts->x_flag_pic && !opts->x_flag_pie)
973 opts->x_flag_shlib = 1;
974 opts->x_flag_opts_finished = true;
977 /* We initialize opts->x_flag_stack_protect to -1 so that targets
978 can set a default value. */
979 if (opts->x_flag_stack_protect == -1)
980 opts->x_flag_stack_protect = DEFAULT_FLAG_SSP;
982 if (opts->x_optimize == 0)
984 /* Inlining does not work if not optimizing,
985 so force it not to be done. */
986 opts->x_warn_inline = 0;
987 opts->x_flag_no_inline = 1;
990 /* The optimization to partition hot and cold basic blocks into separate
991 sections of the .o and executable files does not work (currently)
992 with exception handling. This is because there is no support for
993 generating unwind info. If opts->x_flag_exceptions is turned on
994 we need to turn off the partitioning optimization. */
996 ui_except = targetm_common.except_unwind_info (opts);
998 if (opts->x_flag_exceptions
999 && opts->x_flag_reorder_blocks_and_partition
1000 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))
1002 if (opts_set->x_flag_reorder_blocks_and_partition)
1003 inform (loc,
1004 "%<-freorder-blocks-and-partition%> does not work "
1005 "with exceptions on this architecture");
1006 opts->x_flag_reorder_blocks_and_partition = 0;
1007 opts->x_flag_reorder_blocks = 1;
1010 /* If user requested unwind info, then turn off the partitioning
1011 optimization. */
1013 if (opts->x_flag_unwind_tables
1014 && !targetm_common.unwind_tables_default
1015 && opts->x_flag_reorder_blocks_and_partition
1016 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))
1018 if (opts_set->x_flag_reorder_blocks_and_partition)
1019 inform (loc,
1020 "%<-freorder-blocks-and-partition%> does not support "
1021 "unwind info on this architecture");
1022 opts->x_flag_reorder_blocks_and_partition = 0;
1023 opts->x_flag_reorder_blocks = 1;
1026 /* If the target requested unwind info, then turn off the partitioning
1027 optimization with a different message. Likewise, if the target does not
1028 support named sections. */
1030 if (opts->x_flag_reorder_blocks_and_partition
1031 && (!targetm_common.have_named_sections
1032 || (opts->x_flag_unwind_tables
1033 && targetm_common.unwind_tables_default
1034 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))))
1036 if (opts_set->x_flag_reorder_blocks_and_partition)
1037 inform (loc,
1038 "%<-freorder-blocks-and-partition%> does not work "
1039 "on this architecture");
1040 opts->x_flag_reorder_blocks_and_partition = 0;
1041 opts->x_flag_reorder_blocks = 1;
1045 /* Pipelining of outer loops is only possible when general pipelining
1046 capabilities are requested. */
1047 if (!opts->x_flag_sel_sched_pipelining)
1048 opts->x_flag_sel_sched_pipelining_outer_loops = 0;
1050 if (opts->x_flag_conserve_stack)
1052 maybe_set_param_value (PARAM_LARGE_STACK_FRAME, 100,
1053 opts->x_param_values, opts_set->x_param_values);
1054 maybe_set_param_value (PARAM_STACK_FRAME_GROWTH, 40,
1055 opts->x_param_values, opts_set->x_param_values);
1058 if (opts->x_flag_lto)
1060 #ifdef ENABLE_LTO
1061 opts->x_flag_generate_lto = 1;
1063 /* When generating IL, do not operate in whole-program mode.
1064 Otherwise, symbols will be privatized too early, causing link
1065 errors later. */
1066 opts->x_flag_whole_program = 0;
1067 #else
1068 error_at (loc, "LTO support has not been enabled in this configuration");
1069 #endif
1070 if (!opts->x_flag_fat_lto_objects
1071 && (!HAVE_LTO_PLUGIN
1072 || (opts_set->x_flag_use_linker_plugin
1073 && !opts->x_flag_use_linker_plugin)))
1075 if (opts_set->x_flag_fat_lto_objects)
1076 error_at (loc, "%<-fno-fat-lto-objects%> are supported only with "
1077 "linker plugin");
1078 opts->x_flag_fat_lto_objects = 1;
1082 /* We initialize opts->x_flag_split_stack to -1 so that targets can set a
1083 default value if they choose based on other options. */
1084 if (opts->x_flag_split_stack == -1)
1085 opts->x_flag_split_stack = 0;
1086 else if (opts->x_flag_split_stack)
1088 if (!targetm_common.supports_split_stack (true, opts))
1090 error_at (loc, "%<-fsplit-stack%> is not supported by "
1091 "this compiler configuration");
1092 opts->x_flag_split_stack = 0;
1096 /* If stack splitting is turned on, and the user did not explicitly
1097 request function partitioning, turn off partitioning, as it
1098 confuses the linker when trying to handle partitioned split-stack
1099 code that calls a non-split-stack functions. But if partitioning
1100 was turned on explicitly just hope for the best. */
1101 if (opts->x_flag_split_stack
1102 && opts->x_flag_reorder_blocks_and_partition
1103 && !opts_set->x_flag_reorder_blocks_and_partition)
1104 opts->x_flag_reorder_blocks_and_partition = 0;
1106 if (opts->x_flag_reorder_blocks_and_partition
1107 && !opts_set->x_flag_reorder_functions)
1108 opts->x_flag_reorder_functions = 1;
1110 /* Tune vectorization related parametees according to cost model. */
1111 if (opts->x_flag_vect_cost_model == VECT_COST_MODEL_CHEAP)
1113 maybe_set_param_value (PARAM_VECT_MAX_VERSION_FOR_ALIAS_CHECKS,
1114 6, opts->x_param_values, opts_set->x_param_values);
1115 maybe_set_param_value (PARAM_VECT_MAX_VERSION_FOR_ALIGNMENT_CHECKS,
1116 0, opts->x_param_values, opts_set->x_param_values);
1117 maybe_set_param_value (PARAM_VECT_MAX_PEELING_FOR_ALIGNMENT,
1118 0, opts->x_param_values, opts_set->x_param_values);
1121 /* Set PARAM_MAX_STORES_TO_SINK to 0 if either vectorization or if-conversion
1122 is disabled. */
1123 if ((!opts->x_flag_tree_loop_vectorize && !opts->x_flag_tree_slp_vectorize)
1124 || !opts->x_flag_tree_loop_if_convert)
1125 maybe_set_param_value (PARAM_MAX_STORES_TO_SINK, 0,
1126 opts->x_param_values, opts_set->x_param_values);
1128 /* The -gsplit-dwarf option requires -ggnu-pubnames. */
1129 if (opts->x_dwarf_split_debug_info)
1130 opts->x_debug_generate_pub_sections = 2;
1132 if ((opts->x_flag_sanitize
1133 & (SANITIZE_USER_ADDRESS | SANITIZE_KERNEL_ADDRESS)) == 0)
1135 if (opts->x_flag_sanitize & SANITIZE_POINTER_COMPARE)
1136 error_at (loc,
1137 "%<-fsanitize=pointer-compare%> must be combined with "
1138 "%<-fsanitize=address%> or %<-fsanitize=kernel-address%>");
1139 if (opts->x_flag_sanitize & SANITIZE_POINTER_SUBTRACT)
1140 error_at (loc,
1141 "%<-fsanitize=pointer-subtract%> must be combined with "
1142 "%<-fsanitize=address%> or %<-fsanitize=kernel-address%>");
1145 /* Userspace and kernel ASan conflict with each other. */
1146 if ((opts->x_flag_sanitize & SANITIZE_USER_ADDRESS)
1147 && (opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS))
1148 error_at (loc,
1149 "%<-fsanitize=address%> is incompatible with "
1150 "%<-fsanitize=kernel-address%>");
1152 /* And with TSan. */
1153 if ((opts->x_flag_sanitize & SANITIZE_ADDRESS)
1154 && (opts->x_flag_sanitize & SANITIZE_THREAD))
1155 error_at (loc,
1156 "%<-fsanitize=address%> and %<-fsanitize=kernel-address%> "
1157 "are incompatible with %<-fsanitize=thread%>");
1159 if ((opts->x_flag_sanitize & SANITIZE_LEAK)
1160 && (opts->x_flag_sanitize & SANITIZE_THREAD))
1161 error_at (loc,
1162 "%<-fsanitize=leak%> is incompatible with %<-fsanitize=thread%>");
1164 /* Check error recovery for -fsanitize-recover option. */
1165 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
1166 if ((opts->x_flag_sanitize_recover & sanitizer_opts[i].flag)
1167 && !sanitizer_opts[i].can_recover)
1168 error_at (loc, "%<-fsanitize-recover=%s%> is not supported",
1169 sanitizer_opts[i].name);
1171 /* When instrumenting the pointers, we don't want to remove
1172 the null pointer checks. */
1173 if (opts->x_flag_sanitize & (SANITIZE_NULL | SANITIZE_NONNULL_ATTRIBUTE
1174 | SANITIZE_RETURNS_NONNULL_ATTRIBUTE))
1175 opts->x_flag_delete_null_pointer_checks = 0;
1177 /* Aggressive compiler optimizations may cause false negatives. */
1178 if (opts->x_flag_sanitize & ~(SANITIZE_LEAK | SANITIZE_UNREACHABLE))
1179 opts->x_flag_aggressive_loop_optimizations = 0;
1181 /* Enable -fsanitize-address-use-after-scope if address sanitizer is
1182 enabled. */
1183 if ((opts->x_flag_sanitize & SANITIZE_USER_ADDRESS)
1184 && !opts_set->x_flag_sanitize_address_use_after_scope)
1185 opts->x_flag_sanitize_address_use_after_scope = true;
1187 /* Force -fstack-reuse=none in case -fsanitize-address-use-after-scope
1188 is enabled. */
1189 if (opts->x_flag_sanitize_address_use_after_scope)
1191 if (opts->x_flag_stack_reuse != SR_NONE
1192 && opts_set->x_flag_stack_reuse != SR_NONE)
1193 error_at (loc,
1194 "%<-fsanitize-address-use-after-scope%> requires "
1195 "%<-fstack-reuse=none%> option");
1197 opts->x_flag_stack_reuse = SR_NONE;
1200 if ((opts->x_flag_sanitize & SANITIZE_USER_ADDRESS) && opts->x_flag_tm)
1201 sorry ("transactional memory is not supported with %<-fsanitize=address%>");
1203 if ((opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS) && opts->x_flag_tm)
1204 sorry ("transactional memory is not supported with "
1205 "%<-fsanitize=kernel-address%>");
1207 /* Currently live patching is not support for LTO. */
1208 if (opts->x_flag_live_patching && opts->x_flag_lto)
1209 sorry ("live patching is not supported with LTO");
1211 /* Control IPA optimizations based on different -flive-patching level. */
1212 if (opts->x_flag_live_patching)
1214 control_options_for_live_patching (opts, opts_set,
1215 opts->x_flag_live_patching,
1216 loc);
1220 #define LEFT_COLUMN 27
1222 /* Output ITEM, of length ITEM_WIDTH, in the left column,
1223 followed by word-wrapped HELP in a second column. */
1224 static void
1225 wrap_help (const char *help,
1226 const char *item,
1227 unsigned int item_width,
1228 unsigned int columns)
1230 unsigned int col_width = LEFT_COLUMN;
1231 unsigned int remaining, room, len;
1233 remaining = strlen (help);
1237 room = columns - 3 - MAX (col_width, item_width);
1238 if (room > columns)
1239 room = 0;
1240 len = remaining;
1242 if (room < len)
1244 unsigned int i;
1246 for (i = 0; help[i]; i++)
1248 if (i >= room && len != remaining)
1249 break;
1250 if (help[i] == ' ')
1251 len = i;
1252 else if ((help[i] == '-' || help[i] == '/')
1253 && help[i + 1] != ' '
1254 && i > 0 && ISALPHA (help[i - 1]))
1255 len = i + 1;
1259 printf (" %-*.*s %.*s\n", col_width, item_width, item, len, help);
1260 item_width = 0;
1261 while (help[len] == ' ')
1262 len++;
1263 help += len;
1264 remaining -= len;
1266 while (remaining);
1269 /* Data structure used to print list of valid option values. */
1271 struct option_help_tuple
1273 option_help_tuple (int code, vec<const char *> values):
1274 m_code (code), m_values (values)
1277 /* Code of an option. */
1278 int m_code;
1280 /* List of possible values. */
1281 vec<const char *> m_values;
1284 /* Print help for a specific front-end, etc. */
1285 static void
1286 print_filtered_help (unsigned int include_flags,
1287 unsigned int exclude_flags,
1288 unsigned int any_flags,
1289 unsigned int columns,
1290 struct gcc_options *opts,
1291 unsigned int lang_mask)
1293 unsigned int i;
1294 const char *help;
1295 bool found = false;
1296 bool displayed = false;
1297 char new_help[256];
1299 if (include_flags == CL_PARAMS)
1301 for (i = 0; i < LAST_PARAM; i++)
1303 const char *param = compiler_params[i].option;
1305 help = compiler_params[i].help;
1306 if (help == NULL || *help == '\0')
1308 if (exclude_flags & CL_UNDOCUMENTED)
1309 continue;
1310 help = undocumented_msg;
1313 /* Get the translation. */
1314 help = _(help);
1316 if (!opts->x_quiet_flag)
1318 snprintf (new_help, sizeof (new_help),
1319 _("default %d minimum %d maximum %d"),
1320 compiler_params[i].default_value,
1321 compiler_params[i].min_value,
1322 compiler_params[i].max_value);
1323 help = new_help;
1325 wrap_help (help, param, strlen (param), columns);
1327 putchar ('\n');
1328 return;
1331 if (!opts->x_help_printed)
1332 opts->x_help_printed = XCNEWVAR (char, cl_options_count);
1334 if (!opts->x_help_enum_printed)
1335 opts->x_help_enum_printed = XCNEWVAR (char, cl_enums_count);
1337 auto_vec<option_help_tuple> help_tuples;
1339 for (i = 0; i < cl_options_count; i++)
1341 const struct cl_option *option = cl_options + i;
1342 unsigned int len;
1343 const char *opt;
1344 const char *tab;
1346 if (include_flags == 0
1347 || ((option->flags & include_flags) != include_flags))
1349 if ((option->flags & any_flags) == 0)
1350 continue;
1353 /* Skip unwanted switches. */
1354 if ((option->flags & exclude_flags) != 0)
1355 continue;
1357 /* The driver currently prints its own help text. */
1358 if ((option->flags & CL_DRIVER) != 0
1359 && (option->flags & (((1U << cl_lang_count) - 1)
1360 | CL_COMMON | CL_TARGET)) == 0)
1361 continue;
1363 found = true;
1364 /* Skip switches that have already been printed. */
1365 if (opts->x_help_printed[i])
1366 continue;
1368 opts->x_help_printed[i] = true;
1370 help = option->help;
1371 if (help == NULL)
1373 if (exclude_flags & CL_UNDOCUMENTED)
1374 continue;
1376 help = undocumented_msg;
1379 if (option->alias_target < N_OPTS
1380 && cl_options [option->alias_target].help)
1382 if (help == undocumented_msg)
1384 /* For undocumented options that are aliases for other options
1385 that are documented, point the reader to the other option in
1386 preference of the former. */
1387 snprintf (new_help, sizeof new_help,
1388 _("Same as %s. Use the latter option instead."),
1389 cl_options [option->alias_target].opt_text);
1391 else
1393 /* For documented options with aliases, mention the aliased
1394 option's name for reference. */
1395 snprintf (new_help, sizeof new_help,
1396 _("%s Same as %s."),
1397 help, cl_options [option->alias_target].opt_text);
1400 help = new_help;
1403 if (option->warn_message)
1405 /* Mention that the use of the option will trigger a warning. */
1406 if (help == new_help)
1407 snprintf (new_help + strlen (new_help),
1408 sizeof new_help - strlen (new_help),
1409 " %s", _(use_diagnosed_msg));
1410 else
1411 snprintf (new_help, sizeof new_help,
1412 "%s %s", help, _(use_diagnosed_msg));
1414 help = new_help;
1417 /* Get the translation. */
1418 help = _(help);
1420 /* Find the gap between the name of the
1421 option and its descriptive text. */
1422 tab = strchr (help, '\t');
1423 if (tab)
1425 len = tab - help;
1426 opt = help;
1427 help = tab + 1;
1429 else
1431 opt = option->opt_text;
1432 len = strlen (opt);
1435 /* With the -Q option enabled we change the descriptive text associated
1436 with an option to be an indication of its current setting. */
1437 if (!opts->x_quiet_flag)
1439 void *flag_var = option_flag_var (i, opts);
1441 if (len < (LEFT_COLUMN + 2))
1442 strcpy (new_help, "\t\t");
1443 else
1444 strcpy (new_help, "\t");
1446 if (flag_var != NULL
1447 && option->var_type != CLVC_DEFER)
1449 if (option->flags & CL_JOINED)
1451 if (option->var_type == CLVC_STRING)
1453 if (* (const char **) flag_var != NULL)
1454 snprintf (new_help + strlen (new_help),
1455 sizeof (new_help) - strlen (new_help),
1456 "%s", * (const char **) flag_var);
1458 else if (option->var_type == CLVC_ENUM)
1460 const struct cl_enum *e = &cl_enums[option->var_enum];
1461 int value;
1462 const char *arg = NULL;
1464 value = e->get (flag_var);
1465 enum_value_to_arg (e->values, &arg, value, lang_mask);
1466 if (arg == NULL)
1467 arg = _("[default]");
1468 snprintf (new_help + strlen (new_help),
1469 sizeof (new_help) - strlen (new_help),
1470 "%s", arg);
1472 else
1473 sprintf (new_help + strlen (new_help),
1474 "%d", * (int *) flag_var);
1476 else
1477 strcat (new_help, option_enabled (i, opts)
1478 ? _("[enabled]") : _("[disabled]"));
1481 help = new_help;
1484 if (option->range_max != -1)
1486 char b[128];
1487 snprintf (b, sizeof (b), "<%d,%d>", option->range_min,
1488 option->range_max);
1489 opt = concat (opt, b, NULL);
1490 len += strlen (b);
1493 wrap_help (help, opt, len, columns);
1494 displayed = true;
1496 if (option->var_type == CLVC_ENUM
1497 && opts->x_help_enum_printed[option->var_enum] != 2)
1498 opts->x_help_enum_printed[option->var_enum] = 1;
1499 else
1501 vec<const char *> option_values
1502 = targetm_common.get_valid_option_values (i, NULL);
1503 if (!option_values.is_empty ())
1504 help_tuples.safe_push (option_help_tuple (i, option_values));
1508 if (! found)
1510 unsigned int langs = include_flags & CL_LANG_ALL;
1512 if (langs == 0)
1513 printf (_(" No options with the desired characteristics were found\n"));
1514 else
1516 unsigned int i;
1518 /* PR 31349: Tell the user how to see all of the
1519 options supported by a specific front end. */
1520 for (i = 0; (1U << i) < CL_LANG_ALL; i ++)
1521 if ((1U << i) & langs)
1522 printf (_(" None found. Use --help=%s to show *all* the options supported by the %s front-end.\n"),
1523 lang_names[i], lang_names[i]);
1527 else if (! displayed)
1528 printf (_(" All options with the desired characteristics have already been displayed\n"));
1530 putchar ('\n');
1532 /* Print details of enumerated option arguments, if those
1533 enumerations have help text headings provided. If no help text
1534 is provided, presume that the possible values are listed in the
1535 help text for the relevant options. */
1536 for (i = 0; i < cl_enums_count; i++)
1538 unsigned int j, pos;
1540 if (opts->x_help_enum_printed[i] != 1)
1541 continue;
1542 if (cl_enums[i].help == NULL)
1543 continue;
1544 printf (" %s\n ", _(cl_enums[i].help));
1545 pos = 4;
1546 for (j = 0; cl_enums[i].values[j].arg != NULL; j++)
1548 unsigned int len = strlen (cl_enums[i].values[j].arg);
1550 if (pos > 4 && pos + 1 + len <= columns)
1552 printf (" %s", cl_enums[i].values[j].arg);
1553 pos += 1 + len;
1555 else
1557 if (pos > 4)
1559 printf ("\n ");
1560 pos = 4;
1562 printf ("%s", cl_enums[i].values[j].arg);
1563 pos += len;
1566 printf ("\n\n");
1567 opts->x_help_enum_printed[i] = 2;
1570 for (unsigned i = 0; i < help_tuples.length (); i++)
1572 const struct cl_option *option = cl_options + help_tuples[i].m_code;
1573 printf (" Known valid arguments for %s option:\n ", option->opt_text);
1574 for (unsigned j = 0; j < help_tuples[i].m_values.length (); j++)
1575 printf (" %s", help_tuples[i].m_values[j]);
1576 printf ("\n\n");
1580 /* Display help for a specified type of option.
1581 The options must have ALL of the INCLUDE_FLAGS set
1582 ANY of the flags in the ANY_FLAGS set
1583 and NONE of the EXCLUDE_FLAGS set. The current option state is in
1584 OPTS; LANG_MASK is used for interpreting enumerated option state. */
1585 static void
1586 print_specific_help (unsigned int include_flags,
1587 unsigned int exclude_flags,
1588 unsigned int any_flags,
1589 struct gcc_options *opts,
1590 unsigned int lang_mask)
1592 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
1593 const char * description = NULL;
1594 const char * descrip_extra = "";
1595 size_t i;
1596 unsigned int flag;
1598 /* Sanity check: Make sure that we do not have more
1599 languages than we have bits available to enumerate them. */
1600 gcc_assert ((1U << cl_lang_count) <= CL_MIN_OPTION_CLASS);
1602 /* If we have not done so already, obtain
1603 the desired maximum width of the output. */
1604 if (opts->x_help_columns == 0)
1606 opts->x_help_columns = get_terminal_width ();
1607 if (opts->x_help_columns == INT_MAX)
1608 /* Use a reasonable default. */
1609 opts->x_help_columns = 80;
1612 /* Decide upon the title for the options that we are going to display. */
1613 for (i = 0, flag = 1; flag <= CL_MAX_OPTION_CLASS; flag <<= 1, i ++)
1615 switch (flag & include_flags)
1617 case 0:
1618 case CL_DRIVER:
1619 break;
1621 case CL_TARGET:
1622 description = _("The following options are target specific");
1623 break;
1624 case CL_WARNING:
1625 description = _("The following options control compiler warning messages");
1626 break;
1627 case CL_OPTIMIZATION:
1628 description = _("The following options control optimizations");
1629 break;
1630 case CL_COMMON:
1631 description = _("The following options are language-independent");
1632 break;
1633 case CL_PARAMS:
1634 description = _("The --param option recognizes the following as parameters");
1635 break;
1636 default:
1637 if (i >= cl_lang_count)
1638 break;
1639 if (exclude_flags & all_langs_mask)
1640 description = _("The following options are specific to just the language ");
1641 else
1642 description = _("The following options are supported by the language ");
1643 descrip_extra = lang_names [i];
1644 break;
1648 if (description == NULL)
1650 if (any_flags == 0)
1652 if (include_flags & CL_UNDOCUMENTED)
1653 description = _("The following options are not documented");
1654 else if (include_flags & CL_SEPARATE)
1655 description = _("The following options take separate arguments");
1656 else if (include_flags & CL_JOINED)
1657 description = _("The following options take joined arguments");
1658 else
1660 internal_error ("unrecognized include_flags 0x%x passed to print_specific_help",
1661 include_flags);
1662 return;
1665 else
1667 if (any_flags & all_langs_mask)
1668 description = _("The following options are language-related");
1669 else
1670 description = _("The following options are language-independent");
1674 printf ("%s%s:\n", description, descrip_extra);
1675 print_filtered_help (include_flags, exclude_flags, any_flags,
1676 opts->x_help_columns, opts, lang_mask);
1679 /* Enable FDO-related flags. */
1681 static void
1682 enable_fdo_optimizations (struct gcc_options *opts,
1683 struct gcc_options *opts_set,
1684 int value)
1686 if (!opts_set->x_flag_branch_probabilities)
1687 opts->x_flag_branch_probabilities = value;
1688 if (!opts_set->x_flag_profile_values)
1689 opts->x_flag_profile_values = value;
1690 if (!opts_set->x_flag_unroll_loops)
1691 opts->x_flag_unroll_loops = value;
1692 if (!opts_set->x_flag_peel_loops)
1693 opts->x_flag_peel_loops = value;
1694 if (!opts_set->x_flag_tracer)
1695 opts->x_flag_tracer = value;
1696 if (!opts_set->x_flag_value_profile_transformations)
1697 opts->x_flag_value_profile_transformations = value;
1698 if (!opts_set->x_flag_inline_functions)
1699 opts->x_flag_inline_functions = value;
1700 if (!opts_set->x_flag_ipa_cp)
1701 opts->x_flag_ipa_cp = value;
1702 if (!opts_set->x_flag_ipa_cp_clone
1703 && value && opts->x_flag_ipa_cp)
1704 opts->x_flag_ipa_cp_clone = value;
1705 if (!opts_set->x_flag_ipa_bit_cp
1706 && value && opts->x_flag_ipa_cp)
1707 opts->x_flag_ipa_bit_cp = value;
1708 if (!opts_set->x_flag_predictive_commoning)
1709 opts->x_flag_predictive_commoning = value;
1710 if (!opts_set->x_flag_split_loops)
1711 opts->x_flag_split_loops = value;
1712 if (!opts_set->x_flag_unswitch_loops)
1713 opts->x_flag_unswitch_loops = value;
1714 if (!opts_set->x_flag_gcse_after_reload)
1715 opts->x_flag_gcse_after_reload = value;
1716 if (!opts_set->x_flag_tree_loop_vectorize)
1717 opts->x_flag_tree_loop_vectorize = value;
1718 if (!opts_set->x_flag_tree_slp_vectorize)
1719 opts->x_flag_tree_slp_vectorize = value;
1720 if (!opts_set->x_flag_version_loops_for_strides)
1721 opts->x_flag_version_loops_for_strides = value;
1722 if (!opts_set->x_flag_vect_cost_model)
1723 opts->x_flag_vect_cost_model = VECT_COST_MODEL_DYNAMIC;
1724 if (!opts_set->x_flag_tree_loop_distribute_patterns)
1725 opts->x_flag_tree_loop_distribute_patterns = value;
1726 if (!opts_set->x_flag_loop_interchange)
1727 opts->x_flag_loop_interchange = value;
1728 if (!opts_set->x_flag_unroll_jam)
1729 opts->x_flag_unroll_jam = value;
1730 if (!opts_set->x_flag_tree_loop_distribution)
1731 opts->x_flag_tree_loop_distribution = value;
1734 /* -f{,no-}sanitize{,-recover}= suboptions. */
1735 const struct sanitizer_opts_s sanitizer_opts[] =
1737 #define SANITIZER_OPT(name, flags, recover) \
1738 { #name, flags, sizeof #name - 1, recover }
1739 SANITIZER_OPT (address, (SANITIZE_ADDRESS | SANITIZE_USER_ADDRESS), true),
1740 SANITIZER_OPT (kernel-address, (SANITIZE_ADDRESS | SANITIZE_KERNEL_ADDRESS),
1741 true),
1742 SANITIZER_OPT (pointer-compare, SANITIZE_POINTER_COMPARE, true),
1743 SANITIZER_OPT (pointer-subtract, SANITIZE_POINTER_SUBTRACT, true),
1744 SANITIZER_OPT (thread, SANITIZE_THREAD, false),
1745 SANITIZER_OPT (leak, SANITIZE_LEAK, false),
1746 SANITIZER_OPT (shift, SANITIZE_SHIFT, true),
1747 SANITIZER_OPT (shift-base, SANITIZE_SHIFT_BASE, true),
1748 SANITIZER_OPT (shift-exponent, SANITIZE_SHIFT_EXPONENT, true),
1749 SANITIZER_OPT (integer-divide-by-zero, SANITIZE_DIVIDE, true),
1750 SANITIZER_OPT (undefined, SANITIZE_UNDEFINED, true),
1751 SANITIZER_OPT (unreachable, SANITIZE_UNREACHABLE, false),
1752 SANITIZER_OPT (vla-bound, SANITIZE_VLA, true),
1753 SANITIZER_OPT (return, SANITIZE_RETURN, false),
1754 SANITIZER_OPT (null, SANITIZE_NULL, true),
1755 SANITIZER_OPT (signed-integer-overflow, SANITIZE_SI_OVERFLOW, true),
1756 SANITIZER_OPT (bool, SANITIZE_BOOL, true),
1757 SANITIZER_OPT (enum, SANITIZE_ENUM, true),
1758 SANITIZER_OPT (float-divide-by-zero, SANITIZE_FLOAT_DIVIDE, true),
1759 SANITIZER_OPT (float-cast-overflow, SANITIZE_FLOAT_CAST, true),
1760 SANITIZER_OPT (bounds, SANITIZE_BOUNDS, true),
1761 SANITIZER_OPT (bounds-strict, SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT, true),
1762 SANITIZER_OPT (alignment, SANITIZE_ALIGNMENT, true),
1763 SANITIZER_OPT (nonnull-attribute, SANITIZE_NONNULL_ATTRIBUTE, true),
1764 SANITIZER_OPT (returns-nonnull-attribute, SANITIZE_RETURNS_NONNULL_ATTRIBUTE,
1765 true),
1766 SANITIZER_OPT (object-size, SANITIZE_OBJECT_SIZE, true),
1767 SANITIZER_OPT (vptr, SANITIZE_VPTR, true),
1768 SANITIZER_OPT (pointer-overflow, SANITIZE_POINTER_OVERFLOW, true),
1769 SANITIZER_OPT (builtin, SANITIZE_BUILTIN, true),
1770 SANITIZER_OPT (all, ~0U, true),
1771 #undef SANITIZER_OPT
1772 { NULL, 0U, 0UL, false }
1775 /* -f{,no-}sanitize-coverage= suboptions. */
1776 const struct sanitizer_opts_s coverage_sanitizer_opts[] =
1778 #define COVERAGE_SANITIZER_OPT(name, flags) \
1779 { #name, flags, sizeof #name - 1, true }
1780 COVERAGE_SANITIZER_OPT (trace-pc, SANITIZE_COV_TRACE_PC),
1781 COVERAGE_SANITIZER_OPT (trace-cmp, SANITIZE_COV_TRACE_CMP),
1782 #undef COVERAGE_SANITIZER_OPT
1783 { NULL, 0U, 0UL, false }
1786 /* A struct for describing a run of chars within a string. */
1788 struct string_fragment
1790 string_fragment (const char *start, size_t len)
1791 : m_start (start), m_len (len) {}
1793 const char *m_start;
1794 size_t m_len;
1797 /* Specialization of edit_distance_traits for string_fragment,
1798 for use by get_closest_sanitizer_option. */
1800 template <>
1801 struct edit_distance_traits<const string_fragment &>
1803 static size_t get_length (const string_fragment &fragment)
1805 return fragment.m_len;
1808 static const char *get_string (const string_fragment &fragment)
1810 return fragment.m_start;
1814 /* Given ARG, an unrecognized sanitizer option, return the best
1815 matching sanitizer option, or NULL if there isn't one.
1816 OPTS is array of candidate sanitizer options.
1817 CODE is OPT_fsanitize_, OPT_fsanitize_recover_ or
1818 OPT_fsanitize_coverage_.
1819 VALUE is non-zero for the regular form of the option, zero
1820 for the "no-" form (e.g. "-fno-sanitize-recover="). */
1822 static const char *
1823 get_closest_sanitizer_option (const string_fragment &arg,
1824 const struct sanitizer_opts_s *opts,
1825 enum opt_code code, int value)
1827 best_match <const string_fragment &, const char*> bm (arg);
1828 for (int i = 0; opts[i].name != NULL; ++i)
1830 /* -fsanitize=all is not valid, so don't offer it. */
1831 if (code == OPT_fsanitize_
1832 && opts[i].flag == ~0U
1833 && value)
1834 continue;
1836 /* For -fsanitize-recover= (and not -fno-sanitize-recover=),
1837 don't offer the non-recoverable options. */
1838 if (code == OPT_fsanitize_recover_
1839 && !opts[i].can_recover
1840 && value)
1841 continue;
1843 bm.consider (opts[i].name);
1845 return bm.get_best_meaningful_candidate ();
1848 /* Parse comma separated sanitizer suboptions from P for option SCODE,
1849 adjust previous FLAGS and return new ones. If COMPLAIN is false,
1850 don't issue diagnostics. */
1852 unsigned int
1853 parse_sanitizer_options (const char *p, location_t loc, int scode,
1854 unsigned int flags, int value, bool complain)
1856 enum opt_code code = (enum opt_code) scode;
1858 const struct sanitizer_opts_s *opts;
1859 if (code == OPT_fsanitize_coverage_)
1860 opts = coverage_sanitizer_opts;
1861 else
1862 opts = sanitizer_opts;
1864 while (*p != 0)
1866 size_t len, i;
1867 bool found = false;
1868 const char *comma = strchr (p, ',');
1870 if (comma == NULL)
1871 len = strlen (p);
1872 else
1873 len = comma - p;
1874 if (len == 0)
1876 p = comma + 1;
1877 continue;
1880 /* Check to see if the string matches an option class name. */
1881 for (i = 0; opts[i].name != NULL; ++i)
1882 if (len == opts[i].len && memcmp (p, opts[i].name, len) == 0)
1884 /* Handle both -fsanitize and -fno-sanitize cases. */
1885 if (value && opts[i].flag == ~0U)
1887 if (code == OPT_fsanitize_)
1889 if (complain)
1890 error_at (loc, "%<-fsanitize=all%> option is not valid");
1892 else
1893 flags |= ~(SANITIZE_THREAD | SANITIZE_LEAK
1894 | SANITIZE_UNREACHABLE | SANITIZE_RETURN);
1896 else if (value)
1898 /* Do not enable -fsanitize-recover=unreachable and
1899 -fsanitize-recover=return if -fsanitize-recover=undefined
1900 is selected. */
1901 if (code == OPT_fsanitize_recover_
1902 && opts[i].flag == SANITIZE_UNDEFINED)
1903 flags |= (SANITIZE_UNDEFINED
1904 & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN));
1905 else
1906 flags |= opts[i].flag;
1908 else
1909 flags &= ~opts[i].flag;
1910 found = true;
1911 break;
1914 if (! found && complain)
1916 const char *hint
1917 = get_closest_sanitizer_option (string_fragment (p, len),
1918 opts, code, value);
1920 const char *suffix;
1921 if (code == OPT_fsanitize_recover_)
1922 suffix = "-recover";
1923 else if (code == OPT_fsanitize_coverage_)
1924 suffix = "-coverage";
1925 else
1926 suffix = "";
1928 if (hint)
1929 error_at (loc,
1930 "unrecognized argument to %<-f%ssanitize%s=%> "
1931 "option: %q.*s; did you mean %qs?",
1932 value ? "" : "no-",
1933 suffix, (int) len, p, hint);
1934 else
1935 error_at (loc,
1936 "unrecognized argument to %<-f%ssanitize%s=%> option: "
1937 "%q.*s", value ? "" : "no-",
1938 suffix, (int) len, p);
1941 if (comma == NULL)
1942 break;
1943 p = comma + 1;
1945 return flags;
1948 /* Parse string values of no_sanitize attribute passed in VALUE.
1949 Values are separated with comma. */
1951 unsigned int
1952 parse_no_sanitize_attribute (char *value)
1954 unsigned int flags = 0;
1955 unsigned int i;
1956 char *q = strtok (value, ",");
1958 while (q != NULL)
1960 for (i = 0; sanitizer_opts[i].name != NULL; ++i)
1961 if (strcmp (sanitizer_opts[i].name, q) == 0)
1963 flags |= sanitizer_opts[i].flag;
1964 if (sanitizer_opts[i].flag == SANITIZE_UNDEFINED)
1965 flags |= SANITIZE_UNDEFINED_NONDEFAULT;
1966 break;
1969 if (sanitizer_opts[i].name == NULL)
1970 warning (OPT_Wattributes,
1971 "%<%s%> attribute directive ignored", q);
1973 q = strtok (NULL, ",");
1976 return flags;
1979 /* Parse -falign-NAME format for a FLAG value. Return individual
1980 parsed integer values into RESULT_VALUES array. If REPORT_ERROR is
1981 set, print error message at LOC location. */
1983 bool
1984 parse_and_check_align_values (const char *flag,
1985 const char *name,
1986 auto_vec<unsigned> &result_values,
1987 bool report_error,
1988 location_t loc)
1990 char *str = xstrdup (flag);
1991 for (char *p = strtok (str, ":"); p; p = strtok (NULL, ":"))
1993 char *end;
1994 int v = strtol (p, &end, 10);
1995 if (*end != '\0' || v < 0)
1997 if (report_error)
1998 error_at (loc, "invalid arguments for %<-falign-%s%> option: %qs",
1999 name, flag);
2001 return false;
2004 result_values.safe_push ((unsigned)v);
2007 free (str);
2009 /* Check that we have a correct number of values. */
2010 #ifdef SUBALIGN_LOG
2011 unsigned max_valid_values = 4;
2012 #else
2013 unsigned max_valid_values = 2;
2014 #endif
2016 if (result_values.is_empty ()
2017 || result_values.length () > max_valid_values)
2019 if (report_error)
2020 error_at (loc, "invalid number of arguments for %<-falign-%s%> "
2021 "option: %qs", name, flag);
2022 return false;
2025 for (unsigned i = 0; i < result_values.length (); i++)
2026 if (result_values[i] > MAX_CODE_ALIGN_VALUE)
2028 if (report_error)
2029 error_at (loc, "%<-falign-%s%> is not between 0 and %d",
2030 name, MAX_CODE_ALIGN_VALUE);
2031 return false;
2034 return true;
2037 /* Check that alignment value FLAG for -falign-NAME is valid at a given
2038 location LOC. */
2040 static void
2041 check_alignment_argument (location_t loc, const char *flag, const char *name)
2043 auto_vec<unsigned> align_result;
2044 parse_and_check_align_values (flag, name, align_result, true, loc);
2047 /* Handle target- and language-independent options. Return zero to
2048 generate an "unknown option" message. Only options that need
2049 extra handling need to be listed here; if you simply want
2050 DECODED->value assigned to a variable, it happens automatically. */
2052 bool
2053 common_handle_option (struct gcc_options *opts,
2054 struct gcc_options *opts_set,
2055 const struct cl_decoded_option *decoded,
2056 unsigned int lang_mask, int kind ATTRIBUTE_UNUSED,
2057 location_t loc,
2058 const struct cl_option_handlers *handlers,
2059 diagnostic_context *dc,
2060 void (*target_option_override_hook) (void))
2062 size_t scode = decoded->opt_index;
2063 const char *arg = decoded->arg;
2064 HOST_WIDE_INT value = decoded->value;
2065 enum opt_code code = (enum opt_code) scode;
2067 gcc_assert (decoded->canonical_option_num_elements <= 2);
2069 switch (code)
2071 case OPT__param:
2072 handle_param (opts, opts_set, loc, arg);
2073 break;
2075 case OPT__help:
2077 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
2078 unsigned int undoc_mask;
2079 unsigned int i;
2081 if (lang_mask == CL_DRIVER)
2082 break;
2084 undoc_mask = ((opts->x_verbose_flag | opts->x_extra_warnings)
2086 : CL_UNDOCUMENTED);
2087 target_option_override_hook ();
2088 /* First display any single language specific options. */
2089 for (i = 0; i < cl_lang_count; i++)
2090 print_specific_help
2091 (1U << i, (all_langs_mask & (~ (1U << i))) | undoc_mask, 0, opts,
2092 lang_mask);
2093 /* Next display any multi language specific options. */
2094 print_specific_help (0, undoc_mask, all_langs_mask, opts, lang_mask);
2095 /* Then display any remaining, non-language options. */
2096 for (i = CL_MIN_OPTION_CLASS; i <= CL_MAX_OPTION_CLASS; i <<= 1)
2097 if (i != CL_DRIVER)
2098 print_specific_help (i, undoc_mask, 0, opts, lang_mask);
2099 opts->x_exit_after_options = true;
2100 break;
2103 case OPT__target_help:
2104 if (lang_mask == CL_DRIVER)
2105 break;
2107 target_option_override_hook ();
2108 print_specific_help (CL_TARGET, CL_UNDOCUMENTED, 0, opts, lang_mask);
2109 opts->x_exit_after_options = true;
2110 break;
2112 case OPT__help_:
2114 const char *a = arg;
2115 unsigned int include_flags = 0;
2116 /* Note - by default we include undocumented options when listing
2117 specific classes. If you only want to see documented options
2118 then add ",^undocumented" to the --help= option. E.g.:
2120 --help=target,^undocumented */
2121 unsigned int exclude_flags = 0;
2123 if (lang_mask == CL_DRIVER)
2124 break;
2126 /* Walk along the argument string, parsing each word in turn.
2127 The format is:
2128 arg = [^]{word}[,{arg}]
2129 word = {optimizers|target|warnings|undocumented|
2130 params|common|<language>} */
2131 while (*a != 0)
2133 static const struct
2135 const char *string;
2136 unsigned int flag;
2138 specifics[] =
2140 { "optimizers", CL_OPTIMIZATION },
2141 { "target", CL_TARGET },
2142 { "warnings", CL_WARNING },
2143 { "undocumented", CL_UNDOCUMENTED },
2144 { "params", CL_PARAMS },
2145 { "joined", CL_JOINED },
2146 { "separate", CL_SEPARATE },
2147 { "common", CL_COMMON },
2148 { NULL, 0 }
2150 unsigned int *pflags;
2151 const char *comma;
2152 unsigned int lang_flag, specific_flag;
2153 unsigned int len;
2154 unsigned int i;
2156 if (*a == '^')
2158 ++a;
2159 if (*a == '\0')
2161 error_at (loc, "missing argument to %qs", "--help=^");
2162 break;
2164 pflags = &exclude_flags;
2166 else
2167 pflags = &include_flags;
2169 comma = strchr (a, ',');
2170 if (comma == NULL)
2171 len = strlen (a);
2172 else
2173 len = comma - a;
2174 if (len == 0)
2176 a = comma + 1;
2177 continue;
2180 /* Check to see if the string matches an option class name. */
2181 for (i = 0, specific_flag = 0; specifics[i].string != NULL; i++)
2182 if (strncasecmp (a, specifics[i].string, len) == 0)
2184 specific_flag = specifics[i].flag;
2185 break;
2188 /* Check to see if the string matches a language name.
2189 Note - we rely upon the alpha-sorted nature of the entries in
2190 the lang_names array, specifically that shorter names appear
2191 before their longer variants. (i.e. C before C++). That way
2192 when we are attempting to match --help=c for example we will
2193 match with C first and not C++. */
2194 for (i = 0, lang_flag = 0; i < cl_lang_count; i++)
2195 if (strncasecmp (a, lang_names[i], len) == 0)
2197 lang_flag = 1U << i;
2198 break;
2201 if (specific_flag != 0)
2203 if (lang_flag == 0)
2204 *pflags |= specific_flag;
2205 else
2207 /* The option's argument matches both the start of a
2208 language name and the start of an option class name.
2209 We have a special case for when the user has
2210 specified "--help=c", but otherwise we have to issue
2211 a warning. */
2212 if (strncasecmp (a, "c", len) == 0)
2213 *pflags |= lang_flag;
2214 else
2215 warning_at (loc, 0,
2216 "--help argument %q.*s is ambiguous, "
2217 "please be more specific",
2218 len, a);
2221 else if (lang_flag != 0)
2222 *pflags |= lang_flag;
2223 else
2224 warning_at (loc, 0,
2225 "unrecognized argument to --help= option: %q.*s",
2226 len, a);
2228 if (comma == NULL)
2229 break;
2230 a = comma + 1;
2233 if (include_flags)
2235 target_option_override_hook ();
2236 print_specific_help (include_flags, exclude_flags, 0, opts,
2237 lang_mask);
2239 opts->x_exit_after_options = true;
2240 break;
2243 case OPT__version:
2244 if (lang_mask == CL_DRIVER)
2245 break;
2247 opts->x_exit_after_options = true;
2248 break;
2250 case OPT__completion_:
2251 break;
2253 case OPT_fsanitize_:
2254 opts->x_flag_sanitize
2255 = parse_sanitizer_options (arg, loc, code,
2256 opts->x_flag_sanitize, value, true);
2258 /* Kernel ASan implies normal ASan but does not yet support
2259 all features. */
2260 if (opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS)
2262 maybe_set_param_value (PARAM_ASAN_INSTRUMENTATION_WITH_CALL_THRESHOLD,
2263 0, opts->x_param_values,
2264 opts_set->x_param_values);
2265 maybe_set_param_value (PARAM_ASAN_GLOBALS, 0, opts->x_param_values,
2266 opts_set->x_param_values);
2267 maybe_set_param_value (PARAM_ASAN_STACK, 0, opts->x_param_values,
2268 opts_set->x_param_values);
2269 maybe_set_param_value (PARAM_ASAN_PROTECT_ALLOCAS, 0,
2270 opts->x_param_values,
2271 opts_set->x_param_values);
2272 maybe_set_param_value (PARAM_ASAN_USE_AFTER_RETURN, 0,
2273 opts->x_param_values,
2274 opts_set->x_param_values);
2276 break;
2278 case OPT_fsanitize_recover_:
2279 opts->x_flag_sanitize_recover
2280 = parse_sanitizer_options (arg, loc, code,
2281 opts->x_flag_sanitize_recover, value, true);
2282 break;
2284 case OPT_fasan_shadow_offset_:
2285 /* Deferred. */
2286 break;
2288 case OPT_fsanitize_address_use_after_scope:
2289 opts->x_flag_sanitize_address_use_after_scope = value;
2290 break;
2292 case OPT_fsanitize_recover:
2293 if (value)
2294 opts->x_flag_sanitize_recover
2295 |= (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT)
2296 & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN);
2297 else
2298 opts->x_flag_sanitize_recover
2299 &= ~(SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2300 break;
2302 case OPT_fsanitize_coverage_:
2303 opts->x_flag_sanitize_coverage
2304 = parse_sanitizer_options (arg, loc, code,
2305 opts->x_flag_sanitize_coverage, value, true);
2306 break;
2308 case OPT_O:
2309 case OPT_Os:
2310 case OPT_Ofast:
2311 case OPT_Og:
2312 /* Currently handled in a prescan. */
2313 break;
2315 case OPT_Werror:
2316 dc->warning_as_error_requested = value;
2317 break;
2319 case OPT_Werror_:
2320 if (lang_mask == CL_DRIVER)
2321 break;
2323 enable_warning_as_error (arg, value, lang_mask, handlers,
2324 opts, opts_set, loc, dc);
2325 break;
2327 case OPT_Wfatal_errors:
2328 dc->fatal_errors = value;
2329 break;
2331 case OPT_Wstack_usage_:
2332 opts->x_flag_stack_usage_info = value != -1;
2333 break;
2335 case OPT_Wstrict_aliasing:
2336 set_Wstrict_aliasing (opts, value);
2337 break;
2339 case OPT_Wstrict_overflow:
2340 opts->x_warn_strict_overflow = (value
2341 ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
2342 : 0);
2343 break;
2345 case OPT_Wsystem_headers:
2346 dc->dc_warn_system_headers = value;
2347 break;
2349 case OPT_aux_info:
2350 opts->x_flag_gen_aux_info = 1;
2351 break;
2353 case OPT_auxbase_strip:
2355 char *tmp = xstrdup (arg);
2356 strip_off_ending (tmp, strlen (tmp));
2357 if (tmp[0])
2358 opts->x_aux_base_name = tmp;
2359 else
2360 free (tmp);
2362 break;
2364 case OPT_d:
2365 decode_d_option (arg, opts, loc, dc);
2366 break;
2368 case OPT_fcall_used_:
2369 case OPT_fcall_saved_:
2370 /* Deferred. */
2371 break;
2373 case OPT_fdbg_cnt_:
2374 /* Deferred. */
2375 break;
2377 case OPT_fdbg_cnt_list:
2378 /* Deferred. */
2379 opts->x_exit_after_options = true;
2380 break;
2382 case OPT_fdebug_prefix_map_:
2383 case OPT_ffile_prefix_map_:
2384 /* Deferred. */
2385 break;
2387 case OPT_fdiagnostics_show_location_:
2388 diagnostic_prefixing_rule (dc) = (diagnostic_prefixing_rule_t) value;
2389 break;
2391 case OPT_fdiagnostics_show_caret:
2392 dc->show_caret = value;
2393 break;
2395 case OPT_fdiagnostics_show_labels:
2396 dc->show_labels_p = value;
2397 break;
2399 case OPT_fdiagnostics_show_line_numbers:
2400 dc->show_line_numbers_p = value;
2401 break;
2403 case OPT_fdiagnostics_color_:
2404 diagnostic_color_init (dc, value);
2405 break;
2407 case OPT_fdiagnostics_format_:
2408 diagnostic_output_format_init (dc,
2409 (enum diagnostics_output_format)value);
2410 break;
2412 case OPT_fdiagnostics_parseable_fixits:
2413 dc->parseable_fixits_p = value;
2414 break;
2416 case OPT_fdiagnostics_show_option:
2417 dc->show_option_requested = value;
2418 break;
2420 case OPT_fdiagnostics_minimum_margin_width_:
2421 dc->min_margin_width = value;
2422 break;
2424 case OPT_fdump_:
2425 /* Deferred. */
2426 break;
2428 case OPT_ffast_math:
2429 set_fast_math_flags (opts, value);
2430 break;
2432 case OPT_funsafe_math_optimizations:
2433 set_unsafe_math_optimizations_flags (opts, value);
2434 break;
2436 case OPT_ffixed_:
2437 /* Deferred. */
2438 break;
2440 case OPT_finline_limit_:
2441 set_param_value ("max-inline-insns-single", value / 2,
2442 opts->x_param_values, opts_set->x_param_values);
2443 set_param_value ("max-inline-insns-auto", value / 2,
2444 opts->x_param_values, opts_set->x_param_values);
2445 break;
2447 case OPT_finstrument_functions_exclude_function_list_:
2448 add_comma_separated_to_vector
2449 (&opts->x_flag_instrument_functions_exclude_functions, arg);
2450 break;
2452 case OPT_finstrument_functions_exclude_file_list_:
2453 add_comma_separated_to_vector
2454 (&opts->x_flag_instrument_functions_exclude_files, arg);
2455 break;
2457 case OPT_fmessage_length_:
2458 pp_set_line_maximum_length (dc->printer, value);
2459 diagnostic_set_caret_max_width (dc, value);
2460 break;
2462 case OPT_fopt_info:
2463 case OPT_fopt_info_:
2464 /* Deferred. */
2465 break;
2467 case OPT_foffload_:
2469 const char *p = arg;
2470 opts->x_flag_disable_hsa = true;
2471 while (*p != 0)
2473 const char *comma = strchr (p, ',');
2475 if ((strncmp (p, "disable", 7) == 0)
2476 && (p[7] == ',' || p[7] == '\0'))
2478 opts->x_flag_disable_hsa = true;
2479 break;
2482 if ((strncmp (p, "hsa", 3) == 0)
2483 && (p[3] == ',' || p[3] == '\0'))
2485 #ifdef ENABLE_HSA
2486 opts->x_flag_disable_hsa = false;
2487 #else
2488 sorry ("HSA has not been enabled during configuration");
2489 #endif
2491 if (!comma)
2492 break;
2493 p = comma + 1;
2495 break;
2498 #ifndef ACCEL_COMPILER
2499 case OPT_foffload_abi_:
2500 error_at (loc, "%<-foffload-abi%> option can be specified only for "
2501 "offload compiler");
2502 break;
2503 #endif
2505 case OPT_fpack_struct_:
2506 if (value <= 0 || (value & (value - 1)) || value > 16)
2507 error_at (loc,
2508 "structure alignment must be a small power of two, not %wu",
2509 value);
2510 else
2511 opts->x_initial_max_fld_align = value;
2512 break;
2514 case OPT_fplugin_:
2515 case OPT_fplugin_arg_:
2516 /* Deferred. */
2517 break;
2519 case OPT_fprofile_use_:
2520 opts->x_profile_data_prefix = xstrdup (arg);
2521 opts->x_flag_profile_use = true;
2522 value = true;
2523 /* No break here - do -fprofile-use processing. */
2524 /* FALLTHRU */
2525 case OPT_fprofile_use:
2526 enable_fdo_optimizations (opts, opts_set, value);
2527 if (!opts_set->x_flag_profile_reorder_functions)
2528 opts->x_flag_profile_reorder_functions = value;
2529 /* Indirect call profiling should do all useful transformations
2530 speculative devirtualization does. */
2531 if (!opts_set->x_flag_devirtualize_speculatively
2532 && opts->x_flag_value_profile_transformations)
2533 opts->x_flag_devirtualize_speculatively = false;
2534 break;
2536 case OPT_fauto_profile_:
2537 opts->x_auto_profile_file = xstrdup (arg);
2538 opts->x_flag_auto_profile = true;
2539 value = true;
2540 /* No break here - do -fauto-profile processing. */
2541 /* FALLTHRU */
2542 case OPT_fauto_profile:
2543 enable_fdo_optimizations (opts, opts_set, value);
2544 if (!opts_set->x_flag_profile_correction)
2545 opts->x_flag_profile_correction = value;
2546 maybe_set_param_value (
2547 PARAM_EARLY_INLINER_MAX_ITERATIONS, 10,
2548 opts->x_param_values, opts_set->x_param_values);
2549 break;
2551 case OPT_fprofile_generate_:
2552 opts->x_profile_data_prefix = xstrdup (arg);
2553 value = true;
2554 /* No break here - do -fprofile-generate processing. */
2555 /* FALLTHRU */
2556 case OPT_fprofile_generate:
2557 if (!opts_set->x_profile_arc_flag)
2558 opts->x_profile_arc_flag = value;
2559 if (!opts_set->x_flag_profile_values)
2560 opts->x_flag_profile_values = value;
2561 if (!opts_set->x_flag_inline_functions)
2562 opts->x_flag_inline_functions = value;
2563 if (!opts_set->x_flag_ipa_bit_cp)
2564 opts->x_flag_ipa_bit_cp = value;
2565 /* FIXME: Instrumentation we insert makes ipa-reference bitmaps
2566 quadratic. Disable the pass until better memory representation
2567 is done. */
2568 if (!opts_set->x_flag_ipa_reference)
2569 opts->x_flag_ipa_reference = false;
2570 break;
2572 case OPT_fpatchable_function_entry_:
2574 char *patch_area_arg = xstrdup (arg);
2575 char *comma = strchr (patch_area_arg, ',');
2576 if (comma)
2578 *comma = '\0';
2579 function_entry_patch_area_size =
2580 integral_argument (patch_area_arg);
2581 function_entry_patch_area_start =
2582 integral_argument (comma + 1);
2584 else
2586 function_entry_patch_area_size =
2587 integral_argument (patch_area_arg);
2588 function_entry_patch_area_start = 0;
2590 if (function_entry_patch_area_size < 0
2591 || function_entry_patch_area_start < 0
2592 || function_entry_patch_area_size
2593 < function_entry_patch_area_start)
2594 error ("invalid arguments for %<-fpatchable_function_entry%>");
2595 free (patch_area_arg);
2597 break;
2599 case OPT_ftree_vectorize:
2600 /* Automatically sets -ftree-loop-vectorize and
2601 -ftree-slp-vectorize. Nothing more to do here. */
2602 break;
2603 case OPT_fshow_column:
2604 dc->show_column = value;
2605 break;
2607 case OPT_frandom_seed:
2608 /* The real switch is -fno-random-seed. */
2609 if (value)
2610 return false;
2611 /* Deferred. */
2612 break;
2614 case OPT_frandom_seed_:
2615 /* Deferred. */
2616 break;
2618 case OPT_fsched_verbose_:
2619 #ifdef INSN_SCHEDULING
2620 /* Handled with Var in common.opt. */
2621 break;
2622 #else
2623 return false;
2624 #endif
2626 case OPT_fsched_stalled_insns_:
2627 opts->x_flag_sched_stalled_insns = value;
2628 if (opts->x_flag_sched_stalled_insns == 0)
2629 opts->x_flag_sched_stalled_insns = -1;
2630 break;
2632 case OPT_fsched_stalled_insns_dep_:
2633 opts->x_flag_sched_stalled_insns_dep = value;
2634 break;
2636 case OPT_fstack_check_:
2637 if (!strcmp (arg, "no"))
2638 opts->x_flag_stack_check = NO_STACK_CHECK;
2639 else if (!strcmp (arg, "generic"))
2640 /* This is the old stack checking method. */
2641 opts->x_flag_stack_check = STACK_CHECK_BUILTIN
2642 ? FULL_BUILTIN_STACK_CHECK
2643 : GENERIC_STACK_CHECK;
2644 else if (!strcmp (arg, "specific"))
2645 /* This is the new stack checking method. */
2646 opts->x_flag_stack_check = STACK_CHECK_BUILTIN
2647 ? FULL_BUILTIN_STACK_CHECK
2648 : STACK_CHECK_STATIC_BUILTIN
2649 ? STATIC_BUILTIN_STACK_CHECK
2650 : GENERIC_STACK_CHECK;
2651 else
2652 warning_at (loc, 0, "unknown stack check parameter %qs", arg);
2653 break;
2655 case OPT_fstack_limit:
2656 /* The real switch is -fno-stack-limit. */
2657 if (value)
2658 return false;
2659 /* Deferred. */
2660 break;
2662 case OPT_fstack_limit_register_:
2663 case OPT_fstack_limit_symbol_:
2664 /* Deferred. */
2665 break;
2667 case OPT_fstack_usage:
2668 opts->x_flag_stack_usage = value;
2669 opts->x_flag_stack_usage_info = value != 0;
2670 break;
2672 case OPT_g:
2673 set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg, opts, opts_set,
2674 loc);
2675 break;
2677 case OPT_gdwarf:
2678 if (arg && strlen (arg) != 0)
2680 error_at (loc, "%<-gdwarf%s%> is ambiguous; "
2681 "use %<-gdwarf-%s%> for DWARF version "
2682 "or %<-gdwarf%> %<-g%s%> for debug level", arg, arg, arg);
2683 break;
2685 else
2686 value = opts->x_dwarf_version;
2688 /* FALLTHRU */
2689 case OPT_gdwarf_:
2690 if (value < 2 || value > 5)
2691 error_at (loc, "dwarf version %wu is not supported", value);
2692 else
2693 opts->x_dwarf_version = value;
2694 set_debug_level (DWARF2_DEBUG, false, "", opts, opts_set, loc);
2695 break;
2697 case OPT_gsplit_dwarf:
2698 set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, "", opts, opts_set,
2699 loc);
2700 break;
2702 case OPT_ggdb:
2703 set_debug_level (NO_DEBUG, 2, arg, opts, opts_set, loc);
2704 break;
2706 case OPT_gstabs:
2707 case OPT_gstabs_:
2708 set_debug_level (DBX_DEBUG, code == OPT_gstabs_, arg, opts, opts_set,
2709 loc);
2710 break;
2712 case OPT_gvms:
2713 set_debug_level (VMS_DEBUG, false, arg, opts, opts_set, loc);
2714 break;
2716 case OPT_gxcoff:
2717 case OPT_gxcoff_:
2718 set_debug_level (XCOFF_DEBUG, code == OPT_gxcoff_, arg, opts, opts_set,
2719 loc);
2720 break;
2722 case OPT_gz:
2723 case OPT_gz_:
2724 /* Handled completely via specs. */
2725 break;
2727 case OPT_pedantic_errors:
2728 dc->pedantic_errors = 1;
2729 control_warning_option (OPT_Wpedantic, DK_ERROR, NULL, value,
2730 loc, lang_mask,
2731 handlers, opts, opts_set,
2732 dc);
2733 break;
2735 case OPT_flto:
2736 opts->x_flag_lto = value ? "" : NULL;
2737 break;
2739 case OPT_w:
2740 dc->dc_inhibit_warnings = true;
2741 break;
2743 case OPT_fmax_errors_:
2744 dc->max_errors = value;
2745 break;
2747 case OPT_fuse_ld_bfd:
2748 case OPT_fuse_ld_gold:
2749 case OPT_fuse_ld_lld:
2750 case OPT_fuse_linker_plugin:
2751 /* No-op. Used by the driver and passed to us because it starts with f.*/
2752 break;
2754 case OPT_fwrapv:
2755 if (value)
2756 opts->x_flag_trapv = 0;
2757 break;
2759 case OPT_ftrapv:
2760 if (value)
2761 opts->x_flag_wrapv = 0;
2762 break;
2764 case OPT_fstrict_overflow:
2765 opts->x_flag_wrapv = !value;
2766 opts->x_flag_wrapv_pointer = !value;
2767 if (!value)
2768 opts->x_flag_trapv = 0;
2769 break;
2771 case OPT_fipa_icf:
2772 opts->x_flag_ipa_icf_functions = value;
2773 opts->x_flag_ipa_icf_variables = value;
2774 break;
2776 case OPT_falign_loops_:
2777 check_alignment_argument (loc, arg, "loops");
2778 break;
2780 case OPT_falign_jumps_:
2781 check_alignment_argument (loc, arg, "jumps");
2782 break;
2784 case OPT_falign_labels_:
2785 check_alignment_argument (loc, arg, "labels");
2786 break;
2788 case OPT_falign_functions_:
2789 check_alignment_argument (loc, arg, "functions");
2790 break;
2792 default:
2793 /* If the flag was handled in a standard way, assume the lack of
2794 processing here is intentional. */
2795 gcc_assert (option_flag_var (scode, opts));
2796 break;
2799 common_handle_option_auto (opts, opts_set, decoded, lang_mask, kind,
2800 loc, handlers, dc);
2801 return true;
2804 /* Handle --param NAME=VALUE. */
2805 static void
2806 handle_param (struct gcc_options *opts, struct gcc_options *opts_set,
2807 location_t loc, const char *carg)
2809 char *equal, *arg;
2810 int value;
2812 arg = xstrdup (carg);
2813 equal = strchr (arg, '=');
2814 if (!equal)
2815 error_at (loc, "%s: --param arguments should be of the form NAME=VALUE",
2816 arg);
2817 else
2819 *equal = '\0';
2821 enum compiler_param index;
2822 if (!find_param (arg, &index))
2824 const char *suggestion = find_param_fuzzy (arg);
2825 if (suggestion)
2826 error_at (loc, "invalid --param name %qs; did you mean %qs?",
2827 arg, suggestion);
2828 else
2829 error_at (loc, "invalid --param name %qs", arg);
2831 else
2833 if (!param_string_value_p (index, equal + 1, &value))
2834 value = integral_argument (equal + 1);
2836 if (value == -1)
2837 error_at (loc, "invalid --param value %qs", equal + 1);
2838 else
2839 set_param_value (arg, value,
2840 opts->x_param_values, opts_set->x_param_values);
2844 free (arg);
2847 /* Used to set the level of strict aliasing warnings in OPTS,
2848 when no level is specified (i.e., when -Wstrict-aliasing, and not
2849 -Wstrict-aliasing=level was given).
2850 ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified,
2851 and 0 otherwise. After calling this function, wstrict_aliasing will be
2852 set to the default value of -Wstrict_aliasing=level, currently 3. */
2853 static void
2854 set_Wstrict_aliasing (struct gcc_options *opts, int onoff)
2856 gcc_assert (onoff == 0 || onoff == 1);
2857 if (onoff != 0)
2858 opts->x_warn_strict_aliasing = 3;
2859 else
2860 opts->x_warn_strict_aliasing = 0;
2863 /* The following routines are useful in setting all the flags that
2864 -ffast-math and -fno-fast-math imply. */
2865 static void
2866 set_fast_math_flags (struct gcc_options *opts, int set)
2868 if (!opts->frontend_set_flag_unsafe_math_optimizations)
2870 opts->x_flag_unsafe_math_optimizations = set;
2871 set_unsafe_math_optimizations_flags (opts, set);
2873 if (!opts->frontend_set_flag_finite_math_only)
2874 opts->x_flag_finite_math_only = set;
2875 if (!opts->frontend_set_flag_errno_math)
2876 opts->x_flag_errno_math = !set;
2877 if (set)
2879 if (opts->frontend_set_flag_excess_precision_cmdline
2880 == EXCESS_PRECISION_DEFAULT)
2881 opts->x_flag_excess_precision_cmdline
2882 = set ? EXCESS_PRECISION_FAST : EXCESS_PRECISION_DEFAULT;
2883 if (!opts->frontend_set_flag_signaling_nans)
2884 opts->x_flag_signaling_nans = 0;
2885 if (!opts->frontend_set_flag_rounding_math)
2886 opts->x_flag_rounding_math = 0;
2887 if (!opts->frontend_set_flag_cx_limited_range)
2888 opts->x_flag_cx_limited_range = 1;
2892 /* When -funsafe-math-optimizations is set the following
2893 flags are set as well. */
2894 static void
2895 set_unsafe_math_optimizations_flags (struct gcc_options *opts, int set)
2897 if (!opts->frontend_set_flag_trapping_math)
2898 opts->x_flag_trapping_math = !set;
2899 if (!opts->frontend_set_flag_signed_zeros)
2900 opts->x_flag_signed_zeros = !set;
2901 if (!opts->frontend_set_flag_associative_math)
2902 opts->x_flag_associative_math = set;
2903 if (!opts->frontend_set_flag_reciprocal_math)
2904 opts->x_flag_reciprocal_math = set;
2907 /* Return true iff flags in OPTS are set as if -ffast-math. */
2908 bool
2909 fast_math_flags_set_p (const struct gcc_options *opts)
2911 return (!opts->x_flag_trapping_math
2912 && opts->x_flag_unsafe_math_optimizations
2913 && opts->x_flag_finite_math_only
2914 && !opts->x_flag_signed_zeros
2915 && !opts->x_flag_errno_math
2916 && opts->x_flag_excess_precision_cmdline
2917 == EXCESS_PRECISION_FAST);
2920 /* Return true iff flags are set as if -ffast-math but using the flags stored
2921 in the struct cl_optimization structure. */
2922 bool
2923 fast_math_flags_struct_set_p (struct cl_optimization *opt)
2925 return (!opt->x_flag_trapping_math
2926 && opt->x_flag_unsafe_math_optimizations
2927 && opt->x_flag_finite_math_only
2928 && !opt->x_flag_signed_zeros
2929 && !opt->x_flag_errno_math);
2932 /* Handle a debug output -g switch for options OPTS
2933 (OPTS_SET->x_write_symbols storing whether a debug type was passed
2934 explicitly), location LOC. EXTENDED is true or false to support
2935 extended output (2 is special and means "-ggdb" was given). */
2936 static void
2937 set_debug_level (enum debug_info_type type, int extended, const char *arg,
2938 struct gcc_options *opts, struct gcc_options *opts_set,
2939 location_t loc)
2941 opts->x_use_gnu_debug_info_extensions = extended;
2943 if (type == NO_DEBUG)
2945 if (opts->x_write_symbols == NO_DEBUG)
2947 opts->x_write_symbols = PREFERRED_DEBUGGING_TYPE;
2949 if (extended == 2)
2951 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_LINENO_DEBUGGING_INFO
2952 opts->x_write_symbols = DWARF2_DEBUG;
2953 #elif defined DBX_DEBUGGING_INFO
2954 opts->x_write_symbols = DBX_DEBUG;
2955 #endif
2958 if (opts->x_write_symbols == NO_DEBUG)
2959 warning_at (loc, 0, "target system does not support debug output");
2962 else
2964 /* Does it conflict with an already selected type? */
2965 if (opts_set->x_write_symbols != NO_DEBUG
2966 && opts->x_write_symbols != NO_DEBUG
2967 && type != opts->x_write_symbols)
2968 error_at (loc, "debug format %qs conflicts with prior selection",
2969 debug_type_names[type]);
2970 opts->x_write_symbols = type;
2971 opts_set->x_write_symbols = type;
2974 /* A debug flag without a level defaults to level 2.
2975 If off or at level 1, set it to level 2, but if already
2976 at level 3, don't lower it. */
2977 if (*arg == '\0')
2979 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
2980 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
2982 else
2984 int argval = integral_argument (arg);
2985 if (argval == -1)
2986 error_at (loc, "unrecognized debug output level %qs", arg);
2987 else if (argval > 3)
2988 error_at (loc, "debug output level %qs is too high", arg);
2989 else
2990 opts->x_debug_info_level = (enum debug_info_levels) argval;
2994 /* Arrange to dump core on error for diagnostic context DC. (The
2995 regular error message is still printed first, except in the case of
2996 abort ().) */
2998 static void
2999 setup_core_dumping (diagnostic_context *dc)
3001 #ifdef SIGABRT
3002 signal (SIGABRT, SIG_DFL);
3003 #endif
3004 #if defined(HAVE_SETRLIMIT)
3006 struct rlimit rlim;
3007 if (getrlimit (RLIMIT_CORE, &rlim) != 0)
3008 fatal_error (input_location, "getting core file size maximum limit: %m");
3009 rlim.rlim_cur = rlim.rlim_max;
3010 if (setrlimit (RLIMIT_CORE, &rlim) != 0)
3011 fatal_error (input_location,
3012 "setting core file size limit to maximum: %m");
3014 #endif
3015 diagnostic_abort_on_error (dc);
3018 /* Parse a -d<ARG> command line switch for OPTS, location LOC,
3019 diagnostic context DC. */
3021 static void
3022 decode_d_option (const char *arg, struct gcc_options *opts,
3023 location_t loc, diagnostic_context *dc)
3025 int c;
3027 while (*arg)
3028 switch (c = *arg++)
3030 case 'A':
3031 opts->x_flag_debug_asm = 1;
3032 break;
3033 case 'p':
3034 opts->x_flag_print_asm_name = 1;
3035 break;
3036 case 'P':
3037 opts->x_flag_dump_rtl_in_asm = 1;
3038 opts->x_flag_print_asm_name = 1;
3039 break;
3040 case 'x':
3041 opts->x_rtl_dump_and_exit = 1;
3042 break;
3043 case 'D': /* These are handled by the preprocessor. */
3044 case 'I':
3045 case 'M':
3046 case 'N':
3047 case 'U':
3048 break;
3049 case 'H':
3050 setup_core_dumping (dc);
3051 break;
3052 case 'a':
3053 opts->x_flag_dump_all_passed = true;
3054 break;
3056 default:
3057 warning_at (loc, 0, "unrecognized gcc debugging option: %c", c);
3058 break;
3062 /* Enable (or disable if VALUE is 0) a warning option ARG (language
3063 mask LANG_MASK, option handlers HANDLERS) as an error for option
3064 structures OPTS and OPTS_SET, diagnostic context DC (possibly
3065 NULL), location LOC. This is used by -Werror=. */
3067 static void
3068 enable_warning_as_error (const char *arg, int value, unsigned int lang_mask,
3069 const struct cl_option_handlers *handlers,
3070 struct gcc_options *opts,
3071 struct gcc_options *opts_set,
3072 location_t loc, diagnostic_context *dc)
3074 char *new_option;
3075 int option_index;
3077 new_option = XNEWVEC (char, strlen (arg) + 2);
3078 new_option[0] = 'W';
3079 strcpy (new_option + 1, arg);
3080 option_index = find_opt (new_option, lang_mask);
3081 if (option_index == OPT_SPECIAL_unknown)
3082 error_at (loc, "%<-Werror=%s%>: no option -%s", arg, new_option);
3083 else if (!(cl_options[option_index].flags & CL_WARNING))
3084 error_at (loc, "%<-Werror=%s%>: -%s is not an option that controls "
3085 "warnings", arg, new_option);
3086 else
3088 const diagnostic_t kind = value ? DK_ERROR : DK_WARNING;
3089 const char *arg = NULL;
3091 if (cl_options[option_index].flags & CL_JOINED)
3092 arg = new_option + cl_options[option_index].opt_len;
3093 control_warning_option (option_index, (int) kind, arg, value,
3094 loc, lang_mask,
3095 handlers, opts, opts_set, dc);
3097 free (new_option);
3100 /* Return malloced memory for the name of the option OPTION_INDEX
3101 which enabled a diagnostic (context CONTEXT), originally of type
3102 ORIG_DIAG_KIND but possibly converted to DIAG_KIND by options such
3103 as -Werror. */
3105 char *
3106 option_name (diagnostic_context *context, int option_index,
3107 diagnostic_t orig_diag_kind, diagnostic_t diag_kind)
3109 if (option_index)
3111 /* A warning classified as an error. */
3112 if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN)
3113 && diag_kind == DK_ERROR)
3114 return concat (cl_options[OPT_Werror_].opt_text,
3115 /* Skip over "-W". */
3116 cl_options[option_index].opt_text + 2,
3117 NULL);
3118 /* A warning with option. */
3119 else
3120 return xstrdup (cl_options[option_index].opt_text);
3122 /* A warning without option classified as an error. */
3123 else if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN
3124 || diag_kind == DK_WARNING)
3125 && context->warning_as_error_requested)
3126 return xstrdup (cl_options[OPT_Werror].opt_text);
3127 else
3128 return NULL;