C++: -Wwrite-strings: use location of string constant
[official-gcc.git] / gcc / opts-common.c
blob2b5f63844fb53faa6dec9916e7486649ecdfb02c
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
667 || new_opt_index == OPT_SPECIAL_deprecated)
669 gcc_assert (option->alias_arg == NULL);
670 gcc_assert (option->neg_alias_arg == NULL);
671 opt_index = new_opt_index;
672 arg = NULL;
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 /* Convert the argument to lowercase if appropriate. */
748 if (arg && option->cl_tolower)
750 size_t j;
751 size_t len = strlen (arg);
752 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
754 for (j = 0; j < len; j++)
755 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
756 arg_lower[len] = 0;
757 arg = arg_lower;
760 /* If the switch takes an integer argument, convert it. */
761 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
763 int error = 0;
764 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
765 if (error)
766 errors |= CL_ERR_UINT_ARG;
768 /* Reject value out of a range. */
769 if (option->range_max != -1
770 && (value < option->range_min || value > option->range_max))
771 errors |= CL_ERR_INT_RANGE_ARG;
774 /* If the switch takes an enumerated argument, convert it. */
775 if (arg && (option->var_type == CLVC_ENUM))
777 const struct cl_enum *e = &cl_enums[option->var_enum];
779 gcc_assert (value == 1);
780 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
782 const char *carg = NULL;
784 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
785 arg = carg;
786 gcc_assert (carg != NULL);
788 else
789 errors |= CL_ERR_ENUM_ARG;
792 done:
793 decoded->opt_index = opt_index;
794 decoded->arg = arg;
795 decoded->value = value;
796 decoded->errors = errors;
797 decoded->warn_message = warn_message;
799 if (opt_index == OPT_SPECIAL_unknown)
800 gcc_assert (result == 1);
802 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
803 decoded->canonical_option_num_elements = result;
804 total_len = 0;
805 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
807 if (i < result)
809 size_t len;
810 if (opt_index == OPT_SPECIAL_unknown)
811 decoded->canonical_option[i] = argv[i];
812 else
813 decoded->canonical_option[i] = NULL;
814 len = strlen (argv[i]);
815 /* If the argument is an empty string, we will print it as "" in
816 orig_option_with_args_text. */
817 total_len += (len != 0 ? len : 2) + 1;
819 else
820 decoded->canonical_option[i] = NULL;
822 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore
823 && opt_index != OPT_SPECIAL_deprecated)
825 generate_canonical_option (opt_index, arg, value, decoded);
826 if (separate_args > 1)
828 for (i = 0; i < separate_args; i++)
830 if (argv[extra_args + 1 + i] == NULL)
831 break;
832 else
833 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
835 gcc_assert (result == 1 + i);
836 decoded->canonical_option_num_elements = result;
839 decoded->orig_option_with_args_text
840 = p = XOBNEWVEC (&opts_obstack, char, total_len);
841 for (i = 0; i < result; i++)
843 size_t len = strlen (argv[i]);
845 /* Print the empty string verbally. */
846 if (len == 0)
848 *p++ = '"';
849 *p++ = '"';
851 else
852 memcpy (p, argv[i], len);
853 p += len;
854 if (i == result - 1)
855 *p++ = 0;
856 else
857 *p++ = ' ';
860 return result;
863 /* Obstack for option strings. */
865 struct obstack opts_obstack;
867 /* Like libiberty concat, but allocate using opts_obstack. */
869 char *
870 opts_concat (const char *first, ...)
872 char *newstr, *end;
873 size_t length = 0;
874 const char *arg;
875 va_list ap;
877 /* First compute the size of the result and get sufficient memory. */
878 va_start (ap, first);
879 for (arg = first; arg; arg = va_arg (ap, const char *))
880 length += strlen (arg);
881 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
882 va_end (ap);
884 /* Now copy the individual pieces to the result string. */
885 va_start (ap, first);
886 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
888 length = strlen (arg);
889 memcpy (end, arg, length);
890 end += length;
892 *end = '\0';
893 va_end (ap);
894 return newstr;
897 /* Decode command-line options (ARGC and ARGV being the arguments of
898 main) into an array, setting *DECODED_OPTIONS to a pointer to that
899 array and *DECODED_OPTIONS_COUNT to the number of entries in the
900 array. The first entry in the array is always one for the program
901 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
902 flags applicable for decoding (including CL_COMMON and CL_TARGET if
903 those options should be considered applicable). Do not produce any
904 diagnostics or set state outside of these variables. */
906 void
907 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
908 unsigned int lang_mask,
909 struct cl_decoded_option **decoded_options,
910 unsigned int *decoded_options_count)
912 unsigned int n, i;
913 struct cl_decoded_option *opt_array;
914 unsigned int num_decoded_options;
916 opt_array = XNEWVEC (struct cl_decoded_option, argc);
918 opt_array[0].opt_index = OPT_SPECIAL_program_name;
919 opt_array[0].warn_message = NULL;
920 opt_array[0].arg = argv[0];
921 opt_array[0].orig_option_with_args_text = argv[0];
922 opt_array[0].canonical_option_num_elements = 1;
923 opt_array[0].canonical_option[0] = argv[0];
924 opt_array[0].canonical_option[1] = NULL;
925 opt_array[0].canonical_option[2] = NULL;
926 opt_array[0].canonical_option[3] = NULL;
927 opt_array[0].value = 1;
928 opt_array[0].errors = 0;
929 num_decoded_options = 1;
931 for (i = 1; i < argc; i += n)
933 const char *opt = argv[i];
935 /* Interpret "-" or a non-switch as a file name. */
936 if (opt[0] != '-' || opt[1] == '\0')
938 generate_option_input_file (opt, &opt_array[num_decoded_options]);
939 num_decoded_options++;
940 n = 1;
941 continue;
944 n = decode_cmdline_option (argv + i, lang_mask,
945 &opt_array[num_decoded_options]);
946 num_decoded_options++;
949 *decoded_options = opt_array;
950 *decoded_options_count = num_decoded_options;
951 prune_options (decoded_options, decoded_options_count);
954 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
955 next one is the same as ORIG_NEXT_OPT_IDX. */
957 static bool
958 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
960 /* An option can be canceled by the same option or an option with
961 Negative. */
962 if (cl_options [next_opt_idx].neg_index == opt_idx)
963 return true;
965 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
966 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
967 orig_next_opt_idx);
969 return false;
972 /* Filter out options canceled by the ones after them. */
974 static void
975 prune_options (struct cl_decoded_option **decoded_options,
976 unsigned int *decoded_options_count)
978 unsigned int old_decoded_options_count = *decoded_options_count;
979 struct cl_decoded_option *old_decoded_options = *decoded_options;
980 unsigned int new_decoded_options_count;
981 struct cl_decoded_option *new_decoded_options
982 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
983 unsigned int i;
984 const struct cl_option *option;
985 unsigned int fdiagnostics_color_idx = 0;
987 /* Remove arguments which are negated by others after them. */
988 new_decoded_options_count = 0;
989 for (i = 0; i < old_decoded_options_count; i++)
991 unsigned int j, opt_idx, next_opt_idx;
993 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
994 goto keep;
996 opt_idx = old_decoded_options[i].opt_index;
997 switch (opt_idx)
999 case OPT_SPECIAL_unknown:
1000 case OPT_SPECIAL_ignore:
1001 case OPT_SPECIAL_deprecated:
1002 case OPT_SPECIAL_program_name:
1003 case OPT_SPECIAL_input_file:
1004 goto keep;
1006 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1007 case OPT_fdiagnostics_color_:
1008 fdiagnostics_color_idx = i;
1009 continue;
1011 default:
1012 gcc_assert (opt_idx < cl_options_count);
1013 option = &cl_options[opt_idx];
1014 if (option->neg_index < 0)
1015 goto keep;
1017 /* Skip joined switches. */
1018 if ((option->flags & CL_JOINED))
1019 goto keep;
1021 for (j = i + 1; j < old_decoded_options_count; j++)
1023 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1024 continue;
1025 next_opt_idx = old_decoded_options[j].opt_index;
1026 if (next_opt_idx >= cl_options_count)
1027 continue;
1028 if (cl_options[next_opt_idx].neg_index < 0)
1029 continue;
1030 if ((cl_options[next_opt_idx].flags & CL_JOINED))
1031 continue;
1032 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
1033 break;
1035 if (j == old_decoded_options_count)
1037 keep:
1038 new_decoded_options[new_decoded_options_count]
1039 = old_decoded_options[i];
1040 new_decoded_options_count++;
1042 break;
1046 if (fdiagnostics_color_idx >= 1)
1048 /* We put the last -fdiagnostics-color= at the first position
1049 after argv[0] so it can take effect immediately. */
1050 memmove (new_decoded_options + 2, new_decoded_options + 1,
1051 sizeof (struct cl_decoded_option)
1052 * (new_decoded_options_count - 1));
1053 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
1054 new_decoded_options_count++;
1057 free (old_decoded_options);
1058 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1059 new_decoded_options,
1060 new_decoded_options_count);
1061 *decoded_options = new_decoded_options;
1062 *decoded_options_count = new_decoded_options_count;
1065 /* Handle option DECODED for the language indicated by LANG_MASK,
1066 using the handlers in HANDLERS and setting fields in OPTS and
1067 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1068 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1069 option for options from the source file, UNKNOWN_LOCATION
1070 otherwise. GENERATED_P is true for an option generated as part of
1071 processing another option or otherwise generated internally, false
1072 for one explicitly passed by the user. control_warning_option
1073 generated options are considered explicitly passed by the user.
1074 Returns false if the switch was invalid. DC is the diagnostic
1075 context for options affecting diagnostics state, or NULL. */
1077 static bool
1078 handle_option (struct gcc_options *opts,
1079 struct gcc_options *opts_set,
1080 const struct cl_decoded_option *decoded,
1081 unsigned int lang_mask, int kind, location_t loc,
1082 const struct cl_option_handlers *handlers,
1083 bool generated_p, diagnostic_context *dc)
1085 size_t opt_index = decoded->opt_index;
1086 const char *arg = decoded->arg;
1087 HOST_WIDE_INT value = decoded->value;
1088 const struct cl_option *option = &cl_options[opt_index];
1089 void *flag_var = option_flag_var (opt_index, opts);
1090 size_t i;
1092 if (flag_var)
1093 set_option (opts, (generated_p ? NULL : opts_set),
1094 opt_index, value, arg, kind, loc, dc);
1096 for (i = 0; i < handlers->num_handlers; i++)
1097 if (option->flags & handlers->handlers[i].mask)
1099 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1100 lang_mask, kind, loc,
1101 handlers, dc,
1102 handlers->target_option_override_hook))
1103 return false;
1106 return true;
1109 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1110 option instead of DECODED. This is used for callbacks when one
1111 option implies another instead of an option being decoded from the
1112 command line. */
1114 bool
1115 handle_generated_option (struct gcc_options *opts,
1116 struct gcc_options *opts_set,
1117 size_t opt_index, const char *arg, HOST_WIDE_INT value,
1118 unsigned int lang_mask, int kind, location_t loc,
1119 const struct cl_option_handlers *handlers,
1120 bool generated_p, diagnostic_context *dc)
1122 struct cl_decoded_option decoded;
1124 generate_option (opt_index, arg, value, lang_mask, &decoded);
1125 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1126 handlers, generated_p, dc);
1129 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1130 VALUE for a front end using LANG_MASK. This is used when the
1131 compiler generates options internally. */
1133 void
1134 generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
1135 unsigned int lang_mask, struct cl_decoded_option *decoded)
1137 const struct cl_option *option = &cl_options[opt_index];
1139 decoded->opt_index = opt_index;
1140 decoded->warn_message = NULL;
1141 decoded->arg = arg;
1142 decoded->value = value;
1143 decoded->errors = (option_ok_for_language (option, lang_mask)
1145 : CL_ERR_WRONG_LANG);
1147 generate_canonical_option (opt_index, arg, value, decoded);
1148 switch (decoded->canonical_option_num_elements)
1150 case 1:
1151 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1152 break;
1154 case 2:
1155 decoded->orig_option_with_args_text
1156 = opts_concat (decoded->canonical_option[0], " ",
1157 decoded->canonical_option[1], NULL);
1158 break;
1160 default:
1161 gcc_unreachable ();
1165 /* Fill in *DECODED with an option for input file FILE. */
1167 void
1168 generate_option_input_file (const char *file,
1169 struct cl_decoded_option *decoded)
1171 decoded->opt_index = OPT_SPECIAL_input_file;
1172 decoded->warn_message = NULL;
1173 decoded->arg = file;
1174 decoded->orig_option_with_args_text = file;
1175 decoded->canonical_option_num_elements = 1;
1176 decoded->canonical_option[0] = file;
1177 decoded->canonical_option[1] = NULL;
1178 decoded->canonical_option[2] = NULL;
1179 decoded->canonical_option[3] = NULL;
1180 decoded->value = 1;
1181 decoded->errors = 0;
1184 /* Helper function for listing valid choices and hint for misspelled
1185 value. CANDIDATES is a vector containing all valid strings,
1186 STR is set to a heap allocated string that contains all those
1187 strings concatenated, separated by spaces, and the return value
1188 is the closest string from those to ARG, or NULL if nothing is
1189 close enough. Callers should XDELETEVEC (STR) after using it
1190 to avoid memory leaks. */
1192 const char *
1193 candidates_list_and_hint (const char *arg, char *&str,
1194 const auto_vec <const char *> &candidates)
1196 size_t len = 0;
1197 int i;
1198 const char *candidate;
1199 char *p;
1201 FOR_EACH_VEC_ELT (candidates, i, candidate)
1202 len += strlen (candidate) + 1;
1204 str = p = XNEWVEC (char, len);
1205 FOR_EACH_VEC_ELT (candidates, i, candidate)
1207 len = strlen (candidate);
1208 memcpy (p, candidate, len);
1209 p[len] = ' ';
1210 p += len + 1;
1212 p[-1] = '\0';
1213 return find_closest_string (arg, &candidates);
1216 /* Perform diagnostics for read_cmdline_option and control_warning_option
1217 functions. Returns true if an error has been diagnosed.
1218 LOC and LANG_MASK arguments like in read_cmdline_option.
1219 OPTION is the option to report diagnostics for, OPT the name
1220 of the option as text, ARG the argument of the option (for joined
1221 options), ERRORS is bitmask of CL_ERR_* values. */
1223 static bool
1224 cmdline_handle_error (location_t loc, const struct cl_option *option,
1225 const char *opt, const char *arg, int errors,
1226 unsigned int lang_mask)
1228 if (errors & CL_ERR_DISABLED)
1230 error_at (loc, "command line option %qs"
1231 " is not supported by this configuration", opt);
1232 return true;
1235 if (errors & CL_ERR_MISSING_ARG)
1237 if (option->missing_argument_error)
1238 error_at (loc, option->missing_argument_error, opt);
1239 else
1240 error_at (loc, "missing argument to %qs", opt);
1241 return true;
1244 if (errors & CL_ERR_UINT_ARG)
1246 if (option->cl_byte_size)
1247 error_at (loc, "argument to %qs should be a non-negative integer "
1248 "optionally followed by a size unit",
1249 option->opt_text);
1250 else
1251 error_at (loc, "argument to %qs should be a non-negative integer",
1252 option->opt_text);
1253 return true;
1256 if (errors & CL_ERR_INT_RANGE_ARG)
1258 error_at (loc, "argument to %qs is not between %d and %d",
1259 option->opt_text, option->range_min, option->range_max);
1260 return true;
1263 if (errors & CL_ERR_ENUM_ARG)
1265 const struct cl_enum *e = &cl_enums[option->var_enum];
1266 unsigned int i;
1267 char *s;
1269 if (e->unknown_error)
1270 error_at (loc, e->unknown_error, arg);
1271 else
1272 error_at (loc, "unrecognized argument in option %qs", opt);
1274 auto_vec <const char *> candidates;
1275 for (i = 0; e->values[i].arg != NULL; i++)
1277 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1278 continue;
1279 candidates.safe_push (e->values[i].arg);
1281 const char *hint = candidates_list_and_hint (arg, s, candidates);
1282 if (hint)
1283 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1284 option->opt_text, s, hint);
1285 else
1286 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1287 XDELETEVEC (s);
1289 return true;
1292 return false;
1295 /* Handle the switch DECODED (location LOC) for the language indicated
1296 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1297 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1298 diagnostic options. */
1300 void
1301 read_cmdline_option (struct gcc_options *opts,
1302 struct gcc_options *opts_set,
1303 struct cl_decoded_option *decoded,
1304 location_t loc,
1305 unsigned int lang_mask,
1306 const struct cl_option_handlers *handlers,
1307 diagnostic_context *dc)
1309 const struct cl_option *option;
1310 const char *opt = decoded->orig_option_with_args_text;
1312 if (decoded->warn_message)
1313 warning_at (loc, 0, decoded->warn_message, opt);
1315 if (decoded->opt_index == OPT_SPECIAL_unknown)
1317 if (handlers->unknown_option_callback (decoded))
1318 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1319 return;
1322 if (decoded->opt_index == OPT_SPECIAL_ignore)
1323 return;
1325 if (decoded->opt_index == OPT_SPECIAL_deprecated)
1327 /* Warn only about positive ignored options. */
1328 if (decoded->value)
1329 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1330 return;
1333 option = &cl_options[decoded->opt_index];
1335 if (decoded->errors
1336 && cmdline_handle_error (loc, option, opt, decoded->arg,
1337 decoded->errors, lang_mask))
1338 return;
1340 if (decoded->errors & CL_ERR_WRONG_LANG)
1342 handlers->wrong_lang_callback (decoded, lang_mask);
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 || opt_index == OPT_SPECIAL_deprecated)
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);