]>
Commit | Line | Data |
---|---|---|
14c7833c | 1 | /* Command line option handling. |
eb50f63a | 2 | Copyright (C) 2006, 2007, 2008, 2010, 2011 Free Software Foundation, Inc. |
14c7833c L |
3 | |
4 | This file is part of GCC. | |
5 | ||
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 | |
9dcd6f09 | 8 | Software Foundation; either version 3, or (at your option) any later |
14c7833c L |
9 | version. |
10 | ||
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 | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
14c7833c L |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "intl.h" | |
23 | #include "coretypes.h" | |
24 | #include "opts.h" | |
a75bfaa6 | 25 | #include "flags.h" |
5f20c657 | 26 | #include "diagnostic.h" |
14c7833c | 27 | |
60cf253a JM |
28 | static void prune_options (struct cl_decoded_option **, unsigned int *); |
29 | ||
14c7833c | 30 | /* Perform a binary search to find which option the command-line INPUT |
6e2f1956 JM |
31 | matches. Returns its index in the option array, and |
32 | OPT_SPECIAL_unknown on failure. | |
14c7833c L |
33 | |
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". | |
38 | ||
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 | |
af47e6ac | 45 | issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member, |
14c7833c L |
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). | |
49 | ||
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. */ | |
54 | size_t | |
eb50f63a | 55 | find_opt (const char *input, unsigned int lang_mask) |
14c7833c | 56 | { |
e200444e | 57 | size_t mn, mn_orig, mx, md, opt_len; |
14c7833c L |
58 | size_t match_wrong_lang; |
59 | int comp; | |
60 | ||
61 | mn = 0; | |
62 | mx = cl_options_count; | |
63 | ||
64 | /* Find mn such this lexicographical inequality holds: | |
65 | cl_options[mn] <= input < cl_options[mn + 1]. */ | |
66 | while (mx - mn > 1) | |
67 | { | |
68 | md = (mn + mx) / 2; | |
69 | opt_len = cl_options[md].opt_len; | |
70 | comp = strncmp (input, cl_options[md].opt_text + 1, opt_len); | |
71 | ||
72 | if (comp < 0) | |
73 | mx = md; | |
74 | else | |
75 | mn = md; | |
76 | } | |
77 | ||
e200444e JM |
78 | mn_orig = mn; |
79 | ||
14c7833c | 80 | /* This is the switch that is the best match but for a different |
6e2f1956 JM |
81 | front end, or OPT_SPECIAL_unknown if there is no match at all. */ |
82 | match_wrong_lang = OPT_SPECIAL_unknown; | |
14c7833c L |
83 | |
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. */ | |
87 | do | |
88 | { | |
89 | const struct cl_option *opt = &cl_options[mn]; | |
90 | ||
91 | /* Is the input either an exact match or a prefix that takes a | |
92 | joined argument? */ | |
93 | if (!strncmp (input, opt->opt_text + 1, opt->opt_len) | |
94 | && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED))) | |
95 | { | |
96 | /* If language is OK, return it. */ | |
97 | if (opt->flags & lang_mask) | |
98 | return mn; | |
99 | ||
100 | /* If we haven't remembered a prior match, remember this | |
101 | one. Any prior match is necessarily better. */ | |
6e2f1956 | 102 | if (match_wrong_lang == OPT_SPECIAL_unknown) |
14c7833c L |
103 | match_wrong_lang = mn; |
104 | } | |
105 | ||
106 | /* Try the next possibility. This is cl_options_count if there | |
107 | are no more. */ | |
108 | mn = opt->back_chain; | |
109 | } | |
110 | while (mn != cl_options_count); | |
111 | ||
e200444e JM |
112 | if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-') |
113 | { | |
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) | |
122 | { | |
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 | |
127 | ambiguous. */ | |
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. */ | |
140 | else | |
141 | return OPT_SPECIAL_unknown; | |
142 | mnc++; | |
143 | } | |
144 | } | |
145 | ||
6e2f1956 | 146 | /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */ |
14c7833c L |
147 | return match_wrong_lang; |
148 | } | |
149 | ||
5d4b393f JM |
150 | /* If ARG is a non-negative integer made up solely of digits, return its |
151 | value, otherwise return -1. */ | |
152 | ||
153 | int | |
154 | integral_argument (const char *arg) | |
155 | { | |
156 | const char *p = arg; | |
157 | ||
158 | while (*p && ISDIGIT (*p)) | |
159 | p++; | |
160 | ||
161 | if (*p == '\0') | |
162 | return atoi (arg); | |
163 | ||
164 | return -1; | |
165 | } | |
166 | ||
d9d16a19 JM |
167 | /* Return whether OPTION is OK for the language given by |
168 | LANG_MASK. */ | |
169 | static bool | |
170 | option_ok_for_language (const struct cl_option *option, | |
171 | unsigned int lang_mask) | |
172 | { | |
173 | if (!(option->flags & lang_mask)) | |
174 | return false; | |
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 | |
179 | are specified. */ | |
180 | return false; | |
181 | return true; | |
182 | } | |
183 | ||
e6d4b984 JM |
184 | /* Return whether ENUM_ARG is OK for the language given by |
185 | LANG_MASK. */ | |
186 | ||
187 | static bool | |
188 | enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg, | |
189 | unsigned int lang_mask) | |
190 | { | |
191 | return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY); | |
192 | } | |
193 | ||
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. */ | |
197 | ||
198 | static bool | |
199 | enum_arg_to_value (const struct cl_enum_arg *enum_args, | |
200 | const char *arg, int *value, unsigned int lang_mask) | |
201 | { | |
202 | unsigned int i; | |
203 | ||
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)) | |
207 | { | |
208 | *value = enum_args[i].value; | |
209 | return true; | |
210 | } | |
211 | ||
212 | return false; | |
213 | } | |
214 | ||
8023568e JM |
215 | /* Look up ARG in the enum used by option OPT_INDEX for language |
216 | LANG_MASK, returning true and storing the value in *VALUE if found, | |
217 | and returning false without modifying *VALUE if not found. */ | |
218 | ||
219 | bool | |
220 | opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value, | |
221 | unsigned int lang_mask) | |
222 | { | |
223 | const struct cl_option *option = &cl_options[opt_index]; | |
224 | ||
225 | gcc_assert (option->var_type == CLVC_ENUM); | |
226 | ||
227 | return enum_arg_to_value (cl_enums[option->var_enum].values, arg, | |
228 | value, lang_mask); | |
229 | } | |
230 | ||
e6d4b984 JM |
231 | /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the |
232 | corresponding string in *ARGP, returning true if the found string | |
233 | was marked as canonical, false otherwise. If VALUE is not found | |
234 | (which may be the case for uninitialized values if the relevant | |
235 | option has not been passed), set *ARGP to NULL and return | |
236 | false. */ | |
237 | ||
238 | bool | |
239 | enum_value_to_arg (const struct cl_enum_arg *enum_args, | |
240 | const char **argp, int value, unsigned int lang_mask) | |
241 | { | |
242 | unsigned int i; | |
243 | ||
244 | for (i = 0; enum_args[i].arg != NULL; i++) | |
245 | if (enum_args[i].value == value | |
246 | && (enum_args[i].flags & CL_ENUM_CANONICAL) | |
247 | && enum_arg_ok_for_language (&enum_args[i], lang_mask)) | |
248 | { | |
249 | *argp = enum_args[i].arg; | |
250 | return true; | |
251 | } | |
252 | ||
253 | for (i = 0; enum_args[i].arg != NULL; i++) | |
254 | if (enum_args[i].value == value | |
255 | && enum_arg_ok_for_language (&enum_args[i], lang_mask)) | |
256 | { | |
257 | *argp = enum_args[i].arg; | |
258 | return false; | |
259 | } | |
260 | ||
261 | *argp = NULL; | |
262 | return false; | |
263 | } | |
5de8299c JM |
264 | |
265 | /* Fill in the canonical option part of *DECODED with an option | |
266 | described by OPT_INDEX, ARG and VALUE. */ | |
267 | ||
268 | static void | |
269 | generate_canonical_option (size_t opt_index, const char *arg, int value, | |
270 | struct cl_decoded_option *decoded) | |
271 | { | |
272 | const struct cl_option *option = &cl_options[opt_index]; | |
273 | const char *opt_text = option->opt_text; | |
274 | ||
275 | if (value == 0 | |
300d83d9 | 276 | && !option->cl_reject_negative |
5de8299c JM |
277 | && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm')) |
278 | { | |
279 | char *t = XNEWVEC (char, option->opt_len + 5); | |
280 | t[0] = '-'; | |
281 | t[1] = opt_text[1]; | |
282 | t[2] = 'n'; | |
283 | t[3] = 'o'; | |
284 | t[4] = '-'; | |
285 | memcpy (t + 5, opt_text + 2, option->opt_len); | |
286 | opt_text = t; | |
287 | } | |
288 | ||
289 | decoded->canonical_option[2] = NULL; | |
290 | decoded->canonical_option[3] = NULL; | |
291 | ||
292 | if (arg) | |
293 | { | |
d1583032 | 294 | if ((option->flags & CL_SEPARATE) |
300d83d9 | 295 | && !option->cl_separate_alias) |
5de8299c JM |
296 | { |
297 | decoded->canonical_option[0] = opt_text; | |
298 | decoded->canonical_option[1] = arg; | |
299 | decoded->canonical_option_num_elements = 2; | |
300 | } | |
301 | else | |
302 | { | |
303 | gcc_assert (option->flags & CL_JOINED); | |
304 | decoded->canonical_option[0] = concat (opt_text, arg, NULL); | |
305 | decoded->canonical_option[1] = NULL; | |
306 | decoded->canonical_option_num_elements = 1; | |
307 | } | |
308 | } | |
309 | else | |
310 | { | |
311 | decoded->canonical_option[0] = opt_text; | |
312 | decoded->canonical_option[1] = NULL; | |
313 | decoded->canonical_option_num_elements = 1; | |
314 | } | |
315 | } | |
316 | ||
e200444e JM |
317 | /* Structure describing mappings from options on the command line to |
318 | options to look up with find_opt. */ | |
319 | struct option_map | |
320 | { | |
321 | /* Prefix of the option on the command line. */ | |
322 | const char *opt0; | |
323 | /* If two argv elements are considered to be merged into one option, | |
324 | prefix for the second element, otherwise NULL. */ | |
325 | const char *opt1; | |
326 | /* The new prefix to map to. */ | |
327 | const char *new_prefix; | |
328 | /* Whether at least one character is needed following opt1 or opt0 | |
329 | for this mapping to be used. (--optimize= is valid for -O, but | |
330 | --warn- is not valid for -W.) */ | |
331 | bool another_char_needed; | |
332 | /* Whether the original option is a negated form of the option | |
333 | resulting from this map. */ | |
334 | bool negated; | |
335 | }; | |
336 | static const struct option_map option_map[] = | |
337 | { | |
338 | { "-Wno-", NULL, "-W", false, true }, | |
339 | { "-fno-", NULL, "-f", false, true }, | |
340 | { "-mno-", NULL, "-m", false, true }, | |
341 | { "--debug=", NULL, "-g", false, false }, | |
342 | { "--machine-", NULL, "-m", true, false }, | |
343 | { "--machine-no-", NULL, "-m", false, true }, | |
344 | { "--machine=", NULL, "-m", false, false }, | |
345 | { "--machine=no-", NULL, "-m", false, true }, | |
346 | { "--machine", "", "-m", false, false }, | |
347 | { "--machine", "no-", "-m", false, true }, | |
348 | { "--optimize=", NULL, "-O", false, false }, | |
349 | { "--std=", NULL, "-std=", false, false }, | |
350 | { "--std", "", "-std=", false, false }, | |
351 | { "--warn-", NULL, "-W", true, false }, | |
352 | { "--warn-no-", NULL, "-W", false, true }, | |
353 | { "--", NULL, "-f", true, false }, | |
354 | { "--no-", NULL, "-f", false, true } | |
355 | }; | |
356 | ||
5d4b393f | 357 | /* Decode the switch beginning at ARGV for the language indicated by |
603349bf JM |
358 | LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into |
359 | the structure *DECODED. Returns the number of switches | |
360 | consumed. */ | |
5d4b393f | 361 | |
6e2f1956 | 362 | static unsigned int |
5d4b393f JM |
363 | decode_cmdline_option (const char **argv, unsigned int lang_mask, |
364 | struct cl_decoded_option *decoded) | |
365 | { | |
366 | size_t opt_index; | |
e200444e | 367 | const char *arg = 0; |
5d4b393f | 368 | int value = 1; |
c8967d95 | 369 | unsigned int result = 1, i, extra_args, separate_args = 0; |
e200444e | 370 | int adjust_len = 0; |
eea13ead JM |
371 | size_t total_len; |
372 | char *p; | |
5d4b393f JM |
373 | const struct cl_option *option; |
374 | int errors = 0; | |
2d2bd949 | 375 | const char *warn_message = NULL; |
c878765b JM |
376 | bool separate_arg_flag; |
377 | bool joined_arg_flag; | |
d1583032 | 378 | bool have_separate_arg = false; |
5d4b393f | 379 | |
e200444e | 380 | extra_args = 0; |
5d4b393f | 381 | |
e200444e JM |
382 | opt_index = find_opt (argv[0] + 1, lang_mask); |
383 | i = 0; | |
384 | while (opt_index == OPT_SPECIAL_unknown | |
385 | && i < ARRAY_SIZE (option_map)) | |
5d4b393f | 386 | { |
e200444e JM |
387 | const char *opt0 = option_map[i].opt0; |
388 | const char *opt1 = option_map[i].opt1; | |
389 | const char *new_prefix = option_map[i].new_prefix; | |
390 | bool another_char_needed = option_map[i].another_char_needed; | |
391 | size_t opt0_len = strlen (opt0); | |
392 | size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1)); | |
393 | size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len); | |
394 | size_t new_prefix_len = strlen (new_prefix); | |
395 | ||
396 | extra_args = (opt1 == NULL ? 0 : 1); | |
397 | value = !option_map[i].negated; | |
398 | ||
399 | if (strncmp (argv[0], opt0, opt0_len) == 0 | |
400 | && (opt1 == NULL | |
401 | || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0)) | |
402 | && (!another_char_needed | |
403 | || argv[extra_args][optn_len] != 0)) | |
404 | { | |
405 | size_t arglen = strlen (argv[extra_args]); | |
406 | char *dup; | |
407 | ||
408 | adjust_len = (int) optn_len - (int) new_prefix_len; | |
409 | dup = XNEWVEC (char, arglen + 1 - adjust_len); | |
410 | memcpy (dup, new_prefix, new_prefix_len); | |
411 | memcpy (dup + new_prefix_len, argv[extra_args] + optn_len, | |
412 | arglen - optn_len + 1); | |
413 | opt_index = find_opt (dup + 1, lang_mask); | |
414 | free (dup); | |
415 | } | |
416 | i++; | |
5d4b393f JM |
417 | } |
418 | ||
6e2f1956 JM |
419 | if (opt_index == OPT_SPECIAL_unknown) |
420 | { | |
421 | arg = argv[0]; | |
e200444e JM |
422 | extra_args = 0; |
423 | value = 1; | |
6e2f1956 JM |
424 | goto done; |
425 | } | |
5d4b393f JM |
426 | |
427 | option = &cl_options[opt_index]; | |
428 | ||
429 | /* Reject negative form of switches that don't take negatives as | |
430 | unrecognized. */ | |
300d83d9 | 431 | if (!value && option->cl_reject_negative) |
5d4b393f | 432 | { |
6e2f1956 | 433 | opt_index = OPT_SPECIAL_unknown; |
184eb658 | 434 | errors |= CL_ERR_NEGATIVE; |
6e2f1956 | 435 | arg = argv[0]; |
5d4b393f JM |
436 | goto done; |
437 | } | |
438 | ||
e200444e | 439 | result = extra_args + 1; |
2d2bd949 JM |
440 | warn_message = option->warn_message; |
441 | ||
5d4b393f | 442 | /* Check to see if the option is disabled for this configuration. */ |
300d83d9 | 443 | if (option->cl_disabled) |
5d4b393f JM |
444 | errors |= CL_ERR_DISABLED; |
445 | ||
c878765b | 446 | /* Determine whether there may be a separate argument based on |
c243beb0 JM |
447 | whether this option is being processed for the driver, and, if |
448 | so, how many such arguments. */ | |
c878765b | 449 | separate_arg_flag = ((option->flags & CL_SEPARATE) |
300d83d9 | 450 | && !(option->cl_no_driver_arg |
c878765b | 451 | && (lang_mask & CL_DRIVER))); |
c243beb0 | 452 | separate_args = (separate_arg_flag |
300d83d9 | 453 | ? option->cl_separate_nargs + 1 |
c243beb0 | 454 | : 0); |
c878765b JM |
455 | joined_arg_flag = (option->flags & CL_JOINED) != 0; |
456 | ||
5d4b393f | 457 | /* Sort out any argument the switch takes. */ |
c878765b | 458 | if (joined_arg_flag) |
5d4b393f JM |
459 | { |
460 | /* Have arg point to the original switch. This is because | |
461 | some code, such as disable_builtin_function, expects its | |
462 | argument to be persistent until the program exits. */ | |
e200444e | 463 | arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len; |
5d4b393f | 464 | |
300d83d9 | 465 | if (*arg == '\0' && !option->cl_missing_ok) |
5d4b393f | 466 | { |
c878765b | 467 | if (separate_arg_flag) |
5d4b393f | 468 | { |
e200444e JM |
469 | arg = argv[extra_args + 1]; |
470 | result = extra_args + 2; | |
5d4b393f | 471 | if (arg == NULL) |
e200444e | 472 | result = extra_args + 1; |
d1583032 JM |
473 | else |
474 | have_separate_arg = true; | |
5d4b393f JM |
475 | } |
476 | else | |
477 | /* Missing argument. */ | |
478 | arg = NULL; | |
479 | } | |
480 | } | |
c878765b | 481 | else if (separate_arg_flag) |
5d4b393f | 482 | { |
e200444e | 483 | arg = argv[extra_args + 1]; |
c243beb0 JM |
484 | for (i = 0; i < separate_args; i++) |
485 | if (argv[extra_args + 1 + i] == NULL) | |
486 | { | |
487 | errors |= CL_ERR_MISSING_ARG; | |
488 | break; | |
489 | } | |
490 | result = extra_args + 1 + i; | |
491 | if (arg != NULL) | |
d1583032 | 492 | have_separate_arg = true; |
5d4b393f JM |
493 | } |
494 | ||
5de8299c JM |
495 | if (arg == NULL && (separate_arg_flag || joined_arg_flag)) |
496 | errors |= CL_ERR_MISSING_ARG; | |
497 | ||
2d2bd949 JM |
498 | /* Is this option an alias (or an ignored option, marked as an alias |
499 | of OPT_SPECIAL_ignore)? */ | |
d1583032 | 500 | if (option->alias_target != N_OPTS |
300d83d9 | 501 | && (!option->cl_separate_alias || have_separate_arg)) |
5de8299c JM |
502 | { |
503 | size_t new_opt_index = option->alias_target; | |
5de8299c | 504 | |
2d2bd949 | 505 | if (new_opt_index == OPT_SPECIAL_ignore) |
5de8299c | 506 | { |
2d2bd949 JM |
507 | gcc_assert (option->alias_arg == NULL); |
508 | gcc_assert (option->neg_alias_arg == NULL); | |
509 | opt_index = new_opt_index; | |
510 | arg = NULL; | |
5de8299c JM |
511 | value = 1; |
512 | } | |
2d2bd949 | 513 | else |
5de8299c | 514 | { |
2d2bd949 | 515 | const struct cl_option *new_option = &cl_options[new_opt_index]; |
5de8299c | 516 | |
2d2bd949 | 517 | /* The new option must not be an alias itself. */ |
e200444e | 518 | gcc_assert (new_option->alias_target == N_OPTS |
300d83d9 | 519 | || new_option->cl_separate_alias); |
5de8299c | 520 | |
2d2bd949 JM |
521 | if (option->neg_alias_arg) |
522 | { | |
523 | gcc_assert (option->alias_arg != NULL); | |
524 | gcc_assert (arg == NULL); | |
300d83d9 | 525 | gcc_assert (!option->cl_negative_alias); |
2d2bd949 JM |
526 | if (value) |
527 | arg = option->alias_arg; | |
528 | else | |
529 | arg = option->neg_alias_arg; | |
530 | value = 1; | |
531 | } | |
532 | else if (option->alias_arg) | |
533 | { | |
534 | gcc_assert (value == 1); | |
535 | gcc_assert (arg == NULL); | |
300d83d9 | 536 | gcc_assert (!option->cl_negative_alias); |
2d2bd949 JM |
537 | arg = option->alias_arg; |
538 | } | |
5de8299c | 539 | |
300d83d9 | 540 | if (option->cl_negative_alias) |
666a21a2 JM |
541 | value = !value; |
542 | ||
2d2bd949 JM |
543 | opt_index = new_opt_index; |
544 | option = new_option; | |
5de8299c | 545 | |
2d2bd949 | 546 | if (value == 0) |
300d83d9 | 547 | gcc_assert (!option->cl_reject_negative); |
2d2bd949 JM |
548 | |
549 | /* Recompute what arguments are allowed. */ | |
550 | separate_arg_flag = ((option->flags & CL_SEPARATE) | |
300d83d9 | 551 | && !(option->cl_no_driver_arg |
2d2bd949 JM |
552 | && (lang_mask & CL_DRIVER))); |
553 | joined_arg_flag = (option->flags & CL_JOINED) != 0; | |
554 | ||
300d83d9 | 555 | if (separate_args > 1 || option->cl_separate_nargs) |
c243beb0 | 556 | gcc_assert (separate_args |
300d83d9 | 557 | == (unsigned int) option->cl_separate_nargs + 1); |
c243beb0 | 558 | |
2d2bd949 JM |
559 | if (!(errors & CL_ERR_MISSING_ARG)) |
560 | { | |
561 | if (separate_arg_flag || joined_arg_flag) | |
e200444e | 562 | { |
300d83d9 | 563 | if (option->cl_missing_ok && arg == NULL) |
e200444e JM |
564 | arg = ""; |
565 | gcc_assert (arg != NULL); | |
566 | } | |
2d2bd949 JM |
567 | else |
568 | gcc_assert (arg == NULL); | |
569 | } | |
5de8299c | 570 | |
2d2bd949 JM |
571 | /* Recheck for warnings and disabled options. */ |
572 | if (option->warn_message) | |
573 | { | |
574 | gcc_assert (warn_message == NULL); | |
575 | warn_message = option->warn_message; | |
576 | } | |
300d83d9 | 577 | if (option->cl_disabled) |
2d2bd949 JM |
578 | errors |= CL_ERR_DISABLED; |
579 | } | |
5de8299c JM |
580 | } |
581 | ||
5d4b393f | 582 | /* Check if this is a switch for a different front end. */ |
d9d16a19 | 583 | if (!option_ok_for_language (option, lang_mask)) |
5d4b393f | 584 | errors |= CL_ERR_WRONG_LANG; |
5d4b393f | 585 | |
413519ae JM |
586 | /* Convert the argument to lowercase if appropriate. */ |
587 | if (arg && option->cl_tolower) | |
588 | { | |
589 | size_t j; | |
590 | size_t len = strlen (arg); | |
591 | char *arg_lower = XNEWVEC (char, len + 1); | |
592 | ||
593 | for (j = 0; j < len; j++) | |
594 | arg_lower[j] = TOLOWER ((unsigned char) arg[j]); | |
595 | arg_lower[len] = 0; | |
596 | arg = arg_lower; | |
597 | } | |
598 | ||
5d4b393f | 599 | /* If the switch takes an integer, convert it. */ |
300d83d9 | 600 | if (arg && option->cl_uinteger) |
5d4b393f JM |
601 | { |
602 | value = integral_argument (arg); | |
603 | if (value == -1) | |
604 | errors |= CL_ERR_UINT_ARG; | |
605 | } | |
606 | ||
e6d4b984 JM |
607 | /* If the switch takes an enumerated argument, convert it. */ |
608 | if (arg && (option->var_type == CLVC_ENUM)) | |
609 | { | |
610 | const struct cl_enum *e = &cl_enums[option->var_enum]; | |
611 | ||
612 | gcc_assert (value == 1); | |
613 | if (enum_arg_to_value (e->values, arg, &value, lang_mask)) | |
614 | { | |
615 | const char *carg = NULL; | |
616 | ||
617 | if (enum_value_to_arg (e->values, &carg, value, lang_mask)) | |
618 | arg = carg; | |
619 | gcc_assert (carg != NULL); | |
620 | } | |
621 | else | |
622 | errors |= CL_ERR_ENUM_ARG; | |
623 | } | |
624 | ||
5d4b393f | 625 | done: |
5d4b393f JM |
626 | decoded->opt_index = opt_index; |
627 | decoded->arg = arg; | |
628 | decoded->value = value; | |
629 | decoded->errors = errors; | |
2d2bd949 | 630 | decoded->warn_message = warn_message; |
eea13ead JM |
631 | |
632 | if (opt_index == OPT_SPECIAL_unknown) | |
c243beb0 | 633 | gcc_assert (result == 1); |
eea13ead JM |
634 | |
635 | gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option)); | |
636 | decoded->canonical_option_num_elements = result; | |
637 | total_len = 0; | |
638 | for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++) | |
639 | { | |
640 | if (i < result) | |
641 | { | |
7fcf46f5 | 642 | size_t len; |
5de8299c JM |
643 | if (opt_index == OPT_SPECIAL_unknown) |
644 | decoded->canonical_option[i] = argv[i]; | |
645 | else | |
646 | decoded->canonical_option[i] = NULL; | |
7fcf46f5 JZ |
647 | len = strlen (argv[i]); |
648 | /* If the argument is an empty string, we will print it as "" in | |
649 | orig_option_with_args_text. */ | |
650 | total_len += (len != 0 ? len : 2) + 1; | |
eea13ead JM |
651 | } |
652 | else | |
653 | decoded->canonical_option[i] = NULL; | |
654 | } | |
2d2bd949 | 655 | if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore) |
c243beb0 JM |
656 | { |
657 | generate_canonical_option (opt_index, arg, value, decoded); | |
658 | if (separate_args > 1) | |
659 | { | |
660 | for (i = 0; i < separate_args; i++) | |
661 | { | |
662 | if (argv[extra_args + 1 + i] == NULL) | |
663 | break; | |
664 | else | |
665 | decoded->canonical_option[1 + i] = argv[extra_args + 1 + i]; | |
666 | } | |
667 | gcc_assert (result == 1 + i); | |
668 | decoded->canonical_option_num_elements = result; | |
669 | } | |
670 | } | |
eea13ead JM |
671 | decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len); |
672 | for (i = 0; i < result; i++) | |
673 | { | |
674 | size_t len = strlen (argv[i]); | |
675 | ||
7fcf46f5 JZ |
676 | /* Print the empty string verbally. */ |
677 | if (len == 0) | |
678 | { | |
679 | *p++ = '"'; | |
680 | *p++ = '"'; | |
681 | } | |
682 | else | |
683 | memcpy (p, argv[i], len); | |
eea13ead JM |
684 | p += len; |
685 | if (i == result - 1) | |
686 | *p++ = 0; | |
687 | else | |
688 | *p++ = ' '; | |
689 | } | |
690 | ||
5d4b393f JM |
691 | return result; |
692 | } | |
693 | ||
6e2f1956 JM |
694 | /* Decode command-line options (ARGC and ARGV being the arguments of |
695 | main) into an array, setting *DECODED_OPTIONS to a pointer to that | |
696 | array and *DECODED_OPTIONS_COUNT to the number of entries in the | |
697 | array. The first entry in the array is always one for the program | |
698 | name (OPT_SPECIAL_program_name). LANG_MASK indicates the language | |
603349bf JM |
699 | flags applicable for decoding (including CL_COMMON and CL_TARGET if |
700 | those options should be considered applicable). Do not produce any | |
701 | diagnostics or set state outside of these variables. */ | |
6e2f1956 JM |
702 | |
703 | void | |
704 | decode_cmdline_options_to_array (unsigned int argc, const char **argv, | |
705 | unsigned int lang_mask, | |
706 | struct cl_decoded_option **decoded_options, | |
707 | unsigned int *decoded_options_count) | |
708 | { | |
2be55a25 | 709 | unsigned int n, i; |
6e2f1956 JM |
710 | struct cl_decoded_option *opt_array; |
711 | unsigned int num_decoded_options; | |
712 | ||
713 | opt_array = XNEWVEC (struct cl_decoded_option, argc); | |
714 | ||
715 | opt_array[0].opt_index = OPT_SPECIAL_program_name; | |
2d2bd949 | 716 | opt_array[0].warn_message = NULL; |
6e2f1956 JM |
717 | opt_array[0].arg = argv[0]; |
718 | opt_array[0].orig_option_with_args_text = argv[0]; | |
eea13ead | 719 | opt_array[0].canonical_option_num_elements = 1; |
7a9bf9a4 JM |
720 | opt_array[0].canonical_option[0] = argv[0]; |
721 | opt_array[0].canonical_option[1] = NULL; | |
eea13ead JM |
722 | opt_array[0].canonical_option[2] = NULL; |
723 | opt_array[0].canonical_option[3] = NULL; | |
6e2f1956 JM |
724 | opt_array[0].value = 1; |
725 | opt_array[0].errors = 0; | |
726 | num_decoded_options = 1; | |
727 | ||
728 | for (i = 1; i < argc; i += n) | |
729 | { | |
730 | const char *opt = argv[i]; | |
731 | ||
732 | /* Interpret "-" or a non-switch as a file name. */ | |
733 | if (opt[0] != '-' || opt[1] == '\0') | |
734 | { | |
d9d16a19 | 735 | generate_option_input_file (opt, &opt_array[num_decoded_options]); |
6e2f1956 JM |
736 | num_decoded_options++; |
737 | n = 1; | |
738 | continue; | |
739 | } | |
740 | ||
741 | n = decode_cmdline_option (argv + i, lang_mask, | |
742 | &opt_array[num_decoded_options]); | |
743 | num_decoded_options++; | |
744 | } | |
745 | ||
6e2f1956 JM |
746 | *decoded_options = opt_array; |
747 | *decoded_options_count = num_decoded_options; | |
60cf253a | 748 | prune_options (decoded_options, decoded_options_count); |
6e2f1956 JM |
749 | } |
750 | ||
14c7833c L |
751 | /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the |
752 | next one is the same as ORIG_NEXT_OPT_IDX. */ | |
753 | ||
754 | static bool | |
755 | cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx) | |
756 | { | |
757 | /* An option can be canceled by the same option or an option with | |
758 | Negative. */ | |
759 | if (cl_options [next_opt_idx].neg_index == opt_idx) | |
760 | return true; | |
761 | ||
762 | if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx) | |
763 | return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index, | |
764 | orig_next_opt_idx); | |
b8698a0f | 765 | |
14c7833c L |
766 | return false; |
767 | } | |
768 | ||
769 | /* Filter out options canceled by the ones after them. */ | |
770 | ||
60cf253a JM |
771 | static void |
772 | prune_options (struct cl_decoded_option **decoded_options, | |
773 | unsigned int *decoded_options_count) | |
14c7833c | 774 | { |
60cf253a JM |
775 | unsigned int old_decoded_options_count = *decoded_options_count; |
776 | struct cl_decoded_option *old_decoded_options = *decoded_options; | |
777 | unsigned int new_decoded_options_count; | |
778 | struct cl_decoded_option *new_decoded_options | |
779 | = XNEWVEC (struct cl_decoded_option, old_decoded_options_count); | |
780 | unsigned int i; | |
14c7833c | 781 | const struct cl_option *option; |
14c7833c L |
782 | |
783 | /* Remove arguments which are negated by others after them. */ | |
60cf253a JM |
784 | new_decoded_options_count = 0; |
785 | for (i = 0; i < old_decoded_options_count; i++) | |
14c7833c | 786 | { |
60cf253a JM |
787 | unsigned int j, opt_idx, next_opt_idx; |
788 | ||
789 | if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG) | |
790 | goto keep; | |
14c7833c | 791 | |
60cf253a JM |
792 | opt_idx = old_decoded_options[i].opt_index; |
793 | switch (opt_idx) | |
14c7833c | 794 | { |
60cf253a JM |
795 | case OPT_SPECIAL_unknown: |
796 | case OPT_SPECIAL_ignore: | |
797 | case OPT_SPECIAL_program_name: | |
798 | case OPT_SPECIAL_input_file: | |
799 | goto keep; | |
800 | ||
801 | default: | |
802 | gcc_assert (opt_idx < cl_options_count); | |
803 | option = &cl_options[opt_idx]; | |
804 | if (option->neg_index < 0) | |
805 | goto keep; | |
806 | ||
807 | /* Skip joined switches. */ | |
808 | if ((option->flags & CL_JOINED)) | |
809 | goto keep; | |
810 | ||
811 | for (j = i + 1; j < old_decoded_options_count; j++) | |
14c7833c | 812 | { |
60cf253a JM |
813 | if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG) |
814 | continue; | |
815 | next_opt_idx = old_decoded_options[j].opt_index; | |
816 | if (next_opt_idx >= cl_options_count) | |
817 | continue; | |
818 | if (cl_options[next_opt_idx].neg_index < 0) | |
819 | continue; | |
820 | if ((cl_options[next_opt_idx].flags & CL_JOINED)) | |
821 | continue; | |
822 | if (cancel_option (opt_idx, next_opt_idx, next_opt_idx)) | |
14c7833c L |
823 | break; |
824 | } | |
60cf253a JM |
825 | if (j == old_decoded_options_count) |
826 | { | |
14c7833c | 827 | keep: |
60cf253a JM |
828 | new_decoded_options[new_decoded_options_count] |
829 | = old_decoded_options[i]; | |
830 | new_decoded_options_count++; | |
831 | } | |
832 | break; | |
14c7833c L |
833 | } |
834 | } | |
835 | ||
60cf253a JM |
836 | free (old_decoded_options); |
837 | new_decoded_options = XRESIZEVEC (struct cl_decoded_option, | |
838 | new_decoded_options, | |
839 | new_decoded_options_count); | |
840 | *decoded_options = new_decoded_options; | |
841 | *decoded_options_count = new_decoded_options_count; | |
14c7833c | 842 | } |
5f20c657 | 843 | |
481e1176 | 844 | /* Handle option DECODED for the language indicated by LANG_MASK, |
d4d24ba4 JM |
845 | using the handlers in HANDLERS and setting fields in OPTS and |
846 | OPTS_SET. KIND is the diagnostic_t if this is a diagnostics | |
a4d8c676 JM |
847 | option, DK_UNSPECIFIED otherwise, and LOC is the location of the |
848 | option for options from the source file, UNKNOWN_LOCATION | |
849 | otherwise. GENERATED_P is true for an option generated as part of | |
850 | processing another option or otherwise generated internally, false | |
851 | for one explicitly passed by the user. Returns false if the switch | |
852 | was invalid. DC is the diagnostic context for options affecting | |
853 | diagnostics state, or NULL. */ | |
5f20c657 | 854 | |
a4d8c676 | 855 | static bool |
46625112 | 856 | handle_option (struct gcc_options *opts, |
d4d24ba4 | 857 | struct gcc_options *opts_set, |
46625112 | 858 | const struct cl_decoded_option *decoded, |
a4d8c676 | 859 | unsigned int lang_mask, int kind, location_t loc, |
d4d24ba4 | 860 | const struct cl_option_handlers *handlers, |
1ebe4b4f | 861 | bool generated_p, diagnostic_context *dc) |
5f20c657 | 862 | { |
481e1176 JM |
863 | size_t opt_index = decoded->opt_index; |
864 | const char *arg = decoded->arg; | |
865 | int value = decoded->value; | |
5f20c657 | 866 | const struct cl_option *option = &cl_options[opt_index]; |
46625112 | 867 | void *flag_var = option_flag_var (opt_index, opts); |
5f20c657 JM |
868 | size_t i; |
869 | ||
46625112 | 870 | if (flag_var) |
d4d24ba4 | 871 | set_option (opts, (generated_p ? NULL : opts_set), |
a4d8c676 | 872 | opt_index, value, arg, kind, loc, dc); |
5f20c657 JM |
873 | |
874 | for (i = 0; i < handlers->num_handlers; i++) | |
875 | if (option->flags & handlers->handlers[i].mask) | |
876 | { | |
d4d24ba4 | 877 | if (!handlers->handlers[i].handler (opts, opts_set, decoded, |
a4d8c676 JM |
878 | lang_mask, kind, loc, |
879 | handlers, dc)) | |
5f20c657 JM |
880 | return false; |
881 | else | |
481e1176 | 882 | handlers->post_handling_callback (decoded, |
5f20c657 JM |
883 | handlers->handlers[i].mask); |
884 | } | |
885 | ||
886 | return true; | |
887 | } | |
888 | ||
481e1176 JM |
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 | |
892 | command line. */ | |
893 | ||
894 | bool | |
d4d24ba4 JM |
895 | handle_generated_option (struct gcc_options *opts, |
896 | struct gcc_options *opts_set, | |
897 | size_t opt_index, const char *arg, int value, | |
a4d8c676 | 898 | unsigned int lang_mask, int kind, location_t loc, |
1ebe4b4f JM |
899 | const struct cl_option_handlers *handlers, |
900 | diagnostic_context *dc) | |
481e1176 | 901 | { |
481e1176 JM |
902 | struct cl_decoded_option decoded; |
903 | ||
d9d16a19 | 904 | generate_option (opt_index, arg, value, lang_mask, &decoded); |
a4d8c676 JM |
905 | return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc, |
906 | handlers, true, dc); | |
d9d16a19 JM |
907 | } |
908 | ||
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. */ | |
912 | ||
913 | void | |
914 | generate_option (size_t opt_index, const char *arg, int value, | |
915 | unsigned int lang_mask, struct cl_decoded_option *decoded) | |
916 | { | |
917 | const struct cl_option *option = &cl_options[opt_index]; | |
918 | ||
919 | decoded->opt_index = opt_index; | |
2d2bd949 | 920 | decoded->warn_message = NULL; |
d9d16a19 | 921 | decoded->arg = arg; |
d9d16a19 JM |
922 | decoded->value = value; |
923 | decoded->errors = (option_ok_for_language (option, lang_mask) | |
924 | ? 0 | |
925 | : CL_ERR_WRONG_LANG); | |
481e1176 | 926 | |
5de8299c JM |
927 | generate_canonical_option (opt_index, arg, value, decoded); |
928 | switch (decoded->canonical_option_num_elements) | |
481e1176 | 929 | { |
5de8299c JM |
930 | case 1: |
931 | decoded->orig_option_with_args_text = decoded->canonical_option[0]; | |
932 | break; | |
933 | ||
934 | case 2: | |
935 | decoded->orig_option_with_args_text | |
936 | = concat (decoded->canonical_option[0], " ", | |
937 | decoded->canonical_option[1], NULL); | |
938 | break; | |
939 | ||
940 | default: | |
941 | gcc_unreachable (); | |
481e1176 | 942 | } |
d9d16a19 | 943 | } |
481e1176 | 944 | |
d9d16a19 JM |
945 | /* Fill in *DECODED with an option for input file FILE. */ |
946 | ||
947 | void | |
948 | generate_option_input_file (const char *file, | |
949 | struct cl_decoded_option *decoded) | |
950 | { | |
951 | decoded->opt_index = OPT_SPECIAL_input_file; | |
2d2bd949 | 952 | decoded->warn_message = NULL; |
d9d16a19 JM |
953 | decoded->arg = file; |
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; | |
960 | decoded->value = 1; | |
961 | decoded->errors = 0; | |
481e1176 JM |
962 | } |
963 | ||
a4d8c676 JM |
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 | |
1ebe4b4f | 967 | diagnostic options. */ |
5f20c657 JM |
968 | |
969 | void | |
46625112 | 970 | read_cmdline_option (struct gcc_options *opts, |
d4d24ba4 | 971 | struct gcc_options *opts_set, |
46625112 | 972 | struct cl_decoded_option *decoded, |
a4d8c676 | 973 | location_t loc, |
5f20c657 | 974 | unsigned int lang_mask, |
1ebe4b4f JM |
975 | const struct cl_option_handlers *handlers, |
976 | diagnostic_context *dc) | |
5f20c657 JM |
977 | { |
978 | const struct cl_option *option; | |
2d2bd949 JM |
979 | const char *opt = decoded->orig_option_with_args_text; |
980 | ||
981 | if (decoded->warn_message) | |
a4d8c676 | 982 | warning_at (loc, 0, decoded->warn_message, opt); |
5f20c657 JM |
983 | |
984 | if (decoded->opt_index == OPT_SPECIAL_unknown) | |
985 | { | |
481e1176 | 986 | if (handlers->unknown_option_callback (decoded)) |
a4d8c676 | 987 | error_at (loc, "unrecognized command line option %qs", decoded->arg); |
5f20c657 JM |
988 | return; |
989 | } | |
990 | ||
2d2bd949 JM |
991 | if (decoded->opt_index == OPT_SPECIAL_ignore) |
992 | return; | |
993 | ||
5f20c657 | 994 | option = &cl_options[decoded->opt_index]; |
5f20c657 JM |
995 | |
996 | if (decoded->errors & CL_ERR_DISABLED) | |
997 | { | |
a4d8c676 JM |
998 | error_at (loc, "command line option %qs" |
999 | " is not supported by this configuration", opt); | |
5f20c657 JM |
1000 | return; |
1001 | } | |
1002 | ||
5f20c657 JM |
1003 | if (decoded->errors & CL_ERR_MISSING_ARG) |
1004 | { | |
1005 | if (option->missing_argument_error) | |
a4d8c676 | 1006 | error_at (loc, option->missing_argument_error, opt); |
5f20c657 | 1007 | else |
a4d8c676 | 1008 | error_at (loc, "missing argument to %qs", opt); |
5f20c657 JM |
1009 | return; |
1010 | } | |
1011 | ||
1012 | if (decoded->errors & CL_ERR_UINT_ARG) | |
1013 | { | |
a4d8c676 JM |
1014 | error_at (loc, "argument to %qs should be a non-negative integer", |
1015 | option->opt_text); | |
5f20c657 JM |
1016 | return; |
1017 | } | |
1018 | ||
e6d4b984 JM |
1019 | if (decoded->errors & CL_ERR_ENUM_ARG) |
1020 | { | |
1021 | const struct cl_enum *e = &cl_enums[option->var_enum]; | |
1022 | unsigned int i; | |
1023 | size_t len; | |
1024 | char *s, *p; | |
1025 | ||
1026 | if (e->unknown_error) | |
1027 | error_at (loc, e->unknown_error, decoded->arg); | |
1028 | else | |
1029 | error_at (loc, "unrecognized argument in option %qs", opt); | |
1030 | ||
1031 | len = 0; | |
1032 | for (i = 0; e->values[i].arg != NULL; i++) | |
1033 | len += strlen (e->values[i].arg) + 1; | |
1034 | ||
1035 | s = XALLOCAVEC (char, len); | |
1036 | p = s; | |
1037 | for (i = 0; e->values[i].arg != NULL; i++) | |
1038 | { | |
1039 | size_t arglen = strlen (e->values[i].arg); | |
1040 | memcpy (p, e->values[i].arg, arglen); | |
1041 | p[arglen] = ' '; | |
1042 | p += arglen + 1; | |
1043 | } | |
1044 | p[-1] = 0; | |
1045 | inform (loc, "valid arguments to %qs are: %s", option->opt_text, s); | |
1046 | return; | |
1047 | } | |
1048 | ||
4df47bca JM |
1049 | if (decoded->errors & CL_ERR_WRONG_LANG) |
1050 | { | |
1051 | handlers->wrong_lang_callback (decoded, lang_mask); | |
1052 | return; | |
1053 | } | |
1054 | ||
5f20c657 JM |
1055 | gcc_assert (!decoded->errors); |
1056 | ||
d4d24ba4 | 1057 | if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED, |
a4d8c676 JM |
1058 | loc, handlers, false, dc)) |
1059 | error_at (loc, "unrecognized command line option %qs", opt); | |
5f20c657 JM |
1060 | } |
1061 | ||
d4d24ba4 | 1062 | /* Set any field in OPTS, and OPTS_SET if not NULL, for option |
a4d8c676 JM |
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. */ | |
5f20c657 JM |
1066 | |
1067 | void | |
d4d24ba4 | 1068 | set_option (struct gcc_options *opts, struct gcc_options *opts_set, |
1ebe4b4f | 1069 | int opt_index, int value, const char *arg, int kind, |
a4d8c676 | 1070 | location_t loc, diagnostic_context *dc) |
5f20c657 JM |
1071 | { |
1072 | const struct cl_option *option = &cl_options[opt_index]; | |
46625112 | 1073 | void *flag_var = option_flag_var (opt_index, opts); |
d4d24ba4 | 1074 | void *set_flag_var = NULL; |
5f20c657 | 1075 | |
46625112 | 1076 | if (!flag_var) |
5f20c657 JM |
1077 | return; |
1078 | ||
d4d24ba4 JM |
1079 | if (opts_set != NULL) |
1080 | set_flag_var = option_flag_var (opt_index, opts_set); | |
1081 | ||
5f20c657 JM |
1082 | switch (option->var_type) |
1083 | { | |
1084 | case CLVC_BOOLEAN: | |
46625112 | 1085 | *(int *) flag_var = value; |
d4d24ba4 JM |
1086 | if (set_flag_var) |
1087 | *(int *) set_flag_var = 1; | |
5f20c657 JM |
1088 | break; |
1089 | ||
1090 | case CLVC_EQUAL: | |
46625112 JM |
1091 | *(int *) flag_var = (value |
1092 | ? option->var_value | |
1093 | : !option->var_value); | |
d4d24ba4 JM |
1094 | if (set_flag_var) |
1095 | *(int *) set_flag_var = 1; | |
5f20c657 JM |
1096 | break; |
1097 | ||
1098 | case CLVC_BIT_CLEAR: | |
1099 | case CLVC_BIT_SET: | |
1100 | if ((value != 0) == (option->var_type == CLVC_BIT_SET)) | |
46625112 | 1101 | *(int *) flag_var |= option->var_value; |
5f20c657 | 1102 | else |
46625112 | 1103 | *(int *) flag_var &= ~option->var_value; |
d4d24ba4 JM |
1104 | if (set_flag_var) |
1105 | *(int *) set_flag_var |= option->var_value; | |
5f20c657 JM |
1106 | break; |
1107 | ||
1108 | case CLVC_STRING: | |
46625112 | 1109 | *(const char **) flag_var = arg; |
d4d24ba4 JM |
1110 | if (set_flag_var) |
1111 | *(const char **) set_flag_var = ""; | |
5f20c657 | 1112 | break; |
21bf1558 | 1113 | |
e6d4b984 JM |
1114 | case CLVC_ENUM: |
1115 | { | |
1116 | const struct cl_enum *e = &cl_enums[option->var_enum]; | |
1117 | ||
1118 | e->set (flag_var, value); | |
1119 | if (set_flag_var) | |
1120 | e->set (set_flag_var, 1); | |
1121 | } | |
1122 | break; | |
1123 | ||
21bf1558 JM |
1124 | case CLVC_DEFER: |
1125 | { | |
1126 | VEC(cl_deferred_option,heap) *vec | |
1127 | = (VEC(cl_deferred_option,heap) *) *(void **) flag_var; | |
1128 | cl_deferred_option *p; | |
1129 | ||
1130 | p = VEC_safe_push (cl_deferred_option, heap, vec, NULL); | |
1131 | p->opt_index = opt_index; | |
1132 | p->arg = arg; | |
1133 | p->value = value; | |
1134 | *(void **) flag_var = vec; | |
1135 | if (set_flag_var) | |
1136 | *(void **) set_flag_var = vec; | |
1137 | } | |
1138 | break; | |
5f20c657 JM |
1139 | } |
1140 | ||
1ebe4b4f JM |
1141 | if ((diagnostic_t) kind != DK_UNSPECIFIED |
1142 | && dc != NULL) | |
a4d8c676 | 1143 | diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc); |
5f20c657 | 1144 | } |
46625112 JM |
1145 | |
1146 | /* Return the address of the flag variable for option OPT_INDEX in | |
1147 | options structure OPTS, or NULL if there is no flag variable. */ | |
1148 | ||
1149 | void * | |
1150 | option_flag_var (int opt_index, struct gcc_options *opts) | |
1151 | { | |
1152 | const struct cl_option *option = &cl_options[opt_index]; | |
1153 | ||
1154 | if (option->flag_var_offset == (unsigned short) -1) | |
1155 | return NULL; | |
1156 | return (void *)(((char *) opts) + option->flag_var_offset); | |
1157 | } | |
c5fa0890 | 1158 | |
c98cd5bf JM |
1159 | /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled, |
1160 | or -1 if it isn't a simple on-off switch. */ | |
1161 | ||
1162 | int | |
1163 | option_enabled (int opt_idx, void *opts) | |
1164 | { | |
1165 | const struct cl_option *option = &(cl_options[opt_idx]); | |
1166 | struct gcc_options *optsg = (struct gcc_options *) opts; | |
1167 | void *flag_var = option_flag_var (opt_idx, optsg); | |
1168 | ||
1169 | if (flag_var) | |
1170 | switch (option->var_type) | |
1171 | { | |
1172 | case CLVC_BOOLEAN: | |
1173 | return *(int *) flag_var != 0; | |
1174 | ||
1175 | case CLVC_EQUAL: | |
1176 | return *(int *) flag_var == option->var_value; | |
1177 | ||
1178 | case CLVC_BIT_CLEAR: | |
1179 | return (*(int *) flag_var & option->var_value) == 0; | |
1180 | ||
1181 | case CLVC_BIT_SET: | |
1182 | return (*(int *) flag_var & option->var_value) != 0; | |
1183 | ||
1184 | case CLVC_STRING: | |
e6d4b984 | 1185 | case CLVC_ENUM: |
c98cd5bf JM |
1186 | case CLVC_DEFER: |
1187 | break; | |
1188 | } | |
1189 | return -1; | |
1190 | } | |
1191 | ||
1192 | /* Fill STATE with the current state of option OPTION in OPTS. Return | |
1193 | true if there is some state to store. */ | |
1194 | ||
1195 | bool | |
1196 | get_option_state (struct gcc_options *opts, int option, | |
1197 | struct cl_option_state *state) | |
1198 | { | |
1199 | void *flag_var = option_flag_var (option, opts); | |
1200 | ||
1201 | if (flag_var == 0) | |
1202 | return false; | |
1203 | ||
1204 | switch (cl_options[option].var_type) | |
1205 | { | |
1206 | case CLVC_BOOLEAN: | |
1207 | case CLVC_EQUAL: | |
1208 | state->data = flag_var; | |
1209 | state->size = sizeof (int); | |
1210 | break; | |
1211 | ||
1212 | case CLVC_BIT_CLEAR: | |
1213 | case CLVC_BIT_SET: | |
1214 | state->ch = option_enabled (option, opts); | |
1215 | state->data = &state->ch; | |
1216 | state->size = 1; | |
1217 | break; | |
1218 | ||
1219 | case CLVC_STRING: | |
1220 | state->data = *(const char **) flag_var; | |
1221 | if (state->data == 0) | |
1222 | state->data = ""; | |
1223 | state->size = strlen ((const char *) state->data) + 1; | |
1224 | break; | |
1225 | ||
e6d4b984 JM |
1226 | case CLVC_ENUM: |
1227 | state->data = flag_var; | |
1228 | state->size = cl_enums[cl_options[option].var_enum].var_size; | |
1229 | break; | |
1230 | ||
c98cd5bf JM |
1231 | case CLVC_DEFER: |
1232 | return false; | |
1233 | } | |
1234 | return true; | |
1235 | } | |
1236 | ||
c5fa0890 JM |
1237 | /* Set a warning option OPT_INDEX (language mask LANG_MASK, option |
1238 | handlers HANDLERS) to have diagnostic kind KIND for option | |
1239 | structures OPTS and OPTS_SET and diagnostic context DC (possibly | |
1240 | NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY, | |
1241 | the warning option in question is implied at this point. This is | |
1242 | used by -Werror= and #pragma GCC diagnostic. */ | |
1243 | ||
1244 | void | |
1245 | control_warning_option (unsigned int opt_index, int kind, bool imply, | |
1246 | location_t loc, unsigned int lang_mask, | |
1247 | const struct cl_option_handlers *handlers, | |
1248 | struct gcc_options *opts, | |
1249 | struct gcc_options *opts_set, | |
1250 | diagnostic_context *dc) | |
1251 | { | |
1252 | if (cl_options[opt_index].alias_target != N_OPTS) | |
1253 | opt_index = cl_options[opt_index].alias_target; | |
1254 | if (opt_index == OPT_SPECIAL_ignore) | |
1255 | return; | |
1256 | if (dc) | |
1257 | diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc); | |
1258 | if (imply) | |
1259 | { | |
1260 | /* -Werror=foo implies -Wfoo. */ | |
1261 | if (cl_options[opt_index].var_type == CLVC_BOOLEAN) | |
1262 | handle_generated_option (opts, opts_set, | |
1263 | opt_index, NULL, 1, lang_mask, | |
1264 | kind, loc, handlers, dc); | |
1265 | } | |
1266 | } |