OSDN Git Service

* defaults.h (DEFAULT_WORD_SWITCH_TAKES_ARG,
[pf3gnuchains/gcc-fork.git] / gcc / opts-common.c
1 /* Command line option handling.
2    Copyright (C) 2006, 2007, 2008, 2010 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 #include "tm.h" /* For TARGET_OPTION_TRANSLATE_TABLE.  */
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, 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 integer made up solely of digits, 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   return -1;
166 }
167
168 /* Return whether OPTION is OK for the language given by
169    LANG_MASK.  */
170 static bool
171 option_ok_for_language (const struct cl_option *option,
172                         unsigned int lang_mask)
173 {
174   if (!(option->flags & lang_mask))
175     return false;
176   else if ((option->flags & CL_TARGET)
177            && (option->flags & (CL_LANG_ALL | CL_DRIVER))
178            && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
179     /* Complain for target flag language mismatches if any languages
180        are specified.  */
181     return false;
182   return true;
183 }
184
185
186 /* Fill in the canonical option part of *DECODED with an option
187    described by OPT_INDEX, ARG and VALUE.  */
188
189 static void
190 generate_canonical_option (size_t opt_index, const char *arg, int value,
191                            struct cl_decoded_option *decoded)
192 {
193   const struct cl_option *option = &cl_options[opt_index];
194   const char *opt_text = option->opt_text;
195
196   if (value == 0
197       && !(option->flags & CL_REJECT_NEGATIVE)
198       && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
199     {
200       char *t = XNEWVEC (char, option->opt_len + 5);
201       t[0] = '-';
202       t[1] = opt_text[1];
203       t[2] = 'n';
204       t[3] = 'o';
205       t[4] = '-';
206       memcpy (t + 5, opt_text + 2, option->opt_len);
207       opt_text = t;
208     }
209
210   decoded->canonical_option[2] = NULL;
211   decoded->canonical_option[3] = NULL;
212
213   if (arg)
214     {
215       if ((option->flags & CL_SEPARATE)
216           && !(option->flags & CL_SEPARATE_ALIAS))
217         {
218           decoded->canonical_option[0] = opt_text;
219           decoded->canonical_option[1] = arg;
220           decoded->canonical_option_num_elements = 2;
221         }
222       else
223         {
224           gcc_assert (option->flags & CL_JOINED);
225           decoded->canonical_option[0] = concat (opt_text, arg, NULL);
226           decoded->canonical_option[1] = NULL;
227           decoded->canonical_option_num_elements = 1;
228         }
229     }
230   else
231     {
232       decoded->canonical_option[0] = opt_text;
233       decoded->canonical_option[1] = NULL;
234       decoded->canonical_option_num_elements = 1;
235     }
236 }
237
238 /* Structure describing mappings from options on the command line to
239    options to look up with find_opt.  */
240 struct option_map
241 {
242   /* Prefix of the option on the command line.  */
243   const char *opt0;
244   /* If two argv elements are considered to be merged into one option,
245      prefix for the second element, otherwise NULL.  */
246   const char *opt1;
247   /* The new prefix to map to.  */
248   const char *new_prefix;
249   /* Whether at least one character is needed following opt1 or opt0
250      for this mapping to be used.  (--optimize= is valid for -O, but
251      --warn- is not valid for -W.)  */
252   bool another_char_needed;
253   /* Whether the original option is a negated form of the option
254      resulting from this map.  */
255   bool negated;
256 };
257 static const struct option_map option_map[] =
258   {
259     { "-Wno-", NULL, "-W", false, true },
260     { "-fno-", NULL, "-f", false, true },
261     { "-mno-", NULL, "-m", false, true },
262     { "--debug=", NULL, "-g", false, false },
263     { "--machine-", NULL, "-m", true, false },
264     { "--machine-no-", NULL, "-m", false, true },
265     { "--machine=", NULL, "-m", false, false },
266     { "--machine=no-", NULL, "-m", false, true },
267     { "--machine", "", "-m", false, false },
268     { "--machine", "no-", "-m", false, true },
269     { "--optimize=", NULL, "-O", false, false },
270     { "--std=", NULL, "-std=", false, false },
271     { "--std", "", "-std=", false, false },
272     { "--warn-", NULL, "-W", true, false },
273     { "--warn-no-", NULL, "-W", false, true },
274     { "--", NULL, "-f", true, false },
275     { "--no-", NULL, "-f", false, true }
276   };
277
278 /* Decode the switch beginning at ARGV for the language indicated by
279    LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
280    the structure *DECODED.  Returns the number of switches
281    consumed.  */
282
283 static unsigned int
284 decode_cmdline_option (const char **argv, unsigned int lang_mask,
285                        struct cl_decoded_option *decoded)
286 {
287   size_t opt_index;
288   const char *arg = 0;
289   int value = 1;
290   unsigned int result = 1, i, extra_args, separate_args;
291   int adjust_len = 0;
292   size_t total_len;
293   char *p;
294   const struct cl_option *option;
295   int errors = 0;
296   const char *warn_message = NULL;
297   bool separate_arg_flag;
298   bool joined_arg_flag;
299   bool have_separate_arg = false;
300
301   extra_args = 0;
302
303   opt_index = find_opt (argv[0] + 1, lang_mask);
304   i = 0;
305   while (opt_index == OPT_SPECIAL_unknown
306          && i < ARRAY_SIZE (option_map))
307     {
308       const char *opt0 = option_map[i].opt0;
309       const char *opt1 = option_map[i].opt1;
310       const char *new_prefix = option_map[i].new_prefix;
311       bool another_char_needed = option_map[i].another_char_needed;
312       size_t opt0_len = strlen (opt0);
313       size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
314       size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
315       size_t new_prefix_len = strlen (new_prefix);
316
317       extra_args = (opt1 == NULL ? 0 : 1);
318       value = !option_map[i].negated;
319
320       if (strncmp (argv[0], opt0, opt0_len) == 0
321           && (opt1 == NULL
322               || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
323           && (!another_char_needed
324               || argv[extra_args][optn_len] != 0))
325         {
326           size_t arglen = strlen (argv[extra_args]);
327           char *dup;
328
329           adjust_len = (int) optn_len - (int) new_prefix_len;
330           dup = XNEWVEC (char, arglen + 1 - adjust_len);
331           memcpy (dup, new_prefix, new_prefix_len);
332           memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
333                   arglen - optn_len + 1);
334           opt_index = find_opt (dup + 1, lang_mask);
335           free (dup);
336         }
337       i++;
338     }
339
340   if (opt_index == OPT_SPECIAL_unknown)
341     {
342       arg = argv[0];
343       extra_args = 0;
344       value = 1;
345       goto done;
346     }
347
348   option = &cl_options[opt_index];
349
350   /* Reject negative form of switches that don't take negatives as
351      unrecognized.  */
352   if (!value && (option->flags & CL_REJECT_NEGATIVE))
353     {
354       opt_index = OPT_SPECIAL_unknown;
355       errors |= CL_ERR_NEGATIVE;
356       arg = argv[0];
357       goto done;
358     }
359
360   result = extra_args + 1;
361   warn_message = option->warn_message;
362
363   /* Check to see if the option is disabled for this configuration.  */
364   if (option->flags & CL_DISABLED)
365     errors |= CL_ERR_DISABLED;
366
367   /* Determine whether there may be a separate argument based on
368      whether this option is being processed for the driver, and, if
369      so, how many such arguments.  */
370   separate_arg_flag = ((option->flags & CL_SEPARATE)
371                        && !((option->flags & CL_NO_DRIVER_ARG)
372                             && (lang_mask & CL_DRIVER)));
373   separate_args = (separate_arg_flag
374                    ? ((option->flags & CL_SEPARATE_NARGS_MASK)
375                       >> CL_SEPARATE_NARGS_SHIFT) + 1
376                    : 0);
377   joined_arg_flag = (option->flags & CL_JOINED) != 0;
378
379   /* Sort out any argument the switch takes.  */
380   if (joined_arg_flag)
381     {
382       /* Have arg point to the original switch.  This is because
383          some code, such as disable_builtin_function, expects its
384          argument to be persistent until the program exits.  */
385       arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
386
387       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
388         {
389           if (separate_arg_flag)
390             {
391               arg = argv[extra_args + 1];
392               result = extra_args + 2;
393               if (arg == NULL)
394                 result = extra_args + 1;
395               else
396                 have_separate_arg = true;
397             }
398           else
399             /* Missing argument.  */
400             arg = NULL;
401         }
402     }
403   else if (separate_arg_flag)
404     {
405       arg = argv[extra_args + 1];
406       for (i = 0; i < separate_args; i++)
407         if (argv[extra_args + 1 + i] == NULL)
408           {
409             errors |= CL_ERR_MISSING_ARG;
410             break;
411           }
412       result = extra_args + 1 + i;
413       if (arg != NULL)
414         have_separate_arg = true;
415     }
416
417   if (arg == NULL && (separate_arg_flag || joined_arg_flag))
418     errors |= CL_ERR_MISSING_ARG;
419
420   /* Is this option an alias (or an ignored option, marked as an alias
421      of OPT_SPECIAL_ignore)?  */
422   if (option->alias_target != N_OPTS
423       && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
424     {
425       size_t new_opt_index = option->alias_target;
426
427       if (new_opt_index == OPT_SPECIAL_ignore)
428         {
429           gcc_assert (option->alias_arg == NULL);
430           gcc_assert (option->neg_alias_arg == NULL);
431           opt_index = new_opt_index;
432           arg = NULL;
433           value = 1;
434         }
435       else
436         {
437           const struct cl_option *new_option = &cl_options[new_opt_index];
438
439           /* The new option must not be an alias itself.  */
440           gcc_assert (new_option->alias_target == N_OPTS
441                       || (new_option->flags & CL_SEPARATE_ALIAS));
442
443           if (option->neg_alias_arg)
444             {
445               gcc_assert (option->alias_arg != NULL);
446               gcc_assert (arg == NULL);
447               if (value)
448                 arg = option->alias_arg;
449               else
450                 arg = option->neg_alias_arg;
451               value = 1;
452             }
453           else if (option->alias_arg)
454             {
455               gcc_assert (value == 1);
456               gcc_assert (arg == NULL);
457               arg = option->alias_arg;
458             }
459
460           opt_index = new_opt_index;
461           option = new_option;
462
463           if (value == 0)
464             gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
465
466           /* Recompute what arguments are allowed.  */
467           separate_arg_flag = ((option->flags & CL_SEPARATE)
468                                && !((option->flags & CL_NO_DRIVER_ARG)
469                                     && (lang_mask & CL_DRIVER)));
470           joined_arg_flag = (option->flags & CL_JOINED) != 0;
471
472           if (separate_args > 1 || (option->flags & CL_SEPARATE_NARGS_MASK))
473             gcc_assert (separate_args
474                         == ((option->flags & CL_SEPARATE_NARGS_MASK)
475                             >> CL_SEPARATE_NARGS_SHIFT) + 1);
476
477           if (!(errors & CL_ERR_MISSING_ARG))
478             {
479               if (separate_arg_flag || joined_arg_flag)
480                 {
481                   if ((option->flags & CL_MISSING_OK) && arg == NULL)
482                     arg = "";
483                   gcc_assert (arg != NULL);
484                 }
485               else
486                 gcc_assert (arg == NULL);
487             }
488
489           /* Recheck for warnings and disabled options.  */
490           if (option->warn_message)
491             {
492               gcc_assert (warn_message == NULL);
493               warn_message = option->warn_message;
494             }
495           if (option->flags & CL_DISABLED)
496             errors |= CL_ERR_DISABLED;
497         }
498     }
499
500   /* Check if this is a switch for a different front end.  */
501   if (!option_ok_for_language (option, lang_mask))
502     errors |= CL_ERR_WRONG_LANG;
503
504   /* If the switch takes an integer, convert it.  */
505   if (arg && (option->flags & CL_UINTEGER))
506     {
507       value = integral_argument (arg);
508       if (value == -1)
509         errors |= CL_ERR_UINT_ARG;
510     }
511
512  done:
513   decoded->opt_index = opt_index;
514   decoded->arg = arg;
515   decoded->value = value;
516   decoded->errors = errors;
517   decoded->warn_message = warn_message;
518
519   if (opt_index == OPT_SPECIAL_unknown)
520     gcc_assert (result == 1);
521
522   gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
523   decoded->canonical_option_num_elements = result;
524   total_len = 0;
525   for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
526     {
527       if (i < result)
528         {
529           if (opt_index == OPT_SPECIAL_unknown)
530             decoded->canonical_option[i] = argv[i];
531           else
532             decoded->canonical_option[i] = NULL;
533           total_len += strlen (argv[i]) + 1;
534         }
535       else
536         decoded->canonical_option[i] = NULL;
537     }
538   if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
539     {
540       generate_canonical_option (opt_index, arg, value, decoded);
541       if (separate_args > 1)
542         {
543           for (i = 0; i < separate_args; i++)
544             {
545               if (argv[extra_args + 1 + i] == NULL)
546                   break;
547               else
548                 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
549             }
550           gcc_assert (result == 1 + i);
551           decoded->canonical_option_num_elements = result;
552         }
553     }
554   decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
555   for (i = 0; i < result; i++)
556     {
557       size_t len = strlen (argv[i]);
558
559       memcpy (p, argv[i], len);
560       p += len;
561       if (i == result - 1)
562         *p++ = 0;
563       else
564         *p++ = ' ';
565     }
566
567   return result;
568 }
569
570 #ifdef TARGET_OPTION_TRANSLATE_TABLE
571 static const struct {
572   const char *const option_found;
573   const char *const replacements;
574 } target_option_translations[] =
575 {
576   TARGET_OPTION_TRANSLATE_TABLE,
577   { 0, 0 }
578 };
579 #endif
580
581 /* Decode command-line options (ARGC and ARGV being the arguments of
582    main) into an array, setting *DECODED_OPTIONS to a pointer to that
583    array and *DECODED_OPTIONS_COUNT to the number of entries in the
584    array.  The first entry in the array is always one for the program
585    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
586    flags applicable for decoding (including CL_COMMON and CL_TARGET if
587    those options should be considered applicable).  Do not produce any
588    diagnostics or set state outside of these variables.  */
589
590 void
591 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
592                                  unsigned int lang_mask,
593                                  struct cl_decoded_option **decoded_options,
594                                  unsigned int *decoded_options_count)
595 {
596   unsigned int n, i, target_translate_from;
597   struct cl_decoded_option *opt_array;
598   unsigned int num_decoded_options;
599   bool argv_copied = false;
600
601   opt_array = XNEWVEC (struct cl_decoded_option, argc);
602
603   opt_array[0].opt_index = OPT_SPECIAL_program_name;
604   opt_array[0].warn_message = NULL;
605   opt_array[0].arg = argv[0];
606   opt_array[0].orig_option_with_args_text = argv[0];
607   opt_array[0].canonical_option_num_elements = 1;
608   opt_array[0].canonical_option[0] = argv[0];
609   opt_array[0].canonical_option[1] = NULL;
610   opt_array[0].canonical_option[2] = NULL;
611   opt_array[0].canonical_option[3] = NULL;
612   opt_array[0].value = 1;
613   opt_array[0].errors = 0;
614   num_decoded_options = 1;
615
616   target_translate_from = 1;
617   for (i = 1; i < argc; i += n)
618     {
619       const char *opt = argv[i];
620
621       /* Interpret "-" or a non-switch as a file name.  */
622       if (opt[0] != '-' || opt[1] == '\0')
623         {
624           generate_option_input_file (opt, &opt_array[num_decoded_options]);
625           num_decoded_options++;
626           n = 1;
627           continue;
628         }
629
630       if (i >= target_translate_from && (lang_mask & CL_DRIVER))
631         {
632 #ifdef TARGET_OPTION_TRANSLATE_TABLE
633           int tott_idx;
634
635           for (tott_idx = 0;
636                target_option_translations[tott_idx].option_found;
637                tott_idx++)
638             {
639               if (strcmp (target_option_translations[tott_idx].option_found,
640                           argv[i]) == 0)
641                 {
642                   unsigned int spaces = 0;
643                   unsigned int m = 0;
644                   const char *sp;
645                   char *np;
646
647                   for (sp = target_option_translations[tott_idx].replacements;
648                        *sp; sp++)
649                     {
650                       if (*sp == ' ')
651                         {
652                           spaces++;
653                           while (*sp == ' ')
654                             sp++;
655                           sp--;
656                         }
657                     }
658
659                   if (spaces)
660                     {
661                       int new_argc = argc + spaces;
662                       if (argv_copied)
663                         argv = XRESIZEVEC (const char *, argv, new_argc + 1);
664                       else
665                         {
666                           const char **new_argv = XNEWVEC (const char *,
667                                                            new_argc + 1);
668                           memcpy (new_argv, argv,
669                                   (argc + 1) * sizeof (const char *));
670                           argv = new_argv;
671                           argv_copied = true;
672                         }
673                       memmove (&argv[i] + spaces, &argv[i],
674                                (argc + 1 - i) * sizeof (const char *));
675                       argc = new_argc;
676                       opt_array = XRESIZEVEC (struct cl_decoded_option,
677                                               opt_array, argc);
678                     }
679
680                   sp = target_option_translations[tott_idx].replacements;
681                   np = xstrdup (sp);
682
683                   while (1)
684                     {
685                       while (*np == ' ')
686                         np++;
687                       if (*np == 0)
688                         break;
689                       argv[i + m++] = np;
690                       while (*np != ' ' && *np)
691                         np++;
692                       if (*np == 0)
693                         break;
694                       *np++ = 0;
695                     }
696
697                   target_translate_from = i + m;
698                   gcc_assert (m == spaces + 1);
699                   break;
700                 }
701             }
702 #endif
703         }
704
705       n = decode_cmdline_option (argv + i, lang_mask,
706                                  &opt_array[num_decoded_options]);
707       num_decoded_options++;
708     }
709
710   if (argv_copied)
711     free (argv);
712   *decoded_options = opt_array;
713   *decoded_options_count = num_decoded_options;
714   prune_options (decoded_options, decoded_options_count);
715 }
716
717 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
718    next one is the same as ORIG_NEXT_OPT_IDX.  */
719
720 static bool
721 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
722 {
723   /* An option can be canceled by the same option or an option with
724      Negative.  */
725   if (cl_options [next_opt_idx].neg_index == opt_idx)
726     return true;
727
728   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
729     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
730                           orig_next_opt_idx);
731
732   return false;
733 }
734
735 /* Filter out options canceled by the ones after them.  */
736
737 static void
738 prune_options (struct cl_decoded_option **decoded_options,
739                unsigned int *decoded_options_count)
740 {
741   unsigned int old_decoded_options_count = *decoded_options_count;
742   struct cl_decoded_option *old_decoded_options = *decoded_options;
743   unsigned int new_decoded_options_count;
744   struct cl_decoded_option *new_decoded_options
745     = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
746   unsigned int i;
747   const struct cl_option *option;
748
749   /* Remove arguments which are negated by others after them.  */
750   new_decoded_options_count = 0;
751   for (i = 0; i < old_decoded_options_count; i++)
752     {
753       unsigned int j, opt_idx, next_opt_idx;
754
755       if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
756         goto keep;
757
758       opt_idx = old_decoded_options[i].opt_index;
759       switch (opt_idx)
760         {
761         case OPT_SPECIAL_unknown:
762         case OPT_SPECIAL_ignore:
763         case OPT_SPECIAL_program_name:
764         case OPT_SPECIAL_input_file:
765           goto keep;
766
767         default:
768           gcc_assert (opt_idx < cl_options_count);
769           option = &cl_options[opt_idx];
770           if (option->neg_index < 0)
771             goto keep;
772
773           /* Skip joined switches.  */
774           if ((option->flags & CL_JOINED))
775             goto keep;
776
777           for (j = i + 1; j < old_decoded_options_count; j++)
778             {
779               if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
780                 continue;
781               next_opt_idx = old_decoded_options[j].opt_index;
782               if (next_opt_idx >= cl_options_count)
783                 continue;
784               if (cl_options[next_opt_idx].neg_index < 0)
785                 continue;
786               if ((cl_options[next_opt_idx].flags & CL_JOINED))
787                   continue;
788               if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
789                 break;
790             }
791           if (j == old_decoded_options_count)
792             {
793 keep:
794               new_decoded_options[new_decoded_options_count]
795                 = old_decoded_options[i];
796               new_decoded_options_count++;
797             }
798           break;
799         }
800     }
801
802   free (old_decoded_options);
803   new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
804                                     new_decoded_options,
805                                     new_decoded_options_count);
806   *decoded_options = new_decoded_options;
807   *decoded_options_count = new_decoded_options_count;
808 }
809
810 /* Handle option DECODED for the language indicated by LANG_MASK,
811    using the handlers in HANDLERS and setting fields in OPTS and
812    OPTS_SET.  KIND is the diagnostic_t if this is a diagnostics
813    option, DK_UNSPECIFIED otherwise.  GENERATED_P is true for an
814    option generated as part of processing another option or otherwise
815    generated internally, false for one explicitly passed by the user.
816    Returns false if the switch was invalid.  DC is the diagnostic
817    context for options affecting diagnostics state, or NULL.  */
818
819 bool
820 handle_option (struct gcc_options *opts,
821                struct gcc_options *opts_set,
822                const struct cl_decoded_option *decoded,
823                unsigned int lang_mask, int kind,
824                const struct cl_option_handlers *handlers,
825                bool generated_p, diagnostic_context *dc)
826 {
827   size_t opt_index = decoded->opt_index;
828   const char *arg = decoded->arg;
829   int value = decoded->value;
830   const struct cl_option *option = &cl_options[opt_index];
831   void *flag_var = option_flag_var (opt_index, opts);
832   size_t i;
833
834   if (flag_var)
835     set_option (opts, (generated_p ? NULL : opts_set),
836                 opt_index, value, arg, kind, dc);
837
838   for (i = 0; i < handlers->num_handlers; i++)
839     if (option->flags & handlers->handlers[i].mask)
840       {
841         if (!handlers->handlers[i].handler (opts, opts_set, decoded,
842                                             lang_mask, kind, handlers))
843           return false;
844         else
845           handlers->post_handling_callback (decoded,
846                                             handlers->handlers[i].mask);
847       }
848   
849   return true;
850 }
851
852 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
853    option instead of DECODED.  This is used for callbacks when one
854    option implies another instead of an option being decoded from the
855    command line.  */
856
857 bool
858 handle_generated_option (struct gcc_options *opts,
859                          struct gcc_options *opts_set,
860                          size_t opt_index, const char *arg, int value,
861                          unsigned int lang_mask, int kind,
862                          const struct cl_option_handlers *handlers,
863                          diagnostic_context *dc)
864 {
865   struct cl_decoded_option decoded;
866
867   generate_option (opt_index, arg, value, lang_mask, &decoded);
868   return handle_option (opts, opts_set, &decoded, lang_mask, kind, handlers,
869                         true, dc);
870 }
871
872 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
873    VALUE for a front end using LANG_MASK.  This is used when the
874    compiler generates options internally.  */
875
876 void
877 generate_option (size_t opt_index, const char *arg, int value,
878                  unsigned int lang_mask, struct cl_decoded_option *decoded)
879 {
880   const struct cl_option *option = &cl_options[opt_index];
881
882   decoded->opt_index = opt_index;
883   decoded->warn_message = NULL;
884   decoded->arg = arg;
885   decoded->value = value;
886   decoded->errors = (option_ok_for_language (option, lang_mask)
887                      ? 0
888                      : CL_ERR_WRONG_LANG);
889
890   generate_canonical_option (opt_index, arg, value, decoded);
891   switch (decoded->canonical_option_num_elements)
892     {
893     case 1:
894       decoded->orig_option_with_args_text = decoded->canonical_option[0];
895       break;
896
897     case 2:
898       decoded->orig_option_with_args_text
899         = concat (decoded->canonical_option[0], " ",
900                   decoded->canonical_option[1], NULL);
901       break;
902
903     default:
904       gcc_unreachable ();
905     }
906 }
907
908 /* Fill in *DECODED with an option for input file FILE.  */
909
910 void
911 generate_option_input_file (const char *file,
912                             struct cl_decoded_option *decoded)
913 {
914   decoded->opt_index = OPT_SPECIAL_input_file;
915   decoded->warn_message = NULL;
916   decoded->arg = file;
917   decoded->orig_option_with_args_text = file;
918   decoded->canonical_option_num_elements = 1;
919   decoded->canonical_option[0] = file;
920   decoded->canonical_option[1] = NULL;
921   decoded->canonical_option[2] = NULL;
922   decoded->canonical_option[3] = NULL;
923   decoded->value = 1;
924   decoded->errors = 0;
925 }
926
927 /* Handle the switch DECODED for the language indicated by LANG_MASK,
928    using the handlers in *HANDLERS and setting fields in OPTS and
929    OPTS_SET and using diagnostic context DC (if not NULL) for
930    diagnostic options.  */
931
932 void
933 read_cmdline_option (struct gcc_options *opts,
934                      struct gcc_options *opts_set,
935                      struct cl_decoded_option *decoded,
936                      unsigned int lang_mask,
937                      const struct cl_option_handlers *handlers,
938                      diagnostic_context *dc)
939 {
940   const struct cl_option *option;
941   const char *opt = decoded->orig_option_with_args_text;
942
943   if (decoded->warn_message)
944     warning (0, decoded->warn_message, opt);
945
946   if (decoded->opt_index == OPT_SPECIAL_unknown)
947     {
948       if (handlers->unknown_option_callback (decoded))
949         error ("unrecognized command line option %qs", decoded->arg);
950       return;
951     }
952
953   if (decoded->opt_index == OPT_SPECIAL_ignore)
954     return;
955
956   option = &cl_options[decoded->opt_index];
957
958   if (decoded->errors & CL_ERR_DISABLED)
959     {
960       error ("command line option %qs"
961              " is not supported by this configuration", opt);
962       return;
963     }
964
965   if (decoded->errors & CL_ERR_WRONG_LANG)
966     {
967       handlers->wrong_lang_callback (decoded, lang_mask);
968       return;
969     }
970
971   if (decoded->errors & CL_ERR_MISSING_ARG)
972     {
973       if (option->missing_argument_error)
974         error (option->missing_argument_error, opt);
975       else
976         error ("missing argument to %qs", opt);
977       return;
978     }
979
980   if (decoded->errors & CL_ERR_UINT_ARG)
981     {
982       error ("argument to %qs should be a non-negative integer",
983              option->opt_text);
984       return;
985     }
986
987   gcc_assert (!decoded->errors);
988
989   if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
990                       handlers, false, dc))
991     error ("unrecognized command line option %qs", opt);
992 }
993
994 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
995    OPT_INDEX according to VALUE and ARG, diagnostic kind KIND, using
996    diagnostic context DC if not NULL for diagnostic
997    classification.  */
998
999 void
1000 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1001             int opt_index, int value, const char *arg, int kind,
1002             diagnostic_context *dc)
1003 {
1004   const struct cl_option *option = &cl_options[opt_index];
1005   void *flag_var = option_flag_var (opt_index, opts);
1006   void *set_flag_var = NULL;
1007
1008   if (!flag_var)
1009     return;
1010
1011   if (opts_set != NULL)
1012     set_flag_var = option_flag_var (opt_index, opts_set);
1013
1014   switch (option->var_type)
1015     {
1016     case CLVC_BOOLEAN:
1017         *(int *) flag_var = value;
1018         if (set_flag_var)
1019           *(int *) set_flag_var = 1;
1020         break;
1021
1022     case CLVC_EQUAL:
1023         *(int *) flag_var = (value
1024                              ? option->var_value
1025                              : !option->var_value);
1026         if (set_flag_var)
1027           *(int *) set_flag_var = 1;
1028         break;
1029
1030     case CLVC_BIT_CLEAR:
1031     case CLVC_BIT_SET:
1032         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1033           *(int *) flag_var |= option->var_value;
1034         else
1035           *(int *) flag_var &= ~option->var_value;
1036         if (set_flag_var)
1037           *(int *) set_flag_var |= option->var_value;
1038         break;
1039
1040     case CLVC_STRING:
1041         *(const char **) flag_var = arg;
1042         if (set_flag_var)
1043           *(const char **) set_flag_var = "";
1044         break;
1045     }
1046
1047   if ((diagnostic_t) kind != DK_UNSPECIFIED
1048       && dc != NULL)
1049     diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind,
1050                                     UNKNOWN_LOCATION);
1051 }
1052
1053 /* Return the address of the flag variable for option OPT_INDEX in
1054    options structure OPTS, or NULL if there is no flag variable.  */
1055
1056 void *
1057 option_flag_var (int opt_index, struct gcc_options *opts)
1058 {
1059   const struct cl_option *option = &cl_options[opt_index];
1060
1061   if (option->flag_var_offset == (unsigned short) -1)
1062     return NULL;
1063   return (void *)(((char *) opts) + option->flag_var_offset);
1064 }