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