OSDN Git Service

* doc/options.texi (SeparateAlias): Document.
[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 "options.h"
26 #include "diagnostic.h"
27 #include "tm.h" /* For SWITCH_TAKES_ARG and WORD_SWITCH_TAKES_ARG.  */
28
29 /* Perform a binary search to find which option the command-line INPUT
30    matches.  Returns its index in the option array, and
31    OPT_SPECIAL_unknown on failure.
32
33    This routine is quite subtle.  A normal binary search is not good
34    enough because some options can be suffixed with an argument, and
35    multiple sub-matches can occur, e.g. input of "-pedantic" matching
36    the initial substring of "-pedantic-errors".
37
38    A more complicated example is -gstabs.  It should match "-g" with
39    an argument of "stabs".  Suppose, however, that the number and list
40    of switches are such that the binary search tests "-gen-decls"
41    before having tested "-g".  This doesn't match, and as "-gen-decls"
42    is less than "-gstabs", it will become the lower bound of the
43    binary search range, and "-g" will never be seen.  To resolve this
44    issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
45    to "-g" so that failed searches that end between "-gen-decls" and
46    the lexicographically subsequent switch know to go back and see if
47    "-g" causes a match (which it does in this example).
48
49    This search is done in such a way that the longest match for the
50    front end in question wins.  If there is no match for the current
51    front end, the longest match for a different front end is returned
52    (or N_OPTS if none) and the caller emits an error message.  */
53 size_t
54 find_opt (const char *input, int lang_mask)
55 {
56   size_t mn, mx, md, opt_len;
57   size_t match_wrong_lang;
58   int comp;
59
60   mn = 0;
61   mx = cl_options_count;
62
63   /* Find mn such this lexicographical inequality holds:
64      cl_options[mn] <= input < cl_options[mn + 1].  */
65   while (mx - mn > 1)
66     {
67       md = (mn + mx) / 2;
68       opt_len = cl_options[md].opt_len;
69       comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
70
71       if (comp < 0)
72         mx = md;
73       else
74         mn = md;
75     }
76
77   /* This is the switch that is the best match but for a different
78      front end, or OPT_SPECIAL_unknown if there is no match at all.  */
79   match_wrong_lang = OPT_SPECIAL_unknown;
80
81   /* Backtrace the chain of possible matches, returning the longest
82      one, if any, that fits best.  With current GCC switches, this
83      loop executes at most twice.  */
84   do
85     {
86       const struct cl_option *opt = &cl_options[mn];
87
88       /* Is the input either an exact match or a prefix that takes a
89          joined argument?  */
90       if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
91           && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
92         {
93           /* If language is OK, return it.  */
94           if (opt->flags & lang_mask)
95             return mn;
96
97           /* If we haven't remembered a prior match, remember this
98              one.  Any prior match is necessarily better.  */
99           if (match_wrong_lang == OPT_SPECIAL_unknown)
100             match_wrong_lang = mn;
101         }
102
103       /* Try the next possibility.  This is cl_options_count if there
104          are no more.  */
105       mn = opt->back_chain;
106     }
107   while (mn != cl_options_count);
108
109   /* Return the best wrong match, or OPT_SPECIAL_unknown if none.  */
110   return match_wrong_lang;
111 }
112
113 /* If ARG is a non-negative integer made up solely of digits, return its
114    value, otherwise return -1.  */
115
116 int
117 integral_argument (const char *arg)
118 {
119   const char *p = arg;
120
121   while (*p && ISDIGIT (*p))
122     p++;
123
124   if (*p == '\0')
125     return atoi (arg);
126
127   return -1;
128 }
129
130 /* Return whether OPTION is OK for the language given by
131    LANG_MASK.  */
132 static bool
133 option_ok_for_language (const struct cl_option *option,
134                         unsigned int lang_mask)
135 {
136   if (!(option->flags & lang_mask))
137     return false;
138   else if ((option->flags & CL_TARGET)
139            && (option->flags & (CL_LANG_ALL | CL_DRIVER))
140            && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
141     /* Complain for target flag language mismatches if any languages
142        are specified.  */
143     return false;
144   return true;
145 }
146
147
148 /* Fill in the canonical option part of *DECODED with an option
149    described by OPT_INDEX, ARG and VALUE.  */
150
151 static void
152 generate_canonical_option (size_t opt_index, const char *arg, int value,
153                            struct cl_decoded_option *decoded)
154 {
155   const struct cl_option *option = &cl_options[opt_index];
156   const char *opt_text = option->opt_text;
157
158   if (value == 0
159       && !(option->flags & CL_REJECT_NEGATIVE)
160       && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
161     {
162       char *t = XNEWVEC (char, option->opt_len + 5);
163       t[0] = '-';
164       t[1] = opt_text[1];
165       t[2] = 'n';
166       t[3] = 'o';
167       t[4] = '-';
168       memcpy (t + 5, opt_text + 2, option->opt_len);
169       opt_text = t;
170     }
171
172   decoded->canonical_option[2] = NULL;
173   decoded->canonical_option[3] = NULL;
174
175   if (arg)
176     {
177       if ((option->flags & CL_SEPARATE)
178           && !(option->flags & CL_SEPARATE_ALIAS))
179         {
180           decoded->canonical_option[0] = opt_text;
181           decoded->canonical_option[1] = arg;
182           decoded->canonical_option_num_elements = 2;
183         }
184       else
185         {
186           gcc_assert (option->flags & CL_JOINED);
187           decoded->canonical_option[0] = concat (opt_text, arg, NULL);
188           decoded->canonical_option[1] = NULL;
189           decoded->canonical_option_num_elements = 1;
190         }
191     }
192   else
193     {
194       decoded->canonical_option[0] = opt_text;
195       decoded->canonical_option[1] = NULL;
196       decoded->canonical_option_num_elements = 1;
197     }
198 }
199
200 /* Decode the switch beginning at ARGV for the language indicated by
201    LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
202    the structure *DECODED.  Returns the number of switches
203    consumed.  */
204
205 static unsigned int
206 decode_cmdline_option (const char **argv, unsigned int lang_mask,
207                        struct cl_decoded_option *decoded)
208 {
209   size_t opt_index;
210   const char *opt, *arg = 0;
211   char *dup = 0;
212   int value = 1;
213   unsigned int result = 1, i;
214   size_t total_len;
215   char *p;
216   const struct cl_option *option;
217   int errors = 0;
218   const char *warn_message = NULL;
219   bool separate_arg_flag;
220   bool joined_arg_flag;
221   bool have_separate_arg = false;
222
223   opt = argv[0];
224
225   opt_index = find_opt (opt + 1, lang_mask);
226   if (opt_index == OPT_SPECIAL_unknown
227       && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
228       && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
229     {
230       /* Drop the "no-" from negative switches.  */
231       size_t len = strlen (opt) - 3;
232
233       dup = XNEWVEC (char, len + 1);
234       dup[0] = '-';
235       dup[1] = opt[1];
236       memcpy (dup + 2, opt + 5, len - 2 + 1);
237       opt = dup;
238       value = 0;
239       opt_index = find_opt (opt + 1, lang_mask);
240     }
241
242   if (opt_index == OPT_SPECIAL_unknown)
243     {
244       arg = argv[0];
245       goto done;
246     }
247
248   option = &cl_options[opt_index];
249
250   /* Reject negative form of switches that don't take negatives as
251      unrecognized.  */
252   if (!value && (option->flags & CL_REJECT_NEGATIVE))
253     {
254       opt_index = OPT_SPECIAL_unknown;
255       errors |= CL_ERR_NEGATIVE;
256       arg = argv[0];
257       goto done;
258     }
259
260   warn_message = option->warn_message;
261
262   /* Check to see if the option is disabled for this configuration.  */
263   if (option->flags & CL_DISABLED)
264     errors |= CL_ERR_DISABLED;
265
266   /* Determine whether there may be a separate argument based on
267      whether this option is being processed for the driver.  */
268   separate_arg_flag = ((option->flags & CL_SEPARATE)
269                        && !((option->flags & CL_NO_DRIVER_ARG)
270                             && (lang_mask & CL_DRIVER)));
271   joined_arg_flag = (option->flags & CL_JOINED) != 0;
272
273   /* Sort out any argument the switch takes.  */
274   if (joined_arg_flag)
275     {
276       /* Have arg point to the original switch.  This is because
277          some code, such as disable_builtin_function, expects its
278          argument to be persistent until the program exits.  */
279       arg = argv[0] + cl_options[opt_index].opt_len + 1;
280       if (!value)
281         arg += strlen ("no-");
282
283       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
284         {
285           if (separate_arg_flag)
286             {
287               arg = argv[1];
288               result = 2;
289               if (arg == NULL)
290                 result = 1;
291               else
292                 have_separate_arg = true;
293             }
294           else
295             /* Missing argument.  */
296             arg = NULL;
297         }
298     }
299   else if (separate_arg_flag)
300     {
301       arg = argv[1];
302       result = 2;
303       if (arg == NULL)
304         result = 1;
305       else
306         have_separate_arg = true;
307     }
308
309   if (arg == NULL && (separate_arg_flag || joined_arg_flag))
310     errors |= CL_ERR_MISSING_ARG;
311
312   /* Is this option an alias (or an ignored option, marked as an alias
313      of OPT_SPECIAL_ignore)?  */
314   if (option->alias_target != N_OPTS
315       && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
316     {
317       size_t new_opt_index = option->alias_target;
318
319       if (new_opt_index == OPT_SPECIAL_ignore)
320         {
321           gcc_assert (option->alias_arg == NULL);
322           gcc_assert (option->neg_alias_arg == NULL);
323           opt_index = new_opt_index;
324           arg = NULL;
325           value = 1;
326         }
327       else
328         {
329           const struct cl_option *new_option = &cl_options[new_opt_index];
330
331           /* The new option must not be an alias itself.  */
332           gcc_assert (new_option->alias_target == N_OPTS);
333
334           if (option->neg_alias_arg)
335             {
336               gcc_assert (option->alias_arg != NULL);
337               gcc_assert (arg == NULL);
338               if (value)
339                 arg = option->alias_arg;
340               else
341                 arg = option->neg_alias_arg;
342               value = 1;
343             }
344           else if (option->alias_arg)
345             {
346               gcc_assert (value == 1);
347               gcc_assert (arg == NULL);
348               arg = option->alias_arg;
349             }
350
351           opt_index = new_opt_index;
352           option = new_option;
353
354           if (value == 0)
355             gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
356
357           /* Recompute what arguments are allowed.  */
358           separate_arg_flag = ((option->flags & CL_SEPARATE)
359                                && !((option->flags & CL_NO_DRIVER_ARG)
360                                     && (lang_mask & CL_DRIVER)));
361           joined_arg_flag = (option->flags & CL_JOINED) != 0;
362
363           if (!(errors & CL_ERR_MISSING_ARG))
364             {
365               if (separate_arg_flag || joined_arg_flag)
366                 gcc_assert (arg != NULL);
367               else
368                 gcc_assert (arg == NULL);
369             }
370
371           /* Recheck for warnings and disabled options.  */
372           if (option->warn_message)
373             {
374               gcc_assert (warn_message == NULL);
375               warn_message = option->warn_message;
376             }
377           if (option->flags & CL_DISABLED)
378             errors |= CL_ERR_DISABLED;
379         }
380     }
381
382   /* Check if this is a switch for a different front end.  */
383   if (!option_ok_for_language (option, lang_mask))
384     errors |= CL_ERR_WRONG_LANG;
385
386   /* If the switch takes an integer, convert it.  */
387   if (arg && (option->flags & CL_UINTEGER))
388     {
389       value = integral_argument (arg);
390       if (value == -1)
391         errors |= CL_ERR_UINT_ARG;
392     }
393
394  done:
395   if (dup)
396     free (dup);
397   decoded->opt_index = opt_index;
398   decoded->arg = arg;
399   decoded->value = value;
400   decoded->errors = errors;
401   decoded->warn_message = warn_message;
402
403   if (opt_index == OPT_SPECIAL_unknown)
404     {
405       /* Skip the correct number of arguments for options handled
406          through specs.  */
407       const char *popt = argv[0] + 1;
408       int c = *popt;
409
410       gcc_assert (result == 1);
411       if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
412         result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
413       else if (WORD_SWITCH_TAKES_ARG (popt))
414         result += WORD_SWITCH_TAKES_ARG (popt);
415       if (result > 1)
416         for (i = 1; i < result; i++)
417           if (argv[i] == NULL)
418             {
419               result = i;
420               break;
421             }
422     }
423
424   gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
425   decoded->canonical_option_num_elements = result;
426   total_len = 0;
427   for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
428     {
429       if (i < result)
430         {
431           if (opt_index == OPT_SPECIAL_unknown)
432             decoded->canonical_option[i] = argv[i];
433           else
434             decoded->canonical_option[i] = NULL;
435           total_len += strlen (argv[i]) + 1;
436         }
437       else
438         decoded->canonical_option[i] = NULL;
439     }
440   if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
441     generate_canonical_option (opt_index, arg, value, decoded);
442   decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
443   for (i = 0; i < result; i++)
444     {
445       size_t len = strlen (argv[i]);
446
447       memcpy (p, argv[i], len);
448       p += len;
449       if (i == result - 1)
450         *p++ = 0;
451       else
452         *p++ = ' ';
453     }
454
455   return result;
456 }
457
458 /* Decode command-line options (ARGC and ARGV being the arguments of
459    main) into an array, setting *DECODED_OPTIONS to a pointer to that
460    array and *DECODED_OPTIONS_COUNT to the number of entries in the
461    array.  The first entry in the array is always one for the program
462    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
463    flags applicable for decoding (including CL_COMMON and CL_TARGET if
464    those options should be considered applicable).  Do not produce any
465    diagnostics or set state outside of these variables.  */
466
467 void
468 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
469                                  unsigned int lang_mask,
470                                  struct cl_decoded_option **decoded_options,
471                                  unsigned int *decoded_options_count)
472 {
473   unsigned int n, i;
474   struct cl_decoded_option *opt_array;
475   unsigned int num_decoded_options;
476
477   opt_array = XNEWVEC (struct cl_decoded_option, argc);
478
479   opt_array[0].opt_index = OPT_SPECIAL_program_name;
480   opt_array[0].warn_message = NULL;
481   opt_array[0].arg = argv[0];
482   opt_array[0].orig_option_with_args_text = argv[0];
483   opt_array[0].canonical_option_num_elements = 1;
484   opt_array[0].canonical_option[0] = argv[0];
485   opt_array[0].canonical_option[1] = NULL;
486   opt_array[0].canonical_option[2] = NULL;
487   opt_array[0].canonical_option[3] = NULL;
488   opt_array[0].value = 1;
489   opt_array[0].errors = 0;
490   num_decoded_options = 1;
491
492   for (i = 1; i < argc; i += n)
493     {
494       const char *opt = argv[i];
495
496       /* Interpret "-" or a non-switch as a file name.  */
497       if (opt[0] != '-' || opt[1] == '\0')
498         {
499           generate_option_input_file (opt, &opt_array[num_decoded_options]);
500           num_decoded_options++;
501           n = 1;
502           continue;
503         }
504
505       n = decode_cmdline_option (argv + i, lang_mask,
506                                  &opt_array[num_decoded_options]);
507       num_decoded_options++;
508     }
509
510   opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
511                           num_decoded_options);
512   *decoded_options = opt_array;
513   *decoded_options_count = num_decoded_options;
514 }
515
516 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
517    next one is the same as ORIG_NEXT_OPT_IDX.  */
518
519 static bool
520 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
521 {
522   /* An option can be canceled by the same option or an option with
523      Negative.  */
524   if (cl_options [next_opt_idx].neg_index == opt_idx)
525     return true;
526
527   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
528     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
529                           orig_next_opt_idx);
530
531   return false;
532 }
533
534 /* Filter out options canceled by the ones after them.  */
535
536 void
537 prune_options (int *argcp, char ***argvp)
538 {
539   int argc = *argcp;
540   int *options = XNEWVEC (int, argc);
541   /* We will only return this replacement argv if we remove at least
542      one argument, so it does not need to be size (argc + 1) to
543      make room for the terminating NULL because we will always have
544      freed up at least one slot when we end up using it at all.  */
545   char **argv = XNEWVEC (char *, argc);
546   int i, arg_count, need_prune = 0;
547   const struct cl_option *option;
548   size_t opt_index;
549
550   /* Scan all arguments.  */
551   for (i = 1; i < argc; i++)
552     {
553       int value = 1;
554       const char *opt = (*argvp) [i];
555
556       opt_index = find_opt (opt + 1, -1);
557       if (opt_index == OPT_SPECIAL_unknown
558           && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
559           && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
560         {
561           char *dup;
562
563           /* Drop the "no-" from negative switches.  */
564           size_t len = strlen (opt) - 3;
565
566           dup = XNEWVEC (char, len + 1);
567           dup[0] = '-';
568           dup[1] = opt[1];
569           memcpy (dup + 2, opt + 5, len - 2 + 1);
570           opt = dup;
571           value = 0;
572           opt_index = find_opt (opt + 1, -1);
573           free (dup);
574         }
575
576       if (opt_index == OPT_SPECIAL_unknown)
577         {
578 cont:
579           options [i] = 0;
580           continue;
581         }
582
583       option = &cl_options[opt_index];
584       if (option->neg_index < 0)
585         goto cont;
586
587       /* Skip joined switches.  */
588       if ((option->flags & CL_JOINED))
589         goto cont;
590
591       /* Reject negative form of switches that don't take negatives as
592          unrecognized.  */
593       if (!value && (option->flags & CL_REJECT_NEGATIVE))
594         goto cont;
595
596       options [i] = (int) opt_index;
597       need_prune |= options [i];
598     }
599
600   if (!need_prune)
601     goto done;
602
603   /* Remove arguments which are negated by others after them.  */
604   argv [0] = (*argvp) [0];
605   arg_count = 1;
606   for (i = 1; i < argc; i++)
607     {
608       int j, opt_idx;
609
610       opt_idx = options [i];
611       if (opt_idx)
612         {
613           int next_opt_idx;
614           for (j = i + 1; j < argc; j++)
615             {
616               next_opt_idx = options [j];
617               if (next_opt_idx
618                   && cancel_option (opt_idx, next_opt_idx,
619                                     next_opt_idx))
620                 break;
621             }
622         }
623       else
624         goto keep;
625
626       if (j == argc)
627         {
628 keep:
629           argv [arg_count] = (*argvp) [i];
630           arg_count++;
631         }
632     }
633
634   if (arg_count != argc)
635     {
636       *argcp = arg_count;
637       *argvp = argv;
638       /* Add NULL-termination.  Guaranteed not to overflow because
639          arg_count here can only be less than argc.  */
640       argv[arg_count] = 0;
641     }
642   else
643     {
644 done:
645       free (argv);
646     }
647
648   free (options);
649 }
650
651 /* Handle option DECODED for the language indicated by LANG_MASK,
652    using the handlers in HANDLERS.  KIND is the diagnostic_t if this
653    is a diagnostics option, DK_UNSPECIFIED otherwise.  Returns false
654    if the switch was invalid.  */
655
656 bool
657 handle_option (const struct cl_decoded_option *decoded,
658                unsigned int lang_mask, int kind,
659                const struct cl_option_handlers *handlers)
660 {
661   size_t opt_index = decoded->opt_index;
662   const char *arg = decoded->arg;
663   int value = decoded->value;
664   const struct cl_option *option = &cl_options[opt_index];
665   size_t i;
666
667   if (option->flag_var)
668     set_option (opt_index, value, arg, kind);
669
670   for (i = 0; i < handlers->num_handlers; i++)
671     if (option->flags & handlers->handlers[i].mask)
672       {
673         if (!handlers->handlers[i].handler (decoded,
674                                             lang_mask, kind, handlers))
675           return false;
676         else
677           handlers->post_handling_callback (decoded,
678                                             handlers->handlers[i].mask);
679       }
680   
681   return true;
682 }
683
684 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
685    option instead of DECODED.  This is used for callbacks when one
686    option implies another instead of an option being decoded from the
687    command line.  */
688
689 bool
690 handle_generated_option (size_t opt_index, const char *arg, int value,
691                          unsigned int lang_mask, int kind,
692                          const struct cl_option_handlers *handlers)
693 {
694   struct cl_decoded_option decoded;
695
696   generate_option (opt_index, arg, value, lang_mask, &decoded);
697   return handle_option (&decoded, lang_mask, kind, handlers);
698 }
699
700 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
701    VALUE for a front end using LANG_MASK.  This is used when the
702    compiler generates options internally.  */
703
704 void
705 generate_option (size_t opt_index, const char *arg, int value,
706                  unsigned int lang_mask, struct cl_decoded_option *decoded)
707 {
708   const struct cl_option *option = &cl_options[opt_index];
709
710   decoded->opt_index = opt_index;
711   decoded->warn_message = NULL;
712   decoded->arg = arg;
713   decoded->value = value;
714   decoded->errors = (option_ok_for_language (option, lang_mask)
715                      ? 0
716                      : CL_ERR_WRONG_LANG);
717
718   generate_canonical_option (opt_index, arg, value, decoded);
719   switch (decoded->canonical_option_num_elements)
720     {
721     case 1:
722       decoded->orig_option_with_args_text = decoded->canonical_option[0];
723       break;
724
725     case 2:
726       decoded->orig_option_with_args_text
727         = concat (decoded->canonical_option[0], " ",
728                   decoded->canonical_option[1], NULL);
729       break;
730
731     default:
732       gcc_unreachable ();
733     }
734 }
735
736 /* Fill in *DECODED with an option for input file FILE.  */
737
738 void
739 generate_option_input_file (const char *file,
740                             struct cl_decoded_option *decoded)
741 {
742   decoded->opt_index = OPT_SPECIAL_input_file;
743   decoded->warn_message = NULL;
744   decoded->arg = file;
745   decoded->orig_option_with_args_text = file;
746   decoded->canonical_option_num_elements = 1;
747   decoded->canonical_option[0] = file;
748   decoded->canonical_option[1] = NULL;
749   decoded->canonical_option[2] = NULL;
750   decoded->canonical_option[3] = NULL;
751   decoded->value = 1;
752   decoded->errors = 0;
753 }
754
755 /* Handle the switch DECODED for the language indicated by LANG_MASK,
756    using the handlers in *HANDLERS.  */
757
758 void
759 read_cmdline_option (struct cl_decoded_option *decoded,
760                      unsigned int lang_mask,
761                      const struct cl_option_handlers *handlers)
762 {
763   const struct cl_option *option;
764   const char *opt = decoded->orig_option_with_args_text;
765
766   if (decoded->warn_message)
767     warning (0, decoded->warn_message, opt);
768
769   if (decoded->opt_index == OPT_SPECIAL_unknown)
770     {
771       if (handlers->unknown_option_callback (decoded))
772         error ("unrecognized command line option %qs", decoded->arg);
773       return;
774     }
775
776   if (decoded->opt_index == OPT_SPECIAL_ignore)
777     return;
778
779   option = &cl_options[decoded->opt_index];
780
781   if (decoded->errors & CL_ERR_DISABLED)
782     {
783       error ("command line option %qs"
784              " is not supported by this configuration", opt);
785       return;
786     }
787
788   if (decoded->errors & CL_ERR_WRONG_LANG)
789     {
790       handlers->wrong_lang_callback (decoded, lang_mask);
791       return;
792     }
793
794   if (decoded->errors & CL_ERR_MISSING_ARG)
795     {
796       if (option->missing_argument_error)
797         error (option->missing_argument_error, opt);
798       else
799         error ("missing argument to %qs", opt);
800       return;
801     }
802
803   if (decoded->errors & CL_ERR_UINT_ARG)
804     {
805       error ("argument to %qs should be a non-negative integer",
806              option->opt_text);
807       return;
808     }
809
810   gcc_assert (!decoded->errors);
811
812   if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
813     error ("unrecognized command line option %qs", opt);
814 }
815
816 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
817    diagnostic kind KIND.  */
818
819 void
820 set_option (int opt_index, int value, const char *arg, int kind)
821 {
822   const struct cl_option *option = &cl_options[opt_index];
823
824   if (!option->flag_var)
825     return;
826
827   switch (option->var_type)
828     {
829     case CLVC_BOOLEAN:
830         *(int *) option->flag_var = value;
831         break;
832
833     case CLVC_EQUAL:
834         *(int *) option->flag_var = (value
835                                      ? option->var_value
836                                      : !option->var_value);
837         break;
838
839     case CLVC_BIT_CLEAR:
840     case CLVC_BIT_SET:
841         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
842           *(int *) option->flag_var |= option->var_value;
843         else
844           *(int *) option->flag_var &= ~option->var_value;
845         if (option->flag_var == &target_flags)
846           target_flags_explicit |= option->var_value;
847         break;
848
849     case CLVC_STRING:
850         *(const char **) option->flag_var = arg;
851         break;
852     }
853
854   if ((diagnostic_t) kind != DK_UNSPECIFIED)
855     diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,
856                                     UNKNOWN_LOCATION);
857 }