Fixed dates in ChangeLogs for my last commit - apologies
[official-gcc.git] / gcc / opts-common.c
blob078610fdb579cbbe8be2ec177c1b41e78d6aed0d
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
9 version.
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
14 for more details.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "intl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "options.h"
26 #include "diagnostic.h"
27 #include "tm.h" /* For SWITCH_TAKES_ARG, WORD_SWITCH_TAKES_ARG and
28 TARGET_OPTION_TRANSLATE_TABLE. */
30 static void prune_options (struct cl_decoded_option **, unsigned int *);
32 /* Perform a binary search to find which option the command-line INPUT
33 matches. Returns its index in the option array, and
34 OPT_SPECIAL_unknown on failure.
36 This routine is quite subtle. A normal binary search is not good
37 enough because some options can be suffixed with an argument, and
38 multiple sub-matches can occur, e.g. input of "-pedantic" matching
39 the initial substring of "-pedantic-errors".
41 A more complicated example is -gstabs. It should match "-g" with
42 an argument of "stabs". Suppose, however, that the number and list
43 of switches are such that the binary search tests "-gen-decls"
44 before having tested "-g". This doesn't match, and as "-gen-decls"
45 is less than "-gstabs", it will become the lower bound of the
46 binary search range, and "-g" will never be seen. To resolve this
47 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
48 to "-g" so that failed searches that end between "-gen-decls" and
49 the lexicographically subsequent switch know to go back and see if
50 "-g" causes a match (which it does in this example).
52 This search is done in such a way that the longest match for the
53 front end in question wins. If there is no match for the current
54 front end, the longest match for a different front end is returned
55 (or N_OPTS if none) and the caller emits an error message. */
56 size_t
57 find_opt (const char *input, int lang_mask)
59 size_t mn, mn_orig, mx, md, opt_len;
60 size_t match_wrong_lang;
61 int comp;
63 mn = 0;
64 mx = cl_options_count;
66 /* Find mn such this lexicographical inequality holds:
67 cl_options[mn] <= input < cl_options[mn + 1]. */
68 while (mx - mn > 1)
70 md = (mn + mx) / 2;
71 opt_len = cl_options[md].opt_len;
72 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
74 if (comp < 0)
75 mx = md;
76 else
77 mn = md;
80 mn_orig = mn;
82 /* This is the switch that is the best match but for a different
83 front end, or OPT_SPECIAL_unknown if there is no match at all. */
84 match_wrong_lang = OPT_SPECIAL_unknown;
86 /* Backtrace the chain of possible matches, returning the longest
87 one, if any, that fits best. With current GCC switches, this
88 loop executes at most twice. */
91 const struct cl_option *opt = &cl_options[mn];
93 /* Is the input either an exact match or a prefix that takes a
94 joined argument? */
95 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
96 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
98 /* If language is OK, return it. */
99 if (opt->flags & lang_mask)
100 return mn;
102 /* If we haven't remembered a prior match, remember this
103 one. Any prior match is necessarily better. */
104 if (match_wrong_lang == OPT_SPECIAL_unknown)
105 match_wrong_lang = mn;
108 /* Try the next possibility. This is cl_options_count if there
109 are no more. */
110 mn = opt->back_chain;
112 while (mn != cl_options_count);
114 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
116 /* Long options, starting "--", may be abbreviated if the
117 abbreviation is unambiguous. This only applies to options
118 not taking a joined argument, and abbreviations of "--option"
119 are permitted even if there is a variant "--option=". */
120 size_t mnc = mn_orig + 1;
121 size_t cmp_len = strlen (input);
122 while (mnc < cl_options_count
123 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
125 /* Option matching this abbreviation. OK if it is the first
126 match and that does not take a joined argument, or the
127 second match, taking a joined argument and with only '='
128 added to the first match; otherwise considered
129 ambiguous. */
130 if (mnc == mn_orig + 1
131 && !(cl_options[mnc].flags & CL_JOINED))
132 match_wrong_lang = mnc;
133 else if (mnc == mn_orig + 2
134 && match_wrong_lang == mn_orig + 1
135 && (cl_options[mnc].flags & CL_JOINED)
136 && (cl_options[mnc].opt_len
137 == cl_options[mn_orig + 1].opt_len + 1)
138 && strncmp (cl_options[mnc].opt_text + 1,
139 cl_options[mn_orig + 1].opt_text + 1,
140 cl_options[mn_orig + 1].opt_len) == 0)
141 ; /* OK, as long as there are no more matches. */
142 else
143 return OPT_SPECIAL_unknown;
144 mnc++;
148 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
149 return match_wrong_lang;
152 /* If ARG is a non-negative integer made up solely of digits, return its
153 value, otherwise return -1. */
156 integral_argument (const char *arg)
158 const char *p = arg;
160 while (*p && ISDIGIT (*p))
161 p++;
163 if (*p == '\0')
164 return atoi (arg);
166 return -1;
169 /* Return whether OPTION is OK for the language given by
170 LANG_MASK. */
171 static bool
172 option_ok_for_language (const struct cl_option *option,
173 unsigned int lang_mask)
175 if (!(option->flags & lang_mask))
176 return false;
177 else if ((option->flags & CL_TARGET)
178 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
179 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
180 /* Complain for target flag language mismatches if any languages
181 are specified. */
182 return false;
183 return true;
187 /* Fill in the canonical option part of *DECODED with an option
188 described by OPT_INDEX, ARG and VALUE. */
190 static void
191 generate_canonical_option (size_t opt_index, const char *arg, int value,
192 struct cl_decoded_option *decoded)
194 const struct cl_option *option = &cl_options[opt_index];
195 const char *opt_text = option->opt_text;
197 if (value == 0
198 && !(option->flags & CL_REJECT_NEGATIVE)
199 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
201 char *t = XNEWVEC (char, option->opt_len + 5);
202 t[0] = '-';
203 t[1] = opt_text[1];
204 t[2] = 'n';
205 t[3] = 'o';
206 t[4] = '-';
207 memcpy (t + 5, opt_text + 2, option->opt_len);
208 opt_text = t;
211 decoded->canonical_option[2] = NULL;
212 decoded->canonical_option[3] = NULL;
214 if (arg)
216 if ((option->flags & CL_SEPARATE)
217 && !(option->flags & CL_SEPARATE_ALIAS))
219 decoded->canonical_option[0] = opt_text;
220 decoded->canonical_option[1] = arg;
221 decoded->canonical_option_num_elements = 2;
223 else
225 gcc_assert (option->flags & CL_JOINED);
226 decoded->canonical_option[0] = concat (opt_text, arg, NULL);
227 decoded->canonical_option[1] = NULL;
228 decoded->canonical_option_num_elements = 1;
231 else
233 decoded->canonical_option[0] = opt_text;
234 decoded->canonical_option[1] = NULL;
235 decoded->canonical_option_num_elements = 1;
239 /* Structure describing mappings from options on the command line to
240 options to look up with find_opt. */
241 struct option_map
243 /* Prefix of the option on the command line. */
244 const char *opt0;
245 /* If two argv elements are considered to be merged into one option,
246 prefix for the second element, otherwise NULL. */
247 const char *opt1;
248 /* The new prefix to map to. */
249 const char *new_prefix;
250 /* Whether at least one character is needed following opt1 or opt0
251 for this mapping to be used. (--optimize= is valid for -O, but
252 --warn- is not valid for -W.) */
253 bool another_char_needed;
254 /* Whether the original option is a negated form of the option
255 resulting from this map. */
256 bool negated;
258 static const struct option_map option_map[] =
260 { "-Wno-", NULL, "-W", false, true },
261 { "-fno-", NULL, "-f", false, true },
262 { "-mno-", NULL, "-m", false, true },
263 { "--debug=", NULL, "-g", false, false },
264 { "--machine-", NULL, "-m", true, false },
265 { "--machine-no-", NULL, "-m", false, true },
266 { "--machine=", NULL, "-m", false, false },
267 { "--machine=no-", NULL, "-m", false, true },
268 { "--machine", "", "-m", false, false },
269 { "--machine", "no-", "-m", false, true },
270 { "--optimize=", NULL, "-O", false, false },
271 { "--std=", NULL, "-std=", false, false },
272 { "--std", "", "-std=", false, false },
273 { "--warn-", NULL, "-W", true, false },
274 { "--warn-no-", NULL, "-W", false, true },
275 { "--", NULL, "-f", true, false },
276 { "--no-", NULL, "-f", false, true }
279 /* Decode the switch beginning at ARGV for the language indicated by
280 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
281 the structure *DECODED. Returns the number of switches
282 consumed. */
284 static unsigned int
285 decode_cmdline_option (const char **argv, unsigned int lang_mask,
286 struct cl_decoded_option *decoded)
288 size_t opt_index;
289 const char *arg = 0;
290 int value = 1;
291 unsigned int result = 1, i, extra_args;
292 int adjust_len = 0;
293 size_t total_len;
294 char *p;
295 const struct cl_option *option;
296 int errors = 0;
297 const char *warn_message = NULL;
298 bool separate_arg_flag;
299 bool joined_arg_flag;
300 bool have_separate_arg = false;
302 extra_args = 0;
304 opt_index = find_opt (argv[0] + 1, lang_mask);
305 i = 0;
306 while (opt_index == OPT_SPECIAL_unknown
307 && i < ARRAY_SIZE (option_map))
309 const char *opt0 = option_map[i].opt0;
310 const char *opt1 = option_map[i].opt1;
311 const char *new_prefix = option_map[i].new_prefix;
312 bool another_char_needed = option_map[i].another_char_needed;
313 size_t opt0_len = strlen (opt0);
314 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
315 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
316 size_t new_prefix_len = strlen (new_prefix);
318 extra_args = (opt1 == NULL ? 0 : 1);
319 value = !option_map[i].negated;
321 if (strncmp (argv[0], opt0, opt0_len) == 0
322 && (opt1 == NULL
323 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
324 && (!another_char_needed
325 || argv[extra_args][optn_len] != 0))
327 size_t arglen = strlen (argv[extra_args]);
328 char *dup;
330 adjust_len = (int) optn_len - (int) new_prefix_len;
331 dup = XNEWVEC (char, arglen + 1 - adjust_len);
332 memcpy (dup, new_prefix, new_prefix_len);
333 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
334 arglen - optn_len + 1);
335 opt_index = find_opt (dup + 1, lang_mask);
336 free (dup);
338 i++;
341 if (opt_index == OPT_SPECIAL_unknown)
343 arg = argv[0];
344 extra_args = 0;
345 value = 1;
346 goto done;
349 option = &cl_options[opt_index];
351 /* Reject negative form of switches that don't take negatives as
352 unrecognized. */
353 if (!value && (option->flags & CL_REJECT_NEGATIVE))
355 opt_index = OPT_SPECIAL_unknown;
356 errors |= CL_ERR_NEGATIVE;
357 arg = argv[0];
358 goto done;
361 result = extra_args + 1;
362 warn_message = option->warn_message;
364 /* Check to see if the option is disabled for this configuration. */
365 if (option->flags & CL_DISABLED)
366 errors |= CL_ERR_DISABLED;
368 /* Determine whether there may be a separate argument based on
369 whether this option is being processed for the driver. */
370 separate_arg_flag = ((option->flags & CL_SEPARATE)
371 && !((option->flags & CL_NO_DRIVER_ARG)
372 && (lang_mask & CL_DRIVER)));
373 joined_arg_flag = (option->flags & CL_JOINED) != 0;
375 /* Sort out any argument the switch takes. */
376 if (joined_arg_flag)
378 /* Have arg point to the original switch. This is because
379 some code, such as disable_builtin_function, expects its
380 argument to be persistent until the program exits. */
381 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
383 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
385 if (separate_arg_flag)
387 arg = argv[extra_args + 1];
388 result = extra_args + 2;
389 if (arg == NULL)
390 result = extra_args + 1;
391 else
392 have_separate_arg = true;
394 else
395 /* Missing argument. */
396 arg = NULL;
399 else if (separate_arg_flag)
401 arg = argv[extra_args + 1];
402 result = extra_args + 2;
403 if (arg == NULL)
404 result = extra_args + 1;
405 else
406 have_separate_arg = true;
409 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
410 errors |= CL_ERR_MISSING_ARG;
412 /* Is this option an alias (or an ignored option, marked as an alias
413 of OPT_SPECIAL_ignore)? */
414 if (option->alias_target != N_OPTS
415 && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
417 size_t new_opt_index = option->alias_target;
419 if (new_opt_index == OPT_SPECIAL_ignore)
421 gcc_assert (option->alias_arg == NULL);
422 gcc_assert (option->neg_alias_arg == NULL);
423 opt_index = new_opt_index;
424 arg = NULL;
425 value = 1;
427 else
429 const struct cl_option *new_option = &cl_options[new_opt_index];
431 /* The new option must not be an alias itself. */
432 gcc_assert (new_option->alias_target == N_OPTS
433 || (new_option->flags & CL_SEPARATE_ALIAS));
435 if (option->neg_alias_arg)
437 gcc_assert (option->alias_arg != NULL);
438 gcc_assert (arg == NULL);
439 if (value)
440 arg = option->alias_arg;
441 else
442 arg = option->neg_alias_arg;
443 value = 1;
445 else if (option->alias_arg)
447 gcc_assert (value == 1);
448 gcc_assert (arg == NULL);
449 arg = option->alias_arg;
452 opt_index = new_opt_index;
453 option = new_option;
455 if (value == 0)
456 gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
458 /* Recompute what arguments are allowed. */
459 separate_arg_flag = ((option->flags & CL_SEPARATE)
460 && !((option->flags & CL_NO_DRIVER_ARG)
461 && (lang_mask & CL_DRIVER)));
462 joined_arg_flag = (option->flags & CL_JOINED) != 0;
464 if (!(errors & CL_ERR_MISSING_ARG))
466 if (separate_arg_flag || joined_arg_flag)
468 if ((option->flags & CL_MISSING_OK) && arg == NULL)
469 arg = "";
470 gcc_assert (arg != NULL);
472 else
473 gcc_assert (arg == NULL);
476 /* Recheck for warnings and disabled options. */
477 if (option->warn_message)
479 gcc_assert (warn_message == NULL);
480 warn_message = option->warn_message;
482 if (option->flags & CL_DISABLED)
483 errors |= CL_ERR_DISABLED;
487 /* Check if this is a switch for a different front end. */
488 if (!option_ok_for_language (option, lang_mask))
489 errors |= CL_ERR_WRONG_LANG;
491 /* If the switch takes an integer, convert it. */
492 if (arg && (option->flags & CL_UINTEGER))
494 value = integral_argument (arg);
495 if (value == -1)
496 errors |= CL_ERR_UINT_ARG;
499 done:
500 decoded->opt_index = opt_index;
501 decoded->arg = arg;
502 decoded->value = value;
503 decoded->errors = errors;
504 decoded->warn_message = warn_message;
506 if (opt_index == OPT_SPECIAL_unknown)
508 /* Skip the correct number of arguments for options handled
509 through specs. */
510 const char *popt = argv[0] + 1;
511 int c = *popt;
513 gcc_assert (result == 1);
514 if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
515 result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
516 else if (WORD_SWITCH_TAKES_ARG (popt))
517 result += WORD_SWITCH_TAKES_ARG (popt);
518 if (result > 1)
519 for (i = 1; i < result; i++)
520 if (argv[i] == NULL)
522 result = i;
523 break;
527 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
528 decoded->canonical_option_num_elements = result;
529 total_len = 0;
530 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
532 if (i < result)
534 if (opt_index == OPT_SPECIAL_unknown)
535 decoded->canonical_option[i] = argv[i];
536 else
537 decoded->canonical_option[i] = NULL;
538 total_len += strlen (argv[i]) + 1;
540 else
541 decoded->canonical_option[i] = NULL;
543 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
544 generate_canonical_option (opt_index, arg, value, decoded);
545 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
546 for (i = 0; i < result; i++)
548 size_t len = strlen (argv[i]);
550 memcpy (p, argv[i], len);
551 p += len;
552 if (i == result - 1)
553 *p++ = 0;
554 else
555 *p++ = ' ';
558 return result;
561 #ifdef TARGET_OPTION_TRANSLATE_TABLE
562 static const struct {
563 const char *const option_found;
564 const char *const replacements;
565 } target_option_translations[] =
567 TARGET_OPTION_TRANSLATE_TABLE,
568 { 0, 0 }
570 #endif
572 /* Decode command-line options (ARGC and ARGV being the arguments of
573 main) into an array, setting *DECODED_OPTIONS to a pointer to that
574 array and *DECODED_OPTIONS_COUNT to the number of entries in the
575 array. The first entry in the array is always one for the program
576 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
577 flags applicable for decoding (including CL_COMMON and CL_TARGET if
578 those options should be considered applicable). Do not produce any
579 diagnostics or set state outside of these variables. */
581 void
582 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
583 unsigned int lang_mask,
584 struct cl_decoded_option **decoded_options,
585 unsigned int *decoded_options_count)
587 unsigned int n, i, target_translate_from;
588 struct cl_decoded_option *opt_array;
589 unsigned int num_decoded_options;
590 bool argv_copied = false;
592 opt_array = XNEWVEC (struct cl_decoded_option, argc);
594 opt_array[0].opt_index = OPT_SPECIAL_program_name;
595 opt_array[0].warn_message = NULL;
596 opt_array[0].arg = argv[0];
597 opt_array[0].orig_option_with_args_text = argv[0];
598 opt_array[0].canonical_option_num_elements = 1;
599 opt_array[0].canonical_option[0] = argv[0];
600 opt_array[0].canonical_option[1] = NULL;
601 opt_array[0].canonical_option[2] = NULL;
602 opt_array[0].canonical_option[3] = NULL;
603 opt_array[0].value = 1;
604 opt_array[0].errors = 0;
605 num_decoded_options = 1;
607 target_translate_from = 1;
608 for (i = 1; i < argc; i += n)
610 const char *opt = argv[i];
612 /* Interpret "-" or a non-switch as a file name. */
613 if (opt[0] != '-' || opt[1] == '\0')
615 generate_option_input_file (opt, &opt_array[num_decoded_options]);
616 num_decoded_options++;
617 n = 1;
618 continue;
621 if (i >= target_translate_from && (lang_mask & CL_DRIVER))
623 #ifdef TARGET_OPTION_TRANSLATE_TABLE
624 int tott_idx;
626 for (tott_idx = 0;
627 target_option_translations[tott_idx].option_found;
628 tott_idx++)
630 if (strcmp (target_option_translations[tott_idx].option_found,
631 argv[i]) == 0)
633 unsigned int spaces = 0;
634 unsigned int m = 0;
635 const char *sp;
636 char *np;
638 for (sp = target_option_translations[tott_idx].replacements;
639 *sp; sp++)
641 if (*sp == ' ')
643 spaces++;
644 while (*sp == ' ')
645 sp++;
646 sp--;
650 if (spaces)
652 int new_argc = argc + spaces;
653 if (argv_copied)
654 argv = XRESIZEVEC (const char *, argv, new_argc + 1);
655 else
657 const char **new_argv = XNEWVEC (const char *,
658 new_argc + 1);
659 memcpy (new_argv, argv,
660 (argc + 1) * sizeof (const char *));
661 argv = new_argv;
662 argv_copied = true;
664 memmove (&argv[i] + spaces, &argv[i],
665 (argc + 1 - i) * sizeof (const char *));
666 argc = new_argc;
667 opt_array = XRESIZEVEC (struct cl_decoded_option,
668 opt_array, argc);
671 sp = target_option_translations[tott_idx].replacements;
672 np = xstrdup (sp);
674 while (1)
676 while (*np == ' ')
677 np++;
678 if (*np == 0)
679 break;
680 argv[i + m++] = np;
681 while (*np != ' ' && *np)
682 np++;
683 if (*np == 0)
684 break;
685 *np++ = 0;
688 target_translate_from = i + m;
689 gcc_assert (m == spaces + 1);
690 break;
693 #endif
696 n = decode_cmdline_option (argv + i, lang_mask,
697 &opt_array[num_decoded_options]);
698 num_decoded_options++;
701 if (argv_copied)
702 free (argv);
703 *decoded_options = opt_array;
704 *decoded_options_count = num_decoded_options;
705 prune_options (decoded_options, decoded_options_count);
708 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
709 next one is the same as ORIG_NEXT_OPT_IDX. */
711 static bool
712 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
714 /* An option can be canceled by the same option or an option with
715 Negative. */
716 if (cl_options [next_opt_idx].neg_index == opt_idx)
717 return true;
719 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
720 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
721 orig_next_opt_idx);
723 return false;
726 /* Filter out options canceled by the ones after them. */
728 static void
729 prune_options (struct cl_decoded_option **decoded_options,
730 unsigned int *decoded_options_count)
732 unsigned int old_decoded_options_count = *decoded_options_count;
733 struct cl_decoded_option *old_decoded_options = *decoded_options;
734 unsigned int new_decoded_options_count;
735 struct cl_decoded_option *new_decoded_options
736 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
737 unsigned int i;
738 const struct cl_option *option;
740 /* Remove arguments which are negated by others after them. */
741 new_decoded_options_count = 0;
742 for (i = 0; i < old_decoded_options_count; i++)
744 unsigned int j, opt_idx, next_opt_idx;
746 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
747 goto keep;
749 opt_idx = old_decoded_options[i].opt_index;
750 switch (opt_idx)
752 case OPT_SPECIAL_unknown:
753 case OPT_SPECIAL_ignore:
754 case OPT_SPECIAL_program_name:
755 case OPT_SPECIAL_input_file:
756 goto keep;
758 default:
759 gcc_assert (opt_idx < cl_options_count);
760 option = &cl_options[opt_idx];
761 if (option->neg_index < 0)
762 goto keep;
764 /* Skip joined switches. */
765 if ((option->flags & CL_JOINED))
766 goto keep;
768 for (j = i + 1; j < old_decoded_options_count; j++)
770 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
771 continue;
772 next_opt_idx = old_decoded_options[j].opt_index;
773 if (next_opt_idx >= cl_options_count)
774 continue;
775 if (cl_options[next_opt_idx].neg_index < 0)
776 continue;
777 if ((cl_options[next_opt_idx].flags & CL_JOINED))
778 continue;
779 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
780 break;
782 if (j == old_decoded_options_count)
784 keep:
785 new_decoded_options[new_decoded_options_count]
786 = old_decoded_options[i];
787 new_decoded_options_count++;
789 break;
793 free (old_decoded_options);
794 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
795 new_decoded_options,
796 new_decoded_options_count);
797 *decoded_options = new_decoded_options;
798 *decoded_options_count = new_decoded_options_count;
801 /* Handle option DECODED for the language indicated by LANG_MASK,
802 using the handlers in HANDLERS. KIND is the diagnostic_t if this
803 is a diagnostics option, DK_UNSPECIFIED otherwise. Returns false
804 if the switch was invalid. */
806 bool
807 handle_option (const struct cl_decoded_option *decoded,
808 unsigned int lang_mask, int kind,
809 const struct cl_option_handlers *handlers)
811 size_t opt_index = decoded->opt_index;
812 const char *arg = decoded->arg;
813 int value = decoded->value;
814 const struct cl_option *option = &cl_options[opt_index];
815 size_t i;
817 if (option->flag_var)
818 set_option (opt_index, value, arg, kind);
820 for (i = 0; i < handlers->num_handlers; i++)
821 if (option->flags & handlers->handlers[i].mask)
823 if (!handlers->handlers[i].handler (decoded,
824 lang_mask, kind, handlers))
825 return false;
826 else
827 handlers->post_handling_callback (decoded,
828 handlers->handlers[i].mask);
831 return true;
834 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
835 option instead of DECODED. This is used for callbacks when one
836 option implies another instead of an option being decoded from the
837 command line. */
839 bool
840 handle_generated_option (size_t opt_index, const char *arg, int value,
841 unsigned int lang_mask, int kind,
842 const struct cl_option_handlers *handlers)
844 struct cl_decoded_option decoded;
846 generate_option (opt_index, arg, value, lang_mask, &decoded);
847 return handle_option (&decoded, lang_mask, kind, handlers);
850 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
851 VALUE for a front end using LANG_MASK. This is used when the
852 compiler generates options internally. */
854 void
855 generate_option (size_t opt_index, const char *arg, int value,
856 unsigned int lang_mask, struct cl_decoded_option *decoded)
858 const struct cl_option *option = &cl_options[opt_index];
860 decoded->opt_index = opt_index;
861 decoded->warn_message = NULL;
862 decoded->arg = arg;
863 decoded->value = value;
864 decoded->errors = (option_ok_for_language (option, lang_mask)
866 : CL_ERR_WRONG_LANG);
868 generate_canonical_option (opt_index, arg, value, decoded);
869 switch (decoded->canonical_option_num_elements)
871 case 1:
872 decoded->orig_option_with_args_text = decoded->canonical_option[0];
873 break;
875 case 2:
876 decoded->orig_option_with_args_text
877 = concat (decoded->canonical_option[0], " ",
878 decoded->canonical_option[1], NULL);
879 break;
881 default:
882 gcc_unreachable ();
886 /* Fill in *DECODED with an option for input file FILE. */
888 void
889 generate_option_input_file (const char *file,
890 struct cl_decoded_option *decoded)
892 decoded->opt_index = OPT_SPECIAL_input_file;
893 decoded->warn_message = NULL;
894 decoded->arg = file;
895 decoded->orig_option_with_args_text = file;
896 decoded->canonical_option_num_elements = 1;
897 decoded->canonical_option[0] = file;
898 decoded->canonical_option[1] = NULL;
899 decoded->canonical_option[2] = NULL;
900 decoded->canonical_option[3] = NULL;
901 decoded->value = 1;
902 decoded->errors = 0;
905 /* Handle the switch DECODED for the language indicated by LANG_MASK,
906 using the handlers in *HANDLERS. */
908 void
909 read_cmdline_option (struct cl_decoded_option *decoded,
910 unsigned int lang_mask,
911 const struct cl_option_handlers *handlers)
913 const struct cl_option *option;
914 const char *opt = decoded->orig_option_with_args_text;
916 if (decoded->warn_message)
917 warning (0, decoded->warn_message, opt);
919 if (decoded->opt_index == OPT_SPECIAL_unknown)
921 if (handlers->unknown_option_callback (decoded))
922 error ("unrecognized command line option %qs", decoded->arg);
923 return;
926 if (decoded->opt_index == OPT_SPECIAL_ignore)
927 return;
929 option = &cl_options[decoded->opt_index];
931 if (decoded->errors & CL_ERR_DISABLED)
933 error ("command line option %qs"
934 " is not supported by this configuration", opt);
935 return;
938 if (decoded->errors & CL_ERR_WRONG_LANG)
940 handlers->wrong_lang_callback (decoded, lang_mask);
941 return;
944 if (decoded->errors & CL_ERR_MISSING_ARG)
946 if (option->missing_argument_error)
947 error (option->missing_argument_error, opt);
948 else
949 error ("missing argument to %qs", opt);
950 return;
953 if (decoded->errors & CL_ERR_UINT_ARG)
955 error ("argument to %qs should be a non-negative integer",
956 option->opt_text);
957 return;
960 gcc_assert (!decoded->errors);
962 if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
963 error ("unrecognized command line option %qs", opt);
966 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
967 diagnostic kind KIND. */
969 void
970 set_option (int opt_index, int value, const char *arg, int kind)
972 const struct cl_option *option = &cl_options[opt_index];
974 if (!option->flag_var)
975 return;
977 switch (option->var_type)
979 case CLVC_BOOLEAN:
980 *(int *) option->flag_var = value;
981 break;
983 case CLVC_EQUAL:
984 *(int *) option->flag_var = (value
985 ? option->var_value
986 : !option->var_value);
987 break;
989 case CLVC_BIT_CLEAR:
990 case CLVC_BIT_SET:
991 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
992 *(int *) option->flag_var |= option->var_value;
993 else
994 *(int *) option->flag_var &= ~option->var_value;
995 if (option->flag_var == &target_flags)
996 target_flags_explicit |= option->var_value;
997 break;
999 case CLVC_STRING:
1000 *(const char **) option->flag_var = arg;
1001 break;
1004 if ((diagnostic_t) kind != DK_UNSPECIFIED)
1005 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,
1006 UNKNOWN_LOCATION);