xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/opts-common.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /* Command line option handling.
2    Copyright (C) 2006-2017 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
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.
10 
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.
15 
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/>.  */
19 
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"
28 
29 static void prune_options (struct cl_decoded_option **, unsigned int *);
30 
31 /* Perform a binary search to find which option the command-line INPUT
32    matches.  Returns its index in the option array, and
33    OPT_SPECIAL_unknown on failure.
34 
35    This routine is quite subtle.  A normal binary search is not good
36    enough because some options can be suffixed with an argument, and
37    multiple sub-matches can occur, e.g. input of "-pedantic" matching
38    the initial substring of "-pedantic-errors".
39 
40    A more complicated example is -gstabs.  It should match "-g" with
41    an argument of "stabs".  Suppose, however, that the number and list
42    of switches are such that the binary search tests "-gen-decls"
43    before having tested "-g".  This doesn't match, and as "-gen-decls"
44    is less than "-gstabs", it will become the lower bound of the
45    binary search range, and "-g" will never be seen.  To resolve this
46    issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
47    to "-g" so that failed searches that end between "-gen-decls" and
48    the lexicographically subsequent switch know to go back and see if
49    "-g" causes a match (which it does in this example).
50 
51    This search is done in such a way that the longest match for the
52    front end in question wins.  If there is no match for the current
53    front end, the longest match for a different front end is returned
54    (or N_OPTS if none) and the caller emits an error message.  */
55 size_t
56 find_opt (const char *input, unsigned int lang_mask)
57 {
58   size_t mn, mn_orig, mx, md, opt_len;
59   size_t match_wrong_lang;
60   int comp;
61 
62   mn = 0;
63   mx = cl_options_count;
64 
65   /* Find mn such this lexicographical inequality holds:
66      cl_options[mn] <= input < cl_options[mn + 1].  */
67   while (mx - mn > 1)
68     {
69       md = (mn + mx) / 2;
70       opt_len = cl_options[md].opt_len;
71       comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
72 
73       if (comp < 0)
74 	mx = md;
75       else
76 	mn = md;
77     }
78 
79   mn_orig = mn;
80 
81   /* This is the switch that is the best match but for a different
82      front end, or OPT_SPECIAL_unknown if there is no match at all.  */
83   match_wrong_lang = OPT_SPECIAL_unknown;
84 
85   /* Backtrace the chain of possible matches, returning the longest
86      one, if any, that fits best.  With current GCC switches, this
87      loop executes at most twice.  */
88   do
89     {
90       const struct cl_option *opt = &cl_options[mn];
91 
92       /* Is the input either an exact match or a prefix that takes a
93 	 joined argument?  */
94       if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
95 	  && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
96 	{
97 	  /* If language is OK, return it.  */
98 	  if (opt->flags & lang_mask)
99 	    return mn;
100 
101 	  /* If we haven't remembered a prior match, remember this
102 	     one.  Any prior match is necessarily better.  */
103 	  if (match_wrong_lang == OPT_SPECIAL_unknown)
104 	    match_wrong_lang = mn;
105 	}
106 
107       /* Try the next possibility.  This is cl_options_count if there
108 	 are no more.  */
109       mn = opt->back_chain;
110     }
111   while (mn != cl_options_count);
112 
113   if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
114     {
115       /* Long options, starting "--", may be abbreviated if the
116 	 abbreviation is unambiguous.  This only applies to options
117 	 not taking a joined argument, and abbreviations of "--option"
118 	 are permitted even if there is a variant "--option=".  */
119       size_t mnc = mn_orig + 1;
120       size_t cmp_len = strlen (input);
121       while (mnc < cl_options_count
122 	     && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
123 	{
124 	  /* Option matching this abbreviation.  OK if it is the first
125 	     match and that does not take a joined argument, or the
126 	     second match, taking a joined argument and with only '='
127 	     added to the first match; otherwise considered
128 	     ambiguous.  */
129 	  if (mnc == mn_orig + 1
130 	      && !(cl_options[mnc].flags & CL_JOINED))
131 	    match_wrong_lang = mnc;
132 	  else if (mnc == mn_orig + 2
133 		   && match_wrong_lang == mn_orig + 1
134 		   && (cl_options[mnc].flags & CL_JOINED)
135 		   && (cl_options[mnc].opt_len
136 		       == cl_options[mn_orig + 1].opt_len + 1)
137 		   && strncmp (cl_options[mnc].opt_text + 1,
138 			       cl_options[mn_orig + 1].opt_text + 1,
139 			       cl_options[mn_orig + 1].opt_len) == 0)
140 	    ; /* OK, as long as there are no more matches.  */
141 	  else
142 	    return OPT_SPECIAL_unknown;
143 	  mnc++;
144 	}
145     }
146 
147   /* Return the best wrong match, or OPT_SPECIAL_unknown if none.  */
148   return match_wrong_lang;
149 }
150 
151 /* If ARG is a non-negative decimal or hexadecimal integer, return its
152    value, otherwise return -1.  */
153 
154 int
155 integral_argument (const char *arg)
156 {
157   const char *p = arg;
158 
159   while (*p && ISDIGIT (*p))
160     p++;
161 
162   if (*p == '\0')
163     return atoi (arg);
164 
165   /* It wasn't a decimal number - try hexadecimal.  */
166   if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
167     {
168       p = arg + 2;
169       while (*p && ISXDIGIT (*p))
170 	p++;
171 
172       if (p != arg + 2 && *p == '\0')
173 	return strtol (arg, NULL, 16);
174     }
175 
176   return -1;
177 }
178 
179 /* Return whether OPTION is OK for the language given by
180    LANG_MASK.  */
181 static bool
182 option_ok_for_language (const struct cl_option *option,
183 			unsigned int lang_mask)
184 {
185   if (!(option->flags & lang_mask))
186     return false;
187   else if ((option->flags & CL_TARGET)
188 	   && (option->flags & (CL_LANG_ALL | CL_DRIVER))
189 	   && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
190     /* Complain for target flag language mismatches if any languages
191        are specified.  */
192     return false;
193   return true;
194 }
195 
196 /* Return whether ENUM_ARG is OK for the language given by
197    LANG_MASK.  */
198 
199 static bool
200 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
201 			  unsigned int lang_mask)
202 {
203   return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
204 }
205 
206 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
207    storing the value in *VALUE if found, and returning false without
208    modifying *VALUE if not found.  */
209 
210 static bool
211 enum_arg_to_value (const struct cl_enum_arg *enum_args,
212 		   const char *arg, int *value, unsigned int lang_mask)
213 {
214   unsigned int i;
215 
216   for (i = 0; enum_args[i].arg != NULL; i++)
217     if (strcmp (arg, enum_args[i].arg) == 0
218 	&& enum_arg_ok_for_language (&enum_args[i], lang_mask))
219       {
220 	*value = enum_args[i].value;
221 	return true;
222       }
223 
224   return false;
225 }
226 
227 /* Look up ARG in the enum used by option OPT_INDEX for language
228    LANG_MASK, returning true and storing the value in *VALUE if found,
229    and returning false without modifying *VALUE if not found.  */
230 
231 bool
232 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
233 		       unsigned int lang_mask)
234 {
235   const struct cl_option *option = &cl_options[opt_index];
236 
237   gcc_assert (option->var_type == CLVC_ENUM);
238 
239   return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
240 			    value, lang_mask);
241 }
242 
243 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
244    corresponding string in *ARGP, returning true if the found string
245    was marked as canonical, false otherwise.  If VALUE is not found
246    (which may be the case for uninitialized values if the relevant
247    option has not been passed), set *ARGP to NULL and return
248    false.  */
249 
250 bool
251 enum_value_to_arg (const struct cl_enum_arg *enum_args,
252 		   const char **argp, int value, unsigned int lang_mask)
253 {
254   unsigned int i;
255 
256   for (i = 0; enum_args[i].arg != NULL; i++)
257     if (enum_args[i].value == value
258 	&& (enum_args[i].flags & CL_ENUM_CANONICAL)
259 	&& enum_arg_ok_for_language (&enum_args[i], lang_mask))
260       {
261 	*argp = enum_args[i].arg;
262 	return true;
263       }
264 
265   for (i = 0; enum_args[i].arg != NULL; i++)
266     if (enum_args[i].value == value
267 	&& enum_arg_ok_for_language (&enum_args[i], lang_mask))
268       {
269 	*argp = enum_args[i].arg;
270 	return false;
271       }
272 
273   *argp = NULL;
274   return false;
275 }
276 
277 /* Fill in the canonical option part of *DECODED with an option
278    described by OPT_INDEX, ARG and VALUE.  */
279 
280 static void
281 generate_canonical_option (size_t opt_index, const char *arg, int value,
282 			   struct cl_decoded_option *decoded)
283 {
284   const struct cl_option *option = &cl_options[opt_index];
285   const char *opt_text = option->opt_text;
286 
287   if (value == 0
288       && !option->cl_reject_negative
289       && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
290     {
291       char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
292       t[0] = '-';
293       t[1] = opt_text[1];
294       t[2] = 'n';
295       t[3] = 'o';
296       t[4] = '-';
297       memcpy (t + 5, opt_text + 2, option->opt_len);
298       opt_text = t;
299     }
300 
301   decoded->canonical_option[2] = NULL;
302   decoded->canonical_option[3] = NULL;
303 
304   if (arg)
305     {
306       if ((option->flags & CL_SEPARATE)
307 	  && !option->cl_separate_alias)
308 	{
309 	  decoded->canonical_option[0] = opt_text;
310 	  decoded->canonical_option[1] = arg;
311 	  decoded->canonical_option_num_elements = 2;
312 	}
313       else
314 	{
315 	  gcc_assert (option->flags & CL_JOINED);
316 	  decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
317 	  decoded->canonical_option[1] = NULL;
318 	  decoded->canonical_option_num_elements = 1;
319 	}
320     }
321   else
322     {
323       decoded->canonical_option[0] = opt_text;
324       decoded->canonical_option[1] = NULL;
325       decoded->canonical_option_num_elements = 1;
326     }
327 }
328 
329 /* Structure describing mappings from options on the command line to
330    options to look up with find_opt.  */
331 struct option_map
332 {
333   /* Prefix of the option on the command line.  */
334   const char *opt0;
335   /* If two argv elements are considered to be merged into one option,
336      prefix for the second element, otherwise NULL.  */
337   const char *opt1;
338   /* The new prefix to map to.  */
339   const char *new_prefix;
340   /* Whether at least one character is needed following opt1 or opt0
341      for this mapping to be used.  (--optimize= is valid for -O, but
342      --warn- is not valid for -W.)  */
343   bool another_char_needed;
344   /* Whether the original option is a negated form of the option
345      resulting from this map.  */
346   bool negated;
347 };
348 static const struct option_map option_map[] =
349   {
350     { "-Wno-", NULL, "-W", false, true },
351     { "-fno-", NULL, "-f", false, true },
352     { "-mno-", NULL, "-m", false, true },
353     { "--debug=", NULL, "-g", false, false },
354     { "--machine-", NULL, "-m", true, false },
355     { "--machine-no-", NULL, "-m", false, true },
356     { "--machine=", NULL, "-m", false, false },
357     { "--machine=no-", NULL, "-m", false, true },
358     { "--machine", "", "-m", false, false },
359     { "--machine", "no-", "-m", false, true },
360     { "--optimize=", NULL, "-O", false, false },
361     { "--std=", NULL, "-std=", false, false },
362     { "--std", "", "-std=", false, false },
363     { "--warn-", NULL, "-W", true, false },
364     { "--warn-no-", NULL, "-W", false, true },
365     { "--", NULL, "-f", true, false },
366     { "--no-", NULL, "-f", false, true }
367   };
368 
369 /* Helper function for gcc.c's driver::suggest_option, for populating the
370    vec of suggestions for misspelled options.
371 
372    option_map above provides various prefixes for spelling command-line
373    options, which decode_cmdline_option uses to map spellings of options
374    to specific options.  We want to do the reverse: to find all the ways
375    that a user could validly spell an option.
376 
377    Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
378    of its valid variant spellings to CANDIDATES, each without a leading
379    dash.
380 
381    For example, given "-Wabi-tag", the following are added to CANDIDATES:
382      "Wabi-tag"
383      "Wno-abi-tag"
384      "-warn-abi-tag"
385      "-warn-no-abi-tag".
386 
387    The added strings must be freed using free.  */
388 
389 void
390 add_misspelling_candidates (auto_vec<char *> *candidates,
391 			    const struct cl_option *option,
392 			    const char *opt_text)
393 {
394   gcc_assert (candidates);
395   gcc_assert (option);
396   gcc_assert (opt_text);
397   candidates->safe_push (xstrdup (opt_text + 1));
398   for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
399     {
400       const char *opt0 = option_map[i].opt0;
401       const char *new_prefix = option_map[i].new_prefix;
402       size_t new_prefix_len = strlen (new_prefix);
403 
404       if (option->cl_reject_negative && option_map[i].negated)
405 	continue;
406 
407       if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
408 	{
409 	  char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
410 				      NULL);
411 	  candidates->safe_push (alternative);
412 	}
413     }
414 }
415 
416 /* Decode the switch beginning at ARGV for the language indicated by
417    LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
418    the structure *DECODED.  Returns the number of switches
419    consumed.  */
420 
421 static unsigned int
422 decode_cmdline_option (const char **argv, unsigned int lang_mask,
423 		       struct cl_decoded_option *decoded)
424 {
425   size_t opt_index;
426   const char *arg = 0;
427   int value = 1;
428   unsigned int result = 1, i, extra_args, separate_args = 0;
429   int adjust_len = 0;
430   size_t total_len;
431   char *p;
432   const struct cl_option *option;
433   int errors = 0;
434   const char *warn_message = NULL;
435   bool separate_arg_flag;
436   bool joined_arg_flag;
437   bool have_separate_arg = false;
438 
439   extra_args = 0;
440 
441   const char *opt_value = argv[0] + 1;
442   opt_index = find_opt (opt_value, lang_mask);
443   i = 0;
444   while (opt_index == OPT_SPECIAL_unknown
445 	 && i < ARRAY_SIZE (option_map))
446     {
447       const char *opt0 = option_map[i].opt0;
448       const char *opt1 = option_map[i].opt1;
449       const char *new_prefix = option_map[i].new_prefix;
450       bool another_char_needed = option_map[i].another_char_needed;
451       size_t opt0_len = strlen (opt0);
452       size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
453       size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
454       size_t new_prefix_len = strlen (new_prefix);
455 
456       extra_args = (opt1 == NULL ? 0 : 1);
457       value = !option_map[i].negated;
458 
459       if (strncmp (argv[0], opt0, opt0_len) == 0
460 	  && (opt1 == NULL
461 	      || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
462 	  && (!another_char_needed
463 	      || argv[extra_args][optn_len] != 0))
464 	{
465 	  size_t arglen = strlen (argv[extra_args]);
466 	  char *dup;
467 
468 	  adjust_len = (int) optn_len - (int) new_prefix_len;
469 	  dup = XNEWVEC (char, arglen + 1 - adjust_len);
470 	  memcpy (dup, new_prefix, new_prefix_len);
471 	  memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
472 		  arglen - optn_len + 1);
473 	  opt_index = find_opt (dup + 1, lang_mask);
474 	  free (dup);
475 	}
476       i++;
477     }
478 
479   if (opt_index == OPT_SPECIAL_unknown)
480     {
481       arg = argv[0];
482       extra_args = 0;
483       value = 1;
484       goto done;
485     }
486 
487   option = &cl_options[opt_index];
488 
489   /* Reject negative form of switches that don't take negatives as
490      unrecognized.  */
491   if (!value && option->cl_reject_negative)
492     {
493       opt_index = OPT_SPECIAL_unknown;
494       errors |= CL_ERR_NEGATIVE;
495       arg = argv[0];
496       goto done;
497     }
498 
499   result = extra_args + 1;
500   warn_message = option->warn_message;
501 
502   /* Check to see if the option is disabled for this configuration.  */
503   if (option->cl_disabled)
504     errors |= CL_ERR_DISABLED;
505 
506   /* Determine whether there may be a separate argument based on
507      whether this option is being processed for the driver, and, if
508      so, how many such arguments.  */
509   separate_arg_flag = ((option->flags & CL_SEPARATE)
510 		       && !(option->cl_no_driver_arg
511 			    && (lang_mask & CL_DRIVER)));
512   separate_args = (separate_arg_flag
513 		   ? option->cl_separate_nargs + 1
514 		   : 0);
515   joined_arg_flag = (option->flags & CL_JOINED) != 0;
516 
517   /* Sort out any argument the switch takes.  */
518   if (joined_arg_flag)
519     {
520       /* Have arg point to the original switch.  This is because
521 	 some code, such as disable_builtin_function, expects its
522 	 argument to be persistent until the program exits.  */
523       arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
524 
525       if (*arg == '\0' && !option->cl_missing_ok)
526 	{
527 	  if (separate_arg_flag)
528 	    {
529 	      arg = argv[extra_args + 1];
530 	      result = extra_args + 2;
531 	      if (arg == NULL)
532 		result = extra_args + 1;
533 	      else
534 		have_separate_arg = true;
535 	    }
536 	  else
537 	    /* Missing argument.  */
538 	    arg = NULL;
539 	}
540     }
541   else if (separate_arg_flag)
542     {
543       arg = argv[extra_args + 1];
544       for (i = 0; i < separate_args; i++)
545 	if (argv[extra_args + 1 + i] == NULL)
546 	  {
547 	    errors |= CL_ERR_MISSING_ARG;
548 	    break;
549 	  }
550       result = extra_args + 1 + i;
551       if (arg != NULL)
552 	have_separate_arg = true;
553     }
554 
555   if (arg == NULL && (separate_arg_flag || joined_arg_flag))
556     errors |= CL_ERR_MISSING_ARG;
557 
558   /* Is this option an alias (or an ignored option, marked as an alias
559      of OPT_SPECIAL_ignore)?  */
560   if (option->alias_target != N_OPTS
561       && (!option->cl_separate_alias || have_separate_arg))
562     {
563       size_t new_opt_index = option->alias_target;
564 
565       if (new_opt_index == OPT_SPECIAL_ignore)
566 	{
567 	  gcc_assert (option->alias_arg == NULL);
568 	  gcc_assert (option->neg_alias_arg == NULL);
569 	  opt_index = new_opt_index;
570 	  arg = NULL;
571 	  value = 1;
572 	}
573       else
574 	{
575 	  const struct cl_option *new_option = &cl_options[new_opt_index];
576 
577 	  /* The new option must not be an alias itself.  */
578 	  gcc_assert (new_option->alias_target == N_OPTS
579 		      || new_option->cl_separate_alias);
580 
581 	  if (option->neg_alias_arg)
582 	    {
583 	      gcc_assert (option->alias_arg != NULL);
584 	      gcc_assert (arg == NULL);
585 	      gcc_assert (!option->cl_negative_alias);
586 	      if (value)
587 		arg = option->alias_arg;
588 	      else
589 		arg = option->neg_alias_arg;
590 	      value = 1;
591 	    }
592 	  else if (option->alias_arg)
593 	    {
594 	      gcc_assert (value == 1);
595 	      gcc_assert (arg == NULL);
596 	      gcc_assert (!option->cl_negative_alias);
597 	      arg = option->alias_arg;
598 	    }
599 
600 	  if (option->cl_negative_alias)
601 	    value = !value;
602 
603 	  opt_index = new_opt_index;
604 	  option = new_option;
605 
606 	  if (value == 0)
607 	    gcc_assert (!option->cl_reject_negative);
608 
609 	  /* Recompute what arguments are allowed.  */
610 	  separate_arg_flag = ((option->flags & CL_SEPARATE)
611 			       && !(option->cl_no_driver_arg
612 				    && (lang_mask & CL_DRIVER)));
613 	  joined_arg_flag = (option->flags & CL_JOINED) != 0;
614 
615 	  if (separate_args > 1 || option->cl_separate_nargs)
616 	    gcc_assert (separate_args
617 			== (unsigned int) option->cl_separate_nargs + 1);
618 
619 	  if (!(errors & CL_ERR_MISSING_ARG))
620 	    {
621 	      if (separate_arg_flag || joined_arg_flag)
622 		{
623 		  if (option->cl_missing_ok && arg == NULL)
624 		    arg = "";
625 		  gcc_assert (arg != NULL);
626 		}
627 	      else
628 		gcc_assert (arg == NULL);
629 	    }
630 
631 	  /* Recheck for warnings and disabled options.  */
632 	  if (option->warn_message)
633 	    {
634 	      gcc_assert (warn_message == NULL);
635 	      warn_message = option->warn_message;
636 	    }
637 	  if (option->cl_disabled)
638 	    errors |= CL_ERR_DISABLED;
639 	}
640     }
641 
642   /* Check if this is a switch for a different front end.  */
643   if (!option_ok_for_language (option, lang_mask))
644     errors |= CL_ERR_WRONG_LANG;
645   else if (strcmp (option->opt_text, "-Werror=") == 0
646 	   && strchr (opt_value, ',') == NULL)
647     {
648       /* Verify that -Werror argument is a valid warning
649 	 for a language.  */
650       char *werror_arg = xstrdup (opt_value + 6);
651       werror_arg[0] = 'W';
652 
653       size_t warning_index = find_opt (werror_arg, lang_mask);
654       if (warning_index != OPT_SPECIAL_unknown)
655 	{
656 	  const struct cl_option *warning_option
657 	    = &cl_options[warning_index];
658 	  if (!option_ok_for_language (warning_option, lang_mask))
659 	    errors |= CL_ERR_WRONG_LANG;
660 	}
661     }
662 
663   /* Convert the argument to lowercase if appropriate.  */
664   if (arg && option->cl_tolower)
665     {
666       size_t j;
667       size_t len = strlen (arg);
668       char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
669 
670       for (j = 0; j < len; j++)
671 	arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
672       arg_lower[len] = 0;
673       arg = arg_lower;
674     }
675 
676   /* If the switch takes an integer, convert it.  */
677   if (arg && option->cl_uinteger)
678     {
679       value = integral_argument (arg);
680       if (value == -1)
681 	errors |= CL_ERR_UINT_ARG;
682     }
683 
684   /* If the switch takes an enumerated argument, convert it.  */
685   if (arg && (option->var_type == CLVC_ENUM))
686     {
687       const struct cl_enum *e = &cl_enums[option->var_enum];
688 
689       gcc_assert (value == 1);
690       if (enum_arg_to_value (e->values, arg, &value, lang_mask))
691 	{
692 	  const char *carg = NULL;
693 
694 	  if (enum_value_to_arg (e->values, &carg, value, lang_mask))
695 	    arg = carg;
696 	  gcc_assert (carg != NULL);
697 	}
698       else
699 	errors |= CL_ERR_ENUM_ARG;
700     }
701 
702  done:
703   decoded->opt_index = opt_index;
704   decoded->arg = arg;
705   decoded->value = value;
706   decoded->errors = errors;
707   decoded->warn_message = warn_message;
708 
709   if (opt_index == OPT_SPECIAL_unknown)
710     gcc_assert (result == 1);
711 
712   gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
713   decoded->canonical_option_num_elements = result;
714   total_len = 0;
715   for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
716     {
717       if (i < result)
718 	{
719 	  size_t len;
720 	  if (opt_index == OPT_SPECIAL_unknown)
721 	    decoded->canonical_option[i] = argv[i];
722 	  else
723 	    decoded->canonical_option[i] = NULL;
724 	  len = strlen (argv[i]);
725 	  /* If the argument is an empty string, we will print it as "" in
726 	     orig_option_with_args_text.  */
727 	  total_len += (len != 0 ? len : 2) + 1;
728 	}
729       else
730 	decoded->canonical_option[i] = NULL;
731     }
732   if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
733     {
734       generate_canonical_option (opt_index, arg, value, decoded);
735       if (separate_args > 1)
736 	{
737 	  for (i = 0; i < separate_args; i++)
738 	    {
739 	      if (argv[extra_args + 1 + i] == NULL)
740 		  break;
741 	      else
742 		decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
743 	    }
744 	  gcc_assert (result == 1 + i);
745 	  decoded->canonical_option_num_elements = result;
746 	}
747     }
748   decoded->orig_option_with_args_text
749     = p = XOBNEWVEC (&opts_obstack, char, total_len);
750   for (i = 0; i < result; i++)
751     {
752       size_t len = strlen (argv[i]);
753 
754       /* Print the empty string verbally.  */
755       if (len == 0)
756 	{
757 	  *p++ = '"';
758 	  *p++ = '"';
759 	}
760       else
761 	memcpy (p, argv[i], len);
762       p += len;
763       if (i == result - 1)
764 	*p++ = 0;
765       else
766 	*p++ = ' ';
767     }
768 
769   return result;
770 }
771 
772 /* Obstack for option strings.  */
773 
774 struct obstack opts_obstack;
775 
776 /* Like libiberty concat, but allocate using opts_obstack.  */
777 
778 char *
779 opts_concat (const char *first, ...)
780 {
781   char *newstr, *end;
782   size_t length = 0;
783   const char *arg;
784   va_list ap;
785 
786   /* First compute the size of the result and get sufficient memory.  */
787   va_start (ap, first);
788   for (arg = first; arg; arg = va_arg (ap, const char *))
789     length += strlen (arg);
790   newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
791   va_end (ap);
792 
793   /* Now copy the individual pieces to the result string. */
794   va_start (ap, first);
795   for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
796     {
797       length = strlen (arg);
798       memcpy (end, arg, length);
799       end += length;
800     }
801   *end = '\0';
802   va_end (ap);
803   return newstr;
804 }
805 
806 /* Decode command-line options (ARGC and ARGV being the arguments of
807    main) into an array, setting *DECODED_OPTIONS to a pointer to that
808    array and *DECODED_OPTIONS_COUNT to the number of entries in the
809    array.  The first entry in the array is always one for the program
810    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
811    flags applicable for decoding (including CL_COMMON and CL_TARGET if
812    those options should be considered applicable).  Do not produce any
813    diagnostics or set state outside of these variables.  */
814 
815 void
816 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
817 				 unsigned int lang_mask,
818 				 struct cl_decoded_option **decoded_options,
819 				 unsigned int *decoded_options_count)
820 {
821   unsigned int n, i;
822   struct cl_decoded_option *opt_array;
823   unsigned int num_decoded_options;
824 
825   opt_array = XNEWVEC (struct cl_decoded_option, argc);
826 
827   opt_array[0].opt_index = OPT_SPECIAL_program_name;
828   opt_array[0].warn_message = NULL;
829   opt_array[0].arg = argv[0];
830   opt_array[0].orig_option_with_args_text = argv[0];
831   opt_array[0].canonical_option_num_elements = 1;
832   opt_array[0].canonical_option[0] = argv[0];
833   opt_array[0].canonical_option[1] = NULL;
834   opt_array[0].canonical_option[2] = NULL;
835   opt_array[0].canonical_option[3] = NULL;
836   opt_array[0].value = 1;
837   opt_array[0].errors = 0;
838   num_decoded_options = 1;
839 
840   for (i = 1; i < argc; i += n)
841     {
842       const char *opt = argv[i];
843 
844       /* Interpret "-" or a non-switch as a file name.  */
845       if (opt[0] != '-' || opt[1] == '\0')
846 	{
847 	  generate_option_input_file (opt, &opt_array[num_decoded_options]);
848 	  num_decoded_options++;
849 	  n = 1;
850 	  continue;
851 	}
852 
853       n = decode_cmdline_option (argv + i, lang_mask,
854 				 &opt_array[num_decoded_options]);
855       num_decoded_options++;
856     }
857 
858   *decoded_options = opt_array;
859   *decoded_options_count = num_decoded_options;
860   prune_options (decoded_options, decoded_options_count);
861 }
862 
863 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
864    next one is the same as ORIG_NEXT_OPT_IDX.  */
865 
866 static bool
867 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
868 {
869   /* An option can be canceled by the same option or an option with
870      Negative.  */
871   if (cl_options [next_opt_idx].neg_index == opt_idx)
872     return true;
873 
874   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
875     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
876 			  orig_next_opt_idx);
877 
878   return false;
879 }
880 
881 /* Filter out options canceled by the ones after them.  */
882 
883 static void
884 prune_options (struct cl_decoded_option **decoded_options,
885 	       unsigned int *decoded_options_count)
886 {
887   unsigned int old_decoded_options_count = *decoded_options_count;
888   struct cl_decoded_option *old_decoded_options = *decoded_options;
889   unsigned int new_decoded_options_count;
890   struct cl_decoded_option *new_decoded_options
891     = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
892   unsigned int i;
893   const struct cl_option *option;
894   unsigned int fdiagnostics_color_idx = 0;
895 
896   /* Remove arguments which are negated by others after them.  */
897   new_decoded_options_count = 0;
898   for (i = 0; i < old_decoded_options_count; i++)
899     {
900       unsigned int j, opt_idx, next_opt_idx;
901 
902       if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
903 	goto keep;
904 
905       opt_idx = old_decoded_options[i].opt_index;
906       switch (opt_idx)
907 	{
908 	case OPT_SPECIAL_unknown:
909 	case OPT_SPECIAL_ignore:
910 	case OPT_SPECIAL_program_name:
911 	case OPT_SPECIAL_input_file:
912 	  goto keep;
913 
914 	/* Do not save OPT_fdiagnostics_color_, just remember the last one.  */
915 	case OPT_fdiagnostics_color_:
916 	  fdiagnostics_color_idx = i;
917 	  continue;
918 
919 	default:
920 	  gcc_assert (opt_idx < cl_options_count);
921 	  option = &cl_options[opt_idx];
922 	  if (option->neg_index < 0)
923 	    goto keep;
924 
925 	  /* Skip joined switches.  */
926 	  if ((option->flags & CL_JOINED))
927 	    goto keep;
928 
929 	  for (j = i + 1; j < old_decoded_options_count; j++)
930 	    {
931 	      if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
932 		continue;
933 	      next_opt_idx = old_decoded_options[j].opt_index;
934 	      if (next_opt_idx >= cl_options_count)
935 		continue;
936 	      if (cl_options[next_opt_idx].neg_index < 0)
937 		continue;
938 	      if ((cl_options[next_opt_idx].flags & CL_JOINED))
939 		  continue;
940 	      if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
941 		break;
942 	    }
943 	  if (j == old_decoded_options_count)
944 	    {
945 keep:
946 	      new_decoded_options[new_decoded_options_count]
947 		= old_decoded_options[i];
948 	      new_decoded_options_count++;
949 	    }
950 	  break;
951 	}
952     }
953 
954   if (fdiagnostics_color_idx >= 1)
955     {
956       /* We put the last -fdiagnostics-color= at the first position
957 	 after argv[0] so it can take effect immediately.  */
958       memmove (new_decoded_options + 2, new_decoded_options + 1,
959 	       sizeof (struct cl_decoded_option)
960 	       * (new_decoded_options_count - 1));
961       new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
962       new_decoded_options_count++;
963     }
964 
965   free (old_decoded_options);
966   new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
967 				    new_decoded_options,
968 				    new_decoded_options_count);
969   *decoded_options = new_decoded_options;
970   *decoded_options_count = new_decoded_options_count;
971 }
972 
973 /* Handle option DECODED for the language indicated by LANG_MASK,
974    using the handlers in HANDLERS and setting fields in OPTS and
975    OPTS_SET.  KIND is the diagnostic_t if this is a diagnostics
976    option, DK_UNSPECIFIED otherwise, and LOC is the location of the
977    option for options from the source file, UNKNOWN_LOCATION
978    otherwise.  GENERATED_P is true for an option generated as part of
979    processing another option or otherwise generated internally, false
980    for one explicitly passed by the user.  control_warning_option
981    generated options are considered explicitly passed by the user.
982    Returns false if the switch was invalid.  DC is the diagnostic
983    context for options affecting diagnostics state, or NULL.  */
984 
985 static bool
986 handle_option (struct gcc_options *opts,
987 	       struct gcc_options *opts_set,
988 	       const struct cl_decoded_option *decoded,
989 	       unsigned int lang_mask, int kind, location_t loc,
990 	       const struct cl_option_handlers *handlers,
991 	       bool generated_p, diagnostic_context *dc)
992 {
993   size_t opt_index = decoded->opt_index;
994   const char *arg = decoded->arg;
995   int value = decoded->value;
996   const struct cl_option *option = &cl_options[opt_index];
997   void *flag_var = option_flag_var (opt_index, opts);
998   size_t i;
999 
1000   if (flag_var)
1001     set_option (opts, (generated_p ? NULL : opts_set),
1002 		opt_index, value, arg, kind, loc, dc);
1003 
1004   for (i = 0; i < handlers->num_handlers; i++)
1005     if (option->flags & handlers->handlers[i].mask)
1006       {
1007 	if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1008 					    lang_mask, kind, loc,
1009 					    handlers, dc,
1010 					    handlers->target_option_override_hook))
1011 	  return false;
1012       }
1013 
1014   return true;
1015 }
1016 
1017 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1018    option instead of DECODED.  This is used for callbacks when one
1019    option implies another instead of an option being decoded from the
1020    command line.  */
1021 
1022 bool
1023 handle_generated_option (struct gcc_options *opts,
1024 			 struct gcc_options *opts_set,
1025 			 size_t opt_index, const char *arg, int value,
1026 			 unsigned int lang_mask, int kind, location_t loc,
1027 			 const struct cl_option_handlers *handlers,
1028 			 bool generated_p, diagnostic_context *dc)
1029 {
1030   struct cl_decoded_option decoded;
1031 
1032   generate_option (opt_index, arg, value, lang_mask, &decoded);
1033   return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1034 			handlers, generated_p, dc);
1035 }
1036 
1037 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1038    VALUE for a front end using LANG_MASK.  This is used when the
1039    compiler generates options internally.  */
1040 
1041 void
1042 generate_option (size_t opt_index, const char *arg, int value,
1043 		 unsigned int lang_mask, struct cl_decoded_option *decoded)
1044 {
1045   const struct cl_option *option = &cl_options[opt_index];
1046 
1047   decoded->opt_index = opt_index;
1048   decoded->warn_message = NULL;
1049   decoded->arg = arg;
1050   decoded->value = value;
1051   decoded->errors = (option_ok_for_language (option, lang_mask)
1052 		     ? 0
1053 		     : CL_ERR_WRONG_LANG);
1054 
1055   generate_canonical_option (opt_index, arg, value, decoded);
1056   switch (decoded->canonical_option_num_elements)
1057     {
1058     case 1:
1059       decoded->orig_option_with_args_text = decoded->canonical_option[0];
1060       break;
1061 
1062     case 2:
1063       decoded->orig_option_with_args_text
1064 	= opts_concat (decoded->canonical_option[0], " ",
1065 		       decoded->canonical_option[1], NULL);
1066       break;
1067 
1068     default:
1069       gcc_unreachable ();
1070     }
1071 }
1072 
1073 /* Fill in *DECODED with an option for input file FILE.  */
1074 
1075 void
1076 generate_option_input_file (const char *file,
1077 			    struct cl_decoded_option *decoded)
1078 {
1079   decoded->opt_index = OPT_SPECIAL_input_file;
1080   decoded->warn_message = NULL;
1081   decoded->arg = file;
1082   decoded->orig_option_with_args_text = file;
1083   decoded->canonical_option_num_elements = 1;
1084   decoded->canonical_option[0] = file;
1085   decoded->canonical_option[1] = NULL;
1086   decoded->canonical_option[2] = NULL;
1087   decoded->canonical_option[3] = NULL;
1088   decoded->value = 1;
1089   decoded->errors = 0;
1090 }
1091 
1092 /* Helper function for listing valid choices and hint for misspelled
1093    value.  CANDIDATES is a vector containing all valid strings,
1094    STR is set to a heap allocated string that contains all those
1095    strings concatenated, separated by spaces, and the return value
1096    is the closest string from those to ARG, or NULL if nothing is
1097    close enough.  Callers should XDELETEVEC (STR) after using it
1098    to avoid memory leaks.  */
1099 
1100 const char *
1101 candidates_list_and_hint (const char *arg, char *&str,
1102 			  const auto_vec <const char *> &candidates)
1103 {
1104   size_t len = 0;
1105   int i;
1106   const char *candidate;
1107   char *p;
1108 
1109   FOR_EACH_VEC_ELT (candidates, i, candidate)
1110     len += strlen (candidate) + 1;
1111 
1112   str = p = XNEWVEC (char, len);
1113   FOR_EACH_VEC_ELT (candidates, i, candidate)
1114     {
1115       len = strlen (candidate);
1116       memcpy (p, candidate, len);
1117       p[len] = ' ';
1118       p += len + 1;
1119     }
1120   p[-1] = '\0';
1121   return find_closest_string (arg, &candidates);
1122 }
1123 
1124 /* Perform diagnostics for read_cmdline_option and control_warning_option
1125    functions.  Returns true if an error has been diagnosed.
1126    LOC and LANG_MASK arguments like in read_cmdline_option.
1127    OPTION is the option to report diagnostics for, OPT the name
1128    of the option as text, ARG the argument of the option (for joined
1129    options), ERRORS is bitmask of CL_ERR_* values.  */
1130 
1131 static bool
1132 cmdline_handle_error (location_t loc, const struct cl_option *option,
1133 		      const char *opt, const char *arg, int errors,
1134 		      unsigned int lang_mask)
1135 {
1136   if (errors & CL_ERR_DISABLED)
1137     {
1138       error_at (loc, "command line option %qs"
1139 		     " is not supported by this configuration", opt);
1140       return true;
1141     }
1142 
1143   if (errors & CL_ERR_MISSING_ARG)
1144     {
1145       if (option->missing_argument_error)
1146 	error_at (loc, option->missing_argument_error, opt);
1147       else
1148 	error_at (loc, "missing argument to %qs", opt);
1149       return true;
1150     }
1151 
1152   if (errors & CL_ERR_UINT_ARG)
1153     {
1154       error_at (loc, "argument to %qs should be a non-negative integer",
1155 		option->opt_text);
1156       return true;
1157     }
1158 
1159   if (errors & CL_ERR_ENUM_ARG)
1160     {
1161       const struct cl_enum *e = &cl_enums[option->var_enum];
1162       unsigned int i;
1163       char *s;
1164 
1165       if (e->unknown_error)
1166 	error_at (loc, e->unknown_error, arg);
1167       else
1168 	error_at (loc, "unrecognized argument in option %qs", opt);
1169 
1170       auto_vec <const char *> candidates;
1171       for (i = 0; e->values[i].arg != NULL; i++)
1172 	{
1173 	  if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1174 	    continue;
1175 	  candidates.safe_push (e->values[i].arg);
1176 	}
1177       const char *hint = candidates_list_and_hint (arg, s, candidates);
1178       if (hint)
1179 	inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1180 		option->opt_text, s, hint);
1181       else
1182 	inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1183       XDELETEVEC (s);
1184 
1185       return true;
1186     }
1187 
1188   return false;
1189 }
1190 
1191 /* Handle the switch DECODED (location LOC) for the language indicated
1192    by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1193    OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1194    diagnostic options.  */
1195 
1196 void
1197 read_cmdline_option (struct gcc_options *opts,
1198 		     struct gcc_options *opts_set,
1199 		     struct cl_decoded_option *decoded,
1200 		     location_t loc,
1201 		     unsigned int lang_mask,
1202 		     const struct cl_option_handlers *handlers,
1203 		     diagnostic_context *dc)
1204 {
1205   const struct cl_option *option;
1206   const char *opt = decoded->orig_option_with_args_text;
1207 
1208   if (decoded->warn_message)
1209     warning_at (loc, 0, decoded->warn_message, opt);
1210 
1211   if (decoded->opt_index == OPT_SPECIAL_unknown)
1212     {
1213       if (handlers->unknown_option_callback (decoded))
1214 	error_at (loc, "unrecognized command line option %qs", decoded->arg);
1215       return;
1216     }
1217 
1218   if (decoded->opt_index == OPT_SPECIAL_ignore)
1219     return;
1220 
1221   option = &cl_options[decoded->opt_index];
1222 
1223   if (decoded->errors
1224       && cmdline_handle_error (loc, option, opt, decoded->arg,
1225 			       decoded->errors, lang_mask))
1226     return;
1227 
1228   if (decoded->errors & CL_ERR_WRONG_LANG)
1229     {
1230       handlers->wrong_lang_callback (decoded, lang_mask);
1231       return;
1232     }
1233 
1234   gcc_assert (!decoded->errors);
1235 
1236   if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1237 		      loc, handlers, false, dc))
1238     error_at (loc, "unrecognized command line option %qs", opt);
1239 }
1240 
1241 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1242    OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1243    location LOC, using diagnostic context DC if not NULL for
1244    diagnostic classification.  */
1245 
1246 void
1247 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1248 	    int opt_index, int value, const char *arg, int kind,
1249 	    location_t loc, diagnostic_context *dc)
1250 {
1251   const struct cl_option *option = &cl_options[opt_index];
1252   void *flag_var = option_flag_var (opt_index, opts);
1253   void *set_flag_var = NULL;
1254 
1255   if (!flag_var)
1256     return;
1257 
1258   if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1259     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1260 
1261   if (opts_set != NULL)
1262     set_flag_var = option_flag_var (opt_index, opts_set);
1263 
1264   switch (option->var_type)
1265     {
1266     case CLVC_BOOLEAN:
1267 	*(int *) flag_var = value;
1268 	if (set_flag_var)
1269 	  *(int *) set_flag_var = 1;
1270 	break;
1271 
1272     case CLVC_EQUAL:
1273 	if (option->cl_host_wide_int)
1274 	  *(HOST_WIDE_INT *) flag_var = (value
1275 					 ? option->var_value
1276 					 : !option->var_value);
1277 	else
1278 	  *(int *) flag_var = (value
1279 			       ? option->var_value
1280 			       : !option->var_value);
1281 	if (set_flag_var)
1282 	  *(int *) set_flag_var = 1;
1283 	break;
1284 
1285     case CLVC_BIT_CLEAR:
1286     case CLVC_BIT_SET:
1287 	if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1288 	  {
1289 	    if (option->cl_host_wide_int)
1290 	      *(HOST_WIDE_INT *) flag_var |= option->var_value;
1291 	    else
1292 	      *(int *) flag_var |= option->var_value;
1293 	  }
1294 	else
1295 	  {
1296 	    if (option->cl_host_wide_int)
1297 	      *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1298 	    else
1299 	      *(int *) flag_var &= ~option->var_value;
1300 	  }
1301 	if (set_flag_var)
1302 	  {
1303 	    if (option->cl_host_wide_int)
1304 	      *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1305 	    else
1306 	      *(int *) set_flag_var |= option->var_value;
1307 	  }
1308 	break;
1309 
1310     case CLVC_STRING:
1311 	*(const char **) flag_var = arg;
1312 	if (set_flag_var)
1313 	  *(const char **) set_flag_var = "";
1314 	break;
1315 
1316     case CLVC_ENUM:
1317       {
1318 	const struct cl_enum *e = &cl_enums[option->var_enum];
1319 
1320 	e->set (flag_var, value);
1321 	if (set_flag_var)
1322 	  e->set (set_flag_var, 1);
1323       }
1324       break;
1325 
1326     case CLVC_DEFER:
1327 	{
1328 	  vec<cl_deferred_option> *v
1329 	    = (vec<cl_deferred_option> *) *(void **) flag_var;
1330 	  cl_deferred_option p = {opt_index, arg, value};
1331 	  if (!v)
1332 	    v = XCNEW (vec<cl_deferred_option>);
1333 	  v->safe_push (p);
1334 	  *(void **) flag_var = v;
1335 	  if (set_flag_var)
1336 	    *(void **) set_flag_var = v;
1337 	}
1338 	break;
1339     }
1340 }
1341 
1342 /* Return the address of the flag variable for option OPT_INDEX in
1343    options structure OPTS, or NULL if there is no flag variable.  */
1344 
1345 void *
1346 option_flag_var (int opt_index, struct gcc_options *opts)
1347 {
1348   const struct cl_option *option = &cl_options[opt_index];
1349 
1350   if (option->flag_var_offset == (unsigned short) -1)
1351     return NULL;
1352   return (void *)(((char *) opts) + option->flag_var_offset);
1353 }
1354 
1355 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1356    or -1 if it isn't a simple on-off switch.  */
1357 
1358 int
1359 option_enabled (int opt_idx, void *opts)
1360 {
1361   const struct cl_option *option = &(cl_options[opt_idx]);
1362   struct gcc_options *optsg = (struct gcc_options *) opts;
1363   void *flag_var = option_flag_var (opt_idx, optsg);
1364 
1365   if (flag_var)
1366     switch (option->var_type)
1367       {
1368       case CLVC_BOOLEAN:
1369 	return *(int *) flag_var != 0;
1370 
1371       case CLVC_EQUAL:
1372 	if (option->cl_host_wide_int)
1373 	  return *(HOST_WIDE_INT *) flag_var == option->var_value;
1374 	else
1375 	  return *(int *) flag_var == option->var_value;
1376 
1377       case CLVC_BIT_CLEAR:
1378 	if (option->cl_host_wide_int)
1379 	  return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1380 	else
1381 	  return (*(int *) flag_var & option->var_value) == 0;
1382 
1383       case CLVC_BIT_SET:
1384 	if (option->cl_host_wide_int)
1385 	  return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1386 	else
1387 	  return (*(int *) flag_var & option->var_value) != 0;
1388 
1389       case CLVC_STRING:
1390       case CLVC_ENUM:
1391       case CLVC_DEFER:
1392 	break;
1393       }
1394   return -1;
1395 }
1396 
1397 /* Fill STATE with the current state of option OPTION in OPTS.  Return
1398    true if there is some state to store.  */
1399 
1400 bool
1401 get_option_state (struct gcc_options *opts, int option,
1402 		  struct cl_option_state *state)
1403 {
1404   void *flag_var = option_flag_var (option, opts);
1405 
1406   if (flag_var == 0)
1407     return false;
1408 
1409   switch (cl_options[option].var_type)
1410     {
1411     case CLVC_BOOLEAN:
1412     case CLVC_EQUAL:
1413       state->data = flag_var;
1414       state->size = (cl_options[option].cl_host_wide_int
1415 		     ? sizeof (HOST_WIDE_INT)
1416 		     : sizeof (int));
1417       break;
1418 
1419     case CLVC_BIT_CLEAR:
1420     case CLVC_BIT_SET:
1421       state->ch = option_enabled (option, opts);
1422       state->data = &state->ch;
1423       state->size = 1;
1424       break;
1425 
1426     case CLVC_STRING:
1427       state->data = *(const char **) flag_var;
1428       if (state->data == 0)
1429 	state->data = "";
1430       state->size = strlen ((const char *) state->data) + 1;
1431       break;
1432 
1433     case CLVC_ENUM:
1434       state->data = flag_var;
1435       state->size = cl_enums[cl_options[option].var_enum].var_size;
1436       break;
1437 
1438     case CLVC_DEFER:
1439       return false;
1440     }
1441   return true;
1442 }
1443 
1444 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1445    handlers HANDLERS) to have diagnostic kind KIND for option
1446    structures OPTS and OPTS_SET and diagnostic context DC (possibly
1447    NULL), at location LOC (UNKNOWN_LOCATION for -Werror=).  ARG is the
1448    argument of the option for joined options, or NULL otherwise.  If IMPLY,
1449    the warning option in question is implied at this point.  This is
1450    used by -Werror= and #pragma GCC diagnostic.  */
1451 
1452 void
1453 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1454 			bool imply, location_t loc, unsigned int lang_mask,
1455 			const struct cl_option_handlers *handlers,
1456 			struct gcc_options *opts,
1457 			struct gcc_options *opts_set,
1458 			diagnostic_context *dc)
1459 {
1460   if (cl_options[opt_index].alias_target != N_OPTS)
1461     {
1462       gcc_assert (!cl_options[opt_index].cl_separate_alias
1463 		  && !cl_options[opt_index].cl_negative_alias);
1464       if (cl_options[opt_index].alias_arg)
1465 	arg = cl_options[opt_index].alias_arg;
1466       opt_index = cl_options[opt_index].alias_target;
1467     }
1468   if (opt_index == OPT_SPECIAL_ignore)
1469     return;
1470   if (dc)
1471     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1472   if (imply)
1473     {
1474       const struct cl_option *option = &cl_options[opt_index];
1475 
1476       /* -Werror=foo implies -Wfoo.  */
1477       if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1478 	{
1479 	  int value = 1;
1480 
1481 	  if (arg && *arg == '\0' && !option->cl_missing_ok)
1482 	    arg = NULL;
1483 
1484 	  if ((option->flags & CL_JOINED) && arg == NULL)
1485 	    {
1486 	      cmdline_handle_error (loc, option, option->opt_text, arg,
1487 				    CL_ERR_MISSING_ARG, lang_mask);
1488 	      return;
1489 	    }
1490 
1491 	  /* If the switch takes an integer, convert it.  */
1492 	  if (arg && option->cl_uinteger)
1493 	    {
1494 	      value = integral_argument (arg);
1495 	      if (value == -1)
1496 		{
1497 		  cmdline_handle_error (loc, option, option->opt_text, arg,
1498 					CL_ERR_UINT_ARG, lang_mask);
1499 		  return;
1500 		}
1501 	    }
1502 
1503 	  /* If the switch takes an enumerated argument, convert it.  */
1504 	  if (arg && option->var_type == CLVC_ENUM)
1505 	    {
1506 	      const struct cl_enum *e = &cl_enums[option->var_enum];
1507 
1508 	      if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1509 		{
1510 		  const char *carg = NULL;
1511 
1512 		  if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1513 		    arg = carg;
1514 		  gcc_assert (carg != NULL);
1515 		}
1516 	      else
1517 		{
1518 		  cmdline_handle_error (loc, option, option->opt_text, arg,
1519 					CL_ERR_ENUM_ARG, lang_mask);
1520 		  return;
1521 		}
1522 	    }
1523 
1524 	  handle_generated_option (opts, opts_set,
1525 				   opt_index, arg, value, lang_mask,
1526 				   kind, loc, handlers, false, dc);
1527 	}
1528     }
1529 }
1530