OSDN Git Service

* opts.h (CL_ERR_NEGATIVE): Define.
[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 /* Decode the switch beginning at ARGV for the language indicated by
148    LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
149    the structure *DECODED.  Returns the number of switches
150    consumed.  */
151
152 static unsigned int
153 decode_cmdline_option (const char **argv, unsigned int lang_mask,
154                        struct cl_decoded_option *decoded)
155 {
156   size_t opt_index;
157   const char *opt, *arg = 0;
158   char *dup = 0;
159   int value = 1;
160   unsigned int result = 1, i;
161   size_t total_len;
162   char *p;
163   const struct cl_option *option;
164   int errors = 0;
165   bool separate_arg_flag;
166   bool joined_arg_flag;
167
168   opt = argv[0];
169
170   opt_index = find_opt (opt + 1, lang_mask);
171   if (opt_index == OPT_SPECIAL_unknown
172       && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
173       && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
174     {
175       /* Drop the "no-" from negative switches.  */
176       size_t len = strlen (opt) - 3;
177
178       dup = XNEWVEC (char, len + 1);
179       dup[0] = '-';
180       dup[1] = opt[1];
181       memcpy (dup + 2, opt + 5, len - 2 + 1);
182       opt = dup;
183       value = 0;
184       opt_index = find_opt (opt + 1, lang_mask);
185     }
186
187   if (opt_index == OPT_SPECIAL_unknown)
188     {
189       arg = argv[0];
190       goto done;
191     }
192
193   option = &cl_options[opt_index];
194
195   /* Reject negative form of switches that don't take negatives as
196      unrecognized.  */
197   if (!value && (option->flags & CL_REJECT_NEGATIVE))
198     {
199       opt_index = OPT_SPECIAL_unknown;
200       errors |= CL_ERR_NEGATIVE;
201       arg = argv[0];
202       goto done;
203     }
204
205   /* Check to see if the option is disabled for this configuration.  */
206   if (option->flags & CL_DISABLED)
207     errors |= CL_ERR_DISABLED;
208
209   /* Determine whether there may be a separate argument based on
210      whether this option is being processed for the driver.  */
211   separate_arg_flag = ((option->flags & CL_SEPARATE)
212                        && !((option->flags & CL_NO_DRIVER_ARG)
213                             && (lang_mask & CL_DRIVER)));
214   joined_arg_flag = (option->flags & CL_JOINED) != 0;
215
216   /* Sort out any argument the switch takes.  */
217   if (joined_arg_flag)
218     {
219       /* Have arg point to the original switch.  This is because
220          some code, such as disable_builtin_function, expects its
221          argument to be persistent until the program exits.  */
222       arg = argv[0] + cl_options[opt_index].opt_len + 1;
223       if (!value)
224         arg += strlen ("no-");
225
226       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
227         {
228           if (separate_arg_flag)
229             {
230               arg = argv[1];
231               result = 2;
232               if (arg == NULL)
233                 result = 1;
234             }
235           else
236             /* Missing argument.  */
237             arg = NULL;
238         }
239     }
240   else if (separate_arg_flag)
241     {
242       arg = argv[1];
243       result = 2;
244       if (arg == NULL)
245         result = 1;
246     }
247
248   /* Check if this is a switch for a different front end.  */
249   if (!option_ok_for_language (option, lang_mask))
250     errors |= CL_ERR_WRONG_LANG;
251
252   if (arg == NULL && (separate_arg_flag || joined_arg_flag))
253     errors |= CL_ERR_MISSING_ARG;
254
255   /* If the switch takes an integer, convert it.  */
256   if (arg && (option->flags & CL_UINTEGER))
257     {
258       value = integral_argument (arg);
259       if (value == -1)
260         errors |= CL_ERR_UINT_ARG;
261     }
262
263  done:
264   if (dup)
265     free (dup);
266   decoded->opt_index = opt_index;
267   decoded->arg = arg;
268   decoded->value = value;
269   decoded->errors = errors;
270
271   if (opt_index == OPT_SPECIAL_unknown)
272     {
273       /* Skip the correct number of arguments for options handled
274          through specs.  */
275       const char *popt = argv[0] + 1;
276       int c = *popt;
277
278       gcc_assert (result == 1);
279       if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
280         result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
281       else if (WORD_SWITCH_TAKES_ARG (popt))
282         result += WORD_SWITCH_TAKES_ARG (popt);
283       if (result > 1)
284         for (i = 1; i < result; i++)
285           if (argv[i] == NULL)
286             {
287               result = i;
288               break;
289             }
290     }
291
292   gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
293   decoded->canonical_option_num_elements = result;
294   total_len = 0;
295   for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
296     {
297       if (i < result)
298         {
299           decoded->canonical_option[i] = argv[i];
300           total_len += strlen (argv[i]) + 1;
301         }
302       else
303         decoded->canonical_option[i] = NULL;
304     }
305   decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
306   for (i = 0; i < result; i++)
307     {
308       size_t len = strlen (argv[i]);
309
310       memcpy (p, argv[i], len);
311       p += len;
312       if (i == result - 1)
313         *p++ = 0;
314       else
315         *p++ = ' ';
316     }
317
318   return result;
319 }
320
321 /* Decode command-line options (ARGC and ARGV being the arguments of
322    main) into an array, setting *DECODED_OPTIONS to a pointer to that
323    array and *DECODED_OPTIONS_COUNT to the number of entries in the
324    array.  The first entry in the array is always one for the program
325    name (OPT_SPECIAL_program_name).  LANG_MASK indicates the language
326    flags applicable for decoding (including CL_COMMON and CL_TARGET if
327    those options should be considered applicable).  Do not produce any
328    diagnostics or set state outside of these variables.  */
329
330 void
331 decode_cmdline_options_to_array (unsigned int argc, const char **argv, 
332                                  unsigned int lang_mask,
333                                  struct cl_decoded_option **decoded_options,
334                                  unsigned int *decoded_options_count)
335 {
336   unsigned int n, i;
337   struct cl_decoded_option *opt_array;
338   unsigned int num_decoded_options;
339
340   opt_array = XNEWVEC (struct cl_decoded_option, argc);
341
342   opt_array[0].opt_index = OPT_SPECIAL_program_name;
343   opt_array[0].arg = argv[0];
344   opt_array[0].orig_option_with_args_text = argv[0];
345   opt_array[0].canonical_option_num_elements = 1;
346   opt_array[0].canonical_option[0] = argv[0];
347   opt_array[0].canonical_option[1] = NULL;
348   opt_array[0].canonical_option[2] = NULL;
349   opt_array[0].canonical_option[3] = NULL;
350   opt_array[0].value = 1;
351   opt_array[0].errors = 0;
352   num_decoded_options = 1;
353
354   for (i = 1; i < argc; i += n)
355     {
356       const char *opt = argv[i];
357
358       /* Interpret "-" or a non-switch as a file name.  */
359       if (opt[0] != '-' || opt[1] == '\0')
360         {
361           generate_option_input_file (opt, &opt_array[num_decoded_options]);
362           num_decoded_options++;
363           n = 1;
364           continue;
365         }
366
367       n = decode_cmdline_option (argv + i, lang_mask,
368                                  &opt_array[num_decoded_options]);
369       num_decoded_options++;
370     }
371
372   opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
373                           num_decoded_options);
374   *decoded_options = opt_array;
375   *decoded_options_count = num_decoded_options;
376 }
377
378 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
379    next one is the same as ORIG_NEXT_OPT_IDX.  */
380
381 static bool
382 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
383 {
384   /* An option can be canceled by the same option or an option with
385      Negative.  */
386   if (cl_options [next_opt_idx].neg_index == opt_idx)
387     return true;
388
389   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
390     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
391                           orig_next_opt_idx);
392
393   return false;
394 }
395
396 /* Filter out options canceled by the ones after them.  */
397
398 void
399 prune_options (int *argcp, char ***argvp)
400 {
401   int argc = *argcp;
402   int *options = XNEWVEC (int, argc);
403   /* We will only return this replacement argv if we remove at least
404      one argument, so it does not need to be size (argc + 1) to
405      make room for the terminating NULL because we will always have
406      freed up at least one slot when we end up using it at all.  */
407   char **argv = XNEWVEC (char *, argc);
408   int i, arg_count, need_prune = 0;
409   const struct cl_option *option;
410   size_t opt_index;
411
412   /* Scan all arguments.  */
413   for (i = 1; i < argc; i++)
414     {
415       int value = 1;
416       const char *opt = (*argvp) [i];
417
418       opt_index = find_opt (opt + 1, -1);
419       if (opt_index == OPT_SPECIAL_unknown
420           && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
421           && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
422         {
423           char *dup;
424
425           /* Drop the "no-" from negative switches.  */
426           size_t len = strlen (opt) - 3;
427
428           dup = XNEWVEC (char, len + 1);
429           dup[0] = '-';
430           dup[1] = opt[1];
431           memcpy (dup + 2, opt + 5, len - 2 + 1);
432           opt = dup;
433           value = 0;
434           opt_index = find_opt (opt + 1, -1);
435           free (dup);
436         }
437
438       if (opt_index == OPT_SPECIAL_unknown)
439         {
440 cont:
441           options [i] = 0;
442           continue;
443         }
444
445       option = &cl_options[opt_index];
446       if (option->neg_index < 0)
447         goto cont;
448
449       /* Skip joined switches.  */
450       if ((option->flags & CL_JOINED))
451         goto cont;
452
453       /* Reject negative form of switches that don't take negatives as
454          unrecognized.  */
455       if (!value && (option->flags & CL_REJECT_NEGATIVE))
456         goto cont;
457
458       options [i] = (int) opt_index;
459       need_prune |= options [i];
460     }
461
462   if (!need_prune)
463     goto done;
464
465   /* Remove arguments which are negated by others after them.  */
466   argv [0] = (*argvp) [0];
467   arg_count = 1;
468   for (i = 1; i < argc; i++)
469     {
470       int j, opt_idx;
471
472       opt_idx = options [i];
473       if (opt_idx)
474         {
475           int next_opt_idx;
476           for (j = i + 1; j < argc; j++)
477             {
478               next_opt_idx = options [j];
479               if (next_opt_idx
480                   && cancel_option (opt_idx, next_opt_idx,
481                                     next_opt_idx))
482                 break;
483             }
484         }
485       else
486         goto keep;
487
488       if (j == argc)
489         {
490 keep:
491           argv [arg_count] = (*argvp) [i];
492           arg_count++;
493         }
494     }
495
496   if (arg_count != argc)
497     {
498       *argcp = arg_count;
499       *argvp = argv;
500       /* Add NULL-termination.  Guaranteed not to overflow because
501          arg_count here can only be less than argc.  */
502       argv[arg_count] = 0;
503     }
504   else
505     {
506 done:
507       free (argv);
508     }
509
510   free (options);
511 }
512
513 /* Handle option DECODED for the language indicated by LANG_MASK,
514    using the handlers in HANDLERS.  KIND is the diagnostic_t if this
515    is a diagnostics option, DK_UNSPECIFIED otherwise.  Returns false
516    if the switch was invalid.  */
517
518 bool
519 handle_option (const struct cl_decoded_option *decoded,
520                unsigned int lang_mask, int kind,
521                const struct cl_option_handlers *handlers)
522 {
523   size_t opt_index = decoded->opt_index;
524   const char *arg = decoded->arg;
525   int value = decoded->value;
526   const struct cl_option *option = &cl_options[opt_index];
527   size_t i;
528
529   if (option->flag_var)
530     set_option (opt_index, value, arg, kind);
531
532   for (i = 0; i < handlers->num_handlers; i++)
533     if (option->flags & handlers->handlers[i].mask)
534       {
535         if (!handlers->handlers[i].handler (decoded,
536                                             lang_mask, kind, handlers))
537           return false;
538         else
539           handlers->post_handling_callback (decoded,
540                                             handlers->handlers[i].mask);
541       }
542   
543   return true;
544 }
545
546 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
547    option instead of DECODED.  This is used for callbacks when one
548    option implies another instead of an option being decoded from the
549    command line.  */
550
551 bool
552 handle_generated_option (size_t opt_index, const char *arg, int value,
553                          unsigned int lang_mask, int kind,
554                          const struct cl_option_handlers *handlers)
555 {
556   struct cl_decoded_option decoded;
557
558   generate_option (opt_index, arg, value, lang_mask, &decoded);
559   return handle_option (&decoded, lang_mask, kind, handlers);
560 }
561
562 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
563    VALUE for a front end using LANG_MASK.  This is used when the
564    compiler generates options internally.  */
565
566 void
567 generate_option (size_t opt_index, const char *arg, int value,
568                  unsigned int lang_mask, struct cl_decoded_option *decoded)
569 {
570   const struct cl_option *option = &cl_options[opt_index];
571
572   decoded->opt_index = opt_index;
573   decoded->arg = arg;
574   decoded->canonical_option[2] = NULL;
575   decoded->canonical_option[3] = NULL;
576   decoded->value = value;
577   decoded->errors = (option_ok_for_language (option, lang_mask)
578                      ? 0
579                      : CL_ERR_WRONG_LANG);
580
581   if (arg)
582     {
583       if (option->flags & CL_SEPARATE)
584         {
585           decoded->orig_option_with_args_text = concat (option->opt_text, " ",
586                                                         arg, NULL);
587           decoded->canonical_option[0] = option->opt_text;
588           decoded->canonical_option[1] = arg;
589           decoded->canonical_option_num_elements = 2;
590         }
591       else
592         {
593           gcc_assert (option->flags & CL_JOINED);
594           decoded->orig_option_with_args_text = concat (option->opt_text, arg,
595                                                         NULL);
596           decoded->canonical_option[0] = decoded->orig_option_with_args_text;
597           decoded->canonical_option[1] = NULL;
598           decoded->canonical_option_num_elements = 1;
599         }
600     }
601   else
602     {
603       decoded->orig_option_with_args_text = option->opt_text;
604       decoded->canonical_option[0] = option->opt_text;
605       decoded->canonical_option[1] = NULL;
606       decoded->canonical_option_num_elements = 1;
607     }
608 }
609
610 /* Fill in *DECODED with an option for input file FILE.  */
611
612 void
613 generate_option_input_file (const char *file,
614                             struct cl_decoded_option *decoded)
615 {
616   decoded->opt_index = OPT_SPECIAL_input_file;
617   decoded->arg = file;
618   decoded->orig_option_with_args_text = file;
619   decoded->canonical_option_num_elements = 1;
620   decoded->canonical_option[0] = file;
621   decoded->canonical_option[1] = NULL;
622   decoded->canonical_option[2] = NULL;
623   decoded->canonical_option[3] = NULL;
624   decoded->value = 1;
625   decoded->errors = 0;
626 }
627
628 /* Handle the switch DECODED for the language indicated by LANG_MASK,
629    using the handlers in *HANDLERS.  */
630
631 void
632 read_cmdline_option (struct cl_decoded_option *decoded,
633                      unsigned int lang_mask,
634                      const struct cl_option_handlers *handlers)
635 {
636   const struct cl_option *option;
637   const char *opt;
638
639   if (decoded->opt_index == OPT_SPECIAL_unknown)
640     {
641       if (handlers->unknown_option_callback (decoded))
642         error ("unrecognized command line option %qs", decoded->arg);
643       return;
644     }
645
646   option = &cl_options[decoded->opt_index];
647   opt = decoded->orig_option_with_args_text;
648
649   if (decoded->errors & CL_ERR_DISABLED)
650     {
651       error ("command line option %qs"
652              " is not supported by this configuration", opt);
653       return;
654     }
655
656   if (decoded->errors & CL_ERR_WRONG_LANG)
657     {
658       handlers->wrong_lang_callback (decoded, lang_mask);
659       return;
660     }
661
662   if (decoded->errors & CL_ERR_MISSING_ARG)
663     {
664       if (option->missing_argument_error)
665         error (option->missing_argument_error, opt);
666       else
667         error ("missing argument to %qs", opt);
668       return;
669     }
670
671   if (decoded->errors & CL_ERR_UINT_ARG)
672     {
673       error ("argument to %qs should be a non-negative integer",
674              option->opt_text);
675       return;
676     }
677
678   gcc_assert (!decoded->errors);
679
680   if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
681     error ("unrecognized command line option %qs", opt);
682 }
683
684 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
685    diagnostic kind KIND.  */
686
687 void
688 set_option (int opt_index, int value, const char *arg, int kind)
689 {
690   const struct cl_option *option = &cl_options[opt_index];
691
692   if (!option->flag_var)
693     return;
694
695   switch (option->var_type)
696     {
697     case CLVC_BOOLEAN:
698         *(int *) option->flag_var = value;
699         break;
700
701     case CLVC_EQUAL:
702         *(int *) option->flag_var = (value
703                                      ? option->var_value
704                                      : !option->var_value);
705         break;
706
707     case CLVC_BIT_CLEAR:
708     case CLVC_BIT_SET:
709         if ((value != 0) == (option->var_type == CLVC_BIT_SET))
710           *(int *) option->flag_var |= option->var_value;
711         else
712           *(int *) option->flag_var &= ~option->var_value;
713         if (option->flag_var == &target_flags)
714           target_flags_explicit |= option->var_value;
715         break;
716
717     case CLVC_STRING:
718         *(const char **) option->flag_var = arg;
719         break;
720     }
721
722   if ((diagnostic_t) kind != DK_UNSPECIFIED)
723     diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,
724                                     UNKNOWN_LOCATION);
725 }