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 TARGET_OPTION_TRANSLATE_TABLE. */
29 static void prune_options (struct cl_decoded_option **, unsigned int *);
31 /* Perform a binary search to find which option the command-line INPUT
32 matches. Returns its index in the option array, and
33 OPT_SPECIAL_unknown on failure.
35 This routine is quite subtle. A normal binary search is not good
36 enough because some options can be suffixed with an argument, and
37 multiple sub-matches can occur, e.g. input of "-pedantic" matching
38 the initial substring of "-pedantic-errors".
40 A more complicated example is -gstabs. It should match "-g" with
41 an argument of "stabs". Suppose, however, that the number and list
42 of switches are such that the binary search tests "-gen-decls"
43 before having tested "-g". This doesn't match, and as "-gen-decls"
44 is less than "-gstabs", it will become the lower bound of the
45 binary search range, and "-g" will never be seen. To resolve this
46 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
47 to "-g" so that failed searches that end between "-gen-decls" and
48 the lexicographically subsequent switch know to go back and see if
49 "-g" causes a match (which it does in this example).
51 This search is done in such a way that the longest match for the
52 front end in question wins. If there is no match for the current
53 front end, the longest match for a different front end is returned
54 (or N_OPTS if none) and the caller emits an error message. */
56 find_opt (const char *input, int lang_mask)
58 size_t mn, mn_orig, mx, md, opt_len;
59 size_t match_wrong_lang;
63 mx = cl_options_count;
65 /* Find mn such this lexicographical inequality holds:
66 cl_options[mn] <= input < cl_options[mn + 1]. */
70 opt_len = cl_options[md].opt_len;
71 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
81 /* This is the switch that is the best match but for a different
82 front end, or OPT_SPECIAL_unknown if there is no match at all. */
83 match_wrong_lang = OPT_SPECIAL_unknown;
85 /* Backtrace the chain of possible matches, returning the longest
86 one, if any, that fits best. With current GCC switches, this
87 loop executes at most twice. */
90 const struct cl_option *opt = &cl_options[mn];
92 /* Is the input either an exact match or a prefix that takes a
94 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
95 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
97 /* If language is OK, return it. */
98 if (opt->flags & lang_mask)
101 /* If we haven't remembered a prior match, remember this
102 one. Any prior match is necessarily better. */
103 if (match_wrong_lang == OPT_SPECIAL_unknown)
104 match_wrong_lang = mn;
107 /* Try the next possibility. This is cl_options_count if there
109 mn = opt->back_chain;
111 while (mn != cl_options_count);
113 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
115 /* Long options, starting "--", may be abbreviated if the
116 abbreviation is unambiguous. This only applies to options
117 not taking a joined argument, and abbreviations of "--option"
118 are permitted even if there is a variant "--option=". */
119 size_t mnc = mn_orig + 1;
120 size_t cmp_len = strlen (input);
121 while (mnc < cl_options_count
122 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
124 /* Option matching this abbreviation. OK if it is the first
125 match and that does not take a joined argument, or the
126 second match, taking a joined argument and with only '='
127 added to the first match; otherwise considered
129 if (mnc == mn_orig + 1
130 && !(cl_options[mnc].flags & CL_JOINED))
131 match_wrong_lang = mnc;
132 else if (mnc == mn_orig + 2
133 && match_wrong_lang == mn_orig + 1
134 && (cl_options[mnc].flags & CL_JOINED)
135 && (cl_options[mnc].opt_len
136 == cl_options[mn_orig + 1].opt_len + 1)
137 && strncmp (cl_options[mnc].opt_text + 1,
138 cl_options[mn_orig + 1].opt_text + 1,
139 cl_options[mn_orig + 1].opt_len) == 0)
140 ; /* OK, as long as there are no more matches. */
142 return OPT_SPECIAL_unknown;
147 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
148 return match_wrong_lang;
151 /* If ARG is a non-negative integer made up solely of digits, return its
152 value, otherwise return -1. */
155 integral_argument (const char *arg)
159 while (*p && ISDIGIT (*p))
168 /* Return whether OPTION is OK for the language given by
171 option_ok_for_language (const struct cl_option *option,
172 unsigned int lang_mask)
174 if (!(option->flags & lang_mask))
176 else if ((option->flags & CL_TARGET)
177 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
178 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
179 /* Complain for target flag language mismatches if any languages
186 /* Fill in the canonical option part of *DECODED with an option
187 described by OPT_INDEX, ARG and VALUE. */
190 generate_canonical_option (size_t opt_index, const char *arg, int value,
191 struct cl_decoded_option *decoded)
193 const struct cl_option *option = &cl_options[opt_index];
194 const char *opt_text = option->opt_text;
197 && !(option->flags & CL_REJECT_NEGATIVE)
198 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
200 char *t = XNEWVEC (char, option->opt_len + 5);
206 memcpy (t + 5, opt_text + 2, option->opt_len);
210 decoded->canonical_option[2] = NULL;
211 decoded->canonical_option[3] = NULL;
215 if ((option->flags & CL_SEPARATE)
216 && !(option->flags & CL_SEPARATE_ALIAS))
218 decoded->canonical_option[0] = opt_text;
219 decoded->canonical_option[1] = arg;
220 decoded->canonical_option_num_elements = 2;
224 gcc_assert (option->flags & CL_JOINED);
225 decoded->canonical_option[0] = concat (opt_text, arg, NULL);
226 decoded->canonical_option[1] = NULL;
227 decoded->canonical_option_num_elements = 1;
232 decoded->canonical_option[0] = opt_text;
233 decoded->canonical_option[1] = NULL;
234 decoded->canonical_option_num_elements = 1;
238 /* Structure describing mappings from options on the command line to
239 options to look up with find_opt. */
242 /* Prefix of the option on the command line. */
244 /* If two argv elements are considered to be merged into one option,
245 prefix for the second element, otherwise NULL. */
247 /* The new prefix to map to. */
248 const char *new_prefix;
249 /* Whether at least one character is needed following opt1 or opt0
250 for this mapping to be used. (--optimize= is valid for -O, but
251 --warn- is not valid for -W.) */
252 bool another_char_needed;
253 /* Whether the original option is a negated form of the option
254 resulting from this map. */
257 static const struct option_map option_map[] =
259 { "-Wno-", NULL, "-W", false, true },
260 { "-fno-", NULL, "-f", false, true },
261 { "-mno-", NULL, "-m", false, true },
262 { "--debug=", NULL, "-g", false, false },
263 { "--machine-", NULL, "-m", true, false },
264 { "--machine-no-", NULL, "-m", false, true },
265 { "--machine=", NULL, "-m", false, false },
266 { "--machine=no-", NULL, "-m", false, true },
267 { "--machine", "", "-m", false, false },
268 { "--machine", "no-", "-m", false, true },
269 { "--optimize=", NULL, "-O", false, false },
270 { "--std=", NULL, "-std=", false, false },
271 { "--std", "", "-std=", false, false },
272 { "--warn-", NULL, "-W", true, false },
273 { "--warn-no-", NULL, "-W", false, true },
274 { "--", NULL, "-f", true, false },
275 { "--no-", NULL, "-f", false, true }
278 /* Decode the switch beginning at ARGV for the language indicated by
279 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
280 the structure *DECODED. Returns the number of switches
284 decode_cmdline_option (const char **argv, unsigned int lang_mask,
285 struct cl_decoded_option *decoded)
290 unsigned int result = 1, i, extra_args, separate_args;
294 const struct cl_option *option;
296 const char *warn_message = NULL;
297 bool separate_arg_flag;
298 bool joined_arg_flag;
299 bool have_separate_arg = false;
303 opt_index = find_opt (argv[0] + 1, lang_mask);
305 while (opt_index == OPT_SPECIAL_unknown
306 && i < ARRAY_SIZE (option_map))
308 const char *opt0 = option_map[i].opt0;
309 const char *opt1 = option_map[i].opt1;
310 const char *new_prefix = option_map[i].new_prefix;
311 bool another_char_needed = option_map[i].another_char_needed;
312 size_t opt0_len = strlen (opt0);
313 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
314 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
315 size_t new_prefix_len = strlen (new_prefix);
317 extra_args = (opt1 == NULL ? 0 : 1);
318 value = !option_map[i].negated;
320 if (strncmp (argv[0], opt0, opt0_len) == 0
322 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
323 && (!another_char_needed
324 || argv[extra_args][optn_len] != 0))
326 size_t arglen = strlen (argv[extra_args]);
329 adjust_len = (int) optn_len - (int) new_prefix_len;
330 dup = XNEWVEC (char, arglen + 1 - adjust_len);
331 memcpy (dup, new_prefix, new_prefix_len);
332 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
333 arglen - optn_len + 1);
334 opt_index = find_opt (dup + 1, lang_mask);
340 if (opt_index == OPT_SPECIAL_unknown)
348 option = &cl_options[opt_index];
350 /* Reject negative form of switches that don't take negatives as
352 if (!value && (option->flags & CL_REJECT_NEGATIVE))
354 opt_index = OPT_SPECIAL_unknown;
355 errors |= CL_ERR_NEGATIVE;
360 result = extra_args + 1;
361 warn_message = option->warn_message;
363 /* Check to see if the option is disabled for this configuration. */
364 if (option->flags & CL_DISABLED)
365 errors |= CL_ERR_DISABLED;
367 /* Determine whether there may be a separate argument based on
368 whether this option is being processed for the driver, and, if
369 so, how many such arguments. */
370 separate_arg_flag = ((option->flags & CL_SEPARATE)
371 && !((option->flags & CL_NO_DRIVER_ARG)
372 && (lang_mask & CL_DRIVER)));
373 separate_args = (separate_arg_flag
374 ? ((option->flags & CL_SEPARATE_NARGS_MASK)
375 >> CL_SEPARATE_NARGS_SHIFT) + 1
377 joined_arg_flag = (option->flags & CL_JOINED) != 0;
379 /* Sort out any argument the switch takes. */
382 /* Have arg point to the original switch. This is because
383 some code, such as disable_builtin_function, expects its
384 argument to be persistent until the program exits. */
385 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
387 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
389 if (separate_arg_flag)
391 arg = argv[extra_args + 1];
392 result = extra_args + 2;
394 result = extra_args + 1;
396 have_separate_arg = true;
399 /* Missing argument. */
403 else if (separate_arg_flag)
405 arg = argv[extra_args + 1];
406 for (i = 0; i < separate_args; i++)
407 if (argv[extra_args + 1 + i] == NULL)
409 errors |= CL_ERR_MISSING_ARG;
412 result = extra_args + 1 + i;
414 have_separate_arg = true;
417 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
418 errors |= CL_ERR_MISSING_ARG;
420 /* Is this option an alias (or an ignored option, marked as an alias
421 of OPT_SPECIAL_ignore)? */
422 if (option->alias_target != N_OPTS
423 && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
425 size_t new_opt_index = option->alias_target;
427 if (new_opt_index == OPT_SPECIAL_ignore)
429 gcc_assert (option->alias_arg == NULL);
430 gcc_assert (option->neg_alias_arg == NULL);
431 opt_index = new_opt_index;
437 const struct cl_option *new_option = &cl_options[new_opt_index];
439 /* The new option must not be an alias itself. */
440 gcc_assert (new_option->alias_target == N_OPTS
441 || (new_option->flags & CL_SEPARATE_ALIAS));
443 if (option->neg_alias_arg)
445 gcc_assert (option->alias_arg != NULL);
446 gcc_assert (arg == NULL);
448 arg = option->alias_arg;
450 arg = option->neg_alias_arg;
453 else if (option->alias_arg)
455 gcc_assert (value == 1);
456 gcc_assert (arg == NULL);
457 arg = option->alias_arg;
460 opt_index = new_opt_index;
464 gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
466 /* Recompute what arguments are allowed. */
467 separate_arg_flag = ((option->flags & CL_SEPARATE)
468 && !((option->flags & CL_NO_DRIVER_ARG)
469 && (lang_mask & CL_DRIVER)));
470 joined_arg_flag = (option->flags & CL_JOINED) != 0;
472 if (separate_args > 1 || (option->flags & CL_SEPARATE_NARGS_MASK))
473 gcc_assert (separate_args
474 == ((option->flags & CL_SEPARATE_NARGS_MASK)
475 >> CL_SEPARATE_NARGS_SHIFT) + 1);
477 if (!(errors & CL_ERR_MISSING_ARG))
479 if (separate_arg_flag || joined_arg_flag)
481 if ((option->flags & CL_MISSING_OK) && arg == NULL)
483 gcc_assert (arg != NULL);
486 gcc_assert (arg == NULL);
489 /* Recheck for warnings and disabled options. */
490 if (option->warn_message)
492 gcc_assert (warn_message == NULL);
493 warn_message = option->warn_message;
495 if (option->flags & CL_DISABLED)
496 errors |= CL_ERR_DISABLED;
500 /* Check if this is a switch for a different front end. */
501 if (!option_ok_for_language (option, lang_mask))
502 errors |= CL_ERR_WRONG_LANG;
504 /* If the switch takes an integer, convert it. */
505 if (arg && (option->flags & CL_UINTEGER))
507 value = integral_argument (arg);
509 errors |= CL_ERR_UINT_ARG;
513 decoded->opt_index = opt_index;
515 decoded->value = value;
516 decoded->errors = errors;
517 decoded->warn_message = warn_message;
519 if (opt_index == OPT_SPECIAL_unknown)
520 gcc_assert (result == 1);
522 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
523 decoded->canonical_option_num_elements = result;
525 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
529 if (opt_index == OPT_SPECIAL_unknown)
530 decoded->canonical_option[i] = argv[i];
532 decoded->canonical_option[i] = NULL;
533 total_len += strlen (argv[i]) + 1;
536 decoded->canonical_option[i] = NULL;
538 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
540 generate_canonical_option (opt_index, arg, value, decoded);
541 if (separate_args > 1)
543 for (i = 0; i < separate_args; i++)
545 if (argv[extra_args + 1 + i] == NULL)
548 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
550 gcc_assert (result == 1 + i);
551 decoded->canonical_option_num_elements = result;
554 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
555 for (i = 0; i < result; i++)
557 size_t len = strlen (argv[i]);
559 memcpy (p, argv[i], len);
570 #ifdef TARGET_OPTION_TRANSLATE_TABLE
571 static const struct {
572 const char *const option_found;
573 const char *const replacements;
574 } target_option_translations[] =
576 TARGET_OPTION_TRANSLATE_TABLE,
581 /* Decode command-line options (ARGC and ARGV being the arguments of
582 main) into an array, setting *DECODED_OPTIONS to a pointer to that
583 array and *DECODED_OPTIONS_COUNT to the number of entries in the
584 array. The first entry in the array is always one for the program
585 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
586 flags applicable for decoding (including CL_COMMON and CL_TARGET if
587 those options should be considered applicable). Do not produce any
588 diagnostics or set state outside of these variables. */
591 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
592 unsigned int lang_mask,
593 struct cl_decoded_option **decoded_options,
594 unsigned int *decoded_options_count)
596 unsigned int n, i, target_translate_from;
597 struct cl_decoded_option *opt_array;
598 unsigned int num_decoded_options;
599 bool argv_copied = false;
601 opt_array = XNEWVEC (struct cl_decoded_option, argc);
603 opt_array[0].opt_index = OPT_SPECIAL_program_name;
604 opt_array[0].warn_message = NULL;
605 opt_array[0].arg = argv[0];
606 opt_array[0].orig_option_with_args_text = argv[0];
607 opt_array[0].canonical_option_num_elements = 1;
608 opt_array[0].canonical_option[0] = argv[0];
609 opt_array[0].canonical_option[1] = NULL;
610 opt_array[0].canonical_option[2] = NULL;
611 opt_array[0].canonical_option[3] = NULL;
612 opt_array[0].value = 1;
613 opt_array[0].errors = 0;
614 num_decoded_options = 1;
616 target_translate_from = 1;
617 for (i = 1; i < argc; i += n)
619 const char *opt = argv[i];
621 /* Interpret "-" or a non-switch as a file name. */
622 if (opt[0] != '-' || opt[1] == '\0')
624 generate_option_input_file (opt, &opt_array[num_decoded_options]);
625 num_decoded_options++;
630 if (i >= target_translate_from && (lang_mask & CL_DRIVER))
632 #ifdef TARGET_OPTION_TRANSLATE_TABLE
636 target_option_translations[tott_idx].option_found;
639 if (strcmp (target_option_translations[tott_idx].option_found,
642 unsigned int spaces = 0;
647 for (sp = target_option_translations[tott_idx].replacements;
661 int new_argc = argc + spaces;
663 argv = XRESIZEVEC (const char *, argv, new_argc + 1);
666 const char **new_argv = XNEWVEC (const char *,
668 memcpy (new_argv, argv,
669 (argc + 1) * sizeof (const char *));
673 memmove (&argv[i] + spaces, &argv[i],
674 (argc + 1 - i) * sizeof (const char *));
676 opt_array = XRESIZEVEC (struct cl_decoded_option,
680 sp = target_option_translations[tott_idx].replacements;
690 while (*np != ' ' && *np)
697 target_translate_from = i + m;
698 gcc_assert (m == spaces + 1);
705 n = decode_cmdline_option (argv + i, lang_mask,
706 &opt_array[num_decoded_options]);
707 num_decoded_options++;
712 *decoded_options = opt_array;
713 *decoded_options_count = num_decoded_options;
714 prune_options (decoded_options, decoded_options_count);
717 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
718 next one is the same as ORIG_NEXT_OPT_IDX. */
721 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
723 /* An option can be canceled by the same option or an option with
725 if (cl_options [next_opt_idx].neg_index == opt_idx)
728 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
729 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
735 /* Filter out options canceled by the ones after them. */
738 prune_options (struct cl_decoded_option **decoded_options,
739 unsigned int *decoded_options_count)
741 unsigned int old_decoded_options_count = *decoded_options_count;
742 struct cl_decoded_option *old_decoded_options = *decoded_options;
743 unsigned int new_decoded_options_count;
744 struct cl_decoded_option *new_decoded_options
745 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
747 const struct cl_option *option;
749 /* Remove arguments which are negated by others after them. */
750 new_decoded_options_count = 0;
751 for (i = 0; i < old_decoded_options_count; i++)
753 unsigned int j, opt_idx, next_opt_idx;
755 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
758 opt_idx = old_decoded_options[i].opt_index;
761 case OPT_SPECIAL_unknown:
762 case OPT_SPECIAL_ignore:
763 case OPT_SPECIAL_program_name:
764 case OPT_SPECIAL_input_file:
768 gcc_assert (opt_idx < cl_options_count);
769 option = &cl_options[opt_idx];
770 if (option->neg_index < 0)
773 /* Skip joined switches. */
774 if ((option->flags & CL_JOINED))
777 for (j = i + 1; j < old_decoded_options_count; j++)
779 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
781 next_opt_idx = old_decoded_options[j].opt_index;
782 if (next_opt_idx >= cl_options_count)
784 if (cl_options[next_opt_idx].neg_index < 0)
786 if ((cl_options[next_opt_idx].flags & CL_JOINED))
788 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
791 if (j == old_decoded_options_count)
794 new_decoded_options[new_decoded_options_count]
795 = old_decoded_options[i];
796 new_decoded_options_count++;
802 free (old_decoded_options);
803 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
805 new_decoded_options_count);
806 *decoded_options = new_decoded_options;
807 *decoded_options_count = new_decoded_options_count;
810 /* Handle option DECODED for the language indicated by LANG_MASK,
811 using the handlers in HANDLERS and setting fields in OPTS and
812 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
813 option, DK_UNSPECIFIED otherwise. GENERATED_P is true for an
814 option generated as part of processing another option or otherwise
815 generated internally, false for one explicitly passed by the user.
816 Returns false if the switch was invalid. DC is the diagnostic
817 context for options affecting diagnostics state, or NULL. */
820 handle_option (struct gcc_options *opts,
821 struct gcc_options *opts_set,
822 const struct cl_decoded_option *decoded,
823 unsigned int lang_mask, int kind,
824 const struct cl_option_handlers *handlers,
825 bool generated_p, diagnostic_context *dc)
827 size_t opt_index = decoded->opt_index;
828 const char *arg = decoded->arg;
829 int value = decoded->value;
830 const struct cl_option *option = &cl_options[opt_index];
831 void *flag_var = option_flag_var (opt_index, opts);
835 set_option (opts, (generated_p ? NULL : opts_set),
836 opt_index, value, arg, kind, dc);
838 for (i = 0; i < handlers->num_handlers; i++)
839 if (option->flags & handlers->handlers[i].mask)
841 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
842 lang_mask, kind, handlers))
845 handlers->post_handling_callback (decoded,
846 handlers->handlers[i].mask);
852 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
853 option instead of DECODED. This is used for callbacks when one
854 option implies another instead of an option being decoded from the
858 handle_generated_option (struct gcc_options *opts,
859 struct gcc_options *opts_set,
860 size_t opt_index, const char *arg, int value,
861 unsigned int lang_mask, int kind,
862 const struct cl_option_handlers *handlers,
863 diagnostic_context *dc)
865 struct cl_decoded_option decoded;
867 generate_option (opt_index, arg, value, lang_mask, &decoded);
868 return handle_option (opts, opts_set, &decoded, lang_mask, kind, handlers,
872 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
873 VALUE for a front end using LANG_MASK. This is used when the
874 compiler generates options internally. */
877 generate_option (size_t opt_index, const char *arg, int value,
878 unsigned int lang_mask, struct cl_decoded_option *decoded)
880 const struct cl_option *option = &cl_options[opt_index];
882 decoded->opt_index = opt_index;
883 decoded->warn_message = NULL;
885 decoded->value = value;
886 decoded->errors = (option_ok_for_language (option, lang_mask)
888 : CL_ERR_WRONG_LANG);
890 generate_canonical_option (opt_index, arg, value, decoded);
891 switch (decoded->canonical_option_num_elements)
894 decoded->orig_option_with_args_text = decoded->canonical_option[0];
898 decoded->orig_option_with_args_text
899 = concat (decoded->canonical_option[0], " ",
900 decoded->canonical_option[1], NULL);
908 /* Fill in *DECODED with an option for input file FILE. */
911 generate_option_input_file (const char *file,
912 struct cl_decoded_option *decoded)
914 decoded->opt_index = OPT_SPECIAL_input_file;
915 decoded->warn_message = NULL;
917 decoded->orig_option_with_args_text = file;
918 decoded->canonical_option_num_elements = 1;
919 decoded->canonical_option[0] = file;
920 decoded->canonical_option[1] = NULL;
921 decoded->canonical_option[2] = NULL;
922 decoded->canonical_option[3] = NULL;
927 /* Handle the switch DECODED for the language indicated by LANG_MASK,
928 using the handlers in *HANDLERS and setting fields in OPTS and
929 OPTS_SET and using diagnostic context DC (if not NULL) for
930 diagnostic options. */
933 read_cmdline_option (struct gcc_options *opts,
934 struct gcc_options *opts_set,
935 struct cl_decoded_option *decoded,
936 unsigned int lang_mask,
937 const struct cl_option_handlers *handlers,
938 diagnostic_context *dc)
940 const struct cl_option *option;
941 const char *opt = decoded->orig_option_with_args_text;
943 if (decoded->warn_message)
944 warning (0, decoded->warn_message, opt);
946 if (decoded->opt_index == OPT_SPECIAL_unknown)
948 if (handlers->unknown_option_callback (decoded))
949 error ("unrecognized command line option %qs", decoded->arg);
953 if (decoded->opt_index == OPT_SPECIAL_ignore)
956 option = &cl_options[decoded->opt_index];
958 if (decoded->errors & CL_ERR_DISABLED)
960 error ("command line option %qs"
961 " is not supported by this configuration", opt);
965 if (decoded->errors & CL_ERR_WRONG_LANG)
967 handlers->wrong_lang_callback (decoded, lang_mask);
971 if (decoded->errors & CL_ERR_MISSING_ARG)
973 if (option->missing_argument_error)
974 error (option->missing_argument_error, opt);
976 error ("missing argument to %qs", opt);
980 if (decoded->errors & CL_ERR_UINT_ARG)
982 error ("argument to %qs should be a non-negative integer",
987 gcc_assert (!decoded->errors);
989 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
990 handlers, false, dc))
991 error ("unrecognized command line option %qs", opt);
994 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
995 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND, using
996 diagnostic context DC if not NULL for diagnostic
1000 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1001 int opt_index, int value, const char *arg, int kind,
1002 diagnostic_context *dc)
1004 const struct cl_option *option = &cl_options[opt_index];
1005 void *flag_var = option_flag_var (opt_index, opts);
1006 void *set_flag_var = NULL;
1011 if (opts_set != NULL)
1012 set_flag_var = option_flag_var (opt_index, opts_set);
1014 switch (option->var_type)
1017 *(int *) flag_var = value;
1019 *(int *) set_flag_var = 1;
1023 *(int *) flag_var = (value
1025 : !option->var_value);
1027 *(int *) set_flag_var = 1;
1030 case CLVC_BIT_CLEAR:
1032 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1033 *(int *) flag_var |= option->var_value;
1035 *(int *) flag_var &= ~option->var_value;
1037 *(int *) set_flag_var |= option->var_value;
1041 *(const char **) flag_var = arg;
1043 *(const char **) set_flag_var = "";
1047 if ((diagnostic_t) kind != DK_UNSPECIFIED
1049 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind,
1053 /* Return the address of the flag variable for option OPT_INDEX in
1054 options structure OPTS, or NULL if there is no flag variable. */
1057 option_flag_var (int opt_index, struct gcc_options *opts)
1059 const struct cl_option *option = &cl_options[opt_index];
1061 if (option->flag_var_offset == (unsigned short) -1)
1063 return (void *)(((char *) opts) + option->flag_var_offset);