* gcc.target/powerpc/altivec-volatile.c: Adjust expected warning.
[official-gcc.git] / gcc / opts-common.c
blobcde8ccfe29b3a43c4c109eecad965696a93decd5
1 /* Command line option handling.
2 Copyright (C) 2006, 2007, 2008, 2010 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "intl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "options.h"
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.
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".
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).
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)
54 size_t mn, mx, md, opt_len;
55 size_t match_wrong_lang;
56 int comp;
58 mn = 0;
59 mx = cl_options_count;
61 /* Find mn such this lexicographical inequality holds:
62 cl_options[mn] <= input < cl_options[mn + 1]. */
63 while (mx - mn > 1)
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);
69 if (comp < 0)
70 mx = md;
71 else
72 mn = md;
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;
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. */
84 const struct cl_option *opt = &cl_options[mn];
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)))
91 /* If language is OK, return it. */
92 if (opt->flags & lang_mask)
93 return mn;
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;
101 /* Try the next possibility. This is cl_options_count if there
102 are no more. */
103 mn = opt->back_chain;
105 while (mn != cl_options_count);
107 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
108 return match_wrong_lang;
111 /* If ARG is a non-negative integer made up solely of digits, return its
112 value, otherwise return -1. */
115 integral_argument (const char *arg)
117 const char *p = arg;
119 while (*p && ISDIGIT (*p))
120 p++;
122 if (*p == '\0')
123 return atoi (arg);
125 return -1;
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. */
132 static unsigned int
133 decode_cmdline_option (const char **argv, unsigned int lang_mask,
134 struct cl_decoded_option *decoded)
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;
144 opt = argv[0];
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] == '-')
151 /* Drop the "no-" from negative switches. */
152 size_t len = strlen (opt) - 3;
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);
163 if (opt_index == OPT_SPECIAL_unknown)
165 arg = argv[0];
166 goto done;
169 option = &cl_options[opt_index];
171 /* Reject negative form of switches that don't take negatives as
172 unrecognized. */
173 if (!value && (option->flags & CL_REJECT_NEGATIVE))
175 opt_index = OPT_SPECIAL_unknown;
176 arg = argv[0];
177 goto done;
180 /* Check to see if the option is disabled for this configuration. */
181 if (option->flags & CL_DISABLED)
182 errors |= CL_ERR_DISABLED;
184 /* Sort out any argument the switch takes. */
185 if (option->flags & CL_JOINED)
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-");
194 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
196 if (option->flags & CL_SEPARATE)
198 arg = argv[1];
199 result = 2;
200 if (arg == NULL)
201 result = 1;
203 else
204 /* Missing argument. */
205 arg = NULL;
208 else if (option->flags & CL_SEPARATE)
210 arg = argv[1];
211 result = 2;
212 if (arg == NULL)
213 result = 1;
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;
226 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
227 errors |= CL_ERR_MISSING_ARG;
229 /* If the switch takes an integer, convert it. */
230 if (arg && (option->flags & CL_UINTEGER))
232 value = integral_argument (arg);
233 if (value == -1)
234 errors |= CL_ERR_UINT_ARG;
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)
246 case 1:
247 decoded->orig_option_with_args_text = argv[0];
248 break;
249 case 2:
250 decoded->orig_option_with_args_text = concat (argv[0], " ",
251 argv[1], NULL);
252 break;
253 default:
254 gcc_unreachable ();
256 return result;
259 /* Decode command-line options (ARGC and ARGV being the arguments of
260 main) into an array, setting *DECODED_OPTIONS to a pointer to that
261 array and *DECODED_OPTIONS_COUNT to the number of entries in the
262 array. The first entry in the array is always one for the program
263 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
264 applicable for decoding. Do not produce any diagnostics or set
265 state outside of these variables. */
267 void
268 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
269 unsigned int lang_mask,
270 struct cl_decoded_option **decoded_options,
271 unsigned int *decoded_options_count)
273 unsigned int n, i;
274 struct cl_decoded_option *opt_array;
275 unsigned int num_decoded_options;
277 opt_array = XNEWVEC (struct cl_decoded_option, argc);
279 opt_array[0].opt_index = OPT_SPECIAL_program_name;
280 opt_array[0].arg = argv[0];
281 opt_array[0].orig_option_with_args_text = argv[0];
282 opt_array[0].value = 1;
283 opt_array[0].errors = 0;
284 num_decoded_options = 1;
286 for (i = 1; i < argc; i += n)
288 const char *opt = argv[i];
290 /* Interpret "-" or a non-switch as a file name. */
291 if (opt[0] != '-' || opt[1] == '\0')
293 opt_array[num_decoded_options].opt_index = OPT_SPECIAL_input_file;
294 opt_array[num_decoded_options].arg = opt;
295 opt_array[num_decoded_options].orig_option_with_args_text = opt;
296 opt_array[num_decoded_options].value = 1;
297 opt_array[num_decoded_options].errors = 0;
298 num_decoded_options++;
299 n = 1;
300 continue;
303 n = decode_cmdline_option (argv + i, lang_mask,
304 &opt_array[num_decoded_options]);
305 num_decoded_options++;
308 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
309 num_decoded_options);
310 *decoded_options = opt_array;
311 *decoded_options_count = num_decoded_options;
314 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
315 next one is the same as ORIG_NEXT_OPT_IDX. */
317 static bool
318 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
320 /* An option can be canceled by the same option or an option with
321 Negative. */
322 if (cl_options [next_opt_idx].neg_index == opt_idx)
323 return true;
325 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
326 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
327 orig_next_opt_idx);
329 return false;
332 /* Filter out options canceled by the ones after them. */
334 void
335 prune_options (int *argcp, char ***argvp)
337 int argc = *argcp;
338 int *options = XNEWVEC (int, argc);
339 /* We will only return this replacement argv if we remove at least
340 one argument, so it does not need to be size (argc + 1) to
341 make room for the terminating NULL because we will always have
342 freed up at least one slot when we end up using it at all. */
343 char **argv = XNEWVEC (char *, argc);
344 int i, arg_count, need_prune = 0;
345 const struct cl_option *option;
346 size_t opt_index;
348 /* Scan all arguments. */
349 for (i = 1; i < argc; i++)
351 int value = 1;
352 const char *opt = (*argvp) [i];
354 opt_index = find_opt (opt + 1, -1);
355 if (opt_index == OPT_SPECIAL_unknown
356 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
357 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
359 char *dup;
361 /* Drop the "no-" from negative switches. */
362 size_t len = strlen (opt) - 3;
364 dup = XNEWVEC (char, len + 1);
365 dup[0] = '-';
366 dup[1] = opt[1];
367 memcpy (dup + 2, opt + 5, len - 2 + 1);
368 opt = dup;
369 value = 0;
370 opt_index = find_opt (opt + 1, -1);
371 free (dup);
374 if (opt_index == OPT_SPECIAL_unknown)
376 cont:
377 options [i] = 0;
378 continue;
381 option = &cl_options[opt_index];
382 if (option->neg_index < 0)
383 goto cont;
385 /* Skip joined switches. */
386 if ((option->flags & CL_JOINED))
387 goto cont;
389 /* Reject negative form of switches that don't take negatives as
390 unrecognized. */
391 if (!value && (option->flags & CL_REJECT_NEGATIVE))
392 goto cont;
394 options [i] = (int) opt_index;
395 need_prune |= options [i];
398 if (!need_prune)
399 goto done;
401 /* Remove arguments which are negated by others after them. */
402 argv [0] = (*argvp) [0];
403 arg_count = 1;
404 for (i = 1; i < argc; i++)
406 int j, opt_idx;
408 opt_idx = options [i];
409 if (opt_idx)
411 int next_opt_idx;
412 for (j = i + 1; j < argc; j++)
414 next_opt_idx = options [j];
415 if (next_opt_idx
416 && cancel_option (opt_idx, next_opt_idx,
417 next_opt_idx))
418 break;
421 else
422 goto keep;
424 if (j == argc)
426 keep:
427 argv [arg_count] = (*argvp) [i];
428 arg_count++;
432 if (arg_count != argc)
434 *argcp = arg_count;
435 *argvp = argv;
436 /* Add NULL-termination. Guaranteed not to overflow because
437 arg_count here can only be less than argc. */
438 argv[arg_count] = 0;
440 else
442 done:
443 free (argv);
446 free (options);