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"
27 /* Perform a binary search to find which option the command-line INPUT
28 matches. Returns its index in the option array, and
29 OPT_SPECIAL_unknown on failure.
31 This routine is quite subtle. A normal binary search is not good
32 enough because some options can be suffixed with an argument, and
33 multiple sub-matches can occur, e.g. input of "-pedantic" matching
34 the initial substring of "-pedantic-errors".
36 A more complicated example is -gstabs. It should match "-g" with
37 an argument of "stabs". Suppose, however, that the number and list
38 of switches are such that the binary search tests "-gen-decls"
39 before having tested "-g". This doesn't match, and as "-gen-decls"
40 is less than "-gstabs", it will become the lower bound of the
41 binary search range, and "-g" will never be seen. To resolve this
42 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
43 to "-g" so that failed searches that end between "-gen-decls" and
44 the lexicographically subsequent switch know to go back and see if
45 "-g" causes a match (which it does in this example).
47 This search is done in such a way that the longest match for the
48 front end in question wins. If there is no match for the current
49 front end, the longest match for a different front end is returned
50 (or N_OPTS if none) and the caller emits an error message. */
52 find_opt (const char *input
, int lang_mask
)
54 size_t mn
, mx
, md
, opt_len
;
55 size_t match_wrong_lang
;
59 mx
= cl_options_count
;
61 /* Find mn such this lexicographical inequality holds:
62 cl_options[mn] <= input < cl_options[mn + 1]. */
66 opt_len
= cl_options
[md
].opt_len
;
67 comp
= strncmp (input
, cl_options
[md
].opt_text
+ 1, opt_len
);
75 /* This is the switch that is the best match but for a different
76 front end, or OPT_SPECIAL_unknown if there is no match at all. */
77 match_wrong_lang
= OPT_SPECIAL_unknown
;
79 /* Backtrace the chain of possible matches, returning the longest
80 one, if any, that fits best. With current GCC switches, this
81 loop executes at most twice. */
84 const struct cl_option
*opt
= &cl_options
[mn
];
86 /* Is the input either an exact match or a prefix that takes a
88 if (!strncmp (input
, opt
->opt_text
+ 1, opt
->opt_len
)
89 && (input
[opt
->opt_len
] == '\0' || (opt
->flags
& CL_JOINED
)))
91 /* If language is OK, return it. */
92 if (opt
->flags
& lang_mask
)
95 /* If we haven't remembered a prior match, remember this
96 one. Any prior match is necessarily better. */
97 if (match_wrong_lang
== OPT_SPECIAL_unknown
)
98 match_wrong_lang
= mn
;
101 /* Try the next possibility. This is cl_options_count if there
103 mn
= opt
->back_chain
;
105 while (mn
!= cl_options_count
);
107 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
108 return match_wrong_lang
;
111 /* If ARG is a non-negative integer made up solely of digits, return its
112 value, otherwise return -1. */
115 integral_argument (const char *arg
)
119 while (*p
&& ISDIGIT (*p
))
128 /* Decode the switch beginning at ARGV for the language indicated by
129 LANG_MASK, into the structure *DECODED. Returns the number of
130 switches consumed. */
133 decode_cmdline_option (const char **argv
, unsigned int lang_mask
,
134 struct cl_decoded_option
*decoded
)
137 const char *opt
, *arg
= 0;
140 unsigned int result
= 1;
141 const struct cl_option
*option
;
146 opt_index
= find_opt (opt
+ 1, lang_mask
| CL_COMMON
| CL_TARGET
);
147 if (opt_index
== OPT_SPECIAL_unknown
148 && (opt
[1] == 'W' || opt
[1] == 'f' || opt
[1] == 'm')
149 && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-')
151 /* Drop the "no-" from negative switches. */
152 size_t len
= strlen (opt
) - 3;
154 dup
= XNEWVEC (char, len
+ 1);
157 memcpy (dup
+ 2, opt
+ 5, len
- 2 + 1);
160 opt_index
= find_opt (opt
+ 1, lang_mask
| CL_COMMON
| CL_TARGET
);
163 if (opt_index
== OPT_SPECIAL_unknown
)
169 option
= &cl_options
[opt_index
];
171 /* Reject negative form of switches that don't take negatives as
173 if (!value
&& (option
->flags
& CL_REJECT_NEGATIVE
))
175 opt_index
= OPT_SPECIAL_unknown
;
180 /* Check to see if the option is disabled for this configuration. */
181 if (option
->flags
& CL_DISABLED
)
182 errors
|= CL_ERR_DISABLED
;
184 /* Sort out any argument the switch takes. */
185 if (option
->flags
& CL_JOINED
)
187 /* Have arg point to the original switch. This is because
188 some code, such as disable_builtin_function, expects its
189 argument to be persistent until the program exits. */
190 arg
= argv
[0] + cl_options
[opt_index
].opt_len
+ 1;
192 arg
+= strlen ("no-");
194 if (*arg
== '\0' && !(option
->flags
& CL_MISSING_OK
))
196 if (option
->flags
& CL_SEPARATE
)
204 /* Missing argument. */
208 else if (option
->flags
& CL_SEPARATE
)
216 /* Check if this is a switch for a different front end. */
217 if (!(option
->flags
& (lang_mask
| CL_COMMON
| CL_TARGET
)))
218 errors
|= CL_ERR_WRONG_LANG
;
219 else if ((option
->flags
& CL_TARGET
)
220 && (option
->flags
& CL_LANG_ALL
)
221 && !(option
->flags
& lang_mask
))
222 /* Complain for target flag language mismatches if any languages
224 errors
|= CL_ERR_WRONG_LANG
;
226 if (arg
== NULL
&& (option
->flags
& (CL_JOINED
| CL_SEPARATE
)))
227 errors
|= CL_ERR_MISSING_ARG
;
229 /* If the switch takes an integer, convert it. */
230 if (arg
&& (option
->flags
& CL_UINTEGER
))
232 value
= integral_argument (arg
);
234 errors
|= CL_ERR_UINT_ARG
;
240 decoded
->opt_index
= opt_index
;
242 decoded
->value
= value
;
243 decoded
->errors
= errors
;
247 decoded
->orig_option_with_args_text
= argv
[0];
248 decoded
->canonical_option
[0] = argv
[0];
249 decoded
->canonical_option
[1] = NULL
;
252 decoded
->orig_option_with_args_text
= concat (argv
[0], " ",
254 decoded
->canonical_option
[0] = argv
[0];
255 decoded
->canonical_option
[1] = argv
[1];
263 /* Decode command-line options (ARGC and ARGV being the arguments of
264 main) into an array, setting *DECODED_OPTIONS to a pointer to that
265 array and *DECODED_OPTIONS_COUNT to the number of entries in the
266 array. The first entry in the array is always one for the program
267 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
268 applicable for decoding. Do not produce any diagnostics or set
269 state outside of these variables. */
272 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
273 unsigned int lang_mask
,
274 struct cl_decoded_option
**decoded_options
,
275 unsigned int *decoded_options_count
)
278 struct cl_decoded_option
*opt_array
;
279 unsigned int num_decoded_options
;
281 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
283 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
284 opt_array
[0].arg
= argv
[0];
285 opt_array
[0].orig_option_with_args_text
= argv
[0];
286 opt_array
[0].canonical_option
[0] = argv
[0];
287 opt_array
[0].canonical_option
[1] = NULL
;
288 opt_array
[0].value
= 1;
289 opt_array
[0].errors
= 0;
290 num_decoded_options
= 1;
292 for (i
= 1; i
< argc
; i
+= n
)
294 const char *opt
= argv
[i
];
296 /* Interpret "-" or a non-switch as a file name. */
297 if (opt
[0] != '-' || opt
[1] == '\0')
299 opt_array
[num_decoded_options
].opt_index
= OPT_SPECIAL_input_file
;
300 opt_array
[num_decoded_options
].arg
= opt
;
301 opt_array
[num_decoded_options
].orig_option_with_args_text
= opt
;
302 opt_array
[num_decoded_options
].canonical_option
[0] = opt
;
303 opt_array
[num_decoded_options
].canonical_option
[1] = NULL
;
304 opt_array
[num_decoded_options
].value
= 1;
305 opt_array
[num_decoded_options
].errors
= 0;
306 num_decoded_options
++;
311 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
312 &opt_array
[num_decoded_options
]);
313 num_decoded_options
++;
316 opt_array
= XRESIZEVEC (struct cl_decoded_option
, opt_array
,
317 num_decoded_options
);
318 *decoded_options
= opt_array
;
319 *decoded_options_count
= num_decoded_options
;
322 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
323 next one is the same as ORIG_NEXT_OPT_IDX. */
326 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
328 /* An option can be canceled by the same option or an option with
330 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
333 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
334 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
340 /* Filter out options canceled by the ones after them. */
343 prune_options (int *argcp
, char ***argvp
)
346 int *options
= XNEWVEC (int, argc
);
347 /* We will only return this replacement argv if we remove at least
348 one argument, so it does not need to be size (argc + 1) to
349 make room for the terminating NULL because we will always have
350 freed up at least one slot when we end up using it at all. */
351 char **argv
= XNEWVEC (char *, argc
);
352 int i
, arg_count
, need_prune
= 0;
353 const struct cl_option
*option
;
356 /* Scan all arguments. */
357 for (i
= 1; i
< argc
; i
++)
360 const char *opt
= (*argvp
) [i
];
362 opt_index
= find_opt (opt
+ 1, -1);
363 if (opt_index
== OPT_SPECIAL_unknown
364 && (opt
[1] == 'W' || opt
[1] == 'f' || opt
[1] == 'm')
365 && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-')
369 /* Drop the "no-" from negative switches. */
370 size_t len
= strlen (opt
) - 3;
372 dup
= XNEWVEC (char, len
+ 1);
375 memcpy (dup
+ 2, opt
+ 5, len
- 2 + 1);
378 opt_index
= find_opt (opt
+ 1, -1);
382 if (opt_index
== OPT_SPECIAL_unknown
)
389 option
= &cl_options
[opt_index
];
390 if (option
->neg_index
< 0)
393 /* Skip joined switches. */
394 if ((option
->flags
& CL_JOINED
))
397 /* Reject negative form of switches that don't take negatives as
399 if (!value
&& (option
->flags
& CL_REJECT_NEGATIVE
))
402 options
[i
] = (int) opt_index
;
403 need_prune
|= options
[i
];
409 /* Remove arguments which are negated by others after them. */
410 argv
[0] = (*argvp
) [0];
412 for (i
= 1; i
< argc
; i
++)
416 opt_idx
= options
[i
];
420 for (j
= i
+ 1; j
< argc
; j
++)
422 next_opt_idx
= options
[j
];
424 && cancel_option (opt_idx
, next_opt_idx
,
435 argv
[arg_count
] = (*argvp
) [i
];
440 if (arg_count
!= argc
)
444 /* Add NULL-termination. Guaranteed not to overflow because
445 arg_count here can only be less than argc. */