1 /* Command line option handling.
2 Copyright (C) 2006-2016 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/>. */
23 #include "coretypes.h"
26 #include "diagnostic.h"
28 static void prune_options (struct cl_decoded_option
**, unsigned int *);
30 /* Perform a binary search to find which option the command-line INPUT
31 matches. Returns its index in the option array, and
32 OPT_SPECIAL_unknown on failure.
34 This routine is quite subtle. A normal binary search is not good
35 enough because some options can be suffixed with an argument, and
36 multiple sub-matches can occur, e.g. input of "-pedantic" matching
37 the initial substring of "-pedantic-errors".
39 A more complicated example is -gstabs. It should match "-g" with
40 an argument of "stabs". Suppose, however, that the number and list
41 of switches are such that the binary search tests "-gen-decls"
42 before having tested "-g". This doesn't match, and as "-gen-decls"
43 is less than "-gstabs", it will become the lower bound of the
44 binary search range, and "-g" will never be seen. To resolve this
45 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
46 to "-g" so that failed searches that end between "-gen-decls" and
47 the lexicographically subsequent switch know to go back and see if
48 "-g" causes a match (which it does in this example).
50 This search is done in such a way that the longest match for the
51 front end in question wins. If there is no match for the current
52 front end, the longest match for a different front end is returned
53 (or N_OPTS if none) and the caller emits an error message. */
55 find_opt (const char *input
, unsigned int lang_mask
)
57 size_t mn
, mn_orig
, mx
, md
, opt_len
;
58 size_t match_wrong_lang
;
62 mx
= cl_options_count
;
64 /* Find mn such this lexicographical inequality holds:
65 cl_options[mn] <= input < cl_options[mn + 1]. */
69 opt_len
= cl_options
[md
].opt_len
;
70 comp
= strncmp (input
, cl_options
[md
].opt_text
+ 1, opt_len
);
80 /* This is the switch that is the best match but for a different
81 front end, or OPT_SPECIAL_unknown if there is no match at all. */
82 match_wrong_lang
= OPT_SPECIAL_unknown
;
84 /* Backtrace the chain of possible matches, returning the longest
85 one, if any, that fits best. With current GCC switches, this
86 loop executes at most twice. */
89 const struct cl_option
*opt
= &cl_options
[mn
];
91 /* Is the input either an exact match or a prefix that takes a
93 if (!strncmp (input
, opt
->opt_text
+ 1, opt
->opt_len
)
94 && (input
[opt
->opt_len
] == '\0' || (opt
->flags
& CL_JOINED
)))
96 /* If language is OK, return it. */
97 if (opt
->flags
& lang_mask
)
100 /* If we haven't remembered a prior match, remember this
101 one. Any prior match is necessarily better. */
102 if (match_wrong_lang
== OPT_SPECIAL_unknown
)
103 match_wrong_lang
= mn
;
106 /* Try the next possibility. This is cl_options_count if there
108 mn
= opt
->back_chain
;
110 while (mn
!= cl_options_count
);
112 if (match_wrong_lang
== OPT_SPECIAL_unknown
&& input
[0] == '-')
114 /* Long options, starting "--", may be abbreviated if the
115 abbreviation is unambiguous. This only applies to options
116 not taking a joined argument, and abbreviations of "--option"
117 are permitted even if there is a variant "--option=". */
118 size_t mnc
= mn_orig
+ 1;
119 size_t cmp_len
= strlen (input
);
120 while (mnc
< cl_options_count
121 && strncmp (input
, cl_options
[mnc
].opt_text
+ 1, cmp_len
) == 0)
123 /* Option matching this abbreviation. OK if it is the first
124 match and that does not take a joined argument, or the
125 second match, taking a joined argument and with only '='
126 added to the first match; otherwise considered
128 if (mnc
== mn_orig
+ 1
129 && !(cl_options
[mnc
].flags
& CL_JOINED
))
130 match_wrong_lang
= mnc
;
131 else if (mnc
== mn_orig
+ 2
132 && match_wrong_lang
== mn_orig
+ 1
133 && (cl_options
[mnc
].flags
& CL_JOINED
)
134 && (cl_options
[mnc
].opt_len
135 == cl_options
[mn_orig
+ 1].opt_len
+ 1)
136 && strncmp (cl_options
[mnc
].opt_text
+ 1,
137 cl_options
[mn_orig
+ 1].opt_text
+ 1,
138 cl_options
[mn_orig
+ 1].opt_len
) == 0)
139 ; /* OK, as long as there are no more matches. */
141 return OPT_SPECIAL_unknown
;
146 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
147 return match_wrong_lang
;
150 /* If ARG is a non-negative decimal or hexadecimal integer, return its
151 value, otherwise return -1. */
154 integral_argument (const char *arg
)
158 while (*p
&& ISDIGIT (*p
))
164 /* It wasn't a decimal number - try hexadecimal. */
165 if (arg
[0] == '0' && (arg
[1] == 'x' || arg
[1] == 'X'))
168 while (*p
&& ISXDIGIT (*p
))
171 if (p
!= arg
+ 2 && *p
== '\0')
172 return strtol (arg
, NULL
, 16);
178 /* Return whether OPTION is OK for the language given by
181 option_ok_for_language (const struct cl_option
*option
,
182 unsigned int lang_mask
)
184 if (!(option
->flags
& lang_mask
))
186 else if ((option
->flags
& CL_TARGET
)
187 && (option
->flags
& (CL_LANG_ALL
| CL_DRIVER
))
188 && !(option
->flags
& (lang_mask
& ~CL_COMMON
& ~CL_TARGET
)))
189 /* Complain for target flag language mismatches if any languages
195 /* Return whether ENUM_ARG is OK for the language given by
199 enum_arg_ok_for_language (const struct cl_enum_arg
*enum_arg
,
200 unsigned int lang_mask
)
202 return (lang_mask
& CL_DRIVER
) || !(enum_arg
->flags
& CL_ENUM_DRIVER_ONLY
);
205 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
206 storing the value in *VALUE if found, and returning false without
207 modifying *VALUE if not found. */
210 enum_arg_to_value (const struct cl_enum_arg
*enum_args
,
211 const char *arg
, int *value
, unsigned int lang_mask
)
215 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
216 if (strcmp (arg
, enum_args
[i
].arg
) == 0
217 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
219 *value
= enum_args
[i
].value
;
226 /* Look up ARG in the enum used by option OPT_INDEX for language
227 LANG_MASK, returning true and storing the value in *VALUE if found,
228 and returning false without modifying *VALUE if not found. */
231 opt_enum_arg_to_value (size_t opt_index
, const char *arg
, int *value
,
232 unsigned int lang_mask
)
234 const struct cl_option
*option
= &cl_options
[opt_index
];
236 gcc_assert (option
->var_type
== CLVC_ENUM
);
238 return enum_arg_to_value (cl_enums
[option
->var_enum
].values
, arg
,
242 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
243 corresponding string in *ARGP, returning true if the found string
244 was marked as canonical, false otherwise. If VALUE is not found
245 (which may be the case for uninitialized values if the relevant
246 option has not been passed), set *ARGP to NULL and return
250 enum_value_to_arg (const struct cl_enum_arg
*enum_args
,
251 const char **argp
, int value
, unsigned int lang_mask
)
255 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
256 if (enum_args
[i
].value
== value
257 && (enum_args
[i
].flags
& CL_ENUM_CANONICAL
)
258 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
260 *argp
= enum_args
[i
].arg
;
264 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
265 if (enum_args
[i
].value
== value
266 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
268 *argp
= enum_args
[i
].arg
;
276 /* Fill in the canonical option part of *DECODED with an option
277 described by OPT_INDEX, ARG and VALUE. */
280 generate_canonical_option (size_t opt_index
, const char *arg
, int value
,
281 struct cl_decoded_option
*decoded
)
283 const struct cl_option
*option
= &cl_options
[opt_index
];
284 const char *opt_text
= option
->opt_text
;
287 && !option
->cl_reject_negative
288 && (opt_text
[1] == 'W' || opt_text
[1] == 'f' || opt_text
[1] == 'm'))
290 char *t
= XOBNEWVEC (&opts_obstack
, char, option
->opt_len
+ 5);
296 memcpy (t
+ 5, opt_text
+ 2, option
->opt_len
);
300 decoded
->canonical_option
[2] = NULL
;
301 decoded
->canonical_option
[3] = NULL
;
305 if ((option
->flags
& CL_SEPARATE
)
306 && !option
->cl_separate_alias
)
308 decoded
->canonical_option
[0] = opt_text
;
309 decoded
->canonical_option
[1] = arg
;
310 decoded
->canonical_option_num_elements
= 2;
314 gcc_assert (option
->flags
& CL_JOINED
);
315 decoded
->canonical_option
[0] = opts_concat (opt_text
, arg
, NULL
);
316 decoded
->canonical_option
[1] = NULL
;
317 decoded
->canonical_option_num_elements
= 1;
322 decoded
->canonical_option
[0] = opt_text
;
323 decoded
->canonical_option
[1] = NULL
;
324 decoded
->canonical_option_num_elements
= 1;
328 /* Structure describing mappings from options on the command line to
329 options to look up with find_opt. */
332 /* Prefix of the option on the command line. */
334 /* If two argv elements are considered to be merged into one option,
335 prefix for the second element, otherwise NULL. */
337 /* The new prefix to map to. */
338 const char *new_prefix
;
339 /* Whether at least one character is needed following opt1 or opt0
340 for this mapping to be used. (--optimize= is valid for -O, but
341 --warn- is not valid for -W.) */
342 bool another_char_needed
;
343 /* Whether the original option is a negated form of the option
344 resulting from this map. */
347 static const struct option_map option_map
[] =
349 { "-Wno-", NULL
, "-W", false, true },
350 { "-fno-", NULL
, "-f", false, true },
351 { "-mno-", NULL
, "-m", false, true },
352 { "--debug=", NULL
, "-g", false, false },
353 { "--machine-", NULL
, "-m", true, false },
354 { "--machine-no-", NULL
, "-m", false, true },
355 { "--machine=", NULL
, "-m", false, false },
356 { "--machine=no-", NULL
, "-m", false, true },
357 { "--machine", "", "-m", false, false },
358 { "--machine", "no-", "-m", false, true },
359 { "--optimize=", NULL
, "-O", false, false },
360 { "--std=", NULL
, "-std=", false, false },
361 { "--std", "", "-std=", false, false },
362 { "--warn-", NULL
, "-W", true, false },
363 { "--warn-no-", NULL
, "-W", false, true },
364 { "--", NULL
, "-f", true, false },
365 { "--no-", NULL
, "-f", false, true }
368 /* Helper function for gcc.c's driver::suggest_option, for populating the
369 vec of suggestions for misspelled options.
371 option_map above provides various prefixes for spelling command-line
372 options, which decode_cmdline_option uses to map spellings of options
373 to specific options. We want to do the reverse: to find all the ways
374 that a user could validly spell an option.
376 Given valid OPT_TEXT (with a leading dash), add it and all of its valid
377 variant spellings to CANDIDATES, each without a leading dash.
379 For example, given "-Wabi-tag", the following are added to CANDIDATES:
385 The added strings must be freed using free. */
388 add_misspelling_candidates (auto_vec
<char *> *candidates
,
389 const char *opt_text
)
391 gcc_assert (candidates
);
392 gcc_assert (opt_text
);
393 candidates
->safe_push (xstrdup (opt_text
+ 1));
394 for (unsigned i
= 0; i
< ARRAY_SIZE (option_map
); i
++)
396 const char *opt0
= option_map
[i
].opt0
;
397 const char *new_prefix
= option_map
[i
].new_prefix
;
398 size_t new_prefix_len
= strlen (new_prefix
);
400 if (strncmp (opt_text
, new_prefix
, new_prefix_len
) == 0)
402 char *alternative
= concat (opt0
+ 1, opt_text
+ new_prefix_len
,
404 candidates
->safe_push (alternative
);
409 /* Decode the switch beginning at ARGV for the language indicated by
410 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
411 the structure *DECODED. Returns the number of switches
415 decode_cmdline_option (const char **argv
, unsigned int lang_mask
,
416 struct cl_decoded_option
*decoded
)
421 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
425 const struct cl_option
*option
;
427 const char *warn_message
= NULL
;
428 bool separate_arg_flag
;
429 bool joined_arg_flag
;
430 bool have_separate_arg
= false;
434 opt_index
= find_opt (argv
[0] + 1, lang_mask
);
436 while (opt_index
== OPT_SPECIAL_unknown
437 && i
< ARRAY_SIZE (option_map
))
439 const char *opt0
= option_map
[i
].opt0
;
440 const char *opt1
= option_map
[i
].opt1
;
441 const char *new_prefix
= option_map
[i
].new_prefix
;
442 bool another_char_needed
= option_map
[i
].another_char_needed
;
443 size_t opt0_len
= strlen (opt0
);
444 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
445 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
446 size_t new_prefix_len
= strlen (new_prefix
);
448 extra_args
= (opt1
== NULL
? 0 : 1);
449 value
= !option_map
[i
].negated
;
451 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
453 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
454 && (!another_char_needed
455 || argv
[extra_args
][optn_len
] != 0))
457 size_t arglen
= strlen (argv
[extra_args
]);
460 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
461 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
462 memcpy (dup
, new_prefix
, new_prefix_len
);
463 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
464 arglen
- optn_len
+ 1);
465 opt_index
= find_opt (dup
+ 1, lang_mask
);
471 if (opt_index
== OPT_SPECIAL_unknown
)
479 option
= &cl_options
[opt_index
];
481 /* Reject negative form of switches that don't take negatives as
483 if (!value
&& option
->cl_reject_negative
)
485 opt_index
= OPT_SPECIAL_unknown
;
486 errors
|= CL_ERR_NEGATIVE
;
491 result
= extra_args
+ 1;
492 warn_message
= option
->warn_message
;
494 /* Check to see if the option is disabled for this configuration. */
495 if (option
->cl_disabled
)
496 errors
|= CL_ERR_DISABLED
;
498 /* Determine whether there may be a separate argument based on
499 whether this option is being processed for the driver, and, if
500 so, how many such arguments. */
501 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
502 && !(option
->cl_no_driver_arg
503 && (lang_mask
& CL_DRIVER
)));
504 separate_args
= (separate_arg_flag
505 ? option
->cl_separate_nargs
+ 1
507 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
509 /* Sort out any argument the switch takes. */
512 /* Have arg point to the original switch. This is because
513 some code, such as disable_builtin_function, expects its
514 argument to be persistent until the program exits. */
515 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
517 if (*arg
== '\0' && !option
->cl_missing_ok
)
519 if (separate_arg_flag
)
521 arg
= argv
[extra_args
+ 1];
522 result
= extra_args
+ 2;
524 result
= extra_args
+ 1;
526 have_separate_arg
= true;
529 /* Missing argument. */
533 else if (separate_arg_flag
)
535 arg
= argv
[extra_args
+ 1];
536 for (i
= 0; i
< separate_args
; i
++)
537 if (argv
[extra_args
+ 1 + i
] == NULL
)
539 errors
|= CL_ERR_MISSING_ARG
;
542 result
= extra_args
+ 1 + i
;
544 have_separate_arg
= true;
547 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
548 errors
|= CL_ERR_MISSING_ARG
;
550 /* Is this option an alias (or an ignored option, marked as an alias
551 of OPT_SPECIAL_ignore)? */
552 if (option
->alias_target
!= N_OPTS
553 && (!option
->cl_separate_alias
|| have_separate_arg
))
555 size_t new_opt_index
= option
->alias_target
;
557 if (new_opt_index
== OPT_SPECIAL_ignore
)
559 gcc_assert (option
->alias_arg
== NULL
);
560 gcc_assert (option
->neg_alias_arg
== NULL
);
561 opt_index
= new_opt_index
;
567 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
569 /* The new option must not be an alias itself. */
570 gcc_assert (new_option
->alias_target
== N_OPTS
571 || new_option
->cl_separate_alias
);
573 if (option
->neg_alias_arg
)
575 gcc_assert (option
->alias_arg
!= NULL
);
576 gcc_assert (arg
== NULL
);
577 gcc_assert (!option
->cl_negative_alias
);
579 arg
= option
->alias_arg
;
581 arg
= option
->neg_alias_arg
;
584 else if (option
->alias_arg
)
586 gcc_assert (value
== 1);
587 gcc_assert (arg
== NULL
);
588 gcc_assert (!option
->cl_negative_alias
);
589 arg
= option
->alias_arg
;
592 if (option
->cl_negative_alias
)
595 opt_index
= new_opt_index
;
599 gcc_assert (!option
->cl_reject_negative
);
601 /* Recompute what arguments are allowed. */
602 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
603 && !(option
->cl_no_driver_arg
604 && (lang_mask
& CL_DRIVER
)));
605 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
607 if (separate_args
> 1 || option
->cl_separate_nargs
)
608 gcc_assert (separate_args
609 == (unsigned int) option
->cl_separate_nargs
+ 1);
611 if (!(errors
& CL_ERR_MISSING_ARG
))
613 if (separate_arg_flag
|| joined_arg_flag
)
615 if (option
->cl_missing_ok
&& arg
== NULL
)
617 gcc_assert (arg
!= NULL
);
620 gcc_assert (arg
== NULL
);
623 /* Recheck for warnings and disabled options. */
624 if (option
->warn_message
)
626 gcc_assert (warn_message
== NULL
);
627 warn_message
= option
->warn_message
;
629 if (option
->cl_disabled
)
630 errors
|= CL_ERR_DISABLED
;
634 /* Check if this is a switch for a different front end. */
635 if (!option_ok_for_language (option
, lang_mask
))
636 errors
|= CL_ERR_WRONG_LANG
;
638 /* Convert the argument to lowercase if appropriate. */
639 if (arg
&& option
->cl_tolower
)
642 size_t len
= strlen (arg
);
643 char *arg_lower
= XOBNEWVEC (&opts_obstack
, char, len
+ 1);
645 for (j
= 0; j
< len
; j
++)
646 arg_lower
[j
] = TOLOWER ((unsigned char) arg
[j
]);
651 /* If the switch takes an integer, convert it. */
652 if (arg
&& option
->cl_uinteger
)
654 value
= integral_argument (arg
);
656 errors
|= CL_ERR_UINT_ARG
;
659 /* If the switch takes an enumerated argument, convert it. */
660 if (arg
&& (option
->var_type
== CLVC_ENUM
))
662 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
664 gcc_assert (value
== 1);
665 if (enum_arg_to_value (e
->values
, arg
, &value
, lang_mask
))
667 const char *carg
= NULL
;
669 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
671 gcc_assert (carg
!= NULL
);
674 errors
|= CL_ERR_ENUM_ARG
;
678 decoded
->opt_index
= opt_index
;
680 decoded
->value
= value
;
681 decoded
->errors
= errors
;
682 decoded
->warn_message
= warn_message
;
684 if (opt_index
== OPT_SPECIAL_unknown
)
685 gcc_assert (result
== 1);
687 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
688 decoded
->canonical_option_num_elements
= result
;
690 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
695 if (opt_index
== OPT_SPECIAL_unknown
)
696 decoded
->canonical_option
[i
] = argv
[i
];
698 decoded
->canonical_option
[i
] = NULL
;
699 len
= strlen (argv
[i
]);
700 /* If the argument is an empty string, we will print it as "" in
701 orig_option_with_args_text. */
702 total_len
+= (len
!= 0 ? len
: 2) + 1;
705 decoded
->canonical_option
[i
] = NULL
;
707 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
)
709 generate_canonical_option (opt_index
, arg
, value
, decoded
);
710 if (separate_args
> 1)
712 for (i
= 0; i
< separate_args
; i
++)
714 if (argv
[extra_args
+ 1 + i
] == NULL
)
717 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
719 gcc_assert (result
== 1 + i
);
720 decoded
->canonical_option_num_elements
= result
;
723 decoded
->orig_option_with_args_text
724 = p
= XOBNEWVEC (&opts_obstack
, char, total_len
);
725 for (i
= 0; i
< result
; i
++)
727 size_t len
= strlen (argv
[i
]);
729 /* Print the empty string verbally. */
736 memcpy (p
, argv
[i
], len
);
747 /* Obstack for option strings. */
749 struct obstack opts_obstack
;
751 /* Like libiberty concat, but allocate using opts_obstack. */
754 opts_concat (const char *first
, ...)
761 /* First compute the size of the result and get sufficient memory. */
762 va_start (ap
, first
);
763 for (arg
= first
; arg
; arg
= va_arg (ap
, const char *))
764 length
+= strlen (arg
);
765 newstr
= XOBNEWVEC (&opts_obstack
, char, length
+ 1);
768 /* Now copy the individual pieces to the result string. */
769 va_start (ap
, first
);
770 for (arg
= first
, end
= newstr
; arg
; arg
= va_arg (ap
, const char *))
772 length
= strlen (arg
);
773 memcpy (end
, arg
, length
);
781 /* Decode command-line options (ARGC and ARGV being the arguments of
782 main) into an array, setting *DECODED_OPTIONS to a pointer to that
783 array and *DECODED_OPTIONS_COUNT to the number of entries in the
784 array. The first entry in the array is always one for the program
785 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
786 flags applicable for decoding (including CL_COMMON and CL_TARGET if
787 those options should be considered applicable). Do not produce any
788 diagnostics or set state outside of these variables. */
791 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
792 unsigned int lang_mask
,
793 struct cl_decoded_option
**decoded_options
,
794 unsigned int *decoded_options_count
)
797 struct cl_decoded_option
*opt_array
;
798 unsigned int num_decoded_options
;
800 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
802 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
803 opt_array
[0].warn_message
= NULL
;
804 opt_array
[0].arg
= argv
[0];
805 opt_array
[0].orig_option_with_args_text
= argv
[0];
806 opt_array
[0].canonical_option_num_elements
= 1;
807 opt_array
[0].canonical_option
[0] = argv
[0];
808 opt_array
[0].canonical_option
[1] = NULL
;
809 opt_array
[0].canonical_option
[2] = NULL
;
810 opt_array
[0].canonical_option
[3] = NULL
;
811 opt_array
[0].value
= 1;
812 opt_array
[0].errors
= 0;
813 num_decoded_options
= 1;
815 for (i
= 1; i
< argc
; i
+= n
)
817 const char *opt
= argv
[i
];
819 /* Interpret "-" or a non-switch as a file name. */
820 if (opt
[0] != '-' || opt
[1] == '\0')
822 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
823 num_decoded_options
++;
828 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
829 &opt_array
[num_decoded_options
]);
830 num_decoded_options
++;
833 *decoded_options
= opt_array
;
834 *decoded_options_count
= num_decoded_options
;
835 prune_options (decoded_options
, decoded_options_count
);
838 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
839 next one is the same as ORIG_NEXT_OPT_IDX. */
842 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
844 /* An option can be canceled by the same option or an option with
846 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
849 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
850 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
856 /* Filter out options canceled by the ones after them. */
859 prune_options (struct cl_decoded_option
**decoded_options
,
860 unsigned int *decoded_options_count
)
862 unsigned int old_decoded_options_count
= *decoded_options_count
;
863 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
864 unsigned int new_decoded_options_count
;
865 struct cl_decoded_option
*new_decoded_options
866 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
868 const struct cl_option
*option
;
869 unsigned int fdiagnostics_color_idx
= 0;
871 /* Remove arguments which are negated by others after them. */
872 new_decoded_options_count
= 0;
873 for (i
= 0; i
< old_decoded_options_count
; i
++)
875 unsigned int j
, opt_idx
, next_opt_idx
;
877 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
880 opt_idx
= old_decoded_options
[i
].opt_index
;
883 case OPT_SPECIAL_unknown
:
884 case OPT_SPECIAL_ignore
:
885 case OPT_SPECIAL_program_name
:
886 case OPT_SPECIAL_input_file
:
889 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
890 case OPT_fdiagnostics_color_
:
891 fdiagnostics_color_idx
= i
;
895 gcc_assert (opt_idx
< cl_options_count
);
896 option
= &cl_options
[opt_idx
];
897 if (option
->neg_index
< 0)
900 /* Skip joined switches. */
901 if ((option
->flags
& CL_JOINED
))
904 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
906 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
908 next_opt_idx
= old_decoded_options
[j
].opt_index
;
909 if (next_opt_idx
>= cl_options_count
)
911 if (cl_options
[next_opt_idx
].neg_index
< 0)
913 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
))
915 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
918 if (j
== old_decoded_options_count
)
921 new_decoded_options
[new_decoded_options_count
]
922 = old_decoded_options
[i
];
923 new_decoded_options_count
++;
929 if (fdiagnostics_color_idx
>= 1)
931 /* We put the last -fdiagnostics-color= at the first position
932 after argv[0] so it can take effect immediately. */
933 memmove (new_decoded_options
+ 2, new_decoded_options
+ 1,
934 sizeof (struct cl_decoded_option
)
935 * (new_decoded_options_count
- 1));
936 new_decoded_options
[1] = old_decoded_options
[fdiagnostics_color_idx
];
937 new_decoded_options_count
++;
940 free (old_decoded_options
);
941 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
943 new_decoded_options_count
);
944 *decoded_options
= new_decoded_options
;
945 *decoded_options_count
= new_decoded_options_count
;
948 /* Handle option DECODED for the language indicated by LANG_MASK,
949 using the handlers in HANDLERS and setting fields in OPTS and
950 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
951 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
952 option for options from the source file, UNKNOWN_LOCATION
953 otherwise. GENERATED_P is true for an option generated as part of
954 processing another option or otherwise generated internally, false
955 for one explicitly passed by the user. Returns false if the switch
956 was invalid. DC is the diagnostic context for options affecting
957 diagnostics state, or NULL. */
960 handle_option (struct gcc_options
*opts
,
961 struct gcc_options
*opts_set
,
962 const struct cl_decoded_option
*decoded
,
963 unsigned int lang_mask
, int kind
, location_t loc
,
964 const struct cl_option_handlers
*handlers
,
965 bool generated_p
, diagnostic_context
*dc
)
967 size_t opt_index
= decoded
->opt_index
;
968 const char *arg
= decoded
->arg
;
969 int value
= decoded
->value
;
970 const struct cl_option
*option
= &cl_options
[opt_index
];
971 void *flag_var
= option_flag_var (opt_index
, opts
);
975 set_option (opts
, (generated_p
? NULL
: opts_set
),
976 opt_index
, value
, arg
, kind
, loc
, dc
);
978 for (i
= 0; i
< handlers
->num_handlers
; i
++)
979 if (option
->flags
& handlers
->handlers
[i
].mask
)
981 if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
982 lang_mask
, kind
, loc
,
990 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
991 option instead of DECODED. This is used for callbacks when one
992 option implies another instead of an option being decoded from the
996 handle_generated_option (struct gcc_options
*opts
,
997 struct gcc_options
*opts_set
,
998 size_t opt_index
, const char *arg
, int value
,
999 unsigned int lang_mask
, int kind
, location_t loc
,
1000 const struct cl_option_handlers
*handlers
,
1001 diagnostic_context
*dc
)
1003 struct cl_decoded_option decoded
;
1005 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
1006 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
1007 handlers
, true, dc
);
1010 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1011 VALUE for a front end using LANG_MASK. This is used when the
1012 compiler generates options internally. */
1015 generate_option (size_t opt_index
, const char *arg
, int value
,
1016 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
1018 const struct cl_option
*option
= &cl_options
[opt_index
];
1020 decoded
->opt_index
= opt_index
;
1021 decoded
->warn_message
= NULL
;
1023 decoded
->value
= value
;
1024 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
1026 : CL_ERR_WRONG_LANG
);
1028 generate_canonical_option (opt_index
, arg
, value
, decoded
);
1029 switch (decoded
->canonical_option_num_elements
)
1032 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
1036 decoded
->orig_option_with_args_text
1037 = opts_concat (decoded
->canonical_option
[0], " ",
1038 decoded
->canonical_option
[1], NULL
);
1046 /* Fill in *DECODED with an option for input file FILE. */
1049 generate_option_input_file (const char *file
,
1050 struct cl_decoded_option
*decoded
)
1052 decoded
->opt_index
= OPT_SPECIAL_input_file
;
1053 decoded
->warn_message
= NULL
;
1054 decoded
->arg
= file
;
1055 decoded
->orig_option_with_args_text
= file
;
1056 decoded
->canonical_option_num_elements
= 1;
1057 decoded
->canonical_option
[0] = file
;
1058 decoded
->canonical_option
[1] = NULL
;
1059 decoded
->canonical_option
[2] = NULL
;
1060 decoded
->canonical_option
[3] = NULL
;
1062 decoded
->errors
= 0;
1065 /* Perform diagnostics for read_cmdline_option and control_warning_option
1066 functions. Returns true if an error has been diagnosed.
1067 LOC and LANG_MASK arguments like in read_cmdline_option.
1068 OPTION is the option to report diagnostics for, OPT the name
1069 of the option as text, ARG the argument of the option (for joined
1070 options), ERRORS is bitmask of CL_ERR_* values. */
1073 cmdline_handle_error (location_t loc
, const struct cl_option
*option
,
1074 const char *opt
, const char *arg
, int errors
,
1075 unsigned int lang_mask
)
1077 if (errors
& CL_ERR_DISABLED
)
1079 error_at (loc
, "command line option %qs"
1080 " is not supported by this configuration", opt
);
1084 if (errors
& CL_ERR_MISSING_ARG
)
1086 if (option
->missing_argument_error
)
1087 error_at (loc
, option
->missing_argument_error
, opt
);
1089 error_at (loc
, "missing argument to %qs", opt
);
1093 if (errors
& CL_ERR_UINT_ARG
)
1095 error_at (loc
, "argument to %qs should be a non-negative integer",
1100 if (errors
& CL_ERR_ENUM_ARG
)
1102 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1107 if (e
->unknown_error
)
1108 error_at (loc
, e
->unknown_error
, arg
);
1110 error_at (loc
, "unrecognized argument in option %qs", opt
);
1113 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1114 len
+= strlen (e
->values
[i
].arg
) + 1;
1116 s
= XALLOCAVEC (char, len
);
1118 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1120 if (!enum_arg_ok_for_language (&e
->values
[i
], lang_mask
))
1122 size_t arglen
= strlen (e
->values
[i
].arg
);
1123 memcpy (p
, e
->values
[i
].arg
, arglen
);
1128 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1135 /* Handle the switch DECODED (location LOC) for the language indicated
1136 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1137 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1138 diagnostic options. */
1141 read_cmdline_option (struct gcc_options
*opts
,
1142 struct gcc_options
*opts_set
,
1143 struct cl_decoded_option
*decoded
,
1145 unsigned int lang_mask
,
1146 const struct cl_option_handlers
*handlers
,
1147 diagnostic_context
*dc
)
1149 const struct cl_option
*option
;
1150 const char *opt
= decoded
->orig_option_with_args_text
;
1152 if (decoded
->warn_message
)
1153 warning_at (loc
, 0, decoded
->warn_message
, opt
);
1155 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
1157 if (handlers
->unknown_option_callback (decoded
))
1158 error_at (loc
, "unrecognized command line option %qs", decoded
->arg
);
1162 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
1165 option
= &cl_options
[decoded
->opt_index
];
1168 && cmdline_handle_error (loc
, option
, opt
, decoded
->arg
,
1169 decoded
->errors
, lang_mask
))
1172 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
1174 handlers
->wrong_lang_callback (decoded
, lang_mask
);
1178 gcc_assert (!decoded
->errors
);
1180 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1181 loc
, handlers
, false, dc
))
1182 error_at (loc
, "unrecognized command line option %qs", opt
);
1185 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1186 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1187 location LOC, using diagnostic context DC if not NULL for
1188 diagnostic classification. */
1191 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1192 int opt_index
, int value
, const char *arg
, int kind
,
1193 location_t loc
, diagnostic_context
*dc
)
1195 const struct cl_option
*option
= &cl_options
[opt_index
];
1196 void *flag_var
= option_flag_var (opt_index
, opts
);
1197 void *set_flag_var
= NULL
;
1202 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
&& dc
!= NULL
)
1203 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1205 if (opts_set
!= NULL
)
1206 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1208 switch (option
->var_type
)
1211 *(int *) flag_var
= value
;
1213 *(int *) set_flag_var
= 1;
1217 if (option
->cl_host_wide_int
)
1218 *(HOST_WIDE_INT
*) flag_var
= (value
1220 : !option
->var_value
);
1222 *(int *) flag_var
= (value
1224 : !option
->var_value
);
1226 *(int *) set_flag_var
= 1;
1229 case CLVC_BIT_CLEAR
:
1231 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1233 if (option
->cl_host_wide_int
)
1234 *(HOST_WIDE_INT
*) flag_var
|= option
->var_value
;
1236 *(int *) flag_var
|= option
->var_value
;
1240 if (option
->cl_host_wide_int
)
1241 *(HOST_WIDE_INT
*) flag_var
&= ~option
->var_value
;
1243 *(int *) flag_var
&= ~option
->var_value
;
1247 if (option
->cl_host_wide_int
)
1248 *(HOST_WIDE_INT
*) set_flag_var
|= option
->var_value
;
1250 *(int *) set_flag_var
|= option
->var_value
;
1255 *(const char **) flag_var
= arg
;
1257 *(const char **) set_flag_var
= "";
1262 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1264 e
->set (flag_var
, value
);
1266 e
->set (set_flag_var
, 1);
1272 vec
<cl_deferred_option
> *v
1273 = (vec
<cl_deferred_option
> *) *(void **) flag_var
;
1274 cl_deferred_option p
= {opt_index
, arg
, value
};
1276 v
= XCNEW (vec
<cl_deferred_option
>);
1278 *(void **) flag_var
= v
;
1280 *(void **) set_flag_var
= v
;
1286 /* Return the address of the flag variable for option OPT_INDEX in
1287 options structure OPTS, or NULL if there is no flag variable. */
1290 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1292 const struct cl_option
*option
= &cl_options
[opt_index
];
1294 if (option
->flag_var_offset
== (unsigned short) -1)
1296 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1299 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1300 or -1 if it isn't a simple on-off switch. */
1303 option_enabled (int opt_idx
, void *opts
)
1305 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1306 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1307 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1310 switch (option
->var_type
)
1313 return *(int *) flag_var
!= 0;
1316 if (option
->cl_host_wide_int
)
1317 return *(HOST_WIDE_INT
*) flag_var
== option
->var_value
;
1319 return *(int *) flag_var
== option
->var_value
;
1321 case CLVC_BIT_CLEAR
:
1322 if (option
->cl_host_wide_int
)
1323 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) == 0;
1325 return (*(int *) flag_var
& option
->var_value
) == 0;
1328 if (option
->cl_host_wide_int
)
1329 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) != 0;
1331 return (*(int *) flag_var
& option
->var_value
) != 0;
1341 /* Fill STATE with the current state of option OPTION in OPTS. Return
1342 true if there is some state to store. */
1345 get_option_state (struct gcc_options
*opts
, int option
,
1346 struct cl_option_state
*state
)
1348 void *flag_var
= option_flag_var (option
, opts
);
1353 switch (cl_options
[option
].var_type
)
1357 state
->data
= flag_var
;
1358 state
->size
= (cl_options
[option
].cl_host_wide_int
1359 ? sizeof (HOST_WIDE_INT
)
1363 case CLVC_BIT_CLEAR
:
1365 state
->ch
= option_enabled (option
, opts
);
1366 state
->data
= &state
->ch
;
1371 state
->data
= *(const char **) flag_var
;
1372 if (state
->data
== 0)
1374 state
->size
= strlen ((const char *) state
->data
) + 1;
1378 state
->data
= flag_var
;
1379 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1388 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1389 handlers HANDLERS) to have diagnostic kind KIND for option
1390 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1391 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1392 argument of the option for joined options, or NULL otherwise. If IMPLY,
1393 the warning option in question is implied at this point. This is
1394 used by -Werror= and #pragma GCC diagnostic. */
1397 control_warning_option (unsigned int opt_index
, int kind
, const char *arg
,
1398 bool imply
, location_t loc
, unsigned int lang_mask
,
1399 const struct cl_option_handlers
*handlers
,
1400 struct gcc_options
*opts
,
1401 struct gcc_options
*opts_set
,
1402 diagnostic_context
*dc
)
1404 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1406 gcc_assert (!cl_options
[opt_index
].cl_separate_alias
1407 && !cl_options
[opt_index
].cl_negative_alias
);
1408 if (cl_options
[opt_index
].alias_arg
)
1409 arg
= cl_options
[opt_index
].alias_arg
;
1410 opt_index
= cl_options
[opt_index
].alias_target
;
1412 if (opt_index
== OPT_SPECIAL_ignore
)
1415 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1418 const struct cl_option
*option
= &cl_options
[opt_index
];
1420 /* -Werror=foo implies -Wfoo. */
1421 if (option
->var_type
== CLVC_BOOLEAN
|| option
->var_type
== CLVC_ENUM
)
1425 if (arg
&& *arg
== '\0' && !option
->cl_missing_ok
)
1428 if ((option
->flags
& CL_JOINED
) && arg
== NULL
)
1430 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1431 CL_ERR_MISSING_ARG
, lang_mask
);
1435 /* If the switch takes an integer, convert it. */
1436 if (arg
&& option
->cl_uinteger
)
1438 value
= integral_argument (arg
);
1441 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1442 CL_ERR_UINT_ARG
, lang_mask
);
1447 /* If the switch takes an enumerated argument, convert it. */
1448 if (arg
&& option
->var_type
== CLVC_ENUM
)
1450 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1452 if (enum_arg_to_value (e
->values
, arg
, &value
, lang_mask
))
1454 const char *carg
= NULL
;
1456 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
1458 gcc_assert (carg
!= NULL
);
1462 cmdline_handle_error (loc
, option
, option
->opt_text
, arg
,
1463 CL_ERR_ENUM_ARG
, lang_mask
);
1468 handle_generated_option (opts
, opts_set
,
1469 opt_index
, arg
, value
, lang_mask
,
1470 kind
, loc
, handlers
, dc
);