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"
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
, 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
184 /* Return whether ENUM_ARG is OK for the language given by
188 enum_arg_ok_for_language (const struct cl_enum_arg
*enum_arg
,
189 unsigned int lang_mask
)
191 return (lang_mask
& CL_DRIVER
) || !(enum_arg
->flags
& CL_ENUM_DRIVER_ONLY
);
194 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
195 storing the value in *VALUE if found, and returning false without
196 modifying *VALUE if not found. */
199 enum_arg_to_value (const struct cl_enum_arg
*enum_args
,
200 const char *arg
, int *value
, unsigned int lang_mask
)
204 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
205 if (strcmp (arg
, enum_args
[i
].arg
) == 0
206 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
208 *value
= enum_args
[i
].value
;
215 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
216 corresponding string in *ARGP, returning true if the found string
217 was marked as canonical, false otherwise. If VALUE is not found
218 (which may be the case for uninitialized values if the relevant
219 option has not been passed), set *ARGP to NULL and return
223 enum_value_to_arg (const struct cl_enum_arg
*enum_args
,
224 const char **argp
, int value
, unsigned int lang_mask
)
228 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
229 if (enum_args
[i
].value
== value
230 && (enum_args
[i
].flags
& CL_ENUM_CANONICAL
)
231 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
233 *argp
= enum_args
[i
].arg
;
237 for (i
= 0; enum_args
[i
].arg
!= NULL
; i
++)
238 if (enum_args
[i
].value
== value
239 && enum_arg_ok_for_language (&enum_args
[i
], lang_mask
))
241 *argp
= enum_args
[i
].arg
;
249 /* Fill in the canonical option part of *DECODED with an option
250 described by OPT_INDEX, ARG and VALUE. */
253 generate_canonical_option (size_t opt_index
, const char *arg
, int value
,
254 struct cl_decoded_option
*decoded
)
256 const struct cl_option
*option
= &cl_options
[opt_index
];
257 const char *opt_text
= option
->opt_text
;
260 && !(option
->flags
& CL_REJECT_NEGATIVE
)
261 && (opt_text
[1] == 'W' || opt_text
[1] == 'f' || opt_text
[1] == 'm'))
263 char *t
= XNEWVEC (char, option
->opt_len
+ 5);
269 memcpy (t
+ 5, opt_text
+ 2, option
->opt_len
);
273 decoded
->canonical_option
[2] = NULL
;
274 decoded
->canonical_option
[3] = NULL
;
278 if ((option
->flags
& CL_SEPARATE
)
279 && !(option
->flags
& CL_SEPARATE_ALIAS
))
281 decoded
->canonical_option
[0] = opt_text
;
282 decoded
->canonical_option
[1] = arg
;
283 decoded
->canonical_option_num_elements
= 2;
287 gcc_assert (option
->flags
& CL_JOINED
);
288 decoded
->canonical_option
[0] = concat (opt_text
, arg
, NULL
);
289 decoded
->canonical_option
[1] = NULL
;
290 decoded
->canonical_option_num_elements
= 1;
295 decoded
->canonical_option
[0] = opt_text
;
296 decoded
->canonical_option
[1] = NULL
;
297 decoded
->canonical_option_num_elements
= 1;
301 /* Structure describing mappings from options on the command line to
302 options to look up with find_opt. */
305 /* Prefix of the option on the command line. */
307 /* If two argv elements are considered to be merged into one option,
308 prefix for the second element, otherwise NULL. */
310 /* The new prefix to map to. */
311 const char *new_prefix
;
312 /* Whether at least one character is needed following opt1 or opt0
313 for this mapping to be used. (--optimize= is valid for -O, but
314 --warn- is not valid for -W.) */
315 bool another_char_needed
;
316 /* Whether the original option is a negated form of the option
317 resulting from this map. */
320 static const struct option_map option_map
[] =
322 { "-Wno-", NULL
, "-W", false, true },
323 { "-fno-", NULL
, "-f", false, true },
324 { "-mno-", NULL
, "-m", false, true },
325 { "--debug=", NULL
, "-g", false, false },
326 { "--machine-", NULL
, "-m", true, false },
327 { "--machine-no-", NULL
, "-m", false, true },
328 { "--machine=", NULL
, "-m", false, false },
329 { "--machine=no-", NULL
, "-m", false, true },
330 { "--machine", "", "-m", false, false },
331 { "--machine", "no-", "-m", false, true },
332 { "--optimize=", NULL
, "-O", false, false },
333 { "--std=", NULL
, "-std=", false, false },
334 { "--std", "", "-std=", false, false },
335 { "--warn-", NULL
, "-W", true, false },
336 { "--warn-no-", NULL
, "-W", false, true },
337 { "--", NULL
, "-f", true, false },
338 { "--no-", NULL
, "-f", false, true }
341 /* Decode the switch beginning at ARGV for the language indicated by
342 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
343 the structure *DECODED. Returns the number of switches
347 decode_cmdline_option (const char **argv
, unsigned int lang_mask
,
348 struct cl_decoded_option
*decoded
)
353 unsigned int result
= 1, i
, extra_args
, separate_args
= 0;
357 const struct cl_option
*option
;
359 const char *warn_message
= NULL
;
360 bool separate_arg_flag
;
361 bool joined_arg_flag
;
362 bool have_separate_arg
= false;
366 opt_index
= find_opt (argv
[0] + 1, lang_mask
);
368 while (opt_index
== OPT_SPECIAL_unknown
369 && i
< ARRAY_SIZE (option_map
))
371 const char *opt0
= option_map
[i
].opt0
;
372 const char *opt1
= option_map
[i
].opt1
;
373 const char *new_prefix
= option_map
[i
].new_prefix
;
374 bool another_char_needed
= option_map
[i
].another_char_needed
;
375 size_t opt0_len
= strlen (opt0
);
376 size_t opt1_len
= (opt1
== NULL
? 0 : strlen (opt1
));
377 size_t optn_len
= (opt1
== NULL
? opt0_len
: opt1_len
);
378 size_t new_prefix_len
= strlen (new_prefix
);
380 extra_args
= (opt1
== NULL
? 0 : 1);
381 value
= !option_map
[i
].negated
;
383 if (strncmp (argv
[0], opt0
, opt0_len
) == 0
385 || (argv
[1] != NULL
&& strncmp (argv
[1], opt1
, opt1_len
) == 0))
386 && (!another_char_needed
387 || argv
[extra_args
][optn_len
] != 0))
389 size_t arglen
= strlen (argv
[extra_args
]);
392 adjust_len
= (int) optn_len
- (int) new_prefix_len
;
393 dup
= XNEWVEC (char, arglen
+ 1 - adjust_len
);
394 memcpy (dup
, new_prefix
, new_prefix_len
);
395 memcpy (dup
+ new_prefix_len
, argv
[extra_args
] + optn_len
,
396 arglen
- optn_len
+ 1);
397 opt_index
= find_opt (dup
+ 1, lang_mask
);
403 if (opt_index
== OPT_SPECIAL_unknown
)
411 option
= &cl_options
[opt_index
];
413 /* Reject negative form of switches that don't take negatives as
415 if (!value
&& (option
->flags
& CL_REJECT_NEGATIVE
))
417 opt_index
= OPT_SPECIAL_unknown
;
418 errors
|= CL_ERR_NEGATIVE
;
423 result
= extra_args
+ 1;
424 warn_message
= option
->warn_message
;
426 /* Check to see if the option is disabled for this configuration. */
427 if (option
->flags
& CL_DISABLED
)
428 errors
|= CL_ERR_DISABLED
;
430 /* Determine whether there may be a separate argument based on
431 whether this option is being processed for the driver, and, if
432 so, how many such arguments. */
433 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
434 && !((option
->flags
& CL_NO_DRIVER_ARG
)
435 && (lang_mask
& CL_DRIVER
)));
436 separate_args
= (separate_arg_flag
437 ? ((option
->flags
& CL_SEPARATE_NARGS_MASK
)
438 >> CL_SEPARATE_NARGS_SHIFT
) + 1
440 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
442 /* Sort out any argument the switch takes. */
445 /* Have arg point to the original switch. This is because
446 some code, such as disable_builtin_function, expects its
447 argument to be persistent until the program exits. */
448 arg
= argv
[extra_args
] + cl_options
[opt_index
].opt_len
+ 1 + adjust_len
;
450 if (*arg
== '\0' && !(option
->flags
& CL_MISSING_OK
))
452 if (separate_arg_flag
)
454 arg
= argv
[extra_args
+ 1];
455 result
= extra_args
+ 2;
457 result
= extra_args
+ 1;
459 have_separate_arg
= true;
462 /* Missing argument. */
466 else if (separate_arg_flag
)
468 arg
= argv
[extra_args
+ 1];
469 for (i
= 0; i
< separate_args
; i
++)
470 if (argv
[extra_args
+ 1 + i
] == NULL
)
472 errors
|= CL_ERR_MISSING_ARG
;
475 result
= extra_args
+ 1 + i
;
477 have_separate_arg
= true;
480 if (arg
== NULL
&& (separate_arg_flag
|| joined_arg_flag
))
481 errors
|= CL_ERR_MISSING_ARG
;
483 /* Is this option an alias (or an ignored option, marked as an alias
484 of OPT_SPECIAL_ignore)? */
485 if (option
->alias_target
!= N_OPTS
486 && (!(option
->flags
& CL_SEPARATE_ALIAS
) || have_separate_arg
))
488 size_t new_opt_index
= option
->alias_target
;
490 if (new_opt_index
== OPT_SPECIAL_ignore
)
492 gcc_assert (option
->alias_arg
== NULL
);
493 gcc_assert (option
->neg_alias_arg
== NULL
);
494 opt_index
= new_opt_index
;
500 const struct cl_option
*new_option
= &cl_options
[new_opt_index
];
502 /* The new option must not be an alias itself. */
503 gcc_assert (new_option
->alias_target
== N_OPTS
504 || (new_option
->flags
& CL_SEPARATE_ALIAS
));
506 if (option
->neg_alias_arg
)
508 gcc_assert (option
->alias_arg
!= NULL
);
509 gcc_assert (arg
== NULL
);
511 arg
= option
->alias_arg
;
513 arg
= option
->neg_alias_arg
;
516 else if (option
->alias_arg
)
518 gcc_assert (value
== 1);
519 gcc_assert (arg
== NULL
);
520 arg
= option
->alias_arg
;
523 opt_index
= new_opt_index
;
527 gcc_assert (!(option
->flags
& CL_REJECT_NEGATIVE
));
529 /* Recompute what arguments are allowed. */
530 separate_arg_flag
= ((option
->flags
& CL_SEPARATE
)
531 && !((option
->flags
& CL_NO_DRIVER_ARG
)
532 && (lang_mask
& CL_DRIVER
)));
533 joined_arg_flag
= (option
->flags
& CL_JOINED
) != 0;
535 if (separate_args
> 1 || (option
->flags
& CL_SEPARATE_NARGS_MASK
))
536 gcc_assert (separate_args
537 == ((option
->flags
& CL_SEPARATE_NARGS_MASK
)
538 >> CL_SEPARATE_NARGS_SHIFT
) + 1);
540 if (!(errors
& CL_ERR_MISSING_ARG
))
542 if (separate_arg_flag
|| joined_arg_flag
)
544 if ((option
->flags
& CL_MISSING_OK
) && arg
== NULL
)
546 gcc_assert (arg
!= NULL
);
549 gcc_assert (arg
== NULL
);
552 /* Recheck for warnings and disabled options. */
553 if (option
->warn_message
)
555 gcc_assert (warn_message
== NULL
);
556 warn_message
= option
->warn_message
;
558 if (option
->flags
& CL_DISABLED
)
559 errors
|= CL_ERR_DISABLED
;
563 /* Check if this is a switch for a different front end. */
564 if (!option_ok_for_language (option
, lang_mask
))
565 errors
|= CL_ERR_WRONG_LANG
;
567 /* If the switch takes an integer, convert it. */
568 if (arg
&& (option
->flags
& CL_UINTEGER
))
570 value
= integral_argument (arg
);
572 errors
|= CL_ERR_UINT_ARG
;
575 /* If the switch takes an enumerated argument, convert it. */
576 if (arg
&& (option
->var_type
== CLVC_ENUM
))
578 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
580 gcc_assert (value
== 1);
581 if (enum_arg_to_value (e
->values
, arg
, &value
, lang_mask
))
583 const char *carg
= NULL
;
585 if (enum_value_to_arg (e
->values
, &carg
, value
, lang_mask
))
587 gcc_assert (carg
!= NULL
);
590 errors
|= CL_ERR_ENUM_ARG
;
594 decoded
->opt_index
= opt_index
;
596 decoded
->value
= value
;
597 decoded
->errors
= errors
;
598 decoded
->warn_message
= warn_message
;
600 if (opt_index
== OPT_SPECIAL_unknown
)
601 gcc_assert (result
== 1);
603 gcc_assert (result
>= 1 && result
<= ARRAY_SIZE (decoded
->canonical_option
));
604 decoded
->canonical_option_num_elements
= result
;
606 for (i
= 0; i
< ARRAY_SIZE (decoded
->canonical_option
); i
++)
610 if (opt_index
== OPT_SPECIAL_unknown
)
611 decoded
->canonical_option
[i
] = argv
[i
];
613 decoded
->canonical_option
[i
] = NULL
;
614 total_len
+= strlen (argv
[i
]) + 1;
617 decoded
->canonical_option
[i
] = NULL
;
619 if (opt_index
!= OPT_SPECIAL_unknown
&& opt_index
!= OPT_SPECIAL_ignore
)
621 generate_canonical_option (opt_index
, arg
, value
, decoded
);
622 if (separate_args
> 1)
624 for (i
= 0; i
< separate_args
; i
++)
626 if (argv
[extra_args
+ 1 + i
] == NULL
)
629 decoded
->canonical_option
[1 + i
] = argv
[extra_args
+ 1 + i
];
631 gcc_assert (result
== 1 + i
);
632 decoded
->canonical_option_num_elements
= result
;
635 decoded
->orig_option_with_args_text
= p
= XNEWVEC (char, total_len
);
636 for (i
= 0; i
< result
; i
++)
638 size_t len
= strlen (argv
[i
]);
640 memcpy (p
, argv
[i
], len
);
651 /* Decode command-line options (ARGC and ARGV being the arguments of
652 main) into an array, setting *DECODED_OPTIONS to a pointer to that
653 array and *DECODED_OPTIONS_COUNT to the number of entries in the
654 array. The first entry in the array is always one for the program
655 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
656 flags applicable for decoding (including CL_COMMON and CL_TARGET if
657 those options should be considered applicable). Do not produce any
658 diagnostics or set state outside of these variables. */
661 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
662 unsigned int lang_mask
,
663 struct cl_decoded_option
**decoded_options
,
664 unsigned int *decoded_options_count
)
667 struct cl_decoded_option
*opt_array
;
668 unsigned int num_decoded_options
;
669 bool argv_copied
= false;
671 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
673 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
674 opt_array
[0].warn_message
= NULL
;
675 opt_array
[0].arg
= argv
[0];
676 opt_array
[0].orig_option_with_args_text
= argv
[0];
677 opt_array
[0].canonical_option_num_elements
= 1;
678 opt_array
[0].canonical_option
[0] = argv
[0];
679 opt_array
[0].canonical_option
[1] = NULL
;
680 opt_array
[0].canonical_option
[2] = NULL
;
681 opt_array
[0].canonical_option
[3] = NULL
;
682 opt_array
[0].value
= 1;
683 opt_array
[0].errors
= 0;
684 num_decoded_options
= 1;
686 for (i
= 1; i
< argc
; i
+= n
)
688 const char *opt
= argv
[i
];
690 /* Interpret "-" or a non-switch as a file name. */
691 if (opt
[0] != '-' || opt
[1] == '\0')
693 generate_option_input_file (opt
, &opt_array
[num_decoded_options
]);
694 num_decoded_options
++;
699 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
700 &opt_array
[num_decoded_options
]);
701 num_decoded_options
++;
706 *decoded_options
= opt_array
;
707 *decoded_options_count
= num_decoded_options
;
708 prune_options (decoded_options
, decoded_options_count
);
711 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
712 next one is the same as ORIG_NEXT_OPT_IDX. */
715 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
717 /* An option can be canceled by the same option or an option with
719 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
722 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
723 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
729 /* Filter out options canceled by the ones after them. */
732 prune_options (struct cl_decoded_option
**decoded_options
,
733 unsigned int *decoded_options_count
)
735 unsigned int old_decoded_options_count
= *decoded_options_count
;
736 struct cl_decoded_option
*old_decoded_options
= *decoded_options
;
737 unsigned int new_decoded_options_count
;
738 struct cl_decoded_option
*new_decoded_options
739 = XNEWVEC (struct cl_decoded_option
, old_decoded_options_count
);
741 const struct cl_option
*option
;
743 /* Remove arguments which are negated by others after them. */
744 new_decoded_options_count
= 0;
745 for (i
= 0; i
< old_decoded_options_count
; i
++)
747 unsigned int j
, opt_idx
, next_opt_idx
;
749 if (old_decoded_options
[i
].errors
& ~CL_ERR_WRONG_LANG
)
752 opt_idx
= old_decoded_options
[i
].opt_index
;
755 case OPT_SPECIAL_unknown
:
756 case OPT_SPECIAL_ignore
:
757 case OPT_SPECIAL_program_name
:
758 case OPT_SPECIAL_input_file
:
762 gcc_assert (opt_idx
< cl_options_count
);
763 option
= &cl_options
[opt_idx
];
764 if (option
->neg_index
< 0)
767 /* Skip joined switches. */
768 if ((option
->flags
& CL_JOINED
))
771 for (j
= i
+ 1; j
< old_decoded_options_count
; j
++)
773 if (old_decoded_options
[j
].errors
& ~CL_ERR_WRONG_LANG
)
775 next_opt_idx
= old_decoded_options
[j
].opt_index
;
776 if (next_opt_idx
>= cl_options_count
)
778 if (cl_options
[next_opt_idx
].neg_index
< 0)
780 if ((cl_options
[next_opt_idx
].flags
& CL_JOINED
))
782 if (cancel_option (opt_idx
, next_opt_idx
, next_opt_idx
))
785 if (j
== old_decoded_options_count
)
788 new_decoded_options
[new_decoded_options_count
]
789 = old_decoded_options
[i
];
790 new_decoded_options_count
++;
796 free (old_decoded_options
);
797 new_decoded_options
= XRESIZEVEC (struct cl_decoded_option
,
799 new_decoded_options_count
);
800 *decoded_options
= new_decoded_options
;
801 *decoded_options_count
= new_decoded_options_count
;
804 /* Handle option DECODED for the language indicated by LANG_MASK,
805 using the handlers in HANDLERS and setting fields in OPTS and
806 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
807 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
808 option for options from the source file, UNKNOWN_LOCATION
809 otherwise. GENERATED_P is true for an option generated as part of
810 processing another option or otherwise generated internally, false
811 for one explicitly passed by the user. Returns false if the switch
812 was invalid. DC is the diagnostic context for options affecting
813 diagnostics state, or NULL. */
816 handle_option (struct gcc_options
*opts
,
817 struct gcc_options
*opts_set
,
818 const struct cl_decoded_option
*decoded
,
819 unsigned int lang_mask
, int kind
, location_t loc
,
820 const struct cl_option_handlers
*handlers
,
821 bool generated_p
, diagnostic_context
*dc
)
823 size_t opt_index
= decoded
->opt_index
;
824 const char *arg
= decoded
->arg
;
825 int value
= decoded
->value
;
826 const struct cl_option
*option
= &cl_options
[opt_index
];
827 void *flag_var
= option_flag_var (opt_index
, opts
);
831 set_option (opts
, (generated_p
? NULL
: opts_set
),
832 opt_index
, value
, arg
, kind
, loc
, dc
);
834 for (i
= 0; i
< handlers
->num_handlers
; i
++)
835 if (option
->flags
& handlers
->handlers
[i
].mask
)
837 if (!handlers
->handlers
[i
].handler (opts
, opts_set
, decoded
,
838 lang_mask
, kind
, loc
,
842 handlers
->post_handling_callback (decoded
,
843 handlers
->handlers
[i
].mask
);
849 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
850 option instead of DECODED. This is used for callbacks when one
851 option implies another instead of an option being decoded from the
855 handle_generated_option (struct gcc_options
*opts
,
856 struct gcc_options
*opts_set
,
857 size_t opt_index
, const char *arg
, int value
,
858 unsigned int lang_mask
, int kind
, location_t loc
,
859 const struct cl_option_handlers
*handlers
,
860 diagnostic_context
*dc
)
862 struct cl_decoded_option decoded
;
864 generate_option (opt_index
, arg
, value
, lang_mask
, &decoded
);
865 return handle_option (opts
, opts_set
, &decoded
, lang_mask
, kind
, loc
,
869 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
870 VALUE for a front end using LANG_MASK. This is used when the
871 compiler generates options internally. */
874 generate_option (size_t opt_index
, const char *arg
, int value
,
875 unsigned int lang_mask
, struct cl_decoded_option
*decoded
)
877 const struct cl_option
*option
= &cl_options
[opt_index
];
879 decoded
->opt_index
= opt_index
;
880 decoded
->warn_message
= NULL
;
882 decoded
->value
= value
;
883 decoded
->errors
= (option_ok_for_language (option
, lang_mask
)
885 : CL_ERR_WRONG_LANG
);
887 generate_canonical_option (opt_index
, arg
, value
, decoded
);
888 switch (decoded
->canonical_option_num_elements
)
891 decoded
->orig_option_with_args_text
= decoded
->canonical_option
[0];
895 decoded
->orig_option_with_args_text
896 = concat (decoded
->canonical_option
[0], " ",
897 decoded
->canonical_option
[1], NULL
);
905 /* Fill in *DECODED with an option for input file FILE. */
908 generate_option_input_file (const char *file
,
909 struct cl_decoded_option
*decoded
)
911 decoded
->opt_index
= OPT_SPECIAL_input_file
;
912 decoded
->warn_message
= NULL
;
914 decoded
->orig_option_with_args_text
= file
;
915 decoded
->canonical_option_num_elements
= 1;
916 decoded
->canonical_option
[0] = file
;
917 decoded
->canonical_option
[1] = NULL
;
918 decoded
->canonical_option
[2] = NULL
;
919 decoded
->canonical_option
[3] = NULL
;
924 /* Handle the switch DECODED (location LOC) for the language indicated
925 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
926 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
927 diagnostic options. */
930 read_cmdline_option (struct gcc_options
*opts
,
931 struct gcc_options
*opts_set
,
932 struct cl_decoded_option
*decoded
,
934 unsigned int lang_mask
,
935 const struct cl_option_handlers
*handlers
,
936 diagnostic_context
*dc
)
938 const struct cl_option
*option
;
939 const char *opt
= decoded
->orig_option_with_args_text
;
941 if (decoded
->warn_message
)
942 warning_at (loc
, 0, decoded
->warn_message
, opt
);
944 if (decoded
->opt_index
== OPT_SPECIAL_unknown
)
946 if (handlers
->unknown_option_callback (decoded
))
947 error_at (loc
, "unrecognized command line option %qs", decoded
->arg
);
951 if (decoded
->opt_index
== OPT_SPECIAL_ignore
)
954 option
= &cl_options
[decoded
->opt_index
];
956 if (decoded
->errors
& CL_ERR_DISABLED
)
958 error_at (loc
, "command line option %qs"
959 " is not supported by this configuration", opt
);
963 if (decoded
->errors
& CL_ERR_WRONG_LANG
)
965 handlers
->wrong_lang_callback (decoded
, lang_mask
);
969 if (decoded
->errors
& CL_ERR_MISSING_ARG
)
971 if (option
->missing_argument_error
)
972 error_at (loc
, option
->missing_argument_error
, opt
);
974 error_at (loc
, "missing argument to %qs", opt
);
978 if (decoded
->errors
& CL_ERR_UINT_ARG
)
980 error_at (loc
, "argument to %qs should be a non-negative integer",
985 if (decoded
->errors
& CL_ERR_ENUM_ARG
)
987 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
992 if (e
->unknown_error
)
993 error_at (loc
, e
->unknown_error
, decoded
->arg
);
995 error_at (loc
, "unrecognized argument in option %qs", opt
);
998 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
999 len
+= strlen (e
->values
[i
].arg
) + 1;
1001 s
= XALLOCAVEC (char, len
);
1003 for (i
= 0; e
->values
[i
].arg
!= NULL
; i
++)
1005 size_t arglen
= strlen (e
->values
[i
].arg
);
1006 memcpy (p
, e
->values
[i
].arg
, arglen
);
1011 inform (loc
, "valid arguments to %qs are: %s", option
->opt_text
, s
);
1015 gcc_assert (!decoded
->errors
);
1017 if (!handle_option (opts
, opts_set
, decoded
, lang_mask
, DK_UNSPECIFIED
,
1018 loc
, handlers
, false, dc
))
1019 error_at (loc
, "unrecognized command line option %qs", opt
);
1022 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1023 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1024 location LOC, using diagnostic context DC if not NULL for
1025 diagnostic classification. */
1028 set_option (struct gcc_options
*opts
, struct gcc_options
*opts_set
,
1029 int opt_index
, int value
, const char *arg
, int kind
,
1030 location_t loc
, diagnostic_context
*dc
)
1032 const struct cl_option
*option
= &cl_options
[opt_index
];
1033 void *flag_var
= option_flag_var (opt_index
, opts
);
1034 void *set_flag_var
= NULL
;
1039 if (opts_set
!= NULL
)
1040 set_flag_var
= option_flag_var (opt_index
, opts_set
);
1042 switch (option
->var_type
)
1045 *(int *) flag_var
= value
;
1047 *(int *) set_flag_var
= 1;
1051 *(int *) flag_var
= (value
1053 : !option
->var_value
);
1055 *(int *) set_flag_var
= 1;
1058 case CLVC_BIT_CLEAR
:
1060 if ((value
!= 0) == (option
->var_type
== CLVC_BIT_SET
))
1061 *(int *) flag_var
|= option
->var_value
;
1063 *(int *) flag_var
&= ~option
->var_value
;
1065 *(int *) set_flag_var
|= option
->var_value
;
1069 *(const char **) flag_var
= arg
;
1071 *(const char **) set_flag_var
= "";
1076 const struct cl_enum
*e
= &cl_enums
[option
->var_enum
];
1078 e
->set (flag_var
, value
);
1080 e
->set (set_flag_var
, 1);
1086 VEC(cl_deferred_option
,heap
) *vec
1087 = (VEC(cl_deferred_option
,heap
) *) *(void **) flag_var
;
1088 cl_deferred_option
*p
;
1090 p
= VEC_safe_push (cl_deferred_option
, heap
, vec
, NULL
);
1091 p
->opt_index
= opt_index
;
1094 *(void **) flag_var
= vec
;
1096 *(void **) set_flag_var
= vec
;
1101 if ((diagnostic_t
) kind
!= DK_UNSPECIFIED
1103 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1106 /* Return the address of the flag variable for option OPT_INDEX in
1107 options structure OPTS, or NULL if there is no flag variable. */
1110 option_flag_var (int opt_index
, struct gcc_options
*opts
)
1112 const struct cl_option
*option
= &cl_options
[opt_index
];
1114 if (option
->flag_var_offset
== (unsigned short) -1)
1116 return (void *)(((char *) opts
) + option
->flag_var_offset
);
1119 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1120 or -1 if it isn't a simple on-off switch. */
1123 option_enabled (int opt_idx
, void *opts
)
1125 const struct cl_option
*option
= &(cl_options
[opt_idx
]);
1126 struct gcc_options
*optsg
= (struct gcc_options
*) opts
;
1127 void *flag_var
= option_flag_var (opt_idx
, optsg
);
1130 switch (option
->var_type
)
1133 return *(int *) flag_var
!= 0;
1136 return *(int *) flag_var
== option
->var_value
;
1138 case CLVC_BIT_CLEAR
:
1139 return (*(int *) flag_var
& option
->var_value
) == 0;
1142 return (*(int *) flag_var
& option
->var_value
) != 0;
1152 /* Fill STATE with the current state of option OPTION in OPTS. Return
1153 true if there is some state to store. */
1156 get_option_state (struct gcc_options
*opts
, int option
,
1157 struct cl_option_state
*state
)
1159 void *flag_var
= option_flag_var (option
, opts
);
1164 switch (cl_options
[option
].var_type
)
1168 state
->data
= flag_var
;
1169 state
->size
= sizeof (int);
1172 case CLVC_BIT_CLEAR
:
1174 state
->ch
= option_enabled (option
, opts
);
1175 state
->data
= &state
->ch
;
1180 state
->data
= *(const char **) flag_var
;
1181 if (state
->data
== 0)
1183 state
->size
= strlen ((const char *) state
->data
) + 1;
1187 state
->data
= flag_var
;
1188 state
->size
= cl_enums
[cl_options
[option
].var_enum
].var_size
;
1197 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1198 handlers HANDLERS) to have diagnostic kind KIND for option
1199 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1200 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1201 the warning option in question is implied at this point. This is
1202 used by -Werror= and #pragma GCC diagnostic. */
1205 control_warning_option (unsigned int opt_index
, int kind
, bool imply
,
1206 location_t loc
, unsigned int lang_mask
,
1207 const struct cl_option_handlers
*handlers
,
1208 struct gcc_options
*opts
,
1209 struct gcc_options
*opts_set
,
1210 diagnostic_context
*dc
)
1212 if (cl_options
[opt_index
].alias_target
!= N_OPTS
)
1213 opt_index
= cl_options
[opt_index
].alias_target
;
1214 if (opt_index
== OPT_SPECIAL_ignore
)
1217 diagnostic_classify_diagnostic (dc
, opt_index
, (diagnostic_t
) kind
, loc
);
1220 /* -Werror=foo implies -Wfoo. */
1221 if (cl_options
[opt_index
].var_type
== CLVC_BOOLEAN
)
1222 handle_generated_option (opts
, opts_set
,
1223 opt_index
, NULL
, 1, lang_mask
,
1224 kind
, loc
, handlers
, dc
);