1 /* Command line option handling.
2 Copyright (C) 2006-2019 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "intl.h"
23 #include "coretypes.h"
24 #include "opts.h"
25 #include "options.h"
26 #include "diagnostic.h"
27 #include "spellcheck.h"
28
29 static void prune_options (struct cl_decoded_option **, unsigned int *);
30
31 /* An option that is undocumented, that takes a joined argument, and
32 that doesn't fit any of the classes of uses (language/common,
33 driver, target) is assumed to be a prefix used to catch
34 e.g. negated options, and stop them from being further shortened to
35 a prefix that could use the negated option as an argument. For
36 example, we want -gno-statement-frontiers to be taken as a negation
37 of -gstatement-frontiers, but without catching the gno- prefix and
38 signaling it's to be used for option remapping, it would end up
39 backtracked to g with no-statemnet-frontiers as the debug level. */
40
41 static bool
remapping_prefix_p(const struct cl_option * opt)42 remapping_prefix_p (const struct cl_option *opt)
43 {
44 return opt->flags & CL_UNDOCUMENTED
45 && opt->flags & CL_JOINED
46 && !(opt->flags & (CL_DRIVER | CL_TARGET | CL_COMMON | CL_LANG_ALL));
47 }
48
49 /* Perform a binary search to find which option the command-line INPUT
50 matches. Returns its index in the option array, and
51 OPT_SPECIAL_unknown on failure.
52
53 This routine is quite subtle. A normal binary search is not good
54 enough because some options can be suffixed with an argument, and
55 multiple sub-matches can occur, e.g. input of "-pedantic" matching
56 the initial substring of "-pedantic-errors".
57
58 A more complicated example is -gstabs. It should match "-g" with
59 an argument of "stabs". Suppose, however, that the number and list
60 of switches are such that the binary search tests "-gen-decls"
61 before having tested "-g". This doesn't match, and as "-gen-decls"
62 is less than "-gstabs", it will become the lower bound of the
63 binary search range, and "-g" will never be seen. To resolve this
64 issue, 'optc-gen.awk' makes "-gen-decls" point, via the back_chain member,
65 to "-g" so that failed searches that end between "-gen-decls" and
66 the lexicographically subsequent switch know to go back and see if
67 "-g" causes a match (which it does in this example).
68
69 This search is done in such a way that the longest match for the
70 front end in question wins. If there is no match for the current
71 front end, the longest match for a different front end is returned
72 (or N_OPTS if none) and the caller emits an error message. */
73 size_t
find_opt(const char * input,unsigned int lang_mask)74 find_opt (const char *input, unsigned int lang_mask)
75 {
76 size_t mn, mn_orig, mx, md, opt_len;
77 size_t match_wrong_lang;
78 int comp;
79
80 mn = 0;
81 mx = cl_options_count;
82
83 /* Find mn such this lexicographical inequality holds:
84 cl_options[mn] <= input < cl_options[mn + 1]. */
85 while (mx - mn > 1)
86 {
87 md = (mn + mx) / 2;
88 opt_len = cl_options[md].opt_len;
89 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
90
91 if (comp < 0)
92 mx = md;
93 else
94 mn = md;
95 }
96
97 mn_orig = mn;
98
99 /* This is the switch that is the best match but for a different
100 front end, or OPT_SPECIAL_unknown if there is no match at all. */
101 match_wrong_lang = OPT_SPECIAL_unknown;
102
103 /* Backtrace the chain of possible matches, returning the longest
104 one, if any, that fits best. With current GCC switches, this
105 loop executes at most twice. */
106 do
107 {
108 const struct cl_option *opt = &cl_options[mn];
109
110 /* Is the input either an exact match or a prefix that takes a
111 joined argument? */
112 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
113 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
114 {
115 /* If language is OK, return it. */
116 if (opt->flags & lang_mask)
117 return mn;
118
119 if (remapping_prefix_p (opt))
120 return OPT_SPECIAL_unknown;
121
122 /* If we haven't remembered a prior match, remember this
123 one. Any prior match is necessarily better. */
124 if (match_wrong_lang == OPT_SPECIAL_unknown)
125 match_wrong_lang = mn;
126 }
127
128 /* Try the next possibility. This is cl_options_count if there
129 are no more. */
130 mn = opt->back_chain;
131 }
132 while (mn != cl_options_count);
133
134 if (match_wrong_lang == OPT_SPECIAL_unknown && input[0] == '-')
135 {
136 /* Long options, starting "--", may be abbreviated if the
137 abbreviation is unambiguous. This only applies to options
138 not taking a joined argument, and abbreviations of "--option"
139 are permitted even if there is a variant "--option=". */
140 size_t mnc = mn_orig + 1;
141 size_t cmp_len = strlen (input);
142 while (mnc < cl_options_count
143 && strncmp (input, cl_options[mnc].opt_text + 1, cmp_len) == 0)
144 {
145 /* Option matching this abbreviation. OK if it is the first
146 match and that does not take a joined argument, or the
147 second match, taking a joined argument and with only '='
148 added to the first match; otherwise considered
149 ambiguous. */
150 if (mnc == mn_orig + 1
151 && !(cl_options[mnc].flags & CL_JOINED))
152 match_wrong_lang = mnc;
153 else if (mnc == mn_orig + 2
154 && match_wrong_lang == mn_orig + 1
155 && (cl_options[mnc].flags & CL_JOINED)
156 && (cl_options[mnc].opt_len
157 == cl_options[mn_orig + 1].opt_len + 1)
158 && strncmp (cl_options[mnc].opt_text + 1,
159 cl_options[mn_orig + 1].opt_text + 1,
160 cl_options[mn_orig + 1].opt_len) == 0)
161 ; /* OK, as long as there are no more matches. */
162 else
163 return OPT_SPECIAL_unknown;
164 mnc++;
165 }
166 }
167
168 /* Return the best wrong match, or OPT_SPECIAL_unknown if none. */
169 return match_wrong_lang;
170 }
171
172 /* If ARG is a non-negative decimal or hexadecimal integer representable
173 in HOST_WIDE_INT return its value, otherwise return -1. If ERR is not
174 null set *ERR to zero on success or to EINVAL or to the value of errno
175 otherwise. */
176
177 HOST_WIDE_INT
integral_argument(const char * arg,int * err,bool byte_size_suffix)178 integral_argument (const char *arg, int *err, bool byte_size_suffix)
179 {
180 if (!err)
181 err = &errno;
182
183 if (!ISDIGIT (*arg))
184 {
185 *err = EINVAL;
186 return -1;
187 }
188
189 *err = 0;
190 errno = 0;
191
192 char *end = NULL;
193 unsigned HOST_WIDE_INT unit = 1;
194 unsigned HOST_WIDE_INT value = strtoull (arg, &end, 10);
195
196 /* If the value is too large to be represented use the maximum
197 representable value that strtoull sets VALUE to (setting
198 errno to ERANGE). */
199
200 if (end && *end)
201 {
202 if (!byte_size_suffix)
203 {
204 errno = 0;
205 value = strtoull (arg, &end, 0);
206 if (*end)
207 {
208 if (errno)
209 *err = errno;
210 else
211 *err = EINVAL;
212 return -1;
213 }
214
215 return value;
216 }
217
218 /* Numeric option arguments are at most INT_MAX. Make it
219 possible to specify a larger value by accepting common
220 suffixes. */
221 if (!strcmp (end, "kB"))
222 unit = 1000;
223 else if (!strcasecmp (end, "KiB") || !strcmp (end, "KB"))
224 unit = 1024;
225 else if (!strcmp (end, "MB"))
226 unit = HOST_WIDE_INT_UC (1000) * 1000;
227 else if (!strcasecmp (end, "MiB"))
228 unit = HOST_WIDE_INT_UC (1024) * 1024;
229 else if (!strcasecmp (end, "GB"))
230 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000;
231 else if (!strcasecmp (end, "GiB"))
232 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024;
233 else if (!strcasecmp (end, "TB"))
234 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000;
235 else if (!strcasecmp (end, "TiB"))
236 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024;
237 else if (!strcasecmp (end, "PB"))
238 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000;
239 else if (!strcasecmp (end, "PiB"))
240 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024;
241 else if (!strcasecmp (end, "EB"))
242 unit = HOST_WIDE_INT_UC (1000) * 1000 * 1000 * 1000 * 1000
243 * 1000;
244 else if (!strcasecmp (end, "EiB"))
245 unit = HOST_WIDE_INT_UC (1024) * 1024 * 1024 * 1024 * 1024
246 * 1024;
247 else
248 {
249 /* This could mean an unknown suffix or a bad prefix, like
250 "+-1". */
251 *err = EINVAL;
252 return -1;
253 }
254 }
255
256 if (unit)
257 {
258 unsigned HOST_WIDE_INT prod = value * unit;
259 value = prod < value ? HOST_WIDE_INT_M1U : prod;
260 }
261
262 return value;
263 }
264
265 /* Return whether OPTION is OK for the language given by
266 LANG_MASK. */
267 static bool
option_ok_for_language(const struct cl_option * option,unsigned int lang_mask)268 option_ok_for_language (const struct cl_option *option,
269 unsigned int lang_mask)
270 {
271 if (!(option->flags & lang_mask))
272 return false;
273 else if ((option->flags & CL_TARGET)
274 && (option->flags & (CL_LANG_ALL | CL_DRIVER))
275 && !(option->flags & (lang_mask & ~CL_COMMON & ~CL_TARGET)))
276 /* Complain for target flag language mismatches if any languages
277 are specified. */
278 return false;
279 return true;
280 }
281
282 /* Return whether ENUM_ARG is OK for the language given by
283 LANG_MASK. */
284
285 static bool
enum_arg_ok_for_language(const struct cl_enum_arg * enum_arg,unsigned int lang_mask)286 enum_arg_ok_for_language (const struct cl_enum_arg *enum_arg,
287 unsigned int lang_mask)
288 {
289 return (lang_mask & CL_DRIVER) || !(enum_arg->flags & CL_ENUM_DRIVER_ONLY);
290 }
291
292 /* Look up ARG in ENUM_ARGS for language LANG_MASK, returning true and
293 storing the value in *VALUE if found, and returning false without
294 modifying *VALUE if not found. */
295
296 static bool
enum_arg_to_value(const struct cl_enum_arg * enum_args,const char * arg,HOST_WIDE_INT * value,unsigned int lang_mask)297 enum_arg_to_value (const struct cl_enum_arg *enum_args,
298 const char *arg, HOST_WIDE_INT *value,
299 unsigned int lang_mask)
300 {
301 unsigned int i;
302
303 for (i = 0; enum_args[i].arg != NULL; i++)
304 if (strcmp (arg, enum_args[i].arg) == 0
305 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
306 {
307 *value = enum_args[i].value;
308 return true;
309 }
310
311 return false;
312 }
313
314 /* Look up ARG in the enum used by option OPT_INDEX for language
315 LANG_MASK, returning true and storing the value in *VALUE if found,
316 and returning false without modifying *VALUE if not found. */
317
318 bool
opt_enum_arg_to_value(size_t opt_index,const char * arg,int * value,unsigned int lang_mask)319 opt_enum_arg_to_value (size_t opt_index, const char *arg,
320 int *value, unsigned int lang_mask)
321 {
322 const struct cl_option *option = &cl_options[opt_index];
323
324 gcc_assert (option->var_type == CLVC_ENUM);
325
326 HOST_WIDE_INT wideval;
327 if (enum_arg_to_value (cl_enums[option->var_enum].values, arg,
328 &wideval, lang_mask))
329 {
330 *value = wideval;
331 return true;
332 }
333
334 return false;
335 }
336
337 /* Look of VALUE in ENUM_ARGS for language LANG_MASK and store the
338 corresponding string in *ARGP, returning true if the found string
339 was marked as canonical, false otherwise. If VALUE is not found
340 (which may be the case for uninitialized values if the relevant
341 option has not been passed), set *ARGP to NULL and return
342 false. */
343
344 bool
enum_value_to_arg(const struct cl_enum_arg * enum_args,const char ** argp,int value,unsigned int lang_mask)345 enum_value_to_arg (const struct cl_enum_arg *enum_args,
346 const char **argp, int value, unsigned int lang_mask)
347 {
348 unsigned int i;
349
350 for (i = 0; enum_args[i].arg != NULL; i++)
351 if (enum_args[i].value == value
352 && (enum_args[i].flags & CL_ENUM_CANONICAL)
353 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
354 {
355 *argp = enum_args[i].arg;
356 return true;
357 }
358
359 for (i = 0; enum_args[i].arg != NULL; i++)
360 if (enum_args[i].value == value
361 && enum_arg_ok_for_language (&enum_args[i], lang_mask))
362 {
363 *argp = enum_args[i].arg;
364 return false;
365 }
366
367 *argp = NULL;
368 return false;
369 }
370
371 /* Fill in the canonical option part of *DECODED with an option
372 described by OPT_INDEX, ARG and VALUE. */
373
374 static void
generate_canonical_option(size_t opt_index,const char * arg,HOST_WIDE_INT value,struct cl_decoded_option * decoded)375 generate_canonical_option (size_t opt_index, const char *arg,
376 HOST_WIDE_INT value,
377 struct cl_decoded_option *decoded)
378 {
379 const struct cl_option *option = &cl_options[opt_index];
380 const char *opt_text = option->opt_text;
381
382 if (value == 0
383 && !option->cl_reject_negative
384 && (opt_text[1] == 'W' || opt_text[1] == 'f'
385 || opt_text[1] == 'g' || opt_text[1] == 'm'))
386 {
387 char *t = XOBNEWVEC (&opts_obstack, char, option->opt_len + 5);
388 t[0] = '-';
389 t[1] = opt_text[1];
390 t[2] = 'n';
391 t[3] = 'o';
392 t[4] = '-';
393 memcpy (t + 5, opt_text + 2, option->opt_len);
394 opt_text = t;
395 }
396
397 decoded->canonical_option[2] = NULL;
398 decoded->canonical_option[3] = NULL;
399
400 if (arg)
401 {
402 if ((option->flags & CL_SEPARATE)
403 && !option->cl_separate_alias)
404 {
405 decoded->canonical_option[0] = opt_text;
406 decoded->canonical_option[1] = arg;
407 decoded->canonical_option_num_elements = 2;
408 }
409 else
410 {
411 gcc_assert (option->flags & CL_JOINED);
412 decoded->canonical_option[0] = opts_concat (opt_text, arg, NULL);
413 decoded->canonical_option[1] = NULL;
414 decoded->canonical_option_num_elements = 1;
415 }
416 }
417 else
418 {
419 decoded->canonical_option[0] = opt_text;
420 decoded->canonical_option[1] = NULL;
421 decoded->canonical_option_num_elements = 1;
422 }
423 }
424
425 /* Structure describing mappings from options on the command line to
426 options to look up with find_opt. */
427 struct option_map
428 {
429 /* Prefix of the option on the command line. */
430 const char *opt0;
431 /* If two argv elements are considered to be merged into one option,
432 prefix for the second element, otherwise NULL. */
433 const char *opt1;
434 /* The new prefix to map to. */
435 const char *new_prefix;
436 /* Whether at least one character is needed following opt1 or opt0
437 for this mapping to be used. (--optimize= is valid for -O, but
438 --warn- is not valid for -W.) */
439 bool another_char_needed;
440 /* Whether the original option is a negated form of the option
441 resulting from this map. */
442 bool negated;
443 };
444 static const struct option_map option_map[] =
445 {
446 { "-Wno-", NULL, "-W", false, true },
447 { "-fno-", NULL, "-f", false, true },
448 { "-gno-", NULL, "-g", false, true },
449 { "-mno-", NULL, "-m", false, true },
450 { "--debug=", NULL, "-g", false, false },
451 { "--machine-", NULL, "-m", true, false },
452 { "--machine-no-", NULL, "-m", false, true },
453 { "--machine=", NULL, "-m", false, false },
454 { "--machine=no-", NULL, "-m", false, true },
455 { "--machine", "", "-m", false, false },
456 { "--machine", "no-", "-m", false, true },
457 { "--optimize=", NULL, "-O", false, false },
458 { "--std=", NULL, "-std=", false, false },
459 { "--std", "", "-std=", false, false },
460 { "--warn-", NULL, "-W", true, false },
461 { "--warn-no-", NULL, "-W", false, true },
462 { "--", NULL, "-f", true, false },
463 { "--no-", NULL, "-f", false, true }
464 };
465
466 /* Helper function for gcc.c's driver::suggest_option, for populating the
467 vec of suggestions for misspelled options.
468
469 option_map above provides various prefixes for spelling command-line
470 options, which decode_cmdline_option uses to map spellings of options
471 to specific options. We want to do the reverse: to find all the ways
472 that a user could validly spell an option.
473
474 Given valid OPT_TEXT (with a leading dash) for OPTION, add it and all
475 of its valid variant spellings to CANDIDATES, each without a leading
476 dash.
477
478 For example, given "-Wabi-tag", the following are added to CANDIDATES:
479 "Wabi-tag"
480 "Wno-abi-tag"
481 "-warn-abi-tag"
482 "-warn-no-abi-tag".
483
484 The added strings must be freed using free. */
485
486 void
add_misspelling_candidates(auto_vec<char * > * candidates,const struct cl_option * option,const char * opt_text)487 add_misspelling_candidates (auto_vec<char *> *candidates,
488 const struct cl_option *option,
489 const char *opt_text)
490 {
491 gcc_assert (candidates);
492 gcc_assert (option);
493 gcc_assert (opt_text);
494 if (remapping_prefix_p (option))
495 return;
496 candidates->safe_push (xstrdup (opt_text + 1));
497 for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
498 {
499 const char *opt0 = option_map[i].opt0;
500 const char *new_prefix = option_map[i].new_prefix;
501 size_t new_prefix_len = strlen (new_prefix);
502
503 if (option->cl_reject_negative && option_map[i].negated)
504 continue;
505
506 if (strncmp (opt_text, new_prefix, new_prefix_len) == 0)
507 {
508 char *alternative = concat (opt0 + 1, opt_text + new_prefix_len,
509 NULL);
510 candidates->safe_push (alternative);
511 }
512 }
513 }
514
515 /* Decode the switch beginning at ARGV for the language indicated by
516 LANG_MASK (including CL_COMMON and CL_TARGET if applicable), into
517 the structure *DECODED. Returns the number of switches
518 consumed. */
519
520 static unsigned int
decode_cmdline_option(const char ** argv,unsigned int lang_mask,struct cl_decoded_option * decoded)521 decode_cmdline_option (const char **argv, unsigned int lang_mask,
522 struct cl_decoded_option *decoded)
523 {
524 size_t opt_index;
525 const char *arg = 0;
526 HOST_WIDE_INT value = 1;
527 unsigned int result = 1, i, extra_args, separate_args = 0;
528 int adjust_len = 0;
529 size_t total_len;
530 char *p;
531 const struct cl_option *option;
532 int errors = 0;
533 const char *warn_message = NULL;
534 bool separate_arg_flag;
535 bool joined_arg_flag;
536 bool have_separate_arg = false;
537
538 extra_args = 0;
539
540 const char *opt_value = argv[0] + 1;
541 opt_index = find_opt (opt_value, lang_mask);
542 i = 0;
543 while (opt_index == OPT_SPECIAL_unknown
544 && i < ARRAY_SIZE (option_map))
545 {
546 const char *opt0 = option_map[i].opt0;
547 const char *opt1 = option_map[i].opt1;
548 const char *new_prefix = option_map[i].new_prefix;
549 bool another_char_needed = option_map[i].another_char_needed;
550 size_t opt0_len = strlen (opt0);
551 size_t opt1_len = (opt1 == NULL ? 0 : strlen (opt1));
552 size_t optn_len = (opt1 == NULL ? opt0_len : opt1_len);
553 size_t new_prefix_len = strlen (new_prefix);
554
555 extra_args = (opt1 == NULL ? 0 : 1);
556 value = !option_map[i].negated;
557
558 if (strncmp (argv[0], opt0, opt0_len) == 0
559 && (opt1 == NULL
560 || (argv[1] != NULL && strncmp (argv[1], opt1, opt1_len) == 0))
561 && (!another_char_needed
562 || argv[extra_args][optn_len] != 0))
563 {
564 size_t arglen = strlen (argv[extra_args]);
565 char *dup;
566
567 adjust_len = (int) optn_len - (int) new_prefix_len;
568 dup = XNEWVEC (char, arglen + 1 - adjust_len);
569 memcpy (dup, new_prefix, new_prefix_len);
570 memcpy (dup + new_prefix_len, argv[extra_args] + optn_len,
571 arglen - optn_len + 1);
572 opt_index = find_opt (dup + 1, lang_mask);
573 free (dup);
574 }
575 i++;
576 }
577
578 if (opt_index == OPT_SPECIAL_unknown)
579 {
580 arg = argv[0];
581 extra_args = 0;
582 value = 1;
583 goto done;
584 }
585
586 option = &cl_options[opt_index];
587
588 /* Reject negative form of switches that don't take negatives as
589 unrecognized. */
590 if (!value && option->cl_reject_negative)
591 {
592 opt_index = OPT_SPECIAL_unknown;
593 errors |= CL_ERR_NEGATIVE;
594 arg = argv[0];
595 goto done;
596 }
597
598 /* Clear the initial value for size options (it will be overwritten
599 later based on the Init(value) specification in the opt file. */
600 if (option->var_type == CLVC_SIZE)
601 value = 0;
602
603 result = extra_args + 1;
604 warn_message = option->warn_message;
605
606 /* Check to see if the option is disabled for this configuration. */
607 if (option->cl_disabled)
608 errors |= CL_ERR_DISABLED;
609
610 /* Determine whether there may be a separate argument based on
611 whether this option is being processed for the driver, and, if
612 so, how many such arguments. */
613 separate_arg_flag = ((option->flags & CL_SEPARATE)
614 && !(option->cl_no_driver_arg
615 && (lang_mask & CL_DRIVER)));
616 separate_args = (separate_arg_flag
617 ? option->cl_separate_nargs + 1
618 : 0);
619 joined_arg_flag = (option->flags & CL_JOINED) != 0;
620
621 /* Sort out any argument the switch takes. */
622 if (joined_arg_flag)
623 {
624 /* Have arg point to the original switch. This is because
625 some code, such as disable_builtin_function, expects its
626 argument to be persistent until the program exits. */
627 arg = argv[extra_args] + cl_options[opt_index].opt_len + 1 + adjust_len;
628
629 if (*arg == '\0' && !option->cl_missing_ok)
630 {
631 if (separate_arg_flag)
632 {
633 arg = argv[extra_args + 1];
634 result = extra_args + 2;
635 if (arg == NULL)
636 result = extra_args + 1;
637 else
638 have_separate_arg = true;
639 }
640 else
641 /* Missing argument. */
642 arg = NULL;
643 }
644 }
645 else if (separate_arg_flag)
646 {
647 arg = argv[extra_args + 1];
648 for (i = 0; i < separate_args; i++)
649 if (argv[extra_args + 1 + i] == NULL)
650 {
651 errors |= CL_ERR_MISSING_ARG;
652 break;
653 }
654 result = extra_args + 1 + i;
655 if (arg != NULL)
656 have_separate_arg = true;
657 }
658
659 if (arg == NULL && (separate_arg_flag || joined_arg_flag))
660 errors |= CL_ERR_MISSING_ARG;
661
662 /* Is this option an alias (or an ignored option, marked as an alias
663 of OPT_SPECIAL_ignore)? */
664 if (option->alias_target != N_OPTS
665 && (!option->cl_separate_alias || have_separate_arg))
666 {
667 size_t new_opt_index = option->alias_target;
668
669 if (new_opt_index == OPT_SPECIAL_ignore
670 || new_opt_index == OPT_SPECIAL_deprecated)
671 {
672 gcc_assert (option->alias_arg == NULL);
673 gcc_assert (option->neg_alias_arg == NULL);
674 opt_index = new_opt_index;
675 arg = NULL;
676 }
677 else
678 {
679 const struct cl_option *new_option = &cl_options[new_opt_index];
680
681 /* The new option must not be an alias itself. */
682 gcc_assert (new_option->alias_target == N_OPTS
683 || new_option->cl_separate_alias);
684
685 if (option->neg_alias_arg)
686 {
687 gcc_assert (option->alias_arg != NULL);
688 gcc_assert (arg == NULL);
689 gcc_assert (!option->cl_negative_alias);
690 if (value)
691 arg = option->alias_arg;
692 else
693 arg = option->neg_alias_arg;
694 value = 1;
695 }
696 else if (option->alias_arg)
697 {
698 gcc_assert (value == 1);
699 gcc_assert (arg == NULL);
700 gcc_assert (!option->cl_negative_alias);
701 arg = option->alias_arg;
702 }
703
704 if (option->cl_negative_alias)
705 value = !value;
706
707 opt_index = new_opt_index;
708 option = new_option;
709
710 if (value == 0)
711 gcc_assert (!option->cl_reject_negative);
712
713 /* Recompute what arguments are allowed. */
714 separate_arg_flag = ((option->flags & CL_SEPARATE)
715 && !(option->cl_no_driver_arg
716 && (lang_mask & CL_DRIVER)));
717 joined_arg_flag = (option->flags & CL_JOINED) != 0;
718
719 if (separate_args > 1 || option->cl_separate_nargs)
720 gcc_assert (separate_args
721 == (unsigned int) option->cl_separate_nargs + 1);
722
723 if (!(errors & CL_ERR_MISSING_ARG))
724 {
725 if (separate_arg_flag || joined_arg_flag)
726 {
727 if (option->cl_missing_ok && arg == NULL)
728 arg = "";
729 gcc_assert (arg != NULL);
730 }
731 else
732 gcc_assert (arg == NULL);
733 }
734
735 /* Recheck for warnings and disabled options. */
736 if (option->warn_message)
737 {
738 gcc_assert (warn_message == NULL);
739 warn_message = option->warn_message;
740 }
741 if (option->cl_disabled)
742 errors |= CL_ERR_DISABLED;
743 }
744 }
745
746 /* Check if this is a switch for a different front end. */
747 if (!option_ok_for_language (option, lang_mask))
748 errors |= CL_ERR_WRONG_LANG;
749 else if (strcmp (option->opt_text, "-Werror=") == 0
750 && strchr (opt_value, ',') == NULL)
751 {
752 /* Verify that -Werror argument is a valid warning
753 for a language. */
754 char *werror_arg = xstrdup (opt_value + 6);
755 werror_arg[0] = 'W';
756
757 size_t warning_index = find_opt (werror_arg, lang_mask);
758 if (warning_index != OPT_SPECIAL_unknown)
759 {
760 const struct cl_option *warning_option
761 = &cl_options[warning_index];
762 if (!option_ok_for_language (warning_option, lang_mask))
763 errors |= CL_ERR_WRONG_LANG;
764 }
765 }
766
767 /* Convert the argument to lowercase if appropriate. */
768 if (arg && option->cl_tolower)
769 {
770 size_t j;
771 size_t len = strlen (arg);
772 char *arg_lower = XOBNEWVEC (&opts_obstack, char, len + 1);
773
774 for (j = 0; j < len; j++)
775 arg_lower[j] = TOLOWER ((unsigned char) arg[j]);
776 arg_lower[len] = 0;
777 arg = arg_lower;
778 }
779
780 /* If the switch takes an integer argument, convert it. */
781 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
782 {
783 int error = 0;
784 value = *arg ? integral_argument (arg, &error, option->cl_byte_size) : 0;
785 if (error)
786 errors |= CL_ERR_UINT_ARG;
787
788 /* Reject value out of a range. */
789 if (option->range_max != -1
790 && (value < option->range_min || value > option->range_max))
791 errors |= CL_ERR_INT_RANGE_ARG;
792 }
793
794 /* If the switch takes an enumerated argument, convert it. */
795 if (arg && (option->var_type == CLVC_ENUM))
796 {
797 const struct cl_enum *e = &cl_enums[option->var_enum];
798
799 gcc_assert (value == 1);
800 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
801 {
802 const char *carg = NULL;
803
804 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
805 arg = carg;
806 gcc_assert (carg != NULL);
807 }
808 else
809 errors |= CL_ERR_ENUM_ARG;
810 }
811
812 done:
813 decoded->opt_index = opt_index;
814 decoded->arg = arg;
815 decoded->value = value;
816 decoded->errors = errors;
817 decoded->warn_message = warn_message;
818
819 if (opt_index == OPT_SPECIAL_unknown)
820 gcc_assert (result == 1);
821
822 gcc_assert (result >= 1 && result <= ARRAY_SIZE (decoded->canonical_option));
823 decoded->canonical_option_num_elements = result;
824 total_len = 0;
825 for (i = 0; i < ARRAY_SIZE (decoded->canonical_option); i++)
826 {
827 if (i < result)
828 {
829 size_t len;
830 if (opt_index == OPT_SPECIAL_unknown)
831 decoded->canonical_option[i] = argv[i];
832 else
833 decoded->canonical_option[i] = NULL;
834 len = strlen (argv[i]);
835 /* If the argument is an empty string, we will print it as "" in
836 orig_option_with_args_text. */
837 total_len += (len != 0 ? len : 2) + 1;
838 }
839 else
840 decoded->canonical_option[i] = NULL;
841 }
842 if (opt_index != OPT_SPECIAL_unknown && opt_index != OPT_SPECIAL_ignore
843 && opt_index != OPT_SPECIAL_deprecated)
844 {
845 generate_canonical_option (opt_index, arg, value, decoded);
846 if (separate_args > 1)
847 {
848 for (i = 0; i < separate_args; i++)
849 {
850 if (argv[extra_args + 1 + i] == NULL)
851 break;
852 else
853 decoded->canonical_option[1 + i] = argv[extra_args + 1 + i];
854 }
855 gcc_assert (result == 1 + i);
856 decoded->canonical_option_num_elements = result;
857 }
858 }
859 decoded->orig_option_with_args_text
860 = p = XOBNEWVEC (&opts_obstack, char, total_len);
861 for (i = 0; i < result; i++)
862 {
863 size_t len = strlen (argv[i]);
864
865 /* Print the empty string verbally. */
866 if (len == 0)
867 {
868 *p++ = '"';
869 *p++ = '"';
870 }
871 else
872 memcpy (p, argv[i], len);
873 p += len;
874 if (i == result - 1)
875 *p++ = 0;
876 else
877 *p++ = ' ';
878 }
879
880 return result;
881 }
882
883 /* Obstack for option strings. */
884
885 struct obstack opts_obstack;
886
887 /* Like libiberty concat, but allocate using opts_obstack. */
888
889 char *
opts_concat(const char * first,...)890 opts_concat (const char *first, ...)
891 {
892 char *newstr, *end;
893 size_t length = 0;
894 const char *arg;
895 va_list ap;
896
897 /* First compute the size of the result and get sufficient memory. */
898 va_start (ap, first);
899 for (arg = first; arg; arg = va_arg (ap, const char *))
900 length += strlen (arg);
901 newstr = XOBNEWVEC (&opts_obstack, char, length + 1);
902 va_end (ap);
903
904 /* Now copy the individual pieces to the result string. */
905 va_start (ap, first);
906 for (arg = first, end = newstr; arg; arg = va_arg (ap, const char *))
907 {
908 length = strlen (arg);
909 memcpy (end, arg, length);
910 end += length;
911 }
912 *end = '\0';
913 va_end (ap);
914 return newstr;
915 }
916
917 /* Decode command-line options (ARGC and ARGV being the arguments of
918 main) into an array, setting *DECODED_OPTIONS to a pointer to that
919 array and *DECODED_OPTIONS_COUNT to the number of entries in the
920 array. The first entry in the array is always one for the program
921 name (OPT_SPECIAL_program_name). LANG_MASK indicates the language
922 flags applicable for decoding (including CL_COMMON and CL_TARGET if
923 those options should be considered applicable). Do not produce any
924 diagnostics or set state outside of these variables. */
925
926 void
decode_cmdline_options_to_array(unsigned int argc,const char ** argv,unsigned int lang_mask,struct cl_decoded_option ** decoded_options,unsigned int * decoded_options_count)927 decode_cmdline_options_to_array (unsigned int argc, const char **argv,
928 unsigned int lang_mask,
929 struct cl_decoded_option **decoded_options,
930 unsigned int *decoded_options_count)
931 {
932 unsigned int n, i;
933 struct cl_decoded_option *opt_array;
934 unsigned int num_decoded_options;
935
936 opt_array = XNEWVEC (struct cl_decoded_option, argc);
937
938 opt_array[0].opt_index = OPT_SPECIAL_program_name;
939 opt_array[0].warn_message = NULL;
940 opt_array[0].arg = argv[0];
941 opt_array[0].orig_option_with_args_text = argv[0];
942 opt_array[0].canonical_option_num_elements = 1;
943 opt_array[0].canonical_option[0] = argv[0];
944 opt_array[0].canonical_option[1] = NULL;
945 opt_array[0].canonical_option[2] = NULL;
946 opt_array[0].canonical_option[3] = NULL;
947 opt_array[0].value = 1;
948 opt_array[0].errors = 0;
949 num_decoded_options = 1;
950
951 for (i = 1; i < argc; i += n)
952 {
953 const char *opt = argv[i];
954
955 /* Interpret "-" or a non-switch as a file name. */
956 if (opt[0] != '-' || opt[1] == '\0')
957 {
958 generate_option_input_file (opt, &opt_array[num_decoded_options]);
959 num_decoded_options++;
960 n = 1;
961 continue;
962 }
963
964 n = decode_cmdline_option (argv + i, lang_mask,
965 &opt_array[num_decoded_options]);
966 num_decoded_options++;
967 }
968
969 *decoded_options = opt_array;
970 *decoded_options_count = num_decoded_options;
971 prune_options (decoded_options, decoded_options_count);
972 }
973
974 /* Return true if NEXT_OPT_IDX cancels OPT_IDX. Return false if the
975 next one is the same as ORIG_NEXT_OPT_IDX. */
976
977 static bool
cancel_option(int opt_idx,int next_opt_idx,int orig_next_opt_idx)978 cancel_option (int opt_idx, int next_opt_idx, int orig_next_opt_idx)
979 {
980 /* An option can be canceled by the same option or an option with
981 Negative. */
982 if (cl_options [next_opt_idx].neg_index == opt_idx)
983 return true;
984
985 if (cl_options [next_opt_idx].neg_index != orig_next_opt_idx)
986 return cancel_option (opt_idx, cl_options [next_opt_idx].neg_index,
987 orig_next_opt_idx);
988
989 return false;
990 }
991
992 /* Filter out options canceled by the ones after them. */
993
994 static void
prune_options(struct cl_decoded_option ** decoded_options,unsigned int * decoded_options_count)995 prune_options (struct cl_decoded_option **decoded_options,
996 unsigned int *decoded_options_count)
997 {
998 unsigned int old_decoded_options_count = *decoded_options_count;
999 struct cl_decoded_option *old_decoded_options = *decoded_options;
1000 unsigned int new_decoded_options_count;
1001 struct cl_decoded_option *new_decoded_options
1002 = XNEWVEC (struct cl_decoded_option, old_decoded_options_count);
1003 unsigned int i;
1004 const struct cl_option *option;
1005 unsigned int fdiagnostics_color_idx = 0;
1006
1007 /* Remove arguments which are negated by others after them. */
1008 new_decoded_options_count = 0;
1009 for (i = 0; i < old_decoded_options_count; i++)
1010 {
1011 unsigned int j, opt_idx, next_opt_idx;
1012
1013 if (old_decoded_options[i].errors & ~CL_ERR_WRONG_LANG)
1014 goto keep;
1015
1016 opt_idx = old_decoded_options[i].opt_index;
1017 switch (opt_idx)
1018 {
1019 case OPT_SPECIAL_unknown:
1020 case OPT_SPECIAL_ignore:
1021 case OPT_SPECIAL_deprecated:
1022 case OPT_SPECIAL_program_name:
1023 case OPT_SPECIAL_input_file:
1024 goto keep;
1025
1026 /* Do not save OPT_fdiagnostics_color_, just remember the last one. */
1027 case OPT_fdiagnostics_color_:
1028 fdiagnostics_color_idx = i;
1029 continue;
1030
1031 default:
1032 gcc_assert (opt_idx < cl_options_count);
1033 option = &cl_options[opt_idx];
1034 if (option->neg_index < 0)
1035 goto keep;
1036
1037 /* Skip joined switches. */
1038 if ((option->flags & CL_JOINED)
1039 && (!option->cl_reject_negative
1040 || (unsigned int) option->neg_index != opt_idx))
1041 goto keep;
1042
1043 for (j = i + 1; j < old_decoded_options_count; j++)
1044 {
1045 if (old_decoded_options[j].errors & ~CL_ERR_WRONG_LANG)
1046 continue;
1047 next_opt_idx = old_decoded_options[j].opt_index;
1048 if (next_opt_idx >= cl_options_count)
1049 continue;
1050 if (cl_options[next_opt_idx].neg_index < 0)
1051 continue;
1052 if ((cl_options[next_opt_idx].flags & CL_JOINED)
1053 && (!cl_options[next_opt_idx].cl_reject_negative
1054 || ((unsigned int) cl_options[next_opt_idx].neg_index
1055 != next_opt_idx)))
1056 continue;
1057 if (cancel_option (opt_idx, next_opt_idx, next_opt_idx))
1058 break;
1059 }
1060 if (j == old_decoded_options_count)
1061 {
1062 keep:
1063 new_decoded_options[new_decoded_options_count]
1064 = old_decoded_options[i];
1065 new_decoded_options_count++;
1066 }
1067 break;
1068 }
1069 }
1070
1071 if (fdiagnostics_color_idx >= 1)
1072 {
1073 /* We put the last -fdiagnostics-color= at the first position
1074 after argv[0] so it can take effect immediately. */
1075 memmove (new_decoded_options + 2, new_decoded_options + 1,
1076 sizeof (struct cl_decoded_option)
1077 * (new_decoded_options_count - 1));
1078 new_decoded_options[1] = old_decoded_options[fdiagnostics_color_idx];
1079 new_decoded_options_count++;
1080 }
1081
1082 free (old_decoded_options);
1083 new_decoded_options = XRESIZEVEC (struct cl_decoded_option,
1084 new_decoded_options,
1085 new_decoded_options_count);
1086 *decoded_options = new_decoded_options;
1087 *decoded_options_count = new_decoded_options_count;
1088 }
1089
1090 /* Handle option DECODED for the language indicated by LANG_MASK,
1091 using the handlers in HANDLERS and setting fields in OPTS and
1092 OPTS_SET. KIND is the diagnostic_t if this is a diagnostics
1093 option, DK_UNSPECIFIED otherwise, and LOC is the location of the
1094 option for options from the source file, UNKNOWN_LOCATION
1095 otherwise. GENERATED_P is true for an option generated as part of
1096 processing another option or otherwise generated internally, false
1097 for one explicitly passed by the user. control_warning_option
1098 generated options are considered explicitly passed by the user.
1099 Returns false if the switch was invalid. DC is the diagnostic
1100 context for options affecting diagnostics state, or NULL. */
1101
1102 static bool
handle_option(struct gcc_options * opts,struct gcc_options * opts_set,const struct cl_decoded_option * decoded,unsigned int lang_mask,int kind,location_t loc,const struct cl_option_handlers * handlers,bool generated_p,diagnostic_context * dc)1103 handle_option (struct gcc_options *opts,
1104 struct gcc_options *opts_set,
1105 const struct cl_decoded_option *decoded,
1106 unsigned int lang_mask, int kind, location_t loc,
1107 const struct cl_option_handlers *handlers,
1108 bool generated_p, diagnostic_context *dc)
1109 {
1110 size_t opt_index = decoded->opt_index;
1111 const char *arg = decoded->arg;
1112 HOST_WIDE_INT value = decoded->value;
1113 const struct cl_option *option = &cl_options[opt_index];
1114 void *flag_var = option_flag_var (opt_index, opts);
1115 size_t i;
1116
1117 if (flag_var)
1118 set_option (opts, (generated_p ? NULL : opts_set),
1119 opt_index, value, arg, kind, loc, dc);
1120
1121 for (i = 0; i < handlers->num_handlers; i++)
1122 if (option->flags & handlers->handlers[i].mask)
1123 {
1124 if (!handlers->handlers[i].handler (opts, opts_set, decoded,
1125 lang_mask, kind, loc,
1126 handlers, dc,
1127 handlers->target_option_override_hook))
1128 return false;
1129 }
1130
1131 return true;
1132 }
1133
1134 /* Like handle_option, but OPT_INDEX, ARG and VALUE describe the
1135 option instead of DECODED. This is used for callbacks when one
1136 option implies another instead of an option being decoded from the
1137 command line. */
1138
1139 bool
handle_generated_option(struct gcc_options * opts,struct gcc_options * opts_set,size_t opt_index,const char * arg,HOST_WIDE_INT value,unsigned int lang_mask,int kind,location_t loc,const struct cl_option_handlers * handlers,bool generated_p,diagnostic_context * dc)1140 handle_generated_option (struct gcc_options *opts,
1141 struct gcc_options *opts_set,
1142 size_t opt_index, const char *arg, HOST_WIDE_INT value,
1143 unsigned int lang_mask, int kind, location_t loc,
1144 const struct cl_option_handlers *handlers,
1145 bool generated_p, diagnostic_context *dc)
1146 {
1147 struct cl_decoded_option decoded;
1148
1149 generate_option (opt_index, arg, value, lang_mask, &decoded);
1150 return handle_option (opts, opts_set, &decoded, lang_mask, kind, loc,
1151 handlers, generated_p, dc);
1152 }
1153
1154 /* Fill in *DECODED with an option described by OPT_INDEX, ARG and
1155 VALUE for a front end using LANG_MASK. This is used when the
1156 compiler generates options internally. */
1157
1158 void
generate_option(size_t opt_index,const char * arg,HOST_WIDE_INT value,unsigned int lang_mask,struct cl_decoded_option * decoded)1159 generate_option (size_t opt_index, const char *arg, HOST_WIDE_INT value,
1160 unsigned int lang_mask, struct cl_decoded_option *decoded)
1161 {
1162 const struct cl_option *option = &cl_options[opt_index];
1163
1164 decoded->opt_index = opt_index;
1165 decoded->warn_message = NULL;
1166 decoded->arg = arg;
1167 decoded->value = value;
1168 decoded->errors = (option_ok_for_language (option, lang_mask)
1169 ? 0
1170 : CL_ERR_WRONG_LANG);
1171
1172 generate_canonical_option (opt_index, arg, value, decoded);
1173 switch (decoded->canonical_option_num_elements)
1174 {
1175 case 1:
1176 decoded->orig_option_with_args_text = decoded->canonical_option[0];
1177 break;
1178
1179 case 2:
1180 decoded->orig_option_with_args_text
1181 = opts_concat (decoded->canonical_option[0], " ",
1182 decoded->canonical_option[1], NULL);
1183 break;
1184
1185 default:
1186 gcc_unreachable ();
1187 }
1188 }
1189
1190 /* Fill in *DECODED with an option for input file FILE. */
1191
1192 void
generate_option_input_file(const char * file,struct cl_decoded_option * decoded)1193 generate_option_input_file (const char *file,
1194 struct cl_decoded_option *decoded)
1195 {
1196 decoded->opt_index = OPT_SPECIAL_input_file;
1197 decoded->warn_message = NULL;
1198 decoded->arg = file;
1199 decoded->orig_option_with_args_text = file;
1200 decoded->canonical_option_num_elements = 1;
1201 decoded->canonical_option[0] = file;
1202 decoded->canonical_option[1] = NULL;
1203 decoded->canonical_option[2] = NULL;
1204 decoded->canonical_option[3] = NULL;
1205 decoded->value = 1;
1206 decoded->errors = 0;
1207 }
1208
1209 /* Helper function for listing valid choices and hint for misspelled
1210 value. CANDIDATES is a vector containing all valid strings,
1211 STR is set to a heap allocated string that contains all those
1212 strings concatenated, separated by spaces, and the return value
1213 is the closest string from those to ARG, or NULL if nothing is
1214 close enough. Callers should XDELETEVEC (STR) after using it
1215 to avoid memory leaks. */
1216
1217 const char *
candidates_list_and_hint(const char * arg,char * & str,const auto_vec<const char * > & candidates)1218 candidates_list_and_hint (const char *arg, char *&str,
1219 const auto_vec <const char *> &candidates)
1220 {
1221 size_t len = 0;
1222 int i;
1223 const char *candidate;
1224 char *p;
1225
1226 FOR_EACH_VEC_ELT (candidates, i, candidate)
1227 len += strlen (candidate) + 1;
1228
1229 str = p = XNEWVEC (char, len);
1230 FOR_EACH_VEC_ELT (candidates, i, candidate)
1231 {
1232 len = strlen (candidate);
1233 memcpy (p, candidate, len);
1234 p[len] = ' ';
1235 p += len + 1;
1236 }
1237 p[-1] = '\0';
1238 return find_closest_string (arg, &candidates);
1239 }
1240
1241 /* Perform diagnostics for read_cmdline_option and control_warning_option
1242 functions. Returns true if an error has been diagnosed.
1243 LOC and LANG_MASK arguments like in read_cmdline_option.
1244 OPTION is the option to report diagnostics for, OPT the name
1245 of the option as text, ARG the argument of the option (for joined
1246 options), ERRORS is bitmask of CL_ERR_* values. */
1247
1248 static bool
cmdline_handle_error(location_t loc,const struct cl_option * option,const char * opt,const char * arg,int errors,unsigned int lang_mask)1249 cmdline_handle_error (location_t loc, const struct cl_option *option,
1250 const char *opt, const char *arg, int errors,
1251 unsigned int lang_mask)
1252 {
1253 if (errors & CL_ERR_DISABLED)
1254 {
1255 error_at (loc, "command line option %qs"
1256 " is not supported by this configuration", opt);
1257 return true;
1258 }
1259
1260 if (errors & CL_ERR_MISSING_ARG)
1261 {
1262 if (option->missing_argument_error)
1263 error_at (loc, option->missing_argument_error, opt);
1264 else
1265 error_at (loc, "missing argument to %qs", opt);
1266 return true;
1267 }
1268
1269 if (errors & CL_ERR_UINT_ARG)
1270 {
1271 if (option->cl_byte_size)
1272 error_at (loc, "argument to %qs should be a non-negative integer "
1273 "optionally followed by a size unit",
1274 option->opt_text);
1275 else
1276 error_at (loc, "argument to %qs should be a non-negative integer",
1277 option->opt_text);
1278 return true;
1279 }
1280
1281 if (errors & CL_ERR_INT_RANGE_ARG)
1282 {
1283 error_at (loc, "argument to %qs is not between %d and %d",
1284 option->opt_text, option->range_min, option->range_max);
1285 return true;
1286 }
1287
1288 if (errors & CL_ERR_ENUM_ARG)
1289 {
1290 const struct cl_enum *e = &cl_enums[option->var_enum];
1291 unsigned int i;
1292 char *s;
1293
1294 auto_diagnostic_group d;
1295 if (e->unknown_error)
1296 error_at (loc, e->unknown_error, arg);
1297 else
1298 error_at (loc, "unrecognized argument in option %qs", opt);
1299
1300 auto_vec <const char *> candidates;
1301 for (i = 0; e->values[i].arg != NULL; i++)
1302 {
1303 if (!enum_arg_ok_for_language (&e->values[i], lang_mask))
1304 continue;
1305 candidates.safe_push (e->values[i].arg);
1306 }
1307 const char *hint = candidates_list_and_hint (arg, s, candidates);
1308 if (hint)
1309 inform (loc, "valid arguments to %qs are: %s; did you mean %qs?",
1310 option->opt_text, s, hint);
1311 else
1312 inform (loc, "valid arguments to %qs are: %s", option->opt_text, s);
1313 XDELETEVEC (s);
1314
1315 return true;
1316 }
1317
1318 return false;
1319 }
1320
1321 /* Handle the switch DECODED (location LOC) for the language indicated
1322 by LANG_MASK, using the handlers in *HANDLERS and setting fields in
1323 OPTS and OPTS_SET and using diagnostic context DC (if not NULL) for
1324 diagnostic options. */
1325
1326 void
read_cmdline_option(struct gcc_options * opts,struct gcc_options * opts_set,struct cl_decoded_option * decoded,location_t loc,unsigned int lang_mask,const struct cl_option_handlers * handlers,diagnostic_context * dc)1327 read_cmdline_option (struct gcc_options *opts,
1328 struct gcc_options *opts_set,
1329 struct cl_decoded_option *decoded,
1330 location_t loc,
1331 unsigned int lang_mask,
1332 const struct cl_option_handlers *handlers,
1333 diagnostic_context *dc)
1334 {
1335 const struct cl_option *option;
1336 const char *opt = decoded->orig_option_with_args_text;
1337
1338 if (decoded->warn_message)
1339 warning_at (loc, 0, decoded->warn_message, opt);
1340
1341 if (decoded->opt_index == OPT_SPECIAL_unknown)
1342 {
1343 if (handlers->unknown_option_callback (decoded))
1344 error_at (loc, "unrecognized command line option %qs", decoded->arg);
1345 return;
1346 }
1347
1348 if (decoded->opt_index == OPT_SPECIAL_ignore)
1349 return;
1350
1351 if (decoded->opt_index == OPT_SPECIAL_deprecated)
1352 {
1353 /* Warn only about positive ignored options. */
1354 if (decoded->value)
1355 warning_at (loc, 0, "switch %qs is no longer supported", opt);
1356 return;
1357 }
1358
1359 option = &cl_options[decoded->opt_index];
1360
1361 if (decoded->errors
1362 && cmdline_handle_error (loc, option, opt, decoded->arg,
1363 decoded->errors, lang_mask))
1364 return;
1365
1366 if (decoded->errors & CL_ERR_WRONG_LANG)
1367 {
1368 handlers->wrong_lang_callback (decoded, lang_mask);
1369 return;
1370 }
1371
1372 gcc_assert (!decoded->errors);
1373
1374 if (!handle_option (opts, opts_set, decoded, lang_mask, DK_UNSPECIFIED,
1375 loc, handlers, false, dc))
1376 error_at (loc, "unrecognized command line option %qs", opt);
1377 }
1378
1379 /* Set any field in OPTS, and OPTS_SET if not NULL, for option
1380 OPT_INDEX according to VALUE and ARG, diagnostic kind KIND,
1381 location LOC, using diagnostic context DC if not NULL for
1382 diagnostic classification. */
1383
1384 void
set_option(struct gcc_options * opts,struct gcc_options * opts_set,int opt_index,HOST_WIDE_INT value,const char * arg,int kind,location_t loc,diagnostic_context * dc)1385 set_option (struct gcc_options *opts, struct gcc_options *opts_set,
1386 int opt_index, HOST_WIDE_INT value, const char *arg, int kind,
1387 location_t loc, diagnostic_context *dc)
1388 {
1389 const struct cl_option *option = &cl_options[opt_index];
1390 void *flag_var = option_flag_var (opt_index, opts);
1391 void *set_flag_var = NULL;
1392
1393 if (!flag_var)
1394 return;
1395
1396 if ((diagnostic_t) kind != DK_UNSPECIFIED && dc != NULL)
1397 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1398
1399 if (opts_set != NULL)
1400 set_flag_var = option_flag_var (opt_index, opts_set);
1401
1402 switch (option->var_type)
1403 {
1404 case CLVC_BOOLEAN:
1405 if (option->cl_host_wide_int)
1406 {
1407 *(HOST_WIDE_INT *) flag_var = value;
1408 if (set_flag_var)
1409 *(HOST_WIDE_INT *) set_flag_var = 1;
1410 }
1411 else
1412 {
1413 *(int *) flag_var = value;
1414 if (set_flag_var)
1415 *(int *) set_flag_var = 1;
1416 }
1417
1418 break;
1419
1420 case CLVC_SIZE:
1421 if (option->cl_host_wide_int)
1422 {
1423 *(HOST_WIDE_INT *) flag_var = value;
1424 if (set_flag_var)
1425 *(HOST_WIDE_INT *) set_flag_var = value;
1426 }
1427 else
1428 {
1429 *(int *) flag_var = value;
1430 if (set_flag_var)
1431 *(int *) set_flag_var = value;
1432 }
1433
1434 break;
1435
1436 case CLVC_EQUAL:
1437 if (option->cl_host_wide_int)
1438 {
1439 *(HOST_WIDE_INT *) flag_var = (value
1440 ? option->var_value
1441 : !option->var_value);
1442 if (set_flag_var)
1443 *(HOST_WIDE_INT *) set_flag_var = 1;
1444 }
1445 else
1446 {
1447 *(int *) flag_var = (value
1448 ? option->var_value
1449 : !option->var_value);
1450 if (set_flag_var)
1451 *(int *) set_flag_var = 1;
1452 }
1453 break;
1454
1455 case CLVC_BIT_CLEAR:
1456 case CLVC_BIT_SET:
1457 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
1458 {
1459 if (option->cl_host_wide_int)
1460 *(HOST_WIDE_INT *) flag_var |= option->var_value;
1461 else
1462 *(int *) flag_var |= option->var_value;
1463 }
1464 else
1465 {
1466 if (option->cl_host_wide_int)
1467 *(HOST_WIDE_INT *) flag_var &= ~option->var_value;
1468 else
1469 *(int *) flag_var &= ~option->var_value;
1470 }
1471 if (set_flag_var)
1472 {
1473 if (option->cl_host_wide_int)
1474 *(HOST_WIDE_INT *) set_flag_var |= option->var_value;
1475 else
1476 *(int *) set_flag_var |= option->var_value;
1477 }
1478 break;
1479
1480 case CLVC_STRING:
1481 *(const char **) flag_var = arg;
1482 if (set_flag_var)
1483 *(const char **) set_flag_var = "";
1484 break;
1485
1486 case CLVC_ENUM:
1487 {
1488 const struct cl_enum *e = &cl_enums[option->var_enum];
1489
1490 e->set (flag_var, value);
1491 if (set_flag_var)
1492 e->set (set_flag_var, 1);
1493 }
1494 break;
1495
1496 case CLVC_DEFER:
1497 {
1498 vec<cl_deferred_option> *v
1499 = (vec<cl_deferred_option> *) *(void **) flag_var;
1500 cl_deferred_option p = {opt_index, arg, value};
1501 if (!v)
1502 v = XCNEW (vec<cl_deferred_option>);
1503 v->safe_push (p);
1504 *(void **) flag_var = v;
1505 if (set_flag_var)
1506 *(void **) set_flag_var = v;
1507 }
1508 break;
1509 }
1510 }
1511
1512 /* Return the address of the flag variable for option OPT_INDEX in
1513 options structure OPTS, or NULL if there is no flag variable. */
1514
1515 void *
option_flag_var(int opt_index,struct gcc_options * opts)1516 option_flag_var (int opt_index, struct gcc_options *opts)
1517 {
1518 const struct cl_option *option = &cl_options[opt_index];
1519
1520 if (option->flag_var_offset == (unsigned short) -1)
1521 return NULL;
1522 return (void *)(((char *) opts) + option->flag_var_offset);
1523 }
1524
1525 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
1526 or -1 if it isn't a simple on-off switch. */
1527
1528 int
option_enabled(int opt_idx,void * opts)1529 option_enabled (int opt_idx, void *opts)
1530 {
1531 const struct cl_option *option = &(cl_options[opt_idx]);
1532 struct gcc_options *optsg = (struct gcc_options *) opts;
1533 void *flag_var = option_flag_var (opt_idx, optsg);
1534
1535 if (flag_var)
1536 switch (option->var_type)
1537 {
1538 case CLVC_BOOLEAN:
1539 if (option->cl_host_wide_int)
1540 return *(HOST_WIDE_INT *) flag_var != 0;
1541 else
1542 return *(int *) flag_var != 0;
1543
1544 case CLVC_EQUAL:
1545 if (option->cl_host_wide_int)
1546 return *(HOST_WIDE_INT *) flag_var == option->var_value;
1547 else
1548 return *(int *) flag_var == option->var_value;
1549
1550 case CLVC_BIT_CLEAR:
1551 if (option->cl_host_wide_int)
1552 return (*(HOST_WIDE_INT *) flag_var & option->var_value) == 0;
1553 else
1554 return (*(int *) flag_var & option->var_value) == 0;
1555
1556 case CLVC_BIT_SET:
1557 if (option->cl_host_wide_int)
1558 return (*(HOST_WIDE_INT *) flag_var & option->var_value) != 0;
1559 else
1560 return (*(int *) flag_var & option->var_value) != 0;
1561
1562 case CLVC_SIZE:
1563 if (option->cl_host_wide_int)
1564 return *(HOST_WIDE_INT *) flag_var != -1;
1565 else
1566 return *(int *) flag_var != -1;
1567
1568 case CLVC_STRING:
1569 case CLVC_ENUM:
1570 case CLVC_DEFER:
1571 break;
1572 }
1573 return -1;
1574 }
1575
1576 /* Fill STATE with the current state of option OPTION in OPTS. Return
1577 true if there is some state to store. */
1578
1579 bool
get_option_state(struct gcc_options * opts,int option,struct cl_option_state * state)1580 get_option_state (struct gcc_options *opts, int option,
1581 struct cl_option_state *state)
1582 {
1583 void *flag_var = option_flag_var (option, opts);
1584
1585 if (flag_var == 0)
1586 return false;
1587
1588 switch (cl_options[option].var_type)
1589 {
1590 case CLVC_BOOLEAN:
1591 case CLVC_EQUAL:
1592 case CLVC_SIZE:
1593 state->data = flag_var;
1594 state->size = (cl_options[option].cl_host_wide_int
1595 ? sizeof (HOST_WIDE_INT)
1596 : sizeof (int));
1597 break;
1598
1599 case CLVC_BIT_CLEAR:
1600 case CLVC_BIT_SET:
1601 state->ch = option_enabled (option, opts);
1602 state->data = &state->ch;
1603 state->size = 1;
1604 break;
1605
1606 case CLVC_STRING:
1607 state->data = *(const char **) flag_var;
1608 if (state->data == 0)
1609 state->data = "";
1610 state->size = strlen ((const char *) state->data) + 1;
1611 break;
1612
1613 case CLVC_ENUM:
1614 state->data = flag_var;
1615 state->size = cl_enums[cl_options[option].var_enum].var_size;
1616 break;
1617
1618 case CLVC_DEFER:
1619 return false;
1620 }
1621 return true;
1622 }
1623
1624 /* Set a warning option OPT_INDEX (language mask LANG_MASK, option
1625 handlers HANDLERS) to have diagnostic kind KIND for option
1626 structures OPTS and OPTS_SET and diagnostic context DC (possibly
1627 NULL), at location LOC (UNKNOWN_LOCATION for -Werror=). ARG is the
1628 argument of the option for joined options, or NULL otherwise. If IMPLY,
1629 the warning option in question is implied at this point. This is
1630 used by -Werror= and #pragma GCC diagnostic. */
1631
1632 void
control_warning_option(unsigned int opt_index,int kind,const char * arg,bool imply,location_t loc,unsigned int lang_mask,const struct cl_option_handlers * handlers,struct gcc_options * opts,struct gcc_options * opts_set,diagnostic_context * dc)1633 control_warning_option (unsigned int opt_index, int kind, const char *arg,
1634 bool imply, location_t loc, unsigned int lang_mask,
1635 const struct cl_option_handlers *handlers,
1636 struct gcc_options *opts,
1637 struct gcc_options *opts_set,
1638 diagnostic_context *dc)
1639 {
1640 if (cl_options[opt_index].alias_target != N_OPTS)
1641 {
1642 gcc_assert (!cl_options[opt_index].cl_separate_alias
1643 && !cl_options[opt_index].cl_negative_alias);
1644 if (cl_options[opt_index].alias_arg)
1645 arg = cl_options[opt_index].alias_arg;
1646 opt_index = cl_options[opt_index].alias_target;
1647 }
1648 if (opt_index == OPT_SPECIAL_ignore || opt_index == OPT_SPECIAL_deprecated)
1649 return;
1650 if (dc)
1651 diagnostic_classify_diagnostic (dc, opt_index, (diagnostic_t) kind, loc);
1652 if (imply)
1653 {
1654 const struct cl_option *option = &cl_options[opt_index];
1655
1656 /* -Werror=foo implies -Wfoo. */
1657 if (option->var_type == CLVC_BOOLEAN
1658 || option->var_type == CLVC_ENUM
1659 || option->var_type == CLVC_SIZE)
1660 {
1661 HOST_WIDE_INT value = 1;
1662
1663 if (arg && *arg == '\0' && !option->cl_missing_ok)
1664 arg = NULL;
1665
1666 if ((option->flags & CL_JOINED) && arg == NULL)
1667 {
1668 cmdline_handle_error (loc, option, option->opt_text, arg,
1669 CL_ERR_MISSING_ARG, lang_mask);
1670 return;
1671 }
1672
1673 /* If the switch takes an integer argument, convert it. */
1674 if (arg && (option->cl_uinteger || option->cl_host_wide_int))
1675 {
1676 int error = 0;
1677 value = *arg ? integral_argument (arg, &error,
1678 option->cl_byte_size) : 0;
1679 if (error)
1680 {
1681 cmdline_handle_error (loc, option, option->opt_text, arg,
1682 CL_ERR_UINT_ARG, lang_mask);
1683 return;
1684 }
1685 }
1686
1687 /* If the switch takes an enumerated argument, convert it. */
1688 if (arg && option->var_type == CLVC_ENUM)
1689 {
1690 const struct cl_enum *e = &cl_enums[option->var_enum];
1691
1692 if (enum_arg_to_value (e->values, arg, &value, lang_mask))
1693 {
1694 const char *carg = NULL;
1695
1696 if (enum_value_to_arg (e->values, &carg, value, lang_mask))
1697 arg = carg;
1698 gcc_assert (carg != NULL);
1699 }
1700 else
1701 {
1702 cmdline_handle_error (loc, option, option->opt_text, arg,
1703 CL_ERR_ENUM_ARG, lang_mask);
1704 return;
1705 }
1706 }
1707
1708 handle_generated_option (opts, opts_set,
1709 opt_index, arg, value, lang_mask,
1710 kind, loc, handlers, false, dc);
1711 }
1712 }
1713 }
1714
1715 /* Parse options in COLLECT_GCC_OPTIONS and push them on ARGV_OBSTACK.
1716 Store number of arguments into ARGC_P. */
1717
1718 void
parse_options_from_collect_gcc_options(const char * collect_gcc_options,obstack * argv_obstack,int * argc_p)1719 parse_options_from_collect_gcc_options (const char *collect_gcc_options,
1720 obstack *argv_obstack,
1721 int *argc_p)
1722 {
1723 char *argv_storage = xstrdup (collect_gcc_options);
1724 int j, k;
1725
1726 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
1727 {
1728 if (argv_storage[j] == '\'')
1729 {
1730 obstack_ptr_grow (argv_obstack, &argv_storage[k]);
1731 ++j;
1732 do
1733 {
1734 if (argv_storage[j] == '\0')
1735 fatal_error (input_location,
1736 "malformed %<COLLECT_GCC_OPTIONS%>");
1737 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
1738 {
1739 argv_storage[k++] = '\'';
1740 j += 4;
1741 }
1742 else if (argv_storage[j] == '\'')
1743 break;
1744 else
1745 argv_storage[k++] = argv_storage[j++];
1746 }
1747 while (1);
1748 argv_storage[k++] = '\0';
1749 }
1750 }
1751
1752 obstack_ptr_grow (argv_obstack, NULL);
1753 *argc_p = obstack_object_size (argv_obstack) / sizeof (void *) - 1;
1754 }
1755
1756 /* Prepend -Xassembler for each option in COLLECT_AS_OPTIONS,
1757 and push on O. */
1758
prepend_xassembler_to_collect_as_options(const char * collect_as_options,obstack * o)1759 void prepend_xassembler_to_collect_as_options (const char *collect_as_options,
1760 obstack *o)
1761 {
1762 obstack opts_obstack;
1763 int opts_count;
1764
1765 obstack_init (&opts_obstack);
1766 parse_options_from_collect_gcc_options (collect_as_options,
1767 &opts_obstack, &opts_count);
1768 const char **assembler_opts = XOBFINISH (&opts_obstack, const char **);
1769
1770 for (int i = 0; i < opts_count; i++)
1771 {
1772 obstack_grow (o, " '-Xassembler' ",
1773 strlen (" '-Xassembler' "));
1774 const char *opt = assembler_opts[i];
1775 obstack_1grow (o, '\'');
1776 obstack_grow (o, opt, strlen (opt));
1777 obstack_1grow (o, '\'');
1778 }
1779 }
1780