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"
37 /* Value of the -G xx switch, and whether it was passed or not. */
38 unsigned HOST_WIDE_INT g_switch_value;
41 /* True if we should exit after parsing options. */
42 bool exit_after_options;
47 /* Print various extra warnings. -W/-Wextra. */
50 /* Don't print warning messages. -w. */
51 bool inhibit_warnings;
53 /* Treat warnings as errors. -Werror. */
54 bool warnings_are_errors;
56 /* Warn if a function returns an aggregate, since there are often
57 incompatible calling conventions for doing this. */
58 bool warn_aggregate_return;
60 /* Nonzero means warn about pointer casts that increase the required
61 alignment of the target type (and might therefore lead to a crash
62 due to a misaligned access). */
65 /* Nonzero means warn about uses of __attribute__((deprecated))
67 bool warn_deprecated_decl = true;
69 /* Warn when an optimization pass is disabled. */
70 bool warn_disabled_optimization;
72 /* Nonzero means warn if inline function is too large. */
75 /* True to warn about any objects definitions whose size is larger
76 than N bytes. Also want about function definitions whose returned
77 values are larger than N bytes, where N is `larger_than_size'. */
78 bool warn_larger_than;
79 HOST_WIDE_INT larger_than_size;
81 /* Warn about functions which might be candidates for attribute noreturn. */
82 bool warn_missing_noreturn;
84 /* True to warn about code which is never reached. */
87 /* Warn if packed attribute on struct is unnecessary and inefficient. */
90 /* Warn when gcc pads a structure to an alignment boundary. */
93 /* True means warn about all declarations which shadow others. */
96 /* Nonzero means warn about constructs which might not be
97 strict-aliasing safe. */
98 bool warn_strict_aliasing;
100 /* True to warn if a switch on an enum, that does not have a default
101 case, fails to have a case for every enum value. */
104 /* Warn if a switch does not have a default case. */
105 bool warn_switch_default;
107 /* Warn if a switch on an enum fails to have a case for every enum
108 value (regardless of the presence or otherwise of a default case). */
109 bool warn_switch_enum;
111 /* Don't suppress warnings from system headers. -Wsystem-headers. */
112 bool warn_system_headers;
114 /* True to warn about variables used before they are initialized. */
115 int warn_uninitialized;
117 /* True to warn about unused variables, functions et.al. */
118 bool warn_unused_function;
119 bool warn_unused_label;
120 bool warn_unused_parameter;
121 bool warn_unused_variable;
122 bool warn_unused_value;
124 /* Hack for cooperation between set_Wunused and set_Wextra. */
125 static bool maybe_warn_unused_parameter;
127 static size_t find_opt (const char *, int);
128 static int common_handle_option (size_t scode, const char *arg, int value);
129 static void handle_param (const char *);
130 static void set_Wextra (int);
132 /* Perform a binary search to find which option the command-line INPUT
133 matches. Returns its index in the option array, and N_OPTS on
136 Complications arise since some options can be suffixed with an
137 argument, and multiple complete matches can occur, e.g. -pedantic
138 and -pedantic-errors. Also, some options are only accepted by some
139 languages. If a switch matches for a different language and
140 doesn't match any alternatives for the true front end, the index of
141 the matched switch is returned anyway. The caller should check for
144 find_opt (const char *input, int lang_mask)
148 size_t result = cl_options_count;
152 mx = cl_options_count;
158 opt_len = cl_options[md].opt_len;
159 comp = strncmp (input, cl_options[md].opt_text, opt_len);
167 /* The switch matches. It it an exact match? */
168 if (input[opt_len] == '\0')
174 /* If the switch takes no arguments this is not a proper
175 match, so we continue the search (e.g. input="stdc++"
176 match was "stdc"). */
177 if (!(cl_options[md].flags & CL_JOINED))
180 /* Is this switch valid for this front end? */
181 if (!(cl_options[md].flags & lang_mask))
183 /* If subsequently we don't find a better match,
184 return this and let the caller report it as a bad
190 /* Two scenarios remain: we have the switch's argument,
191 or we match a longer option. This can happen with
192 -iwithprefix and -withprefixbefore. The longest
193 possible option match succeeds.
195 Scan forwards, and return an exact match. Otherwise
196 return the longest valid option-accepting match (mx).
197 This loops at most twice with current options. */
199 for (md = md + 1; md < cl_options_count; md++)
201 opt_len = cl_options[md].opt_len;
202 comp = strncmp (input, cl_options[md].opt_text, opt_len);
207 if (input[opt_len] == '\0')
209 if (cl_options[md].flags & lang_mask
210 && cl_options[md].flags & CL_JOINED)
222 /* If ARG is a non-negative integer made up solely of digits, return its
223 value, otherwise return -1. */
225 integral_argument (const char *arg)
229 while (*p && ISDIGIT (*p))
238 /* Handle the switch beginning at ARGV, with ARGC remaining. */
240 handle_option (int argc ATTRIBUTE_UNUSED, char **argv, int lang_mask)
243 const char *opt, *arg = 0;
247 const struct cl_option *option;
251 /* Interpret "-" or a non-switch as a file name. */
252 if (opt[0] != '-' || opt[1] == '\0')
254 opt_index = cl_options_count;
256 main_input_filename = opt;
257 result = (*lang_hooks.handle_option) (opt_index, arg, value);
261 /* Drop the "no-" from negative switches. */
262 if ((opt[1] == 'W' || opt[1] == 'f')
263 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
265 size_t len = strlen (opt) - 3;
267 dup = xmalloc (len + 1);
270 memcpy (dup + 2, opt + 5, len - 2 + 1);
275 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON);
276 if (opt_index == cl_options_count)
279 option = &cl_options[opt_index];
281 /* Reject negative form of switches that don't take negatives. */
282 if (!value && (option->flags & CL_REJECT_NEGATIVE))
285 /* We've recognized this switch. */
288 /* Sort out any argument the switch takes. */
289 if (option->flags & CL_JOINED)
291 /* Have arg point to the original switch. This is because
292 some code, such as disable_builtin_function, expects its
293 argument to be persistent until the program exits. */
294 arg = argv[0] + cl_options[opt_index].opt_len + 1;
296 arg += strlen ("no-");
298 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
300 if (option->flags & CL_SEPARATE)
306 /* Missing argument. */
310 else if (option->flags & CL_SEPARATE)
316 /* If the switch takes an integer, convert it. */
317 if (arg && (option->flags & CL_UINTEGER))
319 value = integral_argument (arg);
322 error ("argument to \"-%s\" should be a non-negative integer",
328 if (option->flags & lang_mask)
329 if ((*lang_hooks.handle_option) (opt_index, arg, value) == 0)
332 if (result && (option->flags & CL_COMMON))
333 if (common_handle_option (opt_index, arg, value) == 0)
343 /* Handle target- and language-independent options. Return zero to
344 generate an "unknown option" message. */
346 common_handle_option (size_t scode, const char *arg,
347 int value ATTRIBUTE_UNUSED)
349 const struct cl_option *option = &cl_options[scode];
350 enum opt_code code = (enum opt_code) scode;
352 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
354 error ("missing argument to \"-%s\"", option->opt_text);
365 exit_after_options = true;
372 case OPT__target_help:
373 display_target_options ();
374 exit_after_options = true;
378 print_version (stderr, "");
379 exit_after_options = true;
383 g_switch_value = value;
389 /* Currently handled in a prescan. */
393 /* For backward compatibility, -W is the same as -Wextra. */
397 case OPT_Waggregate_return:
398 warn_aggregate_return = value;
401 case OPT_Wcast_align:
402 warn_cast_align = value;
405 case OPT_Wdeprecated_declarations:
406 warn_deprecated_decl = value;
408 case OPT_Wdisabled_optimization:
409 warn_disabled_optimization = value;
413 warnings_are_errors = value;
424 case OPT_Wlarger_than_:
425 larger_than_size = value;
426 warn_larger_than = value != -1;
429 case OPT_Wmissing_noreturn:
430 warn_missing_noreturn = value;
445 case OPT_Wstrict_aliasing:
446 warn_strict_aliasing = value;
453 case OPT_Wswitch_default:
454 warn_switch_default = value;
457 case OPT_Wswitch_enum:
458 warn_switch_enum = value;
461 case OPT_Wsystem_headers:
462 warn_system_headers = value;
465 case OPT_Wuninitialized:
466 warn_uninitialized = value;
469 case OPT_Wunreachable_code:
470 warn_notreached = value;
477 case OPT_Wunused_function:
478 warn_unused_function = value;
481 case OPT_Wunused_label:
482 warn_unused_label = value;
485 case OPT_Wunused_parameter:
486 warn_unused_parameter = value;
489 case OPT_Wunused_value:
490 warn_unused_value = value;
493 case OPT_Wunused_variable:
494 warn_unused_variable = value;
499 aux_info_file_name = arg;
500 flag_gen_aux_info = 1;
507 case OPT_auxbase_strip:
509 char *tmp = xstrdup (arg);
510 strip_off_ending (tmp, strlen (tmp));
517 decode_d_option (arg);
521 dump_base_name = arg;
524 case OPT_falign_functions_:
525 align_functions = value;
528 case OPT_falign_jumps_:
532 case OPT_falign_labels_:
533 align_labels = value;
536 case OPT_falign_loops_:
540 case OPT_fcall_used_:
541 fix_register (arg, 0, 1);
544 case OPT_fcall_saved_:
545 fix_register (arg, 0, 0);
549 set_fast_math_flags (value);
553 fix_register (arg, 1, 1);
556 case OPT_fstack_limit_register_:
558 int reg = decode_reg_name (arg);
560 error ("unrecognized register name \"%s\"", arg);
562 stack_limit_rtx = gen_rtx_REG (Pmode, reg);
566 case OPT_fstack_limit_symbol_:
567 stack_limit_rtx = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (arg));
570 case OPT_ftls_model_:
571 if (!strcmp (arg, "global-dynamic"))
572 flag_tls_default = TLS_MODEL_GLOBAL_DYNAMIC;
573 else if (!strcmp (arg, "local-dynamic"))
574 flag_tls_default = TLS_MODEL_LOCAL_DYNAMIC;
575 else if (!strcmp (arg, "initial-exec"))
576 flag_tls_default = TLS_MODEL_INITIAL_EXEC;
577 else if (!strcmp (arg, "local-exec"))
578 flag_tls_default = TLS_MODEL_LOCAL_EXEC;
580 warning ("unknown tls-model \"%s\"", arg);
584 decode_g_option (arg);
588 set_target_switch (arg);
603 case OPT_pedantic_errors:
604 flag_pedantic_errors = pedantic = 1;
616 inhibit_warnings = true;
623 /* Handle --param NAME=VALUE. */
625 handle_param (const char *carg)
630 arg = xstrdup (carg);
631 equal = strchr (arg, '=');
633 error ("%s: --param arguments should be of the form NAME=VALUE", arg);
636 value = integral_argument (equal + 1);
638 error ("invalid --param value `%s'", equal + 1);
642 set_param_value (arg, value);
649 /* Handle -W and -Wextra. */
651 set_Wextra (int setting)
653 extra_warnings = setting;
654 warn_unused_value = setting;
655 warn_unused_parameter = (setting && maybe_warn_unused_parameter);
657 /* We save the value of warn_uninitialized, since if they put
658 -Wuninitialized on the command line, we need to generate a
659 warning about not using it without also specifying -O. */
661 warn_uninitialized = 0;
662 else if (warn_uninitialized != 1)
663 warn_uninitialized = 2;
666 /* Initialize unused warning flags. */
668 set_Wunused (int setting)
670 warn_unused_function = setting;
671 warn_unused_label = setting;
672 /* Unused function parameter warnings are reported when either
673 ``-Wextra -Wunused'' or ``-Wunused-parameter'' is specified.
674 Thus, if -Wextra has already been seen, set warn_unused_parameter;
675 otherwise set maybe_warn_extra_parameter, which will be picked up
677 maybe_warn_unused_parameter = setting;
678 warn_unused_parameter = (setting && extra_warnings);
679 warn_unused_variable = setting;
680 warn_unused_value = setting;
683 /* The following routines are useful in setting all the flags that
684 -ffast-math and -fno-fast-math imply. */
686 set_fast_math_flags (int set)
688 flag_trapping_math = !set;
689 flag_unsafe_math_optimizations = set;
690 flag_finite_math_only = set;
691 flag_errno_math = !set;
693 flag_signaling_nans = 0;
696 /* Return true iff flags are set as if -ffast-math. */
698 fast_math_flags_set_p (void)
700 return (!flag_trapping_math
701 && flag_unsafe_math_optimizations
702 && flag_finite_math_only
703 && !flag_errno_math);