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