1/* Command line option handling.
2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef GCC_OPTS_H
21#define GCC_OPTS_H
22
23#include "obstack.h"
24
25/* Specifies how a switch's VAR_VALUE relates to its FLAG_VAR. */
26enum cl_var_type {
27 /* The switch is an integer value. */
28 CLVC_INTEGER,
29
30 /* The switch is enabled when FLAG_VAR == VAR_VALUE. */
31 CLVC_EQUAL,
32
33 /* The switch is enabled when VAR_VALUE is not set in FLAG_VAR. */
34 CLVC_BIT_CLEAR,
35
36 /* The switch is enabled when VAR_VALUE is set in FLAG_VAR. */
37 CLVC_BIT_SET,
38
39 /* The switch is enabled when FLAG_VAR is less than HOST_WIDE_INT_M1U. */
40 CLVC_SIZE,
41
42 /* The switch takes a string argument and FLAG_VAR points to that
43 argument. */
44 CLVC_STRING,
45
46 /* The switch takes an enumerated argument (VAR_ENUM says what
47 enumeration) and FLAG_VAR points to that argument. */
48 CLVC_ENUM,
49
50 /* The switch should be stored in the VEC pointed to by FLAG_VAR for
51 later processing. */
52 CLVC_DEFER
53};
54
55/* Values for var_value member of CLVC_ENUM. */
56enum cl_enum_var_value {
57 /* Enum without EnumSet or EnumBitSet. */
58 CLEV_NORMAL,
59
60 /* EnumSet. */
61 CLEV_SET,
62
63 /* EnumBitSet. */
64 CLEV_BITSET
65};
66
67struct cl_option
68{
69 /* Text of the option, including initial '-'. */
70 const char *opt_text;
71 /* Help text for --help, or NULL. */
72 const char *help;
73 /* Error message for missing argument, or NULL. */
74 const char *missing_argument_error;
75 /* Warning to give when this option is used, or NULL. */
76 const char *warn_message;
77 /* Argument of alias target when positive option given, or NULL. */
78 const char *alias_arg;
79 /* Argument of alias target when negative option given, or NULL. */
80 const char *neg_alias_arg;
81 /* Alias target, or N_OPTS if not an alias. */
82 unsigned short alias_target;
83 /* Previous option that is an initial substring of this one, or
84 N_OPTS if none. */
85 unsigned short back_chain;
86 /* Option length, not including initial '-'. */
87 unsigned char opt_len;
88 /* Next option in a sequence marked with Negative, or -1 if none.
89 For a single option with both a negative and a positve form
90 (such as -Wall and -Wno-all), NEG_IDX is equal to the option's
91 own index (i.e., cl_options[IDX].neg_idx == IDX holds). */
92 int neg_index;
93 /* CL_* flags for this option. */
94 unsigned int flags;
95 /* Disabled in this configuration. */
96 BOOL_BITFIELD cl_disabled : 1;
97 /* Options marked with CL_SEPARATE take a number of separate
98 arguments (1 to 4) that is one more than the number in this
99 bit-field. */
100 unsigned int cl_separate_nargs : 2;
101 /* Option is an alias when used with separate argument. */
102 BOOL_BITFIELD cl_separate_alias : 1;
103 /* Alias to negative form of option. */
104 BOOL_BITFIELD cl_negative_alias : 1;
105 /* Option takes no argument in the driver. */
106 BOOL_BITFIELD cl_no_driver_arg : 1;
107 /* Reject this option in the driver. */
108 BOOL_BITFIELD cl_reject_driver : 1;
109 /* Reject no- form. */
110 BOOL_BITFIELD cl_reject_negative : 1;
111 /* Missing argument OK (joined). */
112 BOOL_BITFIELD cl_missing_ok : 1;
113 /* Argument is an integer >=0. */
114 BOOL_BITFIELD cl_uinteger : 1;
115 /* Argument is a HOST_WIDE_INT. */
116 BOOL_BITFIELD cl_host_wide_int : 1;
117 /* Argument should be converted to lowercase. */
118 BOOL_BITFIELD cl_tolower : 1;
119 /* Argument is an unsigned integer with an optional byte suffix. */
120 BOOL_BITFIELD cl_byte_size: 1;
121 /* Offset of field for this option in struct gcc_options, or
122 (unsigned short) -1 if none. */
123 unsigned short flag_var_offset;
124 /* Index in cl_enums of enum used for this option's arguments, for
125 CLVC_ENUM options. */
126 unsigned short var_enum;
127 /* How this option's value is determined and sets a field. */
128 enum cl_var_type var_type;
129 /* Value or bit-mask with which to set a field. */
130 HOST_WIDE_INT var_value;
131 /* Range info minimum, or -1. */
132 int range_min;
133 /* Range info maximum, or -1. */
134 int range_max;
135};
136
137struct cl_var
138{
139 /* Name of the variable. */
140 const char *var_name;
141 /* Offset of field for this var in struct gcc_options. */
142 unsigned short var_offset;
143};
144
145/* Records that the state of an option consists of SIZE bytes starting
146 at DATA. DATA might point to CH in some cases. */
147struct cl_option_state {
148 const void *data;
149 size_t size;
150 char ch;
151};
152
153extern const struct cl_option cl_options[];
154extern const unsigned int cl_options_count;
155#ifdef ENABLE_PLUGIN
156extern const struct cl_var cl_vars[];
157#endif
158extern const char *const lang_names[];
159extern const unsigned int cl_lang_count;
160
161#define CL_PARAMS (1U << 16) /* Fake entry. Used to display --param info with --help. */
162#define CL_WARNING (1U << 17) /* Enables an (optional) warning message. */
163#define CL_OPTIMIZATION (1U << 18) /* Enables an (optional) optimization. */
164#define CL_DRIVER (1U << 19) /* Driver option. */
165#define CL_TARGET (1U << 20) /* Target-specific option. */
166#define CL_COMMON (1U << 21) /* Language-independent. */
167
168#define CL_MIN_OPTION_CLASS CL_PARAMS
169#define CL_MAX_OPTION_CLASS CL_COMMON
170
171/* From here on the bits describe attributes of the options.
172 Before this point the bits have described the class of the option.
173 This distinction is important because --help will not list options
174 which only have these higher bits set. */
175
176#define CL_JOINED (1U << 22) /* If takes joined argument. */
177#define CL_SEPARATE (1U << 23) /* If takes a separate argument. */
178#define CL_UNDOCUMENTED (1U << 24) /* Do not output with --help. */
179#define CL_NO_DWARF_RECORD (1U << 25) /* Do not add to producer string. */
180#define CL_PCH_IGNORE (1U << 26) /* Do compare state for pch. */
181
182/* Flags for an enumerated option argument. */
183#define CL_ENUM_CANONICAL (1 << 0) /* Canonical for this value. */
184#define CL_ENUM_DRIVER_ONLY (1 << 1) /* Only accepted in the driver. */
185#define CL_ENUM_SET_SHIFT 2 /* Shift for enum set. */
186
187/* Structure describing an enumerated option argument. */
188
189struct cl_enum_arg
190{
191 /* The argument text, or NULL at the end of the array. */
192 const char *arg;
193
194 /* The corresponding integer value. */
195 int value;
196
197 /* Flags associated with this argument. */
198 unsigned int flags;
199};
200
201/* Structure describing an enumerated set of option arguments. */
202
203struct cl_enum
204{
205 /* Help text, or NULL if the values should not be listed in --help
206 output. */
207 const char *help;
208
209 /* Error message for unknown arguments, or NULL to use a generic
210 error. */
211 const char *unknown_error;
212
213 /* Array of possible values. */
214 const struct cl_enum_arg *values;
215
216 /* The size of the type used to store a value. */
217 size_t var_size;
218
219 /* Function to set a variable of this type. */
220 void (*set) (void *var, int value);
221
222 /* Function to get the value of a variable of this type. */
223 int (*get) (const void *var);
224};
225
226extern const struct cl_enum cl_enums[];
227extern const unsigned int cl_enums_count;
228
229/* Possible ways in which a command-line option may be erroneous.
230 These do not include not being known at all; an option index of
231 OPT_SPECIAL_unknown is used for that. */
232
233#define CL_ERR_DISABLED (1 << 0) /* Disabled in this configuration. */
234#define CL_ERR_MISSING_ARG (1 << 1) /* Argument required but missing. */
235#define CL_ERR_WRONG_LANG (1 << 2) /* Option for wrong language. */
236#define CL_ERR_UINT_ARG (1 << 3) /* Bad unsigned integer argument. */
237#define CL_ERR_INT_RANGE_ARG (1 << 4) /* Bad unsigned integer argument. */
238#define CL_ERR_ENUM_ARG (1 << 5) /* Bad enumerated argument. */
239#define CL_ERR_NEGATIVE (1 << 6) /* Negative form of option
240 not permitted (together
241 with OPT_SPECIAL_unknown). */
242#define CL_ERR_ENUM_SET_ARG (1 << 7) /* Bad argument of enumerated set. */
243
244/* Structure describing the result of decoding an option. */
245
246struct cl_decoded_option
247{
248 /* The index of this option, or an OPT_SPECIAL_* value for
249 non-options and unknown options. */
250 size_t opt_index;
251
252 /* Any warning to give for use of this option, or NULL if none. */
253 const char *warn_message;
254
255 /* The string argument, or NULL if none. For OPT_SPECIAL_* cases,
256 the option or non-option command-line argument. */
257 const char *arg;
258
259 /* The original text of option plus arguments, with separate argv
260 elements concatenated into one string with spaces separating
261 them. This is for such uses as diagnostics and
262 -frecord-gcc-switches. */
263 const char *orig_option_with_args_text;
264
265 /* The canonical form of the option and its argument, for when it is
266 necessary to reconstruct argv elements (in particular, for
267 processing specs and passing options to subprocesses from the
268 driver). */
269 const char *canonical_option[4];
270
271 /* The number of elements in the canonical form of the option and
272 arguments; always at least 1. */
273 size_t canonical_option_num_elements;
274
275 /* For a boolean option, 1 for the true case and 0 for the "no-"
276 case. For an unsigned integer option, the value of the
277 argument. For enum the value of the enumerator corresponding
278 to argument string. 1 in all other cases. */
279 HOST_WIDE_INT value;
280
281 /* For EnumSet the value mask. Variable should be changed to
282 value | (prev_value & ~mask). */
283 HOST_WIDE_INT mask;
284
285 /* Any flags describing errors detected in this option. */
286 int errors;
287};
288
289/* Structure describing an option deferred for handling after the main
290 option handlers. */
291
292struct cl_deferred_option
293{
294 /* Elements from struct cl_decoded_option used for deferred
295 options. */
296 size_t opt_index;
297 const char *arg;
298 int value;
299};
300
301/* Structure describing a single option-handling callback. */
302
303struct cl_option_handler_func
304{
305 /* The function called to handle the option. */
306 bool (*handler) (struct gcc_options *opts,
307 struct gcc_options *opts_set,
308 const struct cl_decoded_option *decoded,
309 unsigned int lang_mask, int kind, location_t loc,
310 const struct cl_option_handlers *handlers,
311 diagnostic_context *dc,
312 void (*target_option_override_hook) (void));
313
314 /* The mask that must have some bit in common with the flags for the
315 option for this particular handler to be used. */
316 unsigned int mask;
317};
318
319/* Structure describing the callbacks used in handling options. */
320
321struct cl_option_handlers
322{
323 /* Callback for an unknown option to determine whether to give an
324 error for it, and possibly store information to diagnose the
325 option at a later point. Return true if an error should be
326 given, false otherwise. */
327 bool (*unknown_option_callback) (const struct cl_decoded_option *decoded);
328
329 /* Callback to handle, and possibly diagnose, an option for another
330 language. */
331 void (*wrong_lang_callback) (const struct cl_decoded_option *decoded,
332 unsigned int lang_mask);
333
334 /* Target option override hook. */
335 void (*target_option_override_hook) (void);
336
337 /* The number of individual handlers. */
338 size_t num_handlers;
339
340 /* The handlers themselves. */
341 struct cl_option_handler_func handlers[3];
342};
343
344/* Hold command-line options associated with stack limitation. */
345extern const char *opt_fstack_limit_symbol_arg;
346extern int opt_fstack_limit_register_no;
347
348/* Input file names. */
349
350extern const char **in_fnames;
351
352/* The count of input filenames. */
353
354extern unsigned num_in_fnames;
355
356extern char *opts_concat (const char *first, ...);
357
358/* Obstack for option strings. */
359
360extern struct obstack opts_obstack;
361
362size_t find_opt (const char *input, unsigned int lang_mask);
363extern HOST_WIDE_INT integral_argument (const char *arg, int * = NULL, bool = false);
364extern bool enum_value_to_arg (const struct cl_enum_arg *enum_args,
365 const char **argp, int value,
366 unsigned int lang_mask);
367extern void decode_cmdline_options_to_array (unsigned int argc,
368 const char **argv,
369 unsigned int lang_mask,
370 struct cl_decoded_option **decoded_options,
371 unsigned int *decoded_options_count);
372extern void init_options_once (void);
373extern void init_options_struct (struct gcc_options *opts,
374 struct gcc_options *opts_set);
375extern void init_opts_obstack (void);
376extern void decode_cmdline_options_to_array_default_mask (unsigned int argc,
377 const char **argv,
378 struct cl_decoded_option **decoded_options,
379 unsigned int *decoded_options_count);
380extern void set_default_handlers (struct cl_option_handlers *handlers,
381 void (*target_option_override_hook) (void));
382extern void decode_options (struct gcc_options *opts,
383 struct gcc_options *opts_set,
384 struct cl_decoded_option *decoded_options,
385 unsigned int decoded_options_count,
386 location_t loc,
387 diagnostic_context *dc,
388 void (*target_option_override_hook) (void));
389extern int option_enabled (int opt_idx, unsigned lang_mask, void *opts);
390
391extern bool get_option_state (struct gcc_options *, int,
392 struct cl_option_state *);
393extern void set_option (struct gcc_options *opts,
394 struct gcc_options *opts_set,
395 int opt_index, HOST_WIDE_INT value, const char *arg,
396 int kind, location_t loc, diagnostic_context *dc,
397 HOST_WIDE_INT = 0);
398extern void *option_flag_var (int opt_index, struct gcc_options *opts);
399bool handle_generated_option (struct gcc_options *opts,
400 struct gcc_options *opts_set,
401 size_t opt_index, const char *arg,
402 HOST_WIDE_INT value,
403 unsigned int lang_mask, int kind, location_t loc,
404 const struct cl_option_handlers *handlers,
405 bool generated_p, diagnostic_context *dc);
406void generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
407 unsigned int lang_mask,
408 struct cl_decoded_option *decoded);
409void generate_option_input_file (const char *file,
410 struct cl_decoded_option *decoded);
411extern void read_cmdline_option (struct gcc_options *opts,
412 struct gcc_options *opts_set,
413 struct cl_decoded_option *decoded,
414 location_t loc,
415 unsigned int lang_mask,
416 const struct cl_option_handlers *handlers,
417 diagnostic_context *dc);
418extern void control_warning_option (unsigned int opt_index, int kind,
419 const char *arg, bool imply, location_t loc,
420 unsigned int lang_mask,
421 const struct cl_option_handlers *handlers,
422 struct gcc_options *opts,
423 struct gcc_options *opts_set,
424 diagnostic_context *dc);
425extern char *write_langs (unsigned int mask);
426extern void print_ignored_options (void);
427extern void handle_common_deferred_options (void);
428extern void handle_deferred_dump_options (void);
429unsigned int parse_sanitizer_options (const char *, location_t, int,
430 unsigned int, int, bool);
431
432unsigned int parse_no_sanitize_attribute (char *value);
433extern bool common_handle_option (struct gcc_options *opts,
434 struct gcc_options *opts_set,
435 const struct cl_decoded_option *decoded,
436 unsigned int lang_mask, int kind,
437 location_t loc,
438 const struct cl_option_handlers *handlers,
439 diagnostic_context *dc,
440 void (*target_option_override_hook) (void));
441extern bool target_handle_option (struct gcc_options *opts,
442 struct gcc_options *opts_set,
443 const struct cl_decoded_option *decoded,
444 unsigned int lang_mask, int kind,
445 location_t loc,
446 const struct cl_option_handlers *handlers,
447 diagnostic_context *dc,
448 void (*target_option_override_hook) (void));
449extern void finish_options (struct gcc_options *opts,
450 struct gcc_options *opts_set,
451 location_t loc);
452extern void diagnose_options (gcc_options *opts, gcc_options *opts_set,
453 location_t loc);
454extern void print_help (struct gcc_options *opts, unsigned int lang_mask, const
455 char *help_option_argument);
456extern void default_options_optimization (struct gcc_options *opts,
457 struct gcc_options *opts_set,
458 struct cl_decoded_option *decoded_options,
459 unsigned int decoded_options_count,
460 location_t loc,
461 unsigned int lang_mask,
462 const struct cl_option_handlers *handlers,
463 diagnostic_context *dc);
464extern void set_struct_debug_option (struct gcc_options *opts,
465 location_t loc,
466 const char *value);
467extern bool opt_enum_arg_to_value (size_t opt_index, const char *arg,
468 int *value,
469 unsigned int lang_mask);
470
471extern const struct sanitizer_opts_s
472{
473 const char *const name;
474 unsigned int flag;
475 size_t len;
476 bool can_recover;
477 bool can_trap;
478} sanitizer_opts[];
479
480extern const struct zero_call_used_regs_opts_s
481{
482 const char *const name;
483 unsigned int flag;
484} zero_call_used_regs_opts[];
485
486extern vec<const char *> help_option_arguments;
487
488extern void add_misspelling_candidates (auto_vec<char *> *candidates,
489 const struct cl_option *option,
490 const char *base_option);
491extern const char *candidates_list_and_hint (const char *arg, char *&str,
492 const auto_vec <const char *> &
493 candidates);
494
495
496extern bool parse_and_check_align_values (const char *flag,
497 const char *name,
498 auto_vec<unsigned> &result_values,
499 bool report_error,
500 location_t loc);
501
502extern void parse_and_check_patch_area (const char *arg, bool report_error,
503 HOST_WIDE_INT *patch_area_size,
504 HOST_WIDE_INT *patch_area_start);
505
506extern void parse_options_from_collect_gcc_options (const char *, obstack *,
507 int *);
508
509extern void prepend_xassembler_to_collect_as_options (const char *, obstack *);
510
511extern char *gen_command_line_string (cl_decoded_option *options,
512 unsigned int options_count);
513extern char *gen_producer_string (const char *language_string,
514 cl_decoded_option *options,
515 unsigned int options_count);
516
517/* Set OPTION in OPTS to VALUE if the option is not set in OPTS_SET. */
518
519#define SET_OPTION_IF_UNSET(OPTS, OPTS_SET, OPTION, VALUE) \
520 do \
521 { \
522 if (!(OPTS_SET)->x_ ## OPTION) \
523 (OPTS)->x_ ## OPTION = VALUE; \
524 } \
525 while (false)
526
527/* Return true if OPTION is set by user in global options. */
528
529#define OPTION_SET_P(OPTION) global_options_set.x_ ## OPTION
530
531/* Find all the switches given to us
532 and make a vector describing them.
533 The elements of the vector are strings, one per switch given.
534 If a switch uses following arguments, then the `part1' field
535 is the switch itself and the `args' field
536 is a null-terminated vector containing the following arguments.
537 Bits in the `live_cond' field are:
538 SWITCH_LIVE to indicate this switch is true in a conditional spec.
539 SWITCH_FALSE to indicate this switch is overridden by a later switch.
540 SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
541 SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored.
542 SWITCH_KEEP_FOR_GCC to indicate that this switch, otherwise ignored,
543 should be included in COLLECT_GCC_OPTIONS.
544 in all do_spec calls afterwards. Used for %<S from self specs.
545 The `known' field describes whether this is an internal switch.
546 The `validated' field describes whether any spec has looked at this switch;
547 if it remains false at the end of the run, the switch must be meaningless.
548 The `ordering' field is used to temporarily mark switches that have to be
549 kept in a specific order. */
550
551#define SWITCH_LIVE (1 << 0)
552#define SWITCH_FALSE (1 << 1)
553#define SWITCH_IGNORE (1 << 2)
554#define SWITCH_IGNORE_PERMANENTLY (1 << 3)
555#define SWITCH_KEEP_FOR_GCC (1 << 4)
556
557struct switchstr
558{
559 const char *part1;
560 const char **args;
561 unsigned int live_cond;
562 bool known;
563 bool validated;
564 bool ordering;
565};
566
567#endif
568

source code of gcc/opts.h