OSDN Git Service

* opts-common.c: Include options.h.
[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 N_OPTS
29    (cl_options_count) 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 cl_options_count if there is no match at all.  */
77   match_wrong_lang = cl_options_count;
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 == cl_options_count)
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 cl_options_count 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 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 == cl_options_count
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 == cl_options_count)
164     goto done;
165
166   option = &cl_options[opt_index];
167
168   /* Reject negative form of switches that don't take negatives as
169      unrecognized.  */
170   if (!value && (option->flags & CL_REJECT_NEGATIVE))
171     {
172       opt_index = cl_options_count;
173       goto done;
174     }
175
176   /* Check to see if the option is disabled for this configuration.  */
177   if (option->flags & CL_DISABLED)
178     errors |= CL_ERR_DISABLED;
179
180   /* Sort out any argument the switch takes.  */
181   if (option->flags & CL_JOINED)
182     {
183       /* Have arg point to the original switch.  This is because
184          some code, such as disable_builtin_function, expects its
185          argument to be persistent until the program exits.  */
186       arg = argv[0] + cl_options[opt_index].opt_len + 1;
187       if (!value)
188         arg += strlen ("no-");
189
190       if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
191         {
192           if (option->flags & CL_SEPARATE)
193             {
194               arg = argv[1];
195               result = 2;
196               if (arg == NULL)
197                 result = 1;
198             }
199           else
200             /* Missing argument.  */
201             arg = NULL;
202         }
203     }
204   else if (option->flags & CL_SEPARATE)
205     {
206       arg = argv[1];
207       result = 2;
208       if (arg == NULL)
209         result = 1;
210     }
211
212   /* Check if this is a switch for a different front end.  */
213   if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
214     errors |= CL_ERR_WRONG_LANG;
215   else if ((option->flags & CL_TARGET)
216            && (option->flags & CL_LANG_ALL)
217            && !(option->flags & lang_mask))
218     /* Complain for target flag language mismatches if any languages
219        are specified.  */
220       errors |= CL_ERR_WRONG_LANG;
221
222   if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
223     errors |= CL_ERR_MISSING_ARG;
224
225   /* If the switch takes an integer, convert it.  */
226   if (arg && (option->flags & CL_UINTEGER))
227     {
228       value = integral_argument (arg);
229       if (value == -1)
230         errors |= CL_ERR_UINT_ARG;
231     }
232
233  done:
234   if (dup)
235     free (dup);
236   decoded->opt_index = opt_index;
237   decoded->arg = arg;
238   decoded->value = value;
239   decoded->errors = errors;
240   return result;
241 }
242
243 /* Return true if NEXT_OPT_IDX cancels OPT_IDX.  Return false if the
244    next one is the same as ORIG_NEXT_OPT_IDX.  */
245
246 static bool
247 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
248 {
249   /* An option can be canceled by the same option or an option with
250      Negative.  */
251   if (cl_options [next_opt_idx].neg_index == opt_idx)
252     return true;
253
254   if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
255     return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
256                           orig_next_opt_idx);
257
258   return false;
259 }
260
261 /* Filter out options canceled by the ones after them.  */
262
263 void
264 prune_options (int *argcp, char ***argvp)
265 {
266   int argc = *argcp;
267   int *options = XNEWVEC (int, argc);
268   /* We will only return this replacement argv if we remove at least
269      one argument, so it does not need to be size (argc + 1) to
270      make room for the terminating NULL because we will always have
271      freed up at least one slot when we end up using it at all.  */
272   char **argv = XNEWVEC (char *, argc);
273   int i, arg_count, need_prune = 0;
274   const struct cl_option *option;
275   size_t opt_index;
276
277   /* Scan all arguments.  */
278   for (i = 1; i < argc; i++)
279     {
280       int value = 1;
281       const char *opt = (*argvp) [i];
282
283       opt_index = find_opt (opt + 1, -1);
284       if (opt_index == cl_options_count
285           && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
286           && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
287         {
288           char *dup;
289
290           /* Drop the "no-" from negative switches.  */
291           size_t len = strlen (opt) - 3;
292
293           dup = XNEWVEC (char, len + 1);
294           dup[0] = '-';
295           dup[1] = opt[1];
296           memcpy (dup + 2, opt + 5, len - 2 + 1);
297           opt = dup;
298           value = 0;
299           opt_index = find_opt (opt + 1, -1);
300           free (dup);
301         }
302
303       if (opt_index == cl_options_count)
304         {
305 cont:
306           options [i] = 0;
307           continue;
308         }
309
310       option = &cl_options[opt_index];
311       if (option->neg_index < 0)
312         goto cont;
313
314       /* Skip joined switches.  */
315       if ((option->flags & CL_JOINED))
316         goto cont;
317
318       /* Reject negative form of switches that don't take negatives as
319          unrecognized.  */
320       if (!value && (option->flags & CL_REJECT_NEGATIVE))
321         goto cont;
322
323       options [i] = (int) opt_index;
324       need_prune |= options [i];
325     }
326
327   if (!need_prune)
328     goto done;
329
330   /* Remove arguments which are negated by others after them.  */
331   argv [0] = (*argvp) [0];
332   arg_count = 1;
333   for (i = 1; i < argc; i++)
334     {
335       int j, opt_idx;
336
337       opt_idx = options [i];
338       if (opt_idx)
339         {
340           int next_opt_idx;
341           for (j = i + 1; j < argc; j++)
342             {
343               next_opt_idx = options [j];
344               if (next_opt_idx
345                   && cancel_option (opt_idx, next_opt_idx,
346                                     next_opt_idx))
347                 break;
348             }
349         }
350       else
351         goto keep;
352
353       if (j == argc)
354         {
355 keep:
356           argv [arg_count] = (*argvp) [i];
357           arg_count++;
358         }
359     }
360
361   if (arg_count != argc)
362     {
363       *argcp = arg_count;
364       *argvp = argv;
365       /* Add NULL-termination.  Guaranteed not to overflow because
366          arg_count here can only be less than argc.  */
367       argv[arg_count] = 0;
368     }
369   else
370     {
371 done:
372       free (argv);
373     }
374
375   free (options);
376 }