1/* Command line option handling.
2 Copyright (C) 2006-2017 Free Software Foundation, Inc.
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
8Software Foundation; either version 3, or (at your option) any later
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
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#include "config.h"
21#include "system.h"
22#include "intl.h"
23#include "coretypes.h"
24#include "opts.h"
25#include "options.h"
26#include "diagnostic.h"
27#include "spellcheck.h"
28
29static void prune_options (struct cl_decoded_option **, unsigned int *);
30
31/* An option that is undocumented, that takes a joined argument, and
32 that doesn't fit any of the classes of uses (language/common,
33 driver, target) is assumed to be a prefix used to catch
34 e.g. negated options, and stop them from being further shortened to
35 a prefix that could use the negated option as an argument. For
36 example, we want -gno-statement-frontiers to be taken as a negation
37 of -gstatement-frontiers, but without catching the gno- prefix and
38 signaling it's to be used for option remapping, it would end up
39 backtracked to g with no-statemnet-frontiers as the debug level. */
40
41static bool
42remapping_prefix_p (const struct cl_option *opt)
43{
44 return opt->flags & CL_UNDOCUMENTED
45 && opt->flags & CL_JOINED
46 && !(opt->flags & (CL_DRIVER | CL_TARGET | CL_COMMON | CL_LANG_ALL));
47}
48
49/* Perform a binary search to find which option the command-line INPUT
50 matches. Returns its index in the option array, and
51 OPT_SPECIAL_unknown on failure.
52
53 This routine is quite subtle. A normal binary search is not good
54 enough because some options can be suffixed with an argument, and
55 multiple sub-matches can occur, e.g. input of "-pedantic" matching
56 the initial substring of "-pedantic-errors".
57
58 A more complicated example is -gstabs. It should match "-g" with
59 an argument of "stabs". Suppose, however, that the number and list
60 of switches are such that the binary search tests "-gen-decls"
61 before having tested "-g". This doesn't match, and as "-gen-decls"
62 is less than "-gstabs", it will become the lower bound of the
63 binary search range, and "-g" will never be seen. To resolve this
64 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
65 to "-g" so that failed searches that end between "-gen-decls" and
66 the lexicographically subsequent switch know to go back and see if
67 "-g" causes a match (which it does in this example).
68
69 This search is done in such a way that the longest match for the
70 front end in question wins. If there is no match for the current
71 front end, the longest match for a different front end is returned
72 (or N_OPTS if none) and the caller emits an error message. */
73size_t
74find_opt (const char *input, unsigned int lang_mask)
75{
76 size_t mn, mn_orig, mx, md, opt_len;
77 size_t match_wrong_lang;
78 int comp;
79
80 mn = 0;
81 mx = cl_options_count;
82
83 /* Find mn such this lexicographical inequality holds:
84 cl_options[mn] <= input < cl_options[mn + 1]. */
85 while (mx - mn > 1)
86 {
87 md = (mn + mx) / 2;
88 opt_len = cl_options[md].opt_len;
89 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
90
91 if (comp < 0)
92 mx = md;
93 else
94 mn = md;
95 }
96
97 mn_orig = mn;
98
99 /* This is the switch that is the best match but for a different
100 front end, or OPT_SPECIAL_unknown if there is no match at all. */
101 match_wrong_lang = OPT_SPECIAL_unknown;
102
103 /* Backtrace the chain of possible matches, returning the longest
104 one, if any, that fits best. With current GCC switches, this
105 loop executes at most twice. */
106 do
107 {
108 const struct cl_option *opt = &cl_options[mn];
109
110 /* Is the input either an exact match or a prefix that takes a
111 joined argument? */
112 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
113 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
114 {
115 /* If language is OK, return it. */
116 if (opt->flags & lang_mask)
117 return mn;
118
119 if (remapping_prefix_p (opt))
120 return OPT_SPECIAL_unknown;
121
122 /* If we haven't remembered a prior match, remember this
123 one. Any prior match is necessarily better. */
124 if (match_wrong_lang == OPT_SPECIAL_unknown)
125 match_wrong_lang = mn;
126 }
127
128 /* Try the next possibility. This is cl_options_count if there
129 are no more. */
130 mn = opt->back_chain;
131 }
132 while (mn != cl_options_count);
133
134 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
135 {
136 /* Long options, starting "--", may be abbreviated if the
137 abbreviation is unambiguous. This only applies to options
138 not taking a joined argument, and abbreviations of "--option"
139 are permitted even if there is a variant "--option=". */
140 size_t mnc = mn_orig + 1;
141 size_t cmp_len = strlen (input);
142 while (mnc < cl_options_count
143 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
144 {
145 /* Option matching this abbreviation. OK if it is the first
146 match and that does not take a joined argument, or the
147 second match, taking a joined argument and with only '='
148 added to the first match; otherwise considered
149 ambiguous. */
150 if (mnc == mn_orig + 1
151 && !(cl_options[mnc].flags & CL_JOINED))
152 match_wrong_lang = mnc;
153 else if (mnc == mn_orig + 2
154 && match_wrong_lang == mn_orig + 1
155 && (cl_options[mnc].flags & CL_JOINED)
156 && (cl_options[mnc].opt_len
157 == cl_options[mn_orig + 1].opt_len + 1)
158 && strncmp (cl_options[mnc].opt_text + 1,
159 cl_options[mn_orig + 1].opt_text + 1,
160 cl_options[mn_orig + 1].opt_len) == 0)
161 ; /* OK, as long as there are no more matches. */
162 else
163 return OPT_SPECIAL_unknown;
164 mnc++;
165 }
166 }
167
168 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
169 return match_wrong_lang;
170}
171
172/* If ARG is a non-negative decimal or hexadecimal integer, return its
173 value, otherwise return -1. */
174
175int
176integral_argument (const char *arg)
177{
178 const char *p = arg;
179
180 while (*p && ISDIGIT (*p))
181 p++;
182
183 if (*p == '\0')
184 return atoi (arg);
185
186 /* It wasn't a decimal number - try hexadecimal. */
187 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
188 {
189 p = arg + 2;
190 while (*p && ISXDIGIT (*p))
191 p++;
192
193 if (p != arg + 2 && *p == '\0')
194 return strtol (arg, NULL, 16);
195 }
196
197 return -1;
198}
199
200/* Return whether OPTION is OK for the language given by
201 LANG_MASK. */
202static bool
203option_ok_for_language (const struct cl_option *option,
204 unsigned int lang_mask)
205{
206 if (!(option->flags & lang_mask))
207 return false;
208 else if ((option->flags & CL_TARGET)
209 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
210 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
211 /* Complain for target flag language mismatches if any languages
212 are specified. */
213 return false;
214 return true;
215}
216
217/* Return whether ENUM_ARG is OK for the language given by
218 LANG_MASK. */
219
220static bool
221enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
222 unsigned int lang_mask)
223{
224 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
225}
226
227/* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
228 storing the value in *VALUE if found, and returning false without
229 modifying *VALUE if not found. */
230
231static bool
232enum_arg_to_value (const struct cl_enum_arg *enum_args,
233 const char *arg, int *value, unsigned int lang_mask)
234{
235 unsigned int i;
236
237 for (i = 0; enum_args[i].arg != NULL; i++)
238 if (strcmp (arg, enum_args[i].arg) == 0
239 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
240 {
241 *value = enum_args[i].value;
242 return true;
243 }
244
245 return false;
246}
247
248/* Look up ARG in the enum used by option OPT_INDEX for language
249 LANG_MASK, returning true and storing the value in *VALUE if found,
250 and returning false without modifying *VALUE if not found. */
251
252bool
253opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
254 unsigned int lang_mask)
255{
256 const struct cl_option *option = &cl_options[opt_index];
257
258 gcc_assert (option->var_type == CLVC_ENUM);
259
260 return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
261 value, lang_mask);
262}
263
264/* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
265 corresponding string in *ARGP, returning true if the found string
266 was marked as canonical, false otherwise. If VALUE is not found
267 (which may be the case for uninitialized values if the relevant
268 option has not been passed), set *ARGP to NULL and return
269 false. */
270
271bool
272enum_value_to_arg (const struct cl_enum_arg *enum_args,
273 const char **argp, int value, unsigned int lang_mask)
274{
275 unsigned int i;
276
277 for (i = 0; enum_args[i].arg != NULL; i++)
278 if (enum_args[i].value == value
279 && (enum_args[i].flags & CL_ENUM_CANONICAL)
280 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
281 {
282 *argp = enum_args[i].arg;
283 return true;
284 }
285
286 for (i = 0; enum_args[i].arg != NULL; i++)
287 if (enum_args[i].value == value
288 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
289 {
290 *argp = enum_args[i].arg;
291 return false;
292 }
293
294 *argp = NULL;
295 return false;
296}
297
298/* Fill in the canonical option part of *DECODED with an option
299 described by OPT_INDEX, ARG and VALUE. */
300
301static void
302generate_canonical_option (size_t opt_index, const char *arg, int value,
303 struct cl_decoded_option *decoded)
304{
305 const struct cl_option *option = &cl_options[opt_index];
306 const char *opt_text = option->opt_text;
307
308 if (value == 0
309 && !option->cl_reject_negative
310 && (opt_text[1] == 'W' || opt_text[1] == 'f'
311 || opt_text[1] == 'g' || opt_text[1] == 'm'))
312 {
313 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
314 t[0] = '-';
315 t[1] = opt_text[1];
316 t[2] = 'n';
317 t[3] = 'o';
318 t[4] = '-';
319 memcpy (t + 5, opt_text + 2, option->opt_len);
320 opt_text = t;
321 }
322
323 decoded->canonical_option[2] = NULL;
324 decoded->canonical_option[3] = NULL;
325
326 if (arg)
327 {
328 if ((option->flags & CL_SEPARATE)
329 && !option->cl_separate_alias)
330 {
331 decoded->canonical_option[0] = opt_text;
332 decoded->canonical_option[1] = arg;
333 decoded->canonical_option_num_elements = 2;
334 }
335 else
336 {
337 gcc_assert (option->flags & CL_JOINED);
338 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
339 decoded->canonical_option[1] = NULL;
340 decoded->canonical_option_num_elements = 1;
341 }
342 }
343 else
344 {
345 decoded->canonical_option[0] = opt_text;
346 decoded->canonical_option[1] = NULL;
347 decoded->canonical_option_num_elements = 1;
348 }
349}
350
351/* Structure describing mappings from options on the command line to
352 options to look up with find_opt. */
353struct option_map
354{
355 /* Prefix of the option on the command line. */
356 const char *opt0;
357 /* If two argv elements are considered to be merged into one option,
358 prefix for the second element, otherwise NULL. */
359 const char *opt1;
360 /* The new prefix to map to. */
361 const char *new_prefix;
362 /* Whether at least one character is needed following opt1 or opt0
363 for this mapping to be used. (--optimize= is valid for -O, but
364 --warn- is not valid for -W.) */
365 bool another_char_needed;
366 /* Whether the original option is a negated form of the option
367 resulting from this map. */
368 bool negated;
369};
370static const struct option_map option_map[] =
371 {
372 { "-Wno-", NULL, "-W", false, true },
373 { "-fno-", NULL, "-f", false, true },
374 { "-gno-", NULL, "-g", false, true },
375 { "-mno-", NULL, "-m", false, true },
376 { "--debug=", NULL, "-g", false, false },
377 { "--machine-", NULL, "-m", true, false },
378 { "--machine-no-", NULL, "-m", false, true },
379 { "--machine=", NULL, "-m", false, false },
380 { "--machine=no-", NULL, "-m", false, true },
381 { "--machine", "", "-m", false, false },
382 { "--machine", "no-", "-m", false, true },
383 { "--optimize=", NULL, "-O", false, false },
384 { "--std=", NULL, "-std=", false, false },
385 { "--std", "", "-std=", false, false },
386 { "--warn-", NULL, "-W", true, false },
387 { "--warn-no-", NULL, "-W", false, true },
388 { "--", NULL, "-f", true, false },
389 { "--no-", NULL, "-f", false, true }
390 };
391
392/* Helper function for gcc.c's driver::suggest_option, for populating the
393 vec of suggestions for misspelled options.
394
395 option_map above provides various prefixes for spelling command-line
396 options, which decode_cmdline_option uses to map spellings of options
397 to specific options. We want to do the reverse: to find all the ways
398 that a user could validly spell an option.
399
400 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
401 of its valid variant spellings to CANDIDATES, each without a leading
402 dash.
403
404 For example, given "-Wabi-tag", the following are added to CANDIDATES:
405 "Wabi-tag"
406 "Wno-abi-tag"
407 "-warn-abi-tag"
408 "-warn-no-abi-tag".
409
410 The added strings must be freed using free. */
411
412void
413add_misspelling_candidates (auto_vec<char *> *candidates,
414 const struct cl_option *option,
415 const char *opt_text)
416{
417 gcc_assert (candidates);
418 gcc_assert (option);
419 gcc_assert (opt_text);
420 if (remapping_prefix_p (option))
421 return;
422 candidates->safe_push (xstrdup (opt_text + 1));
423 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
424 {
425 const char *opt0 = option_map[i].opt0;
426 const char *new_prefix = option_map[i].new_prefix;
427 size_t new_prefix_len = strlen (new_prefix);
428
429 if (option->cl_reject_negative && option_map[i].negated)
430 continue;
431
432 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
433 {
434 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
435 NULL);
436 candidates->safe_push (alternative);
437 }
438 }
439}
440
441/* Decode the switch beginning at ARGV for the language indicated by
442 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
443 the structure *DECODED. Returns the number of switches
444 consumed. */
445
446static unsigned int
447decode_cmdline_option (const char **argv, unsigned int lang_mask,
448 struct cl_decoded_option *decoded)
449{
450 size_t opt_index;
451 const char *arg = 0;
452 int value = 1;
453 unsigned int result = 1, i, extra_args, separate_args = 0;
454 int adjust_len = 0;
455 size_t total_len;
456 char *p;
457 const struct cl_option *option;
458 int errors = 0;
459 const char *warn_message = NULL;
460 bool separate_arg_flag;
461 bool joined_arg_flag;
462 bool have_separate_arg = false;
463
464 extra_args = 0;
465
466 opt_index = find_opt (argv[0] + 1, lang_mask);
467 i = 0;
468 while (opt_index == OPT_SPECIAL_unknown
469 && i < ARRAY_SIZE (option_map))
470 {
471 const char *opt0 = option_map[i].opt0;
472 const char *opt1 = option_map[i].opt1;
473 const char *new_prefix = option_map[i].new_prefix;
474 bool another_char_needed = option_map[i].another_char_needed;
475 size_t opt0_len = strlen (opt0);
476 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
477 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
478 size_t new_prefix_len = strlen (new_prefix);
479
480 extra_args = (opt1 == NULL ? 0 : 1);
481 value = !option_map[i].negated;
482
483 if (strncmp (argv[0], opt0, opt0_len) == 0
484 && (opt1 == NULL
485 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
486 && (!another_char_needed
487 || argv[extra_args][optn_len] != 0))
488 {
489 size_t arglen = strlen (argv[extra_args]);
490 char *dup;
491
492 adjust_len = (int) optn_len - (int) new_prefix_len;
493 dup = XNEWVEC (char, arglen + 1 - adjust_len);
494 memcpy (dup, new_prefix, new_prefix_len);
495 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
496 arglen - optn_len + 1);
497 opt_index = find_opt (dup + 1, lang_mask);
498 free (dup);
499 }
500 i++;
501 }
502
503 if (opt_index == OPT_SPECIAL_unknown)
504 {
505 arg = argv[0];
506 extra_args = 0;
507 value = 1;
508 goto done;
509 }
510
511 option = &cl_options[opt_index];
512
513 /* Reject negative form of switches that don't take negatives as
514 unrecognized. */
515 if (!value && option->cl_reject_negative)
516 {
517 opt_index = OPT_SPECIAL_unknown;
518 errors |= CL_ERR_NEGATIVE;
519 arg = argv[0];
520 goto done;
521 }
522
523 result = extra_args + 1;
524 warn_message = option->warn_message;
525
526 /* Check to see if the option is disabled for this configuration. */
527 if (option->cl_disabled)
528 errors |= CL_ERR_DISABLED;
529
530 /* Determine whether there may be a separate argument based on
531 whether this option is being processed for the driver, and, if
532 so, how many such arguments. */
533 separate_arg_flag = ((option->flags & CL_SEPARATE)
534 && !(option->cl_no_driver_arg
535 && (lang_mask & CL_DRIVER)));
536 separate_args = (separate_arg_flag
537 ? option->cl_separate_nargs + 1
538 : 0);
539 joined_arg_flag = (option->flags & CL_JOINED) != 0;
540
541 /* Sort out any argument the switch takes. */
542 if (joined_arg_flag)
543 {
544 /* Have arg point to the original switch. This is because
545 some code, such as disable_builtin_function, expects its
546 argument to be persistent until the program exits. */
547 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
548
549 if (*arg == '\0' && !option->cl_missing_ok)
550 {
551 if (separate_arg_flag)
552 {
553 arg = argv[extra_args + 1];
554 result = extra_args + 2;
555 if (arg == NULL)
556 result = extra_args + 1;
557 else
558 have_separate_arg = true;
559 }
560 else
561 /* Missing argument. */
562 arg = NULL;
563 }
564 }
565 else if (separate_arg_flag)
566 {
567 arg = argv[extra_args + 1];
568 for (i = 0; i < separate_args; i++)
569 if (argv[extra_args + 1 + i] == NULL)
570 {
571 errors |= CL_ERR_MISSING_ARG;
572 break;
573 }
574 result = extra_args + 1 + i;
575 if (arg != NULL)
576 have_separate_arg = true;
577 }
578
579 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
580 errors |= CL_ERR_MISSING_ARG;
581
582 /* Is this option an alias (or an ignored option, marked as an alias
583 of OPT_SPECIAL_ignore)? */
584 if (option->alias_target != N_OPTS
585 && (!option->cl_separate_alias || have_separate_arg))
586 {
587 size_t new_opt_index = option->alias_target;
588
589 if (new_opt_index == OPT_SPECIAL_ignore)
590 {
591 gcc_assert (option->alias_arg == NULL);
592 gcc_assert (option->neg_alias_arg == NULL);
593 opt_index = new_opt_index;
594 arg = NULL;
595 value = 1;
596 }
597 else
598 {
599 const struct cl_option *new_option = &cl_options[new_opt_index];
600
601 /* The new option must not be an alias itself. */
602 gcc_assert (new_option->alias_target == N_OPTS
603 || new_option->cl_separate_alias);
604
605 if (option->neg_alias_arg)
606 {
607 gcc_assert (option->alias_arg != NULL);
608 gcc_assert (arg == NULL);
609 gcc_assert (!option->cl_negative_alias);
610 if (value)
611 arg = option->alias_arg;
612 else
613 arg = option->neg_alias_arg;
614 value = 1;
615 }
616 else if (option->alias_arg)
617 {
618 gcc_assert (value == 1);
619 gcc_assert (arg == NULL);
620 gcc_assert (!option->cl_negative_alias);
621 arg = option->alias_arg;
622 }
623
624 if (option->cl_negative_alias)
625 value = !value;
626
627 opt_index = new_opt_index;
628 option = new_option;
629
630 if (value == 0)
631 gcc_assert (!option->cl_reject_negative);
632
633 /* Recompute what arguments are allowed. */
634 separate_arg_flag = ((option->flags & CL_SEPARATE)
635 && !(option->cl_no_driver_arg
636 && (lang_mask & CL_DRIVER)));
637 joined_arg_flag = (option->flags & CL_JOINED) != 0;
638
639 if (separate_args > 1 || option->cl_separate_nargs)
640 gcc_assert (separate_args
641 == (unsigned int) option->cl_separate_nargs + 1);
642
643 if (!(errors & CL_ERR_MISSING_ARG))
644 {
645 if (separate_arg_flag || joined_arg_flag)
646 {
647 if (option->cl_missing_ok && arg == NULL)
648 arg = "";
649 gcc_assert (arg != NULL);
650 }
651 else
652 gcc_assert (arg == NULL);
653 }
654
655 /* Recheck for warnings and disabled options. */
656 if (option->warn_message)
657 {
658 gcc_assert (warn_message == NULL);
659 warn_message = option->warn_message;
660 }
661 if (option->cl_disabled)
662 errors |= CL_ERR_DISABLED;
663 }
664 }
665
666 /* Check if this is a switch for a different front end. */
667 if (!option_ok_for_language (option, lang_mask))
668 errors |= CL_ERR_WRONG_LANG;
669
670 /* Convert the argument to lowercase if appropriate. */
671 if (arg && option->cl_tolower)
672 {
673 size_t j;
674 size_t len = strlen (arg);
675 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
676
677 for (j = 0; j < len; j++)
678 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
679 arg_lower[len] = 0;
680 arg = arg_lower;
681 }
682
683 /* If the switch takes an integer, convert it. */
684 if (arg && option->cl_uinteger)
685 {
686 value = integral_argument (arg);
687 if (value == -1)
688 errors |= CL_ERR_UINT_ARG;
689
690 /* Reject value out of a range. */
691 if (option->range_max != -1
692 && (value < option->range_min || value > option->range_max))
693 errors |= CL_ERR_INT_RANGE_ARG;
694 }
695
696 /* If the switch takes an enumerated argument, convert it. */
697 if (arg && (option->var_type == CLVC_ENUM))
698 {
699 const struct cl_enum *e = &cl_enums[option->var_enum];
700
701 gcc_assert (value == 1);
702 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
703 {
704 const char *carg = NULL;
705
706 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
707 arg = carg;
708 gcc_assert (carg != NULL);
709 }
710 else
711 errors |= CL_ERR_ENUM_ARG;
712 }
713
714 done:
715 decoded->opt_index = opt_index;
716 decoded->arg = arg;
717 decoded->value = value;
718 decoded->errors = errors;
719 decoded->warn_message = warn_message;
720
721 if (opt_index == OPT_SPECIAL_unknown)
722 gcc_assert (result == 1);
723
724 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
725 decoded->canonical_option_num_elements = result;
726 total_len = 0;
727 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
728 {
729 if (i < result)
730 {
731 size_t len;
732 if (opt_index == OPT_SPECIAL_unknown)
733 decoded->canonical_option[i] = argv[i];
734 else
735 decoded->canonical_option[i] = NULL;
736 len = strlen (argv[i]);
737 /* If the argument is an empty string, we will print it as "" in
738 orig_option_with_args_text. */
739 total_len += (len != 0 ? len : 2) + 1;
740 }
741 else
742 decoded->canonical_option[i] = NULL;
743 }
744 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
745 {
746 generate_canonical_option (opt_index, arg, value, decoded);
747 if (separate_args > 1)
748 {
749 for (i = 0; i < separate_args; i++)
750 {
751 if (argv[extra_args + 1 + i] == NULL)
752 break;
753 else
754 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
755 }
756 gcc_assert (result == 1 + i);
757 decoded->canonical_option_num_elements = result;
758 }
759 }
760 decoded->orig_option_with_args_text
761 = p = XOBNEWVEC (&opts_obstack, char, total_len);
762 for (i = 0; i < result; i++)
763 {
764 size_t len = strlen (argv[i]);
765
766 /* Print the empty string verbally. */
767 if (len == 0)
768 {
769 *p++ = '"';
770 *p++ = '"';
771 }
772 else
773 memcpy (p, argv[i], len);
774 p += len;
775 if (i == result - 1)
776 *p++ = 0;
777 else
778 *p++ = ' ';
779 }
780
781 return result;
782}
783
784/* Obstack for option strings. */
785
786struct obstack opts_obstack;
787
788/* Like libiberty concat, but allocate using opts_obstack. */
789
790char *
791opts_concat (const char *first, ...)
792{
793 char *newstr, *end;
794 size_t length = 0;
795 const char *arg;
796 va_list ap;
797
798 /* First compute the size of the result and get sufficient memory. */
799 va_start (ap, first);
800 for (arg = first; arg; arg = va_arg (ap, const char *))
801 length += strlen (arg);
802 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
803 va_end (ap);
804
805 /* Now copy the individual pieces to the result string. */
806 va_start (ap, first);
807 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
808 {
809 length = strlen (arg);
810 memcpy (end, arg, length);
811 end += length;
812 }
813 *end = '\0';
814 va_end (ap);
815 return newstr;
816}
817
818/* Decode command-line options (ARGC and ARGV being the arguments of
819 main) into an array, setting *DECODED_OPTIONS to a pointer to that
820 array and *DECODED_OPTIONS_COUNT to the number of entries in the
821 array. The first entry in the array is always one for the program
822 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
823 flags applicable for decoding (including CL_COMMON and CL_TARGET if
824 those options should be considered applicable). Do not produce any
825 diagnostics or set state outside of these variables. */
826
827void
828decode_cmdline_options_to_array (unsigned int argc, const char **argv,
829 unsigned int lang_mask,
830 struct cl_decoded_option **decoded_options,
831 unsigned int *decoded_options_count)
832{
833 unsigned int n, i;
834 struct cl_decoded_option *opt_array;
835 unsigned int num_decoded_options;
836
837 opt_array = XNEWVEC (struct cl_decoded_option, argc);
838
839 opt_array[0].opt_index = OPT_SPECIAL_program_name;
840 opt_array[0].warn_message = NULL;
841 opt_array[0].arg = argv[0];
842 opt_array[0].orig_option_with_args_text = argv[0];
843 opt_array[0].canonical_option_num_elements = 1;
844 opt_array[0].canonical_option[0] = argv[0];
845 opt_array[0].canonical_option[1] = NULL;
846 opt_array[0].canonical_option[2] = NULL;
847 opt_array[0].canonical_option[3] = NULL;
848 opt_array[0].value = 1;
849 opt_array[0].errors = 0;
850 num_decoded_options = 1;
851
852 for (i = 1; i < argc; i += n)
853 {
854 const char *opt = argv[i];
855
856 /* Interpret "-" or a non-switch as a file name. */
857 if (opt[0] != '-' || opt[1] == '\0')
858 {
859 generate_option_input_file (opt, &opt_array[num_decoded_options]);
860 num_decoded_options++;
861 n = 1;
862 continue;
863 }
864
865 n = decode_cmdline_option (argv + i, lang_mask,
866 &opt_array[num_decoded_options]);
867 num_decoded_options++;
868 }
869
870 *decoded_options = opt_array;
871 *decoded_options_count = num_decoded_options;
872 prune_options (decoded_options, decoded_options_count);
873}
874
875/* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
876 next one is the same as ORIG_NEXT_OPT_IDX. */
877
878static bool
879cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
880{
881 /* An option can be canceled by the same option or an option with
882 Negative. */
883 if (cl_options [next_opt_idx].neg_index == opt_idx)
884 return true;
885
886 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
887 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
888 orig_next_opt_idx);
889
890 return false;
891}
892
893/* Filter out options canceled by the ones after them. */
894
895static void
896prune_options (struct cl_decoded_option **decoded_options,
897 unsigned int *decoded_options_count)
898{
899 unsigned int old_decoded_options_count = *decoded_options_count;
900 struct cl_decoded_option *old_decoded_options = *decoded_options;
901 unsigned int new_decoded_options_count;
902 struct cl_decoded_option *new_decoded_options
903 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
904 unsigned int i;
905 const struct cl_option *option;
906 unsigned int fdiagnostics_color_idx = 0;
907
908 /* Remove arguments which are negated by others after them. */
909 new_decoded_options_count = 0;
910 for (i = 0; i < old_decoded_options_count; i++)
911 {
912 unsigned int j, opt_idx, next_opt_idx;
913
914 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
915 goto keep;
916
917 opt_idx = old_decoded_options[i].opt_index;
918 switch (opt_idx)
919 {
920 case OPT_SPECIAL_unknown:
921 case OPT_SPECIAL_ignore:
922 case OPT_SPECIAL_program_name:
923 case OPT_SPECIAL_input_file:
924 goto keep;
925
926 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
927 case OPT_fdiagnostics_color_:
928 fdiagnostics_color_idx = i;
929 continue;
930
931 default:
932 gcc_assert (opt_idx < cl_options_count);
933 option = &cl_options[opt_idx];
934 if (option->neg_index < 0)
935 goto keep;
936
937 /* Skip joined switches. */
938 if ((option->flags & CL_JOINED))
939 goto keep;
940
941 for (j = i + 1; j < old_decoded_options_count; j++)
942 {
943 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
944 continue;
945 next_opt_idx = old_decoded_options[j].opt_index;
946 if (next_opt_idx >= cl_options_count)
947 continue;
948 if (cl_options[next_opt_idx].neg_index < 0)
949 continue;
950 if ((cl_options[next_opt_idx].flags & CL_JOINED))
951 continue;
952 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
953 break;
954 }
955 if (j == old_decoded_options_count)
956 {
957keep:
958 new_decoded_options[new_decoded_options_count]
959 = old_decoded_options[i];
960 new_decoded_options_count++;
961 }
962 break;
963 }
964 }
965
966 if (fdiagnostics_color_idx >= 1)
967 {
968 /* We put the last -fdiagnostics-color= at the first position
969 after argv[0] so it can take effect immediately. */
970 memmove (new_decoded_options + 2, new_decoded_options + 1,
971 sizeof (struct cl_decoded_option)
972 * (new_decoded_options_count - 1));
973 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
974 new_decoded_options_count++;
975 }
976
977 free (old_decoded_options);
978 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
979 new_decoded_options,
980 new_decoded_options_count);
981 *decoded_options = new_decoded_options;
982 *decoded_options_count = new_decoded_options_count;
983}
984
985/* Handle option DECODED for the language indicated by LANG_MASK,
986 using the handlers in HANDLERS and setting fields in OPTS and
987 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
988 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
989 option for options from the source file, UNKNOWN_LOCATION
990 otherwise. GENERATED_P is true for an option generated as part of
991 processing another option or otherwise generated internally, false
992 for one explicitly passed by the user. control_warning_option
993 generated options are considered explicitly passed by the user.
994 Returns false if the switch was invalid. DC is the diagnostic
995 context for options affecting diagnostics state, or NULL. */
996
997static bool
998handle_option (struct gcc_options *opts,
999 struct gcc_options *opts_set,
1000 const struct cl_decoded_option *decoded,
1001 unsigned int lang_mask, int kind, location_t loc,
1002 const struct cl_option_handlers *handlers,
1003 bool generated_p, diagnostic_context *dc)
1004{
1005 size_t opt_index = decoded->opt_index;
1006 const char *arg = decoded->arg;
1007 int value = decoded->value;
1008 const struct cl_option *option = &cl_options[opt_index];
1009 void *flag_var = option_flag_var (opt_index, opts);
1010 size_t i;
1011
1012 if (flag_var)
1013 set_option (opts, (generated_p ? NULL : opts_set),
1014 opt_index, value, arg, kind, loc, dc);
1015
1016 for (i = 0; i < handlers->num_handlers; i++)
1017 if (option->flags & handlers->handlers[i].mask)
1018 {
1019 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1020 lang_mask, kind, loc,
1021 handlers, dc,
1022 handlers->target_option_override_hook))
1023 return false;
1024 }
1025
1026 return true;
1027}
1028
1029/* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1030 option instead of DECODED. This is used for callbacks when one
1031 option implies another instead of an option being decoded from the
1032 command line. */
1033
1034bool
1035handle_generated_option (struct gcc_options *opts,
1036 struct gcc_options *opts_set,
1037 size_t opt_index, const char *arg, int value,
1038 unsigned int lang_mask, int kind, location_t loc,
1039 const struct cl_option_handlers *handlers,
1040 bool generated_p, diagnostic_context *dc)
1041{
1042 struct cl_decoded_option decoded;
1043
1044 generate_option (opt_index, arg, value, lang_mask, &decoded);
1045 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1046 handlers, generated_p, dc);
1047}
1048
1049/* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1050 VALUE for a front end using LANG_MASK. This is used when the
1051 compiler generates options internally. */
1052
1053void
1054generate_option (size_t opt_index, const char *arg, int value,
1055 unsigned int lang_mask, struct cl_decoded_option *decoded)
1056{
1057 const struct cl_option *option = &cl_options[opt_index];
1058
1059 decoded->opt_index = opt_index;
1060 decoded->warn_message = NULL;
1061 decoded->arg = arg;
1062 decoded->value = value;
1063 decoded->errors = (option_ok_for_language (option, lang_mask)
1064 ? 0
1065 : CL_ERR_WRONG_LANG);
1066
1067 generate_canonical_option (opt_index, arg, value, decoded);
1068 switch (decoded->canonical_option_num_elements)
1069 {
1070 case 1:
1071 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1072 break;
1073
1074 case 2:
1075 decoded->orig_option_with_args_text
1076 = opts_concat (decoded->canonical_option[0], " ",
1077 decoded->canonical_option[1], NULL);
1078 break;
1079
1080 default:
1081 gcc_unreachable ();
1082 }
1083}
1084
1085/* Fill in *DECODED with an option for input file FILE. */
1086
1087void
1088generate_option_input_file (const char *file,
1089 struct cl_decoded_option *decoded)
1090{
1091 decoded->opt_index = OPT_SPECIAL_input_file;
1092 decoded->warn_message = NULL;
1093 decoded->arg = file;
1094 decoded->orig_option_with_args_text = file;
1095 decoded->canonical_option_num_elements = 1;
1096 decoded->canonical_option[0] = file;
1097 decoded->canonical_option[1] = NULL;
1098 decoded->canonical_option[2] = NULL;
1099 decoded->canonical_option[3] = NULL;
1100 decoded->value = 1;
1101 decoded->errors = 0;
1102}
1103
1104/* Helper function for listing valid choices and hint for misspelled
1105 value. CANDIDATES is a vector containing all valid strings,
1106 STR is set to a heap allocated string that contains all those
1107 strings concatenated, separated by spaces, and the return value
1108 is the closest string from those to ARG, or NULL if nothing is
1109 close enough. Callers should XDELETEVEC (STR) after using it
1110 to avoid memory leaks. */
1111
1112const char *
1113candidates_list_and_hint (const char *arg, char *&str,
1114 const auto_vec <const char *> &candidates)
1115{
1116 size_t len = 0;
1117 int i;
1118 const char *candidate;
1119 char *p;
1120
1121 FOR_EACH_VEC_ELT (candidates, i, candidate)
1122 len += strlen (candidate) + 1;
1123
1124 str = p = XNEWVEC (char, len);
1125 FOR_EACH_VEC_ELT (candidates, i, candidate)
1126 {
1127 len = strlen (candidate);
1128 memcpy (p, candidate, len);
1129 p[len] = ' ';
1130 p += len + 1;
1131 }
1132 p[-1] = '\0';
1133 return find_closest_string (arg, &candidates);
1134}
1135
1136/* Perform diagnostics for read_cmdline_option and control_warning_option
1137 functions. Returns true if an error has been diagnosed.
1138 LOC and LANG_MASK arguments like in read_cmdline_option.
1139 OPTION is the option to report diagnostics for, OPT the name
1140 of the option as text, ARG the argument of the option (for joined
1141 options), ERRORS is bitmask of CL_ERR_* values. */
1142
1143static bool
1144cmdline_handle_error (location_t loc, const struct cl_option *option,
1145 const char *opt, const char *arg, int errors,
1146 unsigned int lang_mask)
1147{
1148 if (errors & CL_ERR_DISABLED)
1149 {
1150 error_at (loc, "command line option %qs"
1151 " is not supported by this configuration", opt);
1152 return true;
1153 }
1154
1155 if (errors & CL_ERR_MISSING_ARG)
1156 {
1157 if (option->missing_argument_error)
1158 error_at (loc, option->missing_argument_error, opt);
1159 else
1160 error_at (loc, "missing argument to %qs", opt);
1161 return true;
1162 }
1163
1164 if (errors & CL_ERR_UINT_ARG)
1165 {
1166 error_at (loc, "argument to %qs should be a non-negative integer",
1167 option->opt_text);
1168 return true;
1169 }
1170
1171 if (errors & CL_ERR_INT_RANGE_ARG)
1172 {
1173 error_at (loc, "argument to %qs is not between %d and %d",
1174 option->opt_text, option->range_min, option->range_max);
1175 return true;
1176 }
1177
1178 if (errors & CL_ERR_ENUM_ARG)
1179 {
1180 const struct cl_enum *e = &cl_enums[option->var_enum];
1181 unsigned int i;
1182 char *s;
1183
1184 if (e->unknown_error)
1185 error_at (loc, e->unknown_error, arg);
1186 else
1187 error_at (loc, "unrecognized argument in option %qs", opt);
1188
1189 auto_vec <const char *> candidates;
1190 for (i = 0; e->values[i].arg != NULL; i++)
1191 {
1192 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1193 continue;
1194 candidates.safe_push (e->values[i].arg);
1195 }
1196 const char *hint = candidates_list_and_hint (arg, s, candidates);
1197 if (hint)
1198 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1199 option->opt_text, s, hint);
1200 else
1201 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1202 XDELETEVEC (s);
1203
1204 return true;
1205 }
1206
1207 return false;
1208}
1209
1210/* Handle the switch DECODED (location LOC) for the language indicated
1211 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1212 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1213 diagnostic options. */
1214
1215void
1216read_cmdline_option (struct gcc_options *opts,
1217 struct gcc_options *opts_set,
1218 struct cl_decoded_option *decoded,
1219 location_t loc,
1220 unsigned int lang_mask,
1221 const struct cl_option_handlers *handlers,
1222 diagnostic_context *dc)
1223{
1224 const struct cl_option *option;
1225 const char *opt = decoded->orig_option_with_args_text;
1226
1227 if (decoded->warn_message)
1228 warning_at (loc, 0, decoded->warn_message, opt);
1229
1230 if (decoded->opt_index == OPT_SPECIAL_unknown)
1231 {
1232 if (handlers->unknown_option_callback (decoded))
1233 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1234 return;
1235 }
1236
1237 if (decoded->opt_index == OPT_SPECIAL_ignore)
1238 return;
1239
1240 option = &cl_options[decoded->opt_index];
1241
1242 if (decoded->errors
1243 && cmdline_handle_error (loc, option, opt, decoded->arg,
1244 decoded->errors, lang_mask))
1245 return;
1246
1247 if (decoded->errors & CL_ERR_WRONG_LANG)
1248 {
1249 handlers->wrong_lang_callback (decoded, lang_mask);
1250 return;
1251 }
1252
1253 gcc_assert (!decoded->errors);
1254
1255 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1256 loc, handlers, false, dc))
1257 error_at (loc, "unrecognized command line option %qs", opt);
1258}
1259
1260/* Set any field in OPTS, and OPTS_SET if not NULL, for option
1261 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1262 location LOC, using diagnostic context DC if not NULL for
1263 diagnostic classification. */
1264
1265void
1266set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1267 int opt_index, int value, const char *arg, int kind,
1268 location_t loc, diagnostic_context *dc)
1269{
1270 const struct cl_option *option = &cl_options[opt_index];
1271 void *flag_var = option_flag_var (opt_index, opts);
1272 void *set_flag_var = NULL;
1273
1274 if (!flag_var)
1275 return;
1276
1277 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1278 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1279
1280 if (opts_set != NULL)
1281 set_flag_var = option_flag_var (opt_index, opts_set);
1282
1283 switch (option->var_type)
1284 {
1285 case CLVC_BOOLEAN:
1286 *(int *) flag_var = value;
1287 if (set_flag_var)
1288 *(int *) set_flag_var = 1;
1289 break;
1290
1291 case CLVC_EQUAL:
1292 if (option->cl_host_wide_int)
1293 *(HOST_WIDE_INT *) flag_var = (value
1294 ? option->var_value
1295 : !option->var_value);
1296 else
1297 *(int *) flag_var = (value
1298 ? option->var_value
1299 : !option->var_value);
1300 if (set_flag_var)
1301 *(int *) set_flag_var = 1;
1302 break;
1303
1304 case CLVC_BIT_CLEAR:
1305 case CLVC_BIT_SET:
1306 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1307 {
1308 if (option->cl_host_wide_int)
1309 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1310 else
1311 *(int *) flag_var |= option->var_value;
1312 }
1313 else
1314 {
1315 if (option->cl_host_wide_int)
1316 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1317 else
1318 *(int *) flag_var &= ~option->var_value;
1319 }
1320 if (set_flag_var)
1321 {
1322 if (option->cl_host_wide_int)
1323 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1324 else
1325 *(int *) set_flag_var |= option->var_value;
1326 }
1327 break;
1328
1329 case CLVC_STRING:
1330 *(const char **) flag_var = arg;
1331 if (set_flag_var)
1332 *(const char **) set_flag_var = "";
1333 break;
1334
1335 case CLVC_ENUM:
1336 {
1337 const struct cl_enum *e = &cl_enums[option->var_enum];
1338
1339 e->set (flag_var, value);
1340 if (set_flag_var)
1341 e->set (set_flag_var, 1);
1342 }
1343 break;
1344
1345 case CLVC_DEFER:
1346 {
1347 vec<cl_deferred_option> *v
1348 = (vec<cl_deferred_option> *) *(void **) flag_var;
1349 cl_deferred_option p = {opt_index, arg, value};
1350 if (!v)
1351 v = XCNEW (vec<cl_deferred_option>);
1352 v->safe_push (p);
1353 *(void **) flag_var = v;
1354 if (set_flag_var)
1355 *(void **) set_flag_var = v;
1356 }
1357 break;
1358 }
1359}
1360
1361/* Return the address of the flag variable for option OPT_INDEX in
1362 options structure OPTS, or NULL if there is no flag variable. */
1363
1364void *
1365option_flag_var (int opt_index, struct gcc_options *opts)
1366{
1367 const struct cl_option *option = &cl_options[opt_index];
1368
1369 if (option->flag_var_offset == (unsigned short) -1)
1370 return NULL;
1371 return (void *)(((char *) opts) + option->flag_var_offset);
1372}
1373
1374/* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1375 or -1 if it isn't a simple on-off switch. */
1376
1377int
1378option_enabled (int opt_idx, void *opts)
1379{
1380 const struct cl_option *option = &(cl_options[opt_idx]);
1381 struct gcc_options *optsg = (struct gcc_options *) opts;
1382 void *flag_var = option_flag_var (opt_idx, optsg);
1383
1384 if (flag_var)
1385 switch (option->var_type)
1386 {
1387 case CLVC_BOOLEAN:
1388 return *(int *) flag_var != 0;
1389
1390 case CLVC_EQUAL:
1391 if (option->cl_host_wide_int)
1392 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1393 else
1394 return *(int *) flag_var == option->var_value;
1395
1396 case CLVC_BIT_CLEAR:
1397 if (option->cl_host_wide_int)
1398 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1399 else
1400 return (*(int *) flag_var & option->var_value) == 0;
1401
1402 case CLVC_BIT_SET:
1403 if (option->cl_host_wide_int)
1404 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1405 else
1406 return (*(int *) flag_var & option->var_value) != 0;
1407
1408 case CLVC_STRING:
1409 case CLVC_ENUM:
1410 case CLVC_DEFER:
1411 break;
1412 }
1413 return -1;
1414}
1415
1416/* Fill STATE with the current state of option OPTION in OPTS. Return
1417 true if there is some state to store. */
1418
1419bool
1420get_option_state (struct gcc_options *opts, int option,
1421 struct cl_option_state *state)
1422{
1423 void *flag_var = option_flag_var (option, opts);
1424
1425 if (flag_var == 0)
1426 return false;
1427
1428 switch (cl_options[option].var_type)
1429 {
1430 case CLVC_BOOLEAN:
1431 case CLVC_EQUAL:
1432 state->data = flag_var;
1433 state->size = (cl_options[option].cl_host_wide_int
1434 ? sizeof (HOST_WIDE_INT)
1435 : sizeof (int));
1436 break;
1437
1438 case CLVC_BIT_CLEAR:
1439 case CLVC_BIT_SET:
1440 state->ch = option_enabled (option, opts);
1441 state->data = &state->ch;
1442 state->size = 1;
1443 break;
1444
1445 case CLVC_STRING:
1446 state->data = *(const char **) flag_var;
1447 if (state->data == 0)
1448 state->data = "";
1449 state->size = strlen ((const char *) state->data) + 1;
1450 break;
1451
1452 case CLVC_ENUM:
1453 state->data = flag_var;
1454 state->size = cl_enums[cl_options[option].var_enum].var_size;
1455 break;
1456
1457 case CLVC_DEFER:
1458 return false;
1459 }
1460 return true;
1461}
1462
1463/* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1464 handlers HANDLERS) to have diagnostic kind KIND for option
1465 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1466 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1467 argument of the option for joined options, or NULL otherwise. If IMPLY,
1468 the warning option in question is implied at this point. This is
1469 used by -Werror= and #pragma GCC diagnostic. */
1470
1471void
1472control_warning_option (unsigned int opt_index, int kind, const char *arg,
1473 bool imply, location_t loc, unsigned int lang_mask,
1474 const struct cl_option_handlers *handlers,
1475 struct gcc_options *opts,
1476 struct gcc_options *opts_set,
1477 diagnostic_context *dc)
1478{
1479 if (cl_options[opt_index].alias_target != N_OPTS)
1480 {
1481 gcc_assert (!cl_options[opt_index].cl_separate_alias
1482 && !cl_options[opt_index].cl_negative_alias);
1483 if (cl_options[opt_index].alias_arg)
1484 arg = cl_options[opt_index].alias_arg;
1485 opt_index = cl_options[opt_index].alias_target;
1486 }
1487 if (opt_index == OPT_SPECIAL_ignore)
1488 return;
1489 if (dc)
1490 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1491 if (imply)
1492 {
1493 const struct cl_option *option = &cl_options[opt_index];
1494
1495 /* -Werror=foo implies -Wfoo. */
1496 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1497 {
1498 int value = 1;
1499
1500 if (arg && *arg == '\0' && !option->cl_missing_ok)
1501 arg = NULL;
1502
1503 if ((option->flags & CL_JOINED) && arg == NULL)
1504 {
1505 cmdline_handle_error (loc, option, option->opt_text, arg,
1506 CL_ERR_MISSING_ARG, lang_mask);
1507 return;
1508 }
1509
1510 /* If the switch takes an integer, convert it. */
1511 if (arg && option->cl_uinteger)
1512 {
1513 value = integral_argument (arg);
1514 if (value == -1)
1515 {
1516 cmdline_handle_error (loc, option, option->opt_text, arg,
1517 CL_ERR_UINT_ARG, lang_mask);
1518 return;
1519 }
1520 }
1521
1522 /* If the switch takes an enumerated argument, convert it. */
1523 if (arg && option->var_type == CLVC_ENUM)
1524 {
1525 const struct cl_enum *e = &cl_enums[option->var_enum];
1526
1527 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1528 {
1529 const char *carg = NULL;
1530
1531 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1532 arg = carg;
1533 gcc_assert (carg != NULL);
1534 }
1535 else
1536 {
1537 cmdline_handle_error (loc, option, option->opt_text, arg,
1538 CL_ERR_ENUM_ARG, lang_mask);
1539 return;
1540 }
1541 }
1542
1543 handle_generated_option (opts, opts_set,
1544 opt_index, arg, value, lang_mask,
1545 kind, loc, handlers, false, dc);
1546 }
1547 }
1548}
1549