1 /* Command line option handling.
2 Copyright (C) 2006, 2007, 2008, 2010, 2011, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "diagnostic.h"
29 static void prune_options (struct cl_decoded_option
**, unsigned int *);
31 /* Perform a binary search to find which option the command-line INPUT
32 matches. Returns its index in the option array, and
33 OPT_SPECIAL_unknown on failure.
35 This routine is quite subtle. A normal binary search is not good
36 enough because some options can be suffixed with an argument, and
37 multiple sub-matches can occur, e.g. input of "-pedantic" matching
38 the initial substring of "-pedantic-errors".
40 A more complicated example is -gstabs. It should match "-g" with
41 an argument of "stabs". Suppose, however, that the number and list
42 of switches are such that the binary search tests "-gen-decls"
43 before having tested "-g". This doesn't match, and as "-gen-decls"
44 is less than "-gstabs", it will become the lower bound of the
45 binary search range, and "-g" will never be seen. To resolve this
46 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
47 to "-g" so that failed searches that end between "-gen-decls" and
48 the lexicographically subsequent switch know to go back and see if
49 "-g" causes a match (which it does in this example).
51 This search is done in such a way that the longest match for the
52 front end in question wins. If there is no match for the current
53 front end, the longest match for a different front end is returned
54 (or N_OPTS if none) and the caller emits an error message. */
56 find_opt (const char *input
, unsigned int lang_mask
)
58 size_t mn
, mn_orig
, mx
, md
, opt_len
;
59 size_t match_wrong_lang
;
63 mx
= cl_options_count
;
65 /* Find mn such this lexicographical inequality holds:
66 cl_options[mn] <= input < cl_options[mn + 1]. */
70 opt_len
= cl_options
[md
].opt_len
;
71 comp
= strncmp (input
, cl_options
[md
].opt_text
+ 1, opt_len
);
81 /* This is the switch that is the best match but for a different
82 front end, or OPT_SPECIAL_unknown if there is no match at all. */
83 match_wrong_lang
= OPT_SPECIAL_unknown
;
85 /* Backtrace the chain of possible matches, returning the longest
86 one, if any, that fits best. With current GCC switches, this
87 loop executes at most twice. */
90 const struct cl_option
*opt
= &cl_options
[mn
];
92 /* Is the input either an exact match or a prefix that takes a
94 if (!strncmp (input
, opt
->opt_text
+ 1, opt
->opt_len
)
95 && (input
[opt
->opt_len
] == '\0' || (opt
->flags
& CL_JOINED
)))
97 /* If language is OK, return it. */
98 if (opt
->flags
& lang_mask
)
101 /* If we haven't remembered a prior match, remember this
102 one. Any prior match is necessarily better. */
103 if (match_wrong_lang
== OPT_SPECIAL_unknown
)
104 match_wrong_lang
= mn
;
107 /* Try the next possibility. This is cl_options_count if there
109 mn
= opt
->back_chain
;
111 while (mn
!= cl_options_count
);
113 if (match_wrong_lang
== OPT_SPECIAL_unknown
&& input
[0] == '-')
115 /* Long options, starting "--", may be abbreviated if the
116 abbreviation is unambiguous. This only applies to options
117 not taking a joined argument, and abbreviations of "--option"
118 are permitted even if there is a variant "--option=". */
119 size_t mnc
= mn_orig
+ 1;
120 size_t cmp_len
= strlen (input
);
121 while (mnc
< cl_options_count
122 && strncmp (input
, cl_options
[mnc
].opt_text
+ 1, cmp_len
) == 0)
124 /* Option matching this abbreviation. OK if it is the first
125 match and that does not take a joined argument, or the
126 second match, taking a joined argument and with only '='
127 added to the first match; otherwise considered
129 if (mnc
== mn_orig
+ 1
130 && !(cl_options
[mnc
].flags
& CL_JOINED
))
131 match_wrong_lang
= mnc
;
132 else if (mnc
== mn_orig
+ 2
133 && match_wrong_lang
== mn_orig
+ 1
134 && (cl_options
[mnc
].flags
& CL_JOINED
)
135 && (cl_options
[mnc
].opt_len
136 == cl_options
[mn_orig
+ 1].opt_len
+ 1)
137 && strncmp (cl_options
[mnc
].opt_text
+ 1,
138 cl_options
[mn_orig
+ 1].opt_text
+ 1,
139 cl_options
[mn_orig
+ 1].opt_len
) == 0)
140 ; /* OK, as long as there are no more matches. */
142 return OPT_SPECIAL_unknown
;
147 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
148 return match_wrong_lang
;
151 /* If ARG is a non-negative integer made up solely of digits, return its
152 value, otherwise return -1. */
155 integral_argument (const char *arg
)
159 while (*p
&& ISDIGIT (*p
))
168 /* Return whether OPTION is OK for the language given by
171 option_ok_for_language (const struct cl_option
*option
,
172 unsigned int lang_mask
)
174 if (!(option
->flags
& lang_mask
))
176 else if ((option
->flags
& CL_TARGET
)
177 && (option
->flags
& (CL_LANG_ALL
| CL_DRIVER
))
178 && !(option
->flags
& (lang_mask
& ~CL_COMMON
& ~CL_TARGET
)))
179 /* Complain for target flag language mismatches if any languages
185 /* Return whether ENUM_ARG is OK for the language given by
189 enum_arg_ok_for_language (const struct cl_enum_arg
*enum_arg
,
190 unsigned int lang_mask
)
192 return (lang_mask
& CL_DRIVER
) || !(enum_arg
->flags
& CL_ENUM_DRIVER_ONLY
);
195 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
196 storing the value in *VALUE if found, and returning false without
197 modifying *VALUE if not found. */
200 enum_arg_to_value (const struct cl_enum_arg
*enum_args
,
201 const char *arg
, int *value
, unsigned int lang_mask
)
205 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
206 if (strcmp (arg
, enum_args
[i
].arg
) == 0
207 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
209 *value
= enum_args
[i
].value
;
216 /* Look up ARG in the enum used by option OPT_INDEX for language
217 LANG_MASK, returning true and storing the value in *VALUE if found,
218 and returning false without modifying *VALUE if not found. */
221 opt_enum_arg_to_value (size_t opt_index
, const char *arg
, int *value
,
222 unsigned int lang_mask
)
224 const struct cl_option
*option
= &cl_options
[opt_index
];
226 gcc_assert (option
->var_type
== CLVC_ENUM
);
228 return enum_arg_to_value (cl_enums
[option
->var_enum
].values
, arg
,
232 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
233 corresponding string in *ARGP, returning true if the found string
234 was marked as canonical, false otherwise. If VALUE is not found
235 (which may be the case for uninitialized values if the relevant
236 option has not been passed), set *ARGP to NULL and return
240 enum_value_to_arg (const struct cl_enum_arg
*enum_args
,
241 const char **argp
, int value
, unsigned int lang_mask
)
245 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
246 if (enum_args
[i
].value
== value
247 && (enum_args
[i
].flags
& CL_ENUM_CANONICAL
)
248 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
250 *argp
= enum_args
[i
].arg
;
254 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
255 if (enum_args
[i
].value
== value
256 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
258 *argp
= enum_args
[i
].arg
;
266 /* Fill in the canonical option part of *DECODED with an option
267 described by OPT_INDEX, ARG and VALUE. */
270 generate_canonical_option (size_t opt_index
, const char *arg
, int value
,
271 struct cl_decoded_option
*decoded
)
273 const struct cl_option
*option
= &cl_options
[opt_index
];
274 const char *opt_text
= option
->opt_text
;
277 && !option
->cl_reject_negative
278 && (opt_text
[1] == 'W' || opt_text
[1] == 'f' || opt_text
[1] == 'm'))
280 char *t
= XNEWVEC (char, option
->opt_len
+ 5);
286 memcpy (t
+ 5, opt_text
+ 2, option
->opt_len
);
290 decoded
->canonical_option
[2] = NULL
;
291 decoded
->canonical_option
[3] = NULL
;
295 if ((option
->flags
& CL_SEPARATE
)
296 && !option
->cl_separate_alias
)
298 decoded
->canonical_option
[0] = opt_text
;
299 decoded
->canonical_option
[1] = arg
;
300 decoded
->canonical_option_num_elements
= 2;
304 gcc_assert (option
->flags
& CL_JOINED
);
305 decoded
->canonical_option
[0] = concat (opt_text
, arg
, NULL
);
306 decoded
->canonical_option
[1] = NULL
;
307 decoded
->canonical_option_num_elements
= 1;
308 if (opt_text
!= option
->opt_text
)
309 free (CONST_CAST (char *, opt_text
));
314 decoded
->canonical_option
[0] = opt_text
;
315 decoded
->canonical_option
[1] = NULL
;
316 decoded
->canonical_option_num_elements
= 1;
320 /* Structure describing mappings from options on the command line to
321 options to look up with find_opt. */
324 /* Prefix of the option on the command line. */
326 /* If two argv elements are considered to be merged into one option,
327 prefix for the second element, otherwise NULL. */
329 /* The new prefix to map to. */
330 const char *new_prefix
;
331 /* Whether at least one character is needed following opt1 or opt0
332 for this mapping to be used. (--optimize= is valid for -O, but
333 --warn- is not valid for -W.) */
334 bool another_char_needed
;
335 /* Whether the original option is a negated form of the option
336 resulting from this map. */
339 static const struct option_map option_map
[] =
341 { "-Wno-", NULL
, "-W", false, true },
342 { "-fno-", NULL
, "-f", false, true },
343 { "-mno-", NULL
, "-m", false, true },
344 { "--debug=", NULL
, "-g", false, false },
345 { "--machine-", NULL
, "-m", true, false },
346 { "--machine-no-", NULL
, "-m", false, true },
347 { "--machine=", NULL
, "-m", false, false },
348 { "--machine=no-", NULL
, "-m", false, true },
349 { "--machine", "", "-m", false, false },
350 { "--machine", "no-", "-m", false, true },
351 { "--optimize=", NULL
, "-O", false, false },
352 { "--std=", NULL
, "-std=", false, false },
353 { "--std", "", "-std=", false, false },
354 { "--warn-", NULL
, "-W", true, false },
355 { "--warn-no-", NULL
, "-W", false, true },
356 { "--", NULL
, "-f", true, false },
357 { "--no-", NULL
, "-f", false, true }
360 /* Decode the switch beginning at ARGV for the language indicated by
361 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
362 the structure *DECODED. Returns the number of switches
366 decode_cmdline_option (const char **argv
, unsigned int lang_mask
,
367 struct cl_decoded_option
*decoded
)
372 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
376 const struct cl_option
*option
;
378 const char *warn_message
= NULL
;
379 bool separate_arg_flag
;
380 bool joined_arg_flag
;
381 bool have_separate_arg
= false;
385 opt_index
= find_opt (argv
[0] + 1, lang_mask
);
387 while (opt_index
== OPT_SPECIAL_unknown
388 && i
< ARRAY_SIZE (option_map
))
390 const char *opt0
= option_map
[i
].opt0
;
391 const char *opt1
= option_map
[i
].opt1
;
392 const char *new_prefix
= option_map
[i
].new_prefix
;
393 bool another_char_needed
= option_map
[i
].another_char_needed
;
394 size_t opt0_len
= strlen (opt0
);
395 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
396 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
397 size_t new_prefix_len
= strlen (new_prefix
);
399 extra_args
= (opt1
== NULL
? 0 : 1);
400 value
= !option_map
[i
].negated
;
402 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
404 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
405 && (!another_char_needed
406 || argv
[extra_args
][optn_len
] != 0))
408 size_t arglen
= strlen (argv
[extra_args
]);
411 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
412 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
413 memcpy (dup
, new_prefix
, new_prefix_len
);
414 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
415 arglen
- optn_len
+ 1);
416 opt_index
= find_opt (dup
+ 1, lang_mask
);
422 if (opt_index
== OPT_SPECIAL_unknown
)
430 option
= &cl_options
[opt_index
];
432 /* Reject negative form of switches that don't take negatives as
434 if (!value
&& option
->cl_reject_negative
)
436 opt_index
= OPT_SPECIAL_unknown
;
437 errors
|= CL_ERR_NEGATIVE
;
442 result
= extra_args
+ 1;
443 warn_message
= option
->warn_message
;
445 /* Check to see if the option is disabled for this configuration. */
446 if (option
->cl_disabled
)
447 errors
|= CL_ERR_DISABLED
;
449 /* Determine whether there may be a separate argument based on
450 whether this option is being processed for the driver, and, if
451 so, how many such arguments. */
452 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
453 && !(option
->cl_no_driver_arg
454 && (lang_mask
& CL_DRIVER
)));
455 separate_args
= (separate_arg_flag
456 ? option
->cl_separate_nargs
+ 1
458 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
460 /* Sort out any argument the switch takes. */
463 /* Have arg point to the original switch. This is because
464 some code, such as disable_builtin_function, expects its
465 argument to be persistent until the program exits. */
466 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
468 if (*arg
== '\0' && !option
->cl_missing_ok
)
470 if (separate_arg_flag
)
472 arg
= argv
[extra_args
+ 1];
473 result
= extra_args
+ 2;
475 result
= extra_args
+ 1;
477 have_separate_arg
= true;
480 /* Missing argument. */
484 else if (separate_arg_flag
)
486 arg
= argv
[extra_args
+ 1];
487 for (i
= 0; i
< separate_args
; i
++)
488 if (argv
[extra_args
+ 1 + i
] == NULL
)
490 errors
|= CL_ERR_MISSING_ARG
;
493 result
= extra_args
+ 1 + i
;
495 have_separate_arg
= true;
498 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
499 errors
|= CL_ERR_MISSING_ARG
;
501 /* Is this option an alias (or an ignored option, marked as an alias
502 of OPT_SPECIAL_ignore)? */
503 if (option
->alias_target
!= N_OPTS
504 && (!option
->cl_separate_alias
|| have_separate_arg
))
506 size_t new_opt_index
= option
->alias_target
;
508 if (new_opt_index
== OPT_SPECIAL_ignore
)
510 gcc_assert (option
->alias_arg
== NULL
);
511 gcc_assert (option
->neg_alias_arg
== NULL
);
512 opt_index
= new_opt_index
;
518 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
520 /* The new option must not be an alias itself. */
521 gcc_assert (new_option
->alias_target
== N_OPTS
522 || new_option
->cl_separate_alias
);
524 if (option
->neg_alias_arg
)
526 gcc_assert (option
->alias_arg
!= NULL
);
527 gcc_assert (arg
== NULL
);
528 gcc_assert (!option
->cl_negative_alias
);
530 arg
= option
->alias_arg
;
532 arg
= option
->neg_alias_arg
;
535 else if (option
->alias_arg
)
537 gcc_assert (value
== 1);
538 gcc_assert (arg
== NULL
);
539 gcc_assert (!option
->cl_negative_alias
);
540 arg
= option
->alias_arg
;
543 if (option
->cl_negative_alias
)
546 opt_index
= new_opt_index
;
550 gcc_assert (!option
->cl_reject_negative
);
552 /* Recompute what arguments are allowed. */
553 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
554 && !(option
->cl_no_driver_arg
555 && (lang_mask
& CL_DRIVER
)));
556 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
558 if (separate_args
> 1 || option
->cl_separate_nargs
)
559 gcc_assert (separate_args
560 == (unsigned int) option
->cl_separate_nargs
+ 1);
562 if (!(errors
& CL_ERR_MISSING_ARG
))
564 if (separate_arg_flag
|| joined_arg_flag
)
566 if (option
->cl_missing_ok
&& arg
== NULL
)
568 gcc_assert (arg
!= NULL
);
571 gcc_assert (arg
== NULL
);
574 /* Recheck for warnings and disabled options. */
575 if (option
->warn_message
)
577 gcc_assert (warn_message
== NULL
);
578 warn_message
= option
->warn_message
;
580 if (option
->cl_disabled
)
581 errors
|= CL_ERR_DISABLED
;
585 /* Check if this is a switch for a different front end. */
586 if (!option_ok_for_language (option
, lang_mask
))
587 errors
|= CL_ERR_WRONG_LANG
;
589 /* Convert the argument to lowercase if appropriate. */
590 if (arg
&& option
->cl_tolower
)
593 size_t len
= strlen (arg
);
594 char *arg_lower
= XNEWVEC (char, len
+ 1);
596 for (j
= 0; j
< len
; j
++)
597 arg_lower
[j
] = TOLOWER ((unsigned char) arg
[j
]);
602 /* If the switch takes an integer, convert it. */
603 if (arg
&& option
->cl_uinteger
)
605 value
= integral_argument (arg
);
607 errors
|= CL_ERR_UINT_ARG
;
610 /* If the switch takes an enumerated argument, convert it. */
611 if (arg
&& (option
->var_type
== CLVC_ENUM
))
613 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
615 gcc_assert (value
== 1);
616 if (enum_arg_to_value (e
->values
, arg
, &value
, lang_mask
))
618 const char *carg
= NULL
;
620 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
622 gcc_assert (carg
!= NULL
);
625 errors
|= CL_ERR_ENUM_ARG
;
629 decoded
->opt_index
= opt_index
;
631 decoded
->value
= value
;
632 decoded
->errors
= errors
;
633 decoded
->warn_message
= warn_message
;
635 if (opt_index
== OPT_SPECIAL_unknown
)
636 gcc_assert (result
== 1);
638 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
639 decoded
->canonical_option_num_elements
= result
;
641 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
646 if (opt_index
== OPT_SPECIAL_unknown
)
647 decoded
->canonical_option
[i
] = argv
[i
];
649 decoded
->canonical_option
[i
] = NULL
;
650 len
= strlen (argv
[i
]);
651 /* If the argument is an empty string, we will print it as "" in
652 orig_option_with_args_text. */
653 total_len
+= (len
!= 0 ? len
: 2) + 1;
656 decoded
->canonical_option
[i
] = NULL
;
658 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
)
660 generate_canonical_option (opt_index
, arg
, value
, decoded
);
661 if (separate_args
> 1)
663 for (i
= 0; i
< separate_args
; i
++)
665 if (argv
[extra_args
+ 1 + i
] == NULL
)
668 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
670 gcc_assert (result
== 1 + i
);
671 decoded
->canonical_option_num_elements
= result
;
674 decoded
->orig_option_with_args_text
= p
= XNEWVEC (char, total_len
);
675 for (i
= 0; i
< result
; i
++)
677 size_t len
= strlen (argv
[i
]);
679 /* Print the empty string verbally. */
686 memcpy (p
, argv
[i
], len
);
697 /* Decode command-line options (ARGC and ARGV being the arguments of
698 main) into an array, setting *DECODED_OPTIONS to a pointer to that
699 array and *DECODED_OPTIONS_COUNT to the number of entries in the
700 array. The first entry in the array is always one for the program
701 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
702 flags applicable for decoding (including CL_COMMON and CL_TARGET if
703 those options should be considered applicable). Do not produce any
704 diagnostics or set state outside of these variables. */
707 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
708 unsigned int lang_mask
,
709 struct cl_decoded_option
**decoded_options
,
710 unsigned int *decoded_options_count
)
713 struct cl_decoded_option
*opt_array
;
714 unsigned int num_decoded_options
;
716 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
718 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
719 opt_array
[0].warn_message
= NULL
;
720 opt_array
[0].arg
= argv
[0];
721 opt_array
[0].orig_option_with_args_text
= argv
[0];
722 opt_array
[0].canonical_option_num_elements
= 1;
723 opt_array
[0].canonical_option
[0] = argv
[0];
724 opt_array
[0].canonical_option
[1] = NULL
;
725 opt_array
[0].canonical_option
[2] = NULL
;
726 opt_array
[0].canonical_option
[3] = NULL
;
727 opt_array
[0].value
= 1;
728 opt_array
[0].errors
= 0;
729 num_decoded_options
= 1;
731 for (i
= 1; i
< argc
; i
+= n
)
733 const char *opt
= argv
[i
];
735 /* Interpret "-" or a non-switch as a file name. */
736 if (opt
[0] != '-' || opt
[1] == '\0')
738 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
739 num_decoded_options
++;
744 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
745 &opt_array
[num_decoded_options
]);
746 num_decoded_options
++;
749 *decoded_options
= opt_array
;
750 *decoded_options_count
= num_decoded_options
;
751 prune_options (decoded_options
, decoded_options_count
);
754 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
755 next one is the same as ORIG_NEXT_OPT_IDX. */
758 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
760 /* An option can be canceled by the same option or an option with
762 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
765 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
766 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
772 /* Filter out options canceled by the ones after them. */
775 prune_options (struct cl_decoded_option
**decoded_options
,
776 unsigned int *decoded_options_count
)
778 unsigned int old_decoded_options_count
= *decoded_options_count
;
779 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
780 unsigned int new_decoded_options_count
;
781 struct cl_decoded_option
*new_decoded_options
782 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
784 const struct cl_option
*option
;
786 /* Remove arguments which are negated by others after them. */
787 new_decoded_options_count
= 0;
788 for (i
= 0; i
< old_decoded_options_count
; i
++)
790 unsigned int j
, opt_idx
, next_opt_idx
;
792 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
795 opt_idx
= old_decoded_options
[i
].opt_index
;
798 case OPT_SPECIAL_unknown
:
799 case OPT_SPECIAL_ignore
:
800 case OPT_SPECIAL_program_name
:
801 case OPT_SPECIAL_input_file
:
805 gcc_assert (opt_idx
< cl_options_count
);
806 option
= &cl_options
[opt_idx
];
807 if (option
->neg_index
< 0)
810 /* Skip joined switches. */
811 if ((option
->flags
& CL_JOINED
))
814 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
816 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
818 next_opt_idx
= old_decoded_options
[j
].opt_index
;
819 if (next_opt_idx
>= cl_options_count
)
821 if (cl_options
[next_opt_idx
].neg_index
< 0)
823 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
))
825 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
828 if (j
== old_decoded_options_count
)
831 new_decoded_options
[new_decoded_options_count
]
832 = old_decoded_options
[i
];
833 new_decoded_options_count
++;
839 free (old_decoded_options
);
840 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
842 new_decoded_options_count
);
843 *decoded_options
= new_decoded_options
;
844 *decoded_options_count
= new_decoded_options_count
;
847 /* Handle option DECODED for the language indicated by LANG_MASK,
848 using the handlers in HANDLERS and setting fields in OPTS and
849 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
850 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
851 option for options from the source file, UNKNOWN_LOCATION
852 otherwise. GENERATED_P is true for an option generated as part of
853 processing another option or otherwise generated internally, false
854 for one explicitly passed by the user. Returns false if the switch
855 was invalid. DC is the diagnostic context for options affecting
856 diagnostics state, or NULL. */
859 handle_option (struct gcc_options
*opts
,
860 struct gcc_options
*opts_set
,
861 const struct cl_decoded_option
*decoded
,
862 unsigned int lang_mask
, int kind
, location_t loc
,
863 const struct cl_option_handlers
*handlers
,
864 bool generated_p
, diagnostic_context
*dc
)
866 size_t opt_index
= decoded
->opt_index
;
867 const char *arg
= decoded
->arg
;
868 int value
= decoded
->value
;
869 const struct cl_option
*option
= &cl_options
[opt_index
];
870 void *flag_var
= option_flag_var (opt_index
, opts
);
874 set_option (opts
, (generated_p
? NULL
: opts_set
),
875 opt_index
, value
, arg
, kind
, loc
, dc
);
877 for (i
= 0; i
< handlers
->num_handlers
; i
++)
878 if (option
->flags
& handlers
->handlers
[i
].mask
)
880 if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
881 lang_mask
, kind
, loc
,
889 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
890 option instead of DECODED. This is used for callbacks when one
891 option implies another instead of an option being decoded from the
895 handle_generated_option (struct gcc_options
*opts
,
896 struct gcc_options
*opts_set
,
897 size_t opt_index
, const char *arg
, int value
,
898 unsigned int lang_mask
, int kind
, location_t loc
,
899 const struct cl_option_handlers
*handlers
,
900 diagnostic_context
*dc
)
902 struct cl_decoded_option decoded
;
904 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
905 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
909 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
910 VALUE for a front end using LANG_MASK. This is used when the
911 compiler generates options internally. */
914 generate_option (size_t opt_index
, const char *arg
, int value
,
915 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
917 const struct cl_option
*option
= &cl_options
[opt_index
];
919 decoded
->opt_index
= opt_index
;
920 decoded
->warn_message
= NULL
;
922 decoded
->value
= value
;
923 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
925 : CL_ERR_WRONG_LANG
);
927 generate_canonical_option (opt_index
, arg
, value
, decoded
);
928 switch (decoded
->canonical_option_num_elements
)
931 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
935 decoded
->orig_option_with_args_text
936 = concat (decoded
->canonical_option
[0], " ",
937 decoded
->canonical_option
[1], NULL
);
945 /* Fill in *DECODED with an option for input file FILE. */
948 generate_option_input_file (const char *file
,
949 struct cl_decoded_option
*decoded
)
951 decoded
->opt_index
= OPT_SPECIAL_input_file
;
952 decoded
->warn_message
= NULL
;
954 decoded
->orig_option_with_args_text
= file
;
955 decoded
->canonical_option_num_elements
= 1;
956 decoded
->canonical_option
[0] = file
;
957 decoded
->canonical_option
[1] = NULL
;
958 decoded
->canonical_option
[2] = NULL
;
959 decoded
->canonical_option
[3] = NULL
;
964 /* Handle the switch DECODED (location LOC) for the language indicated
965 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
966 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
967 diagnostic options. */
970 read_cmdline_option (struct gcc_options
*opts
,
971 struct gcc_options
*opts_set
,
972 struct cl_decoded_option
*decoded
,
974 unsigned int lang_mask
,
975 const struct cl_option_handlers
*handlers
,
976 diagnostic_context
*dc
)
978 const struct cl_option
*option
;
979 const char *opt
= decoded
->orig_option_with_args_text
;
981 if (decoded
->warn_message
)
982 warning_at (loc
, 0, decoded
->warn_message
, opt
);
984 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
986 if (handlers
->unknown_option_callback (decoded
))
987 error_at (loc
, "unrecognized command line option %qs", decoded
->arg
);
991 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
994 option
= &cl_options
[decoded
->opt_index
];
996 if (decoded
->errors
& CL_ERR_DISABLED
)
998 error_at (loc
, "command line option %qs"
999 " is not supported by this configuration", opt
);
1003 if (decoded
->errors
& CL_ERR_MISSING_ARG
)
1005 if (option
->missing_argument_error
)
1006 error_at (loc
, option
->missing_argument_error
, opt
);
1008 error_at (loc
, "missing argument to %qs", opt
);
1012 if (decoded
->errors
& CL_ERR_UINT_ARG
)
1014 error_at (loc
, "argument to %qs should be a non-negative integer",
1019 if (decoded
->errors
& CL_ERR_ENUM_ARG
)
1021 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1026 if (e
->unknown_error
)
1027 error_at (loc
, e
->unknown_error
, decoded
->arg
);
1029 error_at (loc
, "unrecognized argument in option %qs", opt
);
1032 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1033 len
+= strlen (e
->values
[i
].arg
) + 1;
1035 s
= XALLOCAVEC (char, len
);
1037 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1039 size_t arglen
= strlen (e
->values
[i
].arg
);
1040 memcpy (p
, e
->values
[i
].arg
, arglen
);
1045 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1049 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
1051 handlers
->wrong_lang_callback (decoded
, lang_mask
);
1055 gcc_assert (!decoded
->errors
);
1057 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1058 loc
, handlers
, false, dc
))
1059 error_at (loc
, "unrecognized command line option %qs", opt
);
1062 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1063 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1064 location LOC, using diagnostic context DC if not NULL for
1065 diagnostic classification. */
1068 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1069 int opt_index
, int value
, const char *arg
, int kind
,
1070 location_t loc
, diagnostic_context
*dc
)
1072 const struct cl_option
*option
= &cl_options
[opt_index
];
1073 void *flag_var
= option_flag_var (opt_index
, opts
);
1074 void *set_flag_var
= NULL
;
1079 if (opts_set
!= NULL
)
1080 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1082 switch (option
->var_type
)
1085 *(int *) flag_var
= value
;
1087 *(int *) set_flag_var
= 1;
1091 if (option
->cl_host_wide_int
)
1092 *(HOST_WIDE_INT
*) flag_var
= (value
1094 : !option
->var_value
);
1096 *(int *) flag_var
= (value
1098 : !option
->var_value
);
1100 *(int *) set_flag_var
= 1;
1103 case CLVC_BIT_CLEAR
:
1105 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1107 if (option
->cl_host_wide_int
)
1108 *(HOST_WIDE_INT
*) flag_var
|= option
->var_value
;
1110 *(int *) flag_var
|= option
->var_value
;
1114 if (option
->cl_host_wide_int
)
1115 *(HOST_WIDE_INT
*) flag_var
&= ~option
->var_value
;
1117 *(int *) flag_var
&= ~option
->var_value
;
1121 if (option
->cl_host_wide_int
)
1122 *(HOST_WIDE_INT
*) set_flag_var
|= option
->var_value
;
1124 *(int *) set_flag_var
|= option
->var_value
;
1129 *(const char **) flag_var
= arg
;
1131 *(const char **) set_flag_var
= "";
1136 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1138 e
->set (flag_var
, value
);
1140 e
->set (set_flag_var
, 1);
1146 vec
<cl_deferred_option
> *v
1147 = (vec
<cl_deferred_option
> *) *(void **) flag_var
;
1148 cl_deferred_option p
= {opt_index
, arg
, value
};
1150 v
= XCNEW (vec
<cl_deferred_option
>);
1152 *(void **) flag_var
= v
;
1154 *(void **) set_flag_var
= v
;
1159 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
1161 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1164 /* Return the address of the flag variable for option OPT_INDEX in
1165 options structure OPTS, or NULL if there is no flag variable. */
1168 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1170 const struct cl_option
*option
= &cl_options
[opt_index
];
1172 if (option
->flag_var_offset
== (unsigned short) -1)
1174 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1177 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1178 or -1 if it isn't a simple on-off switch. */
1181 option_enabled (int opt_idx
, void *opts
)
1183 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1184 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1185 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1188 switch (option
->var_type
)
1191 return *(int *) flag_var
!= 0;
1194 if (option
->cl_host_wide_int
)
1195 return *(HOST_WIDE_INT
*) flag_var
== option
->var_value
;
1197 return *(int *) flag_var
== option
->var_value
;
1199 case CLVC_BIT_CLEAR
:
1200 if (option
->cl_host_wide_int
)
1201 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) == 0;
1203 return (*(int *) flag_var
& option
->var_value
) == 0;
1206 if (option
->cl_host_wide_int
)
1207 return (*(HOST_WIDE_INT
*) flag_var
& option
->var_value
) != 0;
1209 return (*(int *) flag_var
& option
->var_value
) != 0;
1219 /* Fill STATE with the current state of option OPTION in OPTS. Return
1220 true if there is some state to store. */
1223 get_option_state (struct gcc_options
*opts
, int option
,
1224 struct cl_option_state
*state
)
1226 void *flag_var
= option_flag_var (option
, opts
);
1231 switch (cl_options
[option
].var_type
)
1235 state
->data
= flag_var
;
1236 state
->size
= (cl_options
[option
].cl_host_wide_int
1237 ? sizeof (HOST_WIDE_INT
)
1241 case CLVC_BIT_CLEAR
:
1243 state
->ch
= option_enabled (option
, opts
);
1244 state
->data
= &state
->ch
;
1249 state
->data
= *(const char **) flag_var
;
1250 if (state
->data
== 0)
1252 state
->size
= strlen ((const char *) state
->data
) + 1;
1256 state
->data
= flag_var
;
1257 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1266 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1267 handlers HANDLERS) to have diagnostic kind KIND for option
1268 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1269 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1270 the warning option in question is implied at this point. This is
1271 used by -Werror= and #pragma GCC diagnostic. */
1274 control_warning_option (unsigned int opt_index
, int kind
, bool imply
,
1275 location_t loc
, unsigned int lang_mask
,
1276 const struct cl_option_handlers
*handlers
,
1277 struct gcc_options
*opts
,
1278 struct gcc_options
*opts_set
,
1279 diagnostic_context
*dc
)
1281 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1282 opt_index
= cl_options
[opt_index
].alias_target
;
1283 if (opt_index
== OPT_SPECIAL_ignore
)
1286 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1289 /* -Werror=foo implies -Wfoo. */
1290 if (cl_options
[opt_index
].var_type
== CLVC_BOOLEAN
)
1291 handle_generated_option (opts
, opts_set
,
1292 opt_index
, NULL
, 1, lang_mask
,
1293 kind
, loc
, handlers
, dc
);