d: Add testcase from PR108962
[official-gcc.git] / gcc / opts.cc
blobac81d4e42944d5daf3b27fb334a6c4b531459f30
1 /* Command line option handling.
2 Copyright (C) 2002-2023 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 "diagnostic.h"
29 #include "opts-diagnostic.h"
30 #include "insn-attr-common.h"
31 #include "common/common-target.h"
32 #include "spellcheck.h"
33 #include "opt-suggestions.h"
34 #include "diagnostic-color.h"
35 #include "version.h"
36 #include "selftest.h"
37 #include "file-prefix-map.h"
38 #include "diagnostic-text-art.h"
40 /* In this file all option sets are explicit. */
41 #undef OPTION_SET_P
43 /* Set by -fcanon-prefix-map. */
44 bool flag_canon_prefix_map;
46 static void set_Wstrict_aliasing (struct gcc_options *opts, int onoff);
48 /* Names of fundamental debug info formats indexed by enum
49 debug_info_type. */
51 const char *const debug_type_names[] =
53 "none", "stabs", "dwarf-2", "xcoff", "vms", "ctf", "btf"
56 /* Bitmasks of fundamental debug info formats indexed by enum
57 debug_info_type. */
59 static uint32_t debug_type_masks[] =
61 NO_DEBUG, DWARF2_DEBUG, VMS_DEBUG,
62 CTF_DEBUG, BTF_DEBUG
65 /* Names of the set of debug formats requested by user. Updated and accessed
66 via debug_set_names. */
68 static char df_set_names[sizeof "none stabs dwarf-2 xcoff vms ctf btf"];
70 /* Get enum debug_info_type of the specified debug format, for error messages.
71 Can be used only for individual debug format types. */
73 enum debug_info_type
74 debug_set_to_format (uint32_t debug_info_set)
76 int idx = 0;
77 enum debug_info_type dinfo_type = DINFO_TYPE_NONE;
78 /* Find first set bit. */
79 if (debug_info_set)
80 idx = exact_log2 (debug_info_set & - debug_info_set);
81 /* Check that only one bit is set, if at all. This function is meant to be
82 used only for vanilla debug_info_set bitmask values, i.e. for individual
83 debug format types upto DINFO_TYPE_MAX. */
84 gcc_assert ((debug_info_set & (debug_info_set - 1)) == 0);
85 dinfo_type = (enum debug_info_type)idx;
86 gcc_assert (dinfo_type <= DINFO_TYPE_MAX);
87 return dinfo_type;
90 /* Get the number of debug formats enabled for output. */
92 unsigned int
93 debug_set_count (uint32_t w_symbols)
95 unsigned int count = 0;
96 while (w_symbols)
98 ++ count;
99 w_symbols &= ~ (w_symbols & - w_symbols);
101 return count;
104 /* Get the names of the debug formats enabled for output. */
106 const char *
107 debug_set_names (uint32_t w_symbols)
109 uint32_t df_mask = 0;
110 /* Reset the string to be returned. */
111 memset (df_set_names, 0, sizeof (df_set_names));
112 /* Get the popcount. */
113 int num_set_df = debug_set_count (w_symbols);
114 /* Iterate over the debug formats. Add name string for those enabled. */
115 for (int i = DINFO_TYPE_NONE; i <= DINFO_TYPE_MAX; i++)
117 df_mask = debug_type_masks[i];
118 if (w_symbols & df_mask)
120 strcat (df_set_names, debug_type_names[i]);
121 num_set_df--;
122 if (num_set_df)
123 strcat (df_set_names, " ");
124 else
125 break;
127 else if (!w_symbols)
129 /* No debug formats enabled. */
130 gcc_assert (i == DINFO_TYPE_NONE);
131 strcat (df_set_names, debug_type_names[i]);
132 break;
135 return df_set_names;
138 /* Return TRUE iff BTF debug info is enabled. */
140 bool
141 btf_debuginfo_p ()
143 return (write_symbols & BTF_DEBUG);
146 /* Return TRUE iff BTF with CO-RE debug info is enabled. */
148 bool
149 btf_with_core_debuginfo_p ()
151 return (write_symbols & BTF_WITH_CORE_DEBUG);
154 /* Return TRUE iff CTF debug info is enabled. */
156 bool
157 ctf_debuginfo_p ()
159 return (write_symbols & CTF_DEBUG);
162 /* Return TRUE iff dwarf2 debug info is enabled. */
164 bool
165 dwarf_debuginfo_p (struct gcc_options *opts)
167 return (opts->x_write_symbols & DWARF2_DEBUG);
170 /* Return true iff the debug info format is to be generated based on DWARF
171 DIEs (like CTF and BTF debug info formats). */
173 bool dwarf_based_debuginfo_p ()
175 return ((write_symbols & CTF_DEBUG)
176 || (write_symbols & BTF_DEBUG));
179 /* All flag uses below need to explicitely reference the option sets
180 to operate on. */
181 #define global_options DO_NOT_USE
182 #define global_options_set DO_NOT_USE
184 /* Parse the -femit-struct-debug-detailed option value
185 and set the flag variables. */
187 #define MATCH( prefix, string ) \
188 ((strncmp (prefix, string, sizeof prefix - 1) == 0) \
189 ? ((string += sizeof prefix - 1), 1) : 0)
191 void
192 set_struct_debug_option (struct gcc_options *opts, location_t loc,
193 const char *spec)
195 /* various labels for comparison */
196 static const char dfn_lbl[] = "dfn:", dir_lbl[] = "dir:", ind_lbl[] = "ind:";
197 static const char ord_lbl[] = "ord:", gen_lbl[] = "gen:";
198 static const char none_lbl[] = "none", any_lbl[] = "any";
199 static const char base_lbl[] = "base", sys_lbl[] = "sys";
201 enum debug_struct_file files = DINFO_STRUCT_FILE_ANY;
202 /* Default is to apply to as much as possible. */
203 enum debug_info_usage usage = DINFO_USAGE_NUM_ENUMS;
204 int ord = 1, gen = 1;
206 /* What usage? */
207 if (MATCH (dfn_lbl, spec))
208 usage = DINFO_USAGE_DFN;
209 else if (MATCH (dir_lbl, spec))
210 usage = DINFO_USAGE_DIR_USE;
211 else if (MATCH (ind_lbl, spec))
212 usage = DINFO_USAGE_IND_USE;
214 /* Generics or not? */
215 if (MATCH (ord_lbl, spec))
216 gen = 0;
217 else if (MATCH (gen_lbl, spec))
218 ord = 0;
220 /* What allowable environment? */
221 if (MATCH (none_lbl, spec))
222 files = DINFO_STRUCT_FILE_NONE;
223 else if (MATCH (any_lbl, spec))
224 files = DINFO_STRUCT_FILE_ANY;
225 else if (MATCH (sys_lbl, spec))
226 files = DINFO_STRUCT_FILE_SYS;
227 else if (MATCH (base_lbl, spec))
228 files = DINFO_STRUCT_FILE_BASE;
229 else
230 error_at (loc,
231 "argument %qs to %<-femit-struct-debug-detailed%> "
232 "not recognized",
233 spec);
235 /* Effect the specification. */
236 if (usage == DINFO_USAGE_NUM_ENUMS)
238 if (ord)
240 opts->x_debug_struct_ordinary[DINFO_USAGE_DFN] = files;
241 opts->x_debug_struct_ordinary[DINFO_USAGE_DIR_USE] = files;
242 opts->x_debug_struct_ordinary[DINFO_USAGE_IND_USE] = files;
244 if (gen)
246 opts->x_debug_struct_generic[DINFO_USAGE_DFN] = files;
247 opts->x_debug_struct_generic[DINFO_USAGE_DIR_USE] = files;
248 opts->x_debug_struct_generic[DINFO_USAGE_IND_USE] = files;
251 else
253 if (ord)
254 opts->x_debug_struct_ordinary[usage] = files;
255 if (gen)
256 opts->x_debug_struct_generic[usage] = files;
259 if (*spec == ',')
260 set_struct_debug_option (opts, loc, spec+1);
261 else
263 /* No more -femit-struct-debug-detailed specifications.
264 Do final checks. */
265 if (*spec != '\0')
266 error_at (loc,
267 "argument %qs to %<-femit-struct-debug-detailed%> unknown",
268 spec);
269 if (opts->x_debug_struct_ordinary[DINFO_USAGE_DIR_USE]
270 < opts->x_debug_struct_ordinary[DINFO_USAGE_IND_USE]
271 || opts->x_debug_struct_generic[DINFO_USAGE_DIR_USE]
272 < opts->x_debug_struct_generic[DINFO_USAGE_IND_USE])
273 error_at (loc,
274 "%<-femit-struct-debug-detailed=dir:...%> must allow "
275 "at least as much as "
276 "%<-femit-struct-debug-detailed=ind:...%>");
280 /* Strip off a legitimate source ending from the input string NAME of
281 length LEN. Rather than having to know the names used by all of
282 our front ends, we strip off an ending of a period followed by
283 up to fource characters. (C++ uses ".cpp".) */
285 void
286 strip_off_ending (char *name, int len)
288 int i;
289 for (i = 2; i < 5 && len > i; i++)
291 if (name[len - i] == '.')
293 name[len - i] = '\0';
294 break;
299 /* Find the base name of a path, stripping off both directories and
300 a single final extension. */
302 base_of_path (const char *path, const char **base_out)
304 const char *base = path;
305 const char *dot = 0;
306 const char *p = path;
307 char c = *p;
308 while (c)
310 if (IS_DIR_SEPARATOR (c))
312 base = p + 1;
313 dot = 0;
315 else if (c == '.')
316 dot = p;
317 c = *++p;
319 if (!dot)
320 dot = p;
321 *base_out = base;
322 return dot - base;
325 /* What to print when a switch has no documentation. */
326 static const char undocumented_msg[] = N_("This option lacks documentation.");
327 static const char use_diagnosed_msg[] = N_("Uses of this option are diagnosed.");
329 typedef char *char_p; /* For DEF_VEC_P. */
331 static void set_debug_level (uint32_t dinfo, int extended,
332 const char *arg, struct gcc_options *opts,
333 struct gcc_options *opts_set,
334 location_t loc);
335 static void set_fast_math_flags (struct gcc_options *opts, int set);
336 static void decode_d_option (const char *arg, struct gcc_options *opts,
337 location_t loc, diagnostic_context *dc);
338 static void set_unsafe_math_optimizations_flags (struct gcc_options *opts,
339 int set);
340 static void enable_warning_as_error (const char *arg, int value,
341 unsigned int lang_mask,
342 const struct cl_option_handlers *handlers,
343 struct gcc_options *opts,
344 struct gcc_options *opts_set,
345 location_t loc,
346 diagnostic_context *dc);
348 /* Handle a back-end option; arguments and return value as for
349 handle_option. */
351 bool
352 target_handle_option (struct gcc_options *opts,
353 struct gcc_options *opts_set,
354 const struct cl_decoded_option *decoded,
355 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
356 location_t loc,
357 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
358 diagnostic_context *dc, void (*) (void))
360 gcc_assert (dc == global_dc);
361 gcc_assert (kind == DK_UNSPECIFIED);
362 return targetm_common.handle_option (opts, opts_set, decoded, loc);
365 /* Add comma-separated strings to a char_p vector. */
367 static void
368 add_comma_separated_to_vector (void **pvec, const char *arg)
370 char *tmp;
371 char *r;
372 char *w;
373 char *token_start;
374 vec<char_p> *v = (vec<char_p> *) *pvec;
376 vec_check_alloc (v, 1);
378 /* We never free this string. */
379 tmp = xstrdup (arg);
381 r = tmp;
382 w = tmp;
383 token_start = tmp;
385 while (*r != '\0')
387 if (*r == ',')
389 *w++ = '\0';
390 ++r;
391 v->safe_push (token_start);
392 token_start = w;
394 if (*r == '\\' && r[1] == ',')
396 *w++ = ',';
397 r += 2;
399 else
400 *w++ = *r++;
403 *w = '\0';
404 if (*token_start != '\0')
405 v->safe_push (token_start);
407 *pvec = v;
410 /* Initialize opts_obstack. */
412 void
413 init_opts_obstack (void)
415 gcc_obstack_init (&opts_obstack);
418 /* Initialize OPTS and OPTS_SET before using them in parsing options. */
420 void
421 init_options_struct (struct gcc_options *opts, struct gcc_options *opts_set)
423 /* Ensure that opts_obstack has already been initialized by the time
424 that we initialize any gcc_options instances (PR jit/68446). */
425 gcc_assert (opts_obstack.chunk_size > 0);
427 *opts = global_options_init;
429 if (opts_set)
430 memset (opts_set, 0, sizeof (*opts_set));
432 /* Initialize whether `char' is signed. */
433 opts->x_flag_signed_char = DEFAULT_SIGNED_CHAR;
434 /* Set this to a special "uninitialized" value. The actual default
435 is set after target options have been processed. */
436 opts->x_flag_short_enums = 2;
438 /* Initialize target_flags before default_options_optimization
439 so the latter can modify it. */
440 opts->x_target_flags = targetm_common.default_target_flags;
442 /* Some targets have ABI-specified unwind tables. */
443 opts->x_flag_unwind_tables = targetm_common.unwind_tables_default;
445 /* Some targets have other target-specific initialization. */
446 targetm_common.option_init_struct (opts);
449 /* If indicated by the optimization level LEVEL (-Os if SIZE is set,
450 -Ofast if FAST is set, -Og if DEBUG is set), apply the option DEFAULT_OPT
451 to OPTS and OPTS_SET, diagnostic context DC, location LOC, with language
452 mask LANG_MASK and option handlers HANDLERS. */
454 static void
455 maybe_default_option (struct gcc_options *opts,
456 struct gcc_options *opts_set,
457 const struct default_options *default_opt,
458 int level, bool size, bool fast, bool debug,
459 unsigned int lang_mask,
460 const struct cl_option_handlers *handlers,
461 location_t loc,
462 diagnostic_context *dc)
464 const struct cl_option *option = &cl_options[default_opt->opt_index];
465 bool enabled;
467 if (size)
468 gcc_assert (level == 2);
469 if (fast)
470 gcc_assert (level == 3);
471 if (debug)
472 gcc_assert (level == 1);
474 switch (default_opt->levels)
476 case OPT_LEVELS_ALL:
477 enabled = true;
478 break;
480 case OPT_LEVELS_0_ONLY:
481 enabled = (level == 0);
482 break;
484 case OPT_LEVELS_1_PLUS:
485 enabled = (level >= 1);
486 break;
488 case OPT_LEVELS_1_PLUS_SPEED_ONLY:
489 enabled = (level >= 1 && !size && !debug);
490 break;
492 case OPT_LEVELS_1_PLUS_NOT_DEBUG:
493 enabled = (level >= 1 && !debug);
494 break;
496 case OPT_LEVELS_2_PLUS:
497 enabled = (level >= 2);
498 break;
500 case OPT_LEVELS_2_PLUS_SPEED_ONLY:
501 enabled = (level >= 2 && !size && !debug);
502 break;
504 case OPT_LEVELS_3_PLUS:
505 enabled = (level >= 3);
506 break;
508 case OPT_LEVELS_3_PLUS_AND_SIZE:
509 enabled = (level >= 3 || size);
510 break;
512 case OPT_LEVELS_SIZE:
513 enabled = size;
514 break;
516 case OPT_LEVELS_FAST:
517 enabled = fast;
518 break;
520 case OPT_LEVELS_NONE:
521 default:
522 gcc_unreachable ();
525 if (enabled)
526 handle_generated_option (opts, opts_set, default_opt->opt_index,
527 default_opt->arg, default_opt->value,
528 lang_mask, DK_UNSPECIFIED, loc,
529 handlers, true, dc);
530 else if (default_opt->arg == NULL
531 && !option->cl_reject_negative
532 && !(option->flags & CL_PARAMS))
533 handle_generated_option (opts, opts_set, default_opt->opt_index,
534 default_opt->arg, !default_opt->value,
535 lang_mask, DK_UNSPECIFIED, loc,
536 handlers, true, dc);
539 /* As indicated by the optimization level LEVEL (-Os if SIZE is set,
540 -Ofast if FAST is set), apply the options in array DEFAULT_OPTS to
541 OPTS and OPTS_SET, diagnostic context DC, location LOC, with
542 language mask LANG_MASK and option handlers HANDLERS. */
544 static void
545 maybe_default_options (struct gcc_options *opts,
546 struct gcc_options *opts_set,
547 const struct default_options *default_opts,
548 int level, bool size, bool fast, bool debug,
549 unsigned int lang_mask,
550 const struct cl_option_handlers *handlers,
551 location_t loc,
552 diagnostic_context *dc)
554 size_t i;
556 for (i = 0; default_opts[i].levels != OPT_LEVELS_NONE; i++)
557 maybe_default_option (opts, opts_set, &default_opts[i],
558 level, size, fast, debug,
559 lang_mask, handlers, loc, dc);
562 /* Table of options enabled by default at different levels.
563 Please keep this list sorted by level and alphabetized within
564 each level; this makes it easier to keep the documentation
565 in sync. */
567 static const struct default_options default_options_table[] =
569 /* -O1 and -Og optimizations. */
570 { OPT_LEVELS_1_PLUS, OPT_fcombine_stack_adjustments, NULL, 1 },
571 { OPT_LEVELS_1_PLUS, OPT_fcompare_elim, NULL, 1 },
572 { OPT_LEVELS_1_PLUS, OPT_fcprop_registers, NULL, 1 },
573 { OPT_LEVELS_1_PLUS, OPT_fdefer_pop, NULL, 1 },
574 { OPT_LEVELS_1_PLUS, OPT_fforward_propagate, NULL, 1 },
575 { OPT_LEVELS_1_PLUS, OPT_fguess_branch_probability, NULL, 1 },
576 { OPT_LEVELS_1_PLUS, OPT_fipa_profile, NULL, 1 },
577 { OPT_LEVELS_1_PLUS, OPT_fipa_pure_const, NULL, 1 },
578 { OPT_LEVELS_1_PLUS, OPT_fipa_reference, NULL, 1 },
579 { OPT_LEVELS_1_PLUS, OPT_fipa_reference_addressable, NULL, 1 },
580 { OPT_LEVELS_1_PLUS, OPT_fmerge_constants, NULL, 1 },
581 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
582 { OPT_LEVELS_1_PLUS, OPT_freorder_blocks, NULL, 1 },
583 { OPT_LEVELS_1_PLUS, OPT_fshrink_wrap, NULL, 1 },
584 { OPT_LEVELS_1_PLUS, OPT_fsplit_wide_types, NULL, 1 },
585 { OPT_LEVELS_1_PLUS, OPT_fthread_jumps, NULL, 1 },
586 { OPT_LEVELS_1_PLUS, OPT_ftree_builtin_call_dce, NULL, 1 },
587 { OPT_LEVELS_1_PLUS, OPT_ftree_ccp, NULL, 1 },
588 { OPT_LEVELS_1_PLUS, OPT_ftree_ch, NULL, 1 },
589 { OPT_LEVELS_1_PLUS, OPT_ftree_coalesce_vars, NULL, 1 },
590 { OPT_LEVELS_1_PLUS, OPT_ftree_copy_prop, NULL, 1 },
591 { OPT_LEVELS_1_PLUS, OPT_ftree_dce, NULL, 1 },
592 { OPT_LEVELS_1_PLUS, OPT_ftree_dominator_opts, NULL, 1 },
593 { OPT_LEVELS_1_PLUS, OPT_ftree_fre, NULL, 1 },
594 { OPT_LEVELS_1_PLUS, OPT_ftree_sink, NULL, 1 },
595 { OPT_LEVELS_1_PLUS, OPT_ftree_slsr, NULL, 1 },
596 { OPT_LEVELS_1_PLUS, OPT_ftree_ter, NULL, 1 },
597 { OPT_LEVELS_1_PLUS, OPT_fvar_tracking, NULL, 1 },
599 /* -O1 (and not -Og) optimizations. */
600 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fbranch_count_reg, NULL, 1 },
601 #if DELAY_SLOTS
602 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fdelayed_branch, NULL, 1 },
603 #endif
604 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fdse, NULL, 1 },
605 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fif_conversion, NULL, 1 },
606 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fif_conversion2, NULL, 1 },
607 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_finline_functions_called_once, NULL, 1 },
608 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fmove_loop_invariants, NULL, 1 },
609 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fmove_loop_stores, NULL, 1 },
610 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fssa_phiopt, NULL, 1 },
611 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fipa_modref, NULL, 1 },
612 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_bit_ccp, NULL, 1 },
613 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_dse, NULL, 1 },
614 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_pta, NULL, 1 },
615 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_sra, NULL, 1 },
617 /* -O2 and -Os optimizations. */
618 { OPT_LEVELS_2_PLUS, OPT_fcaller_saves, NULL, 1 },
619 { OPT_LEVELS_2_PLUS, OPT_fcode_hoisting, NULL, 1 },
620 { OPT_LEVELS_2_PLUS, OPT_fcrossjumping, NULL, 1 },
621 { OPT_LEVELS_2_PLUS, OPT_fcse_follow_jumps, NULL, 1 },
622 { OPT_LEVELS_2_PLUS, OPT_fdevirtualize, NULL, 1 },
623 { OPT_LEVELS_2_PLUS, OPT_fdevirtualize_speculatively, NULL, 1 },
624 { OPT_LEVELS_2_PLUS, OPT_fexpensive_optimizations, NULL, 1 },
625 { OPT_LEVELS_2_PLUS, OPT_fgcse, NULL, 1 },
626 { OPT_LEVELS_2_PLUS, OPT_fhoist_adjacent_loads, NULL, 1 },
627 { OPT_LEVELS_2_PLUS, OPT_findirect_inlining, NULL, 1 },
628 { OPT_LEVELS_2_PLUS, OPT_finline_small_functions, NULL, 1 },
629 { OPT_LEVELS_2_PLUS, OPT_fipa_bit_cp, NULL, 1 },
630 { OPT_LEVELS_2_PLUS, OPT_fipa_cp, NULL, 1 },
631 { OPT_LEVELS_2_PLUS, OPT_fipa_icf, NULL, 1 },
632 { OPT_LEVELS_2_PLUS, OPT_fipa_ra, NULL, 1 },
633 { OPT_LEVELS_2_PLUS, OPT_fipa_sra, NULL, 1 },
634 { OPT_LEVELS_2_PLUS, OPT_fipa_vrp, NULL, 1 },
635 { OPT_LEVELS_2_PLUS, OPT_fisolate_erroneous_paths_dereference, NULL, 1 },
636 { OPT_LEVELS_2_PLUS, OPT_flra_remat, NULL, 1 },
637 { OPT_LEVELS_2_PLUS, OPT_foptimize_sibling_calls, NULL, 1 },
638 { OPT_LEVELS_2_PLUS, OPT_fpartial_inlining, NULL, 1 },
639 { OPT_LEVELS_2_PLUS, OPT_fpeephole2, NULL, 1 },
640 { OPT_LEVELS_2_PLUS, OPT_freorder_functions, NULL, 1 },
641 { OPT_LEVELS_2_PLUS, OPT_frerun_cse_after_loop, NULL, 1 },
642 #ifdef INSN_SCHEDULING
643 { OPT_LEVELS_2_PLUS, OPT_fschedule_insns2, NULL, 1 },
644 #endif
645 { OPT_LEVELS_2_PLUS, OPT_fstrict_aliasing, NULL, 1 },
646 { OPT_LEVELS_2_PLUS, OPT_fstore_merging, NULL, 1 },
647 { OPT_LEVELS_2_PLUS, OPT_ftree_pre, NULL, 1 },
648 { OPT_LEVELS_2_PLUS, OPT_ftree_switch_conversion, NULL, 1 },
649 { OPT_LEVELS_2_PLUS, OPT_ftree_tail_merge, NULL, 1 },
650 { OPT_LEVELS_2_PLUS, OPT_ftree_vrp, NULL, 1 },
651 { OPT_LEVELS_2_PLUS, OPT_fvect_cost_model_, NULL,
652 VECT_COST_MODEL_VERY_CHEAP },
653 { OPT_LEVELS_2_PLUS, OPT_finline_functions, NULL, 1 },
654 { OPT_LEVELS_2_PLUS, OPT_ftree_loop_distribute_patterns, NULL, 1 },
656 /* -O2 and above optimizations, but not -Os or -Og. */
657 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_functions, NULL, 1 },
658 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_jumps, NULL, 1 },
659 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_labels, NULL, 1 },
660 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_loops, NULL, 1 },
661 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_foptimize_strlen, NULL, 1 },
662 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_freorder_blocks_algorithm_, NULL,
663 REORDER_BLOCKS_ALGORITHM_STC },
664 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_ftree_loop_vectorize, NULL, 1 },
665 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_ftree_slp_vectorize, NULL, 1 },
666 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_fopenmp_target_simd_clone_, NULL,
667 OMP_TARGET_SIMD_CLONE_NOHOST },
668 #ifdef INSN_SCHEDULING
669 /* Only run the pre-regalloc scheduling pass if optimizing for speed. */
670 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_fschedule_insns, NULL, 1 },
671 #endif
673 /* -O3 and -Os optimizations. */
675 /* -O3 optimizations. */
676 { OPT_LEVELS_3_PLUS, OPT_fgcse_after_reload, NULL, 1 },
677 { OPT_LEVELS_3_PLUS, OPT_fipa_cp_clone, NULL, 1 },
678 { OPT_LEVELS_3_PLUS, OPT_floop_interchange, NULL, 1 },
679 { OPT_LEVELS_3_PLUS, OPT_floop_unroll_and_jam, NULL, 1 },
680 { OPT_LEVELS_3_PLUS, OPT_fpeel_loops, NULL, 1 },
681 { OPT_LEVELS_3_PLUS, OPT_fpredictive_commoning, NULL, 1 },
682 { OPT_LEVELS_3_PLUS, OPT_fsplit_loops, NULL, 1 },
683 { OPT_LEVELS_3_PLUS, OPT_fsplit_paths, NULL, 1 },
684 { OPT_LEVELS_3_PLUS, OPT_ftree_loop_distribution, NULL, 1 },
685 { OPT_LEVELS_3_PLUS, OPT_ftree_partial_pre, NULL, 1 },
686 { OPT_LEVELS_3_PLUS, OPT_funswitch_loops, NULL, 1 },
687 { OPT_LEVELS_3_PLUS, OPT_fvect_cost_model_, NULL, VECT_COST_MODEL_DYNAMIC },
688 { OPT_LEVELS_3_PLUS, OPT_fversion_loops_for_strides, NULL, 1 },
690 /* -O3 parameters. */
691 { OPT_LEVELS_3_PLUS, OPT__param_max_inline_insns_auto_, NULL, 30 },
692 { OPT_LEVELS_3_PLUS, OPT__param_early_inlining_insns_, NULL, 14 },
693 { OPT_LEVELS_3_PLUS, OPT__param_inline_heuristics_hint_percent_, NULL, 600 },
694 { OPT_LEVELS_3_PLUS, OPT__param_inline_min_speedup_, NULL, 15 },
695 { OPT_LEVELS_3_PLUS, OPT__param_max_inline_insns_single_, NULL, 200 },
697 /* -Ofast adds optimizations to -O3. */
698 { OPT_LEVELS_FAST, OPT_ffast_math, NULL, 1 },
699 { OPT_LEVELS_FAST, OPT_fallow_store_data_races, NULL, 1 },
700 { OPT_LEVELS_FAST, OPT_fsemantic_interposition, NULL, 0 },
702 { OPT_LEVELS_NONE, 0, NULL, 0 }
705 /* Default the options in OPTS and OPTS_SET based on the optimization
706 settings in DECODED_OPTIONS and DECODED_OPTIONS_COUNT. */
707 void
708 default_options_optimization (struct gcc_options *opts,
709 struct gcc_options *opts_set,
710 struct cl_decoded_option *decoded_options,
711 unsigned int decoded_options_count,
712 location_t loc,
713 unsigned int lang_mask,
714 const struct cl_option_handlers *handlers,
715 diagnostic_context *dc)
717 unsigned int i;
718 int opt2;
719 bool openacc_mode = false;
721 /* Scan to see what optimization level has been specified. That will
722 determine the default value of many flags. */
723 for (i = 1; i < decoded_options_count; i++)
725 struct cl_decoded_option *opt = &decoded_options[i];
726 switch (opt->opt_index)
728 case OPT_O:
729 if (*opt->arg == '\0')
731 opts->x_optimize = 1;
732 opts->x_optimize_size = 0;
733 opts->x_optimize_fast = 0;
734 opts->x_optimize_debug = 0;
736 else
738 const int optimize_val = integral_argument (opt->arg);
739 if (optimize_val == -1)
740 error_at (loc, "argument to %<-O%> should be a non-negative "
741 "integer, %<g%>, %<s%>, %<z%> or %<fast%>");
742 else
744 opts->x_optimize = optimize_val;
745 if ((unsigned int) opts->x_optimize > 255)
746 opts->x_optimize = 255;
747 opts->x_optimize_size = 0;
748 opts->x_optimize_fast = 0;
749 opts->x_optimize_debug = 0;
752 break;
754 case OPT_Os:
755 opts->x_optimize_size = 1;
757 /* Optimizing for size forces optimize to be 2. */
758 opts->x_optimize = 2;
759 opts->x_optimize_fast = 0;
760 opts->x_optimize_debug = 0;
761 break;
763 case OPT_Oz:
764 opts->x_optimize_size = 2;
766 /* Optimizing for size forces optimize to be 2. */
767 opts->x_optimize = 2;
768 opts->x_optimize_fast = 0;
769 opts->x_optimize_debug = 0;
770 break;
772 case OPT_Ofast:
773 /* -Ofast only adds flags to -O3. */
774 opts->x_optimize_size = 0;
775 opts->x_optimize = 3;
776 opts->x_optimize_fast = 1;
777 opts->x_optimize_debug = 0;
778 break;
780 case OPT_Og:
781 /* -Og selects optimization level 1. */
782 opts->x_optimize_size = 0;
783 opts->x_optimize = 1;
784 opts->x_optimize_fast = 0;
785 opts->x_optimize_debug = 1;
786 break;
788 case OPT_fopenacc:
789 if (opt->value)
790 openacc_mode = true;
791 break;
793 default:
794 /* Ignore other options in this prescan. */
795 break;
799 maybe_default_options (opts, opts_set, default_options_table,
800 opts->x_optimize, opts->x_optimize_size,
801 opts->x_optimize_fast, opts->x_optimize_debug,
802 lang_mask, handlers, loc, dc);
804 /* -O2 param settings. */
805 opt2 = (opts->x_optimize >= 2);
807 if (openacc_mode)
808 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_pta, true);
810 /* Track fields in field-sensitive alias analysis. */
811 if (opt2)
812 SET_OPTION_IF_UNSET (opts, opts_set, param_max_fields_for_field_sensitive,
813 100);
815 if (opts->x_optimize_size)
816 /* We want to crossjump as much as possible. */
817 SET_OPTION_IF_UNSET (opts, opts_set, param_min_crossjump_insns, 1);
819 /* Restrict the amount of work combine does at -Og while retaining
820 most of its useful transforms. */
821 if (opts->x_optimize_debug)
822 SET_OPTION_IF_UNSET (opts, opts_set, param_max_combine_insns, 2);
824 /* Allow default optimizations to be specified on a per-machine basis. */
825 maybe_default_options (opts, opts_set,
826 targetm_common.option_optimization_table,
827 opts->x_optimize, opts->x_optimize_size,
828 opts->x_optimize_fast, opts->x_optimize_debug,
829 lang_mask, handlers, loc, dc);
832 /* Control IPA optimizations based on different live patching LEVEL. */
833 static void
834 control_options_for_live_patching (struct gcc_options *opts,
835 struct gcc_options *opts_set,
836 enum live_patching_level level,
837 location_t loc)
839 gcc_assert (level > LIVE_PATCHING_NONE);
841 switch (level)
843 case LIVE_PATCHING_INLINE_ONLY_STATIC:
844 #define LIVE_PATCHING_OPTION "-flive-patching=inline-only-static"
845 if (opts_set->x_flag_ipa_cp_clone && opts->x_flag_ipa_cp_clone)
846 error_at (loc, "%qs is incompatible with %qs",
847 "-fipa-cp-clone", LIVE_PATCHING_OPTION);
848 else
849 opts->x_flag_ipa_cp_clone = 0;
851 if (opts_set->x_flag_ipa_sra && opts->x_flag_ipa_sra)
852 error_at (loc, "%qs is incompatible with %qs",
853 "-fipa-sra", LIVE_PATCHING_OPTION);
854 else
855 opts->x_flag_ipa_sra = 0;
857 if (opts_set->x_flag_partial_inlining && opts->x_flag_partial_inlining)
858 error_at (loc, "%qs is incompatible with %qs",
859 "-fpartial-inlining", LIVE_PATCHING_OPTION);
860 else
861 opts->x_flag_partial_inlining = 0;
863 if (opts_set->x_flag_ipa_cp && opts->x_flag_ipa_cp)
864 error_at (loc, "%qs is incompatible with %qs",
865 "-fipa-cp", LIVE_PATCHING_OPTION);
866 else
867 opts->x_flag_ipa_cp = 0;
869 /* FALLTHROUGH. */
870 case LIVE_PATCHING_INLINE_CLONE:
871 #undef LIVE_PATCHING_OPTION
872 #define LIVE_PATCHING_OPTION "-flive-patching=inline-only-static|inline-clone"
873 /* live patching should disable whole-program optimization. */
874 if (opts_set->x_flag_whole_program && opts->x_flag_whole_program)
875 error_at (loc, "%qs is incompatible with %qs",
876 "-fwhole-program", LIVE_PATCHING_OPTION);
877 else
878 opts->x_flag_whole_program = 0;
880 /* visibility change should be excluded by !flag_whole_program
881 && !in_lto_p && !flag_ipa_cp_clone && !flag_ipa_sra
882 && !flag_partial_inlining. */
884 if (opts_set->x_flag_ipa_pta && opts->x_flag_ipa_pta)
885 error_at (loc, "%qs is incompatible with %qs",
886 "-fipa-pta", LIVE_PATCHING_OPTION);
887 else
888 opts->x_flag_ipa_pta = 0;
890 if (opts_set->x_flag_ipa_reference && opts->x_flag_ipa_reference)
891 error_at (loc, "%qs is incompatible with %qs",
892 "-fipa-reference", LIVE_PATCHING_OPTION);
893 else
894 opts->x_flag_ipa_reference = 0;
896 if (opts_set->x_flag_ipa_ra && opts->x_flag_ipa_ra)
897 error_at (loc, "%qs is incompatible with %qs",
898 "-fipa-ra", LIVE_PATCHING_OPTION);
899 else
900 opts->x_flag_ipa_ra = 0;
902 if (opts_set->x_flag_ipa_icf && opts->x_flag_ipa_icf)
903 error_at (loc, "%qs is incompatible with %qs",
904 "-fipa-icf", LIVE_PATCHING_OPTION);
905 else
906 opts->x_flag_ipa_icf = 0;
908 if (opts_set->x_flag_ipa_icf_functions && opts->x_flag_ipa_icf_functions)
909 error_at (loc, "%qs is incompatible with %qs",
910 "-fipa-icf-functions", LIVE_PATCHING_OPTION);
911 else
912 opts->x_flag_ipa_icf_functions = 0;
914 if (opts_set->x_flag_ipa_icf_variables && opts->x_flag_ipa_icf_variables)
915 error_at (loc, "%qs is incompatible with %qs",
916 "-fipa-icf-variables", LIVE_PATCHING_OPTION);
917 else
918 opts->x_flag_ipa_icf_variables = 0;
920 if (opts_set->x_flag_ipa_bit_cp && opts->x_flag_ipa_bit_cp)
921 error_at (loc, "%qs is incompatible with %qs",
922 "-fipa-bit-cp", LIVE_PATCHING_OPTION);
923 else
924 opts->x_flag_ipa_bit_cp = 0;
926 if (opts_set->x_flag_ipa_vrp && opts->x_flag_ipa_vrp)
927 error_at (loc, "%qs is incompatible with %qs",
928 "-fipa-vrp", LIVE_PATCHING_OPTION);
929 else
930 opts->x_flag_ipa_vrp = 0;
932 if (opts_set->x_flag_ipa_pure_const && opts->x_flag_ipa_pure_const)
933 error_at (loc, "%qs is incompatible with %qs",
934 "-fipa-pure-const", LIVE_PATCHING_OPTION);
935 else
936 opts->x_flag_ipa_pure_const = 0;
938 if (opts_set->x_flag_ipa_modref && opts->x_flag_ipa_modref)
939 error_at (loc,
940 "%<-fipa-modref%> is incompatible with %qs",
941 LIVE_PATCHING_OPTION);
942 else
943 opts->x_flag_ipa_modref = 0;
945 /* FIXME: disable unreachable code removal. */
947 /* discovery of functions/variables with no address taken. */
948 if (opts_set->x_flag_ipa_reference_addressable
949 && opts->x_flag_ipa_reference_addressable)
950 error_at (loc, "%qs is incompatible with %qs",
951 "-fipa-reference-addressable", LIVE_PATCHING_OPTION);
952 else
953 opts->x_flag_ipa_reference_addressable = 0;
955 /* ipa stack alignment propagation. */
956 if (opts_set->x_flag_ipa_stack_alignment
957 && opts->x_flag_ipa_stack_alignment)
958 error_at (loc, "%qs is incompatible with %qs",
959 "-fipa-stack-alignment", LIVE_PATCHING_OPTION);
960 else
961 opts->x_flag_ipa_stack_alignment = 0;
962 break;
963 default:
964 gcc_unreachable ();
967 #undef LIVE_PATCHING_OPTION
970 /* --help option argument if set. */
971 vec<const char *> help_option_arguments;
973 /* Return the string name describing a sanitizer argument which has been
974 provided on the command line and has set this particular flag. */
975 const char *
976 find_sanitizer_argument (struct gcc_options *opts, unsigned int flags)
978 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
980 /* Need to find the sanitizer_opts element which:
981 a) Could have set the flags requested.
982 b) Has been set on the command line.
984 Can have (a) without (b) if the flag requested is e.g.
985 SANITIZE_ADDRESS, since both -fsanitize=address and
986 -fsanitize=kernel-address set this flag.
988 Can have (b) without (a) by requesting more than one sanitizer on the
989 command line. */
990 if ((sanitizer_opts[i].flag & opts->x_flag_sanitize)
991 != sanitizer_opts[i].flag)
992 continue;
993 if ((sanitizer_opts[i].flag & flags) != flags)
994 continue;
995 return sanitizer_opts[i].name;
997 return NULL;
1001 /* Report an error to the user about sanitizer options they have requested
1002 which have set conflicting flags.
1004 LEFT and RIGHT indicate sanitizer flags which conflict with each other, this
1005 function reports an error if both have been set in OPTS->x_flag_sanitize and
1006 ensures the error identifies the requested command line options that have
1007 set these flags. */
1008 static void
1009 report_conflicting_sanitizer_options (struct gcc_options *opts, location_t loc,
1010 unsigned int left, unsigned int right)
1012 unsigned int left_seen = (opts->x_flag_sanitize & left);
1013 unsigned int right_seen = (opts->x_flag_sanitize & right);
1014 if (left_seen && right_seen)
1016 const char* left_arg = find_sanitizer_argument (opts, left_seen);
1017 const char* right_arg = find_sanitizer_argument (opts, right_seen);
1018 gcc_assert (left_arg && right_arg);
1019 error_at (loc,
1020 "%<-fsanitize=%s%> is incompatible with %<-fsanitize=%s%>",
1021 left_arg, right_arg);
1025 /* After all options at LOC have been read into OPTS and OPTS_SET,
1026 finalize settings of those options and diagnose incompatible
1027 combinations. */
1028 void
1029 finish_options (struct gcc_options *opts, struct gcc_options *opts_set,
1030 location_t loc)
1032 if (opts->x_dump_base_name
1033 && ! opts->x_dump_base_name_prefixed)
1035 const char *sep = opts->x_dump_base_name;
1037 for (; *sep; sep++)
1038 if (IS_DIR_SEPARATOR (*sep))
1039 break;
1041 if (*sep)
1042 /* If dump_base_path contains subdirectories, don't prepend
1043 anything. */;
1044 else if (opts->x_dump_dir_name)
1045 /* We have a DUMP_DIR_NAME, prepend that. */
1046 opts->x_dump_base_name = opts_concat (opts->x_dump_dir_name,
1047 opts->x_dump_base_name, NULL);
1049 /* It is definitely prefixed now. */
1050 opts->x_dump_base_name_prefixed = true;
1053 /* Handle related options for unit-at-a-time, toplevel-reorder, and
1054 section-anchors. */
1055 if (!opts->x_flag_unit_at_a_time)
1057 if (opts->x_flag_section_anchors && opts_set->x_flag_section_anchors)
1058 error_at (loc, "section anchors must be disabled when unit-at-a-time "
1059 "is disabled");
1060 opts->x_flag_section_anchors = 0;
1061 if (opts->x_flag_toplevel_reorder == 1)
1062 error_at (loc, "toplevel reorder must be disabled when unit-at-a-time "
1063 "is disabled");
1064 opts->x_flag_toplevel_reorder = 0;
1067 /* -fself-test depends on the state of the compiler prior to
1068 compiling anything. Ideally it should be run on an empty source
1069 file. However, in case we get run with actual source, assume
1070 -fsyntax-only which will inhibit any compiler initialization
1071 which may confuse the self tests. */
1072 if (opts->x_flag_self_test)
1073 opts->x_flag_syntax_only = 1;
1075 if (opts->x_flag_tm && opts->x_flag_non_call_exceptions)
1076 sorry ("transactional memory is not supported with non-call exceptions");
1078 /* Unless the user has asked for section anchors, we disable toplevel
1079 reordering at -O0 to disable transformations that might be surprising
1080 to end users and to get -fno-toplevel-reorder tested. */
1081 if (!opts->x_optimize
1082 && opts->x_flag_toplevel_reorder == 2
1083 && !(opts->x_flag_section_anchors && opts_set->x_flag_section_anchors))
1085 opts->x_flag_toplevel_reorder = 0;
1086 opts->x_flag_section_anchors = 0;
1088 if (!opts->x_flag_toplevel_reorder)
1090 if (opts->x_flag_section_anchors && opts_set->x_flag_section_anchors)
1091 error_at (loc, "section anchors must be disabled when toplevel reorder"
1092 " is disabled");
1093 opts->x_flag_section_anchors = 0;
1096 if (!opts->x_flag_opts_finished)
1098 /* We initialize opts->x_flag_pie to -1 so that targets can set a
1099 default value. */
1100 if (opts->x_flag_pie == -1)
1102 /* We initialize opts->x_flag_pic to -1 so that we can tell if
1103 -fpic, -fPIC, -fno-pic or -fno-PIC is used. */
1104 if (opts->x_flag_pic == -1)
1105 opts->x_flag_pie = DEFAULT_FLAG_PIE;
1106 else
1107 opts->x_flag_pie = 0;
1109 /* If -fPIE or -fpie is used, turn on PIC. */
1110 if (opts->x_flag_pie)
1111 opts->x_flag_pic = opts->x_flag_pie;
1112 else if (opts->x_flag_pic == -1)
1113 opts->x_flag_pic = 0;
1114 if (opts->x_flag_pic && !opts->x_flag_pie)
1115 opts->x_flag_shlib = 1;
1116 opts->x_flag_opts_finished = true;
1119 /* We initialize opts->x_flag_stack_protect to -1 so that targets
1120 can set a default value. */
1121 if (opts->x_flag_stack_protect == -1)
1122 opts->x_flag_stack_protect = DEFAULT_FLAG_SSP;
1124 if (opts->x_optimize == 0)
1126 /* Inlining does not work if not optimizing,
1127 so force it not to be done. */
1128 opts->x_warn_inline = 0;
1129 opts->x_flag_no_inline = 1;
1132 /* At -O0 or -Og, turn __builtin_unreachable into a trap. */
1133 if (!opts->x_optimize || opts->x_optimize_debug)
1134 SET_OPTION_IF_UNSET (opts, opts_set, flag_unreachable_traps, true);
1136 /* Pipelining of outer loops is only possible when general pipelining
1137 capabilities are requested. */
1138 if (!opts->x_flag_sel_sched_pipelining)
1139 opts->x_flag_sel_sched_pipelining_outer_loops = 0;
1141 if (opts->x_flag_conserve_stack)
1143 SET_OPTION_IF_UNSET (opts, opts_set, param_large_stack_frame, 100);
1144 SET_OPTION_IF_UNSET (opts, opts_set, param_stack_frame_growth, 40);
1147 if (opts->x_flag_lto)
1149 #ifdef ENABLE_LTO
1150 opts->x_flag_generate_lto = 1;
1152 /* When generating IL, do not operate in whole-program mode.
1153 Otherwise, symbols will be privatized too early, causing link
1154 errors later. */
1155 opts->x_flag_whole_program = 0;
1156 #else
1157 error_at (loc, "LTO support has not been enabled in this configuration");
1158 #endif
1159 if (!opts->x_flag_fat_lto_objects
1160 && (!HAVE_LTO_PLUGIN
1161 || (opts_set->x_flag_use_linker_plugin
1162 && !opts->x_flag_use_linker_plugin)))
1164 if (opts_set->x_flag_fat_lto_objects)
1165 error_at (loc, "%<-fno-fat-lto-objects%> are supported only with "
1166 "linker plugin");
1167 opts->x_flag_fat_lto_objects = 1;
1170 /* -gsplit-dwarf isn't compatible with LTO, see PR88389. */
1171 if (opts->x_dwarf_split_debug_info)
1173 inform (loc, "%<-gsplit-dwarf%> is not supported with LTO,"
1174 " disabling");
1175 opts->x_dwarf_split_debug_info = 0;
1179 /* We initialize opts->x_flag_split_stack to -1 so that targets can set a
1180 default value if they choose based on other options. */
1181 if (opts->x_flag_split_stack == -1)
1182 opts->x_flag_split_stack = 0;
1183 else if (opts->x_flag_split_stack)
1185 if (!targetm_common.supports_split_stack (true, opts))
1187 error_at (loc, "%<-fsplit-stack%> is not supported by "
1188 "this compiler configuration");
1189 opts->x_flag_split_stack = 0;
1193 /* If stack splitting is turned on, and the user did not explicitly
1194 request function partitioning, turn off partitioning, as it
1195 confuses the linker when trying to handle partitioned split-stack
1196 code that calls a non-split-stack functions. But if partitioning
1197 was turned on explicitly just hope for the best. */
1198 if (opts->x_flag_split_stack
1199 && opts->x_flag_reorder_blocks_and_partition)
1200 SET_OPTION_IF_UNSET (opts, opts_set, flag_reorder_blocks_and_partition, 0);
1202 if (opts->x_flag_reorder_blocks_and_partition)
1203 SET_OPTION_IF_UNSET (opts, opts_set, flag_reorder_functions, 1);
1205 /* The -gsplit-dwarf option requires -ggnu-pubnames. */
1206 if (opts->x_dwarf_split_debug_info)
1207 opts->x_debug_generate_pub_sections = 2;
1209 if ((opts->x_flag_sanitize
1210 & (SANITIZE_USER_ADDRESS | SANITIZE_KERNEL_ADDRESS)) == 0)
1212 if (opts->x_flag_sanitize & SANITIZE_POINTER_COMPARE)
1213 error_at (loc,
1214 "%<-fsanitize=pointer-compare%> must be combined with "
1215 "%<-fsanitize=address%> or %<-fsanitize=kernel-address%>");
1216 if (opts->x_flag_sanitize & SANITIZE_POINTER_SUBTRACT)
1217 error_at (loc,
1218 "%<-fsanitize=pointer-subtract%> must be combined with "
1219 "%<-fsanitize=address%> or %<-fsanitize=kernel-address%>");
1222 /* Address sanitizers conflict with the thread sanitizer. */
1223 report_conflicting_sanitizer_options (opts, loc, SANITIZE_THREAD,
1224 SANITIZE_ADDRESS);
1225 report_conflicting_sanitizer_options (opts, loc, SANITIZE_THREAD,
1226 SANITIZE_HWADDRESS);
1227 /* The leak sanitizer conflicts with the thread sanitizer. */
1228 report_conflicting_sanitizer_options (opts, loc, SANITIZE_LEAK,
1229 SANITIZE_THREAD);
1231 /* No combination of HWASAN and ASAN work together. */
1232 report_conflicting_sanitizer_options (opts, loc,
1233 SANITIZE_HWADDRESS, SANITIZE_ADDRESS);
1235 /* The userspace and kernel address sanitizers conflict with each other. */
1236 report_conflicting_sanitizer_options (opts, loc, SANITIZE_USER_HWADDRESS,
1237 SANITIZE_KERNEL_HWADDRESS);
1238 report_conflicting_sanitizer_options (opts, loc, SANITIZE_USER_ADDRESS,
1239 SANITIZE_KERNEL_ADDRESS);
1241 /* Check error recovery for -fsanitize-recover option. */
1242 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
1243 if ((opts->x_flag_sanitize_recover & sanitizer_opts[i].flag)
1244 && !sanitizer_opts[i].can_recover)
1245 error_at (loc, "%<-fsanitize-recover=%s%> is not supported",
1246 sanitizer_opts[i].name);
1248 /* Check -fsanitize-trap option. */
1249 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
1250 if ((opts->x_flag_sanitize_trap & sanitizer_opts[i].flag)
1251 && !sanitizer_opts[i].can_trap
1252 /* Allow -fsanitize-trap=all or -fsanitize-trap=undefined
1253 to set flag_sanitize_trap & SANITIZE_VPTR bit which will
1254 effectively disable -fsanitize=vptr, just disallow
1255 explicit -fsanitize-trap=vptr. */
1256 && sanitizer_opts[i].flag != SANITIZE_VPTR)
1257 error_at (loc, "%<-fsanitize-trap=%s%> is not supported",
1258 sanitizer_opts[i].name);
1260 /* When instrumenting the pointers, we don't want to remove
1261 the null pointer checks. */
1262 if (opts->x_flag_sanitize & (SANITIZE_NULL | SANITIZE_NONNULL_ATTRIBUTE
1263 | SANITIZE_RETURNS_NONNULL_ATTRIBUTE))
1264 opts->x_flag_delete_null_pointer_checks = 0;
1266 /* Aggressive compiler optimizations may cause false negatives. */
1267 if (opts->x_flag_sanitize & ~(SANITIZE_LEAK | SANITIZE_UNREACHABLE))
1268 opts->x_flag_aggressive_loop_optimizations = 0;
1270 /* Enable -fsanitize-address-use-after-scope if either address sanitizer is
1271 enabled. */
1272 if (opts->x_flag_sanitize
1273 & (SANITIZE_USER_ADDRESS | SANITIZE_USER_HWADDRESS))
1274 SET_OPTION_IF_UNSET (opts, opts_set, flag_sanitize_address_use_after_scope,
1275 true);
1277 /* Force -fstack-reuse=none in case -fsanitize-address-use-after-scope
1278 is enabled. */
1279 if (opts->x_flag_sanitize_address_use_after_scope)
1281 if (opts->x_flag_stack_reuse != SR_NONE
1282 && opts_set->x_flag_stack_reuse != SR_NONE)
1283 error_at (loc,
1284 "%<-fsanitize-address-use-after-scope%> requires "
1285 "%<-fstack-reuse=none%> option");
1287 opts->x_flag_stack_reuse = SR_NONE;
1290 if ((opts->x_flag_sanitize & SANITIZE_USER_ADDRESS) && opts->x_flag_tm)
1291 sorry ("transactional memory is not supported with %<-fsanitize=address%>");
1293 if ((opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS) && opts->x_flag_tm)
1294 sorry ("transactional memory is not supported with "
1295 "%<-fsanitize=kernel-address%>");
1297 /* Currently live patching is not support for LTO. */
1298 if (opts->x_flag_live_patching == LIVE_PATCHING_INLINE_ONLY_STATIC && opts->x_flag_lto)
1299 sorry ("live patching (with %qs) is not supported with LTO",
1300 "inline-only-static");
1302 /* Currently vtable verification is not supported for LTO */
1303 if (opts->x_flag_vtable_verify && opts->x_flag_lto)
1304 sorry ("vtable verification is not supported with LTO");
1306 /* Control IPA optimizations based on different -flive-patching level. */
1307 if (opts->x_flag_live_patching)
1308 control_options_for_live_patching (opts, opts_set,
1309 opts->x_flag_live_patching,
1310 loc);
1312 /* Allow cunroll to grow size accordingly. */
1313 if (!opts_set->x_flag_cunroll_grow_size)
1314 opts->x_flag_cunroll_grow_size
1315 = (opts->x_flag_unroll_loops
1316 || opts->x_flag_peel_loops
1317 || opts->x_optimize >= 3);
1319 /* With -fcx-limited-range, we do cheap and quick complex arithmetic. */
1320 if (opts->x_flag_cx_limited_range)
1321 opts->x_flag_complex_method = 0;
1322 else if (opts_set->x_flag_cx_limited_range)
1323 opts->x_flag_complex_method = opts->x_flag_default_complex_method;
1325 /* With -fcx-fortran-rules, we do something in-between cheap and C99. */
1326 if (opts->x_flag_cx_fortran_rules)
1327 opts->x_flag_complex_method = 1;
1328 else if (opts_set->x_flag_cx_fortran_rules)
1329 opts->x_flag_complex_method = opts->x_flag_default_complex_method;
1331 /* Use -fvect-cost-model=cheap instead of -fvect-cost-mode=very-cheap
1332 by default with explicit -ftree-{loop,slp}-vectorize. */
1333 if (opts->x_optimize == 2
1334 && (opts_set->x_flag_tree_loop_vectorize
1335 || opts_set->x_flag_tree_vectorize))
1336 SET_OPTION_IF_UNSET (opts, opts_set, flag_vect_cost_model,
1337 VECT_COST_MODEL_CHEAP);
1339 if (opts->x_flag_gtoggle)
1341 /* Make sure to process -gtoggle only once. */
1342 opts->x_flag_gtoggle = false;
1343 if (opts->x_debug_info_level == DINFO_LEVEL_NONE)
1345 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
1347 if (opts->x_write_symbols == NO_DEBUG)
1348 opts->x_write_symbols = PREFERRED_DEBUGGING_TYPE;
1350 else
1351 opts->x_debug_info_level = DINFO_LEVEL_NONE;
1354 if (!opts_set->x_debug_nonbind_markers_p)
1355 opts->x_debug_nonbind_markers_p
1356 = (opts->x_optimize
1357 && opts->x_debug_info_level >= DINFO_LEVEL_NORMAL
1358 && dwarf_debuginfo_p (opts)
1359 && !(opts->x_flag_selective_scheduling
1360 || opts->x_flag_selective_scheduling2));
1362 /* We know which debug output will be used so we can set flag_var_tracking
1363 and flag_var_tracking_uninit if the user has not specified them. */
1364 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL
1365 || !dwarf_debuginfo_p (opts)
1366 /* We have not yet initialized debug hooks so match that to check
1367 whether we're only doing DWARF2_LINENO_DEBUGGING_INFO. */
1368 #ifndef DWARF2_DEBUGGING_INFO
1369 || true
1370 #endif
1373 if ((opts_set->x_flag_var_tracking && opts->x_flag_var_tracking == 1)
1374 || (opts_set->x_flag_var_tracking_uninit
1375 && opts->x_flag_var_tracking_uninit == 1))
1377 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
1378 warning_at (UNKNOWN_LOCATION, 0,
1379 "variable tracking requested, but useless unless "
1380 "producing debug info");
1381 else
1382 warning_at (UNKNOWN_LOCATION, 0,
1383 "variable tracking requested, but not supported "
1384 "by this debug format");
1386 opts->x_flag_var_tracking = 0;
1387 opts->x_flag_var_tracking_uninit = 0;
1388 opts->x_flag_var_tracking_assignments = 0;
1391 /* One could use EnabledBy, but it would lead to a circular dependency. */
1392 if (!opts_set->x_flag_var_tracking_uninit)
1393 opts->x_flag_var_tracking_uninit = opts->x_flag_var_tracking;
1395 if (!opts_set->x_flag_var_tracking_assignments)
1396 opts->x_flag_var_tracking_assignments
1397 = (opts->x_flag_var_tracking
1398 && !(opts->x_flag_selective_scheduling
1399 || opts->x_flag_selective_scheduling2));
1401 if (opts->x_flag_var_tracking_assignments_toggle)
1402 opts->x_flag_var_tracking_assignments
1403 = !opts->x_flag_var_tracking_assignments;
1405 if (opts->x_flag_var_tracking_assignments && !opts->x_flag_var_tracking)
1406 opts->x_flag_var_tracking = opts->x_flag_var_tracking_assignments = -1;
1408 if (opts->x_flag_var_tracking_assignments
1409 && (opts->x_flag_selective_scheduling
1410 || opts->x_flag_selective_scheduling2))
1411 warning_at (loc, 0,
1412 "var-tracking-assignments changes selective scheduling");
1414 if (opts->x_flag_syntax_only)
1416 opts->x_write_symbols = NO_DEBUG;
1417 opts->x_profile_flag = 0;
1420 if (opts->x_warn_strict_flex_arrays)
1421 if (opts->x_flag_strict_flex_arrays == 0)
1423 opts->x_warn_strict_flex_arrays = 0;
1424 warning_at (UNKNOWN_LOCATION, 0,
1425 "%<-Wstrict-flex-arrays%> is ignored when"
1426 " %<-fstrict-flex-arrays%> is not present");
1429 diagnose_options (opts, opts_set, loc);
1432 /* The function diagnoses incompatible combinations for provided options
1433 (OPTS and OPTS_SET) at a given LOCation. The function is called both
1434 when command line is parsed (after the target optimization hook) and
1435 when an optimize/target attribute (or pragma) is used. */
1437 void diagnose_options (gcc_options *opts, gcc_options *opts_set,
1438 location_t loc)
1440 /* The optimization to partition hot and cold basic blocks into separate
1441 sections of the .o and executable files does not work (currently)
1442 with exception handling. This is because there is no support for
1443 generating unwind info. If opts->x_flag_exceptions is turned on
1444 we need to turn off the partitioning optimization. */
1446 enum unwind_info_type ui_except
1447 = targetm_common.except_unwind_info (opts);
1449 if (opts->x_flag_exceptions
1450 && opts->x_flag_reorder_blocks_and_partition
1451 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))
1453 if (opts_set->x_flag_reorder_blocks_and_partition)
1454 inform (loc,
1455 "%<-freorder-blocks-and-partition%> does not work "
1456 "with exceptions on this architecture");
1457 opts->x_flag_reorder_blocks_and_partition = 0;
1458 opts->x_flag_reorder_blocks = 1;
1461 /* If user requested unwind info, then turn off the partitioning
1462 optimization. */
1464 if (opts->x_flag_unwind_tables
1465 && !targetm_common.unwind_tables_default
1466 && opts->x_flag_reorder_blocks_and_partition
1467 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))
1469 if (opts_set->x_flag_reorder_blocks_and_partition)
1470 inform (loc,
1471 "%<-freorder-blocks-and-partition%> does not support "
1472 "unwind info on this architecture");
1473 opts->x_flag_reorder_blocks_and_partition = 0;
1474 opts->x_flag_reorder_blocks = 1;
1477 /* If the target requested unwind info, then turn off the partitioning
1478 optimization with a different message. Likewise, if the target does not
1479 support named sections. */
1481 if (opts->x_flag_reorder_blocks_and_partition
1482 && (!targetm_common.have_named_sections
1483 || (opts->x_flag_unwind_tables
1484 && targetm_common.unwind_tables_default
1485 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))))
1487 if (opts_set->x_flag_reorder_blocks_and_partition)
1488 inform (loc,
1489 "%<-freorder-blocks-and-partition%> does not work "
1490 "on this architecture");
1491 opts->x_flag_reorder_blocks_and_partition = 0;
1492 opts->x_flag_reorder_blocks = 1;
1498 #define LEFT_COLUMN 27
1500 /* Output ITEM, of length ITEM_WIDTH, in the left column,
1501 followed by word-wrapped HELP in a second column. */
1502 static void
1503 wrap_help (const char *help,
1504 const char *item,
1505 unsigned int item_width,
1506 unsigned int columns)
1508 unsigned int col_width = LEFT_COLUMN;
1509 unsigned int remaining, room, len;
1511 remaining = strlen (help);
1515 room = columns - 3 - MAX (col_width, item_width);
1516 if (room > columns)
1517 room = 0;
1518 len = remaining;
1520 if (room < len)
1522 unsigned int i;
1524 for (i = 0; help[i]; i++)
1526 if (i >= room && len != remaining)
1527 break;
1528 if (help[i] == ' ')
1529 len = i;
1530 else if ((help[i] == '-' || help[i] == '/')
1531 && help[i + 1] != ' '
1532 && i > 0 && ISALPHA (help[i - 1]))
1533 len = i + 1;
1537 printf (" %-*.*s %.*s\n", col_width, item_width, item, len, help);
1538 item_width = 0;
1539 while (help[len] == ' ')
1540 len++;
1541 help += len;
1542 remaining -= len;
1544 while (remaining);
1547 /* Data structure used to print list of valid option values. */
1549 class option_help_tuple
1551 public:
1552 option_help_tuple (int code, vec<const char *> values):
1553 m_code (code), m_values (values)
1556 /* Code of an option. */
1557 int m_code;
1559 /* List of possible values. */
1560 vec<const char *> m_values;
1563 /* Print help for a specific front-end, etc. */
1564 static void
1565 print_filtered_help (unsigned int include_flags,
1566 unsigned int exclude_flags,
1567 unsigned int any_flags,
1568 unsigned int columns,
1569 struct gcc_options *opts,
1570 unsigned int lang_mask)
1572 unsigned int i;
1573 const char *help;
1574 bool found = false;
1575 bool displayed = false;
1576 char new_help[256];
1578 if (!opts->x_help_printed)
1579 opts->x_help_printed = XCNEWVAR (char, cl_options_count);
1581 if (!opts->x_help_enum_printed)
1582 opts->x_help_enum_printed = XCNEWVAR (char, cl_enums_count);
1584 auto_vec<option_help_tuple> help_tuples;
1586 for (i = 0; i < cl_options_count; i++)
1588 const struct cl_option *option = cl_options + i;
1589 unsigned int len;
1590 const char *opt;
1591 const char *tab;
1593 if (include_flags == 0
1594 || ((option->flags & include_flags) != include_flags))
1596 if ((option->flags & any_flags) == 0)
1597 continue;
1600 /* Skip unwanted switches. */
1601 if ((option->flags & exclude_flags) != 0)
1602 continue;
1604 /* The driver currently prints its own help text. */
1605 if ((option->flags & CL_DRIVER) != 0
1606 && (option->flags & (((1U << cl_lang_count) - 1)
1607 | CL_COMMON | CL_TARGET)) == 0)
1608 continue;
1610 /* If an option contains a language specification,
1611 exclude it from common unless all languages are present. */
1612 if ((include_flags & CL_COMMON)
1613 && !(option->flags & CL_DRIVER)
1614 && (option->flags & CL_LANG_ALL)
1615 && (option->flags & CL_LANG_ALL) != CL_LANG_ALL)
1616 continue;
1618 found = true;
1619 /* Skip switches that have already been printed. */
1620 if (opts->x_help_printed[i])
1621 continue;
1623 opts->x_help_printed[i] = true;
1625 help = option->help;
1626 if (help == NULL)
1628 if (exclude_flags & CL_UNDOCUMENTED)
1629 continue;
1631 help = undocumented_msg;
1634 /* Get the translation. */
1635 help = _(help);
1637 if (option->alias_target < N_OPTS
1638 && cl_options [option->alias_target].help)
1640 const struct cl_option *target = cl_options + option->alias_target;
1641 if (option->help == NULL)
1643 /* The option is undocumented but is an alias for an option that
1644 is documented. If the option has alias arguments, then its
1645 purpose is to provide certain arguments to the other option, so
1646 inform the reader of this. Otherwise, point the reader to the
1647 other option in preference to the former. */
1649 if (option->alias_arg)
1651 if (option->neg_alias_arg)
1652 snprintf (new_help, sizeof new_help,
1653 _("Same as %s%s (or, in negated form, %s%s)."),
1654 target->opt_text, option->alias_arg,
1655 target->opt_text, option->neg_alias_arg);
1656 else
1657 snprintf (new_help, sizeof new_help,
1658 _("Same as %s%s."),
1659 target->opt_text, option->alias_arg);
1661 else
1662 snprintf (new_help, sizeof new_help,
1663 _("Same as %s."),
1664 target->opt_text);
1666 else
1668 /* For documented options with aliases, mention the aliased
1669 option's name for reference. */
1670 snprintf (new_help, sizeof new_help,
1671 _("%s Same as %s."),
1672 help, cl_options [option->alias_target].opt_text);
1675 help = new_help;
1678 if (option->warn_message)
1680 /* Mention that the use of the option will trigger a warning. */
1681 if (help == new_help)
1682 snprintf (new_help + strlen (new_help),
1683 sizeof new_help - strlen (new_help),
1684 " %s", _(use_diagnosed_msg));
1685 else
1686 snprintf (new_help, sizeof new_help,
1687 "%s %s", help, _(use_diagnosed_msg));
1689 help = new_help;
1692 /* Find the gap between the name of the
1693 option and its descriptive text. */
1694 tab = strchr (help, '\t');
1695 if (tab)
1697 len = tab - help;
1698 opt = help;
1699 help = tab + 1;
1701 else
1703 opt = option->opt_text;
1704 len = strlen (opt);
1707 /* With the -Q option enabled we change the descriptive text associated
1708 with an option to be an indication of its current setting. */
1709 if (!opts->x_quiet_flag)
1711 void *flag_var = option_flag_var (i, opts);
1713 if (len < (LEFT_COLUMN + 2))
1714 strcpy (new_help, "\t\t");
1715 else
1716 strcpy (new_help, "\t");
1718 /* Set to print whether the option is enabled or disabled,
1719 or, if it's an alias for another option, the name of
1720 the aliased option. */
1721 bool print_state = false;
1723 if (flag_var != NULL
1724 && option->var_type != CLVC_DEFER)
1726 /* If OPTION is only available for a specific subset
1727 of languages other than this one, mention them. */
1728 bool avail_for_lang = true;
1729 if (unsigned langset = option->flags & CL_LANG_ALL)
1731 if (!(langset & lang_mask))
1733 avail_for_lang = false;
1734 strcat (new_help, _("[available in "));
1735 for (unsigned i = 0, n = 0; (1U << i) < CL_LANG_ALL; ++i)
1736 if (langset & (1U << i))
1738 if (n++)
1739 strcat (new_help, ", ");
1740 strcat (new_help, lang_names[i]);
1742 strcat (new_help, "]");
1745 if (!avail_for_lang)
1746 ; /* Print nothing else if the option is not available
1747 in the current language. */
1748 else if (option->flags & CL_JOINED)
1750 if (option->var_type == CLVC_STRING)
1752 if (* (const char **) flag_var != NULL)
1753 snprintf (new_help + strlen (new_help),
1754 sizeof (new_help) - strlen (new_help),
1755 "%s", * (const char **) flag_var);
1757 else if (option->var_type == CLVC_ENUM)
1759 const struct cl_enum *e = &cl_enums[option->var_enum];
1760 int value;
1761 const char *arg = NULL;
1763 value = e->get (flag_var);
1764 enum_value_to_arg (e->values, &arg, value, lang_mask);
1765 if (arg == NULL)
1766 arg = _("[default]");
1767 snprintf (new_help + strlen (new_help),
1768 sizeof (new_help) - strlen (new_help),
1769 "%s", arg);
1771 else
1773 if (option->cl_host_wide_int)
1774 sprintf (new_help + strlen (new_help),
1775 _("%llu bytes"), (unsigned long long)
1776 *(unsigned HOST_WIDE_INT *) flag_var);
1777 else
1778 sprintf (new_help + strlen (new_help),
1779 "%i", * (int *) flag_var);
1782 else
1783 print_state = true;
1785 else
1786 /* When there is no argument, print the option state only
1787 if the option takes no argument. */
1788 print_state = !(option->flags & CL_JOINED);
1790 if (print_state)
1792 if (option->alias_target < N_OPTS
1793 && option->alias_target != OPT_SPECIAL_warn_removed
1794 && option->alias_target != OPT_SPECIAL_ignore
1795 && option->alias_target != OPT_SPECIAL_input_file
1796 && option->alias_target != OPT_SPECIAL_program_name
1797 && option->alias_target != OPT_SPECIAL_unknown)
1799 const struct cl_option *target
1800 = &cl_options[option->alias_target];
1801 sprintf (new_help + strlen (new_help), "%s%s",
1802 target->opt_text,
1803 option->alias_arg ? option->alias_arg : "");
1805 else if (option->alias_target == OPT_SPECIAL_ignore)
1806 strcat (new_help, ("[ignored]"));
1807 else
1809 /* Print the state for an on/off option. */
1810 int ena = option_enabled (i, lang_mask, opts);
1811 if (ena > 0)
1812 strcat (new_help, _("[enabled]"));
1813 else if (ena == 0)
1814 strcat (new_help, _("[disabled]"));
1818 help = new_help;
1821 if (option->range_max != -1 && tab == NULL)
1823 char b[128];
1824 snprintf (b, sizeof (b), "<%d,%d>", option->range_min,
1825 option->range_max);
1826 opt = concat (opt, b, NULL);
1827 len += strlen (b);
1830 wrap_help (help, opt, len, columns);
1831 displayed = true;
1833 if (option->var_type == CLVC_ENUM
1834 && opts->x_help_enum_printed[option->var_enum] != 2)
1835 opts->x_help_enum_printed[option->var_enum] = 1;
1836 else
1838 vec<const char *> option_values
1839 = targetm_common.get_valid_option_values (i, NULL);
1840 if (!option_values.is_empty ())
1841 help_tuples.safe_push (option_help_tuple (i, option_values));
1845 if (! found)
1847 unsigned int langs = include_flags & CL_LANG_ALL;
1849 if (langs == 0)
1850 printf (_(" No options with the desired characteristics were found\n"));
1851 else
1853 unsigned int i;
1855 /* PR 31349: Tell the user how to see all of the
1856 options supported by a specific front end. */
1857 for (i = 0; (1U << i) < CL_LANG_ALL; i ++)
1858 if ((1U << i) & langs)
1859 printf (_(" None found. Use --help=%s to show *all* the options supported by the %s front-end.\n"),
1860 lang_names[i], lang_names[i]);
1864 else if (! displayed)
1865 printf (_(" All options with the desired characteristics have already been displayed\n"));
1867 putchar ('\n');
1869 /* Print details of enumerated option arguments, if those
1870 enumerations have help text headings provided. If no help text
1871 is provided, presume that the possible values are listed in the
1872 help text for the relevant options. */
1873 for (i = 0; i < cl_enums_count; i++)
1875 unsigned int j, pos;
1877 if (opts->x_help_enum_printed[i] != 1)
1878 continue;
1879 if (cl_enums[i].help == NULL)
1880 continue;
1881 printf (" %s\n ", _(cl_enums[i].help));
1882 pos = 4;
1883 for (j = 0; cl_enums[i].values[j].arg != NULL; j++)
1885 unsigned int len = strlen (cl_enums[i].values[j].arg);
1887 if (pos > 4 && pos + 1 + len <= columns)
1889 printf (" %s", cl_enums[i].values[j].arg);
1890 pos += 1 + len;
1892 else
1894 if (pos > 4)
1896 printf ("\n ");
1897 pos = 4;
1899 printf ("%s", cl_enums[i].values[j].arg);
1900 pos += len;
1903 printf ("\n\n");
1904 opts->x_help_enum_printed[i] = 2;
1907 for (unsigned i = 0; i < help_tuples.length (); i++)
1909 const struct cl_option *option = cl_options + help_tuples[i].m_code;
1910 printf (_(" Known valid arguments for %s option:\n "),
1911 option->opt_text);
1912 for (unsigned j = 0; j < help_tuples[i].m_values.length (); j++)
1913 printf (" %s", help_tuples[i].m_values[j]);
1914 printf ("\n\n");
1918 /* Display help for a specified type of option.
1919 The options must have ALL of the INCLUDE_FLAGS set
1920 ANY of the flags in the ANY_FLAGS set
1921 and NONE of the EXCLUDE_FLAGS set. The current option state is in
1922 OPTS; LANG_MASK is used for interpreting enumerated option state. */
1923 static void
1924 print_specific_help (unsigned int include_flags,
1925 unsigned int exclude_flags,
1926 unsigned int any_flags,
1927 struct gcc_options *opts,
1928 unsigned int lang_mask)
1930 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
1931 const char * description = NULL;
1932 const char * descrip_extra = "";
1933 size_t i;
1934 unsigned int flag;
1936 /* Sanity check: Make sure that we do not have more
1937 languages than we have bits available to enumerate them. */
1938 gcc_assert ((1U << cl_lang_count) <= CL_MIN_OPTION_CLASS);
1940 /* If we have not done so already, obtain
1941 the desired maximum width of the output. */
1942 if (opts->x_help_columns == 0)
1944 opts->x_help_columns = get_terminal_width ();
1945 if (opts->x_help_columns == INT_MAX)
1946 /* Use a reasonable default. */
1947 opts->x_help_columns = 80;
1950 /* Decide upon the title for the options that we are going to display. */
1951 for (i = 0, flag = 1; flag <= CL_MAX_OPTION_CLASS; flag <<= 1, i ++)
1953 switch (flag & include_flags)
1955 case 0:
1956 case CL_DRIVER:
1957 break;
1959 case CL_TARGET:
1960 description = _("The following options are target specific");
1961 break;
1962 case CL_WARNING:
1963 description = _("The following options control compiler warning messages");
1964 break;
1965 case CL_OPTIMIZATION:
1966 description = _("The following options control optimizations");
1967 break;
1968 case CL_COMMON:
1969 description = _("The following options are language-independent");
1970 break;
1971 case CL_PARAMS:
1972 description = _("The following options control parameters");
1973 break;
1974 default:
1975 if (i >= cl_lang_count)
1976 break;
1977 if (exclude_flags & all_langs_mask)
1978 description = _("The following options are specific to just the language ");
1979 else
1980 description = _("The following options are supported by the language ");
1981 descrip_extra = lang_names [i];
1982 break;
1986 if (description == NULL)
1988 if (any_flags == 0)
1990 if (include_flags & CL_UNDOCUMENTED)
1991 description = _("The following options are not documented");
1992 else if (include_flags & CL_SEPARATE)
1993 description = _("The following options take separate arguments");
1994 else if (include_flags & CL_JOINED)
1995 description = _("The following options take joined arguments");
1996 else
1998 internal_error ("unrecognized %<include_flags 0x%x%> passed "
1999 "to %<print_specific_help%>",
2000 include_flags);
2001 return;
2004 else
2006 if (any_flags & all_langs_mask)
2007 description = _("The following options are language-related");
2008 else
2009 description = _("The following options are language-independent");
2013 printf ("%s%s:\n", description, descrip_extra);
2014 print_filtered_help (include_flags, exclude_flags, any_flags,
2015 opts->x_help_columns, opts, lang_mask);
2018 /* Enable FDO-related flags. */
2020 static void
2021 enable_fdo_optimizations (struct gcc_options *opts,
2022 struct gcc_options *opts_set,
2023 int value)
2025 SET_OPTION_IF_UNSET (opts, opts_set, flag_branch_probabilities, value);
2026 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_values, value);
2027 SET_OPTION_IF_UNSET (opts, opts_set, flag_unroll_loops, value);
2028 SET_OPTION_IF_UNSET (opts, opts_set, flag_peel_loops, value);
2029 SET_OPTION_IF_UNSET (opts, opts_set, flag_tracer, value);
2030 SET_OPTION_IF_UNSET (opts, opts_set, flag_value_profile_transformations,
2031 value);
2032 SET_OPTION_IF_UNSET (opts, opts_set, flag_inline_functions, value);
2033 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_cp, value);
2034 if (value)
2036 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_cp_clone, 1);
2037 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_bit_cp, 1);
2039 SET_OPTION_IF_UNSET (opts, opts_set, flag_predictive_commoning, value);
2040 SET_OPTION_IF_UNSET (opts, opts_set, flag_split_loops, value);
2041 SET_OPTION_IF_UNSET (opts, opts_set, flag_unswitch_loops, value);
2042 SET_OPTION_IF_UNSET (opts, opts_set, flag_gcse_after_reload, value);
2043 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_loop_vectorize, value);
2044 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_slp_vectorize, value);
2045 SET_OPTION_IF_UNSET (opts, opts_set, flag_version_loops_for_strides, value);
2046 SET_OPTION_IF_UNSET (opts, opts_set, flag_vect_cost_model,
2047 VECT_COST_MODEL_DYNAMIC);
2048 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_loop_distribute_patterns,
2049 value);
2050 SET_OPTION_IF_UNSET (opts, opts_set, flag_loop_interchange, value);
2051 SET_OPTION_IF_UNSET (opts, opts_set, flag_unroll_jam, value);
2052 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_loop_distribution, value);
2055 /* -f{,no-}sanitize{,-recover}= suboptions. */
2056 const struct sanitizer_opts_s sanitizer_opts[] =
2058 #define SANITIZER_OPT(name, flags, recover, trap) \
2059 { #name, flags, sizeof #name - 1, recover, trap }
2060 SANITIZER_OPT (address, (SANITIZE_ADDRESS | SANITIZE_USER_ADDRESS), true,
2061 false),
2062 SANITIZER_OPT (hwaddress, (SANITIZE_HWADDRESS | SANITIZE_USER_HWADDRESS),
2063 true, false),
2064 SANITIZER_OPT (kernel-address, (SANITIZE_ADDRESS | SANITIZE_KERNEL_ADDRESS),
2065 true, false),
2066 SANITIZER_OPT (kernel-hwaddress,
2067 (SANITIZE_HWADDRESS | SANITIZE_KERNEL_HWADDRESS),
2068 true, false),
2069 SANITIZER_OPT (pointer-compare, SANITIZE_POINTER_COMPARE, true, false),
2070 SANITIZER_OPT (pointer-subtract, SANITIZE_POINTER_SUBTRACT, true, false),
2071 SANITIZER_OPT (thread, SANITIZE_THREAD, false, false),
2072 SANITIZER_OPT (leak, SANITIZE_LEAK, false, false),
2073 SANITIZER_OPT (shift, SANITIZE_SHIFT, true, true),
2074 SANITIZER_OPT (shift-base, SANITIZE_SHIFT_BASE, true, true),
2075 SANITIZER_OPT (shift-exponent, SANITIZE_SHIFT_EXPONENT, true, true),
2076 SANITIZER_OPT (integer-divide-by-zero, SANITIZE_DIVIDE, true, true),
2077 SANITIZER_OPT (undefined, SANITIZE_UNDEFINED, true, true),
2078 SANITIZER_OPT (unreachable, SANITIZE_UNREACHABLE, false, true),
2079 SANITIZER_OPT (vla-bound, SANITIZE_VLA, true, true),
2080 SANITIZER_OPT (return, SANITIZE_RETURN, false, true),
2081 SANITIZER_OPT (null, SANITIZE_NULL, true, true),
2082 SANITIZER_OPT (signed-integer-overflow, SANITIZE_SI_OVERFLOW, true, true),
2083 SANITIZER_OPT (bool, SANITIZE_BOOL, true, true),
2084 SANITIZER_OPT (enum, SANITIZE_ENUM, true, true),
2085 SANITIZER_OPT (float-divide-by-zero, SANITIZE_FLOAT_DIVIDE, true, true),
2086 SANITIZER_OPT (float-cast-overflow, SANITIZE_FLOAT_CAST, true, true),
2087 SANITIZER_OPT (bounds, SANITIZE_BOUNDS, true, true),
2088 SANITIZER_OPT (bounds-strict, SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT, true,
2089 true),
2090 SANITIZER_OPT (alignment, SANITIZE_ALIGNMENT, true, true),
2091 SANITIZER_OPT (nonnull-attribute, SANITIZE_NONNULL_ATTRIBUTE, true, true),
2092 SANITIZER_OPT (returns-nonnull-attribute, SANITIZE_RETURNS_NONNULL_ATTRIBUTE,
2093 true, true),
2094 SANITIZER_OPT (object-size, SANITIZE_OBJECT_SIZE, true, true),
2095 SANITIZER_OPT (vptr, SANITIZE_VPTR, true, false),
2096 SANITIZER_OPT (pointer-overflow, SANITIZE_POINTER_OVERFLOW, true, true),
2097 SANITIZER_OPT (builtin, SANITIZE_BUILTIN, true, true),
2098 SANITIZER_OPT (shadow-call-stack, SANITIZE_SHADOW_CALL_STACK, false, false),
2099 SANITIZER_OPT (all, ~0U, true, true),
2100 #undef SANITIZER_OPT
2101 { NULL, 0U, 0UL, false, false }
2104 /* -fzero-call-used-regs= suboptions. */
2105 const struct zero_call_used_regs_opts_s zero_call_used_regs_opts[] =
2107 #define ZERO_CALL_USED_REGS_OPT(name, flags) \
2108 { #name, flags }
2109 ZERO_CALL_USED_REGS_OPT (skip, zero_regs_flags::SKIP),
2110 ZERO_CALL_USED_REGS_OPT (used-gpr-arg, zero_regs_flags::USED_GPR_ARG),
2111 ZERO_CALL_USED_REGS_OPT (used-gpr, zero_regs_flags::USED_GPR),
2112 ZERO_CALL_USED_REGS_OPT (used-arg, zero_regs_flags::USED_ARG),
2113 ZERO_CALL_USED_REGS_OPT (used, zero_regs_flags::USED),
2114 ZERO_CALL_USED_REGS_OPT (all-gpr-arg, zero_regs_flags::ALL_GPR_ARG),
2115 ZERO_CALL_USED_REGS_OPT (all-gpr, zero_regs_flags::ALL_GPR),
2116 ZERO_CALL_USED_REGS_OPT (all-arg, zero_regs_flags::ALL_ARG),
2117 ZERO_CALL_USED_REGS_OPT (all, zero_regs_flags::ALL),
2118 ZERO_CALL_USED_REGS_OPT (leafy-gpr-arg, zero_regs_flags::LEAFY_GPR_ARG),
2119 ZERO_CALL_USED_REGS_OPT (leafy-gpr, zero_regs_flags::LEAFY_GPR),
2120 ZERO_CALL_USED_REGS_OPT (leafy-arg, zero_regs_flags::LEAFY_ARG),
2121 ZERO_CALL_USED_REGS_OPT (leafy, zero_regs_flags::LEAFY),
2122 #undef ZERO_CALL_USED_REGS_OPT
2123 {NULL, 0U}
2126 /* A struct for describing a run of chars within a string. */
2128 class string_fragment
2130 public:
2131 string_fragment (const char *start, size_t len)
2132 : m_start (start), m_len (len) {}
2134 const char *m_start;
2135 size_t m_len;
2138 /* Specialization of edit_distance_traits for string_fragment,
2139 for use by get_closest_sanitizer_option. */
2141 template <>
2142 struct edit_distance_traits<const string_fragment &>
2144 static size_t get_length (const string_fragment &fragment)
2146 return fragment.m_len;
2149 static const char *get_string (const string_fragment &fragment)
2151 return fragment.m_start;
2155 /* Given ARG, an unrecognized sanitizer option, return the best
2156 matching sanitizer option, or NULL if there isn't one.
2157 OPTS is array of candidate sanitizer options.
2158 CODE is OPT_fsanitize_, OPT_fsanitize_recover_ or OPT_fsanitize_trap_.
2159 VALUE is non-zero for the regular form of the option, zero
2160 for the "no-" form (e.g. "-fno-sanitize-recover="). */
2162 static const char *
2163 get_closest_sanitizer_option (const string_fragment &arg,
2164 const struct sanitizer_opts_s *opts,
2165 enum opt_code code, int value)
2167 best_match <const string_fragment &, const char*> bm (arg);
2168 for (int i = 0; opts[i].name != NULL; ++i)
2170 /* -fsanitize=all is not valid, so don't offer it. */
2171 if (code == OPT_fsanitize_
2172 && opts[i].flag == ~0U
2173 && value)
2174 continue;
2176 /* For -fsanitize-recover= (and not -fno-sanitize-recover=),
2177 don't offer the non-recoverable options. */
2178 if (code == OPT_fsanitize_recover_
2179 && !opts[i].can_recover
2180 && value)
2181 continue;
2183 /* For -fsanitize-trap= (and not -fno-sanitize-trap=),
2184 don't offer the non-trapping options. */
2185 if (code == OPT_fsanitize_trap_
2186 && !opts[i].can_trap
2187 && value)
2188 continue;
2190 bm.consider (opts[i].name);
2192 return bm.get_best_meaningful_candidate ();
2195 /* Parse comma separated sanitizer suboptions from P for option SCODE,
2196 adjust previous FLAGS and return new ones. If COMPLAIN is false,
2197 don't issue diagnostics. */
2199 unsigned int
2200 parse_sanitizer_options (const char *p, location_t loc, int scode,
2201 unsigned int flags, int value, bool complain)
2203 enum opt_code code = (enum opt_code) scode;
2205 while (*p != 0)
2207 size_t len, i;
2208 bool found = false;
2209 const char *comma = strchr (p, ',');
2211 if (comma == NULL)
2212 len = strlen (p);
2213 else
2214 len = comma - p;
2215 if (len == 0)
2217 p = comma + 1;
2218 continue;
2221 /* Check to see if the string matches an option class name. */
2222 for (i = 0; sanitizer_opts[i].name != NULL; ++i)
2223 if (len == sanitizer_opts[i].len
2224 && memcmp (p, sanitizer_opts[i].name, len) == 0)
2226 /* Handle both -fsanitize and -fno-sanitize cases. */
2227 if (value && sanitizer_opts[i].flag == ~0U)
2229 if (code == OPT_fsanitize_)
2231 if (complain)
2232 error_at (loc, "%<-fsanitize=all%> option is not valid");
2234 else if (code == OPT_fsanitize_recover_)
2235 flags |= ~(SANITIZE_THREAD | SANITIZE_LEAK
2236 | SANITIZE_UNREACHABLE | SANITIZE_RETURN
2237 | SANITIZE_SHADOW_CALL_STACK);
2238 else /* if (code == OPT_fsanitize_trap_) */
2239 flags |= (SANITIZE_UNDEFINED
2240 | SANITIZE_UNDEFINED_NONDEFAULT);
2242 else if (value)
2244 /* Do not enable -fsanitize-recover=unreachable and
2245 -fsanitize-recover=return if -fsanitize-recover=undefined
2246 is selected. */
2247 if (code == OPT_fsanitize_recover_
2248 && sanitizer_opts[i].flag == SANITIZE_UNDEFINED)
2249 flags |= (SANITIZE_UNDEFINED
2250 & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN));
2251 else if (code == OPT_fsanitize_trap_
2252 && sanitizer_opts[i].flag == SANITIZE_VPTR)
2253 error_at (loc, "%<-fsanitize-trap=%s%> is not supported",
2254 sanitizer_opts[i].name);
2255 else
2256 flags |= sanitizer_opts[i].flag;
2258 else
2260 flags &= ~sanitizer_opts[i].flag;
2261 /* Don't always clear SANITIZE_ADDRESS if it was previously
2262 set: -fsanitize=address -fno-sanitize=kernel-address should
2263 leave SANITIZE_ADDRESS set. */
2264 if (flags & (SANITIZE_KERNEL_ADDRESS | SANITIZE_USER_ADDRESS))
2265 flags |= SANITIZE_ADDRESS;
2267 found = true;
2268 break;
2271 if (! found && complain)
2273 const char *hint
2274 = get_closest_sanitizer_option (string_fragment (p, len),
2275 sanitizer_opts, code, value);
2277 const char *suffix;
2278 if (code == OPT_fsanitize_recover_)
2279 suffix = "-recover";
2280 else if (code == OPT_fsanitize_trap_)
2281 suffix = "-trap";
2282 else
2283 suffix = "";
2285 if (hint)
2286 error_at (loc,
2287 "unrecognized argument to %<-f%ssanitize%s=%> "
2288 "option: %q.*s; did you mean %qs?",
2289 value ? "" : "no-",
2290 suffix, (int) len, p, hint);
2291 else
2292 error_at (loc,
2293 "unrecognized argument to %<-f%ssanitize%s=%> option: "
2294 "%q.*s", value ? "" : "no-",
2295 suffix, (int) len, p);
2298 if (comma == NULL)
2299 break;
2300 p = comma + 1;
2302 return flags;
2305 /* Parse string values of no_sanitize attribute passed in VALUE.
2306 Values are separated with comma. */
2308 unsigned int
2309 parse_no_sanitize_attribute (char *value)
2311 unsigned int flags = 0;
2312 unsigned int i;
2313 char *q = strtok (value, ",");
2315 while (q != NULL)
2317 for (i = 0; sanitizer_opts[i].name != NULL; ++i)
2318 if (strcmp (sanitizer_opts[i].name, q) == 0)
2320 flags |= sanitizer_opts[i].flag;
2321 if (sanitizer_opts[i].flag == SANITIZE_UNDEFINED)
2322 flags |= SANITIZE_UNDEFINED_NONDEFAULT;
2323 break;
2326 if (sanitizer_opts[i].name == NULL)
2327 warning (OPT_Wattributes,
2328 "%qs attribute directive ignored", q);
2330 q = strtok (NULL, ",");
2333 return flags;
2336 /* Parse -fzero-call-used-regs suboptions from ARG, return the FLAGS. */
2338 unsigned int
2339 parse_zero_call_used_regs_options (const char *arg)
2341 unsigned int flags = 0;
2343 /* Check to see if the string matches a sub-option name. */
2344 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
2345 if (strcmp (arg, zero_call_used_regs_opts[i].name) == 0)
2347 flags = zero_call_used_regs_opts[i].flag;
2348 break;
2351 if (!flags)
2352 error ("unrecognized argument to %<-fzero-call-used-regs=%>: %qs", arg);
2354 return flags;
2357 /* Parse -falign-NAME format for a FLAG value. Return individual
2358 parsed integer values into RESULT_VALUES array. If REPORT_ERROR is
2359 set, print error message at LOC location. */
2361 bool
2362 parse_and_check_align_values (const char *flag,
2363 const char *name,
2364 auto_vec<unsigned> &result_values,
2365 bool report_error,
2366 location_t loc)
2368 char *str = xstrdup (flag);
2369 for (char *p = strtok (str, ":"); p; p = strtok (NULL, ":"))
2371 char *end;
2372 int v = strtol (p, &end, 10);
2373 if (*end != '\0' || v < 0)
2375 if (report_error)
2376 error_at (loc, "invalid arguments for %<-falign-%s%> option: %qs",
2377 name, flag);
2379 return false;
2382 result_values.safe_push ((unsigned)v);
2385 free (str);
2387 /* Check that we have a correct number of values. */
2388 if (result_values.is_empty () || result_values.length () > 4)
2390 if (report_error)
2391 error_at (loc, "invalid number of arguments for %<-falign-%s%> "
2392 "option: %qs", name, flag);
2393 return false;
2396 for (unsigned i = 0; i < result_values.length (); i++)
2397 if (result_values[i] > MAX_CODE_ALIGN_VALUE)
2399 if (report_error)
2400 error_at (loc, "%<-falign-%s%> is not between 0 and %d",
2401 name, MAX_CODE_ALIGN_VALUE);
2402 return false;
2405 return true;
2408 /* Check that alignment value FLAG for -falign-NAME is valid at a given
2409 location LOC. OPT_STR points to the stored -falign-NAME=argument and
2410 OPT_FLAG points to the associated -falign-NAME on/off flag. */
2412 static void
2413 check_alignment_argument (location_t loc, const char *flag, const char *name,
2414 int *opt_flag, const char **opt_str)
2416 auto_vec<unsigned> align_result;
2417 parse_and_check_align_values (flag, name, align_result, true, loc);
2419 if (align_result.length() >= 1 && align_result[0] == 0)
2421 *opt_flag = 1;
2422 *opt_str = NULL;
2426 /* Parse argument of -fpatchable-function-entry option ARG and store
2427 corresponding values to PATCH_AREA_SIZE and PATCH_AREA_START.
2428 If REPORT_ERROR is set to true, generate error for a problematic
2429 option arguments. */
2431 void
2432 parse_and_check_patch_area (const char *arg, bool report_error,
2433 HOST_WIDE_INT *patch_area_size,
2434 HOST_WIDE_INT *patch_area_start)
2436 *patch_area_size = 0;
2437 *patch_area_start = 0;
2439 if (arg == NULL)
2440 return;
2442 char *patch_area_arg = xstrdup (arg);
2443 char *comma = strchr (patch_area_arg, ',');
2444 if (comma)
2446 *comma = '\0';
2447 *patch_area_size = integral_argument (patch_area_arg);
2448 *patch_area_start = integral_argument (comma + 1);
2450 else
2451 *patch_area_size = integral_argument (patch_area_arg);
2453 if (*patch_area_size < 0
2454 || *patch_area_size > USHRT_MAX
2455 || *patch_area_start < 0
2456 || *patch_area_start > USHRT_MAX
2457 || *patch_area_size < *patch_area_start)
2458 if (report_error)
2459 error ("invalid arguments for %<-fpatchable-function-entry%>");
2461 free (patch_area_arg);
2464 /* Print help when OPT__help_ is set. */
2466 void
2467 print_help (struct gcc_options *opts, unsigned int lang_mask,
2468 const char *help_option_argument)
2470 const char *a = help_option_argument;
2471 unsigned int include_flags = 0;
2472 /* Note - by default we include undocumented options when listing
2473 specific classes. If you only want to see documented options
2474 then add ",^undocumented" to the --help= option. E.g.:
2476 --help=target,^undocumented */
2477 unsigned int exclude_flags = 0;
2479 if (lang_mask == CL_DRIVER)
2480 return;
2482 /* Walk along the argument string, parsing each word in turn.
2483 The format is:
2484 arg = [^]{word}[,{arg}]
2485 word = {optimizers|target|warnings|undocumented|
2486 params|common|<language>} */
2487 while (*a != 0)
2489 static const struct
2491 const char *string;
2492 unsigned int flag;
2494 specifics[] =
2496 { "optimizers", CL_OPTIMIZATION },
2497 { "target", CL_TARGET },
2498 { "warnings", CL_WARNING },
2499 { "undocumented", CL_UNDOCUMENTED },
2500 { "params", CL_PARAMS },
2501 { "joined", CL_JOINED },
2502 { "separate", CL_SEPARATE },
2503 { "common", CL_COMMON },
2504 { NULL, 0 }
2506 unsigned int *pflags;
2507 const char *comma;
2508 unsigned int lang_flag, specific_flag;
2509 unsigned int len;
2510 unsigned int i;
2512 if (*a == '^')
2514 ++a;
2515 if (*a == '\0')
2517 error ("missing argument to %qs", "--help=^");
2518 break;
2520 pflags = &exclude_flags;
2522 else
2523 pflags = &include_flags;
2525 comma = strchr (a, ',');
2526 if (comma == NULL)
2527 len = strlen (a);
2528 else
2529 len = comma - a;
2530 if (len == 0)
2532 a = comma + 1;
2533 continue;
2536 /* Check to see if the string matches an option class name. */
2537 for (i = 0, specific_flag = 0; specifics[i].string != NULL; i++)
2538 if (strncasecmp (a, specifics[i].string, len) == 0)
2540 specific_flag = specifics[i].flag;
2541 break;
2544 /* Check to see if the string matches a language name.
2545 Note - we rely upon the alpha-sorted nature of the entries in
2546 the lang_names array, specifically that shorter names appear
2547 before their longer variants. (i.e. C before C++). That way
2548 when we are attempting to match --help=c for example we will
2549 match with C first and not C++. */
2550 for (i = 0, lang_flag = 0; i < cl_lang_count; i++)
2551 if (strncasecmp (a, lang_names[i], len) == 0)
2553 lang_flag = 1U << i;
2554 break;
2557 if (specific_flag != 0)
2559 if (lang_flag == 0)
2560 *pflags |= specific_flag;
2561 else
2563 /* The option's argument matches both the start of a
2564 language name and the start of an option class name.
2565 We have a special case for when the user has
2566 specified "--help=c", but otherwise we have to issue
2567 a warning. */
2568 if (strncasecmp (a, "c", len) == 0)
2569 *pflags |= lang_flag;
2570 else
2571 warning (0,
2572 "%<--help%> argument %q.*s is ambiguous, "
2573 "please be more specific",
2574 len, a);
2577 else if (lang_flag != 0)
2578 *pflags |= lang_flag;
2579 else
2580 warning (0,
2581 "unrecognized argument to %<--help=%> option: %q.*s",
2582 len, a);
2584 if (comma == NULL)
2585 break;
2586 a = comma + 1;
2589 /* We started using PerFunction/Optimization for parameters and
2590 a warning. We should exclude these from optimization options. */
2591 if (include_flags & CL_OPTIMIZATION)
2592 exclude_flags |= CL_WARNING;
2593 if (!(include_flags & CL_PARAMS))
2594 exclude_flags |= CL_PARAMS;
2596 if (include_flags)
2597 print_specific_help (include_flags, exclude_flags, 0, opts,
2598 lang_mask);
2601 /* Handle target- and language-independent options. Return zero to
2602 generate an "unknown option" message. Only options that need
2603 extra handling need to be listed here; if you simply want
2604 DECODED->value assigned to a variable, it happens automatically. */
2606 bool
2607 common_handle_option (struct gcc_options *opts,
2608 struct gcc_options *opts_set,
2609 const struct cl_decoded_option *decoded,
2610 unsigned int lang_mask, int kind ATTRIBUTE_UNUSED,
2611 location_t loc,
2612 const struct cl_option_handlers *handlers,
2613 diagnostic_context *dc,
2614 void (*target_option_override_hook) (void))
2616 size_t scode = decoded->opt_index;
2617 const char *arg = decoded->arg;
2618 HOST_WIDE_INT value = decoded->value;
2619 enum opt_code code = (enum opt_code) scode;
2621 gcc_assert (decoded->canonical_option_num_elements <= 2);
2623 switch (code)
2625 case OPT__help:
2627 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
2628 unsigned int undoc_mask;
2629 unsigned int i;
2631 if (lang_mask == CL_DRIVER)
2632 break;
2634 undoc_mask = ((opts->x_verbose_flag | opts->x_extra_warnings)
2636 : CL_UNDOCUMENTED);
2637 target_option_override_hook ();
2638 /* First display any single language specific options. */
2639 for (i = 0; i < cl_lang_count; i++)
2640 print_specific_help
2641 (1U << i, (all_langs_mask & (~ (1U << i))) | undoc_mask, 0, opts,
2642 lang_mask);
2643 /* Next display any multi language specific options. */
2644 print_specific_help (0, undoc_mask, all_langs_mask, opts, lang_mask);
2645 /* Then display any remaining, non-language options. */
2646 for (i = CL_MIN_OPTION_CLASS; i <= CL_MAX_OPTION_CLASS; i <<= 1)
2647 if (i != CL_DRIVER)
2648 print_specific_help (i, undoc_mask, 0, opts, lang_mask);
2649 opts->x_exit_after_options = true;
2650 break;
2653 case OPT__target_help:
2654 if (lang_mask == CL_DRIVER)
2655 break;
2657 target_option_override_hook ();
2658 print_specific_help (CL_TARGET, 0, 0, opts, lang_mask);
2659 opts->x_exit_after_options = true;
2660 break;
2662 case OPT__help_:
2664 help_option_arguments.safe_push (arg);
2665 opts->x_exit_after_options = true;
2666 break;
2669 case OPT__version:
2670 if (lang_mask == CL_DRIVER)
2671 break;
2673 opts->x_exit_after_options = true;
2674 break;
2676 case OPT__completion_:
2677 break;
2679 case OPT_fsanitize_:
2680 opts_set->x_flag_sanitize = true;
2681 opts->x_flag_sanitize
2682 = parse_sanitizer_options (arg, loc, code,
2683 opts->x_flag_sanitize, value, true);
2685 /* Kernel ASan implies normal ASan but does not yet support
2686 all features. */
2687 if (opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS)
2689 SET_OPTION_IF_UNSET (opts, opts_set,
2690 param_asan_instrumentation_with_call_threshold,
2692 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_globals, 0);
2693 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_stack, 0);
2694 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_protect_allocas, 0);
2695 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_use_after_return, 0);
2697 if (opts->x_flag_sanitize & SANITIZE_KERNEL_HWADDRESS)
2699 SET_OPTION_IF_UNSET (opts, opts_set,
2700 param_hwasan_instrument_stack, 0);
2701 SET_OPTION_IF_UNSET (opts, opts_set,
2702 param_hwasan_random_frame_tag, 0);
2703 SET_OPTION_IF_UNSET (opts, opts_set,
2704 param_hwasan_instrument_allocas, 0);
2706 break;
2708 case OPT_fsanitize_recover_:
2709 opts->x_flag_sanitize_recover
2710 = parse_sanitizer_options (arg, loc, code,
2711 opts->x_flag_sanitize_recover, value, true);
2712 break;
2714 case OPT_fsanitize_trap_:
2715 opts->x_flag_sanitize_trap
2716 = parse_sanitizer_options (arg, loc, code,
2717 opts->x_flag_sanitize_trap, value, true);
2718 break;
2720 case OPT_fasan_shadow_offset_:
2721 /* Deferred. */
2722 break;
2724 case OPT_fsanitize_address_use_after_scope:
2725 opts->x_flag_sanitize_address_use_after_scope = value;
2726 break;
2728 case OPT_fsanitize_recover:
2729 if (value)
2730 opts->x_flag_sanitize_recover
2731 |= (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT)
2732 & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN);
2733 else
2734 opts->x_flag_sanitize_recover
2735 &= ~(SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2736 break;
2738 case OPT_fsanitize_trap:
2739 if (value)
2740 opts->x_flag_sanitize_trap
2741 |= (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2742 else
2743 opts->x_flag_sanitize_trap
2744 &= ~(SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2745 break;
2747 case OPT_O:
2748 case OPT_Os:
2749 case OPT_Ofast:
2750 case OPT_Og:
2751 case OPT_Oz:
2752 /* Currently handled in a prescan. */
2753 break;
2755 case OPT_Wattributes_:
2756 if (lang_mask == CL_DRIVER)
2757 break;
2759 if (value)
2761 error_at (loc, "arguments ignored for %<-Wattributes=%>; use "
2762 "%<-Wno-attributes=%> instead");
2763 break;
2765 else if (arg[strlen (arg) - 1] == ',')
2767 error_at (loc, "trailing %<,%> in arguments for "
2768 "%<-Wno-attributes=%>");
2769 break;
2772 add_comma_separated_to_vector (&opts->x_flag_ignored_attributes, arg);
2773 break;
2775 case OPT_Werror:
2776 dc->warning_as_error_requested = value;
2777 break;
2779 case OPT_Werror_:
2780 if (lang_mask == CL_DRIVER)
2781 break;
2783 enable_warning_as_error (arg, value, lang_mask, handlers,
2784 opts, opts_set, loc, dc);
2785 break;
2787 case OPT_Wfatal_errors:
2788 dc->fatal_errors = value;
2789 break;
2791 case OPT_Wstack_usage_:
2792 opts->x_flag_stack_usage_info = value != -1;
2793 break;
2795 case OPT_Wstrict_aliasing:
2796 set_Wstrict_aliasing (opts, value);
2797 break;
2799 case OPT_Wstrict_overflow:
2800 opts->x_warn_strict_overflow = (value
2801 ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
2802 : 0);
2803 break;
2805 case OPT_Wsystem_headers:
2806 dc->dc_warn_system_headers = value;
2807 break;
2809 case OPT_aux_info:
2810 opts->x_flag_gen_aux_info = 1;
2811 break;
2813 case OPT_d:
2814 decode_d_option (arg, opts, loc, dc);
2815 break;
2817 case OPT_fcall_used_:
2818 case OPT_fcall_saved_:
2819 /* Deferred. */
2820 break;
2822 case OPT_fdbg_cnt_:
2823 /* Deferred. */
2824 break;
2826 case OPT_fdebug_prefix_map_:
2827 case OPT_ffile_prefix_map_:
2828 case OPT_fprofile_prefix_map_:
2829 /* Deferred. */
2830 break;
2832 case OPT_fcanon_prefix_map:
2833 flag_canon_prefix_map = value;
2834 break;
2836 case OPT_fcallgraph_info:
2837 opts->x_flag_callgraph_info = CALLGRAPH_INFO_NAKED;
2838 break;
2840 case OPT_fcallgraph_info_:
2842 char *my_arg, *p;
2843 my_arg = xstrdup (arg);
2844 p = strtok (my_arg, ",");
2845 while (p)
2847 if (strcmp (p, "su") == 0)
2849 opts->x_flag_callgraph_info |= CALLGRAPH_INFO_STACK_USAGE;
2850 opts->x_flag_stack_usage_info = true;
2852 else if (strcmp (p, "da") == 0)
2853 opts->x_flag_callgraph_info |= CALLGRAPH_INFO_DYNAMIC_ALLOC;
2854 else
2855 return 0;
2856 p = strtok (NULL, ",");
2858 free (my_arg);
2860 break;
2862 case OPT_fdiagnostics_show_location_:
2863 diagnostic_prefixing_rule (dc) = (diagnostic_prefixing_rule_t) value;
2864 break;
2866 case OPT_fdiagnostics_show_caret:
2867 dc->show_caret = value;
2868 break;
2870 case OPT_fdiagnostics_show_labels:
2871 dc->show_labels_p = value;
2872 break;
2874 case OPT_fdiagnostics_show_line_numbers:
2875 dc->show_line_numbers_p = value;
2876 break;
2878 case OPT_fdiagnostics_color_:
2879 diagnostic_color_init (dc, value);
2880 break;
2882 case OPT_fdiagnostics_urls_:
2883 diagnostic_urls_init (dc, value);
2884 break;
2886 case OPT_fdiagnostics_format_:
2888 const char *basename = (opts->x_dump_base_name ? opts->x_dump_base_name
2889 : opts->x_main_input_basename);
2890 diagnostic_output_format_init (dc, basename,
2891 (enum diagnostics_output_format)value);
2892 break;
2895 case OPT_fdiagnostics_text_art_charset_:
2896 diagnostics_text_art_charset_init (dc,
2897 (enum diagnostic_text_art_charset)value);
2898 break;
2900 case OPT_fdiagnostics_parseable_fixits:
2901 dc->extra_output_kind = (value
2902 ? EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1
2903 : EXTRA_DIAGNOSTIC_OUTPUT_none);
2904 break;
2906 case OPT_fdiagnostics_column_unit_:
2907 dc->column_unit = (enum diagnostics_column_unit)value;
2908 break;
2910 case OPT_fdiagnostics_column_origin_:
2911 dc->column_origin = value;
2912 break;
2914 case OPT_fdiagnostics_escape_format_:
2915 dc->escape_format = (enum diagnostics_escape_format)value;
2916 break;
2918 case OPT_fdiagnostics_show_cwe:
2919 dc->show_cwe = value;
2920 break;
2922 case OPT_fdiagnostics_show_rules:
2923 dc->show_rules = value;
2924 break;
2926 case OPT_fdiagnostics_path_format_:
2927 dc->path_format = (enum diagnostic_path_format)value;
2928 break;
2930 case OPT_fdiagnostics_show_path_depths:
2931 dc->show_path_depths = value;
2932 break;
2934 case OPT_fdiagnostics_show_option:
2935 dc->show_option_requested = value;
2936 break;
2938 case OPT_fdiagnostics_minimum_margin_width_:
2939 dc->min_margin_width = value;
2940 break;
2942 case OPT_fdump_:
2943 /* Deferred. */
2944 break;
2946 case OPT_ffast_math:
2947 set_fast_math_flags (opts, value);
2948 break;
2950 case OPT_funsafe_math_optimizations:
2951 set_unsafe_math_optimizations_flags (opts, value);
2952 break;
2954 case OPT_ffixed_:
2955 /* Deferred. */
2956 break;
2958 case OPT_finline_limit_:
2959 SET_OPTION_IF_UNSET (opts, opts_set, param_max_inline_insns_single,
2960 value / 2);
2961 SET_OPTION_IF_UNSET (opts, opts_set, param_max_inline_insns_auto,
2962 value / 2);
2963 break;
2965 case OPT_finstrument_functions_exclude_function_list_:
2966 add_comma_separated_to_vector
2967 (&opts->x_flag_instrument_functions_exclude_functions, arg);
2968 break;
2970 case OPT_finstrument_functions_exclude_file_list_:
2971 add_comma_separated_to_vector
2972 (&opts->x_flag_instrument_functions_exclude_files, arg);
2973 break;
2975 case OPT_fmessage_length_:
2976 pp_set_line_maximum_length (dc->printer, value);
2977 diagnostic_set_caret_max_width (dc, value);
2978 break;
2980 case OPT_fopt_info:
2981 case OPT_fopt_info_:
2982 /* Deferred. */
2983 break;
2985 case OPT_foffload_options_:
2986 /* Deferred. */
2987 break;
2989 case OPT_foffload_abi_:
2990 #ifdef ACCEL_COMPILER
2991 /* Handled in the 'mkoffload's. */
2992 #else
2993 error_at (loc, "%<-foffload-abi%> option can be specified only for "
2994 "offload compiler");
2995 #endif
2996 break;
2998 case OPT_fpack_struct_:
2999 if (value <= 0 || (value & (value - 1)) || value > 16)
3000 error_at (loc,
3001 "structure alignment must be a small power of two, not %wu",
3002 value);
3003 else
3004 opts->x_initial_max_fld_align = value;
3005 break;
3007 case OPT_fplugin_:
3008 case OPT_fplugin_arg_:
3009 /* Deferred. */
3010 break;
3012 case OPT_fprofile_use_:
3013 opts->x_profile_data_prefix = xstrdup (arg);
3014 opts->x_flag_profile_use = true;
3015 value = true;
3016 /* No break here - do -fprofile-use processing. */
3017 /* FALLTHRU */
3018 case OPT_fprofile_use:
3019 enable_fdo_optimizations (opts, opts_set, value);
3020 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_reorder_functions,
3021 value);
3022 /* Indirect call profiling should do all useful transformations
3023 speculative devirtualization does. */
3024 if (opts->x_flag_value_profile_transformations)
3025 SET_OPTION_IF_UNSET (opts, opts_set, flag_devirtualize_speculatively,
3026 false);
3027 break;
3029 case OPT_fauto_profile_:
3030 opts->x_auto_profile_file = xstrdup (arg);
3031 opts->x_flag_auto_profile = true;
3032 value = true;
3033 /* No break here - do -fauto-profile processing. */
3034 /* FALLTHRU */
3035 case OPT_fauto_profile:
3036 enable_fdo_optimizations (opts, opts_set, value);
3037 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_correction, value);
3038 break;
3040 case OPT_fprofile_generate_:
3041 opts->x_profile_data_prefix = xstrdup (arg);
3042 value = true;
3043 /* No break here - do -fprofile-generate processing. */
3044 /* FALLTHRU */
3045 case OPT_fprofile_generate:
3046 SET_OPTION_IF_UNSET (opts, opts_set, profile_arc_flag, value);
3047 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_values, value);
3048 SET_OPTION_IF_UNSET (opts, opts_set, flag_inline_functions, value);
3049 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_bit_cp, value);
3050 break;
3052 case OPT_fprofile_info_section:
3053 opts->x_profile_info_section = ".gcov_info";
3054 break;
3056 case OPT_fpatchable_function_entry_:
3058 HOST_WIDE_INT patch_area_size, patch_area_start;
3059 parse_and_check_patch_area (arg, true, &patch_area_size,
3060 &patch_area_start);
3062 break;
3064 case OPT_ftree_vectorize:
3065 /* Automatically sets -ftree-loop-vectorize and
3066 -ftree-slp-vectorize. Nothing more to do here. */
3067 break;
3068 case OPT_fzero_call_used_regs_:
3069 opts->x_flag_zero_call_used_regs
3070 = parse_zero_call_used_regs_options (arg);
3071 break;
3073 case OPT_fshow_column:
3074 dc->show_column = value;
3075 break;
3077 case OPT_frandom_seed:
3078 /* The real switch is -fno-random-seed. */
3079 if (value)
3080 return false;
3081 /* Deferred. */
3082 break;
3084 case OPT_frandom_seed_:
3085 /* Deferred. */
3086 break;
3088 case OPT_fsched_verbose_:
3089 #ifdef INSN_SCHEDULING
3090 /* Handled with Var in common.opt. */
3091 break;
3092 #else
3093 return false;
3094 #endif
3096 case OPT_fsched_stalled_insns_:
3097 opts->x_flag_sched_stalled_insns = value;
3098 if (opts->x_flag_sched_stalled_insns == 0)
3099 opts->x_flag_sched_stalled_insns = -1;
3100 break;
3102 case OPT_fsched_stalled_insns_dep_:
3103 opts->x_flag_sched_stalled_insns_dep = value;
3104 break;
3106 case OPT_fstack_check_:
3107 if (!strcmp (arg, "no"))
3108 opts->x_flag_stack_check = NO_STACK_CHECK;
3109 else if (!strcmp (arg, "generic"))
3110 /* This is the old stack checking method. */
3111 opts->x_flag_stack_check = STACK_CHECK_BUILTIN
3112 ? FULL_BUILTIN_STACK_CHECK
3113 : GENERIC_STACK_CHECK;
3114 else if (!strcmp (arg, "specific"))
3115 /* This is the new stack checking method. */
3116 opts->x_flag_stack_check = STACK_CHECK_BUILTIN
3117 ? FULL_BUILTIN_STACK_CHECK
3118 : STACK_CHECK_STATIC_BUILTIN
3119 ? STATIC_BUILTIN_STACK_CHECK
3120 : GENERIC_STACK_CHECK;
3121 else
3122 warning_at (loc, 0, "unknown stack check parameter %qs", arg);
3123 break;
3125 case OPT_fstack_limit:
3126 /* The real switch is -fno-stack-limit. */
3127 if (value)
3128 return false;
3129 /* Deferred. */
3130 break;
3132 case OPT_fstack_limit_register_:
3133 case OPT_fstack_limit_symbol_:
3134 /* Deferred. */
3135 break;
3137 case OPT_fstack_usage:
3138 opts->x_flag_stack_usage = value;
3139 opts->x_flag_stack_usage_info = value != 0;
3140 break;
3142 case OPT_g:
3143 set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg, opts, opts_set,
3144 loc);
3145 break;
3147 case OPT_gcodeview:
3148 break;
3150 case OPT_gbtf:
3151 set_debug_level (BTF_DEBUG, false, arg, opts, opts_set, loc);
3152 /* set the debug level to level 2, but if already at level 3,
3153 don't lower it. */
3154 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
3155 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3156 break;
3158 case OPT_gctf:
3159 set_debug_level (CTF_DEBUG, false, arg, opts, opts_set, loc);
3160 /* CTF generation feeds off DWARF dies. For optimal CTF, switch debug
3161 info level to 2. If off or at level 1, set it to level 2, but if
3162 already at level 3, don't lower it. */
3163 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL
3164 && opts->x_ctf_debug_info_level > CTFINFO_LEVEL_NONE)
3165 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3166 break;
3168 case OPT_gdwarf:
3169 if (arg && strlen (arg) != 0)
3171 error_at (loc, "%<-gdwarf%s%> is ambiguous; "
3172 "use %<-gdwarf-%s%> for DWARF version "
3173 "or %<-gdwarf%> %<-g%s%> for debug level", arg, arg, arg);
3174 break;
3176 else
3177 value = opts->x_dwarf_version;
3179 /* FALLTHRU */
3180 case OPT_gdwarf_:
3181 if (value < 2 || value > 5)
3182 error_at (loc, "dwarf version %wu is not supported", value);
3183 else
3184 opts->x_dwarf_version = value;
3185 set_debug_level (DWARF2_DEBUG, false, "", opts, opts_set, loc);
3186 break;
3188 case OPT_ggdb:
3189 set_debug_level (NO_DEBUG, 2, arg, opts, opts_set, loc);
3190 break;
3192 case OPT_gvms:
3193 set_debug_level (VMS_DEBUG, false, arg, opts, opts_set, loc);
3194 break;
3196 case OPT_gz:
3197 case OPT_gz_:
3198 /* Handled completely via specs. */
3199 break;
3201 case OPT_pedantic_errors:
3202 dc->pedantic_errors = 1;
3203 control_warning_option (OPT_Wpedantic, DK_ERROR, NULL, value,
3204 loc, lang_mask,
3205 handlers, opts, opts_set,
3206 dc);
3207 break;
3209 case OPT_flto:
3210 opts->x_flag_lto = value ? "" : NULL;
3211 break;
3213 case OPT_flto_:
3214 if (strcmp (arg, "none") != 0
3215 && strcmp (arg, "jobserver") != 0
3216 && strcmp (arg, "auto") != 0
3217 && atoi (arg) == 0)
3218 error_at (loc,
3219 "unrecognized argument to %<-flto=%> option: %qs", arg);
3220 break;
3222 case OPT_w:
3223 dc->dc_inhibit_warnings = true;
3224 break;
3226 case OPT_fmax_errors_:
3227 dc->max_errors = value;
3228 break;
3230 case OPT_fuse_ld_bfd:
3231 case OPT_fuse_ld_gold:
3232 case OPT_fuse_ld_lld:
3233 case OPT_fuse_ld_mold:
3234 case OPT_fuse_linker_plugin:
3235 /* No-op. Used by the driver and passed to us because it starts with f.*/
3236 break;
3238 case OPT_fwrapv:
3239 if (value)
3240 opts->x_flag_trapv = 0;
3241 break;
3243 case OPT_ftrapv:
3244 if (value)
3245 opts->x_flag_wrapv = 0;
3246 break;
3248 case OPT_fstrict_overflow:
3249 opts->x_flag_wrapv = !value;
3250 opts->x_flag_wrapv_pointer = !value;
3251 if (!value)
3252 opts->x_flag_trapv = 0;
3253 break;
3255 case OPT_fipa_icf:
3256 opts->x_flag_ipa_icf_functions = value;
3257 opts->x_flag_ipa_icf_variables = value;
3258 break;
3260 case OPT_falign_loops_:
3261 check_alignment_argument (loc, arg, "loops",
3262 &opts->x_flag_align_loops,
3263 &opts->x_str_align_loops);
3264 break;
3266 case OPT_falign_jumps_:
3267 check_alignment_argument (loc, arg, "jumps",
3268 &opts->x_flag_align_jumps,
3269 &opts->x_str_align_jumps);
3270 break;
3272 case OPT_falign_labels_:
3273 check_alignment_argument (loc, arg, "labels",
3274 &opts->x_flag_align_labels,
3275 &opts->x_str_align_labels);
3276 break;
3278 case OPT_falign_functions_:
3279 check_alignment_argument (loc, arg, "functions",
3280 &opts->x_flag_align_functions,
3281 &opts->x_str_align_functions);
3282 break;
3284 case OPT_ftabstop_:
3285 /* It is documented that we silently ignore silly values. */
3286 if (value >= 1 && value <= 100)
3287 dc->tabstop = value;
3288 break;
3290 case OPT_freport_bug:
3291 dc->report_bug = value;
3292 break;
3294 case OPT_fmultiflags:
3295 gcc_checking_assert (lang_mask == CL_DRIVER);
3296 break;
3298 default:
3299 /* If the flag was handled in a standard way, assume the lack of
3300 processing here is intentional. */
3301 gcc_assert (option_flag_var (scode, opts));
3302 break;
3305 common_handle_option_auto (opts, opts_set, decoded, lang_mask, kind,
3306 loc, handlers, dc);
3307 return true;
3310 /* Used to set the level of strict aliasing warnings in OPTS,
3311 when no level is specified (i.e., when -Wstrict-aliasing, and not
3312 -Wstrict-aliasing=level was given).
3313 ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified,
3314 and 0 otherwise. After calling this function, wstrict_aliasing will be
3315 set to the default value of -Wstrict_aliasing=level, currently 3. */
3316 static void
3317 set_Wstrict_aliasing (struct gcc_options *opts, int onoff)
3319 gcc_assert (onoff == 0 || onoff == 1);
3320 if (onoff != 0)
3321 opts->x_warn_strict_aliasing = 3;
3322 else
3323 opts->x_warn_strict_aliasing = 0;
3326 /* The following routines are useful in setting all the flags that
3327 -ffast-math and -fno-fast-math imply. */
3328 static void
3329 set_fast_math_flags (struct gcc_options *opts, int set)
3331 if (!opts->frontend_set_flag_unsafe_math_optimizations)
3333 opts->x_flag_unsafe_math_optimizations = set;
3334 set_unsafe_math_optimizations_flags (opts, set);
3336 if (!opts->frontend_set_flag_finite_math_only)
3337 opts->x_flag_finite_math_only = set;
3338 if (!opts->frontend_set_flag_errno_math)
3339 opts->x_flag_errno_math = !set;
3340 if (set)
3342 if (opts->frontend_set_flag_excess_precision == EXCESS_PRECISION_DEFAULT)
3343 opts->x_flag_excess_precision
3344 = set ? EXCESS_PRECISION_FAST : EXCESS_PRECISION_DEFAULT;
3345 if (!opts->frontend_set_flag_signaling_nans)
3346 opts->x_flag_signaling_nans = 0;
3347 if (!opts->frontend_set_flag_rounding_math)
3348 opts->x_flag_rounding_math = 0;
3349 if (!opts->frontend_set_flag_cx_limited_range)
3350 opts->x_flag_cx_limited_range = 1;
3354 /* When -funsafe-math-optimizations is set the following
3355 flags are set as well. */
3356 static void
3357 set_unsafe_math_optimizations_flags (struct gcc_options *opts, int set)
3359 if (!opts->frontend_set_flag_trapping_math)
3360 opts->x_flag_trapping_math = !set;
3361 if (!opts->frontend_set_flag_signed_zeros)
3362 opts->x_flag_signed_zeros = !set;
3363 if (!opts->frontend_set_flag_associative_math)
3364 opts->x_flag_associative_math = set;
3365 if (!opts->frontend_set_flag_reciprocal_math)
3366 opts->x_flag_reciprocal_math = set;
3369 /* Return true iff flags in OPTS are set as if -ffast-math. */
3370 bool
3371 fast_math_flags_set_p (const struct gcc_options *opts)
3373 return (!opts->x_flag_trapping_math
3374 && opts->x_flag_unsafe_math_optimizations
3375 && opts->x_flag_finite_math_only
3376 && !opts->x_flag_signed_zeros
3377 && !opts->x_flag_errno_math
3378 && opts->x_flag_excess_precision == EXCESS_PRECISION_FAST);
3381 /* Return true iff flags are set as if -ffast-math but using the flags stored
3382 in the struct cl_optimization structure. */
3383 bool
3384 fast_math_flags_struct_set_p (struct cl_optimization *opt)
3386 return (!opt->x_flag_trapping_math
3387 && opt->x_flag_unsafe_math_optimizations
3388 && opt->x_flag_finite_math_only
3389 && !opt->x_flag_signed_zeros
3390 && !opt->x_flag_errno_math);
3393 /* Handle a debug output -g switch for options OPTS
3394 (OPTS_SET->x_write_symbols storing whether a debug format was passed
3395 explicitly), location LOC. EXTENDED is true or false to support
3396 extended output (2 is special and means "-ggdb" was given). */
3397 static void
3398 set_debug_level (uint32_t dinfo, int extended, const char *arg,
3399 struct gcc_options *opts, struct gcc_options *opts_set,
3400 location_t loc)
3402 if (dinfo == NO_DEBUG)
3404 if (opts->x_write_symbols == NO_DEBUG)
3406 opts->x_write_symbols = PREFERRED_DEBUGGING_TYPE;
3408 if (extended == 2)
3410 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_LINENO_DEBUGGING_INFO
3411 if (opts->x_write_symbols & CTF_DEBUG)
3412 opts->x_write_symbols |= DWARF2_DEBUG;
3413 else
3414 opts->x_write_symbols = DWARF2_DEBUG;
3415 #endif
3418 if (opts->x_write_symbols == NO_DEBUG)
3419 warning_at (loc, 0, "target system does not support debug output");
3421 else if ((opts->x_write_symbols & CTF_DEBUG)
3422 || (opts->x_write_symbols & BTF_DEBUG))
3424 opts->x_write_symbols |= DWARF2_DEBUG;
3425 opts_set->x_write_symbols |= DWARF2_DEBUG;
3428 else
3430 /* Make and retain the choice if both CTF and DWARF debug info are to
3431 be generated. */
3432 if (((dinfo == DWARF2_DEBUG) || (dinfo == CTF_DEBUG))
3433 && ((opts->x_write_symbols == (DWARF2_DEBUG|CTF_DEBUG))
3434 || (opts->x_write_symbols == DWARF2_DEBUG)
3435 || (opts->x_write_symbols == CTF_DEBUG)))
3437 opts->x_write_symbols |= dinfo;
3438 opts_set->x_write_symbols |= dinfo;
3440 /* However, CTF and BTF are not allowed together at this time. */
3441 else if (((dinfo == DWARF2_DEBUG) || (dinfo == BTF_DEBUG))
3442 && ((opts->x_write_symbols == (DWARF2_DEBUG|BTF_DEBUG))
3443 || (opts->x_write_symbols == DWARF2_DEBUG)
3444 || (opts->x_write_symbols == BTF_DEBUG)))
3446 opts->x_write_symbols |= dinfo;
3447 opts_set->x_write_symbols |= dinfo;
3449 else
3451 /* Does it conflict with an already selected debug format? */
3452 if (opts_set->x_write_symbols != NO_DEBUG
3453 && opts->x_write_symbols != NO_DEBUG
3454 && dinfo != opts->x_write_symbols)
3456 gcc_assert (debug_set_count (dinfo) <= 1);
3457 error_at (loc, "debug format %qs conflicts with prior selection",
3458 debug_type_names[debug_set_to_format (dinfo)]);
3460 opts->x_write_symbols = dinfo;
3461 opts_set->x_write_symbols = dinfo;
3465 if (dinfo != BTF_DEBUG)
3467 /* A debug flag without a level defaults to level 2.
3468 If off or at level 1, set it to level 2, but if already
3469 at level 3, don't lower it. */
3470 if (*arg == '\0')
3472 if (dinfo == CTF_DEBUG)
3473 opts->x_ctf_debug_info_level = CTFINFO_LEVEL_NORMAL;
3474 else if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
3475 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3477 else
3479 int argval = integral_argument (arg);
3480 if (argval == -1)
3481 error_at (loc, "unrecognized debug output level %qs", arg);
3482 else if (argval > 3)
3483 error_at (loc, "debug output level %qs is too high", arg);
3484 else
3486 if (dinfo == CTF_DEBUG)
3487 opts->x_ctf_debug_info_level
3488 = (enum ctf_debug_info_levels) argval;
3489 else
3490 opts->x_debug_info_level = (enum debug_info_levels) argval;
3494 else if (*arg != '\0')
3495 error_at (loc, "unrecognized btf debug output level %qs", arg);
3498 /* Arrange to dump core on error for diagnostic context DC. (The
3499 regular error message is still printed first, except in the case of
3500 abort ().) */
3502 static void
3503 setup_core_dumping (diagnostic_context *dc)
3505 #ifdef SIGABRT
3506 signal (SIGABRT, SIG_DFL);
3507 #endif
3508 #if defined(HAVE_SETRLIMIT)
3510 struct rlimit rlim;
3511 if (getrlimit (RLIMIT_CORE, &rlim) != 0)
3512 fatal_error (input_location, "getting core file size maximum limit: %m");
3513 rlim.rlim_cur = rlim.rlim_max;
3514 if (setrlimit (RLIMIT_CORE, &rlim) != 0)
3515 fatal_error (input_location,
3516 "setting core file size limit to maximum: %m");
3518 #endif
3519 diagnostic_abort_on_error (dc);
3522 /* Parse a -d<ARG> command line switch for OPTS, location LOC,
3523 diagnostic context DC. */
3525 static void
3526 decode_d_option (const char *arg, struct gcc_options *opts,
3527 location_t loc, diagnostic_context *dc)
3529 int c;
3531 while (*arg)
3532 switch (c = *arg++)
3534 case 'A':
3535 opts->x_flag_debug_asm = 1;
3536 break;
3537 case 'p':
3538 opts->x_flag_print_asm_name = 1;
3539 break;
3540 case 'P':
3541 opts->x_flag_dump_rtl_in_asm = 1;
3542 opts->x_flag_print_asm_name = 1;
3543 break;
3544 case 'x':
3545 opts->x_rtl_dump_and_exit = 1;
3546 break;
3547 case 'D': /* These are handled by the preprocessor. */
3548 case 'I':
3549 case 'M':
3550 case 'N':
3551 case 'U':
3552 break;
3553 case 'H':
3554 setup_core_dumping (dc);
3555 break;
3556 case 'a':
3557 opts->x_flag_dump_all_passed = true;
3558 break;
3560 default:
3561 warning_at (loc, 0, "unrecognized gcc debugging option: %c", c);
3562 break;
3566 /* Enable (or disable if VALUE is 0) a warning option ARG (language
3567 mask LANG_MASK, option handlers HANDLERS) as an error for option
3568 structures OPTS and OPTS_SET, diagnostic context DC (possibly
3569 NULL), location LOC. This is used by -Werror=. */
3571 static void
3572 enable_warning_as_error (const char *arg, int value, unsigned int lang_mask,
3573 const struct cl_option_handlers *handlers,
3574 struct gcc_options *opts,
3575 struct gcc_options *opts_set,
3576 location_t loc, diagnostic_context *dc)
3578 char *new_option;
3579 int option_index;
3581 new_option = XNEWVEC (char, strlen (arg) + 2);
3582 new_option[0] = 'W';
3583 strcpy (new_option + 1, arg);
3584 option_index = find_opt (new_option, lang_mask);
3585 if (option_index == OPT_SPECIAL_unknown)
3587 option_proposer op;
3588 const char *hint = op.suggest_option (new_option);
3589 if (hint)
3590 error_at (loc, "%<-W%serror=%s%>: no option %<-%s%>;"
3591 " did you mean %<-%s%>?", value ? "" : "no-",
3592 arg, new_option, hint);
3593 else
3594 error_at (loc, "%<-W%serror=%s%>: no option %<-%s%>",
3595 value ? "" : "no-", arg, new_option);
3597 else if (!(cl_options[option_index].flags & CL_WARNING))
3598 error_at (loc, "%<-Werror=%s%>: %<-%s%> is not an option that "
3599 "controls warnings", arg, new_option);
3600 else
3602 const diagnostic_t kind = value ? DK_ERROR : DK_WARNING;
3603 const char *arg = NULL;
3605 if (cl_options[option_index].flags & CL_JOINED)
3606 arg = new_option + cl_options[option_index].opt_len;
3607 control_warning_option (option_index, (int) kind, arg, value,
3608 loc, lang_mask,
3609 handlers, opts, opts_set, dc);
3611 free (new_option);
3614 /* Return malloced memory for the name of the option OPTION_INDEX
3615 which enabled a diagnostic (context CONTEXT), originally of type
3616 ORIG_DIAG_KIND but possibly converted to DIAG_KIND by options such
3617 as -Werror. */
3619 char *
3620 option_name (diagnostic_context *context, int option_index,
3621 diagnostic_t orig_diag_kind, diagnostic_t diag_kind)
3623 if (option_index)
3625 /* A warning classified as an error. */
3626 if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN)
3627 && diag_kind == DK_ERROR)
3628 return concat (cl_options[OPT_Werror_].opt_text,
3629 /* Skip over "-W". */
3630 cl_options[option_index].opt_text + 2,
3631 NULL);
3632 /* A warning with option. */
3633 else
3634 return xstrdup (cl_options[option_index].opt_text);
3636 /* A warning without option classified as an error. */
3637 else if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN
3638 || diag_kind == DK_WARNING)
3639 && context->warning_as_error_requested)
3640 return xstrdup (cl_options[OPT_Werror].opt_text);
3641 else
3642 return NULL;
3645 /* Get the page within the documentation for this option. */
3647 static const char *
3648 get_option_html_page (int option_index)
3650 const cl_option *cl_opt = &cl_options[option_index];
3652 /* Analyzer options are on their own page. */
3653 if (strstr (cl_opt->opt_text, "analyzer-"))
3654 return "gcc/Static-Analyzer-Options.html";
3656 /* Handle -flto= option. */
3657 if (strstr (cl_opt->opt_text, "flto"))
3658 return "gcc/Optimize-Options.html";
3660 #ifdef CL_Fortran
3661 if ((cl_opt->flags & CL_Fortran) != 0
3662 /* If it is option common to both C/C++ and Fortran, it is documented
3663 in gcc/ rather than gfortran/ docs. */
3664 && (cl_opt->flags & CL_C) == 0
3665 #ifdef CL_CXX
3666 && (cl_opt->flags & CL_CXX) == 0
3667 #endif
3669 return "gfortran/Error-and-Warning-Options.html";
3670 #endif
3672 return "gcc/Warning-Options.html";
3675 /* Return malloced memory for a URL describing the option OPTION_INDEX
3676 which enabled a diagnostic (context CONTEXT). */
3678 char *
3679 get_option_url (diagnostic_context *, int option_index)
3681 if (option_index)
3682 return concat (/* DOCUMENTATION_ROOT_URL should be supplied via -D by
3683 the Makefile (see --with-documentation-root-url), and
3684 should have a trailing slash. */
3685 DOCUMENTATION_ROOT_URL,
3687 /* get_option_html_page will return something like
3688 "gcc/Warning-Options.html". */
3689 get_option_html_page (option_index),
3691 /* Expect an anchor of the form "index-Wfoo" e.g.
3692 <a name="index-Wformat"></a>, and thus an id within
3693 the URL of "#index-Wformat". */
3694 "#index", cl_options[option_index].opt_text,
3695 NULL);
3696 else
3697 return NULL;
3700 /* Return a heap allocated producer with command line options. */
3702 char *
3703 gen_command_line_string (cl_decoded_option *options,
3704 unsigned int options_count)
3706 auto_vec<const char *> switches;
3707 char *options_string, *tail;
3708 const char *p;
3709 size_t len = 0;
3711 for (unsigned i = 0; i < options_count; i++)
3712 switch (options[i].opt_index)
3714 case OPT_o:
3715 case OPT_d:
3716 case OPT_dumpbase:
3717 case OPT_dumpbase_ext:
3718 case OPT_dumpdir:
3719 case OPT_quiet:
3720 case OPT_version:
3721 case OPT_v:
3722 case OPT_w:
3723 case OPT_L:
3724 case OPT_D:
3725 case OPT_I:
3726 case OPT_U:
3727 case OPT_SPECIAL_unknown:
3728 case OPT_SPECIAL_ignore:
3729 case OPT_SPECIAL_warn_removed:
3730 case OPT_SPECIAL_program_name:
3731 case OPT_SPECIAL_input_file:
3732 case OPT_grecord_gcc_switches:
3733 case OPT_frecord_gcc_switches:
3734 case OPT__output_pch:
3735 case OPT_fdiagnostics_show_location_:
3736 case OPT_fdiagnostics_show_option:
3737 case OPT_fdiagnostics_show_caret:
3738 case OPT_fdiagnostics_show_labels:
3739 case OPT_fdiagnostics_show_line_numbers:
3740 case OPT_fdiagnostics_color_:
3741 case OPT_fdiagnostics_format_:
3742 case OPT_fverbose_asm:
3743 case OPT____:
3744 case OPT__sysroot_:
3745 case OPT_nostdinc:
3746 case OPT_nostdinc__:
3747 case OPT_fpreprocessed:
3748 case OPT_fltrans_output_list_:
3749 case OPT_fresolution_:
3750 case OPT_fdebug_prefix_map_:
3751 case OPT_fmacro_prefix_map_:
3752 case OPT_ffile_prefix_map_:
3753 case OPT_fprofile_prefix_map_:
3754 case OPT_fcanon_prefix_map:
3755 case OPT_fcompare_debug:
3756 case OPT_fchecking:
3757 case OPT_fchecking_:
3758 /* Ignore these. */
3759 continue;
3760 case OPT_flto_:
3762 const char *lto_canonical = "-flto";
3763 switches.safe_push (lto_canonical);
3764 len += strlen (lto_canonical) + 1;
3765 break;
3767 default:
3768 if (cl_options[options[i].opt_index].flags
3769 & CL_NO_DWARF_RECORD)
3770 continue;
3771 gcc_checking_assert (options[i].canonical_option[0][0] == '-');
3772 switch (options[i].canonical_option[0][1])
3774 case 'M':
3775 case 'i':
3776 case 'W':
3777 continue;
3778 case 'f':
3779 if (strncmp (options[i].canonical_option[0] + 2,
3780 "dump", 4) == 0)
3781 continue;
3782 break;
3783 default:
3784 break;
3786 switches.safe_push (options[i].orig_option_with_args_text);
3787 len += strlen (options[i].orig_option_with_args_text) + 1;
3788 break;
3791 options_string = XNEWVEC (char, len + 1);
3792 tail = options_string;
3794 unsigned i;
3795 FOR_EACH_VEC_ELT (switches, i, p)
3797 len = strlen (p);
3798 memcpy (tail, p, len);
3799 tail += len;
3800 if (i != switches.length () - 1)
3802 *tail = ' ';
3803 ++tail;
3807 *tail = '\0';
3808 return options_string;
3811 /* Return a heap allocated producer string including command line options. */
3813 char *
3814 gen_producer_string (const char *language_string, cl_decoded_option *options,
3815 unsigned int options_count)
3817 char *cmdline = gen_command_line_string (options, options_count);
3818 char *combined = concat (language_string, " ", version_string, " ",
3819 cmdline, NULL);
3820 free (cmdline);
3821 return combined;
3824 #if CHECKING_P
3826 namespace selftest {
3828 /* Verify that get_option_html_page works as expected. */
3830 static void
3831 test_get_option_html_page ()
3833 ASSERT_STREQ (get_option_html_page (OPT_Wcpp), "gcc/Warning-Options.html");
3834 ASSERT_STREQ (get_option_html_page (OPT_Wanalyzer_double_free),
3835 "gcc/Static-Analyzer-Options.html");
3836 #ifdef CL_Fortran
3837 ASSERT_STREQ (get_option_html_page (OPT_Wline_truncation),
3838 "gfortran/Error-and-Warning-Options.html");
3839 #endif
3842 /* Verify EnumSet and EnumBitSet requirements. */
3844 static void
3845 test_enum_sets ()
3847 for (unsigned i = 0; i < cl_options_count; ++i)
3848 if (cl_options[i].var_type == CLVC_ENUM
3849 && cl_options[i].var_value != CLEV_NORMAL)
3851 const struct cl_enum *e = &cl_enums[cl_options[i].var_enum];
3852 unsigned HOST_WIDE_INT used_sets = 0;
3853 unsigned HOST_WIDE_INT mask = 0;
3854 unsigned highest_set = 0;
3855 for (unsigned j = 0; e->values[j].arg; ++j)
3857 unsigned set = e->values[j].flags >> CL_ENUM_SET_SHIFT;
3858 if (cl_options[i].var_value == CLEV_BITSET)
3860 /* For EnumBitSet Set shouldn't be used and Value should
3861 be a power of two. */
3862 ASSERT_TRUE (set == 0);
3863 ASSERT_TRUE (pow2p_hwi (e->values[j].value));
3864 continue;
3866 /* Test that enumerators referenced in EnumSet have all
3867 Set(n) on them within the valid range. */
3868 ASSERT_TRUE (set >= 1 && set <= HOST_BITS_PER_WIDE_INT);
3869 highest_set = MAX (set, highest_set);
3870 used_sets |= HOST_WIDE_INT_1U << (set - 1);
3872 if (cl_options[i].var_value == CLEV_BITSET)
3873 continue;
3874 /* If there is just one set, no point to using EnumSet. */
3875 ASSERT_TRUE (highest_set >= 2);
3876 /* Test that there are no gaps in between the sets. */
3877 if (highest_set == HOST_BITS_PER_WIDE_INT)
3878 ASSERT_TRUE (used_sets == HOST_WIDE_INT_M1U);
3879 else
3880 ASSERT_TRUE (used_sets == (HOST_WIDE_INT_1U << highest_set) - 1);
3881 for (unsigned int j = 1; j <= highest_set; ++j)
3883 unsigned HOST_WIDE_INT this_mask = 0;
3884 for (unsigned k = 0; e->values[k].arg; ++k)
3886 unsigned set = e->values[j].flags >> CL_ENUM_SET_SHIFT;
3887 if (set == j)
3888 this_mask |= e->values[j].value;
3890 ASSERT_TRUE ((mask & this_mask) == 0);
3891 mask |= this_mask;
3896 /* Run all of the selftests within this file. */
3898 void
3899 opts_cc_tests ()
3901 test_get_option_html_page ();
3902 test_enum_sets ();
3905 } // namespace selftest
3907 #endif /* #if CHECKING_P */