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
148 /* Fill in the canonical option part of *DECODED with an option
149 described by OPT_INDEX, ARG and VALUE. */
152 generate_canonical_option (size_t opt_index, const char *arg, int value,
153 struct cl_decoded_option *decoded)
155 const struct cl_option *option = &cl_options[opt_index];
156 const char *opt_text = option->opt_text;
159 && !(option->flags & CL_REJECT_NEGATIVE)
160 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
162 char *t = XNEWVEC (char, option->opt_len + 5);
168 memcpy (t + 5, opt_text + 2, option->opt_len);
172 decoded->canonical_option[2] = NULL;
173 decoded->canonical_option[3] = NULL;
177 if ((option->flags & CL_SEPARATE)
178 && !(option->flags & CL_SEPARATE_ALIAS))
180 decoded->canonical_option[0] = opt_text;
181 decoded->canonical_option[1] = arg;
182 decoded->canonical_option_num_elements = 2;
186 gcc_assert (option->flags & CL_JOINED);
187 decoded->canonical_option[0] = concat (opt_text, arg, NULL);
188 decoded->canonical_option[1] = NULL;
189 decoded->canonical_option_num_elements = 1;
194 decoded->canonical_option[0] = opt_text;
195 decoded->canonical_option[1] = NULL;
196 decoded->canonical_option_num_elements = 1;
200 /* Decode the switch beginning at ARGV for the language indicated by
201 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
202 the structure *DECODED. Returns the number of switches
206 decode_cmdline_option (const char **argv, unsigned int lang_mask,
207 struct cl_decoded_option *decoded)
210 const char *opt, *arg = 0;
213 unsigned int result = 1, i;
216 const struct cl_option *option;
218 const char *warn_message = NULL;
219 bool separate_arg_flag;
220 bool joined_arg_flag;
221 bool have_separate_arg = false;
225 opt_index = find_opt (opt + 1, lang_mask);
226 if (opt_index == OPT_SPECIAL_unknown
227 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
228 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
230 /* Drop the "no-" from negative switches. */
231 size_t len = strlen (opt) - 3;
233 dup = XNEWVEC (char, len + 1);
236 memcpy (dup + 2, opt + 5, len - 2 + 1);
239 opt_index = find_opt (opt + 1, lang_mask);
242 if (opt_index == OPT_SPECIAL_unknown)
248 option = &cl_options[opt_index];
250 /* Reject negative form of switches that don't take negatives as
252 if (!value && (option->flags & CL_REJECT_NEGATIVE))
254 opt_index = OPT_SPECIAL_unknown;
255 errors |= CL_ERR_NEGATIVE;
260 warn_message = option->warn_message;
262 /* Check to see if the option is disabled for this configuration. */
263 if (option->flags & CL_DISABLED)
264 errors |= CL_ERR_DISABLED;
266 /* Determine whether there may be a separate argument based on
267 whether this option is being processed for the driver. */
268 separate_arg_flag = ((option->flags & CL_SEPARATE)
269 && !((option->flags & CL_NO_DRIVER_ARG)
270 && (lang_mask & CL_DRIVER)));
271 joined_arg_flag = (option->flags & CL_JOINED) != 0;
273 /* Sort out any argument the switch takes. */
276 /* Have arg point to the original switch. This is because
277 some code, such as disable_builtin_function, expects its
278 argument to be persistent until the program exits. */
279 arg = argv[0] + cl_options[opt_index].opt_len + 1;
281 arg += strlen ("no-");
283 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
285 if (separate_arg_flag)
292 have_separate_arg = true;
295 /* Missing argument. */
299 else if (separate_arg_flag)
306 have_separate_arg = true;
309 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
310 errors |= CL_ERR_MISSING_ARG;
312 /* Is this option an alias (or an ignored option, marked as an alias
313 of OPT_SPECIAL_ignore)? */
314 if (option->alias_target != N_OPTS
315 && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
317 size_t new_opt_index = option->alias_target;
319 if (new_opt_index == OPT_SPECIAL_ignore)
321 gcc_assert (option->alias_arg == NULL);
322 gcc_assert (option->neg_alias_arg == NULL);
323 opt_index = new_opt_index;
329 const struct cl_option *new_option = &cl_options[new_opt_index];
331 /* The new option must not be an alias itself. */
332 gcc_assert (new_option->alias_target == N_OPTS);
334 if (option->neg_alias_arg)
336 gcc_assert (option->alias_arg != NULL);
337 gcc_assert (arg == NULL);
339 arg = option->alias_arg;
341 arg = option->neg_alias_arg;
344 else if (option->alias_arg)
346 gcc_assert (value == 1);
347 gcc_assert (arg == NULL);
348 arg = option->alias_arg;
351 opt_index = new_opt_index;
355 gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
357 /* Recompute what arguments are allowed. */
358 separate_arg_flag = ((option->flags & CL_SEPARATE)
359 && !((option->flags & CL_NO_DRIVER_ARG)
360 && (lang_mask & CL_DRIVER)));
361 joined_arg_flag = (option->flags & CL_JOINED) != 0;
363 if (!(errors & CL_ERR_MISSING_ARG))
365 if (separate_arg_flag || joined_arg_flag)
366 gcc_assert (arg != NULL);
368 gcc_assert (arg == NULL);
371 /* Recheck for warnings and disabled options. */
372 if (option->warn_message)
374 gcc_assert (warn_message == NULL);
375 warn_message = option->warn_message;
377 if (option->flags & CL_DISABLED)
378 errors |= CL_ERR_DISABLED;
382 /* Check if this is a switch for a different front end. */
383 if (!option_ok_for_language (option, lang_mask))
384 errors |= CL_ERR_WRONG_LANG;
386 /* If the switch takes an integer, convert it. */
387 if (arg && (option->flags & CL_UINTEGER))
389 value = integral_argument (arg);
391 errors |= CL_ERR_UINT_ARG;
397 decoded->opt_index = opt_index;
399 decoded->value = value;
400 decoded->errors = errors;
401 decoded->warn_message = warn_message;
403 if (opt_index == OPT_SPECIAL_unknown)
405 /* Skip the correct number of arguments for options handled
407 const char *popt = argv[0] + 1;
410 gcc_assert (result == 1);
411 if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
412 result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
413 else if (WORD_SWITCH_TAKES_ARG (popt))
414 result += WORD_SWITCH_TAKES_ARG (popt);
416 for (i = 1; i < result; i++)
424 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
425 decoded->canonical_option_num_elements = result;
427 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
431 if (opt_index == OPT_SPECIAL_unknown)
432 decoded->canonical_option[i] = argv[i];
434 decoded->canonical_option[i] = NULL;
435 total_len += strlen (argv[i]) + 1;
438 decoded->canonical_option[i] = NULL;
440 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
441 generate_canonical_option (opt_index, arg, value, decoded);
442 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
443 for (i = 0; i < result; i++)
445 size_t len = strlen (argv[i]);
447 memcpy (p, argv[i], len);
458 /* Decode command-line options (ARGC and ARGV being the arguments of
459 main) into an array, setting *DECODED_OPTIONS to a pointer to that
460 array and *DECODED_OPTIONS_COUNT to the number of entries in the
461 array. The first entry in the array is always one for the program
462 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
463 flags applicable for decoding (including CL_COMMON and CL_TARGET if
464 those options should be considered applicable). Do not produce any
465 diagnostics or set state outside of these variables. */
468 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
469 unsigned int lang_mask,
470 struct cl_decoded_option **decoded_options,
471 unsigned int *decoded_options_count)
474 struct cl_decoded_option *opt_array;
475 unsigned int num_decoded_options;
477 opt_array = XNEWVEC (struct cl_decoded_option, argc);
479 opt_array[0].opt_index = OPT_SPECIAL_program_name;
480 opt_array[0].warn_message = NULL;
481 opt_array[0].arg = argv[0];
482 opt_array[0].orig_option_with_args_text = argv[0];
483 opt_array[0].canonical_option_num_elements = 1;
484 opt_array[0].canonical_option[0] = argv[0];
485 opt_array[0].canonical_option[1] = NULL;
486 opt_array[0].canonical_option[2] = NULL;
487 opt_array[0].canonical_option[3] = NULL;
488 opt_array[0].value = 1;
489 opt_array[0].errors = 0;
490 num_decoded_options = 1;
492 for (i = 1; i < argc; i += n)
494 const char *opt = argv[i];
496 /* Interpret "-" or a non-switch as a file name. */
497 if (opt[0] != '-' || opt[1] == '\0')
499 generate_option_input_file (opt, &opt_array[num_decoded_options]);
500 num_decoded_options++;
505 n = decode_cmdline_option (argv + i, lang_mask,
506 &opt_array[num_decoded_options]);
507 num_decoded_options++;
510 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
511 num_decoded_options);
512 *decoded_options = opt_array;
513 *decoded_options_count = num_decoded_options;
516 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
517 next one is the same as ORIG_NEXT_OPT_IDX. */
520 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
522 /* An option can be canceled by the same option or an option with
524 if (cl_options [next_opt_idx].neg_index == opt_idx)
527 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
528 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
534 /* Filter out options canceled by the ones after them. */
537 prune_options (int *argcp, char ***argvp)
540 int *options = XNEWVEC (int, argc);
541 /* We will only return this replacement argv if we remove at least
542 one argument, so it does not need to be size (argc + 1) to
543 make room for the terminating NULL because we will always have
544 freed up at least one slot when we end up using it at all. */
545 char **argv = XNEWVEC (char *, argc);
546 int i, arg_count, need_prune = 0;
547 const struct cl_option *option;
550 /* Scan all arguments. */
551 for (i = 1; i < argc; i++)
554 const char *opt = (*argvp) [i];
556 opt_index = find_opt (opt + 1, -1);
557 if (opt_index == OPT_SPECIAL_unknown
558 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
559 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
563 /* Drop the "no-" from negative switches. */
564 size_t len = strlen (opt) - 3;
566 dup = XNEWVEC (char, len + 1);
569 memcpy (dup + 2, opt + 5, len - 2 + 1);
572 opt_index = find_opt (opt + 1, -1);
576 if (opt_index == OPT_SPECIAL_unknown)
583 option = &cl_options[opt_index];
584 if (option->neg_index < 0)
587 /* Skip joined switches. */
588 if ((option->flags & CL_JOINED))
591 /* Reject negative form of switches that don't take negatives as
593 if (!value && (option->flags & CL_REJECT_NEGATIVE))
596 options [i] = (int) opt_index;
597 need_prune |= options [i];
603 /* Remove arguments which are negated by others after them. */
604 argv [0] = (*argvp) [0];
606 for (i = 1; i < argc; i++)
610 opt_idx = options [i];
614 for (j = i + 1; j < argc; j++)
616 next_opt_idx = options [j];
618 && cancel_option (opt_idx, next_opt_idx,
629 argv [arg_count] = (*argvp) [i];
634 if (arg_count != argc)
638 /* Add NULL-termination. Guaranteed not to overflow because
639 arg_count here can only be less than argc. */
651 /* Handle option DECODED for the language indicated by LANG_MASK,
652 using the handlers in HANDLERS. KIND is the diagnostic_t if this
653 is a diagnostics option, DK_UNSPECIFIED otherwise. Returns false
654 if the switch was invalid. */
657 handle_option (const struct cl_decoded_option *decoded,
658 unsigned int lang_mask, int kind,
659 const struct cl_option_handlers *handlers)
661 size_t opt_index = decoded->opt_index;
662 const char *arg = decoded->arg;
663 int value = decoded->value;
664 const struct cl_option *option = &cl_options[opt_index];
667 if (option->flag_var)
668 set_option (opt_index, value, arg, kind);
670 for (i = 0; i < handlers->num_handlers; i++)
671 if (option->flags & handlers->handlers[i].mask)
673 if (!handlers->handlers[i].handler (decoded,
674 lang_mask, kind, handlers))
677 handlers->post_handling_callback (decoded,
678 handlers->handlers[i].mask);
684 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
685 option instead of DECODED. This is used for callbacks when one
686 option implies another instead of an option being decoded from the
690 handle_generated_option (size_t opt_index, const char *arg, int value,
691 unsigned int lang_mask, int kind,
692 const struct cl_option_handlers *handlers)
694 struct cl_decoded_option decoded;
696 generate_option (opt_index, arg, value, lang_mask, &decoded);
697 return handle_option (&decoded, lang_mask, kind, handlers);
700 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
701 VALUE for a front end using LANG_MASK. This is used when the
702 compiler generates options internally. */
705 generate_option (size_t opt_index, const char *arg, int value,
706 unsigned int lang_mask, struct cl_decoded_option *decoded)
708 const struct cl_option *option = &cl_options[opt_index];
710 decoded->opt_index = opt_index;
711 decoded->warn_message = NULL;
713 decoded->value = value;
714 decoded->errors = (option_ok_for_language (option, lang_mask)
716 : CL_ERR_WRONG_LANG);
718 generate_canonical_option (opt_index, arg, value, decoded);
719 switch (decoded->canonical_option_num_elements)
722 decoded->orig_option_with_args_text = decoded->canonical_option[0];
726 decoded->orig_option_with_args_text
727 = concat (decoded->canonical_option[0], " ",
728 decoded->canonical_option[1], NULL);
736 /* Fill in *DECODED with an option for input file FILE. */
739 generate_option_input_file (const char *file,
740 struct cl_decoded_option *decoded)
742 decoded->opt_index = OPT_SPECIAL_input_file;
743 decoded->warn_message = NULL;
745 decoded->orig_option_with_args_text = file;
746 decoded->canonical_option_num_elements = 1;
747 decoded->canonical_option[0] = file;
748 decoded->canonical_option[1] = NULL;
749 decoded->canonical_option[2] = NULL;
750 decoded->canonical_option[3] = NULL;
755 /* Handle the switch DECODED for the language indicated by LANG_MASK,
756 using the handlers in *HANDLERS. */
759 read_cmdline_option (struct cl_decoded_option *decoded,
760 unsigned int lang_mask,
761 const struct cl_option_handlers *handlers)
763 const struct cl_option *option;
764 const char *opt = decoded->orig_option_with_args_text;
766 if (decoded->warn_message)
767 warning (0, decoded->warn_message, opt);
769 if (decoded->opt_index == OPT_SPECIAL_unknown)
771 if (handlers->unknown_option_callback (decoded))
772 error ("unrecognized command line option %qs", decoded->arg);
776 if (decoded->opt_index == OPT_SPECIAL_ignore)
779 option = &cl_options[decoded->opt_index];
781 if (decoded->errors & CL_ERR_DISABLED)
783 error ("command line option %qs"
784 " is not supported by this configuration", opt);
788 if (decoded->errors & CL_ERR_WRONG_LANG)
790 handlers->wrong_lang_callback (decoded, lang_mask);
794 if (decoded->errors & CL_ERR_MISSING_ARG)
796 if (option->missing_argument_error)
797 error (option->missing_argument_error, opt);
799 error ("missing argument to %qs", opt);
803 if (decoded->errors & CL_ERR_UINT_ARG)
805 error ("argument to %qs should be a non-negative integer",
810 gcc_assert (!decoded->errors);
812 if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
813 error ("unrecognized command line option %qs", opt);
816 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
817 diagnostic kind KIND. */
820 set_option (int opt_index, int value, const char *arg, int kind)
822 const struct cl_option *option = &cl_options[opt_index];
824 if (!option->flag_var)
827 switch (option->var_type)
830 *(int *) option->flag_var = value;
834 *(int *) option->flag_var = (value
836 : !option->var_value);
841 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
842 *(int *) option->flag_var |= option->var_value;
844 *(int *) option->flag_var &= ~option->var_value;
845 if (option->flag_var == &target_flags)
846 target_flags_explicit |= option->var_value;
850 *(const char **) option->flag_var = arg;
854 if ((diagnostic_t) kind != DK_UNSPECIFIED)
855 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,