OSDN Git Service

* doc/options.texi (NegativeAlias): Document.
[pf3gnuchains/gcc-fork.git] / gcc / opts-common.c
1 /* Command line option handling.
2    Copyright (C) 2006, 2007, 2008, 2010, 2011 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 "flags.h"
26 #include "diagnostic.h"
27
28 static void prune_options (struct cl_decoded_option **, unsigned int *);
29
30 /* Perform a binary search to find which option the command-line INPUT
31    matches.  Returns its index in the option array, and
32    OPT_SPECIAL_unknown on failure.
33
34    This routine is quite subtle.  A normal binary search is not good
35    enough because some options can be suffixed with an argument, and
36    multiple sub-matches can occur, e.g. input of "-pedantic" matching
37    the initial substring of "-pedantic-errors".
38
39    A more complicated example is -gstabs.  It should match "-g" with
40    an argument of "stabs".  Suppose, however, that the number and list
41    of switches are such that the binary search tests "-gen-decls"
42    before having tested "-g".  This doesn't match, and as "-gen-decls"
43    is less than "-gstabs", it will become the lower bound of the
44    binary search range, and "-g" will never be seen.  To resolve this
45    issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
46    to "-g" so that failed searches that end between "-gen-decls" and
47    the lexicographically subsequent switch know to go back and see if
48    "-g" causes a match (which it does in this example).
49
50    This search is done in such a way that the longest match for the
51    front end in question wins.  If there is no match for the current
52    front end, the longest match for a different front end is returned
53    (or N_OPTS if none) and the caller emits an error message.  */
54 size_t
55 find_opt (const char *input, unsigned int lang_mask)
56 {
57   size_t mn, mn_orig, mx, md, opt_len;
58   size_t match_wrong_lang;
59   int comp;
60
61   mn = 0;
62   mx = cl_options_count;
63
64   /* Find mn such this lexicographical inequality holds:
65      cl_options[mn] <= input < cl_options[mn + 1].  */
66   while (mx - mn > 1)
67     {
68       md = (mn + mx) / 2;
69       opt_len = cl_options[md].opt_len;
70       comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
71
72       if (comp < 0)
73         mx = md;
74       else
75         mn = md;
76     }
77
78   mn_orig = mn;
79
80   /* This is the switch that is the best match but for a different
81      front end, or OPT_SPECIAL_unknown if there is no match at all.  */
82   match_wrong_lang = OPT_SPECIAL_unknown;
83
84   /* Backtrace the chain of possible matches, returning the longest
85      one, if any, that fits best.  With current GCC switches, this
86      loop executes at most twice.  */
87   do
88     {
89       const struct cl_option *opt = &cl_options[mn];
90
91       /* Is the input either an exact match or a prefix that takes a
92          joined argument?  */
93       if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
94           && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
95         {
96           /* If language is OK, return it.  */
97           if (opt->flags & lang_mask)
98             return mn;
99
100           /* If we haven't remembered a prior match, remember this
101              one.  Any prior match is necessarily better.  */
102           if (match_wrong_lang == OPT_SPECIAL_unknown)
103             match_wrong_lang = mn;
104         }
105
106       /* Try the next possibility.  This is cl_options_count if there
107          are no more.  */
108       mn = opt->back_chain;
109     }
110   while (mn != cl_options_count);
111
112   if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
113     {
114       /* Long options, starting "--", may be abbreviated if the
115          abbreviation is unambiguous.  This only applies to options
116          not taking a joined argument, and abbreviations of "--option"
117          are permitted even if there is a variant "--option=".  */
118       size_t mnc = mn_orig + 1;
119       size_t cmp_len = strlen (input);
120       while (mnc < cl_options_count
121              && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
122         {
123           /* Option matching this abbreviation.  OK if it is the first
124              match and that does not take a joined argument, or the
125              second match, taking a joined argument and with only '='
126              added to the first match; otherwise considered
127              ambiguous.  */
128           if (mnc == mn_orig + 1
129               && !(cl_options[mnc].flags & CL_JOINED))
130             match_wrong_lang = mnc;
131           else if (mnc == mn_orig + 2
132                    && match_wrong_lang == mn_orig + 1
133                    && (cl_options[mnc].flags & CL_JOINED)
134                    && (cl_options[mnc].opt_len
135                        == cl_options[mn_orig + 1].opt_len + 1)
136                    && strncmp (cl_options[mnc].opt_text + 1,
137                                cl_options[mn_orig + 1].opt_text + 1,
138                                cl_options[mn_orig + 1].opt_len) == 0)
139             ; /* OK, as long as there are no more matches.  */
140           else
141             return OPT_SPECIAL_unknown;
142           mnc++;
143         }
144     }
145
146   /* Return the best wrong match, or OPT_SPECIAL_unknown if none.  */
147   return match_wrong_lang;
148 }
149
150 /* If ARG is a non-negative integer made up solely of digits, return its
151    value, otherwise return -1.  */
152
153 int
154 integral_argument (const char *arg)
155 {
156   const char *p = arg;
157
158   while (*p && ISDIGIT (*p))
159     p++;
160
161   if (*p == '\0')
162     return atoi (arg);
163
164   return -1;
165 }
166
167 /* Return whether OPTION is OK for the language given by
168    LANG_MASK.  */
169 static bool
170 option_ok_for_language (const struct cl_option *option,
171                         unsigned int lang_mask)
172 {
173   if (!(option->flags & lang_mask))
174     return false;
175   else if ((option->flags & CL_TARGET)
176            && (option->flags & (CL_LANG_ALL | CL_DRIVER))
177            && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
178     /* Complain for target flag language mismatches if any languages
179        are specified.  */
180     return false;
181   return true;
182 }
183
184 /* Return whether ENUM_ARG is OK for the language given by
185    LANG_MASK.  */
186
187 static bool
188 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
189                           unsigned int lang_mask)
190 {
191   return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
192 }
193
194 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
195    storing the value in *VALUE if found, and returning false without
196    modifying *VALUE if not found.  */
197
198 static bool
199 enum_arg_to_value (const struct cl_enum_arg *enum_args,
200                    const char *arg, int *value, unsigned int lang_mask)
201 {
202   unsigned int i;
203
204   for (i = 0; enum_args[i].arg != NULL; i++)
205     if (strcmp (arg, enum_args[i].arg) == 0
206         && enum_arg_ok_for_language (&enum_args[i], lang_mask))
207       {
208         *value = enum_args[i].value;
209         return true;
210       }
211
212   return false;
213 }
214
215 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
216    corresponding string in *ARGP, returning true if the found string
217    was marked as canonical, false otherwise.  If VALUE is not found
218    (which may be the case for uninitialized values if the relevant
219    option has not been passed), set *ARGP to NULL and return
220    false.  */
221
222 bool
223 enum_value_to_arg (const struct cl_enum_arg *enum_args,
224                    const char **argp, int value, unsigned int lang_mask)
225 {
226   unsigned int i;
227
228   for (i = 0; enum_args[i].arg != NULL; i++)
229     if (enum_args[i].value == value
230         && (enum_args[i].flags & CL_ENUM_CANONICAL)
231         && enum_arg_ok_for_language (&enum_args[i], lang_mask))
232       {
233         *argp = enum_args[i].arg;
234         return true;
235       }
236
237   for (i = 0; enum_args[i].arg != NULL; i++)
238     if (enum_args[i].value == value
239         && enum_arg_ok_for_language (&enum_args[i], lang_mask))
240       {
241         *argp = enum_args[i].arg;
242         return false;
243       }
244
245   *argp = NULL;
246   return false;
247 }
248
249 /* Fill in the canonical option part of *DECODED with an option
250    described by OPT_INDEX, ARG and VALUE.  */
251
252 static void
253 generate_canonical_option (size_t opt_index, const char *arg, int value,
254                            struct cl_decoded_option *decoded)
255 {
256   const struct cl_option *option = &cl_options[opt_index];
257   const char *opt_text = option->opt_text;
258
259   if (value == 0
260       && !(option->flags & CL_REJECT_NEGATIVE)
261       && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
262     {
263       char *t = XNEWVEC (char, option->opt_len + 5);
264       t[0] = '-';
265       t[1] = opt_text[1];
266       t[2] = 'n';
267       t[3] = 'o';
268       t[4] = '-';
269       memcpy (t + 5, opt_text + 2, option->opt_len);
270       opt_text = t;
271     }
272
273   decoded->canonical_option[2] = NULL;
274   decoded->canonical_option[3] = NULL;
275
276   if (arg)
277     {
278       if ((option->flags & CL_SEPARATE)
279           && !(option->flags & CL_SEPARATE_ALIAS))
280         {
281           decoded->canonical_option[0] = opt_text;
282           decoded->canonical_option[1] = arg;
283           decoded->canonical_option_num_elements = 2;
284         }
285       else
286         {
287           gcc_assert (option->flags & CL_JOINED);
288           decoded->canonical_option[0] = concat (opt_text, arg, NULL);
289           decoded->canonical_option[1] = NULL;
290           decoded->canonical_option_num_elements = 1;
291         }
292     }
293   else
294     {
295       decoded->canonical_option[0] = opt_text;
296       decoded->canonical_option[1] = NULL;
297       decoded->canonical_option_num_elements = 1;
298     }
299 }
300
301 /* Structure describing mappings from options on the command line to
302    options to look up with find_opt.  */
303 struct option_map
304 {
305   /* Prefix of the option on the command line.  */
306   const char *opt0;
307   /* If two argv elements are considered to be merged into one option,
308      prefix for the second element, otherwise NULL.  */
309   const char *opt1;
310   /* The new prefix to map to.  */
311   const char *new_prefix;
312   /* Whether at least one character is needed following opt1 or opt0
313      for this mapping to be used.  (--optimize= is valid for -O, but
314      --warn- is not valid for -W.)  */
315   bool another_char_needed;
316   /* Whether the original option is a negated form of the option
317      resulting from this map.  */
318   bool negated;
319 };
320 static const struct option_map option_map[] =
321   {
322     { "-Wno-", NULL, "-W", false, true },
323     { "-fno-", NULL, "-f", false, true },
324     { "-mno-", NULL, "-m", false, true },
325     { "--debug=", NULL, "-g", false, false },
326     { "--machine-", NULL, "-m", true, false },
327     { "--machine-no-", NULL, "-m", false, true },
328     { "--machine=", NULL, "-m", false, false },
329     { "--machine=no-", NULL, "-m", false, true },
330     { "--machine", "", "-m", false, false },
331     { "--machine", "no-", "-m", false, true },
332     { "--optimize=", NULL, "-O", false, false },
333     { "--std=", NULL, "-std=", false, false },
334     { "--std", "", "-std=", false, false },
335     { "--warn-", NULL, "-W", true, false },
336     { "--warn-no-", NULL, "-W", false, true },
337     { "--", NULL, "-f", true, false },
338     { "--no-", NULL, "-f", false, true }
339   };
340
341 /* Decode the switch beginning at ARGV for the language indicated by
342    LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
343    the structure *DECODED.  Returns the number of switches
344    consumed.  */
345
346 static unsigned int
347 decode_cmdline_option (const char **argv, unsigned int lang_mask,
348                        struct cl_decoded_option *decoded)
349 {
350   size_t opt_index;
351   const char *arg = 0;
352   int value = 1;
353   unsigned int result = 1, i, extra_args, separate_args = 0;
354   int adjust_len = 0;
355   size_t total_len;
356   char *p;
357   const struct cl_option *option;
358   int errors = 0;
359   const char *warn_message = NULL;
360   bool separate_arg_flag;
361   bool joined_arg_flag;
362   bool have_separate_arg = false;
363
364   extra_args = 0;
365
366   opt_index = find_opt (argv[0] + 1, lang_mask);
367   i = 0;
368   while (opt_index == OPT_SPECIAL_unknown
369          && i < ARRAY_SIZE (option_map))
370     {
371       const char *opt0 = option_map[i].opt0;
372       const char *opt1 = option_map[i].opt1;
373       const char *new_prefix = option_map[i].new_prefix;
374       bool another_char_needed = option_map[i].another_char_needed;
375       size_t opt0_len = strlen (opt0);
376       size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
377       size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
378       size_t new_prefix_len = strlen (new_prefix);
379
380       extra_args = (opt1 == NULL ? 0 : 1);
381       value = !option_map[i].negated;
382
383       if (strncmp (argv[0], opt0, opt0_len) == 0
384           && (opt1 == NULL
385               || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
386           && (!another_char_needed
387               || argv[extra_args][optn_len] != 0))
388         {
389           size_t arglen = strlen (argv[extra_args]);
390           char *dup;
391
392           adjust_len = (int) optn_len - (int) new_prefix_len;
393           dup = XNEWVEC (char, arglen + 1 - adjust_len);
394           memcpy (dup, new_prefix, new_prefix_len);
395           memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
396                   arglen - optn_len + 1);
397           opt_index = find_opt (dup + 1, lang_mask);
398           free (dup);
399         }
400       i++;
401     }
402
403   if (opt_index == OPT_SPECIAL_unknown)
404     {
405       arg = argv[0];
406       extra_args = 0;
407       value = 1;
408       goto done;
409     }
410
411   option = &cl_options[opt_index];
412
413   /* Reject negative form of switches that don't take negatives as
414      unrecognized.  */
415   if (!value && (option->flags & CL_REJECT_NEGATIVE))
416     {
417       opt_index = OPT_SPECIAL_unknown;
418       errors |= CL_ERR_NEGATIVE;
419       arg = argv[0];
420       goto done;
421     }
422
423   result = extra_args + 1;
424   warn_message = option->warn_message;
425
426   /* Check to see if the option is disabled for this configuration.  */
427   if (option->flags & CL_DISABLED)
428     errors |= CL_ERR_DISABLED;
429
430   /* Determine whether there may be a separate argument based on
431      whether this option is being processed for the driver, and, if
432      so, how many such arguments.  */
433   separate_arg_flag = ((option->flags & CL_SEPARATE)
434                        && !((option->flags & CL_NO_DRIVER_ARG)
435                             && (lang_mask & CL_DRIVER)));
436   separate_args = (separate_arg_flag
437                    ? ((option->flags & CL_SEPARATE_NARGS_MASK)
438                       >> CL_SEPARATE_NARGS_SHIFT) + 1
439                    : 0);
440   joined_arg_flag = (option->flags & CL_JOINED) != 0;
441
442   /* Sort out any argument the switch takes.  */
443   if (joined_arg_flag)
444     {
445       /* Have arg point to the original switch.  This is because
446          some code, such as disable_builtin_function, expects its
447          argument to be persistent until the program exits.  */
448       arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
449
450       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
451         {
452           if (separate_arg_flag)
453             {
454               arg = argv[extra_args + 1];
455               result = extra_args + 2;
456               if (arg == NULL)
457                 result = extra_args + 1;
458               else
459                 have_separate_arg = true;
460             }
461           else
462             /* Missing argument.  */
463             arg = NULL;
464         }
465     }
466   else if (separate_arg_flag)
467     {
468       arg = argv[extra_args + 1];
469       for (i = 0; i < separate_args; i++)
470         if (argv[extra_args + 1 + i] == NULL)
471           {
472             errors |= CL_ERR_MISSING_ARG;
473             break;
474           }
475       result = extra_args + 1 + i;
476       if (arg != NULL)
477         have_separate_arg = true;
478     }
479
480   if (arg == NULL && (separate_arg_flag || joined_arg_flag))
481     errors |= CL_ERR_MISSING_ARG;
482
483   /* Is this option an alias (or an ignored option, marked as an alias
484      of OPT_SPECIAL_ignore)?  */
485   if (option->alias_target != N_OPTS
486       && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
487     {
488       size_t new_opt_index = option->alias_target;
489
490       if (new_opt_index == OPT_SPECIAL_ignore)
491         {
492           gcc_assert (option->alias_arg == NULL);
493           gcc_assert (option->neg_alias_arg == NULL);
494           opt_index = new_opt_index;
495           arg = NULL;
496           value = 1;
497         }
498       else
499         {
500           const struct cl_option *new_option = &cl_options[new_opt_index];
501
502           /* The new option must not be an alias itself.  */
503           gcc_assert (new_option->alias_target == N_OPTS
504                       || (new_option->flags & CL_SEPARATE_ALIAS));
505
506           if (option->neg_alias_arg)
507             {
508               gcc_assert (option->alias_arg != NULL);
509               gcc_assert (arg == NULL);
510               gcc_assert (!(option->flags & CL_NEGATIVE_ALIAS));
511               if (value)
512                 arg = option->alias_arg;
513               else
514                 arg = option->neg_alias_arg;
515               value = 1;
516             }
517           else if (option->alias_arg)
518             {
519               gcc_assert (value == 1);
520               gcc_assert (arg == NULL);
521               gcc_assert (!(option->flags & CL_NEGATIVE_ALIAS));
522               arg = option->alias_arg;
523             }
524
525           if (option->flags & CL_NEGATIVE_ALIAS)
526             value = !value;
527
528           opt_index = new_opt_index;
529           option = new_option;
530
531           if (value == 0)
532             gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
533
534           /* Recompute what arguments are allowed.  */
535           separate_arg_flag = ((option->flags & CL_SEPARATE)
536                                && !((option->flags & CL_NO_DRIVER_ARG)
537                                     && (lang_mask & CL_DRIVER)));
538           joined_arg_flag = (option->flags & CL_JOINED) != 0;
539
540           if (separate_args > 1 || (option->flags & CL_SEPARATE_NARGS_MASK))
541             gcc_assert (separate_args
542                         == ((option->flags & CL_SEPARATE_NARGS_MASK)
543                             >> CL_SEPARATE_NARGS_SHIFT) + 1);
544
545           if (!(errors & CL_ERR_MISSING_ARG))
546             {
547               if (separate_arg_flag || joined_arg_flag)
548                 {
549                   if ((option->flags & CL_MISSING_OK) && arg == NULL)
550                     arg = "";
551                   gcc_assert (arg != NULL);
552                 }
553               else
554                 gcc_assert (arg == NULL);
555             }
556
557           /* Recheck for warnings and disabled options.  */
558           if (option->warn_message)
559             {
560               gcc_assert (warn_message == NULL);
561               warn_message = option->warn_message;
562             }
563           if (option->flags & CL_DISABLED)
564             errors |= CL_ERR_DISABLED;
565         }
566     }
567
568   /* Check if this is a switch for a different front end.  */
569   if (!option_ok_for_language (option, lang_mask))
570     errors |= CL_ERR_WRONG_LANG;
571
572   /* If the switch takes an integer, convert it.  */
573   if (arg && (option->flags & CL_UINTEGER))
574     {
575       value = integral_argument (arg);
576       if (value == -1)
577         errors |= CL_ERR_UINT_ARG;
578     }
579
580   /* If the switch takes an enumerated argument, convert it.  */
581   if (arg && (option->var_type == CLVC_ENUM))
582     {
583       const struct cl_enum *e = &cl_enums[option->var_enum];
584
585       gcc_assert (value == 1);
586       if (enum_arg_to_value (e->values, arg, &value, lang_mask))
587         {
588           const char *carg = NULL;
589
590           if (enum_value_to_arg (e->values, &carg, value, lang_mask))
591             arg = carg;
592           gcc_assert (carg != NULL);
593         }
594       else
595         errors |= CL_ERR_ENUM_ARG;
596     }
597
598  done:
599   decoded->opt_index = opt_index;
600   decoded->arg = arg;
601   decoded->value = value;
602   decoded->errors = errors;
603   decoded->warn_message = warn_message;
604
605   if (opt_index == OPT_SPECIAL_unknown)
606     gcc_assert (result == 1);
607
608   gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
609   decoded->canonical_option_num_elements = result;
610   total_len = 0;
611   for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
612     {
613       if (i < result)
614         {
615           size_t len;
616           if (opt_index == OPT_SPECIAL_unknown)
617             decoded->canonical_option[i] = argv[i];
618           else
619             decoded->canonical_option[i] = NULL;
620           len = strlen (argv[i]);
621           /* If the argument is an empty string, we will print it as "" in
622              orig_option_with_args_text.  */
623           total_len += (len != 0 ? len : 2) + 1;
624         }
625       else
626         decoded->canonical_option[i] = NULL;
627     }
628   if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
629     {
630       generate_canonical_option (opt_index, arg, value, decoded);
631       if (separate_args > 1)
632         {
633           for (i = 0; i < separate_args; i++)
634             {
635               if (argv[extra_args + 1 + i] == NULL)
636                   break;
637               else
638                 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
639             }
640           gcc_assert (result == 1 + i);
641           decoded->canonical_option_num_elements = result;
642         }
643     }
644   decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
645   for (i = 0; i < result; i++)
646     {
647       size_t len = strlen (argv[i]);
648
649       /* Print the empty string verbally.  */
650       if (len == 0)
651         {
652           *p++ = '"';
653           *p++ = '"';
654         }
655       else
656         memcpy (p, argv[i], len);
657       p += len;
658       if (i == result - 1)
659         *p++ = 0;
660       else
661         *p++ = ' ';
662     }
663
664   return result;
665 }
666
667 /* Decode command-line options (ARGC and ARGV being the arguments of
668    main) into an array, setting *DECODED_OPTIONS to a pointer to that
669    array and *DECODED_OPTIONS_COUNT to the number of entries in the
670    array.  The first entry in the array is always one for the program
671    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
672    flags applicable for decoding (including CL_COMMON and CL_TARGET if
673    those options should be considered applicable).  Do not produce any
674    diagnostics or set state outside of these variables.  */
675
676 void
677 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
678                                  unsigned int lang_mask,
679                                  struct cl_decoded_option **decoded_options,
680                                  unsigned int *decoded_options_count)
681 {
682   unsigned int n, i;
683   struct cl_decoded_option *opt_array;
684   unsigned int num_decoded_options;
685   bool argv_copied = false;
686
687   opt_array = XNEWVEC (struct cl_decoded_option, argc);
688
689   opt_array[0].opt_index = OPT_SPECIAL_program_name;
690   opt_array[0].warn_message = NULL;
691   opt_array[0].arg = argv[0];
692   opt_array[0].orig_option_with_args_text = argv[0];
693   opt_array[0].canonical_option_num_elements = 1;
694   opt_array[0].canonical_option[0] = argv[0];
695   opt_array[0].canonical_option[1] = NULL;
696   opt_array[0].canonical_option[2] = NULL;
697   opt_array[0].canonical_option[3] = NULL;
698   opt_array[0].value = 1;
699   opt_array[0].errors = 0;
700   num_decoded_options = 1;
701
702   for (i = 1; i < argc; i += n)
703     {
704       const char *opt = argv[i];
705
706       /* Interpret "-" or a non-switch as a file name.  */
707       if (opt[0] != '-' || opt[1] == '\0')
708         {
709           generate_option_input_file (opt, &opt_array[num_decoded_options]);
710           num_decoded_options++;
711           n = 1;
712           continue;
713         }
714
715       n = decode_cmdline_option (argv + i, lang_mask,
716                                  &opt_array[num_decoded_options]);
717       num_decoded_options++;
718     }
719
720   if (argv_copied)
721     free (argv);
722   *decoded_options = opt_array;
723   *decoded_options_count = num_decoded_options;
724   prune_options (decoded_options, decoded_options_count);
725 }
726
727 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
728    next one is the same as ORIG_NEXT_OPT_IDX.  */
729
730 static bool
731 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
732 {
733   /* An option can be canceled by the same option or an option with
734      Negative.  */
735   if (cl_options [next_opt_idx].neg_index == opt_idx)
736     return true;
737
738   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
739     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
740                           orig_next_opt_idx);
741
742   return false;
743 }
744
745 /* Filter out options canceled by the ones after them.  */
746
747 static void
748 prune_options (struct cl_decoded_option **decoded_options,
749                unsigned int *decoded_options_count)
750 {
751   unsigned int old_decoded_options_count = *decoded_options_count;
752   struct cl_decoded_option *old_decoded_options = *decoded_options;
753   unsigned int new_decoded_options_count;
754   struct cl_decoded_option *new_decoded_options
755     = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
756   unsigned int i;
757   const struct cl_option *option;
758
759   /* Remove arguments which are negated by others after them.  */
760   new_decoded_options_count = 0;
761   for (i = 0; i < old_decoded_options_count; i++)
762     {
763       unsigned int j, opt_idx, next_opt_idx;
764
765       if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
766         goto keep;
767
768       opt_idx = old_decoded_options[i].opt_index;
769       switch (opt_idx)
770         {
771         case OPT_SPECIAL_unknown:
772         case OPT_SPECIAL_ignore:
773         case OPT_SPECIAL_program_name:
774         case OPT_SPECIAL_input_file:
775           goto keep;
776
777         default:
778           gcc_assert (opt_idx < cl_options_count);
779           option = &cl_options[opt_idx];
780           if (option->neg_index < 0)
781             goto keep;
782
783           /* Skip joined switches.  */
784           if ((option->flags & CL_JOINED))
785             goto keep;
786
787           for (j = i + 1; j < old_decoded_options_count; j++)
788             {
789               if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
790                 continue;
791               next_opt_idx = old_decoded_options[j].opt_index;
792               if (next_opt_idx >= cl_options_count)
793                 continue;
794               if (cl_options[next_opt_idx].neg_index < 0)
795                 continue;
796               if ((cl_options[next_opt_idx].flags & CL_JOINED))
797                   continue;
798               if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
799                 break;
800             }
801           if (j == old_decoded_options_count)
802             {
803 keep:
804               new_decoded_options[new_decoded_options_count]
805                 = old_decoded_options[i];
806               new_decoded_options_count++;
807             }
808           break;
809         }
810     }
811
812   free (old_decoded_options);
813   new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
814                                     new_decoded_options,
815                                     new_decoded_options_count);
816   *decoded_options = new_decoded_options;
817   *decoded_options_count = new_decoded_options_count;
818 }
819
820 /* Handle option DECODED for the language indicated by LANG_MASK,
821    using the handlers in HANDLERS and setting fields in OPTS and
822    OPTS_SET.  KIND is the diagnostic_t if this is a diagnostics
823    option, DK_UNSPECIFIED otherwise, and LOC is the location of the
824    option for options from the source file, UNKNOWN_LOCATION
825    otherwise.  GENERATED_P is true for an option generated as part of
826    processing another option or otherwise generated internally, false
827    for one explicitly passed by the user.  Returns false if the switch
828    was invalid.  DC is the diagnostic context for options affecting
829    diagnostics state, or NULL.  */
830
831 static bool
832 handle_option (struct gcc_options *opts,
833                struct gcc_options *opts_set,
834                const struct cl_decoded_option *decoded,
835                unsigned int lang_mask, int kind, location_t loc,
836                const struct cl_option_handlers *handlers,
837                bool generated_p, diagnostic_context *dc)
838 {
839   size_t opt_index = decoded->opt_index;
840   const char *arg = decoded->arg;
841   int value = decoded->value;
842   const struct cl_option *option = &cl_options[opt_index];
843   void *flag_var = option_flag_var (opt_index, opts);
844   size_t i;
845
846   if (flag_var)
847     set_option (opts, (generated_p ? NULL : opts_set),
848                 opt_index, value, arg, kind, loc, dc);
849
850   for (i = 0; i < handlers->num_handlers; i++)
851     if (option->flags & handlers->handlers[i].mask)
852       {
853         if (!handlers->handlers[i].handler (opts, opts_set, decoded,
854                                             lang_mask, kind, loc,
855                                             handlers, dc))
856           return false;
857         else
858           handlers->post_handling_callback (decoded,
859                                             handlers->handlers[i].mask);
860       }
861   
862   return true;
863 }
864
865 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
866    option instead of DECODED.  This is used for callbacks when one
867    option implies another instead of an option being decoded from the
868    command line.  */
869
870 bool
871 handle_generated_option (struct gcc_options *opts,
872                          struct gcc_options *opts_set,
873                          size_t opt_index, const char *arg, int value,
874                          unsigned int lang_mask, int kind, location_t loc,
875                          const struct cl_option_handlers *handlers,
876                          diagnostic_context *dc)
877 {
878   struct cl_decoded_option decoded;
879
880   generate_option (opt_index, arg, value, lang_mask, &decoded);
881   return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
882                         handlers, true, dc);
883 }
884
885 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
886    VALUE for a front end using LANG_MASK.  This is used when the
887    compiler generates options internally.  */
888
889 void
890 generate_option (size_t opt_index, const char *arg, int value,
891                  unsigned int lang_mask, struct cl_decoded_option *decoded)
892 {
893   const struct cl_option *option = &cl_options[opt_index];
894
895   decoded->opt_index = opt_index;
896   decoded->warn_message = NULL;
897   decoded->arg = arg;
898   decoded->value = value;
899   decoded->errors = (option_ok_for_language (option, lang_mask)
900                      ? 0
901                      : CL_ERR_WRONG_LANG);
902
903   generate_canonical_option (opt_index, arg, value, decoded);
904   switch (decoded->canonical_option_num_elements)
905     {
906     case 1:
907       decoded->orig_option_with_args_text = decoded->canonical_option[0];
908       break;
909
910     case 2:
911       decoded->orig_option_with_args_text
912         = concat (decoded->canonical_option[0], " ",
913                   decoded->canonical_option[1], NULL);
914       break;
915
916     default:
917       gcc_unreachable ();
918     }
919 }
920
921 /* Fill in *DECODED with an option for input file FILE.  */
922
923 void
924 generate_option_input_file (const char *file,
925                             struct cl_decoded_option *decoded)
926 {
927   decoded->opt_index = OPT_SPECIAL_input_file;
928   decoded->warn_message = NULL;
929   decoded->arg = file;
930   decoded->orig_option_with_args_text = file;
931   decoded->canonical_option_num_elements = 1;
932   decoded->canonical_option[0] = file;
933   decoded->canonical_option[1] = NULL;
934   decoded->canonical_option[2] = NULL;
935   decoded->canonical_option[3] = NULL;
936   decoded->value = 1;
937   decoded->errors = 0;
938 }
939
940 /* Handle the switch DECODED (location LOC) for the language indicated
941    by LANG_MASK, using the handlers in *HANDLERS and setting fields in
942    OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
943    diagnostic options.  */
944
945 void
946 read_cmdline_option (struct gcc_options *opts,
947                      struct gcc_options *opts_set,
948                      struct cl_decoded_option *decoded,
949                      location_t loc,
950                      unsigned int lang_mask,
951                      const struct cl_option_handlers *handlers,
952                      diagnostic_context *dc)
953 {
954   const struct cl_option *option;
955   const char *opt = decoded->orig_option_with_args_text;
956
957   if (decoded->warn_message)
958     warning_at (loc, 0, decoded->warn_message, opt);
959
960   if (decoded->opt_index == OPT_SPECIAL_unknown)
961     {
962       if (handlers->unknown_option_callback (decoded))
963         error_at (loc, "unrecognized command line option %qs", decoded->arg);
964       return;
965     }
966
967   if (decoded->opt_index == OPT_SPECIAL_ignore)
968     return;
969
970   option = &cl_options[decoded->opt_index];
971
972   if (decoded->errors & CL_ERR_DISABLED)
973     {
974       error_at (loc, "command line option %qs"
975                 " is not supported by this configuration", opt);
976       return;
977     }
978
979   if (decoded->errors & CL_ERR_MISSING_ARG)
980     {
981       if (option->missing_argument_error)
982         error_at (loc, option->missing_argument_error, opt);
983       else
984         error_at (loc, "missing argument to %qs", opt);
985       return;
986     }
987
988   if (decoded->errors & CL_ERR_UINT_ARG)
989     {
990       error_at (loc, "argument to %qs should be a non-negative integer",
991                 option->opt_text);
992       return;
993     }
994
995   if (decoded->errors & CL_ERR_ENUM_ARG)
996     {
997       const struct cl_enum *e = &cl_enums[option->var_enum];
998       unsigned int i;
999       size_t len;
1000       char *s, *p;
1001
1002       if (e->unknown_error)
1003         error_at (loc, e->unknown_error, decoded->arg);
1004       else
1005         error_at (loc, "unrecognized argument in option %qs", opt);
1006
1007       len = 0;
1008       for (i = 0; e->values[i].arg != NULL; i++)
1009         len += strlen (e->values[i].arg) + 1;
1010
1011       s = XALLOCAVEC (char, len);
1012       p = s;
1013       for (i = 0; e->values[i].arg != NULL; i++)
1014         {
1015           size_t arglen = strlen (e->values[i].arg);
1016           memcpy (p, e->values[i].arg, arglen);
1017           p[arglen] = ' ';
1018           p += arglen + 1;
1019         }
1020       p[-1] = 0;
1021       inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1022       return;
1023     }
1024
1025   if (decoded->errors & CL_ERR_WRONG_LANG)
1026     {
1027       handlers->wrong_lang_callback (decoded, lang_mask);
1028       return;
1029     }
1030
1031   gcc_assert (!decoded->errors);
1032
1033   if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1034                       loc, handlers, false, dc))
1035     error_at (loc, "unrecognized command line option %qs", opt);
1036 }
1037
1038 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1039    OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1040    location LOC, using diagnostic context DC if not NULL for
1041    diagnostic classification.  */
1042
1043 void
1044 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1045             int opt_index, int value, const char *arg, int kind,
1046             location_t loc, diagnostic_context *dc)
1047 {
1048   const struct cl_option *option = &cl_options[opt_index];
1049   void *flag_var = option_flag_var (opt_index, opts);
1050   void *set_flag_var = NULL;
1051
1052   if (!flag_var)
1053     return;
1054
1055   if (opts_set != NULL)
1056     set_flag_var = option_flag_var (opt_index, opts_set);
1057
1058   switch (option->var_type)
1059     {
1060     case CLVC_BOOLEAN:
1061         *(int *) flag_var = value;
1062         if (set_flag_var)
1063           *(int *) set_flag_var = 1;
1064         break;
1065
1066     case CLVC_EQUAL:
1067         *(int *) flag_var = (value
1068                              ? option->var_value
1069                              : !option->var_value);
1070         if (set_flag_var)
1071           *(int *) set_flag_var = 1;
1072         break;
1073
1074     case CLVC_BIT_CLEAR:
1075     case CLVC_BIT_SET:
1076         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1077           *(int *) flag_var |= option->var_value;
1078         else
1079           *(int *) flag_var &= ~option->var_value;
1080         if (set_flag_var)
1081           *(int *) set_flag_var |= option->var_value;
1082         break;
1083
1084     case CLVC_STRING:
1085         *(const char **) flag_var = arg;
1086         if (set_flag_var)
1087           *(const char **) set_flag_var = "";
1088         break;
1089
1090     case CLVC_ENUM:
1091       {
1092         const struct cl_enum *e = &cl_enums[option->var_enum];
1093
1094         e->set (flag_var, value);
1095         if (set_flag_var)
1096           e->set (set_flag_var, 1);
1097       }
1098       break;
1099
1100     case CLVC_DEFER:
1101         {
1102           VEC(cl_deferred_option,heap) *vec
1103             = (VEC(cl_deferred_option,heap) *) *(void **) flag_var;
1104           cl_deferred_option *p;
1105
1106           p = VEC_safe_push (cl_deferred_option, heap, vec, NULL);
1107           p->opt_index = opt_index;
1108           p->arg = arg;
1109           p->value = value;
1110           *(void **) flag_var = vec;
1111           if (set_flag_var)
1112             *(void **) set_flag_var = vec;
1113         }
1114         break;
1115     }
1116
1117   if ((diagnostic_t) kind != DK_UNSPECIFIED
1118       && dc != NULL)
1119     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1120 }
1121
1122 /* Return the address of the flag variable for option OPT_INDEX in
1123    options structure OPTS, or NULL if there is no flag variable.  */
1124
1125 void *
1126 option_flag_var (int opt_index, struct gcc_options *opts)
1127 {
1128   const struct cl_option *option = &cl_options[opt_index];
1129
1130   if (option->flag_var_offset == (unsigned short) -1)
1131     return NULL;
1132   return (void *)(((char *) opts) + option->flag_var_offset);
1133 }
1134
1135 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1136    or -1 if it isn't a simple on-off switch.  */
1137
1138 int
1139 option_enabled (int opt_idx, void *opts)
1140 {
1141   const struct cl_option *option = &(cl_options[opt_idx]);
1142   struct gcc_options *optsg = (struct gcc_options *) opts;
1143   void *flag_var = option_flag_var (opt_idx, optsg);
1144
1145   if (flag_var)
1146     switch (option->var_type)
1147       {
1148       case CLVC_BOOLEAN:
1149         return *(int *) flag_var != 0;
1150
1151       case CLVC_EQUAL:
1152         return *(int *) flag_var == option->var_value;
1153
1154       case CLVC_BIT_CLEAR:
1155         return (*(int *) flag_var & option->var_value) == 0;
1156
1157       case CLVC_BIT_SET:
1158         return (*(int *) flag_var & option->var_value) != 0;
1159
1160       case CLVC_STRING:
1161       case CLVC_ENUM:
1162       case CLVC_DEFER:
1163         break;
1164       }
1165   return -1;
1166 }
1167
1168 /* Fill STATE with the current state of option OPTION in OPTS.  Return
1169    true if there is some state to store.  */
1170
1171 bool
1172 get_option_state (struct gcc_options *opts, int option,
1173                   struct cl_option_state *state)
1174 {
1175   void *flag_var = option_flag_var (option, opts);
1176
1177   if (flag_var == 0)
1178     return false;
1179
1180   switch (cl_options[option].var_type)
1181     {
1182     case CLVC_BOOLEAN:
1183     case CLVC_EQUAL:
1184       state->data = flag_var;
1185       state->size = sizeof (int);
1186       break;
1187
1188     case CLVC_BIT_CLEAR:
1189     case CLVC_BIT_SET:
1190       state->ch = option_enabled (option, opts);
1191       state->data = &state->ch;
1192       state->size = 1;
1193       break;
1194
1195     case CLVC_STRING:
1196       state->data = *(const char **) flag_var;
1197       if (state->data == 0)
1198         state->data = "";
1199       state->size = strlen ((const char *) state->data) + 1;
1200       break;
1201
1202     case CLVC_ENUM:
1203       state->data = flag_var;
1204       state->size = cl_enums[cl_options[option].var_enum].var_size;
1205       break;
1206
1207     case CLVC_DEFER:
1208       return false;
1209     }
1210   return true;
1211 }
1212
1213 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1214    handlers HANDLERS) to have diagnostic kind KIND for option
1215    structures OPTS and OPTS_SET and diagnostic context DC (possibly
1216    NULL), at location LOC (UNKNOWN_LOCATION for -Werror=).  If IMPLY,
1217    the warning option in question is implied at this point.  This is
1218    used by -Werror= and #pragma GCC diagnostic.  */
1219
1220 void
1221 control_warning_option (unsigned int opt_index, int kind, bool imply,
1222                         location_t loc, unsigned int lang_mask,
1223                         const struct cl_option_handlers *handlers,
1224                         struct gcc_options *opts,
1225                         struct gcc_options *opts_set,
1226                         diagnostic_context *dc)
1227 {
1228   if (cl_options[opt_index].alias_target != N_OPTS)
1229     opt_index = cl_options[opt_index].alias_target;
1230   if (opt_index == OPT_SPECIAL_ignore)
1231     return;
1232   if (dc)
1233     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1234   if (imply)
1235     {
1236       /* -Werror=foo implies -Wfoo.  */
1237       if (cl_options[opt_index].var_type == CLVC_BOOLEAN)
1238         handle_generated_option (opts, opts_set,
1239                                  opt_index, NULL, 1, lang_mask,
1240                                  kind, loc, handlers, dc);
1241     }
1242 }