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