OSDN Git Service

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