hppa: Fix indirect_goto constraint
[official-gcc.git] / gcc / opts-common.cc
blob70ac225e3968172cc099364daf581e29a262889b
1 /* Command line option handling.
2 Copyright (C) 2006-2024 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 #define INCLUDE_STRING
21 #include "config.h"
22 #include "system.h"
23 #include "intl.h"
24 #include "coretypes.h"
25 #include "opts.h"
26 #include "options.h"
27 #include "diagnostic.h"
28 #include "opts-diagnostic.h"
29 #include "spellcheck.h"
30 #include "opts-jobserver.h"
32 static void prune_options (struct cl_decoded_option **, unsigned int *);
34 /* An option that is undocumented, that takes a joined argument, and
35 that doesn't fit any of the classes of uses (language/common,
36 driver, target) is assumed to be a prefix used to catch
37 e.g. negated options, and stop them from being further shortened to
38 a prefix that could use the negated option as an argument. For
39 example, we want -gno-statement-frontiers to be taken as a negation
40 of -gstatement-frontiers, but without catching the gno- prefix and
41 signaling it's to be used for option remapping, it would end up
42 backtracked to g with no-statemnet-frontiers as the debug level. */
44 static bool
45 remapping_prefix_p (const struct cl_option *opt)
47 return opt->flags & CL_UNDOCUMENTED
48 && opt->flags & CL_JOINED
49 && !(opt->flags & (CL_DRIVER | CL_TARGET | CL_COMMON | CL_LANG_ALL));
52 /* Perform a binary search to find which option the command-line INPUT
53 matches. Returns its index in the option array, and
54 OPT_SPECIAL_unknown on failure.
56 This routine is quite subtle. A normal binary search is not good
57 enough because some options can be suffixed with an argument, and
58 multiple sub-matches can occur, e.g. input of "-pedantic" matching
59 the initial substring of "-pedantic-errors".
61 A more complicated example is -gstabs. It should match "-g" with
62 an argument of "stabs". Suppose, however, that the number and list
63 of switches are such that the binary search tests "-gen-decls"
64 before having tested "-g". This doesn't match, and as "-gen-decls"
65 is less than "-gstabs", it will become the lower bound of the
66 binary search range, and "-g" will never be seen. To resolve this
67 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
68 to "-g" so that failed searches that end between "-gen-decls" and
69 the lexicographically subsequent switch know to go back and see if
70 "-g" causes a match (which it does in this example).
72 This search is done in such a way that the longest match for the
73 front end in question wins. If there is no match for the current
74 front end, the longest match for a different front end is returned
75 (or N_OPTS if none) and the caller emits an error message. */
76 size_t
77 find_opt (const char *input, unsigned int lang_mask)
79 size_t mn, mn_orig, mx, md, opt_len;
80 size_t match_wrong_lang;
81 int comp;
83 mn = 0;
84 mx = cl_options_count;
86 /* Find mn such this lexicographical inequality holds:
87 cl_options[mn] <= input < cl_options[mn + 1]. */
88 while (mx - mn > 1)
90 md = (mn + mx) / 2;
91 opt_len = cl_options[md].opt_len;
92 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
94 if (comp < 0)
95 mx = md;
96 else
97 mn = md;
100 mn_orig = mn;
102 /* This is the switch that is the best match but for a different
103 front end, or OPT_SPECIAL_unknown if there is no match at all. */
104 match_wrong_lang = OPT_SPECIAL_unknown;
106 /* Backtrace the chain of possible matches, returning the longest
107 one, if any, that fits best. With current GCC switches, this
108 loop executes at most twice. */
111 const struct cl_option *opt = &cl_options[mn];
113 /* Is the input either an exact match or a prefix that takes a
114 joined argument? */
115 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
116 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
118 /* If language is OK, return it. */
119 if (opt->flags & lang_mask)
120 return mn;
122 if (remapping_prefix_p (opt))
123 return OPT_SPECIAL_unknown;
125 /* If we haven't remembered a prior match, remember this
126 one. Any prior match is necessarily better. */
127 if (match_wrong_lang == OPT_SPECIAL_unknown)
128 match_wrong_lang = mn;
131 /* Try the next possibility. This is cl_options_count if there
132 are no more. */
133 mn = opt->back_chain;
135 while (mn != cl_options_count);
137 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
139 /* Long options, starting "--", may be abbreviated if the
140 abbreviation is unambiguous. This only applies to options
141 not taking a joined argument, and abbreviations of "--option"
142 are permitted even if there is a variant "--option=". */
143 size_t mnc = mn_orig + 1;
144 size_t cmp_len = strlen (input);
145 while (mnc < cl_options_count
146 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
148 /* Option matching this abbreviation. OK if it is the first
149 match and that does not take a joined argument, or the
150 second match, taking a joined argument and with only '='
151 added to the first match; otherwise considered
152 ambiguous. */
153 if (mnc == mn_orig + 1
154 && !(cl_options[mnc].flags & CL_JOINED))
155 match_wrong_lang = mnc;
156 else if (mnc == mn_orig + 2
157 && match_wrong_lang == mn_orig + 1
158 && (cl_options[mnc].flags & CL_JOINED)
159 && (cl_options[mnc].opt_len
160 == cl_options[mn_orig + 1].opt_len + 1)
161 && strncmp (cl_options[mnc].opt_text + 1,
162 cl_options[mn_orig + 1].opt_text + 1,
163 cl_options[mn_orig + 1].opt_len) == 0)
164 ; /* OK, as long as there are no more matches. */
165 else
166 return OPT_SPECIAL_unknown;
167 mnc++;
171 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
172 return match_wrong_lang;
175 /* If ARG is a non-negative decimal or hexadecimal integer representable
176 in HOST_WIDE_INT return its value, otherwise return -1. If ERR is not
177 null set *ERR to zero on success or to EINVAL or to the value of errno
178 otherwise. */
180 HOST_WIDE_INT
181 integral_argument (const char *arg, int *err, bool byte_size_suffix)
183 if (!err)
184 err = &errno;
186 if (!ISDIGIT (*arg))
188 *err = EINVAL;
189 return -1;
192 *err = 0;
193 errno = 0;
195 char *end = NULL;
196 unsigned HOST_WIDE_INT unit = 1;
197 unsigned HOST_WIDE_INT value = strtoull (arg, &end, 10);
199 /* If the value is too large to be represented use the maximum
200 representable value that strtoull sets VALUE to (setting
201 errno to ERANGE). */
203 if (end && *end)
205 if (!byte_size_suffix)
207 errno = 0;
208 value = strtoull (arg, &end, 0);
209 if (*end)
211 if (errno)
212 *err = errno;
213 else
214 *err = EINVAL;
215 return -1;
218 return value;
221 /* Numeric option arguments are at most INT_MAX. Make it
222 possible to specify a larger value by accepting common
223 suffixes. */
224 if (!strcmp (end, "kB"))
225 unit = 1000;
226 else if (!strcasecmp (end, "KiB") || !strcmp (end, "KB"))
227 unit = 1024;
228 else if (!strcmp (end, "MB"))
229 unit = HOST_WIDE_INT_UC (1000) * 1000;
230 else if (!strcasecmp (end, "MiB"))
231 unit = HOST_WIDE_INT_UC (1024) * 1024;
232 else if (!strcasecmp (end, "GB"))
233 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000;
234 else if (!strcasecmp (end, "GiB"))
235 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024;
236 else if (!strcasecmp (end, "TB"))
237 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000;
238 else if (!strcasecmp (end, "TiB"))
239 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024;
240 else if (!strcasecmp (end, "PB"))
241 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000;
242 else if (!strcasecmp (end, "PiB"))
243 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024;
244 else if (!strcasecmp (end, "EB"))
245 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000
246 * 1000;
247 else if (!strcasecmp (end, "EiB"))
248 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024
249 * 1024;
250 else
252 /* This could mean an unknown suffix or a bad prefix, like
253 "+-1". */
254 *err = EINVAL;
255 return -1;
259 if (unit)
261 unsigned HOST_WIDE_INT prod = value * unit;
262 value = prod < value ? HOST_WIDE_INT_M1U : prod;
265 return value;
268 /* Return whether OPTION is OK for the language given by
269 LANG_MASK. */
270 static bool
271 option_ok_for_language (const struct cl_option *option,
272 unsigned int lang_mask)
274 if (!(option->flags & lang_mask))
275 return false;
276 else if ((option->flags & CL_TARGET)
277 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
278 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
279 /* Complain for target flag language mismatches if any languages
280 are specified. */
281 return false;
282 return true;
285 /* Return whether ENUM_ARG is OK for the language given by
286 LANG_MASK. */
288 static bool
289 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
290 unsigned int lang_mask)
292 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
295 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning the cl_enum_arg
296 index and storing the value in *VALUE if found, and returning -1 without
297 modifying *VALUE if not found. */
299 static int
300 enum_arg_to_value (const struct cl_enum_arg *enum_args,
301 const char *arg, size_t len, HOST_WIDE_INT *value,
302 unsigned int lang_mask)
304 unsigned int i;
306 for (i = 0; enum_args[i].arg != NULL; i++)
307 if ((len
308 ? (strncmp (arg, enum_args[i].arg, len) == 0
309 && enum_args[i].arg[len] == '\0')
310 : strcmp (arg, enum_args[i].arg) == 0)
311 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
313 *value = enum_args[i].value;
314 return i;
317 return -1;
320 /* Look up ARG in the enum used by option OPT_INDEX for language
321 LANG_MASK, returning true and storing the value in *VALUE if found,
322 and returning false without modifying *VALUE if not found. */
324 bool
325 opt_enum_arg_to_value (size_t opt_index, const char *arg,
326 int *value, unsigned int lang_mask)
328 const struct cl_option *option = &cl_options[opt_index];
330 gcc_assert (option->var_type == CLVC_ENUM);
332 HOST_WIDE_INT wideval;
333 if (enum_arg_to_value (cl_enums[option->var_enum].values, arg, 0,
334 &wideval, lang_mask) >= 0)
336 *value = wideval;
337 return true;
340 return false;
343 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
344 corresponding string in *ARGP, returning true if the found string
345 was marked as canonical, false otherwise. If VALUE is not found
346 (which may be the case for uninitialized values if the relevant
347 option has not been passed), set *ARGP to NULL and return
348 false. */
350 bool
351 enum_value_to_arg (const struct cl_enum_arg *enum_args,
352 const char **argp, int value, unsigned int lang_mask)
354 unsigned int i;
356 for (i = 0; enum_args[i].arg != NULL; i++)
357 if (enum_args[i].value == value
358 && (enum_args[i].flags & CL_ENUM_CANONICAL)
359 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
361 *argp = enum_args[i].arg;
362 return true;
365 for (i = 0; enum_args[i].arg != NULL; i++)
366 if (enum_args[i].value == value
367 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
369 *argp = enum_args[i].arg;
370 return false;
373 *argp = NULL;
374 return false;
377 /* Fill in the canonical option part of *DECODED with an option
378 described by OPT_INDEX, ARG and VALUE. */
380 static void
381 generate_canonical_option (size_t opt_index, const char *arg,
382 HOST_WIDE_INT value,
383 struct cl_decoded_option *decoded)
385 const struct cl_option *option = &cl_options[opt_index];
386 const char *opt_text = option->opt_text;
388 if (value == 0
389 && !option->cl_reject_negative
390 && (opt_text[1] == 'W' || opt_text[1] == 'f'
391 || opt_text[1] == 'g' || opt_text[1] == 'm'))
393 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
394 t[0] = '-';
395 t[1] = opt_text[1];
396 t[2] = 'n';
397 t[3] = 'o';
398 t[4] = '-';
399 memcpy (t + 5, opt_text + 2, option->opt_len);
400 opt_text = t;
403 decoded->canonical_option[2] = NULL;
404 decoded->canonical_option[3] = NULL;
406 if (arg)
408 if ((option->flags & CL_SEPARATE)
409 && !option->cl_separate_alias)
411 decoded->canonical_option[0] = opt_text;
412 decoded->canonical_option[1] = arg;
413 decoded->canonical_option_num_elements = 2;
415 else
417 gcc_assert (option->flags & CL_JOINED);
418 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
419 decoded->canonical_option[1] = NULL;
420 decoded->canonical_option_num_elements = 1;
423 else
425 decoded->canonical_option[0] = opt_text;
426 decoded->canonical_option[1] = NULL;
427 decoded->canonical_option_num_elements = 1;
431 /* Structure describing mappings from options on the command line to
432 options to look up with find_opt. */
433 struct option_map
435 /* Prefix of the option on the command line. */
436 const char *opt0;
437 /* If two argv elements are considered to be merged into one option,
438 prefix for the second element, otherwise NULL. */
439 const char *opt1;
440 /* The new prefix to map to. */
441 const char *new_prefix;
442 /* Whether at least one character is needed following opt1 or opt0
443 for this mapping to be used. (--optimize= is valid for -O, but
444 --warn- is not valid for -W.) */
445 bool another_char_needed;
446 /* Whether the original option is a negated form of the option
447 resulting from this map. */
448 bool negated;
450 static const struct option_map option_map[] =
452 { "-Wno-", NULL, "-W", false, true },
453 { "-fno-", NULL, "-f", false, true },
454 { "-gno-", NULL, "-g", false, true },
455 { "-mno-", NULL, "-m", false, true },
456 { "--debug=", NULL, "-g", false, false },
457 { "--machine-", NULL, "-m", true, false },
458 { "--machine-no-", NULL, "-m", false, true },
459 { "--machine=", NULL, "-m", false, false },
460 { "--machine=no-", NULL, "-m", false, true },
461 { "--machine", "", "-m", false, false },
462 { "--machine", "no-", "-m", false, true },
463 { "--optimize=", NULL, "-O", false, false },
464 { "--std=", NULL, "-std=", false, false },
465 { "--std", "", "-std=", false, false },
466 { "--warn-", NULL, "-W", true, false },
467 { "--warn-no-", NULL, "-W", false, true },
468 { "--", NULL, "-f", true, false },
469 { "--no-", NULL, "-f", false, true }
472 /* Given buffer P of size SZ, look for a prefix within OPTION_MAP;
473 if found, return the prefix and write the new prefix to *OUT_NEW_PREFIX.
474 Otherwise return nullptr. */
476 const char *
477 get_option_prefix_remapping (const char *p, size_t sz,
478 const char **out_new_prefix)
480 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
482 const char * const old_prefix = option_map[i].opt0;
483 const size_t old_prefix_len = strlen (old_prefix);
484 if (old_prefix_len <= sz
485 && !memcmp (p, old_prefix, old_prefix_len))
487 *out_new_prefix = option_map[i].new_prefix;
488 return old_prefix;
491 return nullptr;
494 /* Helper function for gcc.cc's driver::suggest_option, for populating the
495 vec of suggestions for misspelled options.
497 option_map above provides various prefixes for spelling command-line
498 options, which decode_cmdline_option uses to map spellings of options
499 to specific options. We want to do the reverse: to find all the ways
500 that a user could validly spell an option.
502 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
503 of its valid variant spellings to CANDIDATES, each without a leading
504 dash.
506 For example, given "-Wabi-tag", the following are added to CANDIDATES:
507 "Wabi-tag"
508 "Wno-abi-tag"
509 "-warn-abi-tag"
510 "-warn-no-abi-tag".
512 The added strings must be freed using free. */
514 void
515 add_misspelling_candidates (auto_vec<char *> *candidates,
516 const struct cl_option *option,
517 const char *opt_text)
519 gcc_assert (candidates);
520 gcc_assert (option);
521 gcc_assert (opt_text);
522 if (remapping_prefix_p (option))
523 return;
524 candidates->safe_push (xstrdup (opt_text + 1));
525 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
527 const char *opt0 = option_map[i].opt0;
528 const char *opt1 = option_map[i].opt1;
529 const char *new_prefix = option_map[i].new_prefix;
530 size_t new_prefix_len = strlen (new_prefix);
532 if (option->cl_reject_negative && option_map[i].negated)
533 continue;
535 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
537 char *alternative
538 = concat (opt0 + 1, opt1 ? " " : "", opt1 ? opt1 : "",
539 opt_text + new_prefix_len, NULL);
540 candidates->safe_push (alternative);
544 /* For all params (e.g. --param=key=value),
545 include also '--param key=value'. */
546 const char *prefix = "--param=";
547 if (strstr (opt_text, prefix) == opt_text)
549 char *param = xstrdup (opt_text + 1);
550 gcc_assert (param[6] == '=');
551 param[6] = ' ';
552 candidates->safe_push (param);
556 /* Decode the switch beginning at ARGV for the language indicated by
557 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
558 the structure *DECODED. Returns the number of switches
559 consumed. */
561 static unsigned int
562 decode_cmdline_option (const char *const *argv, unsigned int lang_mask,
563 struct cl_decoded_option *decoded)
565 size_t opt_index;
566 const char *arg = 0;
567 HOST_WIDE_INT value = 1, mask = 0;
568 unsigned int result = 1, i, extra_args, separate_args = 0;
569 int adjust_len = 0;
570 size_t total_len;
571 char *p;
572 const struct cl_option *option;
573 int errors = 0;
574 const char *warn_message = NULL;
575 bool separate_arg_flag;
576 bool joined_arg_flag;
577 bool have_separate_arg = false;
579 extra_args = 0;
581 const char *opt_value = argv[0] + 1;
582 opt_index = find_opt (opt_value, lang_mask);
583 i = 0;
584 while (opt_index == OPT_SPECIAL_unknown
585 && i < ARRAY_SIZE (option_map))
587 const char *opt0 = option_map[i].opt0;
588 const char *opt1 = option_map[i].opt1;
589 const char *new_prefix = option_map[i].new_prefix;
590 bool another_char_needed = option_map[i].another_char_needed;
591 size_t opt0_len = strlen (opt0);
592 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
593 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
594 size_t new_prefix_len = strlen (new_prefix);
596 extra_args = (opt1 == NULL ? 0 : 1);
597 value = !option_map[i].negated;
599 if (strncmp (argv[0], opt0, opt0_len) == 0
600 && (opt1 == NULL
601 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
602 && (!another_char_needed
603 || argv[extra_args][optn_len] != 0))
605 size_t arglen = strlen (argv[extra_args]);
606 char *dup;
608 adjust_len = (int) optn_len - (int) new_prefix_len;
609 dup = XNEWVEC (char, arglen + 1 - adjust_len);
610 memcpy (dup, new_prefix, new_prefix_len);
611 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
612 arglen - optn_len + 1);
613 opt_index = find_opt (dup + 1, lang_mask);
614 free (dup);
616 i++;
619 if (opt_index == OPT_SPECIAL_unknown)
621 arg = argv[0];
622 extra_args = 0;
623 value = 1;
624 goto done;
627 option = &cl_options[opt_index];
629 /* Reject negative form of switches that don't take negatives as
630 unrecognized. */
631 if (!value && option->cl_reject_negative)
633 opt_index = OPT_SPECIAL_unknown;
634 errors |= CL_ERR_NEGATIVE;
635 arg = argv[0];
636 goto done;
639 /* Clear the initial value for size options (it will be overwritten
640 later based on the Init(value) specification in the opt file. */
641 if (option->var_type == CLVC_SIZE)
642 value = 0;
644 result = extra_args + 1;
645 warn_message = option->warn_message;
647 /* Check to see if the option is disabled for this configuration. */
648 if (option->cl_disabled)
649 errors |= CL_ERR_DISABLED;
651 /* Determine whether there may be a separate argument based on
652 whether this option is being processed for the driver, and, if
653 so, how many such arguments. */
654 separate_arg_flag = ((option->flags & CL_SEPARATE)
655 && !(option->cl_no_driver_arg
656 && (lang_mask & CL_DRIVER)));
657 separate_args = (separate_arg_flag
658 ? option->cl_separate_nargs + 1
659 : 0);
660 joined_arg_flag = (option->flags & CL_JOINED) != 0;
662 /* Sort out any argument the switch takes. */
663 if (joined_arg_flag)
665 /* Have arg point to the original switch. This is because
666 some code, such as disable_builtin_function, expects its
667 argument to be persistent until the program exits. */
668 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
670 if (*arg == '\0' && !option->cl_missing_ok)
672 if (separate_arg_flag)
674 arg = argv[extra_args + 1];
675 result = extra_args + 2;
676 if (arg == NULL)
677 result = extra_args + 1;
678 else
679 have_separate_arg = true;
681 else
682 /* Missing argument. */
683 arg = NULL;
686 else if (separate_arg_flag)
688 arg = argv[extra_args + 1];
689 for (i = 0; i < separate_args; i++)
690 if (argv[extra_args + 1 + i] == NULL)
692 errors |= CL_ERR_MISSING_ARG;
693 break;
695 result = extra_args + 1 + i;
696 if (arg != NULL)
697 have_separate_arg = true;
700 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
701 errors |= CL_ERR_MISSING_ARG;
703 /* Is this option an alias (or an ignored option, marked as an alias
704 of OPT_SPECIAL_ignore)? */
705 if (option->alias_target != N_OPTS
706 && (!option->cl_separate_alias || have_separate_arg))
708 size_t new_opt_index = option->alias_target;
710 if (new_opt_index == OPT_SPECIAL_ignore
711 || new_opt_index == OPT_SPECIAL_warn_removed)
713 gcc_assert (option->alias_arg == NULL);
714 gcc_assert (option->neg_alias_arg == NULL);
715 opt_index = new_opt_index;
716 arg = NULL;
718 else
720 const struct cl_option *new_option = &cl_options[new_opt_index];
722 /* The new option must not be an alias itself. */
723 gcc_assert (new_option->alias_target == N_OPTS
724 || new_option->cl_separate_alias);
726 if (option->neg_alias_arg)
728 gcc_assert (option->alias_arg != NULL);
729 gcc_assert (arg == NULL);
730 gcc_assert (!option->cl_negative_alias);
731 if (value)
732 arg = option->alias_arg;
733 else
734 arg = option->neg_alias_arg;
735 value = 1;
737 else if (option->alias_arg)
739 gcc_assert (value == 1);
740 gcc_assert (arg == NULL);
741 gcc_assert (!option->cl_negative_alias);
742 arg = option->alias_arg;
745 if (option->cl_negative_alias)
746 value = !value;
748 opt_index = new_opt_index;
749 option = new_option;
751 if (value == 0)
752 gcc_assert (!option->cl_reject_negative);
754 /* Recompute what arguments are allowed. */
755 separate_arg_flag = ((option->flags & CL_SEPARATE)
756 && !(option->cl_no_driver_arg
757 && (lang_mask & CL_DRIVER)));
758 joined_arg_flag = (option->flags & CL_JOINED) != 0;
760 if (separate_args > 1 || option->cl_separate_nargs)
761 gcc_assert (separate_args
762 == (unsigned int) option->cl_separate_nargs + 1);
764 if (!(errors & CL_ERR_MISSING_ARG))
766 if (separate_arg_flag || joined_arg_flag)
768 if (option->cl_missing_ok && arg == NULL)
769 arg = "";
770 gcc_assert (arg != NULL);
772 else
773 gcc_assert (arg == NULL);
776 /* Recheck for warnings and disabled options. */
777 if (option->warn_message)
779 gcc_assert (warn_message == NULL);
780 warn_message = option->warn_message;
782 if (option->cl_disabled)
783 errors |= CL_ERR_DISABLED;
787 /* Check if this is a switch for a different front end. */
788 if (!option_ok_for_language (option, lang_mask))
789 errors |= CL_ERR_WRONG_LANG;
790 else if (strcmp (option->opt_text, "-Werror=") == 0
791 && strchr (opt_value, ',') == NULL)
793 /* Verify that -Werror argument is a valid warning
794 for a language. */
795 char *werror_arg = xstrdup (opt_value + 6);
796 werror_arg[0] = 'W';
798 size_t warning_index = find_opt (werror_arg, lang_mask);
799 free (werror_arg);
800 if (warning_index != OPT_SPECIAL_unknown)
802 const struct cl_option *warning_option
803 = &cl_options[warning_index];
804 if (!option_ok_for_language (warning_option, lang_mask))
805 errors |= CL_ERR_WRONG_LANG;
809 /* Convert the argument to lowercase if appropriate. */
810 if (arg && option->cl_tolower)
812 size_t j;
813 size_t len = strlen (arg);
814 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
816 for (j = 0; j < len; j++)
817 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
818 arg_lower[len] = 0;
819 arg = arg_lower;
822 /* If the switch takes an integer argument, convert it. */
823 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
825 int error = 0;
826 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
827 if (error)
828 errors |= CL_ERR_UINT_ARG;
830 /* Reject value out of a range. */
831 if (option->range_max != -1
832 && (value < option->range_min || value > option->range_max))
833 errors |= CL_ERR_INT_RANGE_ARG;
836 /* If the switch takes an enumerated argument, convert it. */
837 if (arg && (option->var_type == CLVC_ENUM))
839 const struct cl_enum *e = &cl_enums[option->var_enum];
841 gcc_assert (option->var_value != CLEV_NORMAL || value == 1);
842 if (option->var_value != CLEV_NORMAL)
844 const char *p = arg;
845 HOST_WIDE_INT sum_value = 0;
846 unsigned HOST_WIDE_INT used_sets = 0;
849 const char *q = strchr (p, ',');
850 HOST_WIDE_INT this_value = 0;
851 if (q && q == p)
853 errors |= CL_ERR_ENUM_SET_ARG;
854 break;
856 int idx = enum_arg_to_value (e->values, p, q ? q - p : 0,
857 &this_value, lang_mask);
858 if (idx < 0)
860 errors |= CL_ERR_ENUM_SET_ARG;
861 break;
864 HOST_WIDE_INT this_mask = 0;
865 if (option->var_value == CLEV_SET)
867 unsigned set = e->values[idx].flags >> CL_ENUM_SET_SHIFT;
868 gcc_checking_assert (set >= 1
869 && set <= HOST_BITS_PER_WIDE_INT);
870 if ((used_sets & (HOST_WIDE_INT_1U << (set - 1))) != 0)
872 errors |= CL_ERR_ENUM_SET_ARG;
873 break;
875 used_sets |= HOST_WIDE_INT_1U << (set - 1);
877 for (int i = 0; e->values[i].arg != NULL; i++)
878 if (set == (e->values[i].flags >> CL_ENUM_SET_SHIFT))
879 this_mask |= e->values[i].value;
881 else
883 gcc_assert (option->var_value == CLEV_BITSET
884 && ((e->values[idx].flags >> CL_ENUM_SET_SHIFT)
885 == 0));
886 this_mask = this_value;
889 sum_value |= this_value;
890 mask |= this_mask;
891 if (q == NULL)
892 break;
893 p = q + 1;
895 while (1);
896 if (value == 1)
897 value = sum_value;
898 else
899 gcc_checking_assert (value == 0);
901 else if (enum_arg_to_value (e->values, arg, 0, &value, lang_mask) >= 0)
903 const char *carg = NULL;
905 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
906 arg = carg;
907 gcc_assert (carg != NULL);
909 else
910 errors |= CL_ERR_ENUM_ARG;
913 done:
914 decoded->opt_index = opt_index;
915 decoded->arg = arg;
916 decoded->value = value;
917 decoded->mask = mask;
918 decoded->errors = errors;
919 decoded->warn_message = warn_message;
921 if (opt_index == OPT_SPECIAL_unknown)
922 gcc_assert (result == 1);
924 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
925 decoded->canonical_option_num_elements = result;
926 total_len = 0;
927 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
929 if (i < result)
931 size_t len;
932 if (opt_index == OPT_SPECIAL_unknown)
933 decoded->canonical_option[i] = argv[i];
934 else
935 decoded->canonical_option[i] = NULL;
936 len = strlen (argv[i]);
937 /* If the argument is an empty string, we will print it as "" in
938 orig_option_with_args_text. */
939 total_len += (len != 0 ? len : 2) + 1;
941 else
942 decoded->canonical_option[i] = NULL;
944 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore
945 && opt_index != OPT_SPECIAL_warn_removed)
947 generate_canonical_option (opt_index, arg, value, decoded);
948 if (separate_args > 1)
950 for (i = 0; i < separate_args; i++)
952 if (argv[extra_args + 1 + i] == NULL)
953 break;
954 else
955 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
957 gcc_assert (result == 1 + i);
958 decoded->canonical_option_num_elements = result;
961 decoded->orig_option_with_args_text
962 = p = XOBNEWVEC (&opts_obstack, char, total_len);
963 for (i = 0; i < result; i++)
965 size_t len = strlen (argv[i]);
967 /* Print the empty string verbally. */
968 if (len == 0)
970 *p++ = '"';
971 *p++ = '"';
973 else
974 memcpy (p, argv[i], len);
975 p += len;
976 if (i == result - 1)
977 *p++ = 0;
978 else
979 *p++ = ' ';
982 return result;
985 /* Obstack for option strings. */
987 struct obstack opts_obstack;
989 /* Like libiberty concat, but allocate using opts_obstack. */
991 char *
992 opts_concat (const char *first, ...)
994 char *newstr, *end;
995 size_t length = 0;
996 const char *arg;
997 va_list ap;
999 /* First compute the size of the result and get sufficient memory. */
1000 va_start (ap, first);
1001 for (arg = first; arg; arg = va_arg (ap, const char *))
1002 length += strlen (arg);
1003 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
1004 va_end (ap);
1006 /* Now copy the individual pieces to the result string. */
1007 va_start (ap, first);
1008 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
1010 length = strlen (arg);
1011 memcpy (end, arg, length);
1012 end += length;
1014 *end = '\0';
1015 va_end (ap);
1016 return newstr;
1019 /* Decode command-line options (ARGC and ARGV being the arguments of
1020 main) into an array, setting *DECODED_OPTIONS to a pointer to that
1021 array and *DECODED_OPTIONS_COUNT to the number of entries in the
1022 array. The first entry in the array is always one for the program
1023 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
1024 flags applicable for decoding (including CL_COMMON and CL_TARGET if
1025 those options should be considered applicable). Do not produce any
1026 diagnostics or set state outside of these variables. */
1028 void
1029 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
1030 unsigned int lang_mask,
1031 struct cl_decoded_option **decoded_options,
1032 unsigned int *decoded_options_count)
1034 unsigned int n, i;
1035 struct cl_decoded_option *opt_array;
1036 unsigned int num_decoded_options;
1038 int opt_array_len = argc;
1039 opt_array = XNEWVEC (struct cl_decoded_option, opt_array_len);
1041 opt_array[0].opt_index = OPT_SPECIAL_program_name;
1042 opt_array[0].warn_message = NULL;
1043 opt_array[0].arg = argv[0];
1044 opt_array[0].orig_option_with_args_text = argv[0];
1045 opt_array[0].canonical_option_num_elements = 1;
1046 opt_array[0].canonical_option[0] = argv[0];
1047 opt_array[0].canonical_option[1] = NULL;
1048 opt_array[0].canonical_option[2] = NULL;
1049 opt_array[0].canonical_option[3] = NULL;
1050 opt_array[0].value = 1;
1051 opt_array[0].mask = 0;
1052 opt_array[0].errors = 0;
1053 num_decoded_options = 1;
1055 for (i = 1; i < argc; i += n)
1057 const char *opt = argv[i];
1059 /* Interpret "-" or a non-switch as a file name. */
1060 if (opt[0] != '-' || opt[1] == '\0')
1062 generate_option_input_file (opt, &opt_array[num_decoded_options]);
1063 num_decoded_options++;
1064 n = 1;
1065 continue;
1068 /* Interpret "--param" "key=name" as "--param=key=name". */
1069 const char *needle = "--param";
1070 if (i + 1 < argc && strcmp (opt, needle) == 0)
1072 const char *replacement
1073 = opts_concat (needle, "=", argv[i + 1], NULL);
1074 argv[++i] = replacement;
1077 /* Expand -fdiagnostics-plain-output to its constituents. This needs
1078 to happen here so that prune_options can handle -fdiagnostics-color
1079 specially. */
1080 if (!strcmp (opt, "-fdiagnostics-plain-output"))
1082 /* If you have changed the default diagnostics output, and this new
1083 output is not appropriately "plain" (e.g., the change needs to be
1084 undone in order for the testsuite to work properly), then please do
1085 the following:
1086 1. Add the necessary option to undo the new behavior to
1087 the array below.
1088 2. Update the documentation for -fdiagnostics-plain-output
1089 in invoke.texi. */
1090 const char *const expanded_args[] = {
1091 "-fno-diagnostics-show-caret",
1092 "-fno-diagnostics-show-line-numbers",
1093 "-fdiagnostics-color=never",
1094 "-fdiagnostics-urls=never",
1095 "-fdiagnostics-path-format=separate-events",
1096 "-fdiagnostics-text-art-charset=none",
1097 "-fno-diagnostics-show-event-links"
1098 /* We don't put "-fno-diagnostics-show-highlight-colors" here
1099 as -fdiagnostics-color=never makes it redundant. */
1101 const int num_expanded = ARRAY_SIZE (expanded_args);
1102 opt_array_len += num_expanded - 1;
1103 opt_array = XRESIZEVEC (struct cl_decoded_option,
1104 opt_array, opt_array_len);
1105 for (int j = 0, nj; j < num_expanded; j += nj)
1107 nj = decode_cmdline_option (expanded_args + j, lang_mask,
1108 &opt_array[num_decoded_options]);
1109 num_decoded_options++;
1112 n = 1;
1113 continue;
1116 n = decode_cmdline_option (argv + i, lang_mask,
1117 &opt_array[num_decoded_options]);
1118 num_decoded_options++;
1121 *decoded_options = opt_array;
1122 *decoded_options_count = num_decoded_options;
1123 prune_options (decoded_options, decoded_options_count);
1126 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
1127 next one is the same as ORIG_NEXT_OPT_IDX. */
1129 static bool
1130 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
1132 /* An option can be canceled by the same option or an option with
1133 Negative. */
1134 if (cl_options [next_opt_idx].neg_index == opt_idx)
1135 return true;
1137 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
1138 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
1139 orig_next_opt_idx);
1141 return false;
1144 /* Filter out options canceled by the ones after them, and related
1145 rearrangement. */
1147 static void
1148 prune_options (struct cl_decoded_option **decoded_options,
1149 unsigned int *decoded_options_count)
1151 unsigned int old_decoded_options_count = *decoded_options_count;
1152 struct cl_decoded_option *old_decoded_options = *decoded_options;
1153 unsigned int new_decoded_options_count;
1154 struct cl_decoded_option *new_decoded_options
1155 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
1156 unsigned int i;
1157 const struct cl_option *option;
1158 unsigned int options_to_prepend = 0;
1159 unsigned int Wcomplain_wrong_lang_idx = 0;
1160 unsigned int fdiagnostics_color_idx = 0;
1161 unsigned int fdiagnostics_urls_idx = 0;
1163 /* Remove arguments which are negated by others after them. */
1164 new_decoded_options_count = 0;
1165 for (i = 0; i < old_decoded_options_count; i++)
1167 unsigned int j, opt_idx, next_opt_idx;
1169 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
1170 goto keep;
1172 opt_idx = old_decoded_options[i].opt_index;
1173 switch (opt_idx)
1175 case OPT_SPECIAL_unknown:
1176 case OPT_SPECIAL_ignore:
1177 case OPT_SPECIAL_warn_removed:
1178 case OPT_SPECIAL_program_name:
1179 case OPT_SPECIAL_input_file:
1180 goto keep;
1182 /* Do not handle the following yet, just remember the last one. */
1183 case OPT_Wcomplain_wrong_lang:
1184 gcc_checking_assert (i != 0);
1185 if (Wcomplain_wrong_lang_idx == 0)
1186 ++options_to_prepend;
1187 Wcomplain_wrong_lang_idx = i;
1188 continue;
1189 case OPT_fdiagnostics_color_:
1190 gcc_checking_assert (i != 0);
1191 if (fdiagnostics_color_idx == 0)
1192 ++options_to_prepend;
1193 fdiagnostics_color_idx = i;
1194 continue;
1195 case OPT_fdiagnostics_urls_:
1196 gcc_checking_assert (i != 0);
1197 if (fdiagnostics_urls_idx == 0)
1198 ++options_to_prepend;
1199 fdiagnostics_urls_idx = i;
1200 continue;
1202 default:
1203 gcc_assert (opt_idx < cl_options_count);
1204 option = &cl_options[opt_idx];
1205 if (option->neg_index < 0)
1206 goto keep;
1208 /* Skip joined switches. */
1209 if ((option->flags & CL_JOINED)
1210 && (!option->cl_reject_negative
1211 || (unsigned int) option->neg_index != opt_idx))
1212 goto keep;
1214 for (j = i + 1; j < old_decoded_options_count; j++)
1216 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1217 continue;
1218 next_opt_idx = old_decoded_options[j].opt_index;
1219 if (next_opt_idx >= cl_options_count)
1220 continue;
1221 if (cl_options[next_opt_idx].neg_index < 0)
1222 continue;
1223 if ((cl_options[next_opt_idx].flags & CL_JOINED)
1224 && (!cl_options[next_opt_idx].cl_reject_negative
1225 || ((unsigned int) cl_options[next_opt_idx].neg_index
1226 != next_opt_idx)))
1227 continue;
1228 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
1229 break;
1231 if (j == old_decoded_options_count)
1233 keep:
1234 new_decoded_options[new_decoded_options_count]
1235 = old_decoded_options[i];
1236 new_decoded_options_count++;
1238 break;
1242 /* For those not yet handled, put (only) the last at a front position after
1243 'argv[0]', so they can take effect immediately. */
1244 if (options_to_prepend)
1246 const unsigned int argv_0 = 1;
1247 memmove (new_decoded_options + argv_0 + options_to_prepend,
1248 new_decoded_options + argv_0,
1249 sizeof (struct cl_decoded_option)
1250 * (new_decoded_options_count - argv_0));
1251 unsigned int options_prepended = 0;
1252 if (Wcomplain_wrong_lang_idx != 0)
1254 new_decoded_options[argv_0 + options_prepended++]
1255 = old_decoded_options[Wcomplain_wrong_lang_idx];
1256 new_decoded_options_count++;
1258 if (fdiagnostics_color_idx != 0)
1260 new_decoded_options[argv_0 + options_prepended++]
1261 = old_decoded_options[fdiagnostics_color_idx];
1262 new_decoded_options_count++;
1264 if (fdiagnostics_urls_idx != 0)
1266 new_decoded_options[argv_0 + options_prepended++]
1267 = old_decoded_options[fdiagnostics_urls_idx];
1268 new_decoded_options_count++;
1270 gcc_checking_assert (options_to_prepend == options_prepended);
1273 free (old_decoded_options);
1274 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1275 new_decoded_options,
1276 new_decoded_options_count);
1277 *decoded_options = new_decoded_options;
1278 *decoded_options_count = new_decoded_options_count;
1281 /* Handle option DECODED for the language indicated by LANG_MASK,
1282 using the handlers in HANDLERS and setting fields in OPTS and
1283 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1284 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1285 option for options from the source file, UNKNOWN_LOCATION
1286 otherwise. GENERATED_P is true for an option generated as part of
1287 processing another option or otherwise generated internally, false
1288 for one explicitly passed by the user. control_warning_option
1289 generated options are considered explicitly passed by the user.
1290 Returns false if the switch was invalid. DC is the diagnostic
1291 context for options affecting diagnostics state, or NULL. */
1293 static bool
1294 handle_option (struct gcc_options *opts,
1295 struct gcc_options *opts_set,
1296 const struct cl_decoded_option *decoded,
1297 unsigned int lang_mask, int kind, location_t loc,
1298 const struct cl_option_handlers *handlers,
1299 bool generated_p, diagnostic_context *dc)
1301 size_t opt_index = decoded->opt_index;
1302 const char *arg = decoded->arg;
1303 HOST_WIDE_INT value = decoded->value;
1304 HOST_WIDE_INT mask = decoded->mask;
1305 const struct cl_option *option = &cl_options[opt_index];
1306 void *flag_var = option_flag_var (opt_index, opts);
1307 size_t i;
1309 if (flag_var)
1310 set_option (opts, (generated_p ? NULL : opts_set),
1311 opt_index, value, arg, kind, loc, dc, mask);
1313 for (i = 0; i < handlers->num_handlers; i++)
1314 if (option->flags & handlers->handlers[i].mask)
1316 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1317 lang_mask, kind, loc,
1318 handlers, dc,
1319 handlers->target_option_override_hook))
1320 return false;
1323 return true;
1326 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1327 option instead of DECODED. This is used for callbacks when one
1328 option implies another instead of an option being decoded from the
1329 command line. */
1331 bool
1332 handle_generated_option (struct gcc_options *opts,
1333 struct gcc_options *opts_set,
1334 size_t opt_index, const char *arg, HOST_WIDE_INT value,
1335 unsigned int lang_mask, int kind, location_t loc,
1336 const struct cl_option_handlers *handlers,
1337 bool generated_p, diagnostic_context *dc)
1339 struct cl_decoded_option decoded;
1341 generate_option (opt_index, arg, value, lang_mask, &decoded);
1342 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1343 handlers, generated_p, dc);
1346 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1347 VALUE for a front end using LANG_MASK. This is used when the
1348 compiler generates options internally. */
1350 void
1351 generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
1352 unsigned int lang_mask, struct cl_decoded_option *decoded)
1354 const struct cl_option *option = &cl_options[opt_index];
1356 decoded->opt_index = opt_index;
1357 decoded->warn_message = NULL;
1358 decoded->arg = arg;
1359 decoded->value = value;
1360 decoded->mask = 0;
1361 decoded->errors = (option_ok_for_language (option, lang_mask)
1363 : CL_ERR_WRONG_LANG);
1365 generate_canonical_option (opt_index, arg, value, decoded);
1366 switch (decoded->canonical_option_num_elements)
1368 case 1:
1369 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1370 break;
1372 case 2:
1373 decoded->orig_option_with_args_text
1374 = opts_concat (decoded->canonical_option[0], " ",
1375 decoded->canonical_option[1], NULL);
1376 break;
1378 default:
1379 gcc_unreachable ();
1383 /* Fill in *DECODED with an option for input file FILE. */
1385 void
1386 generate_option_input_file (const char *file,
1387 struct cl_decoded_option *decoded)
1389 decoded->opt_index = OPT_SPECIAL_input_file;
1390 decoded->warn_message = NULL;
1391 decoded->arg = file;
1392 decoded->orig_option_with_args_text = file;
1393 decoded->canonical_option_num_elements = 1;
1394 decoded->canonical_option[0] = file;
1395 decoded->canonical_option[1] = NULL;
1396 decoded->canonical_option[2] = NULL;
1397 decoded->canonical_option[3] = NULL;
1398 decoded->value = 1;
1399 decoded->mask = 0;
1400 decoded->errors = 0;
1403 /* Helper function for listing valid choices and hint for misspelled
1404 value. CANDIDATES is a vector containing all valid strings,
1405 STR is set to a heap allocated string that contains all those
1406 strings concatenated, separated by spaces, and the return value
1407 is the closest string from those to ARG, or NULL if nothing is
1408 close enough. Callers should XDELETEVEC (STR) after using it
1409 to avoid memory leaks. */
1411 const char *
1412 candidates_list_and_hint (const char *arg, char *&str,
1413 const auto_vec <const char *> &candidates)
1415 size_t len = 0;
1416 int i;
1417 const char *candidate;
1418 char *p;
1420 gcc_assert (!candidates.is_empty ());
1422 FOR_EACH_VEC_ELT (candidates, i, candidate)
1423 len += strlen (candidate) + 1;
1425 str = p = XNEWVEC (char, len);
1426 FOR_EACH_VEC_ELT (candidates, i, candidate)
1428 len = strlen (candidate);
1429 memcpy (p, candidate, len);
1430 p[len] = ' ';
1431 p += len + 1;
1433 p[-1] = '\0';
1434 return find_closest_string (arg, &candidates);
1437 /* Perform diagnostics for read_cmdline_option and control_warning_option
1438 functions. Returns true if an error has been diagnosed.
1439 LOC and LANG_MASK arguments like in read_cmdline_option.
1440 OPTION is the option to report diagnostics for, OPT the name
1441 of the option as text, ARG the argument of the option (for joined
1442 options), ERRORS is bitmask of CL_ERR_* values. */
1444 static bool
1445 cmdline_handle_error (location_t loc, const struct cl_option *option,
1446 const char *opt, const char *arg, int errors,
1447 unsigned int lang_mask)
1449 if (errors & CL_ERR_DISABLED)
1451 error_at (loc, "command-line option %qs"
1452 " is not supported by this configuration", opt);
1453 return true;
1456 if (errors & CL_ERR_MISSING_ARG)
1458 if (option->missing_argument_error)
1459 error_at (loc, option->missing_argument_error, opt);
1460 else
1461 error_at (loc, "missing argument to %qs", opt);
1462 return true;
1465 if (errors & CL_ERR_UINT_ARG)
1467 if (option->cl_byte_size)
1468 error_at (loc, "argument to %qs should be a non-negative integer "
1469 "optionally followed by a size unit",
1470 option->opt_text);
1471 else
1472 error_at (loc, "argument to %qs should be a non-negative integer",
1473 option->opt_text);
1474 return true;
1477 if (errors & CL_ERR_INT_RANGE_ARG)
1479 error_at (loc, "argument to %qs is not between %d and %d",
1480 option->opt_text, option->range_min, option->range_max);
1481 return true;
1484 if (errors & CL_ERR_ENUM_SET_ARG)
1486 const struct cl_enum *e = &cl_enums[option->var_enum];
1487 const char *p = arg;
1488 unsigned HOST_WIDE_INT used_sets = 0;
1489 const char *second_opt = NULL;
1490 size_t second_opt_len = 0;
1491 errors = 0;
1494 const char *q = strchr (p, ',');
1495 HOST_WIDE_INT this_value = 0;
1496 if (q && q == p)
1498 arg = "";
1499 errors = CL_ERR_ENUM_ARG;
1500 break;
1502 int idx = enum_arg_to_value (e->values, p, q ? q - p : 0,
1503 &this_value, lang_mask);
1504 if (idx < 0)
1506 if (q == NULL)
1507 q = strchr (p, '\0');
1508 char *narg = XALLOCAVEC (char, (q - p) + 1);
1509 memcpy (narg, p, q - p);
1510 narg[q - p] = '\0';
1511 arg = narg;
1512 errors = CL_ERR_ENUM_ARG;
1513 break;
1516 if (option->var_value == CLEV_BITSET)
1518 if (q == NULL)
1519 break;
1520 p = q + 1;
1521 continue;
1524 unsigned set = e->values[idx].flags >> CL_ENUM_SET_SHIFT;
1525 gcc_checking_assert (set >= 1 && set <= HOST_BITS_PER_WIDE_INT);
1526 if ((used_sets & (HOST_WIDE_INT_1U << (set - 1))) != 0)
1528 if (q == NULL)
1529 q = strchr (p, '\0');
1530 if (second_opt == NULL)
1532 used_sets = HOST_WIDE_INT_1U << (set - 1);
1533 second_opt = p;
1534 second_opt_len = q - p;
1535 p = arg;
1536 continue;
1538 char *args = XALLOCAVEC (char, (q - p) + 1 + second_opt_len + 1);
1539 memcpy (args, p, q - p);
1540 args[q - p] = '\0';
1541 memcpy (args + (q - p) + 1, second_opt, second_opt_len);
1542 args[(q - p) + 1 + second_opt_len] = '\0';
1543 error_at (loc, "invalid argument in option %qs", opt);
1544 if (strcmp (args, args + (q - p) + 1) == 0)
1545 inform (loc, "%qs specified multiple times in the same option",
1546 args);
1547 else
1548 inform (loc, "%qs is mutually exclusive with %qs and cannot be"
1549 " specified together", args, args + (q - p) + 1);
1550 return true;
1552 used_sets |= HOST_WIDE_INT_1U << (set - 1);
1553 if (q == NULL)
1554 break;
1555 p = q + 1;
1557 while (1);
1560 if (errors & CL_ERR_ENUM_ARG)
1562 const struct cl_enum *e = &cl_enums[option->var_enum];
1563 unsigned int i;
1564 char *s;
1566 auto_diagnostic_group d;
1567 if (e->unknown_error)
1568 error_at (loc, e->unknown_error, arg);
1569 else
1570 error_at (loc, "unrecognized argument in option %qs", opt);
1572 auto_vec <const char *> candidates;
1573 for (i = 0; e->values[i].arg != NULL; i++)
1575 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1576 continue;
1577 candidates.safe_push (e->values[i].arg);
1579 const char *hint = candidates_list_and_hint (arg, s, candidates);
1580 if (hint)
1581 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1582 option->opt_text, s, hint);
1583 else
1584 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1585 XDELETEVEC (s);
1587 return true;
1590 return false;
1593 /* Handle the switch DECODED (location LOC) for the language indicated
1594 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1595 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1596 diagnostic options. */
1598 void
1599 read_cmdline_option (struct gcc_options *opts,
1600 struct gcc_options *opts_set,
1601 struct cl_decoded_option *decoded,
1602 location_t loc,
1603 unsigned int lang_mask,
1604 const struct cl_option_handlers *handlers,
1605 diagnostic_context *dc)
1607 const struct cl_option *option;
1608 const char *opt = decoded->orig_option_with_args_text;
1610 if (decoded->warn_message)
1611 warning_at (loc, 0, decoded->warn_message, opt);
1613 if (decoded->opt_index == OPT_SPECIAL_unknown)
1615 if (handlers->unknown_option_callback (decoded))
1616 error_at (loc, "unrecognized command-line option %qs", decoded->arg);
1617 return;
1620 if (decoded->opt_index == OPT_SPECIAL_ignore)
1621 return;
1623 if (decoded->opt_index == OPT_SPECIAL_warn_removed)
1625 /* Warn only about positive ignored options. */
1626 if (decoded->value)
1627 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1628 return;
1631 option = &cl_options[decoded->opt_index];
1633 if (decoded->errors
1634 && cmdline_handle_error (loc, option, opt, decoded->arg,
1635 decoded->errors, lang_mask))
1636 return;
1638 if (decoded->errors & CL_ERR_WRONG_LANG)
1640 handlers->wrong_lang_callback (decoded, lang_mask);
1641 return;
1644 gcc_assert (!decoded->errors);
1646 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1647 loc, handlers, false, dc))
1648 error_at (loc, "unrecognized command-line option %qs", opt);
1651 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1652 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1653 location LOC, using diagnostic context DC if not NULL for
1654 diagnostic classification. */
1656 void
1657 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1658 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
1659 location_t loc, diagnostic_context *dc,
1660 HOST_WIDE_INT mask /* = 0 */)
1662 const struct cl_option *option = &cl_options[opt_index];
1663 void *flag_var = option_flag_var (opt_index, opts);
1664 void *set_flag_var = NULL;
1666 if (!flag_var)
1667 return;
1669 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1670 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1672 if (opts_set != NULL)
1673 set_flag_var = option_flag_var (opt_index, opts_set);
1675 switch (option->var_type)
1677 case CLVC_INTEGER:
1678 if (option->cl_host_wide_int)
1680 *(HOST_WIDE_INT *) flag_var = value;
1681 if (set_flag_var)
1682 *(HOST_WIDE_INT *) set_flag_var = 1;
1684 else
1686 if (value > INT_MAX)
1687 error_at (loc, "argument to %qs is bigger than %d",
1688 option->opt_text, INT_MAX);
1689 else
1691 *(int *) flag_var = value;
1692 if (set_flag_var)
1693 *(int *) set_flag_var = 1;
1697 break;
1699 case CLVC_SIZE:
1700 if (option->cl_host_wide_int)
1702 *(HOST_WIDE_INT *) flag_var = value;
1703 if (set_flag_var)
1704 *(HOST_WIDE_INT *) set_flag_var = value;
1706 else
1708 *(int *) flag_var = value;
1709 if (set_flag_var)
1710 *(int *) set_flag_var = value;
1713 break;
1715 case CLVC_EQUAL:
1716 if (option->cl_host_wide_int)
1718 *(HOST_WIDE_INT *) flag_var = (value
1719 ? option->var_value
1720 : !option->var_value);
1721 if (set_flag_var)
1722 *(HOST_WIDE_INT *) set_flag_var = 1;
1724 else
1726 *(int *) flag_var = (value
1727 ? option->var_value
1728 : !option->var_value);
1729 if (set_flag_var)
1730 *(int *) set_flag_var = 1;
1732 break;
1734 case CLVC_BIT_CLEAR:
1735 case CLVC_BIT_SET:
1736 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1738 if (option->cl_host_wide_int)
1739 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1740 else
1741 *(int *) flag_var |= option->var_value;
1743 else
1745 if (option->cl_host_wide_int)
1746 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1747 else
1748 *(int *) flag_var &= ~option->var_value;
1750 if (set_flag_var)
1752 if (option->cl_host_wide_int)
1753 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1754 else
1755 *(int *) set_flag_var |= option->var_value;
1757 break;
1759 case CLVC_STRING:
1760 *(const char **) flag_var = arg;
1761 if (set_flag_var)
1762 *(const char **) set_flag_var = "";
1763 break;
1765 case CLVC_ENUM:
1767 const struct cl_enum *e = &cl_enums[option->var_enum];
1769 if (mask)
1770 e->set (flag_var, value | (e->get (flag_var) & ~mask));
1771 else
1772 e->set (flag_var, value);
1773 if (set_flag_var)
1774 e->set (set_flag_var, 1);
1776 break;
1778 case CLVC_DEFER:
1780 vec<cl_deferred_option> *v
1781 = (vec<cl_deferred_option> *) *(void **) flag_var;
1782 cl_deferred_option p = {opt_index, arg, value};
1783 if (!v)
1784 v = XCNEW (vec<cl_deferred_option>);
1785 v->safe_push (p);
1786 *(void **) flag_var = v;
1787 if (set_flag_var)
1788 *(void **) set_flag_var = v;
1790 break;
1794 /* Return the address of the flag variable for option OPT_INDEX in
1795 options structure OPTS, or NULL if there is no flag variable. */
1797 void *
1798 option_flag_var (int opt_index, struct gcc_options *opts)
1800 const struct cl_option *option = &cl_options[opt_index];
1802 if (option->flag_var_offset == (unsigned short) -1)
1803 return NULL;
1804 return (void *)(((char *) opts) + option->flag_var_offset);
1807 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1808 or -1 if it isn't a simple on-off switch
1809 (or if the value is unknown, typically set later in target). */
1812 option_enabled (int opt_idx, unsigned lang_mask, void *opts)
1814 const struct cl_option *option = &(cl_options[opt_idx]);
1816 /* A language-specific option can only be considered enabled when it's
1817 valid for the current language. */
1818 if (!(option->flags & CL_COMMON)
1819 && (option->flags & CL_LANG_ALL)
1820 && !(option->flags & lang_mask))
1821 return 0;
1823 struct gcc_options *optsg = (struct gcc_options *) opts;
1824 void *flag_var = option_flag_var (opt_idx, optsg);
1826 if (flag_var)
1827 switch (option->var_type)
1829 case CLVC_INTEGER:
1830 if (option->cl_host_wide_int)
1832 HOST_WIDE_INT v = *(HOST_WIDE_INT *) flag_var;
1833 return v != 0 ? (v < 0 ? -1 : 1) : 0;
1835 else
1837 int v = *(int *) flag_var;
1838 return v != 0 ? (v < 0 ? -1 : 1) : 0;
1841 case CLVC_EQUAL:
1842 if (option->cl_host_wide_int)
1843 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1844 else
1845 return *(int *) flag_var == option->var_value;
1847 case CLVC_BIT_CLEAR:
1848 if (option->cl_host_wide_int)
1849 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1850 else
1851 return (*(int *) flag_var & option->var_value) == 0;
1853 case CLVC_BIT_SET:
1854 if (option->cl_host_wide_int)
1855 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1856 else
1857 return (*(int *) flag_var & option->var_value) != 0;
1859 case CLVC_SIZE:
1860 if (option->cl_host_wide_int)
1861 return *(HOST_WIDE_INT *) flag_var != -1;
1862 else
1863 return *(int *) flag_var != -1;
1865 case CLVC_STRING:
1866 case CLVC_ENUM:
1867 case CLVC_DEFER:
1868 break;
1870 return -1;
1874 compiler_diagnostic_option_manager::
1875 option_enabled_p (diagnostic_option_id opt_id) const
1877 return option_enabled (opt_id.m_idx, m_lang_mask, m_opts);
1880 /* Fill STATE with the current state of option OPTION in OPTS. Return
1881 true if there is some state to store. */
1883 bool
1884 get_option_state (struct gcc_options *opts, int option,
1885 struct cl_option_state *state)
1887 void *flag_var = option_flag_var (option, opts);
1889 if (flag_var == 0)
1890 return false;
1892 switch (cl_options[option].var_type)
1894 case CLVC_INTEGER:
1895 case CLVC_EQUAL:
1896 case CLVC_SIZE:
1897 state->data = flag_var;
1898 state->size = (cl_options[option].cl_host_wide_int
1899 ? sizeof (HOST_WIDE_INT)
1900 : sizeof (int));
1901 break;
1903 case CLVC_BIT_CLEAR:
1904 case CLVC_BIT_SET:
1905 state->ch = option_enabled (option, -1, opts);
1906 state->data = &state->ch;
1907 state->size = 1;
1908 break;
1910 case CLVC_STRING:
1911 state->data = *(const char **) flag_var;
1912 if (state->data == 0)
1913 state->data = "";
1914 state->size = strlen ((const char *) state->data) + 1;
1915 break;
1917 case CLVC_ENUM:
1918 state->data = flag_var;
1919 state->size = cl_enums[cl_options[option].var_enum].var_size;
1920 break;
1922 case CLVC_DEFER:
1923 return false;
1925 return true;
1928 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1929 handlers HANDLERS) to have diagnostic kind KIND for option
1930 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1931 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1932 argument of the option for joined options, or NULL otherwise. If IMPLY,
1933 the warning option in question is implied at this point. This is
1934 used by -Werror= and #pragma GCC diagnostic. */
1936 void
1937 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1938 bool imply, location_t loc, unsigned int lang_mask,
1939 const struct cl_option_handlers *handlers,
1940 struct gcc_options *opts,
1941 struct gcc_options *opts_set,
1942 diagnostic_context *dc)
1944 if (cl_options[opt_index].alias_target != N_OPTS)
1946 gcc_assert (!cl_options[opt_index].cl_separate_alias
1947 && !cl_options[opt_index].cl_negative_alias);
1948 if (cl_options[opt_index].alias_arg)
1949 arg = cl_options[opt_index].alias_arg;
1950 opt_index = cl_options[opt_index].alias_target;
1952 if (opt_index == OPT_SPECIAL_ignore || opt_index == OPT_SPECIAL_warn_removed)
1953 return;
1954 if (dc)
1955 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1956 if (imply)
1958 /* -Werror=foo implies -Wfoo. */
1959 const struct cl_option *option = &cl_options[opt_index];
1960 HOST_WIDE_INT value = 1;
1962 if (option->var_type == CLVC_INTEGER
1963 || option->var_type == CLVC_ENUM
1964 || option->var_type == CLVC_SIZE)
1967 if (arg && *arg == '\0' && !option->cl_missing_ok)
1968 arg = NULL;
1970 if ((option->flags & CL_JOINED) && arg == NULL)
1972 cmdline_handle_error (loc, option, option->opt_text, arg,
1973 CL_ERR_MISSING_ARG, lang_mask);
1974 return;
1977 /* If the switch takes an integer argument, convert it. */
1978 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
1980 int error = 0;
1981 value = *arg ? integral_argument (arg, &error,
1982 option->cl_byte_size) : 0;
1983 if (error)
1985 cmdline_handle_error (loc, option, option->opt_text, arg,
1986 CL_ERR_UINT_ARG, lang_mask);
1987 return;
1991 /* If the switch takes an enumerated argument, convert it. */
1992 if (arg && option->var_type == CLVC_ENUM)
1994 const struct cl_enum *e = &cl_enums[option->var_enum];
1996 if (enum_arg_to_value (e->values, arg, 0, &value,
1997 lang_mask) >= 0)
1999 const char *carg = NULL;
2001 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
2002 arg = carg;
2003 gcc_assert (carg != NULL);
2005 else
2007 cmdline_handle_error (loc, option, option->opt_text, arg,
2008 CL_ERR_ENUM_ARG, lang_mask);
2009 return;
2014 handle_generated_option (opts, opts_set,
2015 opt_index, arg, value, lang_mask,
2016 kind, loc, handlers, false, dc);
2020 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
2021 Store number of arguments into ARGC_P. */
2023 void
2024 parse_options_from_collect_gcc_options (const char *collect_gcc_options,
2025 obstack *argv_obstack,
2026 int *argc_p)
2028 char *argv_storage = xstrdup (collect_gcc_options);
2029 int j, k;
2031 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
2033 if (argv_storage[j] == '\'')
2035 obstack_ptr_grow (argv_obstack, &argv_storage[k]);
2036 ++j;
2039 if (argv_storage[j] == '\0')
2040 fatal_error (input_location,
2041 "malformed %<COLLECT_GCC_OPTIONS%>");
2042 else if (startswith (&argv_storage[j], "'\\''"))
2044 argv_storage[k++] = '\'';
2045 j += 4;
2047 else if (argv_storage[j] == '\'')
2048 break;
2049 else
2050 argv_storage[k++] = argv_storage[j++];
2052 while (1);
2053 argv_storage[k++] = '\0';
2057 obstack_ptr_grow (argv_obstack, NULL);
2058 *argc_p = obstack_object_size (argv_obstack) / sizeof (void *) - 1;
2061 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
2062 and push on O. */
2064 void prepend_xassembler_to_collect_as_options (const char *collect_as_options,
2065 obstack *o)
2067 obstack opts_obstack;
2068 int opts_count;
2070 obstack_init (&opts_obstack);
2071 parse_options_from_collect_gcc_options (collect_as_options,
2072 &opts_obstack, &opts_count);
2073 const char **assembler_opts = XOBFINISH (&opts_obstack, const char **);
2075 for (int i = 0; i < opts_count; i++)
2077 obstack_grow (o, " '-Xassembler' ",
2078 strlen (" '-Xassembler' "));
2079 const char *opt = assembler_opts[i];
2080 obstack_1grow (o, '\'');
2081 obstack_grow (o, opt, strlen (opt));
2082 obstack_1grow (o, '\'');
2086 jobserver_info::jobserver_info ()
2088 /* Traditionally, GNU make uses opened pipes for jobserver-auth,
2089 e.g. --jobserver-auth=3,4.
2090 Starting with GNU make 4.4, one can use --jobserver-style=fifo
2091 and then named pipe is used: --jobserver-auth=fifo:/tmp/hcsparta. */
2093 /* Detect jobserver and drop it if it's not working. */
2094 string js_needle = "--jobserver-auth=";
2095 string fifo_prefix = "fifo:";
2097 const char *envval = getenv ("MAKEFLAGS");
2098 if (envval != NULL)
2100 string makeflags = envval;
2101 size_t n = makeflags.rfind (js_needle);
2102 if (n != string::npos)
2104 string ending = makeflags.substr (n + js_needle.size ());
2105 if (ending.find (fifo_prefix) == 0)
2107 ending = ending.substr (fifo_prefix.size ());
2108 pipe_path = ending.substr (0, ending.find (' '));
2109 is_active = true;
2111 else if (sscanf (makeflags.c_str () + n + js_needle.size (),
2112 "%d,%d", &rfd, &wfd) == 2
2113 && rfd > 0
2114 && wfd > 0
2115 && is_valid_fd (rfd)
2116 && is_valid_fd (wfd))
2117 is_active = true;
2118 else
2120 string dup = makeflags.substr (0, n);
2121 size_t pos = makeflags.find (' ', n);
2122 if (pos != string::npos)
2123 dup += makeflags.substr (pos);
2124 skipped_makeflags = "MAKEFLAGS=" + dup;
2125 error_msg
2126 = "cannot access %<" + js_needle + "%> file descriptors";
2129 error_msg = "%<" + js_needle + "%> is not present in %<MAKEFLAGS%>";
2131 else
2132 error_msg = "%<MAKEFLAGS%> environment variable is unset";
2134 if (!error_msg.empty ())
2135 error_msg = "jobserver is not available: " + error_msg;
2138 void
2139 jobserver_info::connect ()
2141 if (!pipe_path.empty ())
2143 #if HOST_HAS_O_NONBLOCK
2144 pipefd = open (pipe_path.c_str (), O_RDWR | O_NONBLOCK);
2145 is_connected = true;
2146 #else
2147 is_connected = false;
2148 #endif
2150 else
2151 is_connected = true;
2154 void
2155 jobserver_info::disconnect ()
2157 if (!pipe_path.empty ())
2159 gcc_assert (close (pipefd) == 0);
2160 pipefd = -1;
2164 bool
2165 jobserver_info::get_token ()
2167 int fd = pipe_path.empty () ? rfd : pipefd;
2168 char c;
2169 unsigned n = read (fd, &c, 1);
2170 if (n != 1)
2172 gcc_assert (errno == EAGAIN);
2173 return false;
2175 else
2176 return true;
2179 void
2180 jobserver_info::return_token ()
2182 int fd = pipe_path.empty () ? wfd : pipefd;
2183 char c = 'G';
2184 gcc_assert (write (fd, &c, 1) == 1);