]> gcc.gnu.org Git - gcc.git/blob - gcc/opts-common.cc
fortran: Expand ieee_arithmetic module's ieee_value inline [PR106579]
[gcc.git] / gcc / opts-common.cc
1 /* Command line option handling.
2 Copyright (C) 2006-2022 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 the cl_enum_arg
293 index and storing the value in *VALUE if found, and returning -1 without
294 modifying *VALUE if not found. */
295
296 static int
297 enum_arg_to_value (const struct cl_enum_arg *enum_args,
298 const char *arg, size_t len, 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 ((len
305 ? (strncmp (arg, enum_args[i].arg, len) == 0
306 && enum_args[i].arg[len] == '\0')
307 : strcmp (arg, enum_args[i].arg) == 0)
308 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
309 {
310 *value = enum_args[i].value;
311 return i;
312 }
313
314 return -1;
315 }
316
317 /* Look up ARG in the enum used by option OPT_INDEX for language
318 LANG_MASK, returning true and storing the value in *VALUE if found,
319 and returning false without modifying *VALUE if not found. */
320
321 bool
322 opt_enum_arg_to_value (size_t opt_index, const char *arg,
323 int *value, unsigned int lang_mask)
324 {
325 const struct cl_option *option = &cl_options[opt_index];
326
327 gcc_assert (option->var_type == CLVC_ENUM);
328
329 HOST_WIDE_INT wideval;
330 if (enum_arg_to_value (cl_enums[option->var_enum].values, arg, 0,
331 &wideval, lang_mask) >= 0)
332 {
333 *value = wideval;
334 return true;
335 }
336
337 return false;
338 }
339
340 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
341 corresponding string in *ARGP, returning true if the found string
342 was marked as canonical, false otherwise. If VALUE is not found
343 (which may be the case for uninitialized values if the relevant
344 option has not been passed), set *ARGP to NULL and return
345 false. */
346
347 bool
348 enum_value_to_arg (const struct cl_enum_arg *enum_args,
349 const char **argp, int value, unsigned int lang_mask)
350 {
351 unsigned int i;
352
353 for (i = 0; enum_args[i].arg != NULL; i++)
354 if (enum_args[i].value == value
355 && (enum_args[i].flags & CL_ENUM_CANONICAL)
356 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
357 {
358 *argp = enum_args[i].arg;
359 return true;
360 }
361
362 for (i = 0; enum_args[i].arg != NULL; i++)
363 if (enum_args[i].value == value
364 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
365 {
366 *argp = enum_args[i].arg;
367 return false;
368 }
369
370 *argp = NULL;
371 return false;
372 }
373
374 /* Fill in the canonical option part of *DECODED with an option
375 described by OPT_INDEX, ARG and VALUE. */
376
377 static void
378 generate_canonical_option (size_t opt_index, const char *arg,
379 HOST_WIDE_INT value,
380 struct cl_decoded_option *decoded)
381 {
382 const struct cl_option *option = &cl_options[opt_index];
383 const char *opt_text = option->opt_text;
384
385 if (value == 0
386 && !option->cl_reject_negative
387 && (opt_text[1] == 'W' || opt_text[1] == 'f'
388 || opt_text[1] == 'g' || opt_text[1] == 'm'))
389 {
390 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
391 t[0] = '-';
392 t[1] = opt_text[1];
393 t[2] = 'n';
394 t[3] = 'o';
395 t[4] = '-';
396 memcpy (t + 5, opt_text + 2, option->opt_len);
397 opt_text = t;
398 }
399
400 decoded->canonical_option[2] = NULL;
401 decoded->canonical_option[3] = NULL;
402
403 if (arg)
404 {
405 if ((option->flags & CL_SEPARATE)
406 && !option->cl_separate_alias)
407 {
408 decoded->canonical_option[0] = opt_text;
409 decoded->canonical_option[1] = arg;
410 decoded->canonical_option_num_elements = 2;
411 }
412 else
413 {
414 gcc_assert (option->flags & CL_JOINED);
415 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
416 decoded->canonical_option[1] = NULL;
417 decoded->canonical_option_num_elements = 1;
418 }
419 }
420 else
421 {
422 decoded->canonical_option[0] = opt_text;
423 decoded->canonical_option[1] = NULL;
424 decoded->canonical_option_num_elements = 1;
425 }
426 }
427
428 /* Structure describing mappings from options on the command line to
429 options to look up with find_opt. */
430 struct option_map
431 {
432 /* Prefix of the option on the command line. */
433 const char *opt0;
434 /* If two argv elements are considered to be merged into one option,
435 prefix for the second element, otherwise NULL. */
436 const char *opt1;
437 /* The new prefix to map to. */
438 const char *new_prefix;
439 /* Whether at least one character is needed following opt1 or opt0
440 for this mapping to be used. (--optimize= is valid for -O, but
441 --warn- is not valid for -W.) */
442 bool another_char_needed;
443 /* Whether the original option is a negated form of the option
444 resulting from this map. */
445 bool negated;
446 };
447 static const struct option_map option_map[] =
448 {
449 { "-Wno-", NULL, "-W", false, true },
450 { "-fno-", NULL, "-f", false, true },
451 { "-gno-", NULL, "-g", false, true },
452 { "-mno-", NULL, "-m", false, true },
453 { "--debug=", NULL, "-g", false, false },
454 { "--machine-", NULL, "-m", true, false },
455 { "--machine-no-", NULL, "-m", false, true },
456 { "--machine=", NULL, "-m", false, false },
457 { "--machine=no-", NULL, "-m", false, true },
458 { "--machine", "", "-m", false, false },
459 { "--machine", "no-", "-m", false, true },
460 { "--optimize=", NULL, "-O", false, false },
461 { "--std=", NULL, "-std=", false, false },
462 { "--std", "", "-std=", false, false },
463 { "--warn-", NULL, "-W", true, false },
464 { "--warn-no-", NULL, "-W", false, true },
465 { "--", NULL, "-f", true, false },
466 { "--no-", NULL, "-f", false, true }
467 };
468
469 /* Helper function for gcc.cc's driver::suggest_option, for populating the
470 vec of suggestions for misspelled options.
471
472 option_map above provides various prefixes for spelling command-line
473 options, which decode_cmdline_option uses to map spellings of options
474 to specific options. We want to do the reverse: to find all the ways
475 that a user could validly spell an option.
476
477 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
478 of its valid variant spellings to CANDIDATES, each without a leading
479 dash.
480
481 For example, given "-Wabi-tag", the following are added to CANDIDATES:
482 "Wabi-tag"
483 "Wno-abi-tag"
484 "-warn-abi-tag"
485 "-warn-no-abi-tag".
486
487 The added strings must be freed using free. */
488
489 void
490 add_misspelling_candidates (auto_vec<char *> *candidates,
491 const struct cl_option *option,
492 const char *opt_text)
493 {
494 gcc_assert (candidates);
495 gcc_assert (option);
496 gcc_assert (opt_text);
497 if (remapping_prefix_p (option))
498 return;
499 candidates->safe_push (xstrdup (opt_text + 1));
500 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
501 {
502 const char *opt0 = option_map[i].opt0;
503 const char *new_prefix = option_map[i].new_prefix;
504 size_t new_prefix_len = strlen (new_prefix);
505
506 if (option->cl_reject_negative && option_map[i].negated)
507 continue;
508
509 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
510 {
511 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
512 NULL);
513 candidates->safe_push (alternative);
514 }
515 }
516
517 /* For all params (e.g. --param=key=value),
518 include also '--param key=value'. */
519 const char *prefix = "--param=";
520 if (strstr (opt_text, prefix) == opt_text)
521 {
522 char *param = xstrdup (opt_text + 1);
523 gcc_assert (param[6] == '=');
524 param[6] = ' ';
525 candidates->safe_push (param);
526 }
527 }
528
529 /* Decode the switch beginning at ARGV for the language indicated by
530 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
531 the structure *DECODED. Returns the number of switches
532 consumed. */
533
534 static unsigned int
535 decode_cmdline_option (const char *const *argv, unsigned int lang_mask,
536 struct cl_decoded_option *decoded)
537 {
538 size_t opt_index;
539 const char *arg = 0;
540 HOST_WIDE_INT value = 1, mask = 0;
541 unsigned int result = 1, i, extra_args, separate_args = 0;
542 int adjust_len = 0;
543 size_t total_len;
544 char *p;
545 const struct cl_option *option;
546 int errors = 0;
547 const char *warn_message = NULL;
548 bool separate_arg_flag;
549 bool joined_arg_flag;
550 bool have_separate_arg = false;
551
552 extra_args = 0;
553
554 const char *opt_value = argv[0] + 1;
555 opt_index = find_opt (opt_value, lang_mask);
556 i = 0;
557 while (opt_index == OPT_SPECIAL_unknown
558 && i < ARRAY_SIZE (option_map))
559 {
560 const char *opt0 = option_map[i].opt0;
561 const char *opt1 = option_map[i].opt1;
562 const char *new_prefix = option_map[i].new_prefix;
563 bool another_char_needed = option_map[i].another_char_needed;
564 size_t opt0_len = strlen (opt0);
565 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
566 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
567 size_t new_prefix_len = strlen (new_prefix);
568
569 extra_args = (opt1 == NULL ? 0 : 1);
570 value = !option_map[i].negated;
571
572 if (strncmp (argv[0], opt0, opt0_len) == 0
573 && (opt1 == NULL
574 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
575 && (!another_char_needed
576 || argv[extra_args][optn_len] != 0))
577 {
578 size_t arglen = strlen (argv[extra_args]);
579 char *dup;
580
581 adjust_len = (int) optn_len - (int) new_prefix_len;
582 dup = XNEWVEC (char, arglen + 1 - adjust_len);
583 memcpy (dup, new_prefix, new_prefix_len);
584 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
585 arglen - optn_len + 1);
586 opt_index = find_opt (dup + 1, lang_mask);
587 free (dup);
588 }
589 i++;
590 }
591
592 if (opt_index == OPT_SPECIAL_unknown)
593 {
594 arg = argv[0];
595 extra_args = 0;
596 value = 1;
597 goto done;
598 }
599
600 option = &cl_options[opt_index];
601
602 /* Reject negative form of switches that don't take negatives as
603 unrecognized. */
604 if (!value && option->cl_reject_negative)
605 {
606 opt_index = OPT_SPECIAL_unknown;
607 errors |= CL_ERR_NEGATIVE;
608 arg = argv[0];
609 goto done;
610 }
611
612 /* Clear the initial value for size options (it will be overwritten
613 later based on the Init(value) specification in the opt file. */
614 if (option->var_type == CLVC_SIZE)
615 value = 0;
616
617 result = extra_args + 1;
618 warn_message = option->warn_message;
619
620 /* Check to see if the option is disabled for this configuration. */
621 if (option->cl_disabled)
622 errors |= CL_ERR_DISABLED;
623
624 /* Determine whether there may be a separate argument based on
625 whether this option is being processed for the driver, and, if
626 so, how many such arguments. */
627 separate_arg_flag = ((option->flags & CL_SEPARATE)
628 && !(option->cl_no_driver_arg
629 && (lang_mask & CL_DRIVER)));
630 separate_args = (separate_arg_flag
631 ? option->cl_separate_nargs + 1
632 : 0);
633 joined_arg_flag = (option->flags & CL_JOINED) != 0;
634
635 /* Sort out any argument the switch takes. */
636 if (joined_arg_flag)
637 {
638 /* Have arg point to the original switch. This is because
639 some code, such as disable_builtin_function, expects its
640 argument to be persistent until the program exits. */
641 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
642
643 if (*arg == '\0' && !option->cl_missing_ok)
644 {
645 if (separate_arg_flag)
646 {
647 arg = argv[extra_args + 1];
648 result = extra_args + 2;
649 if (arg == NULL)
650 result = extra_args + 1;
651 else
652 have_separate_arg = true;
653 }
654 else
655 /* Missing argument. */
656 arg = NULL;
657 }
658 }
659 else if (separate_arg_flag)
660 {
661 arg = argv[extra_args + 1];
662 for (i = 0; i < separate_args; i++)
663 if (argv[extra_args + 1 + i] == NULL)
664 {
665 errors |= CL_ERR_MISSING_ARG;
666 break;
667 }
668 result = extra_args + 1 + i;
669 if (arg != NULL)
670 have_separate_arg = true;
671 }
672
673 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
674 errors |= CL_ERR_MISSING_ARG;
675
676 /* Is this option an alias (or an ignored option, marked as an alias
677 of OPT_SPECIAL_ignore)? */
678 if (option->alias_target != N_OPTS
679 && (!option->cl_separate_alias || have_separate_arg))
680 {
681 size_t new_opt_index = option->alias_target;
682
683 if (new_opt_index == OPT_SPECIAL_ignore
684 || new_opt_index == OPT_SPECIAL_warn_removed)
685 {
686 gcc_assert (option->alias_arg == NULL);
687 gcc_assert (option->neg_alias_arg == NULL);
688 opt_index = new_opt_index;
689 arg = NULL;
690 }
691 else
692 {
693 const struct cl_option *new_option = &cl_options[new_opt_index];
694
695 /* The new option must not be an alias itself. */
696 gcc_assert (new_option->alias_target == N_OPTS
697 || new_option->cl_separate_alias);
698
699 if (option->neg_alias_arg)
700 {
701 gcc_assert (option->alias_arg != NULL);
702 gcc_assert (arg == NULL);
703 gcc_assert (!option->cl_negative_alias);
704 if (value)
705 arg = option->alias_arg;
706 else
707 arg = option->neg_alias_arg;
708 value = 1;
709 }
710 else if (option->alias_arg)
711 {
712 gcc_assert (value == 1);
713 gcc_assert (arg == NULL);
714 gcc_assert (!option->cl_negative_alias);
715 arg = option->alias_arg;
716 }
717
718 if (option->cl_negative_alias)
719 value = !value;
720
721 opt_index = new_opt_index;
722 option = new_option;
723
724 if (value == 0)
725 gcc_assert (!option->cl_reject_negative);
726
727 /* Recompute what arguments are allowed. */
728 separate_arg_flag = ((option->flags & CL_SEPARATE)
729 && !(option->cl_no_driver_arg
730 && (lang_mask & CL_DRIVER)));
731 joined_arg_flag = (option->flags & CL_JOINED) != 0;
732
733 if (separate_args > 1 || option->cl_separate_nargs)
734 gcc_assert (separate_args
735 == (unsigned int) option->cl_separate_nargs + 1);
736
737 if (!(errors & CL_ERR_MISSING_ARG))
738 {
739 if (separate_arg_flag || joined_arg_flag)
740 {
741 if (option->cl_missing_ok && arg == NULL)
742 arg = "";
743 gcc_assert (arg != NULL);
744 }
745 else
746 gcc_assert (arg == NULL);
747 }
748
749 /* Recheck for warnings and disabled options. */
750 if (option->warn_message)
751 {
752 gcc_assert (warn_message == NULL);
753 warn_message = option->warn_message;
754 }
755 if (option->cl_disabled)
756 errors |= CL_ERR_DISABLED;
757 }
758 }
759
760 /* Check if this is a switch for a different front end. */
761 if (!option_ok_for_language (option, lang_mask))
762 errors |= CL_ERR_WRONG_LANG;
763 else if (strcmp (option->opt_text, "-Werror=") == 0
764 && strchr (opt_value, ',') == NULL)
765 {
766 /* Verify that -Werror argument is a valid warning
767 for a language. */
768 char *werror_arg = xstrdup (opt_value + 6);
769 werror_arg[0] = 'W';
770
771 size_t warning_index = find_opt (werror_arg, lang_mask);
772 free (werror_arg);
773 if (warning_index != OPT_SPECIAL_unknown)
774 {
775 const struct cl_option *warning_option
776 = &cl_options[warning_index];
777 if (!option_ok_for_language (warning_option, lang_mask))
778 errors |= CL_ERR_WRONG_LANG;
779 }
780 }
781
782 /* Convert the argument to lowercase if appropriate. */
783 if (arg && option->cl_tolower)
784 {
785 size_t j;
786 size_t len = strlen (arg);
787 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
788
789 for (j = 0; j < len; j++)
790 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
791 arg_lower[len] = 0;
792 arg = arg_lower;
793 }
794
795 /* If the switch takes an integer argument, convert it. */
796 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
797 {
798 int error = 0;
799 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
800 if (error)
801 errors |= CL_ERR_UINT_ARG;
802
803 /* Reject value out of a range. */
804 if (option->range_max != -1
805 && (value < option->range_min || value > option->range_max))
806 errors |= CL_ERR_INT_RANGE_ARG;
807 }
808
809 /* If the switch takes an enumerated argument, convert it. */
810 if (arg && (option->var_type == CLVC_ENUM))
811 {
812 const struct cl_enum *e = &cl_enums[option->var_enum];
813
814 gcc_assert (option->var_value != CLEV_NORMAL || value == 1);
815 if (option->var_value != CLEV_NORMAL)
816 {
817 const char *p = arg;
818 HOST_WIDE_INT sum_value = 0;
819 unsigned HOST_WIDE_INT used_sets = 0;
820 do
821 {
822 const char *q = strchr (p, ',');
823 HOST_WIDE_INT this_value = 0;
824 if (q && q == p)
825 {
826 errors |= CL_ERR_ENUM_SET_ARG;
827 break;
828 }
829 int idx = enum_arg_to_value (e->values, p, q ? q - p : 0,
830 &this_value, lang_mask);
831 if (idx < 0)
832 {
833 errors |= CL_ERR_ENUM_SET_ARG;
834 break;
835 }
836
837 HOST_WIDE_INT this_mask = 0;
838 if (option->var_value == CLEV_SET)
839 {
840 unsigned set = e->values[idx].flags >> CL_ENUM_SET_SHIFT;
841 gcc_checking_assert (set >= 1
842 && set <= HOST_BITS_PER_WIDE_INT);
843 if ((used_sets & (HOST_WIDE_INT_1U << (set - 1))) != 0)
844 {
845 errors |= CL_ERR_ENUM_SET_ARG;
846 break;
847 }
848 used_sets |= HOST_WIDE_INT_1U << (set - 1);
849
850 for (int i = 0; e->values[i].arg != NULL; i++)
851 if (set == (e->values[i].flags >> CL_ENUM_SET_SHIFT))
852 this_mask |= e->values[i].value;
853 }
854 else
855 {
856 gcc_assert (option->var_value == CLEV_BITSET
857 && ((e->values[idx].flags >> CL_ENUM_SET_SHIFT)
858 == 0));
859 this_mask = this_value;
860 }
861
862 sum_value |= this_value;
863 mask |= this_mask;
864 if (q == NULL)
865 break;
866 p = q + 1;
867 }
868 while (1);
869 if (value == 1)
870 value = sum_value;
871 else
872 gcc_checking_assert (value == 0);
873 }
874 else if (enum_arg_to_value (e->values, arg, 0, &value, lang_mask) >= 0)
875 {
876 const char *carg = NULL;
877
878 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
879 arg = carg;
880 gcc_assert (carg != NULL);
881 }
882 else
883 errors |= CL_ERR_ENUM_ARG;
884 }
885
886 done:
887 decoded->opt_index = opt_index;
888 decoded->arg = arg;
889 decoded->value = value;
890 decoded->mask = mask;
891 decoded->errors = errors;
892 decoded->warn_message = warn_message;
893
894 if (opt_index == OPT_SPECIAL_unknown)
895 gcc_assert (result == 1);
896
897 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
898 decoded->canonical_option_num_elements = result;
899 total_len = 0;
900 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
901 {
902 if (i < result)
903 {
904 size_t len;
905 if (opt_index == OPT_SPECIAL_unknown)
906 decoded->canonical_option[i] = argv[i];
907 else
908 decoded->canonical_option[i] = NULL;
909 len = strlen (argv[i]);
910 /* If the argument is an empty string, we will print it as "" in
911 orig_option_with_args_text. */
912 total_len += (len != 0 ? len : 2) + 1;
913 }
914 else
915 decoded->canonical_option[i] = NULL;
916 }
917 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore
918 && opt_index != OPT_SPECIAL_warn_removed)
919 {
920 generate_canonical_option (opt_index, arg, value, decoded);
921 if (separate_args > 1)
922 {
923 for (i = 0; i < separate_args; i++)
924 {
925 if (argv[extra_args + 1 + i] == NULL)
926 break;
927 else
928 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
929 }
930 gcc_assert (result == 1 + i);
931 decoded->canonical_option_num_elements = result;
932 }
933 }
934 decoded->orig_option_with_args_text
935 = p = XOBNEWVEC (&opts_obstack, char, total_len);
936 for (i = 0; i < result; i++)
937 {
938 size_t len = strlen (argv[i]);
939
940 /* Print the empty string verbally. */
941 if (len == 0)
942 {
943 *p++ = '"';
944 *p++ = '"';
945 }
946 else
947 memcpy (p, argv[i], len);
948 p += len;
949 if (i == result - 1)
950 *p++ = 0;
951 else
952 *p++ = ' ';
953 }
954
955 return result;
956 }
957
958 /* Obstack for option strings. */
959
960 struct obstack opts_obstack;
961
962 /* Like libiberty concat, but allocate using opts_obstack. */
963
964 char *
965 opts_concat (const char *first, ...)
966 {
967 char *newstr, *end;
968 size_t length = 0;
969 const char *arg;
970 va_list ap;
971
972 /* First compute the size of the result and get sufficient memory. */
973 va_start (ap, first);
974 for (arg = first; arg; arg = va_arg (ap, const char *))
975 length += strlen (arg);
976 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
977 va_end (ap);
978
979 /* Now copy the individual pieces to the result string. */
980 va_start (ap, first);
981 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
982 {
983 length = strlen (arg);
984 memcpy (end, arg, length);
985 end += length;
986 }
987 *end = '\0';
988 va_end (ap);
989 return newstr;
990 }
991
992 /* Decode command-line options (ARGC and ARGV being the arguments of
993 main) into an array, setting *DECODED_OPTIONS to a pointer to that
994 array and *DECODED_OPTIONS_COUNT to the number of entries in the
995 array. The first entry in the array is always one for the program
996 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
997 flags applicable for decoding (including CL_COMMON and CL_TARGET if
998 those options should be considered applicable). Do not produce any
999 diagnostics or set state outside of these variables. */
1000
1001 void
1002 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
1003 unsigned int lang_mask,
1004 struct cl_decoded_option **decoded_options,
1005 unsigned int *decoded_options_count)
1006 {
1007 unsigned int n, i;
1008 struct cl_decoded_option *opt_array;
1009 unsigned int num_decoded_options;
1010
1011 int opt_array_len = argc;
1012 opt_array = XNEWVEC (struct cl_decoded_option, opt_array_len);
1013
1014 opt_array[0].opt_index = OPT_SPECIAL_program_name;
1015 opt_array[0].warn_message = NULL;
1016 opt_array[0].arg = argv[0];
1017 opt_array[0].orig_option_with_args_text = argv[0];
1018 opt_array[0].canonical_option_num_elements = 1;
1019 opt_array[0].canonical_option[0] = argv[0];
1020 opt_array[0].canonical_option[1] = NULL;
1021 opt_array[0].canonical_option[2] = NULL;
1022 opt_array[0].canonical_option[3] = NULL;
1023 opt_array[0].value = 1;
1024 opt_array[0].mask = 0;
1025 opt_array[0].errors = 0;
1026 num_decoded_options = 1;
1027
1028 for (i = 1; i < argc; i += n)
1029 {
1030 const char *opt = argv[i];
1031
1032 /* Interpret "-" or a non-switch as a file name. */
1033 if (opt[0] != '-' || opt[1] == '\0')
1034 {
1035 generate_option_input_file (opt, &opt_array[num_decoded_options]);
1036 num_decoded_options++;
1037 n = 1;
1038 continue;
1039 }
1040
1041 /* Interpret "--param" "key=name" as "--param=key=name". */
1042 const char *needle = "--param";
1043 if (i + 1 < argc && strcmp (opt, needle) == 0)
1044 {
1045 const char *replacement
1046 = opts_concat (needle, "=", argv[i + 1], NULL);
1047 argv[++i] = replacement;
1048 }
1049
1050 /* Expand -fdiagnostics-plain-output to its constituents. This needs
1051 to happen here so that prune_options can handle -fdiagnostics-color
1052 specially. */
1053 if (!strcmp (opt, "-fdiagnostics-plain-output"))
1054 {
1055 /* If you have changed the default diagnostics output, and this new
1056 output is not appropriately "plain" (e.g., the change needs to be
1057 undone in order for the testsuite to work properly), then please do
1058 the following:
1059 1. Add the necessary option to undo the new behavior to
1060 the array below.
1061 2. Update the documentation for -fdiagnostics-plain-output
1062 in invoke.texi. */
1063 const char *const expanded_args[] = {
1064 "-fno-diagnostics-show-caret",
1065 "-fno-diagnostics-show-line-numbers",
1066 "-fdiagnostics-color=never",
1067 "-fdiagnostics-urls=never",
1068 "-fdiagnostics-path-format=separate-events",
1069 };
1070 const int num_expanded = ARRAY_SIZE (expanded_args);
1071 opt_array_len += num_expanded - 1;
1072 opt_array = XRESIZEVEC (struct cl_decoded_option,
1073 opt_array, opt_array_len);
1074 for (int j = 0, nj; j < num_expanded; j += nj)
1075 {
1076 nj = decode_cmdline_option (expanded_args + j, lang_mask,
1077 &opt_array[num_decoded_options]);
1078 num_decoded_options++;
1079 }
1080
1081 n = 1;
1082 continue;
1083 }
1084
1085 n = decode_cmdline_option (argv + i, lang_mask,
1086 &opt_array[num_decoded_options]);
1087 num_decoded_options++;
1088 }
1089
1090 *decoded_options = opt_array;
1091 *decoded_options_count = num_decoded_options;
1092 prune_options (decoded_options, decoded_options_count);
1093 }
1094
1095 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
1096 next one is the same as ORIG_NEXT_OPT_IDX. */
1097
1098 static bool
1099 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
1100 {
1101 /* An option can be canceled by the same option or an option with
1102 Negative. */
1103 if (cl_options [next_opt_idx].neg_index == opt_idx)
1104 return true;
1105
1106 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
1107 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
1108 orig_next_opt_idx);
1109
1110 return false;
1111 }
1112
1113 /* Filter out options canceled by the ones after them. */
1114
1115 static void
1116 prune_options (struct cl_decoded_option **decoded_options,
1117 unsigned int *decoded_options_count)
1118 {
1119 unsigned int old_decoded_options_count = *decoded_options_count;
1120 struct cl_decoded_option *old_decoded_options = *decoded_options;
1121 unsigned int new_decoded_options_count;
1122 struct cl_decoded_option *new_decoded_options
1123 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
1124 unsigned int i;
1125 const struct cl_option *option;
1126 unsigned int fdiagnostics_color_idx = 0;
1127
1128 /* Remove arguments which are negated by others after them. */
1129 new_decoded_options_count = 0;
1130 for (i = 0; i < old_decoded_options_count; i++)
1131 {
1132 unsigned int j, opt_idx, next_opt_idx;
1133
1134 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
1135 goto keep;
1136
1137 opt_idx = old_decoded_options[i].opt_index;
1138 switch (opt_idx)
1139 {
1140 case OPT_SPECIAL_unknown:
1141 case OPT_SPECIAL_ignore:
1142 case OPT_SPECIAL_warn_removed:
1143 case OPT_SPECIAL_program_name:
1144 case OPT_SPECIAL_input_file:
1145 goto keep;
1146
1147 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1148 case OPT_fdiagnostics_color_:
1149 fdiagnostics_color_idx = i;
1150 continue;
1151
1152 default:
1153 gcc_assert (opt_idx < cl_options_count);
1154 option = &cl_options[opt_idx];
1155 if (option->neg_index < 0)
1156 goto keep;
1157
1158 /* Skip joined switches. */
1159 if ((option->flags & CL_JOINED)
1160 && (!option->cl_reject_negative
1161 || (unsigned int) option->neg_index != opt_idx))
1162 goto keep;
1163
1164 for (j = i + 1; j < old_decoded_options_count; j++)
1165 {
1166 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1167 continue;
1168 next_opt_idx = old_decoded_options[j].opt_index;
1169 if (next_opt_idx >= cl_options_count)
1170 continue;
1171 if (cl_options[next_opt_idx].neg_index < 0)
1172 continue;
1173 if ((cl_options[next_opt_idx].flags & CL_JOINED)
1174 && (!cl_options[next_opt_idx].cl_reject_negative
1175 || ((unsigned int) cl_options[next_opt_idx].neg_index
1176 != next_opt_idx)))
1177 continue;
1178 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
1179 break;
1180 }
1181 if (j == old_decoded_options_count)
1182 {
1183 keep:
1184 new_decoded_options[new_decoded_options_count]
1185 = old_decoded_options[i];
1186 new_decoded_options_count++;
1187 }
1188 break;
1189 }
1190 }
1191
1192 if (fdiagnostics_color_idx >= 1)
1193 {
1194 /* We put the last -fdiagnostics-color= at the first position
1195 after argv[0] so it can take effect immediately. */
1196 memmove (new_decoded_options + 2, new_decoded_options + 1,
1197 sizeof (struct cl_decoded_option)
1198 * (new_decoded_options_count - 1));
1199 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
1200 new_decoded_options_count++;
1201 }
1202
1203 free (old_decoded_options);
1204 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1205 new_decoded_options,
1206 new_decoded_options_count);
1207 *decoded_options = new_decoded_options;
1208 *decoded_options_count = new_decoded_options_count;
1209 }
1210
1211 /* Handle option DECODED for the language indicated by LANG_MASK,
1212 using the handlers in HANDLERS and setting fields in OPTS and
1213 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1214 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1215 option for options from the source file, UNKNOWN_LOCATION
1216 otherwise. GENERATED_P is true for an option generated as part of
1217 processing another option or otherwise generated internally, false
1218 for one explicitly passed by the user. control_warning_option
1219 generated options are considered explicitly passed by the user.
1220 Returns false if the switch was invalid. DC is the diagnostic
1221 context for options affecting diagnostics state, or NULL. */
1222
1223 static bool
1224 handle_option (struct gcc_options *opts,
1225 struct gcc_options *opts_set,
1226 const struct cl_decoded_option *decoded,
1227 unsigned int lang_mask, int kind, location_t loc,
1228 const struct cl_option_handlers *handlers,
1229 bool generated_p, diagnostic_context *dc)
1230 {
1231 size_t opt_index = decoded->opt_index;
1232 const char *arg = decoded->arg;
1233 HOST_WIDE_INT value = decoded->value;
1234 HOST_WIDE_INT mask = decoded->mask;
1235 const struct cl_option *option = &cl_options[opt_index];
1236 void *flag_var = option_flag_var (opt_index, opts);
1237 size_t i;
1238
1239 if (flag_var)
1240 set_option (opts, (generated_p ? NULL : opts_set),
1241 opt_index, value, arg, kind, loc, dc, mask);
1242
1243 for (i = 0; i < handlers->num_handlers; i++)
1244 if (option->flags & handlers->handlers[i].mask)
1245 {
1246 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1247 lang_mask, kind, loc,
1248 handlers, dc,
1249 handlers->target_option_override_hook))
1250 return false;
1251 }
1252
1253 return true;
1254 }
1255
1256 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1257 option instead of DECODED. This is used for callbacks when one
1258 option implies another instead of an option being decoded from the
1259 command line. */
1260
1261 bool
1262 handle_generated_option (struct gcc_options *opts,
1263 struct gcc_options *opts_set,
1264 size_t opt_index, const char *arg, HOST_WIDE_INT value,
1265 unsigned int lang_mask, int kind, location_t loc,
1266 const struct cl_option_handlers *handlers,
1267 bool generated_p, diagnostic_context *dc)
1268 {
1269 struct cl_decoded_option decoded;
1270
1271 generate_option (opt_index, arg, value, lang_mask, &decoded);
1272 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1273 handlers, generated_p, dc);
1274 }
1275
1276 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1277 VALUE for a front end using LANG_MASK. This is used when the
1278 compiler generates options internally. */
1279
1280 void
1281 generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
1282 unsigned int lang_mask, struct cl_decoded_option *decoded)
1283 {
1284 const struct cl_option *option = &cl_options[opt_index];
1285
1286 decoded->opt_index = opt_index;
1287 decoded->warn_message = NULL;
1288 decoded->arg = arg;
1289 decoded->value = value;
1290 decoded->mask = 0;
1291 decoded->errors = (option_ok_for_language (option, lang_mask)
1292 ? 0
1293 : CL_ERR_WRONG_LANG);
1294
1295 generate_canonical_option (opt_index, arg, value, decoded);
1296 switch (decoded->canonical_option_num_elements)
1297 {
1298 case 1:
1299 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1300 break;
1301
1302 case 2:
1303 decoded->orig_option_with_args_text
1304 = opts_concat (decoded->canonical_option[0], " ",
1305 decoded->canonical_option[1], NULL);
1306 break;
1307
1308 default:
1309 gcc_unreachable ();
1310 }
1311 }
1312
1313 /* Fill in *DECODED with an option for input file FILE. */
1314
1315 void
1316 generate_option_input_file (const char *file,
1317 struct cl_decoded_option *decoded)
1318 {
1319 decoded->opt_index = OPT_SPECIAL_input_file;
1320 decoded->warn_message = NULL;
1321 decoded->arg = file;
1322 decoded->orig_option_with_args_text = file;
1323 decoded->canonical_option_num_elements = 1;
1324 decoded->canonical_option[0] = file;
1325 decoded->canonical_option[1] = NULL;
1326 decoded->canonical_option[2] = NULL;
1327 decoded->canonical_option[3] = NULL;
1328 decoded->value = 1;
1329 decoded->mask = 0;
1330 decoded->errors = 0;
1331 }
1332
1333 /* Helper function for listing valid choices and hint for misspelled
1334 value. CANDIDATES is a vector containing all valid strings,
1335 STR is set to a heap allocated string that contains all those
1336 strings concatenated, separated by spaces, and the return value
1337 is the closest string from those to ARG, or NULL if nothing is
1338 close enough. Callers should XDELETEVEC (STR) after using it
1339 to avoid memory leaks. */
1340
1341 const char *
1342 candidates_list_and_hint (const char *arg, char *&str,
1343 const auto_vec <const char *> &candidates)
1344 {
1345 size_t len = 0;
1346 int i;
1347 const char *candidate;
1348 char *p;
1349
1350 FOR_EACH_VEC_ELT (candidates, i, candidate)
1351 len += strlen (candidate) + 1;
1352
1353 str = p = XNEWVEC (char, len);
1354 FOR_EACH_VEC_ELT (candidates, i, candidate)
1355 {
1356 len = strlen (candidate);
1357 memcpy (p, candidate, len);
1358 p[len] = ' ';
1359 p += len + 1;
1360 }
1361 p[-1] = '\0';
1362 return find_closest_string (arg, &candidates);
1363 }
1364
1365 /* Perform diagnostics for read_cmdline_option and control_warning_option
1366 functions. Returns true if an error has been diagnosed.
1367 LOC and LANG_MASK arguments like in read_cmdline_option.
1368 OPTION is the option to report diagnostics for, OPT the name
1369 of the option as text, ARG the argument of the option (for joined
1370 options), ERRORS is bitmask of CL_ERR_* values. */
1371
1372 static bool
1373 cmdline_handle_error (location_t loc, const struct cl_option *option,
1374 const char *opt, const char *arg, int errors,
1375 unsigned int lang_mask)
1376 {
1377 if (errors & CL_ERR_DISABLED)
1378 {
1379 error_at (loc, "command-line option %qs"
1380 " is not supported by this configuration", opt);
1381 return true;
1382 }
1383
1384 if (errors & CL_ERR_MISSING_ARG)
1385 {
1386 if (option->missing_argument_error)
1387 error_at (loc, option->missing_argument_error, opt);
1388 else
1389 error_at (loc, "missing argument to %qs", opt);
1390 return true;
1391 }
1392
1393 if (errors & CL_ERR_UINT_ARG)
1394 {
1395 if (option->cl_byte_size)
1396 error_at (loc, "argument to %qs should be a non-negative integer "
1397 "optionally followed by a size unit",
1398 option->opt_text);
1399 else
1400 error_at (loc, "argument to %qs should be a non-negative integer",
1401 option->opt_text);
1402 return true;
1403 }
1404
1405 if (errors & CL_ERR_INT_RANGE_ARG)
1406 {
1407 error_at (loc, "argument to %qs is not between %d and %d",
1408 option->opt_text, option->range_min, option->range_max);
1409 return true;
1410 }
1411
1412 if (errors & CL_ERR_ENUM_SET_ARG)
1413 {
1414 const struct cl_enum *e = &cl_enums[option->var_enum];
1415 const char *p = arg;
1416 unsigned HOST_WIDE_INT used_sets = 0;
1417 const char *second_opt = NULL;
1418 size_t second_opt_len = 0;
1419 errors = 0;
1420 do
1421 {
1422 const char *q = strchr (p, ',');
1423 HOST_WIDE_INT this_value = 0;
1424 if (q && q == p)
1425 {
1426 arg = "";
1427 errors = CL_ERR_ENUM_ARG;
1428 break;
1429 }
1430 int idx = enum_arg_to_value (e->values, p, q ? q - p : 0,
1431 &this_value, lang_mask);
1432 if (idx < 0)
1433 {
1434 if (q == NULL)
1435 q = strchr (p, '\0');
1436 char *narg = XALLOCAVEC (char, (q - p) + 1);
1437 memcpy (narg, p, q - p);
1438 narg[q - p] = '\0';
1439 arg = narg;
1440 errors = CL_ERR_ENUM_ARG;
1441 break;
1442 }
1443
1444 if (option->var_value == CLEV_BITSET)
1445 {
1446 if (q == NULL)
1447 break;
1448 p = q + 1;
1449 continue;
1450 }
1451
1452 unsigned set = e->values[idx].flags >> CL_ENUM_SET_SHIFT;
1453 gcc_checking_assert (set >= 1 && set <= HOST_BITS_PER_WIDE_INT);
1454 if ((used_sets & (HOST_WIDE_INT_1U << (set - 1))) != 0)
1455 {
1456 if (q == NULL)
1457 q = strchr (p, '\0');
1458 if (second_opt == NULL)
1459 {
1460 used_sets = HOST_WIDE_INT_1U << (set - 1);
1461 second_opt = p;
1462 second_opt_len = q - p;
1463 p = arg;
1464 continue;
1465 }
1466 char *args = XALLOCAVEC (char, (q - p) + 1 + second_opt_len + 1);
1467 memcpy (args, p, q - p);
1468 args[q - p] = '\0';
1469 memcpy (args + (q - p) + 1, second_opt, second_opt_len);
1470 args[(q - p) + 1 + second_opt_len] = '\0';
1471 error_at (loc, "invalid argument in option %qs", opt);
1472 if (strcmp (args, args + (q - p) + 1) == 0)
1473 inform (loc, "%qs specified multiple times in the same option",
1474 args);
1475 else
1476 inform (loc, "%qs is mutually exclusive with %qs and cannot be"
1477 " specified together", args, args + (q - p) + 1);
1478 return true;
1479 }
1480 used_sets |= HOST_WIDE_INT_1U << (set - 1);
1481 if (q == NULL)
1482 break;
1483 p = q + 1;
1484 }
1485 while (1);
1486 }
1487
1488 if (errors & CL_ERR_ENUM_ARG)
1489 {
1490 const struct cl_enum *e = &cl_enums[option->var_enum];
1491 unsigned int i;
1492 char *s;
1493
1494 auto_diagnostic_group d;
1495 if (e->unknown_error)
1496 error_at (loc, e->unknown_error, arg);
1497 else
1498 error_at (loc, "unrecognized argument in option %qs", opt);
1499
1500 auto_vec <const char *> candidates;
1501 for (i = 0; e->values[i].arg != NULL; i++)
1502 {
1503 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1504 continue;
1505 candidates.safe_push (e->values[i].arg);
1506 }
1507 const char *hint = candidates_list_and_hint (arg, s, candidates);
1508 if (hint)
1509 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1510 option->opt_text, s, hint);
1511 else
1512 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1513 XDELETEVEC (s);
1514
1515 return true;
1516 }
1517
1518 return false;
1519 }
1520
1521 /* Handle the switch DECODED (location LOC) for the language indicated
1522 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1523 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1524 diagnostic options. */
1525
1526 void
1527 read_cmdline_option (struct gcc_options *opts,
1528 struct gcc_options *opts_set,
1529 struct cl_decoded_option *decoded,
1530 location_t loc,
1531 unsigned int lang_mask,
1532 const struct cl_option_handlers *handlers,
1533 diagnostic_context *dc)
1534 {
1535 const struct cl_option *option;
1536 const char *opt = decoded->orig_option_with_args_text;
1537
1538 if (decoded->warn_message)
1539 warning_at (loc, 0, decoded->warn_message, opt);
1540
1541 if (decoded->opt_index == OPT_SPECIAL_unknown)
1542 {
1543 if (handlers->unknown_option_callback (decoded))
1544 error_at (loc, "unrecognized command-line option %qs", decoded->arg);
1545 return;
1546 }
1547
1548 if (decoded->opt_index == OPT_SPECIAL_ignore)
1549 return;
1550
1551 if (decoded->opt_index == OPT_SPECIAL_warn_removed)
1552 {
1553 /* Warn only about positive ignored options. */
1554 if (decoded->value)
1555 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1556 return;
1557 }
1558
1559 option = &cl_options[decoded->opt_index];
1560
1561 if (decoded->errors
1562 && cmdline_handle_error (loc, option, opt, decoded->arg,
1563 decoded->errors, lang_mask))
1564 return;
1565
1566 if (decoded->errors & CL_ERR_WRONG_LANG)
1567 {
1568 handlers->wrong_lang_callback (decoded, lang_mask);
1569 return;
1570 }
1571
1572 gcc_assert (!decoded->errors);
1573
1574 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1575 loc, handlers, false, dc))
1576 error_at (loc, "unrecognized command-line option %qs", opt);
1577 }
1578
1579 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1580 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1581 location LOC, using diagnostic context DC if not NULL for
1582 diagnostic classification. */
1583
1584 void
1585 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1586 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
1587 location_t loc, diagnostic_context *dc,
1588 HOST_WIDE_INT mask /* = 0 */)
1589 {
1590 const struct cl_option *option = &cl_options[opt_index];
1591 void *flag_var = option_flag_var (opt_index, opts);
1592 void *set_flag_var = NULL;
1593
1594 if (!flag_var)
1595 return;
1596
1597 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1598 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1599
1600 if (opts_set != NULL)
1601 set_flag_var = option_flag_var (opt_index, opts_set);
1602
1603 switch (option->var_type)
1604 {
1605 case CLVC_INTEGER:
1606 if (option->cl_host_wide_int)
1607 {
1608 *(HOST_WIDE_INT *) flag_var = value;
1609 if (set_flag_var)
1610 *(HOST_WIDE_INT *) set_flag_var = 1;
1611 }
1612 else
1613 {
1614 if (value > INT_MAX)
1615 error_at (loc, "argument to %qs is bigger than %d",
1616 option->opt_text, INT_MAX);
1617 else
1618 {
1619 *(int *) flag_var = value;
1620 if (set_flag_var)
1621 *(int *) set_flag_var = 1;
1622 }
1623 }
1624
1625 break;
1626
1627 case CLVC_SIZE:
1628 if (option->cl_host_wide_int)
1629 {
1630 *(HOST_WIDE_INT *) flag_var = value;
1631 if (set_flag_var)
1632 *(HOST_WIDE_INT *) set_flag_var = value;
1633 }
1634 else
1635 {
1636 *(int *) flag_var = value;
1637 if (set_flag_var)
1638 *(int *) set_flag_var = value;
1639 }
1640
1641 break;
1642
1643 case CLVC_EQUAL:
1644 if (option->cl_host_wide_int)
1645 {
1646 *(HOST_WIDE_INT *) flag_var = (value
1647 ? option->var_value
1648 : !option->var_value);
1649 if (set_flag_var)
1650 *(HOST_WIDE_INT *) set_flag_var = 1;
1651 }
1652 else
1653 {
1654 *(int *) flag_var = (value
1655 ? option->var_value
1656 : !option->var_value);
1657 if (set_flag_var)
1658 *(int *) set_flag_var = 1;
1659 }
1660 break;
1661
1662 case CLVC_BIT_CLEAR:
1663 case CLVC_BIT_SET:
1664 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1665 {
1666 if (option->cl_host_wide_int)
1667 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1668 else
1669 *(int *) flag_var |= option->var_value;
1670 }
1671 else
1672 {
1673 if (option->cl_host_wide_int)
1674 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1675 else
1676 *(int *) flag_var &= ~option->var_value;
1677 }
1678 if (set_flag_var)
1679 {
1680 if (option->cl_host_wide_int)
1681 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1682 else
1683 *(int *) set_flag_var |= option->var_value;
1684 }
1685 break;
1686
1687 case CLVC_STRING:
1688 *(const char **) flag_var = arg;
1689 if (set_flag_var)
1690 *(const char **) set_flag_var = "";
1691 break;
1692
1693 case CLVC_ENUM:
1694 {
1695 const struct cl_enum *e = &cl_enums[option->var_enum];
1696
1697 if (mask)
1698 e->set (flag_var, value | (e->get (flag_var) & ~mask));
1699 else
1700 e->set (flag_var, value);
1701 if (set_flag_var)
1702 e->set (set_flag_var, 1);
1703 }
1704 break;
1705
1706 case CLVC_DEFER:
1707 {
1708 vec<cl_deferred_option> *v
1709 = (vec<cl_deferred_option> *) *(void **) flag_var;
1710 cl_deferred_option p = {opt_index, arg, value};
1711 if (!v)
1712 v = XCNEW (vec<cl_deferred_option>);
1713 v->safe_push (p);
1714 *(void **) flag_var = v;
1715 if (set_flag_var)
1716 *(void **) set_flag_var = v;
1717 }
1718 break;
1719 }
1720 }
1721
1722 /* Return the address of the flag variable for option OPT_INDEX in
1723 options structure OPTS, or NULL if there is no flag variable. */
1724
1725 void *
1726 option_flag_var (int opt_index, struct gcc_options *opts)
1727 {
1728 const struct cl_option *option = &cl_options[opt_index];
1729
1730 if (option->flag_var_offset == (unsigned short) -1)
1731 return NULL;
1732 return (void *)(((char *) opts) + option->flag_var_offset);
1733 }
1734
1735 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1736 or -1 if it isn't a simple on-off switch
1737 (or if the value is unknown, typically set later in target). */
1738
1739 int
1740 option_enabled (int opt_idx, unsigned lang_mask, void *opts)
1741 {
1742 const struct cl_option *option = &(cl_options[opt_idx]);
1743
1744 /* A language-specific option can only be considered enabled when it's
1745 valid for the current language. */
1746 if (!(option->flags & CL_COMMON)
1747 && (option->flags & CL_LANG_ALL)
1748 && !(option->flags & lang_mask))
1749 return 0;
1750
1751 struct gcc_options *optsg = (struct gcc_options *) opts;
1752 void *flag_var = option_flag_var (opt_idx, optsg);
1753
1754 if (flag_var)
1755 switch (option->var_type)
1756 {
1757 case CLVC_INTEGER:
1758 if (option->cl_host_wide_int)
1759 {
1760 HOST_WIDE_INT v = *(HOST_WIDE_INT *) flag_var;
1761 return v != 0 ? (v < 0 ? -1 : 1) : 0;
1762 }
1763 else
1764 {
1765 int v = *(int *) flag_var;
1766 return v != 0 ? (v < 0 ? -1 : 1) : 0;
1767 }
1768
1769 case CLVC_EQUAL:
1770 if (option->cl_host_wide_int)
1771 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1772 else
1773 return *(int *) flag_var == option->var_value;
1774
1775 case CLVC_BIT_CLEAR:
1776 if (option->cl_host_wide_int)
1777 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1778 else
1779 return (*(int *) flag_var & option->var_value) == 0;
1780
1781 case CLVC_BIT_SET:
1782 if (option->cl_host_wide_int)
1783 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1784 else
1785 return (*(int *) flag_var & option->var_value) != 0;
1786
1787 case CLVC_SIZE:
1788 if (option->cl_host_wide_int)
1789 return *(HOST_WIDE_INT *) flag_var != -1;
1790 else
1791 return *(int *) flag_var != -1;
1792
1793 case CLVC_STRING:
1794 case CLVC_ENUM:
1795 case CLVC_DEFER:
1796 break;
1797 }
1798 return -1;
1799 }
1800
1801 /* Fill STATE with the current state of option OPTION in OPTS. Return
1802 true if there is some state to store. */
1803
1804 bool
1805 get_option_state (struct gcc_options *opts, int option,
1806 struct cl_option_state *state)
1807 {
1808 void *flag_var = option_flag_var (option, opts);
1809
1810 if (flag_var == 0)
1811 return false;
1812
1813 switch (cl_options[option].var_type)
1814 {
1815 case CLVC_INTEGER:
1816 case CLVC_EQUAL:
1817 case CLVC_SIZE:
1818 state->data = flag_var;
1819 state->size = (cl_options[option].cl_host_wide_int
1820 ? sizeof (HOST_WIDE_INT)
1821 : sizeof (int));
1822 break;
1823
1824 case CLVC_BIT_CLEAR:
1825 case CLVC_BIT_SET:
1826 state->ch = option_enabled (option, -1, opts);
1827 state->data = &state->ch;
1828 state->size = 1;
1829 break;
1830
1831 case CLVC_STRING:
1832 state->data = *(const char **) flag_var;
1833 if (state->data == 0)
1834 state->data = "";
1835 state->size = strlen ((const char *) state->data) + 1;
1836 break;
1837
1838 case CLVC_ENUM:
1839 state->data = flag_var;
1840 state->size = cl_enums[cl_options[option].var_enum].var_size;
1841 break;
1842
1843 case CLVC_DEFER:
1844 return false;
1845 }
1846 return true;
1847 }
1848
1849 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1850 handlers HANDLERS) to have diagnostic kind KIND for option
1851 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1852 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1853 argument of the option for joined options, or NULL otherwise. If IMPLY,
1854 the warning option in question is implied at this point. This is
1855 used by -Werror= and #pragma GCC diagnostic. */
1856
1857 void
1858 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1859 bool imply, location_t loc, unsigned int lang_mask,
1860 const struct cl_option_handlers *handlers,
1861 struct gcc_options *opts,
1862 struct gcc_options *opts_set,
1863 diagnostic_context *dc)
1864 {
1865 if (cl_options[opt_index].alias_target != N_OPTS)
1866 {
1867 gcc_assert (!cl_options[opt_index].cl_separate_alias
1868 && !cl_options[opt_index].cl_negative_alias);
1869 if (cl_options[opt_index].alias_arg)
1870 arg = cl_options[opt_index].alias_arg;
1871 opt_index = cl_options[opt_index].alias_target;
1872 }
1873 if (opt_index == OPT_SPECIAL_ignore || opt_index == OPT_SPECIAL_warn_removed)
1874 return;
1875 if (dc)
1876 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1877 if (imply)
1878 {
1879 const struct cl_option *option = &cl_options[opt_index];
1880
1881 /* -Werror=foo implies -Wfoo. */
1882 if (option->var_type == CLVC_INTEGER
1883 || option->var_type == CLVC_ENUM
1884 || option->var_type == CLVC_SIZE)
1885 {
1886 HOST_WIDE_INT value = 1;
1887
1888 if (arg && *arg == '\0' && !option->cl_missing_ok)
1889 arg = NULL;
1890
1891 if ((option->flags & CL_JOINED) && arg == NULL)
1892 {
1893 cmdline_handle_error (loc, option, option->opt_text, arg,
1894 CL_ERR_MISSING_ARG, lang_mask);
1895 return;
1896 }
1897
1898 /* If the switch takes an integer argument, convert it. */
1899 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
1900 {
1901 int error = 0;
1902 value = *arg ? integral_argument (arg, &error,
1903 option->cl_byte_size) : 0;
1904 if (error)
1905 {
1906 cmdline_handle_error (loc, option, option->opt_text, arg,
1907 CL_ERR_UINT_ARG, lang_mask);
1908 return;
1909 }
1910 }
1911
1912 /* If the switch takes an enumerated argument, convert it. */
1913 if (arg && option->var_type == CLVC_ENUM)
1914 {
1915 const struct cl_enum *e = &cl_enums[option->var_enum];
1916
1917 if (enum_arg_to_value (e->values, arg, 0, &value,
1918 lang_mask) >= 0)
1919 {
1920 const char *carg = NULL;
1921
1922 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1923 arg = carg;
1924 gcc_assert (carg != NULL);
1925 }
1926 else
1927 {
1928 cmdline_handle_error (loc, option, option->opt_text, arg,
1929 CL_ERR_ENUM_ARG, lang_mask);
1930 return;
1931 }
1932 }
1933
1934 handle_generated_option (opts, opts_set,
1935 opt_index, arg, value, lang_mask,
1936 kind, loc, handlers, false, dc);
1937 }
1938 }
1939 }
1940
1941 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
1942 Store number of arguments into ARGC_P. */
1943
1944 void
1945 parse_options_from_collect_gcc_options (const char *collect_gcc_options,
1946 obstack *argv_obstack,
1947 int *argc_p)
1948 {
1949 char *argv_storage = xstrdup (collect_gcc_options);
1950 int j, k;
1951
1952 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
1953 {
1954 if (argv_storage[j] == '\'')
1955 {
1956 obstack_ptr_grow (argv_obstack, &argv_storage[k]);
1957 ++j;
1958 do
1959 {
1960 if (argv_storage[j] == '\0')
1961 fatal_error (input_location,
1962 "malformed %<COLLECT_GCC_OPTIONS%>");
1963 else if (startswith (&argv_storage[j], "'\\''"))
1964 {
1965 argv_storage[k++] = '\'';
1966 j += 4;
1967 }
1968 else if (argv_storage[j] == '\'')
1969 break;
1970 else
1971 argv_storage[k++] = argv_storage[j++];
1972 }
1973 while (1);
1974 argv_storage[k++] = '\0';
1975 }
1976 }
1977
1978 obstack_ptr_grow (argv_obstack, NULL);
1979 *argc_p = obstack_object_size (argv_obstack) / sizeof (void *) - 1;
1980 }
1981
1982 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
1983 and push on O. */
1984
1985 void prepend_xassembler_to_collect_as_options (const char *collect_as_options,
1986 obstack *o)
1987 {
1988 obstack opts_obstack;
1989 int opts_count;
1990
1991 obstack_init (&opts_obstack);
1992 parse_options_from_collect_gcc_options (collect_as_options,
1993 &opts_obstack, &opts_count);
1994 const char **assembler_opts = XOBFINISH (&opts_obstack, const char **);
1995
1996 for (int i = 0; i < opts_count; i++)
1997 {
1998 obstack_grow (o, " '-Xassembler' ",
1999 strlen (" '-Xassembler' "));
2000 const char *opt = assembler_opts[i];
2001 obstack_1grow (o, '\'');
2002 obstack_grow (o, opt, strlen (opt));
2003 obstack_1grow (o, '\'');
2004 }
2005 }
This page took 0.126442 seconds and 5 git commands to generate.