[AArch64] PR target/68129: Define TARGET_SUPPORTS_WIDE_INT
[official-gcc.git] / gcc / opts-common.c
blobd9bf4d4b897d4f2af7a581172ff0a5bd07a9ebf7
1 /* Command line option handling.
2 Copyright (C) 2006-2015 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"
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 decimal or hexadecimal integer, 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 /* It wasn't a decimal number - try hexadecimal. */
165 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
167 p = arg + 2;
168 while (*p && ISXDIGIT (*p))
169 p++;
171 if (p != arg + 2 && *p == '\0')
172 return strtol (arg, NULL, 16);
175 return -1;
178 /* Return whether OPTION is OK for the language given by
179 LANG_MASK. */
180 static bool
181 option_ok_for_language (const struct cl_option *option,
182 unsigned int lang_mask)
184 if (!(option->flags & lang_mask))
185 return false;
186 else if ((option->flags & CL_TARGET)
187 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
188 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
189 /* Complain for target flag language mismatches if any languages
190 are specified. */
191 return false;
192 return true;
195 /* Return whether ENUM_ARG is OK for the language given by
196 LANG_MASK. */
198 static bool
199 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
200 unsigned int lang_mask)
202 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
205 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
206 storing the value in *VALUE if found, and returning false without
207 modifying *VALUE if not found. */
209 static bool
210 enum_arg_to_value (const struct cl_enum_arg *enum_args,
211 const char *arg, int *value, unsigned int lang_mask)
213 unsigned int i;
215 for (i = 0; enum_args[i].arg != NULL; i++)
216 if (strcmp (arg, enum_args[i].arg) == 0
217 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
219 *value = enum_args[i].value;
220 return true;
223 return false;
226 /* Look up ARG in the enum used by option OPT_INDEX for language
227 LANG_MASK, returning true and storing the value in *VALUE if found,
228 and returning false without modifying *VALUE if not found. */
230 bool
231 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
232 unsigned int lang_mask)
234 const struct cl_option *option = &cl_options[opt_index];
236 gcc_assert (option->var_type == CLVC_ENUM);
238 return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
239 value, lang_mask);
242 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
243 corresponding string in *ARGP, returning true if the found string
244 was marked as canonical, false otherwise. If VALUE is not found
245 (which may be the case for uninitialized values if the relevant
246 option has not been passed), set *ARGP to NULL and return
247 false. */
249 bool
250 enum_value_to_arg (const struct cl_enum_arg *enum_args,
251 const char **argp, int value, unsigned int lang_mask)
253 unsigned int i;
255 for (i = 0; enum_args[i].arg != NULL; i++)
256 if (enum_args[i].value == value
257 && (enum_args[i].flags & CL_ENUM_CANONICAL)
258 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
260 *argp = enum_args[i].arg;
261 return true;
264 for (i = 0; enum_args[i].arg != NULL; i++)
265 if (enum_args[i].value == value
266 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
268 *argp = enum_args[i].arg;
269 return false;
272 *argp = NULL;
273 return false;
276 /* Fill in the canonical option part of *DECODED with an option
277 described by OPT_INDEX, ARG and VALUE. */
279 static void
280 generate_canonical_option (size_t opt_index, const char *arg, int value,
281 struct cl_decoded_option *decoded)
283 const struct cl_option *option = &cl_options[opt_index];
284 const char *opt_text = option->opt_text;
286 if (value == 0
287 && !option->cl_reject_negative
288 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
290 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
291 t[0] = '-';
292 t[1] = opt_text[1];
293 t[2] = 'n';
294 t[3] = 'o';
295 t[4] = '-';
296 memcpy (t + 5, opt_text + 2, option->opt_len);
297 opt_text = t;
300 decoded->canonical_option[2] = NULL;
301 decoded->canonical_option[3] = NULL;
303 if (arg)
305 if ((option->flags & CL_SEPARATE)
306 && !option->cl_separate_alias)
308 decoded->canonical_option[0] = opt_text;
309 decoded->canonical_option[1] = arg;
310 decoded->canonical_option_num_elements = 2;
312 else
314 gcc_assert (option->flags & CL_JOINED);
315 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
316 decoded->canonical_option[1] = NULL;
317 decoded->canonical_option_num_elements = 1;
320 else
322 decoded->canonical_option[0] = opt_text;
323 decoded->canonical_option[1] = NULL;
324 decoded->canonical_option_num_elements = 1;
328 /* Structure describing mappings from options on the command line to
329 options to look up with find_opt. */
330 struct option_map
332 /* Prefix of the option on the command line. */
333 const char *opt0;
334 /* If two argv elements are considered to be merged into one option,
335 prefix for the second element, otherwise NULL. */
336 const char *opt1;
337 /* The new prefix to map to. */
338 const char *new_prefix;
339 /* Whether at least one character is needed following opt1 or opt0
340 for this mapping to be used. (--optimize= is valid for -O, but
341 --warn- is not valid for -W.) */
342 bool another_char_needed;
343 /* Whether the original option is a negated form of the option
344 resulting from this map. */
345 bool negated;
347 static const struct option_map option_map[] =
349 { "-Wno-", NULL, "-W", false, true },
350 { "-fno-", NULL, "-f", false, true },
351 { "-mno-", NULL, "-m", false, true },
352 { "--debug=", NULL, "-g", false, false },
353 { "--machine-", NULL, "-m", true, false },
354 { "--machine-no-", NULL, "-m", false, true },
355 { "--machine=", NULL, "-m", false, false },
356 { "--machine=no-", NULL, "-m", false, true },
357 { "--machine", "", "-m", false, false },
358 { "--machine", "no-", "-m", false, true },
359 { "--optimize=", NULL, "-O", false, false },
360 { "--std=", NULL, "-std=", false, false },
361 { "--std", "", "-std=", false, false },
362 { "--warn-", NULL, "-W", true, false },
363 { "--warn-no-", NULL, "-W", false, true },
364 { "--", NULL, "-f", true, false },
365 { "--no-", NULL, "-f", false, true }
368 /* Decode the switch beginning at ARGV for the language indicated by
369 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
370 the structure *DECODED. Returns the number of switches
371 consumed. */
373 static unsigned int
374 decode_cmdline_option (const char **argv, unsigned int lang_mask,
375 struct cl_decoded_option *decoded)
377 size_t opt_index;
378 const char *arg = 0;
379 int value = 1;
380 unsigned int result = 1, i, extra_args, separate_args = 0;
381 int adjust_len = 0;
382 size_t total_len;
383 char *p;
384 const struct cl_option *option;
385 int errors = 0;
386 const char *warn_message = NULL;
387 bool separate_arg_flag;
388 bool joined_arg_flag;
389 bool have_separate_arg = false;
391 extra_args = 0;
393 opt_index = find_opt (argv[0] + 1, lang_mask);
394 i = 0;
395 while (opt_index == OPT_SPECIAL_unknown
396 && i < ARRAY_SIZE (option_map))
398 const char *opt0 = option_map[i].opt0;
399 const char *opt1 = option_map[i].opt1;
400 const char *new_prefix = option_map[i].new_prefix;
401 bool another_char_needed = option_map[i].another_char_needed;
402 size_t opt0_len = strlen (opt0);
403 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
404 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
405 size_t new_prefix_len = strlen (new_prefix);
407 extra_args = (opt1 == NULL ? 0 : 1);
408 value = !option_map[i].negated;
410 if (strncmp (argv[0], opt0, opt0_len) == 0
411 && (opt1 == NULL
412 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
413 && (!another_char_needed
414 || argv[extra_args][optn_len] != 0))
416 size_t arglen = strlen (argv[extra_args]);
417 char *dup;
419 adjust_len = (int) optn_len - (int) new_prefix_len;
420 dup = XNEWVEC (char, arglen + 1 - adjust_len);
421 memcpy (dup, new_prefix, new_prefix_len);
422 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
423 arglen - optn_len + 1);
424 opt_index = find_opt (dup + 1, lang_mask);
425 free (dup);
427 i++;
430 if (opt_index == OPT_SPECIAL_unknown)
432 arg = argv[0];
433 extra_args = 0;
434 value = 1;
435 goto done;
438 option = &cl_options[opt_index];
440 /* Reject negative form of switches that don't take negatives as
441 unrecognized. */
442 if (!value && option->cl_reject_negative)
444 opt_index = OPT_SPECIAL_unknown;
445 errors |= CL_ERR_NEGATIVE;
446 arg = argv[0];
447 goto done;
450 result = extra_args + 1;
451 warn_message = option->warn_message;
453 /* Check to see if the option is disabled for this configuration. */
454 if (option->cl_disabled)
455 errors |= CL_ERR_DISABLED;
457 /* Determine whether there may be a separate argument based on
458 whether this option is being processed for the driver, and, if
459 so, how many such arguments. */
460 separate_arg_flag = ((option->flags & CL_SEPARATE)
461 && !(option->cl_no_driver_arg
462 && (lang_mask & CL_DRIVER)));
463 separate_args = (separate_arg_flag
464 ? option->cl_separate_nargs + 1
465 : 0);
466 joined_arg_flag = (option->flags & CL_JOINED) != 0;
468 /* Sort out any argument the switch takes. */
469 if (joined_arg_flag)
471 /* Have arg point to the original switch. This is because
472 some code, such as disable_builtin_function, expects its
473 argument to be persistent until the program exits. */
474 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
476 if (*arg == '\0' && !option->cl_missing_ok)
478 if (separate_arg_flag)
480 arg = argv[extra_args + 1];
481 result = extra_args + 2;
482 if (arg == NULL)
483 result = extra_args + 1;
484 else
485 have_separate_arg = true;
487 else
488 /* Missing argument. */
489 arg = NULL;
492 else if (separate_arg_flag)
494 arg = argv[extra_args + 1];
495 for (i = 0; i < separate_args; i++)
496 if (argv[extra_args + 1 + i] == NULL)
498 errors |= CL_ERR_MISSING_ARG;
499 break;
501 result = extra_args + 1 + i;
502 if (arg != NULL)
503 have_separate_arg = true;
506 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
507 errors |= CL_ERR_MISSING_ARG;
509 /* Is this option an alias (or an ignored option, marked as an alias
510 of OPT_SPECIAL_ignore)? */
511 if (option->alias_target != N_OPTS
512 && (!option->cl_separate_alias || have_separate_arg))
514 size_t new_opt_index = option->alias_target;
516 if (new_opt_index == OPT_SPECIAL_ignore)
518 gcc_assert (option->alias_arg == NULL);
519 gcc_assert (option->neg_alias_arg == NULL);
520 opt_index = new_opt_index;
521 arg = NULL;
522 value = 1;
524 else
526 const struct cl_option *new_option = &cl_options[new_opt_index];
528 /* The new option must not be an alias itself. */
529 gcc_assert (new_option->alias_target == N_OPTS
530 || new_option->cl_separate_alias);
532 if (option->neg_alias_arg)
534 gcc_assert (option->alias_arg != NULL);
535 gcc_assert (arg == NULL);
536 gcc_assert (!option->cl_negative_alias);
537 if (value)
538 arg = option->alias_arg;
539 else
540 arg = option->neg_alias_arg;
541 value = 1;
543 else if (option->alias_arg)
545 gcc_assert (value == 1);
546 gcc_assert (arg == NULL);
547 gcc_assert (!option->cl_negative_alias);
548 arg = option->alias_arg;
551 if (option->cl_negative_alias)
552 value = !value;
554 opt_index = new_opt_index;
555 option = new_option;
557 if (value == 0)
558 gcc_assert (!option->cl_reject_negative);
560 /* Recompute what arguments are allowed. */
561 separate_arg_flag = ((option->flags & CL_SEPARATE)
562 && !(option->cl_no_driver_arg
563 && (lang_mask & CL_DRIVER)));
564 joined_arg_flag = (option->flags & CL_JOINED) != 0;
566 if (separate_args > 1 || option->cl_separate_nargs)
567 gcc_assert (separate_args
568 == (unsigned int) option->cl_separate_nargs + 1);
570 if (!(errors & CL_ERR_MISSING_ARG))
572 if (separate_arg_flag || joined_arg_flag)
574 if (option->cl_missing_ok && arg == NULL)
575 arg = "";
576 gcc_assert (arg != NULL);
578 else
579 gcc_assert (arg == NULL);
582 /* Recheck for warnings and disabled options. */
583 if (option->warn_message)
585 gcc_assert (warn_message == NULL);
586 warn_message = option->warn_message;
588 if (option->cl_disabled)
589 errors |= CL_ERR_DISABLED;
593 /* Check if this is a switch for a different front end. */
594 if (!option_ok_for_language (option, lang_mask))
595 errors |= CL_ERR_WRONG_LANG;
597 /* Convert the argument to lowercase if appropriate. */
598 if (arg && option->cl_tolower)
600 size_t j;
601 size_t len = strlen (arg);
602 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
604 for (j = 0; j < len; j++)
605 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
606 arg_lower[len] = 0;
607 arg = arg_lower;
610 /* If the switch takes an integer, convert it. */
611 if (arg && option->cl_uinteger)
613 value = integral_argument (arg);
614 if (value == -1)
615 errors |= CL_ERR_UINT_ARG;
618 /* If the switch takes an enumerated argument, convert it. */
619 if (arg && (option->var_type == CLVC_ENUM))
621 const struct cl_enum *e = &cl_enums[option->var_enum];
623 gcc_assert (value == 1);
624 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
626 const char *carg = NULL;
628 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
629 arg = carg;
630 gcc_assert (carg != NULL);
632 else
633 errors |= CL_ERR_ENUM_ARG;
636 done:
637 decoded->opt_index = opt_index;
638 decoded->arg = arg;
639 decoded->value = value;
640 decoded->errors = errors;
641 decoded->warn_message = warn_message;
643 if (opt_index == OPT_SPECIAL_unknown)
644 gcc_assert (result == 1);
646 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
647 decoded->canonical_option_num_elements = result;
648 total_len = 0;
649 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
651 if (i < result)
653 size_t len;
654 if (opt_index == OPT_SPECIAL_unknown)
655 decoded->canonical_option[i] = argv[i];
656 else
657 decoded->canonical_option[i] = NULL;
658 len = strlen (argv[i]);
659 /* If the argument is an empty string, we will print it as "" in
660 orig_option_with_args_text. */
661 total_len += (len != 0 ? len : 2) + 1;
663 else
664 decoded->canonical_option[i] = NULL;
666 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
668 generate_canonical_option (opt_index, arg, value, decoded);
669 if (separate_args > 1)
671 for (i = 0; i < separate_args; i++)
673 if (argv[extra_args + 1 + i] == NULL)
674 break;
675 else
676 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
678 gcc_assert (result == 1 + i);
679 decoded->canonical_option_num_elements = result;
682 decoded->orig_option_with_args_text
683 = p = XOBNEWVEC (&opts_obstack, char, total_len);
684 for (i = 0; i < result; i++)
686 size_t len = strlen (argv[i]);
688 /* Print the empty string verbally. */
689 if (len == 0)
691 *p++ = '"';
692 *p++ = '"';
694 else
695 memcpy (p, argv[i], len);
696 p += len;
697 if (i == result - 1)
698 *p++ = 0;
699 else
700 *p++ = ' ';
703 return result;
706 /* Obstack for option strings. */
708 struct obstack opts_obstack;
710 /* Like libiberty concat, but allocate using opts_obstack. */
712 char *
713 opts_concat (const char *first, ...)
715 char *newstr, *end;
716 size_t length = 0;
717 const char *arg;
718 va_list ap;
720 /* First compute the size of the result and get sufficient memory. */
721 va_start (ap, first);
722 for (arg = first; arg; arg = va_arg (ap, const char *))
723 length += strlen (arg);
724 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
725 va_end (ap);
727 /* Now copy the individual pieces to the result string. */
728 va_start (ap, first);
729 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
731 length = strlen (arg);
732 memcpy (end, arg, length);
733 end += length;
735 *end = '\0';
736 va_end (ap);
737 return newstr;
740 /* Decode command-line options (ARGC and ARGV being the arguments of
741 main) into an array, setting *DECODED_OPTIONS to a pointer to that
742 array and *DECODED_OPTIONS_COUNT to the number of entries in the
743 array. The first entry in the array is always one for the program
744 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
745 flags applicable for decoding (including CL_COMMON and CL_TARGET if
746 those options should be considered applicable). Do not produce any
747 diagnostics or set state outside of these variables. */
749 void
750 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
751 unsigned int lang_mask,
752 struct cl_decoded_option **decoded_options,
753 unsigned int *decoded_options_count)
755 unsigned int n, i;
756 struct cl_decoded_option *opt_array;
757 unsigned int num_decoded_options;
759 opt_array = XNEWVEC (struct cl_decoded_option, argc);
761 opt_array[0].opt_index = OPT_SPECIAL_program_name;
762 opt_array[0].warn_message = NULL;
763 opt_array[0].arg = argv[0];
764 opt_array[0].orig_option_with_args_text = argv[0];
765 opt_array[0].canonical_option_num_elements = 1;
766 opt_array[0].canonical_option[0] = argv[0];
767 opt_array[0].canonical_option[1] = NULL;
768 opt_array[0].canonical_option[2] = NULL;
769 opt_array[0].canonical_option[3] = NULL;
770 opt_array[0].value = 1;
771 opt_array[0].errors = 0;
772 num_decoded_options = 1;
774 for (i = 1; i < argc; i += n)
776 const char *opt = argv[i];
778 /* Interpret "-" or a non-switch as a file name. */
779 if (opt[0] != '-' || opt[1] == '\0')
781 generate_option_input_file (opt, &opt_array[num_decoded_options]);
782 num_decoded_options++;
783 n = 1;
784 continue;
787 n = decode_cmdline_option (argv + i, lang_mask,
788 &opt_array[num_decoded_options]);
789 num_decoded_options++;
792 *decoded_options = opt_array;
793 *decoded_options_count = num_decoded_options;
794 prune_options (decoded_options, decoded_options_count);
797 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
798 next one is the same as ORIG_NEXT_OPT_IDX. */
800 static bool
801 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
803 /* An option can be canceled by the same option or an option with
804 Negative. */
805 if (cl_options [next_opt_idx].neg_index == opt_idx)
806 return true;
808 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
809 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
810 orig_next_opt_idx);
812 return false;
815 /* Filter out options canceled by the ones after them. */
817 static void
818 prune_options (struct cl_decoded_option **decoded_options,
819 unsigned int *decoded_options_count)
821 unsigned int old_decoded_options_count = *decoded_options_count;
822 struct cl_decoded_option *old_decoded_options = *decoded_options;
823 unsigned int new_decoded_options_count;
824 struct cl_decoded_option *new_decoded_options
825 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
826 unsigned int i;
827 const struct cl_option *option;
828 unsigned int fdiagnostics_color_idx = 0;
830 /* Remove arguments which are negated by others after them. */
831 new_decoded_options_count = 0;
832 for (i = 0; i < old_decoded_options_count; i++)
834 unsigned int j, opt_idx, next_opt_idx;
836 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
837 goto keep;
839 opt_idx = old_decoded_options[i].opt_index;
840 switch (opt_idx)
842 case OPT_SPECIAL_unknown:
843 case OPT_SPECIAL_ignore:
844 case OPT_SPECIAL_program_name:
845 case OPT_SPECIAL_input_file:
846 goto keep;
848 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
849 case OPT_fdiagnostics_color_:
850 fdiagnostics_color_idx = i;
851 continue;
853 default:
854 gcc_assert (opt_idx < cl_options_count);
855 option = &cl_options[opt_idx];
856 if (option->neg_index < 0)
857 goto keep;
859 /* Skip joined switches. */
860 if ((option->flags & CL_JOINED))
861 goto keep;
863 for (j = i + 1; j < old_decoded_options_count; j++)
865 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
866 continue;
867 next_opt_idx = old_decoded_options[j].opt_index;
868 if (next_opt_idx >= cl_options_count)
869 continue;
870 if (cl_options[next_opt_idx].neg_index < 0)
871 continue;
872 if ((cl_options[next_opt_idx].flags & CL_JOINED))
873 continue;
874 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
875 break;
877 if (j == old_decoded_options_count)
879 keep:
880 new_decoded_options[new_decoded_options_count]
881 = old_decoded_options[i];
882 new_decoded_options_count++;
884 break;
888 if (fdiagnostics_color_idx > 1)
890 /* We put the last -fdiagnostics-color= at the first position
891 after argv[0] so it can take effect immediately. */
892 memmove (new_decoded_options + 2, new_decoded_options + 1,
893 sizeof (struct cl_decoded_option)
894 * (new_decoded_options_count - 1));
895 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
896 new_decoded_options_count++;
899 free (old_decoded_options);
900 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
901 new_decoded_options,
902 new_decoded_options_count);
903 *decoded_options = new_decoded_options;
904 *decoded_options_count = new_decoded_options_count;
907 /* Handle option DECODED for the language indicated by LANG_MASK,
908 using the handlers in HANDLERS and setting fields in OPTS and
909 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
910 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
911 option for options from the source file, UNKNOWN_LOCATION
912 otherwise. GENERATED_P is true for an option generated as part of
913 processing another option or otherwise generated internally, false
914 for one explicitly passed by the user. Returns false if the switch
915 was invalid. DC is the diagnostic context for options affecting
916 diagnostics state, or NULL. */
918 static bool
919 handle_option (struct gcc_options *opts,
920 struct gcc_options *opts_set,
921 const struct cl_decoded_option *decoded,
922 unsigned int lang_mask, int kind, location_t loc,
923 const struct cl_option_handlers *handlers,
924 bool generated_p, diagnostic_context *dc)
926 size_t opt_index = decoded->opt_index;
927 const char *arg = decoded->arg;
928 int value = decoded->value;
929 const struct cl_option *option = &cl_options[opt_index];
930 void *flag_var = option_flag_var (opt_index, opts);
931 size_t i;
933 if (flag_var)
934 set_option (opts, (generated_p ? NULL : opts_set),
935 opt_index, value, arg, kind, loc, dc);
937 for (i = 0; i < handlers->num_handlers; i++)
938 if (option->flags & handlers->handlers[i].mask)
940 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
941 lang_mask, kind, loc,
942 handlers, dc))
943 return false;
946 return true;
949 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
950 option instead of DECODED. This is used for callbacks when one
951 option implies another instead of an option being decoded from the
952 command line. */
954 bool
955 handle_generated_option (struct gcc_options *opts,
956 struct gcc_options *opts_set,
957 size_t opt_index, const char *arg, int value,
958 unsigned int lang_mask, int kind, location_t loc,
959 const struct cl_option_handlers *handlers,
960 diagnostic_context *dc)
962 struct cl_decoded_option decoded;
964 generate_option (opt_index, arg, value, lang_mask, &decoded);
965 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
966 handlers, true, dc);
969 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
970 VALUE for a front end using LANG_MASK. This is used when the
971 compiler generates options internally. */
973 void
974 generate_option (size_t opt_index, const char *arg, int value,
975 unsigned int lang_mask, struct cl_decoded_option *decoded)
977 const struct cl_option *option = &cl_options[opt_index];
979 decoded->opt_index = opt_index;
980 decoded->warn_message = NULL;
981 decoded->arg = arg;
982 decoded->value = value;
983 decoded->errors = (option_ok_for_language (option, lang_mask)
985 : CL_ERR_WRONG_LANG);
987 generate_canonical_option (opt_index, arg, value, decoded);
988 switch (decoded->canonical_option_num_elements)
990 case 1:
991 decoded->orig_option_with_args_text = decoded->canonical_option[0];
992 break;
994 case 2:
995 decoded->orig_option_with_args_text
996 = opts_concat (decoded->canonical_option[0], " ",
997 decoded->canonical_option[1], NULL);
998 break;
1000 default:
1001 gcc_unreachable ();
1005 /* Fill in *DECODED with an option for input file FILE. */
1007 void
1008 generate_option_input_file (const char *file,
1009 struct cl_decoded_option *decoded)
1011 decoded->opt_index = OPT_SPECIAL_input_file;
1012 decoded->warn_message = NULL;
1013 decoded->arg = file;
1014 decoded->orig_option_with_args_text = file;
1015 decoded->canonical_option_num_elements = 1;
1016 decoded->canonical_option[0] = file;
1017 decoded->canonical_option[1] = NULL;
1018 decoded->canonical_option[2] = NULL;
1019 decoded->canonical_option[3] = NULL;
1020 decoded->value = 1;
1021 decoded->errors = 0;
1024 /* Handle the switch DECODED (location LOC) for the language indicated
1025 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1026 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1027 diagnostic options. */
1029 void
1030 read_cmdline_option (struct gcc_options *opts,
1031 struct gcc_options *opts_set,
1032 struct cl_decoded_option *decoded,
1033 location_t loc,
1034 unsigned int lang_mask,
1035 const struct cl_option_handlers *handlers,
1036 diagnostic_context *dc)
1038 const struct cl_option *option;
1039 const char *opt = decoded->orig_option_with_args_text;
1041 if (decoded->warn_message)
1042 warning_at (loc, 0, decoded->warn_message, opt);
1044 if (decoded->opt_index == OPT_SPECIAL_unknown)
1046 if (handlers->unknown_option_callback (decoded))
1047 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1048 return;
1051 if (decoded->opt_index == OPT_SPECIAL_ignore)
1052 return;
1054 option = &cl_options[decoded->opt_index];
1056 if (decoded->errors & CL_ERR_DISABLED)
1058 error_at (loc, "command line option %qs"
1059 " is not supported by this configuration", opt);
1060 return;
1063 if (decoded->errors & CL_ERR_MISSING_ARG)
1065 if (option->missing_argument_error)
1066 error_at (loc, option->missing_argument_error, opt);
1067 else
1068 error_at (loc, "missing argument to %qs", opt);
1069 return;
1072 if (decoded->errors & CL_ERR_UINT_ARG)
1074 error_at (loc, "argument to %qs should be a non-negative integer",
1075 option->opt_text);
1076 return;
1079 if (decoded->errors & CL_ERR_ENUM_ARG)
1081 const struct cl_enum *e = &cl_enums[option->var_enum];
1082 unsigned int i;
1083 size_t len;
1084 char *s, *p;
1086 if (e->unknown_error)
1087 error_at (loc, e->unknown_error, decoded->arg);
1088 else
1089 error_at (loc, "unrecognized argument in option %qs", opt);
1091 len = 0;
1092 for (i = 0; e->values[i].arg != NULL; i++)
1093 len += strlen (e->values[i].arg) + 1;
1095 s = XALLOCAVEC (char, len);
1096 p = s;
1097 for (i = 0; e->values[i].arg != NULL; i++)
1099 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1100 continue;
1101 size_t arglen = strlen (e->values[i].arg);
1102 memcpy (p, e->values[i].arg, arglen);
1103 p[arglen] = ' ';
1104 p += arglen + 1;
1106 p[-1] = 0;
1107 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1108 return;
1111 if (decoded->errors & CL_ERR_WRONG_LANG)
1113 handlers->wrong_lang_callback (decoded, lang_mask);
1114 return;
1117 gcc_assert (!decoded->errors);
1119 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1120 loc, handlers, false, dc))
1121 error_at (loc, "unrecognized command line option %qs", opt);
1124 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1125 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1126 location LOC, using diagnostic context DC if not NULL for
1127 diagnostic classification. */
1129 void
1130 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1131 int opt_index, int value, const char *arg, int kind,
1132 location_t loc, diagnostic_context *dc)
1134 const struct cl_option *option = &cl_options[opt_index];
1135 void *flag_var = option_flag_var (opt_index, opts);
1136 void *set_flag_var = NULL;
1138 if (!flag_var)
1139 return;
1141 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1142 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1144 if (opts_set != NULL)
1145 set_flag_var = option_flag_var (opt_index, opts_set);
1147 switch (option->var_type)
1149 case CLVC_BOOLEAN:
1150 *(int *) flag_var = value;
1151 if (set_flag_var)
1152 *(int *) set_flag_var = 1;
1153 break;
1155 case CLVC_EQUAL:
1156 if (option->cl_host_wide_int)
1157 *(HOST_WIDE_INT *) flag_var = (value
1158 ? option->var_value
1159 : !option->var_value);
1160 else
1161 *(int *) flag_var = (value
1162 ? option->var_value
1163 : !option->var_value);
1164 if (set_flag_var)
1165 *(int *) set_flag_var = 1;
1166 break;
1168 case CLVC_BIT_CLEAR:
1169 case CLVC_BIT_SET:
1170 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1172 if (option->cl_host_wide_int)
1173 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1174 else
1175 *(int *) flag_var |= option->var_value;
1177 else
1179 if (option->cl_host_wide_int)
1180 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1181 else
1182 *(int *) flag_var &= ~option->var_value;
1184 if (set_flag_var)
1186 if (option->cl_host_wide_int)
1187 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1188 else
1189 *(int *) set_flag_var |= option->var_value;
1191 break;
1193 case CLVC_STRING:
1194 *(const char **) flag_var = arg;
1195 if (set_flag_var)
1196 *(const char **) set_flag_var = "";
1197 break;
1199 case CLVC_ENUM:
1201 const struct cl_enum *e = &cl_enums[option->var_enum];
1203 e->set (flag_var, value);
1204 if (set_flag_var)
1205 e->set (set_flag_var, 1);
1207 break;
1209 case CLVC_DEFER:
1211 vec<cl_deferred_option> *v
1212 = (vec<cl_deferred_option> *) *(void **) flag_var;
1213 cl_deferred_option p = {opt_index, arg, value};
1214 if (!v)
1215 v = XCNEW (vec<cl_deferred_option>);
1216 v->safe_push (p);
1217 *(void **) flag_var = v;
1218 if (set_flag_var)
1219 *(void **) set_flag_var = v;
1221 break;
1225 /* Return the address of the flag variable for option OPT_INDEX in
1226 options structure OPTS, or NULL if there is no flag variable. */
1228 void *
1229 option_flag_var (int opt_index, struct gcc_options *opts)
1231 const struct cl_option *option = &cl_options[opt_index];
1233 if (option->flag_var_offset == (unsigned short) -1)
1234 return NULL;
1235 return (void *)(((char *) opts) + option->flag_var_offset);
1238 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1239 or -1 if it isn't a simple on-off switch. */
1242 option_enabled (int opt_idx, void *opts)
1244 const struct cl_option *option = &(cl_options[opt_idx]);
1245 struct gcc_options *optsg = (struct gcc_options *) opts;
1246 void *flag_var = option_flag_var (opt_idx, optsg);
1248 if (flag_var)
1249 switch (option->var_type)
1251 case CLVC_BOOLEAN:
1252 return *(int *) flag_var != 0;
1254 case CLVC_EQUAL:
1255 if (option->cl_host_wide_int)
1256 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1257 else
1258 return *(int *) flag_var == option->var_value;
1260 case CLVC_BIT_CLEAR:
1261 if (option->cl_host_wide_int)
1262 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1263 else
1264 return (*(int *) flag_var & option->var_value) == 0;
1266 case CLVC_BIT_SET:
1267 if (option->cl_host_wide_int)
1268 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1269 else
1270 return (*(int *) flag_var & option->var_value) != 0;
1272 case CLVC_STRING:
1273 case CLVC_ENUM:
1274 case CLVC_DEFER:
1275 break;
1277 return -1;
1280 /* Fill STATE with the current state of option OPTION in OPTS. Return
1281 true if there is some state to store. */
1283 bool
1284 get_option_state (struct gcc_options *opts, int option,
1285 struct cl_option_state *state)
1287 void *flag_var = option_flag_var (option, opts);
1289 if (flag_var == 0)
1290 return false;
1292 switch (cl_options[option].var_type)
1294 case CLVC_BOOLEAN:
1295 case CLVC_EQUAL:
1296 state->data = flag_var;
1297 state->size = (cl_options[option].cl_host_wide_int
1298 ? sizeof (HOST_WIDE_INT)
1299 : sizeof (int));
1300 break;
1302 case CLVC_BIT_CLEAR:
1303 case CLVC_BIT_SET:
1304 state->ch = option_enabled (option, opts);
1305 state->data = &state->ch;
1306 state->size = 1;
1307 break;
1309 case CLVC_STRING:
1310 state->data = *(const char **) flag_var;
1311 if (state->data == 0)
1312 state->data = "";
1313 state->size = strlen ((const char *) state->data) + 1;
1314 break;
1316 case CLVC_ENUM:
1317 state->data = flag_var;
1318 state->size = cl_enums[cl_options[option].var_enum].var_size;
1319 break;
1321 case CLVC_DEFER:
1322 return false;
1324 return true;
1327 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1328 handlers HANDLERS) to have diagnostic kind KIND for option
1329 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1330 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1331 the warning option in question is implied at this point. This is
1332 used by -Werror= and #pragma GCC diagnostic. */
1334 void
1335 control_warning_option (unsigned int opt_index, int kind, bool imply,
1336 location_t loc, unsigned int lang_mask,
1337 const struct cl_option_handlers *handlers,
1338 struct gcc_options *opts,
1339 struct gcc_options *opts_set,
1340 diagnostic_context *dc)
1342 if (cl_options[opt_index].alias_target != N_OPTS)
1343 opt_index = cl_options[opt_index].alias_target;
1344 if (opt_index == OPT_SPECIAL_ignore)
1345 return;
1346 if (dc)
1347 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1348 if (imply)
1350 /* -Werror=foo implies -Wfoo. */
1351 if (cl_options[opt_index].var_type == CLVC_BOOLEAN)
1352 handle_generated_option (opts, opts_set,
1353 opt_index, NULL, 1, lang_mask,
1354 kind, loc, handlers, dc);