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 /* Return whether OPTION is OK for the language given by
133 option_ok_for_language (const struct cl_option *option,
134 unsigned int lang_mask)
136 if (!(option->flags & lang_mask))
138 else if ((option->flags & CL_TARGET)
139 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
140 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
141 /* Complain for target flag language mismatches if any languages
147 /* Decode the switch beginning at ARGV for the language indicated by
148 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
149 the structure *DECODED. Returns the number of switches
153 decode_cmdline_option (const char **argv, unsigned int lang_mask,
154 struct cl_decoded_option *decoded)
157 const char *opt, *arg = 0;
160 unsigned int result = 1, i;
163 const struct cl_option *option;
165 bool separate_arg_flag;
166 bool joined_arg_flag;
170 opt_index = find_opt (opt + 1, lang_mask);
171 if (opt_index == OPT_SPECIAL_unknown
172 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
173 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
175 /* Drop the "no-" from negative switches. */
176 size_t len = strlen (opt) - 3;
178 dup = XNEWVEC (char, len + 1);
181 memcpy (dup + 2, opt + 5, len - 2 + 1);
184 opt_index = find_opt (opt + 1, lang_mask);
187 if (opt_index == OPT_SPECIAL_unknown)
193 option = &cl_options[opt_index];
195 /* Reject negative form of switches that don't take negatives as
197 if (!value && (option->flags & CL_REJECT_NEGATIVE))
199 opt_index = OPT_SPECIAL_unknown;
204 /* Check to see if the option is disabled for this configuration. */
205 if (option->flags & CL_DISABLED)
206 errors |= CL_ERR_DISABLED;
208 /* Determine whether there may be a separate argument based on
209 whether this option is being processed for the driver. */
210 separate_arg_flag = ((option->flags & CL_SEPARATE)
211 && !((option->flags & CL_NO_DRIVER_ARG)
212 && (lang_mask & CL_DRIVER)));
213 joined_arg_flag = (option->flags & CL_JOINED) != 0;
215 /* Sort out any argument the switch takes. */
218 /* Have arg point to the original switch. This is because
219 some code, such as disable_builtin_function, expects its
220 argument to be persistent until the program exits. */
221 arg = argv[0] + cl_options[opt_index].opt_len + 1;
223 arg += strlen ("no-");
225 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
227 if (separate_arg_flag)
235 /* Missing argument. */
239 else if (separate_arg_flag)
247 /* Check if this is a switch for a different front end. */
248 if (!option_ok_for_language (option, lang_mask))
249 errors |= CL_ERR_WRONG_LANG;
251 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
252 errors |= CL_ERR_MISSING_ARG;
254 /* If the switch takes an integer, convert it. */
255 if (arg && (option->flags & CL_UINTEGER))
257 value = integral_argument (arg);
259 errors |= CL_ERR_UINT_ARG;
265 decoded->opt_index = opt_index;
267 decoded->value = value;
268 decoded->errors = errors;
270 if (opt_index == OPT_SPECIAL_unknown)
272 /* Skip the correct number of arguments for options handled
274 const char *popt = argv[0] + 1;
277 gcc_assert (result == 1);
278 if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
279 result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
280 else if (WORD_SWITCH_TAKES_ARG (popt))
281 result += WORD_SWITCH_TAKES_ARG (popt);
283 for (i = 1; i < result; i++)
291 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
292 decoded->canonical_option_num_elements = result;
294 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
298 decoded->canonical_option[i] = argv[i];
299 total_len += strlen (argv[i]) + 1;
302 decoded->canonical_option[i] = NULL;
304 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
305 for (i = 0; i < result; i++)
307 size_t len = strlen (argv[i]);
309 memcpy (p, argv[i], len);
320 /* Decode command-line options (ARGC and ARGV being the arguments of
321 main) into an array, setting *DECODED_OPTIONS to a pointer to that
322 array and *DECODED_OPTIONS_COUNT to the number of entries in the
323 array. The first entry in the array is always one for the program
324 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
325 flags applicable for decoding (including CL_COMMON and CL_TARGET if
326 those options should be considered applicable). Do not produce any
327 diagnostics or set state outside of these variables. */
330 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
331 unsigned int lang_mask,
332 struct cl_decoded_option **decoded_options,
333 unsigned int *decoded_options_count)
336 struct cl_decoded_option *opt_array;
337 unsigned int num_decoded_options;
339 opt_array = XNEWVEC (struct cl_decoded_option, argc);
341 opt_array[0].opt_index = OPT_SPECIAL_program_name;
342 opt_array[0].arg = argv[0];
343 opt_array[0].orig_option_with_args_text = argv[0];
344 opt_array[0].canonical_option_num_elements = 1;
345 opt_array[0].canonical_option[0] = argv[0];
346 opt_array[0].canonical_option[1] = NULL;
347 opt_array[0].canonical_option[2] = NULL;
348 opt_array[0].canonical_option[3] = NULL;
349 opt_array[0].value = 1;
350 opt_array[0].errors = 0;
351 num_decoded_options = 1;
353 for (i = 1; i < argc; i += n)
355 const char *opt = argv[i];
357 /* Interpret "-" or a non-switch as a file name. */
358 if (opt[0] != '-' || opt[1] == '\0')
360 generate_option_input_file (opt, &opt_array[num_decoded_options]);
361 num_decoded_options++;
366 n = decode_cmdline_option (argv + i, lang_mask,
367 &opt_array[num_decoded_options]);
368 num_decoded_options++;
371 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
372 num_decoded_options);
373 *decoded_options = opt_array;
374 *decoded_options_count = num_decoded_options;
377 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
378 next one is the same as ORIG_NEXT_OPT_IDX. */
381 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
383 /* An option can be canceled by the same option or an option with
385 if (cl_options [next_opt_idx].neg_index == opt_idx)
388 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
389 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
395 /* Filter out options canceled by the ones after them. */
398 prune_options (int *argcp, char ***argvp)
401 int *options = XNEWVEC (int, argc);
402 /* We will only return this replacement argv if we remove at least
403 one argument, so it does not need to be size (argc + 1) to
404 make room for the terminating NULL because we will always have
405 freed up at least one slot when we end up using it at all. */
406 char **argv = XNEWVEC (char *, argc);
407 int i, arg_count, need_prune = 0;
408 const struct cl_option *option;
411 /* Scan all arguments. */
412 for (i = 1; i < argc; i++)
415 const char *opt = (*argvp) [i];
417 opt_index = find_opt (opt + 1, -1);
418 if (opt_index == OPT_SPECIAL_unknown
419 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
420 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
424 /* Drop the "no-" from negative switches. */
425 size_t len = strlen (opt) - 3;
427 dup = XNEWVEC (char, len + 1);
430 memcpy (dup + 2, opt + 5, len - 2 + 1);
433 opt_index = find_opt (opt + 1, -1);
437 if (opt_index == OPT_SPECIAL_unknown)
444 option = &cl_options[opt_index];
445 if (option->neg_index < 0)
448 /* Skip joined switches. */
449 if ((option->flags & CL_JOINED))
452 /* Reject negative form of switches that don't take negatives as
454 if (!value && (option->flags & CL_REJECT_NEGATIVE))
457 options [i] = (int) opt_index;
458 need_prune |= options [i];
464 /* Remove arguments which are negated by others after them. */
465 argv [0] = (*argvp) [0];
467 for (i = 1; i < argc; i++)
471 opt_idx = options [i];
475 for (j = i + 1; j < argc; j++)
477 next_opt_idx = options [j];
479 && cancel_option (opt_idx, next_opt_idx,
490 argv [arg_count] = (*argvp) [i];
495 if (arg_count != argc)
499 /* Add NULL-termination. Guaranteed not to overflow because
500 arg_count here can only be less than argc. */
512 /* Handle option DECODED for the language indicated by LANG_MASK,
513 using the handlers in HANDLERS. KIND is the diagnostic_t if this
514 is a diagnostics option, DK_UNSPECIFIED otherwise. Returns false
515 if the switch was invalid. */
518 handle_option (const struct cl_decoded_option *decoded,
519 unsigned int lang_mask, int kind,
520 const struct cl_option_handlers *handlers)
522 size_t opt_index = decoded->opt_index;
523 const char *arg = decoded->arg;
524 int value = decoded->value;
525 const struct cl_option *option = &cl_options[opt_index];
528 if (option->flag_var)
529 set_option (opt_index, value, arg, kind);
531 for (i = 0; i < handlers->num_handlers; i++)
532 if (option->flags & handlers->handlers[i].mask)
534 if (!handlers->handlers[i].handler (decoded,
535 lang_mask, kind, handlers))
538 handlers->post_handling_callback (decoded,
539 handlers->handlers[i].mask);
545 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
546 option instead of DECODED. This is used for callbacks when one
547 option implies another instead of an option being decoded from the
551 handle_generated_option (size_t opt_index, const char *arg, int value,
552 unsigned int lang_mask, int kind,
553 const struct cl_option_handlers *handlers)
555 struct cl_decoded_option decoded;
557 generate_option (opt_index, arg, value, lang_mask, &decoded);
558 return handle_option (&decoded, lang_mask, kind, handlers);
561 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
562 VALUE for a front end using LANG_MASK. This is used when the
563 compiler generates options internally. */
566 generate_option (size_t opt_index, const char *arg, int value,
567 unsigned int lang_mask, struct cl_decoded_option *decoded)
569 const struct cl_option *option = &cl_options[opt_index];
571 decoded->opt_index = opt_index;
573 decoded->canonical_option[2] = NULL;
574 decoded->canonical_option[3] = NULL;
575 decoded->value = value;
576 decoded->errors = (option_ok_for_language (option, lang_mask)
578 : CL_ERR_WRONG_LANG);
582 if (option->flags & CL_SEPARATE)
584 decoded->orig_option_with_args_text = concat (option->opt_text, " ",
586 decoded->canonical_option[0] = option->opt_text;
587 decoded->canonical_option[1] = arg;
588 decoded->canonical_option_num_elements = 2;
592 gcc_assert (option->flags & CL_JOINED);
593 decoded->orig_option_with_args_text = concat (option->opt_text, arg,
595 decoded->canonical_option[0] = decoded->orig_option_with_args_text;
596 decoded->canonical_option[1] = NULL;
597 decoded->canonical_option_num_elements = 1;
602 decoded->orig_option_with_args_text = option->opt_text;
603 decoded->canonical_option[0] = option->opt_text;
604 decoded->canonical_option[1] = NULL;
605 decoded->canonical_option_num_elements = 1;
609 /* Fill in *DECODED with an option for input file FILE. */
612 generate_option_input_file (const char *file,
613 struct cl_decoded_option *decoded)
615 decoded->opt_index = OPT_SPECIAL_input_file;
617 decoded->orig_option_with_args_text = file;
618 decoded->canonical_option_num_elements = 1;
619 decoded->canonical_option[0] = file;
620 decoded->canonical_option[1] = NULL;
621 decoded->canonical_option[2] = NULL;
622 decoded->canonical_option[3] = NULL;
627 /* Handle the switch DECODED for the language indicated by LANG_MASK,
628 using the handlers in *HANDLERS. */
631 read_cmdline_option (struct cl_decoded_option *decoded,
632 unsigned int lang_mask,
633 const struct cl_option_handlers *handlers)
635 const struct cl_option *option;
638 if (decoded->opt_index == OPT_SPECIAL_unknown)
640 if (handlers->unknown_option_callback (decoded))
641 error ("unrecognized command line option %qs", decoded->arg);
645 option = &cl_options[decoded->opt_index];
646 opt = decoded->orig_option_with_args_text;
648 if (decoded->errors & CL_ERR_DISABLED)
650 error ("command line option %qs"
651 " is not supported by this configuration", opt);
655 if (decoded->errors & CL_ERR_WRONG_LANG)
657 handlers->wrong_lang_callback (decoded, lang_mask);
661 if (decoded->errors & CL_ERR_MISSING_ARG)
663 if (option->missing_argument_error)
664 error (option->missing_argument_error, opt);
666 error ("missing argument to %qs", opt);
670 if (decoded->errors & CL_ERR_UINT_ARG)
672 error ("argument to %qs should be a non-negative integer",
677 gcc_assert (!decoded->errors);
679 if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
680 error ("unrecognized command line option %qs", opt);
683 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
684 diagnostic kind KIND. */
687 set_option (int opt_index, int value, const char *arg, int kind)
689 const struct cl_option *option = &cl_options[opt_index];
691 if (!option->flag_var)
694 switch (option->var_type)
697 *(int *) option->flag_var = value;
701 *(int *) option->flag_var = (value
703 : !option->var_value);
708 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
709 *(int *) option->flag_var |= option->var_value;
711 *(int *) option->flag_var &= ~option->var_value;
712 if (option->flag_var == &target_flags)
713 target_flags_explicit |= option->var_value;
717 *(const char **) option->flag_var = arg;
721 if ((diagnostic_t) kind != DK_UNSPECIFIED)
722 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,