OSDN Git Service

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