[RS6000] push_secondary_reload ICE
[official-gcc.git] / gcc / opts-common.c
blob8634b520ecf2009b64b9275798db05f1a7afe862
1 /* Command line option handling.
2 Copyright (C) 2006-2016 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 "spellcheck.h"
29 static void prune_options (struct cl_decoded_option **, unsigned int *);
31 /* Perform a binary search to find which option the command-line INPUT
32 matches. Returns its index in the option array, and
33 OPT_SPECIAL_unknown on failure.
35 This routine is quite subtle. A normal binary search is not good
36 enough because some options can be suffixed with an argument, and
37 multiple sub-matches can occur, e.g. input of "-pedantic" matching
38 the initial substring of "-pedantic-errors".
40 A more complicated example is -gstabs. It should match "-g" with
41 an argument of "stabs". Suppose, however, that the number and list
42 of switches are such that the binary search tests "-gen-decls"
43 before having tested "-g". This doesn't match, and as "-gen-decls"
44 is less than "-gstabs", it will become the lower bound of the
45 binary search range, and "-g" will never be seen. To resolve this
46 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
47 to "-g" so that failed searches that end between "-gen-decls" and
48 the lexicographically subsequent switch know to go back and see if
49 "-g" causes a match (which it does in this example).
51 This search is done in such a way that the longest match for the
52 front end in question wins. If there is no match for the current
53 front end, the longest match for a different front end is returned
54 (or N_OPTS if none) and the caller emits an error message. */
55 size_t
56 find_opt (const char *input, unsigned int lang_mask)
58 size_t mn, mn_orig, mx, md, opt_len;
59 size_t match_wrong_lang;
60 int comp;
62 mn = 0;
63 mx = cl_options_count;
65 /* Find mn such this lexicographical inequality holds:
66 cl_options[mn] <= input < cl_options[mn + 1]. */
67 while (mx - mn > 1)
69 md = (mn + mx) / 2;
70 opt_len = cl_options[md].opt_len;
71 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
73 if (comp < 0)
74 mx = md;
75 else
76 mn = md;
79 mn_orig = mn;
81 /* This is the switch that is the best match but for a different
82 front end, or OPT_SPECIAL_unknown if there is no match at all. */
83 match_wrong_lang = OPT_SPECIAL_unknown;
85 /* Backtrace the chain of possible matches, returning the longest
86 one, if any, that fits best. With current GCC switches, this
87 loop executes at most twice. */
90 const struct cl_option *opt = &cl_options[mn];
92 /* Is the input either an exact match or a prefix that takes a
93 joined argument? */
94 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
95 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
97 /* If language is OK, return it. */
98 if (opt->flags & lang_mask)
99 return mn;
101 /* If we haven't remembered a prior match, remember this
102 one. Any prior match is necessarily better. */
103 if (match_wrong_lang == OPT_SPECIAL_unknown)
104 match_wrong_lang = mn;
107 /* Try the next possibility. This is cl_options_count if there
108 are no more. */
109 mn = opt->back_chain;
111 while (mn != cl_options_count);
113 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
115 /* Long options, starting "--", may be abbreviated if the
116 abbreviation is unambiguous. This only applies to options
117 not taking a joined argument, and abbreviations of "--option"
118 are permitted even if there is a variant "--option=". */
119 size_t mnc = mn_orig + 1;
120 size_t cmp_len = strlen (input);
121 while (mnc < cl_options_count
122 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
124 /* Option matching this abbreviation. OK if it is the first
125 match and that does not take a joined argument, or the
126 second match, taking a joined argument and with only '='
127 added to the first match; otherwise considered
128 ambiguous. */
129 if (mnc == mn_orig + 1
130 && !(cl_options[mnc].flags & CL_JOINED))
131 match_wrong_lang = mnc;
132 else if (mnc == mn_orig + 2
133 && match_wrong_lang == mn_orig + 1
134 && (cl_options[mnc].flags & CL_JOINED)
135 && (cl_options[mnc].opt_len
136 == cl_options[mn_orig + 1].opt_len + 1)
137 && strncmp (cl_options[mnc].opt_text + 1,
138 cl_options[mn_orig + 1].opt_text + 1,
139 cl_options[mn_orig + 1].opt_len) == 0)
140 ; /* OK, as long as there are no more matches. */
141 else
142 return OPT_SPECIAL_unknown;
143 mnc++;
147 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
148 return match_wrong_lang;
151 /* If ARG is a non-negative decimal or hexadecimal integer, return its
152 value, otherwise return -1. */
155 integral_argument (const char *arg)
157 const char *p = arg;
159 while (*p && ISDIGIT (*p))
160 p++;
162 if (*p == '\0')
163 return atoi (arg);
165 /* It wasn't a decimal number - try hexadecimal. */
166 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
168 p = arg + 2;
169 while (*p && ISXDIGIT (*p))
170 p++;
172 if (p != arg + 2 && *p == '\0')
173 return strtol (arg, NULL, 16);
176 return -1;
179 /* Return whether OPTION is OK for the language given by
180 LANG_MASK. */
181 static bool
182 option_ok_for_language (const struct cl_option *option,
183 unsigned int lang_mask)
185 if (!(option->flags & lang_mask))
186 return false;
187 else if ((option->flags & CL_TARGET)
188 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
189 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
190 /* Complain for target flag language mismatches if any languages
191 are specified. */
192 return false;
193 return true;
196 /* Return whether ENUM_ARG is OK for the language given by
197 LANG_MASK. */
199 static bool
200 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
201 unsigned int lang_mask)
203 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
206 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
207 storing the value in *VALUE if found, and returning false without
208 modifying *VALUE if not found. */
210 static bool
211 enum_arg_to_value (const struct cl_enum_arg *enum_args,
212 const char *arg, int *value, unsigned int lang_mask)
214 unsigned int i;
216 for (i = 0; enum_args[i].arg != NULL; i++)
217 if (strcmp (arg, enum_args[i].arg) == 0
218 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
220 *value = enum_args[i].value;
221 return true;
224 return false;
227 /* Look up ARG in the enum used by option OPT_INDEX for language
228 LANG_MASK, returning true and storing the value in *VALUE if found,
229 and returning false without modifying *VALUE if not found. */
231 bool
232 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
233 unsigned int lang_mask)
235 const struct cl_option *option = &cl_options[opt_index];
237 gcc_assert (option->var_type == CLVC_ENUM);
239 return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
240 value, lang_mask);
243 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
244 corresponding string in *ARGP, returning true if the found string
245 was marked as canonical, false otherwise. If VALUE is not found
246 (which may be the case for uninitialized values if the relevant
247 option has not been passed), set *ARGP to NULL and return
248 false. */
250 bool
251 enum_value_to_arg (const struct cl_enum_arg *enum_args,
252 const char **argp, int value, unsigned int lang_mask)
254 unsigned int i;
256 for (i = 0; enum_args[i].arg != NULL; i++)
257 if (enum_args[i].value == value
258 && (enum_args[i].flags & CL_ENUM_CANONICAL)
259 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
261 *argp = enum_args[i].arg;
262 return true;
265 for (i = 0; enum_args[i].arg != NULL; i++)
266 if (enum_args[i].value == value
267 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
269 *argp = enum_args[i].arg;
270 return false;
273 *argp = NULL;
274 return false;
277 /* Fill in the canonical option part of *DECODED with an option
278 described by OPT_INDEX, ARG and VALUE. */
280 static void
281 generate_canonical_option (size_t opt_index, const char *arg, int value,
282 struct cl_decoded_option *decoded)
284 const struct cl_option *option = &cl_options[opt_index];
285 const char *opt_text = option->opt_text;
287 if (value == 0
288 && !option->cl_reject_negative
289 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
291 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
292 t[0] = '-';
293 t[1] = opt_text[1];
294 t[2] = 'n';
295 t[3] = 'o';
296 t[4] = '-';
297 memcpy (t + 5, opt_text + 2, option->opt_len);
298 opt_text = t;
301 decoded->canonical_option[2] = NULL;
302 decoded->canonical_option[3] = NULL;
304 if (arg)
306 if ((option->flags & CL_SEPARATE)
307 && !option->cl_separate_alias)
309 decoded->canonical_option[0] = opt_text;
310 decoded->canonical_option[1] = arg;
311 decoded->canonical_option_num_elements = 2;
313 else
315 gcc_assert (option->flags & CL_JOINED);
316 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
317 decoded->canonical_option[1] = NULL;
318 decoded->canonical_option_num_elements = 1;
321 else
323 decoded->canonical_option[0] = opt_text;
324 decoded->canonical_option[1] = NULL;
325 decoded->canonical_option_num_elements = 1;
329 /* Structure describing mappings from options on the command line to
330 options to look up with find_opt. */
331 struct option_map
333 /* Prefix of the option on the command line. */
334 const char *opt0;
335 /* If two argv elements are considered to be merged into one option,
336 prefix for the second element, otherwise NULL. */
337 const char *opt1;
338 /* The new prefix to map to. */
339 const char *new_prefix;
340 /* Whether at least one character is needed following opt1 or opt0
341 for this mapping to be used. (--optimize= is valid for -O, but
342 --warn- is not valid for -W.) */
343 bool another_char_needed;
344 /* Whether the original option is a negated form of the option
345 resulting from this map. */
346 bool negated;
348 static const struct option_map option_map[] =
350 { "-Wno-", NULL, "-W", false, true },
351 { "-fno-", NULL, "-f", false, true },
352 { "-mno-", NULL, "-m", false, true },
353 { "--debug=", NULL, "-g", false, false },
354 { "--machine-", NULL, "-m", true, false },
355 { "--machine-no-", NULL, "-m", false, true },
356 { "--machine=", NULL, "-m", false, false },
357 { "--machine=no-", NULL, "-m", false, true },
358 { "--machine", "", "-m", false, false },
359 { "--machine", "no-", "-m", false, true },
360 { "--optimize=", NULL, "-O", false, false },
361 { "--std=", NULL, "-std=", false, false },
362 { "--std", "", "-std=", false, false },
363 { "--warn-", NULL, "-W", true, false },
364 { "--warn-no-", NULL, "-W", false, true },
365 { "--", NULL, "-f", true, false },
366 { "--no-", NULL, "-f", false, true }
369 /* Helper function for gcc.c's driver::suggest_option, for populating the
370 vec of suggestions for misspelled options.
372 option_map above provides various prefixes for spelling command-line
373 options, which decode_cmdline_option uses to map spellings of options
374 to specific options. We want to do the reverse: to find all the ways
375 that a user could validly spell an option.
377 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
378 of its valid variant spellings to CANDIDATES, each without a leading
379 dash.
381 For example, given "-Wabi-tag", the following are added to CANDIDATES:
382 "Wabi-tag"
383 "Wno-abi-tag"
384 "-warn-abi-tag"
385 "-warn-no-abi-tag".
387 The added strings must be freed using free. */
389 void
390 add_misspelling_candidates (auto_vec<char *> *candidates,
391 const struct cl_option *option,
392 const char *opt_text)
394 gcc_assert (candidates);
395 gcc_assert (option);
396 gcc_assert (opt_text);
397 candidates->safe_push (xstrdup (opt_text + 1));
398 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
400 const char *opt0 = option_map[i].opt0;
401 const char *new_prefix = option_map[i].new_prefix;
402 size_t new_prefix_len = strlen (new_prefix);
404 if (option->cl_reject_negative && option_map[i].negated)
405 continue;
407 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
409 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
410 NULL);
411 candidates->safe_push (alternative);
416 /* Decode the switch beginning at ARGV for the language indicated by
417 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
418 the structure *DECODED. Returns the number of switches
419 consumed. */
421 static unsigned int
422 decode_cmdline_option (const char **argv, unsigned int lang_mask,
423 struct cl_decoded_option *decoded)
425 size_t opt_index;
426 const char *arg = 0;
427 int value = 1;
428 unsigned int result = 1, i, extra_args, separate_args = 0;
429 int adjust_len = 0;
430 size_t total_len;
431 char *p;
432 const struct cl_option *option;
433 int errors = 0;
434 const char *warn_message = NULL;
435 bool separate_arg_flag;
436 bool joined_arg_flag;
437 bool have_separate_arg = false;
439 extra_args = 0;
441 opt_index = find_opt (argv[0] + 1, lang_mask);
442 i = 0;
443 while (opt_index == OPT_SPECIAL_unknown
444 && i < ARRAY_SIZE (option_map))
446 const char *opt0 = option_map[i].opt0;
447 const char *opt1 = option_map[i].opt1;
448 const char *new_prefix = option_map[i].new_prefix;
449 bool another_char_needed = option_map[i].another_char_needed;
450 size_t opt0_len = strlen (opt0);
451 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
452 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
453 size_t new_prefix_len = strlen (new_prefix);
455 extra_args = (opt1 == NULL ? 0 : 1);
456 value = !option_map[i].negated;
458 if (strncmp (argv[0], opt0, opt0_len) == 0
459 && (opt1 == NULL
460 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
461 && (!another_char_needed
462 || argv[extra_args][optn_len] != 0))
464 size_t arglen = strlen (argv[extra_args]);
465 char *dup;
467 adjust_len = (int) optn_len - (int) new_prefix_len;
468 dup = XNEWVEC (char, arglen + 1 - adjust_len);
469 memcpy (dup, new_prefix, new_prefix_len);
470 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
471 arglen - optn_len + 1);
472 opt_index = find_opt (dup + 1, lang_mask);
473 free (dup);
475 i++;
478 if (opt_index == OPT_SPECIAL_unknown)
480 arg = argv[0];
481 extra_args = 0;
482 value = 1;
483 goto done;
486 option = &cl_options[opt_index];
488 /* Reject negative form of switches that don't take negatives as
489 unrecognized. */
490 if (!value && option->cl_reject_negative)
492 opt_index = OPT_SPECIAL_unknown;
493 errors |= CL_ERR_NEGATIVE;
494 arg = argv[0];
495 goto done;
498 result = extra_args + 1;
499 warn_message = option->warn_message;
501 /* Check to see if the option is disabled for this configuration. */
502 if (option->cl_disabled)
503 errors |= CL_ERR_DISABLED;
505 /* Determine whether there may be a separate argument based on
506 whether this option is being processed for the driver, and, if
507 so, how many such arguments. */
508 separate_arg_flag = ((option->flags & CL_SEPARATE)
509 && !(option->cl_no_driver_arg
510 && (lang_mask & CL_DRIVER)));
511 separate_args = (separate_arg_flag
512 ? option->cl_separate_nargs + 1
513 : 0);
514 joined_arg_flag = (option->flags & CL_JOINED) != 0;
516 /* Sort out any argument the switch takes. */
517 if (joined_arg_flag)
519 /* Have arg point to the original switch. This is because
520 some code, such as disable_builtin_function, expects its
521 argument to be persistent until the program exits. */
522 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
524 if (*arg == '\0' && !option->cl_missing_ok)
526 if (separate_arg_flag)
528 arg = argv[extra_args + 1];
529 result = extra_args + 2;
530 if (arg == NULL)
531 result = extra_args + 1;
532 else
533 have_separate_arg = true;
535 else
536 /* Missing argument. */
537 arg = NULL;
540 else if (separate_arg_flag)
542 arg = argv[extra_args + 1];
543 for (i = 0; i < separate_args; i++)
544 if (argv[extra_args + 1 + i] == NULL)
546 errors |= CL_ERR_MISSING_ARG;
547 break;
549 result = extra_args + 1 + i;
550 if (arg != NULL)
551 have_separate_arg = true;
554 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
555 errors |= CL_ERR_MISSING_ARG;
557 /* Is this option an alias (or an ignored option, marked as an alias
558 of OPT_SPECIAL_ignore)? */
559 if (option->alias_target != N_OPTS
560 && (!option->cl_separate_alias || have_separate_arg))
562 size_t new_opt_index = option->alias_target;
564 if (new_opt_index == OPT_SPECIAL_ignore)
566 gcc_assert (option->alias_arg == NULL);
567 gcc_assert (option->neg_alias_arg == NULL);
568 opt_index = new_opt_index;
569 arg = NULL;
570 value = 1;
572 else
574 const struct cl_option *new_option = &cl_options[new_opt_index];
576 /* The new option must not be an alias itself. */
577 gcc_assert (new_option->alias_target == N_OPTS
578 || new_option->cl_separate_alias);
580 if (option->neg_alias_arg)
582 gcc_assert (option->alias_arg != NULL);
583 gcc_assert (arg == NULL);
584 gcc_assert (!option->cl_negative_alias);
585 if (value)
586 arg = option->alias_arg;
587 else
588 arg = option->neg_alias_arg;
589 value = 1;
591 else if (option->alias_arg)
593 gcc_assert (value == 1);
594 gcc_assert (arg == NULL);
595 gcc_assert (!option->cl_negative_alias);
596 arg = option->alias_arg;
599 if (option->cl_negative_alias)
600 value = !value;
602 opt_index = new_opt_index;
603 option = new_option;
605 if (value == 0)
606 gcc_assert (!option->cl_reject_negative);
608 /* Recompute what arguments are allowed. */
609 separate_arg_flag = ((option->flags & CL_SEPARATE)
610 && !(option->cl_no_driver_arg
611 && (lang_mask & CL_DRIVER)));
612 joined_arg_flag = (option->flags & CL_JOINED) != 0;
614 if (separate_args > 1 || option->cl_separate_nargs)
615 gcc_assert (separate_args
616 == (unsigned int) option->cl_separate_nargs + 1);
618 if (!(errors & CL_ERR_MISSING_ARG))
620 if (separate_arg_flag || joined_arg_flag)
622 if (option->cl_missing_ok && arg == NULL)
623 arg = "";
624 gcc_assert (arg != NULL);
626 else
627 gcc_assert (arg == NULL);
630 /* Recheck for warnings and disabled options. */
631 if (option->warn_message)
633 gcc_assert (warn_message == NULL);
634 warn_message = option->warn_message;
636 if (option->cl_disabled)
637 errors |= CL_ERR_DISABLED;
641 /* Check if this is a switch for a different front end. */
642 if (!option_ok_for_language (option, lang_mask))
643 errors |= CL_ERR_WRONG_LANG;
645 /* Convert the argument to lowercase if appropriate. */
646 if (arg && option->cl_tolower)
648 size_t j;
649 size_t len = strlen (arg);
650 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
652 for (j = 0; j < len; j++)
653 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
654 arg_lower[len] = 0;
655 arg = arg_lower;
658 /* If the switch takes an integer, convert it. */
659 if (arg && option->cl_uinteger)
661 value = integral_argument (arg);
662 if (value == -1)
663 errors |= CL_ERR_UINT_ARG;
666 /* If the switch takes an enumerated argument, convert it. */
667 if (arg && (option->var_type == CLVC_ENUM))
669 const struct cl_enum *e = &cl_enums[option->var_enum];
671 gcc_assert (value == 1);
672 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
674 const char *carg = NULL;
676 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
677 arg = carg;
678 gcc_assert (carg != NULL);
680 else
681 errors |= CL_ERR_ENUM_ARG;
684 done:
685 decoded->opt_index = opt_index;
686 decoded->arg = arg;
687 decoded->value = value;
688 decoded->errors = errors;
689 decoded->warn_message = warn_message;
691 if (opt_index == OPT_SPECIAL_unknown)
692 gcc_assert (result == 1);
694 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
695 decoded->canonical_option_num_elements = result;
696 total_len = 0;
697 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
699 if (i < result)
701 size_t len;
702 if (opt_index == OPT_SPECIAL_unknown)
703 decoded->canonical_option[i] = argv[i];
704 else
705 decoded->canonical_option[i] = NULL;
706 len = strlen (argv[i]);
707 /* If the argument is an empty string, we will print it as "" in
708 orig_option_with_args_text. */
709 total_len += (len != 0 ? len : 2) + 1;
711 else
712 decoded->canonical_option[i] = NULL;
714 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
716 generate_canonical_option (opt_index, arg, value, decoded);
717 if (separate_args > 1)
719 for (i = 0; i < separate_args; i++)
721 if (argv[extra_args + 1 + i] == NULL)
722 break;
723 else
724 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
726 gcc_assert (result == 1 + i);
727 decoded->canonical_option_num_elements = result;
730 decoded->orig_option_with_args_text
731 = p = XOBNEWVEC (&opts_obstack, char, total_len);
732 for (i = 0; i < result; i++)
734 size_t len = strlen (argv[i]);
736 /* Print the empty string verbally. */
737 if (len == 0)
739 *p++ = '"';
740 *p++ = '"';
742 else
743 memcpy (p, argv[i], len);
744 p += len;
745 if (i == result - 1)
746 *p++ = 0;
747 else
748 *p++ = ' ';
751 return result;
754 /* Obstack for option strings. */
756 struct obstack opts_obstack;
758 /* Like libiberty concat, but allocate using opts_obstack. */
760 char *
761 opts_concat (const char *first, ...)
763 char *newstr, *end;
764 size_t length = 0;
765 const char *arg;
766 va_list ap;
768 /* First compute the size of the result and get sufficient memory. */
769 va_start (ap, first);
770 for (arg = first; arg; arg = va_arg (ap, const char *))
771 length += strlen (arg);
772 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
773 va_end (ap);
775 /* Now copy the individual pieces to the result string. */
776 va_start (ap, first);
777 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
779 length = strlen (arg);
780 memcpy (end, arg, length);
781 end += length;
783 *end = '\0';
784 va_end (ap);
785 return newstr;
788 /* Decode command-line options (ARGC and ARGV being the arguments of
789 main) into an array, setting *DECODED_OPTIONS to a pointer to that
790 array and *DECODED_OPTIONS_COUNT to the number of entries in the
791 array. The first entry in the array is always one for the program
792 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
793 flags applicable for decoding (including CL_COMMON and CL_TARGET if
794 those options should be considered applicable). Do not produce any
795 diagnostics or set state outside of these variables. */
797 void
798 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
799 unsigned int lang_mask,
800 struct cl_decoded_option **decoded_options,
801 unsigned int *decoded_options_count)
803 unsigned int n, i;
804 struct cl_decoded_option *opt_array;
805 unsigned int num_decoded_options;
807 opt_array = XNEWVEC (struct cl_decoded_option, argc);
809 opt_array[0].opt_index = OPT_SPECIAL_program_name;
810 opt_array[0].warn_message = NULL;
811 opt_array[0].arg = argv[0];
812 opt_array[0].orig_option_with_args_text = argv[0];
813 opt_array[0].canonical_option_num_elements = 1;
814 opt_array[0].canonical_option[0] = argv[0];
815 opt_array[0].canonical_option[1] = NULL;
816 opt_array[0].canonical_option[2] = NULL;
817 opt_array[0].canonical_option[3] = NULL;
818 opt_array[0].value = 1;
819 opt_array[0].errors = 0;
820 num_decoded_options = 1;
822 for (i = 1; i < argc; i += n)
824 const char *opt = argv[i];
826 /* Interpret "-" or a non-switch as a file name. */
827 if (opt[0] != '-' || opt[1] == '\0')
829 generate_option_input_file (opt, &opt_array[num_decoded_options]);
830 num_decoded_options++;
831 n = 1;
832 continue;
835 n = decode_cmdline_option (argv + i, lang_mask,
836 &opt_array[num_decoded_options]);
837 num_decoded_options++;
840 *decoded_options = opt_array;
841 *decoded_options_count = num_decoded_options;
842 prune_options (decoded_options, decoded_options_count);
845 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
846 next one is the same as ORIG_NEXT_OPT_IDX. */
848 static bool
849 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
851 /* An option can be canceled by the same option or an option with
852 Negative. */
853 if (cl_options [next_opt_idx].neg_index == opt_idx)
854 return true;
856 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
857 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
858 orig_next_opt_idx);
860 return false;
863 /* Filter out options canceled by the ones after them. */
865 static void
866 prune_options (struct cl_decoded_option **decoded_options,
867 unsigned int *decoded_options_count)
869 unsigned int old_decoded_options_count = *decoded_options_count;
870 struct cl_decoded_option *old_decoded_options = *decoded_options;
871 unsigned int new_decoded_options_count;
872 struct cl_decoded_option *new_decoded_options
873 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
874 unsigned int i;
875 const struct cl_option *option;
876 unsigned int fdiagnostics_color_idx = 0;
878 /* Remove arguments which are negated by others after them. */
879 new_decoded_options_count = 0;
880 for (i = 0; i < old_decoded_options_count; i++)
882 unsigned int j, opt_idx, next_opt_idx;
884 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
885 goto keep;
887 opt_idx = old_decoded_options[i].opt_index;
888 switch (opt_idx)
890 case OPT_SPECIAL_unknown:
891 case OPT_SPECIAL_ignore:
892 case OPT_SPECIAL_program_name:
893 case OPT_SPECIAL_input_file:
894 goto keep;
896 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
897 case OPT_fdiagnostics_color_:
898 fdiagnostics_color_idx = i;
899 continue;
901 default:
902 gcc_assert (opt_idx < cl_options_count);
903 option = &cl_options[opt_idx];
904 if (option->neg_index < 0)
905 goto keep;
907 /* Skip joined switches. */
908 if ((option->flags & CL_JOINED))
909 goto keep;
911 for (j = i + 1; j < old_decoded_options_count; j++)
913 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
914 continue;
915 next_opt_idx = old_decoded_options[j].opt_index;
916 if (next_opt_idx >= cl_options_count)
917 continue;
918 if (cl_options[next_opt_idx].neg_index < 0)
919 continue;
920 if ((cl_options[next_opt_idx].flags & CL_JOINED))
921 continue;
922 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
923 break;
925 if (j == old_decoded_options_count)
927 keep:
928 new_decoded_options[new_decoded_options_count]
929 = old_decoded_options[i];
930 new_decoded_options_count++;
932 break;
936 if (fdiagnostics_color_idx >= 1)
938 /* We put the last -fdiagnostics-color= at the first position
939 after argv[0] so it can take effect immediately. */
940 memmove (new_decoded_options + 2, new_decoded_options + 1,
941 sizeof (struct cl_decoded_option)
942 * (new_decoded_options_count - 1));
943 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
944 new_decoded_options_count++;
947 free (old_decoded_options);
948 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
949 new_decoded_options,
950 new_decoded_options_count);
951 *decoded_options = new_decoded_options;
952 *decoded_options_count = new_decoded_options_count;
955 /* Handle option DECODED for the language indicated by LANG_MASK,
956 using the handlers in HANDLERS and setting fields in OPTS and
957 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
958 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
959 option for options from the source file, UNKNOWN_LOCATION
960 otherwise. GENERATED_P is true for an option generated as part of
961 processing another option or otherwise generated internally, false
962 for one explicitly passed by the user. Returns false if the switch
963 was invalid. DC is the diagnostic context for options affecting
964 diagnostics state, or NULL. */
966 static bool
967 handle_option (struct gcc_options *opts,
968 struct gcc_options *opts_set,
969 const struct cl_decoded_option *decoded,
970 unsigned int lang_mask, int kind, location_t loc,
971 const struct cl_option_handlers *handlers,
972 bool generated_p, diagnostic_context *dc)
974 size_t opt_index = decoded->opt_index;
975 const char *arg = decoded->arg;
976 int value = decoded->value;
977 const struct cl_option *option = &cl_options[opt_index];
978 void *flag_var = option_flag_var (opt_index, opts);
979 size_t i;
981 if (flag_var)
982 set_option (opts, (generated_p ? NULL : opts_set),
983 opt_index, value, arg, kind, loc, dc);
985 for (i = 0; i < handlers->num_handlers; i++)
986 if (option->flags & handlers->handlers[i].mask)
988 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
989 lang_mask, kind, loc,
990 handlers, dc))
991 return false;
994 return true;
997 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
998 option instead of DECODED. This is used for callbacks when one
999 option implies another instead of an option being decoded from the
1000 command line. */
1002 bool
1003 handle_generated_option (struct gcc_options *opts,
1004 struct gcc_options *opts_set,
1005 size_t opt_index, const char *arg, int value,
1006 unsigned int lang_mask, int kind, location_t loc,
1007 const struct cl_option_handlers *handlers,
1008 diagnostic_context *dc)
1010 struct cl_decoded_option decoded;
1012 generate_option (opt_index, arg, value, lang_mask, &decoded);
1013 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1014 handlers, true, dc);
1017 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1018 VALUE for a front end using LANG_MASK. This is used when the
1019 compiler generates options internally. */
1021 void
1022 generate_option (size_t opt_index, const char *arg, int value,
1023 unsigned int lang_mask, struct cl_decoded_option *decoded)
1025 const struct cl_option *option = &cl_options[opt_index];
1027 decoded->opt_index = opt_index;
1028 decoded->warn_message = NULL;
1029 decoded->arg = arg;
1030 decoded->value = value;
1031 decoded->errors = (option_ok_for_language (option, lang_mask)
1033 : CL_ERR_WRONG_LANG);
1035 generate_canonical_option (opt_index, arg, value, decoded);
1036 switch (decoded->canonical_option_num_elements)
1038 case 1:
1039 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1040 break;
1042 case 2:
1043 decoded->orig_option_with_args_text
1044 = opts_concat (decoded->canonical_option[0], " ",
1045 decoded->canonical_option[1], NULL);
1046 break;
1048 default:
1049 gcc_unreachable ();
1053 /* Fill in *DECODED with an option for input file FILE. */
1055 void
1056 generate_option_input_file (const char *file,
1057 struct cl_decoded_option *decoded)
1059 decoded->opt_index = OPT_SPECIAL_input_file;
1060 decoded->warn_message = NULL;
1061 decoded->arg = file;
1062 decoded->orig_option_with_args_text = file;
1063 decoded->canonical_option_num_elements = 1;
1064 decoded->canonical_option[0] = file;
1065 decoded->canonical_option[1] = NULL;
1066 decoded->canonical_option[2] = NULL;
1067 decoded->canonical_option[3] = NULL;
1068 decoded->value = 1;
1069 decoded->errors = 0;
1072 /* Perform diagnostics for read_cmdline_option and control_warning_option
1073 functions. Returns true if an error has been diagnosed.
1074 LOC and LANG_MASK arguments like in read_cmdline_option.
1075 OPTION is the option to report diagnostics for, OPT the name
1076 of the option as text, ARG the argument of the option (for joined
1077 options), ERRORS is bitmask of CL_ERR_* values. */
1079 static bool
1080 cmdline_handle_error (location_t loc, const struct cl_option *option,
1081 const char *opt, const char *arg, int errors,
1082 unsigned int lang_mask)
1084 if (errors & CL_ERR_DISABLED)
1086 error_at (loc, "command line option %qs"
1087 " is not supported by this configuration", opt);
1088 return true;
1091 if (errors & CL_ERR_MISSING_ARG)
1093 if (option->missing_argument_error)
1094 error_at (loc, option->missing_argument_error, opt);
1095 else
1096 error_at (loc, "missing argument to %qs", opt);
1097 return true;
1100 if (errors & CL_ERR_UINT_ARG)
1102 error_at (loc, "argument to %qs should be a non-negative integer",
1103 option->opt_text);
1104 return true;
1107 if (errors & CL_ERR_ENUM_ARG)
1109 const struct cl_enum *e = &cl_enums[option->var_enum];
1110 unsigned int i;
1111 size_t len;
1112 char *s, *p;
1114 if (e->unknown_error)
1115 error_at (loc, e->unknown_error, arg);
1116 else
1117 error_at (loc, "unrecognized argument in option %qs", opt);
1119 len = 0;
1120 for (i = 0; e->values[i].arg != NULL; i++)
1121 len += strlen (e->values[i].arg) + 1;
1123 auto_vec <const char *> candidates;
1124 s = XALLOCAVEC (char, len);
1125 p = s;
1126 for (i = 0; e->values[i].arg != NULL; i++)
1128 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1129 continue;
1130 size_t arglen = strlen (e->values[i].arg);
1131 memcpy (p, e->values[i].arg, arglen);
1132 p[arglen] = ' ';
1133 p += arglen + 1;
1134 candidates.safe_push (e->values[i].arg);
1136 p[-1] = 0;
1137 const char *hint = find_closest_string (arg, &candidates);
1138 if (hint)
1139 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1140 option->opt_text, s, hint);
1141 else
1142 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1144 return true;
1147 return false;
1150 /* Handle the switch DECODED (location LOC) for the language indicated
1151 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1152 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1153 diagnostic options. */
1155 void
1156 read_cmdline_option (struct gcc_options *opts,
1157 struct gcc_options *opts_set,
1158 struct cl_decoded_option *decoded,
1159 location_t loc,
1160 unsigned int lang_mask,
1161 const struct cl_option_handlers *handlers,
1162 diagnostic_context *dc)
1164 const struct cl_option *option;
1165 const char *opt = decoded->orig_option_with_args_text;
1167 if (decoded->warn_message)
1168 warning_at (loc, 0, decoded->warn_message, opt);
1170 if (decoded->opt_index == OPT_SPECIAL_unknown)
1172 if (handlers->unknown_option_callback (decoded))
1173 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1174 return;
1177 if (decoded->opt_index == OPT_SPECIAL_ignore)
1178 return;
1180 option = &cl_options[decoded->opt_index];
1182 if (decoded->errors
1183 && cmdline_handle_error (loc, option, opt, decoded->arg,
1184 decoded->errors, lang_mask))
1185 return;
1187 if (decoded->errors & CL_ERR_WRONG_LANG)
1189 handlers->wrong_lang_callback (decoded, lang_mask);
1190 return;
1193 gcc_assert (!decoded->errors);
1195 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1196 loc, handlers, false, dc))
1197 error_at (loc, "unrecognized command line option %qs", opt);
1200 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1201 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1202 location LOC, using diagnostic context DC if not NULL for
1203 diagnostic classification. */
1205 void
1206 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1207 int opt_index, int value, const char *arg, int kind,
1208 location_t loc, diagnostic_context *dc)
1210 const struct cl_option *option = &cl_options[opt_index];
1211 void *flag_var = option_flag_var (opt_index, opts);
1212 void *set_flag_var = NULL;
1214 if (!flag_var)
1215 return;
1217 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1218 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1220 if (opts_set != NULL)
1221 set_flag_var = option_flag_var (opt_index, opts_set);
1223 switch (option->var_type)
1225 case CLVC_BOOLEAN:
1226 *(int *) flag_var = value;
1227 if (set_flag_var)
1228 *(int *) set_flag_var = 1;
1229 break;
1231 case CLVC_EQUAL:
1232 if (option->cl_host_wide_int)
1233 *(HOST_WIDE_INT *) flag_var = (value
1234 ? option->var_value
1235 : !option->var_value);
1236 else
1237 *(int *) flag_var = (value
1238 ? option->var_value
1239 : !option->var_value);
1240 if (set_flag_var)
1241 *(int *) set_flag_var = 1;
1242 break;
1244 case CLVC_BIT_CLEAR:
1245 case CLVC_BIT_SET:
1246 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1248 if (option->cl_host_wide_int)
1249 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1250 else
1251 *(int *) flag_var |= option->var_value;
1253 else
1255 if (option->cl_host_wide_int)
1256 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1257 else
1258 *(int *) flag_var &= ~option->var_value;
1260 if (set_flag_var)
1262 if (option->cl_host_wide_int)
1263 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1264 else
1265 *(int *) set_flag_var |= option->var_value;
1267 break;
1269 case CLVC_STRING:
1270 *(const char **) flag_var = arg;
1271 if (set_flag_var)
1272 *(const char **) set_flag_var = "";
1273 break;
1275 case CLVC_ENUM:
1277 const struct cl_enum *e = &cl_enums[option->var_enum];
1279 e->set (flag_var, value);
1280 if (set_flag_var)
1281 e->set (set_flag_var, 1);
1283 break;
1285 case CLVC_DEFER:
1287 vec<cl_deferred_option> *v
1288 = (vec<cl_deferred_option> *) *(void **) flag_var;
1289 cl_deferred_option p = {opt_index, arg, value};
1290 if (!v)
1291 v = XCNEW (vec<cl_deferred_option>);
1292 v->safe_push (p);
1293 *(void **) flag_var = v;
1294 if (set_flag_var)
1295 *(void **) set_flag_var = v;
1297 break;
1301 /* Return the address of the flag variable for option OPT_INDEX in
1302 options structure OPTS, or NULL if there is no flag variable. */
1304 void *
1305 option_flag_var (int opt_index, struct gcc_options *opts)
1307 const struct cl_option *option = &cl_options[opt_index];
1309 if (option->flag_var_offset == (unsigned short) -1)
1310 return NULL;
1311 return (void *)(((char *) opts) + option->flag_var_offset);
1314 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1315 or -1 if it isn't a simple on-off switch. */
1318 option_enabled (int opt_idx, void *opts)
1320 const struct cl_option *option = &(cl_options[opt_idx]);
1321 struct gcc_options *optsg = (struct gcc_options *) opts;
1322 void *flag_var = option_flag_var (opt_idx, optsg);
1324 if (flag_var)
1325 switch (option->var_type)
1327 case CLVC_BOOLEAN:
1328 return *(int *) flag_var != 0;
1330 case CLVC_EQUAL:
1331 if (option->cl_host_wide_int)
1332 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1333 else
1334 return *(int *) flag_var == option->var_value;
1336 case CLVC_BIT_CLEAR:
1337 if (option->cl_host_wide_int)
1338 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1339 else
1340 return (*(int *) flag_var & option->var_value) == 0;
1342 case CLVC_BIT_SET:
1343 if (option->cl_host_wide_int)
1344 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1345 else
1346 return (*(int *) flag_var & option->var_value) != 0;
1348 case CLVC_STRING:
1349 case CLVC_ENUM:
1350 case CLVC_DEFER:
1351 break;
1353 return -1;
1356 /* Fill STATE with the current state of option OPTION in OPTS. Return
1357 true if there is some state to store. */
1359 bool
1360 get_option_state (struct gcc_options *opts, int option,
1361 struct cl_option_state *state)
1363 void *flag_var = option_flag_var (option, opts);
1365 if (flag_var == 0)
1366 return false;
1368 switch (cl_options[option].var_type)
1370 case CLVC_BOOLEAN:
1371 case CLVC_EQUAL:
1372 state->data = flag_var;
1373 state->size = (cl_options[option].cl_host_wide_int
1374 ? sizeof (HOST_WIDE_INT)
1375 : sizeof (int));
1376 break;
1378 case CLVC_BIT_CLEAR:
1379 case CLVC_BIT_SET:
1380 state->ch = option_enabled (option, opts);
1381 state->data = &state->ch;
1382 state->size = 1;
1383 break;
1385 case CLVC_STRING:
1386 state->data = *(const char **) flag_var;
1387 if (state->data == 0)
1388 state->data = "";
1389 state->size = strlen ((const char *) state->data) + 1;
1390 break;
1392 case CLVC_ENUM:
1393 state->data = flag_var;
1394 state->size = cl_enums[cl_options[option].var_enum].var_size;
1395 break;
1397 case CLVC_DEFER:
1398 return false;
1400 return true;
1403 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1404 handlers HANDLERS) to have diagnostic kind KIND for option
1405 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1406 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1407 argument of the option for joined options, or NULL otherwise. If IMPLY,
1408 the warning option in question is implied at this point. This is
1409 used by -Werror= and #pragma GCC diagnostic. */
1411 void
1412 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1413 bool imply, location_t loc, unsigned int lang_mask,
1414 const struct cl_option_handlers *handlers,
1415 struct gcc_options *opts,
1416 struct gcc_options *opts_set,
1417 diagnostic_context *dc)
1419 if (cl_options[opt_index].alias_target != N_OPTS)
1421 gcc_assert (!cl_options[opt_index].cl_separate_alias
1422 && !cl_options[opt_index].cl_negative_alias);
1423 if (cl_options[opt_index].alias_arg)
1424 arg = cl_options[opt_index].alias_arg;
1425 opt_index = cl_options[opt_index].alias_target;
1427 if (opt_index == OPT_SPECIAL_ignore)
1428 return;
1429 if (dc)
1430 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1431 if (imply)
1433 const struct cl_option *option = &cl_options[opt_index];
1435 /* -Werror=foo implies -Wfoo. */
1436 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1438 int value = 1;
1440 if (arg && *arg == '\0' && !option->cl_missing_ok)
1441 arg = NULL;
1443 if ((option->flags & CL_JOINED) && arg == NULL)
1445 cmdline_handle_error (loc, option, option->opt_text, arg,
1446 CL_ERR_MISSING_ARG, lang_mask);
1447 return;
1450 /* If the switch takes an integer, convert it. */
1451 if (arg && option->cl_uinteger)
1453 value = integral_argument (arg);
1454 if (value == -1)
1456 cmdline_handle_error (loc, option, option->opt_text, arg,
1457 CL_ERR_UINT_ARG, lang_mask);
1458 return;
1462 /* If the switch takes an enumerated argument, convert it. */
1463 if (arg && option->var_type == CLVC_ENUM)
1465 const struct cl_enum *e = &cl_enums[option->var_enum];
1467 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1469 const char *carg = NULL;
1471 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1472 arg = carg;
1473 gcc_assert (carg != NULL);
1475 else
1477 cmdline_handle_error (loc, option, option->opt_text, arg,
1478 CL_ERR_ENUM_ARG, lang_mask);
1479 return;
1483 handle_generated_option (opts, opts_set,
1484 opt_index, arg, value, lang_mask,
1485 kind, loc, handlers, dc);