Check int_size_in_bytes in ix86_return_in_memory
[official-gcc.git] / gcc / opts-common.c
blobe53e85fd0d5a8ac26c2cadd5bcc4ca15f19dda7b
1 /* Command line option handling.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "intl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "flags.h"
26 #include "diagnostic.h"
28 static void prune_options (struct cl_decoded_option **, unsigned int *);
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.
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".
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).
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
55 find_opt (const char *input, unsigned int lang_mask)
57 size_t mn, mn_orig, mx, md, opt_len;
58 size_t match_wrong_lang;
59 int comp;
61 mn = 0;
62 mx = cl_options_count;
64 /* Find mn such this lexicographical inequality holds:
65 cl_options[mn] <= input < cl_options[mn + 1]. */
66 while (mx - mn > 1)
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);
72 if (comp < 0)
73 mx = md;
74 else
75 mn = md;
78 mn_orig = mn;
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;
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. */
89 const struct cl_option *opt = &cl_options[mn];
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)))
96 /* If language is OK, return it. */
97 if (opt->flags & lang_mask)
98 return mn;
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;
106 /* Try the next possibility. This is cl_options_count if there
107 are no more. */
108 mn = opt->back_chain;
110 while (mn != cl_options_count);
112 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
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)
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++;
146 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
147 return match_wrong_lang;
150 /* If ARG is a non-negative decimal or hexadecimal integer, return its
151 value, otherwise return -1. */
154 integral_argument (const char *arg)
156 const char *p = arg;
158 while (*p && ISDIGIT (*p))
159 p++;
161 if (*p == '\0')
162 return atoi (arg);
164 /* It wasn't a decimal number - try hexadecimal. */
165 if (arg[0] == '0' && (arg[1] == 'x' || arg[1] == 'X'))
167 p = arg + 2;
168 while (*p && ISXDIGIT (*p))
169 p++;
171 if (p != arg + 2 && *p == '\0')
172 return strtol (arg, NULL, 16);
175 return -1;
178 /* Return whether OPTION is OK for the language given by
179 LANG_MASK. */
180 static bool
181 option_ok_for_language (const struct cl_option *option,
182 unsigned int lang_mask)
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;
195 /* Return whether ENUM_ARG is OK for the language given by
196 LANG_MASK. */
198 static bool
199 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
200 unsigned int lang_mask)
202 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
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. */
209 static bool
210 enum_arg_to_value (const struct cl_enum_arg *enum_args,
211 const char *arg, int *value, unsigned int lang_mask)
213 unsigned int i;
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))
219 *value = enum_args[i].value;
220 return true;
223 return false;
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. */
230 bool
231 opt_enum_arg_to_value (size_t opt_index, const char *arg, int *value,
232 unsigned int lang_mask)
234 const struct cl_option *option = &cl_options[opt_index];
236 gcc_assert (option->var_type == CLVC_ENUM);
238 return enum_arg_to_value (cl_enums[option->var_enum].values, arg,
239 value, lang_mask);
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. */
249 bool
250 enum_value_to_arg (const struct cl_enum_arg *enum_args,
251 const char **argp, int value, unsigned int lang_mask)
253 unsigned int i;
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))
260 *argp = enum_args[i].arg;
261 return true;
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))
268 *argp = enum_args[i].arg;
269 return false;
272 *argp = NULL;
273 return false;
276 /* Fill in the canonical option part of *DECODED with an option
277 described by OPT_INDEX, ARG and VALUE. */
279 static void
280 generate_canonical_option (size_t opt_index, const char *arg, int value,
281 struct cl_decoded_option *decoded)
283 const struct cl_option *option = &cl_options[opt_index];
284 const char *opt_text = option->opt_text;
286 if (value == 0
287 && !option->cl_reject_negative
288 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
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;
300 decoded->canonical_option[2] = NULL;
301 decoded->canonical_option[3] = NULL;
303 if (arg)
305 if ((option->flags & CL_SEPARATE)
306 && !option->cl_separate_alias)
308 decoded->canonical_option[0] = opt_text;
309 decoded->canonical_option[1] = arg;
310 decoded->canonical_option_num_elements = 2;
312 else
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;
320 else
322 decoded->canonical_option[0] = opt_text;
323 decoded->canonical_option[1] = NULL;
324 decoded->canonical_option_num_elements = 1;
328 /* Structure describing mappings from options on the command line to
329 options to look up with find_opt. */
330 struct option_map
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;
347 static const struct option_map option_map[] =
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 }
368 /* Decode the switch beginning at ARGV for the language indicated by
369 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
370 the structure *DECODED. Returns the number of switches
371 consumed. */
373 static unsigned int
374 decode_cmdline_option (const char **argv, unsigned int lang_mask,
375 struct cl_decoded_option *decoded)
377 size_t opt_index;
378 const char *arg = 0;
379 int value = 1;
380 unsigned int result = 1, i, extra_args, separate_args = 0;
381 int adjust_len = 0;
382 size_t total_len;
383 char *p;
384 const struct cl_option *option;
385 int errors = 0;
386 const char *warn_message = NULL;
387 bool separate_arg_flag;
388 bool joined_arg_flag;
389 bool have_separate_arg = false;
391 extra_args = 0;
393 opt_index = find_opt (argv[0] + 1, lang_mask);
394 i = 0;
395 while (opt_index == OPT_SPECIAL_unknown
396 && i < ARRAY_SIZE (option_map))
398 const char *opt0 = option_map[i].opt0;
399 const char *opt1 = option_map[i].opt1;
400 const char *new_prefix = option_map[i].new_prefix;
401 bool another_char_needed = option_map[i].another_char_needed;
402 size_t opt0_len = strlen (opt0);
403 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
404 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
405 size_t new_prefix_len = strlen (new_prefix);
407 extra_args = (opt1 == NULL ? 0 : 1);
408 value = !option_map[i].negated;
410 if (strncmp (argv[0], opt0, opt0_len) == 0
411 && (opt1 == NULL
412 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
413 && (!another_char_needed
414 || argv[extra_args][optn_len] != 0))
416 size_t arglen = strlen (argv[extra_args]);
417 char *dup;
419 adjust_len = (int) optn_len - (int) new_prefix_len;
420 dup = XNEWVEC (char, arglen + 1 - adjust_len);
421 memcpy (dup, new_prefix, new_prefix_len);
422 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
423 arglen - optn_len + 1);
424 opt_index = find_opt (dup + 1, lang_mask);
425 free (dup);
427 i++;
430 if (opt_index == OPT_SPECIAL_unknown)
432 arg = argv[0];
433 extra_args = 0;
434 value = 1;
435 goto done;
438 option = &cl_options[opt_index];
440 /* Reject negative form of switches that don't take negatives as
441 unrecognized. */
442 if (!value && option->cl_reject_negative)
444 opt_index = OPT_SPECIAL_unknown;
445 errors |= CL_ERR_NEGATIVE;
446 arg = argv[0];
447 goto done;
450 result = extra_args + 1;
451 warn_message = option->warn_message;
453 /* Check to see if the option is disabled for this configuration. */
454 if (option->cl_disabled)
455 errors |= CL_ERR_DISABLED;
457 /* Determine whether there may be a separate argument based on
458 whether this option is being processed for the driver, and, if
459 so, how many such arguments. */
460 separate_arg_flag = ((option->flags & CL_SEPARATE)
461 && !(option->cl_no_driver_arg
462 && (lang_mask & CL_DRIVER)));
463 separate_args = (separate_arg_flag
464 ? option->cl_separate_nargs + 1
465 : 0);
466 joined_arg_flag = (option->flags & CL_JOINED) != 0;
468 /* Sort out any argument the switch takes. */
469 if (joined_arg_flag)
471 /* Have arg point to the original switch. This is because
472 some code, such as disable_builtin_function, expects its
473 argument to be persistent until the program exits. */
474 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
476 if (*arg == '\0' && !option->cl_missing_ok)
478 if (separate_arg_flag)
480 arg = argv[extra_args + 1];
481 result = extra_args + 2;
482 if (arg == NULL)
483 result = extra_args + 1;
484 else
485 have_separate_arg = true;
487 else
488 /* Missing argument. */
489 arg = NULL;
492 else if (separate_arg_flag)
494 arg = argv[extra_args + 1];
495 for (i = 0; i < separate_args; i++)
496 if (argv[extra_args + 1 + i] == NULL)
498 errors |= CL_ERR_MISSING_ARG;
499 break;
501 result = extra_args + 1 + i;
502 if (arg != NULL)
503 have_separate_arg = true;
506 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
507 errors |= CL_ERR_MISSING_ARG;
509 /* Is this option an alias (or an ignored option, marked as an alias
510 of OPT_SPECIAL_ignore)? */
511 if (option->alias_target != N_OPTS
512 && (!option->cl_separate_alias || have_separate_arg))
514 size_t new_opt_index = option->alias_target;
516 if (new_opt_index == OPT_SPECIAL_ignore)
518 gcc_assert (option->alias_arg == NULL);
519 gcc_assert (option->neg_alias_arg == NULL);
520 opt_index = new_opt_index;
521 arg = NULL;
522 value = 1;
524 else
526 const struct cl_option *new_option = &cl_options[new_opt_index];
528 /* The new option must not be an alias itself. */
529 gcc_assert (new_option->alias_target == N_OPTS
530 || new_option->cl_separate_alias);
532 if (option->neg_alias_arg)
534 gcc_assert (option->alias_arg != NULL);
535 gcc_assert (arg == NULL);
536 gcc_assert (!option->cl_negative_alias);
537 if (value)
538 arg = option->alias_arg;
539 else
540 arg = option->neg_alias_arg;
541 value = 1;
543 else if (option->alias_arg)
545 gcc_assert (value == 1);
546 gcc_assert (arg == NULL);
547 gcc_assert (!option->cl_negative_alias);
548 arg = option->alias_arg;
551 if (option->cl_negative_alias)
552 value = !value;
554 opt_index = new_opt_index;
555 option = new_option;
557 if (value == 0)
558 gcc_assert (!option->cl_reject_negative);
560 /* Recompute what arguments are allowed. */
561 separate_arg_flag = ((option->flags & CL_SEPARATE)
562 && !(option->cl_no_driver_arg
563 && (lang_mask & CL_DRIVER)));
564 joined_arg_flag = (option->flags & CL_JOINED) != 0;
566 if (separate_args > 1 || option->cl_separate_nargs)
567 gcc_assert (separate_args
568 == (unsigned int) option->cl_separate_nargs + 1);
570 if (!(errors & CL_ERR_MISSING_ARG))
572 if (separate_arg_flag || joined_arg_flag)
574 if (option->cl_missing_ok && arg == NULL)
575 arg = "";
576 gcc_assert (arg != NULL);
578 else
579 gcc_assert (arg == NULL);
582 /* Recheck for warnings and disabled options. */
583 if (option->warn_message)
585 gcc_assert (warn_message == NULL);
586 warn_message = option->warn_message;
588 if (option->cl_disabled)
589 errors |= CL_ERR_DISABLED;
593 /* Check if this is a switch for a different front end. */
594 if (!option_ok_for_language (option, lang_mask))
595 errors |= CL_ERR_WRONG_LANG;
597 /* Convert the argument to lowercase if appropriate. */
598 if (arg && option->cl_tolower)
600 size_t j;
601 size_t len = strlen (arg);
602 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
604 for (j = 0; j < len; j++)
605 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
606 arg_lower[len] = 0;
607 arg = arg_lower;
610 /* If the switch takes an integer, convert it. */
611 if (arg && option->cl_uinteger)
613 value = integral_argument (arg);
614 if (value == -1)
615 errors |= CL_ERR_UINT_ARG;
618 /* If the switch takes an enumerated argument, convert it. */
619 if (arg && (option->var_type == CLVC_ENUM))
621 const struct cl_enum *e = &cl_enums[option->var_enum];
623 gcc_assert (value == 1);
624 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
626 const char *carg = NULL;
628 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
629 arg = carg;
630 gcc_assert (carg != NULL);
632 else
633 errors |= CL_ERR_ENUM_ARG;
636 done:
637 decoded->opt_index = opt_index;
638 decoded->arg = arg;
639 decoded->value = value;
640 decoded->errors = errors;
641 decoded->warn_message = warn_message;
643 if (opt_index == OPT_SPECIAL_unknown)
644 gcc_assert (result == 1);
646 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
647 decoded->canonical_option_num_elements = result;
648 total_len = 0;
649 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
651 if (i < result)
653 size_t len;
654 if (opt_index == OPT_SPECIAL_unknown)
655 decoded->canonical_option[i] = argv[i];
656 else
657 decoded->canonical_option[i] = NULL;
658 len = strlen (argv[i]);
659 /* If the argument is an empty string, we will print it as "" in
660 orig_option_with_args_text. */
661 total_len += (len != 0 ? len : 2) + 1;
663 else
664 decoded->canonical_option[i] = NULL;
666 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
668 generate_canonical_option (opt_index, arg, value, decoded);
669 if (separate_args > 1)
671 for (i = 0; i < separate_args; i++)
673 if (argv[extra_args + 1 + i] == NULL)
674 break;
675 else
676 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
678 gcc_assert (result == 1 + i);
679 decoded->canonical_option_num_elements = result;
682 decoded->orig_option_with_args_text
683 = p = XOBNEWVEC (&opts_obstack, char, total_len);
684 for (i = 0; i < result; i++)
686 size_t len = strlen (argv[i]);
688 /* Print the empty string verbally. */
689 if (len == 0)
691 *p++ = '"';
692 *p++ = '"';
694 else
695 memcpy (p, argv[i], len);
696 p += len;
697 if (i == result - 1)
698 *p++ = 0;
699 else
700 *p++ = ' ';
703 return result;
706 /* Obstack for option strings. */
708 struct obstack opts_obstack;
710 /* Like libiberty concat, but allocate using opts_obstack. */
712 char *
713 opts_concat (const char *first, ...)
715 char *newstr, *end;
716 size_t length = 0;
717 const char *arg;
718 va_list ap;
720 /* First compute the size of the result and get sufficient memory. */
721 va_start (ap, first);
722 for (arg = first; arg; arg = va_arg (ap, const char *))
723 length += strlen (arg);
724 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
725 va_end (ap);
727 /* Now copy the individual pieces to the result string. */
728 va_start (ap, first);
729 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
731 length = strlen (arg);
732 memcpy (end, arg, length);
733 end += length;
735 *end = '\0';
736 va_end (ap);
737 return newstr;
740 /* Decode command-line options (ARGC and ARGV being the arguments of
741 main) into an array, setting *DECODED_OPTIONS to a pointer to that
742 array and *DECODED_OPTIONS_COUNT to the number of entries in the
743 array. The first entry in the array is always one for the program
744 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
745 flags applicable for decoding (including CL_COMMON and CL_TARGET if
746 those options should be considered applicable). Do not produce any
747 diagnostics or set state outside of these variables. */
749 void
750 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
751 unsigned int lang_mask,
752 struct cl_decoded_option **decoded_options,
753 unsigned int *decoded_options_count)
755 unsigned int n, i;
756 struct cl_decoded_option *opt_array;
757 unsigned int num_decoded_options;
759 opt_array = XNEWVEC (struct cl_decoded_option, argc);
761 opt_array[0].opt_index = OPT_SPECIAL_program_name;
762 opt_array[0].warn_message = NULL;
763 opt_array[0].arg = argv[0];
764 opt_array[0].orig_option_with_args_text = argv[0];
765 opt_array[0].canonical_option_num_elements = 1;
766 opt_array[0].canonical_option[0] = argv[0];
767 opt_array[0].canonical_option[1] = NULL;
768 opt_array[0].canonical_option[2] = NULL;
769 opt_array[0].canonical_option[3] = NULL;
770 opt_array[0].value = 1;
771 opt_array[0].errors = 0;
772 num_decoded_options = 1;
774 for (i = 1; i < argc; i += n)
776 const char *opt = argv[i];
778 /* Interpret "-" or a non-switch as a file name. */
779 if (opt[0] != '-' || opt[1] == '\0')
781 generate_option_input_file (opt, &opt_array[num_decoded_options]);
782 num_decoded_options++;
783 n = 1;
784 continue;
787 n = decode_cmdline_option (argv + i, lang_mask,
788 &opt_array[num_decoded_options]);
789 num_decoded_options++;
792 *decoded_options = opt_array;
793 *decoded_options_count = num_decoded_options;
794 prune_options (decoded_options, decoded_options_count);
797 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
798 next one is the same as ORIG_NEXT_OPT_IDX. */
800 static bool
801 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
803 /* An option can be canceled by the same option or an option with
804 Negative. */
805 if (cl_options [next_opt_idx].neg_index == opt_idx)
806 return true;
808 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
809 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
810 orig_next_opt_idx);
812 return false;
815 /* Filter out options canceled by the ones after them. */
817 static void
818 prune_options (struct cl_decoded_option **decoded_options,
819 unsigned int *decoded_options_count)
821 unsigned int old_decoded_options_count = *decoded_options_count;
822 struct cl_decoded_option *old_decoded_options = *decoded_options;
823 unsigned int new_decoded_options_count;
824 struct cl_decoded_option *new_decoded_options
825 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
826 unsigned int i;
827 const struct cl_option *option;
829 /* Remove arguments which are negated by others after them. */
830 new_decoded_options_count = 0;
831 for (i = 0; i < old_decoded_options_count; i++)
833 unsigned int j, opt_idx, next_opt_idx;
835 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
836 goto keep;
838 opt_idx = old_decoded_options[i].opt_index;
839 switch (opt_idx)
841 case OPT_SPECIAL_unknown:
842 case OPT_SPECIAL_ignore:
843 case OPT_SPECIAL_program_name:
844 case OPT_SPECIAL_input_file:
845 goto keep;
847 default:
848 gcc_assert (opt_idx < cl_options_count);
849 option = &cl_options[opt_idx];
850 if (option->neg_index < 0)
851 goto keep;
853 /* Skip joined switches. */
854 if ((option->flags & CL_JOINED))
855 goto keep;
857 for (j = i + 1; j < old_decoded_options_count; j++)
859 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
860 continue;
861 next_opt_idx = old_decoded_options[j].opt_index;
862 if (next_opt_idx >= cl_options_count)
863 continue;
864 if (cl_options[next_opt_idx].neg_index < 0)
865 continue;
866 if ((cl_options[next_opt_idx].flags & CL_JOINED))
867 continue;
868 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
869 break;
871 if (j == old_decoded_options_count)
873 keep:
874 new_decoded_options[new_decoded_options_count]
875 = old_decoded_options[i];
876 new_decoded_options_count++;
878 break;
882 free (old_decoded_options);
883 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
884 new_decoded_options,
885 new_decoded_options_count);
886 *decoded_options = new_decoded_options;
887 *decoded_options_count = new_decoded_options_count;
890 /* Handle option DECODED for the language indicated by LANG_MASK,
891 using the handlers in HANDLERS and setting fields in OPTS and
892 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
893 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
894 option for options from the source file, UNKNOWN_LOCATION
895 otherwise. GENERATED_P is true for an option generated as part of
896 processing another option or otherwise generated internally, false
897 for one explicitly passed by the user. Returns false if the switch
898 was invalid. DC is the diagnostic context for options affecting
899 diagnostics state, or NULL. */
901 static bool
902 handle_option (struct gcc_options *opts,
903 struct gcc_options *opts_set,
904 const struct cl_decoded_option *decoded,
905 unsigned int lang_mask, int kind, location_t loc,
906 const struct cl_option_handlers *handlers,
907 bool generated_p, diagnostic_context *dc)
909 size_t opt_index = decoded->opt_index;
910 const char *arg = decoded->arg;
911 int value = decoded->value;
912 const struct cl_option *option = &cl_options[opt_index];
913 void *flag_var = option_flag_var (opt_index, opts);
914 size_t i;
916 if (flag_var)
917 set_option (opts, (generated_p ? NULL : opts_set),
918 opt_index, value, arg, kind, loc, dc);
920 for (i = 0; i < handlers->num_handlers; i++)
921 if (option->flags & handlers->handlers[i].mask)
923 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
924 lang_mask, kind, loc,
925 handlers, dc))
926 return false;
929 return true;
932 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
933 option instead of DECODED. This is used for callbacks when one
934 option implies another instead of an option being decoded from the
935 command line. */
937 bool
938 handle_generated_option (struct gcc_options *opts,
939 struct gcc_options *opts_set,
940 size_t opt_index, const char *arg, int value,
941 unsigned int lang_mask, int kind, location_t loc,
942 const struct cl_option_handlers *handlers,
943 diagnostic_context *dc)
945 struct cl_decoded_option decoded;
947 generate_option (opt_index, arg, value, lang_mask, &decoded);
948 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
949 handlers, true, dc);
952 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
953 VALUE for a front end using LANG_MASK. This is used when the
954 compiler generates options internally. */
956 void
957 generate_option (size_t opt_index, const char *arg, int value,
958 unsigned int lang_mask, struct cl_decoded_option *decoded)
960 const struct cl_option *option = &cl_options[opt_index];
962 decoded->opt_index = opt_index;
963 decoded->warn_message = NULL;
964 decoded->arg = arg;
965 decoded->value = value;
966 decoded->errors = (option_ok_for_language (option, lang_mask)
968 : CL_ERR_WRONG_LANG);
970 generate_canonical_option (opt_index, arg, value, decoded);
971 switch (decoded->canonical_option_num_elements)
973 case 1:
974 decoded->orig_option_with_args_text = decoded->canonical_option[0];
975 break;
977 case 2:
978 decoded->orig_option_with_args_text
979 = opts_concat (decoded->canonical_option[0], " ",
980 decoded->canonical_option[1], NULL);
981 break;
983 default:
984 gcc_unreachable ();
988 /* Fill in *DECODED with an option for input file FILE. */
990 void
991 generate_option_input_file (const char *file,
992 struct cl_decoded_option *decoded)
994 decoded->opt_index = OPT_SPECIAL_input_file;
995 decoded->warn_message = NULL;
996 decoded->arg = file;
997 decoded->orig_option_with_args_text = file;
998 decoded->canonical_option_num_elements = 1;
999 decoded->canonical_option[0] = file;
1000 decoded->canonical_option[1] = NULL;
1001 decoded->canonical_option[2] = NULL;
1002 decoded->canonical_option[3] = NULL;
1003 decoded->value = 1;
1004 decoded->errors = 0;
1007 /* Handle the switch DECODED (location LOC) for the language indicated
1008 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1009 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1010 diagnostic options. */
1012 void
1013 read_cmdline_option (struct gcc_options *opts,
1014 struct gcc_options *opts_set,
1015 struct cl_decoded_option *decoded,
1016 location_t loc,
1017 unsigned int lang_mask,
1018 const struct cl_option_handlers *handlers,
1019 diagnostic_context *dc)
1021 const struct cl_option *option;
1022 const char *opt = decoded->orig_option_with_args_text;
1024 if (decoded->warn_message)
1025 warning_at (loc, 0, decoded->warn_message, opt);
1027 if (decoded->opt_index == OPT_SPECIAL_unknown)
1029 if (handlers->unknown_option_callback (decoded))
1030 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1031 return;
1034 if (decoded->opt_index == OPT_SPECIAL_ignore)
1035 return;
1037 option = &cl_options[decoded->opt_index];
1039 if (decoded->errors & CL_ERR_DISABLED)
1041 error_at (loc, "command line option %qs"
1042 " is not supported by this configuration", opt);
1043 return;
1046 if (decoded->errors & CL_ERR_MISSING_ARG)
1048 if (option->missing_argument_error)
1049 error_at (loc, option->missing_argument_error, opt);
1050 else
1051 error_at (loc, "missing argument to %qs", opt);
1052 return;
1055 if (decoded->errors & CL_ERR_UINT_ARG)
1057 error_at (loc, "argument to %qs should be a non-negative integer",
1058 option->opt_text);
1059 return;
1062 if (decoded->errors & CL_ERR_ENUM_ARG)
1064 const struct cl_enum *e = &cl_enums[option->var_enum];
1065 unsigned int i;
1066 size_t len;
1067 char *s, *p;
1069 if (e->unknown_error)
1070 error_at (loc, e->unknown_error, decoded->arg);
1071 else
1072 error_at (loc, "unrecognized argument in option %qs", opt);
1074 len = 0;
1075 for (i = 0; e->values[i].arg != NULL; i++)
1076 len += strlen (e->values[i].arg) + 1;
1078 s = XALLOCAVEC (char, len);
1079 p = s;
1080 for (i = 0; e->values[i].arg != NULL; i++)
1082 size_t arglen = strlen (e->values[i].arg);
1083 memcpy (p, e->values[i].arg, arglen);
1084 p[arglen] = ' ';
1085 p += arglen + 1;
1087 p[-1] = 0;
1088 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1089 return;
1092 if (decoded->errors & CL_ERR_WRONG_LANG)
1094 handlers->wrong_lang_callback (decoded, lang_mask);
1095 return;
1098 gcc_assert (!decoded->errors);
1100 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1101 loc, handlers, false, dc))
1102 error_at (loc, "unrecognized command line option %qs", opt);
1105 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1106 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1107 location LOC, using diagnostic context DC if not NULL for
1108 diagnostic classification. */
1110 void
1111 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1112 int opt_index, int value, const char *arg, int kind,
1113 location_t loc, diagnostic_context *dc)
1115 const struct cl_option *option = &cl_options[opt_index];
1116 void *flag_var = option_flag_var (opt_index, opts);
1117 void *set_flag_var = NULL;
1119 if (!flag_var)
1120 return;
1122 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1123 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1125 if (opts_set != NULL)
1126 set_flag_var = option_flag_var (opt_index, opts_set);
1128 switch (option->var_type)
1130 case CLVC_BOOLEAN:
1131 *(int *) flag_var = value;
1132 if (set_flag_var)
1133 *(int *) set_flag_var = 1;
1134 break;
1136 case CLVC_EQUAL:
1137 if (option->cl_host_wide_int)
1138 *(HOST_WIDE_INT *) flag_var = (value
1139 ? option->var_value
1140 : !option->var_value);
1141 else
1142 *(int *) flag_var = (value
1143 ? option->var_value
1144 : !option->var_value);
1145 if (set_flag_var)
1146 *(int *) set_flag_var = 1;
1147 break;
1149 case CLVC_BIT_CLEAR:
1150 case CLVC_BIT_SET:
1151 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1153 if (option->cl_host_wide_int)
1154 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1155 else
1156 *(int *) flag_var |= option->var_value;
1158 else
1160 if (option->cl_host_wide_int)
1161 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1162 else
1163 *(int *) flag_var &= ~option->var_value;
1165 if (set_flag_var)
1167 if (option->cl_host_wide_int)
1168 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1169 else
1170 *(int *) set_flag_var |= option->var_value;
1172 break;
1174 case CLVC_STRING:
1175 *(const char **) flag_var = arg;
1176 if (set_flag_var)
1177 *(const char **) set_flag_var = "";
1178 break;
1180 case CLVC_ENUM:
1182 const struct cl_enum *e = &cl_enums[option->var_enum];
1184 e->set (flag_var, value);
1185 if (set_flag_var)
1186 e->set (set_flag_var, 1);
1188 break;
1190 case CLVC_DEFER:
1192 vec<cl_deferred_option> *v
1193 = (vec<cl_deferred_option> *) *(void **) flag_var;
1194 cl_deferred_option p = {opt_index, arg, value};
1195 if (!v)
1196 v = XCNEW (vec<cl_deferred_option>);
1197 v->safe_push (p);
1198 *(void **) flag_var = v;
1199 if (set_flag_var)
1200 *(void **) set_flag_var = v;
1202 break;
1206 /* Return the address of the flag variable for option OPT_INDEX in
1207 options structure OPTS, or NULL if there is no flag variable. */
1209 void *
1210 option_flag_var (int opt_index, struct gcc_options *opts)
1212 const struct cl_option *option = &cl_options[opt_index];
1214 if (option->flag_var_offset == (unsigned short) -1)
1215 return NULL;
1216 return (void *)(((char *) opts) + option->flag_var_offset);
1219 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1220 or -1 if it isn't a simple on-off switch. */
1223 option_enabled (int opt_idx, void *opts)
1225 const struct cl_option *option = &(cl_options[opt_idx]);
1226 struct gcc_options *optsg = (struct gcc_options *) opts;
1227 void *flag_var = option_flag_var (opt_idx, optsg);
1229 if (flag_var)
1230 switch (option->var_type)
1232 case CLVC_BOOLEAN:
1233 return *(int *) flag_var != 0;
1235 case CLVC_EQUAL:
1236 if (option->cl_host_wide_int)
1237 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1238 else
1239 return *(int *) flag_var == option->var_value;
1241 case CLVC_BIT_CLEAR:
1242 if (option->cl_host_wide_int)
1243 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1244 else
1245 return (*(int *) flag_var & option->var_value) == 0;
1247 case CLVC_BIT_SET:
1248 if (option->cl_host_wide_int)
1249 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1250 else
1251 return (*(int *) flag_var & option->var_value) != 0;
1253 case CLVC_STRING:
1254 case CLVC_ENUM:
1255 case CLVC_DEFER:
1256 break;
1258 return -1;
1261 /* Fill STATE with the current state of option OPTION in OPTS. Return
1262 true if there is some state to store. */
1264 bool
1265 get_option_state (struct gcc_options *opts, int option,
1266 struct cl_option_state *state)
1268 void *flag_var = option_flag_var (option, opts);
1270 if (flag_var == 0)
1271 return false;
1273 switch (cl_options[option].var_type)
1275 case CLVC_BOOLEAN:
1276 case CLVC_EQUAL:
1277 state->data = flag_var;
1278 state->size = (cl_options[option].cl_host_wide_int
1279 ? sizeof (HOST_WIDE_INT)
1280 : sizeof (int));
1281 break;
1283 case CLVC_BIT_CLEAR:
1284 case CLVC_BIT_SET:
1285 state->ch = option_enabled (option, opts);
1286 state->data = &state->ch;
1287 state->size = 1;
1288 break;
1290 case CLVC_STRING:
1291 state->data = *(const char **) flag_var;
1292 if (state->data == 0)
1293 state->data = "";
1294 state->size = strlen ((const char *) state->data) + 1;
1295 break;
1297 case CLVC_ENUM:
1298 state->data = flag_var;
1299 state->size = cl_enums[cl_options[option].var_enum].var_size;
1300 break;
1302 case CLVC_DEFER:
1303 return false;
1305 return true;
1308 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1309 handlers HANDLERS) to have diagnostic kind KIND for option
1310 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1311 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). If IMPLY,
1312 the warning option in question is implied at this point. This is
1313 used by -Werror= and #pragma GCC diagnostic. */
1315 void
1316 control_warning_option (unsigned int opt_index, int kind, bool imply,
1317 location_t loc, unsigned int lang_mask,
1318 const struct cl_option_handlers *handlers,
1319 struct gcc_options *opts,
1320 struct gcc_options *opts_set,
1321 diagnostic_context *dc)
1323 if (cl_options[opt_index].alias_target != N_OPTS)
1324 opt_index = cl_options[opt_index].alias_target;
1325 if (opt_index == OPT_SPECIAL_ignore)
1326 return;
1327 if (dc)
1328 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1329 if (imply)
1331 /* -Werror=foo implies -Wfoo. */
1332 if (cl_options[opt_index].var_type == CLVC_BOOLEAN)
1333 handle_generated_option (opts, opts_set,
1334 opt_index, NULL, 1, lang_mask,
1335 kind, loc, handlers, dc);