2018-06-21 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / opts-common.c
blob004da73e614bb477106c4159751db8088b288d28
1 /* Command line option handling.
2 Copyright (C) 2006-2018 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
9 version.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "intl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "options.h"
26 #include "diagnostic.h"
27 #include "spellcheck.h"
29 static void prune_options (struct cl_decoded_option **, unsigned int *);
31 /* An option that is undocumented, that takes a joined argument, and
32 that doesn't fit any of the classes of uses (language/common,
33 driver, target) is assumed to be a prefix used to catch
34 e.g. negated options, and stop them from being further shortened to
35 a prefix that could use the negated option as an argument. For
36 example, we want -gno-statement-frontiers to be taken as a negation
37 of -gstatement-frontiers, but without catching the gno- prefix and
38 signaling it's to be used for option remapping, it would end up
39 backtracked to g with no-statemnet-frontiers as the debug level. */
41 static bool
42 remapping_prefix_p (const struct cl_option *opt)
44 return opt->flags & CL_UNDOCUMENTED
45 && opt->flags & CL_JOINED
46 && !(opt->flags & (CL_DRIVER | CL_TARGET | CL_COMMON | CL_LANG_ALL));
49 /* Perform a binary search to find which option the command-line INPUT
50 matches. Returns its index in the option array, and
51 OPT_SPECIAL_unknown on failure.
53 This routine is quite subtle. A normal binary search is not good
54 enough because some options can be suffixed with an argument, and
55 multiple sub-matches can occur, e.g. input of "-pedantic" matching
56 the initial substring of "-pedantic-errors".
58 A more complicated example is -gstabs. It should match "-g" with
59 an argument of "stabs". Suppose, however, that the number and list
60 of switches are such that the binary search tests "-gen-decls"
61 before having tested "-g". This doesn't match, and as "-gen-decls"
62 is less than "-gstabs", it will become the lower bound of the
63 binary search range, and "-g" will never be seen. To resolve this
64 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
65 to "-g" so that failed searches that end between "-gen-decls" and
66 the lexicographically subsequent switch know to go back and see if
67 "-g" causes a match (which it does in this example).
69 This search is done in such a way that the longest match for the
70 front end in question wins. If there is no match for the current
71 front end, the longest match for a different front end is returned
72 (or N_OPTS if none) and the caller emits an error message. */
73 size_t
74 find_opt (const char *input, unsigned int lang_mask)
76 size_t mn, mn_orig, mx, md, opt_len;
77 size_t match_wrong_lang;
78 int comp;
80 mn = 0;
81 mx = cl_options_count;
83 /* Find mn such this lexicographical inequality holds:
84 cl_options[mn] <= input < cl_options[mn + 1]. */
85 while (mx - mn > 1)
87 md = (mn + mx) / 2;
88 opt_len = cl_options[md].opt_len;
89 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
91 if (comp < 0)
92 mx = md;
93 else
94 mn = md;
97 mn_orig = mn;
99 /* This is the switch that is the best match but for a different
100 front end, or OPT_SPECIAL_unknown if there is no match at all. */
101 match_wrong_lang = OPT_SPECIAL_unknown;
103 /* Backtrace the chain of possible matches, returning the longest
104 one, if any, that fits best. With current GCC switches, this
105 loop executes at most twice. */
108 const struct cl_option *opt = &cl_options[mn];
110 /* Is the input either an exact match or a prefix that takes a
111 joined argument? */
112 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
113 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
115 /* If language is OK, return it. */
116 if (opt->flags & lang_mask)
117 return mn;
119 if (remapping_prefix_p (opt))
120 return OPT_SPECIAL_unknown;
122 /* If we haven't remembered a prior match, remember this
123 one. Any prior match is necessarily better. */
124 if (match_wrong_lang == OPT_SPECIAL_unknown)
125 match_wrong_lang = mn;
128 /* Try the next possibility. This is cl_options_count if there
129 are no more. */
130 mn = opt->back_chain;
132 while (mn != cl_options_count);
134 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
136 /* Long options, starting "--", may be abbreviated if the
137 abbreviation is unambiguous. This only applies to options
138 not taking a joined argument, and abbreviations of "--option"
139 are permitted even if there is a variant "--option=". */
140 size_t mnc = mn_orig + 1;
141 size_t cmp_len = strlen (input);
142 while (mnc < cl_options_count
143 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
145 /* Option matching this abbreviation. OK if it is the first
146 match and that does not take a joined argument, or the
147 second match, taking a joined argument and with only '='
148 added to the first match; otherwise considered
149 ambiguous. */
150 if (mnc == mn_orig + 1
151 && !(cl_options[mnc].flags & CL_JOINED))
152 match_wrong_lang = mnc;
153 else if (mnc == mn_orig + 2
154 && match_wrong_lang == mn_orig + 1
155 && (cl_options[mnc].flags & CL_JOINED)
156 && (cl_options[mnc].opt_len
157 == cl_options[mn_orig + 1].opt_len + 1)
158 && strncmp (cl_options[mnc].opt_text + 1,
159 cl_options[mn_orig + 1].opt_text + 1,
160 cl_options[mn_orig + 1].opt_len) == 0)
161 ; /* OK, as long as there are no more matches. */
162 else
163 return OPT_SPECIAL_unknown;
164 mnc++;
168 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
169 return match_wrong_lang;
172 /* If ARG is a non-negative decimal or hexadecimal integer, return its
173 value, otherwise return -1. */
176 integral_argument (const char *arg)
178 const char *p = arg;
180 while (*p && ISDIGIT (*p))
181 p++;
183 if (*p == '\0')
184 return atoi (arg);
186 /* It wasn't a decimal number - try hexadecimal. */
187 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
189 p = arg + 2;
190 while (*p && ISXDIGIT (*p))
191 p++;
193 if (p != arg + 2 && *p == '\0')
194 return strtol (arg, NULL, 16);
197 return -1;
200 /* Return whether OPTION is OK for the language given by
201 LANG_MASK. */
202 static bool
203 option_ok_for_language (const struct cl_option *option,
204 unsigned int lang_mask)
206 if (!(option->flags & lang_mask))
207 return false;
208 else if ((option->flags & CL_TARGET)
209 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
210 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
211 /* Complain for target flag language mismatches if any languages
212 are specified. */
213 return false;
214 return true;
217 /* Return whether ENUM_ARG is OK for the language given by
218 LANG_MASK. */
220 static bool
221 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
222 unsigned int lang_mask)
224 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
227 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
228 storing the value in *VALUE if found, and returning false without
229 modifying *VALUE if not found. */
231 static bool
232 enum_arg_to_value (const struct cl_enum_arg *enum_args,
233 const char *arg, int *value, unsigned int lang_mask)
235 unsigned int i;
237 for (i = 0; enum_args[i].arg != NULL; i++)
238 if (strcmp (arg, enum_args[i].arg) == 0
239 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
241 *value = enum_args[i].value;
242 return true;
245 return false;
248 /* Look up ARG in the enum used by option OPT_INDEX for language
249 LANG_MASK, returning true and storing the value in *VALUE if found,
250 and returning false without modifying *VALUE if not found. */
252 bool
253 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
254 unsigned int lang_mask)
256 const struct cl_option *option = &cl_options[opt_index];
258 gcc_assert (option->var_type == CLVC_ENUM);
260 return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
261 value, lang_mask);
264 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
265 corresponding string in *ARGP, returning true if the found string
266 was marked as canonical, false otherwise. If VALUE is not found
267 (which may be the case for uninitialized values if the relevant
268 option has not been passed), set *ARGP to NULL and return
269 false. */
271 bool
272 enum_value_to_arg (const struct cl_enum_arg *enum_args,
273 const char **argp, int value, unsigned int lang_mask)
275 unsigned int i;
277 for (i = 0; enum_args[i].arg != NULL; i++)
278 if (enum_args[i].value == value
279 && (enum_args[i].flags & CL_ENUM_CANONICAL)
280 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
282 *argp = enum_args[i].arg;
283 return true;
286 for (i = 0; enum_args[i].arg != NULL; i++)
287 if (enum_args[i].value == value
288 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
290 *argp = enum_args[i].arg;
291 return false;
294 *argp = NULL;
295 return false;
298 /* Fill in the canonical option part of *DECODED with an option
299 described by OPT_INDEX, ARG and VALUE. */
301 static void
302 generate_canonical_option (size_t opt_index, const char *arg, int value,
303 struct cl_decoded_option *decoded)
305 const struct cl_option *option = &cl_options[opt_index];
306 const char *opt_text = option->opt_text;
308 if (value == 0
309 && !option->cl_reject_negative
310 && (opt_text[1] == 'W' || opt_text[1] == 'f'
311 || opt_text[1] == 'g' || opt_text[1] == 'm'))
313 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
314 t[0] = '-';
315 t[1] = opt_text[1];
316 t[2] = 'n';
317 t[3] = 'o';
318 t[4] = '-';
319 memcpy (t + 5, opt_text + 2, option->opt_len);
320 opt_text = t;
323 decoded->canonical_option[2] = NULL;
324 decoded->canonical_option[3] = NULL;
326 if (arg)
328 if ((option->flags & CL_SEPARATE)
329 && !option->cl_separate_alias)
331 decoded->canonical_option[0] = opt_text;
332 decoded->canonical_option[1] = arg;
333 decoded->canonical_option_num_elements = 2;
335 else
337 gcc_assert (option->flags & CL_JOINED);
338 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
339 decoded->canonical_option[1] = NULL;
340 decoded->canonical_option_num_elements = 1;
343 else
345 decoded->canonical_option[0] = opt_text;
346 decoded->canonical_option[1] = NULL;
347 decoded->canonical_option_num_elements = 1;
351 /* Structure describing mappings from options on the command line to
352 options to look up with find_opt. */
353 struct option_map
355 /* Prefix of the option on the command line. */
356 const char *opt0;
357 /* If two argv elements are considered to be merged into one option,
358 prefix for the second element, otherwise NULL. */
359 const char *opt1;
360 /* The new prefix to map to. */
361 const char *new_prefix;
362 /* Whether at least one character is needed following opt1 or opt0
363 for this mapping to be used. (--optimize= is valid for -O, but
364 --warn- is not valid for -W.) */
365 bool another_char_needed;
366 /* Whether the original option is a negated form of the option
367 resulting from this map. */
368 bool negated;
370 static const struct option_map option_map[] =
372 { "-Wno-", NULL, "-W", false, true },
373 { "-fno-", NULL, "-f", false, true },
374 { "-gno-", NULL, "-g", false, true },
375 { "-mno-", NULL, "-m", false, true },
376 { "--debug=", NULL, "-g", false, false },
377 { "--machine-", NULL, "-m", true, false },
378 { "--machine-no-", NULL, "-m", false, true },
379 { "--machine=", NULL, "-m", false, false },
380 { "--machine=no-", NULL, "-m", false, true },
381 { "--machine", "", "-m", false, false },
382 { "--machine", "no-", "-m", false, true },
383 { "--optimize=", NULL, "-O", false, false },
384 { "--std=", NULL, "-std=", false, false },
385 { "--std", "", "-std=", false, false },
386 { "--warn-", NULL, "-W", true, false },
387 { "--warn-no-", NULL, "-W", false, true },
388 { "--", NULL, "-f", true, false },
389 { "--no-", NULL, "-f", false, true }
392 /* Helper function for gcc.c's driver::suggest_option, for populating the
393 vec of suggestions for misspelled options.
395 option_map above provides various prefixes for spelling command-line
396 options, which decode_cmdline_option uses to map spellings of options
397 to specific options. We want to do the reverse: to find all the ways
398 that a user could validly spell an option.
400 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
401 of its valid variant spellings to CANDIDATES, each without a leading
402 dash.
404 For example, given "-Wabi-tag", the following are added to CANDIDATES:
405 "Wabi-tag"
406 "Wno-abi-tag"
407 "-warn-abi-tag"
408 "-warn-no-abi-tag".
410 The added strings must be freed using free. */
412 void
413 add_misspelling_candidates (auto_vec<char *> *candidates,
414 const struct cl_option *option,
415 const char *opt_text)
417 gcc_assert (candidates);
418 gcc_assert (option);
419 gcc_assert (opt_text);
420 if (remapping_prefix_p (option))
421 return;
422 candidates->safe_push (xstrdup (opt_text + 1));
423 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
425 const char *opt0 = option_map[i].opt0;
426 const char *new_prefix = option_map[i].new_prefix;
427 size_t new_prefix_len = strlen (new_prefix);
429 if (option->cl_reject_negative && option_map[i].negated)
430 continue;
432 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
434 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
435 NULL);
436 candidates->safe_push (alternative);
441 /* Decode the switch beginning at ARGV for the language indicated by
442 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
443 the structure *DECODED. Returns the number of switches
444 consumed. */
446 static unsigned int
447 decode_cmdline_option (const char **argv, unsigned int lang_mask,
448 struct cl_decoded_option *decoded)
450 size_t opt_index;
451 const char *arg = 0;
452 int value = 1;
453 unsigned int result = 1, i, extra_args, separate_args = 0;
454 int adjust_len = 0;
455 size_t total_len;
456 char *p;
457 const struct cl_option *option;
458 int errors = 0;
459 const char *warn_message = NULL;
460 bool separate_arg_flag;
461 bool joined_arg_flag;
462 bool have_separate_arg = false;
464 extra_args = 0;
466 opt_index = find_opt (argv[0] + 1, lang_mask);
467 i = 0;
468 while (opt_index == OPT_SPECIAL_unknown
469 && i < ARRAY_SIZE (option_map))
471 const char *opt0 = option_map[i].opt0;
472 const char *opt1 = option_map[i].opt1;
473 const char *new_prefix = option_map[i].new_prefix;
474 bool another_char_needed = option_map[i].another_char_needed;
475 size_t opt0_len = strlen (opt0);
476 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
477 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
478 size_t new_prefix_len = strlen (new_prefix);
480 extra_args = (opt1 == NULL ? 0 : 1);
481 value = !option_map[i].negated;
483 if (strncmp (argv[0], opt0, opt0_len) == 0
484 && (opt1 == NULL
485 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
486 && (!another_char_needed
487 || argv[extra_args][optn_len] != 0))
489 size_t arglen = strlen (argv[extra_args]);
490 char *dup;
492 adjust_len = (int) optn_len - (int) new_prefix_len;
493 dup = XNEWVEC (char, arglen + 1 - adjust_len);
494 memcpy (dup, new_prefix, new_prefix_len);
495 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
496 arglen - optn_len + 1);
497 opt_index = find_opt (dup + 1, lang_mask);
498 free (dup);
500 i++;
503 if (opt_index == OPT_SPECIAL_unknown)
505 arg = argv[0];
506 extra_args = 0;
507 value = 1;
508 goto done;
511 option = &cl_options[opt_index];
513 /* Reject negative form of switches that don't take negatives as
514 unrecognized. */
515 if (!value && option->cl_reject_negative)
517 opt_index = OPT_SPECIAL_unknown;
518 errors |= CL_ERR_NEGATIVE;
519 arg = argv[0];
520 goto done;
523 result = extra_args + 1;
524 warn_message = option->warn_message;
526 /* Check to see if the option is disabled for this configuration. */
527 if (option->cl_disabled)
528 errors |= CL_ERR_DISABLED;
530 /* Determine whether there may be a separate argument based on
531 whether this option is being processed for the driver, and, if
532 so, how many such arguments. */
533 separate_arg_flag = ((option->flags & CL_SEPARATE)
534 && !(option->cl_no_driver_arg
535 && (lang_mask & CL_DRIVER)));
536 separate_args = (separate_arg_flag
537 ? option->cl_separate_nargs + 1
538 : 0);
539 joined_arg_flag = (option->flags & CL_JOINED) != 0;
541 /* Sort out any argument the switch takes. */
542 if (joined_arg_flag)
544 /* Have arg point to the original switch. This is because
545 some code, such as disable_builtin_function, expects its
546 argument to be persistent until the program exits. */
547 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
549 if (*arg == '\0' && !option->cl_missing_ok)
551 if (separate_arg_flag)
553 arg = argv[extra_args + 1];
554 result = extra_args + 2;
555 if (arg == NULL)
556 result = extra_args + 1;
557 else
558 have_separate_arg = true;
560 else
561 /* Missing argument. */
562 arg = NULL;
565 else if (separate_arg_flag)
567 arg = argv[extra_args + 1];
568 for (i = 0; i < separate_args; i++)
569 if (argv[extra_args + 1 + i] == NULL)
571 errors |= CL_ERR_MISSING_ARG;
572 break;
574 result = extra_args + 1 + i;
575 if (arg != NULL)
576 have_separate_arg = true;
579 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
580 errors |= CL_ERR_MISSING_ARG;
582 /* Is this option an alias (or an ignored option, marked as an alias
583 of OPT_SPECIAL_ignore)? */
584 if (option->alias_target != N_OPTS
585 && (!option->cl_separate_alias || have_separate_arg))
587 size_t new_opt_index = option->alias_target;
589 if (new_opt_index == OPT_SPECIAL_ignore)
591 gcc_assert (option->alias_arg == NULL);
592 gcc_assert (option->neg_alias_arg == NULL);
593 opt_index = new_opt_index;
594 arg = NULL;
595 value = 1;
597 else
599 const struct cl_option *new_option = &cl_options[new_opt_index];
601 /* The new option must not be an alias itself. */
602 gcc_assert (new_option->alias_target == N_OPTS
603 || new_option->cl_separate_alias);
605 if (option->neg_alias_arg)
607 gcc_assert (option->alias_arg != NULL);
608 gcc_assert (arg == NULL);
609 gcc_assert (!option->cl_negative_alias);
610 if (value)
611 arg = option->alias_arg;
612 else
613 arg = option->neg_alias_arg;
614 value = 1;
616 else if (option->alias_arg)
618 gcc_assert (value == 1);
619 gcc_assert (arg == NULL);
620 gcc_assert (!option->cl_negative_alias);
621 arg = option->alias_arg;
624 if (option->cl_negative_alias)
625 value = !value;
627 opt_index = new_opt_index;
628 option = new_option;
630 if (value == 0)
631 gcc_assert (!option->cl_reject_negative);
633 /* Recompute what arguments are allowed. */
634 separate_arg_flag = ((option->flags & CL_SEPARATE)
635 && !(option->cl_no_driver_arg
636 && (lang_mask & CL_DRIVER)));
637 joined_arg_flag = (option->flags & CL_JOINED) != 0;
639 if (separate_args > 1 || option->cl_separate_nargs)
640 gcc_assert (separate_args
641 == (unsigned int) option->cl_separate_nargs + 1);
643 if (!(errors & CL_ERR_MISSING_ARG))
645 if (separate_arg_flag || joined_arg_flag)
647 if (option->cl_missing_ok && arg == NULL)
648 arg = "";
649 gcc_assert (arg != NULL);
651 else
652 gcc_assert (arg == NULL);
655 /* Recheck for warnings and disabled options. */
656 if (option->warn_message)
658 gcc_assert (warn_message == NULL);
659 warn_message = option->warn_message;
661 if (option->cl_disabled)
662 errors |= CL_ERR_DISABLED;
666 /* Check if this is a switch for a different front end. */
667 if (!option_ok_for_language (option, lang_mask))
668 errors |= CL_ERR_WRONG_LANG;
670 /* Mark all deprecated options. */
671 if (option->cl_deprecated)
672 errors |= CL_ERR_DEPRECATED;
674 /* Convert the argument to lowercase if appropriate. */
675 if (arg && option->cl_tolower)
677 size_t j;
678 size_t len = strlen (arg);
679 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
681 for (j = 0; j < len; j++)
682 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
683 arg_lower[len] = 0;
684 arg = arg_lower;
687 /* If the switch takes an integer, convert it. */
688 if (arg && option->cl_uinteger)
690 value = integral_argument (arg);
691 if (value == -1)
692 errors |= CL_ERR_UINT_ARG;
694 /* Reject value out of a range. */
695 if (option->range_max != -1
696 && (value < option->range_min || value > option->range_max))
697 errors |= CL_ERR_INT_RANGE_ARG;
700 /* If the switch takes an enumerated argument, convert it. */
701 if (arg && (option->var_type == CLVC_ENUM))
703 const struct cl_enum *e = &cl_enums[option->var_enum];
705 gcc_assert (value == 1);
706 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
708 const char *carg = NULL;
710 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
711 arg = carg;
712 gcc_assert (carg != NULL);
714 else
715 errors |= CL_ERR_ENUM_ARG;
718 done:
719 decoded->opt_index = opt_index;
720 decoded->arg = arg;
721 decoded->value = value;
722 decoded->errors = errors;
723 decoded->warn_message = warn_message;
725 if (opt_index == OPT_SPECIAL_unknown)
726 gcc_assert (result == 1);
728 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
729 decoded->canonical_option_num_elements = result;
730 total_len = 0;
731 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
733 if (i < result)
735 size_t len;
736 if (opt_index == OPT_SPECIAL_unknown)
737 decoded->canonical_option[i] = argv[i];
738 else
739 decoded->canonical_option[i] = NULL;
740 len = strlen (argv[i]);
741 /* If the argument is an empty string, we will print it as "" in
742 orig_option_with_args_text. */
743 total_len += (len != 0 ? len : 2) + 1;
745 else
746 decoded->canonical_option[i] = NULL;
748 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
750 generate_canonical_option (opt_index, arg, value, decoded);
751 if (separate_args > 1)
753 for (i = 0; i < separate_args; i++)
755 if (argv[extra_args + 1 + i] == NULL)
756 break;
757 else
758 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
760 gcc_assert (result == 1 + i);
761 decoded->canonical_option_num_elements = result;
764 decoded->orig_option_with_args_text
765 = p = XOBNEWVEC (&opts_obstack, char, total_len);
766 for (i = 0; i < result; i++)
768 size_t len = strlen (argv[i]);
770 /* Print the empty string verbally. */
771 if (len == 0)
773 *p++ = '"';
774 *p++ = '"';
776 else
777 memcpy (p, argv[i], len);
778 p += len;
779 if (i == result - 1)
780 *p++ = 0;
781 else
782 *p++ = ' ';
785 return result;
788 /* Obstack for option strings. */
790 struct obstack opts_obstack;
792 /* Like libiberty concat, but allocate using opts_obstack. */
794 char *
795 opts_concat (const char *first, ...)
797 char *newstr, *end;
798 size_t length = 0;
799 const char *arg;
800 va_list ap;
802 /* First compute the size of the result and get sufficient memory. */
803 va_start (ap, first);
804 for (arg = first; arg; arg = va_arg (ap, const char *))
805 length += strlen (arg);
806 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
807 va_end (ap);
809 /* Now copy the individual pieces to the result string. */
810 va_start (ap, first);
811 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
813 length = strlen (arg);
814 memcpy (end, arg, length);
815 end += length;
817 *end = '\0';
818 va_end (ap);
819 return newstr;
822 /* Decode command-line options (ARGC and ARGV being the arguments of
823 main) into an array, setting *DECODED_OPTIONS to a pointer to that
824 array and *DECODED_OPTIONS_COUNT to the number of entries in the
825 array. The first entry in the array is always one for the program
826 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
827 flags applicable for decoding (including CL_COMMON and CL_TARGET if
828 those options should be considered applicable). Do not produce any
829 diagnostics or set state outside of these variables. */
831 void
832 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
833 unsigned int lang_mask,
834 struct cl_decoded_option **decoded_options,
835 unsigned int *decoded_options_count)
837 unsigned int n, i;
838 struct cl_decoded_option *opt_array;
839 unsigned int num_decoded_options;
841 opt_array = XNEWVEC (struct cl_decoded_option, argc);
843 opt_array[0].opt_index = OPT_SPECIAL_program_name;
844 opt_array[0].warn_message = NULL;
845 opt_array[0].arg = argv[0];
846 opt_array[0].orig_option_with_args_text = argv[0];
847 opt_array[0].canonical_option_num_elements = 1;
848 opt_array[0].canonical_option[0] = argv[0];
849 opt_array[0].canonical_option[1] = NULL;
850 opt_array[0].canonical_option[2] = NULL;
851 opt_array[0].canonical_option[3] = NULL;
852 opt_array[0].value = 1;
853 opt_array[0].errors = 0;
854 num_decoded_options = 1;
856 for (i = 1; i < argc; i += n)
858 const char *opt = argv[i];
860 /* Interpret "-" or a non-switch as a file name. */
861 if (opt[0] != '-' || opt[1] == '\0')
863 generate_option_input_file (opt, &opt_array[num_decoded_options]);
864 num_decoded_options++;
865 n = 1;
866 continue;
869 n = decode_cmdline_option (argv + i, lang_mask,
870 &opt_array[num_decoded_options]);
871 num_decoded_options++;
874 *decoded_options = opt_array;
875 *decoded_options_count = num_decoded_options;
876 prune_options (decoded_options, decoded_options_count);
879 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
880 next one is the same as ORIG_NEXT_OPT_IDX. */
882 static bool
883 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
885 /* An option can be canceled by the same option or an option with
886 Negative. */
887 if (cl_options [next_opt_idx].neg_index == opt_idx)
888 return true;
890 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
891 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
892 orig_next_opt_idx);
894 return false;
897 /* Filter out options canceled by the ones after them. */
899 static void
900 prune_options (struct cl_decoded_option **decoded_options,
901 unsigned int *decoded_options_count)
903 unsigned int old_decoded_options_count = *decoded_options_count;
904 struct cl_decoded_option *old_decoded_options = *decoded_options;
905 unsigned int new_decoded_options_count;
906 struct cl_decoded_option *new_decoded_options
907 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
908 unsigned int i;
909 const struct cl_option *option;
910 unsigned int fdiagnostics_color_idx = 0;
912 /* Remove arguments which are negated by others after them. */
913 new_decoded_options_count = 0;
914 for (i = 0; i < old_decoded_options_count; i++)
916 unsigned int j, opt_idx, next_opt_idx;
918 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
919 goto keep;
921 opt_idx = old_decoded_options[i].opt_index;
922 switch (opt_idx)
924 case OPT_SPECIAL_unknown:
925 case OPT_SPECIAL_ignore:
926 case OPT_SPECIAL_program_name:
927 case OPT_SPECIAL_input_file:
928 goto keep;
930 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
931 case OPT_fdiagnostics_color_:
932 fdiagnostics_color_idx = i;
933 continue;
935 default:
936 gcc_assert (opt_idx < cl_options_count);
937 option = &cl_options[opt_idx];
938 if (option->neg_index < 0)
939 goto keep;
941 /* Skip joined switches. */
942 if ((option->flags & CL_JOINED))
943 goto keep;
945 for (j = i + 1; j < old_decoded_options_count; j++)
947 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
948 continue;
949 next_opt_idx = old_decoded_options[j].opt_index;
950 if (next_opt_idx >= cl_options_count)
951 continue;
952 if (cl_options[next_opt_idx].neg_index < 0)
953 continue;
954 if ((cl_options[next_opt_idx].flags & CL_JOINED))
955 continue;
956 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
957 break;
959 if (j == old_decoded_options_count)
961 keep:
962 new_decoded_options[new_decoded_options_count]
963 = old_decoded_options[i];
964 new_decoded_options_count++;
966 break;
970 if (fdiagnostics_color_idx >= 1)
972 /* We put the last -fdiagnostics-color= at the first position
973 after argv[0] so it can take effect immediately. */
974 memmove (new_decoded_options + 2, new_decoded_options + 1,
975 sizeof (struct cl_decoded_option)
976 * (new_decoded_options_count - 1));
977 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
978 new_decoded_options_count++;
981 free (old_decoded_options);
982 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
983 new_decoded_options,
984 new_decoded_options_count);
985 *decoded_options = new_decoded_options;
986 *decoded_options_count = new_decoded_options_count;
989 /* Handle option DECODED for the language indicated by LANG_MASK,
990 using the handlers in HANDLERS and setting fields in OPTS and
991 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
992 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
993 option for options from the source file, UNKNOWN_LOCATION
994 otherwise. GENERATED_P is true for an option generated as part of
995 processing another option or otherwise generated internally, false
996 for one explicitly passed by the user. control_warning_option
997 generated options are considered explicitly passed by the user.
998 Returns false if the switch was invalid. DC is the diagnostic
999 context for options affecting diagnostics state, or NULL. */
1001 static bool
1002 handle_option (struct gcc_options *opts,
1003 struct gcc_options *opts_set,
1004 const struct cl_decoded_option *decoded,
1005 unsigned int lang_mask, int kind, location_t loc,
1006 const struct cl_option_handlers *handlers,
1007 bool generated_p, diagnostic_context *dc)
1009 size_t opt_index = decoded->opt_index;
1010 const char *arg = decoded->arg;
1011 int value = decoded->value;
1012 const struct cl_option *option = &cl_options[opt_index];
1013 void *flag_var = option_flag_var (opt_index, opts);
1014 size_t i;
1016 if (flag_var)
1017 set_option (opts, (generated_p ? NULL : opts_set),
1018 opt_index, value, arg, kind, loc, dc);
1020 for (i = 0; i < handlers->num_handlers; i++)
1021 if (option->flags & handlers->handlers[i].mask)
1023 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1024 lang_mask, kind, loc,
1025 handlers, dc,
1026 handlers->target_option_override_hook))
1027 return false;
1030 return true;
1033 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1034 option instead of DECODED. This is used for callbacks when one
1035 option implies another instead of an option being decoded from the
1036 command line. */
1038 bool
1039 handle_generated_option (struct gcc_options *opts,
1040 struct gcc_options *opts_set,
1041 size_t opt_index, const char *arg, int value,
1042 unsigned int lang_mask, int kind, location_t loc,
1043 const struct cl_option_handlers *handlers,
1044 bool generated_p, diagnostic_context *dc)
1046 struct cl_decoded_option decoded;
1048 generate_option (opt_index, arg, value, lang_mask, &decoded);
1049 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1050 handlers, generated_p, dc);
1053 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1054 VALUE for a front end using LANG_MASK. This is used when the
1055 compiler generates options internally. */
1057 void
1058 generate_option (size_t opt_index, const char *arg, int value,
1059 unsigned int lang_mask, struct cl_decoded_option *decoded)
1061 const struct cl_option *option = &cl_options[opt_index];
1063 decoded->opt_index = opt_index;
1064 decoded->warn_message = NULL;
1065 decoded->arg = arg;
1066 decoded->value = value;
1067 decoded->errors = (option_ok_for_language (option, lang_mask)
1069 : CL_ERR_WRONG_LANG);
1071 generate_canonical_option (opt_index, arg, value, decoded);
1072 switch (decoded->canonical_option_num_elements)
1074 case 1:
1075 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1076 break;
1078 case 2:
1079 decoded->orig_option_with_args_text
1080 = opts_concat (decoded->canonical_option[0], " ",
1081 decoded->canonical_option[1], NULL);
1082 break;
1084 default:
1085 gcc_unreachable ();
1089 /* Fill in *DECODED with an option for input file FILE. */
1091 void
1092 generate_option_input_file (const char *file,
1093 struct cl_decoded_option *decoded)
1095 decoded->opt_index = OPT_SPECIAL_input_file;
1096 decoded->warn_message = NULL;
1097 decoded->arg = file;
1098 decoded->orig_option_with_args_text = file;
1099 decoded->canonical_option_num_elements = 1;
1100 decoded->canonical_option[0] = file;
1101 decoded->canonical_option[1] = NULL;
1102 decoded->canonical_option[2] = NULL;
1103 decoded->canonical_option[3] = NULL;
1104 decoded->value = 1;
1105 decoded->errors = 0;
1108 /* Helper function for listing valid choices and hint for misspelled
1109 value. CANDIDATES is a vector containing all valid strings,
1110 STR is set to a heap allocated string that contains all those
1111 strings concatenated, separated by spaces, and the return value
1112 is the closest string from those to ARG, or NULL if nothing is
1113 close enough. Callers should XDELETEVEC (STR) after using it
1114 to avoid memory leaks. */
1116 const char *
1117 candidates_list_and_hint (const char *arg, char *&str,
1118 const auto_vec <const char *> &candidates)
1120 size_t len = 0;
1121 int i;
1122 const char *candidate;
1123 char *p;
1125 FOR_EACH_VEC_ELT (candidates, i, candidate)
1126 len += strlen (candidate) + 1;
1128 str = p = XNEWVEC (char, len);
1129 FOR_EACH_VEC_ELT (candidates, i, candidate)
1131 len = strlen (candidate);
1132 memcpy (p, candidate, len);
1133 p[len] = ' ';
1134 p += len + 1;
1136 p[-1] = '\0';
1137 return find_closest_string (arg, &candidates);
1140 /* Perform diagnostics for read_cmdline_option and control_warning_option
1141 functions. Returns true if an error has been diagnosed.
1142 LOC and LANG_MASK arguments like in read_cmdline_option.
1143 OPTION is the option to report diagnostics for, OPT the name
1144 of the option as text, ARG the argument of the option (for joined
1145 options), ERRORS is bitmask of CL_ERR_* values. */
1147 static bool
1148 cmdline_handle_error (location_t loc, const struct cl_option *option,
1149 const char *opt, const char *arg, int errors,
1150 unsigned int lang_mask)
1152 if (errors & CL_ERR_DISABLED)
1154 error_at (loc, "command line option %qs"
1155 " is not supported by this configuration", opt);
1156 return true;
1159 if (errors & CL_ERR_MISSING_ARG)
1161 if (option->missing_argument_error)
1162 error_at (loc, option->missing_argument_error, opt);
1163 else
1164 error_at (loc, "missing argument to %qs", opt);
1165 return true;
1168 if (errors & CL_ERR_UINT_ARG)
1170 error_at (loc, "argument to %qs should be a non-negative integer",
1171 option->opt_text);
1172 return true;
1175 if (errors & CL_ERR_INT_RANGE_ARG)
1177 error_at (loc, "argument to %qs is not between %d and %d",
1178 option->opt_text, option->range_min, option->range_max);
1179 return true;
1182 if (errors & CL_ERR_ENUM_ARG)
1184 const struct cl_enum *e = &cl_enums[option->var_enum];
1185 unsigned int i;
1186 char *s;
1188 if (e->unknown_error)
1189 error_at (loc, e->unknown_error, arg);
1190 else
1191 error_at (loc, "unrecognized argument in option %qs", opt);
1193 auto_vec <const char *> candidates;
1194 for (i = 0; e->values[i].arg != NULL; i++)
1196 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1197 continue;
1198 candidates.safe_push (e->values[i].arg);
1200 const char *hint = candidates_list_and_hint (arg, s, candidates);
1201 if (hint)
1202 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1203 option->opt_text, s, hint);
1204 else
1205 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1206 XDELETEVEC (s);
1208 return true;
1211 return false;
1214 /* Handle the switch DECODED (location LOC) for the language indicated
1215 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1216 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1217 diagnostic options. */
1219 void
1220 read_cmdline_option (struct gcc_options *opts,
1221 struct gcc_options *opts_set,
1222 struct cl_decoded_option *decoded,
1223 location_t loc,
1224 unsigned int lang_mask,
1225 const struct cl_option_handlers *handlers,
1226 diagnostic_context *dc)
1228 const struct cl_option *option;
1229 const char *opt = decoded->orig_option_with_args_text;
1231 if (decoded->warn_message)
1232 warning_at (loc, 0, decoded->warn_message, opt);
1234 if (decoded->opt_index == OPT_SPECIAL_unknown)
1236 if (handlers->unknown_option_callback (decoded))
1237 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1238 return;
1241 if (decoded->opt_index == OPT_SPECIAL_ignore)
1242 return;
1244 option = &cl_options[decoded->opt_index];
1246 if (decoded->errors
1247 && cmdline_handle_error (loc, option, opt, decoded->arg,
1248 decoded->errors, lang_mask))
1249 return;
1251 if (decoded->errors & CL_ERR_WRONG_LANG)
1253 handlers->wrong_lang_callback (decoded, lang_mask);
1254 return;
1257 if (decoded->errors & CL_ERR_DEPRECATED)
1259 warning_at (loc, 0, "deprecated command line option %qs", opt);
1260 return;
1263 gcc_assert (!decoded->errors);
1265 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1266 loc, handlers, false, dc))
1267 error_at (loc, "unrecognized command line option %qs", opt);
1270 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1271 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1272 location LOC, using diagnostic context DC if not NULL for
1273 diagnostic classification. */
1275 void
1276 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1277 int opt_index, int value, const char *arg, int kind,
1278 location_t loc, diagnostic_context *dc)
1280 const struct cl_option *option = &cl_options[opt_index];
1281 void *flag_var = option_flag_var (opt_index, opts);
1282 void *set_flag_var = NULL;
1284 if (!flag_var)
1285 return;
1287 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1288 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1290 if (opts_set != NULL)
1291 set_flag_var = option_flag_var (opt_index, opts_set);
1293 switch (option->var_type)
1295 case CLVC_BOOLEAN:
1296 *(int *) flag_var = value;
1297 if (set_flag_var)
1298 *(int *) set_flag_var = 1;
1299 break;
1301 case CLVC_EQUAL:
1302 if (option->cl_host_wide_int)
1303 *(HOST_WIDE_INT *) flag_var = (value
1304 ? option->var_value
1305 : !option->var_value);
1306 else
1307 *(int *) flag_var = (value
1308 ? option->var_value
1309 : !option->var_value);
1310 if (set_flag_var)
1311 *(int *) set_flag_var = 1;
1312 break;
1314 case CLVC_BIT_CLEAR:
1315 case CLVC_BIT_SET:
1316 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1318 if (option->cl_host_wide_int)
1319 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1320 else
1321 *(int *) flag_var |= option->var_value;
1323 else
1325 if (option->cl_host_wide_int)
1326 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1327 else
1328 *(int *) flag_var &= ~option->var_value;
1330 if (set_flag_var)
1332 if (option->cl_host_wide_int)
1333 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1334 else
1335 *(int *) set_flag_var |= option->var_value;
1337 break;
1339 case CLVC_STRING:
1340 *(const char **) flag_var = arg;
1341 if (set_flag_var)
1342 *(const char **) set_flag_var = "";
1343 break;
1345 case CLVC_ENUM:
1347 const struct cl_enum *e = &cl_enums[option->var_enum];
1349 e->set (flag_var, value);
1350 if (set_flag_var)
1351 e->set (set_flag_var, 1);
1353 break;
1355 case CLVC_DEFER:
1357 vec<cl_deferred_option> *v
1358 = (vec<cl_deferred_option> *) *(void **) flag_var;
1359 cl_deferred_option p = {opt_index, arg, value};
1360 if (!v)
1361 v = XCNEW (vec<cl_deferred_option>);
1362 v->safe_push (p);
1363 *(void **) flag_var = v;
1364 if (set_flag_var)
1365 *(void **) set_flag_var = v;
1367 break;
1371 /* Return the address of the flag variable for option OPT_INDEX in
1372 options structure OPTS, or NULL if there is no flag variable. */
1374 void *
1375 option_flag_var (int opt_index, struct gcc_options *opts)
1377 const struct cl_option *option = &cl_options[opt_index];
1379 if (option->flag_var_offset == (unsigned short) -1)
1380 return NULL;
1381 return (void *)(((char *) opts) + option->flag_var_offset);
1384 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1385 or -1 if it isn't a simple on-off switch. */
1388 option_enabled (int opt_idx, void *opts)
1390 const struct cl_option *option = &(cl_options[opt_idx]);
1391 struct gcc_options *optsg = (struct gcc_options *) opts;
1392 void *flag_var = option_flag_var (opt_idx, optsg);
1394 if (flag_var)
1395 switch (option->var_type)
1397 case CLVC_BOOLEAN:
1398 return *(int *) flag_var != 0;
1400 case CLVC_EQUAL:
1401 if (option->cl_host_wide_int)
1402 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1403 else
1404 return *(int *) flag_var == option->var_value;
1406 case CLVC_BIT_CLEAR:
1407 if (option->cl_host_wide_int)
1408 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1409 else
1410 return (*(int *) flag_var & option->var_value) == 0;
1412 case CLVC_BIT_SET:
1413 if (option->cl_host_wide_int)
1414 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1415 else
1416 return (*(int *) flag_var & option->var_value) != 0;
1418 case CLVC_STRING:
1419 case CLVC_ENUM:
1420 case CLVC_DEFER:
1421 break;
1423 return -1;
1426 /* Fill STATE with the current state of option OPTION in OPTS. Return
1427 true if there is some state to store. */
1429 bool
1430 get_option_state (struct gcc_options *opts, int option,
1431 struct cl_option_state *state)
1433 void *flag_var = option_flag_var (option, opts);
1435 if (flag_var == 0)
1436 return false;
1438 switch (cl_options[option].var_type)
1440 case CLVC_BOOLEAN:
1441 case CLVC_EQUAL:
1442 state->data = flag_var;
1443 state->size = (cl_options[option].cl_host_wide_int
1444 ? sizeof (HOST_WIDE_INT)
1445 : sizeof (int));
1446 break;
1448 case CLVC_BIT_CLEAR:
1449 case CLVC_BIT_SET:
1450 state->ch = option_enabled (option, opts);
1451 state->data = &state->ch;
1452 state->size = 1;
1453 break;
1455 case CLVC_STRING:
1456 state->data = *(const char **) flag_var;
1457 if (state->data == 0)
1458 state->data = "";
1459 state->size = strlen ((const char *) state->data) + 1;
1460 break;
1462 case CLVC_ENUM:
1463 state->data = flag_var;
1464 state->size = cl_enums[cl_options[option].var_enum].var_size;
1465 break;
1467 case CLVC_DEFER:
1468 return false;
1470 return true;
1473 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1474 handlers HANDLERS) to have diagnostic kind KIND for option
1475 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1476 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1477 argument of the option for joined options, or NULL otherwise. If IMPLY,
1478 the warning option in question is implied at this point. This is
1479 used by -Werror= and #pragma GCC diagnostic. */
1481 void
1482 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1483 bool imply, location_t loc, unsigned int lang_mask,
1484 const struct cl_option_handlers *handlers,
1485 struct gcc_options *opts,
1486 struct gcc_options *opts_set,
1487 diagnostic_context *dc)
1489 if (cl_options[opt_index].alias_target != N_OPTS)
1491 gcc_assert (!cl_options[opt_index].cl_separate_alias
1492 && !cl_options[opt_index].cl_negative_alias);
1493 if (cl_options[opt_index].alias_arg)
1494 arg = cl_options[opt_index].alias_arg;
1495 opt_index = cl_options[opt_index].alias_target;
1497 if (opt_index == OPT_SPECIAL_ignore)
1498 return;
1499 if (dc)
1500 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1501 if (imply)
1503 const struct cl_option *option = &cl_options[opt_index];
1505 /* -Werror=foo implies -Wfoo. */
1506 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1508 int value = 1;
1510 if (arg && *arg == '\0' && !option->cl_missing_ok)
1511 arg = NULL;
1513 if ((option->flags & CL_JOINED) && arg == NULL)
1515 cmdline_handle_error (loc, option, option->opt_text, arg,
1516 CL_ERR_MISSING_ARG, lang_mask);
1517 return;
1520 /* If the switch takes an integer, convert it. */
1521 if (arg && option->cl_uinteger)
1523 value = integral_argument (arg);
1524 if (value == -1)
1526 cmdline_handle_error (loc, option, option->opt_text, arg,
1527 CL_ERR_UINT_ARG, lang_mask);
1528 return;
1532 /* If the switch takes an enumerated argument, convert it. */
1533 if (arg && option->var_type == CLVC_ENUM)
1535 const struct cl_enum *e = &cl_enums[option->var_enum];
1537 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1539 const char *carg = NULL;
1541 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1542 arg = carg;
1543 gcc_assert (carg != NULL);
1545 else
1547 cmdline_handle_error (loc, option, option->opt_text, arg,
1548 CL_ERR_ENUM_ARG, lang_mask);
1549 return;
1553 handle_generated_option (opts, opts_set,
1554 opt_index, arg, value, lang_mask,
1555 kind, loc, handlers, false, dc);