OSDN Git Service

* doc/invoke.texi (-flto-partition, lto-partitions, lto-minpartition):
[pf3gnuchains/gcc-fork.git] / gcc / opts.c
1 /* Command line option handling.
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Neil Booth.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
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 "expr.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 "opts-diagnostic.h"
40 #include "insn-attr.h"          /* For INSN_SCHEDULING.  */
41 #include "target.h"
42 #include "tree-pass.h"
43 #include "dbgcnt.h"
44 #include "debug.h"
45 #include "plugin.h"
46 #include "except.h"
47 #include "lto-streamer.h"
48
49 /* True if we should exit after parsing options.  */
50 bool exit_after_options;
51
52 /* True to warn about any objects definitions whose size is larger
53    than N bytes.  Also want about function definitions whose returned
54    values are larger than N bytes, where N is `larger_than_size'.  */
55 bool warn_larger_than;
56 HOST_WIDE_INT larger_than_size;
57
58 /* True to warn about any function whose frame size is larger
59    than N bytes. */
60 bool warn_frame_larger_than;
61 HOST_WIDE_INT frame_larger_than_size;
62
63 /* Type(s) of debugging information we are producing (if any).  See
64    flags.h for the definitions of the different possible types of
65    debugging information.  */
66 enum debug_info_type write_symbols = NO_DEBUG;
67
68 /* Level of debugging information we are producing.  See flags.h for
69    the definitions of the different possible levels.  */
70 enum debug_info_level debug_info_level = DINFO_LEVEL_NONE;
71
72 /* A major contribution to object and executable size is debug
73    information size.  A major contribution to debug information size
74    is struct descriptions replicated in several object files. The
75    following flags attempt to reduce this information.  The basic
76    idea is to not emit struct debugging information in the current
77    compilation unit when that information will be generated by
78    another compilation unit.
79
80    Debug information for a struct defined in the current source
81    file should be generated in the object file.  Likewise the
82    debug information for a struct defined in a header should be
83    generated in the object file of the corresponding source file.
84    Both of these case are handled when the base name of the file of
85    the struct definition matches the base name of the source file
86    of the current compilation unit.  This matching emits minimal
87    struct debugging information.
88
89    The base file name matching rule above will fail to emit debug
90    information for structs defined in system headers.  So a second
91    category of files includes system headers in addition to files
92    with matching bases.
93
94    The remaining types of files are library headers and application
95    headers.  We cannot currently distinguish these two types.  */
96
97 enum debug_struct_file
98 {
99   DINFO_STRUCT_FILE_NONE,   /* Debug no structs. */
100   DINFO_STRUCT_FILE_BASE,   /* Debug structs defined in files with the
101                                same base name as the compilation unit. */
102   DINFO_STRUCT_FILE_SYS,    /* Also debug structs defined in system
103                                header files.  */
104   DINFO_STRUCT_FILE_ANY     /* Debug structs defined in all files. */
105 };
106
107 /* Generic structs (e.g. templates not explicitly specialized)
108    may not have a compilation unit associated with them, and so
109    may need to be treated differently from ordinary structs.
110
111    Structs only handled by reference (indirectly), will also usually
112    not need as much debugging information.  */
113
114 static enum debug_struct_file debug_struct_ordinary[DINFO_USAGE_NUM_ENUMS]
115   = { DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY };
116 static enum debug_struct_file debug_struct_generic[DINFO_USAGE_NUM_ENUMS]
117   = { DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY, DINFO_STRUCT_FILE_ANY };
118
119 /* Run the second compilation of -fcompare-debug.  Not defined using
120    Var in common.opt because this is used in Ada code and so must be
121    an actual variable not a macro.  */
122 int flag_compare_debug;
123
124 /* Parse the -femit-struct-debug-detailed option value
125    and set the flag variables. */
126
127 #define MATCH( prefix, string ) \
128   ((strncmp (prefix, string, sizeof prefix - 1) == 0) \
129    ? ((string += sizeof prefix - 1), 1) : 0)
130
131 void
132 set_struct_debug_option (const char *spec)
133 {
134   /* various labels for comparison */
135   static char dfn_lbl[] = "dfn:", dir_lbl[] = "dir:", ind_lbl[] = "ind:";
136   static char ord_lbl[] = "ord:", gen_lbl[] = "gen:";
137   static char none_lbl[] = "none", any_lbl[] = "any";
138   static char base_lbl[] = "base", sys_lbl[] = "sys";
139
140   enum debug_struct_file files = DINFO_STRUCT_FILE_ANY;
141   /* Default is to apply to as much as possible. */
142   enum debug_info_usage usage = DINFO_USAGE_NUM_ENUMS;
143   int ord = 1, gen = 1;
144
145   /* What usage? */
146   if (MATCH (dfn_lbl, spec))
147     usage = DINFO_USAGE_DFN;
148   else if (MATCH (dir_lbl, spec))
149     usage = DINFO_USAGE_DIR_USE;
150   else if (MATCH (ind_lbl, spec))
151     usage = DINFO_USAGE_IND_USE;
152
153   /* Generics or not? */
154   if (MATCH (ord_lbl, spec))
155     gen = 0;
156   else if (MATCH (gen_lbl, spec))
157     ord = 0;
158
159   /* What allowable environment? */
160   if (MATCH (none_lbl, spec))
161     files = DINFO_STRUCT_FILE_NONE;
162   else if (MATCH (any_lbl, spec))
163     files = DINFO_STRUCT_FILE_ANY;
164   else if (MATCH (sys_lbl, spec))
165     files = DINFO_STRUCT_FILE_SYS;
166   else if (MATCH (base_lbl, spec))
167     files = DINFO_STRUCT_FILE_BASE;
168   else
169     error ("argument %qs to %<-femit-struct-debug-detailed%> not recognized",
170            spec);
171
172   /* Effect the specification. */
173   if (usage == DINFO_USAGE_NUM_ENUMS)
174     {
175       if (ord)
176         {
177           debug_struct_ordinary[DINFO_USAGE_DFN] = files;
178           debug_struct_ordinary[DINFO_USAGE_DIR_USE] = files;
179           debug_struct_ordinary[DINFO_USAGE_IND_USE] = files;
180         }
181       if (gen)
182         {
183           debug_struct_generic[DINFO_USAGE_DFN] = files;
184           debug_struct_generic[DINFO_USAGE_DIR_USE] = files;
185           debug_struct_generic[DINFO_USAGE_IND_USE] = files;
186         }
187     }
188   else
189     {
190       if (ord)
191         debug_struct_ordinary[usage] = files;
192       if (gen)
193         debug_struct_generic[usage] = files;
194     }
195
196   if (*spec == ',')
197     set_struct_debug_option (spec+1);
198   else
199     {
200       /* No more -femit-struct-debug-detailed specifications.
201          Do final checks. */
202       if (*spec != '\0')
203         error ("argument %qs to %<-femit-struct-debug-detailed%> unknown",
204                spec);
205       if (debug_struct_ordinary[DINFO_USAGE_DIR_USE]
206                 < debug_struct_ordinary[DINFO_USAGE_IND_USE]
207           || debug_struct_generic[DINFO_USAGE_DIR_USE]
208                 < debug_struct_generic[DINFO_USAGE_IND_USE])
209         error ("%<-femit-struct-debug-detailed=dir:...%> must allow at least"
210                " as much as %<-femit-struct-debug-detailed=ind:...%>");
211     }
212 }
213
214 /* Find the base name of a path, stripping off both directories and
215    a single final extension. */
216 static int
217 base_of_path (const char *path, const char **base_out)
218 {
219   const char *base = path;
220   const char *dot = 0;
221   const char *p = path;
222   char c = *p;
223   while (c)
224     {
225       if (IS_DIR_SEPARATOR(c))
226         {
227           base = p + 1;
228           dot = 0;
229         }
230       else if (c == '.')
231         dot = p;
232       c = *++p;
233     }
234   if (!dot)
235     dot = p;
236   *base_out = base;
237   return dot - base;
238 }
239
240 /* Match the base name of a file to the base name of a compilation unit. */
241
242 static const char *main_input_basename;
243 static int main_input_baselength;
244
245 static int
246 matches_main_base (const char *path)
247 {
248   /* Cache the last query. */
249   static const char *last_path = NULL;
250   static int last_match = 0;
251   if (path != last_path)
252     {
253       const char *base;
254       int length = base_of_path (path, &base);
255       last_path = path;
256       last_match = (length == main_input_baselength
257                     && memcmp (base, main_input_basename, length) == 0);
258     }
259   return last_match;
260 }
261
262 #ifdef DEBUG_DEBUG_STRUCT
263
264 static int
265 dump_struct_debug (tree type, enum debug_info_usage usage,
266                    enum debug_struct_file criterion, int generic,
267                    int matches, int result)
268 {
269   /* Find the type name. */
270   tree type_decl = TYPE_STUB_DECL (type);
271   tree t = type_decl;
272   const char *name = 0;
273   if (TREE_CODE (t) == TYPE_DECL)
274     t = DECL_NAME (t);
275   if (t)
276     name = IDENTIFIER_POINTER (t);
277
278   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
279            criterion,
280            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
281            matches ? "bas" : "hdr",
282            generic ? "gen" : "ord",
283            usage == DINFO_USAGE_DFN ? ";" :
284              usage == DINFO_USAGE_DIR_USE ? "." : "*",
285            result,
286            (void*) type_decl, name);
287   return result;
288 }
289 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
290   dump_struct_debug (type, usage, criterion, generic, matches, result)
291
292 #else
293
294 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
295   (result)
296
297 #endif
298
299
300 bool
301 should_emit_struct_debug (tree type, enum debug_info_usage usage)
302 {
303   enum debug_struct_file criterion;
304   tree type_decl;
305   bool generic = lang_hooks.types.generic_p (type);
306
307   if (generic)
308     criterion = debug_struct_generic[usage];
309   else
310     criterion = debug_struct_ordinary[usage];
311
312   if (criterion == DINFO_STRUCT_FILE_NONE)
313     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
314   if (criterion == DINFO_STRUCT_FILE_ANY)
315     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
316
317   type_decl = TYPE_STUB_DECL (type);
318
319   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
320     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
321
322   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
323     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
324   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
325 }
326
327 /* Nonzero means use GNU-only extensions in the generated symbolic
328    debugging information.  Currently, this only has an effect when
329    write_symbols is set to DBX_DEBUG, XCOFF_DEBUG, or DWARF_DEBUG.  */
330 bool use_gnu_debug_info_extensions;
331
332 /* The default visibility for all symbols (unless overridden) */
333 enum symbol_visibility default_visibility = VISIBILITY_DEFAULT;
334
335 /* Global visibility options.  */
336 struct visibility_flags visibility_options;
337
338 /* What to print when a switch has no documentation.  */
339 static const char undocumented_msg[] = N_("This switch lacks documentation");
340
341 /* Functions excluded from profiling.  */
342
343 typedef char *char_p; /* For DEF_VEC_P.  */
344 DEF_VEC_P(char_p);
345 DEF_VEC_ALLOC_P(char_p,heap);
346
347 static VEC(char_p,heap) *flag_instrument_functions_exclude_functions;
348 static VEC(char_p,heap) *flag_instrument_functions_exclude_files;
349
350 typedef const char *const_char_p; /* For DEF_VEC_P.  */
351 DEF_VEC_P(const_char_p);
352 DEF_VEC_ALLOC_P(const_char_p,heap);
353
354 static VEC(const_char_p,heap) *ignored_options;
355
356 /* Language specific warning pass for unused results.  */
357 bool flag_warn_unused_result = false;
358
359 /* Input file names.  */
360 const char **in_fnames;
361 unsigned num_in_fnames;
362
363 static bool common_handle_option (struct gcc_options *opts,
364                                   struct gcc_options *opts_set,
365                                   const struct cl_decoded_option *decoded,
366                                   unsigned int lang_mask, int kind,
367                                   const struct cl_option_handlers *handlers);
368 static void handle_param (const char *);
369 static char *write_langs (unsigned int lang_mask);
370 static void complain_wrong_lang (const struct cl_decoded_option *,
371                                  unsigned int lang_mask);
372 static void set_debug_level (enum debug_info_type type, int extended,
373                              const char *arg);
374
375 /* Return a malloced slash-separated list of languages in MASK.  */
376 static char *
377 write_langs (unsigned int mask)
378 {
379   unsigned int n = 0, len = 0;
380   const char *lang_name;
381   char *result;
382
383   for (n = 0; (lang_name = lang_names[n]) != 0; n++)
384     if (mask & (1U << n))
385       len += strlen (lang_name) + 1;
386
387   result = XNEWVEC (char, len);
388   len = 0;
389   for (n = 0; (lang_name = lang_names[n]) != 0; n++)
390     if (mask & (1U << n))
391       {
392         if (len)
393           result[len++] = '/';
394         strcpy (result + len, lang_name);
395         len += strlen (lang_name);
396       }
397
398   result[len] = 0;
399
400   return result;
401 }
402
403 /* Complain that switch DECODED does not apply to this front end (mask
404    LANG_MASK).  */
405 static void
406 complain_wrong_lang (const struct cl_decoded_option *decoded,
407                      unsigned int lang_mask)
408 {
409   const struct cl_option *option = &cl_options[decoded->opt_index];
410   const char *text = decoded->orig_option_with_args_text;
411   char *ok_langs = NULL, *bad_lang = NULL;
412   unsigned int opt_flags = option->flags;
413
414   if (!lang_hooks.complain_wrong_lang_p (option))
415     return;
416
417   opt_flags &= ((1U << cl_lang_count) - 1) | CL_DRIVER;
418   if (opt_flags != CL_DRIVER)
419     ok_langs = write_langs (opt_flags);
420   if (lang_mask != CL_DRIVER)
421     bad_lang = write_langs (lang_mask);
422
423   if (opt_flags == CL_DRIVER)
424     error ("command line option %qs is valid for the driver but not for %s",
425            text, bad_lang);
426   else if (lang_mask == CL_DRIVER)
427     gcc_unreachable ();
428   else
429     /* Eventually this should become a hard error IMO.  */
430     warning (0, "command line option %qs is valid for %s but not for %s",
431              text, ok_langs, bad_lang);
432
433   free (ok_langs);
434   free (bad_lang);
435 }
436
437 /* Buffer the unknown option described by the string OPT.  Currently,
438    we only complain about unknown -Wno-* options if they may have
439    prevented a diagnostic. Otherwise, we just ignore them.
440    Note that if we do complain, it is only as a warning, not an error;
441    passing the compiler an unrecognised -Wno-* option should never
442    change whether the compilation succeeds or fails.  */
443
444 static void postpone_unknown_option_warning(const char *opt)
445 {
446   VEC_safe_push (const_char_p, heap, ignored_options, opt);
447 }
448
449 /* Produce a warning for each option previously buffered.  */
450
451 void print_ignored_options (void)
452 {
453   location_t saved_loc = input_location;
454
455   input_location = 0;
456
457   while (!VEC_empty (const_char_p, ignored_options))
458     {
459       const char *opt;
460       opt = VEC_pop (const_char_p, ignored_options);
461       warning (0, "unrecognized command line option \"%s\"", opt);
462     }
463
464   input_location = saved_loc;
465 }
466
467 /* Handle an unknown option DECODED, returning true if an error should be
468    given.  */
469
470 static bool
471 unknown_option_callback (const struct cl_decoded_option *decoded)
472 {
473   const char *opt = decoded->arg;
474
475   if (opt[1] == 'W' && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-'
476       && !(decoded->errors & CL_ERR_NEGATIVE))
477     {
478       /* We don't generate warnings for unknown -Wno-* options unless
479          we issue diagnostics.  */
480       postpone_unknown_option_warning (opt);
481       return false;
482     }
483   else
484     return true;
485 }
486
487 /* Note that an option DECODED has been successfully handled with a
488    handler for mask MASK.  */
489
490 static void
491 post_handling_callback (const struct cl_decoded_option *decoded ATTRIBUTE_UNUSED,
492                         unsigned int mask ATTRIBUTE_UNUSED)
493 {
494 #ifdef ENABLE_LTO
495   lto_register_user_option (decoded->opt_index, decoded->arg,
496                             decoded->value, mask);
497 #endif
498 }
499
500 /* Handle a front-end option; arguments and return value as for
501    handle_option.  */
502
503 static bool
504 lang_handle_option (struct gcc_options *opts,
505                     struct gcc_options *opts_set,
506                     const struct cl_decoded_option *decoded,
507                     unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
508                     const struct cl_option_handlers *handlers)
509 {
510   gcc_assert (opts == &global_options);
511   gcc_assert (opts_set == &global_options_set);
512   gcc_assert (decoded->canonical_option_num_elements <= 2);
513   return lang_hooks.handle_option (decoded->opt_index, decoded->arg,
514                                    decoded->value, kind, handlers);
515 }
516
517 /* Handle a back-end option; arguments and return value as for
518    handle_option.  */
519
520 static bool
521 target_handle_option (struct gcc_options *opts,
522                       struct gcc_options *opts_set,
523                       const struct cl_decoded_option *decoded,
524                       unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
525                       const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
526 {
527   gcc_assert (opts == &global_options);
528   gcc_assert (opts_set == &global_options_set);
529   gcc_assert (decoded->canonical_option_num_elements <= 2);
530   gcc_assert (kind == DK_UNSPECIFIED);
531   return targetm.handle_option (decoded->opt_index, decoded->arg,
532                                 decoded->value);
533 }
534
535 /* Handle FILENAME from the command line.  */
536 static void
537 add_input_filename (const char *filename)
538 {
539   num_in_fnames++;
540   in_fnames = XRESIZEVEC (const char *, in_fnames, num_in_fnames);
541   in_fnames[num_in_fnames - 1] = filename;
542 }
543
544 /* Add comma-separated strings to a char_p vector.  */
545
546 static void
547 add_comma_separated_to_vector (VEC(char_p,heap) **pvec, const char* arg)
548 {
549   char *tmp;
550   char *r;
551   char *w;
552   char *token_start;
553
554   /* We never free this string.  */
555   tmp = xstrdup (arg);
556
557   r = tmp;
558   w = tmp;
559   token_start = tmp;
560
561   while (*r != '\0')
562     {
563       if (*r == ',')
564         {
565           *w++ = '\0';
566           ++r;
567           VEC_safe_push (char_p, heap, *pvec, token_start);
568           token_start = w;
569         }
570       if (*r == '\\' && r[1] == ',')
571         {
572           *w++ = ',';
573           r += 2;
574         }
575       else
576         *w++ = *r++;
577     }
578   if (*token_start != '\0')
579     VEC_safe_push (char_p, heap, *pvec, token_start);
580 }
581
582 /* Return whether we should exclude FNDECL from instrumentation.  */
583
584 bool
585 flag_instrument_functions_exclude_p (tree fndecl)
586 {
587   if (VEC_length (char_p, flag_instrument_functions_exclude_functions) > 0)
588     {
589       const char *name;
590       int i;
591       char *s;
592
593       name = lang_hooks.decl_printable_name (fndecl, 0);
594       FOR_EACH_VEC_ELT (char_p, flag_instrument_functions_exclude_functions,
595                         i, s)
596         if (strstr (name, s) != NULL)
597           return true;
598     }
599
600   if (VEC_length (char_p, flag_instrument_functions_exclude_files) > 0)
601     {
602       const char *name;
603       int i;
604       char *s;
605
606       name = DECL_SOURCE_FILE (fndecl);
607       FOR_EACH_VEC_ELT (char_p, flag_instrument_functions_exclude_files, i, s)
608         if (strstr (name, s) != NULL)
609           return true;
610     }
611
612   return false;
613 }
614
615
616 /* Handle the vector of command line options.  LANG_MASK
617    contains has a single bit set representing the current
618    language.  HANDLERS describes what functions to call for the options.  */
619 static void
620 read_cmdline_options (struct cl_decoded_option *decoded_options,
621                       unsigned int decoded_options_count,
622                       unsigned int lang_mask,
623                       const struct cl_option_handlers *handlers)
624 {
625   unsigned int i;
626
627   for (i = 1; i < decoded_options_count; i++)
628     {
629       if (decoded_options[i].opt_index == OPT_SPECIAL_input_file)
630         {
631           if (main_input_filename == NULL)
632             {
633               main_input_filename = decoded_options[i].arg;
634               main_input_baselength
635                 = base_of_path (main_input_filename, &main_input_basename);
636             }
637           add_input_filename (decoded_options[i].arg);
638           continue;
639         }
640
641       read_cmdline_option (&global_options, &global_options_set,
642                            decoded_options + i, lang_mask, handlers,
643                            global_dc);
644     }
645 }
646
647 /* Parse command line options and set default flag values.  Do minimal
648    options processing.  The decoded options are placed in *DECODED_OPTIONS
649    and *DECODED_OPTIONS_COUNT.  */
650 void
651 decode_options (unsigned int argc, const char **argv,
652                 struct cl_decoded_option **decoded_options,
653                 unsigned int *decoded_options_count)
654 {
655   static bool first_time_p = true;
656   static int initial_min_crossjump_insns;
657   static int initial_max_fields_for_field_sensitive;
658   static int initial_loop_invariant_max_bbs_in_loop;
659   static unsigned int initial_lang_mask;
660   struct cl_option_handlers handlers;
661
662   unsigned int i, lang_mask;
663   int opt1;
664   int opt2;
665   int opt3;
666   int opt1_max;
667   int ofast = 0;
668   enum unwind_info_type ui_except;
669
670   if (first_time_p)
671     {
672       /* Perform language-specific options initialization.  */
673       initial_lang_mask = lang_mask = lang_hooks.option_lang_mask ();
674
675       lang_hooks.initialize_diagnostics (global_dc);
676
677       /* Save initial values of parameters we reset.  */
678       initial_min_crossjump_insns
679         = compiler_params[PARAM_MIN_CROSSJUMP_INSNS].value;
680       initial_max_fields_for_field_sensitive
681         = compiler_params[PARAM_MAX_FIELDS_FOR_FIELD_SENSITIVE].value;
682       initial_loop_invariant_max_bbs_in_loop
683         = compiler_params[PARAM_LOOP_INVARIANT_MAX_BBS_IN_LOOP].value;
684     }
685   else
686     lang_mask = initial_lang_mask;
687
688   decode_cmdline_options_to_array (argc, argv,
689                                    lang_mask | CL_COMMON | CL_TARGET,
690                                    decoded_options, decoded_options_count);
691   if (first_time_p)
692     /* Perform language-specific options initialization.  */
693     lang_hooks.init_options (*decoded_options_count, *decoded_options);
694
695   handlers.unknown_option_callback = unknown_option_callback;
696   handlers.wrong_lang_callback = complain_wrong_lang;
697   handlers.post_handling_callback = post_handling_callback;
698   handlers.num_handlers = 3;
699   handlers.handlers[0].handler = lang_handle_option;
700   handlers.handlers[0].mask = lang_mask;
701   handlers.handlers[1].handler = common_handle_option;
702   handlers.handlers[1].mask = CL_COMMON;
703   handlers.handlers[2].handler = target_handle_option;
704   handlers.handlers[2].mask = CL_TARGET;
705
706   /* Scan to see what optimization level has been specified.  That will
707      determine the default value of many flags.  */
708   for (i = 1; i < *decoded_options_count; i++)
709     {
710       struct cl_decoded_option *opt = &(*decoded_options)[i];
711       switch (opt->opt_index)
712         {
713         case OPT_O:
714           if (*opt->arg == '\0')
715             {
716               optimize = 1;
717               optimize_size = 0;
718               ofast = 0;
719             }
720           else
721             {
722               const int optimize_val = integral_argument (opt->arg);
723               if (optimize_val == -1)
724                 error ("argument to %qs should be a non-negative integer",
725                        "-O");
726               else
727                 {
728                   optimize = optimize_val;
729                   if ((unsigned int) optimize > 255)
730                     optimize = 255;
731                   optimize_size = 0;
732                   ofast = 0;
733                 }
734             }
735           break;
736
737         case OPT_Os:
738           optimize_size = 1;
739
740           /* Optimizing for size forces optimize to be 2.  */
741           optimize = 2;
742           ofast = 0;
743           break;
744
745         case OPT_Ofast:
746           /* -Ofast only adds flags to -O3.  */
747           optimize_size = 0;
748           optimize = 3;
749           ofast = 1;
750           break;
751
752         default:
753           /* Ignore other options in this prescan.  */
754           break;
755         }
756     }
757
758   /* Use priority coloring if cover classes is not defined for the
759      target.  */
760   if (targetm.ira_cover_classes == NULL)
761     flag_ira_algorithm = IRA_ALGORITHM_PRIORITY;
762
763   /* -O1 optimizations.  */
764   opt1 = (optimize >= 1);
765   flag_defer_pop = opt1;
766 #ifdef DELAY_SLOTS
767   flag_delayed_branch = opt1;
768 #endif
769 #ifdef CAN_DEBUG_WITHOUT_FP
770   flag_omit_frame_pointer = opt1;
771 #endif
772   flag_guess_branch_prob = opt1;
773   flag_cprop_registers = opt1;
774   flag_forward_propagate = opt1;
775   flag_if_conversion = opt1;
776   flag_if_conversion2 = opt1;
777   flag_ipa_pure_const = opt1;
778   flag_ipa_reference = opt1;
779   flag_ipa_profile = opt1;
780   flag_merge_constants = opt1;
781   flag_split_wide_types = opt1;
782   flag_tree_ccp = opt1;
783   flag_tree_bit_ccp = opt1;
784   flag_tree_dce = opt1;
785   flag_tree_dom = opt1;
786   flag_tree_dse = opt1;
787   flag_tree_ter = opt1;
788   flag_tree_sra = opt1;
789   flag_tree_copyrename = opt1;
790   flag_tree_fre = opt1;
791   flag_tree_copy_prop = opt1;
792   flag_tree_sink = opt1;
793   flag_tree_ch = opt1;
794   flag_combine_stack_adjustments = opt1;
795
796   /* -O2 optimizations.  */
797   opt2 = (optimize >= 2);
798   flag_inline_small_functions = opt2;
799   flag_indirect_inlining = opt2;
800   flag_partial_inlining = opt2;
801   flag_thread_jumps = opt2;
802   flag_crossjumping = opt2;
803   flag_optimize_sibling_calls = opt2;
804   flag_cse_follow_jumps = opt2;
805   flag_gcse = opt2;
806   flag_expensive_optimizations = opt2;
807   flag_rerun_cse_after_loop = opt2;
808   flag_caller_saves = opt2;
809   flag_peephole2 = opt2;
810 #ifdef INSN_SCHEDULING
811   /* Only run the pre-regalloc scheduling pass if optimizing for speed.  */
812   flag_schedule_insns = opt2 && ! optimize_size;
813   flag_schedule_insns_after_reload = opt2;
814 #endif
815   flag_regmove = opt2;
816   flag_strict_aliasing = opt2;
817   flag_strict_overflow = opt2;
818   flag_reorder_blocks = opt2;
819   flag_reorder_functions = opt2;
820   flag_tree_vrp = opt2;
821   flag_tree_builtin_call_dce = opt2;
822   flag_tree_pre = opt2;
823   flag_tree_switch_conversion = opt2;
824   flag_ipa_cp = opt2;
825   flag_ipa_sra = opt2;
826
827   /* Track fields in field-sensitive alias analysis.  */
828   set_param_value ("max-fields-for-field-sensitive",
829                    (opt2) ? 100 : initial_max_fields_for_field_sensitive);
830
831   /* For -O1 only do loop invariant motion for very small loops.  */
832   set_param_value ("loop-invariant-max-bbs-in-loop",
833                    (opt2) ? initial_loop_invariant_max_bbs_in_loop : 1000);
834
835   /* -O3 optimizations.  */
836   opt3 = (optimize >= 3);
837   flag_tree_loop_distribute_patterns = opt3;
838   flag_predictive_commoning = opt3;
839   flag_inline_functions = opt3;
840   flag_unswitch_loops = opt3;
841   flag_gcse_after_reload = opt3;
842   flag_tree_vectorize = opt3;
843   flag_ipa_cp_clone = opt3;
844   if (flag_ipa_cp_clone)
845     flag_ipa_cp = 1;
846
847   /* Just -O1/-O0 optimizations.  */
848   opt1_max = (optimize <= 1);
849   align_loops = opt1_max;
850   align_jumps = opt1_max;
851   align_labels = opt1_max;
852   align_functions = opt1_max;
853
854   if (optimize_size)
855     {
856       /* Inlining of functions reducing size is a good idea regardless of them
857          being declared inline.  */
858       flag_inline_functions = 1;
859
860       /* Basic optimization options.  */
861       optimize_size = 1;
862       if (optimize > 2)
863         optimize = 2;
864
865       /* We want to crossjump as much as possible.  */
866       set_param_value ("min-crossjump-insns", 1);
867     }
868   else
869     set_param_value ("min-crossjump-insns", initial_min_crossjump_insns);
870
871   /* -Ofast adds optimizations to -O3.  */
872   if (ofast)
873     {
874       /* Which is -ffast-math for now.  */
875       set_fast_math_flags (1);
876       /* Allow targets to enable extra options with -Ofast
877          before general options processing so disabling them
878          again afterwards works.  */
879       targetm.handle_ofast ();
880     }
881
882   /* Enable -Werror=coverage-mismatch by default */
883   enable_warning_as_error ("coverage-mismatch", 1, lang_mask, &handlers,
884                            global_dc);
885
886   if (first_time_p)
887     {
888       /* Initialize whether `char' is signed.  */
889       flag_signed_char = DEFAULT_SIGNED_CHAR;
890       /* Set this to a special "uninitialized" value.  The actual default is
891          set after target options have been processed.  */
892       flag_short_enums = 2;
893
894       /* Initialize target_flags before
895          targetm.target_option.optimization so the latter can modify
896          it.  */
897       target_flags = targetm.default_target_flags;
898
899       /* Some targets have ABI-specified unwind tables.  */
900       flag_unwind_tables = targetm.unwind_tables_default;
901     }
902
903 #ifdef ENABLE_LTO
904   /* Clear any options currently held for LTO.  */
905   lto_clear_user_options ();
906 #endif
907
908   /* Allow default optimizations to be specified on a per-machine basis.  */
909   targetm.target_option.optimization (optimize, optimize_size);
910
911   read_cmdline_options (*decoded_options, *decoded_options_count, lang_mask,
912                         &handlers);
913
914   if (dump_base_name && ! IS_ABSOLUTE_PATH (dump_base_name))
915     {
916       /* First try to make DUMP_BASE_NAME relative to the DUMP_DIR_NAME
917          directory.  Then try to make DUMP_BASE_NAME relative to the
918          AUX_BASE_NAME directory, typically the directory to contain
919          the object file.  */
920       if (dump_dir_name)
921         dump_base_name = concat (dump_dir_name, dump_base_name, NULL);
922       else if (aux_base_name)
923         {
924           const char *aux_base;
925
926           base_of_path (aux_base_name, &aux_base);
927           if (aux_base_name != aux_base)
928             {
929               int dir_len = aux_base - aux_base_name;
930               char *new_dump_base_name =
931                 XNEWVEC (char, strlen(dump_base_name) + dir_len + 1);
932
933               /* Copy directory component from AUX_BASE_NAME.  */
934               memcpy (new_dump_base_name, aux_base_name, dir_len);
935               /* Append existing DUMP_BASE_NAME.  */
936               strcpy (new_dump_base_name + dir_len, dump_base_name);
937               dump_base_name = new_dump_base_name;
938             }
939         }
940     }
941
942   /* Handle related options for unit-at-a-time, toplevel-reorder, and
943      section-anchors.  */
944   if (!flag_unit_at_a_time)
945     {
946       if (flag_section_anchors == 1)
947         error ("Section anchors must be disabled when unit-at-a-time "
948                "is disabled.");
949       flag_section_anchors = 0;
950       if (flag_toplevel_reorder == 1)
951         error ("Toplevel reorder must be disabled when unit-at-a-time "
952                "is disabled.");
953       flag_toplevel_reorder = 0;
954     }
955
956   /* -Wmissing-noreturn is alias for -Wsuggest-attribute=noreturn.  */
957   if (warn_missing_noreturn)
958     warn_suggest_attribute_noreturn = true;
959     
960   /* Unless the user has asked for section anchors, we disable toplevel
961      reordering at -O0 to disable transformations that might be surprising
962      to end users and to get -fno-toplevel-reorder tested.  */
963   if (!optimize && flag_toplevel_reorder == 2 && flag_section_anchors != 1)
964     {
965       flag_toplevel_reorder = 0;
966       flag_section_anchors = 0;
967     }
968   if (!flag_toplevel_reorder)
969     {
970       if (flag_section_anchors == 1)
971         error ("section anchors must be disabled when toplevel reorder"
972                " is disabled");
973       flag_section_anchors = 0;
974     }
975
976   if (first_time_p)
977     {
978       if (flag_pie)
979         flag_pic = flag_pie;
980       if (flag_pic && !flag_pie)
981         flag_shlib = 1;
982       first_time_p = false;
983     }
984
985   if (optimize == 0)
986     {
987       /* Inlining does not work if not optimizing,
988          so force it not to be done.  */
989       warn_inline = 0;
990       flag_no_inline = 1;
991     }
992
993   /* The optimization to partition hot and cold basic blocks into separate
994      sections of the .o and executable files does not work (currently)
995      with exception handling.  This is because there is no support for
996      generating unwind info.  If flag_exceptions is turned on we need to
997      turn off the partitioning optimization.  */
998
999   ui_except = targetm.except_unwind_info ();
1000
1001   if (flag_exceptions
1002       && flag_reorder_blocks_and_partition
1003       && (ui_except == UI_SJLJ || ui_except == UI_TARGET))
1004     {
1005       inform (input_location,
1006               "-freorder-blocks-and-partition does not work "
1007               "with exceptions on this architecture");
1008       flag_reorder_blocks_and_partition = 0;
1009       flag_reorder_blocks = 1;
1010     }
1011
1012   /* If user requested unwind info, then turn off the partitioning
1013      optimization.  */
1014
1015   if (flag_unwind_tables
1016       && !targetm.unwind_tables_default
1017       && flag_reorder_blocks_and_partition
1018       && (ui_except == UI_SJLJ || ui_except == UI_TARGET))
1019     {
1020       inform (input_location,
1021               "-freorder-blocks-and-partition does not support "
1022               "unwind info on this architecture");
1023       flag_reorder_blocks_and_partition = 0;
1024       flag_reorder_blocks = 1;
1025     }
1026
1027   /* If the target requested unwind info, then turn off the partitioning
1028      optimization with a different message.  Likewise, if the target does not
1029      support named sections.  */
1030
1031   if (flag_reorder_blocks_and_partition
1032       && (!targetm.have_named_sections
1033           || (flag_unwind_tables
1034               && targetm.unwind_tables_default
1035               && (ui_except == UI_SJLJ || ui_except == UI_TARGET))))
1036     {
1037       inform (input_location,
1038               "-freorder-blocks-and-partition does not work "
1039               "on this architecture");
1040       flag_reorder_blocks_and_partition = 0;
1041       flag_reorder_blocks = 1;
1042     }
1043
1044   /* Pipelining of outer loops is only possible when general pipelining
1045      capabilities are requested.  */
1046   if (!flag_sel_sched_pipelining)
1047     flag_sel_sched_pipelining_outer_loops = 0;
1048
1049   if (!targetm.ira_cover_classes
1050       && flag_ira_algorithm == IRA_ALGORITHM_CB)
1051     {
1052       inform (input_location,
1053               "-fira-algorithm=CB does not work on this architecture");
1054       flag_ira_algorithm = IRA_ALGORITHM_PRIORITY;
1055     }
1056
1057   if (flag_conserve_stack)
1058     {
1059       if (!PARAM_SET_P (PARAM_LARGE_STACK_FRAME))
1060         PARAM_VALUE (PARAM_LARGE_STACK_FRAME) = 100;
1061       if (!PARAM_SET_P (PARAM_STACK_FRAME_GROWTH))
1062         PARAM_VALUE (PARAM_STACK_FRAME_GROWTH) = 40;
1063     }
1064   if (flag_wpa || flag_ltrans)
1065     {
1066       /* These passes are not WHOPR compatible yet.  */
1067       flag_ipa_pta = 0;
1068       flag_ipa_struct_reorg = 0;
1069     }
1070
1071   if (flag_lto || flag_whopr)
1072     {
1073 #ifdef ENABLE_LTO
1074       flag_generate_lto = 1;
1075
1076       /* When generating IL, do not operate in whole-program mode.
1077          Otherwise, symbols will be privatized too early, causing link
1078          errors later.  */
1079       flag_whole_program = 0;
1080 #else
1081       error ("LTO support has not been enabled in this configuration");
1082 #endif
1083     }
1084   if (flag_lto_partition_balanced || flag_lto_partition_1to1)
1085     {
1086       if (flag_lto_partition_balanced && flag_lto_partition_1to1)
1087         error ("Only one -flto-partitoin value can be specified");
1088       if (!flag_whopr)
1089         error ("-flto-partitoin has effect only with -fwhopr");
1090     }
1091
1092   /* Reconcile -flto and -fwhopr.  Set additional flags as appropriate and
1093      check option consistency.  */
1094   if (flag_lto && flag_whopr)
1095     error ("-flto and -fwhopr are mutually exclusive");
1096
1097   /* We initialize flag_split_stack to -1 so that targets can set a
1098      default value if they choose based on other options.  */
1099   if (flag_split_stack == -1)
1100     flag_split_stack = 0;
1101   else if (flag_split_stack)
1102     {
1103       if (!targetm.supports_split_stack (true))
1104         {
1105           error ("%<-fsplit-stack%> is not supported by "
1106                  "this compiler configuration");
1107           flag_split_stack = 0;
1108         }
1109     }
1110 }
1111
1112 #define LEFT_COLUMN     27
1113
1114 /* Output ITEM, of length ITEM_WIDTH, in the left column,
1115    followed by word-wrapped HELP in a second column.  */
1116 static void
1117 wrap_help (const char *help,
1118            const char *item,
1119            unsigned int item_width,
1120            unsigned int columns)
1121 {
1122   unsigned int col_width = LEFT_COLUMN;
1123   unsigned int remaining, room, len;
1124
1125   remaining = strlen (help);
1126
1127   do
1128     {
1129       room = columns - 3 - MAX (col_width, item_width);
1130       if (room > columns)
1131         room = 0;
1132       len = remaining;
1133
1134       if (room < len)
1135         {
1136           unsigned int i;
1137
1138           for (i = 0; help[i]; i++)
1139             {
1140               if (i >= room && len != remaining)
1141                 break;
1142               if (help[i] == ' ')
1143                 len = i;
1144               else if ((help[i] == '-' || help[i] == '/')
1145                        && help[i + 1] != ' '
1146                        && i > 0 && ISALPHA (help[i - 1]))
1147                 len = i + 1;
1148             }
1149         }
1150
1151       printf( "  %-*.*s %.*s\n", col_width, item_width, item, len, help);
1152       item_width = 0;
1153       while (help[len] == ' ')
1154         len++;
1155       help += len;
1156       remaining -= len;
1157     }
1158   while (remaining);
1159 }
1160
1161 /* Print help for a specific front-end, etc.  */
1162 static void
1163 print_filtered_help (unsigned int include_flags,
1164                      unsigned int exclude_flags,
1165                      unsigned int any_flags,
1166                      unsigned int columns)
1167 {
1168   unsigned int i;
1169   const char *help;
1170   static char *printed = NULL;
1171   bool found = false;
1172   bool displayed = false;
1173
1174   if (include_flags == CL_PARAMS)
1175     {
1176       for (i = 0; i < LAST_PARAM; i++)
1177         {
1178           const char *param = compiler_params[i].option;
1179
1180           help = compiler_params[i].help;
1181           if (help == NULL || *help == '\0')
1182             {
1183               if (exclude_flags & CL_UNDOCUMENTED)
1184                 continue;
1185               help = undocumented_msg;
1186             }
1187
1188           /* Get the translation.  */
1189           help = _(help);
1190
1191           wrap_help (help, param, strlen (param), columns);
1192         }
1193       putchar ('\n');
1194       return;
1195     }
1196
1197   if (!printed)
1198     printed = XCNEWVAR (char, cl_options_count);
1199
1200   for (i = 0; i < cl_options_count; i++)
1201     {
1202       static char new_help[128];
1203       const struct cl_option *option = cl_options + i;
1204       unsigned int len;
1205       const char *opt;
1206       const char *tab;
1207
1208       if (include_flags == 0
1209           || ((option->flags & include_flags) != include_flags))
1210         {
1211           if ((option->flags & any_flags) == 0)
1212             continue;
1213         }
1214
1215       /* Skip unwanted switches.  */
1216       if ((option->flags & exclude_flags) != 0)
1217         continue;
1218
1219       /* The driver currently prints its own help text.  */
1220       if ((option->flags & CL_DRIVER) != 0
1221           && (option->flags & (((1U << cl_lang_count) - 1)
1222                                | CL_COMMON | CL_TARGET)) == 0)
1223         continue;
1224
1225       found = true;
1226       /* Skip switches that have already been printed.  */
1227       if (printed[i])
1228         continue;
1229
1230       printed[i] = true;
1231
1232       help = option->help;
1233       if (help == NULL)
1234         {
1235           if (exclude_flags & CL_UNDOCUMENTED)
1236             continue;
1237           help = undocumented_msg;
1238         }
1239
1240       /* Get the translation.  */
1241       help = _(help);
1242
1243       /* Find the gap between the name of the
1244          option and its descriptive text.  */
1245       tab = strchr (help, '\t');
1246       if (tab)
1247         {
1248           len = tab - help;
1249           opt = help;
1250           help = tab + 1;
1251         }
1252       else
1253         {
1254           opt = option->opt_text;
1255           len = strlen (opt);
1256         }
1257
1258       /* With the -Q option enabled we change the descriptive text associated
1259          with an option to be an indication of its current setting.  */
1260       if (!quiet_flag)
1261         {
1262           void *flag_var = option_flag_var (i, &global_options);
1263
1264           if (len < (LEFT_COLUMN + 2))
1265             strcpy (new_help, "\t\t");
1266           else
1267             strcpy (new_help, "\t");
1268
1269           if (flag_var != NULL)
1270             {
1271               if (option->flags & CL_JOINED)
1272                 {
1273                   if (option->var_type == CLVC_STRING)
1274                     {
1275                       if (* (const char **) flag_var != NULL)
1276                         snprintf (new_help + strlen (new_help),
1277                                   sizeof (new_help) - strlen (new_help),
1278                                   * (const char **) flag_var);
1279                     }
1280                   else
1281                     sprintf (new_help + strlen (new_help),
1282                              "%#x", * (int *) flag_var);
1283                 }
1284               else
1285                 strcat (new_help, option_enabled (i, &global_options)
1286                         ? _("[enabled]") : _("[disabled]"));
1287             }
1288
1289           help = new_help;
1290         }
1291
1292       wrap_help (help, opt, len, columns);
1293       displayed = true;
1294     }
1295
1296   if (! found)
1297     {
1298       unsigned int langs = include_flags & CL_LANG_ALL;
1299
1300       if (langs == 0)
1301         printf (_(" No options with the desired characteristics were found\n"));
1302       else
1303         {
1304           unsigned int i;
1305
1306           /* PR 31349: Tell the user how to see all of the
1307              options supported by a specific front end.  */
1308           for (i = 0; (1U << i) < CL_LANG_ALL; i ++)
1309             if ((1U << i) & langs)
1310               printf (_(" None found.  Use --help=%s to show *all* the options supported by the %s front-end\n"),
1311                       lang_names[i], lang_names[i]);
1312         }
1313
1314     }
1315   else if (! displayed)
1316     printf (_(" All options with the desired characteristics have already been displayed\n"));
1317
1318   putchar ('\n');
1319 }
1320
1321 /* Display help for a specified type of option.
1322    The options must have ALL of the INCLUDE_FLAGS set
1323    ANY of the flags in the ANY_FLAGS set
1324    and NONE of the EXCLUDE_FLAGS set.  */
1325 static void
1326 print_specific_help (unsigned int include_flags,
1327                      unsigned int exclude_flags,
1328                      unsigned int any_flags)
1329 {
1330   unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
1331   const char * description = NULL;
1332   const char * descrip_extra = "";
1333   size_t i;
1334   unsigned int flag;
1335   static unsigned int columns = 0;
1336
1337   /* Sanity check: Make sure that we do not have more
1338      languages than we have bits available to enumerate them.  */
1339   gcc_assert ((1U << cl_lang_count) < CL_MIN_OPTION_CLASS);
1340
1341   /* If we have not done so already, obtain
1342      the desired maximum width of the output.  */
1343   if (columns == 0)
1344     {
1345       const char *p;
1346
1347       GET_ENVIRONMENT (p, "COLUMNS");
1348       if (p != NULL)
1349         {
1350           int value = atoi (p);
1351
1352           if (value > 0)
1353             columns = value;
1354         }
1355
1356       if (columns == 0)
1357         /* Use a reasonable default.  */
1358         columns = 80;
1359     }
1360
1361   /* Decide upon the title for the options that we are going to display.  */
1362   for (i = 0, flag = 1; flag <= CL_MAX_OPTION_CLASS; flag <<= 1, i ++)
1363     {
1364       switch (flag & include_flags)
1365         {
1366         case 0:
1367         case CL_DRIVER:
1368           break;
1369
1370         case CL_TARGET:
1371           description = _("The following options are target specific");
1372           break;
1373         case CL_WARNING:
1374           description = _("The following options control compiler warning messages");
1375           break;
1376         case CL_OPTIMIZATION:
1377           description = _("The following options control optimizations");
1378           break;
1379         case CL_COMMON:
1380           description = _("The following options are language-independent");
1381           break;
1382         case CL_PARAMS:
1383           description = _("The --param option recognizes the following as parameters");
1384           break;
1385         default:
1386           if (i >= cl_lang_count)
1387             break;
1388           if (exclude_flags & all_langs_mask)
1389             description = _("The following options are specific to just the language ");
1390           else
1391             description = _("The following options are supported by the language ");
1392           descrip_extra = lang_names [i];
1393           break;
1394         }
1395     }
1396
1397   if (description == NULL)
1398     {
1399       if (any_flags == 0)
1400         {
1401           if (include_flags & CL_UNDOCUMENTED)
1402             description = _("The following options are not documented");
1403           else if (include_flags & CL_SEPARATE)
1404             description = _("The following options take separate arguments");
1405           else if (include_flags & CL_JOINED)
1406             description = _("The following options take joined arguments");
1407           else
1408             {
1409               internal_error ("unrecognized include_flags 0x%x passed to print_specific_help",
1410                               include_flags);
1411               return;
1412             }
1413         }
1414       else
1415         {
1416           if (any_flags & all_langs_mask)
1417             description = _("The following options are language-related");
1418           else
1419             description = _("The following options are language-independent");
1420         }
1421     }
1422
1423   printf ("%s%s:\n", description, descrip_extra);
1424   print_filtered_help (include_flags, exclude_flags, any_flags, columns);
1425 }
1426
1427 /* Handle target- and language-independent options.  Return zero to
1428    generate an "unknown option" message.  Only options that need
1429    extra handling need to be listed here; if you simply want
1430    DECODED->value assigned to a variable, it happens automatically.  */
1431
1432 static bool
1433 common_handle_option (struct gcc_options *opts,
1434                       struct gcc_options *opts_set,
1435                       const struct cl_decoded_option *decoded,
1436                       unsigned int lang_mask, int kind ATTRIBUTE_UNUSED,
1437                       const struct cl_option_handlers *handlers)
1438 {
1439   size_t scode = decoded->opt_index;
1440   const char *arg = decoded->arg;
1441   int value = decoded->value;
1442   static bool verbose = false;
1443   enum opt_code code = (enum opt_code) scode;
1444
1445   gcc_assert (opts == &global_options);
1446   gcc_assert (opts_set == &global_options_set);
1447   gcc_assert (decoded->canonical_option_num_elements <= 2);
1448
1449   switch (code)
1450     {
1451     case OPT__param:
1452       handle_param (arg);
1453       break;
1454
1455     case OPT_v:
1456       verbose = true;
1457       break;
1458
1459     case OPT__help:
1460       {
1461         unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
1462         unsigned int undoc_mask;
1463         unsigned int i;
1464
1465         undoc_mask = (verbose | extra_warnings) ? 0 : CL_UNDOCUMENTED;
1466         /* First display any single language specific options.  */
1467         for (i = 0; i < cl_lang_count; i++)
1468           print_specific_help
1469             (1U << i, (all_langs_mask & (~ (1U << i))) | undoc_mask, 0);
1470         /* Next display any multi language specific options.  */
1471         print_specific_help (0, undoc_mask, all_langs_mask);
1472         /* Then display any remaining, non-language options.  */
1473         for (i = CL_MIN_OPTION_CLASS; i <= CL_MAX_OPTION_CLASS; i <<= 1)
1474           if (i != CL_DRIVER)
1475             print_specific_help (i, undoc_mask, 0);
1476         exit_after_options = true;
1477         break;
1478       }
1479
1480     case OPT__target_help:
1481       print_specific_help (CL_TARGET, CL_UNDOCUMENTED, 0);
1482       exit_after_options = true;
1483
1484       /* Allow the target a chance to give the user some additional information.  */
1485       if (targetm.help)
1486         targetm.help ();
1487       break;
1488
1489     case OPT__help_:
1490       {
1491         const char * a = arg;
1492         unsigned int include_flags = 0;
1493         /* Note - by default we include undocumented options when listing
1494            specific classes.  If you only want to see documented options
1495            then add ",^undocumented" to the --help= option.  E.g.:
1496
1497            --help=target,^undocumented  */
1498         unsigned int exclude_flags = 0;
1499
1500         /* Walk along the argument string, parsing each word in turn.
1501            The format is:
1502            arg = [^]{word}[,{arg}]
1503            word = {optimizers|target|warnings|undocumented|
1504                    params|common|<language>}  */
1505         while (* a != 0)
1506           {
1507             static struct
1508             {
1509               const char * string;
1510               unsigned int flag;
1511             }
1512             specifics[] =
1513             {
1514               { "optimizers", CL_OPTIMIZATION },
1515               { "target", CL_TARGET },
1516               { "warnings", CL_WARNING },
1517               { "undocumented", CL_UNDOCUMENTED },
1518               { "params", CL_PARAMS },
1519               { "joined", CL_JOINED },
1520               { "separate", CL_SEPARATE },
1521               { "common", CL_COMMON },
1522               { NULL, 0 }
1523             };
1524             unsigned int * pflags;
1525             const char * comma;
1526             unsigned int lang_flag, specific_flag;
1527             unsigned int len;
1528             unsigned int i;
1529
1530             if (* a == '^')
1531               {
1532                 ++ a;
1533                 pflags = & exclude_flags;
1534               }
1535             else
1536               pflags = & include_flags;
1537
1538             comma = strchr (a, ',');
1539             if (comma == NULL)
1540               len = strlen (a);
1541             else
1542               len = comma - a;
1543             if (len == 0)
1544               {
1545                 a = comma + 1;
1546                 continue;
1547               }
1548
1549             /* Check to see if the string matches an option class name.  */
1550             for (i = 0, specific_flag = 0; specifics[i].string != NULL; i++)
1551               if (strncasecmp (a, specifics[i].string, len) == 0)
1552                 {
1553                   specific_flag = specifics[i].flag;
1554                   break;
1555                 }
1556
1557             /* Check to see if the string matches a language name.
1558                Note - we rely upon the alpha-sorted nature of the entries in
1559                the lang_names array, specifically that shorter names appear
1560                before their longer variants.  (i.e. C before C++).  That way
1561                when we are attempting to match --help=c for example we will
1562                match with C first and not C++.  */
1563             for (i = 0, lang_flag = 0; i < cl_lang_count; i++)
1564               if (strncasecmp (a, lang_names[i], len) == 0)
1565                 {
1566                   lang_flag = 1U << i;
1567                   break;
1568                 }
1569
1570             if (specific_flag != 0)
1571               {
1572                 if (lang_flag == 0)
1573                   * pflags |= specific_flag;
1574                 else
1575                   {
1576                     /* The option's argument matches both the start of a
1577                        language name and the start of an option class name.
1578                        We have a special case for when the user has
1579                        specified "--help=c", but otherwise we have to issue
1580                        a warning.  */
1581                     if (strncasecmp (a, "c", len) == 0)
1582                       * pflags |= lang_flag;
1583                     else
1584                       fnotice (stderr,
1585                                "warning: --help argument %.*s is ambiguous, please be more specific\n",
1586                                len, a);
1587                   }
1588               }
1589             else if (lang_flag != 0)
1590               * pflags |= lang_flag;
1591             else
1592               fnotice (stderr,
1593                        "warning: unrecognized argument to --help= option: %.*s\n",
1594                        len, a);
1595
1596             if (comma == NULL)
1597               break;
1598             a = comma + 1;
1599           }
1600
1601         if (include_flags)
1602           print_specific_help (include_flags, exclude_flags, 0);
1603         exit_after_options = true;
1604         break;
1605       }
1606
1607     case OPT__version:
1608       exit_after_options = true;
1609       break;
1610
1611     case OPT_O:
1612     case OPT_Os:
1613     case OPT_Ofast:
1614       /* Currently handled in a prescan.  */
1615       break;
1616
1617     case OPT_Werror_:
1618       enable_warning_as_error (arg, value, lang_mask, handlers, global_dc);
1619       break;
1620
1621     case OPT_Wlarger_than_:
1622       larger_than_size = value;
1623       warn_larger_than = value != -1;
1624       break;
1625
1626     case OPT_Wfatal_errors:
1627       global_dc->fatal_errors = value;
1628       break;
1629
1630     case OPT_Wframe_larger_than_:
1631       frame_larger_than_size = value;
1632       warn_frame_larger_than = value != -1;
1633       break;
1634
1635     case OPT_Wstrict_aliasing:
1636       set_Wstrict_aliasing (value);
1637       break;
1638
1639     case OPT_Wstrict_aliasing_:
1640       warn_strict_aliasing = value;
1641       break;
1642
1643     case OPT_Wstrict_overflow:
1644       warn_strict_overflow = (value
1645                               ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
1646                               : 0);
1647       break;
1648
1649     case OPT_Wstrict_overflow_:
1650       warn_strict_overflow = value;
1651       break;
1652
1653     case OPT_Wsystem_headers:
1654       global_dc->dc_warn_system_headers = value;
1655       break;
1656
1657     case OPT_Wunused:
1658       warn_unused = value;
1659       break;
1660
1661     case OPT_aux_info:
1662       aux_info_file_name = arg;
1663       flag_gen_aux_info = 1;
1664       break;
1665
1666     case OPT_auxbase:
1667       aux_base_name = arg;
1668       break;
1669
1670     case OPT_auxbase_strip:
1671       {
1672         char *tmp = xstrdup (arg);
1673         strip_off_ending (tmp, strlen (tmp));
1674         if (tmp[0])
1675           aux_base_name = tmp;
1676       }
1677       break;
1678
1679     case OPT_d:
1680       decode_d_option (arg);
1681       break;
1682
1683     case OPT_dumpbase:
1684       dump_base_name = arg;
1685       break;
1686
1687     case OPT_dumpdir:
1688       dump_dir_name = arg;
1689       break;
1690
1691     case OPT_falign_functions_:
1692       align_functions = value;
1693       break;
1694
1695     case OPT_falign_jumps_:
1696       align_jumps = value;
1697       break;
1698
1699     case OPT_falign_labels_:
1700       align_labels = value;
1701       break;
1702
1703     case OPT_falign_loops_:
1704       align_loops = value;
1705       break;
1706
1707     case OPT_fcall_used_:
1708       fix_register (arg, 0, 1);
1709       break;
1710
1711     case OPT_fcall_saved_:
1712       fix_register (arg, 0, 0);
1713       break;
1714
1715     case OPT_fcompare_debug_second:
1716       flag_compare_debug = value;
1717       break;
1718
1719     case OPT_fdbg_cnt_:
1720       dbg_cnt_process_opt (arg);
1721       break;
1722
1723     case OPT_fdbg_cnt_list:
1724       dbg_cnt_list_all_counters ();
1725       break;
1726
1727     case OPT_fdebug_prefix_map_:
1728       add_debug_prefix_map (arg);
1729       break;
1730
1731     case OPT_fdiagnostics_show_location_:
1732       if (!strcmp (arg, "once"))
1733         diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
1734       else if (!strcmp (arg, "every-line"))
1735         diagnostic_prefixing_rule (global_dc)
1736           = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
1737       else
1738         return false;
1739       break;
1740
1741     case OPT_fdiagnostics_show_option:
1742       global_dc->show_option_requested = value;
1743       break;
1744
1745     case OPT_fdump_:
1746       if (!dump_switch_p (arg))
1747         return false;
1748       break;
1749
1750     case OPT_fexcess_precision_:
1751       if (!strcmp (arg, "fast"))
1752         flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
1753       else if (!strcmp (arg, "standard"))
1754         flag_excess_precision_cmdline = EXCESS_PRECISION_STANDARD;
1755       else
1756         error ("unknown excess precision style \"%s\"", arg);
1757       break;
1758
1759     case OPT_ffast_math:
1760       set_fast_math_flags (value);
1761       break;
1762
1763     case OPT_funsafe_math_optimizations:
1764       set_unsafe_math_optimizations_flags (value);
1765       break;
1766
1767     case OPT_ffixed_:
1768       fix_register (arg, 1, 1);
1769       break;
1770
1771     case OPT_finline_limit_:
1772       set_param_value ("max-inline-insns-single", value / 2);
1773       set_param_value ("max-inline-insns-auto", value / 2);
1774       break;
1775
1776     case OPT_finstrument_functions_exclude_function_list_:
1777       add_comma_separated_to_vector
1778         (&flag_instrument_functions_exclude_functions, arg);
1779       break;
1780
1781     case OPT_finstrument_functions_exclude_file_list_:
1782       add_comma_separated_to_vector
1783         (&flag_instrument_functions_exclude_files, arg);
1784       break;
1785
1786     case OPT_fmessage_length_:
1787       pp_set_line_maximum_length (global_dc->printer, value);
1788       break;
1789
1790     case OPT_fpack_struct_:
1791       if (value <= 0 || (value & (value - 1)) || value > 16)
1792         error ("structure alignment must be a small power of two, not %d", value);
1793       else
1794         {
1795           initial_max_fld_align = value;
1796           maximum_field_alignment = value * BITS_PER_UNIT;
1797         }
1798       break;
1799
1800     case OPT_fplugin_:
1801 #ifdef ENABLE_PLUGIN
1802       add_new_plugin (arg);
1803 #else
1804       error ("Plugin support is disabled.  Configure with --enable-plugin.");
1805 #endif
1806       break;
1807
1808     case OPT_fplugin_arg_:
1809 #ifdef ENABLE_PLUGIN
1810       parse_plugin_arg_opt (arg);
1811 #else
1812       error ("Plugin support is disabled.  Configure with --enable-plugin.");
1813 #endif
1814       break;
1815
1816     case OPT_fprofile_dir_:
1817       profile_data_prefix = xstrdup (arg);
1818       break;
1819
1820     case OPT_fprofile_use_:
1821       profile_data_prefix = xstrdup (arg);
1822       flag_profile_use = true;
1823       value = true;
1824       /* No break here - do -fprofile-use processing. */
1825     case OPT_fprofile_use:
1826       if (!opts_set->x_flag_branch_probabilities)
1827         flag_branch_probabilities = value;
1828       if (!opts_set->x_flag_profile_values)
1829         flag_profile_values = value;
1830       if (!opts_set->x_flag_unroll_loops)
1831         flag_unroll_loops = value;
1832       if (!opts_set->x_flag_peel_loops)
1833         flag_peel_loops = value;
1834       if (!opts_set->x_flag_tracer)
1835         flag_tracer = value;
1836       if (!opts_set->x_flag_value_profile_transformations)
1837         flag_value_profile_transformations = value;
1838       if (!opts_set->x_flag_inline_functions)
1839         flag_inline_functions = value;
1840       if (!opts_set->x_flag_ipa_cp)
1841         flag_ipa_cp = value;
1842       if (!opts_set->x_flag_ipa_cp_clone
1843           && value && flag_ipa_cp)
1844         flag_ipa_cp_clone = value;
1845       if (!opts_set->x_flag_predictive_commoning)
1846         flag_predictive_commoning = value;
1847       if (!opts_set->x_flag_unswitch_loops)
1848         flag_unswitch_loops = value;
1849       if (!opts_set->x_flag_gcse_after_reload)
1850         flag_gcse_after_reload = value;
1851       break;
1852
1853     case OPT_fprofile_generate_:
1854       profile_data_prefix = xstrdup (arg);
1855       value = true;
1856       /* No break here - do -fprofile-generate processing. */
1857     case OPT_fprofile_generate:
1858       if (!opts_set->x_profile_arc_flag)
1859         profile_arc_flag = value;
1860       if (!opts_set->x_flag_profile_values)
1861         flag_profile_values = value;
1862       if (!opts_set->x_flag_value_profile_transformations)
1863         flag_value_profile_transformations = value;
1864       if (!opts_set->x_flag_inline_functions)
1865         flag_inline_functions = value;
1866       break;
1867
1868     case OPT_fshow_column:
1869       global_dc->show_column = value;
1870       break;
1871
1872     case OPT_fvisibility_:
1873       {
1874         if (!strcmp(arg, "default"))
1875           default_visibility = VISIBILITY_DEFAULT;
1876         else if (!strcmp(arg, "internal"))
1877           default_visibility = VISIBILITY_INTERNAL;
1878         else if (!strcmp(arg, "hidden"))
1879           default_visibility = VISIBILITY_HIDDEN;
1880         else if (!strcmp(arg, "protected"))
1881           default_visibility = VISIBILITY_PROTECTED;
1882         else
1883           error ("unrecognized visibility value \"%s\"", arg);
1884       }
1885       break;
1886
1887     case OPT_frandom_seed:
1888       /* The real switch is -fno-random-seed.  */
1889       if (value)
1890         return false;
1891       set_random_seed (NULL);
1892       break;
1893
1894     case OPT_frandom_seed_:
1895       set_random_seed (arg);
1896       break;
1897
1898     case OPT_fsched_verbose_:
1899 #ifdef INSN_SCHEDULING
1900       fix_sched_param ("verbose", arg);
1901       break;
1902 #else
1903       return false;
1904 #endif
1905
1906     case OPT_fsched_stalled_insns_:
1907       flag_sched_stalled_insns = value;
1908       if (flag_sched_stalled_insns == 0)
1909         flag_sched_stalled_insns = -1;
1910       break;
1911
1912     case OPT_fsched_stalled_insns_dep_:
1913       flag_sched_stalled_insns_dep = value;
1914       break;
1915
1916     case OPT_fstack_check_:
1917       if (!strcmp (arg, "no"))
1918         flag_stack_check = NO_STACK_CHECK;
1919       else if (!strcmp (arg, "generic"))
1920         /* This is the old stack checking method.  */
1921         flag_stack_check = STACK_CHECK_BUILTIN
1922                            ? FULL_BUILTIN_STACK_CHECK
1923                            : GENERIC_STACK_CHECK;
1924       else if (!strcmp (arg, "specific"))
1925         /* This is the new stack checking method.  */
1926         flag_stack_check = STACK_CHECK_BUILTIN
1927                            ? FULL_BUILTIN_STACK_CHECK
1928                            : STACK_CHECK_STATIC_BUILTIN
1929                              ? STATIC_BUILTIN_STACK_CHECK
1930                              : GENERIC_STACK_CHECK;
1931       else
1932         warning (0, "unknown stack check parameter \"%s\"", arg);
1933       break;
1934
1935     case OPT_fstack_limit:
1936       /* The real switch is -fno-stack-limit.  */
1937       if (value)
1938         return false;
1939       stack_limit_rtx = NULL_RTX;
1940       break;
1941
1942     case OPT_fstack_limit_register_:
1943       {
1944         int reg = decode_reg_name (arg);
1945         if (reg < 0)
1946           error ("unrecognized register name \"%s\"", arg);
1947         else
1948           stack_limit_rtx = gen_rtx_REG (Pmode, reg);
1949       }
1950       break;
1951
1952     case OPT_fstack_limit_symbol_:
1953       stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
1954       break;
1955
1956     case OPT_ftree_vectorizer_verbose_:
1957       vect_set_verbosity_level (arg);
1958       break;
1959
1960     case OPT_ftls_model_:
1961       if (!strcmp (arg, "global-dynamic"))
1962         flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
1963       else if (!strcmp (arg, "local-dynamic"))
1964         flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
1965       else if (!strcmp (arg, "initial-exec"))
1966         flag_tls_default = TLS_MODEL_INITIAL_EXEC;
1967       else if (!strcmp (arg, "local-exec"))
1968         flag_tls_default = TLS_MODEL_LOCAL_EXEC;
1969       else
1970         warning (0, "unknown tls-model \"%s\"", arg);
1971       break;
1972
1973     case OPT_fira_algorithm_:
1974       if (!strcmp (arg, "CB"))
1975         flag_ira_algorithm = IRA_ALGORITHM_CB;
1976       else if (!strcmp (arg, "priority"))
1977         flag_ira_algorithm = IRA_ALGORITHM_PRIORITY;
1978       else
1979         warning (0, "unknown ira algorithm \"%s\"", arg);
1980       break;
1981
1982     case OPT_fira_region_:
1983       if (!strcmp (arg, "one"))
1984         flag_ira_region = IRA_REGION_ONE;
1985       else if (!strcmp (arg, "all"))
1986         flag_ira_region = IRA_REGION_ALL;
1987       else if (!strcmp (arg, "mixed"))
1988         flag_ira_region = IRA_REGION_MIXED;
1989       else
1990         warning (0, "unknown ira region \"%s\"", arg);
1991       break;
1992
1993     case OPT_fira_verbose_:
1994       flag_ira_verbose = value;
1995       break;
1996
1997     case OPT_g:
1998       set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg);
1999       break;
2000
2001     case OPT_gcoff:
2002       set_debug_level (SDB_DEBUG, false, arg);
2003       break;
2004
2005     case OPT_gdwarf_:
2006       if (value < 2 || value > 4)
2007         error ("dwarf version %d is not supported", value);
2008       else
2009         dwarf_version = value;
2010       set_debug_level (DWARF2_DEBUG, false, "");
2011       break;
2012
2013     case OPT_ggdb:
2014       set_debug_level (NO_DEBUG, 2, arg);
2015       break;
2016
2017     case OPT_gstabs:
2018     case OPT_gstabs_:
2019       set_debug_level (DBX_DEBUG, code == OPT_gstabs_, arg);
2020       break;
2021
2022     case OPT_gvms:
2023       set_debug_level (VMS_DEBUG, false, arg);
2024       break;
2025
2026     case OPT_gxcoff:
2027     case OPT_gxcoff_:
2028       set_debug_level (XCOFF_DEBUG, code == OPT_gxcoff_, arg);
2029       break;
2030
2031     case OPT_o:
2032       asm_file_name = arg;
2033       break;
2034
2035     case OPT_pedantic_errors:
2036       flag_pedantic_errors = pedantic = 1;
2037       global_dc->pedantic_errors = 1;
2038       break;
2039
2040     case OPT_fwhopr_:
2041       flag_whopr = arg;
2042       break;
2043
2044     case OPT_fwhopr:
2045       flag_whopr = "";
2046       break;
2047
2048     case OPT_w:
2049       global_dc->dc_inhibit_warnings = true;
2050       break;
2051
2052     case OPT_fuse_linker_plugin:
2053       /* No-op. Used by the driver and passed to us because it starts with f.*/
2054       break;
2055
2056     default:
2057       /* If the flag was handled in a standard way, assume the lack of
2058          processing here is intentional.  */
2059       gcc_assert (option_flag_var (scode, opts));
2060       break;
2061     }
2062
2063   return true;
2064 }
2065
2066 /* Handle --param NAME=VALUE.  */
2067 static void
2068 handle_param (const char *carg)
2069 {
2070   char *equal, *arg;
2071   int value;
2072
2073   arg = xstrdup (carg);
2074   equal = strchr (arg, '=');
2075   if (!equal)
2076     error ("%s: --param arguments should be of the form NAME=VALUE", arg);
2077   else
2078     {
2079       value = integral_argument (equal + 1);
2080       if (value == -1)
2081         error ("invalid --param value %qs", equal + 1);
2082       else
2083         {
2084           *equal = '\0';
2085           set_param_value (arg, value);
2086         }
2087     }
2088
2089   free (arg);
2090 }
2091
2092 /* Used to set the level of strict aliasing warnings,
2093    when no level is specified (i.e., when -Wstrict-aliasing, and not
2094    -Wstrict-aliasing=level was given).
2095    ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified,
2096    and 0 otherwise.  After calling this function, wstrict_aliasing will be
2097    set to the default value of -Wstrict_aliasing=level, currently 3.  */
2098 void
2099 set_Wstrict_aliasing (int onoff)
2100 {
2101   gcc_assert (onoff == 0 || onoff == 1);
2102   if (onoff != 0)
2103     warn_strict_aliasing = 3;
2104   else
2105     warn_strict_aliasing = 0;
2106 }
2107
2108 /* The following routines are useful in setting all the flags that
2109    -ffast-math and -fno-fast-math imply.  */
2110 void
2111 set_fast_math_flags (int set)
2112 {
2113   flag_unsafe_math_optimizations = set;
2114   set_unsafe_math_optimizations_flags (set);
2115   flag_finite_math_only = set;
2116   flag_errno_math = !set;
2117   if (set)
2118     {
2119       flag_signaling_nans = 0;
2120       flag_rounding_math = 0;
2121       flag_cx_limited_range = 1;
2122     }
2123 }
2124
2125 /* When -funsafe-math-optimizations is set the following
2126    flags are set as well.  */
2127 void
2128 set_unsafe_math_optimizations_flags (int set)
2129 {
2130   flag_trapping_math = !set;
2131   flag_signed_zeros = !set;
2132   flag_associative_math = set;
2133   flag_reciprocal_math = set;
2134 }
2135
2136 /* Return true iff flags are set as if -ffast-math.  */
2137 bool
2138 fast_math_flags_set_p (void)
2139 {
2140   return (!flag_trapping_math
2141           && flag_unsafe_math_optimizations
2142           && flag_finite_math_only
2143           && !flag_signed_zeros
2144           && !flag_errno_math);
2145 }
2146
2147 /* Return true iff flags are set as if -ffast-math but using the flags stored
2148    in the struct cl_optimization structure.  */
2149 bool
2150 fast_math_flags_struct_set_p (struct cl_optimization *opt)
2151 {
2152   return (!opt->x_flag_trapping_math
2153           && opt->x_flag_unsafe_math_optimizations
2154           && opt->x_flag_finite_math_only
2155           && !opt->x_flag_signed_zeros
2156           && !opt->x_flag_errno_math);
2157 }
2158
2159 /* Handle a debug output -g switch.  EXTENDED is true or false to support
2160    extended output (2 is special and means "-ggdb" was given).  */
2161 static void
2162 set_debug_level (enum debug_info_type type, int extended, const char *arg)
2163 {
2164   static bool type_explicit;
2165
2166   use_gnu_debug_info_extensions = extended;
2167
2168   if (type == NO_DEBUG)
2169     {
2170       if (write_symbols == NO_DEBUG)
2171         {
2172           write_symbols = PREFERRED_DEBUGGING_TYPE;
2173
2174           if (extended == 2)
2175             {
2176 #ifdef DWARF2_DEBUGGING_INFO
2177               write_symbols = DWARF2_DEBUG;
2178 #elif defined DBX_DEBUGGING_INFO
2179               write_symbols = DBX_DEBUG;
2180 #endif
2181             }
2182
2183           if (write_symbols == NO_DEBUG)
2184             warning (0, "target system does not support debug output");
2185         }
2186     }
2187   else
2188     {
2189       /* Does it conflict with an already selected type?  */
2190       if (type_explicit && write_symbols != NO_DEBUG && type != write_symbols)
2191         error ("debug format \"%s\" conflicts with prior selection",
2192                debug_type_names[type]);
2193       write_symbols = type;
2194       type_explicit = true;
2195     }
2196
2197   /* A debug flag without a level defaults to level 2.  */
2198   if (*arg == '\0')
2199     {
2200       if (!debug_info_level)
2201         debug_info_level = DINFO_LEVEL_NORMAL;
2202     }
2203   else
2204     {
2205       int argval = integral_argument (arg);
2206       if (argval == -1)
2207         error ("unrecognised debug output level \"%s\"", arg);
2208       else if (argval > 3)
2209         error ("debug output level %s is too high", arg);
2210       else
2211         debug_info_level = (enum debug_info_level) argval;
2212     }
2213 }
2214
2215 /* Return 1 if option OPT_IDX is enabled in OPTS, 0 if it is disabled,
2216    or -1 if it isn't a simple on-off switch.  */
2217
2218 int
2219 option_enabled (int opt_idx, void *opts)
2220 {
2221   const struct cl_option *option = &(cl_options[opt_idx]);
2222   struct gcc_options *optsg = (struct gcc_options *) opts;
2223   void *flag_var = option_flag_var (opt_idx, optsg);
2224
2225   if (flag_var)
2226     switch (option->var_type)
2227       {
2228       case CLVC_BOOLEAN:
2229         return *(int *) flag_var != 0;
2230
2231       case CLVC_EQUAL:
2232         return *(int *) flag_var == option->var_value;
2233
2234       case CLVC_BIT_CLEAR:
2235         return (*(int *) flag_var & option->var_value) == 0;
2236
2237       case CLVC_BIT_SET:
2238         return (*(int *) flag_var & option->var_value) != 0;
2239
2240       case CLVC_STRING:
2241         break;
2242       }
2243   return -1;
2244 }
2245
2246 /* Fill STATE with the current state of option OPTION in OPTS.  Return
2247    true if there is some state to store.  */
2248
2249 bool
2250 get_option_state (struct gcc_options *opts, int option,
2251                   struct cl_option_state *state)
2252 {
2253   void *flag_var = option_flag_var (option, opts);
2254
2255   if (flag_var == 0)
2256     return false;
2257
2258   switch (cl_options[option].var_type)
2259     {
2260     case CLVC_BOOLEAN:
2261     case CLVC_EQUAL:
2262       state->data = flag_var;
2263       state->size = sizeof (int);
2264       break;
2265
2266     case CLVC_BIT_CLEAR:
2267     case CLVC_BIT_SET:
2268       state->ch = option_enabled (option, opts);
2269       state->data = &state->ch;
2270       state->size = 1;
2271       break;
2272
2273     case CLVC_STRING:
2274       state->data = *(const char **) flag_var;
2275       if (state->data == 0)
2276         state->data = "";
2277       state->size = strlen ((const char *) state->data) + 1;
2278       break;
2279     }
2280   return true;
2281 }
2282
2283 /* Callback function, called when -Werror= enables a warning.  */
2284
2285 static void (*warning_as_error_callback) (int) = NULL;
2286
2287 /* Register a callback for enable_warning_as_error calls.  */
2288
2289 void
2290 register_warning_as_error_callback (void (*callback) (int))
2291 {
2292   gcc_assert (warning_as_error_callback == NULL || callback == NULL);
2293   warning_as_error_callback = callback;
2294 }
2295
2296 /* Enable (or disable if VALUE is 0) a warning option ARG (language
2297    mask LANG_MASK, option handlers HANDLERS) as an error for
2298    diagnostic context DC (possibly NULL).  This is used by
2299    -Werror=.  */
2300
2301 void
2302 enable_warning_as_error (const char *arg, int value, unsigned int lang_mask,
2303                          const struct cl_option_handlers *handlers,
2304                          diagnostic_context *dc)
2305 {
2306   char *new_option;
2307   int option_index;
2308
2309   new_option = XNEWVEC (char, strlen (arg) + 2);
2310   new_option[0] = 'W';
2311   strcpy (new_option + 1, arg);
2312   option_index = find_opt (new_option, lang_mask);
2313   if (option_index == OPT_SPECIAL_unknown)
2314     {
2315       error ("-Werror=%s: No option -%s", arg, new_option);
2316     }
2317   else
2318     {
2319       const struct cl_option *option = &cl_options[option_index];
2320       const diagnostic_t kind = value ? DK_ERROR : DK_WARNING;
2321
2322       if (option->alias_target != N_OPTS)
2323         option_index = option->alias_target;
2324       if (option_index == OPT_SPECIAL_ignore)
2325         return;
2326       if (dc)
2327         diagnostic_classify_diagnostic (dc, option_index, kind,
2328                                         UNKNOWN_LOCATION);
2329       if (kind == DK_ERROR)
2330         {
2331           const struct cl_option * const option = cl_options + option_index;
2332
2333           /* -Werror=foo implies -Wfoo.  */
2334           if (option->var_type == CLVC_BOOLEAN)
2335             handle_generated_option (&global_options, &global_options_set,
2336                                      option_index, NULL, value, lang_mask,
2337                                      (int)kind, handlers,
2338                                      dc);
2339
2340           if (warning_as_error_callback)
2341             warning_as_error_callback (option_index);
2342         }
2343     }
2344   free (new_option);
2345 }
2346
2347 /* Return malloced memory for the name of the option OPTION_INDEX
2348    which enabled a diagnostic (context CONTEXT), originally of type
2349    ORIG_DIAG_KIND but possibly converted to DIAG_KIND by options such
2350    as -Werror.  */
2351
2352 char *
2353 option_name (diagnostic_context *context, int option_index,
2354              diagnostic_t orig_diag_kind, diagnostic_t diag_kind)
2355 {
2356   if (option_index)
2357     {
2358       /* A warning classified as an error.  */
2359       if ((orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN)
2360           && diag_kind == DK_ERROR)
2361         return concat (cl_options[OPT_Werror_].opt_text,
2362                        /* Skip over "-W".  */
2363                        cl_options[option_index].opt_text + 2,
2364                        NULL);
2365       /* A warning with option.  */
2366       else
2367         return xstrdup (cl_options[option_index].opt_text);
2368     }
2369   /* A warning without option classified as an error.  */
2370   else if (orig_diag_kind == DK_WARNING || orig_diag_kind == DK_PEDWARN
2371            || diag_kind == DK_WARNING)
2372     {
2373       if (context->warning_as_error_requested)
2374         return xstrdup (cl_options[OPT_Werror].opt_text);
2375       else
2376         return xstrdup (_("enabled by default"));
2377     }
2378   else
2379     return NULL;
2380 }