]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/opts-common.c
re PR target/56455 (ICE in final_scan_insn)
[thirdparty/gcc.git] / gcc / opts-common.c
CommitLineData
14c7833c 1/* Command line option handling.
d1e082c2 2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
14c7833c
L
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
14c7833c
L
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along 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
28static 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. */
54size_t
eb50f63a 55find_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
153int
154integral_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. */
169static bool
170option_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
187static bool
188enum_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
198static bool
199enum_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
219bool
220opt_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
238bool
239enum_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
268static void
269generate_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;
ca3e7c9f
JJ
307 if (opt_text != option->opt_text)
308 free (CONST_CAST (char *, opt_text));
5de8299c
JM
309 }
310 }
311 else
312 {
313 decoded->canonical_option[0] = opt_text;
314 decoded->canonical_option[1] = NULL;
315 decoded->canonical_option_num_elements = 1;
316 }
317}
318
e200444e
JM
319/* Structure describing mappings from options on the command line to
320 options to look up with find_opt. */
321struct option_map
322{
323 /* Prefix of the option on the command line. */
324 const char *opt0;
325 /* If two argv elements are considered to be merged into one option,
326 prefix for the second element, otherwise NULL. */
327 const char *opt1;
328 /* The new prefix to map to. */
329 const char *new_prefix;
330 /* Whether at least one character is needed following opt1 or opt0
331 for this mapping to be used. (--optimize= is valid for -O, but
332 --warn- is not valid for -W.) */
333 bool another_char_needed;
334 /* Whether the original option is a negated form of the option
335 resulting from this map. */
336 bool negated;
337};
338static const struct option_map option_map[] =
339 {
340 { "-Wno-", NULL, "-W", false, true },
341 { "-fno-", NULL, "-f", false, true },
342 { "-mno-", NULL, "-m", false, true },
343 { "--debug=", NULL, "-g", false, false },
344 { "--machine-", NULL, "-m", true, false },
345 { "--machine-no-", NULL, "-m", false, true },
346 { "--machine=", NULL, "-m", false, false },
347 { "--machine=no-", NULL, "-m", false, true },
348 { "--machine", "", "-m", false, false },
349 { "--machine", "no-", "-m", false, true },
350 { "--optimize=", NULL, "-O", false, false },
351 { "--std=", NULL, "-std=", false, false },
352 { "--std", "", "-std=", false, false },
353 { "--warn-", NULL, "-W", true, false },
354 { "--warn-no-", NULL, "-W", false, true },
355 { "--", NULL, "-f", true, false },
356 { "--no-", NULL, "-f", false, true }
357 };
358
5d4b393f 359/* Decode the switch beginning at ARGV for the language indicated by
603349bf
JM
360 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
361 the structure *DECODED. Returns the number of switches
362 consumed. */
5d4b393f 363
6e2f1956 364static unsigned int
5d4b393f
JM
365decode_cmdline_option (const char **argv, unsigned int lang_mask,
366 struct cl_decoded_option *decoded)
367{
368 size_t opt_index;
e200444e 369 const char *arg = 0;
5d4b393f 370 int value = 1;
c8967d95 371 unsigned int result = 1, i, extra_args, separate_args = 0;
e200444e 372 int adjust_len = 0;
eea13ead
JM
373 size_t total_len;
374 char *p;
5d4b393f
JM
375 const struct cl_option *option;
376 int errors = 0;
2d2bd949 377 const char *warn_message = NULL;
c878765b
JM
378 bool separate_arg_flag;
379 bool joined_arg_flag;
d1583032 380 bool have_separate_arg = false;
5d4b393f 381
e200444e 382 extra_args = 0;
5d4b393f 383
e200444e
JM
384 opt_index = find_opt (argv[0] + 1, lang_mask);
385 i = 0;
386 while (opt_index == OPT_SPECIAL_unknown
387 && i < ARRAY_SIZE (option_map))
5d4b393f 388 {
e200444e
JM
389 const char *opt0 = option_map[i].opt0;
390 const char *opt1 = option_map[i].opt1;
391 const char *new_prefix = option_map[i].new_prefix;
392 bool another_char_needed = option_map[i].another_char_needed;
393 size_t opt0_len = strlen (opt0);
394 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
395 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
396 size_t new_prefix_len = strlen (new_prefix);
397
398 extra_args = (opt1 == NULL ? 0 : 1);
399 value = !option_map[i].negated;
400
401 if (strncmp (argv[0], opt0, opt0_len) == 0
402 && (opt1 == NULL
403 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
404 && (!another_char_needed
405 || argv[extra_args][optn_len] != 0))
406 {
407 size_t arglen = strlen (argv[extra_args]);
408 char *dup;
409
410 adjust_len = (int) optn_len - (int) new_prefix_len;
411 dup = XNEWVEC (char, arglen + 1 - adjust_len);
412 memcpy (dup, new_prefix, new_prefix_len);
413 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
414 arglen - optn_len + 1);
415 opt_index = find_opt (dup + 1, lang_mask);
416 free (dup);
417 }
418 i++;
5d4b393f
JM
419 }
420
6e2f1956
JM
421 if (opt_index == OPT_SPECIAL_unknown)
422 {
423 arg = argv[0];
e200444e
JM
424 extra_args = 0;
425 value = 1;
6e2f1956
JM
426 goto done;
427 }
5d4b393f
JM
428
429 option = &cl_options[opt_index];
430
431 /* Reject negative form of switches that don't take negatives as
432 unrecognized. */
300d83d9 433 if (!value && option->cl_reject_negative)
5d4b393f 434 {
6e2f1956 435 opt_index = OPT_SPECIAL_unknown;
184eb658 436 errors |= CL_ERR_NEGATIVE;
6e2f1956 437 arg = argv[0];
5d4b393f
JM
438 goto done;
439 }
440
e200444e 441 result = extra_args + 1;
2d2bd949
JM
442 warn_message = option->warn_message;
443
5d4b393f 444 /* Check to see if the option is disabled for this configuration. */
300d83d9 445 if (option->cl_disabled)
5d4b393f
JM
446 errors |= CL_ERR_DISABLED;
447
c878765b 448 /* Determine whether there may be a separate argument based on
c243beb0
JM
449 whether this option is being processed for the driver, and, if
450 so, how many such arguments. */
c878765b 451 separate_arg_flag = ((option->flags & CL_SEPARATE)
300d83d9 452 && !(option->cl_no_driver_arg
c878765b 453 && (lang_mask & CL_DRIVER)));
c243beb0 454 separate_args = (separate_arg_flag
300d83d9 455 ? option->cl_separate_nargs + 1
c243beb0 456 : 0);
c878765b
JM
457 joined_arg_flag = (option->flags & CL_JOINED) != 0;
458
5d4b393f 459 /* Sort out any argument the switch takes. */
c878765b 460 if (joined_arg_flag)
5d4b393f
JM
461 {
462 /* Have arg point to the original switch. This is because
463 some code, such as disable_builtin_function, expects its
464 argument to be persistent until the program exits. */
e200444e 465 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
5d4b393f 466
300d83d9 467 if (*arg == '\0' && !option->cl_missing_ok)
5d4b393f 468 {
c878765b 469 if (separate_arg_flag)
5d4b393f 470 {
e200444e
JM
471 arg = argv[extra_args + 1];
472 result = extra_args + 2;
5d4b393f 473 if (arg == NULL)
e200444e 474 result = extra_args + 1;
d1583032
JM
475 else
476 have_separate_arg = true;
5d4b393f
JM
477 }
478 else
479 /* Missing argument. */
480 arg = NULL;
481 }
482 }
c878765b 483 else if (separate_arg_flag)
5d4b393f 484 {
e200444e 485 arg = argv[extra_args + 1];
c243beb0
JM
486 for (i = 0; i < separate_args; i++)
487 if (argv[extra_args + 1 + i] == NULL)
488 {
489 errors |= CL_ERR_MISSING_ARG;
490 break;
491 }
492 result = extra_args + 1 + i;
493 if (arg != NULL)
d1583032 494 have_separate_arg = true;
5d4b393f
JM
495 }
496
5de8299c
JM
497 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
498 errors |= CL_ERR_MISSING_ARG;
499
2d2bd949
JM
500 /* Is this option an alias (or an ignored option, marked as an alias
501 of OPT_SPECIAL_ignore)? */
d1583032 502 if (option->alias_target != N_OPTS
300d83d9 503 && (!option->cl_separate_alias || have_separate_arg))
5de8299c
JM
504 {
505 size_t new_opt_index = option->alias_target;
5de8299c 506
2d2bd949 507 if (new_opt_index == OPT_SPECIAL_ignore)
5de8299c 508 {
2d2bd949
JM
509 gcc_assert (option->alias_arg == NULL);
510 gcc_assert (option->neg_alias_arg == NULL);
511 opt_index = new_opt_index;
512 arg = NULL;
5de8299c
JM
513 value = 1;
514 }
2d2bd949 515 else
5de8299c 516 {
2d2bd949 517 const struct cl_option *new_option = &cl_options[new_opt_index];
5de8299c 518
2d2bd949 519 /* The new option must not be an alias itself. */
e200444e 520 gcc_assert (new_option->alias_target == N_OPTS
300d83d9 521 || new_option->cl_separate_alias);
5de8299c 522
2d2bd949
JM
523 if (option->neg_alias_arg)
524 {
525 gcc_assert (option->alias_arg != NULL);
526 gcc_assert (arg == NULL);
300d83d9 527 gcc_assert (!option->cl_negative_alias);
2d2bd949
JM
528 if (value)
529 arg = option->alias_arg;
530 else
531 arg = option->neg_alias_arg;
532 value = 1;
533 }
534 else if (option->alias_arg)
535 {
536 gcc_assert (value == 1);
537 gcc_assert (arg == NULL);
300d83d9 538 gcc_assert (!option->cl_negative_alias);
2d2bd949
JM
539 arg = option->alias_arg;
540 }
5de8299c 541
300d83d9 542 if (option->cl_negative_alias)
666a21a2
JM
543 value = !value;
544
2d2bd949
JM
545 opt_index = new_opt_index;
546 option = new_option;
5de8299c 547
2d2bd949 548 if (value == 0)
300d83d9 549 gcc_assert (!option->cl_reject_negative);
2d2bd949
JM
550
551 /* Recompute what arguments are allowed. */
552 separate_arg_flag = ((option->flags & CL_SEPARATE)
300d83d9 553 && !(option->cl_no_driver_arg
2d2bd949
JM
554 && (lang_mask & CL_DRIVER)));
555 joined_arg_flag = (option->flags & CL_JOINED) != 0;
556
300d83d9 557 if (separate_args > 1 || option->cl_separate_nargs)
c243beb0 558 gcc_assert (separate_args
300d83d9 559 == (unsigned int) option->cl_separate_nargs + 1);
c243beb0 560
2d2bd949
JM
561 if (!(errors & CL_ERR_MISSING_ARG))
562 {
563 if (separate_arg_flag || joined_arg_flag)
e200444e 564 {
300d83d9 565 if (option->cl_missing_ok && arg == NULL)
e200444e
JM
566 arg = "";
567 gcc_assert (arg != NULL);
568 }
2d2bd949
JM
569 else
570 gcc_assert (arg == NULL);
571 }
5de8299c 572
2d2bd949
JM
573 /* Recheck for warnings and disabled options. */
574 if (option->warn_message)
575 {
576 gcc_assert (warn_message == NULL);
577 warn_message = option->warn_message;
578 }
300d83d9 579 if (option->cl_disabled)
2d2bd949
JM
580 errors |= CL_ERR_DISABLED;
581 }
5de8299c
JM
582 }
583
5d4b393f 584 /* Check if this is a switch for a different front end. */
d9d16a19 585 if (!option_ok_for_language (option, lang_mask))
5d4b393f 586 errors |= CL_ERR_WRONG_LANG;
5d4b393f 587
413519ae
JM
588 /* Convert the argument to lowercase if appropriate. */
589 if (arg && option->cl_tolower)
590 {
591 size_t j;
592 size_t len = strlen (arg);
593 char *arg_lower = XNEWVEC (char, len + 1);
594
595 for (j = 0; j < len; j++)
596 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
597 arg_lower[len] = 0;
598 arg = arg_lower;
599 }
600
5d4b393f 601 /* If the switch takes an integer, convert it. */
300d83d9 602 if (arg && option->cl_uinteger)
5d4b393f
JM
603 {
604 value = integral_argument (arg);
605 if (value == -1)
606 errors |= CL_ERR_UINT_ARG;
607 }
608
e6d4b984
JM
609 /* If the switch takes an enumerated argument, convert it. */
610 if (arg && (option->var_type == CLVC_ENUM))
611 {
612 const struct cl_enum *e = &cl_enums[option->var_enum];
613
614 gcc_assert (value == 1);
615 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
616 {
617 const char *carg = NULL;
618
619 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
620 arg = carg;
621 gcc_assert (carg != NULL);
622 }
623 else
624 errors |= CL_ERR_ENUM_ARG;
625 }
626
5d4b393f 627 done:
5d4b393f
JM
628 decoded->opt_index = opt_index;
629 decoded->arg = arg;
630 decoded->value = value;
631 decoded->errors = errors;
2d2bd949 632 decoded->warn_message = warn_message;
eea13ead
JM
633
634 if (opt_index == OPT_SPECIAL_unknown)
c243beb0 635 gcc_assert (result == 1);
eea13ead
JM
636
637 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
638 decoded->canonical_option_num_elements = result;
639 total_len = 0;
640 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
641 {
642 if (i < result)
643 {
7fcf46f5 644 size_t len;
5de8299c
JM
645 if (opt_index == OPT_SPECIAL_unknown)
646 decoded->canonical_option[i] = argv[i];
647 else
648 decoded->canonical_option[i] = NULL;
7fcf46f5
JZ
649 len = strlen (argv[i]);
650 /* If the argument is an empty string, we will print it as "" in
651 orig_option_with_args_text. */
652 total_len += (len != 0 ? len : 2) + 1;
eea13ead
JM
653 }
654 else
655 decoded->canonical_option[i] = NULL;
656 }
2d2bd949 657 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
c243beb0
JM
658 {
659 generate_canonical_option (opt_index, arg, value, decoded);
660 if (separate_args > 1)
661 {
662 for (i = 0; i < separate_args; i++)
663 {
664 if (argv[extra_args + 1 + i] == NULL)
665 break;
666 else
667 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
668 }
669 gcc_assert (result == 1 + i);
670 decoded->canonical_option_num_elements = result;
671 }
672 }
eea13ead
JM
673 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
674 for (i = 0; i < result; i++)
675 {
676 size_t len = strlen (argv[i]);
677
7fcf46f5
JZ
678 /* Print the empty string verbally. */
679 if (len == 0)
680 {
681 *p++ = '"';
682 *p++ = '"';
683 }
684 else
685 memcpy (p, argv[i], len);
eea13ead
JM
686 p += len;
687 if (i == result - 1)
688 *p++ = 0;
689 else
690 *p++ = ' ';
691 }
692
5d4b393f
JM
693 return result;
694}
695
6e2f1956
JM
696/* Decode command-line options (ARGC and ARGV being the arguments of
697 main) into an array, setting *DECODED_OPTIONS to a pointer to that
698 array and *DECODED_OPTIONS_COUNT to the number of entries in the
699 array. The first entry in the array is always one for the program
700 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
603349bf
JM
701 flags applicable for decoding (including CL_COMMON and CL_TARGET if
702 those options should be considered applicable). Do not produce any
703 diagnostics or set state outside of these variables. */
6e2f1956
JM
704
705void
706decode_cmdline_options_to_array (unsigned int argc, const char **argv,
707 unsigned int lang_mask,
708 struct cl_decoded_option **decoded_options,
709 unsigned int *decoded_options_count)
710{
2be55a25 711 unsigned int n, i;
6e2f1956
JM
712 struct cl_decoded_option *opt_array;
713 unsigned int num_decoded_options;
714
715 opt_array = XNEWVEC (struct cl_decoded_option, argc);
716
717 opt_array[0].opt_index = OPT_SPECIAL_program_name;
2d2bd949 718 opt_array[0].warn_message = NULL;
6e2f1956
JM
719 opt_array[0].arg = argv[0];
720 opt_array[0].orig_option_with_args_text = argv[0];
eea13ead 721 opt_array[0].canonical_option_num_elements = 1;
7a9bf9a4
JM
722 opt_array[0].canonical_option[0] = argv[0];
723 opt_array[0].canonical_option[1] = NULL;
eea13ead
JM
724 opt_array[0].canonical_option[2] = NULL;
725 opt_array[0].canonical_option[3] = NULL;
6e2f1956
JM
726 opt_array[0].value = 1;
727 opt_array[0].errors = 0;
728 num_decoded_options = 1;
729
730 for (i = 1; i < argc; i += n)
731 {
732 const char *opt = argv[i];
733
734 /* Interpret "-" or a non-switch as a file name. */
735 if (opt[0] != '-' || opt[1] == '\0')
736 {
d9d16a19 737 generate_option_input_file (opt, &opt_array[num_decoded_options]);
6e2f1956
JM
738 num_decoded_options++;
739 n = 1;
740 continue;
741 }
742
743 n = decode_cmdline_option (argv + i, lang_mask,
744 &opt_array[num_decoded_options]);
745 num_decoded_options++;
746 }
747
6e2f1956
JM
748 *decoded_options = opt_array;
749 *decoded_options_count = num_decoded_options;
60cf253a 750 prune_options (decoded_options, decoded_options_count);
6e2f1956
JM
751}
752
14c7833c
L
753/* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
754 next one is the same as ORIG_NEXT_OPT_IDX. */
755
756static bool
757cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
758{
759 /* An option can be canceled by the same option or an option with
760 Negative. */
761 if (cl_options [next_opt_idx].neg_index == opt_idx)
762 return true;
763
764 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
765 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
766 orig_next_opt_idx);
b8698a0f 767
14c7833c
L
768 return false;
769}
770
771/* Filter out options canceled by the ones after them. */
772
60cf253a
JM
773static void
774prune_options (struct cl_decoded_option **decoded_options,
775 unsigned int *decoded_options_count)
14c7833c 776{
60cf253a
JM
777 unsigned int old_decoded_options_count = *decoded_options_count;
778 struct cl_decoded_option *old_decoded_options = *decoded_options;
779 unsigned int new_decoded_options_count;
780 struct cl_decoded_option *new_decoded_options
781 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
782 unsigned int i;
14c7833c 783 const struct cl_option *option;
14c7833c
L
784
785 /* Remove arguments which are negated by others after them. */
60cf253a
JM
786 new_decoded_options_count = 0;
787 for (i = 0; i < old_decoded_options_count; i++)
14c7833c 788 {
60cf253a
JM
789 unsigned int j, opt_idx, next_opt_idx;
790
791 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
792 goto keep;
14c7833c 793
60cf253a
JM
794 opt_idx = old_decoded_options[i].opt_index;
795 switch (opt_idx)
14c7833c 796 {
60cf253a
JM
797 case OPT_SPECIAL_unknown:
798 case OPT_SPECIAL_ignore:
799 case OPT_SPECIAL_program_name:
800 case OPT_SPECIAL_input_file:
801 goto keep;
802
803 default:
804 gcc_assert (opt_idx < cl_options_count);
805 option = &cl_options[opt_idx];
806 if (option->neg_index < 0)
807 goto keep;
808
809 /* Skip joined switches. */
810 if ((option->flags & CL_JOINED))
811 goto keep;
812
813 for (j = i + 1; j < old_decoded_options_count; j++)
14c7833c 814 {
60cf253a
JM
815 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
816 continue;
817 next_opt_idx = old_decoded_options[j].opt_index;
818 if (next_opt_idx >= cl_options_count)
819 continue;
820 if (cl_options[next_opt_idx].neg_index < 0)
821 continue;
822 if ((cl_options[next_opt_idx].flags & CL_JOINED))
823 continue;
824 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
14c7833c
L
825 break;
826 }
60cf253a
JM
827 if (j == old_decoded_options_count)
828 {
14c7833c 829keep:
60cf253a
JM
830 new_decoded_options[new_decoded_options_count]
831 = old_decoded_options[i];
832 new_decoded_options_count++;
833 }
834 break;
14c7833c
L
835 }
836 }
837
60cf253a
JM
838 free (old_decoded_options);
839 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
840 new_decoded_options,
841 new_decoded_options_count);
842 *decoded_options = new_decoded_options;
843 *decoded_options_count = new_decoded_options_count;
14c7833c 844}
5f20c657 845
481e1176 846/* Handle option DECODED for the language indicated by LANG_MASK,
d4d24ba4
JM
847 using the handlers in HANDLERS and setting fields in OPTS and
848 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
a4d8c676
JM
849 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
850 option for options from the source file, UNKNOWN_LOCATION
851 otherwise. GENERATED_P is true for an option generated as part of
852 processing another option or otherwise generated internally, false
853 for one explicitly passed by the user. Returns false if the switch
854 was invalid. DC is the diagnostic context for options affecting
855 diagnostics state, or NULL. */
5f20c657 856
a4d8c676 857static bool
46625112 858handle_option (struct gcc_options *opts,
d4d24ba4 859 struct gcc_options *opts_set,
46625112 860 const struct cl_decoded_option *decoded,
a4d8c676 861 unsigned int lang_mask, int kind, location_t loc,
d4d24ba4 862 const struct cl_option_handlers *handlers,
1ebe4b4f 863 bool generated_p, diagnostic_context *dc)
5f20c657 864{
481e1176
JM
865 size_t opt_index = decoded->opt_index;
866 const char *arg = decoded->arg;
867 int value = decoded->value;
5f20c657 868 const struct cl_option *option = &cl_options[opt_index];
46625112 869 void *flag_var = option_flag_var (opt_index, opts);
5f20c657
JM
870 size_t i;
871
46625112 872 if (flag_var)
d4d24ba4 873 set_option (opts, (generated_p ? NULL : opts_set),
a4d8c676 874 opt_index, value, arg, kind, loc, dc);
5f20c657
JM
875
876 for (i = 0; i < handlers->num_handlers; i++)
877 if (option->flags & handlers->handlers[i].mask)
878 {
d4d24ba4 879 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
a4d8c676
JM
880 lang_mask, kind, loc,
881 handlers, dc))
5f20c657 882 return false;
5f20c657
JM
883 }
884
885 return true;
886}
887
481e1176
JM
888/* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
889 option instead of DECODED. This is used for callbacks when one
890 option implies another instead of an option being decoded from the
891 command line. */
892
893bool
d4d24ba4
JM
894handle_generated_option (struct gcc_options *opts,
895 struct gcc_options *opts_set,
896 size_t opt_index, const char *arg, int value,
a4d8c676 897 unsigned int lang_mask, int kind, location_t loc,
1ebe4b4f
JM
898 const struct cl_option_handlers *handlers,
899 diagnostic_context *dc)
481e1176 900{
481e1176
JM
901 struct cl_decoded_option decoded;
902
d9d16a19 903 generate_option (opt_index, arg, value, lang_mask, &decoded);
a4d8c676
JM
904 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
905 handlers, true, dc);
d9d16a19
JM
906}
907
908/* Fill in *DECODED with an option described by OPT_INDEX, ARG and
909 VALUE for a front end using LANG_MASK. This is used when the
910 compiler generates options internally. */
911
912void
913generate_option (size_t opt_index, const char *arg, int value,
914 unsigned int lang_mask, struct cl_decoded_option *decoded)
915{
916 const struct cl_option *option = &cl_options[opt_index];
917
918 decoded->opt_index = opt_index;
2d2bd949 919 decoded->warn_message = NULL;
d9d16a19 920 decoded->arg = arg;
d9d16a19
JM
921 decoded->value = value;
922 decoded->errors = (option_ok_for_language (option, lang_mask)
923 ? 0
924 : CL_ERR_WRONG_LANG);
481e1176 925
5de8299c
JM
926 generate_canonical_option (opt_index, arg, value, decoded);
927 switch (decoded->canonical_option_num_elements)
481e1176 928 {
5de8299c
JM
929 case 1:
930 decoded->orig_option_with_args_text = decoded->canonical_option[0];
931 break;
932
933 case 2:
934 decoded->orig_option_with_args_text
935 = concat (decoded->canonical_option[0], " ",
936 decoded->canonical_option[1], NULL);
937 break;
938
939 default:
940 gcc_unreachable ();
481e1176 941 }
d9d16a19 942}
481e1176 943
d9d16a19
JM
944/* Fill in *DECODED with an option for input file FILE. */
945
946void
947generate_option_input_file (const char *file,
948 struct cl_decoded_option *decoded)
949{
950 decoded->opt_index = OPT_SPECIAL_input_file;
2d2bd949 951 decoded->warn_message = NULL;
d9d16a19
JM
952 decoded->arg = file;
953 decoded->orig_option_with_args_text = file;
954 decoded->canonical_option_num_elements = 1;
955 decoded->canonical_option[0] = file;
956 decoded->canonical_option[1] = NULL;
957 decoded->canonical_option[2] = NULL;
958 decoded->canonical_option[3] = NULL;
959 decoded->value = 1;
960 decoded->errors = 0;
481e1176
JM
961}
962
a4d8c676
JM
963/* Handle the switch DECODED (location LOC) for the language indicated
964 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
965 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1ebe4b4f 966 diagnostic options. */
5f20c657
JM
967
968void
46625112 969read_cmdline_option (struct gcc_options *opts,
d4d24ba4 970 struct gcc_options *opts_set,
46625112 971 struct cl_decoded_option *decoded,
a4d8c676 972 location_t loc,
5f20c657 973 unsigned int lang_mask,
1ebe4b4f
JM
974 const struct cl_option_handlers *handlers,
975 diagnostic_context *dc)
5f20c657
JM
976{
977 const struct cl_option *option;
2d2bd949
JM
978 const char *opt = decoded->orig_option_with_args_text;
979
980 if (decoded->warn_message)
a4d8c676 981 warning_at (loc, 0, decoded->warn_message, opt);
5f20c657
JM
982
983 if (decoded->opt_index == OPT_SPECIAL_unknown)
984 {
481e1176 985 if (handlers->unknown_option_callback (decoded))
a4d8c676 986 error_at (loc, "unrecognized command line option %qs", decoded->arg);
5f20c657
JM
987 return;
988 }
989
2d2bd949
JM
990 if (decoded->opt_index == OPT_SPECIAL_ignore)
991 return;
992
5f20c657 993 option = &cl_options[decoded->opt_index];
5f20c657
JM
994
995 if (decoded->errors & CL_ERR_DISABLED)
996 {
a4d8c676
JM
997 error_at (loc, "command line option %qs"
998 " is not supported by this configuration", opt);
5f20c657
JM
999 return;
1000 }
1001
5f20c657
JM
1002 if (decoded->errors & CL_ERR_MISSING_ARG)
1003 {
1004 if (option->missing_argument_error)
a4d8c676 1005 error_at (loc, option->missing_argument_error, opt);
5f20c657 1006 else
a4d8c676 1007 error_at (loc, "missing argument to %qs", opt);
5f20c657
JM
1008 return;
1009 }
1010
1011 if (decoded->errors & CL_ERR_UINT_ARG)
1012 {
a4d8c676
JM
1013 error_at (loc, "argument to %qs should be a non-negative integer",
1014 option->opt_text);
5f20c657
JM
1015 return;
1016 }
1017
e6d4b984
JM
1018 if (decoded->errors & CL_ERR_ENUM_ARG)
1019 {
1020 const struct cl_enum *e = &cl_enums[option->var_enum];
1021 unsigned int i;
1022 size_t len;
1023 char *s, *p;
1024
1025 if (e->unknown_error)
1026 error_at (loc, e->unknown_error, decoded->arg);
1027 else
1028 error_at (loc, "unrecognized argument in option %qs", opt);
1029
1030 len = 0;
1031 for (i = 0; e->values[i].arg != NULL; i++)
1032 len += strlen (e->values[i].arg) + 1;
1033
1034 s = XALLOCAVEC (char, len);
1035 p = s;
1036 for (i = 0; e->values[i].arg != NULL; i++)
1037 {
1038 size_t arglen = strlen (e->values[i].arg);
1039 memcpy (p, e->values[i].arg, arglen);
1040 p[arglen] = ' ';
1041 p += arglen + 1;
1042 }
1043 p[-1] = 0;
1044 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1045 return;
1046 }
1047
4df47bca
JM
1048 if (decoded->errors & CL_ERR_WRONG_LANG)
1049 {
1050 handlers->wrong_lang_callback (decoded, lang_mask);
1051 return;
1052 }
1053
5f20c657
JM
1054 gcc_assert (!decoded->errors);
1055
d4d24ba4 1056 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
a4d8c676
JM
1057 loc, handlers, false, dc))
1058 error_at (loc, "unrecognized command line option %qs", opt);
5f20c657
JM
1059}
1060
d4d24ba4 1061/* Set any field in OPTS, and OPTS_SET if not NULL, for option
a4d8c676
JM
1062 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1063 location LOC, using diagnostic context DC if not NULL for
1064 diagnostic classification. */
5f20c657
JM
1065
1066void
d4d24ba4 1067set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1ebe4b4f 1068 int opt_index, int value, const char *arg, int kind,
a4d8c676 1069 location_t loc, diagnostic_context *dc)
5f20c657
JM
1070{
1071 const struct cl_option *option = &cl_options[opt_index];
46625112 1072 void *flag_var = option_flag_var (opt_index, opts);
d4d24ba4 1073 void *set_flag_var = NULL;
5f20c657 1074
46625112 1075 if (!flag_var)
5f20c657
JM
1076 return;
1077
d4d24ba4
JM
1078 if (opts_set != NULL)
1079 set_flag_var = option_flag_var (opt_index, opts_set);
1080
5f20c657
JM
1081 switch (option->var_type)
1082 {
1083 case CLVC_BOOLEAN:
46625112 1084 *(int *) flag_var = value;
d4d24ba4
JM
1085 if (set_flag_var)
1086 *(int *) set_flag_var = 1;
5f20c657
JM
1087 break;
1088
1089 case CLVC_EQUAL:
99114bbf
L
1090 if (option->cl_host_wide_int)
1091 *(HOST_WIDE_INT *) flag_var = (value
1092 ? option->var_value
1093 : !option->var_value);
1094 else
1095 *(int *) flag_var = (value
1096 ? option->var_value
1097 : !option->var_value);
d4d24ba4
JM
1098 if (set_flag_var)
1099 *(int *) set_flag_var = 1;
5f20c657
JM
1100 break;
1101
1102 case CLVC_BIT_CLEAR:
1103 case CLVC_BIT_SET:
1104 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
99114bbf
L
1105 {
1106 if (option->cl_host_wide_int)
1107 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1108 else
1109 *(int *) flag_var |= option->var_value;
1110 }
5f20c657 1111 else
99114bbf
L
1112 {
1113 if (option->cl_host_wide_int)
1114 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1115 else
1116 *(int *) flag_var &= ~option->var_value;
1117 }
d4d24ba4 1118 if (set_flag_var)
99114bbf
L
1119 {
1120 if (option->cl_host_wide_int)
1121 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1122 else
1123 *(int *) set_flag_var |= option->var_value;
1124 }
5f20c657
JM
1125 break;
1126
1127 case CLVC_STRING:
46625112 1128 *(const char **) flag_var = arg;
d4d24ba4
JM
1129 if (set_flag_var)
1130 *(const char **) set_flag_var = "";
5f20c657 1131 break;
21bf1558 1132
e6d4b984
JM
1133 case CLVC_ENUM:
1134 {
1135 const struct cl_enum *e = &cl_enums[option->var_enum];
1136
1137 e->set (flag_var, value);
1138 if (set_flag_var)
1139 e->set (set_flag_var, 1);
1140 }
1141 break;
1142
21bf1558
JM
1143 case CLVC_DEFER:
1144 {
9771b263
DN
1145 vec<cl_deferred_option> *v
1146 = (vec<cl_deferred_option> *) *(void **) flag_var;
f32682ca 1147 cl_deferred_option p = {opt_index, arg, value};
9771b263
DN
1148 if (!v)
1149 v = XCNEW (vec<cl_deferred_option>);
1150 v->safe_push (p);
1151 *(void **) flag_var = v;
21bf1558 1152 if (set_flag_var)
9771b263 1153 *(void **) set_flag_var = v;
21bf1558
JM
1154 }
1155 break;
5f20c657
JM
1156 }
1157
1ebe4b4f
JM
1158 if ((diagnostic_t) kind != DK_UNSPECIFIED
1159 && dc != NULL)
a4d8c676 1160 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
5f20c657 1161}
46625112
JM
1162
1163/* Return the address of the flag variable for option OPT_INDEX in
1164 options structure OPTS, or NULL if there is no flag variable. */
1165
1166void *
1167option_flag_var (int opt_index, struct gcc_options *opts)
1168{
1169 const struct cl_option *option = &cl_options[opt_index];
1170
1171 if (option->flag_var_offset == (unsigned short) -1)
1172 return NULL;
1173 return (void *)(((char *) opts) + option->flag_var_offset);
1174}
c5fa0890 1175
c98cd5bf
JM
1176/* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1177 or -1 if it isn't a simple on-off switch. */
1178
1179int
1180option_enabled (int opt_idx, void *opts)
1181{
1182 const struct cl_option *option = &(cl_options[opt_idx]);
1183 struct gcc_options *optsg = (struct gcc_options *) opts;
1184 void *flag_var = option_flag_var (opt_idx, optsg);
1185
1186 if (flag_var)
1187 switch (option->var_type)
1188 {
1189 case CLVC_BOOLEAN:
1190 return *(int *) flag_var != 0;
1191
1192 case CLVC_EQUAL:
99114bbf
L
1193 if (option->cl_host_wide_int)
1194 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1195 else
1196 return *(int *) flag_var == option->var_value;
c98cd5bf
JM
1197
1198 case CLVC_BIT_CLEAR:
99114bbf
L
1199 if (option->cl_host_wide_int)
1200 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1201 else
1202 return (*(int *) flag_var & option->var_value) == 0;
c98cd5bf
JM
1203
1204 case CLVC_BIT_SET:
99114bbf
L
1205 if (option->cl_host_wide_int)
1206 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1207 else
1208 return (*(int *) flag_var & option->var_value) != 0;
c98cd5bf
JM
1209
1210 case CLVC_STRING:
e6d4b984 1211 case CLVC_ENUM:
c98cd5bf
JM
1212 case CLVC_DEFER:
1213 break;
1214 }
1215 return -1;
1216}
1217
1218/* Fill STATE with the current state of option OPTION in OPTS. Return
1219 true if there is some state to store. */
1220
1221bool
1222get_option_state (struct gcc_options *opts, int option,
1223 struct cl_option_state *state)
1224{
1225 void *flag_var = option_flag_var (option, opts);
1226
1227 if (flag_var == 0)
1228 return false;
1229
1230 switch (cl_options[option].var_type)
1231 {
1232 case CLVC_BOOLEAN:
1233 case CLVC_EQUAL:
1234 state->data = flag_var;
99114bbf
L
1235 state->size = (cl_options[option].cl_host_wide_int
1236 ? sizeof (HOST_WIDE_INT)
1237 : sizeof (int));
c98cd5bf
JM
1238 break;
1239
1240 case CLVC_BIT_CLEAR:
1241 case CLVC_BIT_SET:
1242 state->ch = option_enabled (option, opts);
1243 state->data = &state->ch;
1244 state->size = 1;
1245 break;
1246
1247 case CLVC_STRING:
1248 state->data = *(const char **) flag_var;
1249 if (state->data == 0)
1250 state->data = "";
1251 state->size = strlen ((const char *) state->data) + 1;
1252 break;
1253
e6d4b984
JM
1254 case CLVC_ENUM:
1255 state->data = flag_var;
1256 state->size = cl_enums[cl_options[option].var_enum].var_size;
1257 break;
1258
c98cd5bf
JM
1259 case CLVC_DEFER:
1260 return false;
1261 }
1262 return true;
1263}
1264
c5fa0890
JM
1265/* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1266 handlers HANDLERS) to have diagnostic kind KIND for option
1267 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1268 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1269 the warning option in question is implied at this point. This is
1270 used by -Werror= and #pragma GCC diagnostic. */
1271
1272void
1273control_warning_option (unsigned int opt_index, int kind, bool imply,
1274 location_t loc, unsigned int lang_mask,
1275 const struct cl_option_handlers *handlers,
1276 struct gcc_options *opts,
1277 struct gcc_options *opts_set,
1278 diagnostic_context *dc)
1279{
1280 if (cl_options[opt_index].alias_target != N_OPTS)
1281 opt_index = cl_options[opt_index].alias_target;
1282 if (opt_index == OPT_SPECIAL_ignore)
1283 return;
1284 if (dc)
1285 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1286 if (imply)
1287 {
1288 /* -Werror=foo implies -Wfoo. */
1289 if (cl_options[opt_index].var_type == CLVC_BOOLEAN)
1290 handle_generated_option (opts, opts_set,
1291 opt_index, NULL, 1, lang_mask,
1292 kind, loc, handlers, dc);
1293 }
1294}