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
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
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/>. */
23 #include "coretypes.h"
26 #include "diagnostic.h"
28 /* Perform a binary search to find which option the command-line INPUT
29 matches. Returns its index in the option array, and
30 OPT_SPECIAL_unknown on failure.
32 This routine is quite subtle. A normal binary search is not good
33 enough because some options can be suffixed with an argument, and
34 multiple sub-matches can occur, e.g. input of "-pedantic" matching
35 the initial substring of "-pedantic-errors".
37 A more complicated example is -gstabs. It should match "-g" with
38 an argument of "stabs". Suppose, however, that the number and list
39 of switches are such that the binary search tests "-gen-decls"
40 before having tested "-g". This doesn't match, and as "-gen-decls"
41 is less than "-gstabs", it will become the lower bound of the
42 binary search range, and "-g" will never be seen. To resolve this
43 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
44 to "-g" so that failed searches that end between "-gen-decls" and
45 the lexicographically subsequent switch know to go back and see if
46 "-g" causes a match (which it does in this example).
48 This search is done in such a way that the longest match for the
49 front end in question wins. If there is no match for the current
50 front end, the longest match for a different front end is returned
51 (or N_OPTS if none) and the caller emits an error message. */
53 find_opt (const char *input, int lang_mask)
55 size_t mn, mx, md, opt_len;
56 size_t match_wrong_lang;
60 mx = cl_options_count;
62 /* Find mn such this lexicographical inequality holds:
63 cl_options[mn] <= input < cl_options[mn + 1]. */
67 opt_len = cl_options[md].opt_len;
68 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
76 /* This is the switch that is the best match but for a different
77 front end, or OPT_SPECIAL_unknown if there is no match at all. */
78 match_wrong_lang = OPT_SPECIAL_unknown;
80 /* Backtrace the chain of possible matches, returning the longest
81 one, if any, that fits best. With current GCC switches, this
82 loop executes at most twice. */
85 const struct cl_option *opt = &cl_options[mn];
87 /* Is the input either an exact match or a prefix that takes a
89 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
90 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
92 /* If language is OK, return it. */
93 if (opt->flags & lang_mask)
96 /* If we haven't remembered a prior match, remember this
97 one. Any prior match is necessarily better. */
98 if (match_wrong_lang == OPT_SPECIAL_unknown)
99 match_wrong_lang = mn;
102 /* Try the next possibility. This is cl_options_count if there
104 mn = opt->back_chain;
106 while (mn != cl_options_count);
108 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
109 return match_wrong_lang;
112 /* If ARG is a non-negative integer made up solely of digits, return its
113 value, otherwise return -1. */
116 integral_argument (const char *arg)
120 while (*p && ISDIGIT (*p))
129 /* Decode the switch beginning at ARGV for the language indicated by
130 LANG_MASK, into the structure *DECODED. Returns the number of
131 switches consumed. */
134 decode_cmdline_option (const char **argv, unsigned int lang_mask,
135 struct cl_decoded_option *decoded)
138 const char *opt, *arg = 0;
141 unsigned int result = 1;
142 const struct cl_option *option;
147 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
148 if (opt_index == OPT_SPECIAL_unknown
149 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
150 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
152 /* Drop the "no-" from negative switches. */
153 size_t len = strlen (opt) - 3;
155 dup = XNEWVEC (char, len + 1);
158 memcpy (dup + 2, opt + 5, len - 2 + 1);
161 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
164 if (opt_index == OPT_SPECIAL_unknown)
170 option = &cl_options[opt_index];
172 /* Reject negative form of switches that don't take negatives as
174 if (!value && (option->flags & CL_REJECT_NEGATIVE))
176 opt_index = OPT_SPECIAL_unknown;
181 /* Check to see if the option is disabled for this configuration. */
182 if (option->flags & CL_DISABLED)
183 errors |= CL_ERR_DISABLED;
185 /* Sort out any argument the switch takes. */
186 if (option->flags & CL_JOINED)
188 /* Have arg point to the original switch. This is because
189 some code, such as disable_builtin_function, expects its
190 argument to be persistent until the program exits. */
191 arg = argv[0] + cl_options[opt_index].opt_len + 1;
193 arg += strlen ("no-");
195 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
197 if (option->flags & CL_SEPARATE)
205 /* Missing argument. */
209 else if (option->flags & CL_SEPARATE)
217 /* Check if this is a switch for a different front end. */
218 if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
219 errors |= CL_ERR_WRONG_LANG;
220 else if ((option->flags & CL_TARGET)
221 && (option->flags & CL_LANG_ALL)
222 && !(option->flags & lang_mask))
223 /* Complain for target flag language mismatches if any languages
225 errors |= CL_ERR_WRONG_LANG;
227 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
228 errors |= CL_ERR_MISSING_ARG;
230 /* If the switch takes an integer, convert it. */
231 if (arg && (option->flags & CL_UINTEGER))
233 value = integral_argument (arg);
235 errors |= CL_ERR_UINT_ARG;
241 decoded->opt_index = opt_index;
243 decoded->value = value;
244 decoded->errors = errors;
248 decoded->orig_option_with_args_text = argv[0];
249 decoded->canonical_option[0] = argv[0];
250 decoded->canonical_option[1] = NULL;
253 decoded->orig_option_with_args_text = concat (argv[0], " ",
255 decoded->canonical_option[0] = argv[0];
256 decoded->canonical_option[1] = argv[1];
264 /* Decode command-line options (ARGC and ARGV being the arguments of
265 main) into an array, setting *DECODED_OPTIONS to a pointer to that
266 array and *DECODED_OPTIONS_COUNT to the number of entries in the
267 array. The first entry in the array is always one for the program
268 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
269 applicable for decoding. Do not produce any diagnostics or set
270 state outside of these variables. */
273 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
274 unsigned int lang_mask,
275 struct cl_decoded_option **decoded_options,
276 unsigned int *decoded_options_count)
279 struct cl_decoded_option *opt_array;
280 unsigned int num_decoded_options;
282 opt_array = XNEWVEC (struct cl_decoded_option, argc);
284 opt_array[0].opt_index = OPT_SPECIAL_program_name;
285 opt_array[0].arg = argv[0];
286 opt_array[0].orig_option_with_args_text = argv[0];
287 opt_array[0].canonical_option[0] = argv[0];
288 opt_array[0].canonical_option[1] = NULL;
289 opt_array[0].value = 1;
290 opt_array[0].errors = 0;
291 num_decoded_options = 1;
293 for (i = 1; i < argc; i += n)
295 const char *opt = argv[i];
297 /* Interpret "-" or a non-switch as a file name. */
298 if (opt[0] != '-' || opt[1] == '\0')
300 opt_array[num_decoded_options].opt_index = OPT_SPECIAL_input_file;
301 opt_array[num_decoded_options].arg = opt;
302 opt_array[num_decoded_options].orig_option_with_args_text = opt;
303 opt_array[num_decoded_options].canonical_option[0] = opt;
304 opt_array[num_decoded_options].canonical_option[1] = NULL;
305 opt_array[num_decoded_options].value = 1;
306 opt_array[num_decoded_options].errors = 0;
307 num_decoded_options++;
312 n = decode_cmdline_option (argv + i, lang_mask,
313 &opt_array[num_decoded_options]);
314 num_decoded_options++;
317 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
318 num_decoded_options);
319 *decoded_options = opt_array;
320 *decoded_options_count = num_decoded_options;
323 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
324 next one is the same as ORIG_NEXT_OPT_IDX. */
327 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
329 /* An option can be canceled by the same option or an option with
331 if (cl_options [next_opt_idx].neg_index == opt_idx)
334 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
335 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
341 /* Filter out options canceled by the ones after them. */
344 prune_options (int *argcp, char ***argvp)
347 int *options = XNEWVEC (int, argc);
348 /* We will only return this replacement argv if we remove at least
349 one argument, so it does not need to be size (argc + 1) to
350 make room for the terminating NULL because we will always have
351 freed up at least one slot when we end up using it at all. */
352 char **argv = XNEWVEC (char *, argc);
353 int i, arg_count, need_prune = 0;
354 const struct cl_option *option;
357 /* Scan all arguments. */
358 for (i = 1; i < argc; i++)
361 const char *opt = (*argvp) [i];
363 opt_index = find_opt (opt + 1, -1);
364 if (opt_index == OPT_SPECIAL_unknown
365 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
366 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
370 /* Drop the "no-" from negative switches. */
371 size_t len = strlen (opt) - 3;
373 dup = XNEWVEC (char, len + 1);
376 memcpy (dup + 2, opt + 5, len - 2 + 1);
379 opt_index = find_opt (opt + 1, -1);
383 if (opt_index == OPT_SPECIAL_unknown)
390 option = &cl_options[opt_index];
391 if (option->neg_index < 0)
394 /* Skip joined switches. */
395 if ((option->flags & CL_JOINED))
398 /* Reject negative form of switches that don't take negatives as
400 if (!value && (option->flags & CL_REJECT_NEGATIVE))
403 options [i] = (int) opt_index;
404 need_prune |= options [i];
410 /* Remove arguments which are negated by others after them. */
411 argv [0] = (*argvp) [0];
413 for (i = 1; i < argc; i++)
417 opt_idx = options [i];
421 for (j = i + 1; j < argc; j++)
423 next_opt_idx = options [j];
425 && cancel_option (opt_idx, next_opt_idx,
436 argv [arg_count] = (*argvp) [i];
441 if (arg_count != argc)
445 /* Add NULL-termination. Guaranteed not to overflow because
446 arg_count here can only be less than argc. */
458 /* Handle option OPT_INDEX, and argument ARG, for the language
459 indicated by LANG_MASK, using the handlers in HANDLERS. VALUE is
460 the option value as for the value field of cl_decoded_option. KIND
461 is the diagnostic_t if this is a diagnostics option, DK_UNSPECIFIED
462 otherwise. Returns false if the switch was invalid. */
465 handle_option (size_t opt_index, const char *arg, int value,
466 unsigned int lang_mask, int kind,
467 const struct cl_option_handlers *handlers)
469 const struct cl_option *option = &cl_options[opt_index];
472 if (option->flag_var)
473 set_option (opt_index, value, arg, kind);
475 for (i = 0; i < handlers->num_handlers; i++)
476 if (option->flags & handlers->handlers[i].mask)
478 if (!handlers->handlers[i].handler (opt_index, arg, value,
479 lang_mask, kind, handlers))
482 handlers->post_handling_callback (opt_index, arg, value,
483 handlers->handlers[i].mask);
489 /* Handle the switch DECODED for the language indicated by LANG_MASK,
490 using the handlers in *HANDLERS. */
493 read_cmdline_option (struct cl_decoded_option *decoded,
494 unsigned int lang_mask,
495 const struct cl_option_handlers *handlers)
497 const struct cl_option *option;
500 if (decoded->opt_index == OPT_SPECIAL_unknown)
504 if (handlers->unknown_option_callback (opt))
505 error ("unrecognized command line option %qs", opt);
509 option = &cl_options[decoded->opt_index];
510 opt = decoded->orig_option_with_args_text;
512 if (decoded->errors & CL_ERR_DISABLED)
514 error ("command line option %qs"
515 " is not supported by this configuration", opt);
519 if (decoded->errors & CL_ERR_WRONG_LANG)
521 handlers->wrong_lang_callback (opt, option, lang_mask);
525 if (decoded->errors & CL_ERR_MISSING_ARG)
527 if (option->missing_argument_error)
528 error (option->missing_argument_error, opt);
530 error ("missing argument to %qs", opt);
534 if (decoded->errors & CL_ERR_UINT_ARG)
536 error ("argument to %qs should be a non-negative integer",
541 gcc_assert (!decoded->errors);
543 if (!handle_option (decoded->opt_index, decoded->arg, decoded->value,
544 lang_mask, DK_UNSPECIFIED, handlers))
545 error ("unrecognized command line option %qs", opt);
548 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
549 diagnostic kind KIND. */
552 set_option (int opt_index, int value, const char *arg, int kind)
554 const struct cl_option *option = &cl_options[opt_index];
556 if (!option->flag_var)
559 switch (option->var_type)
562 *(int *) option->flag_var = value;
566 *(int *) option->flag_var = (value
568 : !option->var_value);
573 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
574 *(int *) option->flag_var |= option->var_value;
576 *(int *) option->flag_var &= ~option->var_value;
577 if (option->flag_var == &target_flags)
578 target_flags_explicit |= option->var_value;
582 *(const char **) option->flag_var = arg;
586 if ((diagnostic_t) kind != DK_UNSPECIFIED)
587 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,