* fold-const.c (c_getstr): Clamp STRING_LENGTH to STRING_SIZE.
[official-gcc.git] / gcc / opts-common.c
blob91586022f7f9d053ab3f127779dc0c5e7f230bed
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 auto_diagnostic_group d;
1270 if (e->unknown_error)
1271 error_at (loc, e->unknown_error, arg);
1272 else
1273 error_at (loc, "unrecognized argument in option %qs", opt);
1275 auto_vec <const char *> candidates;
1276 for (i = 0; e->values[i].arg != NULL; i++)
1278 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1279 continue;
1280 candidates.safe_push (e->values[i].arg);
1282 const char *hint = candidates_list_and_hint (arg, s, candidates);
1283 if (hint)
1284 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1285 option->opt_text, s, hint);
1286 else
1287 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1288 XDELETEVEC (s);
1290 return true;
1293 return false;
1296 /* Handle the switch DECODED (location LOC) for the language indicated
1297 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1298 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1299 diagnostic options. */
1301 void
1302 read_cmdline_option (struct gcc_options *opts,
1303 struct gcc_options *opts_set,
1304 struct cl_decoded_option *decoded,
1305 location_t loc,
1306 unsigned int lang_mask,
1307 const struct cl_option_handlers *handlers,
1308 diagnostic_context *dc)
1310 const struct cl_option *option;
1311 const char *opt = decoded->orig_option_with_args_text;
1313 if (decoded->warn_message)
1314 warning_at (loc, 0, decoded->warn_message, opt);
1316 if (decoded->opt_index == OPT_SPECIAL_unknown)
1318 if (handlers->unknown_option_callback (decoded))
1319 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1320 return;
1323 if (decoded->opt_index == OPT_SPECIAL_ignore)
1324 return;
1326 if (decoded->opt_index == OPT_SPECIAL_deprecated)
1328 /* Warn only about positive ignored options. */
1329 if (decoded->value)
1330 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1331 return;
1334 option = &cl_options[decoded->opt_index];
1336 if (decoded->errors
1337 && cmdline_handle_error (loc, option, opt, decoded->arg,
1338 decoded->errors, lang_mask))
1339 return;
1341 if (decoded->errors & CL_ERR_WRONG_LANG)
1343 handlers->wrong_lang_callback (decoded, lang_mask);
1344 return;
1347 gcc_assert (!decoded->errors);
1349 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1350 loc, handlers, false, dc))
1351 error_at (loc, "unrecognized command line option %qs", opt);
1354 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1355 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1356 location LOC, using diagnostic context DC if not NULL for
1357 diagnostic classification. */
1359 void
1360 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1361 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
1362 location_t loc, diagnostic_context *dc)
1364 const struct cl_option *option = &cl_options[opt_index];
1365 void *flag_var = option_flag_var (opt_index, opts);
1366 void *set_flag_var = NULL;
1368 if (!flag_var)
1369 return;
1371 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1372 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1374 if (opts_set != NULL)
1375 set_flag_var = option_flag_var (opt_index, opts_set);
1377 switch (option->var_type)
1379 case CLVC_BOOLEAN:
1380 if (option->cl_host_wide_int)
1382 *(HOST_WIDE_INT *) flag_var = value;
1383 if (set_flag_var)
1384 *(HOST_WIDE_INT *) set_flag_var = 1;
1386 else
1388 *(int *) flag_var = value;
1389 if (set_flag_var)
1390 *(int *) set_flag_var = 1;
1393 break;
1395 case CLVC_SIZE:
1396 if (option->cl_host_wide_int)
1398 *(HOST_WIDE_INT *) flag_var = value;
1399 if (set_flag_var)
1400 *(HOST_WIDE_INT *) set_flag_var = value;
1402 else
1404 *(int *) flag_var = value;
1405 if (set_flag_var)
1406 *(int *) set_flag_var = value;
1409 break;
1411 case CLVC_EQUAL:
1412 if (option->cl_host_wide_int)
1414 *(HOST_WIDE_INT *) flag_var = (value
1415 ? option->var_value
1416 : !option->var_value);
1417 if (set_flag_var)
1418 *(HOST_WIDE_INT *) set_flag_var = 1;
1420 else
1422 *(int *) flag_var = (value
1423 ? option->var_value
1424 : !option->var_value);
1425 if (set_flag_var)
1426 *(int *) set_flag_var = 1;
1428 break;
1430 case CLVC_BIT_CLEAR:
1431 case CLVC_BIT_SET:
1432 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1434 if (option->cl_host_wide_int)
1435 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1436 else
1437 *(int *) flag_var |= option->var_value;
1439 else
1441 if (option->cl_host_wide_int)
1442 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1443 else
1444 *(int *) flag_var &= ~option->var_value;
1446 if (set_flag_var)
1448 if (option->cl_host_wide_int)
1449 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1450 else
1451 *(int *) set_flag_var |= option->var_value;
1453 break;
1455 case CLVC_STRING:
1456 *(const char **) flag_var = arg;
1457 if (set_flag_var)
1458 *(const char **) set_flag_var = "";
1459 break;
1461 case CLVC_ENUM:
1463 const struct cl_enum *e = &cl_enums[option->var_enum];
1465 e->set (flag_var, value);
1466 if (set_flag_var)
1467 e->set (set_flag_var, 1);
1469 break;
1471 case CLVC_DEFER:
1473 vec<cl_deferred_option> *v
1474 = (vec<cl_deferred_option> *) *(void **) flag_var;
1475 cl_deferred_option p = {opt_index, arg, value};
1476 if (!v)
1477 v = XCNEW (vec<cl_deferred_option>);
1478 v->safe_push (p);
1479 *(void **) flag_var = v;
1480 if (set_flag_var)
1481 *(void **) set_flag_var = v;
1483 break;
1487 /* Return the address of the flag variable for option OPT_INDEX in
1488 options structure OPTS, or NULL if there is no flag variable. */
1490 void *
1491 option_flag_var (int opt_index, struct gcc_options *opts)
1493 const struct cl_option *option = &cl_options[opt_index];
1495 if (option->flag_var_offset == (unsigned short) -1)
1496 return NULL;
1497 return (void *)(((char *) opts) + option->flag_var_offset);
1500 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1501 or -1 if it isn't a simple on-off switch. */
1504 option_enabled (int opt_idx, void *opts)
1506 const struct cl_option *option = &(cl_options[opt_idx]);
1507 struct gcc_options *optsg = (struct gcc_options *) opts;
1508 void *flag_var = option_flag_var (opt_idx, optsg);
1510 if (flag_var)
1511 switch (option->var_type)
1513 case CLVC_BOOLEAN:
1514 if (option->cl_host_wide_int)
1515 return *(HOST_WIDE_INT *) flag_var != 0;
1516 else
1517 return *(int *) flag_var != 0;
1519 case CLVC_EQUAL:
1520 if (option->cl_host_wide_int)
1521 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1522 else
1523 return *(int *) flag_var == option->var_value;
1525 case CLVC_BIT_CLEAR:
1526 if (option->cl_host_wide_int)
1527 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1528 else
1529 return (*(int *) flag_var & option->var_value) == 0;
1531 case CLVC_BIT_SET:
1532 if (option->cl_host_wide_int)
1533 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1534 else
1535 return (*(int *) flag_var & option->var_value) != 0;
1537 case CLVC_SIZE:
1538 if (option->cl_host_wide_int)
1539 return *(HOST_WIDE_INT *) flag_var != -1;
1540 else
1541 return *(int *) flag_var != -1;
1543 case CLVC_STRING:
1544 case CLVC_ENUM:
1545 case CLVC_DEFER:
1546 break;
1548 return -1;
1551 /* Fill STATE with the current state of option OPTION in OPTS. Return
1552 true if there is some state to store. */
1554 bool
1555 get_option_state (struct gcc_options *opts, int option,
1556 struct cl_option_state *state)
1558 void *flag_var = option_flag_var (option, opts);
1560 if (flag_var == 0)
1561 return false;
1563 switch (cl_options[option].var_type)
1565 case CLVC_BOOLEAN:
1566 case CLVC_EQUAL:
1567 case CLVC_SIZE:
1568 state->data = flag_var;
1569 state->size = (cl_options[option].cl_host_wide_int
1570 ? sizeof (HOST_WIDE_INT)
1571 : sizeof (int));
1572 break;
1574 case CLVC_BIT_CLEAR:
1575 case CLVC_BIT_SET:
1576 state->ch = option_enabled (option, opts);
1577 state->data = &state->ch;
1578 state->size = 1;
1579 break;
1581 case CLVC_STRING:
1582 state->data = *(const char **) flag_var;
1583 if (state->data == 0)
1584 state->data = "";
1585 state->size = strlen ((const char *) state->data) + 1;
1586 break;
1588 case CLVC_ENUM:
1589 state->data = flag_var;
1590 state->size = cl_enums[cl_options[option].var_enum].var_size;
1591 break;
1593 case CLVC_DEFER:
1594 return false;
1596 return true;
1599 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1600 handlers HANDLERS) to have diagnostic kind KIND for option
1601 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1602 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1603 argument of the option for joined options, or NULL otherwise. If IMPLY,
1604 the warning option in question is implied at this point. This is
1605 used by -Werror= and #pragma GCC diagnostic. */
1607 void
1608 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1609 bool imply, location_t loc, unsigned int lang_mask,
1610 const struct cl_option_handlers *handlers,
1611 struct gcc_options *opts,
1612 struct gcc_options *opts_set,
1613 diagnostic_context *dc)
1615 if (cl_options[opt_index].alias_target != N_OPTS)
1617 gcc_assert (!cl_options[opt_index].cl_separate_alias
1618 && !cl_options[opt_index].cl_negative_alias);
1619 if (cl_options[opt_index].alias_arg)
1620 arg = cl_options[opt_index].alias_arg;
1621 opt_index = cl_options[opt_index].alias_target;
1623 if (opt_index == OPT_SPECIAL_ignore || opt_index == OPT_SPECIAL_deprecated)
1624 return;
1625 if (dc)
1626 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1627 if (imply)
1629 const struct cl_option *option = &cl_options[opt_index];
1631 /* -Werror=foo implies -Wfoo. */
1632 if (option->var_type == CLVC_BOOLEAN
1633 || option->var_type == CLVC_ENUM
1634 || option->var_type == CLVC_SIZE)
1636 HOST_WIDE_INT value = 1;
1638 if (arg && *arg == '\0' && !option->cl_missing_ok)
1639 arg = NULL;
1641 if ((option->flags & CL_JOINED) && arg == NULL)
1643 cmdline_handle_error (loc, option, option->opt_text, arg,
1644 CL_ERR_MISSING_ARG, lang_mask);
1645 return;
1648 /* If the switch takes an integer argument, convert it. */
1649 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
1651 int error = 0;
1652 value = *arg ? integral_argument (arg, &error,
1653 option->cl_byte_size) : 0;
1654 if (error)
1656 cmdline_handle_error (loc, option, option->opt_text, arg,
1657 CL_ERR_UINT_ARG, lang_mask);
1658 return;
1662 /* If the switch takes an enumerated argument, convert it. */
1663 if (arg && option->var_type == CLVC_ENUM)
1665 const struct cl_enum *e = &cl_enums[option->var_enum];
1667 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1669 const char *carg = NULL;
1671 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1672 arg = carg;
1673 gcc_assert (carg != NULL);
1675 else
1677 cmdline_handle_error (loc, option, option->opt_text, arg,
1678 CL_ERR_ENUM_ARG, lang_mask);
1679 return;
1683 handle_generated_option (opts, opts_set,
1684 opt_index, arg, value, lang_mask,
1685 kind, loc, handlers, false, dc);