Daily bump.
[official-gcc.git] / gcc / opts-common.c
blob900c580019e7dc77a35a546d755c8e79ab6571ef
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"
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 /* Helper function for gcc.c's driver::suggest_option, for populating the
369 vec of suggestions for misspelled options.
371 option_map above provides various prefixes for spelling command-line
372 options, which decode_cmdline_option uses to map spellings of options
373 to specific options. We want to do the reverse: to find all the ways
374 that a user could validly spell an option.
376 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
377 of its valid variant spellings to CANDIDATES, each without a leading
378 dash.
380 For example, given "-Wabi-tag", the following are added to CANDIDATES:
381 "Wabi-tag"
382 "Wno-abi-tag"
383 "-warn-abi-tag"
384 "-warn-no-abi-tag".
386 The added strings must be freed using free. */
388 void
389 add_misspelling_candidates (auto_vec<char *> *candidates,
390 const struct cl_option *option,
391 const char *opt_text)
393 gcc_assert (candidates);
394 gcc_assert (option);
395 gcc_assert (opt_text);
396 candidates->safe_push (xstrdup (opt_text + 1));
397 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
399 const char *opt0 = option_map[i].opt0;
400 const char *new_prefix = option_map[i].new_prefix;
401 size_t new_prefix_len = strlen (new_prefix);
403 if (option->cl_reject_negative && option_map[i].negated)
404 continue;
406 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
408 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
409 NULL);
410 candidates->safe_push (alternative);
415 /* Decode the switch beginning at ARGV for the language indicated by
416 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
417 the structure *DECODED. Returns the number of switches
418 consumed. */
420 static unsigned int
421 decode_cmdline_option (const char **argv, unsigned int lang_mask,
422 struct cl_decoded_option *decoded)
424 size_t opt_index;
425 const char *arg = 0;
426 int value = 1;
427 unsigned int result = 1, i, extra_args, separate_args = 0;
428 int adjust_len = 0;
429 size_t total_len;
430 char *p;
431 const struct cl_option *option;
432 int errors = 0;
433 const char *warn_message = NULL;
434 bool separate_arg_flag;
435 bool joined_arg_flag;
436 bool have_separate_arg = false;
438 extra_args = 0;
440 opt_index = find_opt (argv[0] + 1, lang_mask);
441 i = 0;
442 while (opt_index == OPT_SPECIAL_unknown
443 && i < ARRAY_SIZE (option_map))
445 const char *opt0 = option_map[i].opt0;
446 const char *opt1 = option_map[i].opt1;
447 const char *new_prefix = option_map[i].new_prefix;
448 bool another_char_needed = option_map[i].another_char_needed;
449 size_t opt0_len = strlen (opt0);
450 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
451 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
452 size_t new_prefix_len = strlen (new_prefix);
454 extra_args = (opt1 == NULL ? 0 : 1);
455 value = !option_map[i].negated;
457 if (strncmp (argv[0], opt0, opt0_len) == 0
458 && (opt1 == NULL
459 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
460 && (!another_char_needed
461 || argv[extra_args][optn_len] != 0))
463 size_t arglen = strlen (argv[extra_args]);
464 char *dup;
466 adjust_len = (int) optn_len - (int) new_prefix_len;
467 dup = XNEWVEC (char, arglen + 1 - adjust_len);
468 memcpy (dup, new_prefix, new_prefix_len);
469 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
470 arglen - optn_len + 1);
471 opt_index = find_opt (dup + 1, lang_mask);
472 free (dup);
474 i++;
477 if (opt_index == OPT_SPECIAL_unknown)
479 arg = argv[0];
480 extra_args = 0;
481 value = 1;
482 goto done;
485 option = &cl_options[opt_index];
487 /* Reject negative form of switches that don't take negatives as
488 unrecognized. */
489 if (!value && option->cl_reject_negative)
491 opt_index = OPT_SPECIAL_unknown;
492 errors |= CL_ERR_NEGATIVE;
493 arg = argv[0];
494 goto done;
497 result = extra_args + 1;
498 warn_message = option->warn_message;
500 /* Check to see if the option is disabled for this configuration. */
501 if (option->cl_disabled)
502 errors |= CL_ERR_DISABLED;
504 /* Determine whether there may be a separate argument based on
505 whether this option is being processed for the driver, and, if
506 so, how many such arguments. */
507 separate_arg_flag = ((option->flags & CL_SEPARATE)
508 && !(option->cl_no_driver_arg
509 && (lang_mask & CL_DRIVER)));
510 separate_args = (separate_arg_flag
511 ? option->cl_separate_nargs + 1
512 : 0);
513 joined_arg_flag = (option->flags & CL_JOINED) != 0;
515 /* Sort out any argument the switch takes. */
516 if (joined_arg_flag)
518 /* Have arg point to the original switch. This is because
519 some code, such as disable_builtin_function, expects its
520 argument to be persistent until the program exits. */
521 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
523 if (*arg == '\0' && !option->cl_missing_ok)
525 if (separate_arg_flag)
527 arg = argv[extra_args + 1];
528 result = extra_args + 2;
529 if (arg == NULL)
530 result = extra_args + 1;
531 else
532 have_separate_arg = true;
534 else
535 /* Missing argument. */
536 arg = NULL;
539 else if (separate_arg_flag)
541 arg = argv[extra_args + 1];
542 for (i = 0; i < separate_args; i++)
543 if (argv[extra_args + 1 + i] == NULL)
545 errors |= CL_ERR_MISSING_ARG;
546 break;
548 result = extra_args + 1 + i;
549 if (arg != NULL)
550 have_separate_arg = true;
553 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
554 errors |= CL_ERR_MISSING_ARG;
556 /* Is this option an alias (or an ignored option, marked as an alias
557 of OPT_SPECIAL_ignore)? */
558 if (option->alias_target != N_OPTS
559 && (!option->cl_separate_alias || have_separate_arg))
561 size_t new_opt_index = option->alias_target;
563 if (new_opt_index == OPT_SPECIAL_ignore)
565 gcc_assert (option->alias_arg == NULL);
566 gcc_assert (option->neg_alias_arg == NULL);
567 opt_index = new_opt_index;
568 arg = NULL;
569 value = 1;
571 else
573 const struct cl_option *new_option = &cl_options[new_opt_index];
575 /* The new option must not be an alias itself. */
576 gcc_assert (new_option->alias_target == N_OPTS
577 || new_option->cl_separate_alias);
579 if (option->neg_alias_arg)
581 gcc_assert (option->alias_arg != NULL);
582 gcc_assert (arg == NULL);
583 gcc_assert (!option->cl_negative_alias);
584 if (value)
585 arg = option->alias_arg;
586 else
587 arg = option->neg_alias_arg;
588 value = 1;
590 else if (option->alias_arg)
592 gcc_assert (value == 1);
593 gcc_assert (arg == NULL);
594 gcc_assert (!option->cl_negative_alias);
595 arg = option->alias_arg;
598 if (option->cl_negative_alias)
599 value = !value;
601 opt_index = new_opt_index;
602 option = new_option;
604 if (value == 0)
605 gcc_assert (!option->cl_reject_negative);
607 /* Recompute what arguments are allowed. */
608 separate_arg_flag = ((option->flags & CL_SEPARATE)
609 && !(option->cl_no_driver_arg
610 && (lang_mask & CL_DRIVER)));
611 joined_arg_flag = (option->flags & CL_JOINED) != 0;
613 if (separate_args > 1 || option->cl_separate_nargs)
614 gcc_assert (separate_args
615 == (unsigned int) option->cl_separate_nargs + 1);
617 if (!(errors & CL_ERR_MISSING_ARG))
619 if (separate_arg_flag || joined_arg_flag)
621 if (option->cl_missing_ok && arg == NULL)
622 arg = "";
623 gcc_assert (arg != NULL);
625 else
626 gcc_assert (arg == NULL);
629 /* Recheck for warnings and disabled options. */
630 if (option->warn_message)
632 gcc_assert (warn_message == NULL);
633 warn_message = option->warn_message;
635 if (option->cl_disabled)
636 errors |= CL_ERR_DISABLED;
640 /* Check if this is a switch for a different front end. */
641 if (!option_ok_for_language (option, lang_mask))
642 errors |= CL_ERR_WRONG_LANG;
644 /* Convert the argument to lowercase if appropriate. */
645 if (arg && option->cl_tolower)
647 size_t j;
648 size_t len = strlen (arg);
649 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
651 for (j = 0; j < len; j++)
652 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
653 arg_lower[len] = 0;
654 arg = arg_lower;
657 /* If the switch takes an integer, convert it. */
658 if (arg && option->cl_uinteger)
660 value = integral_argument (arg);
661 if (value == -1)
662 errors |= CL_ERR_UINT_ARG;
665 /* If the switch takes an enumerated argument, convert it. */
666 if (arg && (option->var_type == CLVC_ENUM))
668 const struct cl_enum *e = &cl_enums[option->var_enum];
670 gcc_assert (value == 1);
671 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
673 const char *carg = NULL;
675 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
676 arg = carg;
677 gcc_assert (carg != NULL);
679 else
680 errors |= CL_ERR_ENUM_ARG;
683 done:
684 decoded->opt_index = opt_index;
685 decoded->arg = arg;
686 decoded->value = value;
687 decoded->errors = errors;
688 decoded->warn_message = warn_message;
690 if (opt_index == OPT_SPECIAL_unknown)
691 gcc_assert (result == 1);
693 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
694 decoded->canonical_option_num_elements = result;
695 total_len = 0;
696 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
698 if (i < result)
700 size_t len;
701 if (opt_index == OPT_SPECIAL_unknown)
702 decoded->canonical_option[i] = argv[i];
703 else
704 decoded->canonical_option[i] = NULL;
705 len = strlen (argv[i]);
706 /* If the argument is an empty string, we will print it as "" in
707 orig_option_with_args_text. */
708 total_len += (len != 0 ? len : 2) + 1;
710 else
711 decoded->canonical_option[i] = NULL;
713 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
715 generate_canonical_option (opt_index, arg, value, decoded);
716 if (separate_args > 1)
718 for (i = 0; i < separate_args; i++)
720 if (argv[extra_args + 1 + i] == NULL)
721 break;
722 else
723 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
725 gcc_assert (result == 1 + i);
726 decoded->canonical_option_num_elements = result;
729 decoded->orig_option_with_args_text
730 = p = XOBNEWVEC (&opts_obstack, char, total_len);
731 for (i = 0; i < result; i++)
733 size_t len = strlen (argv[i]);
735 /* Print the empty string verbally. */
736 if (len == 0)
738 *p++ = '"';
739 *p++ = '"';
741 else
742 memcpy (p, argv[i], len);
743 p += len;
744 if (i == result - 1)
745 *p++ = 0;
746 else
747 *p++ = ' ';
750 return result;
753 /* Obstack for option strings. */
755 struct obstack opts_obstack;
757 /* Like libiberty concat, but allocate using opts_obstack. */
759 char *
760 opts_concat (const char *first, ...)
762 char *newstr, *end;
763 size_t length = 0;
764 const char *arg;
765 va_list ap;
767 /* First compute the size of the result and get sufficient memory. */
768 va_start (ap, first);
769 for (arg = first; arg; arg = va_arg (ap, const char *))
770 length += strlen (arg);
771 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
772 va_end (ap);
774 /* Now copy the individual pieces to the result string. */
775 va_start (ap, first);
776 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
778 length = strlen (arg);
779 memcpy (end, arg, length);
780 end += length;
782 *end = '\0';
783 va_end (ap);
784 return newstr;
787 /* Decode command-line options (ARGC and ARGV being the arguments of
788 main) into an array, setting *DECODED_OPTIONS to a pointer to that
789 array and *DECODED_OPTIONS_COUNT to the number of entries in the
790 array. The first entry in the array is always one for the program
791 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
792 flags applicable for decoding (including CL_COMMON and CL_TARGET if
793 those options should be considered applicable). Do not produce any
794 diagnostics or set state outside of these variables. */
796 void
797 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
798 unsigned int lang_mask,
799 struct cl_decoded_option **decoded_options,
800 unsigned int *decoded_options_count)
802 unsigned int n, i;
803 struct cl_decoded_option *opt_array;
804 unsigned int num_decoded_options;
806 opt_array = XNEWVEC (struct cl_decoded_option, argc);
808 opt_array[0].opt_index = OPT_SPECIAL_program_name;
809 opt_array[0].warn_message = NULL;
810 opt_array[0].arg = argv[0];
811 opt_array[0].orig_option_with_args_text = argv[0];
812 opt_array[0].canonical_option_num_elements = 1;
813 opt_array[0].canonical_option[0] = argv[0];
814 opt_array[0].canonical_option[1] = NULL;
815 opt_array[0].canonical_option[2] = NULL;
816 opt_array[0].canonical_option[3] = NULL;
817 opt_array[0].value = 1;
818 opt_array[0].errors = 0;
819 num_decoded_options = 1;
821 for (i = 1; i < argc; i += n)
823 const char *opt = argv[i];
825 /* Interpret "-" or a non-switch as a file name. */
826 if (opt[0] != '-' || opt[1] == '\0')
828 generate_option_input_file (opt, &opt_array[num_decoded_options]);
829 num_decoded_options++;
830 n = 1;
831 continue;
834 n = decode_cmdline_option (argv + i, lang_mask,
835 &opt_array[num_decoded_options]);
836 num_decoded_options++;
839 *decoded_options = opt_array;
840 *decoded_options_count = num_decoded_options;
841 prune_options (decoded_options, decoded_options_count);
844 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
845 next one is the same as ORIG_NEXT_OPT_IDX. */
847 static bool
848 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
850 /* An option can be canceled by the same option or an option with
851 Negative. */
852 if (cl_options [next_opt_idx].neg_index == opt_idx)
853 return true;
855 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
856 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
857 orig_next_opt_idx);
859 return false;
862 /* Filter out options canceled by the ones after them. */
864 static void
865 prune_options (struct cl_decoded_option **decoded_options,
866 unsigned int *decoded_options_count)
868 unsigned int old_decoded_options_count = *decoded_options_count;
869 struct cl_decoded_option *old_decoded_options = *decoded_options;
870 unsigned int new_decoded_options_count;
871 struct cl_decoded_option *new_decoded_options
872 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
873 unsigned int i;
874 const struct cl_option *option;
875 unsigned int fdiagnostics_color_idx = 0;
877 /* Remove arguments which are negated by others after them. */
878 new_decoded_options_count = 0;
879 for (i = 0; i < old_decoded_options_count; i++)
881 unsigned int j, opt_idx, next_opt_idx;
883 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
884 goto keep;
886 opt_idx = old_decoded_options[i].opt_index;
887 switch (opt_idx)
889 case OPT_SPECIAL_unknown:
890 case OPT_SPECIAL_ignore:
891 case OPT_SPECIAL_program_name:
892 case OPT_SPECIAL_input_file:
893 goto keep;
895 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
896 case OPT_fdiagnostics_color_:
897 fdiagnostics_color_idx = i;
898 continue;
900 default:
901 gcc_assert (opt_idx < cl_options_count);
902 option = &cl_options[opt_idx];
903 if (option->neg_index < 0)
904 goto keep;
906 /* Skip joined switches. */
907 if ((option->flags & CL_JOINED))
908 goto keep;
910 for (j = i + 1; j < old_decoded_options_count; j++)
912 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
913 continue;
914 next_opt_idx = old_decoded_options[j].opt_index;
915 if (next_opt_idx >= cl_options_count)
916 continue;
917 if (cl_options[next_opt_idx].neg_index < 0)
918 continue;
919 if ((cl_options[next_opt_idx].flags & CL_JOINED))
920 continue;
921 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
922 break;
924 if (j == old_decoded_options_count)
926 keep:
927 new_decoded_options[new_decoded_options_count]
928 = old_decoded_options[i];
929 new_decoded_options_count++;
931 break;
935 if (fdiagnostics_color_idx >= 1)
937 /* We put the last -fdiagnostics-color= at the first position
938 after argv[0] so it can take effect immediately. */
939 memmove (new_decoded_options + 2, new_decoded_options + 1,
940 sizeof (struct cl_decoded_option)
941 * (new_decoded_options_count - 1));
942 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
943 new_decoded_options_count++;
946 free (old_decoded_options);
947 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
948 new_decoded_options,
949 new_decoded_options_count);
950 *decoded_options = new_decoded_options;
951 *decoded_options_count = new_decoded_options_count;
954 /* Handle option DECODED for the language indicated by LANG_MASK,
955 using the handlers in HANDLERS and setting fields in OPTS and
956 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
957 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
958 option for options from the source file, UNKNOWN_LOCATION
959 otherwise. GENERATED_P is true for an option generated as part of
960 processing another option or otherwise generated internally, false
961 for one explicitly passed by the user. Returns false if the switch
962 was invalid. DC is the diagnostic context for options affecting
963 diagnostics state, or NULL. */
965 static bool
966 handle_option (struct gcc_options *opts,
967 struct gcc_options *opts_set,
968 const struct cl_decoded_option *decoded,
969 unsigned int lang_mask, int kind, location_t loc,
970 const struct cl_option_handlers *handlers,
971 bool generated_p, diagnostic_context *dc)
973 size_t opt_index = decoded->opt_index;
974 const char *arg = decoded->arg;
975 int value = decoded->value;
976 const struct cl_option *option = &cl_options[opt_index];
977 void *flag_var = option_flag_var (opt_index, opts);
978 size_t i;
980 if (flag_var)
981 set_option (opts, (generated_p ? NULL : opts_set),
982 opt_index, value, arg, kind, loc, dc);
984 for (i = 0; i < handlers->num_handlers; i++)
985 if (option->flags & handlers->handlers[i].mask)
987 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
988 lang_mask, kind, loc,
989 handlers, dc))
990 return false;
993 return true;
996 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
997 option instead of DECODED. This is used for callbacks when one
998 option implies another instead of an option being decoded from the
999 command line. */
1001 bool
1002 handle_generated_option (struct gcc_options *opts,
1003 struct gcc_options *opts_set,
1004 size_t opt_index, const char *arg, int value,
1005 unsigned int lang_mask, int kind, location_t loc,
1006 const struct cl_option_handlers *handlers,
1007 diagnostic_context *dc)
1009 struct cl_decoded_option decoded;
1011 generate_option (opt_index, arg, value, lang_mask, &decoded);
1012 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1013 handlers, true, dc);
1016 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1017 VALUE for a front end using LANG_MASK. This is used when the
1018 compiler generates options internally. */
1020 void
1021 generate_option (size_t opt_index, const char *arg, int value,
1022 unsigned int lang_mask, struct cl_decoded_option *decoded)
1024 const struct cl_option *option = &cl_options[opt_index];
1026 decoded->opt_index = opt_index;
1027 decoded->warn_message = NULL;
1028 decoded->arg = arg;
1029 decoded->value = value;
1030 decoded->errors = (option_ok_for_language (option, lang_mask)
1032 : CL_ERR_WRONG_LANG);
1034 generate_canonical_option (opt_index, arg, value, decoded);
1035 switch (decoded->canonical_option_num_elements)
1037 case 1:
1038 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1039 break;
1041 case 2:
1042 decoded->orig_option_with_args_text
1043 = opts_concat (decoded->canonical_option[0], " ",
1044 decoded->canonical_option[1], NULL);
1045 break;
1047 default:
1048 gcc_unreachable ();
1052 /* Fill in *DECODED with an option for input file FILE. */
1054 void
1055 generate_option_input_file (const char *file,
1056 struct cl_decoded_option *decoded)
1058 decoded->opt_index = OPT_SPECIAL_input_file;
1059 decoded->warn_message = NULL;
1060 decoded->arg = file;
1061 decoded->orig_option_with_args_text = file;
1062 decoded->canonical_option_num_elements = 1;
1063 decoded->canonical_option[0] = file;
1064 decoded->canonical_option[1] = NULL;
1065 decoded->canonical_option[2] = NULL;
1066 decoded->canonical_option[3] = NULL;
1067 decoded->value = 1;
1068 decoded->errors = 0;
1071 /* Perform diagnostics for read_cmdline_option and control_warning_option
1072 functions. Returns true if an error has been diagnosed.
1073 LOC and LANG_MASK arguments like in read_cmdline_option.
1074 OPTION is the option to report diagnostics for, OPT the name
1075 of the option as text, ARG the argument of the option (for joined
1076 options), ERRORS is bitmask of CL_ERR_* values. */
1078 static bool
1079 cmdline_handle_error (location_t loc, const struct cl_option *option,
1080 const char *opt, const char *arg, int errors,
1081 unsigned int lang_mask)
1083 if (errors & CL_ERR_DISABLED)
1085 error_at (loc, "command line option %qs"
1086 " is not supported by this configuration", opt);
1087 return true;
1090 if (errors & CL_ERR_MISSING_ARG)
1092 if (option->missing_argument_error)
1093 error_at (loc, option->missing_argument_error, opt);
1094 else
1095 error_at (loc, "missing argument to %qs", opt);
1096 return true;
1099 if (errors & CL_ERR_UINT_ARG)
1101 error_at (loc, "argument to %qs should be a non-negative integer",
1102 option->opt_text);
1103 return true;
1106 if (errors & CL_ERR_ENUM_ARG)
1108 const struct cl_enum *e = &cl_enums[option->var_enum];
1109 unsigned int i;
1110 size_t len;
1111 char *s, *p;
1113 if (e->unknown_error)
1114 error_at (loc, e->unknown_error, arg);
1115 else
1116 error_at (loc, "unrecognized argument in option %qs", opt);
1118 len = 0;
1119 for (i = 0; e->values[i].arg != NULL; i++)
1120 len += strlen (e->values[i].arg) + 1;
1122 s = XALLOCAVEC (char, len);
1123 p = s;
1124 for (i = 0; e->values[i].arg != NULL; i++)
1126 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1127 continue;
1128 size_t arglen = strlen (e->values[i].arg);
1129 memcpy (p, e->values[i].arg, arglen);
1130 p[arglen] = ' ';
1131 p += arglen + 1;
1133 p[-1] = 0;
1134 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1135 return true;
1138 return false;
1141 /* Handle the switch DECODED (location LOC) for the language indicated
1142 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1143 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1144 diagnostic options. */
1146 void
1147 read_cmdline_option (struct gcc_options *opts,
1148 struct gcc_options *opts_set,
1149 struct cl_decoded_option *decoded,
1150 location_t loc,
1151 unsigned int lang_mask,
1152 const struct cl_option_handlers *handlers,
1153 diagnostic_context *dc)
1155 const struct cl_option *option;
1156 const char *opt = decoded->orig_option_with_args_text;
1158 if (decoded->warn_message)
1159 warning_at (loc, 0, decoded->warn_message, opt);
1161 if (decoded->opt_index == OPT_SPECIAL_unknown)
1163 if (handlers->unknown_option_callback (decoded))
1164 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1165 return;
1168 if (decoded->opt_index == OPT_SPECIAL_ignore)
1169 return;
1171 option = &cl_options[decoded->opt_index];
1173 if (decoded->errors
1174 && cmdline_handle_error (loc, option, opt, decoded->arg,
1175 decoded->errors, lang_mask))
1176 return;
1178 if (decoded->errors & CL_ERR_WRONG_LANG)
1180 handlers->wrong_lang_callback (decoded, lang_mask);
1181 return;
1184 gcc_assert (!decoded->errors);
1186 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1187 loc, handlers, false, dc))
1188 error_at (loc, "unrecognized command line option %qs", opt);
1191 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1192 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1193 location LOC, using diagnostic context DC if not NULL for
1194 diagnostic classification. */
1196 void
1197 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1198 int opt_index, int value, const char *arg, int kind,
1199 location_t loc, diagnostic_context *dc)
1201 const struct cl_option *option = &cl_options[opt_index];
1202 void *flag_var = option_flag_var (opt_index, opts);
1203 void *set_flag_var = NULL;
1205 if (!flag_var)
1206 return;
1208 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1209 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1211 if (opts_set != NULL)
1212 set_flag_var = option_flag_var (opt_index, opts_set);
1214 switch (option->var_type)
1216 case CLVC_BOOLEAN:
1217 *(int *) flag_var = value;
1218 if (set_flag_var)
1219 *(int *) set_flag_var = 1;
1220 break;
1222 case CLVC_EQUAL:
1223 if (option->cl_host_wide_int)
1224 *(HOST_WIDE_INT *) flag_var = (value
1225 ? option->var_value
1226 : !option->var_value);
1227 else
1228 *(int *) flag_var = (value
1229 ? option->var_value
1230 : !option->var_value);
1231 if (set_flag_var)
1232 *(int *) set_flag_var = 1;
1233 break;
1235 case CLVC_BIT_CLEAR:
1236 case CLVC_BIT_SET:
1237 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1239 if (option->cl_host_wide_int)
1240 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1241 else
1242 *(int *) flag_var |= option->var_value;
1244 else
1246 if (option->cl_host_wide_int)
1247 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1248 else
1249 *(int *) flag_var &= ~option->var_value;
1251 if (set_flag_var)
1253 if (option->cl_host_wide_int)
1254 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1255 else
1256 *(int *) set_flag_var |= option->var_value;
1258 break;
1260 case CLVC_STRING:
1261 *(const char **) flag_var = arg;
1262 if (set_flag_var)
1263 *(const char **) set_flag_var = "";
1264 break;
1266 case CLVC_ENUM:
1268 const struct cl_enum *e = &cl_enums[option->var_enum];
1270 e->set (flag_var, value);
1271 if (set_flag_var)
1272 e->set (set_flag_var, 1);
1274 break;
1276 case CLVC_DEFER:
1278 vec<cl_deferred_option> *v
1279 = (vec<cl_deferred_option> *) *(void **) flag_var;
1280 cl_deferred_option p = {opt_index, arg, value};
1281 if (!v)
1282 v = XCNEW (vec<cl_deferred_option>);
1283 v->safe_push (p);
1284 *(void **) flag_var = v;
1285 if (set_flag_var)
1286 *(void **) set_flag_var = v;
1288 break;
1292 /* Return the address of the flag variable for option OPT_INDEX in
1293 options structure OPTS, or NULL if there is no flag variable. */
1295 void *
1296 option_flag_var (int opt_index, struct gcc_options *opts)
1298 const struct cl_option *option = &cl_options[opt_index];
1300 if (option->flag_var_offset == (unsigned short) -1)
1301 return NULL;
1302 return (void *)(((char *) opts) + option->flag_var_offset);
1305 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1306 or -1 if it isn't a simple on-off switch. */
1309 option_enabled (int opt_idx, void *opts)
1311 const struct cl_option *option = &(cl_options[opt_idx]);
1312 struct gcc_options *optsg = (struct gcc_options *) opts;
1313 void *flag_var = option_flag_var (opt_idx, optsg);
1315 if (flag_var)
1316 switch (option->var_type)
1318 case CLVC_BOOLEAN:
1319 return *(int *) flag_var != 0;
1321 case CLVC_EQUAL:
1322 if (option->cl_host_wide_int)
1323 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1324 else
1325 return *(int *) flag_var == option->var_value;
1327 case CLVC_BIT_CLEAR:
1328 if (option->cl_host_wide_int)
1329 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1330 else
1331 return (*(int *) flag_var & option->var_value) == 0;
1333 case CLVC_BIT_SET:
1334 if (option->cl_host_wide_int)
1335 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1336 else
1337 return (*(int *) flag_var & option->var_value) != 0;
1339 case CLVC_STRING:
1340 case CLVC_ENUM:
1341 case CLVC_DEFER:
1342 break;
1344 return -1;
1347 /* Fill STATE with the current state of option OPTION in OPTS. Return
1348 true if there is some state to store. */
1350 bool
1351 get_option_state (struct gcc_options *opts, int option,
1352 struct cl_option_state *state)
1354 void *flag_var = option_flag_var (option, opts);
1356 if (flag_var == 0)
1357 return false;
1359 switch (cl_options[option].var_type)
1361 case CLVC_BOOLEAN:
1362 case CLVC_EQUAL:
1363 state->data = flag_var;
1364 state->size = (cl_options[option].cl_host_wide_int
1365 ? sizeof (HOST_WIDE_INT)
1366 : sizeof (int));
1367 break;
1369 case CLVC_BIT_CLEAR:
1370 case CLVC_BIT_SET:
1371 state->ch = option_enabled (option, opts);
1372 state->data = &state->ch;
1373 state->size = 1;
1374 break;
1376 case CLVC_STRING:
1377 state->data = *(const char **) flag_var;
1378 if (state->data == 0)
1379 state->data = "";
1380 state->size = strlen ((const char *) state->data) + 1;
1381 break;
1383 case CLVC_ENUM:
1384 state->data = flag_var;
1385 state->size = cl_enums[cl_options[option].var_enum].var_size;
1386 break;
1388 case CLVC_DEFER:
1389 return false;
1391 return true;
1394 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1395 handlers HANDLERS) to have diagnostic kind KIND for option
1396 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1397 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1398 argument of the option for joined options, or NULL otherwise. If IMPLY,
1399 the warning option in question is implied at this point. This is
1400 used by -Werror= and #pragma GCC diagnostic. */
1402 void
1403 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1404 bool imply, location_t loc, unsigned int lang_mask,
1405 const struct cl_option_handlers *handlers,
1406 struct gcc_options *opts,
1407 struct gcc_options *opts_set,
1408 diagnostic_context *dc)
1410 if (cl_options[opt_index].alias_target != N_OPTS)
1412 gcc_assert (!cl_options[opt_index].cl_separate_alias
1413 && !cl_options[opt_index].cl_negative_alias);
1414 if (cl_options[opt_index].alias_arg)
1415 arg = cl_options[opt_index].alias_arg;
1416 opt_index = cl_options[opt_index].alias_target;
1418 if (opt_index == OPT_SPECIAL_ignore)
1419 return;
1420 if (dc)
1421 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1422 if (imply)
1424 const struct cl_option *option = &cl_options[opt_index];
1426 /* -Werror=foo implies -Wfoo. */
1427 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1429 int value = 1;
1431 if (arg && *arg == '\0' && !option->cl_missing_ok)
1432 arg = NULL;
1434 if ((option->flags & CL_JOINED) && arg == NULL)
1436 cmdline_handle_error (loc, option, option->opt_text, arg,
1437 CL_ERR_MISSING_ARG, lang_mask);
1438 return;
1441 /* If the switch takes an integer, convert it. */
1442 if (arg && option->cl_uinteger)
1444 value = integral_argument (arg);
1445 if (value == -1)
1447 cmdline_handle_error (loc, option, option->opt_text, arg,
1448 CL_ERR_UINT_ARG, lang_mask);
1449 return;
1453 /* If the switch takes an enumerated argument, convert it. */
1454 if (arg && option->var_type == CLVC_ENUM)
1456 const struct cl_enum *e = &cl_enums[option->var_enum];
1458 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1460 const char *carg = NULL;
1462 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1463 arg = carg;
1464 gcc_assert (carg != NULL);
1466 else
1468 cmdline_handle_error (loc, option, option->opt_text, arg,
1469 CL_ERR_ENUM_ARG, lang_mask);
1470 return;
1474 handle_generated_option (opts, opts_set,
1475 opt_index, arg, value, lang_mask,
1476 kind, loc, handlers, dc);