libtool-version: Bump soversion.
[official-gcc.git] / gcc / opts-common.c
blob4e1ef497ed8015155b0cc48f35d6aff523e761e2
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), add it and all of its valid
378 variant spellings to CANDIDATES, each without a leading 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 char *opt_text)
392 gcc_assert (candidates);
393 gcc_assert (opt_text);
394 candidates->safe_push (xstrdup (opt_text + 1));
395 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
397 const char *opt0 = option_map[i].opt0;
398 const char *new_prefix = option_map[i].new_prefix;
399 size_t new_prefix_len = strlen (new_prefix);
401 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
403 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
404 NULL);
405 candidates->safe_push (alternative);
410 /* Decode the switch beginning at ARGV for the language indicated by
411 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
412 the structure *DECODED. Returns the number of switches
413 consumed. */
415 static unsigned int
416 decode_cmdline_option (const char **argv, unsigned int lang_mask,
417 struct cl_decoded_option *decoded)
419 size_t opt_index;
420 const char *arg = 0;
421 int value = 1;
422 unsigned int result = 1, i, extra_args, separate_args = 0;
423 int adjust_len = 0;
424 size_t total_len;
425 char *p;
426 const struct cl_option *option;
427 int errors = 0;
428 const char *warn_message = NULL;
429 bool separate_arg_flag;
430 bool joined_arg_flag;
431 bool have_separate_arg = false;
433 extra_args = 0;
435 opt_index = find_opt (argv[0] + 1, lang_mask);
436 i = 0;
437 while (opt_index == OPT_SPECIAL_unknown
438 && i < ARRAY_SIZE (option_map))
440 const char *opt0 = option_map[i].opt0;
441 const char *opt1 = option_map[i].opt1;
442 const char *new_prefix = option_map[i].new_prefix;
443 bool another_char_needed = option_map[i].another_char_needed;
444 size_t opt0_len = strlen (opt0);
445 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
446 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
447 size_t new_prefix_len = strlen (new_prefix);
449 extra_args = (opt1 == NULL ? 0 : 1);
450 value = !option_map[i].negated;
452 if (strncmp (argv[0], opt0, opt0_len) == 0
453 && (opt1 == NULL
454 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
455 && (!another_char_needed
456 || argv[extra_args][optn_len] != 0))
458 size_t arglen = strlen (argv[extra_args]);
459 char *dup;
461 adjust_len = (int) optn_len - (int) new_prefix_len;
462 dup = XNEWVEC (char, arglen + 1 - adjust_len);
463 memcpy (dup, new_prefix, new_prefix_len);
464 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
465 arglen - optn_len + 1);
466 opt_index = find_opt (dup + 1, lang_mask);
467 free (dup);
469 i++;
472 if (opt_index == OPT_SPECIAL_unknown)
474 arg = argv[0];
475 extra_args = 0;
476 value = 1;
477 goto done;
480 option = &cl_options[opt_index];
482 /* Reject negative form of switches that don't take negatives as
483 unrecognized. */
484 if (!value && option->cl_reject_negative)
486 opt_index = OPT_SPECIAL_unknown;
487 errors |= CL_ERR_NEGATIVE;
488 arg = argv[0];
489 goto done;
492 result = extra_args + 1;
493 warn_message = option->warn_message;
495 /* Check to see if the option is disabled for this configuration. */
496 if (option->cl_disabled)
497 errors |= CL_ERR_DISABLED;
499 /* Determine whether there may be a separate argument based on
500 whether this option is being processed for the driver, and, if
501 so, how many such arguments. */
502 separate_arg_flag = ((option->flags & CL_SEPARATE)
503 && !(option->cl_no_driver_arg
504 && (lang_mask & CL_DRIVER)));
505 separate_args = (separate_arg_flag
506 ? option->cl_separate_nargs + 1
507 : 0);
508 joined_arg_flag = (option->flags & CL_JOINED) != 0;
510 /* Sort out any argument the switch takes. */
511 if (joined_arg_flag)
513 /* Have arg point to the original switch. This is because
514 some code, such as disable_builtin_function, expects its
515 argument to be persistent until the program exits. */
516 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
518 if (*arg == '\0' && !option->cl_missing_ok)
520 if (separate_arg_flag)
522 arg = argv[extra_args + 1];
523 result = extra_args + 2;
524 if (arg == NULL)
525 result = extra_args + 1;
526 else
527 have_separate_arg = true;
529 else
530 /* Missing argument. */
531 arg = NULL;
534 else if (separate_arg_flag)
536 arg = argv[extra_args + 1];
537 for (i = 0; i < separate_args; i++)
538 if (argv[extra_args + 1 + i] == NULL)
540 errors |= CL_ERR_MISSING_ARG;
541 break;
543 result = extra_args + 1 + i;
544 if (arg != NULL)
545 have_separate_arg = true;
548 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
549 errors |= CL_ERR_MISSING_ARG;
551 /* Is this option an alias (or an ignored option, marked as an alias
552 of OPT_SPECIAL_ignore)? */
553 if (option->alias_target != N_OPTS
554 && (!option->cl_separate_alias || have_separate_arg))
556 size_t new_opt_index = option->alias_target;
558 if (new_opt_index == OPT_SPECIAL_ignore)
560 gcc_assert (option->alias_arg == NULL);
561 gcc_assert (option->neg_alias_arg == NULL);
562 opt_index = new_opt_index;
563 arg = NULL;
564 value = 1;
566 else
568 const struct cl_option *new_option = &cl_options[new_opt_index];
570 /* The new option must not be an alias itself. */
571 gcc_assert (new_option->alias_target == N_OPTS
572 || new_option->cl_separate_alias);
574 if (option->neg_alias_arg)
576 gcc_assert (option->alias_arg != NULL);
577 gcc_assert (arg == NULL);
578 gcc_assert (!option->cl_negative_alias);
579 if (value)
580 arg = option->alias_arg;
581 else
582 arg = option->neg_alias_arg;
583 value = 1;
585 else if (option->alias_arg)
587 gcc_assert (value == 1);
588 gcc_assert (arg == NULL);
589 gcc_assert (!option->cl_negative_alias);
590 arg = option->alias_arg;
593 if (option->cl_negative_alias)
594 value = !value;
596 opt_index = new_opt_index;
597 option = new_option;
599 if (value == 0)
600 gcc_assert (!option->cl_reject_negative);
602 /* Recompute what arguments are allowed. */
603 separate_arg_flag = ((option->flags & CL_SEPARATE)
604 && !(option->cl_no_driver_arg
605 && (lang_mask & CL_DRIVER)));
606 joined_arg_flag = (option->flags & CL_JOINED) != 0;
608 if (separate_args > 1 || option->cl_separate_nargs)
609 gcc_assert (separate_args
610 == (unsigned int) option->cl_separate_nargs + 1);
612 if (!(errors & CL_ERR_MISSING_ARG))
614 if (separate_arg_flag || joined_arg_flag)
616 if (option->cl_missing_ok && arg == NULL)
617 arg = "";
618 gcc_assert (arg != NULL);
620 else
621 gcc_assert (arg == NULL);
624 /* Recheck for warnings and disabled options. */
625 if (option->warn_message)
627 gcc_assert (warn_message == NULL);
628 warn_message = option->warn_message;
630 if (option->cl_disabled)
631 errors |= CL_ERR_DISABLED;
635 /* Check if this is a switch for a different front end. */
636 if (!option_ok_for_language (option, lang_mask))
637 errors |= CL_ERR_WRONG_LANG;
639 /* Convert the argument to lowercase if appropriate. */
640 if (arg && option->cl_tolower)
642 size_t j;
643 size_t len = strlen (arg);
644 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
646 for (j = 0; j < len; j++)
647 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
648 arg_lower[len] = 0;
649 arg = arg_lower;
652 /* If the switch takes an integer, convert it. */
653 if (arg && option->cl_uinteger)
655 value = integral_argument (arg);
656 if (value == -1)
657 errors |= CL_ERR_UINT_ARG;
660 /* If the switch takes an enumerated argument, convert it. */
661 if (arg && (option->var_type == CLVC_ENUM))
663 const struct cl_enum *e = &cl_enums[option->var_enum];
665 gcc_assert (value == 1);
666 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
668 const char *carg = NULL;
670 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
671 arg = carg;
672 gcc_assert (carg != NULL);
674 else
675 errors |= CL_ERR_ENUM_ARG;
678 done:
679 decoded->opt_index = opt_index;
680 decoded->arg = arg;
681 decoded->value = value;
682 decoded->errors = errors;
683 decoded->warn_message = warn_message;
685 if (opt_index == OPT_SPECIAL_unknown)
686 gcc_assert (result == 1);
688 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
689 decoded->canonical_option_num_elements = result;
690 total_len = 0;
691 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
693 if (i < result)
695 size_t len;
696 if (opt_index == OPT_SPECIAL_unknown)
697 decoded->canonical_option[i] = argv[i];
698 else
699 decoded->canonical_option[i] = NULL;
700 len = strlen (argv[i]);
701 /* If the argument is an empty string, we will print it as "" in
702 orig_option_with_args_text. */
703 total_len += (len != 0 ? len : 2) + 1;
705 else
706 decoded->canonical_option[i] = NULL;
708 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
710 generate_canonical_option (opt_index, arg, value, decoded);
711 if (separate_args > 1)
713 for (i = 0; i < separate_args; i++)
715 if (argv[extra_args + 1 + i] == NULL)
716 break;
717 else
718 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
720 gcc_assert (result == 1 + i);
721 decoded->canonical_option_num_elements = result;
724 decoded->orig_option_with_args_text
725 = p = XOBNEWVEC (&opts_obstack, char, total_len);
726 for (i = 0; i < result; i++)
728 size_t len = strlen (argv[i]);
730 /* Print the empty string verbally. */
731 if (len == 0)
733 *p++ = '"';
734 *p++ = '"';
736 else
737 memcpy (p, argv[i], len);
738 p += len;
739 if (i == result - 1)
740 *p++ = 0;
741 else
742 *p++ = ' ';
745 return result;
748 /* Obstack for option strings. */
750 struct obstack opts_obstack;
752 /* Like libiberty concat, but allocate using opts_obstack. */
754 char *
755 opts_concat (const char *first, ...)
757 char *newstr, *end;
758 size_t length = 0;
759 const char *arg;
760 va_list ap;
762 /* First compute the size of the result and get sufficient memory. */
763 va_start (ap, first);
764 for (arg = first; arg; arg = va_arg (ap, const char *))
765 length += strlen (arg);
766 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
767 va_end (ap);
769 /* Now copy the individual pieces to the result string. */
770 va_start (ap, first);
771 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
773 length = strlen (arg);
774 memcpy (end, arg, length);
775 end += length;
777 *end = '\0';
778 va_end (ap);
779 return newstr;
782 /* Decode command-line options (ARGC and ARGV being the arguments of
783 main) into an array, setting *DECODED_OPTIONS to a pointer to that
784 array and *DECODED_OPTIONS_COUNT to the number of entries in the
785 array. The first entry in the array is always one for the program
786 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
787 flags applicable for decoding (including CL_COMMON and CL_TARGET if
788 those options should be considered applicable). Do not produce any
789 diagnostics or set state outside of these variables. */
791 void
792 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
793 unsigned int lang_mask,
794 struct cl_decoded_option **decoded_options,
795 unsigned int *decoded_options_count)
797 unsigned int n, i;
798 struct cl_decoded_option *opt_array;
799 unsigned int num_decoded_options;
801 opt_array = XNEWVEC (struct cl_decoded_option, argc);
803 opt_array[0].opt_index = OPT_SPECIAL_program_name;
804 opt_array[0].warn_message = NULL;
805 opt_array[0].arg = argv[0];
806 opt_array[0].orig_option_with_args_text = argv[0];
807 opt_array[0].canonical_option_num_elements = 1;
808 opt_array[0].canonical_option[0] = argv[0];
809 opt_array[0].canonical_option[1] = NULL;
810 opt_array[0].canonical_option[2] = NULL;
811 opt_array[0].canonical_option[3] = NULL;
812 opt_array[0].value = 1;
813 opt_array[0].errors = 0;
814 num_decoded_options = 1;
816 for (i = 1; i < argc; i += n)
818 const char *opt = argv[i];
820 /* Interpret "-" or a non-switch as a file name. */
821 if (opt[0] != '-' || opt[1] == '\0')
823 generate_option_input_file (opt, &opt_array[num_decoded_options]);
824 num_decoded_options++;
825 n = 1;
826 continue;
829 n = decode_cmdline_option (argv + i, lang_mask,
830 &opt_array[num_decoded_options]);
831 num_decoded_options++;
834 *decoded_options = opt_array;
835 *decoded_options_count = num_decoded_options;
836 prune_options (decoded_options, decoded_options_count);
839 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
840 next one is the same as ORIG_NEXT_OPT_IDX. */
842 static bool
843 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
845 /* An option can be canceled by the same option or an option with
846 Negative. */
847 if (cl_options [next_opt_idx].neg_index == opt_idx)
848 return true;
850 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
851 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
852 orig_next_opt_idx);
854 return false;
857 /* Filter out options canceled by the ones after them. */
859 static void
860 prune_options (struct cl_decoded_option **decoded_options,
861 unsigned int *decoded_options_count)
863 unsigned int old_decoded_options_count = *decoded_options_count;
864 struct cl_decoded_option *old_decoded_options = *decoded_options;
865 unsigned int new_decoded_options_count;
866 struct cl_decoded_option *new_decoded_options
867 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
868 unsigned int i;
869 const struct cl_option *option;
870 unsigned int fdiagnostics_color_idx = 0;
872 /* Remove arguments which are negated by others after them. */
873 new_decoded_options_count = 0;
874 for (i = 0; i < old_decoded_options_count; i++)
876 unsigned int j, opt_idx, next_opt_idx;
878 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
879 goto keep;
881 opt_idx = old_decoded_options[i].opt_index;
882 switch (opt_idx)
884 case OPT_SPECIAL_unknown:
885 case OPT_SPECIAL_ignore:
886 case OPT_SPECIAL_program_name:
887 case OPT_SPECIAL_input_file:
888 goto keep;
890 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
891 case OPT_fdiagnostics_color_:
892 fdiagnostics_color_idx = i;
893 continue;
895 default:
896 gcc_assert (opt_idx < cl_options_count);
897 option = &cl_options[opt_idx];
898 if (option->neg_index < 0)
899 goto keep;
901 /* Skip joined switches. */
902 if ((option->flags & CL_JOINED))
903 goto keep;
905 for (j = i + 1; j < old_decoded_options_count; j++)
907 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
908 continue;
909 next_opt_idx = old_decoded_options[j].opt_index;
910 if (next_opt_idx >= cl_options_count)
911 continue;
912 if (cl_options[next_opt_idx].neg_index < 0)
913 continue;
914 if ((cl_options[next_opt_idx].flags & CL_JOINED))
915 continue;
916 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
917 break;
919 if (j == old_decoded_options_count)
921 keep:
922 new_decoded_options[new_decoded_options_count]
923 = old_decoded_options[i];
924 new_decoded_options_count++;
926 break;
930 if (fdiagnostics_color_idx >= 1)
932 /* We put the last -fdiagnostics-color= at the first position
933 after argv[0] so it can take effect immediately. */
934 memmove (new_decoded_options + 2, new_decoded_options + 1,
935 sizeof (struct cl_decoded_option)
936 * (new_decoded_options_count - 1));
937 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
938 new_decoded_options_count++;
941 free (old_decoded_options);
942 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
943 new_decoded_options,
944 new_decoded_options_count);
945 *decoded_options = new_decoded_options;
946 *decoded_options_count = new_decoded_options_count;
949 /* Handle option DECODED for the language indicated by LANG_MASK,
950 using the handlers in HANDLERS and setting fields in OPTS and
951 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
952 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
953 option for options from the source file, UNKNOWN_LOCATION
954 otherwise. GENERATED_P is true for an option generated as part of
955 processing another option or otherwise generated internally, false
956 for one explicitly passed by the user. Returns false if the switch
957 was invalid. DC is the diagnostic context for options affecting
958 diagnostics state, or NULL. */
960 static bool
961 handle_option (struct gcc_options *opts,
962 struct gcc_options *opts_set,
963 const struct cl_decoded_option *decoded,
964 unsigned int lang_mask, int kind, location_t loc,
965 const struct cl_option_handlers *handlers,
966 bool generated_p, diagnostic_context *dc)
968 size_t opt_index = decoded->opt_index;
969 const char *arg = decoded->arg;
970 int value = decoded->value;
971 const struct cl_option *option = &cl_options[opt_index];
972 void *flag_var = option_flag_var (opt_index, opts);
973 size_t i;
975 if (flag_var)
976 set_option (opts, (generated_p ? NULL : opts_set),
977 opt_index, value, arg, kind, loc, dc);
979 for (i = 0; i < handlers->num_handlers; i++)
980 if (option->flags & handlers->handlers[i].mask)
982 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
983 lang_mask, kind, loc,
984 handlers, dc))
985 return false;
988 return true;
991 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
992 option instead of DECODED. This is used for callbacks when one
993 option implies another instead of an option being decoded from the
994 command line. */
996 bool
997 handle_generated_option (struct gcc_options *opts,
998 struct gcc_options *opts_set,
999 size_t opt_index, const char *arg, int value,
1000 unsigned int lang_mask, int kind, location_t loc,
1001 const struct cl_option_handlers *handlers,
1002 diagnostic_context *dc)
1004 struct cl_decoded_option decoded;
1006 generate_option (opt_index, arg, value, lang_mask, &decoded);
1007 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1008 handlers, true, dc);
1011 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1012 VALUE for a front end using LANG_MASK. This is used when the
1013 compiler generates options internally. */
1015 void
1016 generate_option (size_t opt_index, const char *arg, int value,
1017 unsigned int lang_mask, struct cl_decoded_option *decoded)
1019 const struct cl_option *option = &cl_options[opt_index];
1021 decoded->opt_index = opt_index;
1022 decoded->warn_message = NULL;
1023 decoded->arg = arg;
1024 decoded->value = value;
1025 decoded->errors = (option_ok_for_language (option, lang_mask)
1027 : CL_ERR_WRONG_LANG);
1029 generate_canonical_option (opt_index, arg, value, decoded);
1030 switch (decoded->canonical_option_num_elements)
1032 case 1:
1033 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1034 break;
1036 case 2:
1037 decoded->orig_option_with_args_text
1038 = opts_concat (decoded->canonical_option[0], " ",
1039 decoded->canonical_option[1], NULL);
1040 break;
1042 default:
1043 gcc_unreachable ();
1047 /* Fill in *DECODED with an option for input file FILE. */
1049 void
1050 generate_option_input_file (const char *file,
1051 struct cl_decoded_option *decoded)
1053 decoded->opt_index = OPT_SPECIAL_input_file;
1054 decoded->warn_message = NULL;
1055 decoded->arg = file;
1056 decoded->orig_option_with_args_text = file;
1057 decoded->canonical_option_num_elements = 1;
1058 decoded->canonical_option[0] = file;
1059 decoded->canonical_option[1] = NULL;
1060 decoded->canonical_option[2] = NULL;
1061 decoded->canonical_option[3] = NULL;
1062 decoded->value = 1;
1063 decoded->errors = 0;
1066 /* Perform diagnostics for read_cmdline_option and control_warning_option
1067 functions. Returns true if an error has been diagnosed.
1068 LOC and LANG_MASK arguments like in read_cmdline_option.
1069 OPTION is the option to report diagnostics for, OPT the name
1070 of the option as text, ARG the argument of the option (for joined
1071 options), ERRORS is bitmask of CL_ERR_* values. */
1073 static bool
1074 cmdline_handle_error (location_t loc, const struct cl_option *option,
1075 const char *opt, const char *arg, int errors,
1076 unsigned int lang_mask)
1078 if (errors & CL_ERR_DISABLED)
1080 error_at (loc, "command line option %qs"
1081 " is not supported by this configuration", opt);
1082 return true;
1085 if (errors & CL_ERR_MISSING_ARG)
1087 if (option->missing_argument_error)
1088 error_at (loc, option->missing_argument_error, opt);
1089 else
1090 error_at (loc, "missing argument to %qs", opt);
1091 return true;
1094 if (errors & CL_ERR_UINT_ARG)
1096 error_at (loc, "argument to %qs should be a non-negative integer",
1097 option->opt_text);
1098 return true;
1101 if (errors & CL_ERR_ENUM_ARG)
1103 const struct cl_enum *e = &cl_enums[option->var_enum];
1104 unsigned int i;
1105 size_t len;
1106 char *s, *p;
1108 if (e->unknown_error)
1109 error_at (loc, e->unknown_error, arg);
1110 else
1111 error_at (loc, "unrecognized argument in option %qs", opt);
1113 len = 0;
1114 for (i = 0; e->values[i].arg != NULL; i++)
1115 len += strlen (e->values[i].arg) + 1;
1117 auto_vec <const char *> candidates;
1118 s = XALLOCAVEC (char, len);
1119 p = s;
1120 for (i = 0; e->values[i].arg != NULL; i++)
1122 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1123 continue;
1124 size_t arglen = strlen (e->values[i].arg);
1125 memcpy (p, e->values[i].arg, arglen);
1126 p[arglen] = ' ';
1127 p += arglen + 1;
1128 candidates.safe_push (e->values[i].arg);
1130 p[-1] = 0;
1131 const char *hint = find_closest_string (arg, &candidates);
1132 if (hint)
1133 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1134 option->opt_text, s, hint);
1135 else
1136 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1138 return true;
1141 return false;
1144 /* Handle the switch DECODED (location LOC) for the language indicated
1145 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1146 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1147 diagnostic options. */
1149 void
1150 read_cmdline_option (struct gcc_options *opts,
1151 struct gcc_options *opts_set,
1152 struct cl_decoded_option *decoded,
1153 location_t loc,
1154 unsigned int lang_mask,
1155 const struct cl_option_handlers *handlers,
1156 diagnostic_context *dc)
1158 const struct cl_option *option;
1159 const char *opt = decoded->orig_option_with_args_text;
1161 if (decoded->warn_message)
1162 warning_at (loc, 0, decoded->warn_message, opt);
1164 if (decoded->opt_index == OPT_SPECIAL_unknown)
1166 if (handlers->unknown_option_callback (decoded))
1167 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1168 return;
1171 if (decoded->opt_index == OPT_SPECIAL_ignore)
1172 return;
1174 option = &cl_options[decoded->opt_index];
1176 if (decoded->errors
1177 && cmdline_handle_error (loc, option, opt, decoded->arg,
1178 decoded->errors, lang_mask))
1179 return;
1181 if (decoded->errors & CL_ERR_WRONG_LANG)
1183 handlers->wrong_lang_callback (decoded, lang_mask);
1184 return;
1187 gcc_assert (!decoded->errors);
1189 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1190 loc, handlers, false, dc))
1191 error_at (loc, "unrecognized command line option %qs", opt);
1194 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1195 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1196 location LOC, using diagnostic context DC if not NULL for
1197 diagnostic classification. */
1199 void
1200 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1201 int opt_index, int value, const char *arg, int kind,
1202 location_t loc, diagnostic_context *dc)
1204 const struct cl_option *option = &cl_options[opt_index];
1205 void *flag_var = option_flag_var (opt_index, opts);
1206 void *set_flag_var = NULL;
1208 if (!flag_var)
1209 return;
1211 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1212 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1214 if (opts_set != NULL)
1215 set_flag_var = option_flag_var (opt_index, opts_set);
1217 switch (option->var_type)
1219 case CLVC_BOOLEAN:
1220 *(int *) flag_var = value;
1221 if (set_flag_var)
1222 *(int *) set_flag_var = 1;
1223 break;
1225 case CLVC_EQUAL:
1226 if (option->cl_host_wide_int)
1227 *(HOST_WIDE_INT *) flag_var = (value
1228 ? option->var_value
1229 : !option->var_value);
1230 else
1231 *(int *) flag_var = (value
1232 ? option->var_value
1233 : !option->var_value);
1234 if (set_flag_var)
1235 *(int *) set_flag_var = 1;
1236 break;
1238 case CLVC_BIT_CLEAR:
1239 case CLVC_BIT_SET:
1240 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1242 if (option->cl_host_wide_int)
1243 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1244 else
1245 *(int *) flag_var |= option->var_value;
1247 else
1249 if (option->cl_host_wide_int)
1250 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1251 else
1252 *(int *) flag_var &= ~option->var_value;
1254 if (set_flag_var)
1256 if (option->cl_host_wide_int)
1257 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1258 else
1259 *(int *) set_flag_var |= option->var_value;
1261 break;
1263 case CLVC_STRING:
1264 *(const char **) flag_var = arg;
1265 if (set_flag_var)
1266 *(const char **) set_flag_var = "";
1267 break;
1269 case CLVC_ENUM:
1271 const struct cl_enum *e = &cl_enums[option->var_enum];
1273 e->set (flag_var, value);
1274 if (set_flag_var)
1275 e->set (set_flag_var, 1);
1277 break;
1279 case CLVC_DEFER:
1281 vec<cl_deferred_option> *v
1282 = (vec<cl_deferred_option> *) *(void **) flag_var;
1283 cl_deferred_option p = {opt_index, arg, value};
1284 if (!v)
1285 v = XCNEW (vec<cl_deferred_option>);
1286 v->safe_push (p);
1287 *(void **) flag_var = v;
1288 if (set_flag_var)
1289 *(void **) set_flag_var = v;
1291 break;
1295 /* Return the address of the flag variable for option OPT_INDEX in
1296 options structure OPTS, or NULL if there is no flag variable. */
1298 void *
1299 option_flag_var (int opt_index, struct gcc_options *opts)
1301 const struct cl_option *option = &cl_options[opt_index];
1303 if (option->flag_var_offset == (unsigned short) -1)
1304 return NULL;
1305 return (void *)(((char *) opts) + option->flag_var_offset);
1308 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1309 or -1 if it isn't a simple on-off switch. */
1312 option_enabled (int opt_idx, void *opts)
1314 const struct cl_option *option = &(cl_options[opt_idx]);
1315 struct gcc_options *optsg = (struct gcc_options *) opts;
1316 void *flag_var = option_flag_var (opt_idx, optsg);
1318 if (flag_var)
1319 switch (option->var_type)
1321 case CLVC_BOOLEAN:
1322 return *(int *) flag_var != 0;
1324 case CLVC_EQUAL:
1325 if (option->cl_host_wide_int)
1326 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1327 else
1328 return *(int *) flag_var == option->var_value;
1330 case CLVC_BIT_CLEAR:
1331 if (option->cl_host_wide_int)
1332 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1333 else
1334 return (*(int *) flag_var & option->var_value) == 0;
1336 case CLVC_BIT_SET:
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_STRING:
1343 case CLVC_ENUM:
1344 case CLVC_DEFER:
1345 break;
1347 return -1;
1350 /* Fill STATE with the current state of option OPTION in OPTS. Return
1351 true if there is some state to store. */
1353 bool
1354 get_option_state (struct gcc_options *opts, int option,
1355 struct cl_option_state *state)
1357 void *flag_var = option_flag_var (option, opts);
1359 if (flag_var == 0)
1360 return false;
1362 switch (cl_options[option].var_type)
1364 case CLVC_BOOLEAN:
1365 case CLVC_EQUAL:
1366 state->data = flag_var;
1367 state->size = (cl_options[option].cl_host_wide_int
1368 ? sizeof (HOST_WIDE_INT)
1369 : sizeof (int));
1370 break;
1372 case CLVC_BIT_CLEAR:
1373 case CLVC_BIT_SET:
1374 state->ch = option_enabled (option, opts);
1375 state->data = &state->ch;
1376 state->size = 1;
1377 break;
1379 case CLVC_STRING:
1380 state->data = *(const char **) flag_var;
1381 if (state->data == 0)
1382 state->data = "";
1383 state->size = strlen ((const char *) state->data) + 1;
1384 break;
1386 case CLVC_ENUM:
1387 state->data = flag_var;
1388 state->size = cl_enums[cl_options[option].var_enum].var_size;
1389 break;
1391 case CLVC_DEFER:
1392 return false;
1394 return true;
1397 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1398 handlers HANDLERS) to have diagnostic kind KIND for option
1399 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1400 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1401 argument of the option for joined options, or NULL otherwise. If IMPLY,
1402 the warning option in question is implied at this point. This is
1403 used by -Werror= and #pragma GCC diagnostic. */
1405 void
1406 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1407 bool imply, location_t loc, unsigned int lang_mask,
1408 const struct cl_option_handlers *handlers,
1409 struct gcc_options *opts,
1410 struct gcc_options *opts_set,
1411 diagnostic_context *dc)
1413 if (cl_options[opt_index].alias_target != N_OPTS)
1415 gcc_assert (!cl_options[opt_index].cl_separate_alias
1416 && !cl_options[opt_index].cl_negative_alias);
1417 if (cl_options[opt_index].alias_arg)
1418 arg = cl_options[opt_index].alias_arg;
1419 opt_index = cl_options[opt_index].alias_target;
1421 if (opt_index == OPT_SPECIAL_ignore)
1422 return;
1423 if (dc)
1424 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1425 if (imply)
1427 const struct cl_option *option = &cl_options[opt_index];
1429 /* -Werror=foo implies -Wfoo. */
1430 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1432 int value = 1;
1434 if (arg && *arg == '\0' && !option->cl_missing_ok)
1435 arg = NULL;
1437 if ((option->flags & CL_JOINED) && arg == NULL)
1439 cmdline_handle_error (loc, option, option->opt_text, arg,
1440 CL_ERR_MISSING_ARG, lang_mask);
1441 return;
1444 /* If the switch takes an integer, convert it. */
1445 if (arg && option->cl_uinteger)
1447 value = integral_argument (arg);
1448 if (value == -1)
1450 cmdline_handle_error (loc, option, option->opt_text, arg,
1451 CL_ERR_UINT_ARG, lang_mask);
1452 return;
1456 /* If the switch takes an enumerated argument, convert it. */
1457 if (arg && option->var_type == CLVC_ENUM)
1459 const struct cl_enum *e = &cl_enums[option->var_enum];
1461 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1463 const char *carg = NULL;
1465 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1466 arg = carg;
1467 gcc_assert (carg != NULL);
1469 else
1471 cmdline_handle_error (loc, option, option->opt_text, arg,
1472 CL_ERR_ENUM_ARG, lang_mask);
1473 return;
1477 handle_generated_option (opts, opts_set,
1478 opt_index, arg, value, lang_mask,
1479 kind, loc, handlers, dc);