2013-12-05 Marek Polacek <polacek@redhat.com>
[official-gcc.git] / gcc / opts-common.c
blob9d186a759cb5618b98eed91c60cc4fce38b98086
1 /* Command line option handling.
2 Copyright (C) 2006-2013 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 "flags.h"
26 #include "diagnostic.h"
28 static void prune_options (struct cl_decoded_option **, unsigned int *);
30 /* Perform a binary search to find which option the command-line INPUT
31 matches. Returns its index in the option array, and
32 OPT_SPECIAL_unknown on failure.
34 This routine is quite subtle. A normal binary search is not good
35 enough because some options can be suffixed with an argument, and
36 multiple sub-matches can occur, e.g. input of "-pedantic" matching
37 the initial substring of "-pedantic-errors".
39 A more complicated example is -gstabs. It should match "-g" with
40 an argument of "stabs". Suppose, however, that the number and list
41 of switches are such that the binary search tests "-gen-decls"
42 before having tested "-g". This doesn't match, and as "-gen-decls"
43 is less than "-gstabs", it will become the lower bound of the
44 binary search range, and "-g" will never be seen. To resolve this
45 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
46 to "-g" so that failed searches that end between "-gen-decls" and
47 the lexicographically subsequent switch know to go back and see if
48 "-g" causes a match (which it does in this example).
50 This search is done in such a way that the longest match for the
51 front end in question wins. If there is no match for the current
52 front end, the longest match for a different front end is returned
53 (or N_OPTS if none) and the caller emits an error message. */
54 size_t
55 find_opt (const char *input, unsigned int lang_mask)
57 size_t mn, mn_orig, mx, md, opt_len;
58 size_t match_wrong_lang;
59 int comp;
61 mn = 0;
62 mx = cl_options_count;
64 /* Find mn such this lexicographical inequality holds:
65 cl_options[mn] <= input < cl_options[mn + 1]. */
66 while (mx - mn > 1)
68 md = (mn + mx) / 2;
69 opt_len = cl_options[md].opt_len;
70 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
72 if (comp < 0)
73 mx = md;
74 else
75 mn = md;
78 mn_orig = mn;
80 /* This is the switch that is the best match but for a different
81 front end, or OPT_SPECIAL_unknown if there is no match at all. */
82 match_wrong_lang = OPT_SPECIAL_unknown;
84 /* Backtrace the chain of possible matches, returning the longest
85 one, if any, that fits best. With current GCC switches, this
86 loop executes at most twice. */
89 const struct cl_option *opt = &cl_options[mn];
91 /* Is the input either an exact match or a prefix that takes a
92 joined argument? */
93 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
94 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
96 /* If language is OK, return it. */
97 if (opt->flags & lang_mask)
98 return mn;
100 /* If we haven't remembered a prior match, remember this
101 one. Any prior match is necessarily better. */
102 if (match_wrong_lang == OPT_SPECIAL_unknown)
103 match_wrong_lang = mn;
106 /* Try the next possibility. This is cl_options_count if there
107 are no more. */
108 mn = opt->back_chain;
110 while (mn != cl_options_count);
112 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
114 /* Long options, starting "--", may be abbreviated if the
115 abbreviation is unambiguous. This only applies to options
116 not taking a joined argument, and abbreviations of "--option"
117 are permitted even if there is a variant "--option=". */
118 size_t mnc = mn_orig + 1;
119 size_t cmp_len = strlen (input);
120 while (mnc < cl_options_count
121 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
123 /* Option matching this abbreviation. OK if it is the first
124 match and that does not take a joined argument, or the
125 second match, taking a joined argument and with only '='
126 added to the first match; otherwise considered
127 ambiguous. */
128 if (mnc == mn_orig + 1
129 && !(cl_options[mnc].flags & CL_JOINED))
130 match_wrong_lang = mnc;
131 else if (mnc == mn_orig + 2
132 && match_wrong_lang == mn_orig + 1
133 && (cl_options[mnc].flags & CL_JOINED)
134 && (cl_options[mnc].opt_len
135 == cl_options[mn_orig + 1].opt_len + 1)
136 && strncmp (cl_options[mnc].opt_text + 1,
137 cl_options[mn_orig + 1].opt_text + 1,
138 cl_options[mn_orig + 1].opt_len) == 0)
139 ; /* OK, as long as there are no more matches. */
140 else
141 return OPT_SPECIAL_unknown;
142 mnc++;
146 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
147 return match_wrong_lang;
150 /* If ARG is a non-negative integer made up solely of digits, return its
151 value, otherwise return -1. */
154 integral_argument (const char *arg)
156 const char *p = arg;
158 while (*p && ISDIGIT (*p))
159 p++;
161 if (*p == '\0')
162 return atoi (arg);
164 return -1;
167 /* Return whether OPTION is OK for the language given by
168 LANG_MASK. */
169 static bool
170 option_ok_for_language (const struct cl_option *option,
171 unsigned int lang_mask)
173 if (!(option->flags & lang_mask))
174 return false;
175 else if ((option->flags & CL_TARGET)
176 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
177 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
178 /* Complain for target flag language mismatches if any languages
179 are specified. */
180 return false;
181 return true;
184 /* Return whether ENUM_ARG is OK for the language given by
185 LANG_MASK. */
187 static bool
188 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
189 unsigned int lang_mask)
191 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
194 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
195 storing the value in *VALUE if found, and returning false without
196 modifying *VALUE if not found. */
198 static bool
199 enum_arg_to_value (const struct cl_enum_arg *enum_args,
200 const char *arg, int *value, unsigned int lang_mask)
202 unsigned int i;
204 for (i = 0; enum_args[i].arg != NULL; i++)
205 if (strcmp (arg, enum_args[i].arg) == 0
206 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
208 *value = enum_args[i].value;
209 return true;
212 return false;
215 /* Look up ARG in the enum used by option OPT_INDEX for language
216 LANG_MASK, returning true and storing the value in *VALUE if found,
217 and returning false without modifying *VALUE if not found. */
219 bool
220 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
221 unsigned int lang_mask)
223 const struct cl_option *option = &cl_options[opt_index];
225 gcc_assert (option->var_type == CLVC_ENUM);
227 return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
228 value, lang_mask);
231 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
232 corresponding string in *ARGP, returning true if the found string
233 was marked as canonical, false otherwise. If VALUE is not found
234 (which may be the case for uninitialized values if the relevant
235 option has not been passed), set *ARGP to NULL and return
236 false. */
238 bool
239 enum_value_to_arg (const struct cl_enum_arg *enum_args,
240 const char **argp, int value, unsigned int lang_mask)
242 unsigned int i;
244 for (i = 0; enum_args[i].arg != NULL; i++)
245 if (enum_args[i].value == value
246 && (enum_args[i].flags & CL_ENUM_CANONICAL)
247 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
249 *argp = enum_args[i].arg;
250 return true;
253 for (i = 0; enum_args[i].arg != NULL; i++)
254 if (enum_args[i].value == value
255 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
257 *argp = enum_args[i].arg;
258 return false;
261 *argp = NULL;
262 return false;
265 /* Fill in the canonical option part of *DECODED with an option
266 described by OPT_INDEX, ARG and VALUE. */
268 static void
269 generate_canonical_option (size_t opt_index, const char *arg, int value,
270 struct cl_decoded_option *decoded)
272 const struct cl_option *option = &cl_options[opt_index];
273 const char *opt_text = option->opt_text;
275 if (value == 0
276 && !option->cl_reject_negative
277 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
279 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
280 t[0] = '-';
281 t[1] = opt_text[1];
282 t[2] = 'n';
283 t[3] = 'o';
284 t[4] = '-';
285 memcpy (t + 5, opt_text + 2, option->opt_len);
286 opt_text = t;
289 decoded->canonical_option[2] = NULL;
290 decoded->canonical_option[3] = NULL;
292 if (arg)
294 if ((option->flags & CL_SEPARATE)
295 && !option->cl_separate_alias)
297 decoded->canonical_option[0] = opt_text;
298 decoded->canonical_option[1] = arg;
299 decoded->canonical_option_num_elements = 2;
301 else
303 gcc_assert (option->flags & CL_JOINED);
304 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
305 decoded->canonical_option[1] = NULL;
306 decoded->canonical_option_num_elements = 1;
309 else
311 decoded->canonical_option[0] = opt_text;
312 decoded->canonical_option[1] = NULL;
313 decoded->canonical_option_num_elements = 1;
317 /* Structure describing mappings from options on the command line to
318 options to look up with find_opt. */
319 struct option_map
321 /* Prefix of the option on the command line. */
322 const char *opt0;
323 /* If two argv elements are considered to be merged into one option,
324 prefix for the second element, otherwise NULL. */
325 const char *opt1;
326 /* The new prefix to map to. */
327 const char *new_prefix;
328 /* Whether at least one character is needed following opt1 or opt0
329 for this mapping to be used. (--optimize= is valid for -O, but
330 --warn- is not valid for -W.) */
331 bool another_char_needed;
332 /* Whether the original option is a negated form of the option
333 resulting from this map. */
334 bool negated;
336 static const struct option_map option_map[] =
338 { "-Wno-", NULL, "-W", false, true },
339 { "-fno-", NULL, "-f", false, true },
340 { "-mno-", NULL, "-m", false, true },
341 { "--debug=", NULL, "-g", false, false },
342 { "--machine-", NULL, "-m", true, false },
343 { "--machine-no-", NULL, "-m", false, true },
344 { "--machine=", NULL, "-m", false, false },
345 { "--machine=no-", NULL, "-m", false, true },
346 { "--machine", "", "-m", false, false },
347 { "--machine", "no-", "-m", false, true },
348 { "--optimize=", NULL, "-O", false, false },
349 { "--std=", NULL, "-std=", false, false },
350 { "--std", "", "-std=", false, false },
351 { "--warn-", NULL, "-W", true, false },
352 { "--warn-no-", NULL, "-W", false, true },
353 { "--", NULL, "-f", true, false },
354 { "--no-", NULL, "-f", false, true }
357 /* Decode the switch beginning at ARGV for the language indicated by
358 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
359 the structure *DECODED. Returns the number of switches
360 consumed. */
362 static unsigned int
363 decode_cmdline_option (const char **argv, unsigned int lang_mask,
364 struct cl_decoded_option *decoded)
366 size_t opt_index;
367 const char *arg = 0;
368 int value = 1;
369 unsigned int result = 1, i, extra_args, separate_args = 0;
370 int adjust_len = 0;
371 size_t total_len;
372 char *p;
373 const struct cl_option *option;
374 int errors = 0;
375 const char *warn_message = NULL;
376 bool separate_arg_flag;
377 bool joined_arg_flag;
378 bool have_separate_arg = false;
380 extra_args = 0;
382 opt_index = find_opt (argv[0] + 1, lang_mask);
383 i = 0;
384 while (opt_index == OPT_SPECIAL_unknown
385 && i < ARRAY_SIZE (option_map))
387 const char *opt0 = option_map[i].opt0;
388 const char *opt1 = option_map[i].opt1;
389 const char *new_prefix = option_map[i].new_prefix;
390 bool another_char_needed = option_map[i].another_char_needed;
391 size_t opt0_len = strlen (opt0);
392 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
393 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
394 size_t new_prefix_len = strlen (new_prefix);
396 extra_args = (opt1 == NULL ? 0 : 1);
397 value = !option_map[i].negated;
399 if (strncmp (argv[0], opt0, opt0_len) == 0
400 && (opt1 == NULL
401 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
402 && (!another_char_needed
403 || argv[extra_args][optn_len] != 0))
405 size_t arglen = strlen (argv[extra_args]);
406 char *dup;
408 adjust_len = (int) optn_len - (int) new_prefix_len;
409 dup = XNEWVEC (char, arglen + 1 - adjust_len);
410 memcpy (dup, new_prefix, new_prefix_len);
411 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
412 arglen - optn_len + 1);
413 opt_index = find_opt (dup + 1, lang_mask);
414 free (dup);
416 i++;
419 if (opt_index == OPT_SPECIAL_unknown)
421 arg = argv[0];
422 extra_args = 0;
423 value = 1;
424 goto done;
427 option = &cl_options[opt_index];
429 /* Reject negative form of switches that don't take negatives as
430 unrecognized. */
431 if (!value && option->cl_reject_negative)
433 opt_index = OPT_SPECIAL_unknown;
434 errors |= CL_ERR_NEGATIVE;
435 arg = argv[0];
436 goto done;
439 result = extra_args + 1;
440 warn_message = option->warn_message;
442 /* Check to see if the option is disabled for this configuration. */
443 if (option->cl_disabled)
444 errors |= CL_ERR_DISABLED;
446 /* Determine whether there may be a separate argument based on
447 whether this option is being processed for the driver, and, if
448 so, how many such arguments. */
449 separate_arg_flag = ((option->flags & CL_SEPARATE)
450 && !(option->cl_no_driver_arg
451 && (lang_mask & CL_DRIVER)));
452 separate_args = (separate_arg_flag
453 ? option->cl_separate_nargs + 1
454 : 0);
455 joined_arg_flag = (option->flags & CL_JOINED) != 0;
457 /* Sort out any argument the switch takes. */
458 if (joined_arg_flag)
460 /* Have arg point to the original switch. This is because
461 some code, such as disable_builtin_function, expects its
462 argument to be persistent until the program exits. */
463 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
465 if (*arg == '\0' && !option->cl_missing_ok)
467 if (separate_arg_flag)
469 arg = argv[extra_args + 1];
470 result = extra_args + 2;
471 if (arg == NULL)
472 result = extra_args + 1;
473 else
474 have_separate_arg = true;
476 else
477 /* Missing argument. */
478 arg = NULL;
481 else if (separate_arg_flag)
483 arg = argv[extra_args + 1];
484 for (i = 0; i < separate_args; i++)
485 if (argv[extra_args + 1 + i] == NULL)
487 errors |= CL_ERR_MISSING_ARG;
488 break;
490 result = extra_args + 1 + i;
491 if (arg != NULL)
492 have_separate_arg = true;
495 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
496 errors |= CL_ERR_MISSING_ARG;
498 /* Is this option an alias (or an ignored option, marked as an alias
499 of OPT_SPECIAL_ignore)? */
500 if (option->alias_target != N_OPTS
501 && (!option->cl_separate_alias || have_separate_arg))
503 size_t new_opt_index = option->alias_target;
505 if (new_opt_index == OPT_SPECIAL_ignore)
507 gcc_assert (option->alias_arg == NULL);
508 gcc_assert (option->neg_alias_arg == NULL);
509 opt_index = new_opt_index;
510 arg = NULL;
511 value = 1;
513 else
515 const struct cl_option *new_option = &cl_options[new_opt_index];
517 /* The new option must not be an alias itself. */
518 gcc_assert (new_option->alias_target == N_OPTS
519 || new_option->cl_separate_alias);
521 if (option->neg_alias_arg)
523 gcc_assert (option->alias_arg != NULL);
524 gcc_assert (arg == NULL);
525 gcc_assert (!option->cl_negative_alias);
526 if (value)
527 arg = option->alias_arg;
528 else
529 arg = option->neg_alias_arg;
530 value = 1;
532 else if (option->alias_arg)
534 gcc_assert (value == 1);
535 gcc_assert (arg == NULL);
536 gcc_assert (!option->cl_negative_alias);
537 arg = option->alias_arg;
540 if (option->cl_negative_alias)
541 value = !value;
543 opt_index = new_opt_index;
544 option = new_option;
546 if (value == 0)
547 gcc_assert (!option->cl_reject_negative);
549 /* Recompute what arguments are allowed. */
550 separate_arg_flag = ((option->flags & CL_SEPARATE)
551 && !(option->cl_no_driver_arg
552 && (lang_mask & CL_DRIVER)));
553 joined_arg_flag = (option->flags & CL_JOINED) != 0;
555 if (separate_args > 1 || option->cl_separate_nargs)
556 gcc_assert (separate_args
557 == (unsigned int) option->cl_separate_nargs + 1);
559 if (!(errors & CL_ERR_MISSING_ARG))
561 if (separate_arg_flag || joined_arg_flag)
563 if (option->cl_missing_ok && arg == NULL)
564 arg = "";
565 gcc_assert (arg != NULL);
567 else
568 gcc_assert (arg == NULL);
571 /* Recheck for warnings and disabled options. */
572 if (option->warn_message)
574 gcc_assert (warn_message == NULL);
575 warn_message = option->warn_message;
577 if (option->cl_disabled)
578 errors |= CL_ERR_DISABLED;
582 /* Check if this is a switch for a different front end. */
583 if (!option_ok_for_language (option, lang_mask))
584 errors |= CL_ERR_WRONG_LANG;
586 /* Convert the argument to lowercase if appropriate. */
587 if (arg && option->cl_tolower)
589 size_t j;
590 size_t len = strlen (arg);
591 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
593 for (j = 0; j < len; j++)
594 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
595 arg_lower[len] = 0;
596 arg = arg_lower;
599 /* If the switch takes an integer, convert it. */
600 if (arg && option->cl_uinteger)
602 value = integral_argument (arg);
603 if (value == -1)
604 errors |= CL_ERR_UINT_ARG;
607 /* If the switch takes an enumerated argument, convert it. */
608 if (arg && (option->var_type == CLVC_ENUM))
610 const struct cl_enum *e = &cl_enums[option->var_enum];
612 gcc_assert (value == 1);
613 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
615 const char *carg = NULL;
617 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
618 arg = carg;
619 gcc_assert (carg != NULL);
621 else
622 errors |= CL_ERR_ENUM_ARG;
625 done:
626 decoded->opt_index = opt_index;
627 decoded->arg = arg;
628 decoded->value = value;
629 decoded->errors = errors;
630 decoded->warn_message = warn_message;
632 if (opt_index == OPT_SPECIAL_unknown)
633 gcc_assert (result == 1);
635 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
636 decoded->canonical_option_num_elements = result;
637 total_len = 0;
638 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
640 if (i < result)
642 size_t len;
643 if (opt_index == OPT_SPECIAL_unknown)
644 decoded->canonical_option[i] = argv[i];
645 else
646 decoded->canonical_option[i] = NULL;
647 len = strlen (argv[i]);
648 /* If the argument is an empty string, we will print it as "" in
649 orig_option_with_args_text. */
650 total_len += (len != 0 ? len : 2) + 1;
652 else
653 decoded->canonical_option[i] = NULL;
655 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
657 generate_canonical_option (opt_index, arg, value, decoded);
658 if (separate_args > 1)
660 for (i = 0; i < separate_args; i++)
662 if (argv[extra_args + 1 + i] == NULL)
663 break;
664 else
665 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
667 gcc_assert (result == 1 + i);
668 decoded->canonical_option_num_elements = result;
671 decoded->orig_option_with_args_text
672 = p = XOBNEWVEC (&opts_obstack, char, total_len);
673 for (i = 0; i < result; i++)
675 size_t len = strlen (argv[i]);
677 /* Print the empty string verbally. */
678 if (len == 0)
680 *p++ = '"';
681 *p++ = '"';
683 else
684 memcpy (p, argv[i], len);
685 p += len;
686 if (i == result - 1)
687 *p++ = 0;
688 else
689 *p++ = ' ';
692 return result;
695 /* Obstack for option strings. */
697 struct obstack opts_obstack;
699 /* Like libiberty concat, but allocate using opts_obstack. */
701 char *
702 opts_concat (const char *first, ...)
704 char *newstr, *end;
705 size_t length = 0;
706 const char *arg;
707 va_list ap;
709 /* First compute the size of the result and get sufficient memory. */
710 va_start (ap, first);
711 for (arg = first; arg; arg = va_arg (ap, const char *))
712 length += strlen (arg);
713 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
714 va_end (ap);
716 /* Now copy the individual pieces to the result string. */
717 va_start (ap, first);
718 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
720 length = strlen (arg);
721 memcpy (end, arg, length);
722 end += length;
724 *end = '\0';
725 va_end (ap);
726 return newstr;
729 /* Decode command-line options (ARGC and ARGV being the arguments of
730 main) into an array, setting *DECODED_OPTIONS to a pointer to that
731 array and *DECODED_OPTIONS_COUNT to the number of entries in the
732 array. The first entry in the array is always one for the program
733 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
734 flags applicable for decoding (including CL_COMMON and CL_TARGET if
735 those options should be considered applicable). Do not produce any
736 diagnostics or set state outside of these variables. */
738 void
739 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
740 unsigned int lang_mask,
741 struct cl_decoded_option **decoded_options,
742 unsigned int *decoded_options_count)
744 unsigned int n, i;
745 struct cl_decoded_option *opt_array;
746 unsigned int num_decoded_options;
748 opt_array = XNEWVEC (struct cl_decoded_option, argc);
750 opt_array[0].opt_index = OPT_SPECIAL_program_name;
751 opt_array[0].warn_message = NULL;
752 opt_array[0].arg = argv[0];
753 opt_array[0].orig_option_with_args_text = argv[0];
754 opt_array[0].canonical_option_num_elements = 1;
755 opt_array[0].canonical_option[0] = argv[0];
756 opt_array[0].canonical_option[1] = NULL;
757 opt_array[0].canonical_option[2] = NULL;
758 opt_array[0].canonical_option[3] = NULL;
759 opt_array[0].value = 1;
760 opt_array[0].errors = 0;
761 num_decoded_options = 1;
763 for (i = 1; i < argc; i += n)
765 const char *opt = argv[i];
767 /* Interpret "-" or a non-switch as a file name. */
768 if (opt[0] != '-' || opt[1] == '\0')
770 generate_option_input_file (opt, &opt_array[num_decoded_options]);
771 num_decoded_options++;
772 n = 1;
773 continue;
776 n = decode_cmdline_option (argv + i, lang_mask,
777 &opt_array[num_decoded_options]);
778 num_decoded_options++;
781 *decoded_options = opt_array;
782 *decoded_options_count = num_decoded_options;
783 prune_options (decoded_options, decoded_options_count);
786 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
787 next one is the same as ORIG_NEXT_OPT_IDX. */
789 static bool
790 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
792 /* An option can be canceled by the same option or an option with
793 Negative. */
794 if (cl_options [next_opt_idx].neg_index == opt_idx)
795 return true;
797 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
798 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
799 orig_next_opt_idx);
801 return false;
804 /* Filter out options canceled by the ones after them. */
806 static void
807 prune_options (struct cl_decoded_option **decoded_options,
808 unsigned int *decoded_options_count)
810 unsigned int old_decoded_options_count = *decoded_options_count;
811 struct cl_decoded_option *old_decoded_options = *decoded_options;
812 unsigned int new_decoded_options_count;
813 struct cl_decoded_option *new_decoded_options
814 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
815 unsigned int i;
816 const struct cl_option *option;
818 /* Remove arguments which are negated by others after them. */
819 new_decoded_options_count = 0;
820 for (i = 0; i < old_decoded_options_count; i++)
822 unsigned int j, opt_idx, next_opt_idx;
824 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
825 goto keep;
827 opt_idx = old_decoded_options[i].opt_index;
828 switch (opt_idx)
830 case OPT_SPECIAL_unknown:
831 case OPT_SPECIAL_ignore:
832 case OPT_SPECIAL_program_name:
833 case OPT_SPECIAL_input_file:
834 goto keep;
836 default:
837 gcc_assert (opt_idx < cl_options_count);
838 option = &cl_options[opt_idx];
839 if (option->neg_index < 0)
840 goto keep;
842 /* Skip joined switches. */
843 if ((option->flags & CL_JOINED))
844 goto keep;
846 for (j = i + 1; j < old_decoded_options_count; j++)
848 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
849 continue;
850 next_opt_idx = old_decoded_options[j].opt_index;
851 if (next_opt_idx >= cl_options_count)
852 continue;
853 if (cl_options[next_opt_idx].neg_index < 0)
854 continue;
855 if ((cl_options[next_opt_idx].flags & CL_JOINED))
856 continue;
857 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
858 break;
860 if (j == old_decoded_options_count)
862 keep:
863 new_decoded_options[new_decoded_options_count]
864 = old_decoded_options[i];
865 new_decoded_options_count++;
867 break;
871 free (old_decoded_options);
872 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
873 new_decoded_options,
874 new_decoded_options_count);
875 *decoded_options = new_decoded_options;
876 *decoded_options_count = new_decoded_options_count;
879 /* Handle option DECODED for the language indicated by LANG_MASK,
880 using the handlers in HANDLERS and setting fields in OPTS and
881 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
882 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
883 option for options from the source file, UNKNOWN_LOCATION
884 otherwise. GENERATED_P is true for an option generated as part of
885 processing another option or otherwise generated internally, false
886 for one explicitly passed by the user. Returns false if the switch
887 was invalid. DC is the diagnostic context for options affecting
888 diagnostics state, or NULL. */
890 static bool
891 handle_option (struct gcc_options *opts,
892 struct gcc_options *opts_set,
893 const struct cl_decoded_option *decoded,
894 unsigned int lang_mask, int kind, location_t loc,
895 const struct cl_option_handlers *handlers,
896 bool generated_p, diagnostic_context *dc)
898 size_t opt_index = decoded->opt_index;
899 const char *arg = decoded->arg;
900 int value = decoded->value;
901 const struct cl_option *option = &cl_options[opt_index];
902 void *flag_var = option_flag_var (opt_index, opts);
903 size_t i;
905 if (flag_var)
906 set_option (opts, (generated_p ? NULL : opts_set),
907 opt_index, value, arg, kind, loc, dc);
909 for (i = 0; i < handlers->num_handlers; i++)
910 if (option->flags & handlers->handlers[i].mask)
912 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
913 lang_mask, kind, loc,
914 handlers, dc))
915 return false;
918 return true;
921 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
922 option instead of DECODED. This is used for callbacks when one
923 option implies another instead of an option being decoded from the
924 command line. */
926 bool
927 handle_generated_option (struct gcc_options *opts,
928 struct gcc_options *opts_set,
929 size_t opt_index, const char *arg, int value,
930 unsigned int lang_mask, int kind, location_t loc,
931 const struct cl_option_handlers *handlers,
932 diagnostic_context *dc)
934 struct cl_decoded_option decoded;
936 generate_option (opt_index, arg, value, lang_mask, &decoded);
937 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
938 handlers, true, dc);
941 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
942 VALUE for a front end using LANG_MASK. This is used when the
943 compiler generates options internally. */
945 void
946 generate_option (size_t opt_index, const char *arg, int value,
947 unsigned int lang_mask, struct cl_decoded_option *decoded)
949 const struct cl_option *option = &cl_options[opt_index];
951 decoded->opt_index = opt_index;
952 decoded->warn_message = NULL;
953 decoded->arg = arg;
954 decoded->value = value;
955 decoded->errors = (option_ok_for_language (option, lang_mask)
957 : CL_ERR_WRONG_LANG);
959 generate_canonical_option (opt_index, arg, value, decoded);
960 switch (decoded->canonical_option_num_elements)
962 case 1:
963 decoded->orig_option_with_args_text = decoded->canonical_option[0];
964 break;
966 case 2:
967 decoded->orig_option_with_args_text
968 = opts_concat (decoded->canonical_option[0], " ",
969 decoded->canonical_option[1], NULL);
970 break;
972 default:
973 gcc_unreachable ();
977 /* Fill in *DECODED with an option for input file FILE. */
979 void
980 generate_option_input_file (const char *file,
981 struct cl_decoded_option *decoded)
983 decoded->opt_index = OPT_SPECIAL_input_file;
984 decoded->warn_message = NULL;
985 decoded->arg = file;
986 decoded->orig_option_with_args_text = file;
987 decoded->canonical_option_num_elements = 1;
988 decoded->canonical_option[0] = file;
989 decoded->canonical_option[1] = NULL;
990 decoded->canonical_option[2] = NULL;
991 decoded->canonical_option[3] = NULL;
992 decoded->value = 1;
993 decoded->errors = 0;
996 /* Handle the switch DECODED (location LOC) for the language indicated
997 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
998 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
999 diagnostic options. */
1001 void
1002 read_cmdline_option (struct gcc_options *opts,
1003 struct gcc_options *opts_set,
1004 struct cl_decoded_option *decoded,
1005 location_t loc,
1006 unsigned int lang_mask,
1007 const struct cl_option_handlers *handlers,
1008 diagnostic_context *dc)
1010 const struct cl_option *option;
1011 const char *opt = decoded->orig_option_with_args_text;
1013 if (decoded->warn_message)
1014 warning_at (loc, 0, decoded->warn_message, opt);
1016 if (decoded->opt_index == OPT_SPECIAL_unknown)
1018 if (handlers->unknown_option_callback (decoded))
1019 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1020 return;
1023 if (decoded->opt_index == OPT_SPECIAL_ignore)
1024 return;
1026 option = &cl_options[decoded->opt_index];
1028 if (decoded->errors & CL_ERR_DISABLED)
1030 error_at (loc, "command line option %qs"
1031 " is not supported by this configuration", opt);
1032 return;
1035 if (decoded->errors & CL_ERR_MISSING_ARG)
1037 if (option->missing_argument_error)
1038 error_at (loc, option->missing_argument_error, opt);
1039 else
1040 error_at (loc, "missing argument to %qs", opt);
1041 return;
1044 if (decoded->errors & CL_ERR_UINT_ARG)
1046 error_at (loc, "argument to %qs should be a non-negative integer",
1047 option->opt_text);
1048 return;
1051 if (decoded->errors & CL_ERR_ENUM_ARG)
1053 const struct cl_enum *e = &cl_enums[option->var_enum];
1054 unsigned int i;
1055 size_t len;
1056 char *s, *p;
1058 if (e->unknown_error)
1059 error_at (loc, e->unknown_error, decoded->arg);
1060 else
1061 error_at (loc, "unrecognized argument in option %qs", opt);
1063 len = 0;
1064 for (i = 0; e->values[i].arg != NULL; i++)
1065 len += strlen (e->values[i].arg) + 1;
1067 s = XALLOCAVEC (char, len);
1068 p = s;
1069 for (i = 0; e->values[i].arg != NULL; i++)
1071 size_t arglen = strlen (e->values[i].arg);
1072 memcpy (p, e->values[i].arg, arglen);
1073 p[arglen] = ' ';
1074 p += arglen + 1;
1076 p[-1] = 0;
1077 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1078 return;
1081 if (decoded->errors & CL_ERR_WRONG_LANG)
1083 handlers->wrong_lang_callback (decoded, lang_mask);
1084 return;
1087 gcc_assert (!decoded->errors);
1089 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1090 loc, handlers, false, dc))
1091 error_at (loc, "unrecognized command line option %qs", opt);
1094 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1095 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1096 location LOC, using diagnostic context DC if not NULL for
1097 diagnostic classification. */
1099 void
1100 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1101 int opt_index, int value, const char *arg, int kind,
1102 location_t loc, diagnostic_context *dc)
1104 const struct cl_option *option = &cl_options[opt_index];
1105 void *flag_var = option_flag_var (opt_index, opts);
1106 void *set_flag_var = NULL;
1108 if (!flag_var)
1109 return;
1111 if (opts_set != NULL)
1112 set_flag_var = option_flag_var (opt_index, opts_set);
1114 switch (option->var_type)
1116 case CLVC_BOOLEAN:
1117 *(int *) flag_var = value;
1118 if (set_flag_var)
1119 *(int *) set_flag_var = 1;
1120 break;
1122 case CLVC_EQUAL:
1123 if (option->cl_host_wide_int)
1124 *(HOST_WIDE_INT *) flag_var = (value
1125 ? option->var_value
1126 : !option->var_value);
1127 else
1128 *(int *) flag_var = (value
1129 ? option->var_value
1130 : !option->var_value);
1131 if (set_flag_var)
1132 *(int *) set_flag_var = 1;
1133 break;
1135 case CLVC_BIT_CLEAR:
1136 case CLVC_BIT_SET:
1137 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1139 if (option->cl_host_wide_int)
1140 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1141 else
1142 *(int *) flag_var |= option->var_value;
1144 else
1146 if (option->cl_host_wide_int)
1147 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1148 else
1149 *(int *) flag_var &= ~option->var_value;
1151 if (set_flag_var)
1153 if (option->cl_host_wide_int)
1154 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1155 else
1156 *(int *) set_flag_var |= option->var_value;
1158 break;
1160 case CLVC_STRING:
1161 *(const char **) flag_var = arg;
1162 if (set_flag_var)
1163 *(const char **) set_flag_var = "";
1164 break;
1166 case CLVC_ENUM:
1168 const struct cl_enum *e = &cl_enums[option->var_enum];
1170 e->set (flag_var, value);
1171 if (set_flag_var)
1172 e->set (set_flag_var, 1);
1174 break;
1176 case CLVC_DEFER:
1178 vec<cl_deferred_option> *v
1179 = (vec<cl_deferred_option> *) *(void **) flag_var;
1180 cl_deferred_option p = {opt_index, arg, value};
1181 if (!v)
1182 v = XCNEW (vec<cl_deferred_option>);
1183 v->safe_push (p);
1184 *(void **) flag_var = v;
1185 if (set_flag_var)
1186 *(void **) set_flag_var = v;
1188 break;
1191 if ((diagnostic_t) kind != DK_UNSPECIFIED
1192 && dc != NULL)
1193 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1196 /* Return the address of the flag variable for option OPT_INDEX in
1197 options structure OPTS, or NULL if there is no flag variable. */
1199 void *
1200 option_flag_var (int opt_index, struct gcc_options *opts)
1202 const struct cl_option *option = &cl_options[opt_index];
1204 if (option->flag_var_offset == (unsigned short) -1)
1205 return NULL;
1206 return (void *)(((char *) opts) + option->flag_var_offset);
1209 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1210 or -1 if it isn't a simple on-off switch. */
1213 option_enabled (int opt_idx, void *opts)
1215 const struct cl_option *option = &(cl_options[opt_idx]);
1216 struct gcc_options *optsg = (struct gcc_options *) opts;
1217 void *flag_var = option_flag_var (opt_idx, optsg);
1219 if (flag_var)
1220 switch (option->var_type)
1222 case CLVC_BOOLEAN:
1223 return *(int *) flag_var != 0;
1225 case CLVC_EQUAL:
1226 if (option->cl_host_wide_int)
1227 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1228 else
1229 return *(int *) flag_var == option->var_value;
1231 case CLVC_BIT_CLEAR:
1232 if (option->cl_host_wide_int)
1233 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1234 else
1235 return (*(int *) flag_var & option->var_value) == 0;
1237 case CLVC_BIT_SET:
1238 if (option->cl_host_wide_int)
1239 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1240 else
1241 return (*(int *) flag_var & option->var_value) != 0;
1243 case CLVC_STRING:
1244 case CLVC_ENUM:
1245 case CLVC_DEFER:
1246 break;
1248 return -1;
1251 /* Fill STATE with the current state of option OPTION in OPTS. Return
1252 true if there is some state to store. */
1254 bool
1255 get_option_state (struct gcc_options *opts, int option,
1256 struct cl_option_state *state)
1258 void *flag_var = option_flag_var (option, opts);
1260 if (flag_var == 0)
1261 return false;
1263 switch (cl_options[option].var_type)
1265 case CLVC_BOOLEAN:
1266 case CLVC_EQUAL:
1267 state->data = flag_var;
1268 state->size = (cl_options[option].cl_host_wide_int
1269 ? sizeof (HOST_WIDE_INT)
1270 : sizeof (int));
1271 break;
1273 case CLVC_BIT_CLEAR:
1274 case CLVC_BIT_SET:
1275 state->ch = option_enabled (option, opts);
1276 state->data = &state->ch;
1277 state->size = 1;
1278 break;
1280 case CLVC_STRING:
1281 state->data = *(const char **) flag_var;
1282 if (state->data == 0)
1283 state->data = "";
1284 state->size = strlen ((const char *) state->data) + 1;
1285 break;
1287 case CLVC_ENUM:
1288 state->data = flag_var;
1289 state->size = cl_enums[cl_options[option].var_enum].var_size;
1290 break;
1292 case CLVC_DEFER:
1293 return false;
1295 return true;
1298 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1299 handlers HANDLERS) to have diagnostic kind KIND for option
1300 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1301 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1302 the warning option in question is implied at this point. This is
1303 used by -Werror= and #pragma GCC diagnostic. */
1305 void
1306 control_warning_option (unsigned int opt_index, int kind, bool imply,
1307 location_t loc, unsigned int lang_mask,
1308 const struct cl_option_handlers *handlers,
1309 struct gcc_options *opts,
1310 struct gcc_options *opts_set,
1311 diagnostic_context *dc)
1313 if (cl_options[opt_index].alias_target != N_OPTS)
1314 opt_index = cl_options[opt_index].alias_target;
1315 if (opt_index == OPT_SPECIAL_ignore)
1316 return;
1317 if (dc)
1318 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1319 if (imply)
1321 /* -Werror=foo implies -Wfoo. */
1322 if (cl_options[opt_index].var_type == CLVC_BOOLEAN)
1323 handle_generated_option (opts, opts_set,
1324 opt_index, NULL, 1, lang_mask,
1325 kind, loc, handlers, dc);