]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/opts-common.c
PR c++/89705 - ICE with reference binding with conversion function.
[thirdparty/gcc.git] / gcc / opts-common.c
CommitLineData
a1baa5f1 1/* Command line option handling.
fbd26352 2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
a1baa5f1 3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
a1baa5f1 9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
a1baa5f1 19
20#include "config.h"
21#include "system.h"
22#include "intl.h"
23#include "coretypes.h"
24#include "opts.h"
1eacc14a 25#include "options.h"
b78351e5 26#include "diagnostic.h"
56f34f03 27#include "spellcheck.h"
a1baa5f1 28
e62df35b 29static void prune_options (struct cl_decoded_option **, unsigned int *);
30
a1408eb3 31/* An option that is undocumented, that takes a joined argument, and
32 that doesn't fit any of the classes of uses (language/common,
33 driver, target) is assumed to be a prefix used to catch
34 e.g. negated options, and stop them from being further shortened to
35 a prefix that could use the negated option as an argument. For
36 example, we want -gno-statement-frontiers to be taken as a negation
37 of -gstatement-frontiers, but without catching the gno- prefix and
38 signaling it's to be used for option remapping, it would end up
39 backtracked to g with no-statemnet-frontiers as the debug level. */
40
41static bool
42remapping_prefix_p (const struct cl_option *opt)
43{
44 return opt->flags & CL_UNDOCUMENTED
45 && opt->flags & CL_JOINED
46 && !(opt->flags & (CL_DRIVER | CL_TARGET | CL_COMMON | CL_LANG_ALL));
47}
48
a1baa5f1 49/* Perform a binary search to find which option the command-line INPUT
615ef0bb 50 matches. Returns its index in the option array, and
51 OPT_SPECIAL_unknown on failure.
a1baa5f1 52
53 This routine is quite subtle. A normal binary search is not good
54 enough because some options can be suffixed with an argument, and
55 multiple sub-matches can occur, e.g. input of "-pedantic" matching
56 the initial substring of "-pedantic-errors".
57
58 A more complicated example is -gstabs. It should match "-g" with
59 an argument of "stabs". Suppose, however, that the number and list
60 of switches are such that the binary search tests "-gen-decls"
61 before having tested "-g". This doesn't match, and as "-gen-decls"
62 is less than "-gstabs", it will become the lower bound of the
63 binary search range, and "-g" will never be seen. To resolve this
30b0f428 64 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
a1baa5f1 65 to "-g" so that failed searches that end between "-gen-decls" and
66 the lexicographically subsequent switch know to go back and see if
67 "-g" causes a match (which it does in this example).
68
69 This search is done in such a way that the longest match for the
70 front end in question wins. If there is no match for the current
71 front end, the longest match for a different front end is returned
72 (or N_OPTS if none) and the caller emits an error message. */
73size_t
607eb2c8 74find_opt (const char *input, unsigned int lang_mask)
a1baa5f1 75{
5789e05b 76 size_t mn, mn_orig, mx, md, opt_len;
a1baa5f1 77 size_t match_wrong_lang;
78 int comp;
79
80 mn = 0;
81 mx = cl_options_count;
82
83 /* Find mn such this lexicographical inequality holds:
84 cl_options[mn] <= input < cl_options[mn + 1]. */
85 while (mx - mn > 1)
86 {
87 md = (mn + mx) / 2;
88 opt_len = cl_options[md].opt_len;
89 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
90
91 if (comp < 0)
92 mx = md;
93 else
94 mn = md;
95 }
96
5789e05b 97 mn_orig = mn;
98
a1baa5f1 99 /* This is the switch that is the best match but for a different
615ef0bb 100 front end, or OPT_SPECIAL_unknown if there is no match at all. */
101 match_wrong_lang = OPT_SPECIAL_unknown;
a1baa5f1 102
103 /* Backtrace the chain of possible matches, returning the longest
104 one, if any, that fits best. With current GCC switches, this
105 loop executes at most twice. */
106 do
107 {
108 const struct cl_option *opt = &cl_options[mn];
109
110 /* Is the input either an exact match or a prefix that takes a
111 joined argument? */
112 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
113 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
114 {
115 /* If language is OK, return it. */
116 if (opt->flags & lang_mask)
117 return mn;
118
a1408eb3 119 if (remapping_prefix_p (opt))
120 return OPT_SPECIAL_unknown;
121
a1baa5f1 122 /* If we haven't remembered a prior match, remember this
123 one. Any prior match is necessarily better. */
615ef0bb 124 if (match_wrong_lang == OPT_SPECIAL_unknown)
a1baa5f1 125 match_wrong_lang = mn;
126 }
127
128 /* Try the next possibility. This is cl_options_count if there
129 are no more. */
130 mn = opt->back_chain;
131 }
132 while (mn != cl_options_count);
133
5789e05b 134 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
135 {
136 /* Long options, starting "--", may be abbreviated if the
137 abbreviation is unambiguous. This only applies to options
138 not taking a joined argument, and abbreviations of "--option"
139 are permitted even if there is a variant "--option=". */
140 size_t mnc = mn_orig + 1;
141 size_t cmp_len = strlen (input);
142 while (mnc < cl_options_count
143 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
144 {
145 /* Option matching this abbreviation. OK if it is the first
146 match and that does not take a joined argument, or the
147 second match, taking a joined argument and with only '='
148 added to the first match; otherwise considered
149 ambiguous. */
150 if (mnc == mn_orig + 1
151 && !(cl_options[mnc].flags & CL_JOINED))
152 match_wrong_lang = mnc;
153 else if (mnc == mn_orig + 2
154 && match_wrong_lang == mn_orig + 1
155 && (cl_options[mnc].flags & CL_JOINED)
156 && (cl_options[mnc].opt_len
157 == cl_options[mn_orig + 1].opt_len + 1)
158 && strncmp (cl_options[mnc].opt_text + 1,
159 cl_options[mn_orig + 1].opt_text + 1,
160 cl_options[mn_orig + 1].opt_len) == 0)
161 ; /* OK, as long as there are no more matches. */
162 else
163 return OPT_SPECIAL_unknown;
164 mnc++;
165 }
166 }
167
615ef0bb 168 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
a1baa5f1 169 return match_wrong_lang;
170}
171
8e18705e 172/* If ARG is a non-negative decimal or hexadecimal integer representable
173 in HOST_WIDE_INT return its value, otherwise return -1. If ERR is not
174 null set *ERR to zero on success or to EINVAL or to the value of errno
175 otherwise. */
06907f25 176
8e18705e 177HOST_WIDE_INT
178integral_argument (const char *arg, int *err, bool byte_size_suffix)
06907f25 179{
8e18705e 180 if (!err)
181 err = &errno;
182
183 if (!ISDIGIT (*arg))
184 {
185 *err = EINVAL;
186 return -1;
187 }
188
189 *err = 0;
190 errno = 0;
06907f25 191
8e18705e 192 char *end = NULL;
193 unsigned HOST_WIDE_INT unit = 1;
194 unsigned HOST_WIDE_INT value = strtoull (arg, &end, 10);
06907f25 195
8e18705e 196 /* If the value is too large to be represented use the maximum
197 representable value that strtoull sets VALUE to (setting
198 errno to ERANGE). */
06907f25 199
8e18705e 200 if (end && *end)
de42969d 201 {
8e18705e 202 if (!byte_size_suffix)
203 {
204 errno = 0;
205 value = strtoull (arg, &end, 0);
206 if (*end)
207 {
208 /* errno is most likely EINVAL here. */
209 *err = errno;
210 return -1;
211 }
212
213 return value;
214 }
215
216 /* Numeric option arguments are at most INT_MAX. Make it
217 possible to specify a larger value by accepting common
218 suffixes. */
219 if (!strcmp (end, "kB"))
220 unit = 1000;
221 else if (!strcasecmp (end, "KiB") || !strcmp (end, "KB"))
222 unit = 1024;
223 else if (!strcmp (end, "MB"))
224 unit = HOST_WIDE_INT_UC (1000) * 1000;
225 else if (!strcasecmp (end, "MiB"))
226 unit = HOST_WIDE_INT_UC (1024) * 1024;
227 else if (!strcasecmp (end, "GB"))
228 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000;
229 else if (!strcasecmp (end, "GiB"))
230 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024;
231 else if (!strcasecmp (end, "TB"))
232 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000;
233 else if (!strcasecmp (end, "TiB"))
234 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024;
235 else if (!strcasecmp (end, "PB"))
236 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000;
237 else if (!strcasecmp (end, "PiB"))
238 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024;
239 else if (!strcasecmp (end, "EB"))
240 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000
241 * 1000;
242 else if (!strcasecmp (end, "EiB"))
243 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024
244 * 1024;
245 else
246 {
247 /* This could mean an unknown suffix or a bad prefix, like
248 "+-1". */
249 *err = EINVAL;
250 return -1;
251 }
252 }
de42969d 253
8e18705e 254 if (unit)
255 {
256 unsigned HOST_WIDE_INT prod = value * unit;
257 value = prod < value ? HOST_WIDE_INT_M1U : prod;
de42969d 258 }
259
8e18705e 260 return value;
06907f25 261}
262
cb22f930 263/* Return whether OPTION is OK for the language given by
264 LANG_MASK. */
265static bool
266option_ok_for_language (const struct cl_option *option,
267 unsigned int lang_mask)
268{
269 if (!(option->flags & lang_mask))
270 return false;
271 else if ((option->flags & CL_TARGET)
272 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
273 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
274 /* Complain for target flag language mismatches if any languages
275 are specified. */
276 return false;
277 return true;
278}
279
d62a5950 280/* Return whether ENUM_ARG is OK for the language given by
281 LANG_MASK. */
282
283static bool
284enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
285 unsigned int lang_mask)
286{
287 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
288}
289
290/* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
291 storing the value in *VALUE if found, and returning false without
292 modifying *VALUE if not found. */
293
294static bool
295enum_arg_to_value (const struct cl_enum_arg *enum_args,
8e18705e 296 const char *arg, HOST_WIDE_INT *value,
297 unsigned int lang_mask)
d62a5950 298{
299 unsigned int i;
300
301 for (i = 0; enum_args[i].arg != NULL; i++)
302 if (strcmp (arg, enum_args[i].arg) == 0
303 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
304 {
305 *value = enum_args[i].value;
306 return true;
307 }
308
309 return false;
310}
311
9105695c 312/* Look up ARG in the enum used by option OPT_INDEX for language
313 LANG_MASK, returning true and storing the value in *VALUE if found,
314 and returning false without modifying *VALUE if not found. */
315
316bool
8e18705e 317opt_enum_arg_to_value (size_t opt_index, const char *arg,
318 int *value, unsigned int lang_mask)
9105695c 319{
320 const struct cl_option *option = &cl_options[opt_index];
321
322 gcc_assert (option->var_type == CLVC_ENUM);
323
8e18705e 324 HOST_WIDE_INT wideval;
325 if (enum_arg_to_value (cl_enums[option->var_enum].values, arg,
326 &wideval, lang_mask))
327 {
328 *value = wideval;
329 return true;
330 }
331
332 return false;
9105695c 333}
334
d62a5950 335/* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
336 corresponding string in *ARGP, returning true if the found string
337 was marked as canonical, false otherwise. If VALUE is not found
338 (which may be the case for uninitialized values if the relevant
339 option has not been passed), set *ARGP to NULL and return
340 false. */
341
342bool
343enum_value_to_arg (const struct cl_enum_arg *enum_args,
344 const char **argp, int value, unsigned int lang_mask)
345{
346 unsigned int i;
347
348 for (i = 0; enum_args[i].arg != NULL; i++)
349 if (enum_args[i].value == value
350 && (enum_args[i].flags & CL_ENUM_CANONICAL)
351 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
352 {
353 *argp = enum_args[i].arg;
354 return true;
355 }
356
357 for (i = 0; enum_args[i].arg != NULL; i++)
358 if (enum_args[i].value == value
359 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
360 {
361 *argp = enum_args[i].arg;
362 return false;
363 }
364
365 *argp = NULL;
366 return false;
367}
67089c6b 368
369/* Fill in the canonical option part of *DECODED with an option
370 described by OPT_INDEX, ARG and VALUE. */
371
372static void
8e18705e 373generate_canonical_option (size_t opt_index, const char *arg,
374 HOST_WIDE_INT value,
67089c6b 375 struct cl_decoded_option *decoded)
376{
377 const struct cl_option *option = &cl_options[opt_index];
378 const char *opt_text = option->opt_text;
379
380 if (value == 0
ec840af4 381 && !option->cl_reject_negative
a1408eb3 382 && (opt_text[1] == 'W' || opt_text[1] == 'f'
383 || opt_text[1] == 'g' || opt_text[1] == 'm'))
67089c6b 384 {
ba30d337 385 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
67089c6b 386 t[0] = '-';
387 t[1] = opt_text[1];
388 t[2] = 'n';
389 t[3] = 'o';
390 t[4] = '-';
391 memcpy (t + 5, opt_text + 2, option->opt_len);
392 opt_text = t;
393 }
394
395 decoded->canonical_option[2] = NULL;
396 decoded->canonical_option[3] = NULL;
397
398 if (arg)
399 {
0b1d266f 400 if ((option->flags & CL_SEPARATE)
ec840af4 401 && !option->cl_separate_alias)
67089c6b 402 {
403 decoded->canonical_option[0] = opt_text;
404 decoded->canonical_option[1] = arg;
405 decoded->canonical_option_num_elements = 2;
406 }
407 else
408 {
409 gcc_assert (option->flags & CL_JOINED);
ba30d337 410 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
67089c6b 411 decoded->canonical_option[1] = NULL;
412 decoded->canonical_option_num_elements = 1;
413 }
414 }
415 else
416 {
417 decoded->canonical_option[0] = opt_text;
418 decoded->canonical_option[1] = NULL;
419 decoded->canonical_option_num_elements = 1;
420 }
421}
422
5789e05b 423/* Structure describing mappings from options on the command line to
424 options to look up with find_opt. */
425struct option_map
426{
427 /* Prefix of the option on the command line. */
428 const char *opt0;
429 /* If two argv elements are considered to be merged into one option,
430 prefix for the second element, otherwise NULL. */
431 const char *opt1;
432 /* The new prefix to map to. */
433 const char *new_prefix;
434 /* Whether at least one character is needed following opt1 or opt0
435 for this mapping to be used. (--optimize= is valid for -O, but
436 --warn- is not valid for -W.) */
437 bool another_char_needed;
438 /* Whether the original option is a negated form of the option
439 resulting from this map. */
440 bool negated;
441};
442static const struct option_map option_map[] =
443 {
444 { "-Wno-", NULL, "-W", false, true },
445 { "-fno-", NULL, "-f", false, true },
a1408eb3 446 { "-gno-", NULL, "-g", false, true },
5789e05b 447 { "-mno-", NULL, "-m", false, true },
448 { "--debug=", NULL, "-g", false, false },
449 { "--machine-", NULL, "-m", true, false },
450 { "--machine-no-", NULL, "-m", false, true },
451 { "--machine=", NULL, "-m", false, false },
452 { "--machine=no-", NULL, "-m", false, true },
453 { "--machine", "", "-m", false, false },
454 { "--machine", "no-", "-m", false, true },
455 { "--optimize=", NULL, "-O", false, false },
456 { "--std=", NULL, "-std=", false, false },
457 { "--std", "", "-std=", false, false },
458 { "--warn-", NULL, "-W", true, false },
459 { "--warn-no-", NULL, "-W", false, true },
460 { "--", NULL, "-f", true, false },
461 { "--no-", NULL, "-f", false, true }
462 };
463
da7d5066 464/* Helper function for gcc.c's driver::suggest_option, for populating the
465 vec of suggestions for misspelled options.
466
467 option_map above provides various prefixes for spelling command-line
468 options, which decode_cmdline_option uses to map spellings of options
469 to specific options. We want to do the reverse: to find all the ways
470 that a user could validly spell an option.
471
413c4f6c 472 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
473 of its valid variant spellings to CANDIDATES, each without a leading
474 dash.
da7d5066 475
476 For example, given "-Wabi-tag", the following are added to CANDIDATES:
477 "Wabi-tag"
478 "Wno-abi-tag"
479 "-warn-abi-tag"
480 "-warn-no-abi-tag".
481
482 The added strings must be freed using free. */
483
484void
485add_misspelling_candidates (auto_vec<char *> *candidates,
413c4f6c 486 const struct cl_option *option,
da7d5066 487 const char *opt_text)
488{
489 gcc_assert (candidates);
413c4f6c 490 gcc_assert (option);
da7d5066 491 gcc_assert (opt_text);
a1408eb3 492 if (remapping_prefix_p (option))
493 return;
da7d5066 494 candidates->safe_push (xstrdup (opt_text + 1));
495 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
496 {
497 const char *opt0 = option_map[i].opt0;
498 const char *new_prefix = option_map[i].new_prefix;
499 size_t new_prefix_len = strlen (new_prefix);
500
413c4f6c 501 if (option->cl_reject_negative && option_map[i].negated)
502 continue;
503
da7d5066 504 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
505 {
506 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
507 NULL);
508 candidates->safe_push (alternative);
509 }
510 }
511}
512
06907f25 513/* Decode the switch beginning at ARGV for the language indicated by
e28aa114 514 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
515 the structure *DECODED. Returns the number of switches
516 consumed. */
06907f25 517
615ef0bb 518static unsigned int
06907f25 519decode_cmdline_option (const char **argv, unsigned int lang_mask,
520 struct cl_decoded_option *decoded)
521{
522 size_t opt_index;
5789e05b 523 const char *arg = 0;
8e18705e 524 HOST_WIDE_INT value = 1;
b904552d 525 unsigned int result = 1, i, extra_args, separate_args = 0;
5789e05b 526 int adjust_len = 0;
71a2f35e 527 size_t total_len;
528 char *p;
06907f25 529 const struct cl_option *option;
530 int errors = 0;
3b0273a1 531 const char *warn_message = NULL;
89c69892 532 bool separate_arg_flag;
533 bool joined_arg_flag;
0b1d266f 534 bool have_separate_arg = false;
06907f25 535
5789e05b 536 extra_args = 0;
06907f25 537
5789e05b 538 opt_index = find_opt (argv[0] + 1, lang_mask);
539 i = 0;
540 while (opt_index == OPT_SPECIAL_unknown
541 && i < ARRAY_SIZE (option_map))
06907f25 542 {
5789e05b 543 const char *opt0 = option_map[i].opt0;
544 const char *opt1 = option_map[i].opt1;
545 const char *new_prefix = option_map[i].new_prefix;
546 bool another_char_needed = option_map[i].another_char_needed;
547 size_t opt0_len = strlen (opt0);
548 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
549 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
550 size_t new_prefix_len = strlen (new_prefix);
551
552 extra_args = (opt1 == NULL ? 0 : 1);
553 value = !option_map[i].negated;
554
555 if (strncmp (argv[0], opt0, opt0_len) == 0
556 && (opt1 == NULL
557 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
558 && (!another_char_needed
559 || argv[extra_args][optn_len] != 0))
560 {
561 size_t arglen = strlen (argv[extra_args]);
562 char *dup;
563
564 adjust_len = (int) optn_len - (int) new_prefix_len;
565 dup = XNEWVEC (char, arglen + 1 - adjust_len);
566 memcpy (dup, new_prefix, new_prefix_len);
567 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
568 arglen - optn_len + 1);
569 opt_index = find_opt (dup + 1, lang_mask);
570 free (dup);
571 }
572 i++;
06907f25 573 }
574
615ef0bb 575 if (opt_index == OPT_SPECIAL_unknown)
576 {
577 arg = argv[0];
5789e05b 578 extra_args = 0;
579 value = 1;
615ef0bb 580 goto done;
581 }
06907f25 582
583 option = &cl_options[opt_index];
584
585 /* Reject negative form of switches that don't take negatives as
586 unrecognized. */
ec840af4 587 if (!value && option->cl_reject_negative)
06907f25 588 {
615ef0bb 589 opt_index = OPT_SPECIAL_unknown;
19c8675b 590 errors |= CL_ERR_NEGATIVE;
615ef0bb 591 arg = argv[0];
06907f25 592 goto done;
593 }
594
8e18705e 595 /* Clear the initial value for size options (it will be overwritten
596 later based on the Init(value) specification in the opt file. */
597 if (option->var_type == CLVC_SIZE)
598 value = 0;
599
5789e05b 600 result = extra_args + 1;
3b0273a1 601 warn_message = option->warn_message;
602
06907f25 603 /* Check to see if the option is disabled for this configuration. */
ec840af4 604 if (option->cl_disabled)
06907f25 605 errors |= CL_ERR_DISABLED;
606
89c69892 607 /* Determine whether there may be a separate argument based on
e2028bfa 608 whether this option is being processed for the driver, and, if
609 so, how many such arguments. */
89c69892 610 separate_arg_flag = ((option->flags & CL_SEPARATE)
ec840af4 611 && !(option->cl_no_driver_arg
89c69892 612 && (lang_mask & CL_DRIVER)));
e2028bfa 613 separate_args = (separate_arg_flag
ec840af4 614 ? option->cl_separate_nargs + 1
e2028bfa 615 : 0);
89c69892 616 joined_arg_flag = (option->flags & CL_JOINED) != 0;
617
06907f25 618 /* Sort out any argument the switch takes. */
89c69892 619 if (joined_arg_flag)
06907f25 620 {
621 /* Have arg point to the original switch. This is because
622 some code, such as disable_builtin_function, expects its
623 argument to be persistent until the program exits. */
5789e05b 624 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
06907f25 625
ec840af4 626 if (*arg == '\0' && !option->cl_missing_ok)
06907f25 627 {
89c69892 628 if (separate_arg_flag)
06907f25 629 {
5789e05b 630 arg = argv[extra_args + 1];
631 result = extra_args + 2;
06907f25 632 if (arg == NULL)
5789e05b 633 result = extra_args + 1;
0b1d266f 634 else
635 have_separate_arg = true;
06907f25 636 }
637 else
638 /* Missing argument. */
639 arg = NULL;
640 }
641 }
89c69892 642 else if (separate_arg_flag)
06907f25 643 {
5789e05b 644 arg = argv[extra_args + 1];
e2028bfa 645 for (i = 0; i < separate_args; i++)
646 if (argv[extra_args + 1 + i] == NULL)
647 {
648 errors |= CL_ERR_MISSING_ARG;
649 break;
650 }
651 result = extra_args + 1 + i;
652 if (arg != NULL)
0b1d266f 653 have_separate_arg = true;
06907f25 654 }
655
67089c6b 656 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
657 errors |= CL_ERR_MISSING_ARG;
658
3b0273a1 659 /* Is this option an alias (or an ignored option, marked as an alias
660 of OPT_SPECIAL_ignore)? */
0b1d266f 661 if (option->alias_target != N_OPTS
ec840af4 662 && (!option->cl_separate_alias || have_separate_arg))
67089c6b 663 {
664 size_t new_opt_index = option->alias_target;
67089c6b 665
0c46a085 666 if (new_opt_index == OPT_SPECIAL_ignore
667 || new_opt_index == OPT_SPECIAL_deprecated)
67089c6b 668 {
3b0273a1 669 gcc_assert (option->alias_arg == NULL);
670 gcc_assert (option->neg_alias_arg == NULL);
671 opt_index = new_opt_index;
672 arg = NULL;
67089c6b 673 }
3b0273a1 674 else
67089c6b 675 {
3b0273a1 676 const struct cl_option *new_option = &cl_options[new_opt_index];
67089c6b 677
3b0273a1 678 /* The new option must not be an alias itself. */
5789e05b 679 gcc_assert (new_option->alias_target == N_OPTS
ec840af4 680 || new_option->cl_separate_alias);
67089c6b 681
3b0273a1 682 if (option->neg_alias_arg)
683 {
684 gcc_assert (option->alias_arg != NULL);
685 gcc_assert (arg == NULL);
ec840af4 686 gcc_assert (!option->cl_negative_alias);
3b0273a1 687 if (value)
688 arg = option->alias_arg;
689 else
690 arg = option->neg_alias_arg;
691 value = 1;
692 }
693 else if (option->alias_arg)
694 {
695 gcc_assert (value == 1);
696 gcc_assert (arg == NULL);
ec840af4 697 gcc_assert (!option->cl_negative_alias);
3b0273a1 698 arg = option->alias_arg;
699 }
67089c6b 700
ec840af4 701 if (option->cl_negative_alias)
4e775b8e 702 value = !value;
703
3b0273a1 704 opt_index = new_opt_index;
705 option = new_option;
67089c6b 706
3b0273a1 707 if (value == 0)
ec840af4 708 gcc_assert (!option->cl_reject_negative);
3b0273a1 709
710 /* Recompute what arguments are allowed. */
711 separate_arg_flag = ((option->flags & CL_SEPARATE)
ec840af4 712 && !(option->cl_no_driver_arg
3b0273a1 713 && (lang_mask & CL_DRIVER)));
714 joined_arg_flag = (option->flags & CL_JOINED) != 0;
715
ec840af4 716 if (separate_args > 1 || option->cl_separate_nargs)
e2028bfa 717 gcc_assert (separate_args
ec840af4 718 == (unsigned int) option->cl_separate_nargs + 1);
e2028bfa 719
3b0273a1 720 if (!(errors & CL_ERR_MISSING_ARG))
721 {
722 if (separate_arg_flag || joined_arg_flag)
5789e05b 723 {
ec840af4 724 if (option->cl_missing_ok && arg == NULL)
5789e05b 725 arg = "";
726 gcc_assert (arg != NULL);
727 }
3b0273a1 728 else
729 gcc_assert (arg == NULL);
730 }
67089c6b 731
3b0273a1 732 /* Recheck for warnings and disabled options. */
733 if (option->warn_message)
734 {
735 gcc_assert (warn_message == NULL);
736 warn_message = option->warn_message;
737 }
ec840af4 738 if (option->cl_disabled)
3b0273a1 739 errors |= CL_ERR_DISABLED;
740 }
67089c6b 741 }
742
06907f25 743 /* Check if this is a switch for a different front end. */
cb22f930 744 if (!option_ok_for_language (option, lang_mask))
06907f25 745 errors |= CL_ERR_WRONG_LANG;
06907f25 746
cb14e058 747 /* Convert the argument to lowercase if appropriate. */
748 if (arg && option->cl_tolower)
749 {
750 size_t j;
751 size_t len = strlen (arg);
ba30d337 752 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
cb14e058 753
754 for (j = 0; j < len; j++)
755 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
756 arg_lower[len] = 0;
757 arg = arg_lower;
758 }
759
8e18705e 760 /* If the switch takes an integer argument, convert it. */
761 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
06907f25 762 {
8e18705e 763 int error = 0;
764 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
765 if (error)
06907f25 766 errors |= CL_ERR_UINT_ARG;
52368c34 767
768 /* Reject value out of a range. */
769 if (option->range_max != -1
770 && (value < option->range_min || value > option->range_max))
771 errors |= CL_ERR_INT_RANGE_ARG;
06907f25 772 }
773
d62a5950 774 /* If the switch takes an enumerated argument, convert it. */
775 if (arg && (option->var_type == CLVC_ENUM))
776 {
777 const struct cl_enum *e = &cl_enums[option->var_enum];
778
779 gcc_assert (value == 1);
780 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
781 {
782 const char *carg = NULL;
783
784 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
785 arg = carg;
786 gcc_assert (carg != NULL);
787 }
788 else
789 errors |= CL_ERR_ENUM_ARG;
790 }
791
06907f25 792 done:
06907f25 793 decoded->opt_index = opt_index;
794 decoded->arg = arg;
795 decoded->value = value;
796 decoded->errors = errors;
3b0273a1 797 decoded->warn_message = warn_message;
71a2f35e 798
799 if (opt_index == OPT_SPECIAL_unknown)
e2028bfa 800 gcc_assert (result == 1);
71a2f35e 801
802 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
803 decoded->canonical_option_num_elements = result;
804 total_len = 0;
805 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
806 {
807 if (i < result)
808 {
fde5bd5c 809 size_t len;
67089c6b 810 if (opt_index == OPT_SPECIAL_unknown)
811 decoded->canonical_option[i] = argv[i];
812 else
813 decoded->canonical_option[i] = NULL;
fde5bd5c 814 len = strlen (argv[i]);
815 /* If the argument is an empty string, we will print it as "" in
816 orig_option_with_args_text. */
817 total_len += (len != 0 ? len : 2) + 1;
71a2f35e 818 }
819 else
820 decoded->canonical_option[i] = NULL;
821 }
0c46a085 822 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore
823 && opt_index != OPT_SPECIAL_deprecated)
e2028bfa 824 {
825 generate_canonical_option (opt_index, arg, value, decoded);
826 if (separate_args > 1)
827 {
828 for (i = 0; i < separate_args; i++)
829 {
830 if (argv[extra_args + 1 + i] == NULL)
831 break;
832 else
833 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
834 }
835 gcc_assert (result == 1 + i);
836 decoded->canonical_option_num_elements = result;
837 }
838 }
ba30d337 839 decoded->orig_option_with_args_text
840 = p = XOBNEWVEC (&opts_obstack, char, total_len);
71a2f35e 841 for (i = 0; i < result; i++)
842 {
843 size_t len = strlen (argv[i]);
844
fde5bd5c 845 /* Print the empty string verbally. */
846 if (len == 0)
847 {
848 *p++ = '"';
849 *p++ = '"';
850 }
851 else
852 memcpy (p, argv[i], len);
71a2f35e 853 p += len;
854 if (i == result - 1)
855 *p++ = 0;
856 else
857 *p++ = ' ';
858 }
859
06907f25 860 return result;
861}
862
70639a9d 863/* Obstack for option strings. */
864
865struct obstack opts_obstack;
866
867/* Like libiberty concat, but allocate using opts_obstack. */
868
869char *
870opts_concat (const char *first, ...)
871{
872 char *newstr, *end;
873 size_t length = 0;
874 const char *arg;
875 va_list ap;
876
877 /* First compute the size of the result and get sufficient memory. */
878 va_start (ap, first);
879 for (arg = first; arg; arg = va_arg (ap, const char *))
880 length += strlen (arg);
881 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
882 va_end (ap);
883
884 /* Now copy the individual pieces to the result string. */
885 va_start (ap, first);
886 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
887 {
888 length = strlen (arg);
889 memcpy (end, arg, length);
890 end += length;
891 }
892 *end = '\0';
893 va_end (ap);
894 return newstr;
895}
896
615ef0bb 897/* Decode command-line options (ARGC and ARGV being the arguments of
898 main) into an array, setting *DECODED_OPTIONS to a pointer to that
899 array and *DECODED_OPTIONS_COUNT to the number of entries in the
900 array. The first entry in the array is always one for the program
901 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
e28aa114 902 flags applicable for decoding (including CL_COMMON and CL_TARGET if
903 those options should be considered applicable). Do not produce any
904 diagnostics or set state outside of these variables. */
615ef0bb 905
906void
907decode_cmdline_options_to_array (unsigned int argc, const char **argv,
908 unsigned int lang_mask,
909 struct cl_decoded_option **decoded_options,
910 unsigned int *decoded_options_count)
911{
2c2747c3 912 unsigned int n, i;
615ef0bb 913 struct cl_decoded_option *opt_array;
914 unsigned int num_decoded_options;
915
916 opt_array = XNEWVEC (struct cl_decoded_option, argc);
917
918 opt_array[0].opt_index = OPT_SPECIAL_program_name;
3b0273a1 919 opt_array[0].warn_message = NULL;
615ef0bb 920 opt_array[0].arg = argv[0];
921 opt_array[0].orig_option_with_args_text = argv[0];
71a2f35e 922 opt_array[0].canonical_option_num_elements = 1;
e88d34f6 923 opt_array[0].canonical_option[0] = argv[0];
924 opt_array[0].canonical_option[1] = NULL;
71a2f35e 925 opt_array[0].canonical_option[2] = NULL;
926 opt_array[0].canonical_option[3] = NULL;
615ef0bb 927 opt_array[0].value = 1;
928 opt_array[0].errors = 0;
929 num_decoded_options = 1;
930
931 for (i = 1; i < argc; i += n)
932 {
933 const char *opt = argv[i];
934
935 /* Interpret "-" or a non-switch as a file name. */
936 if (opt[0] != '-' || opt[1] == '\0')
937 {
cb22f930 938 generate_option_input_file (opt, &opt_array[num_decoded_options]);
615ef0bb 939 num_decoded_options++;
940 n = 1;
941 continue;
942 }
943
944 n = decode_cmdline_option (argv + i, lang_mask,
945 &opt_array[num_decoded_options]);
946 num_decoded_options++;
947 }
948
615ef0bb 949 *decoded_options = opt_array;
950 *decoded_options_count = num_decoded_options;
e62df35b 951 prune_options (decoded_options, decoded_options_count);
615ef0bb 952}
953
a1baa5f1 954/* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
955 next one is the same as ORIG_NEXT_OPT_IDX. */
956
957static bool
958cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
959{
960 /* An option can be canceled by the same option or an option with
961 Negative. */
962 if (cl_options [next_opt_idx].neg_index == opt_idx)
963 return true;
964
965 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
966 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
967 orig_next_opt_idx);
48e1416a 968
a1baa5f1 969 return false;
970}
971
972/* Filter out options canceled by the ones after them. */
973
e62df35b 974static void
975prune_options (struct cl_decoded_option **decoded_options,
976 unsigned int *decoded_options_count)
a1baa5f1 977{
e62df35b 978 unsigned int old_decoded_options_count = *decoded_options_count;
979 struct cl_decoded_option *old_decoded_options = *decoded_options;
980 unsigned int new_decoded_options_count;
981 struct cl_decoded_option *new_decoded_options
982 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
983 unsigned int i;
a1baa5f1 984 const struct cl_option *option;
1b3e7760 985 unsigned int fdiagnostics_color_idx = 0;
a1baa5f1 986
987 /* Remove arguments which are negated by others after them. */
e62df35b 988 new_decoded_options_count = 0;
989 for (i = 0; i < old_decoded_options_count; i++)
a1baa5f1 990 {
e62df35b 991 unsigned int j, opt_idx, next_opt_idx;
992
993 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
994 goto keep;
a1baa5f1 995
e62df35b 996 opt_idx = old_decoded_options[i].opt_index;
997 switch (opt_idx)
a1baa5f1 998 {
e62df35b 999 case OPT_SPECIAL_unknown:
1000 case OPT_SPECIAL_ignore:
0c46a085 1001 case OPT_SPECIAL_deprecated:
e62df35b 1002 case OPT_SPECIAL_program_name:
1003 case OPT_SPECIAL_input_file:
1004 goto keep;
1005
1b3e7760 1006 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1007 case OPT_fdiagnostics_color_:
1008 fdiagnostics_color_idx = i;
1009 continue;
1010
e62df35b 1011 default:
1012 gcc_assert (opt_idx < cl_options_count);
1013 option = &cl_options[opt_idx];
1014 if (option->neg_index < 0)
1015 goto keep;
1016
1017 /* Skip joined switches. */
8c0f695d 1018 if ((option->flags & CL_JOINED)
1019 && (!option->cl_reject_negative
1020 || (unsigned int) option->neg_index != opt_idx))
e62df35b 1021 goto keep;
1022
1023 for (j = i + 1; j < old_decoded_options_count; j++)
a1baa5f1 1024 {
e62df35b 1025 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1026 continue;
1027 next_opt_idx = old_decoded_options[j].opt_index;
1028 if (next_opt_idx >= cl_options_count)
1029 continue;
1030 if (cl_options[next_opt_idx].neg_index < 0)
1031 continue;
8c0f695d 1032 if ((cl_options[next_opt_idx].flags & CL_JOINED)
1033 && (!cl_options[next_opt_idx].cl_reject_negative
1034 || ((unsigned int) cl_options[next_opt_idx].neg_index
1035 != next_opt_idx)))
1036 continue;
e62df35b 1037 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
a1baa5f1 1038 break;
1039 }
e62df35b 1040 if (j == old_decoded_options_count)
1041 {
a1baa5f1 1042keep:
e62df35b 1043 new_decoded_options[new_decoded_options_count]
1044 = old_decoded_options[i];
1045 new_decoded_options_count++;
1046 }
1047 break;
a1baa5f1 1048 }
1049 }
1050
160b0cdb 1051 if (fdiagnostics_color_idx >= 1)
1b3e7760 1052 {
1053 /* We put the last -fdiagnostics-color= at the first position
1054 after argv[0] so it can take effect immediately. */
1055 memmove (new_decoded_options + 2, new_decoded_options + 1,
1056 sizeof (struct cl_decoded_option)
1057 * (new_decoded_options_count - 1));
1058 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
1059 new_decoded_options_count++;
1060 }
1061
e62df35b 1062 free (old_decoded_options);
1063 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1064 new_decoded_options,
1065 new_decoded_options_count);
1066 *decoded_options = new_decoded_options;
1067 *decoded_options_count = new_decoded_options_count;
a1baa5f1 1068}
b78351e5 1069
666f4bf0 1070/* Handle option DECODED for the language indicated by LANG_MASK,
f83b64ca 1071 using the handlers in HANDLERS and setting fields in OPTS and
1072 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
3c6c0e40 1073 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1074 option for options from the source file, UNKNOWN_LOCATION
1075 otherwise. GENERATED_P is true for an option generated as part of
1076 processing another option or otherwise generated internally, false
09fd09eb 1077 for one explicitly passed by the user. control_warning_option
1078 generated options are considered explicitly passed by the user.
1079 Returns false if the switch was invalid. DC is the diagnostic
1080 context for options affecting diagnostics state, or NULL. */
b78351e5 1081
3c6c0e40 1082static bool
2c5d2e39 1083handle_option (struct gcc_options *opts,
f83b64ca 1084 struct gcc_options *opts_set,
2c5d2e39 1085 const struct cl_decoded_option *decoded,
3c6c0e40 1086 unsigned int lang_mask, int kind, location_t loc,
f83b64ca 1087 const struct cl_option_handlers *handlers,
24ca3b4e 1088 bool generated_p, diagnostic_context *dc)
b78351e5 1089{
666f4bf0 1090 size_t opt_index = decoded->opt_index;
1091 const char *arg = decoded->arg;
8e18705e 1092 HOST_WIDE_INT value = decoded->value;
b78351e5 1093 const struct cl_option *option = &cl_options[opt_index];
2c5d2e39 1094 void *flag_var = option_flag_var (opt_index, opts);
b78351e5 1095 size_t i;
1096
2c5d2e39 1097 if (flag_var)
f83b64ca 1098 set_option (opts, (generated_p ? NULL : opts_set),
3c6c0e40 1099 opt_index, value, arg, kind, loc, dc);
b78351e5 1100
1101 for (i = 0; i < handlers->num_handlers; i++)
1102 if (option->flags & handlers->handlers[i].mask)
1103 {
f83b64ca 1104 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
3c6c0e40 1105 lang_mask, kind, loc,
98102386 1106 handlers, dc,
1107 handlers->target_option_override_hook))
b78351e5 1108 return false;
b78351e5 1109 }
1110
1111 return true;
1112}
1113
666f4bf0 1114/* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1115 option instead of DECODED. This is used for callbacks when one
1116 option implies another instead of an option being decoded from the
1117 command line. */
1118
1119bool
f83b64ca 1120handle_generated_option (struct gcc_options *opts,
1121 struct gcc_options *opts_set,
8e18705e 1122 size_t opt_index, const char *arg, HOST_WIDE_INT value,
3c6c0e40 1123 unsigned int lang_mask, int kind, location_t loc,
24ca3b4e 1124 const struct cl_option_handlers *handlers,
09fd09eb 1125 bool generated_p, diagnostic_context *dc)
666f4bf0 1126{
666f4bf0 1127 struct cl_decoded_option decoded;
1128
cb22f930 1129 generate_option (opt_index, arg, value, lang_mask, &decoded);
3c6c0e40 1130 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
09fd09eb 1131 handlers, generated_p, dc);
cb22f930 1132}
1133
1134/* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1135 VALUE for a front end using LANG_MASK. This is used when the
1136 compiler generates options internally. */
1137
1138void
8e18705e 1139generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
cb22f930 1140 unsigned int lang_mask, struct cl_decoded_option *decoded)
1141{
1142 const struct cl_option *option = &cl_options[opt_index];
1143
1144 decoded->opt_index = opt_index;
3b0273a1 1145 decoded->warn_message = NULL;
cb22f930 1146 decoded->arg = arg;
cb22f930 1147 decoded->value = value;
1148 decoded->errors = (option_ok_for_language (option, lang_mask)
1149 ? 0
1150 : CL_ERR_WRONG_LANG);
666f4bf0 1151
67089c6b 1152 generate_canonical_option (opt_index, arg, value, decoded);
1153 switch (decoded->canonical_option_num_elements)
666f4bf0 1154 {
67089c6b 1155 case 1:
1156 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1157 break;
1158
1159 case 2:
1160 decoded->orig_option_with_args_text
ba30d337 1161 = opts_concat (decoded->canonical_option[0], " ",
1162 decoded->canonical_option[1], NULL);
67089c6b 1163 break;
1164
1165 default:
1166 gcc_unreachable ();
666f4bf0 1167 }
cb22f930 1168}
666f4bf0 1169
cb22f930 1170/* Fill in *DECODED with an option for input file FILE. */
1171
1172void
1173generate_option_input_file (const char *file,
1174 struct cl_decoded_option *decoded)
1175{
1176 decoded->opt_index = OPT_SPECIAL_input_file;
3b0273a1 1177 decoded->warn_message = NULL;
cb22f930 1178 decoded->arg = file;
1179 decoded->orig_option_with_args_text = file;
1180 decoded->canonical_option_num_elements = 1;
1181 decoded->canonical_option[0] = file;
1182 decoded->canonical_option[1] = NULL;
1183 decoded->canonical_option[2] = NULL;
1184 decoded->canonical_option[3] = NULL;
1185 decoded->value = 1;
1186 decoded->errors = 0;
666f4bf0 1187}
1188
afbacb06 1189/* Helper function for listing valid choices and hint for misspelled
1190 value. CANDIDATES is a vector containing all valid strings,
1191 STR is set to a heap allocated string that contains all those
1192 strings concatenated, separated by spaces, and the return value
1193 is the closest string from those to ARG, or NULL if nothing is
1194 close enough. Callers should XDELETEVEC (STR) after using it
1195 to avoid memory leaks. */
1196
1197const char *
1198candidates_list_and_hint (const char *arg, char *&str,
1199 const auto_vec <const char *> &candidates)
1200{
1201 size_t len = 0;
1202 int i;
1203 const char *candidate;
1204 char *p;
1205
1206 FOR_EACH_VEC_ELT (candidates, i, candidate)
1207 len += strlen (candidate) + 1;
1208
1209 str = p = XNEWVEC (char, len);
1210 FOR_EACH_VEC_ELT (candidates, i, candidate)
1211 {
1212 len = strlen (candidate);
1213 memcpy (p, candidate, len);
1214 p[len] = ' ';
1215 p += len + 1;
1216 }
1217 p[-1] = '\0';
1218 return find_closest_string (arg, &candidates);
1219}
1220
31ba81bd 1221/* Perform diagnostics for read_cmdline_option and control_warning_option
1222 functions. Returns true if an error has been diagnosed.
1223 LOC and LANG_MASK arguments like in read_cmdline_option.
1224 OPTION is the option to report diagnostics for, OPT the name
1225 of the option as text, ARG the argument of the option (for joined
1226 options), ERRORS is bitmask of CL_ERR_* values. */
b78351e5 1227
31ba81bd 1228static bool
1229cmdline_handle_error (location_t loc, const struct cl_option *option,
1230 const char *opt, const char *arg, int errors,
1231 unsigned int lang_mask)
b78351e5 1232{
31ba81bd 1233 if (errors & CL_ERR_DISABLED)
b78351e5 1234 {
3c6c0e40 1235 error_at (loc, "command line option %qs"
31ba81bd 1236 " is not supported by this configuration", opt);
1237 return true;
b78351e5 1238 }
1239
31ba81bd 1240 if (errors & CL_ERR_MISSING_ARG)
b78351e5 1241 {
1242 if (option->missing_argument_error)
3c6c0e40 1243 error_at (loc, option->missing_argument_error, opt);
b78351e5 1244 else
3c6c0e40 1245 error_at (loc, "missing argument to %qs", opt);
31ba81bd 1246 return true;
b78351e5 1247 }
1248
31ba81bd 1249 if (errors & CL_ERR_UINT_ARG)
b78351e5 1250 {
8e18705e 1251 if (option->cl_byte_size)
1252 error_at (loc, "argument to %qs should be a non-negative integer "
1253 "optionally followed by a size unit",
1254 option->opt_text);
1255 else
1256 error_at (loc, "argument to %qs should be a non-negative integer",
1257 option->opt_text);
31ba81bd 1258 return true;
b78351e5 1259 }
1260
52368c34 1261 if (errors & CL_ERR_INT_RANGE_ARG)
1262 {
1263 error_at (loc, "argument to %qs is not between %d and %d",
1264 option->opt_text, option->range_min, option->range_max);
1265 return true;
1266 }
1267
31ba81bd 1268 if (errors & CL_ERR_ENUM_ARG)
d62a5950 1269 {
1270 const struct cl_enum *e = &cl_enums[option->var_enum];
1271 unsigned int i;
afbacb06 1272 char *s;
d62a5950 1273
bc35ef65 1274 auto_diagnostic_group d;
d62a5950 1275 if (e->unknown_error)
31ba81bd 1276 error_at (loc, e->unknown_error, arg);
d62a5950 1277 else
1278 error_at (loc, "unrecognized argument in option %qs", opt);
1279
56f34f03 1280 auto_vec <const char *> candidates;
d62a5950 1281 for (i = 0; e->values[i].arg != NULL; i++)
1282 {
60a45807 1283 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1284 continue;
56f34f03 1285 candidates.safe_push (e->values[i].arg);
d62a5950 1286 }
afbacb06 1287 const char *hint = candidates_list_and_hint (arg, s, candidates);
56f34f03 1288 if (hint)
1289 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1290 option->opt_text, s, hint);
1291 else
1292 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
afbacb06 1293 XDELETEVEC (s);
56f34f03 1294
31ba81bd 1295 return true;
1296 }
1297
1298 return false;
1299}
1300
1301/* Handle the switch DECODED (location LOC) for the language indicated
1302 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1303 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1304 diagnostic options. */
1305
1306void
1307read_cmdline_option (struct gcc_options *opts,
1308 struct gcc_options *opts_set,
1309 struct cl_decoded_option *decoded,
1310 location_t loc,
1311 unsigned int lang_mask,
1312 const struct cl_option_handlers *handlers,
1313 diagnostic_context *dc)
1314{
1315 const struct cl_option *option;
1316 const char *opt = decoded->orig_option_with_args_text;
1317
1318 if (decoded->warn_message)
1319 warning_at (loc, 0, decoded->warn_message, opt);
1320
1321 if (decoded->opt_index == OPT_SPECIAL_unknown)
1322 {
1323 if (handlers->unknown_option_callback (decoded))
1324 error_at (loc, "unrecognized command line option %qs", decoded->arg);
d62a5950 1325 return;
1326 }
1327
31ba81bd 1328 if (decoded->opt_index == OPT_SPECIAL_ignore)
1329 return;
1330
0c46a085 1331 if (decoded->opt_index == OPT_SPECIAL_deprecated)
1332 {
1333 /* Warn only about positive ignored options. */
1334 if (decoded->value)
1335 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1336 return;
1337 }
1338
31ba81bd 1339 option = &cl_options[decoded->opt_index];
1340
1341 if (decoded->errors
1342 && cmdline_handle_error (loc, option, opt, decoded->arg,
1343 decoded->errors, lang_mask))
1344 return;
1345
5585cfe8 1346 if (decoded->errors & CL_ERR_WRONG_LANG)
1347 {
1348 handlers->wrong_lang_callback (decoded, lang_mask);
1349 return;
1350 }
1351
b78351e5 1352 gcc_assert (!decoded->errors);
1353
f83b64ca 1354 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
3c6c0e40 1355 loc, handlers, false, dc))
1356 error_at (loc, "unrecognized command line option %qs", opt);
b78351e5 1357}
1358
f83b64ca 1359/* Set any field in OPTS, and OPTS_SET if not NULL, for option
3c6c0e40 1360 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1361 location LOC, using diagnostic context DC if not NULL for
1362 diagnostic classification. */
b78351e5 1363
1364void
f83b64ca 1365set_option (struct gcc_options *opts, struct gcc_options *opts_set,
8e18705e 1366 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
3c6c0e40 1367 location_t loc, diagnostic_context *dc)
b78351e5 1368{
1369 const struct cl_option *option = &cl_options[opt_index];
2c5d2e39 1370 void *flag_var = option_flag_var (opt_index, opts);
f83b64ca 1371 void *set_flag_var = NULL;
b78351e5 1372
2c5d2e39 1373 if (!flag_var)
b78351e5 1374 return;
1375
e472ce46 1376 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1377 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1378
f83b64ca 1379 if (opts_set != NULL)
1380 set_flag_var = option_flag_var (opt_index, opts_set);
1381
b78351e5 1382 switch (option->var_type)
1383 {
1384 case CLVC_BOOLEAN:
8e18705e 1385 if (option->cl_host_wide_int)
1386 {
1387 *(HOST_WIDE_INT *) flag_var = value;
1388 if (set_flag_var)
1389 *(HOST_WIDE_INT *) set_flag_var = 1;
1390 }
1391 else
1392 {
1393 *(int *) flag_var = value;
1394 if (set_flag_var)
1395 *(int *) set_flag_var = 1;
1396 }
1397
1398 break;
1399
1400 case CLVC_SIZE:
1401 if (option->cl_host_wide_int)
1402 {
1403 *(HOST_WIDE_INT *) flag_var = value;
1404 if (set_flag_var)
1405 *(HOST_WIDE_INT *) set_flag_var = value;
1406 }
1407 else
1408 {
1409 *(int *) flag_var = value;
1410 if (set_flag_var)
1411 *(int *) set_flag_var = value;
1412 }
1413
b78351e5 1414 break;
1415
1416 case CLVC_EQUAL:
8e18705e 1417 if (option->cl_host_wide_int)
1418 {
1419 *(HOST_WIDE_INT *) flag_var = (value
1420 ? option->var_value
1421 : !option->var_value);
1422 if (set_flag_var)
1423 *(HOST_WIDE_INT *) set_flag_var = 1;
1424 }
72ec6882 1425 else
8e18705e 1426 {
1427 *(int *) flag_var = (value
1428 ? option->var_value
1429 : !option->var_value);
1430 if (set_flag_var)
1431 *(int *) set_flag_var = 1;
1432 }
b78351e5 1433 break;
1434
1435 case CLVC_BIT_CLEAR:
1436 case CLVC_BIT_SET:
1437 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
72ec6882 1438 {
1439 if (option->cl_host_wide_int)
1440 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1441 else
1442 *(int *) flag_var |= option->var_value;
1443 }
b78351e5 1444 else
72ec6882 1445 {
1446 if (option->cl_host_wide_int)
1447 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1448 else
1449 *(int *) flag_var &= ~option->var_value;
1450 }
f83b64ca 1451 if (set_flag_var)
72ec6882 1452 {
1453 if (option->cl_host_wide_int)
1454 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1455 else
1456 *(int *) set_flag_var |= option->var_value;
1457 }
b78351e5 1458 break;
1459
1460 case CLVC_STRING:
2c5d2e39 1461 *(const char **) flag_var = arg;
f83b64ca 1462 if (set_flag_var)
1463 *(const char **) set_flag_var = "";
b78351e5 1464 break;
f0da0668 1465
d62a5950 1466 case CLVC_ENUM:
1467 {
1468 const struct cl_enum *e = &cl_enums[option->var_enum];
1469
1470 e->set (flag_var, value);
1471 if (set_flag_var)
1472 e->set (set_flag_var, 1);
1473 }
1474 break;
1475
f0da0668 1476 case CLVC_DEFER:
1477 {
f1f41a6c 1478 vec<cl_deferred_option> *v
1479 = (vec<cl_deferred_option> *) *(void **) flag_var;
e82e4eb5 1480 cl_deferred_option p = {opt_index, arg, value};
f1f41a6c 1481 if (!v)
1482 v = XCNEW (vec<cl_deferred_option>);
1483 v->safe_push (p);
1484 *(void **) flag_var = v;
f0da0668 1485 if (set_flag_var)
f1f41a6c 1486 *(void **) set_flag_var = v;
f0da0668 1487 }
1488 break;
b78351e5 1489 }
b78351e5 1490}
2c5d2e39 1491
1492/* Return the address of the flag variable for option OPT_INDEX in
1493 options structure OPTS, or NULL if there is no flag variable. */
1494
1495void *
1496option_flag_var (int opt_index, struct gcc_options *opts)
1497{
1498 const struct cl_option *option = &cl_options[opt_index];
1499
1500 if (option->flag_var_offset == (unsigned short) -1)
1501 return NULL;
1502 return (void *)(((char *) opts) + option->flag_var_offset);
1503}
c123f04d 1504
79396169 1505/* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1506 or -1 if it isn't a simple on-off switch. */
1507
1508int
1509option_enabled (int opt_idx, void *opts)
1510{
1511 const struct cl_option *option = &(cl_options[opt_idx]);
1512 struct gcc_options *optsg = (struct gcc_options *) opts;
1513 void *flag_var = option_flag_var (opt_idx, optsg);
1514
1515 if (flag_var)
1516 switch (option->var_type)
1517 {
1518 case CLVC_BOOLEAN:
8e18705e 1519 if (option->cl_host_wide_int)
1520 return *(HOST_WIDE_INT *) flag_var != 0;
1521 else
1522 return *(int *) flag_var != 0;
79396169 1523
1524 case CLVC_EQUAL:
72ec6882 1525 if (option->cl_host_wide_int)
1526 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1527 else
1528 return *(int *) flag_var == option->var_value;
79396169 1529
1530 case CLVC_BIT_CLEAR:
72ec6882 1531 if (option->cl_host_wide_int)
1532 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1533 else
1534 return (*(int *) flag_var & option->var_value) == 0;
79396169 1535
1536 case CLVC_BIT_SET:
72ec6882 1537 if (option->cl_host_wide_int)
1538 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1539 else
1540 return (*(int *) flag_var & option->var_value) != 0;
79396169 1541
8e18705e 1542 case CLVC_SIZE:
1543 if (option->cl_host_wide_int)
1544 return *(HOST_WIDE_INT *) flag_var != -1;
1545 else
1546 return *(int *) flag_var != -1;
1547
79396169 1548 case CLVC_STRING:
d62a5950 1549 case CLVC_ENUM:
79396169 1550 case CLVC_DEFER:
1551 break;
1552 }
1553 return -1;
1554}
1555
1556/* Fill STATE with the current state of option OPTION in OPTS. Return
1557 true if there is some state to store. */
1558
1559bool
1560get_option_state (struct gcc_options *opts, int option,
1561 struct cl_option_state *state)
1562{
1563 void *flag_var = option_flag_var (option, opts);
1564
1565 if (flag_var == 0)
1566 return false;
1567
1568 switch (cl_options[option].var_type)
1569 {
1570 case CLVC_BOOLEAN:
1571 case CLVC_EQUAL:
8e18705e 1572 case CLVC_SIZE:
79396169 1573 state->data = flag_var;
72ec6882 1574 state->size = (cl_options[option].cl_host_wide_int
1575 ? sizeof (HOST_WIDE_INT)
1576 : sizeof (int));
79396169 1577 break;
1578
1579 case CLVC_BIT_CLEAR:
1580 case CLVC_BIT_SET:
1581 state->ch = option_enabled (option, opts);
1582 state->data = &state->ch;
1583 state->size = 1;
1584 break;
1585
1586 case CLVC_STRING:
1587 state->data = *(const char **) flag_var;
1588 if (state->data == 0)
1589 state->data = "";
1590 state->size = strlen ((const char *) state->data) + 1;
1591 break;
1592
d62a5950 1593 case CLVC_ENUM:
1594 state->data = flag_var;
1595 state->size = cl_enums[cl_options[option].var_enum].var_size;
1596 break;
1597
79396169 1598 case CLVC_DEFER:
1599 return false;
1600 }
1601 return true;
1602}
1603
c123f04d 1604/* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1605 handlers HANDLERS) to have diagnostic kind KIND for option
1606 structures OPTS and OPTS_SET and diagnostic context DC (possibly
31ba81bd 1607 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1608 argument of the option for joined options, or NULL otherwise. If IMPLY,
c123f04d 1609 the warning option in question is implied at this point. This is
1610 used by -Werror= and #pragma GCC diagnostic. */
1611
1612void
31ba81bd 1613control_warning_option (unsigned int opt_index, int kind, const char *arg,
1614 bool imply, location_t loc, unsigned int lang_mask,
c123f04d 1615 const struct cl_option_handlers *handlers,
1616 struct gcc_options *opts,
1617 struct gcc_options *opts_set,
1618 diagnostic_context *dc)
1619{
1620 if (cl_options[opt_index].alias_target != N_OPTS)
7ca909c6 1621 {
1622 gcc_assert (!cl_options[opt_index].cl_separate_alias
1623 && !cl_options[opt_index].cl_negative_alias);
1624 if (cl_options[opt_index].alias_arg)
1625 arg = cl_options[opt_index].alias_arg;
1626 opt_index = cl_options[opt_index].alias_target;
1627 }
0c46a085 1628 if (opt_index == OPT_SPECIAL_ignore || opt_index == OPT_SPECIAL_deprecated)
c123f04d 1629 return;
1630 if (dc)
1631 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1632 if (imply)
1633 {
31ba81bd 1634 const struct cl_option *option = &cl_options[opt_index];
1635
c123f04d 1636 /* -Werror=foo implies -Wfoo. */
8e18705e 1637 if (option->var_type == CLVC_BOOLEAN
1638 || option->var_type == CLVC_ENUM
1639 || option->var_type == CLVC_SIZE)
31ba81bd 1640 {
8e18705e 1641 HOST_WIDE_INT value = 1;
31ba81bd 1642
1643 if (arg && *arg == '\0' && !option->cl_missing_ok)
1644 arg = NULL;
1645
1646 if ((option->flags & CL_JOINED) && arg == NULL)
1647 {
1648 cmdline_handle_error (loc, option, option->opt_text, arg,
1649 CL_ERR_MISSING_ARG, lang_mask);
1650 return;
1651 }
1652
8e18705e 1653 /* If the switch takes an integer argument, convert it. */
1654 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
31ba81bd 1655 {
8e18705e 1656 int error = 0;
1657 value = *arg ? integral_argument (arg, &error,
1658 option->cl_byte_size) : 0;
1659 if (error)
31ba81bd 1660 {
1661 cmdline_handle_error (loc, option, option->opt_text, arg,
1662 CL_ERR_UINT_ARG, lang_mask);
1663 return;
1664 }
1665 }
1666
1667 /* If the switch takes an enumerated argument, convert it. */
1668 if (arg && option->var_type == CLVC_ENUM)
1669 {
1670 const struct cl_enum *e = &cl_enums[option->var_enum];
1671
1672 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1673 {
1674 const char *carg = NULL;
1675
1676 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1677 arg = carg;
1678 gcc_assert (carg != NULL);
1679 }
1680 else
1681 {
1682 cmdline_handle_error (loc, option, option->opt_text, arg,
1683 CL_ERR_ENUM_ARG, lang_mask);
1684 return;
1685 }
1686 }
1687
1688 handle_generated_option (opts, opts_set,
1689 opt_index, arg, value, lang_mask,
09fd09eb 1690 kind, loc, handlers, false, dc);
31ba81bd 1691 }
c123f04d 1692 }
1693}