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