[AArch64] Fix -mlow-precision-div (PR 86838)
[official-gcc.git] / gcc / opts-common.c
blob11356442e7bfc67c744ed00dc0b28cf70690c721
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 representable
173 in HOST_WIDE_INT return its value, otherwise return -1. If ERR is not
174 null set *ERR to zero on success or to EINVAL or to the value of errno
175 otherwise. */
177 HOST_WIDE_INT
178 integral_argument (const char *arg, int *err, bool byte_size_suffix)
180 if (!err)
181 err = &errno;
183 if (!ISDIGIT (*arg))
185 *err = EINVAL;
186 return -1;
189 *err = 0;
190 errno = 0;
192 char *end = NULL;
193 unsigned HOST_WIDE_INT unit = 1;
194 unsigned HOST_WIDE_INT value = strtoull (arg, &end, 10);
196 /* If the value is too large to be represented use the maximum
197 representable value that strtoull sets VALUE to (setting
198 errno to ERANGE). */
200 if (end && *end)
202 if (!byte_size_suffix)
204 errno = 0;
205 value = strtoull (arg, &end, 0);
206 if (*end)
208 /* errno is most likely EINVAL here. */
209 *err = errno;
210 return -1;
213 return value;
216 /* Numeric option arguments are at most INT_MAX. Make it
217 possible to specify a larger value by accepting common
218 suffixes. */
219 if (!strcmp (end, "kB"))
220 unit = 1000;
221 else if (!strcasecmp (end, "KiB") || !strcmp (end, "KB"))
222 unit = 1024;
223 else if (!strcmp (end, "MB"))
224 unit = HOST_WIDE_INT_UC (1000) * 1000;
225 else if (!strcasecmp (end, "MiB"))
226 unit = HOST_WIDE_INT_UC (1024) * 1024;
227 else if (!strcasecmp (end, "GB"))
228 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000;
229 else if (!strcasecmp (end, "GiB"))
230 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024;
231 else if (!strcasecmp (end, "TB"))
232 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000;
233 else if (!strcasecmp (end, "TiB"))
234 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024;
235 else if (!strcasecmp (end, "PB"))
236 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000;
237 else if (!strcasecmp (end, "PiB"))
238 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024;
239 else if (!strcasecmp (end, "EB"))
240 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000
241 * 1000;
242 else if (!strcasecmp (end, "EiB"))
243 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024
244 * 1024;
245 else
247 /* This could mean an unknown suffix or a bad prefix, like
248 "+-1". */
249 *err = EINVAL;
250 return -1;
254 if (unit)
256 unsigned HOST_WIDE_INT prod = value * unit;
257 value = prod < value ? HOST_WIDE_INT_M1U : prod;
260 return value;
263 /* Return whether OPTION is OK for the language given by
264 LANG_MASK. */
265 static bool
266 option_ok_for_language (const struct cl_option *option,
267 unsigned int lang_mask)
269 if (!(option->flags & lang_mask))
270 return false;
271 else if ((option->flags & CL_TARGET)
272 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
273 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
274 /* Complain for target flag language mismatches if any languages
275 are specified. */
276 return false;
277 return true;
280 /* Return whether ENUM_ARG is OK for the language given by
281 LANG_MASK. */
283 static bool
284 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
285 unsigned int lang_mask)
287 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
290 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
291 storing the value in *VALUE if found, and returning false without
292 modifying *VALUE if not found. */
294 static bool
295 enum_arg_to_value (const struct cl_enum_arg *enum_args,
296 const char *arg, HOST_WIDE_INT *value,
297 unsigned int lang_mask)
299 unsigned int i;
301 for (i = 0; enum_args[i].arg != NULL; i++)
302 if (strcmp (arg, enum_args[i].arg) == 0
303 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
305 *value = enum_args[i].value;
306 return true;
309 return false;
312 /* Look up ARG in the enum used by option OPT_INDEX for language
313 LANG_MASK, returning true and storing the value in *VALUE if found,
314 and returning false without modifying *VALUE if not found. */
316 bool
317 opt_enum_arg_to_value (size_t opt_index, const char *arg,
318 int *value, unsigned int lang_mask)
320 const struct cl_option *option = &cl_options[opt_index];
322 gcc_assert (option->var_type == CLVC_ENUM);
324 HOST_WIDE_INT wideval;
325 if (enum_arg_to_value (cl_enums[option->var_enum].values, arg,
326 &wideval, lang_mask))
328 *value = wideval;
329 return true;
332 return false;
335 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
336 corresponding string in *ARGP, returning true if the found string
337 was marked as canonical, false otherwise. If VALUE is not found
338 (which may be the case for uninitialized values if the relevant
339 option has not been passed), set *ARGP to NULL and return
340 false. */
342 bool
343 enum_value_to_arg (const struct cl_enum_arg *enum_args,
344 const char **argp, int value, unsigned int lang_mask)
346 unsigned int i;
348 for (i = 0; enum_args[i].arg != NULL; i++)
349 if (enum_args[i].value == value
350 && (enum_args[i].flags & CL_ENUM_CANONICAL)
351 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
353 *argp = enum_args[i].arg;
354 return true;
357 for (i = 0; enum_args[i].arg != NULL; i++)
358 if (enum_args[i].value == value
359 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
361 *argp = enum_args[i].arg;
362 return false;
365 *argp = NULL;
366 return false;
369 /* Fill in the canonical option part of *DECODED with an option
370 described by OPT_INDEX, ARG and VALUE. */
372 static void
373 generate_canonical_option (size_t opt_index, const char *arg,
374 HOST_WIDE_INT value,
375 struct cl_decoded_option *decoded)
377 const struct cl_option *option = &cl_options[opt_index];
378 const char *opt_text = option->opt_text;
380 if (value == 0
381 && !option->cl_reject_negative
382 && (opt_text[1] == 'W' || opt_text[1] == 'f'
383 || opt_text[1] == 'g' || opt_text[1] == 'm'))
385 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
386 t[0] = '-';
387 t[1] = opt_text[1];
388 t[2] = 'n';
389 t[3] = 'o';
390 t[4] = '-';
391 memcpy (t + 5, opt_text + 2, option->opt_len);
392 opt_text = t;
395 decoded->canonical_option[2] = NULL;
396 decoded->canonical_option[3] = NULL;
398 if (arg)
400 if ((option->flags & CL_SEPARATE)
401 && !option->cl_separate_alias)
403 decoded->canonical_option[0] = opt_text;
404 decoded->canonical_option[1] = arg;
405 decoded->canonical_option_num_elements = 2;
407 else
409 gcc_assert (option->flags & CL_JOINED);
410 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
411 decoded->canonical_option[1] = NULL;
412 decoded->canonical_option_num_elements = 1;
415 else
417 decoded->canonical_option[0] = opt_text;
418 decoded->canonical_option[1] = NULL;
419 decoded->canonical_option_num_elements = 1;
423 /* Structure describing mappings from options on the command line to
424 options to look up with find_opt. */
425 struct option_map
427 /* Prefix of the option on the command line. */
428 const char *opt0;
429 /* If two argv elements are considered to be merged into one option,
430 prefix for the second element, otherwise NULL. */
431 const char *opt1;
432 /* The new prefix to map to. */
433 const char *new_prefix;
434 /* Whether at least one character is needed following opt1 or opt0
435 for this mapping to be used. (--optimize= is valid for -O, but
436 --warn- is not valid for -W.) */
437 bool another_char_needed;
438 /* Whether the original option is a negated form of the option
439 resulting from this map. */
440 bool negated;
442 static const struct option_map option_map[] =
444 { "-Wno-", NULL, "-W", false, true },
445 { "-fno-", NULL, "-f", false, true },
446 { "-gno-", NULL, "-g", false, true },
447 { "-mno-", NULL, "-m", false, true },
448 { "--debug=", NULL, "-g", false, false },
449 { "--machine-", NULL, "-m", true, false },
450 { "--machine-no-", NULL, "-m", false, true },
451 { "--machine=", NULL, "-m", false, false },
452 { "--machine=no-", NULL, "-m", false, true },
453 { "--machine", "", "-m", false, false },
454 { "--machine", "no-", "-m", false, true },
455 { "--optimize=", NULL, "-O", false, false },
456 { "--std=", NULL, "-std=", false, false },
457 { "--std", "", "-std=", false, false },
458 { "--warn-", NULL, "-W", true, false },
459 { "--warn-no-", NULL, "-W", false, true },
460 { "--", NULL, "-f", true, false },
461 { "--no-", NULL, "-f", false, true }
464 /* Helper function for gcc.c's driver::suggest_option, for populating the
465 vec of suggestions for misspelled options.
467 option_map above provides various prefixes for spelling command-line
468 options, which decode_cmdline_option uses to map spellings of options
469 to specific options. We want to do the reverse: to find all the ways
470 that a user could validly spell an option.
472 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
473 of its valid variant spellings to CANDIDATES, each without a leading
474 dash.
476 For example, given "-Wabi-tag", the following are added to CANDIDATES:
477 "Wabi-tag"
478 "Wno-abi-tag"
479 "-warn-abi-tag"
480 "-warn-no-abi-tag".
482 The added strings must be freed using free. */
484 void
485 add_misspelling_candidates (auto_vec<char *> *candidates,
486 const struct cl_option *option,
487 const char *opt_text)
489 gcc_assert (candidates);
490 gcc_assert (option);
491 gcc_assert (opt_text);
492 if (remapping_prefix_p (option))
493 return;
494 candidates->safe_push (xstrdup (opt_text + 1));
495 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
497 const char *opt0 = option_map[i].opt0;
498 const char *new_prefix = option_map[i].new_prefix;
499 size_t new_prefix_len = strlen (new_prefix);
501 if (option->cl_reject_negative && option_map[i].negated)
502 continue;
504 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
506 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
507 NULL);
508 candidates->safe_push (alternative);
513 /* Decode the switch beginning at ARGV for the language indicated by
514 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
515 the structure *DECODED. Returns the number of switches
516 consumed. */
518 static unsigned int
519 decode_cmdline_option (const char **argv, unsigned int lang_mask,
520 struct cl_decoded_option *decoded)
522 size_t opt_index;
523 const char *arg = 0;
524 HOST_WIDE_INT value = 1;
525 unsigned int result = 1, i, extra_args, separate_args = 0;
526 int adjust_len = 0;
527 size_t total_len;
528 char *p;
529 const struct cl_option *option;
530 int errors = 0;
531 const char *warn_message = NULL;
532 bool separate_arg_flag;
533 bool joined_arg_flag;
534 bool have_separate_arg = false;
536 extra_args = 0;
538 opt_index = find_opt (argv[0] + 1, lang_mask);
539 i = 0;
540 while (opt_index == OPT_SPECIAL_unknown
541 && i < ARRAY_SIZE (option_map))
543 const char *opt0 = option_map[i].opt0;
544 const char *opt1 = option_map[i].opt1;
545 const char *new_prefix = option_map[i].new_prefix;
546 bool another_char_needed = option_map[i].another_char_needed;
547 size_t opt0_len = strlen (opt0);
548 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
549 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
550 size_t new_prefix_len = strlen (new_prefix);
552 extra_args = (opt1 == NULL ? 0 : 1);
553 value = !option_map[i].negated;
555 if (strncmp (argv[0], opt0, opt0_len) == 0
556 && (opt1 == NULL
557 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
558 && (!another_char_needed
559 || argv[extra_args][optn_len] != 0))
561 size_t arglen = strlen (argv[extra_args]);
562 char *dup;
564 adjust_len = (int) optn_len - (int) new_prefix_len;
565 dup = XNEWVEC (char, arglen + 1 - adjust_len);
566 memcpy (dup, new_prefix, new_prefix_len);
567 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
568 arglen - optn_len + 1);
569 opt_index = find_opt (dup + 1, lang_mask);
570 free (dup);
572 i++;
575 if (opt_index == OPT_SPECIAL_unknown)
577 arg = argv[0];
578 extra_args = 0;
579 value = 1;
580 goto done;
583 option = &cl_options[opt_index];
585 /* Reject negative form of switches that don't take negatives as
586 unrecognized. */
587 if (!value && option->cl_reject_negative)
589 opt_index = OPT_SPECIAL_unknown;
590 errors |= CL_ERR_NEGATIVE;
591 arg = argv[0];
592 goto done;
595 /* Clear the initial value for size options (it will be overwritten
596 later based on the Init(value) specification in the opt file. */
597 if (option->var_type == CLVC_SIZE)
598 value = 0;
600 result = extra_args + 1;
601 warn_message = option->warn_message;
603 /* Check to see if the option is disabled for this configuration. */
604 if (option->cl_disabled)
605 errors |= CL_ERR_DISABLED;
607 /* Determine whether there may be a separate argument based on
608 whether this option is being processed for the driver, and, if
609 so, how many such arguments. */
610 separate_arg_flag = ((option->flags & CL_SEPARATE)
611 && !(option->cl_no_driver_arg
612 && (lang_mask & CL_DRIVER)));
613 separate_args = (separate_arg_flag
614 ? option->cl_separate_nargs + 1
615 : 0);
616 joined_arg_flag = (option->flags & CL_JOINED) != 0;
618 /* Sort out any argument the switch takes. */
619 if (joined_arg_flag)
621 /* Have arg point to the original switch. This is because
622 some code, such as disable_builtin_function, expects its
623 argument to be persistent until the program exits. */
624 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
626 if (*arg == '\0' && !option->cl_missing_ok)
628 if (separate_arg_flag)
630 arg = argv[extra_args + 1];
631 result = extra_args + 2;
632 if (arg == NULL)
633 result = extra_args + 1;
634 else
635 have_separate_arg = true;
637 else
638 /* Missing argument. */
639 arg = NULL;
642 else if (separate_arg_flag)
644 arg = argv[extra_args + 1];
645 for (i = 0; i < separate_args; i++)
646 if (argv[extra_args + 1 + i] == NULL)
648 errors |= CL_ERR_MISSING_ARG;
649 break;
651 result = extra_args + 1 + i;
652 if (arg != NULL)
653 have_separate_arg = true;
656 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
657 errors |= CL_ERR_MISSING_ARG;
659 /* Is this option an alias (or an ignored option, marked as an alias
660 of OPT_SPECIAL_ignore)? */
661 if (option->alias_target != N_OPTS
662 && (!option->cl_separate_alias || have_separate_arg))
664 size_t new_opt_index = option->alias_target;
666 if (new_opt_index == OPT_SPECIAL_ignore)
668 gcc_assert (option->alias_arg == NULL);
669 gcc_assert (option->neg_alias_arg == NULL);
670 opt_index = new_opt_index;
671 arg = NULL;
672 value = 1;
674 else
676 const struct cl_option *new_option = &cl_options[new_opt_index];
678 /* The new option must not be an alias itself. */
679 gcc_assert (new_option->alias_target == N_OPTS
680 || new_option->cl_separate_alias);
682 if (option->neg_alias_arg)
684 gcc_assert (option->alias_arg != NULL);
685 gcc_assert (arg == NULL);
686 gcc_assert (!option->cl_negative_alias);
687 if (value)
688 arg = option->alias_arg;
689 else
690 arg = option->neg_alias_arg;
691 value = 1;
693 else if (option->alias_arg)
695 gcc_assert (value == 1);
696 gcc_assert (arg == NULL);
697 gcc_assert (!option->cl_negative_alias);
698 arg = option->alias_arg;
701 if (option->cl_negative_alias)
702 value = !value;
704 opt_index = new_opt_index;
705 option = new_option;
707 if (value == 0)
708 gcc_assert (!option->cl_reject_negative);
710 /* Recompute what arguments are allowed. */
711 separate_arg_flag = ((option->flags & CL_SEPARATE)
712 && !(option->cl_no_driver_arg
713 && (lang_mask & CL_DRIVER)));
714 joined_arg_flag = (option->flags & CL_JOINED) != 0;
716 if (separate_args > 1 || option->cl_separate_nargs)
717 gcc_assert (separate_args
718 == (unsigned int) option->cl_separate_nargs + 1);
720 if (!(errors & CL_ERR_MISSING_ARG))
722 if (separate_arg_flag || joined_arg_flag)
724 if (option->cl_missing_ok && arg == NULL)
725 arg = "";
726 gcc_assert (arg != NULL);
728 else
729 gcc_assert (arg == NULL);
732 /* Recheck for warnings and disabled options. */
733 if (option->warn_message)
735 gcc_assert (warn_message == NULL);
736 warn_message = option->warn_message;
738 if (option->cl_disabled)
739 errors |= CL_ERR_DISABLED;
743 /* Check if this is a switch for a different front end. */
744 if (!option_ok_for_language (option, lang_mask))
745 errors |= CL_ERR_WRONG_LANG;
747 /* Mark all deprecated options. */
748 if (option->cl_deprecated)
749 errors |= CL_ERR_DEPRECATED;
751 /* Convert the argument to lowercase if appropriate. */
752 if (arg && option->cl_tolower)
754 size_t j;
755 size_t len = strlen (arg);
756 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
758 for (j = 0; j < len; j++)
759 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
760 arg_lower[len] = 0;
761 arg = arg_lower;
764 /* If the switch takes an integer argument, convert it. */
765 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
767 int error = 0;
768 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
769 if (error)
770 errors |= CL_ERR_UINT_ARG;
772 /* Reject value out of a range. */
773 if (option->range_max != -1
774 && (value < option->range_min || value > option->range_max))
775 errors |= CL_ERR_INT_RANGE_ARG;
778 /* If the switch takes an enumerated argument, convert it. */
779 if (arg && (option->var_type == CLVC_ENUM))
781 const struct cl_enum *e = &cl_enums[option->var_enum];
783 gcc_assert (value == 1);
784 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
786 const char *carg = NULL;
788 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
789 arg = carg;
790 gcc_assert (carg != NULL);
792 else
793 errors |= CL_ERR_ENUM_ARG;
796 done:
797 decoded->opt_index = opt_index;
798 decoded->arg = arg;
799 decoded->value = value;
800 decoded->errors = errors;
801 decoded->warn_message = warn_message;
803 if (opt_index == OPT_SPECIAL_unknown)
804 gcc_assert (result == 1);
806 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
807 decoded->canonical_option_num_elements = result;
808 total_len = 0;
809 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
811 if (i < result)
813 size_t len;
814 if (opt_index == OPT_SPECIAL_unknown)
815 decoded->canonical_option[i] = argv[i];
816 else
817 decoded->canonical_option[i] = NULL;
818 len = strlen (argv[i]);
819 /* If the argument is an empty string, we will print it as "" in
820 orig_option_with_args_text. */
821 total_len += (len != 0 ? len : 2) + 1;
823 else
824 decoded->canonical_option[i] = NULL;
826 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
828 generate_canonical_option (opt_index, arg, value, decoded);
829 if (separate_args > 1)
831 for (i = 0; i < separate_args; i++)
833 if (argv[extra_args + 1 + i] == NULL)
834 break;
835 else
836 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
838 gcc_assert (result == 1 + i);
839 decoded->canonical_option_num_elements = result;
842 decoded->orig_option_with_args_text
843 = p = XOBNEWVEC (&opts_obstack, char, total_len);
844 for (i = 0; i < result; i++)
846 size_t len = strlen (argv[i]);
848 /* Print the empty string verbally. */
849 if (len == 0)
851 *p++ = '"';
852 *p++ = '"';
854 else
855 memcpy (p, argv[i], len);
856 p += len;
857 if (i == result - 1)
858 *p++ = 0;
859 else
860 *p++ = ' ';
863 return result;
866 /* Obstack for option strings. */
868 struct obstack opts_obstack;
870 /* Like libiberty concat, but allocate using opts_obstack. */
872 char *
873 opts_concat (const char *first, ...)
875 char *newstr, *end;
876 size_t length = 0;
877 const char *arg;
878 va_list ap;
880 /* First compute the size of the result and get sufficient memory. */
881 va_start (ap, first);
882 for (arg = first; arg; arg = va_arg (ap, const char *))
883 length += strlen (arg);
884 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
885 va_end (ap);
887 /* Now copy the individual pieces to the result string. */
888 va_start (ap, first);
889 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
891 length = strlen (arg);
892 memcpy (end, arg, length);
893 end += length;
895 *end = '\0';
896 va_end (ap);
897 return newstr;
900 /* Decode command-line options (ARGC and ARGV being the arguments of
901 main) into an array, setting *DECODED_OPTIONS to a pointer to that
902 array and *DECODED_OPTIONS_COUNT to the number of entries in the
903 array. The first entry in the array is always one for the program
904 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
905 flags applicable for decoding (including CL_COMMON and CL_TARGET if
906 those options should be considered applicable). Do not produce any
907 diagnostics or set state outside of these variables. */
909 void
910 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
911 unsigned int lang_mask,
912 struct cl_decoded_option **decoded_options,
913 unsigned int *decoded_options_count)
915 unsigned int n, i;
916 struct cl_decoded_option *opt_array;
917 unsigned int num_decoded_options;
919 opt_array = XNEWVEC (struct cl_decoded_option, argc);
921 opt_array[0].opt_index = OPT_SPECIAL_program_name;
922 opt_array[0].warn_message = NULL;
923 opt_array[0].arg = argv[0];
924 opt_array[0].orig_option_with_args_text = argv[0];
925 opt_array[0].canonical_option_num_elements = 1;
926 opt_array[0].canonical_option[0] = argv[0];
927 opt_array[0].canonical_option[1] = NULL;
928 opt_array[0].canonical_option[2] = NULL;
929 opt_array[0].canonical_option[3] = NULL;
930 opt_array[0].value = 1;
931 opt_array[0].errors = 0;
932 num_decoded_options = 1;
934 for (i = 1; i < argc; i += n)
936 const char *opt = argv[i];
938 /* Interpret "-" or a non-switch as a file name. */
939 if (opt[0] != '-' || opt[1] == '\0')
941 generate_option_input_file (opt, &opt_array[num_decoded_options]);
942 num_decoded_options++;
943 n = 1;
944 continue;
947 n = decode_cmdline_option (argv + i, lang_mask,
948 &opt_array[num_decoded_options]);
949 num_decoded_options++;
952 *decoded_options = opt_array;
953 *decoded_options_count = num_decoded_options;
954 prune_options (decoded_options, decoded_options_count);
957 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
958 next one is the same as ORIG_NEXT_OPT_IDX. */
960 static bool
961 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
963 /* An option can be canceled by the same option or an option with
964 Negative. */
965 if (cl_options [next_opt_idx].neg_index == opt_idx)
966 return true;
968 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
969 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
970 orig_next_opt_idx);
972 return false;
975 /* Filter out options canceled by the ones after them. */
977 static void
978 prune_options (struct cl_decoded_option **decoded_options,
979 unsigned int *decoded_options_count)
981 unsigned int old_decoded_options_count = *decoded_options_count;
982 struct cl_decoded_option *old_decoded_options = *decoded_options;
983 unsigned int new_decoded_options_count;
984 struct cl_decoded_option *new_decoded_options
985 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
986 unsigned int i;
987 const struct cl_option *option;
988 unsigned int fdiagnostics_color_idx = 0;
990 /* Remove arguments which are negated by others after them. */
991 new_decoded_options_count = 0;
992 for (i = 0; i < old_decoded_options_count; i++)
994 unsigned int j, opt_idx, next_opt_idx;
996 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
997 goto keep;
999 opt_idx = old_decoded_options[i].opt_index;
1000 switch (opt_idx)
1002 case OPT_SPECIAL_unknown:
1003 case OPT_SPECIAL_ignore:
1004 case OPT_SPECIAL_program_name:
1005 case OPT_SPECIAL_input_file:
1006 goto keep;
1008 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1009 case OPT_fdiagnostics_color_:
1010 fdiagnostics_color_idx = i;
1011 continue;
1013 default:
1014 gcc_assert (opt_idx < cl_options_count);
1015 option = &cl_options[opt_idx];
1016 if (option->neg_index < 0)
1017 goto keep;
1019 /* Skip joined switches. */
1020 if ((option->flags & CL_JOINED))
1021 goto keep;
1023 for (j = i + 1; j < old_decoded_options_count; j++)
1025 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1026 continue;
1027 next_opt_idx = old_decoded_options[j].opt_index;
1028 if (next_opt_idx >= cl_options_count)
1029 continue;
1030 if (cl_options[next_opt_idx].neg_index < 0)
1031 continue;
1032 if ((cl_options[next_opt_idx].flags & CL_JOINED))
1033 continue;
1034 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
1035 break;
1037 if (j == old_decoded_options_count)
1039 keep:
1040 new_decoded_options[new_decoded_options_count]
1041 = old_decoded_options[i];
1042 new_decoded_options_count++;
1044 break;
1048 if (fdiagnostics_color_idx >= 1)
1050 /* We put the last -fdiagnostics-color= at the first position
1051 after argv[0] so it can take effect immediately. */
1052 memmove (new_decoded_options + 2, new_decoded_options + 1,
1053 sizeof (struct cl_decoded_option)
1054 * (new_decoded_options_count - 1));
1055 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
1056 new_decoded_options_count++;
1059 free (old_decoded_options);
1060 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1061 new_decoded_options,
1062 new_decoded_options_count);
1063 *decoded_options = new_decoded_options;
1064 *decoded_options_count = new_decoded_options_count;
1067 /* Handle option DECODED for the language indicated by LANG_MASK,
1068 using the handlers in HANDLERS and setting fields in OPTS and
1069 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1070 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1071 option for options from the source file, UNKNOWN_LOCATION
1072 otherwise. GENERATED_P is true for an option generated as part of
1073 processing another option or otherwise generated internally, false
1074 for one explicitly passed by the user. control_warning_option
1075 generated options are considered explicitly passed by the user.
1076 Returns false if the switch was invalid. DC is the diagnostic
1077 context for options affecting diagnostics state, or NULL. */
1079 static bool
1080 handle_option (struct gcc_options *opts,
1081 struct gcc_options *opts_set,
1082 const struct cl_decoded_option *decoded,
1083 unsigned int lang_mask, int kind, location_t loc,
1084 const struct cl_option_handlers *handlers,
1085 bool generated_p, diagnostic_context *dc)
1087 size_t opt_index = decoded->opt_index;
1088 const char *arg = decoded->arg;
1089 HOST_WIDE_INT value = decoded->value;
1090 const struct cl_option *option = &cl_options[opt_index];
1091 void *flag_var = option_flag_var (opt_index, opts);
1092 size_t i;
1094 if (flag_var)
1095 set_option (opts, (generated_p ? NULL : opts_set),
1096 opt_index, value, arg, kind, loc, dc);
1098 for (i = 0; i < handlers->num_handlers; i++)
1099 if (option->flags & handlers->handlers[i].mask)
1101 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1102 lang_mask, kind, loc,
1103 handlers, dc,
1104 handlers->target_option_override_hook))
1105 return false;
1108 return true;
1111 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1112 option instead of DECODED. This is used for callbacks when one
1113 option implies another instead of an option being decoded from the
1114 command line. */
1116 bool
1117 handle_generated_option (struct gcc_options *opts,
1118 struct gcc_options *opts_set,
1119 size_t opt_index, const char *arg, HOST_WIDE_INT value,
1120 unsigned int lang_mask, int kind, location_t loc,
1121 const struct cl_option_handlers *handlers,
1122 bool generated_p, diagnostic_context *dc)
1124 struct cl_decoded_option decoded;
1126 generate_option (opt_index, arg, value, lang_mask, &decoded);
1127 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1128 handlers, generated_p, dc);
1131 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1132 VALUE for a front end using LANG_MASK. This is used when the
1133 compiler generates options internally. */
1135 void
1136 generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
1137 unsigned int lang_mask, struct cl_decoded_option *decoded)
1139 const struct cl_option *option = &cl_options[opt_index];
1141 decoded->opt_index = opt_index;
1142 decoded->warn_message = NULL;
1143 decoded->arg = arg;
1144 decoded->value = value;
1145 decoded->errors = (option_ok_for_language (option, lang_mask)
1147 : CL_ERR_WRONG_LANG);
1149 generate_canonical_option (opt_index, arg, value, decoded);
1150 switch (decoded->canonical_option_num_elements)
1152 case 1:
1153 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1154 break;
1156 case 2:
1157 decoded->orig_option_with_args_text
1158 = opts_concat (decoded->canonical_option[0], " ",
1159 decoded->canonical_option[1], NULL);
1160 break;
1162 default:
1163 gcc_unreachable ();
1167 /* Fill in *DECODED with an option for input file FILE. */
1169 void
1170 generate_option_input_file (const char *file,
1171 struct cl_decoded_option *decoded)
1173 decoded->opt_index = OPT_SPECIAL_input_file;
1174 decoded->warn_message = NULL;
1175 decoded->arg = file;
1176 decoded->orig_option_with_args_text = file;
1177 decoded->canonical_option_num_elements = 1;
1178 decoded->canonical_option[0] = file;
1179 decoded->canonical_option[1] = NULL;
1180 decoded->canonical_option[2] = NULL;
1181 decoded->canonical_option[3] = NULL;
1182 decoded->value = 1;
1183 decoded->errors = 0;
1186 /* Helper function for listing valid choices and hint for misspelled
1187 value. CANDIDATES is a vector containing all valid strings,
1188 STR is set to a heap allocated string that contains all those
1189 strings concatenated, separated by spaces, and the return value
1190 is the closest string from those to ARG, or NULL if nothing is
1191 close enough. Callers should XDELETEVEC (STR) after using it
1192 to avoid memory leaks. */
1194 const char *
1195 candidates_list_and_hint (const char *arg, char *&str,
1196 const auto_vec <const char *> &candidates)
1198 size_t len = 0;
1199 int i;
1200 const char *candidate;
1201 char *p;
1203 FOR_EACH_VEC_ELT (candidates, i, candidate)
1204 len += strlen (candidate) + 1;
1206 str = p = XNEWVEC (char, len);
1207 FOR_EACH_VEC_ELT (candidates, i, candidate)
1209 len = strlen (candidate);
1210 memcpy (p, candidate, len);
1211 p[len] = ' ';
1212 p += len + 1;
1214 p[-1] = '\0';
1215 return find_closest_string (arg, &candidates);
1218 /* Perform diagnostics for read_cmdline_option and control_warning_option
1219 functions. Returns true if an error has been diagnosed.
1220 LOC and LANG_MASK arguments like in read_cmdline_option.
1221 OPTION is the option to report diagnostics for, OPT the name
1222 of the option as text, ARG the argument of the option (for joined
1223 options), ERRORS is bitmask of CL_ERR_* values. */
1225 static bool
1226 cmdline_handle_error (location_t loc, const struct cl_option *option,
1227 const char *opt, const char *arg, int errors,
1228 unsigned int lang_mask)
1230 if (errors & CL_ERR_DISABLED)
1232 error_at (loc, "command line option %qs"
1233 " is not supported by this configuration", opt);
1234 return true;
1237 if (errors & CL_ERR_MISSING_ARG)
1239 if (option->missing_argument_error)
1240 error_at (loc, option->missing_argument_error, opt);
1241 else
1242 error_at (loc, "missing argument to %qs", opt);
1243 return true;
1246 if (errors & CL_ERR_UINT_ARG)
1248 if (option->cl_byte_size)
1249 error_at (loc, "argument to %qs should be a non-negative integer "
1250 "optionally followed by a size unit",
1251 option->opt_text);
1252 else
1253 error_at (loc, "argument to %qs should be a non-negative integer",
1254 option->opt_text);
1255 return true;
1258 if (errors & CL_ERR_INT_RANGE_ARG)
1260 error_at (loc, "argument to %qs is not between %d and %d",
1261 option->opt_text, option->range_min, option->range_max);
1262 return true;
1265 if (errors & CL_ERR_ENUM_ARG)
1267 const struct cl_enum *e = &cl_enums[option->var_enum];
1268 unsigned int i;
1269 char *s;
1271 if (e->unknown_error)
1272 error_at (loc, e->unknown_error, arg);
1273 else
1274 error_at (loc, "unrecognized argument in option %qs", opt);
1276 auto_vec <const char *> candidates;
1277 for (i = 0; e->values[i].arg != NULL; i++)
1279 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1280 continue;
1281 candidates.safe_push (e->values[i].arg);
1283 const char *hint = candidates_list_and_hint (arg, s, candidates);
1284 if (hint)
1285 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1286 option->opt_text, s, hint);
1287 else
1288 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1289 XDELETEVEC (s);
1291 return true;
1294 return false;
1297 /* Handle the switch DECODED (location LOC) for the language indicated
1298 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1299 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1300 diagnostic options. */
1302 void
1303 read_cmdline_option (struct gcc_options *opts,
1304 struct gcc_options *opts_set,
1305 struct cl_decoded_option *decoded,
1306 location_t loc,
1307 unsigned int lang_mask,
1308 const struct cl_option_handlers *handlers,
1309 diagnostic_context *dc)
1311 const struct cl_option *option;
1312 const char *opt = decoded->orig_option_with_args_text;
1314 if (decoded->warn_message)
1315 warning_at (loc, 0, decoded->warn_message, opt);
1317 if (decoded->opt_index == OPT_SPECIAL_unknown)
1319 if (handlers->unknown_option_callback (decoded))
1320 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1321 return;
1324 if (decoded->opt_index == OPT_SPECIAL_ignore)
1325 return;
1327 option = &cl_options[decoded->opt_index];
1329 if (decoded->errors
1330 && cmdline_handle_error (loc, option, opt, decoded->arg,
1331 decoded->errors, lang_mask))
1332 return;
1334 if (decoded->errors & CL_ERR_WRONG_LANG)
1336 handlers->wrong_lang_callback (decoded, lang_mask);
1337 return;
1340 if (decoded->errors & CL_ERR_DEPRECATED)
1342 warning_at (loc, 0, "deprecated command line option %qs", opt);
1343 return;
1346 gcc_assert (!decoded->errors);
1348 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1349 loc, handlers, false, dc))
1350 error_at (loc, "unrecognized command line option %qs", opt);
1353 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1354 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1355 location LOC, using diagnostic context DC if not NULL for
1356 diagnostic classification. */
1358 void
1359 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1360 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
1361 location_t loc, diagnostic_context *dc)
1363 const struct cl_option *option = &cl_options[opt_index];
1364 void *flag_var = option_flag_var (opt_index, opts);
1365 void *set_flag_var = NULL;
1367 if (!flag_var)
1368 return;
1370 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1371 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1373 if (opts_set != NULL)
1374 set_flag_var = option_flag_var (opt_index, opts_set);
1376 switch (option->var_type)
1378 case CLVC_BOOLEAN:
1379 if (option->cl_host_wide_int)
1381 *(HOST_WIDE_INT *) flag_var = value;
1382 if (set_flag_var)
1383 *(HOST_WIDE_INT *) set_flag_var = 1;
1385 else
1387 *(int *) flag_var = value;
1388 if (set_flag_var)
1389 *(int *) set_flag_var = 1;
1392 break;
1394 case CLVC_SIZE:
1395 if (option->cl_host_wide_int)
1397 *(HOST_WIDE_INT *) flag_var = value;
1398 if (set_flag_var)
1399 *(HOST_WIDE_INT *) set_flag_var = value;
1401 else
1403 *(int *) flag_var = value;
1404 if (set_flag_var)
1405 *(int *) set_flag_var = value;
1408 break;
1410 case CLVC_EQUAL:
1411 if (option->cl_host_wide_int)
1413 *(HOST_WIDE_INT *) flag_var = (value
1414 ? option->var_value
1415 : !option->var_value);
1416 if (set_flag_var)
1417 *(HOST_WIDE_INT *) set_flag_var = 1;
1419 else
1421 *(int *) flag_var = (value
1422 ? option->var_value
1423 : !option->var_value);
1424 if (set_flag_var)
1425 *(int *) set_flag_var = 1;
1427 break;
1429 case CLVC_BIT_CLEAR:
1430 case CLVC_BIT_SET:
1431 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1433 if (option->cl_host_wide_int)
1434 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1435 else
1436 *(int *) flag_var |= option->var_value;
1438 else
1440 if (option->cl_host_wide_int)
1441 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1442 else
1443 *(int *) flag_var &= ~option->var_value;
1445 if (set_flag_var)
1447 if (option->cl_host_wide_int)
1448 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1449 else
1450 *(int *) set_flag_var |= option->var_value;
1452 break;
1454 case CLVC_STRING:
1455 *(const char **) flag_var = arg;
1456 if (set_flag_var)
1457 *(const char **) set_flag_var = "";
1458 break;
1460 case CLVC_ENUM:
1462 const struct cl_enum *e = &cl_enums[option->var_enum];
1464 e->set (flag_var, value);
1465 if (set_flag_var)
1466 e->set (set_flag_var, 1);
1468 break;
1470 case CLVC_DEFER:
1472 vec<cl_deferred_option> *v
1473 = (vec<cl_deferred_option> *) *(void **) flag_var;
1474 cl_deferred_option p = {opt_index, arg, value};
1475 if (!v)
1476 v = XCNEW (vec<cl_deferred_option>);
1477 v->safe_push (p);
1478 *(void **) flag_var = v;
1479 if (set_flag_var)
1480 *(void **) set_flag_var = v;
1482 break;
1486 /* Return the address of the flag variable for option OPT_INDEX in
1487 options structure OPTS, or NULL if there is no flag variable. */
1489 void *
1490 option_flag_var (int opt_index, struct gcc_options *opts)
1492 const struct cl_option *option = &cl_options[opt_index];
1494 if (option->flag_var_offset == (unsigned short) -1)
1495 return NULL;
1496 return (void *)(((char *) opts) + option->flag_var_offset);
1499 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1500 or -1 if it isn't a simple on-off switch. */
1503 option_enabled (int opt_idx, void *opts)
1505 const struct cl_option *option = &(cl_options[opt_idx]);
1506 struct gcc_options *optsg = (struct gcc_options *) opts;
1507 void *flag_var = option_flag_var (opt_idx, optsg);
1509 if (flag_var)
1510 switch (option->var_type)
1512 case CLVC_BOOLEAN:
1513 if (option->cl_host_wide_int)
1514 return *(HOST_WIDE_INT *) flag_var != 0;
1515 else
1516 return *(int *) flag_var != 0;
1518 case CLVC_EQUAL:
1519 if (option->cl_host_wide_int)
1520 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1521 else
1522 return *(int *) flag_var == option->var_value;
1524 case CLVC_BIT_CLEAR:
1525 if (option->cl_host_wide_int)
1526 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1527 else
1528 return (*(int *) flag_var & option->var_value) == 0;
1530 case CLVC_BIT_SET:
1531 if (option->cl_host_wide_int)
1532 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1533 else
1534 return (*(int *) flag_var & option->var_value) != 0;
1536 case CLVC_SIZE:
1537 if (option->cl_host_wide_int)
1538 return *(HOST_WIDE_INT *) flag_var != -1;
1539 else
1540 return *(int *) flag_var != -1;
1542 case CLVC_STRING:
1543 case CLVC_ENUM:
1544 case CLVC_DEFER:
1545 break;
1547 return -1;
1550 /* Fill STATE with the current state of option OPTION in OPTS. Return
1551 true if there is some state to store. */
1553 bool
1554 get_option_state (struct gcc_options *opts, int option,
1555 struct cl_option_state *state)
1557 void *flag_var = option_flag_var (option, opts);
1559 if (flag_var == 0)
1560 return false;
1562 switch (cl_options[option].var_type)
1564 case CLVC_BOOLEAN:
1565 case CLVC_EQUAL:
1566 case CLVC_SIZE:
1567 state->data = flag_var;
1568 state->size = (cl_options[option].cl_host_wide_int
1569 ? sizeof (HOST_WIDE_INT)
1570 : sizeof (int));
1571 break;
1573 case CLVC_BIT_CLEAR:
1574 case CLVC_BIT_SET:
1575 state->ch = option_enabled (option, opts);
1576 state->data = &state->ch;
1577 state->size = 1;
1578 break;
1580 case CLVC_STRING:
1581 state->data = *(const char **) flag_var;
1582 if (state->data == 0)
1583 state->data = "";
1584 state->size = strlen ((const char *) state->data) + 1;
1585 break;
1587 case CLVC_ENUM:
1588 state->data = flag_var;
1589 state->size = cl_enums[cl_options[option].var_enum].var_size;
1590 break;
1592 case CLVC_DEFER:
1593 return false;
1595 return true;
1598 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1599 handlers HANDLERS) to have diagnostic kind KIND for option
1600 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1601 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1602 argument of the option for joined options, or NULL otherwise. If IMPLY,
1603 the warning option in question is implied at this point. This is
1604 used by -Werror= and #pragma GCC diagnostic. */
1606 void
1607 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1608 bool imply, location_t loc, unsigned int lang_mask,
1609 const struct cl_option_handlers *handlers,
1610 struct gcc_options *opts,
1611 struct gcc_options *opts_set,
1612 diagnostic_context *dc)
1614 if (cl_options[opt_index].alias_target != N_OPTS)
1616 gcc_assert (!cl_options[opt_index].cl_separate_alias
1617 && !cl_options[opt_index].cl_negative_alias);
1618 if (cl_options[opt_index].alias_arg)
1619 arg = cl_options[opt_index].alias_arg;
1620 opt_index = cl_options[opt_index].alias_target;
1622 if (opt_index == OPT_SPECIAL_ignore)
1623 return;
1624 if (dc)
1625 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1626 if (imply)
1628 const struct cl_option *option = &cl_options[opt_index];
1630 /* -Werror=foo implies -Wfoo. */
1631 if (option->var_type == CLVC_BOOLEAN
1632 || option->var_type == CLVC_ENUM
1633 || option->var_type == CLVC_SIZE)
1635 HOST_WIDE_INT value = 1;
1637 if (arg && *arg == '\0' && !option->cl_missing_ok)
1638 arg = NULL;
1640 if ((option->flags & CL_JOINED) && arg == NULL)
1642 cmdline_handle_error (loc, option, option->opt_text, arg,
1643 CL_ERR_MISSING_ARG, lang_mask);
1644 return;
1647 /* If the switch takes an integer argument, convert it. */
1648 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
1650 int error = 0;
1651 value = *arg ? integral_argument (arg, &error,
1652 option->cl_byte_size) : 0;
1653 if (error)
1655 cmdline_handle_error (loc, option, option->opt_text, arg,
1656 CL_ERR_UINT_ARG, lang_mask);
1657 return;
1661 /* If the switch takes an enumerated argument, convert it. */
1662 if (arg && option->var_type == CLVC_ENUM)
1664 const struct cl_enum *e = &cl_enums[option->var_enum];
1666 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1668 const char *carg = NULL;
1670 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1671 arg = carg;
1672 gcc_assert (carg != NULL);
1674 else
1676 cmdline_handle_error (loc, option, option->opt_text, arg,
1677 CL_ERR_ENUM_ARG, lang_mask);
1678 return;
1682 handle_generated_option (opts, opts_set,
1683 opt_index, arg, value, lang_mask,
1684 kind, loc, handlers, false, dc);