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