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"
27 #include "tm.h" /* For SWITCH_TAKES_ARG and WORD_SWITCH_TAKES_ARG. */
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.
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".
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).
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. */
54 find_opt (const char *input, int lang_mask)
56 size_t mn, mx, md, opt_len;
57 size_t match_wrong_lang;
61 mx = cl_options_count;
63 /* Find mn such this lexicographical inequality holds:
64 cl_options[mn] <= input < cl_options[mn + 1]. */
68 opt_len = cl_options[md].opt_len;
69 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
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;
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. */
86 const struct cl_option *opt = &cl_options[mn];
88 /* Is the input either an exact match or a prefix that takes a
90 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
91 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
93 /* If language is OK, return it. */
94 if (opt->flags & lang_mask)
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;
103 /* Try the next possibility. This is cl_options_count if there
105 mn = opt->back_chain;
107 while (mn != cl_options_count);
109 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
110 return match_wrong_lang;
113 /* If ARG is a non-negative integer made up solely of digits, return its
114 value, otherwise return -1. */
117 integral_argument (const char *arg)
121 while (*p && ISDIGIT (*p))
130 /* Decode the switch beginning at ARGV for the language indicated by
131 LANG_MASK, into the structure *DECODED. Returns the number of
132 switches consumed. */
135 decode_cmdline_option (const char **argv, unsigned int lang_mask,
136 struct cl_decoded_option *decoded)
139 const char *opt, *arg = 0;
142 unsigned int result = 1, i;
145 const struct cl_option *option;
150 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
151 if (opt_index == OPT_SPECIAL_unknown
152 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
153 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
155 /* Drop the "no-" from negative switches. */
156 size_t len = strlen (opt) - 3;
158 dup = XNEWVEC (char, len + 1);
161 memcpy (dup + 2, opt + 5, len - 2 + 1);
164 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
167 if (opt_index == OPT_SPECIAL_unknown)
173 option = &cl_options[opt_index];
175 /* Reject negative form of switches that don't take negatives as
177 if (!value && (option->flags & CL_REJECT_NEGATIVE))
179 opt_index = OPT_SPECIAL_unknown;
184 /* Check to see if the option is disabled for this configuration. */
185 if (option->flags & CL_DISABLED)
186 errors |= CL_ERR_DISABLED;
188 /* Sort out any argument the switch takes. */
189 if (option->flags & CL_JOINED)
191 /* Have arg point to the original switch. This is because
192 some code, such as disable_builtin_function, expects its
193 argument to be persistent until the program exits. */
194 arg = argv[0] + cl_options[opt_index].opt_len + 1;
196 arg += strlen ("no-");
198 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
200 if (option->flags & CL_SEPARATE)
208 /* Missing argument. */
212 else if (option->flags & CL_SEPARATE)
220 /* Check if this is a switch for a different front end. */
221 if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
222 errors |= CL_ERR_WRONG_LANG;
223 else if ((option->flags & CL_TARGET)
224 && (option->flags & CL_LANG_ALL)
225 && !(option->flags & lang_mask))
226 /* Complain for target flag language mismatches if any languages
228 errors |= CL_ERR_WRONG_LANG;
230 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
231 errors |= CL_ERR_MISSING_ARG;
233 /* If the switch takes an integer, convert it. */
234 if (arg && (option->flags & CL_UINTEGER))
236 value = integral_argument (arg);
238 errors |= CL_ERR_UINT_ARG;
244 decoded->opt_index = opt_index;
246 decoded->value = value;
247 decoded->errors = errors;
249 if (opt_index == OPT_SPECIAL_unknown)
251 /* Skip the correct number of arguments for options handled
253 const char *popt = argv[0] + 1;
256 gcc_assert (result == 1);
257 if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
258 result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
259 else if (WORD_SWITCH_TAKES_ARG (popt))
260 result += WORD_SWITCH_TAKES_ARG (popt);
262 for (i = 1; i < result; i++)
270 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
271 decoded->canonical_option_num_elements = result;
273 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
277 decoded->canonical_option[i] = argv[i];
278 total_len += strlen (argv[i]) + 1;
281 decoded->canonical_option[i] = NULL;
283 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
284 for (i = 0; i < result; i++)
286 size_t len = strlen (argv[i]);
288 memcpy (p, argv[i], len);
299 /* Decode command-line options (ARGC and ARGV being the arguments of
300 main) into an array, setting *DECODED_OPTIONS to a pointer to that
301 array and *DECODED_OPTIONS_COUNT to the number of entries in the
302 array. The first entry in the array is always one for the program
303 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
304 applicable for decoding. Do not produce any diagnostics or set
305 state outside of these variables. */
308 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
309 unsigned int lang_mask,
310 struct cl_decoded_option **decoded_options,
311 unsigned int *decoded_options_count)
314 struct cl_decoded_option *opt_array;
315 unsigned int num_decoded_options;
317 opt_array = XNEWVEC (struct cl_decoded_option, argc);
319 opt_array[0].opt_index = OPT_SPECIAL_program_name;
320 opt_array[0].arg = argv[0];
321 opt_array[0].orig_option_with_args_text = argv[0];
322 opt_array[0].canonical_option_num_elements = 1;
323 opt_array[0].canonical_option[0] = argv[0];
324 opt_array[0].canonical_option[1] = NULL;
325 opt_array[0].canonical_option[2] = NULL;
326 opt_array[0].canonical_option[3] = NULL;
327 opt_array[0].value = 1;
328 opt_array[0].errors = 0;
329 num_decoded_options = 1;
331 for (i = 1; i < argc; i += n)
333 const char *opt = argv[i];
335 /* Interpret "-" or a non-switch as a file name. */
336 if (opt[0] != '-' || opt[1] == '\0')
338 opt_array[num_decoded_options].opt_index = OPT_SPECIAL_input_file;
339 opt_array[num_decoded_options].arg = opt;
340 opt_array[num_decoded_options].orig_option_with_args_text = opt;
341 opt_array[num_decoded_options].canonical_option_num_elements = 1;
342 opt_array[num_decoded_options].canonical_option[0] = opt;
343 opt_array[num_decoded_options].canonical_option[1] = NULL;
344 opt_array[num_decoded_options].canonical_option[2] = NULL;
345 opt_array[num_decoded_options].canonical_option[3] = NULL;
346 opt_array[num_decoded_options].value = 1;
347 opt_array[num_decoded_options].errors = 0;
348 num_decoded_options++;
353 n = decode_cmdline_option (argv + i, lang_mask,
354 &opt_array[num_decoded_options]);
355 num_decoded_options++;
358 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
359 num_decoded_options);
360 *decoded_options = opt_array;
361 *decoded_options_count = num_decoded_options;
364 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
365 next one is the same as ORIG_NEXT_OPT_IDX. */
368 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
370 /* An option can be canceled by the same option or an option with
372 if (cl_options [next_opt_idx].neg_index == opt_idx)
375 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
376 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
382 /* Filter out options canceled by the ones after them. */
385 prune_options (int *argcp, char ***argvp)
388 int *options = XNEWVEC (int, argc);
389 /* We will only return this replacement argv if we remove at least
390 one argument, so it does not need to be size (argc + 1) to
391 make room for the terminating NULL because we will always have
392 freed up at least one slot when we end up using it at all. */
393 char **argv = XNEWVEC (char *, argc);
394 int i, arg_count, need_prune = 0;
395 const struct cl_option *option;
398 /* Scan all arguments. */
399 for (i = 1; i < argc; i++)
402 const char *opt = (*argvp) [i];
404 opt_index = find_opt (opt + 1, -1);
405 if (opt_index == OPT_SPECIAL_unknown
406 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
407 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
411 /* Drop the "no-" from negative switches. */
412 size_t len = strlen (opt) - 3;
414 dup = XNEWVEC (char, len + 1);
417 memcpy (dup + 2, opt + 5, len - 2 + 1);
420 opt_index = find_opt (opt + 1, -1);
424 if (opt_index == OPT_SPECIAL_unknown)
431 option = &cl_options[opt_index];
432 if (option->neg_index < 0)
435 /* Skip joined switches. */
436 if ((option->flags & CL_JOINED))
439 /* Reject negative form of switches that don't take negatives as
441 if (!value && (option->flags & CL_REJECT_NEGATIVE))
444 options [i] = (int) opt_index;
445 need_prune |= options [i];
451 /* Remove arguments which are negated by others after them. */
452 argv [0] = (*argvp) [0];
454 for (i = 1; i < argc; i++)
458 opt_idx = options [i];
462 for (j = i + 1; j < argc; j++)
464 next_opt_idx = options [j];
466 && cancel_option (opt_idx, next_opt_idx,
477 argv [arg_count] = (*argvp) [i];
482 if (arg_count != argc)
486 /* Add NULL-termination. Guaranteed not to overflow because
487 arg_count here can only be less than argc. */
499 /* Handle option OPT_INDEX, and argument ARG, for the language
500 indicated by LANG_MASK, using the handlers in HANDLERS. VALUE is
501 the option value as for the value field of cl_decoded_option. KIND
502 is the diagnostic_t if this is a diagnostics option, DK_UNSPECIFIED
503 otherwise. Returns false if the switch was invalid. */
506 handle_option (size_t opt_index, const char *arg, int value,
507 unsigned int lang_mask, int kind,
508 const struct cl_option_handlers *handlers)
510 const struct cl_option *option = &cl_options[opt_index];
513 if (option->flag_var)
514 set_option (opt_index, value, arg, kind);
516 for (i = 0; i < handlers->num_handlers; i++)
517 if (option->flags & handlers->handlers[i].mask)
519 if (!handlers->handlers[i].handler (opt_index, arg, value,
520 lang_mask, kind, handlers))
523 handlers->post_handling_callback (opt_index, arg, value,
524 handlers->handlers[i].mask);
530 /* Handle the switch DECODED for the language indicated by LANG_MASK,
531 using the handlers in *HANDLERS. */
534 read_cmdline_option (struct cl_decoded_option *decoded,
535 unsigned int lang_mask,
536 const struct cl_option_handlers *handlers)
538 const struct cl_option *option;
541 if (decoded->opt_index == OPT_SPECIAL_unknown)
545 if (handlers->unknown_option_callback (opt))
546 error ("unrecognized command line option %qs", opt);
550 option = &cl_options[decoded->opt_index];
551 opt = decoded->orig_option_with_args_text;
553 if (decoded->errors & CL_ERR_DISABLED)
555 error ("command line option %qs"
556 " is not supported by this configuration", opt);
560 if (decoded->errors & CL_ERR_WRONG_LANG)
562 handlers->wrong_lang_callback (opt, option, lang_mask);
566 if (decoded->errors & CL_ERR_MISSING_ARG)
568 if (option->missing_argument_error)
569 error (option->missing_argument_error, opt);
571 error ("missing argument to %qs", opt);
575 if (decoded->errors & CL_ERR_UINT_ARG)
577 error ("argument to %qs should be a non-negative integer",
582 gcc_assert (!decoded->errors);
584 if (!handle_option (decoded->opt_index, decoded->arg, decoded->value,
585 lang_mask, DK_UNSPECIFIED, handlers))
586 error ("unrecognized command line option %qs", opt);
589 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
590 diagnostic kind KIND. */
593 set_option (int opt_index, int value, const char *arg, int kind)
595 const struct cl_option *option = &cl_options[opt_index];
597 if (!option->flag_var)
600 switch (option->var_type)
603 *(int *) option->flag_var = value;
607 *(int *) option->flag_var = (value
609 : !option->var_value);
614 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
615 *(int *) option->flag_var |= option->var_value;
617 *(int *) option->flag_var &= ~option->var_value;
618 if (option->flag_var == &target_flags)
619 target_flags_explicit |= option->var_value;
623 *(const char **) option->flag_var = arg;
627 if ((diagnostic_t) kind != DK_UNSPECIFIED)
628 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,