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#include "config.h"
24#include "system.h"
25#include "intl.h"
26#include "coretypes.h"
27#include "tm.h"
28#include "tree.h"
29#include "rtl.h"
30#include "ggc.h"
31#include "output.h"
32#include "langhooks.h"
33#include "opts.h"
34#include "options.h"
35#include "flags.h"
36#include "toplev.h"
37#include "params.h"
38#include "diagnostic.h"
39#include "tm_p.h"		/* For OPTIMIZATION_OPTIONS.  */
40#include "insn-attr.h"		/* For INSN_SCHEDULING.  */
41#include "target.h"
42#include "tree-pass.h"
43
44/* Value of the -G xx switch, and whether it was passed or not.  */
45unsigned HOST_WIDE_INT g_switch_value;
46bool g_switch_set;
47
48/* True if we should exit after parsing options.  */
49bool exit_after_options;
50
51/* Print various extra warnings.  -W/-Wextra.  */
52bool extra_warnings;
53
54/* True to warn about any objects definitions whose size is larger
55   than N bytes.  Also want about function definitions whose returned
56   values are larger than N bytes, where N is `larger_than_size'.  */
57bool warn_larger_than;
58HOST_WIDE_INT larger_than_size;
59
60/* Nonzero means warn about any function whose stack usage is larger
61   than N bytes.  The value N is in `stack_larger_than_size'.  */
62int warn_stack_larger_than;
63HOST_WIDE_INT stack_larger_than_size;
64
65/* Nonzero means warn about constructs which might not be
66   strict-aliasing safe.  */
67int warn_strict_aliasing;
68
69/* Nonzero means warn about optimizations which rely on undefined
70   signed overflow.  */
71int warn_strict_overflow;
72
73/* Hack for cooperation between set_Wunused and set_Wextra.  */
74static bool maybe_warn_unused_parameter;
75
76/* Type(s) of debugging information we are producing (if any).  See
77   flags.h for the definitions of the different possible types of
78   debugging information.  */
79enum debug_info_type write_symbols = NO_DEBUG;
80
81/* Level of debugging information we are producing.  See flags.h for
82   the definitions of the different possible levels.  */
83enum debug_info_level debug_info_level = DINFO_LEVEL_NONE;
84
85/* Nonzero means use GNU-only extensions in the generated symbolic
86   debugging information.  Currently, this only has an effect when
87   write_symbols is set to DBX_DEBUG, XCOFF_DEBUG, or DWARF_DEBUG.  */
88bool use_gnu_debug_info_extensions;
89
90/* The default visibility for all symbols (unless overridden) */
91enum symbol_visibility default_visibility = VISIBILITY_DEFAULT;
92
93/* Disable unit-at-a-time for frontends that might be still broken in this
94   respect.  */
95
96bool no_unit_at_a_time_default;
97
98/* Global visibility options.  */
99struct visibility_flags visibility_options;
100
101/* Columns of --help display.  */
102static unsigned int columns = 80;
103
104/* What to print when a switch has no documentation.  */
105static const char undocumented_msg[] = N_("This switch lacks documentation");
106
107/* Used for bookkeeping on whether user set these flags so
108   -fprofile-use/-fprofile-generate does not use them.  */
109static bool profile_arc_flag_set, flag_profile_values_set;
110static bool flag_unroll_loops_set, flag_tracer_set;
111static bool flag_value_profile_transformations_set;
112static bool flag_peel_loops_set, flag_branch_probabilities_set;
113
114/* Input file names.  */
115const char **in_fnames;
116unsigned num_in_fnames;
117
118static int common_handle_option (size_t scode, const char *arg, int value,
119				 unsigned int lang_mask);
120static void handle_param (const char *);
121static void set_Wextra (int);
122static unsigned int handle_option (const char **argv, unsigned int lang_mask);
123static char *write_langs (unsigned int lang_mask);
124static void complain_wrong_lang (const char *, const struct cl_option *,
125				 unsigned int lang_mask);
126static void handle_options (unsigned int, const char **, unsigned int);
127static void wrap_help (const char *help, const char *item, unsigned int);
128static void print_target_help (void);
129static void print_help (void);
130static void print_param_help (void);
131static void print_filtered_help (unsigned int);
132static unsigned int print_switch (const char *text, unsigned int indent);
133static void set_debug_level (enum debug_info_type type, int extended,
134			     const char *arg);
135
136/* If ARG is a non-negative integer made up solely of digits, return its
137   value, otherwise return -1.  */
138static int
139integral_argument (const char *arg)
140{
141  const char *p = arg;
142
143  while (*p && ISDIGIT (*p))
144    p++;
145
146  if (*p == '\0')
147    return atoi (arg);
148
149  return -1;
150}
151
152/* Return a malloced slash-separated list of languages in MASK.  */
153static char *
154write_langs (unsigned int mask)
155{
156  unsigned int n = 0, len = 0;
157  const char *lang_name;
158  char *result;
159
160  for (n = 0; (lang_name = lang_names[n]) != 0; n++)
161    if (mask & (1U << n))
162      len += strlen (lang_name) + 1;
163
164  result = XNEWVEC (char, len);
165  len = 0;
166  for (n = 0; (lang_name = lang_names[n]) != 0; n++)
167    if (mask & (1U << n))
168      {
169	if (len)
170	  result[len++] = '/';
171	strcpy (result + len, lang_name);
172	len += strlen (lang_name);
173      }
174
175  result[len] = 0;
176
177  return result;
178}
179
180/* Complain that switch OPT_INDEX does not apply to this front end.  */
181static void
182complain_wrong_lang (const char *text, const struct cl_option *option,
183		     unsigned int lang_mask)
184{
185  char *ok_langs, *bad_lang;
186
187  ok_langs = write_langs (option->flags);
188  bad_lang = write_langs (lang_mask);
189
190  /* Eventually this should become a hard error IMO.  */
191  warning (0, "command line option \"%s\" is valid for %s but not for %s",
192	   text, ok_langs, bad_lang);
193
194  free (ok_langs);
195  free (bad_lang);
196}
197
198static const char *bad_option = NULL;
199
200void
201late_options_error (void)
202{
203  if (bad_option)
204    {
205      input_location = unknown_location;
206      error ("unrecognized command line option \"%s\"", bad_option);
207    }
208}
209
210/* Handle the switch beginning at ARGV for the language indicated by
211   LANG_MASK.  Returns the number of switches consumed.  */
212static unsigned int
213handle_option (const char **argv, unsigned int lang_mask)
214{
215  size_t opt_index;
216  const char *opt, *arg = 0;
217  char *dup = 0;
218  int value = 1;
219  unsigned int result = 0;
220  const struct cl_option *option;
221
222  opt = argv[0];
223
224  opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
225  if (opt_index == cl_options_count
226      && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
227      && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
228    {
229      /* Drop the "no-" from negative switches.  */
230      size_t len = strlen (opt) - 3;
231
232      dup = XNEWVEC (char, len + 1);
233      dup[0] = '-';
234      dup[1] = opt[1];
235      memcpy (dup + 2, opt + 5, len - 2 + 1);
236      opt = dup;
237      value = 0;
238      opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
239    }
240
241  if (opt_index == cl_options_count)
242    {
243      /* ignore unknown -Wno-* options */
244      if (value == 0 && opt[1] == 'W')
245	{
246	  bad_option = argv[0];
247	  result = 1;
248	}
249      goto done;
250    }
251
252  option = &cl_options[opt_index];
253
254  /* Reject negative form of switches that don't take negatives as
255     unrecognized.  */
256  if (!value && (option->flags & CL_REJECT_NEGATIVE))
257    goto done;
258
259  /* We've recognized this switch.  */
260  result = 1;
261
262  /* Check to see if the option is disabled for this configuration.  */
263  if (option->flags & CL_DISABLED)
264    {
265      error ("command line option %qs"
266	     " is not supported by this configuration", opt);
267      goto done;
268    }
269
270  /* Sort out any argument the switch takes.  */
271  if (option->flags & CL_JOINED)
272    {
273      /* Have arg point to the original switch.  This is because
274	 some code, such as disable_builtin_function, expects its
275	 argument to be persistent until the program exits.  */
276      arg = argv[0] + cl_options[opt_index].opt_len + 1;
277      if (!value)
278	arg += strlen ("no-");
279
280      if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
281	{
282	  if (option->flags & CL_SEPARATE)
283	    {
284	      arg = argv[1];
285	      result = 2;
286	    }
287	  else
288	    /* Missing argument.  */
289	    arg = NULL;
290	}
291    }
292  else if (option->flags & CL_SEPARATE)
293    {
294      arg = argv[1];
295      result = 2;
296    }
297
298  /* Now we've swallowed any potential argument, complain if this
299     is a switch for a different front end.  */
300  if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
301    {
302      complain_wrong_lang (argv[0], option, lang_mask);
303      goto done;
304    }
305
306  if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
307    {
308      if (!lang_hooks.missing_argument (opt, opt_index))
309	error ("missing argument to \"%s\"", opt);
310      goto done;
311    }
312
313  /* If the switch takes an integer, convert it.  */
314  if (arg && (option->flags & CL_UINTEGER))
315    {
316      value = integral_argument (arg);
317      if (value == -1)
318	{
319	  error ("argument to \"%s\" should be a non-negative integer",
320		 option->opt_text);
321	  goto done;
322	}
323    }
324
325  if (option->flag_var)
326    switch (option->var_type)
327      {
328      case CLVC_BOOLEAN:
329	*(int *) option->flag_var = value;
330	break;
331
332      case CLVC_EQUAL:
333	*(int *) option->flag_var = (value
334				     ? option->var_value
335				     : !option->var_value);
336	break;
337
338      case CLVC_BIT_CLEAR:
339      case CLVC_BIT_SET:
340	if ((value != 0) == (option->var_type == CLVC_BIT_SET))
341	  *(int *) option->flag_var |= option->var_value;
342	else
343	  *(int *) option->flag_var &= ~option->var_value;
344	if (option->flag_var == &target_flags)
345	  target_flags_explicit |= option->var_value;
346	break;
347
348      case CLVC_STRING:
349	*(const char **) option->flag_var = arg;
350	break;
351      }
352
353  if (option->flags & lang_mask)
354    if (lang_hooks.handle_option (opt_index, arg, value) == 0)
355      result = 0;
356
357  if (result && (option->flags & CL_COMMON))
358    if (common_handle_option (opt_index, arg, value, lang_mask) == 0)
359      result = 0;
360
361  if (result && (option->flags & CL_TARGET))
362    if (!targetm.handle_option (opt_index, arg, value))
363      result = 0;
364
365 done:
366  if (dup)
367    free (dup);
368  return result;
369}
370
371/* Handle FILENAME from the command line.  */
372static void
373add_input_filename (const char *filename)
374{
375  num_in_fnames++;
376  in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
377  in_fnames[num_in_fnames - 1] = filename;
378}
379
380/* Decode and handle the vector of command line options.  LANG_MASK
381   contains has a single bit set representing the current
382   language.  */
383static void
384handle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
385{
386  unsigned int n, i;
387
388  for (i = 1; i < argc; i += n)
389    {
390      const char *opt = argv[i];
391
392      /* Interpret "-" or a non-switch as a file name.  */
393      if (opt[0] != '-' || opt[1] == '\0')
394	{
395	  if (main_input_filename == NULL)
396	    main_input_filename = opt;
397	  add_input_filename (opt);
398	  n = 1;
399	  continue;
400	}
401
402      n = handle_option (argv + i, lang_mask);
403
404      if (!n)
405	{
406	  n = 1;
407	  error ("unrecognized command line option \"%s\"", opt);
408	}
409    }
410}
411
412/* Parse command line options and set default flag values.  Do minimal
413   options processing.  */
414void
415decode_options (unsigned int argc, const char **argv)
416{
417  unsigned int i, lang_mask;
418
419  /* Perform language-specific options initialization.  */
420  lang_mask = lang_hooks.init_options (argc, argv);
421
422  lang_hooks.initialize_diagnostics (global_dc);
423
424  /* Scan to see what optimization level has been specified.  That will
425     determine the default value of many flags.  */
426  for (i = 1; i < argc; i++)
427    {
428      if (!strcmp (argv[i], "-O"))
429	{
430	  optimize = 1;
431	  optimize_size = 0;
432	}
433      else if (argv[i][0] == '-' && argv[i][1] == 'O')
434	{
435	  /* Handle -Os, -O2, -O3, -O69, ...  */
436	  const char *p = &argv[i][2];
437
438	  if ((p[0] == 's' || p[0] == 'z') && (p[1] == 0))
439	    {
440	      optimize_size = 1;
441
442	      /* Optimizing for size forces optimize to be 2.  */
443	      optimize = 2;
444	    }
445	  else
446	    {
447	      const int optimize_val = read_integral_parameter (p, p - 2, -1);
448	      if (optimize_val != -1)
449		{
450		  optimize = optimize_val;
451		  optimize_size = 0;
452		}
453	    }
454	}
455    }
456
457  if (!optimize)
458    {
459      flag_merge_constants = 0;
460    }
461
462  if (optimize >= 1)
463    {
464      flag_defer_pop = 1;
465#ifdef DELAY_SLOTS
466      flag_delayed_branch = 1;
467#endif
468#ifdef CAN_DEBUG_WITHOUT_FP
469      flag_omit_frame_pointer = 1;
470#endif
471      flag_guess_branch_prob = 1;
472      flag_cprop_registers = 1;
473      flag_if_conversion = 1;
474      flag_if_conversion2 = 1;
475      flag_ipa_pure_const = 1;
476      flag_ipa_reference = 1;
477      flag_tree_ccp = 1;
478      flag_tree_dce = 1;
479      flag_tree_dom = 1;
480      flag_tree_dse = 1;
481      flag_tree_ter = 1;
482      flag_tree_live_range_split = 1;
483      flag_tree_sra = 1;
484      flag_tree_copyrename = 1;
485      flag_tree_fre = 1;
486      flag_tree_copy_prop = 1;
487      flag_tree_sink = 1;
488      flag_tree_salias = 1;
489      if (!no_unit_at_a_time_default)
490        flag_unit_at_a_time = 1;
491
492      if (!optimize_size)
493	{
494	  /* Loop header copying usually increases size of the code.  This used
495	     not to be true, since quite often it is possible to verify that
496	     the condition is satisfied in the first iteration and therefore
497	     to eliminate it.  Jump threading handles these cases now.  */
498	  flag_tree_ch = 1;
499	}
500    }
501
502  if (optimize >= 2)
503    {
504      flag_thread_jumps = 1;
505      flag_crossjumping = 1;
506      flag_optimize_sibling_calls = 1;
507      flag_cse_follow_jumps = 1;
508      flag_cse_skip_blocks = 1;
509      flag_gcse = 1;
510      flag_expensive_optimizations = 1;
511      flag_ipa_type_escape = 1;
512      flag_rerun_cse_after_loop = 1;
513      flag_caller_saves = 1;
514      flag_peephole2 = 1;
515#ifdef INSN_SCHEDULING
516      flag_schedule_insns = 1;
517      flag_schedule_insns_after_reload = 1;
518#endif
519      flag_regmove = 1;
520#if !defined(OPENBSD_NATIVE) && !defined(OPENBSD_CROSS)
521      flag_strict_overflow = 1;
522      flag_delete_null_pointer_checks = 1;
523#endif
524      flag_reorder_blocks = 1;
525      flag_reorder_functions = 1;
526      flag_tree_store_ccp = 1;
527      flag_tree_store_copy_prop = 1;
528
529      if (!optimize_size)
530	{
531          /* PRE tends to generate bigger code.  */
532          flag_tree_pre = 1;
533	}
534    }
535
536  if (optimize >= 3)
537    {
538      flag_strict_aliasing = 1;
539      flag_inline_functions = 1;
540      flag_unswitch_loops = 1;
541      flag_gcse_after_reload = 1;
542    }
543
544  if (optimize < 2 || optimize_size)
545    {
546      align_loops = 1;
547      align_jumps = 1;
548      align_labels = 1;
549      align_functions = 1;
550
551      /* Don't reorder blocks when optimizing for size because extra
552	 jump insns may be created; also barrier may create extra padding.
553
554	 More correctly we should have a block reordering mode that tried
555	 to minimize the combined size of all the jumps.  This would more
556	 or less automatically remove extra jumps, but would also try to
557	 use more short jumps instead of long jumps.  */
558      flag_reorder_blocks = 0;
559      flag_reorder_blocks_and_partition = 0;
560    }
561
562  if (optimize_size)
563    {
564      /* Inlining of very small functions usually reduces total size.  */
565      set_param_value ("max-inline-insns-single", 5);
566      set_param_value ("max-inline-insns-auto", 5);
567      flag_inline_functions = 1;
568
569      /* We want to crossjump as much as possible.  */
570      set_param_value ("min-crossjump-insns", 1);
571    }
572
573  /* Initialize whether `char' is signed.  */
574  flag_signed_char = DEFAULT_SIGNED_CHAR;
575  /* Set this to a special "uninitialized" value.  The actual default is set
576     after target options have been processed.  */
577  flag_short_enums = 2;
578
579  /* Initialize target_flags before OPTIMIZATION_OPTIONS so the latter can
580     modify it.  */
581  target_flags = targetm.default_target_flags;
582
583  /* Some tagets have ABI-specified unwind tables.  */
584  flag_unwind_tables = targetm.unwind_tables_default;
585
586#ifdef OPTIMIZATION_OPTIONS
587  /* Allow default optimizations to be specified on a per-machine basis.  */
588  OPTIMIZATION_OPTIONS (optimize, optimize_size);
589#endif
590
591  handle_options (argc, argv, lang_mask);
592
593  if (flag_pic || profile_flag)
594    flag_pie = 0;
595  if (flag_pie)
596    flag_pic = flag_pie;
597  if (flag_pic && !flag_pie)
598    flag_shlib = 1;
599
600  if (flag_no_inline == 2)
601    flag_no_inline = 0;
602  else
603    flag_really_no_inline = flag_no_inline;
604
605  /* Set flag_no_inline before the post_options () hook.  The C front
606     ends use it to determine tree inlining defaults.  FIXME: such
607     code should be lang-independent when all front ends use tree
608     inlining, in which case it, and this condition, should be moved
609     to the top of process_options() instead.  */
610  if (optimize == 0)
611    {
612      /* Inlining does not work if not optimizing,
613	 so force it not to be done.  */
614      flag_no_inline = 1;
615      warn_inline = 0;
616
617      /* The c_decode_option function and decode_option hook set
618	 this to `2' if -Wall is used, so we can avoid giving out
619	 lots of errors for people who don't realize what -Wall does.  */
620      if (warn_uninitialized == 1)
621	warning (OPT_Wuninitialized,
622		 "-Wuninitialized is not supported without -O");
623    }
624
625  if (flag_really_no_inline == 2)
626    flag_really_no_inline = flag_no_inline;
627
628  /* The optimization to partition hot and cold basic blocks into separate
629     sections of the .o and executable files does not work (currently)
630     with exception handling.  This is because there is no support for
631     generating unwind info.  If flag_exceptions is turned on we need to
632     turn off the partitioning optimization.  */
633
634  if (flag_exceptions && flag_reorder_blocks_and_partition)
635    {
636      inform
637	    ("-freorder-blocks-and-partition does not work with exceptions");
638      flag_reorder_blocks_and_partition = 0;
639      flag_reorder_blocks = 1;
640    }
641
642  /* If user requested unwind info, then turn off the partitioning
643     optimization.  */
644
645  if (flag_unwind_tables && ! targetm.unwind_tables_default
646      && flag_reorder_blocks_and_partition)
647    {
648      inform ("-freorder-blocks-and-partition does not support unwind info");
649      flag_reorder_blocks_and_partition = 0;
650      flag_reorder_blocks = 1;
651    }
652
653  /* If the target requested unwind info, then turn off the partitioning
654     optimization with a different message.  Likewise, if the target does not
655     support named sections.  */
656
657  if (flag_reorder_blocks_and_partition
658      && (!targetm.have_named_sections
659	  || (flag_unwind_tables && targetm.unwind_tables_default)))
660    {
661      inform
662       ("-freorder-blocks-and-partition does not work on this architecture");
663      flag_reorder_blocks_and_partition = 0;
664      flag_reorder_blocks = 1;
665    }
666}
667
668/* Handle target- and language-independent options.  Return zero to
669   generate an "unknown option" message.  Only options that need
670   extra handling need to be listed here; if you simply want
671   VALUE assigned to a variable, it happens automatically.  */
672
673static int
674common_handle_option (size_t scode, const char *arg, int value,
675		      unsigned int lang_mask)
676{
677  enum opt_code code = (enum opt_code) scode;
678
679  switch (code)
680    {
681    case OPT__help:
682      print_help ();
683      exit_after_options = true;
684      break;
685
686    case OPT__param:
687      handle_param (arg);
688      break;
689
690    case OPT__target_help:
691      print_target_help ();
692      exit_after_options = true;
693      break;
694
695    case OPT__version:
696      print_version (stderr, "");
697      exit_after_options = true;
698      break;
699
700    case OPT_G:
701      g_switch_value = value;
702      g_switch_set = true;
703      break;
704
705    case OPT_O:
706    case OPT_Os:
707      /* Currently handled in a prescan.  */
708      break;
709
710    case OPT_W:
711      /* For backward compatibility, -W is the same as -Wextra.  */
712      set_Wextra (value);
713      break;
714
715    case OPT_Werror_:
716      {
717	char *new_option;
718	int option_index;
719	new_option = XNEWVEC (char, strlen (arg) + 2);
720	new_option[0] = 'W';
721	strcpy (new_option+1, arg);
722	option_index = find_opt (new_option, lang_mask);
723	if (option_index == N_OPTS)
724	  {
725	    error ("-Werror=%s: No option -%s", arg, new_option);
726	  }
727	else
728	  {
729	    int kind = value ? DK_ERROR : DK_WARNING;
730	    diagnostic_classify_diagnostic (global_dc, option_index, kind);
731
732	    /* -Werror=foo implies -Wfoo.  */
733	    if (cl_options[option_index].var_type == CLVC_BOOLEAN
734		&& cl_options[option_index].flag_var
735		&& kind == DK_ERROR)
736	      *(int *) cl_options[option_index].flag_var = 1;
737	    free (new_option);
738	  }
739      }
740      break;
741
742    case OPT_Wextra:
743      set_Wextra (value);
744      break;
745
746    case OPT_Wlarger_than_:
747      larger_than_size = value;
748      warn_larger_than = value != -1;
749      break;
750
751    case OPT_Wframe_larger_than_:
752    case OPT_Wstack_larger_than_:
753      stack_larger_than_size = value;
754      warn_stack_larger_than = stack_larger_than_size != -1;
755      break;
756
757    case OPT_Wstrict_aliasing:
758    case OPT_Wstrict_aliasing_:
759      warn_strict_aliasing = value;
760      break;
761
762    case OPT_Wstrict_overflow:
763      warn_strict_overflow = (value
764			      ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
765			      : 0);
766      break;
767
768    case OPT_Wstrict_overflow_:
769      warn_strict_overflow = value;
770      break;
771
772    case OPT_Wunused:
773      set_Wunused (value);
774      break;
775
776    case OPT_aux_info:
777    case OPT_aux_info_:
778      aux_info_file_name = arg;
779      flag_gen_aux_info = 1;
780      break;
781
782    case OPT_auxbase:
783      aux_base_name = arg;
784      break;
785
786    case OPT_auxbase_strip:
787      {
788	char *tmp = xstrdup (arg);
789	strip_off_ending (tmp, strlen (tmp));
790	if (tmp[0])
791	  aux_base_name = tmp;
792      }
793      break;
794
795    case OPT_d:
796      decode_d_option (arg);
797      break;
798
799    case OPT_dumpbase:
800      dump_base_name = arg;
801      break;
802
803    case OPT_falign_functions_:
804      align_functions = value;
805      break;
806
807    case OPT_falign_jumps_:
808      align_jumps = value;
809      break;
810
811    case OPT_falign_labels_:
812      align_labels = value;
813      break;
814
815    case OPT_falign_loops_:
816      align_loops = value;
817      break;
818
819    case OPT_fbranch_probabilities:
820      flag_branch_probabilities_set = true;
821      break;
822
823    case OPT_fcall_used_:
824      fix_register (arg, 0, 1);
825      break;
826
827    case OPT_fcall_saved_:
828      fix_register (arg, 0, 0);
829      break;
830
831    case OPT_fdiagnostics_show_location_:
832      if (!strcmp (arg, "once"))
833	diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
834      else if (!strcmp (arg, "every-line"))
835	diagnostic_prefixing_rule (global_dc)
836	  = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
837      else
838	return 0;
839      break;
840
841    case OPT_fdiagnostics_show_option:
842      global_dc->show_option_requested = true;
843      break;
844
845    case OPT_fdump_:
846      if (!dump_switch_p (arg))
847	return 0;
848      break;
849
850    case OPT_ffast_math:
851      set_fast_math_flags (value);
852      break;
853
854    case OPT_ffixed_:
855      fix_register (arg, 1, 1);
856      break;
857
858    case OPT_finline_limit_:
859    case OPT_finline_limit_eq:
860      set_param_value ("max-inline-insns-single", value / 2);
861      set_param_value ("max-inline-insns-auto", value / 2);
862      break;
863
864    case OPT_fmessage_length_:
865      pp_set_line_maximum_length (global_dc->printer, value);
866      break;
867
868    case OPT_fpack_struct_:
869      if (value <= 0 || (value & (value - 1)) || value > 16)
870	error("structure alignment must be a small power of two, not %d", value);
871      else
872	{
873	  initial_max_fld_align = value;
874	  maximum_field_alignment = value * BITS_PER_UNIT;
875	}
876      break;
877
878    case OPT_fpeel_loops:
879      flag_peel_loops_set = true;
880      break;
881
882    case OPT_fprofile_arcs:
883      profile_arc_flag_set = true;
884      break;
885
886    case OPT_fprofile_use:
887      if (!flag_branch_probabilities_set)
888        flag_branch_probabilities = value;
889      if (!flag_profile_values_set)
890        flag_profile_values = value;
891      if (!flag_unroll_loops_set)
892        flag_unroll_loops = value;
893      if (!flag_peel_loops_set)
894        flag_peel_loops = value;
895      if (!flag_tracer_set)
896        flag_tracer = value;
897      if (!flag_value_profile_transformations_set)
898        flag_value_profile_transformations = value;
899      break;
900
901    case OPT_fprofile_generate:
902      if (!profile_arc_flag_set)
903        profile_arc_flag = value;
904      if (!flag_profile_values_set)
905        flag_profile_values = value;
906      if (!flag_value_profile_transformations_set)
907        flag_value_profile_transformations = value;
908      break;
909
910    case OPT_fprofile_values:
911      flag_profile_values_set = true;
912      break;
913
914    case OPT_fvisibility_:
915      {
916        if (!strcmp(arg, "default"))
917          default_visibility = VISIBILITY_DEFAULT;
918        else if (!strcmp(arg, "internal"))
919          default_visibility = VISIBILITY_INTERNAL;
920        else if (!strcmp(arg, "hidden"))
921          default_visibility = VISIBILITY_HIDDEN;
922        else if (!strcmp(arg, "protected"))
923          default_visibility = VISIBILITY_PROTECTED;
924        else
925          error ("unrecognized visibility value \"%s\"", arg);
926      }
927      break;
928
929    case OPT_fvpt:
930      flag_value_profile_transformations_set = true;
931      break;
932
933    case OPT_frandom_seed:
934      /* The real switch is -fno-random-seed.  */
935      if (value)
936	return 0;
937      flag_random_seed = NULL;
938      break;
939
940    case OPT_frandom_seed_:
941      flag_random_seed = arg;
942      break;
943
944    case OPT_fsched_verbose_:
945#ifdef INSN_SCHEDULING
946      fix_sched_param ("verbose", arg);
947      break;
948#else
949      return 0;
950#endif
951
952    case OPT_fsched_stalled_insns_:
953      flag_sched_stalled_insns = value;
954      if (flag_sched_stalled_insns == 0)
955	flag_sched_stalled_insns = -1;
956      break;
957
958    case OPT_fsched_stalled_insns_dep_:
959      flag_sched_stalled_insns_dep = value;
960      break;
961
962    case OPT_fstack_limit:
963      /* The real switch is -fno-stack-limit.  */
964      if (value)
965	return 0;
966      stack_limit_rtx = NULL_RTX;
967      break;
968
969    case OPT_fstack_limit_register_:
970      {
971	int reg = decode_reg_name (arg);
972	if (reg < 0)
973	  error ("unrecognized register name \"%s\"", arg);
974	else
975	  stack_limit_rtx = gen_rtx_REG (Pmode, reg);
976      }
977      break;
978
979    case OPT_fstack_limit_symbol_:
980      stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
981      break;
982
983    case OPT_ftree_vectorizer_verbose_:
984      vect_set_verbosity_level (arg);
985      break;
986
987    case OPT_ftls_model_:
988      if (!strcmp (arg, "global-dynamic"))
989	flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
990      else if (!strcmp (arg, "local-dynamic"))
991	flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
992      else if (!strcmp (arg, "initial-exec"))
993	flag_tls_default = TLS_MODEL_INITIAL_EXEC;
994      else if (!strcmp (arg, "local-exec"))
995	flag_tls_default = TLS_MODEL_LOCAL_EXEC;
996      else
997	warning (0, "unknown tls-model \"%s\"", arg);
998      break;
999
1000    case OPT_ftracer:
1001      flag_tracer_set = true;
1002      break;
1003
1004    case OPT_funroll_loops:
1005      flag_unroll_loops_set = true;
1006      break;
1007
1008    case OPT_g:
1009      set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg);
1010      break;
1011
1012    case OPT_gcoff:
1013      set_debug_level (SDB_DEBUG, false, arg);
1014      break;
1015
1016    case OPT_gdwarf_2:
1017      set_debug_level (DWARF2_DEBUG, false, arg);
1018      break;
1019
1020    case OPT_ggdb:
1021      set_debug_level (NO_DEBUG, 2, arg);
1022      break;
1023
1024    case OPT_gstabs:
1025    case OPT_gstabs_:
1026      set_debug_level (DBX_DEBUG, code == OPT_gstabs_, arg);
1027      break;
1028
1029    case OPT_gvms:
1030      set_debug_level (VMS_DEBUG, false, arg);
1031      break;
1032
1033    case OPT_gxcoff:
1034    case OPT_gxcoff_:
1035      set_debug_level (XCOFF_DEBUG, code == OPT_gxcoff_, arg);
1036      break;
1037
1038    case OPT_o:
1039      asm_file_name = arg;
1040      break;
1041
1042    case OPT_pedantic_errors:
1043      flag_pedantic_errors = pedantic = 1;
1044      break;
1045
1046    case OPT_fforce_mem:
1047      warning (0, "-f[no-]force-mem is nop and option will be removed in 4.3");
1048      break;
1049
1050    case OPT_floop_optimize:
1051    case OPT_frerun_loop_opt:
1052    case OPT_fstrength_reduce:
1053      /* These are no-ops, preserved for backward compatibility.  */
1054      break;
1055
1056    default:
1057      /* If the flag was handled in a standard way, assume the lack of
1058	 processing here is intentional.  */
1059      gcc_assert (cl_options[scode].flag_var);
1060      break;
1061    }
1062
1063  return 1;
1064}
1065
1066/* Handle --param NAME=VALUE.  */
1067static void
1068handle_param (const char *carg)
1069{
1070  char *equal, *arg;
1071  int value;
1072
1073  arg = xstrdup (carg);
1074  equal = strchr (arg, '=');
1075  if (!equal)
1076    error ("%s: --param arguments should be of the form NAME=VALUE", arg);
1077  else
1078    {
1079      value = integral_argument (equal + 1);
1080      if (value == -1)
1081	error ("invalid --param value %qs", equal + 1);
1082      else
1083	{
1084	  *equal = '\0';
1085	  set_param_value (arg, value);
1086	}
1087    }
1088
1089  free (arg);
1090}
1091
1092/* Handle -W and -Wextra.  */
1093static void
1094set_Wextra (int setting)
1095{
1096  extra_warnings = setting;
1097  warn_unused_value = setting;
1098  warn_unused_parameter = (setting && maybe_warn_unused_parameter);
1099
1100  /* We save the value of warn_uninitialized, since if they put
1101     -Wuninitialized on the command line, we need to generate a
1102     warning about not using it without also specifying -O.  */
1103  if (setting == 0)
1104    warn_uninitialized = 0;
1105  else if (warn_uninitialized != 1)
1106    warn_uninitialized = 2;
1107}
1108
1109/* Initialize unused warning flags.  */
1110void
1111set_Wunused (int setting)
1112{
1113  warn_unused_function = setting;
1114  warn_unused_label = setting;
1115  /* Unused function parameter warnings are reported when either
1116     ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
1117     Thus, if -Wextra has already been seen, set warn_unused_parameter;
1118     otherwise set maybe_warn_extra_parameter, which will be picked up
1119     by set_Wextra.  */
1120  maybe_warn_unused_parameter = setting;
1121  warn_unused_parameter = (setting && extra_warnings);
1122  warn_unused_variable = setting;
1123  warn_unused_value = setting;
1124}
1125
1126/* The following routines are useful in setting all the flags that
1127   -ffast-math and -fno-fast-math imply.  */
1128void
1129set_fast_math_flags (int set)
1130{
1131  flag_trapping_math = !set;
1132  flag_unsafe_math_optimizations = set;
1133  flag_finite_math_only = set;
1134  flag_errno_math = !set;
1135  if (set)
1136    {
1137      flag_signaling_nans = 0;
1138      flag_rounding_math = 0;
1139      flag_cx_limited_range = 1;
1140    }
1141}
1142
1143/* Return true iff flags are set as if -ffast-math.  */
1144bool
1145fast_math_flags_set_p (void)
1146{
1147  return (!flag_trapping_math
1148	  && flag_unsafe_math_optimizations
1149	  && flag_finite_math_only
1150	  && !flag_errno_math);
1151}
1152
1153/* Handle a debug output -g switch.  EXTENDED is true or false to support
1154   extended output (2 is special and means "-ggdb" was given).  */
1155static void
1156set_debug_level (enum debug_info_type type, int extended, const char *arg)
1157{
1158  static bool type_explicit;
1159
1160  use_gnu_debug_info_extensions = extended;
1161
1162  if (type == NO_DEBUG)
1163    {
1164      if (write_symbols == NO_DEBUG)
1165	{
1166	  write_symbols = PREFERRED_DEBUGGING_TYPE;
1167
1168	  if (extended == 2)
1169	    {
1170#ifdef DWARF2_DEBUGGING_INFO
1171	      write_symbols = DWARF2_DEBUG;
1172#elif defined DBX_DEBUGGING_INFO
1173	      write_symbols = DBX_DEBUG;
1174#endif
1175	    }
1176
1177	  if (write_symbols == NO_DEBUG)
1178	    warning (0, "target system does not support debug output");
1179	}
1180    }
1181  else
1182    {
1183      /* Does it conflict with an already selected type?  */
1184      if (type_explicit && write_symbols != NO_DEBUG && type != write_symbols)
1185	error ("debug format \"%s\" conflicts with prior selection",
1186	       debug_type_names[type]);
1187      write_symbols = type;
1188      type_explicit = true;
1189    }
1190
1191  /* A debug flag without a level defaults to level 2.  */
1192  if (*arg == '\0')
1193    {
1194      if (!debug_info_level)
1195	debug_info_level = 2;
1196    }
1197  else
1198    {
1199      debug_info_level = integral_argument (arg);
1200      if (debug_info_level == (unsigned int) -1)
1201	error ("unrecognised debug output level \"%s\"", arg);
1202      else if (debug_info_level > 3)
1203	error ("debug output level %s is too high", arg);
1204    }
1205}
1206
1207/* Display help for target options.  */
1208static void
1209print_target_help (void)
1210{
1211  unsigned int i;
1212  static bool displayed = false;
1213
1214  /* Avoid double printing for --help --target-help.  */
1215  if (displayed)
1216    return;
1217
1218  displayed = true;
1219  for (i = 0; i < cl_options_count; i++)
1220    if ((cl_options[i].flags & (CL_TARGET | CL_UNDOCUMENTED)) == CL_TARGET)
1221      {
1222	printf (_("\nTarget specific options:\n"));
1223	print_filtered_help (CL_TARGET);
1224	break;
1225      }
1226}
1227
1228/* Output --help text.  */
1229static void
1230print_help (void)
1231{
1232  size_t i;
1233  const char *p;
1234
1235  GET_ENVIRONMENT (p, "COLUMNS");
1236  if (p)
1237    {
1238      int value = atoi (p);
1239      if (value > 0)
1240	columns = value;
1241    }
1242
1243  puts (_("The following options are language-independent:\n"));
1244
1245  print_filtered_help (CL_COMMON);
1246  print_param_help ();
1247
1248  for (i = 0; lang_names[i]; i++)
1249    {
1250      printf (_("The %s front end recognizes the following options:\n\n"),
1251	      lang_names[i]);
1252      print_filtered_help (1U << i);
1253    }
1254  print_target_help ();
1255}
1256
1257/* Print the help for --param.  */
1258static void
1259print_param_help (void)
1260{
1261  size_t i;
1262
1263  puts (_("The --param option recognizes the following as parameters:\n"));
1264
1265  for (i = 0; i < LAST_PARAM; i++)
1266    {
1267      const char *help = compiler_params[i].help;
1268      const char *param = compiler_params[i].option;
1269
1270      if (help == NULL || *help == '\0')
1271	help = undocumented_msg;
1272
1273      /* Get the translation.  */
1274      help = _(help);
1275
1276      wrap_help (help, param, strlen (param));
1277    }
1278
1279  putchar ('\n');
1280}
1281
1282/* Print help for a specific front-end, etc.  */
1283static void
1284print_filtered_help (unsigned int flag)
1285{
1286  unsigned int i, len, filter, indent = 0;
1287  bool duplicates = false;
1288  const char *help, *opt, *tab;
1289  static char *printed;
1290
1291  if (flag == CL_COMMON || flag == CL_TARGET)
1292    {
1293      filter = flag;
1294      if (!printed)
1295	printed = xmalloc (cl_options_count);
1296      memset (printed, 0, cl_options_count);
1297    }
1298  else
1299    {
1300      /* Don't print COMMON options twice.  */
1301      filter = flag | CL_COMMON;
1302
1303      for (i = 0; i < cl_options_count; i++)
1304	{
1305	  if ((cl_options[i].flags & filter) != flag)
1306	    continue;
1307
1308	  /* Skip help for internal switches.  */
1309	  if (cl_options[i].flags & CL_UNDOCUMENTED)
1310	    continue;
1311
1312	  /* Skip switches that have already been printed, mark them to be
1313	     listed later.  */
1314	  if (printed[i])
1315	    {
1316	      duplicates = true;
1317	      indent = print_switch (cl_options[i].opt_text, indent);
1318	    }
1319	}
1320
1321      if (duplicates)
1322	{
1323	  putchar ('\n');
1324	  putchar ('\n');
1325	}
1326    }
1327
1328  for (i = 0; i < cl_options_count; i++)
1329    {
1330      if ((cl_options[i].flags & filter) != flag)
1331	continue;
1332
1333      /* Skip help for internal switches.  */
1334      if (cl_options[i].flags & CL_UNDOCUMENTED)
1335	continue;
1336
1337      /* Skip switches that have already been printed.  */
1338      if (printed[i])
1339	continue;
1340
1341      printed[i] = true;
1342
1343      help = cl_options[i].help;
1344      if (!help)
1345	help = undocumented_msg;
1346
1347      /* Get the translation.  */
1348      help = _(help);
1349
1350      tab = strchr (help, '\t');
1351      if (tab)
1352	{
1353	  len = tab - help;
1354	  opt = help;
1355	  help = tab + 1;
1356	}
1357      else
1358	{
1359	  opt = cl_options[i].opt_text;
1360	  len = strlen (opt);
1361	}
1362
1363      wrap_help (help, opt, len);
1364    }
1365
1366  putchar ('\n');
1367}
1368
1369/* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
1370   word-wrapped HELP in a second column.  */
1371static unsigned int
1372print_switch (const char *text, unsigned int indent)
1373{
1374  unsigned int len = strlen (text) + 1; /* trailing comma */
1375
1376  if (indent)
1377    {
1378      putchar (',');
1379      if (indent + len > columns)
1380	{
1381	  putchar ('\n');
1382	  putchar (' ');
1383	  indent = 1;
1384	}
1385    }
1386  else
1387    putchar (' ');
1388
1389  putchar (' ');
1390  fputs (text, stdout);
1391
1392  return indent + len + 1;
1393}
1394
1395/* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
1396   word-wrapped HELP in a second column.  */
1397static void
1398wrap_help (const char *help, const char *item, unsigned int item_width)
1399{
1400  unsigned int col_width = 27;
1401  unsigned int remaining, room, len;
1402
1403  remaining = strlen (help);
1404
1405  do
1406    {
1407      room = columns - 3 - MAX (col_width, item_width);
1408      if (room > columns)
1409	room = 0;
1410      len = remaining;
1411
1412      if (room < len)
1413	{
1414	  unsigned int i;
1415
1416	  for (i = 0; help[i]; i++)
1417	    {
1418	      if (i >= room && len != remaining)
1419		break;
1420	      if (help[i] == ' ')
1421		len = i;
1422	      else if ((help[i] == '-' || help[i] == '/')
1423		       && help[i + 1] != ' '
1424		       && i > 0 && ISALPHA (help[i - 1]))
1425		len = i + 1;
1426	    }
1427	}
1428
1429      printf( "  %-*.*s %.*s\n", col_width, item_width, item, len, help);
1430      item_width = 0;
1431      while (help[len] == ' ')
1432	len++;
1433      help += len;
1434      remaining -= len;
1435    }
1436  while (remaining);
1437}
1438
1439/* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
1440   a simple on-off switch.  */
1441
1442int
1443option_enabled (int opt_idx)
1444{
1445  const struct cl_option *option = &(cl_options[opt_idx]);
1446  if (option->flag_var)
1447    switch (option->var_type)
1448      {
1449      case CLVC_BOOLEAN:
1450	return *(int *) option->flag_var != 0;
1451
1452      case CLVC_EQUAL:
1453	return *(int *) option->flag_var == option->var_value;
1454
1455      case CLVC_BIT_CLEAR:
1456	return (*(int *) option->flag_var & option->var_value) == 0;
1457
1458      case CLVC_BIT_SET:
1459	return (*(int *) option->flag_var & option->var_value) != 0;
1460
1461      case CLVC_STRING:
1462	break;
1463      }
1464  return -1;
1465}
1466
1467/* Fill STATE with the current state of option OPTION.  Return true if
1468   there is some state to store.  */
1469
1470bool
1471get_option_state (int option, struct cl_option_state *state)
1472{
1473  if (cl_options[option].flag_var == 0)
1474    return false;
1475
1476  switch (cl_options[option].var_type)
1477    {
1478    case CLVC_BOOLEAN:
1479    case CLVC_EQUAL:
1480      state->data = cl_options[option].flag_var;
1481      state->size = sizeof (int);
1482      break;
1483
1484    case CLVC_BIT_CLEAR:
1485    case CLVC_BIT_SET:
1486      state->ch = option_enabled (option);
1487      state->data = &state->ch;
1488      state->size = 1;
1489      break;
1490
1491    case CLVC_STRING:
1492      state->data = *(const char **) cl_options[option].flag_var;
1493      if (state->data == 0)
1494	state->data = "";
1495      state->size = strlen (state->data) + 1;
1496      break;
1497    }
1498  return true;
1499}
1500