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