1 /* Command line option handling.
2 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Neil Booth.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 #include "coretypes.h"
30 #include "langhooks.h"
36 #include "diagnostic.h"
38 /* Value of the -G xx switch, and whether it was passed or not. */
39 unsigned HOST_WIDE_INT g_switch_value;
42 /* True if we should exit after parsing options. */
43 bool exit_after_options;
48 /* Print various extra warnings. -W/-Wextra. */
51 /* Don't print warning messages. -w. */
52 bool inhibit_warnings;
54 /* Treat warnings as errors. -Werror. */
55 bool warnings_are_errors;
57 /* Warn if a function returns an aggregate, since there are often
58 incompatible calling conventions for doing this. */
59 bool warn_aggregate_return;
61 /* Nonzero means warn about pointer casts that increase the required
62 alignment of the target type (and might therefore lead to a crash
63 due to a misaligned access). */
66 /* Nonzero means warn about uses of __attribute__((deprecated))
68 bool warn_deprecated_decl = true;
70 /* Warn when an optimization pass is disabled. */
71 bool warn_disabled_optimization;
73 /* Nonzero means warn if inline function is too large. */
76 /* True to warn about any objects definitions whose size is larger
77 than N bytes. Also want about function definitions whose returned
78 values are larger than N bytes, where N is `larger_than_size'. */
79 bool warn_larger_than;
80 HOST_WIDE_INT larger_than_size;
82 /* Warn about functions which might be candidates for attribute noreturn. */
83 bool warn_missing_noreturn;
85 /* True to warn about code which is never reached. */
88 /* Warn if packed attribute on struct is unnecessary and inefficient. */
91 /* Warn when gcc pads a structure to an alignment boundary. */
94 /* True means warn about all declarations which shadow others. */
97 /* Nonzero means warn about constructs which might not be
98 strict-aliasing safe. */
99 bool warn_strict_aliasing;
101 /* True to warn if a switch on an enum, that does not have a default
102 case, fails to have a case for every enum value. */
105 /* Warn if a switch does not have a default case. */
106 bool warn_switch_default;
108 /* Warn if a switch on an enum fails to have a case for every enum
109 value (regardless of the presence or otherwise of a default case). */
110 bool warn_switch_enum;
112 /* Don't suppress warnings from system headers. -Wsystem-headers. */
113 bool warn_system_headers;
115 /* True to warn about variables used before they are initialized. */
116 int warn_uninitialized;
118 /* True to warn about unused variables, functions et.al. */
119 bool warn_unused_function;
120 bool warn_unused_label;
121 bool warn_unused_parameter;
122 bool warn_unused_variable;
123 bool warn_unused_value;
125 /* Hack for cooperation between set_Wunused and set_Wextra. */
126 static bool maybe_warn_unused_parameter;
128 static size_t find_opt (const char *, int);
129 static int common_handle_option (size_t scode, const char *arg, int value);
130 static void handle_param (const char *);
131 static void set_Wextra (int);
133 /* Perform a binary search to find which option the command-line INPUT
134 matches. Returns its index in the option array, and N_OPTS
135 (cl_options_count) on failure.
137 This routine is quite subtle. A normal binary search is not good
138 enough because some options can be suffixed with an argument, and
139 multiple sub-matches can occur, e.g. input of "-pedantic" matching
140 the initial substring of "-pedantic-errors".
142 A more complicated example is -gstabs. It should match "-g" with
143 an argument of "stabs". Suppose, however, that the number and list
144 of switches are such that the binary search tests "-gen-decls"
145 before having tested "-g". This doesn't match, and as "-gen-decls"
146 is less than "-gstabs", it will become the lower bound of the
147 binary search range, and "-g" will never be seen. To resolve this
148 issue, opts.sh makes "-gen-decls" point, via the back_chain member,
149 to "-g" so that failed searches that end between "-gen-decls" and
150 the lexicographically subsequent switch know to go back and see if
151 "-g" causes a match (which it does in this example).
153 This search is done in such a way that the longest match for the
154 front end in question wins. If there is no match for the current
155 front end, the longest match for a different front end is returned
156 (or N_OPTS if none) and the caller emits an error message. */
158 find_opt (const char *input, int lang_mask)
160 size_t mn, mx, md, opt_len;
161 size_t match_wrong_lang;
165 mx = cl_options_count;
167 /* Find mn such this lexicographical inequality holds:
168 cl_options[mn] <= input < cl_options[mn + 1]. */
172 opt_len = cl_options[md].opt_len;
173 comp = strncmp (input, cl_options[md].opt_text, opt_len);
181 /* This is the switch that is the best match but for a different
182 front end, or cl_options_count if there is no match at all. */
183 match_wrong_lang = cl_options_count;
185 /* Backtrace the chain of possible matches, returning the longest
186 one, if any, that fits best. With current GCC switches, this
187 loop executes at most twice. */
190 const struct cl_option *opt = &cl_options[mn];
192 /* Is this switch a prefix of the input? */
193 if (!strncmp (input, opt->opt_text, opt->opt_len))
195 /* If language is OK, and the match is exact or the switch
196 takes a joined argument, return it. */
197 if ((opt->flags & lang_mask)
198 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
201 /* If we haven't remembered a prior match, remember this
202 one. Any prior match is necessarily better. */
203 if (match_wrong_lang != cl_options_count)
204 match_wrong_lang = mn;
207 /* Try the next possibility. This is cl_options_count if there
209 mn = opt->back_chain;
211 while (mn != cl_options_count);
213 /* Return the best wrong match, or cl_options_count if none. */
214 return match_wrong_lang;
217 /* If ARG is a non-negative integer made up solely of digits, return its
218 value, otherwise return -1. */
220 integral_argument (const char *arg)
224 while (*p && ISDIGIT (*p))
233 /* Handle the switch beginning at ARGV, with ARGC remaining. */
235 handle_option (int argc ATTRIBUTE_UNUSED, char **argv, int lang_mask)
238 const char *opt, *arg = 0;
242 const struct cl_option *option;
246 /* Interpret "-" or a non-switch as a file name. */
247 if (opt[0] != '-' || opt[1] == '\0')
249 opt_index = cl_options_count;
251 main_input_filename = opt;
252 result = (*lang_hooks.handle_option) (opt_index, arg, value);
256 /* Drop the "no-" from negative switches. */
257 if ((opt[1] == 'W' || opt[1] == 'f')
258 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
260 size_t len = strlen (opt) - 3;
262 dup = xmalloc (len + 1);
265 memcpy (dup + 2, opt + 5, len - 2 + 1);
270 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON);
271 if (opt_index == cl_options_count)
274 option = &cl_options[opt_index];
276 /* Reject negative form of switches that don't take negatives. */
277 if (!value && (option->flags & CL_REJECT_NEGATIVE))
280 /* We've recognized this switch. */
283 /* Sort out any argument the switch takes. */
284 if (option->flags & CL_JOINED)
286 /* Have arg point to the original switch. This is because
287 some code, such as disable_builtin_function, expects its
288 argument to be persistent until the program exits. */
289 arg = argv[0] + cl_options[opt_index].opt_len + 1;
291 arg += strlen ("no-");
293 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
295 if (option->flags & CL_SEPARATE)
301 /* Missing argument. */
305 else if (option->flags & CL_SEPARATE)
311 /* If the switch takes an integer, convert it. */
312 if (arg && (option->flags & CL_UINTEGER))
314 value = integral_argument (arg);
317 error ("argument to \"-%s\" should be a non-negative integer",
323 if (option->flags & lang_mask)
324 if ((*lang_hooks.handle_option) (opt_index, arg, value) == 0)
327 if (result && (option->flags & CL_COMMON))
328 if (common_handle_option (opt_index, arg, value) == 0)
338 /* Handle target- and language-independent options. Return zero to
339 generate an "unknown option" message. */
341 common_handle_option (size_t scode, const char *arg,
342 int value ATTRIBUTE_UNUSED)
344 const struct cl_option *option = &cl_options[scode];
345 enum opt_code code = (enum opt_code) scode;
347 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
349 error ("missing argument to \"-%s\"", option->opt_text);
360 exit_after_options = true;
367 case OPT__target_help:
368 display_target_options ();
369 exit_after_options = true;
373 print_version (stderr, "");
374 exit_after_options = true;
378 g_switch_value = value;
384 /* Currently handled in a prescan. */
388 /* For backward compatibility, -W is the same as -Wextra. */
392 case OPT_Waggregate_return:
393 warn_aggregate_return = value;
396 case OPT_Wcast_align:
397 warn_cast_align = value;
400 case OPT_Wdeprecated_declarations:
401 warn_deprecated_decl = value;
403 case OPT_Wdisabled_optimization:
404 warn_disabled_optimization = value;
408 warnings_are_errors = value;
419 case OPT_Wlarger_than_:
420 larger_than_size = value;
421 warn_larger_than = value != -1;
424 case OPT_Wmissing_noreturn:
425 warn_missing_noreturn = value;
440 case OPT_Wstrict_aliasing:
441 warn_strict_aliasing = value;
448 case OPT_Wswitch_default:
449 warn_switch_default = value;
452 case OPT_Wswitch_enum:
453 warn_switch_enum = value;
456 case OPT_Wsystem_headers:
457 warn_system_headers = value;
460 case OPT_Wuninitialized:
461 warn_uninitialized = value;
464 case OPT_Wunreachable_code:
465 warn_notreached = value;
472 case OPT_Wunused_function:
473 warn_unused_function = value;
476 case OPT_Wunused_label:
477 warn_unused_label = value;
480 case OPT_Wunused_parameter:
481 warn_unused_parameter = value;
484 case OPT_Wunused_value:
485 warn_unused_value = value;
488 case OPT_Wunused_variable:
489 warn_unused_variable = value;
494 aux_info_file_name = arg;
495 flag_gen_aux_info = 1;
502 case OPT_auxbase_strip:
504 char *tmp = xstrdup (arg);
505 strip_off_ending (tmp, strlen (tmp));
512 decode_d_option (arg);
516 dump_base_name = arg;
520 flag_pic = value + value;
524 flag_pie = value + value;
527 case OPT_falign_functions:
528 case OPT_falign_functions_:
529 align_functions = value;
532 case OPT_falign_jumps:
533 case OPT_falign_jumps_:
537 case OPT_falign_labels:
538 case OPT_falign_labels_:
539 align_labels = value;
542 case OPT_falign_loops:
543 case OPT_falign_loops_:
547 case OPT_fargument_alias:
548 flag_argument_noalias = !value;
551 case OPT_fargument_noalias:
552 flag_argument_noalias = value;
555 case OPT_fargument_noalias_global:
556 flag_argument_noalias = value + value;
559 case OPT_fasynchronous_unwind_tables:
560 flag_asynchronous_unwind_tables = value;
563 case OPT_fbounds_check:
564 flag_bounds_check = value;
567 case OPT_fbranch_count_reg:
568 flag_branch_on_count_reg = value;
571 case OPT_fbranch_probabilities:
572 flag_branch_probabilities = value;
575 case OPT_fbranch_target_load_optimize:
576 flag_branch_target_load_optimize = value;
579 case OPT_fbranch_target_load_optimize2:
580 flag_branch_target_load_optimize2 = value;
583 case OPT_fcall_used_:
584 fix_register (arg, 0, 1);
587 case OPT_fcall_saved_:
588 fix_register (arg, 0, 0);
591 case OPT_fcaller_saves:
592 flag_caller_saves = value;
596 flag_no_common = !value;
599 case OPT_fcprop_registers:
600 flag_cprop_registers = value;
603 case OPT_fcrossjumping:
604 flag_crossjumping = value;
607 case OPT_fcse_follow_jumps:
608 flag_cse_follow_jumps = value;
611 case OPT_fcse_skip_blocks:
612 flag_cse_skip_blocks = value;
615 case OPT_fdata_sections:
616 flag_data_sections = value;
620 flag_defer_pop = value;
623 case OPT_fdelayed_branch:
624 flag_delayed_branch = value;
627 case OPT_fdelete_null_pointer_checks:
628 flag_delete_null_pointer_checks = value;
631 case OPT_fdiagnostics_show_location_:
632 if (!strcmp (arg, "once"))
633 diagnostic_prefixing_rule (global_dc) = DIAGNOSTICS_SHOW_PREFIX_ONCE;
634 else if (!strcmp (arg, "every-line"))
635 diagnostic_prefixing_rule (global_dc)
636 = DIAGNOSTICS_SHOW_PREFIX_EVERY_LINE;
641 case OPT_fdump_unnumbered:
642 flag_dump_unnumbered = value;
645 case OPT_feliminate_dwarf2_dups:
646 flag_eliminate_dwarf2_dups = value;
649 case OPT_feliminate_unused_debug_types:
650 flag_eliminate_unused_debug_types = value;
653 case OPT_fexceptions:
654 flag_exceptions = value;
657 case OPT_fexpensive_optimizations:
658 flag_expensive_optimizations = value;
662 set_fast_math_flags (value);
665 case OPT_ffinite_math_only:
666 flag_finite_math_only = value;
670 fix_register (arg, 1, 1);
673 case OPT_ffunction_cse:
674 flag_no_function_cse = !value;
677 case OPT_ffloat_store:
678 flag_float_store = value;
681 case OPT_fforce_addr:
682 flag_force_addr = value;
686 flag_force_mem = value;
689 case OPT_ffunction_sections:
690 flag_function_sections = value;
698 flag_gcse_lm = value;
702 flag_gcse_sm = value;
705 case OPT_fgnu_linker:
706 flag_gnu_linker = value;
709 case OPT_fguess_branch_probability:
710 flag_guess_branch_prob = value;
714 flag_no_ident = !value;
717 case OPT_fif_conversion:
718 flag_if_conversion = value;
721 case OPT_fif_conversion2:
722 flag_if_conversion2 = value;
725 case OPT_finhibit_size_directive:
726 flag_inhibit_size_directive = value;
730 flag_no_inline = !value;
733 case OPT_finline_functions:
734 flag_inline_functions = value;
737 case OPT_finline_limit_:
738 case OPT_finline_limit_eq:
739 set_param_value ("max-inline-insns", value);
740 set_param_value ("max-inline-insns-single", value / 2);
741 set_param_value ("max-inline-insns-auto", value / 2);
742 set_param_value ("max-inline-insns-rtl", value);
743 if (value / 4 < MIN_INLINE_INSNS)
746 set_param_value ("min-inline-insns", value / 4);
748 set_param_value ("min-inline-insns", 10);
752 case OPT_finstrument_functions:
753 flag_instrument_function_entry_exit = value;
756 case OPT_fkeep_inline_functions:
757 flag_keep_inline_functions =value;
760 case OPT_fkeep_static_consts:
761 flag_keep_static_consts = value;
764 case OPT_fleading_underscore:
765 flag_leading_underscore = value;
768 case OPT_floop_optimize:
769 flag_loop_optimize = value;
772 case OPT_fmath_errno:
773 flag_errno_math = value;
776 case OPT_fmem_report:
780 case OPT_fmerge_all_constants:
781 flag_merge_constants = value + value;
784 case OPT_fmerge_constants:
785 flag_merge_constants = value;
788 case OPT_fmessage_length_:
789 output_set_maximum_length (&global_dc->buffer, value);
792 case OPT_fmove_all_movables:
793 flag_move_all_movables = value;
797 flag_new_regalloc = value;
800 case OPT_fnon_call_exceptions:
801 flag_non_call_exceptions = value;
804 case OPT_fold_unroll_all_loops:
805 flag_old_unroll_all_loops = value;
808 case OPT_fold_unroll_loops:
809 flag_old_unroll_loops = value;
812 case OPT_fomit_frame_pointer:
813 flag_omit_frame_pointer = value;
816 case OPT_foptimize_register_move:
817 flag_regmove = value;
820 case OPT_foptimize_sibling_calls:
821 flag_optimize_sibling_calls = value;
824 case OPT_fpack_struct:
825 flag_pack_struct = value;
828 case OPT_fpeel_loops:
829 flag_peel_loops = value;
832 case OPT_fpcc_struct_return:
833 flag_pcc_struct_return = value;
837 flag_no_peephole = !value;
841 flag_peephole2 = value;
852 case OPT_fprefetch_loop_arrays:
853 flag_prefetch_loop_arrays = value;
857 profile_flag = value;
860 case OPT_fprofile_arcs:
861 profile_arc_flag = value;
864 case OPT_frandom_seed:
865 /* The real switch is -fno-random-seed. */
868 flag_random_seed = NULL;
871 case OPT_frandom_seed_:
872 flag_random_seed = arg;
875 case OPT_freduce_all_givs:
876 flag_reduce_all_givs = value;
879 case OPT_freg_struct_return:
880 flag_pcc_struct_return = !value;
884 flag_regmove = value;
887 case OPT_frename_registers:
888 flag_rename_registers = value;
891 case OPT_freorder_blocks:
892 flag_reorder_blocks = value;
895 case OPT_freorder_functions:
896 flag_reorder_functions = value;
899 case OPT_frerun_cse_after_loop:
900 flag_rerun_cse_after_loop = value;
903 case OPT_frerun_loop_opt:
904 flag_rerun_loop_opt = value;
907 case OPT_fsched_interblock:
908 flag_schedule_interblock= value;
911 case OPT_fsched_spec:
912 flag_schedule_speculative = value;
915 case OPT_fsched_spec_load:
916 flag_schedule_speculative_load = value;
919 case OPT_fsched_spec_load_dangerous:
920 flag_schedule_speculative_load_dangerous = value;
923 case OPT_fsched_verbose_:
924 #ifdef INSN_SCHEDULING
925 fix_sched_param ("verbose", arg);
931 case OPT_fsched2_use_superblocks:
932 flag_sched2_use_superblocks = value;
935 case OPT_fsched2_use_traces:
936 flag_sched2_use_traces = value;
939 case OPT_fschedule_insns:
940 flag_schedule_insns = value;
943 case OPT_fschedule_insns2:
944 flag_schedule_insns_after_reload = value;
947 case OPT_fshared_data:
948 flag_shared_data = value;
951 case OPT_fsignaling_nans:
952 flag_signaling_nans = value;
955 case OPT_fsingle_precision_constant:
956 flag_single_precision_constant = value;
964 flag_ssa_ccp = value;
968 flag_ssa_dce = value;
971 case OPT_fstack_check:
972 flag_stack_check = value;
975 case OPT_fstack_limit:
976 /* The real switch is -fno-stack-limit. */
979 stack_limit_rtx = NULL_RTX;
982 case OPT_fstack_limit_register_:
984 int reg = decode_reg_name (arg);
986 error ("unrecognized register name \"%s\"", arg);
988 stack_limit_rtx = gen_rtx_REG (Pmode, reg);
992 case OPT_fstack_limit_symbol_:
993 stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
996 case OPT_fstrength_reduce:
997 flag_strength_reduce = value;
1000 case OPT_fstrict_aliasing:
1001 flag_strict_aliasing = value;
1004 case OPT_fsyntax_only:
1005 flag_syntax_only = value;
1008 case OPT_ftest_coverage:
1009 flag_test_coverage = value;
1012 case OPT_fthread_jumps:
1013 flag_thread_jumps = value;
1016 case OPT_ftime_report:
1017 time_report = value;
1019 case OPT_ftls_model_:
1020 if (!strcmp (arg, "global-dynamic"))
1021 flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
1022 else if (!strcmp (arg, "local-dynamic"))
1023 flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
1024 else if (!strcmp (arg, "initial-exec"))
1025 flag_tls_default = TLS_MODEL_INITIAL_EXEC;
1026 else if (!strcmp (arg, "local-exec"))
1027 flag_tls_default = TLS_MODEL_LOCAL_EXEC;
1029 warning ("unknown tls-model \"%s\"", arg);
1033 flag_tracer = value;
1036 case OPT_ftrapping_math:
1037 flag_trapping_math = value;
1044 case OPT_funit_at_a_time:
1045 flag_unit_at_a_time = value;
1048 case OPT_funroll_all_loops:
1049 flag_unroll_all_loops = value;
1052 case OPT_funroll_loops:
1053 flag_unroll_loops = value;
1056 case OPT_funsafe_math_optimizations:
1057 flag_unsafe_math_optimizations = value;
1060 case OPT_funswitch_loops:
1061 flag_unswitch_loops = value;
1064 case OPT_funwind_tables:
1065 flag_unwind_tables = value;
1068 case OPT_fverbose_asm:
1069 flag_verbose_asm = value;
1076 case OPT_fwritable_strings:
1077 flag_writable_strings = value;
1080 case OPT_fzero_initialized_in_bss:
1081 flag_zero_initialized_in_bss = value;
1085 decode_g_option (arg);
1089 set_target_switch (arg);
1093 asm_file_name = arg;
1104 case OPT_pedantic_errors:
1105 flag_pedantic_errors = pedantic = 1;
1117 inhibit_warnings = true;
1124 /* Handle --param NAME=VALUE. */
1126 handle_param (const char *carg)
1131 arg = xstrdup (carg);
1132 equal = strchr (arg, '=');
1134 error ("%s: --param arguments should be of the form NAME=VALUE", arg);
1137 value = integral_argument (equal + 1);
1139 error ("invalid --param value `%s'", equal + 1);
1143 set_param_value (arg, value);
1150 /* Handle -W and -Wextra. */
1152 set_Wextra (int setting)
1154 extra_warnings = setting;
1155 warn_unused_value = setting;
1156 warn_unused_parameter = (setting && maybe_warn_unused_parameter);
1158 /* We save the value of warn_uninitialized, since if they put
1159 -Wuninitialized on the command line, we need to generate a
1160 warning about not using it without also specifying -O. */
1162 warn_uninitialized = 0;
1163 else if (warn_uninitialized != 1)
1164 warn_uninitialized = 2;
1167 /* Initialize unused warning flags. */
1169 set_Wunused (int setting)
1171 warn_unused_function = setting;
1172 warn_unused_label = setting;
1173 /* Unused function parameter warnings are reported when either
1174 ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
1175 Thus, if -Wextra has already been seen, set warn_unused_parameter;
1176 otherwise set maybe_warn_extra_parameter, which will be picked up
1178 maybe_warn_unused_parameter = setting;
1179 warn_unused_parameter = (setting && extra_warnings);
1180 warn_unused_variable = setting;
1181 warn_unused_value = setting;
1184 /* The following routines are useful in setting all the flags that
1185 -ffast-math and -fno-fast-math imply. */
1187 set_fast_math_flags (int set)
1189 flag_trapping_math = !set;
1190 flag_unsafe_math_optimizations = set;
1191 flag_finite_math_only = set;
1192 flag_errno_math = !set;
1194 flag_signaling_nans = 0;
1197 /* Return true iff flags are set as if -ffast-math. */
1199 fast_math_flags_set_p (void)
1201 return (!flag_trapping_math
1202 && flag_unsafe_math_optimizations
1203 && flag_finite_math_only
1204 && !flag_errno_math);