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
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
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
29 /* True if we should exit after parsing options. */
30 bool exit_after_options;
32 /* Treat warnings as errors. -Werror. */
33 bool warnings_are_errors;
35 /* Don't suppress warnings from system headers. -Wsystem-headers. */
36 bool warn_system_headers;
38 /* Columns of --help display. */
39 static unsigned int columns = 80;
41 /* What to print when a switch has no documentation. */
42 static const char undocumented_msg[] = N_("This switch lacks documentation");
44 /* Input file names. */
45 const char **in_fnames;
46 unsigned num_in_fnames;
48 static int common_handle_option (size_t scode, const char *arg, int value);
49 static unsigned int handle_option (const char **argv, unsigned int lang_mask);
50 static char *write_langs (unsigned int lang_mask);
51 static void complain_wrong_lang (const char *, const struct cl_option *,
52 unsigned int lang_mask);
53 static void handle_options (unsigned int, const char **, unsigned int);
54 static void wrap_help (const char *help, const char *item, unsigned int);
55 static void print_help (void);
56 static void print_filtered_help (unsigned int);
57 static unsigned int print_switch (const char *text, unsigned int indent);
59 /* If ARG is a non-negative integer made up solely of digits, return its
60 value, otherwise return -1. */
62 integral_argument (const char *arg)
66 while (*p && ISDIGIT (*p))
75 /* Return a malloced slash-separated list of languages in MASK. */
77 write_langs (unsigned int mask)
79 unsigned int n = 0, len = 0;
80 const char *lang_name;
83 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
85 len += strlen (lang_name) + 1;
87 result = XNEWVEC (char, len);
89 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
94 strcpy (result + len, lang_name);
95 len += strlen (lang_name);
103 /* Complain that switch OPT_INDEX does not apply to this front end. */
105 complain_wrong_lang (const char *text, const struct cl_option *option,
106 unsigned int lang_mask)
108 char *ok_langs, *bad_lang;
110 ok_langs = write_langs (option->flags);
111 bad_lang = write_langs (lang_mask);
113 /* Eventually this should become a hard error IMO. */
114 warning (0, "command line option \"%s\" is valid for %s but not for %s",
115 text, ok_langs, bad_lang);
121 /* Handle the switch beginning at ARGV for the language indicated by
122 LANG_MASK. Returns the number of switches consumed. */
124 handle_option (const char **argv, unsigned int lang_mask)
127 const char *opt, *arg = 0;
130 unsigned int result = 0;
131 const struct cl_option *option;
135 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
136 if (opt_index == cl_options_count
137 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
138 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
140 /* Drop the "no-" from negative switches. */
141 size_t len = strlen (opt) - 3;
143 dup = XNEWVEC (char, len + 1);
146 memcpy (dup + 2, opt + 5, len - 2 + 1);
149 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
152 if (opt_index == cl_options_count)
155 option = &cl_options[opt_index];
157 /* Reject negative form of switches that don't take negatives as
159 if (!value && (option->flags & CL_REJECT_NEGATIVE))
162 /* We've recognized this switch. */
165 /* Check to see if the option is disabled for this configuration. */
166 if (option->flags & CL_DISABLED)
168 error ("command line option \"%s\""
169 " is not supported by this configuration", opt);
173 /* Sort out any argument the switch takes. */
174 if (option->flags & CL_JOINED)
176 /* Have arg point to the original switch. This is because
177 some code, such as disable_builtin_function, expects its
178 argument to be persistent until the program exits. */
179 arg = argv[0] + cl_options[opt_index].opt_len + 1;
181 arg += strlen ("no-");
183 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
185 if (option->flags & CL_SEPARATE)
191 /* Missing argument. */
195 else if (option->flags & CL_SEPARATE)
201 /* Now we've swallowed any potential argument, complain if this
202 is a switch for a different front end. */
203 if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
205 complain_wrong_lang (argv[0], option, lang_mask);
209 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
211 if (!lang_hooks.missing_argument (opt, opt_index))
212 error ("missing argument to \"%s\"", opt);
216 /* If the switch takes an integer, convert it. */
217 if (arg && (option->flags & CL_UINTEGER))
219 value = integral_argument (arg);
222 error ("argument to \"%s\" should be a non-negative integer",
228 if (option->flag_var)
229 switch (option->var_type)
232 *(int *) option->flag_var = value;
236 *(int *) option->flag_var = (value
238 : !option->var_value);
243 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
244 *(int *) option->flag_var |= option->var_value;
246 *(int *) option->flag_var &= ~option->var_value;
247 ////if (option->flag_var == &target_flags)
248 //// target_flags_explicit |= option->var_value;
252 *(const char **) option->flag_var = arg;
256 if (option->flags & lang_mask)
257 if (lang_hooks.handle_option (opt_index, arg, value) == 0)
260 if (result && (option->flags & CL_COMMON))
261 if (common_handle_option (opt_index, arg, value) == 0)
264 ////if (result && (option->flags & CL_TARGET))
265 //// if (!targetm.handle_option (opt_index, arg, value))
274 /* Handle FILENAME from the command line. */
276 add_input_filename (const char *filename)
279 in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
280 in_fnames[num_in_fnames - 1] = filename;
283 /* Decode and handle the vector of command line options. LANG_MASK
284 contains has a single bit set representing the current
287 handle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
291 for (i = 1; i < argc; i += n)
293 const char *opt = argv[i];
295 /* Interpret "-" or a non-switch as a file name. */
296 if (opt[0] != '-' || opt[1] == '\0')
298 if (main_input_filename == NULL)
299 main_input_filename = opt;
300 add_input_filename (opt);
305 n = handle_option (argv + i, lang_mask);
310 error ("unrecognized command line option \"%s\"", opt);
315 /* Parse command line options and set default flag values. Do minimal
316 options processing. */
318 decode_options (unsigned int argc, const char **argv)
320 unsigned int lang_mask;
322 /* Perform language-specific options initialization. */
323 lang_mask = lang_hooks.init_options (argc, argv);
325 /* Scan to see what optimization level has been specified. That will
326 determine the default value of many flags. */
328 handle_options (argc, argv, lang_mask);
331 /* Handle target- and language-independent options. Return zero to
332 generate an "unknown option" message. Only options that need
333 extra handling need to be listed here; if you simply want
334 VALUE assigned to a variable, it happens automatically. */
337 common_handle_option (size_t scode, const char *arg,
338 int value ATTRIBUTE_UNUSED)
340 enum opt_code code = (enum opt_code) scode;
349 exit_after_options = true;
353 print_version (stderr, "");
354 exit_after_options = true;
358 warnings_are_errors = value;
361 case OPT_Wsystem_headers:
362 warn_system_headers = value;
366 decode_d_option (arg);
369 case OPT_pedantic_errors:
370 flag_pedantic_errors = 1;
374 inhibit_warnings = true;
381 /* Output --help text. */
388 GET_ENVIRONMENT (p, "COLUMNS");
391 int value = atoi (p);
396 puts (_("The following options are language-independent:\n"));
398 print_filtered_help (CL_COMMON);
400 for (i = 0; lang_names[i]; i++)
402 printf (_("The %s front end recognizes the following options:\n\n"),
404 print_filtered_help (1U << i);
408 /* Print help for a specific front-end, etc. */
410 print_filtered_help (unsigned int flag)
412 unsigned int i, len, filter, indent = 0;
413 bool duplicates = false;
414 const char *help, *opt, *tab;
415 static char *printed;
417 if (flag == CL_COMMON || flag == CL_TARGET)
421 printed = xmalloc (cl_options_count);
422 memset (printed, 0, cl_options_count);
426 /* Don't print COMMON options twice. */
427 filter = flag | CL_COMMON;
429 for (i = 0; i < cl_options_count; i++)
431 if ((cl_options[i].flags & filter) != flag)
434 /* Skip help for internal switches. */
435 if (cl_options[i].flags & CL_UNDOCUMENTED)
438 /* Skip switches that have already been printed, mark them to be
443 indent = print_switch (cl_options[i].opt_text, indent);
454 for (i = 0; i < cl_options_count; i++)
456 if ((cl_options[i].flags & filter) != flag)
459 /* Skip help for internal switches. */
460 if (cl_options[i].flags & CL_UNDOCUMENTED)
463 /* Skip switches that have already been printed. */
469 help = cl_options[i].help;
471 help = undocumented_msg;
473 /* Get the translation. */
476 tab = strchr (help, '\t');
485 opt = cl_options[i].opt_text;
489 wrap_help (help, opt, len);
495 /* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
496 word-wrapped HELP in a second column. */
498 print_switch (const char *text, unsigned int indent)
500 unsigned int len = strlen (text) + 1; /* trailing comma */
505 if (indent + len > columns)
516 fputs (text, stdout);
518 return indent + len + 1;
521 /* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
522 word-wrapped HELP in a second column. */
524 wrap_help (const char *help, const char *item, unsigned int item_width)
526 unsigned int col_width = 27;
527 unsigned int remaining, room, len;
529 remaining = strlen (help);
533 room = columns - 3 - MAX (col_width, item_width);
542 for (i = 0; help[i]; i++)
544 if (i >= room && len != remaining)
548 else if ((help[i] == '-' || help[i] == '/')
549 && help[i + 1] != ' '
550 && i > 0 && ISALPHA (help[i - 1]))
555 printf( " %-*.*s %.*s\n", col_width, item_width, item, len, help);
557 while (help[len] == ' ')
565 /* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
566 a simple on-off switch. */
569 option_enabled (int opt_idx)
571 const struct cl_option *option = &(cl_options[opt_idx]);
572 if (option->flag_var)
573 switch (option->var_type)
576 return *(int *) option->flag_var != 0;
579 return *(int *) option->flag_var == option->var_value;
582 return (*(int *) option->flag_var & option->var_value) == 0;
585 return (*(int *) option->flag_var & option->var_value) != 0;
593 /* Fill STATE with the current state of option OPTION. Return true if
594 there is some state to store. */
597 get_option_state (int option, struct cl_option_state *state)
599 if (cl_options[option].flag_var == 0)
602 switch (cl_options[option].var_type)
606 state->data = cl_options[option].flag_var;
607 state->size = sizeof (int);
612 state->ch = option_enabled (option);
613 state->data = &state->ch;
618 state->data = *(const char **) cl_options[option].flag_var;
619 if (state->data == 0)
621 state->size = strlen (state->data) + 1;