OSDN Git Service

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