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