Debian Codegraph @

gcc/opts.cc

raw
1 /* Command line option handling.
2 Copyright (C) 2002-2025 Free Software Foundation, Inc.
3 Contributed by Neil Booth.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "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 "diagnostic-format.h"
36 #include "version.h"
37 #include "selftest.h"
38 #include "file-prefix-map.h"
39
40 /* In this file all option sets are explicit. */
41 #undef OPTION_SET_P
42
43 /* Set by -fcanon-prefix-map. */
44 bool flag_canon_prefix_map;
45
46 /* Set by finish_options when flag_stack_protector was set only because of
47 -fhardened. Yuck. */
48 bool flag_stack_protector_set_by_fhardened_p;
49
50 static void set_Wstrict_aliasing (struct gcc_options *opts, int onoff);
51
52 /* Names of fundamental debug info formats indexed by enum
53 debug_info_type. */
54
55 const char *const debug_type_names[] =
56 {
57 "none", "dwarf-2", "vms", "ctf", "btf", "codeview"
58 };
59
60 /* Bitmasks of fundamental debug info formats indexed by enum
61 debug_info_type. */
62
63 static uint32_t debug_type_masks[] =
64 {
65 NO_DEBUG, DWARF2_DEBUG, VMS_DEBUG,
66 CTF_DEBUG, BTF_DEBUG, CODEVIEW_DEBUG
67 };
68
69 /* Names of the set of debug formats requested by user. Updated and accessed
70 via debug_set_names. */
71
72 static char df_set_names[sizeof "none dwarf-2 vms ctf btf codeview"];
73
74 /* Get enum debug_info_type of the specified debug format, for error messages.
75 Can be used only for individual debug format types. */
76
77 enum debug_info_type
78 debug_set_to_format (uint32_t debug_info_set)
79 {
80 int idx = 0;
81 enum debug_info_type dinfo_type = DINFO_TYPE_NONE;
82 /* Find first set bit. */
83 if (debug_info_set)
84 idx = exact_log2 (debug_info_set & - debug_info_set);
85 /* Check that only one bit is set, if at all. This function is meant to be
86 used only for vanilla debug_info_set bitmask values, i.e. for individual
87 debug format types upto DINFO_TYPE_MAX. */
88 gcc_assert ((debug_info_set & (debug_info_set - 1)) == 0);
89 dinfo_type = (enum debug_info_type)idx;
90 gcc_assert (dinfo_type <= DINFO_TYPE_MAX);
91 return dinfo_type;
92 }
93
94 /* Get the number of debug formats enabled for output. */
95
96 unsigned int
97 debug_set_count (uint32_t w_symbols)
98 {
99 unsigned int count = 0;
100 while (w_symbols)
101 {
102 ++ count;
103 w_symbols &= ~ (w_symbols & - w_symbols);
104 }
105 return count;
106 }
107
108 /* Get the names of the debug formats enabled for output. */
109
110 const char *
111 debug_set_names (uint32_t w_symbols)
112 {
113 uint32_t df_mask = 0;
114 /* Reset the string to be returned. */
115 memset (df_set_names, 0, sizeof (df_set_names));
116 /* Get the popcount. */
117 int num_set_df = debug_set_count (w_symbols);
118 /* Iterate over the debug formats. Add name string for those enabled. */
119 for (int i = DINFO_TYPE_NONE; i <= DINFO_TYPE_MAX; i++)
120 {
121 df_mask = debug_type_masks[i];
122 if (w_symbols & df_mask)
123 {
124 strcat (df_set_names, debug_type_names[i]);
125 num_set_df--;
126 if (num_set_df)
127 strcat (df_set_names, " ");
128 else
129 break;
130 }
131 else if (!w_symbols)
132 {
133 /* No debug formats enabled. */
134 gcc_assert (i == DINFO_TYPE_NONE);
135 strcat (df_set_names, debug_type_names[i]);
136 break;
137 }
138 }
139 return df_set_names;
140 }
141
142 /* Return TRUE iff BTF debug info is enabled. */
143
144 bool
145 btf_debuginfo_p ()
146 {
147 return (write_symbols & BTF_DEBUG);
148 }
149
150 /* Return TRUE iff BTF with CO-RE debug info is enabled. */
151
152 bool
153 btf_with_core_debuginfo_p ()
154 {
155 return (write_symbols & BTF_WITH_CORE_DEBUG);
156 }
157
158 /* Return TRUE iff CTF debug info is enabled. */
159
160 bool
161 ctf_debuginfo_p ()
162 {
163 return (write_symbols & CTF_DEBUG);
164 }
165
166 /* Return TRUE iff CodeView debug info is enabled. */
167
168 bool
169 codeview_debuginfo_p ()
170 {
171 return (write_symbols & CODEVIEW_DEBUG);
172 }
173
174 /* Return TRUE iff dwarf2 debug info is enabled. */
175
176 bool
177 dwarf_debuginfo_p (struct gcc_options *opts)
178 {
179 return (opts->x_write_symbols & DWARF2_DEBUG);
180 }
181
182 /* Return true iff the debug info format is to be generated based on DWARF
183 DIEs (like CTF and BTF debug info formats). */
184
185 bool dwarf_based_debuginfo_p ()
186 {
187 return ((write_symbols & CTF_DEBUG)
188 || (write_symbols & BTF_DEBUG)
189 || (write_symbols & CODEVIEW_DEBUG));
190 }
191
192 /* All flag uses below need to explicitely reference the option sets
193 to operate on. */
194 #define global_options DO_NOT_USE
195 #define global_options_set DO_NOT_USE
196
197 /* Parse the -femit-struct-debug-detailed option value
198 and set the flag variables. */
199
200 #define MATCH( prefix, string ) \
201 ((strncmp (prefix, string, sizeof prefix - 1) == 0) \
202 ? ((string += sizeof prefix - 1), 1) : 0)
203
204 void
205 set_struct_debug_option (struct gcc_options *opts, location_t loc,
206 const char *spec)
207 {
208 /* various labels for comparison */
209 static const char dfn_lbl[] = "dfn:", dir_lbl[] = "dir:", ind_lbl[] = "ind:";
210 static const char ord_lbl[] = "ord:", gen_lbl[] = "gen:";
211 static const char none_lbl[] = "none", any_lbl[] = "any";
212 static const char base_lbl[] = "base", sys_lbl[] = "sys";
213
214 enum debug_struct_file files = DINFO_STRUCT_FILE_ANY;
215 /* Default is to apply to as much as possible. */
216 enum debug_info_usage usage = DINFO_USAGE_NUM_ENUMS;
217 int ord = 1, gen = 1;
218
219 /* What usage? */
220 if (MATCH (dfn_lbl, spec))
221 usage = DINFO_USAGE_DFN;
222 else if (MATCH (dir_lbl, spec))
223 usage = DINFO_USAGE_DIR_USE;
224 else if (MATCH (ind_lbl, spec))
225 usage = DINFO_USAGE_IND_USE;
226
227 /* Generics or not? */
228 if (MATCH (ord_lbl, spec))
229 gen = 0;
230 else if (MATCH (gen_lbl, spec))
231 ord = 0;
232
233 /* What allowable environment? */
234 if (MATCH (none_lbl, spec))
235 files = DINFO_STRUCT_FILE_NONE;
236 else if (MATCH (any_lbl, spec))
237 files = DINFO_STRUCT_FILE_ANY;
238 else if (MATCH (sys_lbl, spec))
239 files = DINFO_STRUCT_FILE_SYS;
240 else if (MATCH (base_lbl, spec))
241 files = DINFO_STRUCT_FILE_BASE;
242 else
243 error_at (loc,
244 "argument %qs to %<-femit-struct-debug-detailed%> "
245 "not recognized",
246 spec);
247
248 /* Effect the specification. */
249 if (usage == DINFO_USAGE_NUM_ENUMS)
250 {
251 if (ord)
252 {
253 opts->x_debug_struct_ordinary[DINFO_USAGE_DFN] = files;
254 opts->x_debug_struct_ordinary[DINFO_USAGE_DIR_USE] = files;
255 opts->x_debug_struct_ordinary[DINFO_USAGE_IND_USE] = files;
256 }
257 if (gen)
258 {
259 opts->x_debug_struct_generic[DINFO_USAGE_DFN] = files;
260 opts->x_debug_struct_generic[DINFO_USAGE_DIR_USE] = files;
261 opts->x_debug_struct_generic[DINFO_USAGE_IND_USE] = files;
262 }
263 }
264 else
265 {
266 if (ord)
267 opts->x_debug_struct_ordinary[usage] = files;
268 if (gen)
269 opts->x_debug_struct_generic[usage] = files;
270 }
271
272 if (*spec == ',')
273 set_struct_debug_option (opts, loc, spec+1);
274 else
275 {
276 /* No more -femit-struct-debug-detailed specifications.
277 Do final checks. */
278 if (*spec != '\0')
279 error_at (loc,
280 "argument %qs to %<-femit-struct-debug-detailed%> unknown",
281 spec);
282 if (opts->x_debug_struct_ordinary[DINFO_USAGE_DIR_USE]
283 < opts->x_debug_struct_ordinary[DINFO_USAGE_IND_USE]
284 || opts->x_debug_struct_generic[DINFO_USAGE_DIR_USE]
285 < opts->x_debug_struct_generic[DINFO_USAGE_IND_USE])
286 error_at (loc,
287 "%<-femit-struct-debug-detailed=dir:...%> must allow "
288 "at least as much as "
289 "%<-femit-struct-debug-detailed=ind:...%>");
290 }
291 }
292
293 /* Strip off a legitimate source ending from the input string NAME of
294 length LEN. Rather than having to know the names used by all of
295 our front ends, we strip off an ending of a period followed by
296 up to fource characters. (C++ uses ".cpp".) */
297
298 void
299 strip_off_ending (char *name, int len)
300 {
301 int i;
302 for (i = 2; i < 5 && len > i; i++)
303 {
304 if (name[len - i] == '.')
305 {
306 name[len - i] = '\0';
307 break;
308 }
309 }
310 }
311
312 /* Find the base name of a path, stripping off both directories and
313 a single final extension. */
314 int
315 base_of_path (const char *path, const char **base_out)
316 {
317 const char *base = path;
318 const char *dot = 0;
319 const char *p = path;
320 char c = *p;
321 while (c)
322 {
323 if (IS_DIR_SEPARATOR (c))
324 {
325 base = p + 1;
326 dot = 0;
327 }
328 else if (c == '.')
329 dot = p;
330 c = *++p;
331 }
332 if (!dot)
333 dot = p;
334 *base_out = base;
335 return dot - base;
336 }
337
338 /* What to print when a switch has no documentation. */
339 static const char undocumented_msg[] = N_("This option lacks documentation.");
340 static const char use_diagnosed_msg[] = N_("Uses of this option are diagnosed.");
341
342 typedef char *char_p; /* For DEF_VEC_P. */
343
344 static void set_debug_level (uint32_t dinfo, int extended,
345 const char *arg, struct gcc_options *opts,
346 struct gcc_options *opts_set,
347 location_t loc);
348 static void set_fast_math_flags (struct gcc_options *opts, int set);
349 static void decode_d_option (const char *arg, struct gcc_options *opts,
350 location_t loc, diagnostic_context *dc);
351 static void set_unsafe_math_optimizations_flags (struct gcc_options *opts,
352 int set);
353 static void enable_warning_as_error (const char *arg, int value,
354 unsigned int lang_mask,
355 const struct cl_option_handlers *handlers,
356 struct gcc_options *opts,
357 struct gcc_options *opts_set,
358 location_t loc,
359 diagnostic_context *dc);
360
361 /* Handle a back-end option; arguments and return value as for
362 handle_option. */
363
364 bool
365 target_handle_option (struct gcc_options *opts,
366 struct gcc_options *opts_set,
367 const struct cl_decoded_option *decoded,
368 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
369 location_t loc,
370 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
371 diagnostic_context *dc, void (*) (void))
372 {
373 gcc_assert (dc == global_dc);
374 gcc_assert (kind == DK_UNSPECIFIED);
375 return targetm_common.handle_option (opts, opts_set, decoded, loc);
376 }
377
378 /* Add comma-separated strings to a char_p vector. */
379
380 static void
381 add_comma_separated_to_vector (void **pvec, const char *arg)
382 {
383 char *tmp;
384 char *r;
385 char *w;
386 char *token_start;
387 vec<char_p> *v = (vec<char_p> *) *pvec;
388
389 vec_check_alloc (v, 1);
390
391 /* We never free this string. */
392 tmp = xstrdup (arg);
393
394 r = tmp;
395 w = tmp;
396 token_start = tmp;
397
398 while (*r != '\0')
399 {
400 if (*r == ',')
401 {
402 *w++ = '\0';
403 ++r;
404 v->safe_push (token_start);
405 token_start = w;
406 }
407 if (*r == '\\' && r[1] == ',')
408 {
409 *w++ = ',';
410 r += 2;
411 }
412 else
413 *w++ = *r++;
414 }
415
416 *w = '\0';
417 if (*token_start != '\0')
418 v->safe_push (token_start);
419
420 *pvec = v;
421 }
422
423 /* Initialize opts_obstack. */
424
425 void
426 init_opts_obstack (void)
427 {
428 gcc_obstack_init (&opts_obstack);
429 }
430
431 /* Initialize OPTS and OPTS_SET before using them in parsing options. */
432
433 void
434 init_options_struct (struct gcc_options *opts, struct gcc_options *opts_set)
435 {
436 /* Ensure that opts_obstack has already been initialized by the time
437 that we initialize any gcc_options instances (PR jit/68446). */
438 gcc_assert (opts_obstack.chunk_size > 0);
439
440 *opts = global_options_init;
441
442 if (opts_set)
443 memset (opts_set, 0, sizeof (*opts_set));
444
445 /* Initialize whether `char' is signed. */
446 opts->x_flag_signed_char = DEFAULT_SIGNED_CHAR;
447
448 /* Initialize target_flags before default_options_optimization
449 so the latter can modify it. */
450 opts->x_target_flags = targetm_common.default_target_flags;
451
452 /* Some targets have ABI-specified unwind tables. */
453 opts->x_flag_unwind_tables = targetm_common.unwind_tables_default;
454
455 /* Some targets have other target-specific initialization. */
456 targetm_common.option_init_struct (opts);
457 }
458
459 /* If indicated by the optimization level LEVEL (-Os if SIZE is set,
460 -Ofast if FAST is set, -Og if DEBUG is set), apply the option DEFAULT_OPT
461 to OPTS and OPTS_SET, diagnostic context DC, location LOC, with language
462 mask LANG_MASK and option handlers HANDLERS. */
463
464 static void
465 maybe_default_option (struct gcc_options *opts,
466 struct gcc_options *opts_set,
467 const struct default_options *default_opt,
468 int level, bool size, bool fast, bool debug,
469 unsigned int lang_mask,
470 const struct cl_option_handlers *handlers,
471 location_t loc,
472 diagnostic_context *dc)
473 {
474 const struct cl_option *option = &cl_options[default_opt->opt_index];
475 bool enabled;
476
477 if (size)
478 gcc_assert (level == 2);
479 if (fast)
480 gcc_assert (level == 3);
481 if (debug)
482 gcc_assert (level == 1);
483
484 switch (default_opt->levels)
485 {
486 case OPT_LEVELS_ALL:
487 enabled = true;
488 break;
489
490 case OPT_LEVELS_0_ONLY:
491 enabled = (level == 0);
492 break;
493
494 case OPT_LEVELS_1_PLUS:
495 enabled = (level >= 1);
496 break;
497
498 case OPT_LEVELS_1_PLUS_SPEED_ONLY:
499 enabled = (level >= 1 && !size && !debug);
500 break;
501
502 case OPT_LEVELS_1_PLUS_NOT_DEBUG:
503 enabled = (level >= 1 && !debug);
504 break;
505
506 case OPT_LEVELS_2_PLUS:
507 enabled = (level >= 2);
508 break;
509
510 case OPT_LEVELS_2_PLUS_SPEED_ONLY:
511 enabled = (level >= 2 && !size && !debug);
512 break;
513
514 case OPT_LEVELS_3_PLUS:
515 enabled = (level >= 3);
516 break;
517
518 case OPT_LEVELS_3_PLUS_AND_SIZE:
519 enabled = (level >= 3 || size);
520 break;
521
522 case OPT_LEVELS_SIZE:
523 enabled = size;
524 break;
525
526 case OPT_LEVELS_FAST:
527 enabled = fast;
528 break;
529
530 case OPT_LEVELS_NONE:
531 default:
532 gcc_unreachable ();
533 }
534
535 if (enabled)
536 handle_generated_option (opts, opts_set, default_opt->opt_index,
537 default_opt->arg, default_opt->value,
538 lang_mask, DK_UNSPECIFIED, loc,
539 handlers, true, dc);
540 else if (default_opt->arg == NULL
541 && !option->cl_reject_negative
542 && !(option->flags & CL_PARAMS))
543 handle_generated_option (opts, opts_set, default_opt->opt_index,
544 default_opt->arg, !default_opt->value,
545 lang_mask, DK_UNSPECIFIED, loc,
546 handlers, true, dc);
547 }
548
549 /* As indicated by the optimization level LEVEL (-Os if SIZE is set,
550 -Ofast if FAST is set), apply the options in array DEFAULT_OPTS to
551 OPTS and OPTS_SET, diagnostic context DC, location LOC, with
552 language mask LANG_MASK and option handlers HANDLERS. */
553
554 static void
555 maybe_default_options (struct gcc_options *opts,
556 struct gcc_options *opts_set,
557 const struct default_options *default_opts,
558 int level, bool size, bool fast, bool debug,
559 unsigned int lang_mask,
560 const struct cl_option_handlers *handlers,
561 location_t loc,
562 diagnostic_context *dc)
563 {
564 size_t i;
565
566 for (i = 0; default_opts[i].levels != OPT_LEVELS_NONE; i++)
567 maybe_default_option (opts, opts_set, &default_opts[i],
568 level, size, fast, debug,
569 lang_mask, handlers, loc, dc);
570 }
571
572 /* Table of options enabled by default at different levels.
573 Please keep this list sorted by level and alphabetized within
574 each level; this makes it easier to keep the documentation
575 in sync. */
576
577 static const struct default_options default_options_table[] =
578 {
579 /* -O1 and -Og optimizations. */
580 { OPT_LEVELS_1_PLUS, OPT_fcombine_stack_adjustments, NULL, 1 },
581 { OPT_LEVELS_1_PLUS, OPT_fcompare_elim, NULL, 1 },
582 { OPT_LEVELS_1_PLUS, OPT_fcprop_registers, NULL, 1 },
583 { OPT_LEVELS_1_PLUS, OPT_fdefer_pop, NULL, 1 },
584 { OPT_LEVELS_1_PLUS, OPT_fforward_propagate, NULL, 1 },
585 { OPT_LEVELS_1_PLUS, OPT_fguess_branch_probability, NULL, 1 },
586 { OPT_LEVELS_1_PLUS, OPT_fipa_profile, NULL, 1 },
587 { OPT_LEVELS_1_PLUS, OPT_fipa_pure_const, NULL, 1 },
588 { OPT_LEVELS_1_PLUS, OPT_fipa_reference, NULL, 1 },
589 { OPT_LEVELS_1_PLUS, OPT_fipa_reference_addressable, NULL, 1 },
590 { OPT_LEVELS_1_PLUS, OPT_fmerge_constants, NULL, 1 },
591 { OPT_LEVELS_1_PLUS, OPT_fomit_frame_pointer, NULL, 1 },
592 { OPT_LEVELS_1_PLUS, OPT_freorder_blocks, NULL, 1 },
593 { OPT_LEVELS_1_PLUS, OPT_fshrink_wrap, NULL, 1 },
594 { OPT_LEVELS_1_PLUS, OPT_fsplit_wide_types, NULL, 1 },
595 { OPT_LEVELS_1_PLUS, OPT_fthread_jumps, NULL, 1 },
596 { OPT_LEVELS_1_PLUS, OPT_ftree_builtin_call_dce, NULL, 1 },
597 { OPT_LEVELS_1_PLUS, OPT_ftree_ccp, NULL, 1 },
598 { OPT_LEVELS_1_PLUS, OPT_ftree_ch, NULL, 1 },
599 { OPT_LEVELS_1_PLUS, OPT_ftree_coalesce_vars, NULL, 1 },
600 { OPT_LEVELS_1_PLUS, OPT_ftree_copy_prop, NULL, 1 },
601 { OPT_LEVELS_1_PLUS, OPT_ftree_dce, NULL, 1 },
602 { OPT_LEVELS_1_PLUS, OPT_ftree_dominator_opts, NULL, 1 },
603 { OPT_LEVELS_1_PLUS, OPT_ftree_fre, NULL, 1 },
604 { OPT_LEVELS_1_PLUS, OPT_ftree_sink, NULL, 1 },
605 { OPT_LEVELS_1_PLUS, OPT_ftree_slsr, NULL, 1 },
606 { OPT_LEVELS_1_PLUS, OPT_ftree_ter, NULL, 1 },
607 { OPT_LEVELS_1_PLUS, OPT_fvar_tracking, NULL, 1 },
608
609 /* -O1 (and not -Og) optimizations. */
610 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fbit_tests, NULL, 1 },
611 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fbranch_count_reg, NULL, 1 },
612 #if DELAY_SLOTS
613 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fdelayed_branch, NULL, 1 },
614 #endif
615 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fdse, NULL, 1 },
616 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fif_conversion, NULL, 1 },
617 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fif_conversion2, NULL, 1 },
618 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_finline_functions_called_once, NULL, 1 },
619 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fjump_tables, NULL, 1 },
620 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fmove_loop_invariants, NULL, 1 },
621 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fmove_loop_stores, NULL, 1 },
622 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fssa_phiopt, NULL, 1 },
623 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_fipa_modref, NULL, 1 },
624 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_bit_ccp, NULL, 1 },
625 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_dse, NULL, 1 },
626 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_pta, NULL, 1 },
627 { OPT_LEVELS_1_PLUS_NOT_DEBUG, OPT_ftree_sra, NULL, 1 },
628
629 /* -O2 and -Os optimizations. */
630 { OPT_LEVELS_2_PLUS, OPT_fcaller_saves, NULL, 1 },
631 { OPT_LEVELS_2_PLUS, OPT_fcode_hoisting, NULL, 1 },
632 { OPT_LEVELS_2_PLUS, OPT_fcrossjumping, NULL, 1 },
633 { OPT_LEVELS_2_PLUS, OPT_fcse_follow_jumps, NULL, 1 },
634 { OPT_LEVELS_2_PLUS, OPT_fdevirtualize, NULL, 1 },
635 { OPT_LEVELS_2_PLUS, OPT_fdevirtualize_speculatively, NULL, 1 },
636 { OPT_LEVELS_2_PLUS, OPT_fexpensive_optimizations, NULL, 1 },
637 { OPT_LEVELS_2_PLUS, OPT_fext_dce, NULL, 1 },
638 { OPT_LEVELS_2_PLUS, OPT_fgcse, NULL, 1 },
639 { OPT_LEVELS_2_PLUS, OPT_fhoist_adjacent_loads, NULL, 1 },
640 { OPT_LEVELS_2_PLUS, OPT_findirect_inlining, NULL, 1 },
641 { OPT_LEVELS_2_PLUS, OPT_finline_small_functions, NULL, 1 },
642 { OPT_LEVELS_2_PLUS, OPT_fipa_bit_cp, NULL, 1 },
643 { OPT_LEVELS_2_PLUS, OPT_fipa_cp, NULL, 1 },
644 { OPT_LEVELS_2_PLUS, OPT_fipa_icf, NULL, 1 },
645 { OPT_LEVELS_2_PLUS, OPT_fipa_ra, NULL, 1 },
646 { OPT_LEVELS_2_PLUS, OPT_fipa_sra, NULL, 1 },
647 { OPT_LEVELS_2_PLUS, OPT_fipa_vrp, NULL, 1 },
648 { OPT_LEVELS_2_PLUS, OPT_fisolate_erroneous_paths_dereference, NULL, 1 },
649 { OPT_LEVELS_2_PLUS, OPT_flra_remat, NULL, 1 },
650 { OPT_LEVELS_2_PLUS, OPT_foptimize_sibling_calls, NULL, 1 },
651 { OPT_LEVELS_2_PLUS, OPT_fpartial_inlining, NULL, 1 },
652 { OPT_LEVELS_2_PLUS, OPT_fpeephole2, NULL, 1 },
653 { OPT_LEVELS_2_PLUS, OPT_freorder_functions, NULL, 1 },
654 { OPT_LEVELS_2_PLUS, OPT_frerun_cse_after_loop, NULL, 1 },
655 #ifdef INSN_SCHEDULING
656 { OPT_LEVELS_2_PLUS, OPT_fschedule_insns2, NULL, 1 },
657 #endif
658 { OPT_LEVELS_2_PLUS, OPT_fstrict_aliasing, NULL, 1 },
659 { OPT_LEVELS_2_PLUS, OPT_fstore_merging, NULL, 1 },
660 { OPT_LEVELS_2_PLUS, OPT_ftree_pre, NULL, 1 },
661 { OPT_LEVELS_2_PLUS, OPT_ftree_switch_conversion, NULL, 1 },
662 { OPT_LEVELS_2_PLUS, OPT_ftree_tail_merge, NULL, 1 },
663 { OPT_LEVELS_2_PLUS, OPT_ftree_vrp, NULL, 1 },
664 { OPT_LEVELS_2_PLUS, OPT_fvect_cost_model_, NULL,
665 VECT_COST_MODEL_VERY_CHEAP },
666 { OPT_LEVELS_2_PLUS, OPT_finline_functions, NULL, 1 },
667 { OPT_LEVELS_2_PLUS, OPT_ftree_loop_distribute_patterns, NULL, 1 },
668 { OPT_LEVELS_2_PLUS, OPT_foptimize_crc, NULL, 1 },
669 { OPT_LEVELS_2_PLUS, OPT_flate_combine_instructions, NULL, 1 },
670
671 /* -O2 and above optimizations, but not -Os or -Og. */
672 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_functions, NULL, 1 },
673 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_jumps, NULL, 1 },
674 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_labels, NULL, 1 },
675 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_falign_loops, NULL, 1 },
676 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_foptimize_strlen, NULL, 1 },
677 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_freorder_blocks_algorithm_, NULL,
678 REORDER_BLOCKS_ALGORITHM_STC },
679 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_ftree_loop_vectorize, NULL, 1 },
680 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_ftree_slp_vectorize, NULL, 1 },
681 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_fopenmp_target_simd_clone_, NULL,
682 OMP_TARGET_SIMD_CLONE_NOHOST },
683 #ifdef INSN_SCHEDULING
684 /* Only run the pre-regalloc scheduling pass if optimizing for speed. */
685 { OPT_LEVELS_2_PLUS_SPEED_ONLY, OPT_fschedule_insns, NULL, 1 },
686 #endif
687
688 /* -O3 and -Os optimizations. */
689
690 /* -O3 optimizations. */
691 { OPT_LEVELS_3_PLUS, OPT_fgcse_after_reload, NULL, 1 },
692 { OPT_LEVELS_3_PLUS, OPT_fipa_cp_clone, NULL, 1 },
693 { OPT_LEVELS_3_PLUS, OPT_floop_interchange, NULL, 1 },
694 { OPT_LEVELS_3_PLUS, OPT_floop_unroll_and_jam, NULL, 1 },
695 { OPT_LEVELS_3_PLUS, OPT_fpeel_loops, NULL, 1 },
696 { OPT_LEVELS_3_PLUS, OPT_fpredictive_commoning, NULL, 1 },
697 { OPT_LEVELS_3_PLUS, OPT_fsplit_loops, NULL, 1 },
698 { OPT_LEVELS_3_PLUS, OPT_fsplit_paths, NULL, 1 },
699 { OPT_LEVELS_3_PLUS, OPT_ftree_loop_distribution, NULL, 1 },
700 { OPT_LEVELS_3_PLUS, OPT_ftree_partial_pre, NULL, 1 },
701 { OPT_LEVELS_3_PLUS, OPT_funswitch_loops, NULL, 1 },
702 { OPT_LEVELS_3_PLUS, OPT_fvect_cost_model_, NULL, VECT_COST_MODEL_DYNAMIC },
703 { OPT_LEVELS_3_PLUS, OPT_fversion_loops_for_strides, NULL, 1 },
704
705 /* -O3 parameters. */
706 { OPT_LEVELS_3_PLUS, OPT__param_max_inline_insns_auto_, NULL, 30 },
707 { OPT_LEVELS_3_PLUS, OPT__param_early_inlining_insns_, NULL, 14 },
708 { OPT_LEVELS_3_PLUS, OPT__param_inline_heuristics_hint_percent_, NULL, 600 },
709 { OPT_LEVELS_3_PLUS, OPT__param_inline_min_speedup_, NULL, 15 },
710 { OPT_LEVELS_3_PLUS, OPT__param_max_inline_insns_single_, NULL, 200 },
711
712 /* -Ofast adds optimizations to -O3. */
713 { OPT_LEVELS_FAST, OPT_ffast_math, NULL, 1 },
714 { OPT_LEVELS_FAST, OPT_fallow_store_data_races, NULL, 1 },
715 { OPT_LEVELS_FAST, OPT_fsemantic_interposition, NULL, 0 },
716
717 { OPT_LEVELS_NONE, 0, NULL, 0 }
718 };
719
720 /* Default the options in OPTS and OPTS_SET based on the optimization
721 settings in DECODED_OPTIONS and DECODED_OPTIONS_COUNT. */
722 void
723 default_options_optimization (struct gcc_options *opts,
724 struct gcc_options *opts_set,
725 struct cl_decoded_option *decoded_options,
726 unsigned int decoded_options_count,
727 location_t loc,
728 unsigned int lang_mask,
729 const struct cl_option_handlers *handlers,
730 diagnostic_context *dc)
731 {
732 unsigned int i;
733 int opt2;
734 bool openacc_mode = false;
735
736 /* Scan to see what optimization level has been specified. That will
737 determine the default value of many flags. */
738 for (i = 1; i < decoded_options_count; i++)
739 {
740 struct cl_decoded_option *opt = &decoded_options[i];
741 switch (opt->opt_index)
742 {
743 case OPT_O:
744 if (*opt->arg == '\0')
745 {
746 opts->x_optimize = 1;
747 opts->x_optimize_size = 0;
748 opts->x_optimize_fast = 0;
749 opts->x_optimize_debug = 0;
750 }
751 else
752 {
753 const int optimize_val = integral_argument (opt->arg);
754 if (optimize_val == -1)
755 error_at (loc, "argument to %<-O%> should be a non-negative "
756 "integer, %<g%>, %<s%>, %<z%> or %<fast%>");
757 else
758 {
759 opts->x_optimize = optimize_val;
760 if ((unsigned int) opts->x_optimize > 255)
761 opts->x_optimize = 255;
762 opts->x_optimize_size = 0;
763 opts->x_optimize_fast = 0;
764 opts->x_optimize_debug = 0;
765 }
766 }
767 break;
768
769 case OPT_Os:
770 opts->x_optimize_size = 1;
771
772 /* Optimizing for size forces optimize to be 2. */
773 opts->x_optimize = 2;
774 opts->x_optimize_fast = 0;
775 opts->x_optimize_debug = 0;
776 break;
777
778 case OPT_Oz:
779 opts->x_optimize_size = 2;
780
781 /* Optimizing for size forces optimize to be 2. */
782 opts->x_optimize = 2;
783 opts->x_optimize_fast = 0;
784 opts->x_optimize_debug = 0;
785 break;
786
787 case OPT_Ofast:
788 /* -Ofast only adds flags to -O3. */
789 opts->x_optimize_size = 0;
790 opts->x_optimize = 3;
791 opts->x_optimize_fast = 1;
792 opts->x_optimize_debug = 0;
793 break;
794
795 case OPT_Og:
796 /* -Og selects optimization level 1. */
797 opts->x_optimize_size = 0;
798 opts->x_optimize = 1;
799 opts->x_optimize_fast = 0;
800 opts->x_optimize_debug = 1;
801 break;
802
803 case OPT_fopenacc:
804 if (opt->value)
805 openacc_mode = true;
806 break;
807
808 default:
809 /* Ignore other options in this prescan. */
810 break;
811 }
812 }
813
814 maybe_default_options (opts, opts_set, default_options_table,
815 opts->x_optimize, opts->x_optimize_size,
816 opts->x_optimize_fast, opts->x_optimize_debug,
817 lang_mask, handlers, loc, dc);
818
819 /* -O2 param settings. */
820 opt2 = (opts->x_optimize >= 2);
821
822 if (openacc_mode)
823 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_pta, true);
824
825 /* Track fields in field-sensitive alias analysis. */
826 if (opt2)
827 SET_OPTION_IF_UNSET (opts, opts_set, param_max_fields_for_field_sensitive,
828 100);
829
830 if (opts->x_optimize_size)
831 /* We want to crossjump as much as possible. */
832 SET_OPTION_IF_UNSET (opts, opts_set, param_min_crossjump_insns, 1);
833
834 /* Restrict the amount of work combine does at -Og while retaining
835 most of its useful transforms. */
836 if (opts->x_optimize_debug)
837 SET_OPTION_IF_UNSET (opts, opts_set, param_max_combine_insns, 2);
838
839 /* Allow default optimizations to be specified on a per-machine basis. */
840 maybe_default_options (opts, opts_set,
841 targetm_common.option_optimization_table,
842 opts->x_optimize, opts->x_optimize_size,
843 opts->x_optimize_fast, opts->x_optimize_debug,
844 lang_mask, handlers, loc, dc);
845 }
846
847 /* Control IPA optimizations based on different live patching LEVEL. */
848 static void
849 control_options_for_live_patching (struct gcc_options *opts,
850 struct gcc_options *opts_set,
851 enum live_patching_level level,
852 location_t loc)
853 {
854 gcc_assert (level > LIVE_PATCHING_NONE);
855
856 switch (level)
857 {
858 case LIVE_PATCHING_INLINE_ONLY_STATIC:
859 #define LIVE_PATCHING_OPTION "-flive-patching=inline-only-static"
860 if (opts_set->x_flag_ipa_cp_clone && opts->x_flag_ipa_cp_clone)
861 error_at (loc, "%qs is incompatible with %qs",
862 "-fipa-cp-clone", LIVE_PATCHING_OPTION);
863 else
864 opts->x_flag_ipa_cp_clone = 0;
865
866 if (opts_set->x_flag_ipa_sra && opts->x_flag_ipa_sra)
867 error_at (loc, "%qs is incompatible with %qs",
868 "-fipa-sra", LIVE_PATCHING_OPTION);
869 else
870 opts->x_flag_ipa_sra = 0;
871
872 if (opts_set->x_flag_partial_inlining && opts->x_flag_partial_inlining)
873 error_at (loc, "%qs is incompatible with %qs",
874 "-fpartial-inlining", LIVE_PATCHING_OPTION);
875 else
876 opts->x_flag_partial_inlining = 0;
877
878 if (opts_set->x_flag_ipa_cp && opts->x_flag_ipa_cp)
879 error_at (loc, "%qs is incompatible with %qs",
880 "-fipa-cp", LIVE_PATCHING_OPTION);
881 else
882 opts->x_flag_ipa_cp = 0;
883
884 /* FALLTHROUGH. */
885 case LIVE_PATCHING_INLINE_CLONE:
886 #undef LIVE_PATCHING_OPTION
887 #define LIVE_PATCHING_OPTION "-flive-patching=inline-only-static|inline-clone"
888 /* live patching should disable whole-program optimization. */
889 if (opts_set->x_flag_whole_program && opts->x_flag_whole_program)
890 error_at (loc, "%qs is incompatible with %qs",
891 "-fwhole-program", LIVE_PATCHING_OPTION);
892 else
893 opts->x_flag_whole_program = 0;
894
895 /* visibility change should be excluded by !flag_whole_program
896 && !in_lto_p && !flag_ipa_cp_clone && !flag_ipa_sra
897 && !flag_partial_inlining. */
898
899 if (opts_set->x_flag_ipa_pta && opts->x_flag_ipa_pta)
900 error_at (loc, "%qs is incompatible with %qs",
901 "-fipa-pta", LIVE_PATCHING_OPTION);
902 else
903 opts->x_flag_ipa_pta = 0;
904
905 if (opts_set->x_flag_ipa_reference && opts->x_flag_ipa_reference)
906 error_at (loc, "%qs is incompatible with %qs",
907 "-fipa-reference", LIVE_PATCHING_OPTION);
908 else
909 opts->x_flag_ipa_reference = 0;
910
911 if (opts_set->x_flag_ipa_ra && opts->x_flag_ipa_ra)
912 error_at (loc, "%qs is incompatible with %qs",
913 "-fipa-ra", LIVE_PATCHING_OPTION);
914 else
915 opts->x_flag_ipa_ra = 0;
916
917 if (opts_set->x_flag_ipa_icf && opts->x_flag_ipa_icf)
918 error_at (loc, "%qs is incompatible with %qs",
919 "-fipa-icf", LIVE_PATCHING_OPTION);
920 else
921 opts->x_flag_ipa_icf = 0;
922
923 if (opts_set->x_flag_ipa_icf_functions && opts->x_flag_ipa_icf_functions)
924 error_at (loc, "%qs is incompatible with %qs",
925 "-fipa-icf-functions", LIVE_PATCHING_OPTION);
926 else
927 opts->x_flag_ipa_icf_functions = 0;
928
929 if (opts_set->x_flag_ipa_icf_variables && opts->x_flag_ipa_icf_variables)
930 error_at (loc, "%qs is incompatible with %qs",
931 "-fipa-icf-variables", LIVE_PATCHING_OPTION);
932 else
933 opts->x_flag_ipa_icf_variables = 0;
934
935 if (opts_set->x_flag_ipa_bit_cp && opts->x_flag_ipa_bit_cp)
936 error_at (loc, "%qs is incompatible with %qs",
937 "-fipa-bit-cp", LIVE_PATCHING_OPTION);
938 else
939 opts->x_flag_ipa_bit_cp = 0;
940
941 if (opts_set->x_flag_ipa_vrp && opts->x_flag_ipa_vrp)
942 error_at (loc, "%qs is incompatible with %qs",
943 "-fipa-vrp", LIVE_PATCHING_OPTION);
944 else
945 opts->x_flag_ipa_vrp = 0;
946
947 if (opts_set->x_flag_ipa_pure_const && opts->x_flag_ipa_pure_const)
948 error_at (loc, "%qs is incompatible with %qs",
949 "-fipa-pure-const", LIVE_PATCHING_OPTION);
950 else
951 opts->x_flag_ipa_pure_const = 0;
952
953 if (opts_set->x_flag_ipa_modref && opts->x_flag_ipa_modref)
954 error_at (loc,
955 "%<-fipa-modref%> is incompatible with %qs",
956 LIVE_PATCHING_OPTION);
957 else
958 opts->x_flag_ipa_modref = 0;
959
960 /* FIXME: disable unreachable code removal. */
961
962 /* discovery of functions/variables with no address taken. */
963 if (opts_set->x_flag_ipa_reference_addressable
964 && opts->x_flag_ipa_reference_addressable)
965 error_at (loc, "%qs is incompatible with %qs",
966 "-fipa-reference-addressable", LIVE_PATCHING_OPTION);
967 else
968 opts->x_flag_ipa_reference_addressable = 0;
969
970 /* ipa stack alignment propagation. */
971 if (opts_set->x_flag_ipa_stack_alignment
972 && opts->x_flag_ipa_stack_alignment)
973 error_at (loc, "%qs is incompatible with %qs",
974 "-fipa-stack-alignment", LIVE_PATCHING_OPTION);
975 else
976 opts->x_flag_ipa_stack_alignment = 0;
977 break;
978 default:
979 gcc_unreachable ();
980 }
981
982 #undef LIVE_PATCHING_OPTION
983 }
984
985 /* --help option argument if set. */
986 vec<const char *> help_option_arguments;
987
988 /* Return the string name describing a sanitizer argument which has been
989 provided on the command line and has set this particular flag. */
990 const char *
991 find_sanitizer_argument (struct gcc_options *opts, unsigned int flags)
992 {
993 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
994 {
995 /* Need to find the sanitizer_opts element which:
996 a) Could have set the flags requested.
997 b) Has been set on the command line.
998
999 Can have (a) without (b) if the flag requested is e.g.
1000 SANITIZE_ADDRESS, since both -fsanitize=address and
1001 -fsanitize=kernel-address set this flag.
1002
1003 Can have (b) without (a) by requesting more than one sanitizer on the
1004 command line. */
1005 if ((sanitizer_opts[i].flag & opts->x_flag_sanitize)
1006 != sanitizer_opts[i].flag)
1007 continue;
1008 if ((sanitizer_opts[i].flag & flags) != flags)
1009 continue;
1010 return sanitizer_opts[i].name;
1011 }
1012 return NULL;
1013 }
1014
1015
1016 /* Report an error to the user about sanitizer options they have requested
1017 which have set conflicting flags.
1018
1019 LEFT and RIGHT indicate sanitizer flags which conflict with each other, this
1020 function reports an error if both have been set in OPTS->x_flag_sanitize and
1021 ensures the error identifies the requested command line options that have
1022 set these flags. */
1023 static void
1024 report_conflicting_sanitizer_options (struct gcc_options *opts, location_t loc,
1025 unsigned int left, unsigned int right)
1026 {
1027 unsigned int left_seen = (opts->x_flag_sanitize & left);
1028 unsigned int right_seen = (opts->x_flag_sanitize & right);
1029 if (left_seen && right_seen)
1030 {
1031 const char* left_arg = find_sanitizer_argument (opts, left_seen);
1032 const char* right_arg = find_sanitizer_argument (opts, right_seen);
1033 gcc_assert (left_arg && right_arg);
1034 error_at (loc,
1035 "%<-fsanitize=%s%> is incompatible with %<-fsanitize=%s%>",
1036 left_arg, right_arg);
1037 }
1038 }
1039
1040 /* Validate from OPTS and OPTS_SET that when -fipa-reorder-for-locality is
1041 enabled no explicit -flto-partition is also passed as the locality cloning
1042 pass uses its own partitioning scheme. */
1043
1044 static void
1045 validate_ipa_reorder_locality_lto_partition (struct gcc_options *opts,
1046 struct gcc_options *opts_set)
1047 {
1048 static bool validated_p = false;
1049
1050 if (opts_set->x_flag_lto_partition)
1051 {
1052 if (opts->x_flag_ipa_reorder_for_locality && !validated_p)
1053 error ("%<-fipa-reorder-for-locality%> is incompatible with"
1054 " an explicit %qs option", "-flto-partition");
1055 }
1056 validated_p = true;
1057 }
1058
1059 /* After all options at LOC have been read into OPTS and OPTS_SET,
1060 finalize settings of those options and diagnose incompatible
1061 combinations. */
1062 void
1063 finish_options (struct gcc_options *opts, struct gcc_options *opts_set,
1064 location_t loc)
1065 {
1066 if (opts->x_dump_base_name
1067 && ! opts->x_dump_base_name_prefixed)
1068 {
1069 const char *sep = opts->x_dump_base_name;
1070
1071 for (; *sep; sep++)
1072 if (IS_DIR_SEPARATOR (*sep))
1073 break;
1074
1075 if (*sep)
1076 /* If dump_base_path contains subdirectories, don't prepend
1077 anything. */;
1078 else if (opts->x_dump_dir_name)
1079 /* We have a DUMP_DIR_NAME, prepend that. */
1080 opts->x_dump_base_name = opts_concat (opts->x_dump_dir_name,
1081 opts->x_dump_base_name, NULL);
1082
1083 /* It is definitely prefixed now. */
1084 opts->x_dump_base_name_prefixed = true;
1085 }
1086
1087 /* Handle related options for unit-at-a-time, toplevel-reorder, and
1088 section-anchors. */
1089 if (!opts->x_flag_unit_at_a_time)
1090 {
1091 if (opts->x_flag_section_anchors && opts_set->x_flag_section_anchors)
1092 error_at (loc, "section anchors must be disabled when unit-at-a-time "
1093 "is disabled");
1094 opts->x_flag_section_anchors = 0;
1095 if (opts->x_flag_toplevel_reorder == 1)
1096 error_at (loc, "toplevel reorder must be disabled when unit-at-a-time "
1097 "is disabled");
1098 opts->x_flag_toplevel_reorder = 0;
1099 }
1100
1101 /* -fself-test depends on the state of the compiler prior to
1102 compiling anything. Ideally it should be run on an empty source
1103 file. However, in case we get run with actual source, assume
1104 -fsyntax-only which will inhibit any compiler initialization
1105 which may confuse the self tests. */
1106 if (opts->x_flag_self_test)
1107 opts->x_flag_syntax_only = 1;
1108
1109 if (opts->x_flag_tm && opts->x_flag_non_call_exceptions)
1110 sorry ("transactional memory is not supported with non-call exceptions");
1111
1112 /* Unless the user has asked for section anchors, we disable toplevel
1113 reordering at -O0 to disable transformations that might be surprising
1114 to end users and to get -fno-toplevel-reorder tested. */
1115 if (!opts->x_optimize
1116 && opts->x_flag_toplevel_reorder == 2
1117 && !(opts->x_flag_section_anchors && opts_set->x_flag_section_anchors))
1118 {
1119 opts->x_flag_toplevel_reorder = 0;
1120 opts->x_flag_section_anchors = 0;
1121 }
1122 if (!opts->x_flag_toplevel_reorder)
1123 {
1124 if (opts->x_flag_section_anchors && opts_set->x_flag_section_anchors)
1125 error_at (loc, "section anchors must be disabled when toplevel reorder"
1126 " is disabled");
1127 opts->x_flag_section_anchors = 0;
1128 }
1129
1130 if (opts->x_flag_hardened)
1131 {
1132 if (!opts_set->x_flag_auto_var_init)
1133 opts->x_flag_auto_var_init = AUTO_INIT_ZERO;
1134 else if (opts->x_flag_auto_var_init != AUTO_INIT_ZERO)
1135 warning_at (loc, OPT_Whardened,
1136 "%<-ftrivial-auto-var-init=zero%> is not enabled by "
1137 "%<-fhardened%> because it was specified on the command "
1138 "line");
1139 }
1140
1141 if (!opts->x_flag_opts_finished)
1142 {
1143 /* We initialize opts->x_flag_pie to -1 so that targets can set a
1144 default value. */
1145 if (opts->x_flag_pie == -1)
1146 {
1147 /* We initialize opts->x_flag_pic to -1 so that we can tell if
1148 -fpic, -fPIC, -fno-pic or -fno-PIC is used. */
1149 if (opts->x_flag_pic == -1)
1150 opts->x_flag_pie = (opts->x_flag_hardened
1151 ? /*-fPIE*/ 2 : DEFAULT_FLAG_PIE);
1152 else
1153 opts->x_flag_pie = 0;
1154 }
1155 /* If -fPIE or -fpie is used, turn on PIC. */
1156 if (opts->x_flag_pie)
1157 opts->x_flag_pic = opts->x_flag_pie;
1158 else if (opts->x_flag_pic == -1)
1159 opts->x_flag_pic = 0;
1160 if (opts->x_flag_pic && !opts->x_flag_pie)
1161 opts->x_flag_shlib = 1;
1162 opts->x_flag_opts_finished = true;
1163 }
1164
1165 /* We initialize opts->x_flag_stack_protect to -1 so that targets
1166 can set a default value. With --enable-default-ssp or -fhardened
1167 the default is -fstack-protector-strong. */
1168 if (opts->x_flag_stack_protect == -1)
1169 {
1170 /* This should check FRAME_GROWS_DOWNWARD, but on some targets it's
1171 defined in such a way that it uses flag_stack_protect which can't
1172 be used here. Moreover, some targets like BPF don't support
1173 -fstack-protector at all but we don't know that here. So remember
1174 that flag_stack_protect was set at the behest of -fhardened. */
1175 if (opts->x_flag_hardened)
1176 {
1177 opts->x_flag_stack_protect = SPCT_FLAG_STRONG;
1178 flag_stack_protector_set_by_fhardened_p = true;
1179 }
1180 else
1181 opts->x_flag_stack_protect = DEFAULT_FLAG_SSP;
1182 }
1183 else if (opts->x_flag_hardened
1184 && opts->x_flag_stack_protect != SPCT_FLAG_STRONG)
1185 warning_at (UNKNOWN_LOCATION, OPT_Whardened,
1186 "%<-fstack-protector-strong%> is not enabled by "
1187 "%<-fhardened%> because it was specified on the command "
1188 "line");
1189
1190 if (opts->x_optimize == 0)
1191 {
1192 /* Inlining does not work if not optimizing,
1193 so force it not to be done. */
1194 opts->x_warn_inline = 0;
1195 opts->x_flag_no_inline = 1;
1196 }
1197
1198 /* At -O0 or -Og, turn __builtin_unreachable into a trap. */
1199 if (!opts->x_optimize || opts->x_optimize_debug)
1200 SET_OPTION_IF_UNSET (opts, opts_set, flag_unreachable_traps, true);
1201
1202 /* Pipelining of outer loops is only possible when general pipelining
1203 capabilities are requested. */
1204 if (!opts->x_flag_sel_sched_pipelining)
1205 opts->x_flag_sel_sched_pipelining_outer_loops = 0;
1206
1207 if (opts->x_flag_conserve_stack)
1208 {
1209 SET_OPTION_IF_UNSET (opts, opts_set, param_large_stack_frame, 100);
1210 SET_OPTION_IF_UNSET (opts, opts_set, param_stack_frame_growth, 40);
1211 }
1212
1213 if (opts->x_flag_lto)
1214 {
1215 #ifdef ENABLE_LTO
1216 opts->x_flag_generate_lto = 1;
1217
1218 /* When generating IL, do not operate in whole-program mode.
1219 Otherwise, symbols will be privatized too early, causing link
1220 errors later. */
1221 opts->x_flag_whole_program = 0;
1222 #else
1223 error_at (loc, "LTO support has not been enabled in this configuration");
1224 #endif
1225 if (!opts->x_flag_fat_lto_objects
1226 && (!HAVE_LTO_PLUGIN
1227 || (opts_set->x_flag_use_linker_plugin
1228 && !opts->x_flag_use_linker_plugin)))
1229 {
1230 if (opts_set->x_flag_fat_lto_objects)
1231 error_at (loc, "%<-fno-fat-lto-objects%> are supported only with "
1232 "linker plugin");
1233 opts->x_flag_fat_lto_objects = 1;
1234 }
1235
1236 /* -gsplit-dwarf isn't compatible with LTO, see PR88389. */
1237 if (opts->x_dwarf_split_debug_info)
1238 {
1239 inform (loc, "%<-gsplit-dwarf%> is not supported with LTO,"
1240 " disabling");
1241 opts->x_dwarf_split_debug_info = 0;
1242 }
1243 }
1244
1245 /* We initialize opts->x_flag_split_stack to -1 so that targets can set a
1246 default value if they choose based on other options. */
1247 if (opts->x_flag_split_stack == -1)
1248 opts->x_flag_split_stack = 0;
1249 else if (opts->x_flag_split_stack)
1250 {
1251 if (!targetm_common.supports_split_stack (true, opts))
1252 {
1253 error_at (loc, "%<-fsplit-stack%> is not supported by "
1254 "this compiler configuration");
1255 opts->x_flag_split_stack = 0;
1256 }
1257 }
1258
1259 /* If stack splitting is turned on, and the user did not explicitly
1260 request function partitioning, turn off partitioning, as it
1261 confuses the linker when trying to handle partitioned split-stack
1262 code that calls a non-split-stack functions. But if partitioning
1263 was turned on explicitly just hope for the best. */
1264 if (opts->x_flag_split_stack
1265 && opts->x_flag_reorder_blocks_and_partition)
1266 SET_OPTION_IF_UNSET (opts, opts_set, flag_reorder_blocks_and_partition, 0);
1267
1268 if (opts->x_flag_reorder_blocks_and_partition)
1269 SET_OPTION_IF_UNSET (opts, opts_set, flag_reorder_functions, 1);
1270
1271 validate_ipa_reorder_locality_lto_partition (opts, opts_set);
1272
1273 /* The -gsplit-dwarf option requires -ggnu-pubnames. */
1274 if (opts->x_dwarf_split_debug_info)
1275 opts->x_debug_generate_pub_sections = 2;
1276
1277 if ((opts->x_flag_sanitize
1278 & (SANITIZE_USER_ADDRESS | SANITIZE_KERNEL_ADDRESS)) == 0)
1279 {
1280 if (opts->x_flag_sanitize & SANITIZE_POINTER_COMPARE)
1281 error_at (loc,
1282 "%<-fsanitize=pointer-compare%> must be combined with "
1283 "%<-fsanitize=address%> or %<-fsanitize=kernel-address%>");
1284 if (opts->x_flag_sanitize & SANITIZE_POINTER_SUBTRACT)
1285 error_at (loc,
1286 "%<-fsanitize=pointer-subtract%> must be combined with "
1287 "%<-fsanitize=address%> or %<-fsanitize=kernel-address%>");
1288 }
1289
1290 /* Address sanitizers conflict with the thread sanitizer. */
1291 report_conflicting_sanitizer_options (opts, loc, SANITIZE_THREAD,
1292 SANITIZE_ADDRESS);
1293 report_conflicting_sanitizer_options (opts, loc, SANITIZE_THREAD,
1294 SANITIZE_HWADDRESS);
1295 /* The leak sanitizer conflicts with the thread sanitizer. */
1296 report_conflicting_sanitizer_options (opts, loc, SANITIZE_LEAK,
1297 SANITIZE_THREAD);
1298
1299 /* No combination of HWASAN and ASAN work together. */
1300 report_conflicting_sanitizer_options (opts, loc,
1301 SANITIZE_HWADDRESS, SANITIZE_ADDRESS);
1302
1303 /* The userspace and kernel address sanitizers conflict with each other. */
1304 report_conflicting_sanitizer_options (opts, loc, SANITIZE_USER_HWADDRESS,
1305 SANITIZE_KERNEL_HWADDRESS);
1306 report_conflicting_sanitizer_options (opts, loc, SANITIZE_USER_ADDRESS,
1307 SANITIZE_KERNEL_ADDRESS);
1308
1309 /* Check error recovery for -fsanitize-recover option. */
1310 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
1311 if ((opts->x_flag_sanitize_recover & sanitizer_opts[i].flag)
1312 && !sanitizer_opts[i].can_recover)
1313 error_at (loc, "%<-fsanitize-recover=%s%> is not supported",
1314 sanitizer_opts[i].name);
1315
1316 /* Check -fsanitize-trap option. */
1317 for (int i = 0; sanitizer_opts[i].name != NULL; ++i)
1318 if ((opts->x_flag_sanitize_trap & sanitizer_opts[i].flag)
1319 && !sanitizer_opts[i].can_trap
1320 /* Allow -fsanitize-trap=all or -fsanitize-trap=undefined
1321 to set flag_sanitize_trap & SANITIZE_VPTR bit which will
1322 effectively disable -fsanitize=vptr, just disallow
1323 explicit -fsanitize-trap=vptr. */
1324 && sanitizer_opts[i].flag != SANITIZE_VPTR)
1325 error_at (loc, "%<-fsanitize-trap=%s%> is not supported",
1326 sanitizer_opts[i].name);
1327
1328 /* When instrumenting the pointers, we don't want to remove
1329 the null pointer checks. */
1330 if (opts->x_flag_sanitize & (SANITIZE_NULL | SANITIZE_NONNULL_ATTRIBUTE
1331 | SANITIZE_RETURNS_NONNULL_ATTRIBUTE))
1332 opts->x_flag_delete_null_pointer_checks = 0;
1333
1334 /* Aggressive compiler optimizations may cause false negatives. */
1335 if (opts->x_flag_sanitize & ~(SANITIZE_LEAK | SANITIZE_UNREACHABLE))
1336 opts->x_flag_aggressive_loop_optimizations = 0;
1337
1338 /* Enable -fsanitize-address-use-after-scope if either address sanitizer is
1339 enabled. */
1340 if (opts->x_flag_sanitize
1341 & (SANITIZE_USER_ADDRESS | SANITIZE_USER_HWADDRESS))
1342 SET_OPTION_IF_UNSET (opts, opts_set, flag_sanitize_address_use_after_scope,
1343 true);
1344
1345 /* Force -fstack-reuse=none in case -fsanitize-address-use-after-scope
1346 is enabled. */
1347 if (opts->x_flag_sanitize_address_use_after_scope)
1348 {
1349 if (opts->x_flag_stack_reuse != SR_NONE
1350 && opts_set->x_flag_stack_reuse != SR_NONE)
1351 error_at (loc,
1352 "%<-fsanitize-address-use-after-scope%> requires "
1353 "%<-fstack-reuse=none%> option");
1354
1355 opts->x_flag_stack_reuse = SR_NONE;
1356 }
1357
1358 if ((opts->x_flag_sanitize & SANITIZE_USER_ADDRESS) && opts->x_flag_tm)
1359 sorry ("transactional memory is not supported with %<-fsanitize=address%>");
1360
1361 if ((opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS) && opts->x_flag_tm)
1362 sorry ("transactional memory is not supported with "
1363 "%<-fsanitize=kernel-address%>");
1364
1365 /* Currently live patching is not support for LTO. */
1366 if (opts->x_flag_live_patching == LIVE_PATCHING_INLINE_ONLY_STATIC && opts->x_flag_lto)
1367 sorry ("live patching (with %qs) is not supported with LTO",
1368 "inline-only-static");
1369
1370 /* Currently vtable verification is not supported for LTO */
1371 if (opts->x_flag_vtable_verify && opts->x_flag_lto)
1372 sorry ("vtable verification is not supported with LTO");
1373
1374 /* Control IPA optimizations based on different -flive-patching level. */
1375 if (opts->x_flag_live_patching)
1376 control_options_for_live_patching (opts, opts_set,
1377 opts->x_flag_live_patching,
1378 loc);
1379
1380 /* Allow cunroll to grow size accordingly. */
1381 if (!opts_set->x_flag_cunroll_grow_size)
1382 opts->x_flag_cunroll_grow_size
1383 = (opts->x_flag_unroll_loops
1384 || opts->x_flag_peel_loops
1385 || opts->x_optimize >= 3);
1386
1387 /* With -fcx-limited-range, we do cheap and quick complex arithmetic. */
1388 if (opts->x_flag_cx_limited_range)
1389 opts->x_flag_complex_method = 0;
1390 else if (opts_set->x_flag_cx_limited_range)
1391 opts->x_flag_complex_method = opts->x_flag_default_complex_method;
1392
1393 /* With -fcx-fortran-rules, we do something in-between cheap and C99. */
1394 if (opts->x_flag_cx_fortran_rules)
1395 opts->x_flag_complex_method = 1;
1396 else if (opts_set->x_flag_cx_fortran_rules)
1397 opts->x_flag_complex_method = opts->x_flag_default_complex_method;
1398
1399 /* Use -fvect-cost-model=cheap instead of -fvect-cost-mode=very-cheap
1400 by default with explicit -ftree-{loop,slp}-vectorize. */
1401 if (opts->x_optimize == 2
1402 && (opts_set->x_flag_tree_loop_vectorize
1403 || opts_set->x_flag_tree_vectorize))
1404 SET_OPTION_IF_UNSET (opts, opts_set, flag_vect_cost_model,
1405 VECT_COST_MODEL_CHEAP);
1406
1407 if (opts->x_flag_gtoggle)
1408 {
1409 /* Make sure to process -gtoggle only once. */
1410 opts->x_flag_gtoggle = false;
1411 if (opts->x_debug_info_level == DINFO_LEVEL_NONE)
1412 {
1413 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
1414
1415 if (opts->x_write_symbols == NO_DEBUG)
1416 opts->x_write_symbols = PREFERRED_DEBUGGING_TYPE;
1417 }
1418 else
1419 opts->x_debug_info_level = DINFO_LEVEL_NONE;
1420 }
1421
1422 if (!opts_set->x_debug_nonbind_markers_p)
1423 opts->x_debug_nonbind_markers_p
1424 = (opts->x_optimize
1425 && opts->x_debug_info_level >= DINFO_LEVEL_NORMAL
1426 && (dwarf_debuginfo_p (opts) || codeview_debuginfo_p ())
1427 && !(opts->x_flag_selective_scheduling
1428 || opts->x_flag_selective_scheduling2));
1429
1430 /* We know which debug output will be used so we can set flag_var_tracking
1431 and flag_var_tracking_uninit if the user has not specified them. */
1432 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL
1433 || (!dwarf_debuginfo_p (opts) && !codeview_debuginfo_p ())
1434 /* We have not yet initialized debug hooks so match that to check
1435 whether we're only doing DWARF2_LINENO_DEBUGGING_INFO. */
1436 #ifndef DWARF2_DEBUGGING_INFO
1437 || true
1438 #endif
1439 )
1440 {
1441 if ((opts_set->x_flag_var_tracking && opts->x_flag_var_tracking == 1)
1442 || (opts_set->x_flag_var_tracking_uninit
1443 && opts->x_flag_var_tracking_uninit == 1))
1444 {
1445 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
1446 warning_at (UNKNOWN_LOCATION, 0,
1447 "variable tracking requested, but useless unless "
1448 "producing debug info");
1449 else
1450 warning_at (UNKNOWN_LOCATION, 0,
1451 "variable tracking requested, but not supported "
1452 "by this debug format");
1453 }
1454 opts->x_flag_var_tracking = 0;
1455 opts->x_flag_var_tracking_uninit = 0;
1456 opts->x_flag_var_tracking_assignments = 0;
1457 }
1458
1459 /* One could use EnabledBy, but it would lead to a circular dependency. */
1460 if (!opts_set->x_flag_var_tracking_uninit)
1461 opts->x_flag_var_tracking_uninit = opts->x_flag_var_tracking;
1462
1463 if (!opts_set->x_flag_var_tracking_assignments)
1464 opts->x_flag_var_tracking_assignments
1465 = (opts->x_flag_var_tracking
1466 && !(opts->x_flag_selective_scheduling
1467 || opts->x_flag_selective_scheduling2));
1468
1469 if (opts->x_flag_var_tracking_assignments_toggle)
1470 opts->x_flag_var_tracking_assignments
1471 = !opts->x_flag_var_tracking_assignments;
1472
1473 if (opts->x_flag_var_tracking_assignments && !opts->x_flag_var_tracking)
1474 opts->x_flag_var_tracking = opts->x_flag_var_tracking_assignments = -1;
1475
1476 if (opts->x_flag_var_tracking_assignments
1477 && (opts->x_flag_selective_scheduling
1478 || opts->x_flag_selective_scheduling2))
1479 warning_at (loc, 0,
1480 "var-tracking-assignments changes selective scheduling");
1481
1482 if (opts->x_flag_syntax_only)
1483 {
1484 opts->x_write_symbols = NO_DEBUG;
1485 opts->x_profile_flag = 0;
1486 }
1487
1488 if (opts->x_warn_strict_flex_arrays)
1489 if (opts->x_flag_strict_flex_arrays == 0)
1490 {
1491 opts->x_warn_strict_flex_arrays = 0;
1492 warning_at (UNKNOWN_LOCATION, 0,
1493 "%<-Wstrict-flex-arrays%> is ignored when"
1494 " %<-fstrict-flex-arrays%> is not present");
1495 }
1496
1497 diagnose_options (opts, opts_set, loc);
1498 }
1499
1500 /* The function diagnoses incompatible combinations for provided options
1501 (OPTS and OPTS_SET) at a given LOCation. The function is called both
1502 when command line is parsed (after the target optimization hook) and
1503 when an optimize/target attribute (or pragma) is used. */
1504
1505 void diagnose_options (gcc_options *opts, gcc_options *opts_set,
1506 location_t loc)
1507 {
1508 /* The optimization to partition hot and cold basic blocks into separate
1509 sections of the .o and executable files does not work (currently)
1510 with exception handling. This is because there is no support for
1511 generating unwind info. If opts->x_flag_exceptions is turned on
1512 we need to turn off the partitioning optimization. */
1513
1514 enum unwind_info_type ui_except
1515 = targetm_common.except_unwind_info (opts);
1516
1517 if (opts->x_flag_exceptions
1518 && opts->x_flag_reorder_blocks_and_partition
1519 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))
1520 {
1521 if (opts_set->x_flag_reorder_blocks_and_partition)
1522 inform (loc,
1523 "%<-freorder-blocks-and-partition%> does not work "
1524 "with exceptions on this architecture");
1525 opts->x_flag_reorder_blocks_and_partition = 0;
1526 opts->x_flag_reorder_blocks = 1;
1527 }
1528
1529 /* If user requested unwind info, then turn off the partitioning
1530 optimization. */
1531
1532 if (opts->x_flag_unwind_tables
1533 && !targetm_common.unwind_tables_default
1534 && opts->x_flag_reorder_blocks_and_partition
1535 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))
1536 {
1537 if (opts_set->x_flag_reorder_blocks_and_partition)
1538 inform (loc,
1539 "%<-freorder-blocks-and-partition%> does not support "
1540 "unwind info on this architecture");
1541 opts->x_flag_reorder_blocks_and_partition = 0;
1542 opts->x_flag_reorder_blocks = 1;
1543 }
1544
1545 /* If the target requested unwind info, then turn off the partitioning
1546 optimization with a different message. Likewise, if the target does not
1547 support named sections. */
1548
1549 if (opts->x_flag_reorder_blocks_and_partition
1550 && (!targetm_common.have_named_sections
1551 || (opts->x_flag_unwind_tables
1552 && targetm_common.unwind_tables_default
1553 && (ui_except == UI_SJLJ || ui_except >= UI_TARGET))))
1554 {
1555 if (opts_set->x_flag_reorder_blocks_and_partition)
1556 inform (loc,
1557 "%<-freorder-blocks-and-partition%> does not work "
1558 "on this architecture");
1559 opts->x_flag_reorder_blocks_and_partition = 0;
1560 opts->x_flag_reorder_blocks = 1;
1561 }
1562
1563
1564 }
1565
1566 #define LEFT_COLUMN 27
1567
1568 /* Output ITEM, of length ITEM_WIDTH, in the left column,
1569 followed by word-wrapped HELP in a second column. */
1570 static void
1571 wrap_help (const char *help,
1572 const char *item,
1573 unsigned int item_width,
1574 unsigned int columns)
1575 {
1576 unsigned int col_width = LEFT_COLUMN;
1577 unsigned int remaining, room, len;
1578
1579 remaining = strlen (help);
1580
1581 do
1582 {
1583 room = columns - 3 - MAX (col_width, item_width);
1584 if (room > columns)
1585 room = 0;
1586 len = remaining;
1587
1588 if (room < len)
1589 {
1590 unsigned int i;
1591
1592 for (i = 0; help[i]; i++)
1593 {
1594 if (i >= room && len != remaining)
1595 break;
1596 if (help[i] == ' ')
1597 len = i;
1598 else if ((help[i] == '-' || help[i] == '/')
1599 && help[i + 1] != ' '
1600 && i > 0 && ISALPHA (help[i - 1]))
1601 len = i + 1;
1602 }
1603 }
1604
1605 printf (" %-*.*s %.*s\n", col_width, item_width, item, len, help);
1606 item_width = 0;
1607 while (help[len] == ' ')
1608 len++;
1609 help += len;
1610 remaining -= len;
1611 }
1612 while (remaining);
1613 }
1614
1615 /* Data structure used to print list of valid option values. */
1616
1617 class option_help_tuple
1618 {
1619 public:
1620 option_help_tuple (int code, vec<const char *> values):
1621 m_code (code), m_values (values)
1622 {}
1623
1624 /* Code of an option. */
1625 int m_code;
1626
1627 /* List of possible values. */
1628 vec<const char *> m_values;
1629 };
1630
1631 /* Print help for a specific front-end, etc. */
1632 static void
1633 print_filtered_help (unsigned int include_flags,
1634 unsigned int exclude_flags,
1635 unsigned int any_flags,
1636 unsigned int columns,
1637 struct gcc_options *opts,
1638 unsigned int lang_mask)
1639 {
1640 unsigned int i;
1641 const char *help;
1642 bool found = false;
1643 bool displayed = false;
1644 char new_help[256];
1645
1646 if (!opts->x_help_printed)
1647 opts->x_help_printed = XCNEWVAR (char, cl_options_count);
1648
1649 if (!opts->x_help_enum_printed)
1650 opts->x_help_enum_printed = XCNEWVAR (char, cl_enums_count);
1651
1652 auto_vec<option_help_tuple> help_tuples;
1653
1654 for (i = 0; i < cl_options_count; i++)
1655 {
1656 const struct cl_option *option = cl_options + i;
1657 unsigned int len;
1658 const char *opt;
1659 const char *tab;
1660
1661 if (include_flags == 0
1662 || ((option->flags & include_flags) != include_flags))
1663 {
1664 if ((option->flags & any_flags) == 0)
1665 continue;
1666 }
1667
1668 /* Skip unwanted switches. */
1669 if ((option->flags & exclude_flags) != 0)
1670 continue;
1671
1672 /* The driver currently prints its own help text. */
1673 if ((option->flags & CL_DRIVER) != 0
1674 && (option->flags & (((1U << cl_lang_count) - 1)
1675 | CL_COMMON | CL_TARGET)) == 0)
1676 continue;
1677
1678 /* If an option contains a language specification,
1679 exclude it from common unless all languages are present. */
1680 if ((include_flags & CL_COMMON)
1681 && !(option->flags & CL_DRIVER)
1682 && (option->flags & CL_LANG_ALL)
1683 && (option->flags & CL_LANG_ALL) != CL_LANG_ALL)
1684 continue;
1685
1686 found = true;
1687 /* Skip switches that have already been printed. */
1688 if (opts->x_help_printed[i])
1689 continue;
1690
1691 opts->x_help_printed[i] = true;
1692
1693 help = option->help;
1694 if (help == NULL)
1695 {
1696 if (exclude_flags & CL_UNDOCUMENTED)
1697 continue;
1698
1699 help = undocumented_msg;
1700 }
1701
1702 /* Get the translation. */
1703 help = _(help);
1704
1705 if (option->alias_target < N_OPTS
1706 && cl_options [option->alias_target].help)
1707 {
1708 const struct cl_option *target = cl_options + option->alias_target;
1709 if (option->help == NULL)
1710 {
1711 /* The option is undocumented but is an alias for an option that
1712 is documented. If the option has alias arguments, then its
1713 purpose is to provide certain arguments to the other option, so
1714 inform the reader of this. Otherwise, point the reader to the
1715 other option in preference to the former. */
1716
1717 if (option->alias_arg)
1718 {
1719 if (option->neg_alias_arg)
1720 snprintf (new_help, sizeof new_help,
1721 _("Same as %s%s (or, in negated form, %s%s)."),
1722 target->opt_text, option->alias_arg,
1723 target->opt_text, option->neg_alias_arg);
1724 else
1725 snprintf (new_help, sizeof new_help,
1726 _("Same as %s%s."),
1727 target->opt_text, option->alias_arg);
1728 }
1729 else
1730 snprintf (new_help, sizeof new_help,
1731 _("Same as %s."),
1732 target->opt_text);
1733 }
1734 else
1735 {
1736 /* For documented options with aliases, mention the aliased
1737 option's name for reference. */
1738 snprintf (new_help, sizeof new_help,
1739 _("%s Same as %s."),
1740 help, cl_options [option->alias_target].opt_text);
1741 }
1742
1743 help = new_help;
1744 }
1745
1746 if (option->warn_message)
1747 {
1748 /* Mention that the use of the option will trigger a warning. */
1749 if (help == new_help)
1750 snprintf (new_help + strlen (new_help),
1751 sizeof new_help - strlen (new_help),
1752 " %s", _(use_diagnosed_msg));
1753 else
1754 snprintf (new_help, sizeof new_help,
1755 "%s %s", help, _(use_diagnosed_msg));
1756
1757 help = new_help;
1758 }
1759
1760 /* Find the gap between the name of the
1761 option and its descriptive text. */
1762 tab = strchr (help, '\t');
1763 if (tab)
1764 {
1765 len = tab - help;
1766 opt = help;
1767 help = tab + 1;
1768 }
1769 else
1770 {
1771 opt = option->opt_text;
1772 len = strlen (opt);
1773 }
1774
1775 /* With the -Q option enabled we change the descriptive text associated
1776 with an option to be an indication of its current setting. */
1777 if (!opts->x_quiet_flag)
1778 {
1779 void *flag_var = option_flag_var (i, opts);
1780
1781 if (len < (LEFT_COLUMN + 2))
1782 strcpy (new_help, "\t\t");
1783 else
1784 strcpy (new_help, "\t");
1785
1786 /* Set to print whether the option is enabled or disabled,
1787 or, if it's an alias for another option, the name of
1788 the aliased option. */
1789 bool print_state = false;
1790
1791 if (flag_var != NULL
1792 && option->var_type != CLVC_DEFER)
1793 {
1794 /* If OPTION is only available for a specific subset
1795 of languages other than this one, mention them. */
1796 bool avail_for_lang = true;
1797 if (unsigned langset = option->flags & CL_LANG_ALL)
1798 {
1799 if (!(langset & lang_mask))
1800 {
1801 avail_for_lang = false;
1802 strcat (new_help, _("[available in "));
1803 for (unsigned i = 0, n = 0; (1U << i) < CL_LANG_ALL; ++i)
1804 if (langset & (1U << i))
1805 {
1806 if (n++)
1807 strcat (new_help, ", ");
1808 strcat (new_help, lang_names[i]);
1809 }
1810 strcat (new_help, "]");
1811 }
1812 }
1813 if (!avail_for_lang)
1814 ; /* Print nothing else if the option is not available
1815 in the current language. */
1816 else if (option->flags & CL_JOINED)
1817 {
1818 if (option->var_type == CLVC_STRING)
1819 {
1820 if (* (const char **) flag_var != NULL)
1821 snprintf (new_help + strlen (new_help),
1822 sizeof (new_help) - strlen (new_help),
1823 "%s", * (const char **) flag_var);
1824 }
1825 else if (option->var_type == CLVC_ENUM)
1826 {
1827 const struct cl_enum *e = &cl_enums[option->var_enum];
1828 int value;
1829 const char *arg = NULL;
1830
1831 value = e->get (flag_var);
1832 enum_value_to_arg (e->values, &arg, value, lang_mask);
1833 if (arg == NULL)
1834 arg = _("[default]");
1835 snprintf (new_help + strlen (new_help),
1836 sizeof (new_help) - strlen (new_help),
1837 "%s", arg);
1838 }
1839 else
1840 {
1841 if (option->cl_host_wide_int)
1842 sprintf (new_help + strlen (new_help),
1843 _("%llu bytes"), (unsigned long long)
1844 *(unsigned HOST_WIDE_INT *) flag_var);
1845 else
1846 sprintf (new_help + strlen (new_help),
1847 "%i", * (int *) flag_var);
1848 }
1849 }
1850 else
1851 print_state = true;
1852 }
1853 else
1854 /* When there is no argument, print the option state only
1855 if the option takes no argument. */
1856 print_state = !(option->flags & CL_JOINED);
1857
1858 if (print_state)
1859 {
1860 if (option->alias_target < N_OPTS
1861 && option->alias_target != OPT_SPECIAL_warn_removed
1862 && option->alias_target != OPT_SPECIAL_ignore
1863 && option->alias_target != OPT_SPECIAL_input_file
1864 && option->alias_target != OPT_SPECIAL_program_name
1865 && option->alias_target != OPT_SPECIAL_unknown)
1866 {
1867 const struct cl_option *target
1868 = &cl_options[option->alias_target];
1869 sprintf (new_help + strlen (new_help), "%s%s",
1870 target->opt_text,
1871 option->alias_arg ? option->alias_arg : "");
1872 }
1873 else if (option->alias_target == OPT_SPECIAL_ignore)
1874 strcat (new_help, ("[ignored]"));
1875 else
1876 {
1877 /* Print the state for an on/off option. */
1878 int ena = option_enabled (i, lang_mask, opts);
1879 if (ena > 0)
1880 strcat (new_help, _("[enabled]"));
1881 else if (ena == 0)
1882 strcat (new_help, _("[disabled]"));
1883 }
1884 }
1885
1886 help = new_help;
1887 }
1888
1889 if (option->range_max != -1 && tab == NULL)
1890 {
1891 char b[128];
1892 snprintf (b, sizeof (b), "<%d,%d>", option->range_min,
1893 option->range_max);
1894 opt = concat (opt, b, NULL);
1895 len += strlen (b);
1896 }
1897
1898 wrap_help (help, opt, len, columns);
1899 displayed = true;
1900
1901 if (option->var_type == CLVC_ENUM
1902 && opts->x_help_enum_printed[option->var_enum] != 2)
1903 opts->x_help_enum_printed[option->var_enum] = 1;
1904 else
1905 {
1906 vec<const char *> option_values
1907 = targetm_common.get_valid_option_values (i, NULL);
1908 if (!option_values.is_empty ())
1909 help_tuples.safe_push (option_help_tuple (i, option_values));
1910 }
1911 }
1912
1913 if (! found)
1914 {
1915 unsigned int langs = include_flags & CL_LANG_ALL;
1916
1917 if (langs == 0)
1918 printf (_(" No options with the desired characteristics were found\n"));
1919 else
1920 {
1921 unsigned int i;
1922
1923 /* PR 31349: Tell the user how to see all of the
1924 options supported by a specific front end. */
1925 for (i = 0; (1U << i) < CL_LANG_ALL; i ++)
1926 if ((1U << i) & langs)
1927 printf (_(" None found. Use --help=%s to show *all* the options supported by the %s front-end.\n"),
1928 lang_names[i], lang_names[i]);
1929 }
1930
1931 }
1932 else if (! displayed)
1933 printf (_(" All options with the desired characteristics have already been displayed\n"));
1934
1935 putchar ('\n');
1936
1937 /* Print details of enumerated option arguments, if those
1938 enumerations have help text headings provided. If no help text
1939 is provided, presume that the possible values are listed in the
1940 help text for the relevant options. */
1941 for (i = 0; i < cl_enums_count; i++)
1942 {
1943 unsigned int j, pos;
1944
1945 if (opts->x_help_enum_printed[i] != 1)
1946 continue;
1947 if (cl_enums[i].help == NULL)
1948 continue;
1949 printf (" %s\n ", _(cl_enums[i].help));
1950 pos = 4;
1951 for (j = 0; cl_enums[i].values[j].arg != NULL; j++)
1952 {
1953 unsigned int len = strlen (cl_enums[i].values[j].arg);
1954
1955 if (pos > 4 && pos + 1 + len <= columns)
1956 {
1957 printf (" %s", cl_enums[i].values[j].arg);
1958 pos += 1 + len;
1959 }
1960 else
1961 {
1962 if (pos > 4)
1963 {
1964 printf ("\n ");
1965 pos = 4;
1966 }
1967 printf ("%s", cl_enums[i].values[j].arg);
1968 pos += len;
1969 }
1970 }
1971 printf ("\n\n");
1972 opts->x_help_enum_printed[i] = 2;
1973 }
1974
1975 for (unsigned i = 0; i < help_tuples.length (); i++)
1976 {
1977 const struct cl_option *option = cl_options + help_tuples[i].m_code;
1978 printf (_(" Known valid arguments for %s option:\n "),
1979 option->opt_text);
1980 for (unsigned j = 0; j < help_tuples[i].m_values.length (); j++)
1981 printf (" %s", help_tuples[i].m_values[j]);
1982 printf ("\n\n");
1983 }
1984 }
1985
1986 /* Display help for a specified type of option.
1987 The options must have ALL of the INCLUDE_FLAGS set
1988 ANY of the flags in the ANY_FLAGS set
1989 and NONE of the EXCLUDE_FLAGS set. The current option state is in
1990 OPTS; LANG_MASK is used for interpreting enumerated option state. */
1991 static void
1992 print_specific_help (unsigned int include_flags,
1993 unsigned int exclude_flags,
1994 unsigned int any_flags,
1995 struct gcc_options *opts,
1996 unsigned int lang_mask)
1997 {
1998 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
1999 const char * description = NULL;
2000 const char * descrip_extra = "";
2001 size_t i;
2002 unsigned int flag;
2003
2004 /* Sanity check: Make sure that we do not have more
2005 languages than we have bits available to enumerate them. */
2006 gcc_assert ((1U << cl_lang_count) <= CL_MIN_OPTION_CLASS);
2007
2008 /* If we have not done so already, obtain
2009 the desired maximum width of the output. */
2010 if (opts->x_help_columns == 0)
2011 {
2012 opts->x_help_columns = get_terminal_width ();
2013 if (opts->x_help_columns == INT_MAX)
2014 /* Use a reasonable default. */
2015 opts->x_help_columns = 80;
2016 }
2017
2018 /* Decide upon the title for the options that we are going to display. */
2019 for (i = 0, flag = 1; flag <= CL_MAX_OPTION_CLASS; flag <<= 1, i ++)
2020 {
2021 switch (flag & include_flags)
2022 {
2023 case 0:
2024 case CL_DRIVER:
2025 break;
2026
2027 case CL_TARGET:
2028 description = _("The following options are target specific");
2029 break;
2030 case CL_WARNING:
2031 description = _("The following options control compiler warning messages");
2032 break;
2033 case CL_OPTIMIZATION:
2034 description = _("The following options control optimizations");
2035 break;
2036 case CL_COMMON:
2037 description = _("The following options are language-independent");
2038 break;
2039 case CL_PARAMS:
2040 description = _("The following options control parameters");
2041 break;
2042 default:
2043 if (i >= cl_lang_count)
2044 break;
2045 if (exclude_flags & all_langs_mask)
2046 description = _("The following options are specific to just the language ");
2047 else
2048 description = _("The following options are supported by the language ");
2049 descrip_extra = lang_names [i];
2050 break;
2051 }
2052 }
2053
2054 if (description == NULL)
2055 {
2056 if (any_flags == 0)
2057 {
2058 if (include_flags & CL_UNDOCUMENTED)
2059 description = _("The following options are not documented");
2060 else if (include_flags & CL_SEPARATE)
2061 description = _("The following options take separate arguments");
2062 else if (include_flags & CL_JOINED)
2063 description = _("The following options take joined arguments");
2064 else
2065 {
2066 internal_error ("unrecognized %<include_flags 0x%x%> passed "
2067 "to %<print_specific_help%>",
2068 include_flags);
2069 return;
2070 }
2071 }
2072 else
2073 {
2074 if (any_flags & all_langs_mask)
2075 description = _("The following options are language-related");
2076 else
2077 description = _("The following options are language-independent");
2078 }
2079 }
2080
2081 printf ("%s%s:\n", description, descrip_extra);
2082 print_filtered_help (include_flags, exclude_flags, any_flags,
2083 opts->x_help_columns, opts, lang_mask);
2084 }
2085
2086 /* Enable FDO-related flags. */
2087
2088 static void
2089 enable_fdo_optimizations (struct gcc_options *opts,
2090 struct gcc_options *opts_set,
2091 int value)
2092 {
2093 SET_OPTION_IF_UNSET (opts, opts_set, flag_branch_probabilities, value);
2094 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_values, value);
2095 SET_OPTION_IF_UNSET (opts, opts_set, flag_unroll_loops, value);
2096 SET_OPTION_IF_UNSET (opts, opts_set, flag_peel_loops, value);
2097 SET_OPTION_IF_UNSET (opts, opts_set, flag_tracer, value);
2098 SET_OPTION_IF_UNSET (opts, opts_set, flag_value_profile_transformations,
2099 value);
2100 SET_OPTION_IF_UNSET (opts, opts_set, flag_inline_functions, value);
2101 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_cp, value);
2102 if (value)
2103 {
2104 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_cp_clone, 1);
2105 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_bit_cp, 1);
2106 }
2107 SET_OPTION_IF_UNSET (opts, opts_set, flag_predictive_commoning, value);
2108 SET_OPTION_IF_UNSET (opts, opts_set, flag_split_loops, value);
2109 SET_OPTION_IF_UNSET (opts, opts_set, flag_unswitch_loops, value);
2110 SET_OPTION_IF_UNSET (opts, opts_set, flag_gcse_after_reload, value);
2111 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_loop_vectorize, value);
2112 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_slp_vectorize, value);
2113 SET_OPTION_IF_UNSET (opts, opts_set, flag_version_loops_for_strides, value);
2114 SET_OPTION_IF_UNSET (opts, opts_set, flag_vect_cost_model,
2115 VECT_COST_MODEL_DYNAMIC);
2116 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_loop_distribute_patterns,
2117 value);
2118 SET_OPTION_IF_UNSET (opts, opts_set, flag_loop_interchange, value);
2119 SET_OPTION_IF_UNSET (opts, opts_set, flag_unroll_jam, value);
2120 SET_OPTION_IF_UNSET (opts, opts_set, flag_tree_loop_distribution, value);
2121 SET_OPTION_IF_UNSET (opts, opts_set, flag_optimize_crc, value);
2122 }
2123
2124 /* -f{,no-}sanitize{,-recover}= suboptions. */
2125 const struct sanitizer_opts_s sanitizer_opts[] =
2126 {
2127 #define SANITIZER_OPT(name, flags, recover, trap) \
2128 { #name, flags, sizeof #name - 1, recover, trap }
2129 SANITIZER_OPT (address, (SANITIZE_ADDRESS | SANITIZE_USER_ADDRESS), true,
2130 false),
2131 SANITIZER_OPT (hwaddress, (SANITIZE_HWADDRESS | SANITIZE_USER_HWADDRESS),
2132 true, false),
2133 SANITIZER_OPT (kernel-address, (SANITIZE_ADDRESS | SANITIZE_KERNEL_ADDRESS),
2134 true, false),
2135 SANITIZER_OPT (kernel-hwaddress,
2136 (SANITIZE_HWADDRESS | SANITIZE_KERNEL_HWADDRESS),
2137 true, false),
2138 SANITIZER_OPT (pointer-compare, SANITIZE_POINTER_COMPARE, true, false),
2139 SANITIZER_OPT (pointer-subtract, SANITIZE_POINTER_SUBTRACT, true, false),
2140 SANITIZER_OPT (thread, SANITIZE_THREAD, false, false),
2141 SANITIZER_OPT (leak, SANITIZE_LEAK, false, false),
2142 SANITIZER_OPT (shift, SANITIZE_SHIFT, true, true),
2143 SANITIZER_OPT (shift-base, SANITIZE_SHIFT_BASE, true, true),
2144 SANITIZER_OPT (shift-exponent, SANITIZE_SHIFT_EXPONENT, true, true),
2145 SANITIZER_OPT (integer-divide-by-zero, SANITIZE_DIVIDE, true, true),
2146 SANITIZER_OPT (undefined, SANITIZE_UNDEFINED, true, true),
2147 SANITIZER_OPT (unreachable, SANITIZE_UNREACHABLE, false, true),
2148 SANITIZER_OPT (vla-bound, SANITIZE_VLA, true, true),
2149 SANITIZER_OPT (return, SANITIZE_RETURN, false, true),
2150 SANITIZER_OPT (null, SANITIZE_NULL, true, true),
2151 SANITIZER_OPT (signed-integer-overflow, SANITIZE_SI_OVERFLOW, true, true),
2152 SANITIZER_OPT (bool, SANITIZE_BOOL, true, true),
2153 SANITIZER_OPT (enum, SANITIZE_ENUM, true, true),
2154 SANITIZER_OPT (float-divide-by-zero, SANITIZE_FLOAT_DIVIDE, true, true),
2155 SANITIZER_OPT (float-cast-overflow, SANITIZE_FLOAT_CAST, true, true),
2156 SANITIZER_OPT (bounds, SANITIZE_BOUNDS, true, true),
2157 SANITIZER_OPT (bounds-strict, SANITIZE_BOUNDS | SANITIZE_BOUNDS_STRICT, true,
2158 true),
2159 SANITIZER_OPT (alignment, SANITIZE_ALIGNMENT, true, true),
2160 SANITIZER_OPT (nonnull-attribute, SANITIZE_NONNULL_ATTRIBUTE, true, true),
2161 SANITIZER_OPT (returns-nonnull-attribute, SANITIZE_RETURNS_NONNULL_ATTRIBUTE,
2162 true, true),
2163 SANITIZER_OPT (object-size, SANITIZE_OBJECT_SIZE, true, true),
2164 SANITIZER_OPT (vptr, SANITIZE_VPTR, true, false),
2165 SANITIZER_OPT (pointer-overflow, SANITIZE_POINTER_OVERFLOW, true, true),
2166 SANITIZER_OPT (builtin, SANITIZE_BUILTIN, true, true),
2167 SANITIZER_OPT (shadow-call-stack, SANITIZE_SHADOW_CALL_STACK, false, false),
2168 SANITIZER_OPT (all, ~0U, true, true),
2169 #undef SANITIZER_OPT
2170 { NULL, 0U, 0UL, false, false }
2171 };
2172
2173 /* -fzero-call-used-regs= suboptions. */
2174 const struct zero_call_used_regs_opts_s zero_call_used_regs_opts[] =
2175 {
2176 #define ZERO_CALL_USED_REGS_OPT(name, flags) \
2177 { #name, flags }
2178 ZERO_CALL_USED_REGS_OPT (skip, zero_regs_flags::SKIP),
2179 ZERO_CALL_USED_REGS_OPT (used-gpr-arg, zero_regs_flags::USED_GPR_ARG),
2180 ZERO_CALL_USED_REGS_OPT (used-gpr, zero_regs_flags::USED_GPR),
2181 ZERO_CALL_USED_REGS_OPT (used-arg, zero_regs_flags::USED_ARG),
2182 ZERO_CALL_USED_REGS_OPT (used, zero_regs_flags::USED),
2183 ZERO_CALL_USED_REGS_OPT (all-gpr-arg, zero_regs_flags::ALL_GPR_ARG),
2184 ZERO_CALL_USED_REGS_OPT (all-gpr, zero_regs_flags::ALL_GPR),
2185 ZERO_CALL_USED_REGS_OPT (all-arg, zero_regs_flags::ALL_ARG),
2186 ZERO_CALL_USED_REGS_OPT (all, zero_regs_flags::ALL),
2187 ZERO_CALL_USED_REGS_OPT (leafy-gpr-arg, zero_regs_flags::LEAFY_GPR_ARG),
2188 ZERO_CALL_USED_REGS_OPT (leafy-gpr, zero_regs_flags::LEAFY_GPR),
2189 ZERO_CALL_USED_REGS_OPT (leafy-arg, zero_regs_flags::LEAFY_ARG),
2190 ZERO_CALL_USED_REGS_OPT (leafy, zero_regs_flags::LEAFY),
2191 #undef ZERO_CALL_USED_REGS_OPT
2192 {NULL, 0U}
2193 };
2194
2195 /* A struct for describing a run of chars within a string. */
2196
2197 class string_fragment
2198 {
2199 public:
2200 string_fragment (const char *start, size_t len)
2201 : m_start (start), m_len (len) {}
2202
2203 const char *m_start;
2204 size_t m_len;
2205 };
2206
2207 /* Specialization of edit_distance_traits for string_fragment,
2208 for use by get_closest_sanitizer_option. */
2209
2210 template <>
2211 struct edit_distance_traits<const string_fragment &>
2212 {
2213 static size_t get_length (const string_fragment &fragment)
2214 {
2215 return fragment.m_len;
2216 }
2217
2218 static const char *get_string (const string_fragment &fragment)
2219 {
2220 return fragment.m_start;
2221 }
2222 };
2223
2224 /* Given ARG, an unrecognized sanitizer option, return the best
2225 matching sanitizer option, or NULL if there isn't one.
2226 OPTS is array of candidate sanitizer options.
2227 CODE is OPT_fsanitize_, OPT_fsanitize_recover_ or OPT_fsanitize_trap_.
2228 VALUE is non-zero for the regular form of the option, zero
2229 for the "no-" form (e.g. "-fno-sanitize-recover="). */
2230
2231 static const char *
2232 get_closest_sanitizer_option (const string_fragment &arg,
2233 const struct sanitizer_opts_s *opts,
2234 enum opt_code code, int value)
2235 {
2236 best_match <const string_fragment &, const char*> bm (arg);
2237 for (int i = 0; opts[i].name != NULL; ++i)
2238 {
2239 /* -fsanitize=all is not valid, so don't offer it. */
2240 if (code == OPT_fsanitize_
2241 && opts[i].flag == ~0U
2242 && value)
2243 continue;
2244
2245 /* For -fsanitize-recover= (and not -fno-sanitize-recover=),
2246 don't offer the non-recoverable options. */
2247 if (code == OPT_fsanitize_recover_
2248 && !opts[i].can_recover
2249 && value)
2250 continue;
2251
2252 /* For -fsanitize-trap= (and not -fno-sanitize-trap=),
2253 don't offer the non-trapping options. */
2254 if (code == OPT_fsanitize_trap_
2255 && !opts[i].can_trap
2256 && value)
2257 continue;
2258
2259 bm.consider (opts[i].name);
2260 }
2261 return bm.get_best_meaningful_candidate ();
2262 }
2263
2264 /* Parse comma separated sanitizer suboptions from P for option SCODE,
2265 adjust previous FLAGS and return new ones. If COMPLAIN is false,
2266 don't issue diagnostics. */
2267
2268 unsigned int
2269 parse_sanitizer_options (const char *p, location_t loc, int scode,
2270 unsigned int flags, int value, bool complain)
2271 {
2272 enum opt_code code = (enum opt_code) scode;
2273
2274 while (*p != 0)
2275 {
2276 size_t len, i;
2277 bool found = false;
2278 const char *comma = strchr (p, ',');
2279
2280 if (comma == NULL)
2281 len = strlen (p);
2282 else
2283 len = comma - p;
2284 if (len == 0)
2285 {
2286 p = comma + 1;
2287 continue;
2288 }
2289
2290 /* Check to see if the string matches an option class name. */
2291 for (i = 0; sanitizer_opts[i].name != NULL; ++i)
2292 if (len == sanitizer_opts[i].len
2293 && memcmp (p, sanitizer_opts[i].name, len) == 0)
2294 {
2295 /* Handle both -fsanitize and -fno-sanitize cases. */
2296 if (value && sanitizer_opts[i].flag == ~0U)
2297 {
2298 if (code == OPT_fsanitize_)
2299 {
2300 if (complain)
2301 error_at (loc, "%<-fsanitize=all%> option is not valid");
2302 }
2303 else if (code == OPT_fsanitize_recover_)
2304 flags |= ~(SANITIZE_THREAD | SANITIZE_LEAK
2305 | SANITIZE_UNREACHABLE | SANITIZE_RETURN
2306 | SANITIZE_SHADOW_CALL_STACK);
2307 else /* if (code == OPT_fsanitize_trap_) */
2308 flags |= (SANITIZE_UNDEFINED
2309 | SANITIZE_UNDEFINED_NONDEFAULT);
2310 }
2311 else if (value)
2312 {
2313 /* Do not enable -fsanitize-recover=unreachable and
2314 -fsanitize-recover=return if -fsanitize-recover=undefined
2315 is selected. */
2316 if (code == OPT_fsanitize_recover_
2317 && sanitizer_opts[i].flag == SANITIZE_UNDEFINED)
2318 flags |= (SANITIZE_UNDEFINED
2319 & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN));
2320 else if (code == OPT_fsanitize_trap_
2321 && sanitizer_opts[i].flag == SANITIZE_VPTR)
2322 error_at (loc, "%<-fsanitize-trap=%s%> is not supported",
2323 sanitizer_opts[i].name);
2324 else
2325 flags |= sanitizer_opts[i].flag;
2326 }
2327 else
2328 {
2329 flags &= ~sanitizer_opts[i].flag;
2330 /* Don't always clear SANITIZE_ADDRESS if it was previously
2331 set: -fsanitize=address -fno-sanitize=kernel-address should
2332 leave SANITIZE_ADDRESS set. */
2333 if (flags & (SANITIZE_KERNEL_ADDRESS | SANITIZE_USER_ADDRESS))
2334 flags |= SANITIZE_ADDRESS;
2335 }
2336 found = true;
2337 break;
2338 }
2339
2340 if (! found && complain)
2341 {
2342 const char *hint
2343 = get_closest_sanitizer_option (string_fragment (p, len),
2344 sanitizer_opts, code, value);
2345
2346 const char *suffix;
2347 if (code == OPT_fsanitize_recover_)
2348 suffix = "-recover";
2349 else if (code == OPT_fsanitize_trap_)
2350 suffix = "-trap";
2351 else
2352 suffix = "";
2353
2354 if (hint)
2355 error_at (loc,
2356 "unrecognized argument to %<-f%ssanitize%s=%> "
2357 "option: %q.*s; did you mean %qs?",
2358 value ? "" : "no-",
2359 suffix, (int) len, p, hint);
2360 else
2361 error_at (loc,
2362 "unrecognized argument to %<-f%ssanitize%s=%> option: "
2363 "%q.*s", value ? "" : "no-",
2364 suffix, (int) len, p);
2365 }
2366
2367 if (comma == NULL)
2368 break;
2369 p = comma + 1;
2370 }
2371 return flags;
2372 }
2373
2374 /* Parse string values of no_sanitize attribute passed in VALUE.
2375 Values are separated with comma. */
2376
2377 unsigned int
2378 parse_no_sanitize_attribute (char *value)
2379 {
2380 unsigned int flags = 0;
2381 unsigned int i;
2382 char *q = strtok (value, ",");
2383
2384 while (q != NULL)
2385 {
2386 for (i = 0; sanitizer_opts[i].name != NULL; ++i)
2387 if (strcmp (sanitizer_opts[i].name, q) == 0)
2388 {
2389 flags |= sanitizer_opts[i].flag;
2390 if (sanitizer_opts[i].flag == SANITIZE_UNDEFINED)
2391 flags |= SANITIZE_UNDEFINED_NONDEFAULT;
2392 break;
2393 }
2394
2395 if (sanitizer_opts[i].name == NULL)
2396 warning (OPT_Wattributes,
2397 "%qs attribute directive ignored", q);
2398
2399 q = strtok (NULL, ",");
2400 }
2401
2402 return flags;
2403 }
2404
2405 /* Parse -fzero-call-used-regs suboptions from ARG, return the FLAGS. */
2406
2407 unsigned int
2408 parse_zero_call_used_regs_options (const char *arg)
2409 {
2410 unsigned int flags = 0;
2411
2412 /* Check to see if the string matches a sub-option name. */
2413 for (unsigned int i = 0; zero_call_used_regs_opts[i].name != NULL; ++i)
2414 if (strcmp (arg, zero_call_used_regs_opts[i].name) == 0)
2415 {
2416 flags = zero_call_used_regs_opts[i].flag;
2417 break;
2418 }
2419
2420 if (!flags)
2421 error ("unrecognized argument to %<-fzero-call-used-regs=%>: %qs", arg);
2422
2423 return flags;
2424 }
2425
2426 /* Parse -falign-NAME format for a FLAG value. Return individual
2427 parsed integer values into RESULT_VALUES array. If REPORT_ERROR is
2428 set, print error message at LOC location. */
2429
2430 bool
2431 parse_and_check_align_values (const char *flag,
2432 const char *name,
2433 auto_vec<unsigned> &result_values,
2434 bool report_error,
2435 location_t loc)
2436 {
2437 char *str = xstrdup (flag);
2438 for (char *p = strtok (str, ":"); p; p = strtok (NULL, ":"))
2439 {
2440 char *end;
2441 int v = strtol (p, &end, 10);
2442 if (*end != '\0' || v < 0)
2443 {
2444 if (report_error)
2445 error_at (loc, "invalid arguments for %<-falign-%s%> option: %qs",
2446 name, flag);
2447
2448 return false;
2449 }
2450
2451 result_values.safe_push ((unsigned)v);
2452 }
2453
2454 free (str);
2455
2456 /* Check that we have a correct number of values. */
2457 if (result_values.is_empty () || result_values.length () > 4)
2458 {
2459 if (report_error)
2460 error_at (loc, "invalid number of arguments for %<-falign-%s%> "
2461 "option: %qs", name, flag);
2462 return false;
2463 }
2464
2465 for (unsigned i = 0; i < result_values.length (); i++)
2466 if (result_values[i] > MAX_CODE_ALIGN_VALUE)
2467 {
2468 if (report_error)
2469 error_at (loc, "%<-falign-%s%> is not between 0 and %d",
2470 name, MAX_CODE_ALIGN_VALUE);
2471 return false;
2472 }
2473
2474 return true;
2475 }
2476
2477 /* Check that alignment value FLAG for -falign-NAME is valid at a given
2478 location LOC. OPT_STR points to the stored -falign-NAME=argument and
2479 OPT_FLAG points to the associated -falign-NAME on/off flag. */
2480
2481 static void
2482 check_alignment_argument (location_t loc, const char *flag, const char *name,
2483 int *opt_flag, const char **opt_str)
2484 {
2485 auto_vec<unsigned> align_result;
2486 parse_and_check_align_values (flag, name, align_result, true, loc);
2487
2488 if (align_result.length() >= 1 && align_result[0] == 0)
2489 {
2490 *opt_flag = 1;
2491 *opt_str = NULL;
2492 }
2493 }
2494
2495 /* Parse argument of -fpatchable-function-entry option ARG and store
2496 corresponding values to PATCH_AREA_SIZE and PATCH_AREA_START.
2497 If REPORT_ERROR is set to true, generate error for a problematic
2498 option arguments. */
2499
2500 void
2501 parse_and_check_patch_area (const char *arg, bool report_error,
2502 HOST_WIDE_INT *patch_area_size,
2503 HOST_WIDE_INT *patch_area_start)
2504 {
2505 *patch_area_size = 0;
2506 *patch_area_start = 0;
2507
2508 if (arg == NULL)
2509 return;
2510
2511 char *patch_area_arg = xstrdup (arg);
2512 char *comma = strchr (patch_area_arg, ',');
2513 if (comma)
2514 {
2515 *comma = '\0';
2516 *patch_area_size = integral_argument (patch_area_arg);
2517 *patch_area_start = integral_argument (comma + 1);
2518 }
2519 else
2520 *patch_area_size = integral_argument (patch_area_arg);
2521
2522 if (*patch_area_size < 0
2523 || *patch_area_size > USHRT_MAX
2524 || *patch_area_start < 0
2525 || *patch_area_start > USHRT_MAX
2526 || *patch_area_size < *patch_area_start)
2527 if (report_error)
2528 error ("invalid arguments for %<-fpatchable-function-entry%>");
2529
2530 free (patch_area_arg);
2531 }
2532
2533 /* Print options enabled by -fhardened. Keep this in sync with the manual! */
2534
2535 static void
2536 print_help_hardened ()
2537 {
2538 printf ("%s\n", "The following options are enabled by -fhardened:");
2539 /* Unfortunately, I can't seem to use targetm.fortify_source_default_level
2540 here. */
2541 printf (" %s\n", "-D_FORTIFY_SOURCE=3 (or =2 for glibc < 2.35)");
2542 printf (" %s\n", "-D_GLIBCXX_ASSERTIONS");
2543 printf (" %s\n", "-ftrivial-auto-var-init=zero");
2544 #ifdef HAVE_LD_PIE
2545 printf (" %s %s\n", "-fPIE", "-pie");
2546 #endif
2547 if (HAVE_LD_NOW_SUPPORT)
2548 printf (" %s\n", "-Wl,-z,now");
2549 if (HAVE_LD_RELRO_SUPPORT)
2550 printf (" %s\n", "-Wl,-z,relro");
2551 printf (" %s\n", "-fstack-protector-strong");
2552 printf (" %s\n", "-fstack-clash-protection");
2553 printf (" %s\n", "-fcf-protection=full");
2554 putchar ('\n');
2555 }
2556
2557 /* Print help when OPT__help_ is set. */
2558
2559 void
2560 print_help (struct gcc_options *opts, unsigned int lang_mask,
2561 const char *help_option_argument)
2562 {
2563 const char *a = help_option_argument;
2564 unsigned int include_flags = 0;
2565 /* Note - by default we include undocumented options when listing
2566 specific classes. If you only want to see documented options
2567 then add ",^undocumented" to the --help= option. E.g.:
2568
2569 --help=target,^undocumented */
2570 unsigned int exclude_flags = 0;
2571
2572 if (lang_mask == CL_DRIVER)
2573 return;
2574
2575 /* Walk along the argument string, parsing each word in turn.
2576 The format is:
2577 arg = [^]{word}[,{arg}]
2578 word = {optimizers|target|warnings|undocumented|
2579 params|common|<language>} */
2580 while (*a != 0)
2581 {
2582 static const struct
2583 {
2584 const char *string;
2585 unsigned int flag;
2586 }
2587 specifics[] =
2588 {
2589 { "optimizers", CL_OPTIMIZATION },
2590 { "target", CL_TARGET },
2591 { "warnings", CL_WARNING },
2592 { "undocumented", CL_UNDOCUMENTED },
2593 { "params", CL_PARAMS },
2594 { "joined", CL_JOINED },
2595 { "separate", CL_SEPARATE },
2596 { "common", CL_COMMON },
2597 { NULL, 0 }
2598 };
2599 unsigned int *pflags;
2600 const char *comma;
2601 unsigned int lang_flag, specific_flag;
2602 unsigned int len;
2603 unsigned int i;
2604
2605 if (*a == '^')
2606 {
2607 ++a;
2608 if (*a == '\0')
2609 {
2610 error ("missing argument to %qs", "--help=^");
2611 break;
2612 }
2613 pflags = &exclude_flags;
2614 }
2615 else
2616 pflags = &include_flags;
2617
2618 comma = strchr (a, ',');
2619 if (comma == NULL)
2620 len = strlen (a);
2621 else
2622 len = comma - a;
2623 if (len == 0)
2624 {
2625 a = comma + 1;
2626 continue;
2627 }
2628
2629 /* Check to see if the string matches an option class name. */
2630 for (i = 0, specific_flag = 0; specifics[i].string != NULL; i++)
2631 if (strncasecmp (a, specifics[i].string, len) == 0)
2632 {
2633 specific_flag = specifics[i].flag;
2634 break;
2635 }
2636
2637 /* Check to see if the string matches a language name.
2638 Note - we rely upon the alpha-sorted nature of the entries in
2639 the lang_names array, specifically that shorter names appear
2640 before their longer variants. (i.e. C before C++). That way
2641 when we are attempting to match --help=c for example we will
2642 match with C first and not C++. */
2643 for (i = 0, lang_flag = 0; i < cl_lang_count; i++)
2644 if (strncasecmp (a, lang_names[i], len) == 0)
2645 {
2646 lang_flag = 1U << i;
2647 break;
2648 }
2649
2650 if (specific_flag != 0)
2651 {
2652 if (lang_flag == 0)
2653 *pflags |= specific_flag;
2654 else
2655 {
2656 /* The option's argument matches both the start of a
2657 language name and the start of an option class name.
2658 We have a special case for when the user has
2659 specified "--help=c", but otherwise we have to issue
2660 a warning. */
2661 if (strncasecmp (a, "c", len) == 0)
2662 *pflags |= lang_flag;
2663 else
2664 warning (0,
2665 "%<--help%> argument %q.*s is ambiguous, "
2666 "please be more specific",
2667 len, a);
2668 }
2669 }
2670 else if (lang_flag != 0)
2671 *pflags |= lang_flag;
2672 else if (strncasecmp (a, "hardened", len) == 0)
2673 print_help_hardened ();
2674 else
2675 warning (0,
2676 "unrecognized argument to %<--help=%> option: %q.*s",
2677 len, a);
2678
2679 if (comma == NULL)
2680 break;
2681 a = comma + 1;
2682 }
2683
2684 /* We started using PerFunction/Optimization for parameters and
2685 a warning. We should exclude these from optimization options. */
2686 if (include_flags & CL_OPTIMIZATION)
2687 exclude_flags |= CL_WARNING;
2688 if (!(include_flags & CL_PARAMS))
2689 exclude_flags |= CL_PARAMS;
2690
2691 if (include_flags)
2692 print_specific_help (include_flags, exclude_flags, 0, opts,
2693 lang_mask);
2694 }
2695
2696 /* Handle target- and language-independent options. Return zero to
2697 generate an "unknown option" message. Only options that need
2698 extra handling need to be listed here; if you simply want
2699 DECODED->value assigned to a variable, it happens automatically. */
2700
2701 bool
2702 common_handle_option (struct gcc_options *opts,
2703 struct gcc_options *opts_set,
2704 const struct cl_decoded_option *decoded,
2705 unsigned int lang_mask, int kind ATTRIBUTE_UNUSED,
2706 location_t loc,
2707 const struct cl_option_handlers *handlers,
2708 diagnostic_context *dc,
2709 void (*target_option_override_hook) (void))
2710 {
2711 size_t scode = decoded->opt_index;
2712 const char *arg = decoded->arg;
2713 HOST_WIDE_INT value = decoded->value;
2714 enum opt_code code = (enum opt_code) scode;
2715
2716 gcc_assert (decoded->canonical_option_num_elements <= 2);
2717
2718 switch (code)
2719 {
2720 case OPT__help:
2721 {
2722 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
2723 unsigned int undoc_mask;
2724 unsigned int i;
2725
2726 if (lang_mask == CL_DRIVER)
2727 break;
2728
2729 undoc_mask = ((opts->x_verbose_flag | opts->x_extra_warnings)
2730 ? 0
2731 : CL_UNDOCUMENTED);
2732 target_option_override_hook ();
2733 /* First display any single language specific options. */
2734 for (i = 0; i < cl_lang_count; i++)
2735 print_specific_help
2736 (1U << i, (all_langs_mask & (~ (1U << i))) | undoc_mask, 0, opts,
2737 lang_mask);
2738 /* Next display any multi language specific options. */
2739 print_specific_help (0, undoc_mask, all_langs_mask, opts, lang_mask);
2740 /* Then display any remaining, non-language options. */
2741 for (i = CL_MIN_OPTION_CLASS; i <= CL_MAX_OPTION_CLASS; i <<= 1)
2742 if (i != CL_DRIVER)
2743 print_specific_help (i, undoc_mask, 0, opts, lang_mask);
2744 opts->x_exit_after_options = true;
2745 break;
2746 }
2747
2748 case OPT__target_help:
2749 if (lang_mask == CL_DRIVER)
2750 break;
2751
2752 target_option_override_hook ();
2753 print_specific_help (CL_TARGET, 0, 0, opts, lang_mask);
2754 opts->x_exit_after_options = true;
2755 break;
2756
2757 case OPT__help_:
2758 {
2759 help_option_arguments.safe_push (arg);
2760 opts->x_exit_after_options = true;
2761 break;
2762 }
2763
2764 case OPT__version:
2765 if (lang_mask == CL_DRIVER)
2766 break;
2767
2768 opts->x_exit_after_options = true;
2769 break;
2770
2771 case OPT__completion_:
2772 break;
2773
2774 case OPT_fsanitize_:
2775 opts_set->x_flag_sanitize = true;
2776 opts->x_flag_sanitize
2777 = parse_sanitizer_options (arg, loc, code,
2778 opts->x_flag_sanitize, value, true);
2779
2780 /* Kernel ASan implies normal ASan but does not yet support
2781 all features. */
2782 if (opts->x_flag_sanitize & SANITIZE_KERNEL_ADDRESS)
2783 {
2784 SET_OPTION_IF_UNSET (opts, opts_set,
2785 param_asan_instrumentation_with_call_threshold,
2786 0);
2787 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_globals, 0);
2788 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_stack, 0);
2789 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_protect_allocas, 0);
2790 SET_OPTION_IF_UNSET (opts, opts_set, param_asan_use_after_return, 0);
2791 }
2792 if (opts->x_flag_sanitize & SANITIZE_KERNEL_HWADDRESS)
2793 {
2794 SET_OPTION_IF_UNSET (opts, opts_set,
2795 param_hwasan_instrument_stack, 0);
2796 SET_OPTION_IF_UNSET (opts, opts_set,
2797 param_hwasan_random_frame_tag, 0);
2798 SET_OPTION_IF_UNSET (opts, opts_set,
2799 param_hwasan_instrument_allocas, 0);
2800 }
2801 break;
2802
2803 case OPT_fsanitize_recover_:
2804 opts->x_flag_sanitize_recover
2805 = parse_sanitizer_options (arg, loc, code,
2806 opts->x_flag_sanitize_recover, value, true);
2807 break;
2808
2809 case OPT_fsanitize_trap_:
2810 opts->x_flag_sanitize_trap
2811 = parse_sanitizer_options (arg, loc, code,
2812 opts->x_flag_sanitize_trap, value, true);
2813 break;
2814
2815 case OPT_fasan_shadow_offset_:
2816 /* Deferred. */
2817 break;
2818
2819 case OPT_fsanitize_address_use_after_scope:
2820 opts->x_flag_sanitize_address_use_after_scope = value;
2821 break;
2822
2823 case OPT_fsanitize_recover:
2824 if (value)
2825 opts->x_flag_sanitize_recover
2826 |= (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT)
2827 & ~(SANITIZE_UNREACHABLE | SANITIZE_RETURN);
2828 else
2829 opts->x_flag_sanitize_recover
2830 &= ~(SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2831 break;
2832
2833 case OPT_fsanitize_trap:
2834 if (value)
2835 opts->x_flag_sanitize_trap
2836 |= (SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2837 else
2838 opts->x_flag_sanitize_trap
2839 &= ~(SANITIZE_UNDEFINED | SANITIZE_UNDEFINED_NONDEFAULT);
2840 break;
2841
2842 case OPT_O:
2843 case OPT_Os:
2844 case OPT_Ofast:
2845 case OPT_Og:
2846 case OPT_Oz:
2847 /* Currently handled in a prescan. */
2848 break;
2849
2850 case OPT_Wattributes_:
2851 if (lang_mask == CL_DRIVER)
2852 break;
2853
2854 if (value)
2855 {
2856 error_at (loc, "arguments ignored for %<-Wattributes=%>; use "
2857 "%<-Wno-attributes=%> instead");
2858 break;
2859 }
2860 else if (arg[strlen (arg) - 1] == ',')
2861 {
2862 error_at (loc, "trailing %<,%> in arguments for "
2863 "%<-Wno-attributes=%>");
2864 break;
2865 }
2866
2867 add_comma_separated_to_vector (&opts->x_flag_ignored_attributes, arg);
2868 break;
2869
2870 case OPT_Werror:
2871 dc->set_warning_as_error_requested (value);
2872 break;
2873
2874 case OPT_Werror_:
2875 if (lang_mask == CL_DRIVER)
2876 break;
2877
2878 enable_warning_as_error (arg, value, lang_mask, handlers,
2879 opts, opts_set, loc, dc);
2880 break;
2881
2882 case OPT_Wfatal_errors:
2883 dc->m_fatal_errors = value;
2884 break;
2885
2886 case OPT_Wstack_usage_:
2887 opts->x_flag_stack_usage_info = value != -1;
2888 break;
2889
2890 case OPT_Wstrict_aliasing:
2891 set_Wstrict_aliasing (opts, value);
2892 break;
2893
2894 case OPT_Wstrict_overflow:
2895 opts->x_warn_strict_overflow = (value
2896 ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
2897 : 0);
2898 break;
2899
2900 case OPT_Wsystem_headers:
2901 dc->m_warn_system_headers = value;
2902 break;
2903
2904 case OPT_aux_info:
2905 opts->x_flag_gen_aux_info = 1;
2906 break;
2907
2908 case OPT_d:
2909 decode_d_option (arg, opts, loc, dc);
2910 break;
2911
2912 case OPT_fcall_used_:
2913 case OPT_fcall_saved_:
2914 /* Deferred. */
2915 break;
2916
2917 case OPT_fdbg_cnt_:
2918 /* Deferred. */
2919 break;
2920
2921 case OPT_fdebug_prefix_map_:
2922 case OPT_ffile_prefix_map_:
2923 case OPT_fprofile_prefix_map_:
2924 /* Deferred. */
2925 break;
2926
2927 case OPT_fcanon_prefix_map:
2928 flag_canon_prefix_map = value;
2929 break;
2930
2931 case OPT_fcallgraph_info:
2932 opts->x_flag_callgraph_info = CALLGRAPH_INFO_NAKED;
2933 break;
2934
2935 case OPT_fcallgraph_info_:
2936 {
2937 char *my_arg, *p;
2938 my_arg = xstrdup (arg);
2939 p = strtok (my_arg, ",");
2940 while (p)
2941 {
2942 if (strcmp (p, "su") == 0)
2943 {
2944 opts->x_flag_callgraph_info |= CALLGRAPH_INFO_STACK_USAGE;
2945 opts->x_flag_stack_usage_info = true;
2946 }
2947 else if (strcmp (p, "da") == 0)
2948 opts->x_flag_callgraph_info |= CALLGRAPH_INFO_DYNAMIC_ALLOC;
2949 else
2950 return 0;
2951 p = strtok (NULL, ",");
2952 }
2953 free (my_arg);
2954 }
2955 break;
2956
2957 case OPT_fdiagnostics_show_location_:
2958 dc->set_prefixing_rule ((diagnostic_prefixing_rule_t) value);
2959 break;
2960
2961 case OPT_fdiagnostics_show_caret:
2962 dc->m_source_printing.enabled = value;
2963 break;
2964
2965 case OPT_fdiagnostics_show_event_links:
2966 dc->m_source_printing.show_event_links_p = value;
2967 break;
2968
2969 case OPT_fdiagnostics_show_labels:
2970 dc->m_source_printing.show_labels_p = value;
2971 break;
2972
2973 case OPT_fdiagnostics_show_line_numbers:
2974 dc->m_source_printing.show_line_numbers_p = value;
2975 break;
2976
2977 case OPT_fdiagnostics_color_:
2978 diagnostic_color_init (dc, value);
2979 break;
2980
2981 case OPT_fdiagnostics_urls_:
2982 diagnostic_urls_init (dc, value);
2983 break;
2984
2985 case OPT_fdiagnostics_format_:
2986 {
2987 const char *basename = (opts->x_dump_base_name ? opts->x_dump_base_name
2988 : opts->x_main_input_basename);
2989 gcc_assert (dc);
2990 diagnostic_output_format_init (*dc,
2991 opts->x_main_input_filename, basename,
2992 (enum diagnostics_output_format)value,
2993 opts->x_flag_diagnostics_json_formatting);
2994 break;
2995 }
2996
2997 case OPT_fdiagnostics_add_output_:
2998 handle_OPT_fdiagnostics_add_output_ (*opts, *dc, arg, loc);
2999 break;
3000
3001 case OPT_fdiagnostics_set_output_:
3002 handle_OPT_fdiagnostics_set_output_ (*opts, *dc, arg, loc);
3003 break;
3004
3005 case OPT_fdiagnostics_text_art_charset_:
3006 dc->set_text_art_charset ((enum diagnostic_text_art_charset)value);
3007 break;
3008
3009 case OPT_fdiagnostics_parseable_fixits:
3010 dc->set_extra_output_kind (value
3011 ? EXTRA_DIAGNOSTIC_OUTPUT_fixits_v1
3012 : EXTRA_DIAGNOSTIC_OUTPUT_none);
3013 break;
3014
3015 case OPT_fdiagnostics_column_unit_:
3016 dc->m_column_unit = (enum diagnostics_column_unit)value;
3017 break;
3018
3019 case OPT_fdiagnostics_column_origin_:
3020 dc->m_column_origin = value;
3021 break;
3022
3023 case OPT_fdiagnostics_escape_format_:
3024 dc->set_escape_format ((enum diagnostics_escape_format)value);
3025 break;
3026
3027 case OPT_fdiagnostics_show_highlight_colors:
3028 dc->set_show_highlight_colors (value);
3029 break;
3030
3031 case OPT_fdiagnostics_show_cwe:
3032 dc->set_show_cwe (value);
3033 break;
3034
3035 case OPT_fdiagnostics_show_rules:
3036 dc->set_show_rules (value);
3037 break;
3038
3039 case OPT_fdiagnostics_path_format_:
3040 dc->set_path_format ((enum diagnostic_path_format)value);
3041 break;
3042
3043 case OPT_fdiagnostics_show_path_depths:
3044 dc->set_show_path_depths (value);
3045 break;
3046
3047 case OPT_fdiagnostics_show_option:
3048 dc->set_show_option_requested (value);
3049 break;
3050
3051 case OPT_fdiagnostics_minimum_margin_width_:
3052 dc->m_source_printing.min_margin_width = value;
3053 break;
3054
3055 case OPT_fdump_:
3056 /* Deferred. */
3057 break;
3058
3059 case OPT_ffast_math:
3060 set_fast_math_flags (opts, value);
3061 break;
3062
3063 case OPT_funsafe_math_optimizations:
3064 set_unsafe_math_optimizations_flags (opts, value);
3065 break;
3066
3067 case OPT_ffixed_:
3068 /* Deferred. */
3069 break;
3070
3071 case OPT_finline_limit_:
3072 SET_OPTION_IF_UNSET (opts, opts_set, param_max_inline_insns_single,
3073 value / 2);
3074 SET_OPTION_IF_UNSET (opts, opts_set, param_max_inline_insns_auto,
3075 value / 2);
3076 break;
3077
3078 case OPT_finstrument_functions_exclude_function_list_:
3079 add_comma_separated_to_vector
3080 (&opts->x_flag_instrument_functions_exclude_functions, arg);
3081 break;
3082
3083 case OPT_finstrument_functions_exclude_file_list_:
3084 add_comma_separated_to_vector
3085 (&opts->x_flag_instrument_functions_exclude_files, arg);
3086 break;
3087
3088 case OPT_fmessage_length_:
3089 pp_set_line_maximum_length (dc->get_reference_printer (), value);
3090 diagnostic_set_caret_max_width (dc, value);
3091 break;
3092
3093 case OPT_fopt_info:
3094 case OPT_fopt_info_:
3095 /* Deferred. */
3096 break;
3097
3098 case OPT_foffload_options_:
3099 /* Deferred. */
3100 break;
3101
3102 case OPT_foffload_abi_:
3103 case OPT_foffload_abi_host_opts_:
3104 #ifdef ACCEL_COMPILER
3105 /* Handled in the 'mkoffload's. */
3106 #else
3107 error_at (loc,
3108 "%qs option can be specified only for offload compiler",
3109 (code == OPT_foffload_abi_) ? "-foffload-abi"
3110 : "-foffload-abi-host-opts");
3111 #endif
3112 break;
3113
3114 case OPT_fpack_struct_:
3115 if (value <= 0 || (value & (value - 1)) || value > 16)
3116 error_at (loc,
3117 "structure alignment must be a small power of two, not %wu",
3118 value);
3119 else
3120 opts->x_initial_max_fld_align = value;
3121 break;
3122
3123 case OPT_fplugin_:
3124 case OPT_fplugin_arg_:
3125 /* Deferred. */
3126 break;
3127
3128 case OPT_fprofile_use_:
3129 opts->x_profile_data_prefix = xstrdup (arg);
3130 opts->x_flag_profile_use = true;
3131 value = true;
3132 /* No break here - do -fprofile-use processing. */
3133 /* FALLTHRU */
3134 case OPT_fprofile_use:
3135 enable_fdo_optimizations (opts, opts_set, value);
3136 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_reorder_functions,
3137 value);
3138 /* Indirect call profiling should do all useful transformations
3139 speculative devirtualization does. */
3140 if (opts->x_flag_value_profile_transformations)
3141 SET_OPTION_IF_UNSET (opts, opts_set, flag_devirtualize_speculatively,
3142 false);
3143 break;
3144
3145 case OPT_fauto_profile_:
3146 opts->x_auto_profile_file = xstrdup (arg);
3147 opts->x_flag_auto_profile = true;
3148 value = true;
3149 /* No break here - do -fauto-profile processing. */
3150 /* FALLTHRU */
3151 case OPT_fauto_profile:
3152 enable_fdo_optimizations (opts, opts_set, value);
3153 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_correction, value);
3154 break;
3155
3156 case OPT_fprofile_generate_:
3157 opts->x_profile_data_prefix = xstrdup (arg);
3158 value = true;
3159 /* No break here - do -fprofile-generate processing. */
3160 /* FALLTHRU */
3161 case OPT_fprofile_generate:
3162 SET_OPTION_IF_UNSET (opts, opts_set, profile_arc_flag, value);
3163 SET_OPTION_IF_UNSET (opts, opts_set, flag_profile_values, value);
3164 SET_OPTION_IF_UNSET (opts, opts_set, flag_inline_functions, value);
3165 SET_OPTION_IF_UNSET (opts, opts_set, flag_ipa_bit_cp, value);
3166 break;
3167
3168 case OPT_fprofile_info_section:
3169 opts->x_profile_info_section = ".gcov_info";
3170 break;
3171
3172 case OPT_fpatchable_function_entry_:
3173 {
3174 HOST_WIDE_INT patch_area_size, patch_area_start;
3175 parse_and_check_patch_area (arg, true, &patch_area_size,
3176 &patch_area_start);
3177 }
3178 break;
3179
3180 case OPT_ftree_vectorize:
3181 /* Automatically sets -ftree-loop-vectorize and
3182 -ftree-slp-vectorize. Nothing more to do here. */
3183 break;
3184 case OPT_fzero_call_used_regs_:
3185 opts->x_flag_zero_call_used_regs
3186 = parse_zero_call_used_regs_options (arg);
3187 break;
3188
3189 case OPT_fshow_column:
3190 dc->m_show_column = value;
3191 break;
3192
3193 case OPT_frandom_seed:
3194 /* The real switch is -fno-random-seed. */
3195 if (value)
3196 return false;
3197 /* Deferred. */
3198 break;
3199
3200 case OPT_frandom_seed_:
3201 /* Deferred. */
3202 break;
3203
3204 case OPT_fsched_verbose_:
3205 #ifdef INSN_SCHEDULING
3206 /* Handled with Var in common.opt. */
3207 break;
3208 #else
3209 return false;
3210 #endif
3211
3212 case OPT_fsched_stalled_insns_:
3213 opts->x_flag_sched_stalled_insns = value;
3214 if (opts->x_flag_sched_stalled_insns == 0)
3215 opts->x_flag_sched_stalled_insns = -1;
3216 break;
3217
3218 case OPT_fsched_stalled_insns_dep_:
3219 opts->x_flag_sched_stalled_insns_dep = value;
3220 break;
3221
3222 case OPT_fstack_check_:
3223 if (!strcmp (arg, "no"))
3224 opts->x_flag_stack_check = NO_STACK_CHECK;
3225 else if (!strcmp (arg, "generic"))
3226 /* This is the old stack checking method. */
3227 opts->x_flag_stack_check = STACK_CHECK_BUILTIN
3228 ? FULL_BUILTIN_STACK_CHECK
3229 : GENERIC_STACK_CHECK;
3230 else if (!strcmp (arg, "specific"))
3231 /* This is the new stack checking method. */
3232 opts->x_flag_stack_check = STACK_CHECK_BUILTIN
3233 ? FULL_BUILTIN_STACK_CHECK
3234 : STACK_CHECK_STATIC_BUILTIN
3235 ? STATIC_BUILTIN_STACK_CHECK
3236 : GENERIC_STACK_CHECK;
3237 else
3238 warning_at (loc, 0, "unknown stack check parameter %qs", arg);
3239 break;
3240
3241 case OPT_fstack_limit:
3242 /* The real switch is -fno-stack-limit. */
3243 if (value)
3244 return false;
3245 /* Deferred. */
3246 break;
3247
3248 case OPT_fstack_limit_register_:
3249 case OPT_fstack_limit_symbol_:
3250 /* Deferred. */
3251 break;
3252
3253 case OPT_fstack_usage:
3254 opts->x_flag_stack_usage = value;
3255 opts->x_flag_stack_usage_info = value != 0;
3256 break;
3257
3258 case OPT_g:
3259 set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg, opts, opts_set,
3260 loc);
3261 break;
3262
3263 case OPT_gcodeview:
3264 set_debug_level (CODEVIEW_DEBUG, false, arg, opts, opts_set, loc);
3265 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
3266 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3267 break;
3268
3269 case OPT_gbtf:
3270 set_debug_level (BTF_DEBUG, false, arg, opts, opts_set, loc);
3271 /* set the debug level to level 2, but if already at level 3,
3272 don't lower it. */
3273 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
3274 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3275 break;
3276
3277 case OPT_gctf:
3278 set_debug_level (CTF_DEBUG, false, arg, opts, opts_set, loc);
3279 /* CTF generation feeds off DWARF dies. For optimal CTF, switch debug
3280 info level to 2. If off or at level 1, set it to level 2, but if
3281 already at level 3, don't lower it. */
3282 if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL
3283 && opts->x_ctf_debug_info_level > CTFINFO_LEVEL_NONE)
3284 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3285 break;
3286
3287 case OPT_gdwarf:
3288 if (arg && strlen (arg) != 0)
3289 {
3290 error_at (loc, "%<-gdwarf%s%> is ambiguous; "
3291 "use %<-gdwarf-%s%> for DWARF version "
3292 "or %<-gdwarf%> %<-g%s%> for debug level", arg, arg, arg);
3293 break;
3294 }
3295 else
3296 value = opts->x_dwarf_version;
3297
3298 /* FALLTHRU */
3299 case OPT_gdwarf_:
3300 if (value < 2 || value > 5)
3301 error_at (loc, "dwarf version %wu is not supported", value);
3302 else
3303 opts->x_dwarf_version = value;
3304 set_debug_level (DWARF2_DEBUG, false, "", opts, opts_set, loc);
3305 break;
3306
3307 case OPT_ggdb:
3308 set_debug_level (NO_DEBUG, 2, arg, opts, opts_set, loc);
3309 break;
3310
3311 case OPT_gvms:
3312 set_debug_level (VMS_DEBUG, false, arg, opts, opts_set, loc);
3313 break;
3314
3315 case OPT_gz:
3316 case OPT_gz_:
3317 /* Handled completely via specs. */
3318 break;
3319
3320 case OPT_pedantic_errors:
3321 dc->m_pedantic_errors = 1;
3322 control_warning_option (OPT_Wpedantic, DK_ERROR, NULL, value,
3323 loc, lang_mask,
3324 handlers, opts, opts_set,
3325 dc);
3326 break;
3327
3328 case OPT_flto:
3329 opts->x_flag_lto = value ? "" : NULL;
3330 break;
3331
3332 case OPT_flto_:
3333 if (strcmp (arg, "none") != 0
3334 && strcmp (arg, "jobserver") != 0
3335 && strcmp (arg, "auto") != 0
3336 && atoi (arg) == 0)
3337 error_at (loc,
3338 "unrecognized argument to %<-flto=%> option: %qs", arg);
3339 break;
3340
3341 case OPT_w:
3342 dc->m_inhibit_warnings = true;
3343 break;
3344
3345 case OPT_fmax_errors_:
3346 dc->set_max_errors (value);
3347 break;
3348
3349 case OPT_fuse_ld_bfd:
3350 case OPT_fuse_ld_gold:
3351 case OPT_fuse_ld_lld:
3352 case OPT_fuse_ld_mold:
3353 case OPT_fuse_linker_plugin:
3354 /* No-op. Used by the driver and passed to us because it starts with f.*/
3355 break;
3356
3357 case OPT_fwrapv:
3358 if (value)
3359 opts->x_flag_trapv = 0;
3360 break;
3361
3362 case OPT_ftrapv:
3363 if (value)
3364 opts->x_flag_wrapv = 0;
3365 break;
3366
3367 case OPT_fstrict_overflow:
3368 opts->x_flag_wrapv = !value;
3369 opts->x_flag_wrapv_pointer = !value;
3370 if (!value)
3371 opts->x_flag_trapv = 0;
3372 break;
3373
3374 case OPT_fipa_icf:
3375 opts->x_flag_ipa_icf_functions = value;
3376 opts->x_flag_ipa_icf_variables = value;
3377 break;
3378
3379 case OPT_falign_loops_:
3380 check_alignment_argument (loc, arg, "loops",
3381 &opts->x_flag_align_loops,
3382 &opts->x_str_align_loops);
3383 break;
3384
3385 case OPT_falign_jumps_:
3386 check_alignment_argument (loc, arg, "jumps",
3387 &opts->x_flag_align_jumps,
3388 &opts->x_str_align_jumps);
3389 break;
3390
3391 case OPT_falign_labels_:
3392 check_alignment_argument (loc, arg, "labels",
3393 &opts->x_flag_align_labels,
3394 &opts->x_str_align_labels);
3395 break;
3396
3397 case OPT_falign_functions_:
3398 check_alignment_argument (loc, arg, "functions",
3399 &opts->x_flag_align_functions,
3400 &opts->x_str_align_functions);
3401 break;
3402
3403 case OPT_ftabstop_:
3404 /* It is documented that we silently ignore silly values. */
3405 if (value >= 1 && value <= 100)
3406 dc->m_tabstop = value;
3407 break;
3408
3409 case OPT_freport_bug:
3410 dc->set_report_bug (value);
3411 break;
3412
3413 case OPT_fmultiflags:
3414 gcc_checking_assert (lang_mask == CL_DRIVER);
3415 break;
3416
3417 default:
3418 /* If the flag was handled in a standard way, assume the lack of
3419 processing here is intentional. */
3420 gcc_assert (option_flag_var (scode, opts));
3421 break;
3422 }
3423
3424 common_handle_option_auto (opts, opts_set, decoded, lang_mask, kind,
3425 loc, handlers, dc);
3426 return true;
3427 }
3428
3429 /* Used to set the level of strict aliasing warnings in OPTS,
3430 when no level is specified (i.e., when -Wstrict-aliasing, and not
3431 -Wstrict-aliasing=level was given).
3432 ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified,
3433 and 0 otherwise. After calling this function, wstrict_aliasing will be
3434 set to the default value of -Wstrict_aliasing=level, currently 3. */
3435 static void
3436 set_Wstrict_aliasing (struct gcc_options *opts, int onoff)
3437 {
3438 gcc_assert (onoff == 0 || onoff == 1);
3439 if (onoff != 0)
3440 opts->x_warn_strict_aliasing = 3;
3441 else
3442 opts->x_warn_strict_aliasing = 0;
3443 }
3444
3445 /* The following routines are useful in setting all the flags that
3446 -ffast-math and -fno-fast-math imply. */
3447 static void
3448 set_fast_math_flags (struct gcc_options *opts, int set)
3449 {
3450 if (!opts->frontend_set_flag_unsafe_math_optimizations)
3451 {
3452 opts->x_flag_unsafe_math_optimizations = set;
3453 set_unsafe_math_optimizations_flags (opts, set);
3454 }
3455 if (!opts->frontend_set_flag_finite_math_only)
3456 opts->x_flag_finite_math_only = set;
3457 if (!opts->frontend_set_flag_errno_math)
3458 opts->x_flag_errno_math = !set;
3459 if (set)
3460 {
3461 if (opts->frontend_set_flag_excess_precision == EXCESS_PRECISION_DEFAULT)
3462 opts->x_flag_excess_precision
3463 = set ? EXCESS_PRECISION_FAST : EXCESS_PRECISION_DEFAULT;
3464 if (!opts->frontend_set_flag_signaling_nans)
3465 opts->x_flag_signaling_nans = 0;
3466 if (!opts->frontend_set_flag_rounding_math)
3467 opts->x_flag_rounding_math = 0;
3468 if (!opts->frontend_set_flag_cx_limited_range)
3469 opts->x_flag_cx_limited_range = 1;
3470 }
3471 }
3472
3473 /* When -funsafe-math-optimizations is set the following
3474 flags are set as well. */
3475 static void
3476 set_unsafe_math_optimizations_flags (struct gcc_options *opts, int set)
3477 {
3478 if (!opts->frontend_set_flag_trapping_math)
3479 opts->x_flag_trapping_math = !set;
3480 if (!opts->frontend_set_flag_signed_zeros)
3481 opts->x_flag_signed_zeros = !set;
3482 if (!opts->frontend_set_flag_associative_math)
3483 opts->x_flag_associative_math = set;
3484 if (!opts->frontend_set_flag_reciprocal_math)
3485 opts->x_flag_reciprocal_math = set;
3486 }
3487
3488 /* Return true iff flags in OPTS are set as if -ffast-math. */
3489 bool
3490 fast_math_flags_set_p (const struct gcc_options *opts)
3491 {
3492 return (!opts->x_flag_trapping_math
3493 && opts->x_flag_unsafe_math_optimizations
3494 && opts->x_flag_finite_math_only
3495 && !opts->x_flag_signed_zeros
3496 && !opts->x_flag_errno_math
3497 && opts->x_flag_excess_precision == EXCESS_PRECISION_FAST);
3498 }
3499
3500 /* Return true iff flags are set as if -ffast-math but using the flags stored
3501 in the struct cl_optimization structure. */
3502 bool
3503 fast_math_flags_struct_set_p (struct cl_optimization *opt)
3504 {
3505 return (!opt->x_flag_trapping_math
3506 && opt->x_flag_unsafe_math_optimizations
3507 && opt->x_flag_finite_math_only
3508 && !opt->x_flag_signed_zeros
3509 && !opt->x_flag_errno_math);
3510 }
3511
3512 /* Handle a debug output -g switch for options OPTS
3513 (OPTS_SET->x_write_symbols storing whether a debug format was passed
3514 explicitly), location LOC. EXTENDED is true or false to support
3515 extended output (2 is special and means "-ggdb" was given). */
3516 static void
3517 set_debug_level (uint32_t dinfo, int extended, const char *arg,
3518 struct gcc_options *opts, struct gcc_options *opts_set,
3519 location_t loc)
3520 {
3521 if (dinfo == NO_DEBUG)
3522 {
3523 if (opts->x_write_symbols == NO_DEBUG)
3524 {
3525 opts->x_write_symbols = PREFERRED_DEBUGGING_TYPE;
3526
3527 if (extended == 2)
3528 {
3529 #if defined DWARF2_DEBUGGING_INFO || defined DWARF2_LINENO_DEBUGGING_INFO
3530 if (opts->x_write_symbols & CTF_DEBUG)
3531 opts->x_write_symbols |= DWARF2_DEBUG;
3532 else
3533 opts->x_write_symbols = DWARF2_DEBUG;
3534 #endif
3535 }
3536
3537 if (opts->x_write_symbols == NO_DEBUG)
3538 warning_at (loc, 0, "target system does not support debug output");
3539 }
3540 else if ((opts->x_write_symbols & CTF_DEBUG)
3541 || (opts->x_write_symbols & BTF_DEBUG)
3542 || (opts->x_write_symbols & CODEVIEW_DEBUG))
3543 {
3544 opts->x_write_symbols |= DWARF2_DEBUG;
3545 opts_set->x_write_symbols |= DWARF2_DEBUG;
3546 }
3547 }
3548 else
3549 {
3550 /* Make and retain the choice if both CTF and DWARF debug info are to
3551 be generated. */
3552 if (((dinfo == DWARF2_DEBUG) || (dinfo == CTF_DEBUG))
3553 && ((opts->x_write_symbols == (DWARF2_DEBUG|CTF_DEBUG))
3554 || (opts->x_write_symbols == DWARF2_DEBUG)
3555 || (opts->x_write_symbols == CTF_DEBUG)))
3556 {
3557 opts->x_write_symbols |= dinfo;
3558 opts_set->x_write_symbols |= dinfo;
3559 }
3560 /* However, CTF and BTF are not allowed together at this time. */
3561 else if (((dinfo == DWARF2_DEBUG) || (dinfo == BTF_DEBUG))
3562 && ((opts->x_write_symbols == (DWARF2_DEBUG|BTF_DEBUG))
3563 || (opts->x_write_symbols == DWARF2_DEBUG)
3564 || (opts->x_write_symbols == BTF_DEBUG)))
3565 {
3566 opts->x_write_symbols |= dinfo;
3567 opts_set->x_write_symbols |= dinfo;
3568 }
3569 else
3570 {
3571 /* Does it conflict with an already selected debug format? */
3572 if (opts_set->x_write_symbols != NO_DEBUG
3573 && opts->x_write_symbols != NO_DEBUG
3574 && dinfo != opts->x_write_symbols)
3575 {
3576 gcc_assert (debug_set_count (dinfo) <= 1);
3577 error_at (loc, "debug format %qs conflicts with prior selection",
3578 debug_type_names[debug_set_to_format (dinfo)]);
3579 }
3580 opts->x_write_symbols = dinfo;
3581 opts_set->x_write_symbols = dinfo;
3582 }
3583 }
3584
3585 if (dinfo != BTF_DEBUG)
3586 {
3587 /* A debug flag without a level defaults to level 2.
3588 If off or at level 1, set it to level 2, but if already
3589 at level 3, don't lower it. */
3590 if (*arg == '\0')
3591 {
3592 if (dinfo == CTF_DEBUG)
3593 opts->x_ctf_debug_info_level = CTFINFO_LEVEL_NORMAL;
3594 else if (opts->x_debug_info_level < DINFO_LEVEL_NORMAL)
3595 opts->x_debug_info_level = DINFO_LEVEL_NORMAL;
3596 }
3597 else
3598 {
3599 int argval = integral_argument (arg);
3600 if (argval == -1)
3601 error_at (loc, "unrecognized debug output level %qs", arg);
3602 else if (argval > 3)
3603 error_at (loc, "debug output level %qs is too high", arg);
3604 else
3605 {
3606 if (dinfo == CTF_DEBUG)
3607 opts->x_ctf_debug_info_level
3608 = (enum ctf_debug_info_levels) argval;
3609 else
3610 opts->x_debug_info_level = (enum debug_info_levels) argval;
3611 }
3612 }
3613 }
3614 else if (*arg != '\0')
3615 error_at (loc, "unrecognized btf debug output level %qs", arg);
3616 }
3617
3618 /* Arrange to dump core on error for diagnostic context DC. (The
3619 regular error message is still printed first, except in the case of
3620 abort ().) */
3621
3622 static void
3623 setup_core_dumping (diagnostic_context *dc)
3624 {
3625 #ifdef SIGABRT
3626 signal (SIGABRT, SIG_DFL);
3627 #endif
3628 #if defined(HAVE_SETRLIMIT)
3629 {
3630 struct rlimit rlim;
3631 if (getrlimit (RLIMIT_CORE, &rlim) != 0)
3632 fatal_error (input_location, "getting core file size maximum limit: %m");
3633 rlim.rlim_cur = rlim.rlim_max;
3634 if (setrlimit (RLIMIT_CORE, &rlim) != 0)
3635 fatal_error (input_location,
3636 "setting core file size limit to maximum: %m");
3637 }
3638 #endif
3639 diagnostic_abort_on_error (dc);
3640 }
3641
3642 /* Parse a -d<ARG> command line switch for OPTS, location LOC,
3643 diagnostic context DC. */
3644
3645 static void
3646 decode_d_option (const char *arg, struct gcc_options *opts,
3647 location_t loc, diagnostic_context *dc)
3648 {
3649 int c;
3650
3651 while (*arg)
3652 switch (c = *arg++)
3653 {
3654 case 'A':
3655 opts->x_flag_debug_asm = 1;
3656 break;
3657 case 'p':
3658 opts->x_flag_print_asm_name = 1;
3659 break;
3660 case 'P':
3661 opts->x_flag_dump_rtl_in_asm = 1;
3662 opts->x_flag_print_asm_name = 1;
3663 break;
3664 case 'x':
3665 opts->x_rtl_dump_and_exit = 1;
3666 break;
3667 case 'D': /* These are handled by the preprocessor. */
3668 case 'I':
3669 case 'M':
3670 case 'N':
3671 case 'U':
3672 break;
3673 case 'H':
3674 setup_core_dumping (dc);
3675 break;
3676 case 'a':
3677 opts->x_flag_dump_all_passed = true;
3678 break;
3679
3680 default:
3681 warning_at (loc, 0, "unrecognized gcc debugging option: %c", c);
3682 break;
3683 }
3684 }
3685
3686 /* Enable (or disable if VALUE is 0) a warning option ARG (language
3687 mask LANG_MASK, option handlers HANDLERS) as an error for option
3688 structures OPTS and OPTS_SET, diagnostic context DC (possibly
3689 NULL), location LOC. This is used by -Werror=. */
3690
3691 static void
3692 enable_warning_as_error (const char *arg, int value, unsigned int lang_mask,
3693 const struct cl_option_handlers *handlers,
3694 struct gcc_options *opts,
3695 struct gcc_options *opts_set,
3696 location_t loc, diagnostic_context *dc)
3697 {
3698 char *new_option;
3699 int option_index;
3700
3701 new_option = XNEWVEC (char, strlen (arg) + 2);
3702 new_option[0] = 'W';
3703 strcpy (new_option + 1, arg);
3704 option_index = find_opt (new_option, lang_mask);
3705 if (option_index == OPT_SPECIAL_unknown)
3706 {
3707 option_proposer op;
3708 const char *hint = op.suggest_option (new_option);
3709 if (hint)
3710 error_at (loc, "%<-W%serror=%s%>: no option %<-%s%>;"
3711 " did you mean %<-%s%>?", value ? "" : "no-",
3712 arg, new_option, hint);
3713 else
3714 error_at (loc, "%<-W%serror=%s%>: no option %<-%s%>",
3715 value ? "" : "no-", arg, new_option);
3716 }
3717 else if (!(cl_options[option_index].flags & CL_WARNING))
3718 error_at (loc, "%<-Werror=%s%>: %<-%s%> is not an option that "
3719 "controls warnings", arg, new_option);
3720 else
3721 {
3722 const diagnostic_t kind = value ? DK_ERROR : DK_WARNING;
3723 const char *arg = NULL;
3724
3725 if (cl_options[option_index].flags & CL_JOINED)
3726 arg = new_option + cl_options[option_index].opt_len;
3727 control_warning_option (option_index, (int) kind, arg, value,
3728 loc, lang_mask,
3729 handlers, opts, opts_set, dc);
3730 }
3731 free (new_option);
3732 }
3733
3734 /* Return malloced memory for the name of the option OPTION_INDEX
3735 which enabled a diagnostic, originally of type
3736 ORIG_DIAG_KIND but possibly converted to DIAG_KIND by options such
3737 as -Werror. */
3738
3739 char *
3740 compiler_diagnostic_option_manager::
3741 make_option_name (diagnostic_option_id option_id,
3742 diagnostic_t orig_diag_kind,
3743 diagnostic_t diag_kind) const
3744 {
3745 if (option_id.m_idx)
3746 {
3747 /* A warning classified as an error. */
3748 if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN)
3749 && diag_kind == DK_ERROR)
3750 return concat (cl_options[OPT_Werror_].opt_text,
3751 /* Skip over "-W". */
3752 cl_options[option_id.m_idx].opt_text + 2,
3753 NULL);
3754 /* A warning with option. */
3755 else
3756 return xstrdup (cl_options[option_id.m_idx].opt_text);
3757 }
3758 /* A warning without option classified as an error. */
3759 else if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN
3760 || diag_kind == DK_WARNING)
3761 && m_context.warning_as_error_requested_p ())
3762 return xstrdup (cl_options[OPT_Werror].opt_text);
3763 else
3764 return NULL;
3765 }
3766
3767 /* Get the page within the documentation for this option. */
3768
3769 static const char *
3770 get_option_html_page (int option_index)
3771 {
3772 const cl_option *cl_opt = &cl_options[option_index];
3773
3774 #ifdef CL_Fortran
3775 if ((cl_opt->flags & CL_Fortran) != 0
3776 /* If it is option common to both C/C++ and Fortran, it is documented
3777 in gcc/ rather than gfortran/ docs. */
3778 && (cl_opt->flags & CL_C) == 0
3779 #ifdef CL_CXX
3780 && (cl_opt->flags & CL_CXX) == 0
3781 #endif
3782 )
3783 return "gfortran/Error-and-Warning-Options.html";
3784 #endif
3785
3786 return nullptr;
3787 }
3788
3789 /* Get the url within the documentation for this option, or NULL. */
3790
3791 label_text
3792 get_option_url_suffix (int option_index, unsigned lang_mask)
3793 {
3794 if (const char *url = get_opt_url_suffix (option_index, lang_mask))
3795
3796 return label_text::borrow (url);
3797
3798 /* Fallback code for some options that aren't handled byt opt_url_suffixes
3799 e.g. links below "gfortran/". */
3800 if (const char *html_page = get_option_html_page (option_index))
3801 return label_text::take
3802 (concat (html_page,
3803 /* Expect an anchor of the form "index-Wfoo" e.g.
3804 <a name="index-Wformat"></a>, and thus an id within
3805 the page of "#index-Wformat". */
3806 "#index",
3807 cl_options[option_index].opt_text,
3808 NULL));
3809
3810 return label_text ();
3811 }
3812
3813 /* Return malloced memory for a URL describing the option OPTION_INDEX
3814 which enabled a diagnostic. */
3815
3816 char *
3817 gcc_diagnostic_option_manager::
3818 make_option_url (diagnostic_option_id option_id) const
3819 {
3820 if (option_id.m_idx)
3821 {
3822 label_text url_suffix = get_option_url_suffix (option_id.m_idx,
3823 m_lang_mask);
3824 if (url_suffix.get ())
3825 return concat (DOCUMENTATION_ROOT_URL, url_suffix.get (), nullptr);
3826 }
3827
3828 return nullptr;
3829 }
3830
3831 /* Return a heap allocated producer with command line options. */
3832
3833 char *
3834 gen_command_line_string (cl_decoded_option *options,
3835 unsigned int options_count)
3836 {
3837 auto_vec<const char *> switches;
3838 char *options_string, *tail;
3839 const char *p;
3840 size_t len = 0;
3841
3842 for (unsigned i = 0; i < options_count; i++)
3843 switch (options[i].opt_index)
3844 {
3845 case OPT_o:
3846 case OPT_d:
3847 case OPT_dumpbase:
3848 case OPT_dumpbase_ext:
3849 case OPT_dumpdir:
3850 case OPT_quiet:
3851 case OPT_version:
3852 case OPT_v:
3853 case OPT_w:
3854 case OPT_L:
3855 case OPT_I:
3856 case OPT_SPECIAL_unknown:
3857 case OPT_SPECIAL_ignore:
3858 case OPT_SPECIAL_warn_removed:
3859 case OPT_SPECIAL_program_name:
3860 case OPT_SPECIAL_input_file:
3861 case OPT_grecord_gcc_switches:
3862 case OPT_frecord_gcc_switches:
3863 case OPT__output_pch:
3864 case OPT_fdiagnostics_show_highlight_colors:
3865 case OPT_fdiagnostics_show_location_:
3866 case OPT_fdiagnostics_show_option:
3867 case OPT_fdiagnostics_show_caret:
3868 case OPT_fdiagnostics_show_event_links:
3869 case OPT_fdiagnostics_show_labels:
3870 case OPT_fdiagnostics_show_line_numbers:
3871 case OPT_fdiagnostics_color_:
3872 case OPT_fdiagnostics_format_:
3873 case OPT_fverbose_asm:
3874 case OPT____:
3875 case OPT__sysroot_:
3876 case OPT_nostdinc:
3877 case OPT_nostdinc__:
3878 case OPT_fpreprocessed:
3879 case OPT_fltrans_output_list_:
3880 case OPT_fresolution_:
3881 case OPT_fdebug_prefix_map_:
3882 case OPT_fmacro_prefix_map_:
3883 case OPT_ffile_prefix_map_:
3884 case OPT_fprofile_prefix_map_:
3885 case OPT_fcanon_prefix_map:
3886 case OPT_fcompare_debug:
3887 case OPT_fchecking:
3888 case OPT_fchecking_:
3889 /* Ignore these. */
3890 continue;
3891 case OPT_D:
3892 case OPT_U:
3893 if (startswith (options[i].arg, "_FORTIFY_SOURCE")
3894 && (options[i].arg[sizeof ("_FORTIFY_SOURCE") - 1] == '\0'
3895 || (options[i].opt_index == OPT_D
3896 && options[i].arg[sizeof ("_FORTIFY_SOURCE") - 1] == '=')))
3897 {
3898 switches.safe_push (options[i].orig_option_with_args_text);
3899 len += strlen (options[i].orig_option_with_args_text) + 1;
3900 }
3901 /* Otherwise ignore these. */
3902 continue;
3903 case OPT_flto_:
3904 {
3905 const char *lto_canonical = "-flto";
3906 switches.safe_push (lto_canonical);
3907 len += strlen (lto_canonical) + 1;
3908 break;
3909 }
3910 default:
3911 if (cl_options[options[i].opt_index].flags
3912 & CL_NO_DWARF_RECORD)
3913 continue;
3914 gcc_checking_assert (options[i].canonical_option[0][0] == '-');
3915 switch (options[i].canonical_option[0][1])
3916 {
3917 case 'M':
3918 case 'i':
3919 case 'W':
3920 continue;
3921 case 'f':
3922 if (strncmp (options[i].canonical_option[0] + 2,
3923 "dump", 4) == 0)
3924 continue;
3925 break;
3926 default:
3927 break;
3928 }
3929 switches.safe_push (options[i].orig_option_with_args_text);
3930 len += strlen (options[i].orig_option_with_args_text) + 1;
3931 break;
3932 }
3933
3934 options_string = XNEWVEC (char, len + 1);
3935 tail = options_string;
3936
3937 unsigned i;
3938 FOR_EACH_VEC_ELT (switches, i, p)
3939 {
3940 len = strlen (p);
3941 memcpy (tail, p, len);
3942 tail += len;
3943 if (i != switches.length () - 1)
3944 {
3945 *tail = ' ';
3946 ++tail;
3947 }
3948 }
3949
3950 *tail = '\0';
3951 return options_string;
3952 }
3953
3954 /* Return a heap allocated producer string including command line options. */
3955
3956 char *
3957 gen_producer_string (const char *language_string, cl_decoded_option *options,
3958 unsigned int options_count)
3959 {
3960 char *cmdline = gen_command_line_string (options, options_count);
3961 char *combined = concat (language_string, " ", version_string, " ",
3962 cmdline, NULL);
3963 free (cmdline);
3964 return combined;
3965 }
3966
3967 #if CHECKING_P
3968
3969 namespace selftest {
3970
3971 /* Verify that get_option_url_suffix works as expected. */
3972
3973 static void
3974 test_get_option_url_suffix ()
3975 {
3976 ASSERT_STREQ (get_option_url_suffix (OPT_Wcpp, 0).get (),
3977 "gcc/Warning-Options.html#index-Wcpp");
3978 ASSERT_STREQ (get_option_url_suffix (OPT_Wanalyzer_double_free, 0).get (),
3979 "gcc/Static-Analyzer-Options.html#index-Wanalyzer-double-free");
3980
3981 /* Test of a D-specific option. */
3982 #ifdef CL_D
3983 ASSERT_EQ (get_option_url_suffix (OPT_fbounds_check_, 0).get (), nullptr);
3984 ASSERT_STREQ (get_option_url_suffix (OPT_fbounds_check_, CL_D).get (),
3985 "gdc/Runtime-Options.html#index-fbounds-check");
3986
3987 /* Test of a D-specific override to an option URL. */
3988 /* Generic URL. */
3989 ASSERT_STREQ (get_option_url_suffix (OPT_fmax_errors_, 0).get (),
3990 "gcc/Warning-Options.html#index-fmax-errors");
3991 /* D-specific URL. */
3992 ASSERT_STREQ (get_option_url_suffix (OPT_fmax_errors_, CL_D).get (),
3993 "gdc/Warnings.html#index-fmax-errors");
3994 #endif
3995
3996 #ifdef CL_Fortran
3997 ASSERT_STREQ
3998 (get_option_url_suffix (OPT_Wline_truncation, CL_Fortran).get (),
3999 "gfortran/Error-and-Warning-Options.html#index-Wline-truncation");
4000 #endif
4001 }
4002
4003 /* Verify EnumSet and EnumBitSet requirements. */
4004
4005 static void
4006 test_enum_sets ()
4007 {
4008 for (unsigned i = 0; i < cl_options_count; ++i)
4009 if (cl_options[i].var_type == CLVC_ENUM
4010 && cl_options[i].var_value != CLEV_NORMAL)
4011 {
4012 const struct cl_enum *e = &cl_enums[cl_options[i].var_enum];
4013 unsigned HOST_WIDE_INT used_sets = 0;
4014 unsigned HOST_WIDE_INT mask = 0;
4015 unsigned highest_set = 0;
4016 for (unsigned j = 0; e->values[j].arg; ++j)
4017 {
4018 unsigned set = e->values[j].flags >> CL_ENUM_SET_SHIFT;
4019 if (cl_options[i].var_value == CLEV_BITSET)
4020 {
4021 /* For EnumBitSet Set shouldn't be used and Value should
4022 be a power of two. */
4023 ASSERT_TRUE (set == 0);
4024 ASSERT_TRUE (pow2p_hwi (e->values[j].value));
4025 continue;
4026 }
4027 /* Test that enumerators referenced in EnumSet have all
4028 Set(n) on them within the valid range. */
4029 ASSERT_TRUE (set >= 1 && set <= HOST_BITS_PER_WIDE_INT);
4030 highest_set = MAX (set, highest_set);
4031 used_sets |= HOST_WIDE_INT_1U << (set - 1);
4032 }
4033 if (cl_options[i].var_value == CLEV_BITSET)
4034 continue;
4035 /* If there is just one set, no point to using EnumSet. */
4036 ASSERT_TRUE (highest_set >= 2);
4037 /* Test that there are no gaps in between the sets. */
4038 if (highest_set == HOST_BITS_PER_WIDE_INT)
4039 ASSERT_TRUE (used_sets == HOST_WIDE_INT_M1U);
4040 else
4041 ASSERT_TRUE (used_sets == (HOST_WIDE_INT_1U << highest_set) - 1);
4042 for (unsigned int j = 1; j <= highest_set; ++j)
4043 {
4044 unsigned HOST_WIDE_INT this_mask = 0;
4045 for (unsigned k = 0; e->values[k].arg; ++k)
4046 {
4047 unsigned set = e->values[j].flags >> CL_ENUM_SET_SHIFT;
4048 if (set == j)
4049 this_mask |= e->values[j].value;
4050 }
4051 ASSERT_TRUE ((mask & this_mask) == 0);
4052 mask |= this_mask;
4053 }
4054 }
4055 }
4056
4057 /* Run all of the selftests within this file. */
4058
4059 void
4060 opts_cc_tests ()
4061 {
4062 test_get_option_url_suffix ();
4063 test_enum_sets ();
4064 }
4065
4066 } // namespace selftest
4067
4068 #endif /* #if CHECKING_P */