PR 30735
[official-gcc.git] / gcc / opts.c
blob195e4e18c3ca63c7e3ffdfa11c8e59b3127a8834
1 /* Command line option handling.
2 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Neil Booth.
6 This file is part of GCC.
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 2, or (at your option) any later
11 version.
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.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "intl.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "tree.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "output.h"
32 #include "langhooks.h"
33 #include "opts.h"
34 #include "options.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "params.h"
38 #include "diagnostic.h"
39 #include "tm_p.h" /* For OPTIMIZATION_OPTIONS. */
40 #include "insn-attr.h" /* For INSN_SCHEDULING. */
41 #include "target.h"
42 #include "tree-pass.h"
44 /* Value of the -G xx switch, and whether it was passed or not. */
45 unsigned HOST_WIDE_INT g_switch_value;
46 bool g_switch_set;
48 /* True if we should exit after parsing options. */
49 bool exit_after_options;
51 /* Print various extra warnings. -W/-Wextra. */
52 bool extra_warnings;
54 /* True to warn about any objects definitions whose size is larger
55 than N bytes. Also want about function definitions whose returned
56 values are larger than N bytes, where N is `larger_than_size'. */
57 bool warn_larger_than;
58 HOST_WIDE_INT larger_than_size;
60 /* Nonzero means warn about constructs which might not be
61 strict-aliasing safe. */
62 int warn_strict_aliasing;
64 /* Nonzero means warn about optimizations which rely on undefined
65 signed overflow. */
66 int warn_strict_overflow;
68 /* Hack for cooperation between set_Wunused and set_Wextra. */
69 static bool maybe_warn_unused_parameter;
71 /* Type(s) of debugging information we are producing (if any). See
72 flags.h for the definitions of the different possible types of
73 debugging information. */
74 enum debug_info_type write_symbols = NO_DEBUG;
76 /* Level of debugging information we are producing. See flags.h for
77 the definitions of the different possible levels. */
78 enum debug_info_level debug_info_level = DINFO_LEVEL_NONE;
80 /* Nonzero means use GNU-only extensions in the generated symbolic
81 debugging information. Currently, this only has an effect when
82 write_symbols is set to DBX_DEBUG, XCOFF_DEBUG, or DWARF_DEBUG. */
83 bool use_gnu_debug_info_extensions;
85 /* The default visibility for all symbols (unless overridden) */
86 enum symbol_visibility default_visibility = VISIBILITY_DEFAULT;
88 /* Disable unit-at-a-time for frontends that might be still broken in this
89 respect. */
91 bool no_unit_at_a_time_default;
93 /* Global visibility options. */
94 struct visibility_flags visibility_options;
96 /* What to print when a switch has no documentation. */
97 static const char undocumented_msg[] = N_("This switch lacks documentation");
99 /* Used for bookkeeping on whether user set these flags so
100 -fprofile-use/-fprofile-generate does not use them. */
101 static bool profile_arc_flag_set, flag_profile_values_set;
102 static bool flag_unroll_loops_set, flag_tracer_set;
103 static bool flag_value_profile_transformations_set;
104 static bool flag_peel_loops_set, flag_branch_probabilities_set;
106 /* Input file names. */
107 const char **in_fnames;
108 unsigned num_in_fnames;
110 static int common_handle_option (size_t scode, const char *arg, int value,
111 unsigned int lang_mask);
112 static void handle_param (const char *);
113 static void set_Wextra (int);
114 static unsigned int handle_option (const char **argv, unsigned int lang_mask);
115 static char *write_langs (unsigned int lang_mask);
116 static void complain_wrong_lang (const char *, const struct cl_option *,
117 unsigned int lang_mask);
118 static void handle_options (unsigned int, const char **, unsigned int);
119 static void set_debug_level (enum debug_info_type type, int extended,
120 const char *arg);
122 /* If ARG is a non-negative integer made up solely of digits, return its
123 value, otherwise return -1. */
124 static int
125 integral_argument (const char *arg)
127 const char *p = arg;
129 while (*p && ISDIGIT (*p))
130 p++;
132 if (*p == '\0')
133 return atoi (arg);
135 return -1;
138 /* Return a malloced slash-separated list of languages in MASK. */
139 static char *
140 write_langs (unsigned int mask)
142 unsigned int n = 0, len = 0;
143 const char *lang_name;
144 char *result;
146 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
147 if (mask & (1U << n))
148 len += strlen (lang_name) + 1;
150 result = XNEWVEC (char, len);
151 len = 0;
152 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
153 if (mask & (1U << n))
155 if (len)
156 result[len++] = '/';
157 strcpy (result + len, lang_name);
158 len += strlen (lang_name);
161 result[len] = 0;
163 return result;
166 /* Complain that switch OPT_INDEX does not apply to this front end. */
167 static void
168 complain_wrong_lang (const char *text, const struct cl_option *option,
169 unsigned int lang_mask)
171 char *ok_langs, *bad_lang;
173 ok_langs = write_langs (option->flags);
174 bad_lang = write_langs (lang_mask);
176 /* Eventually this should become a hard error IMO. */
177 warning (0, "command line option \"%s\" is valid for %s but not for %s",
178 text, ok_langs, bad_lang);
180 free (ok_langs);
181 free (bad_lang);
184 /* Handle the switch beginning at ARGV for the language indicated by
185 LANG_MASK. Returns the number of switches consumed. */
186 static unsigned int
187 handle_option (const char **argv, unsigned int lang_mask)
189 size_t opt_index;
190 const char *opt, *arg = 0;
191 char *dup = 0;
192 int value = 1;
193 unsigned int result = 0;
194 const struct cl_option *option;
196 opt = argv[0];
198 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
199 if (opt_index == cl_options_count
200 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
201 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
203 /* Drop the "no-" from negative switches. */
204 size_t len = strlen (opt) - 3;
206 dup = XNEWVEC (char, len + 1);
207 dup[0] = '-';
208 dup[1] = opt[1];
209 memcpy (dup + 2, opt + 5, len - 2 + 1);
210 opt = dup;
211 value = 0;
212 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
215 if (opt_index == cl_options_count)
216 goto done;
218 option = &cl_options[opt_index];
220 /* Reject negative form of switches that don't take negatives as
221 unrecognized. */
222 if (!value && (option->flags & CL_REJECT_NEGATIVE))
223 goto done;
225 /* We've recognized this switch. */
226 result = 1;
228 /* Check to see if the option is disabled for this configuration. */
229 if (option->flags & CL_DISABLED)
231 error ("command line option %qs"
232 " is not supported by this configuration", opt);
233 goto done;
236 /* Sort out any argument the switch takes. */
237 if (option->flags & CL_JOINED)
239 /* Have arg point to the original switch. This is because
240 some code, such as disable_builtin_function, expects its
241 argument to be persistent until the program exits. */
242 arg = argv[0] + cl_options[opt_index].opt_len + 1;
243 if (!value)
244 arg += strlen ("no-");
246 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
248 if (option->flags & CL_SEPARATE)
250 arg = argv[1];
251 result = 2;
253 else
254 /* Missing argument. */
255 arg = NULL;
258 else if (option->flags & CL_SEPARATE)
260 arg = argv[1];
261 result = 2;
264 /* Now we've swallowed any potential argument, complain if this
265 is a switch for a different front end. */
266 if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
268 complain_wrong_lang (argv[0], option, lang_mask);
269 goto done;
271 else if ((option->flags & CL_TARGET)
272 && (option->flags & CL_LANG_ALL)
273 && !(option->flags & lang_mask))
275 /* Complain for target flag language mismatches if any languages
276 are specified. */
277 complain_wrong_lang (argv[0], option, lang_mask);
278 goto done;
281 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
283 if (!lang_hooks.missing_argument (opt, opt_index))
284 error ("missing argument to \"%s\"", opt);
285 goto done;
288 /* If the switch takes an integer, convert it. */
289 if (arg && (option->flags & CL_UINTEGER))
291 value = integral_argument (arg);
292 if (value == -1)
294 error ("argument to \"%s\" should be a non-negative integer",
295 option->opt_text);
296 goto done;
300 if (option->flag_var)
301 switch (option->var_type)
303 case CLVC_BOOLEAN:
304 *(int *) option->flag_var = value;
305 break;
307 case CLVC_EQUAL:
308 *(int *) option->flag_var = (value
309 ? option->var_value
310 : !option->var_value);
311 break;
313 case CLVC_BIT_CLEAR:
314 case CLVC_BIT_SET:
315 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
316 *(int *) option->flag_var |= option->var_value;
317 else
318 *(int *) option->flag_var &= ~option->var_value;
319 if (option->flag_var == &target_flags)
320 target_flags_explicit |= option->var_value;
321 break;
323 case CLVC_STRING:
324 *(const char **) option->flag_var = arg;
325 break;
328 if (option->flags & lang_mask)
329 if (lang_hooks.handle_option (opt_index, arg, value) == 0)
330 result = 0;
332 if (result && (option->flags & CL_COMMON))
333 if (common_handle_option (opt_index, arg, value, lang_mask) == 0)
334 result = 0;
336 if (result && (option->flags & CL_TARGET))
337 if (!targetm.handle_option (opt_index, arg, value))
338 result = 0;
340 done:
341 if (dup)
342 free (dup);
343 return result;
346 /* Handle FILENAME from the command line. */
347 static void
348 add_input_filename (const char *filename)
350 num_in_fnames++;
351 in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
352 in_fnames[num_in_fnames - 1] = filename;
355 /* Decode and handle the vector of command line options. LANG_MASK
356 contains has a single bit set representing the current
357 language. */
358 static void
359 handle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
361 unsigned int n, i;
363 for (i = 1; i < argc; i += n)
365 const char *opt = argv[i];
367 /* Interpret "-" or a non-switch as a file name. */
368 if (opt[0] != '-' || opt[1] == '\0')
370 if (main_input_filename == NULL)
371 main_input_filename = opt;
372 add_input_filename (opt);
373 n = 1;
374 continue;
377 n = handle_option (argv + i, lang_mask);
379 if (!n)
381 n = 1;
382 error ("unrecognized command line option \"%s\"", opt);
387 /* Parse command line options and set default flag values. Do minimal
388 options processing. */
389 void
390 decode_options (unsigned int argc, const char **argv)
392 unsigned int i, lang_mask;
394 /* Perform language-specific options initialization. */
395 lang_mask = lang_hooks.init_options (argc, argv);
397 lang_hooks.initialize_diagnostics (global_dc);
399 /* Scan to see what optimization level has been specified. That will
400 determine the default value of many flags. */
401 for (i = 1; i < argc; i++)
403 if (!strcmp (argv[i], "-O"))
405 optimize = 1;
406 optimize_size = 0;
408 else if (argv[i][0] == '-' && argv[i][1] == 'O')
410 /* Handle -Os, -O2, -O3, -O69, ... */
411 const char *p = &argv[i][2];
413 if ((p[0] == 's') && (p[1] == 0))
415 optimize_size = 1;
417 /* Optimizing for size forces optimize to be 2. */
418 optimize = 2;
420 else
422 const int optimize_val = read_integral_parameter (p, p - 2, -1);
423 if (optimize_val != -1)
425 optimize = optimize_val;
426 optimize_size = 0;
432 if (!optimize)
434 flag_merge_constants = 0;
437 if (optimize >= 1)
439 flag_defer_pop = 1;
440 #ifdef DELAY_SLOTS
441 flag_delayed_branch = 1;
442 #endif
443 #ifdef CAN_DEBUG_WITHOUT_FP
444 flag_omit_frame_pointer = 1;
445 #endif
446 flag_guess_branch_prob = 1;
447 flag_cprop_registers = 1;
448 flag_if_conversion = 1;
449 flag_if_conversion2 = 1;
450 flag_ipa_pure_const = 1;
451 flag_ipa_reference = 1;
452 flag_split_wide_types = 1;
453 flag_tree_ccp = 1;
454 flag_tree_dce = 1;
455 flag_tree_dom = 1;
456 flag_tree_dse = 1;
457 flag_tree_ter = 1;
458 flag_tree_sra = 1;
459 flag_tree_copyrename = 1;
460 flag_tree_fre = 1;
461 flag_tree_copy_prop = 1;
462 flag_tree_sink = 1;
463 flag_tree_salias = 1;
464 if (!no_unit_at_a_time_default)
465 flag_unit_at_a_time = 1;
467 if (!optimize_size)
469 /* Loop header copying usually increases size of the code. This used
470 not to be true, since quite often it is possible to verify that
471 the condition is satisfied in the first iteration and therefore
472 to eliminate it. Jump threading handles these cases now. */
473 flag_tree_ch = 1;
477 if (optimize >= 2)
479 flag_thread_jumps = 1;
480 flag_crossjumping = 1;
481 flag_optimize_sibling_calls = 1;
482 flag_forward_propagate = 1;
483 flag_cse_follow_jumps = 1;
484 flag_gcse = 1;
485 flag_expensive_optimizations = 1;
486 flag_ipa_type_escape = 1;
487 flag_rerun_cse_after_loop = 1;
488 flag_caller_saves = 1;
489 flag_peephole2 = 1;
490 #ifdef INSN_SCHEDULING
491 flag_schedule_insns = 1;
492 flag_schedule_insns_after_reload = 1;
493 #endif
494 flag_regmove = 1;
495 flag_strict_aliasing = 1;
496 flag_strict_overflow = 1;
497 flag_delete_null_pointer_checks = 1;
498 flag_reorder_blocks = 1;
499 flag_reorder_functions = 1;
500 flag_tree_store_ccp = 1;
501 flag_tree_store_copy_prop = 1;
502 flag_tree_vrp = 1;
504 if (!optimize_size)
506 /* PRE tends to generate bigger code. */
507 flag_tree_pre = 1;
510 /* Allow more virtual operators to increase alias precision. */
511 set_param_value ("max-aliased-vops", 500);
514 if (optimize >= 3)
516 flag_inline_functions = 1;
517 flag_unswitch_loops = 1;
518 flag_gcse_after_reload = 1;
520 /* Allow even more virtual operators. */
521 set_param_value ("max-aliased-vops", 1000);
522 set_param_value ("avg-aliased-vops", 3);
525 if (optimize < 2 || optimize_size)
527 align_loops = 1;
528 align_jumps = 1;
529 align_labels = 1;
530 align_functions = 1;
532 /* Don't reorder blocks when optimizing for size because extra
533 jump insns may be created; also barrier may create extra padding.
535 More correctly we should have a block reordering mode that tried
536 to minimize the combined size of all the jumps. This would more
537 or less automatically remove extra jumps, but would also try to
538 use more short jumps instead of long jumps. */
539 flag_reorder_blocks = 0;
540 flag_reorder_blocks_and_partition = 0;
543 if (optimize_size)
545 /* Inlining of very small functions usually reduces total size. */
546 set_param_value ("max-inline-insns-single", 5);
547 set_param_value ("max-inline-insns-auto", 5);
548 flag_inline_functions = 1;
550 /* We want to crossjump as much as possible. */
551 set_param_value ("min-crossjump-insns", 1);
554 /* Initialize whether `char' is signed. */
555 flag_signed_char = DEFAULT_SIGNED_CHAR;
556 /* Set this to a special "uninitialized" value. The actual default is set
557 after target options have been processed. */
558 flag_short_enums = 2;
560 /* Initialize target_flags before OPTIMIZATION_OPTIONS so the latter can
561 modify it. */
562 target_flags = targetm.default_target_flags;
564 /* Some tagets have ABI-specified unwind tables. */
565 flag_unwind_tables = targetm.unwind_tables_default;
567 #ifdef OPTIMIZATION_OPTIONS
568 /* Allow default optimizations to be specified on a per-machine basis. */
569 OPTIMIZATION_OPTIONS (optimize, optimize_size);
570 #endif
572 handle_options (argc, argv, lang_mask);
574 if (flag_pie)
575 flag_pic = flag_pie;
576 if (flag_pic && !flag_pie)
577 flag_shlib = 1;
579 if (flag_no_inline == 2)
580 flag_no_inline = 0;
581 else
582 flag_really_no_inline = flag_no_inline;
584 /* Set flag_no_inline before the post_options () hook. The C front
585 ends use it to determine tree inlining defaults. FIXME: such
586 code should be lang-independent when all front ends use tree
587 inlining, in which case it, and this condition, should be moved
588 to the top of process_options() instead. */
589 if (optimize == 0)
591 /* Inlining does not work if not optimizing,
592 so force it not to be done. */
593 flag_no_inline = 1;
594 warn_inline = 0;
596 /* The c_decode_option function and decode_option hook set
597 this to `2' if -Wall is used, so we can avoid giving out
598 lots of errors for people who don't realize what -Wall does. */
599 if (warn_uninitialized == 1)
600 warning (OPT_Wuninitialized,
601 "-Wuninitialized is not supported without -O");
604 if (flag_really_no_inline == 2)
605 flag_really_no_inline = flag_no_inline;
607 /* The optimization to partition hot and cold basic blocks into separate
608 sections of the .o and executable files does not work (currently)
609 with exception handling. This is because there is no support for
610 generating unwind info. If flag_exceptions is turned on we need to
611 turn off the partitioning optimization. */
613 if (flag_exceptions && flag_reorder_blocks_and_partition)
615 inform
616 ("-freorder-blocks-and-partition does not work with exceptions");
617 flag_reorder_blocks_and_partition = 0;
618 flag_reorder_blocks = 1;
621 /* If user requested unwind info, then turn off the partitioning
622 optimization. */
624 if (flag_unwind_tables && ! targetm.unwind_tables_default
625 && flag_reorder_blocks_and_partition)
627 inform ("-freorder-blocks-and-partition does not support unwind info");
628 flag_reorder_blocks_and_partition = 0;
629 flag_reorder_blocks = 1;
632 /* If the target requested unwind info, then turn off the partitioning
633 optimization with a different message. Likewise, if the target does not
634 support named sections. */
636 if (flag_reorder_blocks_and_partition
637 && (!targetm.have_named_sections
638 || (flag_unwind_tables && targetm.unwind_tables_default)))
640 inform
641 ("-freorder-blocks-and-partition does not work on this architecture");
642 flag_reorder_blocks_and_partition = 0;
643 flag_reorder_blocks = 1;
647 #define LEFT_COLUMN 27
649 /* Output ITEM, of length ITEM_WIDTH, in the left column,
650 followed by word-wrapped HELP in a second column. */
651 static void
652 wrap_help (const char *help,
653 const char *item,
654 unsigned int item_width,
655 unsigned int columns)
657 unsigned int col_width = LEFT_COLUMN;
658 unsigned int remaining, room, len;
660 remaining = strlen (help);
664 room = columns - 3 - MAX (col_width, item_width);
665 if (room > columns)
666 room = 0;
667 len = remaining;
669 if (room < len)
671 unsigned int i;
673 for (i = 0; help[i]; i++)
675 if (i >= room && len != remaining)
676 break;
677 if (help[i] == ' ')
678 len = i;
679 else if ((help[i] == '-' || help[i] == '/')
680 && help[i + 1] != ' '
681 && i > 0 && ISALPHA (help[i - 1]))
682 len = i + 1;
686 printf( " %-*.*s %.*s\n", col_width, item_width, item, len, help);
687 item_width = 0;
688 while (help[len] == ' ')
689 len++;
690 help += len;
691 remaining -= len;
693 while (remaining);
696 /* Print help for a specific front-end, etc. */
697 static void
698 print_filtered_help (unsigned int include_flags,
699 unsigned int exclude_flags,
700 unsigned int any_flags,
701 unsigned int columns)
703 unsigned int i;
704 const char *help;
705 static char *printed = NULL;
706 bool found = false;
707 bool displayed = false;
709 if (include_flags == CL_PARAMS)
711 for (i = 0; i < LAST_PARAM; i++)
713 const char *param = compiler_params[i].option;
715 help = compiler_params[i].help;
716 if (help == NULL || *help == '\0')
718 if (exclude_flags & CL_UNDOCUMENTED)
719 continue;
720 help = undocumented_msg;
723 /* Get the translation. */
724 help = _(help);
726 wrap_help (help, param, strlen (param), columns);
728 putchar ('\n');
729 return;
732 if (!printed)
733 printed = xcalloc (1, cl_options_count);
735 for (i = 0; i < cl_options_count; i++)
737 static char new_help[128];
738 const struct cl_option *option = cl_options + i;
739 unsigned int len;
740 const char *opt;
741 const char *tab;
743 if (include_flags == 0
744 || ((option->flags & include_flags) != include_flags))
746 if ((option->flags & any_flags) == 0)
747 continue;
750 /* Skip unwanted switches. */
751 if ((option->flags & exclude_flags) != 0)
752 continue;
754 found = true;
755 /* Skip switches that have already been printed. */
756 if (printed[i])
757 continue;
759 printed[i] = true;
761 help = option->help;
762 if (help == NULL)
764 if (exclude_flags & CL_UNDOCUMENTED)
765 continue;
766 help = undocumented_msg;
769 /* Get the translation. */
770 help = _(help);
772 /* Find the gap between the name of the
773 option and its descriptive text. */
774 tab = strchr (help, '\t');
775 if (tab)
777 len = tab - help;
778 opt = help;
779 help = tab + 1;
781 else
783 opt = option->opt_text;
784 len = strlen (opt);
787 /* With the -Q option enabled we change the descriptive text associated
788 with an option to be an indication of its current setting. */
789 if (!quiet_flag)
791 if (len < (LEFT_COLUMN + 2))
792 strcpy (new_help, "\t\t");
793 else
794 strcpy (new_help, "\t");
796 if (option->flag_var != NULL)
798 if (option->flags & CL_JOINED)
800 if (option->var_type == CLVC_STRING)
802 if (* (const char **) option->flag_var != NULL)
803 snprintf (new_help + strlen (new_help),
804 sizeof (new_help) - strlen (new_help),
805 * (const char **) option->flag_var);
807 else
808 sprintf (new_help + strlen (new_help),
809 "%#x", * (int *) option->flag_var);
811 else
812 strcat (new_help, option_enabled (i)
813 ? _("[enabled]") : _("[disabled]"));
816 help = new_help;
819 wrap_help (help, opt, len, columns);
820 displayed = true;
823 if (! found)
824 printf (_(" No options with the desired characteristics were found\n"));
825 else if (! displayed)
826 printf (_(" All options with the desired characteristics have already been displayed\n"));
828 putchar ('\n');
831 /* Display help for a specified type of option.
832 The options must have ALL of the INCLUDE_FLAGS set
833 ANY of the flags in the ANY_FLAGS set
834 and NONE of the EXCLUDE_FLAGS set. */
835 static void
836 print_specific_help (unsigned int include_flags,
837 unsigned int exclude_flags,
838 unsigned int any_flags)
840 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
841 const char * description = NULL;
842 const char * descrip_extra = "";
843 size_t i;
844 unsigned int flag;
845 static unsigned int columns = 0;
847 /* Sanity check: Make sure that we do not have more
848 languages than we have bits available to enumerate them. */
849 gcc_assert ((1U << cl_lang_count) < CL_MIN_OPTION_CLASS);
851 /* If we have not done so already, obtain
852 the desired maximum width of the output. */
853 if (columns == 0)
855 const char *p;
857 GET_ENVIRONMENT (p, "COLUMNS");
858 if (p != NULL)
860 int value = atoi (p);
862 if (value > 0)
863 columns = value;
866 if (columns == 0)
867 /* Use a reasonable default. */
868 columns = 80;
871 /* Decide upon the title for the options that we are going to display. */
872 for (i = 0, flag = 1; flag <= CL_MAX_OPTION_CLASS; flag <<= 1, i ++)
874 switch (flag & include_flags)
876 case 0:
877 break;
879 case CL_TARGET:
880 description = _("The following options are target specific");
881 break;
882 case CL_WARNING:
883 description = _("The following options control compiler warning messages");
884 break;
885 case CL_OPTIMIZATION:
886 description = _("The following options control optimizations");
887 break;
888 case CL_COMMON:
889 description = _("The following options are language-independent");
890 break;
891 case CL_PARAMS:
892 description = _("The --param option recognizes the following as parameters");
893 break;
894 default:
895 if (i >= cl_lang_count)
896 break;
897 if ((exclude_flags & ((1U << cl_lang_count) - 1)) != 0)
899 description = _("The following options are specific to the language ");
900 descrip_extra = lang_names [i];
902 else
903 description = _("The following options are supported by the language ");
904 descrip_extra = lang_names [i];
905 break;
909 if (description == NULL)
911 if (any_flags == 0)
913 if (include_flags == CL_UNDOCUMENTED)
914 description = _("The following options are not documented");
915 else
917 internal_error ("unrecognized include_flags 0x%x passed to print_specific_help",
918 include_flags);
919 return;
922 else
924 if (any_flags & all_langs_mask)
925 description = _("The following options are language-related");
926 else
927 description = _("The following options are language-independent");
931 printf ("%s%s:\n", description, descrip_extra);
932 print_filtered_help (include_flags, exclude_flags, any_flags, columns);
935 /* Handle target- and language-independent options. Return zero to
936 generate an "unknown option" message. Only options that need
937 extra handling need to be listed here; if you simply want
938 VALUE assigned to a variable, it happens automatically. */
940 static int
941 common_handle_option (size_t scode, const char *arg, int value,
942 unsigned int lang_mask)
944 enum opt_code code = (enum opt_code) scode;
946 switch (code)
948 case OPT__param:
949 handle_param (arg);
950 break;
952 case OPT_fhelp:
953 case OPT__help:
955 unsigned int all_langs_mask = (1U << cl_lang_count) - 1;
956 unsigned int undoc_mask;
957 unsigned int i;
959 undoc_mask = extra_warnings ? 0 : CL_UNDOCUMENTED;
960 /* First display any single language specific options. */
961 for (i = 0; i < cl_lang_count; i++)
962 print_specific_help
963 (1U << i, (all_langs_mask & (~ (1U << i))) | undoc_mask, 0);
964 /* Next display any multi language specific options. */
965 print_specific_help (0, undoc_mask, all_langs_mask);
966 /* Then display any remaining, non-language options. */
967 for (i = CL_MIN_OPTION_CLASS; i <= CL_MAX_OPTION_CLASS; i <<= 1)
968 print_specific_help (i, undoc_mask, 0);
969 exit_after_options = true;
970 break;
973 case OPT_ftarget_help:
974 case OPT__target_help:
975 print_specific_help (CL_TARGET, CL_UNDOCUMENTED, 0);
976 exit_after_options = true;
977 break;
979 case OPT_fhelp_:
980 case OPT__help_:
982 const char * a = arg;
983 unsigned int include_flags = 0;
984 /* Note - by default we include undocumented options when listing
985 specific classes. If you only want to see documented options
986 then add ",^undocumented" to the --help= option. e.g.:
988 --help=target,^undocumented */
989 unsigned int exclude_flags = 0;
991 /* Walk along the argument string, parsing each word in turn.
992 The format is:
993 arg = [^]{word}[,{arg}]
994 word = {optimizers|target|warnings|undocumented|
995 params|common|<language>} */
996 while (* a != 0)
998 static struct
1000 const char * string;
1001 unsigned int flag;
1003 specifics[] =
1005 { "optimizers", CL_OPTIMIZATION },
1006 { "target", CL_TARGET },
1007 { "warnings", CL_WARNING },
1008 { "undocumented", CL_UNDOCUMENTED },
1009 { "params", CL_PARAMS },
1010 { "joined", CL_JOINED },
1011 { "separate", CL_SEPARATE },
1012 { "common", CL_COMMON },
1013 { NULL, 0 }
1015 unsigned int * pflags;
1016 char * comma;
1017 unsigned int len;
1018 unsigned int i;
1020 if (* a == '^')
1022 ++ a;
1023 pflags = & exclude_flags;
1025 else
1026 pflags = & include_flags;
1028 comma = strchr (a, ',');
1029 if (comma == NULL)
1030 len = strlen (a);
1031 else
1032 len = comma - a;
1034 for (i = 0; specifics[i].string != NULL; i++)
1035 if (strncasecmp (a, specifics[i].string, len) == 0)
1037 * pflags |= specifics[i].flag;
1038 break;
1041 if (specifics[i].string == NULL)
1043 /* Check to see if the string matches a language name. */
1044 for (i = 0; i < cl_lang_count; i++)
1045 if (strncasecmp (a, lang_names[i], len) == 0)
1047 * pflags |= 1U << i;
1048 break;
1051 if (i == cl_lang_count)
1052 fnotice (stderr,
1053 "warning: unrecognized argument to --help= switch: %.*s\n",
1054 len, a);
1057 if (comma == NULL)
1058 break;
1059 a = comma + 1;
1062 if (include_flags)
1063 print_specific_help (include_flags, exclude_flags, 0);
1064 exit_after_options = true;
1065 break;
1068 case OPT__version:
1069 print_version (stderr, "");
1070 exit_after_options = true;
1071 break;
1073 case OPT_G:
1074 g_switch_value = value;
1075 g_switch_set = true;
1076 break;
1078 case OPT_O:
1079 case OPT_Os:
1080 /* Currently handled in a prescan. */
1081 break;
1083 case OPT_W:
1084 /* For backward compatibility, -W is the same as -Wextra. */
1085 set_Wextra (value);
1086 break;
1088 case OPT_Werror_:
1089 enable_warning_as_error (arg, value, lang_mask);
1090 break;
1092 case OPT_Wextra:
1093 set_Wextra (value);
1094 break;
1096 case OPT_Wlarger_than_:
1097 larger_than_size = value;
1098 warn_larger_than = value != -1;
1099 break;
1101 case OPT_Wstrict_aliasing:
1102 case OPT_Wstrict_aliasing_:
1103 warn_strict_aliasing = value;
1104 break;
1106 case OPT_Wstrict_overflow:
1107 warn_strict_overflow = (value
1108 ? (int) WARN_STRICT_OVERFLOW_CONDITIONAL
1109 : 0);
1110 break;
1112 case OPT_Wstrict_overflow_:
1113 warn_strict_overflow = value;
1114 break;
1116 case OPT_Wunused:
1117 set_Wunused (value);
1118 break;
1120 case OPT_aux_info:
1121 case OPT_aux_info_:
1122 aux_info_file_name = arg;
1123 flag_gen_aux_info = 1;
1124 break;
1126 case OPT_auxbase:
1127 aux_base_name = arg;
1128 break;
1130 case OPT_auxbase_strip:
1132 char *tmp = xstrdup (arg);
1133 strip_off_ending (tmp, strlen (tmp));
1134 if (tmp[0])
1135 aux_base_name = tmp;
1137 break;
1139 case OPT_d:
1140 decode_d_option (arg);
1141 break;
1143 case OPT_dumpbase:
1144 dump_base_name = arg;
1145 break;
1147 case OPT_falign_functions_:
1148 align_functions = value;
1149 break;
1151 case OPT_falign_jumps_:
1152 align_jumps = value;
1153 break;
1155 case OPT_falign_labels_:
1156 align_labels = value;
1157 break;
1159 case OPT_falign_loops_:
1160 align_loops = value;
1161 break;
1163 case OPT_fbranch_probabilities:
1164 flag_branch_probabilities_set = true;
1165 break;
1167 case OPT_fcall_used_:
1168 fix_register (arg, 0, 1);
1169 break;
1171 case OPT_fcall_saved_:
1172 fix_register (arg, 0, 0);
1173 break;
1175 case OPT_fdiagnostics_show_location_:
1176 if (!strcmp (arg, "once"))
1177 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
1178 else if (!strcmp (arg, "every-line"))
1179 diagnostic_prefixing_rule (global_dc)
1180 = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
1181 else
1182 return 0;
1183 break;
1185 case OPT_fdiagnostics_show_option:
1186 global_dc->show_option_requested = true;
1187 break;
1189 case OPT_fdump_:
1190 if (!dump_switch_p (arg))
1191 return 0;
1192 break;
1194 case OPT_ffast_math:
1195 set_fast_math_flags (value);
1196 break;
1198 case OPT_ffixed_:
1199 fix_register (arg, 1, 1);
1200 break;
1202 case OPT_finline_limit_:
1203 case OPT_finline_limit_eq:
1204 set_param_value ("max-inline-insns-single", value / 2);
1205 set_param_value ("max-inline-insns-auto", value / 2);
1206 break;
1208 case OPT_fmessage_length_:
1209 pp_set_line_maximum_length (global_dc->printer, value);
1210 break;
1212 case OPT_fpack_struct_:
1213 if (value <= 0 || (value & (value - 1)) || value > 16)
1214 error ("structure alignment must be a small power of two, not %d", value);
1215 else
1217 initial_max_fld_align = value;
1218 maximum_field_alignment = value * BITS_PER_UNIT;
1220 break;
1222 case OPT_fpeel_loops:
1223 flag_peel_loops_set = true;
1224 break;
1226 case OPT_fprofile_arcs:
1227 profile_arc_flag_set = true;
1228 break;
1230 case OPT_fprofile_use:
1231 if (!flag_branch_probabilities_set)
1232 flag_branch_probabilities = value;
1233 if (!flag_profile_values_set)
1234 flag_profile_values = value;
1235 if (!flag_unroll_loops_set)
1236 flag_unroll_loops = value;
1237 if (!flag_peel_loops_set)
1238 flag_peel_loops = value;
1239 if (!flag_tracer_set)
1240 flag_tracer = value;
1241 if (!flag_value_profile_transformations_set)
1242 flag_value_profile_transformations = value;
1243 break;
1245 case OPT_fprofile_generate:
1246 if (!profile_arc_flag_set)
1247 profile_arc_flag = value;
1248 if (!flag_profile_values_set)
1249 flag_profile_values = value;
1250 if (!flag_value_profile_transformations_set)
1251 flag_value_profile_transformations = value;
1252 break;
1254 case OPT_fprofile_values:
1255 flag_profile_values_set = true;
1256 break;
1258 case OPT_fvisibility_:
1260 if (!strcmp(arg, "default"))
1261 default_visibility = VISIBILITY_DEFAULT;
1262 else if (!strcmp(arg, "internal"))
1263 default_visibility = VISIBILITY_INTERNAL;
1264 else if (!strcmp(arg, "hidden"))
1265 default_visibility = VISIBILITY_HIDDEN;
1266 else if (!strcmp(arg, "protected"))
1267 default_visibility = VISIBILITY_PROTECTED;
1268 else
1269 error ("unrecognized visibility value \"%s\"", arg);
1271 break;
1273 case OPT_fvpt:
1274 flag_value_profile_transformations_set = true;
1275 break;
1277 case OPT_frandom_seed:
1278 /* The real switch is -fno-random-seed. */
1279 if (value)
1280 return 0;
1281 set_random_seed (NULL);
1282 break;
1284 case OPT_frandom_seed_:
1285 set_random_seed (arg);
1286 break;
1288 case OPT_fsched_verbose_:
1289 #ifdef INSN_SCHEDULING
1290 fix_sched_param ("verbose", arg);
1291 break;
1292 #else
1293 return 0;
1294 #endif
1296 case OPT_fsched_stalled_insns_:
1297 flag_sched_stalled_insns = value;
1298 if (flag_sched_stalled_insns == 0)
1299 flag_sched_stalled_insns = -1;
1300 break;
1302 case OPT_fsched_stalled_insns_dep_:
1303 flag_sched_stalled_insns_dep = value;
1304 break;
1306 case OPT_fstack_limit:
1307 /* The real switch is -fno-stack-limit. */
1308 if (value)
1309 return 0;
1310 stack_limit_rtx = NULL_RTX;
1311 break;
1313 case OPT_fstack_limit_register_:
1315 int reg = decode_reg_name (arg);
1316 if (reg < 0)
1317 error ("unrecognized register name \"%s\"", arg);
1318 else
1319 stack_limit_rtx = gen_rtx_REG (Pmode, reg);
1321 break;
1323 case OPT_fstack_limit_symbol_:
1324 stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
1325 break;
1327 case OPT_ftree_vectorizer_verbose_:
1328 vect_set_verbosity_level (arg);
1329 break;
1331 case OPT_ftls_model_:
1332 if (!strcmp (arg, "global-dynamic"))
1333 flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
1334 else if (!strcmp (arg, "local-dynamic"))
1335 flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
1336 else if (!strcmp (arg, "initial-exec"))
1337 flag_tls_default = TLS_MODEL_INITIAL_EXEC;
1338 else if (!strcmp (arg, "local-exec"))
1339 flag_tls_default = TLS_MODEL_LOCAL_EXEC;
1340 else
1341 warning (0, "unknown tls-model \"%s\"", arg);
1342 break;
1344 case OPT_ftracer:
1345 flag_tracer_set = true;
1346 break;
1348 case OPT_funroll_loops:
1349 flag_unroll_loops_set = true;
1350 break;
1352 case OPT_g:
1353 set_debug_level (NO_DEBUG, DEFAULT_GDB_EXTENSIONS, arg);
1354 break;
1356 case OPT_gcoff:
1357 set_debug_level (SDB_DEBUG, false, arg);
1358 break;
1360 case OPT_gdwarf_2:
1361 set_debug_level (DWARF2_DEBUG, false, arg);
1362 break;
1364 case OPT_ggdb:
1365 set_debug_level (NO_DEBUG, 2, arg);
1366 break;
1368 case OPT_gstabs:
1369 case OPT_gstabs_:
1370 set_debug_level (DBX_DEBUG, code == OPT_gstabs_, arg);
1371 break;
1373 case OPT_gvms:
1374 set_debug_level (VMS_DEBUG, false, arg);
1375 break;
1377 case OPT_gxcoff:
1378 case OPT_gxcoff_:
1379 set_debug_level (XCOFF_DEBUG, code == OPT_gxcoff_, arg);
1380 break;
1382 case OPT_o:
1383 asm_file_name = arg;
1384 break;
1386 case OPT_pedantic_errors:
1387 flag_pedantic_errors = pedantic = 1;
1388 break;
1390 case OPT_floop_optimize:
1391 case OPT_frerun_loop_opt:
1392 case OPT_fstrength_reduce:
1393 /* These are no-ops, preserved for backward compatibility. */
1394 break;
1396 default:
1397 /* If the flag was handled in a standard way, assume the lack of
1398 processing here is intentional. */
1399 gcc_assert (cl_options[scode].flag_var);
1400 break;
1403 return 1;
1406 /* Handle --param NAME=VALUE. */
1407 static void
1408 handle_param (const char *carg)
1410 char *equal, *arg;
1411 int value;
1413 arg = xstrdup (carg);
1414 equal = strchr (arg, '=');
1415 if (!equal)
1416 error ("%s: --param arguments should be of the form NAME=VALUE", arg);
1417 else
1419 value = integral_argument (equal + 1);
1420 if (value == -1)
1421 error ("invalid --param value %qs", equal + 1);
1422 else
1424 *equal = '\0';
1425 set_param_value (arg, value);
1429 free (arg);
1432 /* Handle -W and -Wextra. */
1433 static void
1434 set_Wextra (int setting)
1436 extra_warnings = setting;
1437 warn_unused_parameter = (setting && maybe_warn_unused_parameter);
1439 /* We save the value of warn_uninitialized, since if they put
1440 -Wuninitialized on the command line, we need to generate a
1441 warning about not using it without also specifying -O. */
1442 if (setting == 0)
1443 warn_uninitialized = 0;
1444 else if (warn_uninitialized != 1)
1445 warn_uninitialized = 2;
1448 /* Initialize unused warning flags. */
1449 void
1450 set_Wunused (int setting)
1452 warn_unused_function = setting;
1453 warn_unused_label = setting;
1454 /* Unused function parameter warnings are reported when either
1455 ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
1456 Thus, if -Wextra has already been seen, set warn_unused_parameter;
1457 otherwise set maybe_warn_extra_parameter, which will be picked up
1458 by set_Wextra. */
1459 maybe_warn_unused_parameter = setting;
1460 warn_unused_parameter = (setting && extra_warnings);
1461 warn_unused_variable = setting;
1462 warn_unused_value = setting;
1465 /* The following routines are useful in setting all the flags that
1466 -ffast-math and -fno-fast-math imply. */
1467 void
1468 set_fast_math_flags (int set)
1470 flag_trapping_math = !set;
1471 flag_unsafe_math_optimizations = set;
1472 flag_finite_math_only = set;
1473 flag_signed_zeros = !set;
1474 flag_errno_math = !set;
1475 if (set)
1477 flag_signaling_nans = 0;
1478 flag_rounding_math = 0;
1479 flag_cx_limited_range = 1;
1483 /* Return true iff flags are set as if -ffast-math. */
1484 bool
1485 fast_math_flags_set_p (void)
1487 return (!flag_trapping_math
1488 && flag_unsafe_math_optimizations
1489 && flag_finite_math_only
1490 && !flag_signed_zeros
1491 && !flag_errno_math);
1494 /* Handle a debug output -g switch. EXTENDED is true or false to support
1495 extended output (2 is special and means "-ggdb" was given). */
1496 static void
1497 set_debug_level (enum debug_info_type type, int extended, const char *arg)
1499 static bool type_explicit;
1501 use_gnu_debug_info_extensions = extended;
1503 if (type == NO_DEBUG)
1505 if (write_symbols == NO_DEBUG)
1507 write_symbols = PREFERRED_DEBUGGING_TYPE;
1509 if (extended == 2)
1511 #ifdef DWARF2_DEBUGGING_INFO
1512 write_symbols = DWARF2_DEBUG;
1513 #elif defined DBX_DEBUGGING_INFO
1514 write_symbols = DBX_DEBUG;
1515 #endif
1518 if (write_symbols == NO_DEBUG)
1519 warning (0, "target system does not support debug output");
1522 else
1524 /* Does it conflict with an already selected type? */
1525 if (type_explicit && write_symbols != NO_DEBUG && type != write_symbols)
1526 error ("debug format \"%s\" conflicts with prior selection",
1527 debug_type_names[type]);
1528 write_symbols = type;
1529 type_explicit = true;
1532 /* A debug flag without a level defaults to level 2. */
1533 if (*arg == '\0')
1535 if (!debug_info_level)
1536 debug_info_level = 2;
1538 else
1540 debug_info_level = integral_argument (arg);
1541 if (debug_info_level == (unsigned int) -1)
1542 error ("unrecognised debug output level \"%s\"", arg);
1543 else if (debug_info_level > 3)
1544 error ("debug output level %s is too high", arg);
1548 /* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
1549 a simple on-off switch. */
1552 option_enabled (int opt_idx)
1554 const struct cl_option *option = &(cl_options[opt_idx]);
1556 if (option->flag_var)
1557 switch (option->var_type)
1559 case CLVC_BOOLEAN:
1560 return *(int *) option->flag_var != 0;
1562 case CLVC_EQUAL:
1563 return *(int *) option->flag_var == option->var_value;
1565 case CLVC_BIT_CLEAR:
1566 return (*(int *) option->flag_var & option->var_value) == 0;
1568 case CLVC_BIT_SET:
1569 return (*(int *) option->flag_var & option->var_value) != 0;
1571 case CLVC_STRING:
1572 break;
1574 return -1;
1577 /* Fill STATE with the current state of option OPTION. Return true if
1578 there is some state to store. */
1580 bool
1581 get_option_state (int option, struct cl_option_state *state)
1583 if (cl_options[option].flag_var == 0)
1584 return false;
1586 switch (cl_options[option].var_type)
1588 case CLVC_BOOLEAN:
1589 case CLVC_EQUAL:
1590 state->data = cl_options[option].flag_var;
1591 state->size = sizeof (int);
1592 break;
1594 case CLVC_BIT_CLEAR:
1595 case CLVC_BIT_SET:
1596 state->ch = option_enabled (option);
1597 state->data = &state->ch;
1598 state->size = 1;
1599 break;
1601 case CLVC_STRING:
1602 state->data = *(const char **) cl_options[option].flag_var;
1603 if (state->data == 0)
1604 state->data = "";
1605 state->size = strlen (state->data) + 1;
1606 break;
1608 return true;
1611 /* Enable a warning option as an error. This is used by -Werror= and
1612 also by legacy Werror-implicit-function-declaration. */
1614 void
1615 enable_warning_as_error (const char *arg, int value, unsigned int lang_mask)
1617 char *new_option;
1618 int option_index;
1620 new_option = XNEWVEC (char, strlen (arg) + 2);
1621 new_option[0] = 'W';
1622 strcpy (new_option + 1, arg);
1623 option_index = find_opt (new_option, lang_mask);
1624 if (option_index == N_OPTS)
1626 error ("-Werror=%s: No option -%s", arg, new_option);
1628 else
1630 int kind = value ? DK_ERROR : DK_WARNING;
1631 diagnostic_classify_diagnostic (global_dc, option_index, kind);
1633 /* -Werror=foo implies -Wfoo. */
1634 if (cl_options[option_index].var_type == CLVC_BOOLEAN
1635 && cl_options[option_index].flag_var
1636 && kind == DK_ERROR)
1637 *(int *) cl_options[option_index].flag_var = 1;
1639 free (new_option);