1/* Command line option handling. Code involving global state that
2 should not be shared with the driver.
3 Copyright (C) 2002-2023 Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "backend.h"
25#include "rtl.h"
26#include "tree.h"
27#include "tree-pass.h"
28#include "diagnostic.h"
29#include "opts.h"
30#include "flags.h"
31#include "langhooks.h"
32#include "dbgcnt.h"
33#include "debug.h"
34#include "output.h"
35#include "plugin.h"
36#include "toplev.h"
37#include "context.h"
38#include "stringpool.h"
39#include "attribs.h"
40#include "asan.h"
41#include "file-prefix-map.h" /* add_*_prefix_map() */
42
43typedef const char *const_char_p; /* For DEF_VEC_P. */
44
45static vec<const_char_p> ignored_options;
46
47/* Input file names. */
48const char **in_fnames;
49unsigned num_in_fnames;
50
51/* Return a malloced slash-separated list of languages in MASK. */
52
53char *
54write_langs (unsigned int mask)
55{
56 unsigned int n = 0, len = 0;
57 const char *lang_name;
58 char *result;
59
60 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
61 if (mask & (1U << n))
62 len += strlen (s: lang_name) + 1;
63
64 /* Allocate at least one character as we'll terminate the string
65 at the very end of this function. */
66 result = XNEWVEC (char, MAX (1, len));
67 len = 0;
68 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
69 if (mask & (1U << n))
70 {
71 if (len)
72 result[len++] = '/';
73 strcpy (dest: result + len, src: lang_name);
74 len += strlen (s: lang_name);
75 }
76
77 result[len] = 0;
78
79 return result;
80}
81
82/* Complain that switch DECODED does not apply to this front end (mask
83 LANG_MASK). */
84
85static void
86complain_wrong_lang (const struct cl_decoded_option *decoded,
87 unsigned int lang_mask)
88{
89 const struct cl_option *option = &cl_options[decoded->opt_index];
90 const char *text = decoded->orig_option_with_args_text;
91 char *ok_langs = NULL, *bad_lang = NULL;
92 unsigned int opt_flags = option->flags;
93
94 if (!warn_complain_wrong_lang)
95 return;
96
97 if (!lang_hooks.complain_wrong_lang_p (option))
98 return;
99
100 opt_flags &= ((1U << cl_lang_count) - 1) | CL_DRIVER;
101 if (opt_flags != CL_DRIVER)
102 ok_langs = write_langs (mask: opt_flags);
103 if (lang_mask != CL_DRIVER)
104 bad_lang = write_langs (mask: lang_mask);
105
106 if (opt_flags == CL_DRIVER)
107 error ("command-line option %qs is valid for the driver but not for %s",
108 text, bad_lang);
109 else if (lang_mask == CL_DRIVER)
110 gcc_unreachable ();
111 else if (ok_langs[0] != '\0')
112 /* Eventually this should become a hard error IMO. */
113 warning (0, "command-line option %qs is valid for %s but not for %s",
114 text, ok_langs, bad_lang);
115 else
116 /* Happens for -Werror=warning_name. */
117 warning (0, "%<-Werror=%> argument %qs is not valid for %s",
118 text, bad_lang);
119
120 free (ptr: ok_langs);
121 free (ptr: bad_lang);
122}
123
124/* Buffer the unknown option described by the string OPT. Currently,
125 we only complain about unknown -Wno-* options if they may have
126 prevented a diagnostic. Otherwise, we just ignore them. Note that
127 if we do complain, it is only as a warning, not an error; passing
128 the compiler an unrecognized -Wno-* option should never change
129 whether the compilation succeeds or fails. */
130
131static void
132postpone_unknown_option_warning (const char *opt)
133{
134 ignored_options.safe_push (obj: opt);
135}
136
137/* Produce a warning for each option previously buffered. */
138
139void
140print_ignored_options (void)
141{
142 while (!ignored_options.is_empty ())
143 {
144 const char *opt;
145
146 opt = ignored_options.pop ();
147 /* Use inform, not warning_at, to avoid promoting these to errors. */
148 inform (UNKNOWN_LOCATION,
149 "unrecognized command-line option %qs may have been intended "
150 "to silence earlier diagnostics", opt);
151 }
152}
153
154/* Handle an unknown option DECODED, returning true if an error should
155 be given. */
156
157static bool
158unknown_option_callback (const struct cl_decoded_option *decoded)
159{
160 const char *opt = decoded->arg;
161
162 if (opt[1] == 'W' && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-'
163 && !(decoded->errors & CL_ERR_NEGATIVE))
164 {
165 /* We don't generate warnings for unknown -Wno-* options unless
166 we issue diagnostics. */
167 postpone_unknown_option_warning (opt);
168 return false;
169 }
170 else
171 return true;
172}
173
174/* Handle a front-end option; arguments and return value as for
175 handle_option. */
176
177static bool
178lang_handle_option (struct gcc_options *opts,
179 struct gcc_options *opts_set,
180 const struct cl_decoded_option *decoded,
181 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
182 location_t loc,
183 const struct cl_option_handlers *handlers,
184 diagnostic_context *dc,
185 void (*) (void))
186{
187 gcc_assert (opts == &global_options);
188 gcc_assert (opts_set == &global_options_set);
189 gcc_assert (dc == global_dc);
190 gcc_assert (decoded->canonical_option_num_elements <= 2);
191 return lang_hooks.handle_option (decoded->opt_index, decoded->arg,
192 decoded->value, kind, loc, handlers);
193}
194
195/* Handle FILENAME from the command line. */
196
197static void
198add_input_filename (const char *filename)
199{
200 num_in_fnames++;
201 in_fnames = XRESIZEVEC (const char *, in_fnames, num_in_fnames);
202 in_fnames[num_in_fnames - 1] = filename;
203}
204
205/* Handle the vector of command line options (located at LOC), storing
206 the results of processing DECODED_OPTIONS and DECODED_OPTIONS_COUNT
207 in OPTS and OPTS_SET and using DC for diagnostic state. LANG_MASK
208 contains has a single bit set representing the current language.
209 HANDLERS describes what functions to call for the options. */
210
211static void
212read_cmdline_options (struct gcc_options *opts, struct gcc_options *opts_set,
213 struct cl_decoded_option *decoded_options,
214 unsigned int decoded_options_count,
215 location_t loc,
216 unsigned int lang_mask,
217 const struct cl_option_handlers *handlers,
218 diagnostic_context *dc)
219{
220 unsigned int i;
221
222 for (i = 1; i < decoded_options_count; i++)
223 {
224 if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
225 {
226 /* Input files should only ever appear on the main command
227 line. */
228 gcc_assert (opts == &global_options);
229 gcc_assert (opts_set == &global_options_set);
230
231 if (opts->x_main_input_filename == NULL)
232 {
233 opts->x_main_input_filename = decoded_options[i].arg;
234 opts->x_main_input_baselength
235 = base_of_path (path: opts->x_main_input_filename,
236 base_out: &opts->x_main_input_basename);
237 }
238 add_input_filename (filename: decoded_options[i].arg);
239 continue;
240 }
241
242 read_cmdline_option (opts, opts_set,
243 decoded: decoded_options + i, loc, lang_mask, handlers,
244 dc);
245 }
246}
247
248/* Language mask determined at initialization. */
249static unsigned int initial_lang_mask;
250
251/* Initialize global options-related settings at start-up. */
252
253void
254init_options_once (void)
255{
256 /* Perform language-specific options initialization. */
257 initial_lang_mask = lang_hooks.option_lang_mask ();
258
259 lang_hooks.initialize_diagnostics (global_dc);
260 /* ??? Ideally, we should do this earlier and the FEs will override
261 it if desired (none do it so far). However, the way the FEs
262 construct their pretty-printers means that all previous settings
263 are overriden. */
264 diagnostic_color_init (context: global_dc);
265 diagnostic_urls_init (context: global_dc);
266}
267
268/* Decode command-line options to an array, like
269 decode_cmdline_options_to_array and with the same arguments but
270 using the default lang_mask. */
271
272void
273decode_cmdline_options_to_array_default_mask (unsigned int argc,
274 const char **argv,
275 struct cl_decoded_option **decoded_options,
276 unsigned int *decoded_options_count)
277{
278 decode_cmdline_options_to_array (argc, argv,
279 lang_mask: initial_lang_mask | CL_COMMON | CL_TARGET,
280 decoded_options, decoded_options_count);
281}
282
283/* Set *HANDLERS to the default set of option handlers for use in the
284 compilers proper (not the driver). */
285void
286set_default_handlers (struct cl_option_handlers *handlers,
287 void (*target_option_override_hook) (void))
288{
289 handlers->unknown_option_callback = unknown_option_callback;
290 handlers->wrong_lang_callback = complain_wrong_lang;
291 handlers->target_option_override_hook = target_option_override_hook;
292 handlers->num_handlers = 3;
293 handlers->handlers[0].handler = lang_handle_option;
294 handlers->handlers[0].mask = initial_lang_mask;
295 handlers->handlers[1].handler = common_handle_option;
296 handlers->handlers[1].mask = CL_COMMON;
297 handlers->handlers[2].handler = target_handle_option;
298 handlers->handlers[2].mask = CL_TARGET;
299}
300
301/* Parse command line options and set default flag values. Do minimal
302 options processing. The decoded options are in *DECODED_OPTIONS
303 and *DECODED_OPTIONS_COUNT; settings go in OPTS, OPTS_SET and DC;
304 the options are located at LOC. */
305void
306decode_options (struct gcc_options *opts, struct gcc_options *opts_set,
307 struct cl_decoded_option *decoded_options,
308 unsigned int decoded_options_count,
309 location_t loc, diagnostic_context *dc,
310 void (*target_option_override_hook) (void))
311{
312 struct cl_option_handlers handlers;
313
314 unsigned int lang_mask;
315
316 lang_mask = initial_lang_mask;
317
318 set_default_handlers (handlers: &handlers, target_option_override_hook);
319
320 default_options_optimization (opts, opts_set,
321 decoded_options, decoded_options_count,
322 loc, lang_mask, handlers: &handlers, dc);
323
324 read_cmdline_options (opts, opts_set,
325 decoded_options, decoded_options_count,
326 loc, lang_mask,
327 handlers: &handlers, dc);
328
329 finish_options (opts, opts_set, loc);
330
331 /* Print --help=* if used. */
332 unsigned i;
333 const char *arg;
334
335 if (!help_option_arguments.is_empty ())
336 {
337 /* Make sure --help=* sees the overridden values. */
338 target_option_override_hook ();
339
340 FOR_EACH_VEC_ELT (help_option_arguments, i, arg)
341 print_help (opts, lang_mask, help_option_argument: arg);
342 }
343}
344
345/* Hold command-line options associated with stack limitation. */
346const char *opt_fstack_limit_symbol_arg = NULL;
347int opt_fstack_limit_register_no = -1;
348
349/* Process common options that have been deferred until after the
350 handlers have been called for all options. */
351
352void
353handle_common_deferred_options (void)
354{
355 unsigned int i;
356 cl_deferred_option *opt;
357 vec<cl_deferred_option> v;
358
359 if (common_deferred_options)
360 v = *((vec<cl_deferred_option> *) common_deferred_options);
361 else
362 v = vNULL;
363
364 if (flag_dump_all_passed)
365 enable_rtl_dump_file ();
366
367 if (flag_opt_info)
368 opt_info_switch_p (NULL);
369
370 flag_canon_prefix_map = false;
371 FOR_EACH_VEC_ELT (v, i, opt)
372 {
373 switch (opt->opt_index)
374 {
375 case OPT_fcall_used_:
376 fix_register (opt->arg, 0, 1);
377 break;
378
379 case OPT_fcall_saved_:
380 fix_register (opt->arg, 0, 0);
381 break;
382
383 case OPT_fdbg_cnt_:
384 dbg_cnt_process_opt (arg: opt->arg);
385 break;
386
387 case OPT_fdebug_prefix_map_:
388 add_debug_prefix_map (opt->arg);
389 break;
390
391 case OPT_ffile_prefix_map_:
392 add_file_prefix_map (opt->arg);
393 break;
394
395 case OPT_fprofile_prefix_map_:
396 add_profile_prefix_map (opt->arg);
397 break;
398
399 case OPT_fcanon_prefix_map:
400 flag_canon_prefix_map = opt->value;
401 break;
402
403 case OPT_fdump_:
404 /* Deferred until plugins initialized. */
405 break;
406
407 case OPT_fopt_info_:
408 if (!opt_info_switch_p (opt->arg))
409 error ("unrecognized command-line option %<-fopt-info-%s%>",
410 opt->arg);
411 break;
412
413 case OPT_fenable_:
414 case OPT_fdisable_:
415 if (opt->opt_index == OPT_fenable_)
416 enable_pass (opt->arg);
417 else
418 disable_pass (opt->arg);
419 break;
420
421 case OPT_ffixed_:
422 /* Deferred. */
423 fix_register (opt->arg, 1, 1);
424 break;
425
426 case OPT_fplugin_:
427#ifdef ENABLE_PLUGIN
428 add_new_plugin (opt->arg);
429#else
430 error ("plugin support is disabled; configure with "
431 "%<--enable-plugin%>");
432#endif
433 break;
434
435 case OPT_fplugin_arg_:
436#ifdef ENABLE_PLUGIN
437 parse_plugin_arg_opt (opt->arg);
438#else
439 error ("plugin support is disabled; configure with "
440 "%<--enable-plugin%>");
441#endif
442 break;
443
444 case OPT_frandom_seed:
445 /* The real switch is -fno-random-seed. */
446 if (!opt->value)
447 set_random_seed (NULL);
448 break;
449
450 case OPT_frandom_seed_:
451 set_random_seed (opt->arg);
452 break;
453
454 case OPT_fstack_limit:
455 /* The real switch is -fno-stack-limit. */
456 if (!opt->value)
457 stack_limit_rtx = NULL_RTX;
458 break;
459
460 case OPT_fstack_limit_register_:
461 {
462 int reg = decode_reg_name (opt->arg);
463 if (reg < 0)
464 error ("unrecognized register name %qs", opt->arg);
465 else
466 {
467 /* Deactivate previous OPT_fstack_limit_symbol_ options. */
468 opt_fstack_limit_symbol_arg = NULL;
469 opt_fstack_limit_register_no = reg;
470 }
471 }
472 break;
473
474 case OPT_fstack_limit_symbol_:
475 /* Deactivate previous OPT_fstack_limit_register_ options. */
476 opt_fstack_limit_register_no = -1;
477 opt_fstack_limit_symbol_arg = opt->arg;
478 break;
479
480 case OPT_fasan_shadow_offset_:
481 if (!(flag_sanitize & SANITIZE_KERNEL_ADDRESS))
482 error ("%<-fasan-shadow-offset%> should only be used "
483 "with %<-fsanitize=kernel-address%>");
484 if (!set_asan_shadow_offset (opt->arg))
485 error ("unrecognized shadow offset %qs", opt->arg);
486 break;
487
488 case OPT_fsanitize_sections_:
489 set_sanitized_sections (opt->arg);
490 break;
491
492 default:
493 gcc_unreachable ();
494 }
495 }
496}
497
498/* Handle deferred dump options. */
499
500void
501handle_deferred_dump_options (void)
502{
503 unsigned int i;
504 cl_deferred_option *opt;
505 vec<cl_deferred_option> v;
506
507 if (common_deferred_options)
508 v = *((vec<cl_deferred_option> *) common_deferred_options);
509 else
510 v = vNULL;
511 FOR_EACH_VEC_ELT (v, i, opt)
512 if (opt->opt_index == OPT_fdump_)
513 g->get_dumps ()->dump_switch_p (arg: opt->arg);
514}
515

source code of gcc/opts-global.cc