OSDN Git Service

* coretypes.h (struct cl_option_handlers): Declare.
[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
28 /* Perform a binary search to find which option the command-line INPUT
29    matches.  Returns its index in the option array, and
30    OPT_SPECIAL_unknown on failure.
31
32    This routine is quite subtle.  A normal binary search is not good
33    enough because some options can be suffixed with an argument, and
34    multiple sub-matches can occur, e.g. input of "-pedantic" matching
35    the initial substring of "-pedantic-errors".
36
37    A more complicated example is -gstabs.  It should match "-g" with
38    an argument of "stabs".  Suppose, however, that the number and list
39    of switches are such that the binary search tests "-gen-decls"
40    before having tested "-g".  This doesn't match, and as "-gen-decls"
41    is less than "-gstabs", it will become the lower bound of the
42    binary search range, and "-g" will never be seen.  To resolve this
43    issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
44    to "-g" so that failed searches that end between "-gen-decls" and
45    the lexicographically subsequent switch know to go back and see if
46    "-g" causes a match (which it does in this example).
47
48    This search is done in such a way that the longest match for the
49    front end in question wins.  If there is no match for the current
50    front end, the longest match for a different front end is returned
51    (or N_OPTS if none) and the caller emits an error message.  */
52 size_t
53 find_opt (const char *input, int lang_mask)
54 {
55   size_t mn, mx, md, opt_len;
56   size_t match_wrong_lang;
57   int comp;
58
59   mn = 0;
60   mx = cl_options_count;
61
62   /* Find mn such this lexicographical inequality holds:
63      cl_options[mn] <= input < cl_options[mn + 1].  */
64   while (mx - mn > 1)
65     {
66       md = (mn + mx) / 2;
67       opt_len = cl_options[md].opt_len;
68       comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
69
70       if (comp < 0)
71         mx = md;
72       else
73         mn = md;
74     }
75
76   /* This is the switch that is the best match but for a different
77      front end, or OPT_SPECIAL_unknown if there is no match at all.  */
78   match_wrong_lang = OPT_SPECIAL_unknown;
79
80   /* Backtrace the chain of possible matches, returning the longest
81      one, if any, that fits best.  With current GCC switches, this
82      loop executes at most twice.  */
83   do
84     {
85       const struct cl_option *opt = &cl_options[mn];
86
87       /* Is the input either an exact match or a prefix that takes a
88          joined argument?  */
89       if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
90           && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
91         {
92           /* If language is OK, return it.  */
93           if (opt->flags & lang_mask)
94             return mn;
95
96           /* If we haven't remembered a prior match, remember this
97              one.  Any prior match is necessarily better.  */
98           if (match_wrong_lang == OPT_SPECIAL_unknown)
99             match_wrong_lang = mn;
100         }
101
102       /* Try the next possibility.  This is cl_options_count if there
103          are no more.  */
104       mn = opt->back_chain;
105     }
106   while (mn != cl_options_count);
107
108   /* Return the best wrong match, or OPT_SPECIAL_unknown if none.  */
109   return match_wrong_lang;
110 }
111
112 /* If ARG is a non-negative integer made up solely of digits, return its
113    value, otherwise return -1.  */
114
115 int
116 integral_argument (const char *arg)
117 {
118   const char *p = arg;
119
120   while (*p && ISDIGIT (*p))
121     p++;
122
123   if (*p == '\0')
124     return atoi (arg);
125
126   return -1;
127 }
128
129 /* Decode the switch beginning at ARGV for the language indicated by
130    LANG_MASK, into the structure *DECODED.  Returns the number of
131    switches consumed.  */
132
133 static unsigned int
134 decode_cmdline_option (const char **argv, unsigned int lang_mask,
135                        struct cl_decoded_option *decoded)
136 {
137   size_t opt_index;
138   const char *opt, *arg = 0;
139   char *dup = 0;
140   int value = 1;
141   unsigned int result = 1;
142   const struct cl_option *option;
143   int errors = 0;
144
145   opt = argv[0];
146
147   opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
148   if (opt_index == OPT_SPECIAL_unknown
149       && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
150       && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
151     {
152       /* Drop the "no-" from negative switches.  */
153       size_t len = strlen (opt) - 3;
154
155       dup = XNEWVEC (char, len + 1);
156       dup[0] = '-';
157       dup[1] = opt[1];
158       memcpy (dup + 2, opt + 5, len - 2 + 1);
159       opt = dup;
160       value = 0;
161       opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
162     }
163
164   if (opt_index == OPT_SPECIAL_unknown)
165     {
166       arg = argv[0];
167       goto done;
168     }
169
170   option = &cl_options[opt_index];
171
172   /* Reject negative form of switches that don't take negatives as
173      unrecognized.  */
174   if (!value && (option->flags & CL_REJECT_NEGATIVE))
175     {
176       opt_index = OPT_SPECIAL_unknown;
177       arg = argv[0];
178       goto done;
179     }
180
181   /* Check to see if the option is disabled for this configuration.  */
182   if (option->flags & CL_DISABLED)
183     errors |= CL_ERR_DISABLED;
184
185   /* Sort out any argument the switch takes.  */
186   if (option->flags & CL_JOINED)
187     {
188       /* Have arg point to the original switch.  This is because
189          some code, such as disable_builtin_function, expects its
190          argument to be persistent until the program exits.  */
191       arg = argv[0] + cl_options[opt_index].opt_len + 1;
192       if (!value)
193         arg += strlen ("no-");
194
195       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
196         {
197           if (option->flags & CL_SEPARATE)
198             {
199               arg = argv[1];
200               result = 2;
201               if (arg == NULL)
202                 result = 1;
203             }
204           else
205             /* Missing argument.  */
206             arg = NULL;
207         }
208     }
209   else if (option->flags & CL_SEPARATE)
210     {
211       arg = argv[1];
212       result = 2;
213       if (arg == NULL)
214         result = 1;
215     }
216
217   /* Check if this is a switch for a different front end.  */
218   if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
219     errors |= CL_ERR_WRONG_LANG;
220   else if ((option->flags & CL_TARGET)
221            && (option->flags & CL_LANG_ALL)
222            && !(option->flags & lang_mask))
223     /* Complain for target flag language mismatches if any languages
224        are specified.  */
225       errors |= CL_ERR_WRONG_LANG;
226
227   if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
228     errors |= CL_ERR_MISSING_ARG;
229
230   /* If the switch takes an integer, convert it.  */
231   if (arg && (option->flags & CL_UINTEGER))
232     {
233       value = integral_argument (arg);
234       if (value == -1)
235         errors |= CL_ERR_UINT_ARG;
236     }
237
238  done:
239   if (dup)
240     free (dup);
241   decoded->opt_index = opt_index;
242   decoded->arg = arg;
243   decoded->value = value;
244   decoded->errors = errors;
245   switch (result)
246     {
247     case 1:
248       decoded->orig_option_with_args_text = argv[0];
249       decoded->canonical_option[0] = argv[0];
250       decoded->canonical_option[1] = NULL;
251       break;
252     case 2:
253       decoded->orig_option_with_args_text = concat (argv[0], " ",
254                                                     argv[1], NULL);
255       decoded->canonical_option[0] = argv[0];
256       decoded->canonical_option[1] = argv[1];
257       break;
258     default:
259       gcc_unreachable ();
260     }
261   return result;
262 }
263
264 /* Decode command-line options (ARGC and ARGV being the arguments of
265    main) into an array, setting *DECODED_OPTIONS to a pointer to that
266    array and *DECODED_OPTIONS_COUNT to the number of entries in the
267    array.  The first entry in the array is always one for the program
268    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
269    applicable for decoding.  Do not produce any diagnostics or set
270    state outside of these variables.  */
271
272 void
273 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
274                                  unsigned int lang_mask,
275                                  struct cl_decoded_option **decoded_options,
276                                  unsigned int *decoded_options_count)
277 {
278   unsigned int n, i;
279   struct cl_decoded_option *opt_array;
280   unsigned int num_decoded_options;
281
282   opt_array = XNEWVEC (struct cl_decoded_option, argc);
283
284   opt_array[0].opt_index = OPT_SPECIAL_program_name;
285   opt_array[0].arg = argv[0];
286   opt_array[0].orig_option_with_args_text = argv[0];
287   opt_array[0].canonical_option[0] = argv[0];
288   opt_array[0].canonical_option[1] = NULL;
289   opt_array[0].value = 1;
290   opt_array[0].errors = 0;
291   num_decoded_options = 1;
292
293   for (i = 1; i < argc; i += n)
294     {
295       const char *opt = argv[i];
296
297       /* Interpret "-" or a non-switch as a file name.  */
298       if (opt[0] != '-' || opt[1] == '\0')
299         {
300           opt_array[num_decoded_options].opt_index = OPT_SPECIAL_input_file;
301           opt_array[num_decoded_options].arg = opt;
302           opt_array[num_decoded_options].orig_option_with_args_text = opt;
303           opt_array[num_decoded_options].canonical_option[0] = opt;
304           opt_array[num_decoded_options].canonical_option[1] = NULL;
305           opt_array[num_decoded_options].value = 1;
306           opt_array[num_decoded_options].errors = 0;
307           num_decoded_options++;
308           n = 1;
309           continue;
310         }
311
312       n = decode_cmdline_option (argv + i, lang_mask,
313                                  &opt_array[num_decoded_options]);
314       num_decoded_options++;
315     }
316
317   opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
318                           num_decoded_options);
319   *decoded_options = opt_array;
320   *decoded_options_count = num_decoded_options;
321 }
322
323 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
324    next one is the same as ORIG_NEXT_OPT_IDX.  */
325
326 static bool
327 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
328 {
329   /* An option can be canceled by the same option or an option with
330      Negative.  */
331   if (cl_options [next_opt_idx].neg_index == opt_idx)
332     return true;
333
334   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
335     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
336                           orig_next_opt_idx);
337
338   return false;
339 }
340
341 /* Filter out options canceled by the ones after them.  */
342
343 void
344 prune_options (int *argcp, char ***argvp)
345 {
346   int argc = *argcp;
347   int *options = XNEWVEC (int, argc);
348   /* We will only return this replacement argv if we remove at least
349      one argument, so it does not need to be size (argc + 1) to
350      make room for the terminating NULL because we will always have
351      freed up at least one slot when we end up using it at all.  */
352   char **argv = XNEWVEC (char *, argc);
353   int i, arg_count, need_prune = 0;
354   const struct cl_option *option;
355   size_t opt_index;
356
357   /* Scan all arguments.  */
358   for (i = 1; i < argc; i++)
359     {
360       int value = 1;
361       const char *opt = (*argvp) [i];
362
363       opt_index = find_opt (opt + 1, -1);
364       if (opt_index == OPT_SPECIAL_unknown
365           && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
366           && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
367         {
368           char *dup;
369
370           /* Drop the "no-" from negative switches.  */
371           size_t len = strlen (opt) - 3;
372
373           dup = XNEWVEC (char, len + 1);
374           dup[0] = '-';
375           dup[1] = opt[1];
376           memcpy (dup + 2, opt + 5, len - 2 + 1);
377           opt = dup;
378           value = 0;
379           opt_index = find_opt (opt + 1, -1);
380           free (dup);
381         }
382
383       if (opt_index == OPT_SPECIAL_unknown)
384         {
385 cont:
386           options [i] = 0;
387           continue;
388         }
389
390       option = &cl_options[opt_index];
391       if (option->neg_index < 0)
392         goto cont;
393
394       /* Skip joined switches.  */
395       if ((option->flags & CL_JOINED))
396         goto cont;
397
398       /* Reject negative form of switches that don't take negatives as
399          unrecognized.  */
400       if (!value && (option->flags & CL_REJECT_NEGATIVE))
401         goto cont;
402
403       options [i] = (int) opt_index;
404       need_prune |= options [i];
405     }
406
407   if (!need_prune)
408     goto done;
409
410   /* Remove arguments which are negated by others after them.  */
411   argv [0] = (*argvp) [0];
412   arg_count = 1;
413   for (i = 1; i < argc; i++)
414     {
415       int j, opt_idx;
416
417       opt_idx = options [i];
418       if (opt_idx)
419         {
420           int next_opt_idx;
421           for (j = i + 1; j < argc; j++)
422             {
423               next_opt_idx = options [j];
424               if (next_opt_idx
425                   && cancel_option (opt_idx, next_opt_idx,
426                                     next_opt_idx))
427                 break;
428             }
429         }
430       else
431         goto keep;
432
433       if (j == argc)
434         {
435 keep:
436           argv [arg_count] = (*argvp) [i];
437           arg_count++;
438         }
439     }
440
441   if (arg_count != argc)
442     {
443       *argcp = arg_count;
444       *argvp = argv;
445       /* Add NULL-termination.  Guaranteed not to overflow because
446          arg_count here can only be less than argc.  */
447       argv[arg_count] = 0;
448     }
449   else
450     {
451 done:
452       free (argv);
453     }
454
455   free (options);
456 }
457
458 /* Handle option OPT_INDEX, and argument ARG, for the language
459    indicated by LANG_MASK, using the handlers in HANDLERS.  VALUE is
460    the option value as for the value field of cl_decoded_option.  KIND
461    is the diagnostic_t if this is a diagnostics option, DK_UNSPECIFIED
462    otherwise.  Returns false if the switch was invalid.  */
463
464 bool
465 handle_option (size_t opt_index, const char *arg, int value,
466                unsigned int lang_mask, int kind,
467                const struct cl_option_handlers *handlers)
468 {
469   const struct cl_option *option = &cl_options[opt_index];
470   size_t i;
471
472   if (option->flag_var)
473     set_option (opt_index, value, arg, kind);
474
475   for (i = 0; i < handlers->num_handlers; i++)
476     if (option->flags & handlers->handlers[i].mask)
477       {
478         if (!handlers->handlers[i].handler (opt_index, arg, value,
479                                             lang_mask, kind, handlers))
480           return false;
481         else
482           handlers->post_handling_callback (opt_index, arg, value,
483                                             handlers->handlers[i].mask);
484       }
485   
486   return true;
487 }
488
489 /* Handle the switch DECODED for the language indicated by LANG_MASK,
490    using the handlers in *HANDLERS.  */
491
492 void
493 read_cmdline_option (struct cl_decoded_option *decoded,
494                      unsigned int lang_mask,
495                      const struct cl_option_handlers *handlers)
496 {
497   const struct cl_option *option;
498   const char *opt;
499
500   if (decoded->opt_index == OPT_SPECIAL_unknown)
501     {
502       opt = decoded->arg;
503
504       if (handlers->unknown_option_callback (opt))
505         error ("unrecognized command line option %qs", opt);
506       return;
507     }
508
509   option = &cl_options[decoded->opt_index];
510   opt = decoded->orig_option_with_args_text;
511
512   if (decoded->errors & CL_ERR_DISABLED)
513     {
514       error ("command line option %qs"
515              " is not supported by this configuration", opt);
516       return;
517     }
518
519   if (decoded->errors & CL_ERR_WRONG_LANG)
520     {
521       handlers->wrong_lang_callback (opt, option, lang_mask);
522       return;
523     }
524
525   if (decoded->errors & CL_ERR_MISSING_ARG)
526     {
527       if (option->missing_argument_error)
528         error (option->missing_argument_error, opt);
529       else
530         error ("missing argument to %qs", opt);
531       return;
532     }
533
534   if (decoded->errors & CL_ERR_UINT_ARG)
535     {
536       error ("argument to %qs should be a non-negative integer",
537              option->opt_text);
538       return;
539     }
540
541   gcc_assert (!decoded->errors);
542
543   if (!handle_option (decoded->opt_index, decoded->arg, decoded->value,
544                       lang_mask, DK_UNSPECIFIED, handlers))
545     error ("unrecognized command line option %qs", opt);
546 }
547
548 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
549    diagnostic kind KIND.  */
550
551 void
552 set_option (int opt_index, int value, const char *arg, int kind)
553 {
554   const struct cl_option *option = &cl_options[opt_index];
555
556   if (!option->flag_var)
557     return;
558
559   switch (option->var_type)
560     {
561     case CLVC_BOOLEAN:
562         *(int *) option->flag_var = value;
563         break;
564
565     case CLVC_EQUAL:
566         *(int *) option->flag_var = (value
567                                      ? option->var_value
568                                      : !option->var_value);
569         break;
570
571     case CLVC_BIT_CLEAR:
572     case CLVC_BIT_SET:
573         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
574           *(int *) option->flag_var |= option->var_value;
575         else
576           *(int *) option->flag_var &= ~option->var_value;
577         if (option->flag_var == &target_flags)
578           target_flags_explicit |= option->var_value;
579         break;
580
581     case CLVC_STRING:
582         *(const char **) option->flag_var = arg;
583         break;
584     }
585
586   if ((diagnostic_t) kind != DK_UNSPECIFIED)
587     diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,
588                                     UNKNOWN_LOCATION);
589 }