1 /* Command line option handling.
2 Copyright (C) 2006, 2007, 2008, 2010 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"
27 #include "tm.h" /* For SWITCH_TAKES_ARG, WORD_SWITCH_TAKES_ARG and
28 TARGET_OPTION_TRANSLATE_TABLE. */
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
, 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 integer made up solely of digits, return its
151 value, otherwise return -1. */
154 integral_argument (const char *arg
)
158 while (*p
&& ISDIGIT (*p
))
167 /* Return whether OPTION is OK for the language given by
170 option_ok_for_language (const struct cl_option
*option
,
171 unsigned int lang_mask
)
173 if (!(option
->flags
& lang_mask
))
175 else if ((option
->flags
& CL_TARGET
)
176 && (option
->flags
& (CL_LANG_ALL
| CL_DRIVER
))
177 && !(option
->flags
& (lang_mask
& ~CL_COMMON
& ~CL_TARGET
)))
178 /* Complain for target flag language mismatches if any languages
185 /* Fill in the canonical option part of *DECODED with an option
186 described by OPT_INDEX, ARG and VALUE. */
189 generate_canonical_option (size_t opt_index
, const char *arg
, int value
,
190 struct cl_decoded_option
*decoded
)
192 const struct cl_option
*option
= &cl_options
[opt_index
];
193 const char *opt_text
= option
->opt_text
;
196 && !(option
->flags
& CL_REJECT_NEGATIVE
)
197 && (opt_text
[1] == 'W' || opt_text
[1] == 'f' || opt_text
[1] == 'm'))
199 char *t
= XNEWVEC (char, option
->opt_len
+ 5);
205 memcpy (t
+ 5, opt_text
+ 2, option
->opt_len
);
209 decoded
->canonical_option
[2] = NULL
;
210 decoded
->canonical_option
[3] = NULL
;
214 if ((option
->flags
& CL_SEPARATE
)
215 && !(option
->flags
& CL_SEPARATE_ALIAS
))
217 decoded
->canonical_option
[0] = opt_text
;
218 decoded
->canonical_option
[1] = arg
;
219 decoded
->canonical_option_num_elements
= 2;
223 gcc_assert (option
->flags
& CL_JOINED
);
224 decoded
->canonical_option
[0] = concat (opt_text
, arg
, NULL
);
225 decoded
->canonical_option
[1] = NULL
;
226 decoded
->canonical_option_num_elements
= 1;
231 decoded
->canonical_option
[0] = opt_text
;
232 decoded
->canonical_option
[1] = NULL
;
233 decoded
->canonical_option_num_elements
= 1;
237 /* Structure describing mappings from options on the command line to
238 options to look up with find_opt. */
241 /* Prefix of the option on the command line. */
243 /* If two argv elements are considered to be merged into one option,
244 prefix for the second element, otherwise NULL. */
246 /* The new prefix to map to. */
247 const char *new_prefix
;
248 /* Whether at least one character is needed following opt1 or opt0
249 for this mapping to be used. (--optimize= is valid for -O, but
250 --warn- is not valid for -W.) */
251 bool another_char_needed
;
252 /* Whether the original option is a negated form of the option
253 resulting from this map. */
256 static const struct option_map option_map
[] =
258 { "-Wno-", NULL
, "-W", false, true },
259 { "-fno-", NULL
, "-f", false, true },
260 { "-mno-", NULL
, "-m", false, true },
261 { "--debug=", NULL
, "-g", false, false },
262 { "--machine-", NULL
, "-m", true, false },
263 { "--machine-no-", NULL
, "-m", false, true },
264 { "--machine=", NULL
, "-m", false, false },
265 { "--machine=no-", NULL
, "-m", false, true },
266 { "--machine", "", "-m", false, false },
267 { "--machine", "no-", "-m", false, true },
268 { "--optimize=", NULL
, "-O", false, false },
269 { "--std=", NULL
, "-std=", false, false },
270 { "--std", "", "-std=", false, false },
271 { "--warn-", NULL
, "-W", true, false },
272 { "--warn-no-", NULL
, "-W", false, true },
273 { "--", NULL
, "-f", true, false },
274 { "--no-", NULL
, "-f", false, true }
277 /* Decode the switch beginning at ARGV for the language indicated by
278 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
279 the structure *DECODED. Returns the number of switches
283 decode_cmdline_option (const char **argv
, unsigned int lang_mask
,
284 struct cl_decoded_option
*decoded
)
289 unsigned int result
= 1, i
, extra_args
;
293 const struct cl_option
*option
;
295 const char *warn_message
= NULL
;
296 bool separate_arg_flag
;
297 bool joined_arg_flag
;
298 bool have_separate_arg
= false;
302 opt_index
= find_opt (argv
[0] + 1, lang_mask
);
304 while (opt_index
== OPT_SPECIAL_unknown
305 && i
< ARRAY_SIZE (option_map
))
307 const char *opt0
= option_map
[i
].opt0
;
308 const char *opt1
= option_map
[i
].opt1
;
309 const char *new_prefix
= option_map
[i
].new_prefix
;
310 bool another_char_needed
= option_map
[i
].another_char_needed
;
311 size_t opt0_len
= strlen (opt0
);
312 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
313 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
314 size_t new_prefix_len
= strlen (new_prefix
);
316 extra_args
= (opt1
== NULL
? 0 : 1);
317 value
= !option_map
[i
].negated
;
319 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
321 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
322 && (!another_char_needed
323 || argv
[extra_args
][optn_len
] != 0))
325 size_t arglen
= strlen (argv
[extra_args
]);
328 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
329 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
330 memcpy (dup
, new_prefix
, new_prefix_len
);
331 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
332 arglen
- optn_len
+ 1);
333 opt_index
= find_opt (dup
+ 1, lang_mask
);
339 if (opt_index
== OPT_SPECIAL_unknown
)
347 option
= &cl_options
[opt_index
];
349 /* Reject negative form of switches that don't take negatives as
351 if (!value
&& (option
->flags
& CL_REJECT_NEGATIVE
))
353 opt_index
= OPT_SPECIAL_unknown
;
354 errors
|= CL_ERR_NEGATIVE
;
359 result
= extra_args
+ 1;
360 warn_message
= option
->warn_message
;
362 /* Check to see if the option is disabled for this configuration. */
363 if (option
->flags
& CL_DISABLED
)
364 errors
|= CL_ERR_DISABLED
;
366 /* Determine whether there may be a separate argument based on
367 whether this option is being processed for the driver. */
368 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
369 && !((option
->flags
& CL_NO_DRIVER_ARG
)
370 && (lang_mask
& CL_DRIVER
)));
371 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
373 /* Sort out any argument the switch takes. */
376 /* Have arg point to the original switch. This is because
377 some code, such as disable_builtin_function, expects its
378 argument to be persistent until the program exits. */
379 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
381 if (*arg
== '\0' && !(option
->flags
& CL_MISSING_OK
))
383 if (separate_arg_flag
)
385 arg
= argv
[extra_args
+ 1];
386 result
= extra_args
+ 2;
388 result
= extra_args
+ 1;
390 have_separate_arg
= true;
393 /* Missing argument. */
397 else if (separate_arg_flag
)
399 arg
= argv
[extra_args
+ 1];
400 result
= extra_args
+ 2;
402 result
= extra_args
+ 1;
404 have_separate_arg
= true;
407 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
408 errors
|= CL_ERR_MISSING_ARG
;
410 /* Is this option an alias (or an ignored option, marked as an alias
411 of OPT_SPECIAL_ignore)? */
412 if (option
->alias_target
!= N_OPTS
413 && (!(option
->flags
& CL_SEPARATE_ALIAS
) || have_separate_arg
))
415 size_t new_opt_index
= option
->alias_target
;
417 if (new_opt_index
== OPT_SPECIAL_ignore
)
419 gcc_assert (option
->alias_arg
== NULL
);
420 gcc_assert (option
->neg_alias_arg
== NULL
);
421 opt_index
= new_opt_index
;
427 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
429 /* The new option must not be an alias itself. */
430 gcc_assert (new_option
->alias_target
== N_OPTS
431 || (new_option
->flags
& CL_SEPARATE_ALIAS
));
433 if (option
->neg_alias_arg
)
435 gcc_assert (option
->alias_arg
!= NULL
);
436 gcc_assert (arg
== NULL
);
438 arg
= option
->alias_arg
;
440 arg
= option
->neg_alias_arg
;
443 else if (option
->alias_arg
)
445 gcc_assert (value
== 1);
446 gcc_assert (arg
== NULL
);
447 arg
= option
->alias_arg
;
450 opt_index
= new_opt_index
;
454 gcc_assert (!(option
->flags
& CL_REJECT_NEGATIVE
));
456 /* Recompute what arguments are allowed. */
457 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
458 && !((option
->flags
& CL_NO_DRIVER_ARG
)
459 && (lang_mask
& CL_DRIVER
)));
460 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
462 if (!(errors
& CL_ERR_MISSING_ARG
))
464 if (separate_arg_flag
|| joined_arg_flag
)
466 if ((option
->flags
& CL_MISSING_OK
) && arg
== NULL
)
468 gcc_assert (arg
!= NULL
);
471 gcc_assert (arg
== NULL
);
474 /* Recheck for warnings and disabled options. */
475 if (option
->warn_message
)
477 gcc_assert (warn_message
== NULL
);
478 warn_message
= option
->warn_message
;
480 if (option
->flags
& CL_DISABLED
)
481 errors
|= CL_ERR_DISABLED
;
485 /* Check if this is a switch for a different front end. */
486 if (!option_ok_for_language (option
, lang_mask
))
487 errors
|= CL_ERR_WRONG_LANG
;
489 /* If the switch takes an integer, convert it. */
490 if (arg
&& (option
->flags
& CL_UINTEGER
))
492 value
= integral_argument (arg
);
494 errors
|= CL_ERR_UINT_ARG
;
498 decoded
->opt_index
= opt_index
;
500 decoded
->value
= value
;
501 decoded
->errors
= errors
;
502 decoded
->warn_message
= warn_message
;
504 if (opt_index
== OPT_SPECIAL_unknown
)
506 /* Skip the correct number of arguments for options handled
508 const char *popt
= argv
[0] + 1;
511 gcc_assert (result
== 1);
512 if (SWITCH_TAKES_ARG (c
) > (popt
[1] != 0))
513 result
+= SWITCH_TAKES_ARG (c
) - (popt
[1] != 0);
514 else if (WORD_SWITCH_TAKES_ARG (popt
))
515 result
+= WORD_SWITCH_TAKES_ARG (popt
);
517 for (i
= 1; i
< result
; i
++)
525 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
526 decoded
->canonical_option_num_elements
= result
;
528 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
532 if (opt_index
== OPT_SPECIAL_unknown
)
533 decoded
->canonical_option
[i
] = argv
[i
];
535 decoded
->canonical_option
[i
] = NULL
;
536 total_len
+= strlen (argv
[i
]) + 1;
539 decoded
->canonical_option
[i
] = NULL
;
541 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
)
542 generate_canonical_option (opt_index
, arg
, value
, decoded
);
543 decoded
->orig_option_with_args_text
= p
= XNEWVEC (char, total_len
);
544 for (i
= 0; i
< result
; i
++)
546 size_t len
= strlen (argv
[i
]);
548 memcpy (p
, argv
[i
], len
);
559 #ifdef TARGET_OPTION_TRANSLATE_TABLE
560 static const struct {
561 const char *const option_found
;
562 const char *const replacements
;
563 } target_option_translations
[] =
565 TARGET_OPTION_TRANSLATE_TABLE
,
570 /* Decode command-line options (ARGC and ARGV being the arguments of
571 main) into an array, setting *DECODED_OPTIONS to a pointer to that
572 array and *DECODED_OPTIONS_COUNT to the number of entries in the
573 array. The first entry in the array is always one for the program
574 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
575 flags applicable for decoding (including CL_COMMON and CL_TARGET if
576 those options should be considered applicable). Do not produce any
577 diagnostics or set state outside of these variables. */
580 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
581 unsigned int lang_mask
,
582 struct cl_decoded_option
**decoded_options
,
583 unsigned int *decoded_options_count
)
585 unsigned int n
, i
, target_translate_from
;
586 struct cl_decoded_option
*opt_array
;
587 unsigned int num_decoded_options
;
588 bool argv_copied
= false;
590 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
592 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
593 opt_array
[0].warn_message
= NULL
;
594 opt_array
[0].arg
= argv
[0];
595 opt_array
[0].orig_option_with_args_text
= argv
[0];
596 opt_array
[0].canonical_option_num_elements
= 1;
597 opt_array
[0].canonical_option
[0] = argv
[0];
598 opt_array
[0].canonical_option
[1] = NULL
;
599 opt_array
[0].canonical_option
[2] = NULL
;
600 opt_array
[0].canonical_option
[3] = NULL
;
601 opt_array
[0].value
= 1;
602 opt_array
[0].errors
= 0;
603 num_decoded_options
= 1;
605 target_translate_from
= 1;
606 for (i
= 1; i
< argc
; i
+= n
)
608 const char *opt
= argv
[i
];
610 /* Interpret "-" or a non-switch as a file name. */
611 if (opt
[0] != '-' || opt
[1] == '\0')
613 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
614 num_decoded_options
++;
619 if (i
>= target_translate_from
&& (lang_mask
& CL_DRIVER
))
621 #ifdef TARGET_OPTION_TRANSLATE_TABLE
625 target_option_translations
[tott_idx
].option_found
;
628 if (strcmp (target_option_translations
[tott_idx
].option_found
,
631 unsigned int spaces
= 0;
636 for (sp
= target_option_translations
[tott_idx
].replacements
;
650 int new_argc
= argc
+ spaces
;
652 argv
= XRESIZEVEC (const char *, argv
, new_argc
+ 1);
655 const char **new_argv
= XNEWVEC (const char *,
657 memcpy (new_argv
, argv
,
658 (argc
+ 1) * sizeof (const char *));
662 memmove (&argv
[i
] + spaces
, &argv
[i
],
663 (argc
+ 1 - i
) * sizeof (const char *));
665 opt_array
= XRESIZEVEC (struct cl_decoded_option
,
669 sp
= target_option_translations
[tott_idx
].replacements
;
679 while (*np
!= ' ' && *np
)
686 target_translate_from
= i
+ m
;
687 gcc_assert (m
== spaces
+ 1);
694 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
695 &opt_array
[num_decoded_options
]);
696 num_decoded_options
++;
701 opt_array
= XRESIZEVEC (struct cl_decoded_option
, opt_array
,
702 num_decoded_options
);
703 *decoded_options
= opt_array
;
704 *decoded_options_count
= num_decoded_options
;
707 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
708 next one is the same as ORIG_NEXT_OPT_IDX. */
711 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
713 /* An option can be canceled by the same option or an option with
715 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
718 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
719 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
725 /* Filter out options canceled by the ones after them. */
728 prune_options (int *argcp
, char ***argvp
)
731 int *options
= XNEWVEC (int, argc
);
732 /* We will only return this replacement argv if we remove at least
733 one argument, so it does not need to be size (argc + 1) to
734 make room for the terminating NULL because we will always have
735 freed up at least one slot when we end up using it at all. */
736 char **argv
= XNEWVEC (char *, argc
);
737 int i
, arg_count
, need_prune
= 0;
738 const struct cl_option
*option
;
741 /* Scan all arguments. */
742 for (i
= 1; i
< argc
; i
++)
745 const char *opt
= (*argvp
) [i
];
747 opt_index
= find_opt (opt
+ 1, -1);
748 if (opt_index
== OPT_SPECIAL_unknown
749 && (opt
[1] == 'W' || opt
[1] == 'f' || opt
[1] == 'm')
750 && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-')
754 /* Drop the "no-" from negative switches. */
755 size_t len
= strlen (opt
) - 3;
757 dup
= XNEWVEC (char, len
+ 1);
760 memcpy (dup
+ 2, opt
+ 5, len
- 2 + 1);
763 opt_index
= find_opt (opt
+ 1, -1);
767 if (opt_index
== OPT_SPECIAL_unknown
)
774 option
= &cl_options
[opt_index
];
775 if (option
->neg_index
< 0)
778 /* Skip joined switches. */
779 if ((option
->flags
& CL_JOINED
))
782 /* Reject negative form of switches that don't take negatives as
784 if (!value
&& (option
->flags
& CL_REJECT_NEGATIVE
))
787 options
[i
] = (int) opt_index
;
788 need_prune
|= options
[i
];
794 /* Remove arguments which are negated by others after them. */
795 argv
[0] = (*argvp
) [0];
797 for (i
= 1; i
< argc
; i
++)
801 opt_idx
= options
[i
];
805 for (j
= i
+ 1; j
< argc
; j
++)
807 next_opt_idx
= options
[j
];
809 && cancel_option (opt_idx
, next_opt_idx
,
820 argv
[arg_count
] = (*argvp
) [i
];
825 if (arg_count
!= argc
)
829 /* Add NULL-termination. Guaranteed not to overflow because
830 arg_count here can only be less than argc. */
842 /* Handle option DECODED for the language indicated by LANG_MASK,
843 using the handlers in HANDLERS. KIND is the diagnostic_t if this
844 is a diagnostics option, DK_UNSPECIFIED otherwise. Returns false
845 if the switch was invalid. */
848 handle_option (const struct cl_decoded_option
*decoded
,
849 unsigned int lang_mask
, int kind
,
850 const struct cl_option_handlers
*handlers
)
852 size_t opt_index
= decoded
->opt_index
;
853 const char *arg
= decoded
->arg
;
854 int value
= decoded
->value
;
855 const struct cl_option
*option
= &cl_options
[opt_index
];
858 if (option
->flag_var
)
859 set_option (opt_index
, value
, arg
, kind
);
861 for (i
= 0; i
< handlers
->num_handlers
; i
++)
862 if (option
->flags
& handlers
->handlers
[i
].mask
)
864 if (!handlers
->handlers
[i
].handler (decoded
,
865 lang_mask
, kind
, handlers
))
868 handlers
->post_handling_callback (decoded
,
869 handlers
->handlers
[i
].mask
);
875 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
876 option instead of DECODED. This is used for callbacks when one
877 option implies another instead of an option being decoded from the
881 handle_generated_option (size_t opt_index
, const char *arg
, int value
,
882 unsigned int lang_mask
, int kind
,
883 const struct cl_option_handlers
*handlers
)
885 struct cl_decoded_option decoded
;
887 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
888 return handle_option (&decoded
, lang_mask
, kind
, handlers
);
891 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
892 VALUE for a front end using LANG_MASK. This is used when the
893 compiler generates options internally. */
896 generate_option (size_t opt_index
, const char *arg
, int value
,
897 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
899 const struct cl_option
*option
= &cl_options
[opt_index
];
901 decoded
->opt_index
= opt_index
;
902 decoded
->warn_message
= NULL
;
904 decoded
->value
= value
;
905 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
907 : CL_ERR_WRONG_LANG
);
909 generate_canonical_option (opt_index
, arg
, value
, decoded
);
910 switch (decoded
->canonical_option_num_elements
)
913 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
917 decoded
->orig_option_with_args_text
918 = concat (decoded
->canonical_option
[0], " ",
919 decoded
->canonical_option
[1], NULL
);
927 /* Fill in *DECODED with an option for input file FILE. */
930 generate_option_input_file (const char *file
,
931 struct cl_decoded_option
*decoded
)
933 decoded
->opt_index
= OPT_SPECIAL_input_file
;
934 decoded
->warn_message
= NULL
;
936 decoded
->orig_option_with_args_text
= file
;
937 decoded
->canonical_option_num_elements
= 1;
938 decoded
->canonical_option
[0] = file
;
939 decoded
->canonical_option
[1] = NULL
;
940 decoded
->canonical_option
[2] = NULL
;
941 decoded
->canonical_option
[3] = NULL
;
946 /* Handle the switch DECODED for the language indicated by LANG_MASK,
947 using the handlers in *HANDLERS. */
950 read_cmdline_option (struct cl_decoded_option
*decoded
,
951 unsigned int lang_mask
,
952 const struct cl_option_handlers
*handlers
)
954 const struct cl_option
*option
;
955 const char *opt
= decoded
->orig_option_with_args_text
;
957 if (decoded
->warn_message
)
958 warning (0, decoded
->warn_message
, opt
);
960 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
962 if (handlers
->unknown_option_callback (decoded
))
963 error ("unrecognized command line option %qs", decoded
->arg
);
967 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
970 option
= &cl_options
[decoded
->opt_index
];
972 if (decoded
->errors
& CL_ERR_DISABLED
)
974 error ("command line option %qs"
975 " is not supported by this configuration", opt
);
979 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
981 handlers
->wrong_lang_callback (decoded
, lang_mask
);
985 if (decoded
->errors
& CL_ERR_MISSING_ARG
)
987 if (option
->missing_argument_error
)
988 error (option
->missing_argument_error
, opt
);
990 error ("missing argument to %qs", opt
);
994 if (decoded
->errors
& CL_ERR_UINT_ARG
)
996 error ("argument to %qs should be a non-negative integer",
1001 gcc_assert (!decoded
->errors
);
1003 if (!handle_option (decoded
, lang_mask
, DK_UNSPECIFIED
, handlers
))
1004 error ("unrecognized command line option %qs", opt
);
1007 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
1008 diagnostic kind KIND. */
1011 set_option (int opt_index
, int value
, const char *arg
, int kind
)
1013 const struct cl_option
*option
= &cl_options
[opt_index
];
1015 if (!option
->flag_var
)
1018 switch (option
->var_type
)
1021 *(int *) option
->flag_var
= value
;
1025 *(int *) option
->flag_var
= (value
1027 : !option
->var_value
);
1030 case CLVC_BIT_CLEAR
:
1032 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1033 *(int *) option
->flag_var
|= option
->var_value
;
1035 *(int *) option
->flag_var
&= ~option
->var_value
;
1036 if (option
->flag_var
== &target_flags
)
1037 target_flags_explicit
|= option
->var_value
;
1041 *(const char **) option
->flag_var
= arg
;
1045 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
)
1046 diagnostic_classify_diagnostic (global_dc
, opt_index
, (diagnostic_t
) kind
,