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