Update .po files.
[official-gcc.git] / gcc / opts-common.c
blobbb689827227bffc1c675e21ac784096ba7d1af4d
1 /* Command line option handling.
2 Copyright (C) 2006-2016 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 "options.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 /* Helper function for gcc.c's driver::suggest_option, for populating the
369 vec of suggestions for misspelled options.
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.
376 Given valid OPT_TEXT (with a leading dash), add it and all of its valid
377 variant spellings to CANDIDATES, each without a leading dash.
379 For example, given "-Wabi-tag", the following are added to CANDIDATES:
380 "Wabi-tag"
381 "Wno-abi-tag"
382 "-warn-abi-tag"
383 "-warn-no-abi-tag".
385 The added strings must be freed using free. */
387 void
388 add_misspelling_candidates (auto_vec<char *> *candidates,
389 const char *opt_text)
391 gcc_assert (candidates);
392 gcc_assert (opt_text);
393 candidates->safe_push (xstrdup (opt_text + 1));
394 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
396 const char *opt0 = option_map[i].opt0;
397 const char *new_prefix = option_map[i].new_prefix;
398 size_t new_prefix_len = strlen (new_prefix);
400 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
402 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
403 NULL);
404 candidates->safe_push (alternative);
409 /* Decode the switch beginning at ARGV for the language indicated by
410 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
411 the structure *DECODED. Returns the number of switches
412 consumed. */
414 static unsigned int
415 decode_cmdline_option (const char **argv, unsigned int lang_mask,
416 struct cl_decoded_option *decoded)
418 size_t opt_index;
419 const char *arg = 0;
420 int value = 1;
421 unsigned int result = 1, i, extra_args, separate_args = 0;
422 int adjust_len = 0;
423 size_t total_len;
424 char *p;
425 const struct cl_option *option;
426 int errors = 0;
427 const char *warn_message = NULL;
428 bool separate_arg_flag;
429 bool joined_arg_flag;
430 bool have_separate_arg = false;
432 extra_args = 0;
434 opt_index = find_opt (argv[0] + 1, lang_mask);
435 i = 0;
436 while (opt_index == OPT_SPECIAL_unknown
437 && i < ARRAY_SIZE (option_map))
439 const char *opt0 = option_map[i].opt0;
440 const char *opt1 = option_map[i].opt1;
441 const char *new_prefix = option_map[i].new_prefix;
442 bool another_char_needed = option_map[i].another_char_needed;
443 size_t opt0_len = strlen (opt0);
444 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
445 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
446 size_t new_prefix_len = strlen (new_prefix);
448 extra_args = (opt1 == NULL ? 0 : 1);
449 value = !option_map[i].negated;
451 if (strncmp (argv[0], opt0, opt0_len) == 0
452 && (opt1 == NULL
453 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
454 && (!another_char_needed
455 || argv[extra_args][optn_len] != 0))
457 size_t arglen = strlen (argv[extra_args]);
458 char *dup;
460 adjust_len = (int) optn_len - (int) new_prefix_len;
461 dup = XNEWVEC (char, arglen + 1 - adjust_len);
462 memcpy (dup, new_prefix, new_prefix_len);
463 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
464 arglen - optn_len + 1);
465 opt_index = find_opt (dup + 1, lang_mask);
466 free (dup);
468 i++;
471 if (opt_index == OPT_SPECIAL_unknown)
473 arg = argv[0];
474 extra_args = 0;
475 value = 1;
476 goto done;
479 option = &cl_options[opt_index];
481 /* Reject negative form of switches that don't take negatives as
482 unrecognized. */
483 if (!value && option->cl_reject_negative)
485 opt_index = OPT_SPECIAL_unknown;
486 errors |= CL_ERR_NEGATIVE;
487 arg = argv[0];
488 goto done;
491 result = extra_args + 1;
492 warn_message = option->warn_message;
494 /* Check to see if the option is disabled for this configuration. */
495 if (option->cl_disabled)
496 errors |= CL_ERR_DISABLED;
498 /* Determine whether there may be a separate argument based on
499 whether this option is being processed for the driver, and, if
500 so, how many such arguments. */
501 separate_arg_flag = ((option->flags & CL_SEPARATE)
502 && !(option->cl_no_driver_arg
503 && (lang_mask & CL_DRIVER)));
504 separate_args = (separate_arg_flag
505 ? option->cl_separate_nargs + 1
506 : 0);
507 joined_arg_flag = (option->flags & CL_JOINED) != 0;
509 /* Sort out any argument the switch takes. */
510 if (joined_arg_flag)
512 /* Have arg point to the original switch. This is because
513 some code, such as disable_builtin_function, expects its
514 argument to be persistent until the program exits. */
515 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
517 if (*arg == '\0' && !option->cl_missing_ok)
519 if (separate_arg_flag)
521 arg = argv[extra_args + 1];
522 result = extra_args + 2;
523 if (arg == NULL)
524 result = extra_args + 1;
525 else
526 have_separate_arg = true;
528 else
529 /* Missing argument. */
530 arg = NULL;
533 else if (separate_arg_flag)
535 arg = argv[extra_args + 1];
536 for (i = 0; i < separate_args; i++)
537 if (argv[extra_args + 1 + i] == NULL)
539 errors |= CL_ERR_MISSING_ARG;
540 break;
542 result = extra_args + 1 + i;
543 if (arg != NULL)
544 have_separate_arg = true;
547 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
548 errors |= CL_ERR_MISSING_ARG;
550 /* Is this option an alias (or an ignored option, marked as an alias
551 of OPT_SPECIAL_ignore)? */
552 if (option->alias_target != N_OPTS
553 && (!option->cl_separate_alias || have_separate_arg))
555 size_t new_opt_index = option->alias_target;
557 if (new_opt_index == OPT_SPECIAL_ignore)
559 gcc_assert (option->alias_arg == NULL);
560 gcc_assert (option->neg_alias_arg == NULL);
561 opt_index = new_opt_index;
562 arg = NULL;
563 value = 1;
565 else
567 const struct cl_option *new_option = &cl_options[new_opt_index];
569 /* The new option must not be an alias itself. */
570 gcc_assert (new_option->alias_target == N_OPTS
571 || new_option->cl_separate_alias);
573 if (option->neg_alias_arg)
575 gcc_assert (option->alias_arg != NULL);
576 gcc_assert (arg == NULL);
577 gcc_assert (!option->cl_negative_alias);
578 if (value)
579 arg = option->alias_arg;
580 else
581 arg = option->neg_alias_arg;
582 value = 1;
584 else if (option->alias_arg)
586 gcc_assert (value == 1);
587 gcc_assert (arg == NULL);
588 gcc_assert (!option->cl_negative_alias);
589 arg = option->alias_arg;
592 if (option->cl_negative_alias)
593 value = !value;
595 opt_index = new_opt_index;
596 option = new_option;
598 if (value == 0)
599 gcc_assert (!option->cl_reject_negative);
601 /* Recompute what arguments are allowed. */
602 separate_arg_flag = ((option->flags & CL_SEPARATE)
603 && !(option->cl_no_driver_arg
604 && (lang_mask & CL_DRIVER)));
605 joined_arg_flag = (option->flags & CL_JOINED) != 0;
607 if (separate_args > 1 || option->cl_separate_nargs)
608 gcc_assert (separate_args
609 == (unsigned int) option->cl_separate_nargs + 1);
611 if (!(errors & CL_ERR_MISSING_ARG))
613 if (separate_arg_flag || joined_arg_flag)
615 if (option->cl_missing_ok && arg == NULL)
616 arg = "";
617 gcc_assert (arg != NULL);
619 else
620 gcc_assert (arg == NULL);
623 /* Recheck for warnings and disabled options. */
624 if (option->warn_message)
626 gcc_assert (warn_message == NULL);
627 warn_message = option->warn_message;
629 if (option->cl_disabled)
630 errors |= CL_ERR_DISABLED;
634 /* Check if this is a switch for a different front end. */
635 if (!option_ok_for_language (option, lang_mask))
636 errors |= CL_ERR_WRONG_LANG;
638 /* Convert the argument to lowercase if appropriate. */
639 if (arg && option->cl_tolower)
641 size_t j;
642 size_t len = strlen (arg);
643 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
645 for (j = 0; j < len; j++)
646 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
647 arg_lower[len] = 0;
648 arg = arg_lower;
651 /* If the switch takes an integer, convert it. */
652 if (arg && option->cl_uinteger)
654 value = integral_argument (arg);
655 if (value == -1)
656 errors |= CL_ERR_UINT_ARG;
659 /* If the switch takes an enumerated argument, convert it. */
660 if (arg && (option->var_type == CLVC_ENUM))
662 const struct cl_enum *e = &cl_enums[option->var_enum];
664 gcc_assert (value == 1);
665 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
667 const char *carg = NULL;
669 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
670 arg = carg;
671 gcc_assert (carg != NULL);
673 else
674 errors |= CL_ERR_ENUM_ARG;
677 done:
678 decoded->opt_index = opt_index;
679 decoded->arg = arg;
680 decoded->value = value;
681 decoded->errors = errors;
682 decoded->warn_message = warn_message;
684 if (opt_index == OPT_SPECIAL_unknown)
685 gcc_assert (result == 1);
687 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
688 decoded->canonical_option_num_elements = result;
689 total_len = 0;
690 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
692 if (i < result)
694 size_t len;
695 if (opt_index == OPT_SPECIAL_unknown)
696 decoded->canonical_option[i] = argv[i];
697 else
698 decoded->canonical_option[i] = NULL;
699 len = strlen (argv[i]);
700 /* If the argument is an empty string, we will print it as "" in
701 orig_option_with_args_text. */
702 total_len += (len != 0 ? len : 2) + 1;
704 else
705 decoded->canonical_option[i] = NULL;
707 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
709 generate_canonical_option (opt_index, arg, value, decoded);
710 if (separate_args > 1)
712 for (i = 0; i < separate_args; i++)
714 if (argv[extra_args + 1 + i] == NULL)
715 break;
716 else
717 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
719 gcc_assert (result == 1 + i);
720 decoded->canonical_option_num_elements = result;
723 decoded->orig_option_with_args_text
724 = p = XOBNEWVEC (&opts_obstack, char, total_len);
725 for (i = 0; i < result; i++)
727 size_t len = strlen (argv[i]);
729 /* Print the empty string verbally. */
730 if (len == 0)
732 *p++ = '"';
733 *p++ = '"';
735 else
736 memcpy (p, argv[i], len);
737 p += len;
738 if (i == result - 1)
739 *p++ = 0;
740 else
741 *p++ = ' ';
744 return result;
747 /* Obstack for option strings. */
749 struct obstack opts_obstack;
751 /* Like libiberty concat, but allocate using opts_obstack. */
753 char *
754 opts_concat (const char *first, ...)
756 char *newstr, *end;
757 size_t length = 0;
758 const char *arg;
759 va_list ap;
761 /* First compute the size of the result and get sufficient memory. */
762 va_start (ap, first);
763 for (arg = first; arg; arg = va_arg (ap, const char *))
764 length += strlen (arg);
765 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
766 va_end (ap);
768 /* Now copy the individual pieces to the result string. */
769 va_start (ap, first);
770 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
772 length = strlen (arg);
773 memcpy (end, arg, length);
774 end += length;
776 *end = '\0';
777 va_end (ap);
778 return newstr;
781 /* Decode command-line options (ARGC and ARGV being the arguments of
782 main) into an array, setting *DECODED_OPTIONS to a pointer to that
783 array and *DECODED_OPTIONS_COUNT to the number of entries in the
784 array. The first entry in the array is always one for the program
785 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
786 flags applicable for decoding (including CL_COMMON and CL_TARGET if
787 those options should be considered applicable). Do not produce any
788 diagnostics or set state outside of these variables. */
790 void
791 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
792 unsigned int lang_mask,
793 struct cl_decoded_option **decoded_options,
794 unsigned int *decoded_options_count)
796 unsigned int n, i;
797 struct cl_decoded_option *opt_array;
798 unsigned int num_decoded_options;
800 opt_array = XNEWVEC (struct cl_decoded_option, argc);
802 opt_array[0].opt_index = OPT_SPECIAL_program_name;
803 opt_array[0].warn_message = NULL;
804 opt_array[0].arg = argv[0];
805 opt_array[0].orig_option_with_args_text = argv[0];
806 opt_array[0].canonical_option_num_elements = 1;
807 opt_array[0].canonical_option[0] = argv[0];
808 opt_array[0].canonical_option[1] = NULL;
809 opt_array[0].canonical_option[2] = NULL;
810 opt_array[0].canonical_option[3] = NULL;
811 opt_array[0].value = 1;
812 opt_array[0].errors = 0;
813 num_decoded_options = 1;
815 for (i = 1; i < argc; i += n)
817 const char *opt = argv[i];
819 /* Interpret "-" or a non-switch as a file name. */
820 if (opt[0] != '-' || opt[1] == '\0')
822 generate_option_input_file (opt, &opt_array[num_decoded_options]);
823 num_decoded_options++;
824 n = 1;
825 continue;
828 n = decode_cmdline_option (argv + i, lang_mask,
829 &opt_array[num_decoded_options]);
830 num_decoded_options++;
833 *decoded_options = opt_array;
834 *decoded_options_count = num_decoded_options;
835 prune_options (decoded_options, decoded_options_count);
838 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
839 next one is the same as ORIG_NEXT_OPT_IDX. */
841 static bool
842 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
844 /* An option can be canceled by the same option or an option with
845 Negative. */
846 if (cl_options [next_opt_idx].neg_index == opt_idx)
847 return true;
849 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
850 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
851 orig_next_opt_idx);
853 return false;
856 /* Filter out options canceled by the ones after them. */
858 static void
859 prune_options (struct cl_decoded_option **decoded_options,
860 unsigned int *decoded_options_count)
862 unsigned int old_decoded_options_count = *decoded_options_count;
863 struct cl_decoded_option *old_decoded_options = *decoded_options;
864 unsigned int new_decoded_options_count;
865 struct cl_decoded_option *new_decoded_options
866 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
867 unsigned int i;
868 const struct cl_option *option;
869 unsigned int fdiagnostics_color_idx = 0;
871 /* Remove arguments which are negated by others after them. */
872 new_decoded_options_count = 0;
873 for (i = 0; i < old_decoded_options_count; i++)
875 unsigned int j, opt_idx, next_opt_idx;
877 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
878 goto keep;
880 opt_idx = old_decoded_options[i].opt_index;
881 switch (opt_idx)
883 case OPT_SPECIAL_unknown:
884 case OPT_SPECIAL_ignore:
885 case OPT_SPECIAL_program_name:
886 case OPT_SPECIAL_input_file:
887 goto keep;
889 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
890 case OPT_fdiagnostics_color_:
891 fdiagnostics_color_idx = i;
892 continue;
894 default:
895 gcc_assert (opt_idx < cl_options_count);
896 option = &cl_options[opt_idx];
897 if (option->neg_index < 0)
898 goto keep;
900 /* Skip joined switches. */
901 if ((option->flags & CL_JOINED))
902 goto keep;
904 for (j = i + 1; j < old_decoded_options_count; j++)
906 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
907 continue;
908 next_opt_idx = old_decoded_options[j].opt_index;
909 if (next_opt_idx >= cl_options_count)
910 continue;
911 if (cl_options[next_opt_idx].neg_index < 0)
912 continue;
913 if ((cl_options[next_opt_idx].flags & CL_JOINED))
914 continue;
915 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
916 break;
918 if (j == old_decoded_options_count)
920 keep:
921 new_decoded_options[new_decoded_options_count]
922 = old_decoded_options[i];
923 new_decoded_options_count++;
925 break;
929 if (fdiagnostics_color_idx >= 1)
931 /* We put the last -fdiagnostics-color= at the first position
932 after argv[0] so it can take effect immediately. */
933 memmove (new_decoded_options + 2, new_decoded_options + 1,
934 sizeof (struct cl_decoded_option)
935 * (new_decoded_options_count - 1));
936 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
937 new_decoded_options_count++;
940 free (old_decoded_options);
941 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
942 new_decoded_options,
943 new_decoded_options_count);
944 *decoded_options = new_decoded_options;
945 *decoded_options_count = new_decoded_options_count;
948 /* Handle option DECODED for the language indicated by LANG_MASK,
949 using the handlers in HANDLERS and setting fields in OPTS and
950 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
951 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
952 option for options from the source file, UNKNOWN_LOCATION
953 otherwise. GENERATED_P is true for an option generated as part of
954 processing another option or otherwise generated internally, false
955 for one explicitly passed by the user. Returns false if the switch
956 was invalid. DC is the diagnostic context for options affecting
957 diagnostics state, or NULL. */
959 static bool
960 handle_option (struct gcc_options *opts,
961 struct gcc_options *opts_set,
962 const struct cl_decoded_option *decoded,
963 unsigned int lang_mask, int kind, location_t loc,
964 const struct cl_option_handlers *handlers,
965 bool generated_p, diagnostic_context *dc)
967 size_t opt_index = decoded->opt_index;
968 const char *arg = decoded->arg;
969 int value = decoded->value;
970 const struct cl_option *option = &cl_options[opt_index];
971 void *flag_var = option_flag_var (opt_index, opts);
972 size_t i;
974 if (flag_var)
975 set_option (opts, (generated_p ? NULL : opts_set),
976 opt_index, value, arg, kind, loc, dc);
978 for (i = 0; i < handlers->num_handlers; i++)
979 if (option->flags & handlers->handlers[i].mask)
981 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
982 lang_mask, kind, loc,
983 handlers, dc))
984 return false;
987 return true;
990 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
991 option instead of DECODED. This is used for callbacks when one
992 option implies another instead of an option being decoded from the
993 command line. */
995 bool
996 handle_generated_option (struct gcc_options *opts,
997 struct gcc_options *opts_set,
998 size_t opt_index, const char *arg, int value,
999 unsigned int lang_mask, int kind, location_t loc,
1000 const struct cl_option_handlers *handlers,
1001 diagnostic_context *dc)
1003 struct cl_decoded_option decoded;
1005 generate_option (opt_index, arg, value, lang_mask, &decoded);
1006 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1007 handlers, true, dc);
1010 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1011 VALUE for a front end using LANG_MASK. This is used when the
1012 compiler generates options internally. */
1014 void
1015 generate_option (size_t opt_index, const char *arg, int value,
1016 unsigned int lang_mask, struct cl_decoded_option *decoded)
1018 const struct cl_option *option = &cl_options[opt_index];
1020 decoded->opt_index = opt_index;
1021 decoded->warn_message = NULL;
1022 decoded->arg = arg;
1023 decoded->value = value;
1024 decoded->errors = (option_ok_for_language (option, lang_mask)
1026 : CL_ERR_WRONG_LANG);
1028 generate_canonical_option (opt_index, arg, value, decoded);
1029 switch (decoded->canonical_option_num_elements)
1031 case 1:
1032 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1033 break;
1035 case 2:
1036 decoded->orig_option_with_args_text
1037 = opts_concat (decoded->canonical_option[0], " ",
1038 decoded->canonical_option[1], NULL);
1039 break;
1041 default:
1042 gcc_unreachable ();
1046 /* Fill in *DECODED with an option for input file FILE. */
1048 void
1049 generate_option_input_file (const char *file,
1050 struct cl_decoded_option *decoded)
1052 decoded->opt_index = OPT_SPECIAL_input_file;
1053 decoded->warn_message = NULL;
1054 decoded->arg = file;
1055 decoded->orig_option_with_args_text = file;
1056 decoded->canonical_option_num_elements = 1;
1057 decoded->canonical_option[0] = file;
1058 decoded->canonical_option[1] = NULL;
1059 decoded->canonical_option[2] = NULL;
1060 decoded->canonical_option[3] = NULL;
1061 decoded->value = 1;
1062 decoded->errors = 0;
1065 /* Perform diagnostics for read_cmdline_option and control_warning_option
1066 functions. Returns true if an error has been diagnosed.
1067 LOC and LANG_MASK arguments like in read_cmdline_option.
1068 OPTION is the option to report diagnostics for, OPT the name
1069 of the option as text, ARG the argument of the option (for joined
1070 options), ERRORS is bitmask of CL_ERR_* values. */
1072 static bool
1073 cmdline_handle_error (location_t loc, const struct cl_option *option,
1074 const char *opt, const char *arg, int errors,
1075 unsigned int lang_mask)
1077 if (errors & CL_ERR_DISABLED)
1079 error_at (loc, "command line option %qs"
1080 " is not supported by this configuration", opt);
1081 return true;
1084 if (errors & CL_ERR_MISSING_ARG)
1086 if (option->missing_argument_error)
1087 error_at (loc, option->missing_argument_error, opt);
1088 else
1089 error_at (loc, "missing argument to %qs", opt);
1090 return true;
1093 if (errors & CL_ERR_UINT_ARG)
1095 error_at (loc, "argument to %qs should be a non-negative integer",
1096 option->opt_text);
1097 return true;
1100 if (errors & CL_ERR_ENUM_ARG)
1102 const struct cl_enum *e = &cl_enums[option->var_enum];
1103 unsigned int i;
1104 size_t len;
1105 char *s, *p;
1107 if (e->unknown_error)
1108 error_at (loc, e->unknown_error, arg);
1109 else
1110 error_at (loc, "unrecognized argument in option %qs", opt);
1112 len = 0;
1113 for (i = 0; e->values[i].arg != NULL; i++)
1114 len += strlen (e->values[i].arg) + 1;
1116 s = XALLOCAVEC (char, len);
1117 p = s;
1118 for (i = 0; e->values[i].arg != NULL; i++)
1120 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1121 continue;
1122 size_t arglen = strlen (e->values[i].arg);
1123 memcpy (p, e->values[i].arg, arglen);
1124 p[arglen] = ' ';
1125 p += arglen + 1;
1127 p[-1] = 0;
1128 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1129 return true;
1132 return false;
1135 /* Handle the switch DECODED (location LOC) for the language indicated
1136 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1137 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1138 diagnostic options. */
1140 void
1141 read_cmdline_option (struct gcc_options *opts,
1142 struct gcc_options *opts_set,
1143 struct cl_decoded_option *decoded,
1144 location_t loc,
1145 unsigned int lang_mask,
1146 const struct cl_option_handlers *handlers,
1147 diagnostic_context *dc)
1149 const struct cl_option *option;
1150 const char *opt = decoded->orig_option_with_args_text;
1152 if (decoded->warn_message)
1153 warning_at (loc, 0, decoded->warn_message, opt);
1155 if (decoded->opt_index == OPT_SPECIAL_unknown)
1157 if (handlers->unknown_option_callback (decoded))
1158 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1159 return;
1162 if (decoded->opt_index == OPT_SPECIAL_ignore)
1163 return;
1165 option = &cl_options[decoded->opt_index];
1167 if (decoded->errors
1168 && cmdline_handle_error (loc, option, opt, decoded->arg,
1169 decoded->errors, lang_mask))
1170 return;
1172 if (decoded->errors & CL_ERR_WRONG_LANG)
1174 handlers->wrong_lang_callback (decoded, lang_mask);
1175 return;
1178 gcc_assert (!decoded->errors);
1180 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1181 loc, handlers, false, dc))
1182 error_at (loc, "unrecognized command line option %qs", opt);
1185 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1186 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1187 location LOC, using diagnostic context DC if not NULL for
1188 diagnostic classification. */
1190 void
1191 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1192 int opt_index, int value, const char *arg, int kind,
1193 location_t loc, diagnostic_context *dc)
1195 const struct cl_option *option = &cl_options[opt_index];
1196 void *flag_var = option_flag_var (opt_index, opts);
1197 void *set_flag_var = NULL;
1199 if (!flag_var)
1200 return;
1202 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1203 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1205 if (opts_set != NULL)
1206 set_flag_var = option_flag_var (opt_index, opts_set);
1208 switch (option->var_type)
1210 case CLVC_BOOLEAN:
1211 *(int *) flag_var = value;
1212 if (set_flag_var)
1213 *(int *) set_flag_var = 1;
1214 break;
1216 case CLVC_EQUAL:
1217 if (option->cl_host_wide_int)
1218 *(HOST_WIDE_INT *) flag_var = (value
1219 ? option->var_value
1220 : !option->var_value);
1221 else
1222 *(int *) flag_var = (value
1223 ? option->var_value
1224 : !option->var_value);
1225 if (set_flag_var)
1226 *(int *) set_flag_var = 1;
1227 break;
1229 case CLVC_BIT_CLEAR:
1230 case CLVC_BIT_SET:
1231 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1233 if (option->cl_host_wide_int)
1234 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1235 else
1236 *(int *) flag_var |= option->var_value;
1238 else
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;
1245 if (set_flag_var)
1247 if (option->cl_host_wide_int)
1248 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1249 else
1250 *(int *) set_flag_var |= option->var_value;
1252 break;
1254 case CLVC_STRING:
1255 *(const char **) flag_var = arg;
1256 if (set_flag_var)
1257 *(const char **) set_flag_var = "";
1258 break;
1260 case CLVC_ENUM:
1262 const struct cl_enum *e = &cl_enums[option->var_enum];
1264 e->set (flag_var, value);
1265 if (set_flag_var)
1266 e->set (set_flag_var, 1);
1268 break;
1270 case CLVC_DEFER:
1272 vec<cl_deferred_option> *v
1273 = (vec<cl_deferred_option> *) *(void **) flag_var;
1274 cl_deferred_option p = {opt_index, arg, value};
1275 if (!v)
1276 v = XCNEW (vec<cl_deferred_option>);
1277 v->safe_push (p);
1278 *(void **) flag_var = v;
1279 if (set_flag_var)
1280 *(void **) set_flag_var = v;
1282 break;
1286 /* Return the address of the flag variable for option OPT_INDEX in
1287 options structure OPTS, or NULL if there is no flag variable. */
1289 void *
1290 option_flag_var (int opt_index, struct gcc_options *opts)
1292 const struct cl_option *option = &cl_options[opt_index];
1294 if (option->flag_var_offset == (unsigned short) -1)
1295 return NULL;
1296 return (void *)(((char *) opts) + option->flag_var_offset);
1299 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1300 or -1 if it isn't a simple on-off switch. */
1303 option_enabled (int opt_idx, void *opts)
1305 const struct cl_option *option = &(cl_options[opt_idx]);
1306 struct gcc_options *optsg = (struct gcc_options *) opts;
1307 void *flag_var = option_flag_var (opt_idx, optsg);
1309 if (flag_var)
1310 switch (option->var_type)
1312 case CLVC_BOOLEAN:
1313 return *(int *) flag_var != 0;
1315 case CLVC_EQUAL:
1316 if (option->cl_host_wide_int)
1317 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1318 else
1319 return *(int *) flag_var == option->var_value;
1321 case CLVC_BIT_CLEAR:
1322 if (option->cl_host_wide_int)
1323 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1324 else
1325 return (*(int *) flag_var & option->var_value) == 0;
1327 case CLVC_BIT_SET:
1328 if (option->cl_host_wide_int)
1329 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1330 else
1331 return (*(int *) flag_var & option->var_value) != 0;
1333 case CLVC_STRING:
1334 case CLVC_ENUM:
1335 case CLVC_DEFER:
1336 break;
1338 return -1;
1341 /* Fill STATE with the current state of option OPTION in OPTS. Return
1342 true if there is some state to store. */
1344 bool
1345 get_option_state (struct gcc_options *opts, int option,
1346 struct cl_option_state *state)
1348 void *flag_var = option_flag_var (option, opts);
1350 if (flag_var == 0)
1351 return false;
1353 switch (cl_options[option].var_type)
1355 case CLVC_BOOLEAN:
1356 case CLVC_EQUAL:
1357 state->data = flag_var;
1358 state->size = (cl_options[option].cl_host_wide_int
1359 ? sizeof (HOST_WIDE_INT)
1360 : sizeof (int));
1361 break;
1363 case CLVC_BIT_CLEAR:
1364 case CLVC_BIT_SET:
1365 state->ch = option_enabled (option, opts);
1366 state->data = &state->ch;
1367 state->size = 1;
1368 break;
1370 case CLVC_STRING:
1371 state->data = *(const char **) flag_var;
1372 if (state->data == 0)
1373 state->data = "";
1374 state->size = strlen ((const char *) state->data) + 1;
1375 break;
1377 case CLVC_ENUM:
1378 state->data = flag_var;
1379 state->size = cl_enums[cl_options[option].var_enum].var_size;
1380 break;
1382 case CLVC_DEFER:
1383 return false;
1385 return true;
1388 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1389 handlers HANDLERS) to have diagnostic kind KIND for option
1390 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1391 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1392 argument of the option for joined options, or NULL otherwise. If IMPLY,
1393 the warning option in question is implied at this point. This is
1394 used by -Werror= and #pragma GCC diagnostic. */
1396 void
1397 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1398 bool imply, location_t loc, unsigned int lang_mask,
1399 const struct cl_option_handlers *handlers,
1400 struct gcc_options *opts,
1401 struct gcc_options *opts_set,
1402 diagnostic_context *dc)
1404 if (cl_options[opt_index].alias_target != N_OPTS)
1406 gcc_assert (!cl_options[opt_index].cl_separate_alias
1407 && !cl_options[opt_index].cl_negative_alias);
1408 if (cl_options[opt_index].alias_arg)
1409 arg = cl_options[opt_index].alias_arg;
1410 opt_index = cl_options[opt_index].alias_target;
1412 if (opt_index == OPT_SPECIAL_ignore)
1413 return;
1414 if (dc)
1415 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1416 if (imply)
1418 const struct cl_option *option = &cl_options[opt_index];
1420 /* -Werror=foo implies -Wfoo. */
1421 if (option->var_type == CLVC_BOOLEAN || option->var_type == CLVC_ENUM)
1423 int value = 1;
1425 if (arg && *arg == '\0' && !option->cl_missing_ok)
1426 arg = NULL;
1428 if ((option->flags & CL_JOINED) && arg == NULL)
1430 cmdline_handle_error (loc, option, option->opt_text, arg,
1431 CL_ERR_MISSING_ARG, lang_mask);
1432 return;
1435 /* If the switch takes an integer, convert it. */
1436 if (arg && option->cl_uinteger)
1438 value = integral_argument (arg);
1439 if (value == -1)
1441 cmdline_handle_error (loc, option, option->opt_text, arg,
1442 CL_ERR_UINT_ARG, lang_mask);
1443 return;
1447 /* If the switch takes an enumerated argument, convert it. */
1448 if (arg && option->var_type == CLVC_ENUM)
1450 const struct cl_enum *e = &cl_enums[option->var_enum];
1452 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1454 const char *carg = NULL;
1456 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1457 arg = carg;
1458 gcc_assert (carg != NULL);
1460 else
1462 cmdline_handle_error (loc, option, option->opt_text, arg,
1463 CL_ERR_ENUM_ARG, lang_mask);
1464 return;
1468 handle_generated_option (opts, opts_set,
1469 opt_index, arg, value, lang_mask,
1470 kind, loc, handlers, dc);