1 /* Command line option handling.
2 Copyright (C) 2006-2014 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 /* Decode the switch beginning at ARGV for the language indicated by
369 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
370 the structure *DECODED. Returns the number of switches
374 decode_cmdline_option (const char **argv
, unsigned int lang_mask
,
375 struct cl_decoded_option
*decoded
)
380 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
384 const struct cl_option
*option
;
386 const char *warn_message
= NULL
;
387 bool separate_arg_flag
;
388 bool joined_arg_flag
;
389 bool have_separate_arg
= false;
393 opt_index
= find_opt (argv
[0] + 1, lang_mask
);
395 while (opt_index
== OPT_SPECIAL_unknown
396 && i
< ARRAY_SIZE (option_map
))
398 const char *opt0
= option_map
[i
].opt0
;
399 const char *opt1
= option_map
[i
].opt1
;
400 const char *new_prefix
= option_map
[i
].new_prefix
;
401 bool another_char_needed
= option_map
[i
].another_char_needed
;
402 size_t opt0_len
= strlen (opt0
);
403 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
404 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
405 size_t new_prefix_len
= strlen (new_prefix
);
407 extra_args
= (opt1
== NULL
? 0 : 1);
408 value
= !option_map
[i
].negated
;
410 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
412 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
413 && (!another_char_needed
414 || argv
[extra_args
][optn_len
] != 0))
416 size_t arglen
= strlen (argv
[extra_args
]);
419 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
420 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
421 memcpy (dup
, new_prefix
, new_prefix_len
);
422 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
423 arglen
- optn_len
+ 1);
424 opt_index
= find_opt (dup
+ 1, lang_mask
);
430 if (opt_index
== OPT_SPECIAL_unknown
)
438 option
= &cl_options
[opt_index
];
440 /* Reject negative form of switches that don't take negatives as
442 if (!value
&& option
->cl_reject_negative
)
444 opt_index
= OPT_SPECIAL_unknown
;
445 errors
|= CL_ERR_NEGATIVE
;
450 result
= extra_args
+ 1;
451 warn_message
= option
->warn_message
;
453 /* Check to see if the option is disabled for this configuration. */
454 if (option
->cl_disabled
)
455 errors
|= CL_ERR_DISABLED
;
457 /* Determine whether there may be a separate argument based on
458 whether this option is being processed for the driver, and, if
459 so, how many such arguments. */
460 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
461 && !(option
->cl_no_driver_arg
462 && (lang_mask
& CL_DRIVER
)));
463 separate_args
= (separate_arg_flag
464 ? option
->cl_separate_nargs
+ 1
466 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
468 /* Sort out any argument the switch takes. */
471 /* Have arg point to the original switch. This is because
472 some code, such as disable_builtin_function, expects its
473 argument to be persistent until the program exits. */
474 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
476 if (*arg
== '\0' && !option
->cl_missing_ok
)
478 if (separate_arg_flag
)
480 arg
= argv
[extra_args
+ 1];
481 result
= extra_args
+ 2;
483 result
= extra_args
+ 1;
485 have_separate_arg
= true;
488 /* Missing argument. */
492 else if (separate_arg_flag
)
494 arg
= argv
[extra_args
+ 1];
495 for (i
= 0; i
< separate_args
; i
++)
496 if (argv
[extra_args
+ 1 + i
] == NULL
)
498 errors
|= CL_ERR_MISSING_ARG
;
501 result
= extra_args
+ 1 + i
;
503 have_separate_arg
= true;
506 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
507 errors
|= CL_ERR_MISSING_ARG
;
509 /* Is this option an alias (or an ignored option, marked as an alias
510 of OPT_SPECIAL_ignore)? */
511 if (option
->alias_target
!= N_OPTS
512 && (!option
->cl_separate_alias
|| have_separate_arg
))
514 size_t new_opt_index
= option
->alias_target
;
516 if (new_opt_index
== OPT_SPECIAL_ignore
)
518 gcc_assert (option
->alias_arg
== NULL
);
519 gcc_assert (option
->neg_alias_arg
== NULL
);
520 opt_index
= new_opt_index
;
526 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
528 /* The new option must not be an alias itself. */
529 gcc_assert (new_option
->alias_target
== N_OPTS
530 || new_option
->cl_separate_alias
);
532 if (option
->neg_alias_arg
)
534 gcc_assert (option
->alias_arg
!= NULL
);
535 gcc_assert (arg
== NULL
);
536 gcc_assert (!option
->cl_negative_alias
);
538 arg
= option
->alias_arg
;
540 arg
= option
->neg_alias_arg
;
543 else if (option
->alias_arg
)
545 gcc_assert (value
== 1);
546 gcc_assert (arg
== NULL
);
547 gcc_assert (!option
->cl_negative_alias
);
548 arg
= option
->alias_arg
;
551 if (option
->cl_negative_alias
)
554 opt_index
= new_opt_index
;
558 gcc_assert (!option
->cl_reject_negative
);
560 /* Recompute what arguments are allowed. */
561 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
562 && !(option
->cl_no_driver_arg
563 && (lang_mask
& CL_DRIVER
)));
564 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
566 if (separate_args
> 1 || option
->cl_separate_nargs
)
567 gcc_assert (separate_args
568 == (unsigned int) option
->cl_separate_nargs
+ 1);
570 if (!(errors
& CL_ERR_MISSING_ARG
))
572 if (separate_arg_flag
|| joined_arg_flag
)
574 if (option
->cl_missing_ok
&& arg
== NULL
)
576 gcc_assert (arg
!= NULL
);
579 gcc_assert (arg
== NULL
);
582 /* Recheck for warnings and disabled options. */
583 if (option
->warn_message
)
585 gcc_assert (warn_message
== NULL
);
586 warn_message
= option
->warn_message
;
588 if (option
->cl_disabled
)
589 errors
|= CL_ERR_DISABLED
;
593 /* Check if this is a switch for a different front end. */
594 if (!option_ok_for_language (option
, lang_mask
))
595 errors
|= CL_ERR_WRONG_LANG
;
597 /* Convert the argument to lowercase if appropriate. */
598 if (arg
&& option
->cl_tolower
)
601 size_t len
= strlen (arg
);
602 char *arg_lower
= XOBNEWVEC (&opts_obstack
, char, len
+ 1);
604 for (j
= 0; j
< len
; j
++)
605 arg_lower
[j
] = TOLOWER ((unsigned char) arg
[j
]);
610 /* If the switch takes an integer, convert it. */
611 if (arg
&& option
->cl_uinteger
)
613 value
= integral_argument (arg
);
615 errors
|= CL_ERR_UINT_ARG
;
618 /* If the switch takes an enumerated argument, convert it. */
619 if (arg
&& (option
->var_type
== CLVC_ENUM
))
621 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
623 gcc_assert (value
== 1);
624 if (enum_arg_to_value (e
->values
, arg
, &value
, lang_mask
))
626 const char *carg
= NULL
;
628 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
630 gcc_assert (carg
!= NULL
);
633 errors
|= CL_ERR_ENUM_ARG
;
637 decoded
->opt_index
= opt_index
;
639 decoded
->value
= value
;
640 decoded
->errors
= errors
;
641 decoded
->warn_message
= warn_message
;
643 if (opt_index
== OPT_SPECIAL_unknown
)
644 gcc_assert (result
== 1);
646 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
647 decoded
->canonical_option_num_elements
= result
;
649 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
654 if (opt_index
== OPT_SPECIAL_unknown
)
655 decoded
->canonical_option
[i
] = argv
[i
];
657 decoded
->canonical_option
[i
] = NULL
;
658 len
= strlen (argv
[i
]);
659 /* If the argument is an empty string, we will print it as "" in
660 orig_option_with_args_text. */
661 total_len
+= (len
!= 0 ? len
: 2) + 1;
664 decoded
->canonical_option
[i
] = NULL
;
666 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
)
668 generate_canonical_option (opt_index
, arg
, value
, decoded
);
669 if (separate_args
> 1)
671 for (i
= 0; i
< separate_args
; i
++)
673 if (argv
[extra_args
+ 1 + i
] == NULL
)
676 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
678 gcc_assert (result
== 1 + i
);
679 decoded
->canonical_option_num_elements
= result
;
682 decoded
->orig_option_with_args_text
683 = p
= XOBNEWVEC (&opts_obstack
, char, total_len
);
684 for (i
= 0; i
< result
; i
++)
686 size_t len
= strlen (argv
[i
]);
688 /* Print the empty string verbally. */
695 memcpy (p
, argv
[i
], len
);
706 /* Obstack for option strings. */
708 struct obstack opts_obstack
;
710 /* Like libiberty concat, but allocate using opts_obstack. */
713 opts_concat (const char *first
, ...)
720 /* First compute the size of the result and get sufficient memory. */
721 va_start (ap
, first
);
722 for (arg
= first
; arg
; arg
= va_arg (ap
, const char *))
723 length
+= strlen (arg
);
724 newstr
= XOBNEWVEC (&opts_obstack
, char, length
+ 1);
727 /* Now copy the individual pieces to the result string. */
728 va_start (ap
, first
);
729 for (arg
= first
, end
= newstr
; arg
; arg
= va_arg (ap
, const char *))
731 length
= strlen (arg
);
732 memcpy (end
, arg
, length
);
740 /* Decode command-line options (ARGC and ARGV being the arguments of
741 main) into an array, setting *DECODED_OPTIONS to a pointer to that
742 array and *DECODED_OPTIONS_COUNT to the number of entries in the
743 array. The first entry in the array is always one for the program
744 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
745 flags applicable for decoding (including CL_COMMON and CL_TARGET if
746 those options should be considered applicable). Do not produce any
747 diagnostics or set state outside of these variables. */
750 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
751 unsigned int lang_mask
,
752 struct cl_decoded_option
**decoded_options
,
753 unsigned int *decoded_options_count
)
756 struct cl_decoded_option
*opt_array
;
757 unsigned int num_decoded_options
;
759 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
761 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
762 opt_array
[0].warn_message
= NULL
;
763 opt_array
[0].arg
= argv
[0];
764 opt_array
[0].orig_option_with_args_text
= argv
[0];
765 opt_array
[0].canonical_option_num_elements
= 1;
766 opt_array
[0].canonical_option
[0] = argv
[0];
767 opt_array
[0].canonical_option
[1] = NULL
;
768 opt_array
[0].canonical_option
[2] = NULL
;
769 opt_array
[0].canonical_option
[3] = NULL
;
770 opt_array
[0].value
= 1;
771 opt_array
[0].errors
= 0;
772 num_decoded_options
= 1;
774 for (i
= 1; i
< argc
; i
+= n
)
776 const char *opt
= argv
[i
];
778 /* Interpret "-" or a non-switch as a file name. */
779 if (opt
[0] != '-' || opt
[1] == '\0')
781 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
782 num_decoded_options
++;
787 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
788 &opt_array
[num_decoded_options
]);
789 num_decoded_options
++;
792 *decoded_options
= opt_array
;
793 *decoded_options_count
= num_decoded_options
;
794 prune_options (decoded_options
, decoded_options_count
);
797 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
798 next one is the same as ORIG_NEXT_OPT_IDX. */
801 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
803 /* An option can be canceled by the same option or an option with
805 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
808 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
809 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
815 /* Filter out options canceled by the ones after them. */
818 prune_options (struct cl_decoded_option
**decoded_options
,
819 unsigned int *decoded_options_count
)
821 unsigned int old_decoded_options_count
= *decoded_options_count
;
822 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
823 unsigned int new_decoded_options_count
;
824 struct cl_decoded_option
*new_decoded_options
825 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
827 const struct cl_option
*option
;
829 /* Remove arguments which are negated by others after them. */
830 new_decoded_options_count
= 0;
831 for (i
= 0; i
< old_decoded_options_count
; i
++)
833 unsigned int j
, opt_idx
, next_opt_idx
;
835 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
838 opt_idx
= old_decoded_options
[i
].opt_index
;
841 case OPT_SPECIAL_unknown
:
842 case OPT_SPECIAL_ignore
:
843 case OPT_SPECIAL_program_name
:
844 case OPT_SPECIAL_input_file
:
848 gcc_assert (opt_idx
< cl_options_count
);
849 option
= &cl_options
[opt_idx
];
850 if (option
->neg_index
< 0)
853 /* Skip joined switches. */
854 if ((option
->flags
& CL_JOINED
))
857 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
859 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
861 next_opt_idx
= old_decoded_options
[j
].opt_index
;
862 if (next_opt_idx
>= cl_options_count
)
864 if (cl_options
[next_opt_idx
].neg_index
< 0)
866 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
))
868 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
871 if (j
== old_decoded_options_count
)
874 new_decoded_options
[new_decoded_options_count
]
875 = old_decoded_options
[i
];
876 new_decoded_options_count
++;
882 free (old_decoded_options
);
883 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
885 new_decoded_options_count
);
886 *decoded_options
= new_decoded_options
;
887 *decoded_options_count
= new_decoded_options_count
;
890 /* Handle option DECODED for the language indicated by LANG_MASK,
891 using the handlers in HANDLERS and setting fields in OPTS and
892 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
893 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
894 option for options from the source file, UNKNOWN_LOCATION
895 otherwise. GENERATED_P is true for an option generated as part of
896 processing another option or otherwise generated internally, false
897 for one explicitly passed by the user. Returns false if the switch
898 was invalid. DC is the diagnostic context for options affecting
899 diagnostics state, or NULL. */
902 handle_option (struct gcc_options
*opts
,
903 struct gcc_options
*opts_set
,
904 const struct cl_decoded_option
*decoded
,
905 unsigned int lang_mask
, int kind
, location_t loc
,
906 const struct cl_option_handlers
*handlers
,
907 bool generated_p
, diagnostic_context
*dc
)
909 size_t opt_index
= decoded
->opt_index
;
910 const char *arg
= decoded
->arg
;
911 int value
= decoded
->value
;
912 const struct cl_option
*option
= &cl_options
[opt_index
];
913 void *flag_var
= option_flag_var (opt_index
, opts
);
917 set_option (opts
, (generated_p
? NULL
: opts_set
),
918 opt_index
, value
, arg
, kind
, loc
, dc
);
920 for (i
= 0; i
< handlers
->num_handlers
; i
++)
921 if (option
->flags
& handlers
->handlers
[i
].mask
)
923 if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
924 lang_mask
, kind
, loc
,
932 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
933 option instead of DECODED. This is used for callbacks when one
934 option implies another instead of an option being decoded from the
938 handle_generated_option (struct gcc_options
*opts
,
939 struct gcc_options
*opts_set
,
940 size_t opt_index
, const char *arg
, int value
,
941 unsigned int lang_mask
, int kind
, location_t loc
,
942 const struct cl_option_handlers
*handlers
,
943 diagnostic_context
*dc
)
945 struct cl_decoded_option decoded
;
947 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
948 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
952 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
953 VALUE for a front end using LANG_MASK. This is used when the
954 compiler generates options internally. */
957 generate_option (size_t opt_index
, const char *arg
, int value
,
958 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
960 const struct cl_option
*option
= &cl_options
[opt_index
];
962 decoded
->opt_index
= opt_index
;
963 decoded
->warn_message
= NULL
;
965 decoded
->value
= value
;
966 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
968 : CL_ERR_WRONG_LANG
);
970 generate_canonical_option (opt_index
, arg
, value
, decoded
);
971 switch (decoded
->canonical_option_num_elements
)
974 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
978 decoded
->orig_option_with_args_text
979 = opts_concat (decoded
->canonical_option
[0], " ",
980 decoded
->canonical_option
[1], NULL
);
988 /* Fill in *DECODED with an option for input file FILE. */
991 generate_option_input_file (const char *file
,
992 struct cl_decoded_option
*decoded
)
994 decoded
->opt_index
= OPT_SPECIAL_input_file
;
995 decoded
->warn_message
= NULL
;
997 decoded
->orig_option_with_args_text
= file
;
998 decoded
->canonical_option_num_elements
= 1;
999 decoded
->canonical_option
[0] = file
;
1000 decoded
->canonical_option
[1] = NULL
;
1001 decoded
->canonical_option
[2] = NULL
;
1002 decoded
->canonical_option
[3] = NULL
;
1004 decoded
->errors
= 0;
1007 /* Handle the switch DECODED (location LOC) for the language indicated
1008 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1009 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1010 diagnostic options. */
1013 read_cmdline_option (struct gcc_options
*opts
,
1014 struct gcc_options
*opts_set
,
1015 struct cl_decoded_option
*decoded
,
1017 unsigned int lang_mask
,
1018 const struct cl_option_handlers
*handlers
,
1019 diagnostic_context
*dc
)
1021 const struct cl_option
*option
;
1022 const char *opt
= decoded
->orig_option_with_args_text
;
1024 if (decoded
->warn_message
)
1025 warning_at (loc
, 0, decoded
->warn_message
, opt
);
1027 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
1029 if (handlers
->unknown_option_callback (decoded
))
1030 error_at (loc
, "unrecognized command line option %qs", decoded
->arg
);
1034 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
1037 option
= &cl_options
[decoded
->opt_index
];
1039 if (decoded
->errors
& CL_ERR_DISABLED
)
1041 error_at (loc
, "command line option %qs"
1042 " is not supported by this configuration", opt
);
1046 if (decoded
->errors
& CL_ERR_MISSING_ARG
)
1048 if (option
->missing_argument_error
)
1049 error_at (loc
, option
->missing_argument_error
, opt
);
1051 error_at (loc
, "missing argument to %qs", opt
);
1055 if (decoded
->errors
& CL_ERR_UINT_ARG
)
1057 error_at (loc
, "argument to %qs should be a non-negative integer",
1062 if (decoded
->errors
& CL_ERR_ENUM_ARG
)
1064 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1069 if (e
->unknown_error
)
1070 error_at (loc
, e
->unknown_error
, decoded
->arg
);
1072 error_at (loc
, "unrecognized argument in option %qs", opt
);
1075 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1076 len
+= strlen (e
->values
[i
].arg
) + 1;
1078 s
= XALLOCAVEC (char, len
);
1080 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1082 size_t arglen
= strlen (e
->values
[i
].arg
);
1083 memcpy (p
, e
->values
[i
].arg
, arglen
);
1088 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1092 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
1094 handlers
->wrong_lang_callback (decoded
, lang_mask
);
1098 gcc_assert (!decoded
->errors
);
1100 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1101 loc
, handlers
, false, dc
))
1102 error_at (loc
, "unrecognized command line option %qs", opt
);
1105 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1106 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1107 location LOC, using diagnostic context DC if not NULL for
1108 diagnostic classification. */
1111 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1112 int opt_index
, int value
, const char *arg
, int kind
,
1113 location_t loc
, diagnostic_context
*dc
)
1115 const struct cl_option
*option
= &cl_options
[opt_index
];
1116 void *flag_var
= option_flag_var (opt_index
, opts
);
1117 void *set_flag_var
= NULL
;
1122 if (opts_set
!= NULL
)
1123 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1125 switch (option
->var_type
)
1128 *(int *) flag_var
= value
;
1130 *(int *) set_flag_var
= 1;
1134 if (option
->cl_host_wide_int
)
1135 *(HOST_WIDE_INT
*) flag_var
= (value
1137 : !option
->var_value
);
1139 *(int *) flag_var
= (value
1141 : !option
->var_value
);
1143 *(int *) set_flag_var
= 1;
1146 case CLVC_BIT_CLEAR
:
1148 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1150 if (option
->cl_host_wide_int
)
1151 *(HOST_WIDE_INT
*) flag_var
|= option
->var_value
;
1153 *(int *) flag_var
|= option
->var_value
;
1157 if (option
->cl_host_wide_int
)
1158 *(HOST_WIDE_INT
*) flag_var
&= ~option
->var_value
;
1160 *(int *) flag_var
&= ~option
->var_value
;
1164 if (option
->cl_host_wide_int
)
1165 *(HOST_WIDE_INT
*) set_flag_var
|= option
->var_value
;
1167 *(int *) set_flag_var
|= option
->var_value
;
1172 *(const char **) flag_var
= arg
;
1174 *(const char **) set_flag_var
= "";
1179 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1181 e
->set (flag_var
, value
);
1183 e
->set (set_flag_var
, 1);
1189 vec
<cl_deferred_option
> *v
1190 = (vec
<cl_deferred_option
> *) *(void **) flag_var
;
1191 cl_deferred_option p
= {opt_index
, arg
, value
};
1193 v
= XCNEW (vec
<cl_deferred_option
>);
1195 *(void **) flag_var
= v
;
1197 *(void **) set_flag_var
= v
;
1202 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
1204 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1207 /* Return the address of the flag variable for option OPT_INDEX in
1208 options structure OPTS, or NULL if there is no flag variable. */
1211 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1213 const struct cl_option
*option
= &cl_options
[opt_index
];
1215 if (option
->flag_var_offset
== (unsigned short) -1)
1217 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1220 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1221 or -1 if it isn't a simple on-off switch. */
1224 option_enabled (int opt_idx
, void *opts
)
1226 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1227 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1228 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1231 switch (option
->var_type
)
1234 return *(int *) flag_var
!= 0;
1237 if (option
->cl_host_wide_int
)
1238 return *(HOST_WIDE_INT
*) flag_var
== option
->var_value
;
1240 return *(int *) flag_var
== option
->var_value
;
1242 case CLVC_BIT_CLEAR
:
1243 if (option
->cl_host_wide_int
)
1244 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) == 0;
1246 return (*(int *) flag_var
& option
->var_value
) == 0;
1249 if (option
->cl_host_wide_int
)
1250 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) != 0;
1252 return (*(int *) flag_var
& option
->var_value
) != 0;
1262 /* Fill STATE with the current state of option OPTION in OPTS. Return
1263 true if there is some state to store. */
1266 get_option_state (struct gcc_options
*opts
, int option
,
1267 struct cl_option_state
*state
)
1269 void *flag_var
= option_flag_var (option
, opts
);
1274 switch (cl_options
[option
].var_type
)
1278 state
->data
= flag_var
;
1279 state
->size
= (cl_options
[option
].cl_host_wide_int
1280 ? sizeof (HOST_WIDE_INT
)
1284 case CLVC_BIT_CLEAR
:
1286 state
->ch
= option_enabled (option
, opts
);
1287 state
->data
= &state
->ch
;
1292 state
->data
= *(const char **) flag_var
;
1293 if (state
->data
== 0)
1295 state
->size
= strlen ((const char *) state
->data
) + 1;
1299 state
->data
= flag_var
;
1300 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1309 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1310 handlers HANDLERS) to have diagnostic kind KIND for option
1311 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1312 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1313 the warning option in question is implied at this point. This is
1314 used by -Werror= and #pragma GCC diagnostic. */
1317 control_warning_option (unsigned int opt_index
, int kind
, bool imply
,
1318 location_t loc
, unsigned int lang_mask
,
1319 const struct cl_option_handlers
*handlers
,
1320 struct gcc_options
*opts
,
1321 struct gcc_options
*opts_set
,
1322 diagnostic_context
*dc
)
1324 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1325 opt_index
= cl_options
[opt_index
].alias_target
;
1326 if (opt_index
== OPT_SPECIAL_ignore
)
1329 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1332 /* -Werror=foo implies -Wfoo. */
1333 if (cl_options
[opt_index
].var_type
== CLVC_BOOLEAN
)
1334 handle_generated_option (opts
, opts_set
,
1335 opt_index
, NULL
, 1, lang_mask
,
1336 kind
, loc
, handlers
, dc
);