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"
27 #include "langhooks.h"
33 /* Value of the -G xx switch, and whether it was passed or not. */
34 unsigned HOST_WIDE_INT g_switch_value;
37 /* True if we should exit after parsing options. */
38 bool exit_after_options;
43 static size_t find_opt (const char *, int);
44 static int common_handle_option (size_t scode, const char *arg, int value);
46 /* Perform a binary search to find which option the command-line INPUT
47 matches. Returns its index in the option array, and N_OPTS on
50 Complications arise since some options can be suffixed with an
51 argument, and multiple complete matches can occur, e.g. -pedantic
52 and -pedantic-errors. Also, some options are only accepted by some
53 languages. If a switch matches for a different language and
54 doesn't match any alternatives for the true front end, the index of
55 the matched switch is returned anyway. The caller should check for
58 find_opt (const char *input, int lang_mask)
62 size_t result = cl_options_count;
66 mx = cl_options_count;
72 opt_len = cl_options[md].opt_len;
73 comp = strncmp (input, cl_options[md].opt_text, opt_len);
81 /* The switch matches. It it an exact match? */
82 if (input[opt_len] == '\0')
88 /* If the switch takes no arguments this is not a proper
89 match, so we continue the search (e.g. input="stdc++"
91 if (!(cl_options[md].flags & CL_JOINED))
94 /* Is this switch valid for this front end? */
95 if (!(cl_options[md].flags & lang_mask))
97 /* If subsequently we don't find a better match,
98 return this and let the caller report it as a bad
104 /* Two scenarios remain: we have the switch's argument,
105 or we match a longer option. This can happen with
106 -iwithprefix and -withprefixbefore. The longest
107 possible option match succeeds.
109 Scan forwards, and return an exact match. Otherwise
110 return the longest valid option-accepting match (mx).
111 This loops at most twice with current options. */
113 for (md = md + 1; md < cl_options_count; md++)
115 opt_len = cl_options[md].opt_len;
116 if (strncmp (input, cl_options[md].opt_text, opt_len))
118 if (input[opt_len] == '\0')
120 if (cl_options[md].flags & lang_mask
121 && cl_options[md].flags & CL_JOINED)
133 /* Handle the switch beginning at ARGV, with ARGC remaining. */
135 handle_option (int argc ATTRIBUTE_UNUSED, char **argv, int lang_mask)
138 const char *opt, *arg = 0;
141 int result = 0, temp;
142 const struct cl_option *option;
146 /* Interpret "-" or a non-switch as a file name. */
147 if (opt[0] != '-' || opt[1] == '\0')
149 opt_index = cl_options_count;
151 main_input_filename = opt;
152 result = (*lang_hooks.handle_option) (opt_index, arg, on);
156 /* Drop the "no-" from negative switches. */
157 if ((opt[1] == 'W' || opt[1] == 'f')
158 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
160 size_t len = strlen (opt) - 3;
162 dup = xmalloc (len + 1);
165 memcpy (dup + 2, opt + 5, len - 2 + 1);
170 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON);
171 if (opt_index == cl_options_count)
174 option = &cl_options[opt_index];
176 /* Reject negative form of switches that don't take negatives. */
177 if (!on && (option->flags & CL_REJECT_NEGATIVE))
180 /* We've recognized this switch. */
183 /* Sort out any argument the switch takes. */
184 if (option->flags & (CL_JOINED | CL_SEPARATE))
186 if (option->flags & CL_JOINED)
188 /* Have arg point to the original switch. This is because
189 some code, such as disable_builtin_function, expects its
190 argument to be persistent until the program exits. */
191 arg = argv[0] + cl_options[opt_index].opt_len + 1;
193 arg += strlen ("no-");
196 /* If we don't have an argument, and CL_SEPARATE, try the next
197 argument in the vector. */
198 if (!arg || (*arg == '\0' && option->flags & CL_SEPARATE))
204 /* Canonicalize missing arguments as NULL for the handler. */
209 if (option->flags & lang_mask)
211 temp = (*lang_hooks.handle_option) (opt_index, arg, on);
216 if (result > 0 && (option->flags & CL_COMMON))
218 if (common_handle_option (opt_index, arg, on) == 0)
229 /* Handle target- and language-independent options. Return zero to
230 generate an "unknown option" message. */
232 common_handle_option (size_t scode, const char *arg,
233 int value ATTRIBUTE_UNUSED)
235 const struct cl_option *option = &cl_options[scode];
236 enum opt_code code = (enum opt_code) scode;
238 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
240 error ("missing argument to \"-%s\"", option->opt_text);
251 exit_after_options = true;
254 case OPT__target_help:
255 display_target_options ();
256 exit_after_options = true;
260 print_version (stderr, "");
261 exit_after_options = true;
265 g_switch_value = read_integral_parameter (arg, 0, -1);
266 if (g_switch_value == (unsigned HOST_WIDE_INT) -1)
273 aux_info_file_name = arg;
274 flag_gen_aux_info = 1;
281 case OPT_auxbase_strip:
283 char *tmp = xstrdup (arg);
284 strip_off_ending (tmp, strlen (tmp));
291 decode_d_option (arg);
295 dump_base_name = arg;
310 case OPT_pedantic_errors:
311 flag_pedantic_errors = pedantic = 1;
323 inhibit_warnings = 1;