1 /* Command line option handling.
2 Copyright (C) 2006-2022 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
26 #include "diagnostic.h"
27 #include "spellcheck.h"
29 #define untested() { fprintf (stderr, "@@#\n@@@:%s:%d:%s\n", __FILE__, __LINE__, __func__); }
32 static void prune_options (struct cl_decoded_option
**, unsigned int *);
34 /* An option that is undocumented, that takes a joined argument, and
35 that doesn't fit any of the classes of uses (language/common,
36 driver, target) is assumed to be a prefix used to catch
37 e.g. negated options, and stop them from being further shortened to
38 a prefix that could use the negated option as an argument. For
39 example, we want -gno-statement-frontiers to be taken as a negation
40 of -gstatement-frontiers, but without catching the gno- prefix and
41 signaling it's to be used for option remapping, it would end up
42 backtracked to g with no-statemnet-frontiers as the debug level. */
45 remapping_prefix_p (const struct cl_option
*opt
)
47 return opt
->flags
& CL_UNDOCUMENTED
48 && opt
->flags
& CL_JOINED
49 && !(opt
->flags
& (CL_DRIVER
| CL_TARGET
| CL_COMMON
| CL_LANG_ALL
));
52 /* Perform a binary search to find which option the command-line INPUT
53 matches. Returns its index in the option array, and
54 OPT_SPECIAL_unknown on failure.
56 This routine is quite subtle. A normal binary search is not good
57 enough because some options can be suffixed with an argument, and
58 multiple sub-matches can occur, e.g. input of "-pedantic" matching
59 the initial substring of "-pedantic-errors".
61 A more complicated example is -gstabs. It should match "-g" with
62 an argument of "stabs". Suppose, however, that the number and list
63 of switches are such that the binary search tests "-gen-decls"
64 before having tested "-g". This doesn't match, and as "-gen-decls"
65 is less than "-gstabs", it will become the lower bound of the
66 binary search range, and "-g" will never be seen. To resolve this
67 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
68 to "-g" so that failed searches that end between "-gen-decls" and
69 the lexicographically subsequent switch know to go back and see if
70 "-g" causes a match (which it does in this example).
72 This search is done in such a way that the longest match for the
73 front end in question wins. If there is no match for the current
74 front end, the longest match for a different front end is returned
75 (or N_OPTS if none) and the caller emits an error message. */
77 find_opt (const char *input
, unsigned int lang_mask
)
79 size_t mn
, mn_orig
, mx
, md
, opt_len
;
80 size_t match_wrong_lang
;
84 mx
= cl_options_count
;
86 /* Find mn such this lexicographical inequality holds:
87 cl_options[mn] <= input < cl_options[mn + 1]. */
91 opt_len
= cl_options
[md
].opt_len
;
92 comp
= strncmp (input
, cl_options
[md
].opt_text
+ 1, opt_len
);
102 /* This is the switch that is the best match but for a different
103 front end, or OPT_SPECIAL_unknown if there is no match at all. */
104 match_wrong_lang
= OPT_SPECIAL_unknown
;
106 /* Backtrace the chain of possible matches, returning the longest
107 one, if any, that fits best. With current GCC switches, this
108 loop executes at most twice. */
111 const struct cl_option
*opt
= &cl_options
[mn
];
113 /* Is the input either an exact match or a prefix that takes a
115 if (!strncmp (input
, opt
->opt_text
+ 1, opt
->opt_len
)
116 && (input
[opt
->opt_len
] == '\0' || (opt
->flags
& CL_JOINED
)))
118 /* If language is OK, return it. */
119 if (opt
->flags
& lang_mask
){
121 }else if (remapping_prefix_p (opt
)) {untested();
122 return OPT_SPECIAL_unknown
;
124 /* If we haven't remembered a prior match, remember this
125 one. Any prior match is necessarily better. */
126 }else if (match_wrong_lang
== OPT_SPECIAL_unknown
){
127 match_wrong_lang
= mn
;
132 /* Try the next possibility. This is cl_options_count if there
134 mn
= opt
->back_chain
;
136 while (mn
!= cl_options_count
);
138 if (match_wrong_lang
== OPT_SPECIAL_unknown
&& input
[0] == '-')
140 /* Long options, starting "--", may be abbreviated if the
141 abbreviation is unambiguous. This only applies to options
142 not taking a joined argument, and abbreviations of "--option"
143 are permitted even if there is a variant "--option=". */
144 size_t mnc
= mn_orig
+ 1;
145 size_t cmp_len
= strlen (input
);
146 while (mnc
< cl_options_count
147 && strncmp (input
, cl_options
[mnc
].opt_text
+ 1, cmp_len
) == 0)
149 /* Option matching this abbreviation. OK if it is the first
150 match and that does not take a joined argument, or the
151 second match, taking a joined argument and with only '='
152 added to the first match; otherwise considered
154 if (mnc
== mn_orig
+ 1
155 && !(cl_options
[mnc
].flags
& CL_JOINED
))
156 match_wrong_lang
= mnc
;
157 else if (mnc
== mn_orig
+ 2
158 && match_wrong_lang
== mn_orig
+ 1
159 && (cl_options
[mnc
].flags
& CL_JOINED
)
160 && (cl_options
[mnc
].opt_len
161 == cl_options
[mn_orig
+ 1].opt_len
+ 1)
162 && strncmp (cl_options
[mnc
].opt_text
+ 1,
163 cl_options
[mn_orig
+ 1].opt_text
+ 1,
164 cl_options
[mn_orig
+ 1].opt_len
) == 0)
165 ; /* OK, as long as there are no more matches. */
167 return OPT_SPECIAL_unknown
;
172 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
173 return match_wrong_lang
;
176 /* If ARG is a non-negative decimal or hexadecimal integer representable
177 in HOST_WIDE_INT return its value, otherwise return -1. If ERR is not
178 null set *ERR to zero on success or to EINVAL or to the value of errno
182 integral_argument (const char *arg
, int *err
, bool byte_size_suffix
)
197 unsigned HOST_WIDE_INT unit
= 1;
198 unsigned HOST_WIDE_INT value
= strtoull (arg
, &end
, 10);
200 /* If the value is too large to be represented use the maximum
201 representable value that strtoull sets VALUE to (setting
206 if (!byte_size_suffix
)
209 value
= strtoull (arg
, &end
, 0);
222 /* Numeric option arguments are at most INT_MAX. Make it
223 possible to specify a larger value by accepting common
225 if (!strcmp (end
, "kB"))
227 else if (!strcasecmp (end
, "KiB") || !strcmp (end
, "KB"))
229 else if (!strcmp (end
, "MB"))
230 unit
= HOST_WIDE_INT_UC (1000) * 1000;
231 else if (!strcasecmp (end
, "MiB"))
232 unit
= HOST_WIDE_INT_UC (1024) * 1024;
233 else if (!strcasecmp (end
, "GB"))
234 unit
= HOST_WIDE_INT_UC (1000) * 1000 * 1000;
235 else if (!strcasecmp (end
, "GiB"))
236 unit
= HOST_WIDE_INT_UC (1024) * 1024 * 1024;
237 else if (!strcasecmp (end
, "TB"))
238 unit
= HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000;
239 else if (!strcasecmp (end
, "TiB"))
240 unit
= HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024;
241 else if (!strcasecmp (end
, "PB"))
242 unit
= HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000;
243 else if (!strcasecmp (end
, "PiB"))
244 unit
= HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024;
245 else if (!strcasecmp (end
, "EB"))
246 unit
= HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000
248 else if (!strcasecmp (end
, "EiB"))
249 unit
= HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024
253 /* This could mean an unknown suffix or a bad prefix, like
262 unsigned HOST_WIDE_INT prod
= value
* unit
;
263 value
= prod
< value
? HOST_WIDE_INT_M1U
: prod
;
269 /* Return whether OPTION is OK for the language given by
272 option_ok_for_language (const struct cl_option
*option
,
273 unsigned int lang_mask
)
275 if (!(option
->flags
& lang_mask
))
277 else if ((option
->flags
& CL_TARGET
)
278 && (option
->flags
& (CL_LANG_ALL
| CL_DRIVER
))
279 && !(option
->flags
& (lang_mask
& ~CL_COMMON
& ~CL_TARGET
)))
280 /* Complain for target flag language mismatches if any languages
286 /* Return whether ENUM_ARG is OK for the language given by
290 enum_arg_ok_for_language (const struct cl_enum_arg
*enum_arg
,
291 unsigned int lang_mask
)
293 return (lang_mask
& CL_DRIVER
) || !(enum_arg
->flags
& CL_ENUM_DRIVER_ONLY
);
296 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning the cl_enum_arg
297 index and storing the value in *VALUE if found, and returning -1 without
298 modifying *VALUE if not found. */
301 enum_arg_to_value (const struct cl_enum_arg
*enum_args
,
302 const char *arg
, size_t len
, HOST_WIDE_INT
*value
,
303 unsigned int lang_mask
)
307 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
309 ? (strncmp (arg
, enum_args
[i
].arg
, len
) == 0
310 && enum_args
[i
].arg
[len
] == '\0')
311 : strcmp (arg
, enum_args
[i
].arg
) == 0)
312 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
314 *value
= enum_args
[i
].value
;
321 /* Look up ARG in the enum used by option OPT_INDEX for language
322 LANG_MASK, returning true and storing the value in *VALUE if found,
323 and returning false without modifying *VALUE if not found. */
326 opt_enum_arg_to_value (size_t opt_index
, const char *arg
,
327 int *value
, unsigned int lang_mask
)
329 const struct cl_option
*option
= &cl_options
[opt_index
];
331 gcc_assert (option
->var_type
== CLVC_ENUM
);
333 HOST_WIDE_INT wideval
;
334 if (enum_arg_to_value (cl_enums
[option
->var_enum
].values
, arg
, 0,
335 &wideval
, lang_mask
) >= 0)
344 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
345 corresponding string in *ARGP, returning true if the found string
346 was marked as canonical, false otherwise. If VALUE is not found
347 (which may be the case for uninitialized values if the relevant
348 option has not been passed), set *ARGP to NULL and return
352 enum_value_to_arg (const struct cl_enum_arg
*enum_args
,
353 const char **argp
, int value
, unsigned int lang_mask
)
357 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
358 if (enum_args
[i
].value
== value
359 && (enum_args
[i
].flags
& CL_ENUM_CANONICAL
)
360 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
362 *argp
= enum_args
[i
].arg
;
366 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
367 if (enum_args
[i
].value
== value
368 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
370 *argp
= enum_args
[i
].arg
;
378 /* Fill in the canonical option part of *DECODED with an option
379 described by OPT_INDEX, ARG and VALUE. */
382 generate_canonical_option (size_t opt_index
, const char *arg
,
384 struct cl_decoded_option
*decoded
)
386 const struct cl_option
*option
= &cl_options
[opt_index
];
387 const char *opt_text
= option
->opt_text
;
390 && !option
->cl_reject_negative
391 && (opt_text
[1] == 'W' || opt_text
[1] == 'f'
392 || opt_text
[1] == 'g' || opt_text
[1] == 'm'))
394 char *t
= XOBNEWVEC (&opts_obstack
, char, option
->opt_len
+ 5);
400 memcpy (t
+ 5, opt_text
+ 2, option
->opt_len
);
404 decoded
->canonical_option
[2] = NULL
;
405 decoded
->canonical_option
[3] = NULL
;
409 if ((option
->flags
& CL_SEPARATE
)
410 && !option
->cl_separate_alias
)
412 decoded
->canonical_option
[0] = opt_text
;
413 decoded
->canonical_option
[1] = arg
;
414 decoded
->canonical_option_num_elements
= 2;
418 gcc_assert (option
->flags
& CL_JOINED
);
419 decoded
->canonical_option
[0] = opts_concat (opt_text
, arg
, NULL
);
420 decoded
->canonical_option
[1] = NULL
;
421 decoded
->canonical_option_num_elements
= 1;
426 decoded
->canonical_option
[0] = opt_text
;
427 decoded
->canonical_option
[1] = NULL
;
428 decoded
->canonical_option_num_elements
= 1;
432 /* Structure describing mappings from options on the command line to
433 options to look up with find_opt. */
436 /* Prefix of the option on the command line. */
438 /* If two argv elements are considered to be merged into one option,
439 prefix for the second element, otherwise NULL. */
441 /* The new prefix to map to. */
442 const char *new_prefix
;
443 /* Whether at least one character is needed following opt1 or opt0
444 for this mapping to be used. (--optimize= is valid for -O, but
445 --warn- is not valid for -W.) */
446 bool another_char_needed
;
447 /* Whether the original option is a negated form of the option
448 resulting from this map. */
451 static const struct option_map option_map
[] =
453 { "-Wno-", NULL
, "-W", false, true },
454 { "-fno-", NULL
, "-f", false, true },
455 { "-gno-", NULL
, "-g", false, true },
456 { "-mno-", NULL
, "-m", false, true },
457 { "--debug=", NULL
, "-g", false, false },
458 { "--machine-", NULL
, "-m", true, false },
459 { "--machine-no-", NULL
, "-m", false, true },
460 { "--machine=", NULL
, "-m", false, false },
461 { "--machine=no-", NULL
, "-m", false, true },
462 { "--machine", "", "-m", false, false },
463 { "--machine", "no-", "-m", false, true },
464 { "--optimize=", NULL
, "-O", false, false },
465 { "--std=", NULL
, "-std=", false, false },
466 { "--std", "", "-std=", false, false },
467 { "--warn-", NULL
, "-W", true, false },
468 { "--warn-no-", NULL
, "-W", false, true },
469 { "--", NULL
, "-f", true, false },
470 { "--no-", NULL
, "-f", false, true }
473 /* Helper function for gcc.cc's driver::suggest_option, for populating the
474 vec of suggestions for misspelled options.
476 option_map above provides various prefixes for spelling command-line
477 options, which decode_cmdline_option uses to map spellings of options
478 to specific options. We want to do the reverse: to find all the ways
479 that a user could validly spell an option.
481 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
482 of its valid variant spellings to CANDIDATES, each without a leading
485 For example, given "-Wabi-tag", the following are added to CANDIDATES:
491 The added strings must be freed using free. */
494 add_misspelling_candidates (auto_vec
<char *> *candidates
,
495 const struct cl_option
*option
,
496 const char *opt_text
)
498 gcc_assert (candidates
);
500 gcc_assert (opt_text
);
501 if (remapping_prefix_p (option
))
503 candidates
->safe_push (xstrdup (opt_text
+ 1));
504 for (unsigned i
= 0; i
< ARRAY_SIZE (option_map
); i
++)
506 const char *opt0
= option_map
[i
].opt0
;
507 const char *new_prefix
= option_map
[i
].new_prefix
;
508 size_t new_prefix_len
= strlen (new_prefix
);
510 if (option
->cl_reject_negative
&& option_map
[i
].negated
)
513 if (strncmp (opt_text
, new_prefix
, new_prefix_len
) == 0)
515 char *alternative
= concat (opt0
+ 1, opt_text
+ new_prefix_len
,
517 candidates
->safe_push (alternative
);
521 /* For all params (e.g. --param=key=value),
522 include also '--param key=value'. */
523 const char *prefix
= "--param=";
524 if (strstr (opt_text
, prefix
) == opt_text
)
526 char *param
= xstrdup (opt_text
+ 1);
527 gcc_assert (param
[6] == '=');
529 candidates
->safe_push (param
);
533 /* Decode the switch beginning at ARGV for the language indicated by
534 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
535 the structure *DECODED. Returns the number of switches
539 decode_cmdline_option (const char *const *argv
, unsigned int lang_mask
,
540 struct cl_decoded_option
*decoded
)
544 HOST_WIDE_INT value
= 1, mask
= 0;
545 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
549 const struct cl_option
*option
;
551 const char *warn_message
= NULL
;
552 bool separate_arg_flag
;
553 bool joined_arg_flag
;
554 bool have_separate_arg
= false;
558 const char *opt_value
= argv
[0] + 1;
559 opt_index
= find_opt (opt_value
, lang_mask
);
561 while (opt_index
== OPT_SPECIAL_unknown
562 && i
< ARRAY_SIZE (option_map
))
564 const char *opt0
= option_map
[i
].opt0
;
565 const char *opt1
= option_map
[i
].opt1
;
566 const char *new_prefix
= option_map
[i
].new_prefix
;
567 bool another_char_needed
= option_map
[i
].another_char_needed
;
568 size_t opt0_len
= strlen (opt0
);
569 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
570 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
571 size_t new_prefix_len
= strlen (new_prefix
);
573 extra_args
= (opt1
== NULL
? 0 : 1);
574 value
= !option_map
[i
].negated
;
576 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
578 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
579 && (!another_char_needed
580 || argv
[extra_args
][optn_len
] != 0))
582 size_t arglen
= strlen (argv
[extra_args
]);
585 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
586 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
587 memcpy (dup
, new_prefix
, new_prefix_len
);
588 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
589 arglen
- optn_len
+ 1);
590 opt_index
= find_opt (dup
+ 1, lang_mask
);
596 if (opt_index
== OPT_SPECIAL_unknown
)
604 option
= &cl_options
[opt_index
];
606 /* Reject negative form of switches that don't take negatives as
608 if (!value
&& option
->cl_reject_negative
)
610 opt_index
= OPT_SPECIAL_unknown
;
611 errors
|= CL_ERR_NEGATIVE
;
616 /* Clear the initial value for size options (it will be overwritten
617 later based on the Init(value) specification in the opt file. */
618 if (option
->var_type
== CLVC_SIZE
)
621 result
= extra_args
+ 1;
622 warn_message
= option
->warn_message
;
624 /* Check to see if the option is disabled for this configuration. */
625 if (option
->cl_disabled
)
626 errors
|= CL_ERR_DISABLED
;
628 /* Determine whether there may be a separate argument based on
629 whether this option is being processed for the driver, and, if
630 so, how many such arguments. */
631 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
632 && !(option
->cl_no_driver_arg
633 && (lang_mask
& CL_DRIVER
)));
634 separate_args
= (separate_arg_flag
635 ? option
->cl_separate_nargs
+ 1
637 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
639 /* Sort out any argument the switch takes. */
642 /* Have arg point to the original switch. This is because
643 some code, such as disable_builtin_function, expects its
644 argument to be persistent until the program exits. */
645 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
647 if (*arg
== '\0' && !option
->cl_missing_ok
)
649 if (separate_arg_flag
)
651 arg
= argv
[extra_args
+ 1];
652 result
= extra_args
+ 2;
654 result
= extra_args
+ 1;
656 have_separate_arg
= true;
659 /* Missing argument. */
663 else if (separate_arg_flag
)
665 arg
= argv
[extra_args
+ 1];
666 for (i
= 0; i
< separate_args
; i
++)
667 if (argv
[extra_args
+ 1 + i
] == NULL
)
669 errors
|= CL_ERR_MISSING_ARG
;
672 result
= extra_args
+ 1 + i
;
674 have_separate_arg
= true;
677 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
678 errors
|= CL_ERR_MISSING_ARG
;
680 /* Is this option an alias (or an ignored option, marked as an alias
681 of OPT_SPECIAL_ignore)? */
682 if (option
->alias_target
!= N_OPTS
683 && (!option
->cl_separate_alias
|| have_separate_arg
))
685 size_t new_opt_index
= option
->alias_target
;
687 if (new_opt_index
== OPT_SPECIAL_ignore
688 || new_opt_index
== OPT_SPECIAL_warn_removed
)
690 gcc_assert (option
->alias_arg
== NULL
);
691 gcc_assert (option
->neg_alias_arg
== NULL
);
692 opt_index
= new_opt_index
;
697 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
699 /* The new option must not be an alias itself. */
700 gcc_assert (new_option
->alias_target
== N_OPTS
701 || new_option
->cl_separate_alias
);
703 if (option
->neg_alias_arg
)
705 gcc_assert (option
->alias_arg
!= NULL
);
706 gcc_assert (arg
== NULL
);
707 gcc_assert (!option
->cl_negative_alias
);
709 arg
= option
->alias_arg
;
711 arg
= option
->neg_alias_arg
;
714 else if (option
->alias_arg
)
716 gcc_assert (value
== 1);
717 gcc_assert (arg
== NULL
);
718 gcc_assert (!option
->cl_negative_alias
);
719 arg
= option
->alias_arg
;
722 if (option
->cl_negative_alias
)
725 opt_index
= new_opt_index
;
729 gcc_assert (!option
->cl_reject_negative
);
731 /* Recompute what arguments are allowed. */
732 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
733 && !(option
->cl_no_driver_arg
734 && (lang_mask
& CL_DRIVER
)));
735 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
737 if (separate_args
> 1 || option
->cl_separate_nargs
)
738 gcc_assert (separate_args
739 == (unsigned int) option
->cl_separate_nargs
+ 1);
741 if (!(errors
& CL_ERR_MISSING_ARG
))
743 if (separate_arg_flag
|| joined_arg_flag
)
745 if (option
->cl_missing_ok
&& arg
== NULL
)
747 gcc_assert (arg
!= NULL
);
750 gcc_assert (arg
== NULL
);
753 /* Recheck for warnings and disabled options. */
754 if (option
->warn_message
)
756 gcc_assert (warn_message
== NULL
);
757 warn_message
= option
->warn_message
;
759 if (option
->cl_disabled
)
760 errors
|= CL_ERR_DISABLED
;
764 /* Check if this is a switch for a different front end. */
765 if (!option_ok_for_language (option
, lang_mask
))
766 errors
|= CL_ERR_WRONG_LANG
;
767 else if (strcmp (option
->opt_text
, "-Werror=") == 0
768 && strchr (opt_value
, ',') == NULL
)
770 /* Verify that -Werror argument is a valid warning
772 char *werror_arg
= xstrdup (opt_value
+ 6);
775 size_t warning_index
= find_opt (werror_arg
, lang_mask
);
777 if (warning_index
!= OPT_SPECIAL_unknown
)
779 const struct cl_option
*warning_option
780 = &cl_options
[warning_index
];
781 if (!option_ok_for_language (warning_option
, lang_mask
))
782 errors
|= CL_ERR_WRONG_LANG
;
786 /* Convert the argument to lowercase if appropriate. */
787 if (arg
&& option
->cl_tolower
)
790 size_t len
= strlen (arg
);
791 char *arg_lower
= XOBNEWVEC (&opts_obstack
, char, len
+ 1);
793 for (j
= 0; j
< len
; j
++)
794 arg_lower
[j
] = TOLOWER ((unsigned char) arg
[j
]);
799 /* If the switch takes an integer argument, convert it. */
800 if (arg
&& (option
->cl_uinteger
|| option
->cl_host_wide_int
))
803 value
= *arg
? integral_argument (arg
, &error
, option
->cl_byte_size
) : 0;
805 errors
|= CL_ERR_UINT_ARG
;
807 /* Reject value out of a range. */
808 if (option
->range_max
!= -1
809 && (value
< option
->range_min
|| value
> option
->range_max
))
810 errors
|= CL_ERR_INT_RANGE_ARG
;
813 /* If the switch takes an enumerated argument, convert it. */
814 if (arg
&& (option
->var_type
== CLVC_ENUM
))
816 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
818 gcc_assert (option
->var_value
!= CLEV_NORMAL
|| value
== 1);
819 if (option
->var_value
!= CLEV_NORMAL
)
822 HOST_WIDE_INT sum_value
= 0;
823 unsigned HOST_WIDE_INT used_sets
= 0;
826 const char *q
= strchr (p
, ',');
827 HOST_WIDE_INT this_value
= 0;
830 errors
|= CL_ERR_ENUM_SET_ARG
;
833 int idx
= enum_arg_to_value (e
->values
, p
, q
? q
- p
: 0,
834 &this_value
, lang_mask
);
837 errors
|= CL_ERR_ENUM_SET_ARG
;
841 HOST_WIDE_INT this_mask
= 0;
842 if (option
->var_value
== CLEV_SET
)
844 unsigned set
= e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
;
845 gcc_checking_assert (set
>= 1
846 && set
<= HOST_BITS_PER_WIDE_INT
);
847 if ((used_sets
& (HOST_WIDE_INT_1U
<< (set
- 1))) != 0)
849 errors
|= CL_ERR_ENUM_SET_ARG
;
852 used_sets
|= HOST_WIDE_INT_1U
<< (set
- 1);
854 for (int i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
855 if (set
== (e
->values
[i
].flags
>> CL_ENUM_SET_SHIFT
))
856 this_mask
|= e
->values
[i
].value
;
860 gcc_assert (option
->var_value
== CLEV_BITSET
861 && ((e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
)
863 this_mask
= this_value
;
866 sum_value
|= this_value
;
876 gcc_checking_assert (value
== 0);
878 else if (enum_arg_to_value (e
->values
, arg
, 0, &value
, lang_mask
) >= 0)
880 const char *carg
= NULL
;
882 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
884 gcc_assert (carg
!= NULL
);
887 errors
|= CL_ERR_ENUM_ARG
;
891 decoded
->opt_index
= opt_index
;
893 decoded
->value
= value
;
894 decoded
->mask
= mask
;
895 decoded
->errors
= errors
;
896 decoded
->warn_message
= warn_message
;
898 if (opt_index
== OPT_SPECIAL_unknown
)
899 gcc_assert (result
== 1);
901 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
902 decoded
->canonical_option_num_elements
= result
;
904 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
909 if (opt_index
== OPT_SPECIAL_unknown
)
910 decoded
->canonical_option
[i
] = argv
[i
];
912 decoded
->canonical_option
[i
] = NULL
;
913 len
= strlen (argv
[i
]);
914 /* If the argument is an empty string, we will print it as "" in
915 orig_option_with_args_text. */
916 total_len
+= (len
!= 0 ? len
: 2) + 1;
919 decoded
->canonical_option
[i
] = NULL
;
921 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
922 && opt_index
!= OPT_SPECIAL_warn_removed
)
924 generate_canonical_option (opt_index
, arg
, value
, decoded
);
925 if (separate_args
> 1)
927 for (i
= 0; i
< separate_args
; i
++)
929 if (argv
[extra_args
+ 1 + i
] == NULL
)
932 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
934 gcc_assert (result
== 1 + i
);
935 decoded
->canonical_option_num_elements
= result
;
938 decoded
->orig_option_with_args_text
939 = p
= XOBNEWVEC (&opts_obstack
, char, total_len
);
940 for (i
= 0; i
< result
; i
++)
942 size_t len
= strlen (argv
[i
]);
944 /* Print the empty string verbally. */
951 memcpy (p
, argv
[i
], len
);
962 /* Obstack for option strings. */
964 struct obstack opts_obstack
;
966 /* Like libiberty concat, but allocate using opts_obstack. */
969 opts_concat (const char *first
, ...)
976 /* First compute the size of the result and get sufficient memory. */
977 va_start (ap
, first
);
978 for (arg
= first
; arg
; arg
= va_arg (ap
, const char *))
979 length
+= strlen (arg
);
980 newstr
= XOBNEWVEC (&opts_obstack
, char, length
+ 1);
983 /* Now copy the individual pieces to the result string. */
984 va_start (ap
, first
);
985 for (arg
= first
, end
= newstr
; arg
; arg
= va_arg (ap
, const char *))
987 length
= strlen (arg
);
988 memcpy (end
, arg
, length
);
996 /* Decode command-line options (ARGC and ARGV being the arguments of
997 main) into an array, setting *DECODED_OPTIONS to a pointer to that
998 array and *DECODED_OPTIONS_COUNT to the number of entries in the
999 array. The first entry in the array is always one for the program
1000 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
1001 flags applicable for decoding (including CL_COMMON and CL_TARGET if
1002 those options should be considered applicable). Do not produce any
1003 diagnostics or set state outside of these variables. */
1006 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
1007 unsigned int lang_mask
,
1008 struct cl_decoded_option
**decoded_options
,
1009 unsigned int *decoded_options_count
)
1012 struct cl_decoded_option
*opt_array
;
1013 unsigned int num_decoded_options
;
1015 int opt_array_len
= argc
;
1016 opt_array
= XNEWVEC (struct cl_decoded_option
, opt_array_len
);
1018 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
1019 opt_array
[0].warn_message
= NULL
;
1020 opt_array
[0].arg
= argv
[0];
1021 opt_array
[0].orig_option_with_args_text
= argv
[0];
1022 opt_array
[0].canonical_option_num_elements
= 1;
1023 opt_array
[0].canonical_option
[0] = argv
[0];
1024 opt_array
[0].canonical_option
[1] = NULL
;
1025 opt_array
[0].canonical_option
[2] = NULL
;
1026 opt_array
[0].canonical_option
[3] = NULL
;
1027 opt_array
[0].value
= 1;
1028 opt_array
[0].mask
= 0;
1029 opt_array
[0].errors
= 0;
1030 num_decoded_options
= 1;
1032 for (i
= 1; i
< argc
; i
+= n
)
1034 const char *opt
= argv
[i
];
1036 /* Interpret "-" or a non-switch as a file name. */
1037 if (opt
[0] != '-' || opt
[1] == '\0')
1039 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
1040 num_decoded_options
++;
1045 /* Interpret "--param" "key=name" as "--param=key=name". */
1046 const char *needle
= "--param";
1047 if (i
+ 1 < argc
&& strcmp (opt
, needle
) == 0)
1049 const char *replacement
1050 = opts_concat (needle
, "=", argv
[i
+ 1], NULL
);
1051 argv
[++i
] = replacement
;
1054 /* Expand -fdiagnostics-plain-output to its constituents. This needs
1055 to happen here so that prune_options can handle -fdiagnostics-color
1057 if (!strcmp (opt
, "-fdiagnostics-plain-output"))
1059 /* If you have changed the default diagnostics output, and this new
1060 output is not appropriately "plain" (e.g., the change needs to be
1061 undone in order for the testsuite to work properly), then please do
1063 1. Add the necessary option to undo the new behavior to
1065 2. Update the documentation for -fdiagnostics-plain-output
1067 const char *const expanded_args
[] = {
1068 "-fno-diagnostics-show-caret",
1069 "-fno-diagnostics-show-line-numbers",
1070 "-fdiagnostics-color=never",
1071 "-fdiagnostics-urls=never",
1072 "-fdiagnostics-path-format=separate-events",
1074 const int num_expanded
= ARRAY_SIZE (expanded_args
);
1075 opt_array_len
+= num_expanded
- 1;
1076 opt_array
= XRESIZEVEC (struct cl_decoded_option
,
1077 opt_array
, opt_array_len
);
1078 for (int j
= 0, nj
; j
< num_expanded
; j
+= nj
)
1080 nj
= decode_cmdline_option (expanded_args
+ j
, lang_mask
,
1081 &opt_array
[num_decoded_options
]);
1082 num_decoded_options
++;
1089 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
1090 &opt_array
[num_decoded_options
]);
1091 num_decoded_options
++;
1094 *decoded_options
= opt_array
;
1095 *decoded_options_count
= num_decoded_options
;
1096 prune_options (decoded_options
, decoded_options_count
);
1099 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
1100 next one is the same as ORIG_NEXT_OPT_IDX. */
1103 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
1105 /* An option can be canceled by the same option or an option with
1107 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
1110 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
1111 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
1117 /* Filter out options canceled by the ones after them. */
1120 prune_options (struct cl_decoded_option
**decoded_options
,
1121 unsigned int *decoded_options_count
)
1123 unsigned int old_decoded_options_count
= *decoded_options_count
;
1124 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
1125 unsigned int new_decoded_options_count
;
1126 struct cl_decoded_option
*new_decoded_options
1127 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
1129 const struct cl_option
*option
;
1130 unsigned int fdiagnostics_color_idx
= 0;
1132 /* Remove arguments which are negated by others after them. */
1133 new_decoded_options_count
= 0;
1134 for (i
= 0; i
< old_decoded_options_count
; i
++)
1136 unsigned int j
, opt_idx
, next_opt_idx
;
1138 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
1141 opt_idx
= old_decoded_options
[i
].opt_index
;
1144 case OPT_SPECIAL_unknown
:
1145 case OPT_SPECIAL_ignore
:
1146 case OPT_SPECIAL_warn_removed
:
1147 case OPT_SPECIAL_program_name
:
1148 case OPT_SPECIAL_input_file
:
1151 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1152 case OPT_fdiagnostics_color_
:
1153 fdiagnostics_color_idx
= i
;
1157 gcc_assert (opt_idx
< cl_options_count
);
1158 option
= &cl_options
[opt_idx
];
1159 if (option
->neg_index
< 0)
1162 /* Skip joined switches. */
1163 if ((option
->flags
& CL_JOINED
)
1164 && (!option
->cl_reject_negative
1165 || (unsigned int) option
->neg_index
!= opt_idx
))
1168 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
1170 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
1172 next_opt_idx
= old_decoded_options
[j
].opt_index
;
1173 if (next_opt_idx
>= cl_options_count
)
1175 if (cl_options
[next_opt_idx
].neg_index
< 0)
1177 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
)
1178 && (!cl_options
[next_opt_idx
].cl_reject_negative
1179 || ((unsigned int) cl_options
[next_opt_idx
].neg_index
1182 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
1185 if (j
== old_decoded_options_count
)
1188 new_decoded_options
[new_decoded_options_count
]
1189 = old_decoded_options
[i
];
1190 new_decoded_options_count
++;
1196 if (fdiagnostics_color_idx
>= 1)
1198 /* We put the last -fdiagnostics-color= at the first position
1199 after argv[0] so it can take effect immediately. */
1200 memmove (new_decoded_options
+ 2, new_decoded_options
+ 1,
1201 sizeof (struct cl_decoded_option
)
1202 * (new_decoded_options_count
- 1));
1203 new_decoded_options
[1] = old_decoded_options
[fdiagnostics_color_idx
];
1204 new_decoded_options_count
++;
1207 free (old_decoded_options
);
1208 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
1209 new_decoded_options
,
1210 new_decoded_options_count
);
1211 *decoded_options
= new_decoded_options
;
1212 *decoded_options_count
= new_decoded_options_count
;
1215 /* Handle option DECODED for the language indicated by LANG_MASK,
1216 using the handlers in HANDLERS and setting fields in OPTS and
1217 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1218 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1219 option for options from the source file, UNKNOWN_LOCATION
1220 otherwise. GENERATED_P is true for an option generated as part of
1221 processing another option or otherwise generated internally, false
1222 for one explicitly passed by the user. control_warning_option
1223 generated options are considered explicitly passed by the user.
1224 Returns false if the switch was invalid. DC is the diagnostic
1225 context for options affecting diagnostics state, or NULL. */
1228 handle_option (struct gcc_options
*opts
,
1229 struct gcc_options
*opts_set
,
1230 const struct cl_decoded_option
*decoded
,
1231 unsigned int lang_mask
, int kind
, location_t loc
,
1232 const struct cl_option_handlers
*handlers
,
1233 bool generated_p
, diagnostic_context
*dc
)
1235 size_t opt_index
= decoded
->opt_index
;
1236 const char *arg
= decoded
->arg
;
1237 HOST_WIDE_INT value
= decoded
->value
;
1238 HOST_WIDE_INT mask
= decoded
->mask
;
1239 const struct cl_option
*option
= &cl_options
[opt_index
];
1240 void *flag_var
= option_flag_var (opt_index
, opts
);
1244 set_option (opts
, (generated_p
? NULL
: opts_set
),
1245 opt_index
, value
, arg
, kind
, loc
, dc
, mask
);
1247 for (i
= 0; i
< handlers
->num_handlers
; i
++)
1248 if (option
->flags
& handlers
->handlers
[i
].mask
)
1250 if(!handlers
->handlers
[i
].handler
){
1252 }else if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
1253 lang_mask
, kind
, loc
,
1255 handlers
->target_option_override_hook
))
1262 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1263 option instead of DECODED. This is used for callbacks when one
1264 option implies another instead of an option being decoded from the
1268 handle_generated_option (struct gcc_options
*opts
,
1269 struct gcc_options
*opts_set
,
1270 size_t opt_index
, const char *arg
, HOST_WIDE_INT value
,
1271 unsigned int lang_mask
, int kind
, location_t loc
,
1272 const struct cl_option_handlers
*handlers
,
1273 bool generated_p
, diagnostic_context
*dc
)
1275 struct cl_decoded_option decoded
;
1277 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
1278 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
1279 handlers
, generated_p
, dc
);
1282 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1283 VALUE for a front end using LANG_MASK. This is used when the
1284 compiler generates options internally. */
1287 generate_option (size_t opt_index
, const char *arg
, HOST_WIDE_INT value
,
1288 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
1290 const struct cl_option
*option
= &cl_options
[opt_index
];
1292 decoded
->opt_index
= opt_index
;
1293 decoded
->warn_message
= NULL
;
1295 decoded
->value
= value
;
1297 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
1299 : CL_ERR_WRONG_LANG
);
1301 generate_canonical_option (opt_index
, arg
, value
, decoded
);
1302 switch (decoded
->canonical_option_num_elements
)
1305 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
1309 decoded
->orig_option_with_args_text
1310 = opts_concat (decoded
->canonical_option
[0], " ",
1311 decoded
->canonical_option
[1], NULL
);
1319 /* Fill in *DECODED with an option for input file FILE. */
1322 generate_option_input_file (const char *file
,
1323 struct cl_decoded_option
*decoded
)
1325 decoded
->opt_index
= OPT_SPECIAL_input_file
;
1326 decoded
->warn_message
= NULL
;
1327 decoded
->arg
= file
;
1328 decoded
->orig_option_with_args_text
= file
;
1329 decoded
->canonical_option_num_elements
= 1;
1330 decoded
->canonical_option
[0] = file
;
1331 decoded
->canonical_option
[1] = NULL
;
1332 decoded
->canonical_option
[2] = NULL
;
1333 decoded
->canonical_option
[3] = NULL
;
1336 decoded
->errors
= 0;
1339 /* Helper function for listing valid choices and hint for misspelled
1340 value. CANDIDATES is a vector containing all valid strings,
1341 STR is set to a heap allocated string that contains all those
1342 strings concatenated, separated by spaces, and the return value
1343 is the closest string from those to ARG, or NULL if nothing is
1344 close enough. Callers should XDELETEVEC (STR) after using it
1345 to avoid memory leaks. */
1348 candidates_list_and_hint (const char *arg
, char *&str
,
1349 const auto_vec
<const char *> &candidates
)
1353 const char *candidate
;
1356 FOR_EACH_VEC_ELT (candidates
, i
, candidate
)
1357 len
+= strlen (candidate
) + 1;
1359 str
= p
= XNEWVEC (char, len
);
1360 FOR_EACH_VEC_ELT (candidates
, i
, candidate
)
1362 len
= strlen (candidate
);
1363 memcpy (p
, candidate
, len
);
1368 return find_closest_string (arg
, &candidates
);
1371 /* Perform diagnostics for read_cmdline_option and control_warning_option
1372 functions. Returns true if an error has been diagnosed.
1373 LOC and LANG_MASK arguments like in read_cmdline_option.
1374 OPTION is the option to report diagnostics for, OPT the name
1375 of the option as text, ARG the argument of the option (for joined
1376 options), ERRORS is bitmask of CL_ERR_* values. */
1379 cmdline_handle_error (location_t loc
, const struct cl_option
*option
,
1380 const char *opt
, const char *arg
, int errors
,
1381 unsigned int lang_mask
)
1383 if (errors
& CL_ERR_DISABLED
)
1385 error_at (loc
, "command-line option %qs"
1386 " is not supported by this configuration", opt
);
1390 if (errors
& CL_ERR_MISSING_ARG
)
1392 if (option
->missing_argument_error
)
1393 error_at (loc
, option
->missing_argument_error
, opt
);
1395 error_at (loc
, "missing argument to %qs", opt
);
1399 if (errors
& CL_ERR_UINT_ARG
)
1401 if (option
->cl_byte_size
)
1402 error_at (loc
, "argument to %qs should be a non-negative integer "
1403 "optionally followed by a size unit",
1406 error_at (loc
, "argument to %qs should be a non-negative integer",
1411 if (errors
& CL_ERR_INT_RANGE_ARG
)
1413 error_at (loc
, "argument to %qs is not between %d and %d",
1414 option
->opt_text
, option
->range_min
, option
->range_max
);
1418 if (errors
& CL_ERR_ENUM_SET_ARG
)
1420 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1421 const char *p
= arg
;
1422 unsigned HOST_WIDE_INT used_sets
= 0;
1423 const char *second_opt
= NULL
;
1424 size_t second_opt_len
= 0;
1428 const char *q
= strchr (p
, ',');
1429 HOST_WIDE_INT this_value
= 0;
1433 errors
= CL_ERR_ENUM_ARG
;
1436 int idx
= enum_arg_to_value (e
->values
, p
, q
? q
- p
: 0,
1437 &this_value
, lang_mask
);
1441 q
= strchr (p
, '\0');
1442 char *narg
= XALLOCAVEC (char, (q
- p
) + 1);
1443 memcpy (narg
, p
, q
- p
);
1446 errors
= CL_ERR_ENUM_ARG
;
1450 if (option
->var_value
== CLEV_BITSET
)
1458 unsigned set
= e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
;
1459 gcc_checking_assert (set
>= 1 && set
<= HOST_BITS_PER_WIDE_INT
);
1460 if ((used_sets
& (HOST_WIDE_INT_1U
<< (set
- 1))) != 0)
1463 q
= strchr (p
, '\0');
1464 if (second_opt
== NULL
)
1466 used_sets
= HOST_WIDE_INT_1U
<< (set
- 1);
1468 second_opt_len
= q
- p
;
1472 char *args
= XALLOCAVEC (char, (q
- p
) + 1 + second_opt_len
+ 1);
1473 memcpy (args
, p
, q
- p
);
1475 memcpy (args
+ (q
- p
) + 1, second_opt
, second_opt_len
);
1476 args
[(q
- p
) + 1 + second_opt_len
] = '\0';
1477 error_at (loc
, "invalid argument in option %qs", opt
);
1478 if (strcmp (args
, args
+ (q
- p
) + 1) == 0)
1479 inform (loc
, "%qs specified multiple times in the same option",
1482 inform (loc
, "%qs is mutually exclusive with %qs and cannot be"
1483 " specified together", args
, args
+ (q
- p
) + 1);
1486 used_sets
|= HOST_WIDE_INT_1U
<< (set
- 1);
1494 if (errors
& CL_ERR_ENUM_ARG
)
1496 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1500 auto_diagnostic_group d
;
1501 if (e
->unknown_error
)
1502 error_at (loc
, e
->unknown_error
, arg
);
1504 error_at (loc
, "unrecognized argument in option %qs", opt
);
1506 auto_vec
<const char *> candidates
;
1507 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1509 if (!enum_arg_ok_for_language (&e
->values
[i
], lang_mask
))
1511 candidates
.safe_push (e
->values
[i
].arg
);
1513 const char *hint
= candidates_list_and_hint (arg
, s
, candidates
);
1515 inform (loc
, "valid arguments to %qs are: %s; did you mean %qs?",
1516 option
->opt_text
, s
, hint
);
1518 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1527 /* Handle the switch DECODED (location LOC) for the language indicated
1528 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1529 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1530 diagnostic options. */
1533 read_cmdline_option (struct gcc_options
*opts
,
1534 struct gcc_options
*opts_set
,
1535 struct cl_decoded_option
*decoded
,
1537 unsigned int lang_mask
,
1538 const struct cl_option_handlers
*handlers
,
1539 diagnostic_context
*dc
)
1541 const struct cl_option
*option
;
1542 const char *opt
= decoded
->orig_option_with_args_text
;
1544 if (decoded
->warn_message
)
1545 warning_at (loc
, 0, decoded
->warn_message
, opt
);
1547 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
1549 if (handlers
->unknown_option_callback (decoded
)){ untested();
1550 error_at (loc
, "unrecognized command-line option %qs", decoded
->arg
);
1557 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
1560 if (decoded
->opt_index
== OPT_SPECIAL_warn_removed
)
1562 /* Warn only about positive ignored options. */
1564 warning_at (loc
, 0, "switch %qs is no longer supported", opt
);
1569 option
= &cl_options
[decoded
->opt_index
];
1572 && cmdline_handle_error (loc
, option
, opt
, decoded
->arg
,
1573 decoded
->errors
, lang_mask
))
1576 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
1578 handlers
->wrong_lang_callback (decoded
, lang_mask
);
1582 gcc_assert (!decoded
->errors
);
1584 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1585 loc
, handlers
, false, dc
))
1586 error_at (loc
, "unrecognized command-line option %qs", opt
);
1589 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1590 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1591 location LOC, using diagnostic context DC if not NULL for
1592 diagnostic classification. */
1595 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1596 int opt_index
, HOST_WIDE_INT value
, const char *arg
, int kind
,
1597 location_t loc
, diagnostic_context
*dc
,
1598 HOST_WIDE_INT mask
/* = 0 */)
1600 const struct cl_option
*option
= &cl_options
[opt_index
];
1601 void *flag_var
= option_flag_var (opt_index
, opts
);
1602 void *set_flag_var
= NULL
;
1607 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
&& dc
!= NULL
)
1608 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1610 if (opts_set
!= NULL
)
1611 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1613 switch (option
->var_type
)
1616 if (option
->cl_host_wide_int
)
1618 *(HOST_WIDE_INT
*) flag_var
= value
;
1620 *(HOST_WIDE_INT
*) set_flag_var
= 1;
1624 if (value
> INT_MAX
)
1625 error_at (loc
, "argument to %qs is bigger than %d",
1626 option
->opt_text
, INT_MAX
);
1629 *(int *) flag_var
= value
;
1631 *(int *) set_flag_var
= 1;
1638 if (option
->cl_host_wide_int
)
1640 *(HOST_WIDE_INT
*) flag_var
= value
;
1642 *(HOST_WIDE_INT
*) set_flag_var
= value
;
1646 *(int *) flag_var
= value
;
1648 *(int *) set_flag_var
= value
;
1654 if (option
->cl_host_wide_int
)
1656 *(HOST_WIDE_INT
*) flag_var
= (value
1658 : !option
->var_value
);
1660 *(HOST_WIDE_INT
*) set_flag_var
= 1;
1664 *(int *) flag_var
= (value
1666 : !option
->var_value
);
1668 *(int *) set_flag_var
= 1;
1672 case CLVC_BIT_CLEAR
:
1674 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1676 if (option
->cl_host_wide_int
)
1677 *(HOST_WIDE_INT
*) flag_var
|= option
->var_value
;
1679 *(int *) flag_var
|= option
->var_value
;
1683 if (option
->cl_host_wide_int
)
1684 *(HOST_WIDE_INT
*) flag_var
&= ~option
->var_value
;
1686 *(int *) flag_var
&= ~option
->var_value
;
1690 if (option
->cl_host_wide_int
)
1691 *(HOST_WIDE_INT
*) set_flag_var
|= option
->var_value
;
1693 *(int *) set_flag_var
|= option
->var_value
;
1698 *(const char **) flag_var
= arg
;
1700 *(const char **) set_flag_var
= "";
1705 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1708 e
->set (flag_var
, value
| (e
->get (flag_var
) & ~mask
));
1710 e
->set (flag_var
, value
);
1712 e
->set (set_flag_var
, 1);
1718 vec
<cl_deferred_option
> *v
1719 = (vec
<cl_deferred_option
> *) *(void **) flag_var
;
1720 cl_deferred_option p
= {opt_index
, arg
, value
};
1722 v
= XCNEW (vec
<cl_deferred_option
>);
1724 *(void **) flag_var
= v
;
1726 *(void **) set_flag_var
= v
;
1732 /* Return the address of the flag variable for option OPT_INDEX in
1733 options structure OPTS, or NULL if there is no flag variable. */
1736 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1738 const struct cl_option
*option
= &cl_options
[opt_index
];
1740 if (option
->flag_var_offset
== (unsigned short) -1)
1742 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1745 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1746 or -1 if it isn't a simple on-off switch
1747 (or if the value is unknown, typically set later in target). */
1750 option_enabled (int opt_idx
, unsigned lang_mask
, void *opts
)
1752 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1754 /* A language-specific option can only be considered enabled when it's
1755 valid for the current language. */
1756 if (!(option
->flags
& CL_COMMON
)
1757 && (option
->flags
& CL_LANG_ALL
)
1758 && !(option
->flags
& lang_mask
))
1761 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1762 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1765 switch (option
->var_type
)
1768 if (option
->cl_host_wide_int
)
1770 HOST_WIDE_INT v
= *(HOST_WIDE_INT
*) flag_var
;
1771 return v
!= 0 ? (v
< 0 ? -1 : 1) : 0;
1775 int v
= *(int *) flag_var
;
1776 return v
!= 0 ? (v
< 0 ? -1 : 1) : 0;
1780 if (option
->cl_host_wide_int
)
1781 return *(HOST_WIDE_INT
*) flag_var
== option
->var_value
;
1783 return *(int *) flag_var
== option
->var_value
;
1785 case CLVC_BIT_CLEAR
:
1786 if (option
->cl_host_wide_int
)
1787 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) == 0;
1789 return (*(int *) flag_var
& option
->var_value
) == 0;
1792 if (option
->cl_host_wide_int
)
1793 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) != 0;
1795 return (*(int *) flag_var
& option
->var_value
) != 0;
1798 if (option
->cl_host_wide_int
)
1799 return *(HOST_WIDE_INT
*) flag_var
!= -1;
1801 return *(int *) flag_var
!= -1;
1811 /* Fill STATE with the current state of option OPTION in OPTS. Return
1812 true if there is some state to store. */
1815 get_option_state (struct gcc_options
*opts
, int option
,
1816 struct cl_option_state
*state
)
1818 void *flag_var
= option_flag_var (option
, opts
);
1823 switch (cl_options
[option
].var_type
)
1828 state
->data
= flag_var
;
1829 state
->size
= (cl_options
[option
].cl_host_wide_int
1830 ? sizeof (HOST_WIDE_INT
)
1834 case CLVC_BIT_CLEAR
:
1836 state
->ch
= option_enabled (option
, -1, opts
);
1837 state
->data
= &state
->ch
;
1842 state
->data
= *(const char **) flag_var
;
1843 if (state
->data
== 0)
1845 state
->size
= strlen ((const char *) state
->data
) + 1;
1849 state
->data
= flag_var
;
1850 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1859 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1860 handlers HANDLERS) to have diagnostic kind KIND for option
1861 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1862 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1863 argument of the option for joined options, or NULL otherwise. If IMPLY,
1864 the warning option in question is implied at this point. This is
1865 used by -Werror= and #pragma GCC diagnostic. */
1868 control_warning_option (unsigned int opt_index
, int kind
, const char *arg
,
1869 bool imply
, location_t loc
, unsigned int lang_mask
,
1870 const struct cl_option_handlers
*handlers
,
1871 struct gcc_options
*opts
,
1872 struct gcc_options
*opts_set
,
1873 diagnostic_context
*dc
)
1875 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1877 gcc_assert (!cl_options
[opt_index
].cl_separate_alias
1878 && !cl_options
[opt_index
].cl_negative_alias
);
1879 if (cl_options
[opt_index
].alias_arg
)
1880 arg
= cl_options
[opt_index
].alias_arg
;
1881 opt_index
= cl_options
[opt_index
].alias_target
;
1883 if (opt_index
== OPT_SPECIAL_ignore
|| opt_index
== OPT_SPECIAL_warn_removed
)
1886 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1889 const struct cl_option
*option
= &cl_options
[opt_index
];
1891 /* -Werror=foo implies -Wfoo. */
1892 if (option
->var_type
== CLVC_INTEGER
1893 || option
->var_type
== CLVC_ENUM
1894 || option
->var_type
== CLVC_SIZE
)
1896 HOST_WIDE_INT value
= 1;
1898 if (arg
&& *arg
== '\0' && !option
->cl_missing_ok
)
1901 if ((option
->flags
& CL_JOINED
) && arg
== NULL
)
1903 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1904 CL_ERR_MISSING_ARG
, lang_mask
);
1908 /* If the switch takes an integer argument, convert it. */
1909 if (arg
&& (option
->cl_uinteger
|| option
->cl_host_wide_int
))
1912 value
= *arg
? integral_argument (arg
, &error
,
1913 option
->cl_byte_size
) : 0;
1916 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1917 CL_ERR_UINT_ARG
, lang_mask
);
1922 /* If the switch takes an enumerated argument, convert it. */
1923 if (arg
&& option
->var_type
== CLVC_ENUM
)
1925 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1927 if (enum_arg_to_value (e
->values
, arg
, 0, &value
,
1930 const char *carg
= NULL
;
1932 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
1934 gcc_assert (carg
!= NULL
);
1938 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1939 CL_ERR_ENUM_ARG
, lang_mask
);
1944 handle_generated_option (opts
, opts_set
,
1945 opt_index
, arg
, value
, lang_mask
,
1946 kind
, loc
, handlers
, false, dc
);
1951 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
1952 Store number of arguments into ARGC_P. */
1955 parse_options_from_collect_gcc_options (const char *collect_gcc_options
,
1956 obstack
*argv_obstack
,
1959 char *argv_storage
= xstrdup (collect_gcc_options
);
1962 for (j
= 0, k
= 0; argv_storage
[j
] != '\0'; ++j
)
1964 if (argv_storage
[j
] == '\'')
1966 obstack_ptr_grow (argv_obstack
, &argv_storage
[k
]);
1970 if (argv_storage
[j
] == '\0')
1971 fatal_error (input_location
,
1972 "malformed %<COLLECT_GCC_OPTIONS%>");
1973 else if (startswith (&argv_storage
[j
], "'\\''"))
1975 argv_storage
[k
++] = '\'';
1978 else if (argv_storage
[j
] == '\'')
1981 argv_storage
[k
++] = argv_storage
[j
++];
1984 argv_storage
[k
++] = '\0';
1988 obstack_ptr_grow (argv_obstack
, NULL
);
1989 *argc_p
= obstack_object_size (argv_obstack
) / sizeof (void *) - 1;
1992 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
1996 void prepend_xassembler_to_collect_as_options (const char *collect_as_options
,
1999 obstack opts_obstack
;
2002 obstack_init (&opts_obstack
);
2003 parse_options_from_collect_gcc_options (collect_as_options
,
2004 &opts_obstack
, &opts_count
);
2005 const char **assembler_opts
= XOBFINISH (&opts_obstack
, const char **);
2007 for (int i
= 0; i
< opts_count
; i
++)
2009 obstack_grow (o
, " '-Xassembler' ",
2010 strlen (" '-Xassembler' "));
2011 const char *opt
= assembler_opts
[i
];
2012 obstack_1grow (o
, '\'');
2013 obstack_grow (o
, opt
, strlen (opt
));
2014 obstack_1grow (o
, '\'');