1/* Command line option handling.
2   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3   Free Software Foundation, Inc.
4   Contributed by Neil Booth.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING.  If not, write to the Free
20Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
2102110-1301, USA.  */
22
23/* $FreeBSD$ */
24
25#include "config.h"
26#include "system.h"
27#include "intl.h"
28#include "coretypes.h"
29#include "tm.h"
30#include "tree.h"
31#include "rtl.h"
32#include "ggc.h"
33#include "output.h"
34#include "langhooks.h"
35#include "opts.h"
36#include "options.h"
37#include "flags.h"
38#include "toplev.h"
39#include "params.h"
40#include "diagnostic.h"
41#include "tm_p.h"		/* For OPTIMIZATION_OPTIONS.  */
42#include "insn-attr.h"		/* For INSN_SCHEDULING.  */
43#include "target.h"
44#include "tree-pass.h"
45
46/* Value of the -G xx switch, and whether it was passed or not.  */
47unsigned HOST_WIDE_INT g_switch_value;
48bool g_switch_set;
49
50/* True if we should exit after parsing options.  */
51bool exit_after_options;
52
53/* Print various extra warnings.  -W/-Wextra.  */
54bool extra_warnings;
55
56/* True to warn about any objects definitions whose size is larger
57   than N bytes.  Also want about function definitions whose returned
58   values are larger than N bytes, where N is `larger_than_size'.  */
59bool warn_larger_than;
60HOST_WIDE_INT larger_than_size;
61
62/* True to warn about any function whose frame size is larger
63 * than N bytes. */
64bool warn_frame_larger_than;
65HOST_WIDE_INT frame_larger_than_size;
66
67/* Nonzero means warn about constructs which might not be
68   strict-aliasing safe.  */
69int warn_strict_aliasing;
70
71/* Nonzero means warn about optimizations which rely on undefined
72   signed overflow.  */
73int warn_strict_overflow;
74
75/* Hack for cooperation between set_Wunused and set_Wextra.  */
76static bool maybe_warn_unused_parameter;
77
78/* Type(s) of debugging information we are producing (if any).  See
79   flags.h for the definitions of the different possible types of
80   debugging information.  */
81enum debug_info_type write_symbols = NO_DEBUG;
82
83/* Level of debugging information we are producing.  See flags.h for
84   the definitions of the different possible levels.  */
85enum debug_info_level debug_info_level = DINFO_LEVEL_NONE;
86
87/* A major contribution to object and executable size is debug
88   information size.  A major contribution to debug information size
89   is struct descriptions replicated in several object files. The
90   following flags attempt to reduce this information.  The basic
91   idea is to not emit struct debugging information in the current
92   compilation unit when that information will be generated by
93   another compilation unit.
94
95   Debug information for a struct defined in the current source
96   file should be generated in the object file.  Likewise the
97   debug information for a struct defined in a header should be
98   generated in the object file of the corresponding source file.
99   Both of these case are handled when the base name of the file of
100   the struct definition matches the base name of the source file
101   of thet current compilation unit.  This matching emits minimal
102   struct debugging information.
103
104   The base file name matching rule above will fail to emit debug
105   information for structs defined in system headers.  So a second
106   category of files includes system headers in addition to files
107   with matching bases.
108
109   The remaining types of files are library headers and application
110   headers.  We cannot currently distinguish these two types.  */
111
112enum debug_struct_file
113{
114  DINFO_STRUCT_FILE_NONE,   /* Debug no structs. */
115  DINFO_STRUCT_FILE_BASE,   /* Debug structs defined in files with the
116                               same base name as the compilation unit. */
117  DINFO_STRUCT_FILE_SYS,    /* Also debug structs defined in system
118                               header files.  */
119  DINFO_STRUCT_FILE_ANY     /* Debug structs defined in all files. */
120};
121
122/* Generic structs (e.g. templates not explicitly specialized)
123   may not have a compilation unit associated with them, and so
124   may need to be treated differently from ordinary structs.
125
126   Structs only handled by reference (indirectly), will also usually
127   not need as much debugging information.  */
128
129static enum debug_struct_file debug_struct_ordinary[DINFO_USAGE_NUM_ENUMS]
130  = { DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY };
131static enum debug_struct_file debug_struct_generic[DINFO_USAGE_NUM_ENUMS]
132  = { DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY };
133
134/* Parse the -femit-struct-debug-detailed option value
135   and set the flag variables. */
136
137#define MATCH( prefix, string ) \
138  ((strncmp (prefix, string, sizeof prefix - 1) == 0) \
139   ? ((string += sizeof prefix - 1), 1) : 0)
140
141void
142set_struct_debug_option (const char *spec)
143{
144  /* various labels for comparison */
145  static char dfn_lbl[] = "dfn:", dir_lbl[] = "dir:", ind_lbl[] = "ind:";
146  static char ord_lbl[] = "ord:", gen_lbl[] = "gen:";
147  static char none_lbl[] = "none", any_lbl[] = "any";
148  static char base_lbl[] = "base", sys_lbl[] = "sys";
149
150  enum debug_struct_file files = DINFO_STRUCT_FILE_ANY;
151  /* Default is to apply to as much as possible. */
152  enum debug_info_usage usage = DINFO_USAGE_NUM_ENUMS;
153  int ord = 1, gen = 1;
154
155  /* What usage? */
156  if (MATCH (dfn_lbl, spec))
157    usage = DINFO_USAGE_DFN;
158  else if (MATCH (dir_lbl, spec))
159    usage = DINFO_USAGE_DIR_USE;
160  else if (MATCH (ind_lbl, spec))
161    usage = DINFO_USAGE_IND_USE;
162
163  /* Generics or not? */
164  if (MATCH (ord_lbl, spec))
165    gen = 0;
166  else if (MATCH (gen_lbl, spec))
167    ord = 0;
168
169  /* What allowable environment? */
170  if (MATCH (none_lbl, spec))
171    files = DINFO_STRUCT_FILE_NONE;
172  else if (MATCH (any_lbl, spec))
173    files = DINFO_STRUCT_FILE_ANY;
174  else if (MATCH (sys_lbl, spec))
175    files = DINFO_STRUCT_FILE_SYS;
176  else if (MATCH (base_lbl, spec))
177    files = DINFO_STRUCT_FILE_BASE;
178  else
179    error ("argument %qs to %<-femit-struct-debug-detailed%> not recognized",
180           spec);
181
182  /* Effect the specification. */
183  if (usage == DINFO_USAGE_NUM_ENUMS)
184    {
185      if (ord)
186        {
187          debug_struct_ordinary[DINFO_USAGE_DFN] = files;
188          debug_struct_ordinary[DINFO_USAGE_DIR_USE] = files;
189          debug_struct_ordinary[DINFO_USAGE_IND_USE] = files;
190        }
191      if (gen)
192        {
193          debug_struct_generic[DINFO_USAGE_DFN] = files;
194          debug_struct_generic[DINFO_USAGE_DIR_USE] = files;
195          debug_struct_generic[DINFO_USAGE_IND_USE] = files;
196        }
197    }
198  else
199    {
200      if (ord)
201        debug_struct_ordinary[usage] = files;
202      if (gen)
203        debug_struct_generic[usage] = files;
204    }
205
206  if (*spec == ',')
207    set_struct_debug_option (spec+1);
208  else
209    {
210      /* No more -femit-struct-debug-detailed specifications.
211         Do final checks. */
212      if (*spec != '\0')
213	error ("argument %qs to %<-femit-struct-debug-detailed%> unknown",
214               spec);
215      if (debug_struct_ordinary[DINFO_USAGE_DIR_USE]
216		< debug_struct_ordinary[DINFO_USAGE_IND_USE]
217	  || debug_struct_generic[DINFO_USAGE_DIR_USE]
218		< debug_struct_generic[DINFO_USAGE_IND_USE])
219	error ("%<-femit-struct-debug-detailed=dir:...%> must allow at least"
220               " as much as %<-femit-struct-debug-detailed=ind:...%>");
221    }
222}
223
224/* Find the base name of a path, stripping off both directories and
225   a single final extension. */
226static int
227base_of_path (const char *path, const char **base_out)
228{
229  const char *base = path;
230  const char *dot = 0;
231  const char *p = path;
232  char c = *p;
233  while (c)
234    {
235      if (IS_DIR_SEPARATOR(c))
236        {
237          base = p + 1;
238          dot = 0;
239        }
240      else if (c == '.')
241        dot = p;
242      c = *++p;
243    }
244  if (!dot)
245    dot = p;
246  *base_out = base;
247  return dot - base;
248}
249
250/* Match the base name of a file to the base name of a compilation unit. */
251
252static const char *main_input_basename;
253static int main_input_baselength;
254
255static int
256matches_main_base (const char *path)
257{
258  /* Cache the last query. */
259  static const char *last_path = NULL;
260  static int last_match = 0;
261  if (path != last_path)
262    {
263      const char *base;
264      int length = base_of_path (path, &base);
265      last_path = path;
266      last_match = (length == main_input_baselength
267                    && memcmp (base, main_input_basename, length) == 0);
268    }
269  return last_match;
270}
271
272#ifdef DEBUG_DEBUG_STRUCT
273
274static int
275dump_struct_debug (tree type, enum debug_info_usage usage,
276		   enum debug_struct_file criterion, int generic,
277		   int matches, int result)
278{
279  /* Find the type name. */
280  tree type_decl = TYPE_STUB_DECL (type);
281  tree t = type_decl;
282  const char *name = 0;
283  if (TREE_CODE (t) == TYPE_DECL)
284    t = DECL_NAME (t);
285  if (t)
286    name = IDENTIFIER_POINTER (t);
287
288  fprintf (stderr, "	struct %d %s %s %s %s %d %p %s\n",
289	   criterion,
290           DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
291           matches ? "bas" : "hdr",
292           generic ? "gen" : "ord",
293           usage == DINFO_USAGE_DFN ? ";" :
294             usage == DINFO_USAGE_DIR_USE ? "." : "*",
295           result,
296           (void*) type_decl, name);
297  return result;
298}
299#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
300  dump_struct_debug (type, usage, criterion, generic, matches, result)
301
302#else
303
304#define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
305  (result)
306
307#endif
308
309
310bool
311should_emit_struct_debug (tree type, enum debug_info_usage usage)
312{
313  enum debug_struct_file criterion;
314  tree type_decl;
315  bool generic = lang_hooks.types.generic_p (type);
316
317  if (generic)
318    criterion = debug_struct_generic[usage];
319  else
320    criterion = debug_struct_ordinary[usage];
321
322  if (criterion == DINFO_STRUCT_FILE_NONE)
323    return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
324  if (criterion == DINFO_STRUCT_FILE_ANY)
325    return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
326
327  type_decl = TYPE_STUB_DECL (type);
328
329  if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
330    return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
331
332  if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
333    return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
334  return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
335}
336
337/* Nonzero means use GNU-only extensions in the generated symbolic
338   debugging information.  Currently, this only has an effect when
339   write_symbols is set to DBX_DEBUG, XCOFF_DEBUG, or DWARF_DEBUG.  */
340bool use_gnu_debug_info_extensions;
341
342/* The default visibility for all symbols (unless overridden) */
343enum symbol_visibility default_visibility = VISIBILITY_DEFAULT;
344
345/* Disable unit-at-a-time for frontends that might be still broken in this
346   respect.  */
347
348bool no_unit_at_a_time_default;
349
350/* Global visibility options.  */
351struct visibility_flags visibility_options;
352
353/* Columns of --help display.  */
354static unsigned int columns = 80;
355
356/* What to print when a switch has no documentation.  */
357static const char undocumented_msg[] = N_("This switch lacks documentation");
358
359/* Used for bookkeeping on whether user set these flags so
360   -fprofile-use/-fprofile-generate does not use them.  */
361static bool profile_arc_flag_set, flag_profile_values_set;
362static bool flag_unroll_loops_set, flag_tracer_set;
363static bool flag_value_profile_transformations_set;
364static bool flag_peel_loops_set, flag_branch_probabilities_set;
365
366/* Functions excluded from profiling.  */
367
368typedef char *char_p; /* For DEF_VEC_P.  */
369DEF_VEC_P(char_p);
370DEF_VEC_ALLOC_P(char_p,heap);
371
372static VEC(char_p,heap) *flag_instrument_functions_exclude_functions;
373static VEC(char_p,heap) *flag_instrument_functions_exclude_files;
374
375/* Input file names.  */
376const char **in_fnames;
377unsigned num_in_fnames;
378
379static int common_handle_option (size_t scode, const char *arg, int value,
380				 unsigned int lang_mask);
381static void handle_param (const char *);
382static void set_Wextra (int);
383static unsigned int handle_option (const char **argv, unsigned int lang_mask);
384static char *write_langs (unsigned int lang_mask);
385static void complain_wrong_lang (const char *, const struct cl_option *,
386				 unsigned int lang_mask);
387static void handle_options (unsigned int, const char **, unsigned int);
388static void wrap_help (const char *help, const char *item, unsigned int);
389static void print_target_help (void);
390static void print_help (void);
391static void print_param_help (void);
392static void print_filtered_help (unsigned int);
393static unsigned int print_switch (const char *text, unsigned int indent);
394static void set_debug_level (enum debug_info_type type, int extended,
395			     const char *arg);
396
397/* If ARG is a non-negative integer made up solely of digits, return its
398   value, otherwise return -1.  */
399static int
400integral_argument (const char *arg)
401{
402  const char *p = arg;
403
404  while (*p && ISDIGIT (*p))
405    p++;
406
407  if (*p == '\0')
408    return atoi (arg);
409
410  return -1;
411}
412
413/* Return a malloced slash-separated list of languages in MASK.  */
414static char *
415write_langs (unsigned int mask)
416{
417  unsigned int n = 0, len = 0;
418  const char *lang_name;
419  char *result;
420
421  for (n = 0; (lang_name = lang_names[n]) != 0; n++)
422    if (mask & (1U << n))
423      len += strlen (lang_name) + 1;
424
425  result = XNEWVEC (char, len);
426  len = 0;
427  for (n = 0; (lang_name = lang_names[n]) != 0; n++)
428    if (mask & (1U << n))
429      {
430	if (len)
431	  result[len++] = '/';
432	strcpy (result + len, lang_name);
433	len += strlen (lang_name);
434      }
435
436  result[len] = 0;
437
438  return result;
439}
440
441/* Complain that switch OPT_INDEX does not apply to this front end.  */
442static void
443complain_wrong_lang (const char *text, const struct cl_option *option,
444		     unsigned int lang_mask)
445{
446  char *ok_langs, *bad_lang;
447
448  ok_langs = write_langs (option->flags);
449  bad_lang = write_langs (lang_mask);
450
451  /* Eventually this should become a hard error IMO.  */
452  warning (0, "command line option \"%s\" is valid for %s but not for %s",
453	   text, ok_langs, bad_lang);
454
455  free (ok_langs);
456  free (bad_lang);
457}
458
459/* Handle the switch beginning at ARGV for the language indicated by
460   LANG_MASK.  Returns the number of switches consumed.  */
461static unsigned int
462handle_option (const char **argv, unsigned int lang_mask)
463{
464  size_t opt_index;
465  const char *opt, *arg = 0;
466  char *dup = 0;
467  int value = 1;
468  unsigned int result = 0;
469  const struct cl_option *option;
470
471  opt = argv[0];
472
473  opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
474  if (opt_index == cl_options_count
475      && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
476      && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
477    {
478      /* Drop the "no-" from negative switches.  */
479      size_t len = strlen (opt) - 3;
480
481      dup = XNEWVEC (char, len + 1);
482      dup[0] = '-';
483      dup[1] = opt[1];
484      memcpy (dup + 2, opt + 5, len - 2 + 1);
485      opt = dup;
486      value = 0;
487      opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
488    }
489
490  if (opt_index == cl_options_count)
491    goto done;
492
493  option = &cl_options[opt_index];
494
495  /* Reject negative form of switches that don't take negatives as
496     unrecognized.  */
497  if (!value && (option->flags & CL_REJECT_NEGATIVE))
498    goto done;
499
500  /* We've recognized this switch.  */
501  result = 1;
502
503  /* Check to see if the option is disabled for this configuration.  */
504  if (option->flags & CL_DISABLED)
505    {
506      error ("command line option %qs"
507	     " is not supported by this configuration", opt);
508      goto done;
509    }
510
511  /* Sort out any argument the switch takes.  */
512  if (option->flags & CL_JOINED)
513    {
514      /* Have arg point to the original switch.  This is because
515	 some code, such as disable_builtin_function, expects its
516	 argument to be persistent until the program exits.  */
517      arg = argv[0] + cl_options[opt_index].opt_len + 1;
518      if (!value)
519	arg += strlen ("no-");
520
521      if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
522	{
523	  if (option->flags & CL_SEPARATE)
524	    {
525	      arg = argv[1];
526	      result = 2;
527	    }
528	  else
529	    /* Missing argument.  */
530	    arg = NULL;
531	}
532    }
533  else if (option->flags & CL_SEPARATE)
534    {
535      arg = argv[1];
536      result = 2;
537    }
538
539  /* Now we've swallowed any potential argument, complain if this
540     is a switch for a different front end.  */
541  if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
542    {
543      complain_wrong_lang (argv[0], option, lang_mask);
544      goto done;
545    }
546
547  if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
548    {
549      if (!lang_hooks.missing_argument (opt, opt_index))
550	error ("missing argument to \"%s\"", opt);
551      goto done;
552    }
553
554  /* If the switch takes an integer, convert it.  */
555  if (arg && (option->flags & CL_UINTEGER))
556    {
557      value = integral_argument (arg);
558      if (value == -1)
559	{
560	  error ("argument to \"%s\" should be a non-negative integer",
561		 option->opt_text);
562	  goto done;
563	}
564    }
565
566  if (option->flag_var)
567    switch (option->var_type)
568      {
569      case CLVC_BOOLEAN:
570	*(int *) option->flag_var = value;
571	break;
572
573      case CLVC_EQUAL:
574	*(int *) option->flag_var = (value
575				     ? option->var_value
576				     : !option->var_value);
577	break;
578
579      case CLVC_BIT_CLEAR:
580      case CLVC_BIT_SET:
581	if ((value != 0) == (option->var_type == CLVC_BIT_SET))
582	  *(int *) option->flag_var |= option->var_value;
583	else
584	  *(int *) option->flag_var &= ~option->var_value;
585	if (option->flag_var == &target_flags)
586	  target_flags_explicit |= option->var_value;
587	break;
588
589      case CLVC_STRING:
590	*(const char **) option->flag_var = arg;
591	break;
592      }
593
594  if (option->flags & lang_mask)
595    if (lang_hooks.handle_option (opt_index, arg, value) == 0)
596      result = 0;
597
598  if (result && (option->flags & CL_COMMON))
599    if (common_handle_option (opt_index, arg, value, lang_mask) == 0)
600      result = 0;
601
602  if (result && (option->flags & CL_TARGET))
603    if (!targetm.handle_option (opt_index, arg, value))
604      result = 0;
605
606 done:
607  if (dup)
608    free (dup);
609  return result;
610}
611
612/* Handle FILENAME from the command line.  */
613static void
614add_input_filename (const char *filename)
615{
616  num_in_fnames++;
617  in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
618  in_fnames[num_in_fnames - 1] = filename;
619}
620
621/* Add functions or file names to a vector of names to exclude from
622   instrumentation.  */
623
624static void
625add_instrument_functions_exclude_list (VEC(char_p,heap) **pvec,
626				       const char* arg)
627{
628  char *tmp;
629  char *r;
630  char *w;
631  char *token_start;
632
633  /* We never free this string.  */
634  tmp = xstrdup (arg);
635
636  r = tmp;
637  w = tmp;
638  token_start = tmp;
639
640  while (*r != '\0')
641    {
642      if (*r == ',')
643	{
644	  *w++ = '\0';
645	  ++r;
646	  VEC_safe_push (char_p, heap, *pvec, token_start);
647	  token_start = w;
648	}
649      if (*r == '\\' && r[1] == ',')
650	{
651	  *w++ = ',';
652	  r += 2;
653	}
654      else
655	*w++ = *r++;
656    }
657  if (*token_start != '\0')
658    VEC_safe_push (char_p, heap, *pvec, token_start);
659}
660
661/* Return whether we should exclude FNDECL from instrumentation.  */
662
663bool
664flag_instrument_functions_exclude_p (tree fndecl)
665{
666  if (VEC_length (char_p, flag_instrument_functions_exclude_functions) > 0)
667    {
668      const char *name;
669      int i;
670      char *s;
671
672      name = lang_hooks.decl_printable_name (fndecl, 0);
673      for (i = 0;
674	   VEC_iterate (char_p, flag_instrument_functions_exclude_functions,
675			i, s);
676	   ++i)
677	{
678	  if (strstr (name, s) != NULL)
679	    return true;
680	}
681    }
682
683  if (VEC_length (char_p, flag_instrument_functions_exclude_files) > 0)
684    {
685      const char *name;
686      int i;
687      char *s;
688
689      name = DECL_SOURCE_FILE (fndecl);
690      for (i = 0;
691	   VEC_iterate (char_p, flag_instrument_functions_exclude_files, i, s);
692	   ++i)
693	{
694	  if (strstr (name, s) != NULL)
695	    return true;
696	}
697    }
698
699  return false;
700}
701
702/* Decode and handle the vector of command line options.  LANG_MASK
703   contains has a single bit set representing the current
704   language.  */
705static void
706handle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
707{
708  unsigned int n, i;
709
710  for (i = 1; i < argc; i += n)
711    {
712      const char *opt = argv[i];
713
714      /* Interpret "-" or a non-switch as a file name.  */
715      if (opt[0] != '-' || opt[1] == '\0')
716	{
717	  if (main_input_filename == NULL)
718	    {
719	    main_input_filename = opt;
720	      main_input_baselength
721		= base_of_path (main_input_filename, &main_input_basename);
722	    }
723	  add_input_filename (opt);
724	  n = 1;
725	  continue;
726	}
727
728      n = handle_option (argv + i, lang_mask);
729
730      if (!n)
731	{
732	  n = 1;
733	  error ("unrecognized command line option \"%s\"", opt);
734	}
735    }
736}
737
738/* Parse command line options and set default flag values.  Do minimal
739   options processing.  */
740void
741decode_options (unsigned int argc, const char **argv)
742{
743  unsigned int i, lang_mask;
744
745  /* Perform language-specific options initialization.  */
746  lang_mask = lang_hooks.init_options (argc, argv);
747
748  lang_hooks.initialize_diagnostics (global_dc);
749
750  /* Scan to see what optimization level has been specified.  That will
751     determine the default value of many flags.  */
752  for (i = 1; i < argc; i++)
753    {
754      if (!strcmp (argv[i], "-O"))
755	{
756	  optimize = 1;
757	  optimize_size = 0;
758	}
759      else if (argv[i][0] == '-' && argv[i][1] == 'O')
760	{
761	  /* Handle -Os, -O2, -O3, -O69, ...  */
762	  const char *p = &argv[i][2];
763
764	  if ((p[0] == 's') && (p[1] == 0))
765	    {
766	      optimize_size = 1;
767
768	      /* Optimizing for size forces optimize to be 2.  */
769	      optimize = 2;
770	    }
771	  else
772	    {
773	      const int optimize_val = read_integral_parameter (p, p - 2, -1);
774	      if (optimize_val != -1)
775		{
776		  optimize = optimize_val;
777		  optimize_size = 0;
778		}
779	    }
780	}
781    }
782
783  if (!optimize)
784    {
785      flag_merge_constants = 0;
786    }
787
788  if (optimize >= 1)
789    {
790      flag_defer_pop = 1;
791#ifdef DELAY_SLOTS
792      flag_delayed_branch = 1;
793#endif
794#ifdef CAN_DEBUG_WITHOUT_FP
795      flag_omit_frame_pointer = 1;
796#endif
797      flag_guess_branch_prob = 1;
798      flag_cprop_registers = 1;
799      flag_if_conversion = 1;
800      flag_if_conversion2 = 1;
801      flag_ipa_pure_const = 1;
802      flag_ipa_reference = 1;
803      flag_tree_ccp = 1;
804      flag_tree_dce = 1;
805      flag_tree_dom = 1;
806      flag_tree_dse = 1;
807      flag_tree_ter = 1;
808      flag_tree_live_range_split = 1;
809      flag_tree_sra = 1;
810      flag_tree_copyrename = 1;
811      flag_tree_fre = 1;
812      flag_tree_copy_prop = 1;
813      flag_tree_sink = 1;
814      flag_tree_salias = 1;
815      if (!no_unit_at_a_time_default)
816        flag_unit_at_a_time = 1;
817
818      if (!optimize_size)
819	{
820	  /* Loop header copying usually increases size of the code.  This used
821	     not to be true, since quite often it is possible to verify that
822	     the condition is satisfied in the first iteration and therefore
823	     to eliminate it.  Jump threading handles these cases now.  */
824	  flag_tree_ch = 1;
825	}
826    }
827
828  if (optimize >= 2)
829    {
830      flag_thread_jumps = 1;
831      flag_crossjumping = 1;
832      flag_optimize_sibling_calls = 1;
833      flag_cse_follow_jumps = 1;
834      flag_cse_skip_blocks = 1;
835      flag_gcse = 1;
836      flag_expensive_optimizations = 1;
837      flag_ipa_type_escape = 1;
838      flag_rerun_cse_after_loop = 1;
839      flag_caller_saves = 1;
840      flag_peephole2 = 1;
841#ifdef INSN_SCHEDULING
842      flag_schedule_insns = 1;
843      flag_schedule_insns_after_reload = 1;
844#endif
845      flag_regmove = 1;
846      flag_strict_aliasing = 1;
847      flag_strict_overflow = 1;
848      flag_delete_null_pointer_checks = 1;
849      flag_reorder_blocks = 1;
850      flag_reorder_functions = 1;
851      flag_tree_store_ccp = 1;
852      flag_tree_store_copy_prop = 1;
853      /* XXX: some issues with ports have been traced to -ftree-vrp.
854         So remove it from -O2 and above.  Note that jdk1{5,6} are affected
855         and they build with w/-O3 - so we cannot just move it to -O3. */
856      /* flag_tree_vrp = 1; // See GCC tree-optimization/33099 */
857
858      if (!optimize_size)
859	{
860          /* PRE tends to generate bigger code.  */
861          flag_tree_pre = 1;
862	}
863    }
864
865  if (optimize >= 3)
866    {
867      flag_inline_functions = 1;
868      flag_unswitch_loops = 1;
869      flag_gcse_after_reload = 1;
870    }
871
872  if (optimize_size)
873    {
874      align_loops = 1;
875      align_jumps = 1;
876      align_labels = 1;
877      align_functions = 1;
878
879      /* Don't reorder blocks when optimizing for size because extra
880	 jump insns may be created; also barrier may create extra padding.
881
882	 More correctly we should have a block reordering mode that tried
883	 to minimize the combined size of all the jumps.  This would more
884	 or less automatically remove extra jumps, but would also try to
885	 use more short jumps instead of long jumps.  */
886      flag_reorder_blocks = 0;
887      flag_reorder_blocks_and_partition = 0;
888    }
889
890  if (optimize_size)
891    {
892      /* Inlining of very small functions usually reduces total size.  */
893      set_param_value ("max-inline-insns-single", 5);
894      set_param_value ("max-inline-insns-auto", 5);
895      flag_inline_functions = 1;
896
897      /* We want to crossjump as much as possible.  */
898      set_param_value ("min-crossjump-insns", 1);
899    }
900
901  /* Initialize whether `char' is signed.  */
902  flag_signed_char = DEFAULT_SIGNED_CHAR;
903  /* Set this to a special "uninitialized" value.  The actual default is set
904     after target options have been processed.  */
905  flag_short_enums = 2;
906
907  /* Initialize target_flags before OPTIMIZATION_OPTIONS so the latter can
908     modify it.  */
909  target_flags = targetm.default_target_flags;
910
911  /* Some tagets have ABI-specified unwind tables.  */
912  flag_unwind_tables = targetm.unwind_tables_default;
913
914#ifdef OPTIMIZATION_OPTIONS
915  /* Allow default optimizations to be specified on a per-machine basis.  */
916  OPTIMIZATION_OPTIONS (optimize, optimize_size);
917#endif
918
919  handle_options (argc, argv, lang_mask);
920
921  if (flag_pie)
922    flag_pic = flag_pie;
923  if (flag_pic && !flag_pie)
924    flag_shlib = 1;
925
926  if (flag_no_inline == 2)
927    flag_no_inline = 0;
928  else
929    flag_really_no_inline = flag_no_inline;
930
931  /* Set flag_no_inline before the post_options () hook.  The C front
932     ends use it to determine tree inlining defaults.  FIXME: such
933     code should be lang-independent when all front ends use tree
934     inlining, in which case it, and this condition, should be moved
935     to the top of process_options() instead.  */
936  if (optimize == 0)
937    {
938      /* Inlining does not work if not optimizing,
939	 so force it not to be done.  */
940      flag_no_inline = 1;
941      warn_inline = 0;
942
943      /* The c_decode_option function and decode_option hook set
944	 this to `2' if -Wall is used, so we can avoid giving out
945	 lots of errors for people who don't realize what -Wall does.  */
946      if (warn_uninitialized == 1)
947	warning (OPT_Wuninitialized,
948		 "-Wuninitialized is not supported without -O");
949    }
950
951  if (flag_really_no_inline == 2)
952    flag_really_no_inline = flag_no_inline;
953
954  /* The optimization to partition hot and cold basic blocks into separate
955     sections of the .o and executable files does not work (currently)
956     with exception handling.  This is because there is no support for
957     generating unwind info.  If flag_exceptions is turned on we need to
958     turn off the partitioning optimization.  */
959
960  if (flag_exceptions && flag_reorder_blocks_and_partition)
961    {
962      inform
963	    ("-freorder-blocks-and-partition does not work with exceptions");
964      flag_reorder_blocks_and_partition = 0;
965      flag_reorder_blocks = 1;
966    }
967
968  /* If user requested unwind info, then turn off the partitioning
969     optimization.  */
970
971  if (flag_unwind_tables && ! targetm.unwind_tables_default
972      && flag_reorder_blocks_and_partition)
973    {
974      inform ("-freorder-blocks-and-partition does not support unwind info");
975      flag_reorder_blocks_and_partition = 0;
976      flag_reorder_blocks = 1;
977    }
978
979  /* If the target requested unwind info, then turn off the partitioning
980     optimization with a different message.  Likewise, if the target does not
981     support named sections.  */
982
983  if (flag_reorder_blocks_and_partition
984      && (!targetm.have_named_sections
985	  || (flag_unwind_tables && targetm.unwind_tables_default)))
986    {
987      inform
988       ("-freorder-blocks-and-partition does not work on this architecture");
989      flag_reorder_blocks_and_partition = 0;
990      flag_reorder_blocks = 1;
991    }
992}
993
994/* Handle target- and language-independent options.  Return zero to
995   generate an "unknown option" message.  Only options that need
996   extra handling need to be listed here; if you simply want
997   VALUE assigned to a variable, it happens automatically.  */
998
999static int
1000common_handle_option (size_t scode, const char *arg, int value,
1001		      unsigned int lang_mask)
1002{
1003  enum opt_code code = (enum opt_code) scode;
1004
1005  switch (code)
1006    {
1007    case OPT__help:
1008      print_help ();
1009      exit_after_options = true;
1010      break;
1011
1012    case OPT__param:
1013      handle_param (arg);
1014      break;
1015
1016    case OPT__target_help:
1017      print_target_help ();
1018      exit_after_options = true;
1019      break;
1020
1021    case OPT__version:
1022      print_version (stderr, "");
1023      exit_after_options = true;
1024      break;
1025
1026    case OPT_G:
1027      g_switch_value = value;
1028      g_switch_set = true;
1029      break;
1030
1031    case OPT_O:
1032    case OPT_Os:
1033      /* Currently handled in a prescan.  */
1034      break;
1035
1036    case OPT_W:
1037      /* For backward compatibility, -W is the same as -Wextra.  */
1038      set_Wextra (value);
1039      break;
1040
1041    case OPT_Werror_:
1042      {
1043	char *new_option;
1044	int option_index;
1045	new_option = XNEWVEC (char, strlen (arg) + 2);
1046	new_option[0] = 'W';
1047	strcpy (new_option+1, arg);
1048	option_index = find_opt (new_option, lang_mask);
1049	if (option_index == N_OPTS)
1050	  {
1051	    error ("-Werror=%s: No option -%s", arg, new_option);
1052	  }
1053	else
1054	  {
1055	    int kind = value ? DK_ERROR : DK_WARNING;
1056	    diagnostic_classify_diagnostic (global_dc, option_index, kind);
1057
1058	    /* -Werror=foo implies -Wfoo.  */
1059	    if (cl_options[option_index].var_type == CLVC_BOOLEAN
1060		&& cl_options[option_index].flag_var
1061		&& kind == DK_ERROR)
1062	      *(int *) cl_options[option_index].flag_var = 1;
1063	    free (new_option);
1064	  }
1065      }
1066      break;
1067
1068    case OPT_Wextra:
1069      set_Wextra (value);
1070      break;
1071
1072    case OPT_Wlarger_than_:
1073      larger_than_size = value;
1074      warn_larger_than = value != -1;
1075      break;
1076
1077    case OPT_Wframe_larger_than_:
1078      frame_larger_than_size = value;
1079      warn_frame_larger_than = value != -1;
1080      break;
1081
1082    case OPT_Wstrict_aliasing:
1083      set_warn_strict_aliasing (value);
1084      break;
1085
1086    case OPT_Wstrict_aliasing_:
1087      warn_strict_aliasing = value;
1088      break;
1089
1090    case OPT_Wstrict_overflow:
1091      warn_strict_overflow = (value
1092			      ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
1093			      : 0);
1094      break;
1095
1096    case OPT_Wstrict_overflow_:
1097      warn_strict_overflow = value;
1098      break;
1099
1100    case OPT_Wunused:
1101      set_Wunused (value);
1102      break;
1103
1104    case OPT_aux_info:
1105    case OPT_aux_info_:
1106      aux_info_file_name = arg;
1107      flag_gen_aux_info = 1;
1108      break;
1109
1110    case OPT_auxbase:
1111      aux_base_name = arg;
1112      break;
1113
1114    case OPT_auxbase_strip:
1115      {
1116	char *tmp = xstrdup (arg);
1117	strip_off_ending (tmp, strlen (tmp));
1118	if (tmp[0])
1119	  aux_base_name = tmp;
1120      }
1121      break;
1122
1123    case OPT_d:
1124      decode_d_option (arg);
1125      break;
1126
1127    case OPT_dumpbase:
1128      dump_base_name = arg;
1129      break;
1130
1131    case OPT_falign_functions_:
1132      align_functions = value;
1133      break;
1134
1135    case OPT_falign_jumps_:
1136      align_jumps = value;
1137      break;
1138
1139    case OPT_falign_labels_:
1140      align_labels = value;
1141      break;
1142
1143    case OPT_falign_loops_:
1144      align_loops = value;
1145      break;
1146
1147    case OPT_fbranch_probabilities:
1148      flag_branch_probabilities_set = true;
1149      break;
1150
1151    case OPT_fcall_used_:
1152      fix_register (arg, 0, 1);
1153      break;
1154
1155    case OPT_fcall_saved_:
1156      fix_register (arg, 0, 0);
1157      break;
1158
1159    case OPT_fdiagnostics_show_location_:
1160      if (!strcmp (arg, "once"))
1161	diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
1162      else if (!strcmp (arg, "every-line"))
1163	diagnostic_prefixing_rule (global_dc)
1164	  = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
1165      else
1166	return 0;
1167      break;
1168
1169    case OPT_fdiagnostics_show_option:
1170      global_dc->show_option_requested = true;
1171      break;
1172
1173    case OPT_fdump_:
1174      if (!dump_switch_p (arg))
1175	return 0;
1176      break;
1177
1178    case OPT_ffast_math:
1179      set_fast_math_flags (value);
1180      break;
1181
1182    case OPT_ffixed_:
1183      fix_register (arg, 1, 1);
1184      break;
1185
1186    case OPT_finline_limit_:
1187    case OPT_finline_limit_eq:
1188      set_param_value ("max-inline-insns-single", value / 2);
1189      set_param_value ("max-inline-insns-auto", value / 2);
1190      break;
1191
1192    case OPT_finstrument_functions_exclude_function_list_:
1193      add_instrument_functions_exclude_list
1194	(&flag_instrument_functions_exclude_functions, arg);
1195      break;
1196
1197    case OPT_finstrument_functions_exclude_file_list_:
1198      add_instrument_functions_exclude_list
1199	(&flag_instrument_functions_exclude_files, arg);
1200      break;
1201
1202    case OPT_fmessage_length_:
1203      pp_set_line_maximum_length (global_dc->printer, value);
1204      break;
1205
1206    case OPT_fpack_struct_:
1207      if (value <= 0 || (value & (value - 1)) || value > 16)
1208	error("structure alignment must be a small power of two, not %d", value);
1209      else
1210	{
1211	  initial_max_fld_align = value;
1212	  maximum_field_alignment = value * BITS_PER_UNIT;
1213	}
1214      break;
1215
1216    case OPT_fpeel_loops:
1217      flag_peel_loops_set = true;
1218      break;
1219
1220    case OPT_fprofile_arcs:
1221      profile_arc_flag_set = true;
1222      break;
1223
1224    case OPT_fprofile_use:
1225      if (!flag_branch_probabilities_set)
1226        flag_branch_probabilities = value;
1227      if (!flag_profile_values_set)
1228        flag_profile_values = value;
1229      if (!flag_unroll_loops_set)
1230        flag_unroll_loops = value;
1231      if (!flag_peel_loops_set)
1232        flag_peel_loops = value;
1233      if (!flag_tracer_set)
1234        flag_tracer = value;
1235      if (!flag_value_profile_transformations_set)
1236        flag_value_profile_transformations = value;
1237      break;
1238
1239    case OPT_fprofile_generate:
1240      if (!profile_arc_flag_set)
1241        profile_arc_flag = value;
1242      if (!flag_profile_values_set)
1243        flag_profile_values = value;
1244      if (!flag_value_profile_transformations_set)
1245        flag_value_profile_transformations = value;
1246      break;
1247
1248    case OPT_fprofile_values:
1249      flag_profile_values_set = true;
1250      break;
1251
1252    case OPT_fvisibility_:
1253      {
1254        if (!strcmp(arg, "default"))
1255          default_visibility = VISIBILITY_DEFAULT;
1256        else if (!strcmp(arg, "internal"))
1257          default_visibility = VISIBILITY_INTERNAL;
1258        else if (!strcmp(arg, "hidden"))
1259          default_visibility = VISIBILITY_HIDDEN;
1260        else if (!strcmp(arg, "protected"))
1261          default_visibility = VISIBILITY_PROTECTED;
1262        else
1263          error ("unrecognized visibility value \"%s\"", arg);
1264      }
1265      break;
1266
1267    case OPT_fvpt:
1268      flag_value_profile_transformations_set = true;
1269      break;
1270
1271    case OPT_frandom_seed:
1272      /* The real switch is -fno-random-seed.  */
1273      if (value)
1274	return 0;
1275      flag_random_seed = NULL;
1276      break;
1277
1278    case OPT_frandom_seed_:
1279      flag_random_seed = arg;
1280      break;
1281
1282    case OPT_fsched_verbose_:
1283#ifdef INSN_SCHEDULING
1284      fix_sched_param ("verbose", arg);
1285      break;
1286#else
1287      return 0;
1288#endif
1289
1290    case OPT_fsched_stalled_insns_:
1291      flag_sched_stalled_insns = value;
1292      if (flag_sched_stalled_insns == 0)
1293	flag_sched_stalled_insns = -1;
1294      break;
1295
1296    case OPT_fsched_stalled_insns_dep_:
1297      flag_sched_stalled_insns_dep = value;
1298      break;
1299
1300    case OPT_fstack_limit:
1301      /* The real switch is -fno-stack-limit.  */
1302      if (value)
1303	return 0;
1304      stack_limit_rtx = NULL_RTX;
1305      break;
1306
1307    case OPT_fstack_limit_register_:
1308      {
1309	int reg = decode_reg_name (arg);
1310	if (reg < 0)
1311	  error ("unrecognized register name \"%s\"", arg);
1312	else
1313	  stack_limit_rtx = gen_rtx_REG (Pmode, reg);
1314      }
1315      break;
1316
1317    case OPT_fstack_limit_symbol_:
1318      stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
1319      break;
1320
1321    case OPT_ftree_vectorizer_verbose_:
1322      vect_set_verbosity_level (arg);
1323      break;
1324
1325    case OPT_ftls_model_:
1326      if (!strcmp (arg, "global-dynamic"))
1327	flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
1328      else if (!strcmp (arg, "local-dynamic"))
1329	flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
1330      else if (!strcmp (arg, "initial-exec"))
1331	flag_tls_default = TLS_MODEL_INITIAL_EXEC;
1332      else if (!strcmp (arg, "local-exec"))
1333	flag_tls_default = TLS_MODEL_LOCAL_EXEC;
1334      else
1335	warning (0, "unknown tls-model \"%s\"", arg);
1336      break;
1337
1338    case OPT_ftracer:
1339      flag_tracer_set = true;
1340      break;
1341
1342    case OPT_funroll_loops:
1343      flag_unroll_loops_set = true;
1344      break;
1345
1346    case OPT_g:
1347      set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg);
1348      break;
1349
1350    case OPT_gcoff:
1351      set_debug_level (SDB_DEBUG, false, arg);
1352      break;
1353
1354    case OPT_gdwarf_2:
1355      set_debug_level (DWARF2_DEBUG, false, arg);
1356      break;
1357
1358    case OPT_ggdb:
1359      set_debug_level (NO_DEBUG, 2, arg);
1360      break;
1361
1362    case OPT_gstabs:
1363    case OPT_gstabs_:
1364      set_debug_level (DBX_DEBUG, code == OPT_gstabs_, arg);
1365      break;
1366
1367    case OPT_gvms:
1368      set_debug_level (VMS_DEBUG, false, arg);
1369      break;
1370
1371    case OPT_gxcoff:
1372    case OPT_gxcoff_:
1373      set_debug_level (XCOFF_DEBUG, code == OPT_gxcoff_, arg);
1374      break;
1375
1376    case OPT_o:
1377      asm_file_name = arg;
1378      break;
1379
1380    case OPT_pedantic_errors:
1381      flag_pedantic_errors = pedantic = 1;
1382      break;
1383
1384    case OPT_fforce_mem:
1385      warning (0, "-f[no-]force-mem is nop and option will be removed in 4.3");
1386      break;
1387
1388    case OPT_floop_optimize:
1389    case OPT_frerun_loop_opt:
1390    case OPT_fstrength_reduce:
1391      /* These are no-ops, preserved for backward compatibility.  */
1392      break;
1393
1394    default:
1395      /* If the flag was handled in a standard way, assume the lack of
1396	 processing here is intentional.  */
1397      gcc_assert (cl_options[scode].flag_var);
1398      break;
1399    }
1400
1401  return 1;
1402}
1403
1404/* Handle --param NAME=VALUE.  */
1405static void
1406handle_param (const char *carg)
1407{
1408  char *equal, *arg;
1409  int value;
1410
1411  arg = xstrdup (carg);
1412  equal = strchr (arg, '=');
1413  if (!equal)
1414    error ("%s: --param arguments should be of the form NAME=VALUE", arg);
1415  else
1416    {
1417      value = integral_argument (equal + 1);
1418      if (value == -1)
1419	error ("invalid --param value %qs", equal + 1);
1420      else
1421	{
1422	  *equal = '\0';
1423	  set_param_value (arg, value);
1424	}
1425    }
1426
1427  free (arg);
1428}
1429
1430/* Handle -W and -Wextra.  */
1431static void
1432set_Wextra (int setting)
1433{
1434  extra_warnings = setting;
1435  warn_unused_value = setting;
1436  warn_unused_parameter = (setting && maybe_warn_unused_parameter);
1437
1438  /* We save the value of warn_uninitialized, since if they put
1439     -Wuninitialized on the command line, we need to generate a
1440     warning about not using it without also specifying -O.  */
1441  if (setting == 0)
1442    warn_uninitialized = 0;
1443  else if (warn_uninitialized != 1)
1444    warn_uninitialized = 2;
1445}
1446
1447/* Initialize unused warning flags.  */
1448void
1449set_Wunused (int setting)
1450{
1451  warn_unused_function = setting;
1452  warn_unused_label = setting;
1453  /* Unused function parameter warnings are reported when either
1454     ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
1455     Thus, if -Wextra has already been seen, set warn_unused_parameter;
1456     otherwise set maybe_warn_extra_parameter, which will be picked up
1457     by set_Wextra.  */
1458  maybe_warn_unused_parameter = setting;
1459  warn_unused_parameter = (setting && extra_warnings);
1460  warn_unused_variable = setting;
1461  warn_unused_value = setting;
1462}
1463
1464/* Used to set the level of strict aliasing warnings,
1465   when no level is specified (i.e., when -Wstrict-aliasing, and not
1466   -Wstrict-aliasing=level was given).
1467   ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified,
1468   and 0 otherwise.  After calling this function, wstrict_aliasing will be
1469   set to the default value of -Wstrict_aliasing=level, currently 3.  */
1470void
1471set_warn_strict_aliasing (int onoff)
1472{
1473  gcc_assert (onoff == 0 || onoff == 1);
1474  if (onoff != 0)
1475    warn_strict_aliasing = 3;
1476}
1477
1478/* The following routines are useful in setting all the flags that
1479   -ffast-math and -fno-fast-math imply.  */
1480void
1481set_fast_math_flags (int set)
1482{
1483  flag_trapping_math = !set;
1484  flag_unsafe_math_optimizations = set;
1485  flag_finite_math_only = set;
1486  flag_errno_math = !set;
1487  if (set)
1488    {
1489      flag_signaling_nans = 0;
1490      flag_rounding_math = 0;
1491      flag_cx_limited_range = 1;
1492    }
1493}
1494
1495/* Return true iff flags are set as if -ffast-math.  */
1496bool
1497fast_math_flags_set_p (void)
1498{
1499  return (!flag_trapping_math
1500	  && flag_unsafe_math_optimizations
1501	  && flag_finite_math_only
1502	  && !flag_errno_math);
1503}
1504
1505/* Handle a debug output -g switch.  EXTENDED is true or false to support
1506   extended output (2 is special and means "-ggdb" was given).  */
1507static void
1508set_debug_level (enum debug_info_type type, int extended, const char *arg)
1509{
1510  static bool type_explicit;
1511
1512  use_gnu_debug_info_extensions = extended;
1513
1514  if (type == NO_DEBUG)
1515    {
1516      if (write_symbols == NO_DEBUG)
1517	{
1518	  write_symbols = PREFERRED_DEBUGGING_TYPE;
1519
1520	  if (extended == 2)
1521	    {
1522#ifdef DWARF2_DEBUGGING_INFO
1523	      write_symbols = DWARF2_DEBUG;
1524#elif defined DBX_DEBUGGING_INFO
1525	      write_symbols = DBX_DEBUG;
1526#endif
1527	    }
1528
1529	  if (write_symbols == NO_DEBUG)
1530	    warning (0, "target system does not support debug output");
1531	}
1532    }
1533  else
1534    {
1535      /* Does it conflict with an already selected type?  */
1536      if (type_explicit && write_symbols != NO_DEBUG && type != write_symbols)
1537	error ("debug format \"%s\" conflicts with prior selection",
1538	       debug_type_names[type]);
1539      write_symbols = type;
1540      type_explicit = true;
1541    }
1542
1543  /* A debug flag without a level defaults to level 2.  */
1544  if (*arg == '\0')
1545    {
1546      if (!debug_info_level)
1547	debug_info_level = 2;
1548    }
1549  else
1550    {
1551      debug_info_level = integral_argument (arg);
1552      if (debug_info_level == (unsigned int) -1)
1553	error ("unrecognised debug output level \"%s\"", arg);
1554      else if (debug_info_level > 3)
1555	error ("debug output level %s is too high", arg);
1556    }
1557}
1558
1559/* Display help for target options.  */
1560static void
1561print_target_help (void)
1562{
1563  unsigned int i;
1564  static bool displayed = false;
1565
1566  /* Avoid double printing for --help --target-help.  */
1567  if (displayed)
1568    return;
1569
1570  displayed = true;
1571  for (i = 0; i < cl_options_count; i++)
1572    if ((cl_options[i].flags & (CL_TARGET | CL_UNDOCUMENTED)) == CL_TARGET)
1573      {
1574	printf (_("\nTarget specific options:\n"));
1575	print_filtered_help (CL_TARGET);
1576	break;
1577      }
1578}
1579
1580/* Output --help text.  */
1581static void
1582print_help (void)
1583{
1584  size_t i;
1585  const char *p;
1586
1587  GET_ENVIRONMENT (p, "COLUMNS");
1588  if (p)
1589    {
1590      int value = atoi (p);
1591      if (value > 0)
1592	columns = value;
1593    }
1594
1595  puts (_("The following options are language-independent:\n"));
1596
1597  print_filtered_help (CL_COMMON);
1598  print_param_help ();
1599
1600  for (i = 0; lang_names[i]; i++)
1601    {
1602      printf (_("The %s front end recognizes the following options:\n\n"),
1603	      lang_names[i]);
1604      print_filtered_help (1U << i);
1605    }
1606  print_target_help ();
1607}
1608
1609/* Print the help for --param.  */
1610static void
1611print_param_help (void)
1612{
1613  size_t i;
1614
1615  puts (_("The --param option recognizes the following as parameters:\n"));
1616
1617  for (i = 0; i < LAST_PARAM; i++)
1618    {
1619      const char *help = compiler_params[i].help;
1620      const char *param = compiler_params[i].option;
1621
1622      if (help == NULL || *help == '\0')
1623	help = undocumented_msg;
1624
1625      /* Get the translation.  */
1626      help = _(help);
1627
1628      wrap_help (help, param, strlen (param));
1629    }
1630
1631  putchar ('\n');
1632}
1633
1634/* Print help for a specific front-end, etc.  */
1635static void
1636print_filtered_help (unsigned int flag)
1637{
1638  unsigned int i, len, filter, indent = 0;
1639  bool duplicates = false;
1640  const char *help, *opt, *tab;
1641  static char *printed;
1642
1643  if (flag == CL_COMMON || flag == CL_TARGET)
1644    {
1645      filter = flag;
1646      if (!printed)
1647	printed = xmalloc (cl_options_count);
1648      memset (printed, 0, cl_options_count);
1649    }
1650  else
1651    {
1652      /* Don't print COMMON options twice.  */
1653      filter = flag | CL_COMMON;
1654
1655      for (i = 0; i < cl_options_count; i++)
1656	{
1657	  if ((cl_options[i].flags & filter) != flag)
1658	    continue;
1659
1660	  /* Skip help for internal switches.  */
1661	  if (cl_options[i].flags & CL_UNDOCUMENTED)
1662	    continue;
1663
1664	  /* Skip switches that have already been printed, mark them to be
1665	     listed later.  */
1666	  if (printed[i])
1667	    {
1668	      duplicates = true;
1669	      indent = print_switch (cl_options[i].opt_text, indent);
1670	    }
1671	}
1672
1673      if (duplicates)
1674	{
1675	  putchar ('\n');
1676	  putchar ('\n');
1677	}
1678    }
1679
1680  for (i = 0; i < cl_options_count; i++)
1681    {
1682      if ((cl_options[i].flags & filter) != flag)
1683	continue;
1684
1685      /* Skip help for internal switches.  */
1686      if (cl_options[i].flags & CL_UNDOCUMENTED)
1687	continue;
1688
1689      /* Skip switches that have already been printed.  */
1690      if (printed[i])
1691	continue;
1692
1693      printed[i] = true;
1694
1695      help = cl_options[i].help;
1696      if (!help)
1697	help = undocumented_msg;
1698
1699      /* Get the translation.  */
1700      help = _(help);
1701
1702      tab = strchr (help, '\t');
1703      if (tab)
1704	{
1705	  len = tab - help;
1706	  opt = help;
1707	  help = tab + 1;
1708	}
1709      else
1710	{
1711	  opt = cl_options[i].opt_text;
1712	  len = strlen (opt);
1713	}
1714
1715      wrap_help (help, opt, len);
1716    }
1717
1718  putchar ('\n');
1719}
1720
1721/* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
1722   word-wrapped HELP in a second column.  */
1723static unsigned int
1724print_switch (const char *text, unsigned int indent)
1725{
1726  unsigned int len = strlen (text) + 1; /* trailing comma */
1727
1728  if (indent)
1729    {
1730      putchar (',');
1731      if (indent + len > columns)
1732	{
1733	  putchar ('\n');
1734	  putchar (' ');
1735	  indent = 1;
1736	}
1737    }
1738  else
1739    putchar (' ');
1740
1741  putchar (' ');
1742  fputs (text, stdout);
1743
1744  return indent + len + 1;
1745}
1746
1747/* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
1748   word-wrapped HELP in a second column.  */
1749static void
1750wrap_help (const char *help, const char *item, unsigned int item_width)
1751{
1752  unsigned int col_width = 27;
1753  unsigned int remaining, room, len;
1754
1755  remaining = strlen (help);
1756
1757  do
1758    {
1759      room = columns - 3 - MAX (col_width, item_width);
1760      if (room > columns)
1761	room = 0;
1762      len = remaining;
1763
1764      if (room < len)
1765	{
1766	  unsigned int i;
1767
1768	  for (i = 0; help[i]; i++)
1769	    {
1770	      if (i >= room && len != remaining)
1771		break;
1772	      if (help[i] == ' ')
1773		len = i;
1774	      else if ((help[i] == '-' || help[i] == '/')
1775		       && help[i + 1] != ' '
1776		       && i > 0 && ISALPHA (help[i - 1]))
1777		len = i + 1;
1778	    }
1779	}
1780
1781      printf( "  %-*.*s %.*s\n", col_width, item_width, item, len, help);
1782      item_width = 0;
1783      while (help[len] == ' ')
1784	len++;
1785      help += len;
1786      remaining -= len;
1787    }
1788  while (remaining);
1789}
1790
1791/* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
1792   a simple on-off switch.  */
1793
1794int
1795option_enabled (int opt_idx)
1796{
1797  const struct cl_option *option = &(cl_options[opt_idx]);
1798  if (option->flag_var)
1799    switch (option->var_type)
1800      {
1801      case CLVC_BOOLEAN:
1802	return *(int *) option->flag_var != 0;
1803
1804      case CLVC_EQUAL:
1805	return *(int *) option->flag_var == option->var_value;
1806
1807      case CLVC_BIT_CLEAR:
1808	return (*(int *) option->flag_var & option->var_value) == 0;
1809
1810      case CLVC_BIT_SET:
1811	return (*(int *) option->flag_var & option->var_value) != 0;
1812
1813      case CLVC_STRING:
1814	break;
1815      }
1816  return -1;
1817}
1818
1819/* Fill STATE with the current state of option OPTION.  Return true if
1820   there is some state to store.  */
1821
1822bool
1823get_option_state (int option, struct cl_option_state *state)
1824{
1825  if (cl_options[option].flag_var == 0)
1826    return false;
1827
1828  switch (cl_options[option].var_type)
1829    {
1830    case CLVC_BOOLEAN:
1831    case CLVC_EQUAL:
1832      state->data = cl_options[option].flag_var;
1833      state->size = sizeof (int);
1834      break;
1835
1836    case CLVC_BIT_CLEAR:
1837    case CLVC_BIT_SET:
1838      state->ch = option_enabled (option);
1839      state->data = &state->ch;
1840      state->size = 1;
1841      break;
1842
1843    case CLVC_STRING:
1844      state->data = *(const char **) cl_options[option].flag_var;
1845      if (state->data == 0)
1846	state->data = "";
1847      state->size = strlen (state->data) + 1;
1848      break;
1849    }
1850  return true;
1851}
1852