1 /* Command line option handling.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
4 This file is part of GCC.
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
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
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/>. */
20 #define INCLUDE_STRING
24 #include "coretypes.h"
27 #include "diagnostic.h"
28 #include "spellcheck.h"
29 #include "opts-jobserver.h"
31 static void prune_options (struct cl_decoded_option
**, unsigned int *);
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. */
44 remapping_prefix_p (const struct cl_option
*opt
)
46 return opt
->flags
& CL_UNDOCUMENTED
47 && opt
->flags
& CL_JOINED
48 && !(opt
->flags
& (CL_DRIVER
| CL_TARGET
| CL_COMMON
| CL_LANG_ALL
));
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.
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".
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).
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. */
76 find_opt (const char *input
, unsigned int lang_mask
)
78 size_t mn
, mn_orig
, mx
, md
, opt_len
;
79 size_t match_wrong_lang
;
83 mx
= cl_options_count
;
85 /* Find mn such this lexicographical inequality holds:
86 cl_options[mn] <= input < cl_options[mn + 1]. */
90 opt_len
= cl_options
[md
].opt_len
;
91 comp
= strncmp (input
, cl_options
[md
].opt_text
+ 1, opt_len
);
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
;
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. */
110 const struct cl_option
*opt
= &cl_options
[mn
];
112 /* Is the input either an exact match or a prefix that takes a
114 if (!strncmp (input
, opt
->opt_text
+ 1, opt
->opt_len
)
115 && (input
[opt
->opt_len
] == '\0' || (opt
->flags
& CL_JOINED
)))
117 /* If language is OK, return it. */
118 if (opt
->flags
& lang_mask
)
121 if (remapping_prefix_p (opt
))
122 return OPT_SPECIAL_unknown
;
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
;
130 /* Try the next possibility. This is cl_options_count if there
132 mn
= opt
->back_chain
;
134 while (mn
!= cl_options_count
);
136 if (match_wrong_lang
== OPT_SPECIAL_unknown
&& input
[0] == '-')
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)
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
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. */
165 return OPT_SPECIAL_unknown
;
170 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
171 return match_wrong_lang
;
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
180 integral_argument (const char *arg
, int *err
, bool byte_size_suffix
)
195 unsigned HOST_WIDE_INT unit
= 1;
196 unsigned HOST_WIDE_INT value
= strtoull (arg
, &end
, 10);
198 /* If the value is too large to be represented use the maximum
199 representable value that strtoull sets VALUE to (setting
204 if (!byte_size_suffix
)
207 value
= strtoull (arg
, &end
, 0);
220 /* Numeric option arguments are at most INT_MAX. Make it
221 possible to specify a larger value by accepting common
223 if (!strcmp (end
, "kB"))
225 else if (!strcasecmp (end
, "KiB") || !strcmp (end
, "KB"))
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
246 else if (!strcasecmp (end
, "EiB"))
247 unit
= HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024
251 /* This could mean an unknown suffix or a bad prefix, like
260 unsigned HOST_WIDE_INT prod
= value
* unit
;
261 value
= prod
< value
? HOST_WIDE_INT_M1U
: prod
;
267 /* Return whether OPTION is OK for the language given by
270 option_ok_for_language (const struct cl_option
*option
,
271 unsigned int lang_mask
)
273 if (!(option
->flags
& lang_mask
))
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
284 /* Return whether ENUM_ARG is OK for the language given by
288 enum_arg_ok_for_language (const struct cl_enum_arg
*enum_arg
,
289 unsigned int lang_mask
)
291 return (lang_mask
& CL_DRIVER
) || !(enum_arg
->flags
& CL_ENUM_DRIVER_ONLY
);
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. */
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
)
305 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
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
))
312 *value
= enum_args
[i
].value
;
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. */
324 opt_enum_arg_to_value (size_t opt_index
, const char *arg
,
325 int *value
, unsigned int lang_mask
)
327 const struct cl_option
*option
= &cl_options
[opt_index
];
329 gcc_assert (option
->var_type
== CLVC_ENUM
);
331 HOST_WIDE_INT wideval
;
332 if (enum_arg_to_value (cl_enums
[option
->var_enum
].values
, arg
, 0,
333 &wideval
, lang_mask
) >= 0)
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
350 enum_value_to_arg (const struct cl_enum_arg
*enum_args
,
351 const char **argp
, int value
, unsigned int lang_mask
)
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
))
360 *argp
= enum_args
[i
].arg
;
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
))
368 *argp
= enum_args
[i
].arg
;
376 /* Fill in the canonical option part of *DECODED with an option
377 described by OPT_INDEX, ARG and VALUE. */
380 generate_canonical_option (size_t opt_index
, const char *arg
,
382 struct cl_decoded_option
*decoded
)
384 const struct cl_option
*option
= &cl_options
[opt_index
];
385 const char *opt_text
= option
->opt_text
;
388 && !option
->cl_reject_negative
389 && (opt_text
[1] == 'W' || opt_text
[1] == 'f'
390 || opt_text
[1] == 'g' || opt_text
[1] == 'm'))
392 char *t
= XOBNEWVEC (&opts_obstack
, char, option
->opt_len
+ 5);
398 memcpy (t
+ 5, opt_text
+ 2, option
->opt_len
);
402 decoded
->canonical_option
[2] = NULL
;
403 decoded
->canonical_option
[3] = NULL
;
407 if ((option
->flags
& CL_SEPARATE
)
408 && !option
->cl_separate_alias
)
410 decoded
->canonical_option
[0] = opt_text
;
411 decoded
->canonical_option
[1] = arg
;
412 decoded
->canonical_option_num_elements
= 2;
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;
424 decoded
->canonical_option
[0] = opt_text
;
425 decoded
->canonical_option
[1] = NULL
;
426 decoded
->canonical_option_num_elements
= 1;
430 /* Structure describing mappings from options on the command line to
431 options to look up with find_opt. */
434 /* Prefix of the option on the command line. */
436 /* If two argv elements are considered to be merged into one option,
437 prefix for the second element, otherwise NULL. */
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. */
449 static const struct option_map option_map
[] =
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 }
471 /* Helper function for gcc.cc's driver::suggest_option, for populating the
472 vec of suggestions for misspelled options.
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.
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
483 For example, given "-Wabi-tag", the following are added to CANDIDATES:
489 The added strings must be freed using free. */
492 add_misspelling_candidates (auto_vec
<char *> *candidates
,
493 const struct cl_option
*option
,
494 const char *opt_text
)
496 gcc_assert (candidates
);
498 gcc_assert (opt_text
);
499 if (remapping_prefix_p (option
))
501 candidates
->safe_push (xstrdup (opt_text
+ 1));
502 for (unsigned i
= 0; i
< ARRAY_SIZE (option_map
); i
++)
504 const char *opt0
= option_map
[i
].opt0
;
505 const char *new_prefix
= option_map
[i
].new_prefix
;
506 size_t new_prefix_len
= strlen (new_prefix
);
508 if (option
->cl_reject_negative
&& option_map
[i
].negated
)
511 if (strncmp (opt_text
, new_prefix
, new_prefix_len
) == 0)
513 char *alternative
= concat (opt0
+ 1, opt_text
+ new_prefix_len
,
515 candidates
->safe_push (alternative
);
519 /* For all params (e.g. --param=key=value),
520 include also '--param key=value'. */
521 const char *prefix
= "--param=";
522 if (strstr (opt_text
, prefix
) == opt_text
)
524 char *param
= xstrdup (opt_text
+ 1);
525 gcc_assert (param
[6] == '=');
527 candidates
->safe_push (param
);
531 /* Decode the switch beginning at ARGV for the language indicated by
532 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
533 the structure *DECODED. Returns the number of switches
537 decode_cmdline_option (const char *const *argv
, unsigned int lang_mask
,
538 struct cl_decoded_option
*decoded
)
542 HOST_WIDE_INT value
= 1, mask
= 0;
543 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
547 const struct cl_option
*option
;
549 const char *warn_message
= NULL
;
550 bool separate_arg_flag
;
551 bool joined_arg_flag
;
552 bool have_separate_arg
= false;
556 const char *opt_value
= argv
[0] + 1;
557 opt_index
= find_opt (opt_value
, lang_mask
);
559 while (opt_index
== OPT_SPECIAL_unknown
560 && i
< ARRAY_SIZE (option_map
))
562 const char *opt0
= option_map
[i
].opt0
;
563 const char *opt1
= option_map
[i
].opt1
;
564 const char *new_prefix
= option_map
[i
].new_prefix
;
565 bool another_char_needed
= option_map
[i
].another_char_needed
;
566 size_t opt0_len
= strlen (opt0
);
567 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
568 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
569 size_t new_prefix_len
= strlen (new_prefix
);
571 extra_args
= (opt1
== NULL
? 0 : 1);
572 value
= !option_map
[i
].negated
;
574 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
576 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
577 && (!another_char_needed
578 || argv
[extra_args
][optn_len
] != 0))
580 size_t arglen
= strlen (argv
[extra_args
]);
583 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
584 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
585 memcpy (dup
, new_prefix
, new_prefix_len
);
586 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
587 arglen
- optn_len
+ 1);
588 opt_index
= find_opt (dup
+ 1, lang_mask
);
594 if (opt_index
== OPT_SPECIAL_unknown
)
602 option
= &cl_options
[opt_index
];
604 /* Reject negative form of switches that don't take negatives as
606 if (!value
&& option
->cl_reject_negative
)
608 opt_index
= OPT_SPECIAL_unknown
;
609 errors
|= CL_ERR_NEGATIVE
;
614 /* Clear the initial value for size options (it will be overwritten
615 later based on the Init(value) specification in the opt file. */
616 if (option
->var_type
== CLVC_SIZE
)
619 result
= extra_args
+ 1;
620 warn_message
= option
->warn_message
;
622 /* Check to see if the option is disabled for this configuration. */
623 if (option
->cl_disabled
)
624 errors
|= CL_ERR_DISABLED
;
626 /* Determine whether there may be a separate argument based on
627 whether this option is being processed for the driver, and, if
628 so, how many such arguments. */
629 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
630 && !(option
->cl_no_driver_arg
631 && (lang_mask
& CL_DRIVER
)));
632 separate_args
= (separate_arg_flag
633 ? option
->cl_separate_nargs
+ 1
635 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
637 /* Sort out any argument the switch takes. */
640 /* Have arg point to the original switch. This is because
641 some code, such as disable_builtin_function, expects its
642 argument to be persistent until the program exits. */
643 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
645 if (*arg
== '\0' && !option
->cl_missing_ok
)
647 if (separate_arg_flag
)
649 arg
= argv
[extra_args
+ 1];
650 result
= extra_args
+ 2;
652 result
= extra_args
+ 1;
654 have_separate_arg
= true;
657 /* Missing argument. */
661 else if (separate_arg_flag
)
663 arg
= argv
[extra_args
+ 1];
664 for (i
= 0; i
< separate_args
; i
++)
665 if (argv
[extra_args
+ 1 + i
] == NULL
)
667 errors
|= CL_ERR_MISSING_ARG
;
670 result
= extra_args
+ 1 + i
;
672 have_separate_arg
= true;
675 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
676 errors
|= CL_ERR_MISSING_ARG
;
678 /* Is this option an alias (or an ignored option, marked as an alias
679 of OPT_SPECIAL_ignore)? */
680 if (option
->alias_target
!= N_OPTS
681 && (!option
->cl_separate_alias
|| have_separate_arg
))
683 size_t new_opt_index
= option
->alias_target
;
685 if (new_opt_index
== OPT_SPECIAL_ignore
686 || new_opt_index
== OPT_SPECIAL_warn_removed
)
688 gcc_assert (option
->alias_arg
== NULL
);
689 gcc_assert (option
->neg_alias_arg
== NULL
);
690 opt_index
= new_opt_index
;
695 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
697 /* The new option must not be an alias itself. */
698 gcc_assert (new_option
->alias_target
== N_OPTS
699 || new_option
->cl_separate_alias
);
701 if (option
->neg_alias_arg
)
703 gcc_assert (option
->alias_arg
!= NULL
);
704 gcc_assert (arg
== NULL
);
705 gcc_assert (!option
->cl_negative_alias
);
707 arg
= option
->alias_arg
;
709 arg
= option
->neg_alias_arg
;
712 else if (option
->alias_arg
)
714 gcc_assert (value
== 1);
715 gcc_assert (arg
== NULL
);
716 gcc_assert (!option
->cl_negative_alias
);
717 arg
= option
->alias_arg
;
720 if (option
->cl_negative_alias
)
723 opt_index
= new_opt_index
;
727 gcc_assert (!option
->cl_reject_negative
);
729 /* Recompute what arguments are allowed. */
730 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
731 && !(option
->cl_no_driver_arg
732 && (lang_mask
& CL_DRIVER
)));
733 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
735 if (separate_args
> 1 || option
->cl_separate_nargs
)
736 gcc_assert (separate_args
737 == (unsigned int) option
->cl_separate_nargs
+ 1);
739 if (!(errors
& CL_ERR_MISSING_ARG
))
741 if (separate_arg_flag
|| joined_arg_flag
)
743 if (option
->cl_missing_ok
&& arg
== NULL
)
745 gcc_assert (arg
!= NULL
);
748 gcc_assert (arg
== NULL
);
751 /* Recheck for warnings and disabled options. */
752 if (option
->warn_message
)
754 gcc_assert (warn_message
== NULL
);
755 warn_message
= option
->warn_message
;
757 if (option
->cl_disabled
)
758 errors
|= CL_ERR_DISABLED
;
762 /* Check if this is a switch for a different front end. */
763 if (!option_ok_for_language (option
, lang_mask
))
764 errors
|= CL_ERR_WRONG_LANG
;
765 else if (strcmp (option
->opt_text
, "-Werror=") == 0
766 && strchr (opt_value
, ',') == NULL
)
768 /* Verify that -Werror argument is a valid warning
770 char *werror_arg
= xstrdup (opt_value
+ 6);
773 size_t warning_index
= find_opt (werror_arg
, lang_mask
);
775 if (warning_index
!= OPT_SPECIAL_unknown
)
777 const struct cl_option
*warning_option
778 = &cl_options
[warning_index
];
779 if (!option_ok_for_language (warning_option
, lang_mask
))
780 errors
|= CL_ERR_WRONG_LANG
;
784 /* Convert the argument to lowercase if appropriate. */
785 if (arg
&& option
->cl_tolower
)
788 size_t len
= strlen (arg
);
789 char *arg_lower
= XOBNEWVEC (&opts_obstack
, char, len
+ 1);
791 for (j
= 0; j
< len
; j
++)
792 arg_lower
[j
] = TOLOWER ((unsigned char) arg
[j
]);
797 /* If the switch takes an integer argument, convert it. */
798 if (arg
&& (option
->cl_uinteger
|| option
->cl_host_wide_int
))
801 value
= *arg
? integral_argument (arg
, &error
, option
->cl_byte_size
) : 0;
803 errors
|= CL_ERR_UINT_ARG
;
805 /* Reject value out of a range. */
806 if (option
->range_max
!= -1
807 && (value
< option
->range_min
|| value
> option
->range_max
))
808 errors
|= CL_ERR_INT_RANGE_ARG
;
811 /* If the switch takes an enumerated argument, convert it. */
812 if (arg
&& (option
->var_type
== CLVC_ENUM
))
814 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
816 gcc_assert (option
->var_value
!= CLEV_NORMAL
|| value
== 1);
817 if (option
->var_value
!= CLEV_NORMAL
)
820 HOST_WIDE_INT sum_value
= 0;
821 unsigned HOST_WIDE_INT used_sets
= 0;
824 const char *q
= strchr (p
, ',');
825 HOST_WIDE_INT this_value
= 0;
828 errors
|= CL_ERR_ENUM_SET_ARG
;
831 int idx
= enum_arg_to_value (e
->values
, p
, q
? q
- p
: 0,
832 &this_value
, lang_mask
);
835 errors
|= CL_ERR_ENUM_SET_ARG
;
839 HOST_WIDE_INT this_mask
= 0;
840 if (option
->var_value
== CLEV_SET
)
842 unsigned set
= e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
;
843 gcc_checking_assert (set
>= 1
844 && set
<= HOST_BITS_PER_WIDE_INT
);
845 if ((used_sets
& (HOST_WIDE_INT_1U
<< (set
- 1))) != 0)
847 errors
|= CL_ERR_ENUM_SET_ARG
;
850 used_sets
|= HOST_WIDE_INT_1U
<< (set
- 1);
852 for (int i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
853 if (set
== (e
->values
[i
].flags
>> CL_ENUM_SET_SHIFT
))
854 this_mask
|= e
->values
[i
].value
;
858 gcc_assert (option
->var_value
== CLEV_BITSET
859 && ((e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
)
861 this_mask
= this_value
;
864 sum_value
|= this_value
;
874 gcc_checking_assert (value
== 0);
876 else if (enum_arg_to_value (e
->values
, arg
, 0, &value
, lang_mask
) >= 0)
878 const char *carg
= NULL
;
880 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
882 gcc_assert (carg
!= NULL
);
885 errors
|= CL_ERR_ENUM_ARG
;
889 decoded
->opt_index
= opt_index
;
891 decoded
->value
= value
;
892 decoded
->mask
= mask
;
893 decoded
->errors
= errors
;
894 decoded
->warn_message
= warn_message
;
896 if (opt_index
== OPT_SPECIAL_unknown
)
897 gcc_assert (result
== 1);
899 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
900 decoded
->canonical_option_num_elements
= result
;
902 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
907 if (opt_index
== OPT_SPECIAL_unknown
)
908 decoded
->canonical_option
[i
] = argv
[i
];
910 decoded
->canonical_option
[i
] = NULL
;
911 len
= strlen (argv
[i
]);
912 /* If the argument is an empty string, we will print it as "" in
913 orig_option_with_args_text. */
914 total_len
+= (len
!= 0 ? len
: 2) + 1;
917 decoded
->canonical_option
[i
] = NULL
;
919 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
920 && opt_index
!= OPT_SPECIAL_warn_removed
)
922 generate_canonical_option (opt_index
, arg
, value
, decoded
);
923 if (separate_args
> 1)
925 for (i
= 0; i
< separate_args
; i
++)
927 if (argv
[extra_args
+ 1 + i
] == NULL
)
930 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
932 gcc_assert (result
== 1 + i
);
933 decoded
->canonical_option_num_elements
= result
;
936 decoded
->orig_option_with_args_text
937 = p
= XOBNEWVEC (&opts_obstack
, char, total_len
);
938 for (i
= 0; i
< result
; i
++)
940 size_t len
= strlen (argv
[i
]);
942 /* Print the empty string verbally. */
949 memcpy (p
, argv
[i
], len
);
960 /* Obstack for option strings. */
962 struct obstack opts_obstack
;
964 /* Like libiberty concat, but allocate using opts_obstack. */
967 opts_concat (const char *first
, ...)
974 /* First compute the size of the result and get sufficient memory. */
975 va_start (ap
, first
);
976 for (arg
= first
; arg
; arg
= va_arg (ap
, const char *))
977 length
+= strlen (arg
);
978 newstr
= XOBNEWVEC (&opts_obstack
, char, length
+ 1);
981 /* Now copy the individual pieces to the result string. */
982 va_start (ap
, first
);
983 for (arg
= first
, end
= newstr
; arg
; arg
= va_arg (ap
, const char *))
985 length
= strlen (arg
);
986 memcpy (end
, arg
, length
);
994 /* Decode command-line options (ARGC and ARGV being the arguments of
995 main) into an array, setting *DECODED_OPTIONS to a pointer to that
996 array and *DECODED_OPTIONS_COUNT to the number of entries in the
997 array. The first entry in the array is always one for the program
998 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
999 flags applicable for decoding (including CL_COMMON and CL_TARGET if
1000 those options should be considered applicable). Do not produce any
1001 diagnostics or set state outside of these variables. */
1004 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
1005 unsigned int lang_mask
,
1006 struct cl_decoded_option
**decoded_options
,
1007 unsigned int *decoded_options_count
)
1010 struct cl_decoded_option
*opt_array
;
1011 unsigned int num_decoded_options
;
1013 int opt_array_len
= argc
;
1014 opt_array
= XNEWVEC (struct cl_decoded_option
, opt_array_len
);
1016 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
1017 opt_array
[0].warn_message
= NULL
;
1018 opt_array
[0].arg
= argv
[0];
1019 opt_array
[0].orig_option_with_args_text
= argv
[0];
1020 opt_array
[0].canonical_option_num_elements
= 1;
1021 opt_array
[0].canonical_option
[0] = argv
[0];
1022 opt_array
[0].canonical_option
[1] = NULL
;
1023 opt_array
[0].canonical_option
[2] = NULL
;
1024 opt_array
[0].canonical_option
[3] = NULL
;
1025 opt_array
[0].value
= 1;
1026 opt_array
[0].mask
= 0;
1027 opt_array
[0].errors
= 0;
1028 num_decoded_options
= 1;
1030 for (i
= 1; i
< argc
; i
+= n
)
1032 const char *opt
= argv
[i
];
1034 /* Interpret "-" or a non-switch as a file name. */
1035 if (opt
[0] != '-' || opt
[1] == '\0')
1037 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
1038 num_decoded_options
++;
1043 /* Interpret "--param" "key=name" as "--param=key=name". */
1044 const char *needle
= "--param";
1045 if (i
+ 1 < argc
&& strcmp (opt
, needle
) == 0)
1047 const char *replacement
1048 = opts_concat (needle
, "=", argv
[i
+ 1], NULL
);
1049 argv
[++i
] = replacement
;
1052 /* Expand -fdiagnostics-plain-output to its constituents. This needs
1053 to happen here so that prune_options can handle -fdiagnostics-color
1055 if (!strcmp (opt
, "-fdiagnostics-plain-output"))
1057 /* If you have changed the default diagnostics output, and this new
1058 output is not appropriately "plain" (e.g., the change needs to be
1059 undone in order for the testsuite to work properly), then please do
1061 1. Add the necessary option to undo the new behavior to
1063 2. Update the documentation for -fdiagnostics-plain-output
1065 const char *const expanded_args
[] = {
1066 "-fno-diagnostics-show-caret",
1067 "-fno-diagnostics-show-line-numbers",
1068 "-fdiagnostics-color=never",
1069 "-fdiagnostics-urls=never",
1070 "-fdiagnostics-path-format=separate-events",
1072 const int num_expanded
= ARRAY_SIZE (expanded_args
);
1073 opt_array_len
+= num_expanded
- 1;
1074 opt_array
= XRESIZEVEC (struct cl_decoded_option
,
1075 opt_array
, opt_array_len
);
1076 for (int j
= 0, nj
; j
< num_expanded
; j
+= nj
)
1078 nj
= decode_cmdline_option (expanded_args
+ j
, lang_mask
,
1079 &opt_array
[num_decoded_options
]);
1080 num_decoded_options
++;
1087 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
1088 &opt_array
[num_decoded_options
]);
1089 num_decoded_options
++;
1092 *decoded_options
= opt_array
;
1093 *decoded_options_count
= num_decoded_options
;
1094 prune_options (decoded_options
, decoded_options_count
);
1097 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
1098 next one is the same as ORIG_NEXT_OPT_IDX. */
1101 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
1103 /* An option can be canceled by the same option or an option with
1105 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
1108 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
1109 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
1115 /* Filter out options canceled by the ones after them, and related
1119 prune_options (struct cl_decoded_option
**decoded_options
,
1120 unsigned int *decoded_options_count
)
1122 unsigned int old_decoded_options_count
= *decoded_options_count
;
1123 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
1124 unsigned int new_decoded_options_count
;
1125 struct cl_decoded_option
*new_decoded_options
1126 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
1128 const struct cl_option
*option
;
1129 unsigned int options_to_prepend
= 0;
1130 unsigned int Wcomplain_wrong_lang_idx
= 0;
1131 unsigned int fdiagnostics_color_idx
= 0;
1133 /* Remove arguments which are negated by others after them. */
1134 new_decoded_options_count
= 0;
1135 for (i
= 0; i
< old_decoded_options_count
; i
++)
1137 unsigned int j
, opt_idx
, next_opt_idx
;
1139 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
1142 opt_idx
= old_decoded_options
[i
].opt_index
;
1145 case OPT_SPECIAL_unknown
:
1146 case OPT_SPECIAL_ignore
:
1147 case OPT_SPECIAL_warn_removed
:
1148 case OPT_SPECIAL_program_name
:
1149 case OPT_SPECIAL_input_file
:
1152 /* Do not handle the following yet, just remember the last one. */
1153 case OPT_Wcomplain_wrong_lang
:
1154 gcc_checking_assert (i
!= 0);
1155 if (Wcomplain_wrong_lang_idx
== 0)
1156 ++options_to_prepend
;
1157 Wcomplain_wrong_lang_idx
= i
;
1159 case OPT_fdiagnostics_color_
:
1160 gcc_checking_assert (i
!= 0);
1161 if (fdiagnostics_color_idx
== 0)
1162 ++options_to_prepend
;
1163 fdiagnostics_color_idx
= i
;
1167 gcc_assert (opt_idx
< cl_options_count
);
1168 option
= &cl_options
[opt_idx
];
1169 if (option
->neg_index
< 0)
1172 /* Skip joined switches. */
1173 if ((option
->flags
& CL_JOINED
)
1174 && (!option
->cl_reject_negative
1175 || (unsigned int) option
->neg_index
!= opt_idx
))
1178 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
1180 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
1182 next_opt_idx
= old_decoded_options
[j
].opt_index
;
1183 if (next_opt_idx
>= cl_options_count
)
1185 if (cl_options
[next_opt_idx
].neg_index
< 0)
1187 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
)
1188 && (!cl_options
[next_opt_idx
].cl_reject_negative
1189 || ((unsigned int) cl_options
[next_opt_idx
].neg_index
1192 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
1195 if (j
== old_decoded_options_count
)
1198 new_decoded_options
[new_decoded_options_count
]
1199 = old_decoded_options
[i
];
1200 new_decoded_options_count
++;
1206 /* For those not yet handled, put (only) the last at a front position after
1207 'argv[0]', so they can take effect immediately. */
1208 if (options_to_prepend
)
1210 const unsigned int argv_0
= 1;
1211 memmove (new_decoded_options
+ argv_0
+ options_to_prepend
,
1212 new_decoded_options
+ argv_0
,
1213 sizeof (struct cl_decoded_option
)
1214 * (new_decoded_options_count
- argv_0
));
1215 unsigned int options_prepended
= 0;
1216 if (Wcomplain_wrong_lang_idx
!= 0)
1218 new_decoded_options
[argv_0
+ options_prepended
++]
1219 = old_decoded_options
[Wcomplain_wrong_lang_idx
];
1220 new_decoded_options_count
++;
1222 if (fdiagnostics_color_idx
!= 0)
1224 new_decoded_options
[argv_0
+ options_prepended
++]
1225 = old_decoded_options
[fdiagnostics_color_idx
];
1226 new_decoded_options_count
++;
1228 gcc_checking_assert (options_to_prepend
== options_prepended
);
1231 free (old_decoded_options
);
1232 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
1233 new_decoded_options
,
1234 new_decoded_options_count
);
1235 *decoded_options
= new_decoded_options
;
1236 *decoded_options_count
= new_decoded_options_count
;
1239 /* Handle option DECODED for the language indicated by LANG_MASK,
1240 using the handlers in HANDLERS and setting fields in OPTS and
1241 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1242 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1243 option for options from the source file, UNKNOWN_LOCATION
1244 otherwise. GENERATED_P is true for an option generated as part of
1245 processing another option or otherwise generated internally, false
1246 for one explicitly passed by the user. control_warning_option
1247 generated options are considered explicitly passed by the user.
1248 Returns false if the switch was invalid. DC is the diagnostic
1249 context for options affecting diagnostics state, or NULL. */
1252 handle_option (struct gcc_options
*opts
,
1253 struct gcc_options
*opts_set
,
1254 const struct cl_decoded_option
*decoded
,
1255 unsigned int lang_mask
, int kind
, location_t loc
,
1256 const struct cl_option_handlers
*handlers
,
1257 bool generated_p
, diagnostic_context
*dc
)
1259 size_t opt_index
= decoded
->opt_index
;
1260 const char *arg
= decoded
->arg
;
1261 HOST_WIDE_INT value
= decoded
->value
;
1262 HOST_WIDE_INT mask
= decoded
->mask
;
1263 const struct cl_option
*option
= &cl_options
[opt_index
];
1264 void *flag_var
= option_flag_var (opt_index
, opts
);
1268 set_option (opts
, (generated_p
? NULL
: opts_set
),
1269 opt_index
, value
, arg
, kind
, loc
, dc
, mask
);
1271 for (i
= 0; i
< handlers
->num_handlers
; i
++)
1272 if (option
->flags
& handlers
->handlers
[i
].mask
)
1274 if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
1275 lang_mask
, kind
, loc
,
1277 handlers
->target_option_override_hook
))
1284 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1285 option instead of DECODED. This is used for callbacks when one
1286 option implies another instead of an option being decoded from the
1290 handle_generated_option (struct gcc_options
*opts
,
1291 struct gcc_options
*opts_set
,
1292 size_t opt_index
, const char *arg
, HOST_WIDE_INT value
,
1293 unsigned int lang_mask
, int kind
, location_t loc
,
1294 const struct cl_option_handlers
*handlers
,
1295 bool generated_p
, diagnostic_context
*dc
)
1297 struct cl_decoded_option decoded
;
1299 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
1300 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
1301 handlers
, generated_p
, dc
);
1304 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1305 VALUE for a front end using LANG_MASK. This is used when the
1306 compiler generates options internally. */
1309 generate_option (size_t opt_index
, const char *arg
, HOST_WIDE_INT value
,
1310 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
1312 const struct cl_option
*option
= &cl_options
[opt_index
];
1314 decoded
->opt_index
= opt_index
;
1315 decoded
->warn_message
= NULL
;
1317 decoded
->value
= value
;
1319 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
1321 : CL_ERR_WRONG_LANG
);
1323 generate_canonical_option (opt_index
, arg
, value
, decoded
);
1324 switch (decoded
->canonical_option_num_elements
)
1327 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
1331 decoded
->orig_option_with_args_text
1332 = opts_concat (decoded
->canonical_option
[0], " ",
1333 decoded
->canonical_option
[1], NULL
);
1341 /* Fill in *DECODED with an option for input file FILE. */
1344 generate_option_input_file (const char *file
,
1345 struct cl_decoded_option
*decoded
)
1347 decoded
->opt_index
= OPT_SPECIAL_input_file
;
1348 decoded
->warn_message
= NULL
;
1349 decoded
->arg
= file
;
1350 decoded
->orig_option_with_args_text
= file
;
1351 decoded
->canonical_option_num_elements
= 1;
1352 decoded
->canonical_option
[0] = file
;
1353 decoded
->canonical_option
[1] = NULL
;
1354 decoded
->canonical_option
[2] = NULL
;
1355 decoded
->canonical_option
[3] = NULL
;
1358 decoded
->errors
= 0;
1361 /* Helper function for listing valid choices and hint for misspelled
1362 value. CANDIDATES is a vector containing all valid strings,
1363 STR is set to a heap allocated string that contains all those
1364 strings concatenated, separated by spaces, and the return value
1365 is the closest string from those to ARG, or NULL if nothing is
1366 close enough. Callers should XDELETEVEC (STR) after using it
1367 to avoid memory leaks. */
1370 candidates_list_and_hint (const char *arg
, char *&str
,
1371 const auto_vec
<const char *> &candidates
)
1375 const char *candidate
;
1378 gcc_assert (!candidates
.is_empty ());
1380 FOR_EACH_VEC_ELT (candidates
, i
, candidate
)
1381 len
+= strlen (candidate
) + 1;
1383 str
= p
= XNEWVEC (char, len
);
1384 FOR_EACH_VEC_ELT (candidates
, i
, candidate
)
1386 len
= strlen (candidate
);
1387 memcpy (p
, candidate
, len
);
1392 return find_closest_string (arg
, &candidates
);
1395 /* Perform diagnostics for read_cmdline_option and control_warning_option
1396 functions. Returns true if an error has been diagnosed.
1397 LOC and LANG_MASK arguments like in read_cmdline_option.
1398 OPTION is the option to report diagnostics for, OPT the name
1399 of the option as text, ARG the argument of the option (for joined
1400 options), ERRORS is bitmask of CL_ERR_* values. */
1403 cmdline_handle_error (location_t loc
, const struct cl_option
*option
,
1404 const char *opt
, const char *arg
, int errors
,
1405 unsigned int lang_mask
)
1407 if (errors
& CL_ERR_DISABLED
)
1409 error_at (loc
, "command-line option %qs"
1410 " is not supported by this configuration", opt
);
1414 if (errors
& CL_ERR_MISSING_ARG
)
1416 if (option
->missing_argument_error
)
1417 error_at (loc
, option
->missing_argument_error
, opt
);
1419 error_at (loc
, "missing argument to %qs", opt
);
1423 if (errors
& CL_ERR_UINT_ARG
)
1425 if (option
->cl_byte_size
)
1426 error_at (loc
, "argument to %qs should be a non-negative integer "
1427 "optionally followed by a size unit",
1430 error_at (loc
, "argument to %qs should be a non-negative integer",
1435 if (errors
& CL_ERR_INT_RANGE_ARG
)
1437 error_at (loc
, "argument to %qs is not between %d and %d",
1438 option
->opt_text
, option
->range_min
, option
->range_max
);
1442 if (errors
& CL_ERR_ENUM_SET_ARG
)
1444 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1445 const char *p
= arg
;
1446 unsigned HOST_WIDE_INT used_sets
= 0;
1447 const char *second_opt
= NULL
;
1448 size_t second_opt_len
= 0;
1452 const char *q
= strchr (p
, ',');
1453 HOST_WIDE_INT this_value
= 0;
1457 errors
= CL_ERR_ENUM_ARG
;
1460 int idx
= enum_arg_to_value (e
->values
, p
, q
? q
- p
: 0,
1461 &this_value
, lang_mask
);
1465 q
= strchr (p
, '\0');
1466 char *narg
= XALLOCAVEC (char, (q
- p
) + 1);
1467 memcpy (narg
, p
, q
- p
);
1470 errors
= CL_ERR_ENUM_ARG
;
1474 if (option
->var_value
== CLEV_BITSET
)
1482 unsigned set
= e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
;
1483 gcc_checking_assert (set
>= 1 && set
<= HOST_BITS_PER_WIDE_INT
);
1484 if ((used_sets
& (HOST_WIDE_INT_1U
<< (set
- 1))) != 0)
1487 q
= strchr (p
, '\0');
1488 if (second_opt
== NULL
)
1490 used_sets
= HOST_WIDE_INT_1U
<< (set
- 1);
1492 second_opt_len
= q
- p
;
1496 char *args
= XALLOCAVEC (char, (q
- p
) + 1 + second_opt_len
+ 1);
1497 memcpy (args
, p
, q
- p
);
1499 memcpy (args
+ (q
- p
) + 1, second_opt
, second_opt_len
);
1500 args
[(q
- p
) + 1 + second_opt_len
] = '\0';
1501 error_at (loc
, "invalid argument in option %qs", opt
);
1502 if (strcmp (args
, args
+ (q
- p
) + 1) == 0)
1503 inform (loc
, "%qs specified multiple times in the same option",
1506 inform (loc
, "%qs is mutually exclusive with %qs and cannot be"
1507 " specified together", args
, args
+ (q
- p
) + 1);
1510 used_sets
|= HOST_WIDE_INT_1U
<< (set
- 1);
1518 if (errors
& CL_ERR_ENUM_ARG
)
1520 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1524 auto_diagnostic_group d
;
1525 if (e
->unknown_error
)
1526 error_at (loc
, e
->unknown_error
, arg
);
1528 error_at (loc
, "unrecognized argument in option %qs", opt
);
1530 auto_vec
<const char *> candidates
;
1531 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1533 if (!enum_arg_ok_for_language (&e
->values
[i
], lang_mask
))
1535 candidates
.safe_push (e
->values
[i
].arg
);
1537 const char *hint
= candidates_list_and_hint (arg
, s
, candidates
);
1539 inform (loc
, "valid arguments to %qs are: %s; did you mean %qs?",
1540 option
->opt_text
, s
, hint
);
1542 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1551 /* Handle the switch DECODED (location LOC) for the language indicated
1552 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1553 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1554 diagnostic options. */
1557 read_cmdline_option (struct gcc_options
*opts
,
1558 struct gcc_options
*opts_set
,
1559 struct cl_decoded_option
*decoded
,
1561 unsigned int lang_mask
,
1562 const struct cl_option_handlers
*handlers
,
1563 diagnostic_context
*dc
)
1565 const struct cl_option
*option
;
1566 const char *opt
= decoded
->orig_option_with_args_text
;
1568 if (decoded
->warn_message
)
1569 warning_at (loc
, 0, decoded
->warn_message
, opt
);
1571 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
1573 if (handlers
->unknown_option_callback (decoded
))
1574 error_at (loc
, "unrecognized command-line option %qs", decoded
->arg
);
1578 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
1581 if (decoded
->opt_index
== OPT_SPECIAL_warn_removed
)
1583 /* Warn only about positive ignored options. */
1585 warning_at (loc
, 0, "switch %qs is no longer supported", opt
);
1589 option
= &cl_options
[decoded
->opt_index
];
1592 && cmdline_handle_error (loc
, option
, opt
, decoded
->arg
,
1593 decoded
->errors
, lang_mask
))
1596 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
1598 handlers
->wrong_lang_callback (decoded
, lang_mask
);
1602 gcc_assert (!decoded
->errors
);
1604 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1605 loc
, handlers
, false, dc
))
1606 error_at (loc
, "unrecognized command-line option %qs", opt
);
1609 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1610 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1611 location LOC, using diagnostic context DC if not NULL for
1612 diagnostic classification. */
1615 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1616 int opt_index
, HOST_WIDE_INT value
, const char *arg
, int kind
,
1617 location_t loc
, diagnostic_context
*dc
,
1618 HOST_WIDE_INT mask
/* = 0 */)
1620 const struct cl_option
*option
= &cl_options
[opt_index
];
1621 void *flag_var
= option_flag_var (opt_index
, opts
);
1622 void *set_flag_var
= NULL
;
1627 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
&& dc
!= NULL
)
1628 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1630 if (opts_set
!= NULL
)
1631 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1633 switch (option
->var_type
)
1636 if (option
->cl_host_wide_int
)
1638 *(HOST_WIDE_INT
*) flag_var
= value
;
1640 *(HOST_WIDE_INT
*) set_flag_var
= 1;
1644 if (value
> INT_MAX
)
1645 error_at (loc
, "argument to %qs is bigger than %d",
1646 option
->opt_text
, INT_MAX
);
1649 *(int *) flag_var
= value
;
1651 *(int *) set_flag_var
= 1;
1658 if (option
->cl_host_wide_int
)
1660 *(HOST_WIDE_INT
*) flag_var
= value
;
1662 *(HOST_WIDE_INT
*) set_flag_var
= value
;
1666 *(int *) flag_var
= value
;
1668 *(int *) set_flag_var
= value
;
1674 if (option
->cl_host_wide_int
)
1676 *(HOST_WIDE_INT
*) flag_var
= (value
1678 : !option
->var_value
);
1680 *(HOST_WIDE_INT
*) set_flag_var
= 1;
1684 *(int *) flag_var
= (value
1686 : !option
->var_value
);
1688 *(int *) set_flag_var
= 1;
1692 case CLVC_BIT_CLEAR
:
1694 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1696 if (option
->cl_host_wide_int
)
1697 *(HOST_WIDE_INT
*) flag_var
|= option
->var_value
;
1699 *(int *) flag_var
|= option
->var_value
;
1703 if (option
->cl_host_wide_int
)
1704 *(HOST_WIDE_INT
*) flag_var
&= ~option
->var_value
;
1706 *(int *) flag_var
&= ~option
->var_value
;
1710 if (option
->cl_host_wide_int
)
1711 *(HOST_WIDE_INT
*) set_flag_var
|= option
->var_value
;
1713 *(int *) set_flag_var
|= option
->var_value
;
1718 *(const char **) flag_var
= arg
;
1720 *(const char **) set_flag_var
= "";
1725 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1728 e
->set (flag_var
, value
| (e
->get (flag_var
) & ~mask
));
1730 e
->set (flag_var
, value
);
1732 e
->set (set_flag_var
, 1);
1738 vec
<cl_deferred_option
> *v
1739 = (vec
<cl_deferred_option
> *) *(void **) flag_var
;
1740 cl_deferred_option p
= {opt_index
, arg
, value
};
1742 v
= XCNEW (vec
<cl_deferred_option
>);
1744 *(void **) flag_var
= v
;
1746 *(void **) set_flag_var
= v
;
1752 /* Return the address of the flag variable for option OPT_INDEX in
1753 options structure OPTS, or NULL if there is no flag variable. */
1756 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1758 const struct cl_option
*option
= &cl_options
[opt_index
];
1760 if (option
->flag_var_offset
== (unsigned short) -1)
1762 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1765 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1766 or -1 if it isn't a simple on-off switch
1767 (or if the value is unknown, typically set later in target). */
1770 option_enabled (int opt_idx
, unsigned lang_mask
, void *opts
)
1772 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1774 /* A language-specific option can only be considered enabled when it's
1775 valid for the current language. */
1776 if (!(option
->flags
& CL_COMMON
)
1777 && (option
->flags
& CL_LANG_ALL
)
1778 && !(option
->flags
& lang_mask
))
1781 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1782 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1785 switch (option
->var_type
)
1788 if (option
->cl_host_wide_int
)
1790 HOST_WIDE_INT v
= *(HOST_WIDE_INT
*) flag_var
;
1791 return v
!= 0 ? (v
< 0 ? -1 : 1) : 0;
1795 int v
= *(int *) flag_var
;
1796 return v
!= 0 ? (v
< 0 ? -1 : 1) : 0;
1800 if (option
->cl_host_wide_int
)
1801 return *(HOST_WIDE_INT
*) flag_var
== option
->var_value
;
1803 return *(int *) flag_var
== option
->var_value
;
1805 case CLVC_BIT_CLEAR
:
1806 if (option
->cl_host_wide_int
)
1807 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) == 0;
1809 return (*(int *) flag_var
& option
->var_value
) == 0;
1812 if (option
->cl_host_wide_int
)
1813 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) != 0;
1815 return (*(int *) flag_var
& option
->var_value
) != 0;
1818 if (option
->cl_host_wide_int
)
1819 return *(HOST_WIDE_INT
*) flag_var
!= -1;
1821 return *(int *) flag_var
!= -1;
1831 /* Fill STATE with the current state of option OPTION in OPTS. Return
1832 true if there is some state to store. */
1835 get_option_state (struct gcc_options
*opts
, int option
,
1836 struct cl_option_state
*state
)
1838 void *flag_var
= option_flag_var (option
, opts
);
1843 switch (cl_options
[option
].var_type
)
1848 state
->data
= flag_var
;
1849 state
->size
= (cl_options
[option
].cl_host_wide_int
1850 ? sizeof (HOST_WIDE_INT
)
1854 case CLVC_BIT_CLEAR
:
1856 state
->ch
= option_enabled (option
, -1, opts
);
1857 state
->data
= &state
->ch
;
1862 state
->data
= *(const char **) flag_var
;
1863 if (state
->data
== 0)
1865 state
->size
= strlen ((const char *) state
->data
) + 1;
1869 state
->data
= flag_var
;
1870 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1879 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1880 handlers HANDLERS) to have diagnostic kind KIND for option
1881 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1882 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1883 argument of the option for joined options, or NULL otherwise. If IMPLY,
1884 the warning option in question is implied at this point. This is
1885 used by -Werror= and #pragma GCC diagnostic. */
1888 control_warning_option (unsigned int opt_index
, int kind
, const char *arg
,
1889 bool imply
, location_t loc
, unsigned int lang_mask
,
1890 const struct cl_option_handlers
*handlers
,
1891 struct gcc_options
*opts
,
1892 struct gcc_options
*opts_set
,
1893 diagnostic_context
*dc
)
1895 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1897 gcc_assert (!cl_options
[opt_index
].cl_separate_alias
1898 && !cl_options
[opt_index
].cl_negative_alias
);
1899 if (cl_options
[opt_index
].alias_arg
)
1900 arg
= cl_options
[opt_index
].alias_arg
;
1901 opt_index
= cl_options
[opt_index
].alias_target
;
1903 if (opt_index
== OPT_SPECIAL_ignore
|| opt_index
== OPT_SPECIAL_warn_removed
)
1906 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1909 const struct cl_option
*option
= &cl_options
[opt_index
];
1911 /* -Werror=foo implies -Wfoo. */
1912 if (option
->var_type
== CLVC_INTEGER
1913 || option
->var_type
== CLVC_ENUM
1914 || option
->var_type
== CLVC_SIZE
)
1916 HOST_WIDE_INT value
= 1;
1918 if (arg
&& *arg
== '\0' && !option
->cl_missing_ok
)
1921 if ((option
->flags
& CL_JOINED
) && arg
== NULL
)
1923 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1924 CL_ERR_MISSING_ARG
, lang_mask
);
1928 /* If the switch takes an integer argument, convert it. */
1929 if (arg
&& (option
->cl_uinteger
|| option
->cl_host_wide_int
))
1932 value
= *arg
? integral_argument (arg
, &error
,
1933 option
->cl_byte_size
) : 0;
1936 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1937 CL_ERR_UINT_ARG
, lang_mask
);
1942 /* If the switch takes an enumerated argument, convert it. */
1943 if (arg
&& option
->var_type
== CLVC_ENUM
)
1945 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1947 if (enum_arg_to_value (e
->values
, arg
, 0, &value
,
1950 const char *carg
= NULL
;
1952 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
1954 gcc_assert (carg
!= NULL
);
1958 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1959 CL_ERR_ENUM_ARG
, lang_mask
);
1964 handle_generated_option (opts
, opts_set
,
1965 opt_index
, arg
, value
, lang_mask
,
1966 kind
, loc
, handlers
, false, dc
);
1971 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
1972 Store number of arguments into ARGC_P. */
1975 parse_options_from_collect_gcc_options (const char *collect_gcc_options
,
1976 obstack
*argv_obstack
,
1979 char *argv_storage
= xstrdup (collect_gcc_options
);
1982 for (j
= 0, k
= 0; argv_storage
[j
] != '\0'; ++j
)
1984 if (argv_storage
[j
] == '\'')
1986 obstack_ptr_grow (argv_obstack
, &argv_storage
[k
]);
1990 if (argv_storage
[j
] == '\0')
1991 fatal_error (input_location
,
1992 "malformed %<COLLECT_GCC_OPTIONS%>");
1993 else if (startswith (&argv_storage
[j
], "'\\''"))
1995 argv_storage
[k
++] = '\'';
1998 else if (argv_storage
[j
] == '\'')
2001 argv_storage
[k
++] = argv_storage
[j
++];
2004 argv_storage
[k
++] = '\0';
2008 obstack_ptr_grow (argv_obstack
, NULL
);
2009 *argc_p
= obstack_object_size (argv_obstack
) / sizeof (void *) - 1;
2012 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
2015 void prepend_xassembler_to_collect_as_options (const char *collect_as_options
,
2018 obstack opts_obstack
;
2021 obstack_init (&opts_obstack
);
2022 parse_options_from_collect_gcc_options (collect_as_options
,
2023 &opts_obstack
, &opts_count
);
2024 const char **assembler_opts
= XOBFINISH (&opts_obstack
, const char **);
2026 for (int i
= 0; i
< opts_count
; i
++)
2028 obstack_grow (o
, " '-Xassembler' ",
2029 strlen (" '-Xassembler' "));
2030 const char *opt
= assembler_opts
[i
];
2031 obstack_1grow (o
, '\'');
2032 obstack_grow (o
, opt
, strlen (opt
));
2033 obstack_1grow (o
, '\'');
2037 jobserver_info::jobserver_info ()
2039 /* Traditionally, GNU make uses opened pipes for jobserver-auth,
2040 e.g. --jobserver-auth=3,4.
2041 Starting with GNU make 4.4, one can use --jobserver-style=fifo
2042 and then named pipe is used: --jobserver-auth=fifo:/tmp/hcsparta. */
2044 /* Detect jobserver and drop it if it's not working. */
2045 string js_needle
= "--jobserver-auth=";
2046 string fifo_prefix
= "fifo:";
2048 const char *envval
= getenv ("MAKEFLAGS");
2051 string makeflags
= envval
;
2052 size_t n
= makeflags
.rfind (js_needle
);
2053 if (n
!= string::npos
)
2055 string ending
= makeflags
.substr (n
+ js_needle
.size ());
2056 if (ending
.find (fifo_prefix
) == 0)
2058 ending
= ending
.substr (fifo_prefix
.size ());
2059 pipe_path
= ending
.substr (0, ending
.find (' '));
2062 else if (sscanf (makeflags
.c_str () + n
+ js_needle
.size (),
2063 "%d,%d", &rfd
, &wfd
) == 2
2066 && is_valid_fd (rfd
)
2067 && is_valid_fd (wfd
))
2071 string dup
= makeflags
.substr (0, n
);
2072 size_t pos
= makeflags
.find (' ', n
);
2073 if (pos
!= string::npos
)
2074 dup
+= makeflags
.substr (pos
);
2075 skipped_makeflags
= "MAKEFLAGS=" + dup
;
2077 = "cannot access %<" + js_needle
+ "%> file descriptors";
2080 error_msg
= "%<" + js_needle
+ "%> is not present in %<MAKEFLAGS%>";
2083 error_msg
= "%<MAKEFLAGS%> environment variable is unset";
2085 if (!error_msg
.empty ())
2086 error_msg
= "jobserver is not available: " + error_msg
;
2090 jobserver_info::connect ()
2092 if (!pipe_path
.empty ())
2094 #if HOST_HAS_O_NONBLOCK
2095 pipefd
= open (pipe_path
.c_str (), O_RDWR
| O_NONBLOCK
);
2096 is_connected
= true;
2098 is_connected
= false;
2102 is_connected
= true;
2106 jobserver_info::disconnect ()
2108 if (!pipe_path
.empty ())
2110 gcc_assert (close (pipefd
) == 0);
2116 jobserver_info::get_token ()
2118 int fd
= pipe_path
.empty () ? rfd
: pipefd
;
2120 unsigned n
= read (fd
, &c
, 1);
2123 gcc_assert (errno
== EAGAIN
);
2131 jobserver_info::return_token ()
2133 int fd
= pipe_path
.empty () ? wfd
: pipefd
;
2135 gcc_assert (write (fd
, &c
, 1) == 1);