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];
250 decoded
->orig_option_with_args_text
= concat (argv
[0], " ",
259 /* Decode command-line options (ARGC and ARGV being the arguments of
260 main) into an array, setting *DECODED_OPTIONS to a pointer to that
261 array and *DECODED_OPTIONS_COUNT to the number of entries in the
262 array. The first entry in the array is always one for the program
263 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
264 applicable for decoding. Do not produce any diagnostics or set
265 state outside of these variables. */
268 decode_cmdline_options_to_array (unsigned int argc
, const char **argv
,
269 unsigned int lang_mask
,
270 struct cl_decoded_option
**decoded_options
,
271 unsigned int *decoded_options_count
)
274 struct cl_decoded_option
*opt_array
;
275 unsigned int num_decoded_options
;
277 opt_array
= XNEWVEC (struct cl_decoded_option
, argc
);
279 opt_array
[0].opt_index
= OPT_SPECIAL_program_name
;
280 opt_array
[0].arg
= argv
[0];
281 opt_array
[0].orig_option_with_args_text
= argv
[0];
282 opt_array
[0].value
= 1;
283 opt_array
[0].errors
= 0;
284 num_decoded_options
= 1;
286 for (i
= 1; i
< argc
; i
+= n
)
288 const char *opt
= argv
[i
];
290 /* Interpret "-" or a non-switch as a file name. */
291 if (opt
[0] != '-' || opt
[1] == '\0')
293 opt_array
[num_decoded_options
].opt_index
= OPT_SPECIAL_input_file
;
294 opt_array
[num_decoded_options
].arg
= opt
;
295 opt_array
[num_decoded_options
].orig_option_with_args_text
= opt
;
296 opt_array
[num_decoded_options
].value
= 1;
297 opt_array
[num_decoded_options
].errors
= 0;
298 num_decoded_options
++;
303 n
= decode_cmdline_option (argv
+ i
, lang_mask
,
304 &opt_array
[num_decoded_options
]);
305 num_decoded_options
++;
308 opt_array
= XRESIZEVEC (struct cl_decoded_option
, opt_array
,
309 num_decoded_options
);
310 *decoded_options
= opt_array
;
311 *decoded_options_count
= num_decoded_options
;
314 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
315 next one is the same as ORIG_NEXT_OPT_IDX. */
318 cancel_option (int opt_idx
, int next_opt_idx
, int orig_next_opt_idx
)
320 /* An option can be canceled by the same option or an option with
322 if (cl_options
[next_opt_idx
].neg_index
== opt_idx
)
325 if (cl_options
[next_opt_idx
].neg_index
!= orig_next_opt_idx
)
326 return cancel_option (opt_idx
, cl_options
[next_opt_idx
].neg_index
,
332 /* Filter out options canceled by the ones after them. */
335 prune_options (int *argcp
, char ***argvp
)
338 int *options
= XNEWVEC (int, argc
);
339 /* We will only return this replacement argv if we remove at least
340 one argument, so it does not need to be size (argc + 1) to
341 make room for the terminating NULL because we will always have
342 freed up at least one slot when we end up using it at all. */
343 char **argv
= XNEWVEC (char *, argc
);
344 int i
, arg_count
, need_prune
= 0;
345 const struct cl_option
*option
;
348 /* Scan all arguments. */
349 for (i
= 1; i
< argc
; i
++)
352 const char *opt
= (*argvp
) [i
];
354 opt_index
= find_opt (opt
+ 1, -1);
355 if (opt_index
== OPT_SPECIAL_unknown
356 && (opt
[1] == 'W' || opt
[1] == 'f' || opt
[1] == 'm')
357 && opt
[2] == 'n' && opt
[3] == 'o' && opt
[4] == '-')
361 /* Drop the "no-" from negative switches. */
362 size_t len
= strlen (opt
) - 3;
364 dup
= XNEWVEC (char, len
+ 1);
367 memcpy (dup
+ 2, opt
+ 5, len
- 2 + 1);
370 opt_index
= find_opt (opt
+ 1, -1);
374 if (opt_index
== OPT_SPECIAL_unknown
)
381 option
= &cl_options
[opt_index
];
382 if (option
->neg_index
< 0)
385 /* Skip joined switches. */
386 if ((option
->flags
& CL_JOINED
))
389 /* Reject negative form of switches that don't take negatives as
391 if (!value
&& (option
->flags
& CL_REJECT_NEGATIVE
))
394 options
[i
] = (int) opt_index
;
395 need_prune
|= options
[i
];
401 /* Remove arguments which are negated by others after them. */
402 argv
[0] = (*argvp
) [0];
404 for (i
= 1; i
< argc
; i
++)
408 opt_idx
= options
[i
];
412 for (j
= i
+ 1; j
< argc
; j
++)
414 next_opt_idx
= options
[j
];
416 && cancel_option (opt_idx
, next_opt_idx
,
427 argv
[arg_count
] = (*argvp
) [i
];
432 if (arg_count
!= argc
)
436 /* Add NULL-termination. Guaranteed not to overflow because
437 arg_count here can only be less than argc. */