common.opt (-assemble, [...]): New.
[official-gcc.git] / gcc / opts-common.c
blob678b60e891b0b9e80e023791f6a1c1ef7fb2b676
1 /* Command line option handling.
2 Copyright (C) 2006, 2007, 2008, 2010 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"
27 #include "tm.h" /* For SWITCH_TAKES_ARG, WORD_SWITCH_TAKES_ARG and
28 TARGET_OPTION_TRANSLATE_TABLE. */
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, 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 integer made up solely of digits, 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 return -1;
167 /* Return whether OPTION is OK for the language given by
168 LANG_MASK. */
169 static bool
170 option_ok_for_language (const struct cl_option *option,
171 unsigned int lang_mask)
173 if (!(option->flags & lang_mask))
174 return false;
175 else if ((option->flags & CL_TARGET)
176 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
177 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
178 /* Complain for target flag language mismatches if any languages
179 are specified. */
180 return false;
181 return true;
185 /* Fill in the canonical option part of *DECODED with an option
186 described by OPT_INDEX, ARG and VALUE. */
188 static void
189 generate_canonical_option (size_t opt_index, const char *arg, int value,
190 struct cl_decoded_option *decoded)
192 const struct cl_option *option = &cl_options[opt_index];
193 const char *opt_text = option->opt_text;
195 if (value == 0
196 && !(option->flags & CL_REJECT_NEGATIVE)
197 && (opt_text[1] == 'W' || opt_text[1] == 'f' || opt_text[1] == 'm'))
199 char *t = XNEWVEC (char, option->opt_len + 5);
200 t[0] = '-';
201 t[1] = opt_text[1];
202 t[2] = 'n';
203 t[3] = 'o';
204 t[4] = '-';
205 memcpy (t + 5, opt_text + 2, option->opt_len);
206 opt_text = t;
209 decoded->canonical_option[2] = NULL;
210 decoded->canonical_option[3] = NULL;
212 if (arg)
214 if ((option->flags & CL_SEPARATE)
215 && !(option->flags & CL_SEPARATE_ALIAS))
217 decoded->canonical_option[0] = opt_text;
218 decoded->canonical_option[1] = arg;
219 decoded->canonical_option_num_elements = 2;
221 else
223 gcc_assert (option->flags & CL_JOINED);
224 decoded->canonical_option[0] = concat (opt_text, arg, NULL);
225 decoded->canonical_option[1] = NULL;
226 decoded->canonical_option_num_elements = 1;
229 else
231 decoded->canonical_option[0] = opt_text;
232 decoded->canonical_option[1] = NULL;
233 decoded->canonical_option_num_elements = 1;
237 /* Structure describing mappings from options on the command line to
238 options to look up with find_opt. */
239 struct option_map
241 /* Prefix of the option on the command line. */
242 const char *opt0;
243 /* If two argv elements are considered to be merged into one option,
244 prefix for the second element, otherwise NULL. */
245 const char *opt1;
246 /* The new prefix to map to. */
247 const char *new_prefix;
248 /* Whether at least one character is needed following opt1 or opt0
249 for this mapping to be used. (--optimize= is valid for -O, but
250 --warn- is not valid for -W.) */
251 bool another_char_needed;
252 /* Whether the original option is a negated form of the option
253 resulting from this map. */
254 bool negated;
256 static const struct option_map option_map[] =
258 { "-Wno-", NULL, "-W", false, true },
259 { "-fno-", NULL, "-f", false, true },
260 { "-mno-", NULL, "-m", false, true },
261 { "--debug=", NULL, "-g", false, false },
262 { "--machine-", NULL, "-m", true, false },
263 { "--machine-no-", NULL, "-m", false, true },
264 { "--machine=", NULL, "-m", false, false },
265 { "--machine=no-", NULL, "-m", false, true },
266 { "--machine", "", "-m", false, false },
267 { "--machine", "no-", "-m", false, true },
268 { "--optimize=", NULL, "-O", false, false },
269 { "--std=", NULL, "-std=", false, false },
270 { "--std", "", "-std=", false, false },
271 { "--warn-", NULL, "-W", true, false },
272 { "--warn-no-", NULL, "-W", false, true },
273 { "--", NULL, "-f", true, false },
274 { "--no-", NULL, "-f", false, true }
277 /* Decode the switch beginning at ARGV for the language indicated by
278 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
279 the structure *DECODED. Returns the number of switches
280 consumed. */
282 static unsigned int
283 decode_cmdline_option (const char **argv, unsigned int lang_mask,
284 struct cl_decoded_option *decoded)
286 size_t opt_index;
287 const char *arg = 0;
288 int value = 1;
289 unsigned int result = 1, i, extra_args;
290 int adjust_len = 0;
291 size_t total_len;
292 char *p;
293 const struct cl_option *option;
294 int errors = 0;
295 const char *warn_message = NULL;
296 bool separate_arg_flag;
297 bool joined_arg_flag;
298 bool have_separate_arg = false;
300 extra_args = 0;
302 opt_index = find_opt (argv[0] + 1, lang_mask);
303 i = 0;
304 while (opt_index == OPT_SPECIAL_unknown
305 && i < ARRAY_SIZE (option_map))
307 const char *opt0 = option_map[i].opt0;
308 const char *opt1 = option_map[i].opt1;
309 const char *new_prefix = option_map[i].new_prefix;
310 bool another_char_needed = option_map[i].another_char_needed;
311 size_t opt0_len = strlen (opt0);
312 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
313 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
314 size_t new_prefix_len = strlen (new_prefix);
316 extra_args = (opt1 == NULL ? 0 : 1);
317 value = !option_map[i].negated;
319 if (strncmp (argv[0], opt0, opt0_len) == 0
320 && (opt1 == NULL
321 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
322 && (!another_char_needed
323 || argv[extra_args][optn_len] != 0))
325 size_t arglen = strlen (argv[extra_args]);
326 char *dup;
328 adjust_len = (int) optn_len - (int) new_prefix_len;
329 dup = XNEWVEC (char, arglen + 1 - adjust_len);
330 memcpy (dup, new_prefix, new_prefix_len);
331 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
332 arglen - optn_len + 1);
333 opt_index = find_opt (dup + 1, lang_mask);
334 free (dup);
336 i++;
339 if (opt_index == OPT_SPECIAL_unknown)
341 arg = argv[0];
342 extra_args = 0;
343 value = 1;
344 goto done;
347 option = &cl_options[opt_index];
349 /* Reject negative form of switches that don't take negatives as
350 unrecognized. */
351 if (!value && (option->flags & CL_REJECT_NEGATIVE))
353 opt_index = OPT_SPECIAL_unknown;
354 errors |= CL_ERR_NEGATIVE;
355 arg = argv[0];
356 goto done;
359 result = extra_args + 1;
360 warn_message = option->warn_message;
362 /* Check to see if the option is disabled for this configuration. */
363 if (option->flags & CL_DISABLED)
364 errors |= CL_ERR_DISABLED;
366 /* Determine whether there may be a separate argument based on
367 whether this option is being processed for the driver. */
368 separate_arg_flag = ((option->flags & CL_SEPARATE)
369 && !((option->flags & CL_NO_DRIVER_ARG)
370 && (lang_mask & CL_DRIVER)));
371 joined_arg_flag = (option->flags & CL_JOINED) != 0;
373 /* Sort out any argument the switch takes. */
374 if (joined_arg_flag)
376 /* Have arg point to the original switch. This is because
377 some code, such as disable_builtin_function, expects its
378 argument to be persistent until the program exits. */
379 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
381 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
383 if (separate_arg_flag)
385 arg = argv[extra_args + 1];
386 result = extra_args + 2;
387 if (arg == NULL)
388 result = extra_args + 1;
389 else
390 have_separate_arg = true;
392 else
393 /* Missing argument. */
394 arg = NULL;
397 else if (separate_arg_flag)
399 arg = argv[extra_args + 1];
400 result = extra_args + 2;
401 if (arg == NULL)
402 result = extra_args + 1;
403 else
404 have_separate_arg = true;
407 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
408 errors |= CL_ERR_MISSING_ARG;
410 /* Is this option an alias (or an ignored option, marked as an alias
411 of OPT_SPECIAL_ignore)? */
412 if (option->alias_target != N_OPTS
413 && (!(option->flags & CL_SEPARATE_ALIAS) || have_separate_arg))
415 size_t new_opt_index = option->alias_target;
417 if (new_opt_index == OPT_SPECIAL_ignore)
419 gcc_assert (option->alias_arg == NULL);
420 gcc_assert (option->neg_alias_arg == NULL);
421 opt_index = new_opt_index;
422 arg = NULL;
423 value = 1;
425 else
427 const struct cl_option *new_option = &cl_options[new_opt_index];
429 /* The new option must not be an alias itself. */
430 gcc_assert (new_option->alias_target == N_OPTS
431 || (new_option->flags & CL_SEPARATE_ALIAS));
433 if (option->neg_alias_arg)
435 gcc_assert (option->alias_arg != NULL);
436 gcc_assert (arg == NULL);
437 if (value)
438 arg = option->alias_arg;
439 else
440 arg = option->neg_alias_arg;
441 value = 1;
443 else if (option->alias_arg)
445 gcc_assert (value == 1);
446 gcc_assert (arg == NULL);
447 arg = option->alias_arg;
450 opt_index = new_opt_index;
451 option = new_option;
453 if (value == 0)
454 gcc_assert (!(option->flags & CL_REJECT_NEGATIVE));
456 /* Recompute what arguments are allowed. */
457 separate_arg_flag = ((option->flags & CL_SEPARATE)
458 && !((option->flags & CL_NO_DRIVER_ARG)
459 && (lang_mask & CL_DRIVER)));
460 joined_arg_flag = (option->flags & CL_JOINED) != 0;
462 if (!(errors & CL_ERR_MISSING_ARG))
464 if (separate_arg_flag || joined_arg_flag)
466 if ((option->flags & CL_MISSING_OK) && arg == NULL)
467 arg = "";
468 gcc_assert (arg != NULL);
470 else
471 gcc_assert (arg == NULL);
474 /* Recheck for warnings and disabled options. */
475 if (option->warn_message)
477 gcc_assert (warn_message == NULL);
478 warn_message = option->warn_message;
480 if (option->flags & CL_DISABLED)
481 errors |= CL_ERR_DISABLED;
485 /* Check if this is a switch for a different front end. */
486 if (!option_ok_for_language (option, lang_mask))
487 errors |= CL_ERR_WRONG_LANG;
489 /* If the switch takes an integer, convert it. */
490 if (arg && (option->flags & CL_UINTEGER))
492 value = integral_argument (arg);
493 if (value == -1)
494 errors |= CL_ERR_UINT_ARG;
497 done:
498 decoded->opt_index = opt_index;
499 decoded->arg = arg;
500 decoded->value = value;
501 decoded->errors = errors;
502 decoded->warn_message = warn_message;
504 if (opt_index == OPT_SPECIAL_unknown)
506 /* Skip the correct number of arguments for options handled
507 through specs. */
508 const char *popt = argv[0] + 1;
509 int c = *popt;
511 gcc_assert (result == 1);
512 if (SWITCH_TAKES_ARG (c) > (popt[1] != 0))
513 result += SWITCH_TAKES_ARG (c) - (popt[1] != 0);
514 else if (WORD_SWITCH_TAKES_ARG (popt))
515 result += WORD_SWITCH_TAKES_ARG (popt);
516 if (result > 1)
517 for (i = 1; i < result; i++)
518 if (argv[i] == NULL)
520 result = i;
521 break;
525 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
526 decoded->canonical_option_num_elements = result;
527 total_len = 0;
528 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
530 if (i < result)
532 if (opt_index == OPT_SPECIAL_unknown)
533 decoded->canonical_option[i] = argv[i];
534 else
535 decoded->canonical_option[i] = NULL;
536 total_len += strlen (argv[i]) + 1;
538 else
539 decoded->canonical_option[i] = NULL;
541 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore)
542 generate_canonical_option (opt_index, arg, value, decoded);
543 decoded->orig_option_with_args_text = p = XNEWVEC (char, total_len);
544 for (i = 0; i < result; i++)
546 size_t len = strlen (argv[i]);
548 memcpy (p, argv[i], len);
549 p += len;
550 if (i == result - 1)
551 *p++ = 0;
552 else
553 *p++ = ' ';
556 return result;
559 #ifdef TARGET_OPTION_TRANSLATE_TABLE
560 static const struct {
561 const char *const option_found;
562 const char *const replacements;
563 } target_option_translations[] =
565 TARGET_OPTION_TRANSLATE_TABLE,
566 { 0, 0 }
568 #endif
570 /* Decode command-line options (ARGC and ARGV being the arguments of
571 main) into an array, setting *DECODED_OPTIONS to a pointer to that
572 array and *DECODED_OPTIONS_COUNT to the number of entries in the
573 array. The first entry in the array is always one for the program
574 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
575 flags applicable for decoding (including CL_COMMON and CL_TARGET if
576 those options should be considered applicable). Do not produce any
577 diagnostics or set state outside of these variables. */
579 void
580 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
581 unsigned int lang_mask,
582 struct cl_decoded_option **decoded_options,
583 unsigned int *decoded_options_count)
585 unsigned int n, i, target_translate_from;
586 struct cl_decoded_option *opt_array;
587 unsigned int num_decoded_options;
588 bool argv_copied = false;
590 opt_array = XNEWVEC (struct cl_decoded_option, argc);
592 opt_array[0].opt_index = OPT_SPECIAL_program_name;
593 opt_array[0].warn_message = NULL;
594 opt_array[0].arg = argv[0];
595 opt_array[0].orig_option_with_args_text = argv[0];
596 opt_array[0].canonical_option_num_elements = 1;
597 opt_array[0].canonical_option[0] = argv[0];
598 opt_array[0].canonical_option[1] = NULL;
599 opt_array[0].canonical_option[2] = NULL;
600 opt_array[0].canonical_option[3] = NULL;
601 opt_array[0].value = 1;
602 opt_array[0].errors = 0;
603 num_decoded_options = 1;
605 target_translate_from = 1;
606 for (i = 1; i < argc; i += n)
608 const char *opt = argv[i];
610 /* Interpret "-" or a non-switch as a file name. */
611 if (opt[0] != '-' || opt[1] == '\0')
613 generate_option_input_file (opt, &opt_array[num_decoded_options]);
614 num_decoded_options++;
615 n = 1;
616 continue;
619 if (i >= target_translate_from && (lang_mask & CL_DRIVER))
621 #ifdef TARGET_OPTION_TRANSLATE_TABLE
622 int tott_idx;
624 for (tott_idx = 0;
625 target_option_translations[tott_idx].option_found;
626 tott_idx++)
628 if (strcmp (target_option_translations[tott_idx].option_found,
629 argv[i]) == 0)
631 unsigned int spaces = 0;
632 unsigned int m = 0;
633 const char *sp;
634 char *np;
636 for (sp = target_option_translations[tott_idx].replacements;
637 *sp; sp++)
639 if (*sp == ' ')
641 spaces++;
642 while (*sp == ' ')
643 sp++;
644 sp--;
648 if (spaces)
650 int new_argc = argc + spaces;
651 if (argv_copied)
652 argv = XRESIZEVEC (const char *, argv, new_argc + 1);
653 else
655 const char **new_argv = XNEWVEC (const char *,
656 new_argc + 1);
657 memcpy (new_argv, argv,
658 (argc + 1) * sizeof (const char *));
659 argv = new_argv;
660 argv_copied = true;
662 memmove (&argv[i] + spaces, &argv[i],
663 (argc + 1 - i) * sizeof (const char *));
664 argc = new_argc;
665 opt_array = XRESIZEVEC (struct cl_decoded_option,
666 opt_array, argc);
669 sp = target_option_translations[tott_idx].replacements;
670 np = xstrdup (sp);
672 while (1)
674 while (*np == ' ')
675 np++;
676 if (*np == 0)
677 break;
678 argv[i + m++] = np;
679 while (*np != ' ' && *np)
680 np++;
681 if (*np == 0)
682 break;
683 *np++ = 0;
686 target_translate_from = i + m;
687 gcc_assert (m == spaces + 1);
688 break;
691 #endif
694 n = decode_cmdline_option (argv + i, lang_mask,
695 &opt_array[num_decoded_options]);
696 num_decoded_options++;
699 if (argv_copied)
700 free (argv);
701 opt_array = XRESIZEVEC (struct cl_decoded_option, opt_array,
702 num_decoded_options);
703 *decoded_options = opt_array;
704 *decoded_options_count = num_decoded_options;
707 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
708 next one is the same as ORIG_NEXT_OPT_IDX. */
710 static bool
711 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
713 /* An option can be canceled by the same option or an option with
714 Negative. */
715 if (cl_options [next_opt_idx].neg_index == opt_idx)
716 return true;
718 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
719 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
720 orig_next_opt_idx);
722 return false;
725 /* Filter out options canceled by the ones after them. */
727 void
728 prune_options (int *argcp, char ***argvp)
730 int argc = *argcp;
731 int *options = XNEWVEC (int, argc);
732 /* We will only return this replacement argv if we remove at least
733 one argument, so it does not need to be size (argc + 1) to
734 make room for the terminating NULL because we will always have
735 freed up at least one slot when we end up using it at all. */
736 char **argv = XNEWVEC (char *, argc);
737 int i, arg_count, need_prune = 0;
738 const struct cl_option *option;
739 size_t opt_index;
741 /* Scan all arguments. */
742 for (i = 1; i < argc; i++)
744 int value = 1;
745 const char *opt = (*argvp) [i];
747 opt_index = find_opt (opt + 1, -1);
748 if (opt_index == OPT_SPECIAL_unknown
749 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
750 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
752 char *dup;
754 /* Drop the "no-" from negative switches. */
755 size_t len = strlen (opt) - 3;
757 dup = XNEWVEC (char, len + 1);
758 dup[0] = '-';
759 dup[1] = opt[1];
760 memcpy (dup + 2, opt + 5, len - 2 + 1);
761 opt = dup;
762 value = 0;
763 opt_index = find_opt (opt + 1, -1);
764 free (dup);
767 if (opt_index == OPT_SPECIAL_unknown)
769 cont:
770 options [i] = 0;
771 continue;
774 option = &cl_options[opt_index];
775 if (option->neg_index < 0)
776 goto cont;
778 /* Skip joined switches. */
779 if ((option->flags & CL_JOINED))
780 goto cont;
782 /* Reject negative form of switches that don't take negatives as
783 unrecognized. */
784 if (!value && (option->flags & CL_REJECT_NEGATIVE))
785 goto cont;
787 options [i] = (int) opt_index;
788 need_prune |= options [i];
791 if (!need_prune)
792 goto done;
794 /* Remove arguments which are negated by others after them. */
795 argv [0] = (*argvp) [0];
796 arg_count = 1;
797 for (i = 1; i < argc; i++)
799 int j, opt_idx;
801 opt_idx = options [i];
802 if (opt_idx)
804 int next_opt_idx;
805 for (j = i + 1; j < argc; j++)
807 next_opt_idx = options [j];
808 if (next_opt_idx
809 && cancel_option (opt_idx, next_opt_idx,
810 next_opt_idx))
811 break;
814 else
815 goto keep;
817 if (j == argc)
819 keep:
820 argv [arg_count] = (*argvp) [i];
821 arg_count++;
825 if (arg_count != argc)
827 *argcp = arg_count;
828 *argvp = argv;
829 /* Add NULL-termination. Guaranteed not to overflow because
830 arg_count here can only be less than argc. */
831 argv[arg_count] = 0;
833 else
835 done:
836 free (argv);
839 free (options);
842 /* Handle option DECODED for the language indicated by LANG_MASK,
843 using the handlers in HANDLERS. KIND is the diagnostic_t if this
844 is a diagnostics option, DK_UNSPECIFIED otherwise. Returns false
845 if the switch was invalid. */
847 bool
848 handle_option (const struct cl_decoded_option *decoded,
849 unsigned int lang_mask, int kind,
850 const struct cl_option_handlers *handlers)
852 size_t opt_index = decoded->opt_index;
853 const char *arg = decoded->arg;
854 int value = decoded->value;
855 const struct cl_option *option = &cl_options[opt_index];
856 size_t i;
858 if (option->flag_var)
859 set_option (opt_index, value, arg, kind);
861 for (i = 0; i < handlers->num_handlers; i++)
862 if (option->flags & handlers->handlers[i].mask)
864 if (!handlers->handlers[i].handler (decoded,
865 lang_mask, kind, handlers))
866 return false;
867 else
868 handlers->post_handling_callback (decoded,
869 handlers->handlers[i].mask);
872 return true;
875 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
876 option instead of DECODED. This is used for callbacks when one
877 option implies another instead of an option being decoded from the
878 command line. */
880 bool
881 handle_generated_option (size_t opt_index, const char *arg, int value,
882 unsigned int lang_mask, int kind,
883 const struct cl_option_handlers *handlers)
885 struct cl_decoded_option decoded;
887 generate_option (opt_index, arg, value, lang_mask, &decoded);
888 return handle_option (&decoded, lang_mask, kind, handlers);
891 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
892 VALUE for a front end using LANG_MASK. This is used when the
893 compiler generates options internally. */
895 void
896 generate_option (size_t opt_index, const char *arg, int value,
897 unsigned int lang_mask, struct cl_decoded_option *decoded)
899 const struct cl_option *option = &cl_options[opt_index];
901 decoded->opt_index = opt_index;
902 decoded->warn_message = NULL;
903 decoded->arg = arg;
904 decoded->value = value;
905 decoded->errors = (option_ok_for_language (option, lang_mask)
907 : CL_ERR_WRONG_LANG);
909 generate_canonical_option (opt_index, arg, value, decoded);
910 switch (decoded->canonical_option_num_elements)
912 case 1:
913 decoded->orig_option_with_args_text = decoded->canonical_option[0];
914 break;
916 case 2:
917 decoded->orig_option_with_args_text
918 = concat (decoded->canonical_option[0], " ",
919 decoded->canonical_option[1], NULL);
920 break;
922 default:
923 gcc_unreachable ();
927 /* Fill in *DECODED with an option for input file FILE. */
929 void
930 generate_option_input_file (const char *file,
931 struct cl_decoded_option *decoded)
933 decoded->opt_index = OPT_SPECIAL_input_file;
934 decoded->warn_message = NULL;
935 decoded->arg = file;
936 decoded->orig_option_with_args_text = file;
937 decoded->canonical_option_num_elements = 1;
938 decoded->canonical_option[0] = file;
939 decoded->canonical_option[1] = NULL;
940 decoded->canonical_option[2] = NULL;
941 decoded->canonical_option[3] = NULL;
942 decoded->value = 1;
943 decoded->errors = 0;
946 /* Handle the switch DECODED for the language indicated by LANG_MASK,
947 using the handlers in *HANDLERS. */
949 void
950 read_cmdline_option (struct cl_decoded_option *decoded,
951 unsigned int lang_mask,
952 const struct cl_option_handlers *handlers)
954 const struct cl_option *option;
955 const char *opt = decoded->orig_option_with_args_text;
957 if (decoded->warn_message)
958 warning (0, decoded->warn_message, opt);
960 if (decoded->opt_index == OPT_SPECIAL_unknown)
962 if (handlers->unknown_option_callback (decoded))
963 error ("unrecognized command line option %qs", decoded->arg);
964 return;
967 if (decoded->opt_index == OPT_SPECIAL_ignore)
968 return;
970 option = &cl_options[decoded->opt_index];
972 if (decoded->errors & CL_ERR_DISABLED)
974 error ("command line option %qs"
975 " is not supported by this configuration", opt);
976 return;
979 if (decoded->errors & CL_ERR_WRONG_LANG)
981 handlers->wrong_lang_callback (decoded, lang_mask);
982 return;
985 if (decoded->errors & CL_ERR_MISSING_ARG)
987 if (option->missing_argument_error)
988 error (option->missing_argument_error, opt);
989 else
990 error ("missing argument to %qs", opt);
991 return;
994 if (decoded->errors & CL_ERR_UINT_ARG)
996 error ("argument to %qs should be a non-negative integer",
997 option->opt_text);
998 return;
1001 gcc_assert (!decoded->errors);
1003 if (!handle_option (decoded, lang_mask, DK_UNSPECIFIED, handlers))
1004 error ("unrecognized command line option %qs", opt);
1007 /* Set any variable for option OPT_INDEX according to VALUE and ARG,
1008 diagnostic kind KIND. */
1010 void
1011 set_option (int opt_index, int value, const char *arg, int kind)
1013 const struct cl_option *option = &cl_options[opt_index];
1015 if (!option->flag_var)
1016 return;
1018 switch (option->var_type)
1020 case CLVC_BOOLEAN:
1021 *(int *) option->flag_var = value;
1022 break;
1024 case CLVC_EQUAL:
1025 *(int *) option->flag_var = (value
1026 ? option->var_value
1027 : !option->var_value);
1028 break;
1030 case CLVC_BIT_CLEAR:
1031 case CLVC_BIT_SET:
1032 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1033 *(int *) option->flag_var |= option->var_value;
1034 else
1035 *(int *) option->flag_var &= ~option->var_value;
1036 if (option->flag_var == &target_flags)
1037 target_flags_explicit |= option->var_value;
1038 break;
1040 case CLVC_STRING:
1041 *(const char **) option->flag_var = arg;
1042 break;
1045 if ((diagnostic_t) kind != DK_UNSPECIFIED)
1046 diagnostic_classify_diagnostic (global_dc, opt_index, (diagnostic_t) kind,
1047 UNKNOWN_LOCATION);