Daily bump.
[gcc.git] / gcc / opts-common.c
1 /* Command line option handling.
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "intl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "options.h"
26 #include "diagnostic.h"
27 #include "spellcheck.h"
28
29 static void prune_options (struct cl_decoded_option **, unsigned int *);
30
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
41 static bool
42 remapping_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
49 /* Perform a binary search to find which option the command-line INPUT
50 matches. Returns its index in the option array, and
51 OPT_SPECIAL_unknown on failure.
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
64 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
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. */
73 size_t
74 find_opt (const char *input, unsigned int lang_mask)
75 {
76 size_t mn, mn_orig, mx, md, opt_len;
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
97 mn_orig = mn;
98
99 /* This is the switch that is the best match but for a different
100 front end, or OPT_SPECIAL_unknown if there is no match at all. */
101 match_wrong_lang = OPT_SPECIAL_unknown;
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
119 if (remapping_prefix_p (opt))
120 return OPT_SPECIAL_unknown;
121
122 /* If we haven't remembered a prior match, remember this
123 one. Any prior match is necessarily better. */
124 if (match_wrong_lang == OPT_SPECIAL_unknown)
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
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
168 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
169 return match_wrong_lang;
170 }
171
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. */
176
177 HOST_WIDE_INT
178 integral_argument (const char *arg, int *err, bool byte_size_suffix)
179 {
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;
191
192 char *end = NULL;
193 unsigned HOST_WIDE_INT unit = 1;
194 unsigned HOST_WIDE_INT value = strtoull (arg, &end, 10);
195
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). */
199
200 if (end && *end)
201 {
202 if (!byte_size_suffix)
203 {
204 errno = 0;
205 value = strtoull (arg, &end, 0);
206 if (*end)
207 {
208 if (errno)
209 *err = errno;
210 else
211 *err = EINVAL;
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 }
255
256 if (unit)
257 {
258 unsigned HOST_WIDE_INT prod = value * unit;
259 value = prod < value ? HOST_WIDE_INT_M1U : prod;
260 }
261
262 return value;
263 }
264
265 /* Return whether OPTION is OK for the language given by
266 LANG_MASK. */
267 static bool
268 option_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
282 /* Return whether ENUM_ARG is OK for the language given by
283 LANG_MASK. */
284
285 static bool
286 enum_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
296 static bool
297 enum_arg_to_value (const struct cl_enum_arg *enum_args,
298 const char *arg, HOST_WIDE_INT *value,
299 unsigned int lang_mask)
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
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
318 bool
319 opt_enum_arg_to_value (size_t opt_index, const char *arg,
320 int *value, unsigned int lang_mask)
321 {
322 const struct cl_option *option = &cl_options[opt_index];
323
324 gcc_assert (option->var_type == CLVC_ENUM);
325
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;
335 }
336
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
344 bool
345 enum_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 }
370
371 /* Fill in the canonical option part of *DECODED with an option
372 described by OPT_INDEX, ARG and VALUE. */
373
374 static void
375 generate_canonical_option (size_t opt_index, const char *arg,
376 HOST_WIDE_INT value,
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
383 && !option->cl_reject_negative
384 && (opt_text[1] == 'W' || opt_text[1] == 'f'
385 || opt_text[1] == 'g' || opt_text[1] == 'm'))
386 {
387 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
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 {
402 if ((option->flags & CL_SEPARATE)
403 && !option->cl_separate_alias)
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);
412 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
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
425 /* Structure describing mappings from options on the command line to
426 options to look up with find_opt. */
427 struct 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 };
444 static const struct option_map option_map[] =
445 {
446 { "-Wno-", NULL, "-W", false, true },
447 { "-fno-", NULL, "-f", false, true },
448 { "-gno-", NULL, "-g", false, true },
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
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
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.
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
486 void
487 add_misspelling_candidates (auto_vec<char *> *candidates,
488 const struct cl_option *option,
489 const char *opt_text)
490 {
491 gcc_assert (candidates);
492 gcc_assert (option);
493 gcc_assert (opt_text);
494 if (remapping_prefix_p (option))
495 return;
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
503 if (option->cl_reject_negative && option_map[i].negated)
504 continue;
505
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 /* For all params (e.g. --param=key=value),
515 include also '--param key=value'. */
516 const char *prefix = "--param=";
517 if (strstr (opt_text, prefix) == opt_text)
518 {
519 char *param = xstrdup (opt_text + 1);
520 gcc_assert (param[6] == '=');
521 param[6] = ' ';
522 candidates->safe_push (param);
523 }
524 }
525
526 /* Decode the switch beginning at ARGV for the language indicated by
527 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
528 the structure *DECODED. Returns the number of switches
529 consumed. */
530
531 static unsigned int
532 decode_cmdline_option (const char *const *argv, unsigned int lang_mask,
533 struct cl_decoded_option *decoded)
534 {
535 size_t opt_index;
536 const char *arg = 0;
537 HOST_WIDE_INT value = 1;
538 unsigned int result = 1, i, extra_args, separate_args = 0;
539 int adjust_len = 0;
540 size_t total_len;
541 char *p;
542 const struct cl_option *option;
543 int errors = 0;
544 const char *warn_message = NULL;
545 bool separate_arg_flag;
546 bool joined_arg_flag;
547 bool have_separate_arg = false;
548
549 extra_args = 0;
550
551 const char *opt_value = argv[0] + 1;
552 opt_index = find_opt (opt_value, lang_mask);
553 i = 0;
554 while (opt_index == OPT_SPECIAL_unknown
555 && i < ARRAY_SIZE (option_map))
556 {
557 const char *opt0 = option_map[i].opt0;
558 const char *opt1 = option_map[i].opt1;
559 const char *new_prefix = option_map[i].new_prefix;
560 bool another_char_needed = option_map[i].another_char_needed;
561 size_t opt0_len = strlen (opt0);
562 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
563 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
564 size_t new_prefix_len = strlen (new_prefix);
565
566 extra_args = (opt1 == NULL ? 0 : 1);
567 value = !option_map[i].negated;
568
569 if (strncmp (argv[0], opt0, opt0_len) == 0
570 && (opt1 == NULL
571 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
572 && (!another_char_needed
573 || argv[extra_args][optn_len] != 0))
574 {
575 size_t arglen = strlen (argv[extra_args]);
576 char *dup;
577
578 adjust_len = (int) optn_len - (int) new_prefix_len;
579 dup = XNEWVEC (char, arglen + 1 - adjust_len);
580 memcpy (dup, new_prefix, new_prefix_len);
581 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
582 arglen - optn_len + 1);
583 opt_index = find_opt (dup + 1, lang_mask);
584 free (dup);
585 }
586 i++;
587 }
588
589 if (opt_index == OPT_SPECIAL_unknown)
590 {
591 arg = argv[0];
592 extra_args = 0;
593 value = 1;
594 goto done;
595 }
596
597 option = &cl_options[opt_index];
598
599 /* Reject negative form of switches that don't take negatives as
600 unrecognized. */
601 if (!value && option->cl_reject_negative)
602 {
603 opt_index = OPT_SPECIAL_unknown;
604 errors |= CL_ERR_NEGATIVE;
605 arg = argv[0];
606 goto done;
607 }
608
609 /* Clear the initial value for size options (it will be overwritten
610 later based on the Init(value) specification in the opt file. */
611 if (option->var_type == CLVC_SIZE)
612 value = 0;
613
614 result = extra_args + 1;
615 warn_message = option->warn_message;
616
617 /* Check to see if the option is disabled for this configuration. */
618 if (option->cl_disabled)
619 errors |= CL_ERR_DISABLED;
620
621 /* Determine whether there may be a separate argument based on
622 whether this option is being processed for the driver, and, if
623 so, how many such arguments. */
624 separate_arg_flag = ((option->flags & CL_SEPARATE)
625 && !(option->cl_no_driver_arg
626 && (lang_mask & CL_DRIVER)));
627 separate_args = (separate_arg_flag
628 ? option->cl_separate_nargs + 1
629 : 0);
630 joined_arg_flag = (option->flags & CL_JOINED) != 0;
631
632 /* Sort out any argument the switch takes. */
633 if (joined_arg_flag)
634 {
635 /* Have arg point to the original switch. This is because
636 some code, such as disable_builtin_function, expects its
637 argument to be persistent until the program exits. */
638 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
639
640 if (*arg == '\0' && !option->cl_missing_ok)
641 {
642 if (separate_arg_flag)
643 {
644 arg = argv[extra_args + 1];
645 result = extra_args + 2;
646 if (arg == NULL)
647 result = extra_args + 1;
648 else
649 have_separate_arg = true;
650 }
651 else
652 /* Missing argument. */
653 arg = NULL;
654 }
655 }
656 else if (separate_arg_flag)
657 {
658 arg = argv[extra_args + 1];
659 for (i = 0; i < separate_args; i++)
660 if (argv[extra_args + 1 + i] == NULL)
661 {
662 errors |= CL_ERR_MISSING_ARG;
663 break;
664 }
665 result = extra_args + 1 + i;
666 if (arg != NULL)
667 have_separate_arg = true;
668 }
669
670 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
671 errors |= CL_ERR_MISSING_ARG;
672
673 /* Is this option an alias (or an ignored option, marked as an alias
674 of OPT_SPECIAL_ignore)? */
675 if (option->alias_target != N_OPTS
676 && (!option->cl_separate_alias || have_separate_arg))
677 {
678 size_t new_opt_index = option->alias_target;
679
680 if (new_opt_index == OPT_SPECIAL_ignore
681 || new_opt_index == OPT_SPECIAL_warn_removed)
682 {
683 gcc_assert (option->alias_arg == NULL);
684 gcc_assert (option->neg_alias_arg == NULL);
685 opt_index = new_opt_index;
686 arg = NULL;
687 }
688 else
689 {
690 const struct cl_option *new_option = &cl_options[new_opt_index];
691
692 /* The new option must not be an alias itself. */
693 gcc_assert (new_option->alias_target == N_OPTS
694 || new_option->cl_separate_alias);
695
696 if (option->neg_alias_arg)
697 {
698 gcc_assert (option->alias_arg != NULL);
699 gcc_assert (arg == NULL);
700 gcc_assert (!option->cl_negative_alias);
701 if (value)
702 arg = option->alias_arg;
703 else
704 arg = option->neg_alias_arg;
705 value = 1;
706 }
707 else if (option->alias_arg)
708 {
709 gcc_assert (value == 1);
710 gcc_assert (arg == NULL);
711 gcc_assert (!option->cl_negative_alias);
712 arg = option->alias_arg;
713 }
714
715 if (option->cl_negative_alias)
716 value = !value;
717
718 opt_index = new_opt_index;
719 option = new_option;
720
721 if (value == 0)
722 gcc_assert (!option->cl_reject_negative);
723
724 /* Recompute what arguments are allowed. */
725 separate_arg_flag = ((option->flags & CL_SEPARATE)
726 && !(option->cl_no_driver_arg
727 && (lang_mask & CL_DRIVER)));
728 joined_arg_flag = (option->flags & CL_JOINED) != 0;
729
730 if (separate_args > 1 || option->cl_separate_nargs)
731 gcc_assert (separate_args
732 == (unsigned int) option->cl_separate_nargs + 1);
733
734 if (!(errors & CL_ERR_MISSING_ARG))
735 {
736 if (separate_arg_flag || joined_arg_flag)
737 {
738 if (option->cl_missing_ok && arg == NULL)
739 arg = "";
740 gcc_assert (arg != NULL);
741 }
742 else
743 gcc_assert (arg == NULL);
744 }
745
746 /* Recheck for warnings and disabled options. */
747 if (option->warn_message)
748 {
749 gcc_assert (warn_message == NULL);
750 warn_message = option->warn_message;
751 }
752 if (option->cl_disabled)
753 errors |= CL_ERR_DISABLED;
754 }
755 }
756
757 /* Check if this is a switch for a different front end. */
758 if (!option_ok_for_language (option, lang_mask))
759 errors |= CL_ERR_WRONG_LANG;
760 else if (strcmp (option->opt_text, "-Werror=") == 0
761 && strchr (opt_value, ',') == NULL)
762 {
763 /* Verify that -Werror argument is a valid warning
764 for a language. */
765 char *werror_arg = xstrdup (opt_value + 6);
766 werror_arg[0] = 'W';
767
768 size_t warning_index = find_opt (werror_arg, lang_mask);
769 if (warning_index != OPT_SPECIAL_unknown)
770 {
771 const struct cl_option *warning_option
772 = &cl_options[warning_index];
773 if (!option_ok_for_language (warning_option, lang_mask))
774 errors |= CL_ERR_WRONG_LANG;
775 }
776 }
777
778 /* Convert the argument to lowercase if appropriate. */
779 if (arg && option->cl_tolower)
780 {
781 size_t j;
782 size_t len = strlen (arg);
783 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
784
785 for (j = 0; j < len; j++)
786 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
787 arg_lower[len] = 0;
788 arg = arg_lower;
789 }
790
791 /* If the switch takes an integer argument, convert it. */
792 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
793 {
794 int error = 0;
795 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
796 if (error)
797 errors |= CL_ERR_UINT_ARG;
798
799 /* Reject value out of a range. */
800 if (option->range_max != -1
801 && (value < option->range_min || value > option->range_max))
802 errors |= CL_ERR_INT_RANGE_ARG;
803 }
804
805 /* If the switch takes an enumerated argument, convert it. */
806 if (arg && (option->var_type == CLVC_ENUM))
807 {
808 const struct cl_enum *e = &cl_enums[option->var_enum];
809
810 gcc_assert (value == 1);
811 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
812 {
813 const char *carg = NULL;
814
815 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
816 arg = carg;
817 gcc_assert (carg != NULL);
818 }
819 else
820 errors |= CL_ERR_ENUM_ARG;
821 }
822
823 done:
824 decoded->opt_index = opt_index;
825 decoded->arg = arg;
826 decoded->value = value;
827 decoded->errors = errors;
828 decoded->warn_message = warn_message;
829
830 if (opt_index == OPT_SPECIAL_unknown)
831 gcc_assert (result == 1);
832
833 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
834 decoded->canonical_option_num_elements = result;
835 total_len = 0;
836 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
837 {
838 if (i < result)
839 {
840 size_t len;
841 if (opt_index == OPT_SPECIAL_unknown)
842 decoded->canonical_option[i] = argv[i];
843 else
844 decoded->canonical_option[i] = NULL;
845 len = strlen (argv[i]);
846 /* If the argument is an empty string, we will print it as "" in
847 orig_option_with_args_text. */
848 total_len += (len != 0 ? len : 2) + 1;
849 }
850 else
851 decoded->canonical_option[i] = NULL;
852 }
853 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore
854 && opt_index != OPT_SPECIAL_warn_removed)
855 {
856 generate_canonical_option (opt_index, arg, value, decoded);
857 if (separate_args > 1)
858 {
859 for (i = 0; i < separate_args; i++)
860 {
861 if (argv[extra_args + 1 + i] == NULL)
862 break;
863 else
864 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
865 }
866 gcc_assert (result == 1 + i);
867 decoded->canonical_option_num_elements = result;
868 }
869 }
870 decoded->orig_option_with_args_text
871 = p = XOBNEWVEC (&opts_obstack, char, total_len);
872 for (i = 0; i < result; i++)
873 {
874 size_t len = strlen (argv[i]);
875
876 /* Print the empty string verbally. */
877 if (len == 0)
878 {
879 *p++ = '"';
880 *p++ = '"';
881 }
882 else
883 memcpy (p, argv[i], len);
884 p += len;
885 if (i == result - 1)
886 *p++ = 0;
887 else
888 *p++ = ' ';
889 }
890
891 return result;
892 }
893
894 /* Obstack for option strings. */
895
896 struct obstack opts_obstack;
897
898 /* Like libiberty concat, but allocate using opts_obstack. */
899
900 char *
901 opts_concat (const char *first, ...)
902 {
903 char *newstr, *end;
904 size_t length = 0;
905 const char *arg;
906 va_list ap;
907
908 /* First compute the size of the result and get sufficient memory. */
909 va_start (ap, first);
910 for (arg = first; arg; arg = va_arg (ap, const char *))
911 length += strlen (arg);
912 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
913 va_end (ap);
914
915 /* Now copy the individual pieces to the result string. */
916 va_start (ap, first);
917 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
918 {
919 length = strlen (arg);
920 memcpy (end, arg, length);
921 end += length;
922 }
923 *end = '\0';
924 va_end (ap);
925 return newstr;
926 }
927
928 /* Decode command-line options (ARGC and ARGV being the arguments of
929 main) into an array, setting *DECODED_OPTIONS to a pointer to that
930 array and *DECODED_OPTIONS_COUNT to the number of entries in the
931 array. The first entry in the array is always one for the program
932 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
933 flags applicable for decoding (including CL_COMMON and CL_TARGET if
934 those options should be considered applicable). Do not produce any
935 diagnostics or set state outside of these variables. */
936
937 void
938 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
939 unsigned int lang_mask,
940 struct cl_decoded_option **decoded_options,
941 unsigned int *decoded_options_count)
942 {
943 unsigned int n, i;
944 struct cl_decoded_option *opt_array;
945 unsigned int num_decoded_options;
946
947 int opt_array_len = argc;
948 opt_array = XNEWVEC (struct cl_decoded_option, opt_array_len);
949
950 opt_array[0].opt_index = OPT_SPECIAL_program_name;
951 opt_array[0].warn_message = NULL;
952 opt_array[0].arg = argv[0];
953 opt_array[0].orig_option_with_args_text = argv[0];
954 opt_array[0].canonical_option_num_elements = 1;
955 opt_array[0].canonical_option[0] = argv[0];
956 opt_array[0].canonical_option[1] = NULL;
957 opt_array[0].canonical_option[2] = NULL;
958 opt_array[0].canonical_option[3] = NULL;
959 opt_array[0].value = 1;
960 opt_array[0].errors = 0;
961 num_decoded_options = 1;
962
963 for (i = 1; i < argc; i += n)
964 {
965 const char *opt = argv[i];
966
967 /* Interpret "-" or a non-switch as a file name. */
968 if (opt[0] != '-' || opt[1] == '\0')
969 {
970 generate_option_input_file (opt, &opt_array[num_decoded_options]);
971 num_decoded_options++;
972 n = 1;
973 continue;
974 }
975
976 /* Interpret "--param" "key=name" as "--param=key=name". */
977 const char *needle = "--param";
978 if (i + 1 < argc && strcmp (opt, needle) == 0)
979 {
980 const char *replacement
981 = opts_concat (needle, "=", argv[i + 1], NULL);
982 argv[++i] = replacement;
983 }
984
985 /* Expand -fdiagnostics-plain-output to its constituents. This needs
986 to happen here so that prune_options can handle -fdiagnostics-color
987 specially. */
988 if (!strcmp (opt, "-fdiagnostics-plain-output"))
989 {
990 /* If you have changed the default diagnostics output, and this new
991 output is not appropriately "plain" (e.g., the change needs to be
992 undone in order for the testsuite to work properly), then please do
993 the following:
994 1. Add the necessary option to undo the new behavior to
995 the array below.
996 2. Update the documentation for -fdiagnostics-plain-output
997 in invoke.texi. */
998 const char *const expanded_args[] = {
999 "-fno-diagnostics-show-caret",
1000 "-fno-diagnostics-show-line-numbers",
1001 "-fdiagnostics-color=never",
1002 "-fdiagnostics-urls=never",
1003 "-fdiagnostics-path-format=separate-events",
1004 };
1005 const int num_expanded = ARRAY_SIZE (expanded_args);
1006 opt_array_len += num_expanded - 1;
1007 opt_array = XRESIZEVEC (struct cl_decoded_option,
1008 opt_array, opt_array_len);
1009 for (int j = 0, nj; j < num_expanded; j += nj)
1010 {
1011 nj = decode_cmdline_option (expanded_args + j, lang_mask,
1012 &opt_array[num_decoded_options]);
1013 num_decoded_options++;
1014 }
1015
1016 n = 1;
1017 continue;
1018 }
1019
1020 n = decode_cmdline_option (argv + i, lang_mask,
1021 &opt_array[num_decoded_options]);
1022 num_decoded_options++;
1023 }
1024
1025 *decoded_options = opt_array;
1026 *decoded_options_count = num_decoded_options;
1027 prune_options (decoded_options, decoded_options_count);
1028 }
1029
1030 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
1031 next one is the same as ORIG_NEXT_OPT_IDX. */
1032
1033 static bool
1034 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
1035 {
1036 /* An option can be canceled by the same option or an option with
1037 Negative. */
1038 if (cl_options [next_opt_idx].neg_index == opt_idx)
1039 return true;
1040
1041 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
1042 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
1043 orig_next_opt_idx);
1044
1045 return false;
1046 }
1047
1048 /* Filter out options canceled by the ones after them. */
1049
1050 static void
1051 prune_options (struct cl_decoded_option **decoded_options,
1052 unsigned int *decoded_options_count)
1053 {
1054 unsigned int old_decoded_options_count = *decoded_options_count;
1055 struct cl_decoded_option *old_decoded_options = *decoded_options;
1056 unsigned int new_decoded_options_count;
1057 struct cl_decoded_option *new_decoded_options
1058 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
1059 unsigned int i;
1060 const struct cl_option *option;
1061 unsigned int fdiagnostics_color_idx = 0;
1062
1063 /* Remove arguments which are negated by others after them. */
1064 new_decoded_options_count = 0;
1065 for (i = 0; i < old_decoded_options_count; i++)
1066 {
1067 unsigned int j, opt_idx, next_opt_idx;
1068
1069 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
1070 goto keep;
1071
1072 opt_idx = old_decoded_options[i].opt_index;
1073 switch (opt_idx)
1074 {
1075 case OPT_SPECIAL_unknown:
1076 case OPT_SPECIAL_ignore:
1077 case OPT_SPECIAL_warn_removed:
1078 case OPT_SPECIAL_program_name:
1079 case OPT_SPECIAL_input_file:
1080 goto keep;
1081
1082 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1083 case OPT_fdiagnostics_color_:
1084 fdiagnostics_color_idx = i;
1085 continue;
1086
1087 default:
1088 gcc_assert (opt_idx < cl_options_count);
1089 option = &cl_options[opt_idx];
1090 if (option->neg_index < 0)
1091 goto keep;
1092
1093 /* Skip joined switches. */
1094 if ((option->flags & CL_JOINED)
1095 && (!option->cl_reject_negative
1096 || (unsigned int) option->neg_index != opt_idx))
1097 goto keep;
1098
1099 for (j = i + 1; j < old_decoded_options_count; j++)
1100 {
1101 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1102 continue;
1103 next_opt_idx = old_decoded_options[j].opt_index;
1104 if (next_opt_idx >= cl_options_count)
1105 continue;
1106 if (cl_options[next_opt_idx].neg_index < 0)
1107 continue;
1108 if ((cl_options[next_opt_idx].flags & CL_JOINED)
1109 && (!cl_options[next_opt_idx].cl_reject_negative
1110 || ((unsigned int) cl_options[next_opt_idx].neg_index
1111 != next_opt_idx)))
1112 continue;
1113 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
1114 break;
1115 }
1116 if (j == old_decoded_options_count)
1117 {
1118 keep:
1119 new_decoded_options[new_decoded_options_count]
1120 = old_decoded_options[i];
1121 new_decoded_options_count++;
1122 }
1123 break;
1124 }
1125 }
1126
1127 if (fdiagnostics_color_idx >= 1)
1128 {
1129 /* We put the last -fdiagnostics-color= at the first position
1130 after argv[0] so it can take effect immediately. */
1131 memmove (new_decoded_options + 2, new_decoded_options + 1,
1132 sizeof (struct cl_decoded_option)
1133 * (new_decoded_options_count - 1));
1134 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
1135 new_decoded_options_count++;
1136 }
1137
1138 free (old_decoded_options);
1139 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1140 new_decoded_options,
1141 new_decoded_options_count);
1142 *decoded_options = new_decoded_options;
1143 *decoded_options_count = new_decoded_options_count;
1144 }
1145
1146 /* Handle option DECODED for the language indicated by LANG_MASK,
1147 using the handlers in HANDLERS and setting fields in OPTS and
1148 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1149 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1150 option for options from the source file, UNKNOWN_LOCATION
1151 otherwise. GENERATED_P is true for an option generated as part of
1152 processing another option or otherwise generated internally, false
1153 for one explicitly passed by the user. control_warning_option
1154 generated options are considered explicitly passed by the user.
1155 Returns false if the switch was invalid. DC is the diagnostic
1156 context for options affecting diagnostics state, or NULL. */
1157
1158 static bool
1159 handle_option (struct gcc_options *opts,
1160 struct gcc_options *opts_set,
1161 const struct cl_decoded_option *decoded,
1162 unsigned int lang_mask, int kind, location_t loc,
1163 const struct cl_option_handlers *handlers,
1164 bool generated_p, diagnostic_context *dc)
1165 {
1166 size_t opt_index = decoded->opt_index;
1167 const char *arg = decoded->arg;
1168 HOST_WIDE_INT value = decoded->value;
1169 const struct cl_option *option = &cl_options[opt_index];
1170 void *flag_var = option_flag_var (opt_index, opts);
1171 size_t i;
1172
1173 if (flag_var)
1174 set_option (opts, (generated_p ? NULL : opts_set),
1175 opt_index, value, arg, kind, loc, dc);
1176
1177 for (i = 0; i < handlers->num_handlers; i++)
1178 if (option->flags & handlers->handlers[i].mask)
1179 {
1180 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1181 lang_mask, kind, loc,
1182 handlers, dc,
1183 handlers->target_option_override_hook))
1184 return false;
1185 }
1186
1187 return true;
1188 }
1189
1190 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1191 option instead of DECODED. This is used for callbacks when one
1192 option implies another instead of an option being decoded from the
1193 command line. */
1194
1195 bool
1196 handle_generated_option (struct gcc_options *opts,
1197 struct gcc_options *opts_set,
1198 size_t opt_index, const char *arg, HOST_WIDE_INT value,
1199 unsigned int lang_mask, int kind, location_t loc,
1200 const struct cl_option_handlers *handlers,
1201 bool generated_p, diagnostic_context *dc)
1202 {
1203 struct cl_decoded_option decoded;
1204
1205 generate_option (opt_index, arg, value, lang_mask, &decoded);
1206 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1207 handlers, generated_p, dc);
1208 }
1209
1210 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1211 VALUE for a front end using LANG_MASK. This is used when the
1212 compiler generates options internally. */
1213
1214 void
1215 generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
1216 unsigned int lang_mask, struct cl_decoded_option *decoded)
1217 {
1218 const struct cl_option *option = &cl_options[opt_index];
1219
1220 decoded->opt_index = opt_index;
1221 decoded->warn_message = NULL;
1222 decoded->arg = arg;
1223 decoded->value = value;
1224 decoded->errors = (option_ok_for_language (option, lang_mask)
1225 ? 0
1226 : CL_ERR_WRONG_LANG);
1227
1228 generate_canonical_option (opt_index, arg, value, decoded);
1229 switch (decoded->canonical_option_num_elements)
1230 {
1231 case 1:
1232 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1233 break;
1234
1235 case 2:
1236 decoded->orig_option_with_args_text
1237 = opts_concat (decoded->canonical_option[0], " ",
1238 decoded->canonical_option[1], NULL);
1239 break;
1240
1241 default:
1242 gcc_unreachable ();
1243 }
1244 }
1245
1246 /* Fill in *DECODED with an option for input file FILE. */
1247
1248 void
1249 generate_option_input_file (const char *file,
1250 struct cl_decoded_option *decoded)
1251 {
1252 decoded->opt_index = OPT_SPECIAL_input_file;
1253 decoded->warn_message = NULL;
1254 decoded->arg = file;
1255 decoded->orig_option_with_args_text = file;
1256 decoded->canonical_option_num_elements = 1;
1257 decoded->canonical_option[0] = file;
1258 decoded->canonical_option[1] = NULL;
1259 decoded->canonical_option[2] = NULL;
1260 decoded->canonical_option[3] = NULL;
1261 decoded->value = 1;
1262 decoded->errors = 0;
1263 }
1264
1265 /* Helper function for listing valid choices and hint for misspelled
1266 value. CANDIDATES is a vector containing all valid strings,
1267 STR is set to a heap allocated string that contains all those
1268 strings concatenated, separated by spaces, and the return value
1269 is the closest string from those to ARG, or NULL if nothing is
1270 close enough. Callers should XDELETEVEC (STR) after using it
1271 to avoid memory leaks. */
1272
1273 const char *
1274 candidates_list_and_hint (const char *arg, char *&str,
1275 const auto_vec <const char *> &candidates)
1276 {
1277 size_t len = 0;
1278 int i;
1279 const char *candidate;
1280 char *p;
1281
1282 FOR_EACH_VEC_ELT (candidates, i, candidate)
1283 len += strlen (candidate) + 1;
1284
1285 str = p = XNEWVEC (char, len);
1286 FOR_EACH_VEC_ELT (candidates, i, candidate)
1287 {
1288 len = strlen (candidate);
1289 memcpy (p, candidate, len);
1290 p[len] = ' ';
1291 p += len + 1;
1292 }
1293 p[-1] = '\0';
1294 return find_closest_string (arg, &candidates);
1295 }
1296
1297 /* Perform diagnostics for read_cmdline_option and control_warning_option
1298 functions. Returns true if an error has been diagnosed.
1299 LOC and LANG_MASK arguments like in read_cmdline_option.
1300 OPTION is the option to report diagnostics for, OPT the name
1301 of the option as text, ARG the argument of the option (for joined
1302 options), ERRORS is bitmask of CL_ERR_* values. */
1303
1304 static bool
1305 cmdline_handle_error (location_t loc, const struct cl_option *option,
1306 const char *opt, const char *arg, int errors,
1307 unsigned int lang_mask)
1308 {
1309 if (errors & CL_ERR_DISABLED)
1310 {
1311 error_at (loc, "command-line option %qs"
1312 " is not supported by this configuration", opt);
1313 return true;
1314 }
1315
1316 if (errors & CL_ERR_MISSING_ARG)
1317 {
1318 if (option->missing_argument_error)
1319 error_at (loc, option->missing_argument_error, opt);
1320 else
1321 error_at (loc, "missing argument to %qs", opt);
1322 return true;
1323 }
1324
1325 if (errors & CL_ERR_UINT_ARG)
1326 {
1327 if (option->cl_byte_size)
1328 error_at (loc, "argument to %qs should be a non-negative integer "
1329 "optionally followed by a size unit",
1330 option->opt_text);
1331 else
1332 error_at (loc, "argument to %qs should be a non-negative integer",
1333 option->opt_text);
1334 return true;
1335 }
1336
1337 if (errors & CL_ERR_INT_RANGE_ARG)
1338 {
1339 error_at (loc, "argument to %qs is not between %d and %d",
1340 option->opt_text, option->range_min, option->range_max);
1341 return true;
1342 }
1343
1344 if (errors & CL_ERR_ENUM_ARG)
1345 {
1346 const struct cl_enum *e = &cl_enums[option->var_enum];
1347 unsigned int i;
1348 char *s;
1349
1350 auto_diagnostic_group d;
1351 if (e->unknown_error)
1352 error_at (loc, e->unknown_error, arg);
1353 else
1354 error_at (loc, "unrecognized argument in option %qs", opt);
1355
1356 auto_vec <const char *> candidates;
1357 for (i = 0; e->values[i].arg != NULL; i++)
1358 {
1359 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1360 continue;
1361 candidates.safe_push (e->values[i].arg);
1362 }
1363 const char *hint = candidates_list_and_hint (arg, s, candidates);
1364 if (hint)
1365 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1366 option->opt_text, s, hint);
1367 else
1368 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1369 XDELETEVEC (s);
1370
1371 return true;
1372 }
1373
1374 return false;
1375 }
1376
1377 /* Handle the switch DECODED (location LOC) for the language indicated
1378 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1379 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1380 diagnostic options. */
1381
1382 void
1383 read_cmdline_option (struct gcc_options *opts,
1384 struct gcc_options *opts_set,
1385 struct cl_decoded_option *decoded,
1386 location_t loc,
1387 unsigned int lang_mask,
1388 const struct cl_option_handlers *handlers,
1389 diagnostic_context *dc)
1390 {
1391 const struct cl_option *option;
1392 const char *opt = decoded->orig_option_with_args_text;
1393
1394 if (decoded->warn_message)
1395 warning_at (loc, 0, decoded->warn_message, opt);
1396
1397 if (decoded->opt_index == OPT_SPECIAL_unknown)
1398 {
1399 if (handlers->unknown_option_callback (decoded))
1400 error_at (loc, "unrecognized command-line option %qs", decoded->arg);
1401 return;
1402 }
1403
1404 if (decoded->opt_index == OPT_SPECIAL_ignore)
1405 return;
1406
1407 if (decoded->opt_index == OPT_SPECIAL_warn_removed)
1408 {
1409 /* Warn only about positive ignored options. */
1410 if (decoded->value)
1411 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1412 return;
1413 }
1414
1415 option = &cl_options[decoded->opt_index];
1416
1417 if (decoded->errors
1418 && cmdline_handle_error (loc, option, opt, decoded->arg,
1419 decoded->errors, lang_mask))
1420 return;
1421
1422 if (decoded->errors & CL_ERR_WRONG_LANG)
1423 {
1424 handlers->wrong_lang_callback (decoded, lang_mask);
1425 return;
1426 }
1427
1428 gcc_assert (!decoded->errors);
1429
1430 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1431 loc, handlers, false, dc))
1432 error_at (loc, "unrecognized command-line option %qs", opt);
1433 }
1434
1435 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1436 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1437 location LOC, using diagnostic context DC if not NULL for
1438 diagnostic classification. */
1439
1440 void
1441 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1442 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
1443 location_t loc, diagnostic_context *dc)
1444 {
1445 const struct cl_option *option = &cl_options[opt_index];
1446 void *flag_var = option_flag_var (opt_index, opts);
1447 void *set_flag_var = NULL;
1448
1449 if (!flag_var)
1450 return;
1451
1452 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1453 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1454
1455 if (opts_set != NULL)
1456 set_flag_var = option_flag_var (opt_index, opts_set);
1457
1458 switch (option->var_type)
1459 {
1460 case CLVC_BOOLEAN:
1461 if (option->cl_host_wide_int)
1462 {
1463 *(HOST_WIDE_INT *) flag_var = value;
1464 if (set_flag_var)
1465 *(HOST_WIDE_INT *) set_flag_var = 1;
1466 }
1467 else
1468 {
1469 if (value > INT_MAX)
1470 error_at (loc, "argument to %qs is bigger than %d",
1471 option->opt_text, INT_MAX);
1472 else
1473 {
1474 *(int *) flag_var = value;
1475 if (set_flag_var)
1476 *(int *) set_flag_var = 1;
1477 }
1478 }
1479
1480 break;
1481
1482 case CLVC_SIZE:
1483 if (option->cl_host_wide_int)
1484 {
1485 *(HOST_WIDE_INT *) flag_var = value;
1486 if (set_flag_var)
1487 *(HOST_WIDE_INT *) set_flag_var = value;
1488 }
1489 else
1490 {
1491 *(int *) flag_var = value;
1492 if (set_flag_var)
1493 *(int *) set_flag_var = value;
1494 }
1495
1496 break;
1497
1498 case CLVC_EQUAL:
1499 if (option->cl_host_wide_int)
1500 {
1501 *(HOST_WIDE_INT *) flag_var = (value
1502 ? option->var_value
1503 : !option->var_value);
1504 if (set_flag_var)
1505 *(HOST_WIDE_INT *) set_flag_var = 1;
1506 }
1507 else
1508 {
1509 *(int *) flag_var = (value
1510 ? option->var_value
1511 : !option->var_value);
1512 if (set_flag_var)
1513 *(int *) set_flag_var = 1;
1514 }
1515 break;
1516
1517 case CLVC_BIT_CLEAR:
1518 case CLVC_BIT_SET:
1519 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1520 {
1521 if (option->cl_host_wide_int)
1522 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1523 else
1524 *(int *) flag_var |= option->var_value;
1525 }
1526 else
1527 {
1528 if (option->cl_host_wide_int)
1529 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1530 else
1531 *(int *) flag_var &= ~option->var_value;
1532 }
1533 if (set_flag_var)
1534 {
1535 if (option->cl_host_wide_int)
1536 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1537 else
1538 *(int *) set_flag_var |= option->var_value;
1539 }
1540 break;
1541
1542 case CLVC_STRING:
1543 *(const char **) flag_var = arg;
1544 if (set_flag_var)
1545 *(const char **) set_flag_var = "";
1546 break;
1547
1548 case CLVC_ENUM:
1549 {
1550 const struct cl_enum *e = &cl_enums[option->var_enum];
1551
1552 e->set (flag_var, value);
1553 if (set_flag_var)
1554 e->set (set_flag_var, 1);
1555 }
1556 break;
1557
1558 case CLVC_DEFER:
1559 {
1560 vec<cl_deferred_option> *v
1561 = (vec<cl_deferred_option> *) *(void **) flag_var;
1562 cl_deferred_option p = {opt_index, arg, value};
1563 if (!v)
1564 v = XCNEW (vec<cl_deferred_option>);
1565 v->safe_push (p);
1566 *(void **) flag_var = v;
1567 if (set_flag_var)
1568 *(void **) set_flag_var = v;
1569 }
1570 break;
1571 }
1572 }
1573
1574 /* Return the address of the flag variable for option OPT_INDEX in
1575 options structure OPTS, or NULL if there is no flag variable. */
1576
1577 void *
1578 option_flag_var (int opt_index, struct gcc_options *opts)
1579 {
1580 const struct cl_option *option = &cl_options[opt_index];
1581
1582 if (option->flag_var_offset == (unsigned short) -1)
1583 return NULL;
1584 return (void *)(((char *) opts) + option->flag_var_offset);
1585 }
1586
1587 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1588 or -1 if it isn't a simple on-off switch. */
1589
1590 int
1591 option_enabled (int opt_idx, unsigned lang_mask, void *opts)
1592 {
1593 const struct cl_option *option = &(cl_options[opt_idx]);
1594
1595 /* A language-specific option can only be considered enabled when it's
1596 valid for the current language. */
1597 if (!(option->flags & CL_COMMON)
1598 && (option->flags & CL_LANG_ALL)
1599 && !(option->flags & lang_mask))
1600 return 0;
1601
1602 struct gcc_options *optsg = (struct gcc_options *) opts;
1603 void *flag_var = option_flag_var (opt_idx, optsg);
1604
1605 if (flag_var)
1606 switch (option->var_type)
1607 {
1608 case CLVC_BOOLEAN:
1609 if (option->cl_host_wide_int)
1610 return *(HOST_WIDE_INT *) flag_var != 0;
1611 else
1612 return *(int *) flag_var != 0;
1613
1614 case CLVC_EQUAL:
1615 if (option->cl_host_wide_int)
1616 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1617 else
1618 return *(int *) flag_var == option->var_value;
1619
1620 case CLVC_BIT_CLEAR:
1621 if (option->cl_host_wide_int)
1622 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1623 else
1624 return (*(int *) flag_var & option->var_value) == 0;
1625
1626 case CLVC_BIT_SET:
1627 if (option->cl_host_wide_int)
1628 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1629 else
1630 return (*(int *) flag_var & option->var_value) != 0;
1631
1632 case CLVC_SIZE:
1633 if (option->cl_host_wide_int)
1634 return *(HOST_WIDE_INT *) flag_var != -1;
1635 else
1636 return *(int *) flag_var != -1;
1637
1638 case CLVC_STRING:
1639 case CLVC_ENUM:
1640 case CLVC_DEFER:
1641 break;
1642 }
1643 return -1;
1644 }
1645
1646 /* Fill STATE with the current state of option OPTION in OPTS. Return
1647 true if there is some state to store. */
1648
1649 bool
1650 get_option_state (struct gcc_options *opts, int option,
1651 struct cl_option_state *state)
1652 {
1653 void *flag_var = option_flag_var (option, opts);
1654
1655 if (flag_var == 0)
1656 return false;
1657
1658 switch (cl_options[option].var_type)
1659 {
1660 case CLVC_BOOLEAN:
1661 case CLVC_EQUAL:
1662 case CLVC_SIZE:
1663 state->data = flag_var;
1664 state->size = (cl_options[option].cl_host_wide_int
1665 ? sizeof (HOST_WIDE_INT)
1666 : sizeof (int));
1667 break;
1668
1669 case CLVC_BIT_CLEAR:
1670 case CLVC_BIT_SET:
1671 state->ch = option_enabled (option, -1, opts);
1672 state->data = &state->ch;
1673 state->size = 1;
1674 break;
1675
1676 case CLVC_STRING:
1677 state->data = *(const char **) flag_var;
1678 if (state->data == 0)
1679 state->data = "";
1680 state->size = strlen ((const char *) state->data) + 1;
1681 break;
1682
1683 case CLVC_ENUM:
1684 state->data = flag_var;
1685 state->size = cl_enums[cl_options[option].var_enum].var_size;
1686 break;
1687
1688 case CLVC_DEFER:
1689 return false;
1690 }
1691 return true;
1692 }
1693
1694 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1695 handlers HANDLERS) to have diagnostic kind KIND for option
1696 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1697 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1698 argument of the option for joined options, or NULL otherwise. If IMPLY,
1699 the warning option in question is implied at this point. This is
1700 used by -Werror= and #pragma GCC diagnostic. */
1701
1702 void
1703 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1704 bool imply, location_t loc, unsigned int lang_mask,
1705 const struct cl_option_handlers *handlers,
1706 struct gcc_options *opts,
1707 struct gcc_options *opts_set,
1708 diagnostic_context *dc)
1709 {
1710 if (cl_options[opt_index].alias_target != N_OPTS)
1711 {
1712 gcc_assert (!cl_options[opt_index].cl_separate_alias
1713 && !cl_options[opt_index].cl_negative_alias);
1714 if (cl_options[opt_index].alias_arg)
1715 arg = cl_options[opt_index].alias_arg;
1716 opt_index = cl_options[opt_index].alias_target;
1717 }
1718 if (opt_index == OPT_SPECIAL_ignore || opt_index == OPT_SPECIAL_warn_removed)
1719 return;
1720 if (dc)
1721 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1722 if (imply)
1723 {
1724 const struct cl_option *option = &cl_options[opt_index];
1725
1726 /* -Werror=foo implies -Wfoo. */
1727 if (option->var_type == CLVC_BOOLEAN
1728 || option->var_type == CLVC_ENUM
1729 || option->var_type == CLVC_SIZE)
1730 {
1731 HOST_WIDE_INT value = 1;
1732
1733 if (arg && *arg == '\0' && !option->cl_missing_ok)
1734 arg = NULL;
1735
1736 if ((option->flags & CL_JOINED) && arg == NULL)
1737 {
1738 cmdline_handle_error (loc, option, option->opt_text, arg,
1739 CL_ERR_MISSING_ARG, lang_mask);
1740 return;
1741 }
1742
1743 /* If the switch takes an integer argument, convert it. */
1744 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
1745 {
1746 int error = 0;
1747 value = *arg ? integral_argument (arg, &error,
1748 option->cl_byte_size) : 0;
1749 if (error)
1750 {
1751 cmdline_handle_error (loc, option, option->opt_text, arg,
1752 CL_ERR_UINT_ARG, lang_mask);
1753 return;
1754 }
1755 }
1756
1757 /* If the switch takes an enumerated argument, convert it. */
1758 if (arg && option->var_type == CLVC_ENUM)
1759 {
1760 const struct cl_enum *e = &cl_enums[option->var_enum];
1761
1762 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1763 {
1764 const char *carg = NULL;
1765
1766 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1767 arg = carg;
1768 gcc_assert (carg != NULL);
1769 }
1770 else
1771 {
1772 cmdline_handle_error (loc, option, option->opt_text, arg,
1773 CL_ERR_ENUM_ARG, lang_mask);
1774 return;
1775 }
1776 }
1777
1778 handle_generated_option (opts, opts_set,
1779 opt_index, arg, value, lang_mask,
1780 kind, loc, handlers, false, dc);
1781 }
1782 }
1783 }
1784
1785 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
1786 Store number of arguments into ARGC_P. */
1787
1788 void
1789 parse_options_from_collect_gcc_options (const char *collect_gcc_options,
1790 obstack *argv_obstack,
1791 int *argc_p)
1792 {
1793 char *argv_storage = xstrdup (collect_gcc_options);
1794 int j, k;
1795
1796 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
1797 {
1798 if (argv_storage[j] == '\'')
1799 {
1800 obstack_ptr_grow (argv_obstack, &argv_storage[k]);
1801 ++j;
1802 do
1803 {
1804 if (argv_storage[j] == '\0')
1805 fatal_error (input_location,
1806 "malformed %<COLLECT_GCC_OPTIONS%>");
1807 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
1808 {
1809 argv_storage[k++] = '\'';
1810 j += 4;
1811 }
1812 else if (argv_storage[j] == '\'')
1813 break;
1814 else
1815 argv_storage[k++] = argv_storage[j++];
1816 }
1817 while (1);
1818 argv_storage[k++] = '\0';
1819 }
1820 }
1821
1822 obstack_ptr_grow (argv_obstack, NULL);
1823 *argc_p = obstack_object_size (argv_obstack) / sizeof (void *) - 1;
1824 }
1825
1826 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
1827 and push on O. */
1828
1829 void prepend_xassembler_to_collect_as_options (const char *collect_as_options,
1830 obstack *o)
1831 {
1832 obstack opts_obstack;
1833 int opts_count;
1834
1835 obstack_init (&opts_obstack);
1836 parse_options_from_collect_gcc_options (collect_as_options,
1837 &opts_obstack, &opts_count);
1838 const char **assembler_opts = XOBFINISH (&opts_obstack, const char **);
1839
1840 for (int i = 0; i < opts_count; i++)
1841 {
1842 obstack_grow (o, " '-Xassembler' ",
1843 strlen (" '-Xassembler' "));
1844 const char *opt = assembler_opts[i];
1845 obstack_1grow (o, '\'');
1846 obstack_grow (o, opt, strlen (opt));
1847 obstack_1grow (o, '\'');
1848 }
1849 }