MAINTAINERS (Write After Approval): Add myself.
[platform/upstream/gcc.git] / gcc / opts-common.c
1 /* Command line option handling.
2    Copyright (C) 2006-2016 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), add it and all of its valid
378    variant spellings to CANDIDATES, each without a leading dash.
379
380    For example, given "-Wabi-tag", the following are added to CANDIDATES:
381      "Wabi-tag"
382      "Wno-abi-tag"
383      "-warn-abi-tag"
384      "-warn-no-abi-tag".
385
386    The added strings must be freed using free.  */
387
388 void
389 add_misspelling_candidates (auto_vec<char *> *candidates,
390                             const char *opt_text)
391 {
392   gcc_assert (candidates);
393   gcc_assert (opt_text);
394   candidates->safe_push (xstrdup (opt_text + 1));
395   for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
396     {
397       const char *opt0 = option_map[i].opt0;
398       const char *new_prefix = option_map[i].new_prefix;
399       size_t new_prefix_len = strlen (new_prefix);
400
401       if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
402         {
403           char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
404                                       NULL);
405           candidates->safe_push (alternative);
406         }
407     }
408 }
409
410 /* Decode the switch beginning at ARGV for the language indicated by
411    LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
412    the structure *DECODED.  Returns the number of switches
413    consumed.  */
414
415 static unsigned int
416 decode_cmdline_option (const char **argv, unsigned int lang_mask,
417                        struct cl_decoded_option *decoded)
418 {
419   size_t opt_index;
420   const char *arg = 0;
421   int value = 1;
422   unsigned int result = 1, i, extra_args, separate_args = 0;
423   int adjust_len = 0;
424   size_t total_len;
425   char *p;
426   const struct cl_option *option;
427   int errors = 0;
428   const char *warn_message = NULL;
429   bool separate_arg_flag;
430   bool joined_arg_flag;
431   bool have_separate_arg = false;
432
433   extra_args = 0;
434
435   opt_index = find_opt (argv[0] + 1, lang_mask);
436   i = 0;
437   while (opt_index == OPT_SPECIAL_unknown
438          && i < ARRAY_SIZE (option_map))
439     {
440       const char *opt0 = option_map[i].opt0;
441       const char *opt1 = option_map[i].opt1;
442       const char *new_prefix = option_map[i].new_prefix;
443       bool another_char_needed = option_map[i].another_char_needed;
444       size_t opt0_len = strlen (opt0);
445       size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
446       size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
447       size_t new_prefix_len = strlen (new_prefix);
448
449       extra_args = (opt1 == NULL ? 0 : 1);
450       value = !option_map[i].negated;
451
452       if (strncmp (argv[0], opt0, opt0_len) == 0
453           && (opt1 == NULL
454               || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
455           && (!another_char_needed
456               || argv[extra_args][optn_len] != 0))
457         {
458           size_t arglen = strlen (argv[extra_args]);
459           char *dup;
460
461           adjust_len = (int) optn_len - (int) new_prefix_len;
462           dup = XNEWVEC (char, arglen + 1 - adjust_len);
463           memcpy (dup, new_prefix, new_prefix_len);
464           memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
465                   arglen - optn_len + 1);
466           opt_index = find_opt (dup + 1, lang_mask);
467           free (dup);
468         }
469       i++;
470     }
471
472   if (opt_index == OPT_SPECIAL_unknown)
473     {
474       arg = argv[0];
475       extra_args = 0;
476       value = 1;
477       goto done;
478     }
479
480   option = &cl_options[opt_index];
481
482   /* Reject negative form of switches that don't take negatives as
483      unrecognized.  */
484   if (!value && option->cl_reject_negative)
485     {
486       opt_index = OPT_SPECIAL_unknown;
487       errors |= CL_ERR_NEGATIVE;
488       arg = argv[0];
489       goto done;
490     }
491
492   result = extra_args + 1;
493   warn_message = option->warn_message;
494
495   /* Check to see if the option is disabled for this configuration.  */
496   if (option->cl_disabled)
497     errors |= CL_ERR_DISABLED;
498
499   /* Determine whether there may be a separate argument based on
500      whether this option is being processed for the driver, and, if
501      so, how many such arguments.  */
502   separate_arg_flag = ((option->flags & CL_SEPARATE)
503                        && !(option->cl_no_driver_arg
504                             && (lang_mask & CL_DRIVER)));
505   separate_args = (separate_arg_flag
506                    ? option->cl_separate_nargs + 1
507                    : 0);
508   joined_arg_flag = (option->flags & CL_JOINED) != 0;
509
510   /* Sort out any argument the switch takes.  */
511   if (joined_arg_flag)
512     {
513       /* Have arg point to the original switch.  This is because
514          some code, such as disable_builtin_function, expects its
515          argument to be persistent until the program exits.  */
516       arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
517
518       if (*arg == '\0' && !option->cl_missing_ok)
519         {
520           if (separate_arg_flag)
521             {
522               arg = argv[extra_args + 1];
523               result = extra_args + 2;
524               if (arg == NULL)
525                 result = extra_args + 1;
526               else
527                 have_separate_arg = true;
528             }
529           else
530             /* Missing argument.  */
531             arg = NULL;
532         }
533     }
534   else if (separate_arg_flag)
535     {
536       arg = argv[extra_args + 1];
537       for (i = 0; i < separate_args; i++)
538         if (argv[extra_args + 1 + i] == NULL)
539           {
540             errors |= CL_ERR_MISSING_ARG;
541             break;
542           }
543       result = extra_args + 1 + i;
544       if (arg != NULL)
545         have_separate_arg = true;
546     }
547
548   if (arg == NULL && (separate_arg_flag || joined_arg_flag))
549     errors |= CL_ERR_MISSING_ARG;
550
551   /* Is this option an alias (or an ignored option, marked as an alias
552      of OPT_SPECIAL_ignore)?  */
553   if (option->alias_target != N_OPTS
554       && (!option->cl_separate_alias || have_separate_arg))
555     {
556       size_t new_opt_index = option->alias_target;
557
558       if (new_opt_index == OPT_SPECIAL_ignore)
559         {
560           gcc_assert (option->alias_arg == NULL);
561           gcc_assert (option->neg_alias_arg == NULL);
562           opt_index = new_opt_index;
563           arg = NULL;
564           value = 1;
565         }
566       else
567         {
568           const struct cl_option *new_option = &cl_options[new_opt_index];
569
570           /* The new option must not be an alias itself.  */
571           gcc_assert (new_option->alias_target == N_OPTS
572                       || new_option->cl_separate_alias);
573
574           if (option->neg_alias_arg)
575             {
576               gcc_assert (option->alias_arg != NULL);
577               gcc_assert (arg == NULL);
578               gcc_assert (!option->cl_negative_alias);
579               if (value)
580                 arg = option->alias_arg;
581               else
582                 arg = option->neg_alias_arg;
583               value = 1;
584             }
585           else if (option->alias_arg)
586             {
587               gcc_assert (value == 1);
588               gcc_assert (arg == NULL);
589               gcc_assert (!option->cl_negative_alias);
590               arg = option->alias_arg;
591             }
592
593           if (option->cl_negative_alias)
594             value = !value;
595
596           opt_index = new_opt_index;
597           option = new_option;
598
599           if (value == 0)
600             gcc_assert (!option->cl_reject_negative);
601
602           /* Recompute what arguments are allowed.  */
603           separate_arg_flag = ((option->flags & CL_SEPARATE)
604                                && !(option->cl_no_driver_arg
605                                     && (lang_mask & CL_DRIVER)));
606           joined_arg_flag = (option->flags & CL_JOINED) != 0;
607
608           if (separate_args > 1 || option->cl_separate_nargs)
609             gcc_assert (separate_args
610                         == (unsigned int) option->cl_separate_nargs + 1);
611
612           if (!(errors & CL_ERR_MISSING_ARG))
613             {
614               if (separate_arg_flag || joined_arg_flag)
615                 {
616                   if (option->cl_missing_ok && arg == NULL)
617                     arg = "";
618                   gcc_assert (arg != NULL);
619                 }
620               else
621                 gcc_assert (arg == NULL);
622             }
623
624           /* Recheck for warnings and disabled options.  */
625           if (option->warn_message)
626             {
627               gcc_assert (warn_message == NULL);
628               warn_message = option->warn_message;
629             }
630           if (option->cl_disabled)
631             errors |= CL_ERR_DISABLED;
632         }
633     }
634
635   /* Check if this is a switch for a different front end.  */
636   if (!option_ok_for_language (option, lang_mask))
637     errors |= CL_ERR_WRONG_LANG;
638
639   /* Convert the argument to lowercase if appropriate.  */
640   if (arg && option->cl_tolower)
641     {
642       size_t j;
643       size_t len = strlen (arg);
644       char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
645
646       for (j = 0; j < len; j++)
647         arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
648       arg_lower[len] = 0;
649       arg = arg_lower;
650     }
651
652   /* If the switch takes an integer, convert it.  */
653   if (arg && option->cl_uinteger)
654     {
655       value = integral_argument (arg);
656       if (value == -1)
657         errors |= CL_ERR_UINT_ARG;
658     }
659
660   /* If the switch takes an enumerated argument, convert it.  */
661   if (arg && (option->var_type == CLVC_ENUM))
662     {
663       const struct cl_enum *e = &cl_enums[option->var_enum];
664
665       gcc_assert (value == 1);
666       if (enum_arg_to_value (e->values, arg, &value, lang_mask))
667         {
668           const char *carg = NULL;
669
670           if (enum_value_to_arg (e->values, &carg, value, lang_mask))
671             arg = carg;
672           gcc_assert (carg != NULL);
673         }
674       else
675         errors |= CL_ERR_ENUM_ARG;
676     }
677
678  done:
679   decoded->opt_index = opt_index;
680   decoded->arg = arg;
681   decoded->value = value;
682   decoded->errors = errors;
683   decoded->warn_message = warn_message;
684
685   if (opt_index == OPT_SPECIAL_unknown)
686     gcc_assert (result == 1);
687
688   gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
689   decoded->canonical_option_num_elements = result;
690   total_len = 0;
691   for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
692     {
693       if (i < result)
694         {
695           size_t len;
696           if (opt_index == OPT_SPECIAL_unknown)
697             decoded->canonical_option[i] = argv[i];
698           else
699             decoded->canonical_option[i] = NULL;
700           len = strlen (argv[i]);
701           /* If the argument is an empty string, we will print it as "" in
702              orig_option_with_args_text.  */
703           total_len += (len != 0 ? len : 2) + 1;
704         }
705       else
706         decoded->canonical_option[i] = NULL;
707     }
708   if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
709     {
710       generate_canonical_option (opt_index, arg, value, decoded);
711       if (separate_args > 1)
712         {
713           for (i = 0; i < separate_args; i++)
714             {
715               if (argv[extra_args + 1 + i] == NULL)
716                   break;
717               else
718                 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
719             }
720           gcc_assert (result == 1 + i);
721           decoded->canonical_option_num_elements = result;
722         }
723     }
724   decoded->orig_option_with_args_text
725     = p = XOBNEWVEC (&opts_obstack, char, total_len);
726   for (i = 0; i < result; i++)
727     {
728       size_t len = strlen (argv[i]);
729
730       /* Print the empty string verbally.  */
731       if (len == 0)
732         {
733           *p++ = '"';
734           *p++ = '"';
735         }
736       else
737         memcpy (p, argv[i], len);
738       p += len;
739       if (i == result - 1)
740         *p++ = 0;
741       else
742         *p++ = ' ';
743     }
744
745   return result;
746 }
747
748 /* Obstack for option strings.  */
749
750 struct obstack opts_obstack;
751
752 /* Like libiberty concat, but allocate using opts_obstack.  */
753
754 char *
755 opts_concat (const char *first, ...)
756 {
757   char *newstr, *end;
758   size_t length = 0;
759   const char *arg;
760   va_list ap;
761
762   /* First compute the size of the result and get sufficient memory.  */
763   va_start (ap, first);
764   for (arg = first; arg; arg = va_arg (ap, const char *))
765     length += strlen (arg);
766   newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
767   va_end (ap);
768
769   /* Now copy the individual pieces to the result string. */
770   va_start (ap, first);
771   for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
772     {
773       length = strlen (arg);
774       memcpy (end, arg, length);
775       end += length;
776     }
777   *end = '\0';
778   va_end (ap);
779   return newstr;
780 }
781
782 /* Decode command-line options (ARGC and ARGV being the arguments of
783    main) into an array, setting *DECODED_OPTIONS to a pointer to that
784    array and *DECODED_OPTIONS_COUNT to the number of entries in the
785    array.  The first entry in the array is always one for the program
786    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
787    flags applicable for decoding (including CL_COMMON and CL_TARGET if
788    those options should be considered applicable).  Do not produce any
789    diagnostics or set state outside of these variables.  */
790
791 void
792 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
793                                  unsigned int lang_mask,
794                                  struct cl_decoded_option **decoded_options,
795                                  unsigned int *decoded_options_count)
796 {
797   unsigned int n, i;
798   struct cl_decoded_option *opt_array;
799   unsigned int num_decoded_options;
800
801   opt_array = XNEWVEC (struct cl_decoded_option, argc);
802
803   opt_array[0].opt_index = OPT_SPECIAL_program_name;
804   opt_array[0].warn_message = NULL;
805   opt_array[0].arg = argv[0];
806   opt_array[0].orig_option_with_args_text = argv[0];
807   opt_array[0].canonical_option_num_elements = 1;
808   opt_array[0].canonical_option[0] = argv[0];
809   opt_array[0].canonical_option[1] = NULL;
810   opt_array[0].canonical_option[2] = NULL;
811   opt_array[0].canonical_option[3] = NULL;
812   opt_array[0].value = 1;
813   opt_array[0].errors = 0;
814   num_decoded_options = 1;
815
816   for (i = 1; i < argc; i += n)
817     {
818       const char *opt = argv[i];
819
820       /* Interpret "-" or a non-switch as a file name.  */
821       if (opt[0] != '-' || opt[1] == '\0')
822         {
823           generate_option_input_file (opt, &opt_array[num_decoded_options]);
824           num_decoded_options++;
825           n = 1;
826           continue;
827         }
828
829       n = decode_cmdline_option (argv + i, lang_mask,
830                                  &opt_array[num_decoded_options]);
831       num_decoded_options++;
832     }
833
834   *decoded_options = opt_array;
835   *decoded_options_count = num_decoded_options;
836   prune_options (decoded_options, decoded_options_count);
837 }
838
839 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
840    next one is the same as ORIG_NEXT_OPT_IDX.  */
841
842 static bool
843 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
844 {
845   /* An option can be canceled by the same option or an option with
846      Negative.  */
847   if (cl_options [next_opt_idx].neg_index == opt_idx)
848     return true;
849
850   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
851     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
852                           orig_next_opt_idx);
853
854   return false;
855 }
856
857 /* Filter out options canceled by the ones after them.  */
858
859 static void
860 prune_options (struct cl_decoded_option **decoded_options,
861                unsigned int *decoded_options_count)
862 {
863   unsigned int old_decoded_options_count = *decoded_options_count;
864   struct cl_decoded_option *old_decoded_options = *decoded_options;
865   unsigned int new_decoded_options_count;
866   struct cl_decoded_option *new_decoded_options
867     = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
868   unsigned int i;
869   const struct cl_option *option;
870   unsigned int fdiagnostics_color_idx = 0;
871
872   /* Remove arguments which are negated by others after them.  */
873   new_decoded_options_count = 0;
874   for (i = 0; i < old_decoded_options_count; i++)
875     {
876       unsigned int j, opt_idx, next_opt_idx;
877
878       if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
879         goto keep;
880
881       opt_idx = old_decoded_options[i].opt_index;
882       switch (opt_idx)
883         {
884         case OPT_SPECIAL_unknown:
885         case OPT_SPECIAL_ignore:
886         case OPT_SPECIAL_program_name:
887         case OPT_SPECIAL_input_file:
888           goto keep;
889
890         /* Do not save OPT_fdiagnostics_color_, just remember the last one.  */
891         case OPT_fdiagnostics_color_:
892           fdiagnostics_color_idx = i;
893           continue;
894
895         default:
896           gcc_assert (opt_idx < cl_options_count);
897           option = &cl_options[opt_idx];
898           if (option->neg_index < 0)
899             goto keep;
900
901           /* Skip joined switches.  */
902           if ((option->flags & CL_JOINED))
903             goto keep;
904
905           for (j = i + 1; j < old_decoded_options_count; j++)
906             {
907               if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
908                 continue;
909               next_opt_idx = old_decoded_options[j].opt_index;
910               if (next_opt_idx >= cl_options_count)
911                 continue;
912               if (cl_options[next_opt_idx].neg_index < 0)
913                 continue;
914               if ((cl_options[next_opt_idx].flags & CL_JOINED))
915                   continue;
916               if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
917                 break;
918             }
919           if (j == old_decoded_options_count)
920             {
921 keep:
922               new_decoded_options[new_decoded_options_count]
923                 = old_decoded_options[i];
924               new_decoded_options_count++;
925             }
926           break;
927         }
928     }
929
930   if (fdiagnostics_color_idx >= 1)
931     {
932       /* We put the last -fdiagnostics-color= at the first position
933          after argv[0] so it can take effect immediately.  */
934       memmove (new_decoded_options + 2, new_decoded_options + 1,
935                sizeof (struct cl_decoded_option) 
936                * (new_decoded_options_count - 1));
937       new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
938       new_decoded_options_count++;
939     }
940
941   free (old_decoded_options);
942   new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
943                                     new_decoded_options,
944                                     new_decoded_options_count);
945   *decoded_options = new_decoded_options;
946   *decoded_options_count = new_decoded_options_count;
947 }
948
949 /* Handle option DECODED for the language indicated by LANG_MASK,
950    using the handlers in HANDLERS and setting fields in OPTS and
951    OPTS_SET.  KIND is the diagnostic_t if this is a diagnostics
952    option, DK_UNSPECIFIED otherwise, and LOC is the location of the
953    option for options from the source file, UNKNOWN_LOCATION
954    otherwise.  GENERATED_P is true for an option generated as part of
955    processing another option or otherwise generated internally, false
956    for one explicitly passed by the user.  Returns false if the switch
957    was invalid.  DC is the diagnostic context for options affecting
958    diagnostics state, or NULL.  */
959
960 static bool
961 handle_option (struct gcc_options *opts,
962                struct gcc_options *opts_set,
963                const struct cl_decoded_option *decoded,
964                unsigned int lang_mask, int kind, location_t loc,
965                const struct cl_option_handlers *handlers,
966                bool generated_p, diagnostic_context *dc)
967 {
968   size_t opt_index = decoded->opt_index;
969   const char *arg = decoded->arg;
970   int value = decoded->value;
971   const struct cl_option *option = &cl_options[opt_index];
972   void *flag_var = option_flag_var (opt_index, opts);
973   size_t i;
974
975   if (flag_var)
976     set_option (opts, (generated_p ? NULL : opts_set),
977                 opt_index, value, arg, kind, loc, dc);
978
979   for (i = 0; i < handlers->num_handlers; i++)
980     if (option->flags & handlers->handlers[i].mask)
981       {
982         if (!handlers->handlers[i].handler (opts, opts_set, decoded,
983                                             lang_mask, kind, loc,
984                                             handlers, dc))
985           return false;
986       }
987   
988   return true;
989 }
990
991 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
992    option instead of DECODED.  This is used for callbacks when one
993    option implies another instead of an option being decoded from the
994    command line.  */
995
996 bool
997 handle_generated_option (struct gcc_options *opts,
998                          struct gcc_options *opts_set,
999                          size_t opt_index, const char *arg, int value,
1000                          unsigned int lang_mask, int kind, location_t loc,
1001                          const struct cl_option_handlers *handlers,
1002                          diagnostic_context *dc)
1003 {
1004   struct cl_decoded_option decoded;
1005
1006   generate_option (opt_index, arg, value, lang_mask, &decoded);
1007   return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1008                         handlers, true, dc);
1009 }
1010
1011 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1012    VALUE for a front end using LANG_MASK.  This is used when the
1013    compiler generates options internally.  */
1014
1015 void
1016 generate_option (size_t opt_index, const char *arg, int value,
1017                  unsigned int lang_mask, struct cl_decoded_option *decoded)
1018 {
1019   const struct cl_option *option = &cl_options[opt_index];
1020
1021   decoded->opt_index = opt_index;
1022   decoded->warn_message = NULL;
1023   decoded->arg = arg;
1024   decoded->value = value;
1025   decoded->errors = (option_ok_for_language (option, lang_mask)
1026                      ? 0
1027                      : CL_ERR_WRONG_LANG);
1028
1029   generate_canonical_option (opt_index, arg, value, decoded);
1030   switch (decoded->canonical_option_num_elements)
1031     {
1032     case 1:
1033       decoded->orig_option_with_args_text = decoded->canonical_option[0];
1034       break;
1035
1036     case 2:
1037       decoded->orig_option_with_args_text
1038         = opts_concat (decoded->canonical_option[0], " ",
1039                        decoded->canonical_option[1], NULL);
1040       break;
1041
1042     default:
1043       gcc_unreachable ();
1044     }
1045 }
1046
1047 /* Fill in *DECODED with an option for input file FILE.  */
1048
1049 void
1050 generate_option_input_file (const char *file,
1051                             struct cl_decoded_option *decoded)
1052 {
1053   decoded->opt_index = OPT_SPECIAL_input_file;
1054   decoded->warn_message = NULL;
1055   decoded->arg = file;
1056   decoded->orig_option_with_args_text = file;
1057   decoded->canonical_option_num_elements = 1;
1058   decoded->canonical_option[0] = file;
1059   decoded->canonical_option[1] = NULL;
1060   decoded->canonical_option[2] = NULL;
1061   decoded->canonical_option[3] = NULL;
1062   decoded->value = 1;
1063   decoded->errors = 0;
1064 }
1065
1066 /* Perform diagnostics for read_cmdline_option and control_warning_option
1067    functions.  Returns true if an error has been diagnosed.
1068    LOC and LANG_MASK arguments like in read_cmdline_option.
1069    OPTION is the option to report diagnostics for, OPT the name
1070    of the option as text, ARG the argument of the option (for joined
1071    options), ERRORS is bitmask of CL_ERR_* values.  */
1072
1073 static bool
1074 cmdline_handle_error (location_t loc, const struct cl_option *option,
1075                       const char *opt, const char *arg, int errors,
1076                       unsigned int lang_mask)
1077 {
1078   if (errors & CL_ERR_DISABLED)
1079     {
1080       error_at (loc, "command line option %qs"
1081                      " is not supported by this configuration", opt);
1082       return true;
1083     }
1084
1085   if (errors & CL_ERR_MISSING_ARG)
1086     {
1087       if (option->missing_argument_error)
1088         error_at (loc, option->missing_argument_error, opt);
1089       else
1090         error_at (loc, "missing argument to %qs", opt);
1091       return true;
1092     }
1093
1094   if (errors & CL_ERR_UINT_ARG)
1095     {
1096       error_at (loc, "argument to %qs should be a non-negative integer",
1097                 option->opt_text);
1098       return true;
1099     }
1100
1101   if (errors & CL_ERR_ENUM_ARG)
1102     {
1103       const struct cl_enum *e = &cl_enums[option->var_enum];
1104       unsigned int i;
1105       size_t len;
1106       char *s, *p;
1107
1108       if (e->unknown_error)
1109         error_at (loc, e->unknown_error, arg);
1110       else
1111         error_at (loc, "unrecognized argument in option %qs", opt);
1112
1113       len = 0;
1114       for (i = 0; e->values[i].arg != NULL; i++)
1115         len += strlen (e->values[i].arg) + 1;
1116
1117       auto_vec <const char *> candidates;
1118       s = XALLOCAVEC (char, len);
1119       p = s;
1120       for (i = 0; e->values[i].arg != NULL; i++)
1121         {
1122           if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1123             continue;
1124           size_t arglen = strlen (e->values[i].arg);
1125           memcpy (p, e->values[i].arg, arglen);
1126           p[arglen] = ' ';
1127           p += arglen + 1;
1128           candidates.safe_push (e->values[i].arg);
1129         }
1130       p[-1] = 0;
1131       const char *hint = find_closest_string (arg, &candidates);
1132       if (hint)
1133         inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1134                 option->opt_text, s, hint);
1135       else
1136         inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1137
1138       return true;
1139     }
1140
1141   return false;
1142 }
1143
1144 /* Handle the switch DECODED (location LOC) for the language indicated
1145    by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1146    OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1147    diagnostic options.  */
1148
1149 void
1150 read_cmdline_option (struct gcc_options *opts,
1151                      struct gcc_options *opts_set,
1152                      struct cl_decoded_option *decoded,
1153                      location_t loc,
1154                      unsigned int lang_mask,
1155                      const struct cl_option_handlers *handlers,
1156                      diagnostic_context *dc)
1157 {
1158   const struct cl_option *option;
1159   const char *opt = decoded->orig_option_with_args_text;
1160
1161   if (decoded->warn_message)
1162     warning_at (loc, 0, decoded->warn_message, opt);
1163
1164   if (decoded->opt_index == OPT_SPECIAL_unknown)
1165     {
1166       if (handlers->unknown_option_callback (decoded))
1167         error_at (loc, "unrecognized command line option %qs", decoded->arg);
1168       return;
1169     }
1170
1171   if (decoded->opt_index == OPT_SPECIAL_ignore)
1172     return;
1173
1174   option = &cl_options[decoded->opt_index];
1175
1176   if (decoded->errors
1177       && cmdline_handle_error (loc, option, opt, decoded->arg,
1178                                decoded->errors, lang_mask))
1179     return;
1180
1181   if (decoded->errors & CL_ERR_WRONG_LANG)
1182     {
1183       handlers->wrong_lang_callback (decoded, lang_mask);
1184       return;
1185     }
1186
1187   gcc_assert (!decoded->errors);
1188
1189   if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1190                       loc, handlers, false, dc))
1191     error_at (loc, "unrecognized command line option %qs", opt);
1192 }
1193
1194 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1195    OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1196    location LOC, using diagnostic context DC if not NULL for
1197    diagnostic classification.  */
1198
1199 void
1200 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1201             int opt_index, int value, const char *arg, int kind,
1202             location_t loc, diagnostic_context *dc)
1203 {
1204   const struct cl_option *option = &cl_options[opt_index];
1205   void *flag_var = option_flag_var (opt_index, opts);
1206   void *set_flag_var = NULL;
1207
1208   if (!flag_var)
1209     return;
1210
1211   if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1212     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1213
1214   if (opts_set != NULL)
1215     set_flag_var = option_flag_var (opt_index, opts_set);
1216
1217   switch (option->var_type)
1218     {
1219     case CLVC_BOOLEAN:
1220         *(int *) flag_var = value;
1221         if (set_flag_var)
1222           *(int *) set_flag_var = 1;
1223         break;
1224
1225     case CLVC_EQUAL:
1226         if (option->cl_host_wide_int) 
1227           *(HOST_WIDE_INT *) flag_var = (value
1228                                          ? option->var_value
1229                                          : !option->var_value);
1230         else
1231           *(int *) flag_var = (value
1232                                ? option->var_value
1233                                : !option->var_value);
1234         if (set_flag_var)
1235           *(int *) set_flag_var = 1;
1236         break;
1237
1238     case CLVC_BIT_CLEAR:
1239     case CLVC_BIT_SET:
1240         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1241           {
1242             if (option->cl_host_wide_int) 
1243               *(HOST_WIDE_INT *) flag_var |= option->var_value;
1244             else 
1245               *(int *) flag_var |= option->var_value;
1246           }
1247         else
1248           {
1249             if (option->cl_host_wide_int) 
1250               *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1251             else 
1252               *(int *) flag_var &= ~option->var_value;
1253           }
1254         if (set_flag_var)
1255           {
1256             if (option->cl_host_wide_int) 
1257               *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1258             else
1259               *(int *) set_flag_var |= option->var_value;
1260           }
1261         break;
1262
1263     case CLVC_STRING:
1264         *(const char **) flag_var = arg;
1265         if (set_flag_var)
1266           *(const char **) set_flag_var = "";
1267         break;
1268
1269     case CLVC_ENUM:
1270       {
1271         const struct cl_enum *e = &cl_enums[option->var_enum];
1272
1273         e->set (flag_var, value);
1274         if (set_flag_var)
1275           e->set (set_flag_var, 1);
1276       }
1277       break;
1278
1279     case CLVC_DEFER:
1280         {
1281           vec<cl_deferred_option> *v
1282             = (vec<cl_deferred_option> *) *(void **) flag_var;
1283           cl_deferred_option p = {opt_index, arg, value};
1284           if (!v)
1285             v = XCNEW (vec<cl_deferred_option>);
1286           v->safe_push (p);
1287           *(void **) flag_var = v;
1288           if (set_flag_var)
1289             *(void **) set_flag_var = v;
1290         }
1291         break;
1292     }
1293 }
1294
1295 /* Return the address of the flag variable for option OPT_INDEX in
1296    options structure OPTS, or NULL if there is no flag variable.  */
1297
1298 void *
1299 option_flag_var (int opt_index, struct gcc_options *opts)
1300 {
1301   const struct cl_option *option = &cl_options[opt_index];
1302
1303   if (option->flag_var_offset == (unsigned short) -1)
1304     return NULL;
1305   return (void *)(((char *) opts) + option->flag_var_offset);
1306 }
1307
1308 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1309    or -1 if it isn't a simple on-off switch.  */
1310
1311 int
1312 option_enabled (int opt_idx, void *opts)
1313 {
1314   const struct cl_option *option = &(cl_options[opt_idx]);
1315   struct gcc_options *optsg = (struct gcc_options *) opts;
1316   void *flag_var = option_flag_var (opt_idx, optsg);
1317
1318   if (flag_var)
1319     switch (option->var_type)
1320       {
1321       case CLVC_BOOLEAN:
1322         return *(int *) flag_var != 0;
1323
1324       case CLVC_EQUAL:
1325         if (option->cl_host_wide_int) 
1326           return *(HOST_WIDE_INT *) flag_var == option->var_value;
1327         else
1328           return *(int *) flag_var == option->var_value;
1329
1330       case CLVC_BIT_CLEAR:
1331         if (option->cl_host_wide_int) 
1332           return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1333         else
1334           return (*(int *) flag_var & option->var_value) == 0;
1335
1336       case CLVC_BIT_SET:
1337         if (option->cl_host_wide_int) 
1338           return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1339         else 
1340           return (*(int *) flag_var & option->var_value) != 0;
1341
1342       case CLVC_STRING:
1343       case CLVC_ENUM:
1344       case CLVC_DEFER:
1345         break;
1346       }
1347   return -1;
1348 }
1349
1350 /* Fill STATE with the current state of option OPTION in OPTS.  Return
1351    true if there is some state to store.  */
1352
1353 bool
1354 get_option_state (struct gcc_options *opts, int option,
1355                   struct cl_option_state *state)
1356 {
1357   void *flag_var = option_flag_var (option, opts);
1358
1359   if (flag_var == 0)
1360     return false;
1361
1362   switch (cl_options[option].var_type)
1363     {
1364     case CLVC_BOOLEAN:
1365     case CLVC_EQUAL:
1366       state->data = flag_var;
1367       state->size = (cl_options[option].cl_host_wide_int
1368                      ? sizeof (HOST_WIDE_INT)
1369                      : sizeof (int));
1370       break;
1371
1372     case CLVC_BIT_CLEAR:
1373     case CLVC_BIT_SET:
1374       state->ch = option_enabled (option, opts);
1375       state->data = &state->ch;
1376       state->size = 1;
1377       break;
1378
1379     case CLVC_STRING:
1380       state->data = *(const char **) flag_var;
1381       if (state->data == 0)
1382         state->data = "";
1383       state->size = strlen ((const char *) state->data) + 1;
1384       break;
1385
1386     case CLVC_ENUM:
1387       state->data = flag_var;
1388       state->size = cl_enums[cl_options[option].var_enum].var_size;
1389       break;
1390
1391     case CLVC_DEFER:
1392       return false;
1393     }
1394   return true;
1395 }
1396
1397 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1398    handlers HANDLERS) to have diagnostic kind KIND for option
1399    structures OPTS and OPTS_SET and diagnostic context DC (possibly
1400    NULL), at location LOC (UNKNOWN_LOCATION for -Werror=).  ARG is the
1401    argument of the option for joined options, or NULL otherwise.  If IMPLY,
1402    the warning option in question is implied at this point.  This is
1403    used by -Werror= and #pragma GCC diagnostic.  */
1404
1405 void
1406 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1407                         bool imply, location_t loc, unsigned int lang_mask,
1408                         const struct cl_option_handlers *handlers,
1409                         struct gcc_options *opts,
1410                         struct gcc_options *opts_set,
1411                         diagnostic_context *dc)
1412 {
1413   if (cl_options[opt_index].alias_target != N_OPTS)
1414     {
1415       gcc_assert (!cl_options[opt_index].cl_separate_alias
1416                   && !cl_options[opt_index].cl_negative_alias);
1417       if (cl_options[opt_index].alias_arg)
1418         arg = cl_options[opt_index].alias_arg;
1419       opt_index = cl_options[opt_index].alias_target;
1420     }
1421   if (opt_index == OPT_SPECIAL_ignore)
1422     return;
1423   if (dc)
1424     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1425   if (imply)
1426     {
1427       const struct cl_option *option = &cl_options[opt_index];
1428
1429       /* -Werror=foo implies -Wfoo.  */
1430       if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1431         {
1432           int value = 1;
1433
1434           if (arg && *arg == '\0' && !option->cl_missing_ok)
1435             arg = NULL;
1436
1437           if ((option->flags & CL_JOINED) && arg == NULL)
1438             {
1439               cmdline_handle_error (loc, option, option->opt_text, arg,
1440                                     CL_ERR_MISSING_ARG, lang_mask);
1441               return;
1442             }
1443
1444           /* If the switch takes an integer, convert it.  */
1445           if (arg && option->cl_uinteger)
1446             {
1447               value = integral_argument (arg);
1448               if (value == -1)
1449                 {
1450                   cmdline_handle_error (loc, option, option->opt_text, arg,
1451                                         CL_ERR_UINT_ARG, lang_mask);
1452                   return;
1453                 }
1454             }
1455
1456           /* If the switch takes an enumerated argument, convert it.  */
1457           if (arg && option->var_type == CLVC_ENUM)
1458             {
1459               const struct cl_enum *e = &cl_enums[option->var_enum];
1460
1461               if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1462                 {
1463                   const char *carg = NULL;
1464
1465                   if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1466                     arg = carg;
1467                   gcc_assert (carg != NULL);
1468                 }
1469               else
1470                 {
1471                   cmdline_handle_error (loc, option, option->opt_text, arg,
1472                                         CL_ERR_ENUM_ARG, lang_mask);
1473                   return;
1474                 }
1475             }
1476
1477           handle_generated_option (opts, opts_set,
1478                                    opt_index, arg, value, lang_mask,
1479                                    kind, loc, handlers, dc);
1480         }
1481     }
1482 }