1 /* Command line option handling.
2 Copyright (C) 2006-2024 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 /* Given buffer P of size SZ, look for a prefix within OPTION_MAP;
472 if found, return the prefix and write the new prefix to *OUT_NEW_PREFIX.
473 Otherwise return nullptr. */
476 get_option_prefix_remapping (const char *p
, size_t sz
,
477 const char **out_new_prefix
)
479 for (unsigned i
= 0; i
< ARRAY_SIZE (option_map
); i
++)
481 const char * const old_prefix
= option_map
[i
].opt0
;
482 const size_t old_prefix_len
= strlen (old_prefix
);
483 if (old_prefix_len
<= sz
484 && !memcmp (p
, old_prefix
, old_prefix_len
))
486 *out_new_prefix
= option_map
[i
].new_prefix
;
493 /* Helper function for gcc.cc's driver::suggest_option, for populating the
494 vec of suggestions for misspelled options.
496 option_map above provides various prefixes for spelling command-line
497 options, which decode_cmdline_option uses to map spellings of options
498 to specific options. We want to do the reverse: to find all the ways
499 that a user could validly spell an option.
501 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
502 of its valid variant spellings to CANDIDATES, each without a leading
505 For example, given "-Wabi-tag", the following are added to CANDIDATES:
511 The added strings must be freed using free. */
514 add_misspelling_candidates (auto_vec
<char *> *candidates
,
515 const struct cl_option
*option
,
516 const char *opt_text
)
518 gcc_assert (candidates
);
520 gcc_assert (opt_text
);
521 if (remapping_prefix_p (option
))
523 candidates
->safe_push (xstrdup (opt_text
+ 1));
524 for (unsigned i
= 0; i
< ARRAY_SIZE (option_map
); i
++)
526 const char *opt0
= option_map
[i
].opt0
;
527 const char *new_prefix
= option_map
[i
].new_prefix
;
528 size_t new_prefix_len
= strlen (new_prefix
);
530 if (option
->cl_reject_negative
&& option_map
[i
].negated
)
533 if (strncmp (opt_text
, new_prefix
, new_prefix_len
) == 0)
535 char *alternative
= concat (opt0
+ 1, opt_text
+ new_prefix_len
,
537 candidates
->safe_push (alternative
);
541 /* For all params (e.g. --param=key=value),
542 include also '--param key=value'. */
543 const char *prefix
= "--param=";
544 if (strstr (opt_text
, prefix
) == opt_text
)
546 char *param
= xstrdup (opt_text
+ 1);
547 gcc_assert (param
[6] == '=');
549 candidates
->safe_push (param
);
553 /* Decode the switch beginning at ARGV for the language indicated by
554 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
555 the structure *DECODED. Returns the number of switches
559 decode_cmdline_option (const char *const *argv
, unsigned int lang_mask
,
560 struct cl_decoded_option
*decoded
)
564 HOST_WIDE_INT value
= 1, mask
= 0;
565 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
569 const struct cl_option
*option
;
571 const char *warn_message
= NULL
;
572 bool separate_arg_flag
;
573 bool joined_arg_flag
;
574 bool have_separate_arg
= false;
578 const char *opt_value
= argv
[0] + 1;
579 opt_index
= find_opt (opt_value
, lang_mask
);
581 while (opt_index
== OPT_SPECIAL_unknown
582 && i
< ARRAY_SIZE (option_map
))
584 const char *opt0
= option_map
[i
].opt0
;
585 const char *opt1
= option_map
[i
].opt1
;
586 const char *new_prefix
= option_map
[i
].new_prefix
;
587 bool another_char_needed
= option_map
[i
].another_char_needed
;
588 size_t opt0_len
= strlen (opt0
);
589 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
590 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
591 size_t new_prefix_len
= strlen (new_prefix
);
593 extra_args
= (opt1
== NULL
? 0 : 1);
594 value
= !option_map
[i
].negated
;
596 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
598 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
599 && (!another_char_needed
600 || argv
[extra_args
][optn_len
] != 0))
602 size_t arglen
= strlen (argv
[extra_args
]);
605 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
606 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
607 memcpy (dup
, new_prefix
, new_prefix_len
);
608 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
609 arglen
- optn_len
+ 1);
610 opt_index
= find_opt (dup
+ 1, lang_mask
);
616 if (opt_index
== OPT_SPECIAL_unknown
)
624 option
= &cl_options
[opt_index
];
626 /* Reject negative form of switches that don't take negatives as
628 if (!value
&& option
->cl_reject_negative
)
630 opt_index
= OPT_SPECIAL_unknown
;
631 errors
|= CL_ERR_NEGATIVE
;
636 /* Clear the initial value for size options (it will be overwritten
637 later based on the Init(value) specification in the opt file. */
638 if (option
->var_type
== CLVC_SIZE
)
641 result
= extra_args
+ 1;
642 warn_message
= option
->warn_message
;
644 /* Check to see if the option is disabled for this configuration. */
645 if (option
->cl_disabled
)
646 errors
|= CL_ERR_DISABLED
;
648 /* Determine whether there may be a separate argument based on
649 whether this option is being processed for the driver, and, if
650 so, how many such arguments. */
651 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
652 && !(option
->cl_no_driver_arg
653 && (lang_mask
& CL_DRIVER
)));
654 separate_args
= (separate_arg_flag
655 ? option
->cl_separate_nargs
+ 1
657 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
659 /* Sort out any argument the switch takes. */
662 /* Have arg point to the original switch. This is because
663 some code, such as disable_builtin_function, expects its
664 argument to be persistent until the program exits. */
665 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
667 if (*arg
== '\0' && !option
->cl_missing_ok
)
669 if (separate_arg_flag
)
671 arg
= argv
[extra_args
+ 1];
672 result
= extra_args
+ 2;
674 result
= extra_args
+ 1;
676 have_separate_arg
= true;
679 /* Missing argument. */
683 else if (separate_arg_flag
)
685 arg
= argv
[extra_args
+ 1];
686 for (i
= 0; i
< separate_args
; i
++)
687 if (argv
[extra_args
+ 1 + i
] == NULL
)
689 errors
|= CL_ERR_MISSING_ARG
;
692 result
= extra_args
+ 1 + i
;
694 have_separate_arg
= true;
697 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
698 errors
|= CL_ERR_MISSING_ARG
;
700 /* Is this option an alias (or an ignored option, marked as an alias
701 of OPT_SPECIAL_ignore)? */
702 if (option
->alias_target
!= N_OPTS
703 && (!option
->cl_separate_alias
|| have_separate_arg
))
705 size_t new_opt_index
= option
->alias_target
;
707 if (new_opt_index
== OPT_SPECIAL_ignore
708 || new_opt_index
== OPT_SPECIAL_warn_removed
)
710 gcc_assert (option
->alias_arg
== NULL
);
711 gcc_assert (option
->neg_alias_arg
== NULL
);
712 opt_index
= new_opt_index
;
717 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
719 /* The new option must not be an alias itself. */
720 gcc_assert (new_option
->alias_target
== N_OPTS
721 || new_option
->cl_separate_alias
);
723 if (option
->neg_alias_arg
)
725 gcc_assert (option
->alias_arg
!= NULL
);
726 gcc_assert (arg
== NULL
);
727 gcc_assert (!option
->cl_negative_alias
);
729 arg
= option
->alias_arg
;
731 arg
= option
->neg_alias_arg
;
734 else if (option
->alias_arg
)
736 gcc_assert (value
== 1);
737 gcc_assert (arg
== NULL
);
738 gcc_assert (!option
->cl_negative_alias
);
739 arg
= option
->alias_arg
;
742 if (option
->cl_negative_alias
)
745 opt_index
= new_opt_index
;
749 gcc_assert (!option
->cl_reject_negative
);
751 /* Recompute what arguments are allowed. */
752 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
753 && !(option
->cl_no_driver_arg
754 && (lang_mask
& CL_DRIVER
)));
755 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
757 if (separate_args
> 1 || option
->cl_separate_nargs
)
758 gcc_assert (separate_args
759 == (unsigned int) option
->cl_separate_nargs
+ 1);
761 if (!(errors
& CL_ERR_MISSING_ARG
))
763 if (separate_arg_flag
|| joined_arg_flag
)
765 if (option
->cl_missing_ok
&& arg
== NULL
)
767 gcc_assert (arg
!= NULL
);
770 gcc_assert (arg
== NULL
);
773 /* Recheck for warnings and disabled options. */
774 if (option
->warn_message
)
776 gcc_assert (warn_message
== NULL
);
777 warn_message
= option
->warn_message
;
779 if (option
->cl_disabled
)
780 errors
|= CL_ERR_DISABLED
;
784 /* Check if this is a switch for a different front end. */
785 if (!option_ok_for_language (option
, lang_mask
))
786 errors
|= CL_ERR_WRONG_LANG
;
787 else if (strcmp (option
->opt_text
, "-Werror=") == 0
788 && strchr (opt_value
, ',') == NULL
)
790 /* Verify that -Werror argument is a valid warning
792 char *werror_arg
= xstrdup (opt_value
+ 6);
795 size_t warning_index
= find_opt (werror_arg
, lang_mask
);
797 if (warning_index
!= OPT_SPECIAL_unknown
)
799 const struct cl_option
*warning_option
800 = &cl_options
[warning_index
];
801 if (!option_ok_for_language (warning_option
, lang_mask
))
802 errors
|= CL_ERR_WRONG_LANG
;
806 /* Convert the argument to lowercase if appropriate. */
807 if (arg
&& option
->cl_tolower
)
810 size_t len
= strlen (arg
);
811 char *arg_lower
= XOBNEWVEC (&opts_obstack
, char, len
+ 1);
813 for (j
= 0; j
< len
; j
++)
814 arg_lower
[j
] = TOLOWER ((unsigned char) arg
[j
]);
819 /* If the switch takes an integer argument, convert it. */
820 if (arg
&& (option
->cl_uinteger
|| option
->cl_host_wide_int
))
823 value
= *arg
? integral_argument (arg
, &error
, option
->cl_byte_size
) : 0;
825 errors
|= CL_ERR_UINT_ARG
;
827 /* Reject value out of a range. */
828 if (option
->range_max
!= -1
829 && (value
< option
->range_min
|| value
> option
->range_max
))
830 errors
|= CL_ERR_INT_RANGE_ARG
;
833 /* If the switch takes an enumerated argument, convert it. */
834 if (arg
&& (option
->var_type
== CLVC_ENUM
))
836 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
838 gcc_assert (option
->var_value
!= CLEV_NORMAL
|| value
== 1);
839 if (option
->var_value
!= CLEV_NORMAL
)
842 HOST_WIDE_INT sum_value
= 0;
843 unsigned HOST_WIDE_INT used_sets
= 0;
846 const char *q
= strchr (p
, ',');
847 HOST_WIDE_INT this_value
= 0;
850 errors
|= CL_ERR_ENUM_SET_ARG
;
853 int idx
= enum_arg_to_value (e
->values
, p
, q
? q
- p
: 0,
854 &this_value
, lang_mask
);
857 errors
|= CL_ERR_ENUM_SET_ARG
;
861 HOST_WIDE_INT this_mask
= 0;
862 if (option
->var_value
== CLEV_SET
)
864 unsigned set
= e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
;
865 gcc_checking_assert (set
>= 1
866 && set
<= HOST_BITS_PER_WIDE_INT
);
867 if ((used_sets
& (HOST_WIDE_INT_1U
<< (set
- 1))) != 0)
869 errors
|= CL_ERR_ENUM_SET_ARG
;
872 used_sets
|= HOST_WIDE_INT_1U
<< (set
- 1);
874 for (int i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
875 if (set
== (e
->values
[i
].flags
>> CL_ENUM_SET_SHIFT
))
876 this_mask
|= e
->values
[i
].value
;
880 gcc_assert (option
->var_value
== CLEV_BITSET
881 && ((e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
)
883 this_mask
= this_value
;
886 sum_value
|= this_value
;
896 gcc_checking_assert (value
== 0);
898 else if (enum_arg_to_value (e
->values
, arg
, 0, &value
, lang_mask
) >= 0)
900 const char *carg
= NULL
;
902 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
904 gcc_assert (carg
!= NULL
);
907 errors
|= CL_ERR_ENUM_ARG
;
911 decoded
->opt_index
= opt_index
;
913 decoded
->value
= value
;
914 decoded
->mask
= mask
;
915 decoded
->errors
= errors
;
916 decoded
->warn_message
= warn_message
;
918 if (opt_index
== OPT_SPECIAL_unknown
)
919 gcc_assert (result
== 1);
921 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
922 decoded
->canonical_option_num_elements
= result
;
924 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
929 if (opt_index
== OPT_SPECIAL_unknown
)
930 decoded
->canonical_option
[i
] = argv
[i
];
932 decoded
->canonical_option
[i
] = NULL
;
933 len
= strlen (argv
[i
]);
934 /* If the argument is an empty string, we will print it as "" in
935 orig_option_with_args_text. */
936 total_len
+= (len
!= 0 ? len
: 2) + 1;
939 decoded
->canonical_option
[i
] = NULL
;
941 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
942 && opt_index
!= OPT_SPECIAL_warn_removed
)
944 generate_canonical_option (opt_index
, arg
, value
, decoded
);
945 if (separate_args
> 1)
947 for (i
= 0; i
< separate_args
; i
++)
949 if (argv
[extra_args
+ 1 + i
] == NULL
)
952 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
954 gcc_assert (result
== 1 + i
);
955 decoded
->canonical_option_num_elements
= result
;
958 decoded
->orig_option_with_args_text
959 = p
= XOBNEWVEC (&opts_obstack
, char, total_len
);
960 for (i
= 0; i
< result
; i
++)
962 size_t len
= strlen (argv
[i
]);
964 /* Print the empty string verbally. */
971 memcpy (p
, argv
[i
], len
);
982 /* Obstack for option strings. */
984 struct obstack opts_obstack
;
986 /* Like libiberty concat, but allocate using opts_obstack. */
989 opts_concat (const char *first
, ...)
996 /* First compute the size of the result and get sufficient memory. */
997 va_start (ap
, first
);
998 for (arg
= first
; arg
; arg
= va_arg (ap
, const char *))
999 length
+= strlen (arg
);
1000 newstr
= XOBNEWVEC (&opts_obstack
, char, length
+ 1);
1003 /* Now copy the individual pieces to the result string. */
1004 va_start (ap
, first
);
1005 for (arg
= first
, end
= newstr
; arg
; arg
= va_arg (ap
, const char *))
1007 length
= strlen (arg
);
1008 memcpy (end
, arg
, length
);
1016 /* Decode command-line options (ARGC and ARGV being the arguments of
1017 main) into an array, setting *DECODED_OPTIONS to a pointer to that
1018 array and *DECODED_OPTIONS_COUNT to the number of entries in the
1019 array. The first entry in the array is always one for the program
1020 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
1021 flags applicable for decoding (including CL_COMMON and CL_TARGET if
1022 those options should be considered applicable). Do not produce any
1023 diagnostics or set state outside of these variables. */
1026 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
1027 unsigned int lang_mask
,
1028 struct cl_decoded_option
**decoded_options
,
1029 unsigned int *decoded_options_count
)
1032 struct cl_decoded_option
*opt_array
;
1033 unsigned int num_decoded_options
;
1035 int opt_array_len
= argc
;
1036 opt_array
= XNEWVEC (struct cl_decoded_option
, opt_array_len
);
1038 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
1039 opt_array
[0].warn_message
= NULL
;
1040 opt_array
[0].arg
= argv
[0];
1041 opt_array
[0].orig_option_with_args_text
= argv
[0];
1042 opt_array
[0].canonical_option_num_elements
= 1;
1043 opt_array
[0].canonical_option
[0] = argv
[0];
1044 opt_array
[0].canonical_option
[1] = NULL
;
1045 opt_array
[0].canonical_option
[2] = NULL
;
1046 opt_array
[0].canonical_option
[3] = NULL
;
1047 opt_array
[0].value
= 1;
1048 opt_array
[0].mask
= 0;
1049 opt_array
[0].errors
= 0;
1050 num_decoded_options
= 1;
1052 for (i
= 1; i
< argc
; i
+= n
)
1054 const char *opt
= argv
[i
];
1056 /* Interpret "-" or a non-switch as a file name. */
1057 if (opt
[0] != '-' || opt
[1] == '\0')
1059 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
1060 num_decoded_options
++;
1065 /* Interpret "--param" "key=name" as "--param=key=name". */
1066 const char *needle
= "--param";
1067 if (i
+ 1 < argc
&& strcmp (opt
, needle
) == 0)
1069 const char *replacement
1070 = opts_concat (needle
, "=", argv
[i
+ 1], NULL
);
1071 argv
[++i
] = replacement
;
1074 /* Expand -fdiagnostics-plain-output to its constituents. This needs
1075 to happen here so that prune_options can handle -fdiagnostics-color
1077 if (!strcmp (opt
, "-fdiagnostics-plain-output"))
1079 /* If you have changed the default diagnostics output, and this new
1080 output is not appropriately "plain" (e.g., the change needs to be
1081 undone in order for the testsuite to work properly), then please do
1083 1. Add the necessary option to undo the new behavior to
1085 2. Update the documentation for -fdiagnostics-plain-output
1087 const char *const expanded_args
[] = {
1088 "-fno-diagnostics-show-caret",
1089 "-fno-diagnostics-show-line-numbers",
1090 "-fdiagnostics-color=never",
1091 "-fdiagnostics-urls=never",
1092 "-fdiagnostics-path-format=separate-events",
1093 "-fdiagnostics-text-art-charset=none"
1095 const int num_expanded
= ARRAY_SIZE (expanded_args
);
1096 opt_array_len
+= num_expanded
- 1;
1097 opt_array
= XRESIZEVEC (struct cl_decoded_option
,
1098 opt_array
, opt_array_len
);
1099 for (int j
= 0, nj
; j
< num_expanded
; j
+= nj
)
1101 nj
= decode_cmdline_option (expanded_args
+ j
, lang_mask
,
1102 &opt_array
[num_decoded_options
]);
1103 num_decoded_options
++;
1110 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
1111 &opt_array
[num_decoded_options
]);
1112 num_decoded_options
++;
1115 *decoded_options
= opt_array
;
1116 *decoded_options_count
= num_decoded_options
;
1117 prune_options (decoded_options
, decoded_options_count
);
1120 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
1121 next one is the same as ORIG_NEXT_OPT_IDX. */
1124 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
1126 /* An option can be canceled by the same option or an option with
1128 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
1131 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
1132 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
1138 /* Filter out options canceled by the ones after them, and related
1142 prune_options (struct cl_decoded_option
**decoded_options
,
1143 unsigned int *decoded_options_count
)
1145 unsigned int old_decoded_options_count
= *decoded_options_count
;
1146 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
1147 unsigned int new_decoded_options_count
;
1148 struct cl_decoded_option
*new_decoded_options
1149 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
1151 const struct cl_option
*option
;
1152 unsigned int options_to_prepend
= 0;
1153 unsigned int Wcomplain_wrong_lang_idx
= 0;
1154 unsigned int fdiagnostics_color_idx
= 0;
1156 /* Remove arguments which are negated by others after them. */
1157 new_decoded_options_count
= 0;
1158 for (i
= 0; i
< old_decoded_options_count
; i
++)
1160 unsigned int j
, opt_idx
, next_opt_idx
;
1162 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
1165 opt_idx
= old_decoded_options
[i
].opt_index
;
1168 case OPT_SPECIAL_unknown
:
1169 case OPT_SPECIAL_ignore
:
1170 case OPT_SPECIAL_warn_removed
:
1171 case OPT_SPECIAL_program_name
:
1172 case OPT_SPECIAL_input_file
:
1175 /* Do not handle the following yet, just remember the last one. */
1176 case OPT_Wcomplain_wrong_lang
:
1177 gcc_checking_assert (i
!= 0);
1178 if (Wcomplain_wrong_lang_idx
== 0)
1179 ++options_to_prepend
;
1180 Wcomplain_wrong_lang_idx
= i
;
1182 case OPT_fdiagnostics_color_
:
1183 gcc_checking_assert (i
!= 0);
1184 if (fdiagnostics_color_idx
== 0)
1185 ++options_to_prepend
;
1186 fdiagnostics_color_idx
= i
;
1190 gcc_assert (opt_idx
< cl_options_count
);
1191 option
= &cl_options
[opt_idx
];
1192 if (option
->neg_index
< 0)
1195 /* Skip joined switches. */
1196 if ((option
->flags
& CL_JOINED
)
1197 && (!option
->cl_reject_negative
1198 || (unsigned int) option
->neg_index
!= opt_idx
))
1201 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
1203 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
1205 next_opt_idx
= old_decoded_options
[j
].opt_index
;
1206 if (next_opt_idx
>= cl_options_count
)
1208 if (cl_options
[next_opt_idx
].neg_index
< 0)
1210 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
)
1211 && (!cl_options
[next_opt_idx
].cl_reject_negative
1212 || ((unsigned int) cl_options
[next_opt_idx
].neg_index
1215 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
1218 if (j
== old_decoded_options_count
)
1221 new_decoded_options
[new_decoded_options_count
]
1222 = old_decoded_options
[i
];
1223 new_decoded_options_count
++;
1229 /* For those not yet handled, put (only) the last at a front position after
1230 'argv[0]', so they can take effect immediately. */
1231 if (options_to_prepend
)
1233 const unsigned int argv_0
= 1;
1234 memmove (new_decoded_options
+ argv_0
+ options_to_prepend
,
1235 new_decoded_options
+ argv_0
,
1236 sizeof (struct cl_decoded_option
)
1237 * (new_decoded_options_count
- argv_0
));
1238 unsigned int options_prepended
= 0;
1239 if (Wcomplain_wrong_lang_idx
!= 0)
1241 new_decoded_options
[argv_0
+ options_prepended
++]
1242 = old_decoded_options
[Wcomplain_wrong_lang_idx
];
1243 new_decoded_options_count
++;
1245 if (fdiagnostics_color_idx
!= 0)
1247 new_decoded_options
[argv_0
+ options_prepended
++]
1248 = old_decoded_options
[fdiagnostics_color_idx
];
1249 new_decoded_options_count
++;
1251 gcc_checking_assert (options_to_prepend
== options_prepended
);
1254 free (old_decoded_options
);
1255 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
1256 new_decoded_options
,
1257 new_decoded_options_count
);
1258 *decoded_options
= new_decoded_options
;
1259 *decoded_options_count
= new_decoded_options_count
;
1262 /* Handle option DECODED for the language indicated by LANG_MASK,
1263 using the handlers in HANDLERS and setting fields in OPTS and
1264 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1265 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1266 option for options from the source file, UNKNOWN_LOCATION
1267 otherwise. GENERATED_P is true for an option generated as part of
1268 processing another option or otherwise generated internally, false
1269 for one explicitly passed by the user. control_warning_option
1270 generated options are considered explicitly passed by the user.
1271 Returns false if the switch was invalid. DC is the diagnostic
1272 context for options affecting diagnostics state, or NULL. */
1275 handle_option (struct gcc_options
*opts
,
1276 struct gcc_options
*opts_set
,
1277 const struct cl_decoded_option
*decoded
,
1278 unsigned int lang_mask
, int kind
, location_t loc
,
1279 const struct cl_option_handlers
*handlers
,
1280 bool generated_p
, diagnostic_context
*dc
)
1282 size_t opt_index
= decoded
->opt_index
;
1283 const char *arg
= decoded
->arg
;
1284 HOST_WIDE_INT value
= decoded
->value
;
1285 HOST_WIDE_INT mask
= decoded
->mask
;
1286 const struct cl_option
*option
= &cl_options
[opt_index
];
1287 void *flag_var
= option_flag_var (opt_index
, opts
);
1291 set_option (opts
, (generated_p
? NULL
: opts_set
),
1292 opt_index
, value
, arg
, kind
, loc
, dc
, mask
);
1294 for (i
= 0; i
< handlers
->num_handlers
; i
++)
1295 if (option
->flags
& handlers
->handlers
[i
].mask
)
1297 if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
1298 lang_mask
, kind
, loc
,
1300 handlers
->target_option_override_hook
))
1307 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1308 option instead of DECODED. This is used for callbacks when one
1309 option implies another instead of an option being decoded from the
1313 handle_generated_option (struct gcc_options
*opts
,
1314 struct gcc_options
*opts_set
,
1315 size_t opt_index
, const char *arg
, HOST_WIDE_INT value
,
1316 unsigned int lang_mask
, int kind
, location_t loc
,
1317 const struct cl_option_handlers
*handlers
,
1318 bool generated_p
, diagnostic_context
*dc
)
1320 struct cl_decoded_option decoded
;
1322 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
1323 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
1324 handlers
, generated_p
, dc
);
1327 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1328 VALUE for a front end using LANG_MASK. This is used when the
1329 compiler generates options internally. */
1332 generate_option (size_t opt_index
, const char *arg
, HOST_WIDE_INT value
,
1333 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
1335 const struct cl_option
*option
= &cl_options
[opt_index
];
1337 decoded
->opt_index
= opt_index
;
1338 decoded
->warn_message
= NULL
;
1340 decoded
->value
= value
;
1342 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
1344 : CL_ERR_WRONG_LANG
);
1346 generate_canonical_option (opt_index
, arg
, value
, decoded
);
1347 switch (decoded
->canonical_option_num_elements
)
1350 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
1354 decoded
->orig_option_with_args_text
1355 = opts_concat (decoded
->canonical_option
[0], " ",
1356 decoded
->canonical_option
[1], NULL
);
1364 /* Fill in *DECODED with an option for input file FILE. */
1367 generate_option_input_file (const char *file
,
1368 struct cl_decoded_option
*decoded
)
1370 decoded
->opt_index
= OPT_SPECIAL_input_file
;
1371 decoded
->warn_message
= NULL
;
1372 decoded
->arg
= file
;
1373 decoded
->orig_option_with_args_text
= file
;
1374 decoded
->canonical_option_num_elements
= 1;
1375 decoded
->canonical_option
[0] = file
;
1376 decoded
->canonical_option
[1] = NULL
;
1377 decoded
->canonical_option
[2] = NULL
;
1378 decoded
->canonical_option
[3] = NULL
;
1381 decoded
->errors
= 0;
1384 /* Helper function for listing valid choices and hint for misspelled
1385 value. CANDIDATES is a vector containing all valid strings,
1386 STR is set to a heap allocated string that contains all those
1387 strings concatenated, separated by spaces, and the return value
1388 is the closest string from those to ARG, or NULL if nothing is
1389 close enough. Callers should XDELETEVEC (STR) after using it
1390 to avoid memory leaks. */
1393 candidates_list_and_hint (const char *arg
, char *&str
,
1394 const auto_vec
<const char *> &candidates
)
1398 const char *candidate
;
1401 gcc_assert (!candidates
.is_empty ());
1403 FOR_EACH_VEC_ELT (candidates
, i
, candidate
)
1404 len
+= strlen (candidate
) + 1;
1406 str
= p
= XNEWVEC (char, len
);
1407 FOR_EACH_VEC_ELT (candidates
, i
, candidate
)
1409 len
= strlen (candidate
);
1410 memcpy (p
, candidate
, len
);
1415 return find_closest_string (arg
, &candidates
);
1418 /* Perform diagnostics for read_cmdline_option and control_warning_option
1419 functions. Returns true if an error has been diagnosed.
1420 LOC and LANG_MASK arguments like in read_cmdline_option.
1421 OPTION is the option to report diagnostics for, OPT the name
1422 of the option as text, ARG the argument of the option (for joined
1423 options), ERRORS is bitmask of CL_ERR_* values. */
1426 cmdline_handle_error (location_t loc
, const struct cl_option
*option
,
1427 const char *opt
, const char *arg
, int errors
,
1428 unsigned int lang_mask
)
1430 if (errors
& CL_ERR_DISABLED
)
1432 error_at (loc
, "command-line option %qs"
1433 " is not supported by this configuration", opt
);
1437 if (errors
& CL_ERR_MISSING_ARG
)
1439 if (option
->missing_argument_error
)
1440 error_at (loc
, option
->missing_argument_error
, opt
);
1442 error_at (loc
, "missing argument to %qs", opt
);
1446 if (errors
& CL_ERR_UINT_ARG
)
1448 if (option
->cl_byte_size
)
1449 error_at (loc
, "argument to %qs should be a non-negative integer "
1450 "optionally followed by a size unit",
1453 error_at (loc
, "argument to %qs should be a non-negative integer",
1458 if (errors
& CL_ERR_INT_RANGE_ARG
)
1460 error_at (loc
, "argument to %qs is not between %d and %d",
1461 option
->opt_text
, option
->range_min
, option
->range_max
);
1465 if (errors
& CL_ERR_ENUM_SET_ARG
)
1467 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1468 const char *p
= arg
;
1469 unsigned HOST_WIDE_INT used_sets
= 0;
1470 const char *second_opt
= NULL
;
1471 size_t second_opt_len
= 0;
1475 const char *q
= strchr (p
, ',');
1476 HOST_WIDE_INT this_value
= 0;
1480 errors
= CL_ERR_ENUM_ARG
;
1483 int idx
= enum_arg_to_value (e
->values
, p
, q
? q
- p
: 0,
1484 &this_value
, lang_mask
);
1488 q
= strchr (p
, '\0');
1489 char *narg
= XALLOCAVEC (char, (q
- p
) + 1);
1490 memcpy (narg
, p
, q
- p
);
1493 errors
= CL_ERR_ENUM_ARG
;
1497 if (option
->var_value
== CLEV_BITSET
)
1505 unsigned set
= e
->values
[idx
].flags
>> CL_ENUM_SET_SHIFT
;
1506 gcc_checking_assert (set
>= 1 && set
<= HOST_BITS_PER_WIDE_INT
);
1507 if ((used_sets
& (HOST_WIDE_INT_1U
<< (set
- 1))) != 0)
1510 q
= strchr (p
, '\0');
1511 if (second_opt
== NULL
)
1513 used_sets
= HOST_WIDE_INT_1U
<< (set
- 1);
1515 second_opt_len
= q
- p
;
1519 char *args
= XALLOCAVEC (char, (q
- p
) + 1 + second_opt_len
+ 1);
1520 memcpy (args
, p
, q
- p
);
1522 memcpy (args
+ (q
- p
) + 1, second_opt
, second_opt_len
);
1523 args
[(q
- p
) + 1 + second_opt_len
] = '\0';
1524 error_at (loc
, "invalid argument in option %qs", opt
);
1525 if (strcmp (args
, args
+ (q
- p
) + 1) == 0)
1526 inform (loc
, "%qs specified multiple times in the same option",
1529 inform (loc
, "%qs is mutually exclusive with %qs and cannot be"
1530 " specified together", args
, args
+ (q
- p
) + 1);
1533 used_sets
|= HOST_WIDE_INT_1U
<< (set
- 1);
1541 if (errors
& CL_ERR_ENUM_ARG
)
1543 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1547 auto_diagnostic_group d
;
1548 if (e
->unknown_error
)
1549 error_at (loc
, e
->unknown_error
, arg
);
1551 error_at (loc
, "unrecognized argument in option %qs", opt
);
1553 auto_vec
<const char *> candidates
;
1554 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1556 if (!enum_arg_ok_for_language (&e
->values
[i
], lang_mask
))
1558 candidates
.safe_push (e
->values
[i
].arg
);
1560 const char *hint
= candidates_list_and_hint (arg
, s
, candidates
);
1562 inform (loc
, "valid arguments to %qs are: %s; did you mean %qs?",
1563 option
->opt_text
, s
, hint
);
1565 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1574 /* Handle the switch DECODED (location LOC) for the language indicated
1575 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1576 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1577 diagnostic options. */
1580 read_cmdline_option (struct gcc_options
*opts
,
1581 struct gcc_options
*opts_set
,
1582 struct cl_decoded_option
*decoded
,
1584 unsigned int lang_mask
,
1585 const struct cl_option_handlers
*handlers
,
1586 diagnostic_context
*dc
)
1588 const struct cl_option
*option
;
1589 const char *opt
= decoded
->orig_option_with_args_text
;
1591 if (decoded
->warn_message
)
1592 warning_at (loc
, 0, decoded
->warn_message
, opt
);
1594 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
1596 if (handlers
->unknown_option_callback (decoded
))
1597 error_at (loc
, "unrecognized command-line option %qs", decoded
->arg
);
1601 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
1604 if (decoded
->opt_index
== OPT_SPECIAL_warn_removed
)
1606 /* Warn only about positive ignored options. */
1608 warning_at (loc
, 0, "switch %qs is no longer supported", opt
);
1612 option
= &cl_options
[decoded
->opt_index
];
1615 && cmdline_handle_error (loc
, option
, opt
, decoded
->arg
,
1616 decoded
->errors
, lang_mask
))
1619 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
1621 handlers
->wrong_lang_callback (decoded
, lang_mask
);
1625 gcc_assert (!decoded
->errors
);
1627 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1628 loc
, handlers
, false, dc
))
1629 error_at (loc
, "unrecognized command-line option %qs", opt
);
1632 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1633 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1634 location LOC, using diagnostic context DC if not NULL for
1635 diagnostic classification. */
1638 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1639 int opt_index
, HOST_WIDE_INT value
, const char *arg
, int kind
,
1640 location_t loc
, diagnostic_context
*dc
,
1641 HOST_WIDE_INT mask
/* = 0 */)
1643 const struct cl_option
*option
= &cl_options
[opt_index
];
1644 void *flag_var
= option_flag_var (opt_index
, opts
);
1645 void *set_flag_var
= NULL
;
1650 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
&& dc
!= NULL
)
1651 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1653 if (opts_set
!= NULL
)
1654 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1656 switch (option
->var_type
)
1659 if (option
->cl_host_wide_int
)
1661 *(HOST_WIDE_INT
*) flag_var
= value
;
1663 *(HOST_WIDE_INT
*) set_flag_var
= 1;
1667 if (value
> INT_MAX
)
1668 error_at (loc
, "argument to %qs is bigger than %d",
1669 option
->opt_text
, INT_MAX
);
1672 *(int *) flag_var
= value
;
1674 *(int *) set_flag_var
= 1;
1681 if (option
->cl_host_wide_int
)
1683 *(HOST_WIDE_INT
*) flag_var
= value
;
1685 *(HOST_WIDE_INT
*) set_flag_var
= value
;
1689 *(int *) flag_var
= value
;
1691 *(int *) set_flag_var
= value
;
1697 if (option
->cl_host_wide_int
)
1699 *(HOST_WIDE_INT
*) flag_var
= (value
1701 : !option
->var_value
);
1703 *(HOST_WIDE_INT
*) set_flag_var
= 1;
1707 *(int *) flag_var
= (value
1709 : !option
->var_value
);
1711 *(int *) set_flag_var
= 1;
1715 case CLVC_BIT_CLEAR
:
1717 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1719 if (option
->cl_host_wide_int
)
1720 *(HOST_WIDE_INT
*) flag_var
|= option
->var_value
;
1722 *(int *) flag_var
|= option
->var_value
;
1726 if (option
->cl_host_wide_int
)
1727 *(HOST_WIDE_INT
*) flag_var
&= ~option
->var_value
;
1729 *(int *) flag_var
&= ~option
->var_value
;
1733 if (option
->cl_host_wide_int
)
1734 *(HOST_WIDE_INT
*) set_flag_var
|= option
->var_value
;
1736 *(int *) set_flag_var
|= option
->var_value
;
1741 *(const char **) flag_var
= arg
;
1743 *(const char **) set_flag_var
= "";
1748 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1751 e
->set (flag_var
, value
| (e
->get (flag_var
) & ~mask
));
1753 e
->set (flag_var
, value
);
1755 e
->set (set_flag_var
, 1);
1761 vec
<cl_deferred_option
> *v
1762 = (vec
<cl_deferred_option
> *) *(void **) flag_var
;
1763 cl_deferred_option p
= {opt_index
, arg
, value
};
1765 v
= XCNEW (vec
<cl_deferred_option
>);
1767 *(void **) flag_var
= v
;
1769 *(void **) set_flag_var
= v
;
1775 /* Return the address of the flag variable for option OPT_INDEX in
1776 options structure OPTS, or NULL if there is no flag variable. */
1779 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1781 const struct cl_option
*option
= &cl_options
[opt_index
];
1783 if (option
->flag_var_offset
== (unsigned short) -1)
1785 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1788 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1789 or -1 if it isn't a simple on-off switch
1790 (or if the value is unknown, typically set later in target). */
1793 option_enabled (int opt_idx
, unsigned lang_mask
, void *opts
)
1795 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1797 /* A language-specific option can only be considered enabled when it's
1798 valid for the current language. */
1799 if (!(option
->flags
& CL_COMMON
)
1800 && (option
->flags
& CL_LANG_ALL
)
1801 && !(option
->flags
& lang_mask
))
1804 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1805 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1808 switch (option
->var_type
)
1811 if (option
->cl_host_wide_int
)
1813 HOST_WIDE_INT v
= *(HOST_WIDE_INT
*) flag_var
;
1814 return v
!= 0 ? (v
< 0 ? -1 : 1) : 0;
1818 int v
= *(int *) flag_var
;
1819 return v
!= 0 ? (v
< 0 ? -1 : 1) : 0;
1823 if (option
->cl_host_wide_int
)
1824 return *(HOST_WIDE_INT
*) flag_var
== option
->var_value
;
1826 return *(int *) flag_var
== option
->var_value
;
1828 case CLVC_BIT_CLEAR
:
1829 if (option
->cl_host_wide_int
)
1830 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) == 0;
1832 return (*(int *) flag_var
& option
->var_value
) == 0;
1835 if (option
->cl_host_wide_int
)
1836 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) != 0;
1838 return (*(int *) flag_var
& option
->var_value
) != 0;
1841 if (option
->cl_host_wide_int
)
1842 return *(HOST_WIDE_INT
*) flag_var
!= -1;
1844 return *(int *) flag_var
!= -1;
1854 /* Fill STATE with the current state of option OPTION in OPTS. Return
1855 true if there is some state to store. */
1858 get_option_state (struct gcc_options
*opts
, int option
,
1859 struct cl_option_state
*state
)
1861 void *flag_var
= option_flag_var (option
, opts
);
1866 switch (cl_options
[option
].var_type
)
1871 state
->data
= flag_var
;
1872 state
->size
= (cl_options
[option
].cl_host_wide_int
1873 ? sizeof (HOST_WIDE_INT
)
1877 case CLVC_BIT_CLEAR
:
1879 state
->ch
= option_enabled (option
, -1, opts
);
1880 state
->data
= &state
->ch
;
1885 state
->data
= *(const char **) flag_var
;
1886 if (state
->data
== 0)
1888 state
->size
= strlen ((const char *) state
->data
) + 1;
1892 state
->data
= flag_var
;
1893 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1902 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1903 handlers HANDLERS) to have diagnostic kind KIND for option
1904 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1905 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1906 argument of the option for joined options, or NULL otherwise. If IMPLY,
1907 the warning option in question is implied at this point. This is
1908 used by -Werror= and #pragma GCC diagnostic. */
1911 control_warning_option (unsigned int opt_index
, int kind
, const char *arg
,
1912 bool imply
, location_t loc
, unsigned int lang_mask
,
1913 const struct cl_option_handlers
*handlers
,
1914 struct gcc_options
*opts
,
1915 struct gcc_options
*opts_set
,
1916 diagnostic_context
*dc
)
1918 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1920 gcc_assert (!cl_options
[opt_index
].cl_separate_alias
1921 && !cl_options
[opt_index
].cl_negative_alias
);
1922 if (cl_options
[opt_index
].alias_arg
)
1923 arg
= cl_options
[opt_index
].alias_arg
;
1924 opt_index
= cl_options
[opt_index
].alias_target
;
1926 if (opt_index
== OPT_SPECIAL_ignore
|| opt_index
== OPT_SPECIAL_warn_removed
)
1929 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1932 /* -Werror=foo implies -Wfoo. */
1933 const struct cl_option
*option
= &cl_options
[opt_index
];
1934 HOST_WIDE_INT value
= 1;
1936 if (option
->var_type
== CLVC_INTEGER
1937 || option
->var_type
== CLVC_ENUM
1938 || option
->var_type
== CLVC_SIZE
)
1941 if (arg
&& *arg
== '\0' && !option
->cl_missing_ok
)
1944 if ((option
->flags
& CL_JOINED
) && arg
== NULL
)
1946 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1947 CL_ERR_MISSING_ARG
, lang_mask
);
1951 /* If the switch takes an integer argument, convert it. */
1952 if (arg
&& (option
->cl_uinteger
|| option
->cl_host_wide_int
))
1955 value
= *arg
? integral_argument (arg
, &error
,
1956 option
->cl_byte_size
) : 0;
1959 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1960 CL_ERR_UINT_ARG
, lang_mask
);
1965 /* If the switch takes an enumerated argument, convert it. */
1966 if (arg
&& option
->var_type
== CLVC_ENUM
)
1968 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1970 if (enum_arg_to_value (e
->values
, arg
, 0, &value
,
1973 const char *carg
= NULL
;
1975 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
1977 gcc_assert (carg
!= NULL
);
1981 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1982 CL_ERR_ENUM_ARG
, lang_mask
);
1988 handle_generated_option (opts
, opts_set
,
1989 opt_index
, arg
, value
, lang_mask
,
1990 kind
, loc
, handlers
, false, dc
);
1994 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
1995 Store number of arguments into ARGC_P. */
1998 parse_options_from_collect_gcc_options (const char *collect_gcc_options
,
1999 obstack
*argv_obstack
,
2002 char *argv_storage
= xstrdup (collect_gcc_options
);
2005 for (j
= 0, k
= 0; argv_storage
[j
] != '\0'; ++j
)
2007 if (argv_storage
[j
] == '\'')
2009 obstack_ptr_grow (argv_obstack
, &argv_storage
[k
]);
2013 if (argv_storage
[j
] == '\0')
2014 fatal_error (input_location
,
2015 "malformed %<COLLECT_GCC_OPTIONS%>");
2016 else if (startswith (&argv_storage
[j
], "'\\''"))
2018 argv_storage
[k
++] = '\'';
2021 else if (argv_storage
[j
] == '\'')
2024 argv_storage
[k
++] = argv_storage
[j
++];
2027 argv_storage
[k
++] = '\0';
2031 obstack_ptr_grow (argv_obstack
, NULL
);
2032 *argc_p
= obstack_object_size (argv_obstack
) / sizeof (void *) - 1;
2035 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
2038 void prepend_xassembler_to_collect_as_options (const char *collect_as_options
,
2041 obstack opts_obstack
;
2044 obstack_init (&opts_obstack
);
2045 parse_options_from_collect_gcc_options (collect_as_options
,
2046 &opts_obstack
, &opts_count
);
2047 const char **assembler_opts
= XOBFINISH (&opts_obstack
, const char **);
2049 for (int i
= 0; i
< opts_count
; i
++)
2051 obstack_grow (o
, " '-Xassembler' ",
2052 strlen (" '-Xassembler' "));
2053 const char *opt
= assembler_opts
[i
];
2054 obstack_1grow (o
, '\'');
2055 obstack_grow (o
, opt
, strlen (opt
));
2056 obstack_1grow (o
, '\'');
2060 jobserver_info::jobserver_info ()
2062 /* Traditionally, GNU make uses opened pipes for jobserver-auth,
2063 e.g. --jobserver-auth=3,4.
2064 Starting with GNU make 4.4, one can use --jobserver-style=fifo
2065 and then named pipe is used: --jobserver-auth=fifo:/tmp/hcsparta. */
2067 /* Detect jobserver and drop it if it's not working. */
2068 string js_needle
= "--jobserver-auth=";
2069 string fifo_prefix
= "fifo:";
2071 const char *envval
= getenv ("MAKEFLAGS");
2074 string makeflags
= envval
;
2075 size_t n
= makeflags
.rfind (js_needle
);
2076 if (n
!= string::npos
)
2078 string ending
= makeflags
.substr (n
+ js_needle
.size ());
2079 if (ending
.find (fifo_prefix
) == 0)
2081 ending
= ending
.substr (fifo_prefix
.size ());
2082 pipe_path
= ending
.substr (0, ending
.find (' '));
2085 else if (sscanf (makeflags
.c_str () + n
+ js_needle
.size (),
2086 "%d,%d", &rfd
, &wfd
) == 2
2089 && is_valid_fd (rfd
)
2090 && is_valid_fd (wfd
))
2094 string dup
= makeflags
.substr (0, n
);
2095 size_t pos
= makeflags
.find (' ', n
);
2096 if (pos
!= string::npos
)
2097 dup
+= makeflags
.substr (pos
);
2098 skipped_makeflags
= "MAKEFLAGS=" + dup
;
2100 = "cannot access %<" + js_needle
+ "%> file descriptors";
2103 error_msg
= "%<" + js_needle
+ "%> is not present in %<MAKEFLAGS%>";
2106 error_msg
= "%<MAKEFLAGS%> environment variable is unset";
2108 if (!error_msg
.empty ())
2109 error_msg
= "jobserver is not available: " + error_msg
;
2113 jobserver_info::connect ()
2115 if (!pipe_path
.empty ())
2117 #if HOST_HAS_O_NONBLOCK
2118 pipefd
= open (pipe_path
.c_str (), O_RDWR
| O_NONBLOCK
);
2119 is_connected
= true;
2121 is_connected
= false;
2125 is_connected
= true;
2129 jobserver_info::disconnect ()
2131 if (!pipe_path
.empty ())
2133 gcc_assert (close (pipefd
) == 0);
2139 jobserver_info::get_token ()
2141 int fd
= pipe_path
.empty () ? rfd
: pipefd
;
2143 unsigned n
= read (fd
, &c
, 1);
2146 gcc_assert (errno
== EAGAIN
);
2154 jobserver_info::return_token ()
2156 int fd
= pipe_path
.empty () ? wfd
: pipefd
;
2158 gcc_assert (write (fd
, &c
, 1) == 1);