OSDN Git Service

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