1 /* Command line option handling.
2 Copyright (C) 2002, 2003, 2004 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
28 /* True if we should exit after parsing options. */
29 bool exit_after_options;
31 /* Treat warnings as errors. -Werror. */
32 bool warnings_are_errors;
34 /* Don't suppress warnings from system headers. -Wsystem-headers. */
35 bool warn_system_headers;
37 /* Columns of --help display. */
38 static unsigned int columns = 80;
40 /* What to print when a switch has no documentation. */
41 static const char undocumented_msg[] = N_("This switch lacks documentation");
43 /* Input file names. */
44 const char **in_fnames;
45 unsigned num_in_fnames;
47 static size_t find_opt (const char *, int);
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 flag);
57 static unsigned int print_switch (const char *text, unsigned int indent);
59 /* Perform a binary search to find which option the command-line INPUT
60 matches. Returns its index in the option array, and N_OPTS
61 (cl_options_count) on failure.
63 This routine is quite subtle. A normal binary search is not good
64 enough because some options can be suffixed with an argument, and
65 multiple sub-matches can occur, e.g. input of "-pedantic" matching
66 the initial substring of "-pedantic-errors".
68 A more complicated example is -gstabs. It should match "-g" with
69 an argument of "stabs". Suppose, however, that the number and list
70 of switches are such that the binary search tests "-gen-decls"
71 before having tested "-g". This doesn't match, and as "-gen-decls"
72 is less than "-gstabs", it will become the lower bound of the
73 binary search range, and "-g" will never be seen. To resolve this
74 issue, opts.sh makes "-gen-decls" point, via the back_chain member,
75 to "-g" so that failed searches that end between "-gen-decls" and
76 the lexicographically subsequent switch know to go back and see if
77 "-g" causes a match (which it does in this example).
79 This search is done in such a way that the longest match for the
80 front end in question wins. If there is no match for the current
81 front end, the longest match for a different front end is returned
82 (or N_OPTS if none) and the caller emits an error message. */
84 find_opt (const char *input, int lang_mask)
86 size_t mn, mx, md, opt_len;
87 size_t match_wrong_lang;
91 mx = cl_options_count;
93 /* Find mn such this lexicographical inequality holds:
94 cl_options[mn] <= input < cl_options[mn + 1]. */
98 opt_len = cl_options[md].opt_len;
99 comp = strncmp (input, cl_options[md].opt_text + 1, opt_len);
107 /* This is the switch that is the best match but for a different
108 front end, or cl_options_count if there is no match at all. */
109 match_wrong_lang = cl_options_count;
111 /* Backtrace the chain of possible matches, returning the longest
112 one, if any, that fits best. With current GCC switches, this
113 loop executes at most twice. */
116 const struct cl_option *opt = &cl_options[mn];
118 /* Is the input either an exact match or a prefix that takes a
120 if (!strncmp (input, opt->opt_text + 1, opt->opt_len)
121 && (input[opt->opt_len] == '\0' || (opt->flags & CL_JOINED)))
123 /* If language is OK, return it. */
124 if (opt->flags & lang_mask)
127 /* If we haven't remembered a prior match, remember this
128 one. Any prior match is necessarily better. */
129 if (match_wrong_lang == cl_options_count)
130 match_wrong_lang = mn;
133 /* Try the next possibility. This is cl_options_count if there
135 mn = opt->back_chain;
137 while (mn != cl_options_count);
139 /* Return the best wrong match, or cl_options_count if none. */
140 return match_wrong_lang;
143 /* If ARG is a non-negative integer made up solely of digits, return its
144 value, otherwise return -1. */
146 integral_argument (const char *arg)
150 while (*p && ISDIGIT (*p))
159 /* Return a malloced slash-separated list of languages in MASK. */
161 write_langs (unsigned int mask)
163 unsigned int n = 0, len = 0;
164 const char *lang_name;
167 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
168 if (mask & (1U << n))
169 len += strlen (lang_name) + 1;
171 result = xmalloc (len);
173 for (n = 0; (lang_name = lang_names[n]) != 0; n++)
174 if (mask & (1U << n))
178 strcpy (result + len, lang_name);
179 len += strlen (lang_name);
187 /* Complain that switch OPT_INDEX does not apply to this front end. */
189 complain_wrong_lang (const char *text, const struct cl_option *option,
190 unsigned int lang_mask)
192 char *ok_langs, *bad_lang;
194 ok_langs = write_langs (option->flags);
195 bad_lang = write_langs (lang_mask);
197 /* Eventually this should become a hard error IMO. */
198 warning (0, "command line option \"%s\" is valid for %s but not for %s",
199 text, ok_langs, bad_lang);
205 /* Handle the switch beginning at ARGV for the language indicated by
206 LANG_MASK. Returns the number of switches consumed. */
208 handle_option (const char **argv, unsigned int lang_mask)
211 const char *opt, *arg = 0;
214 unsigned int result = 0;
215 const struct cl_option *option;
219 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
220 if (opt_index == cl_options_count
221 && (opt[1] == 'W' || opt[1] == 'f' || opt[1] == 'm')
222 && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-')
224 /* Drop the "no-" from negative switches. */
225 size_t len = strlen (opt) - 3;
227 dup = xmalloc (len + 1);
230 memcpy (dup + 2, opt + 5, len - 2 + 1);
233 opt_index = find_opt (opt + 1, lang_mask | CL_COMMON | CL_TARGET);
236 if (opt_index == cl_options_count)
239 option = &cl_options[opt_index];
241 /* Reject negative form of switches that don't take negatives as
243 if (!value && (option->flags & CL_REJECT_NEGATIVE))
246 /* We've recognized this switch. */
249 /* Check to see if the option is disabled for this configuration. */
250 if (option->flags & CL_DISABLED)
252 error ("command line option \"%s\""
253 " is not supported by this configuration", opt);
257 /* Sort out any argument the switch takes. */
258 if (option->flags & CL_JOINED)
260 /* Have arg point to the original switch. This is because
261 some code, such as disable_builtin_function, expects its
262 argument to be persistent until the program exits. */
263 arg = argv[0] + cl_options[opt_index].opt_len + 1;
265 arg += strlen ("no-");
267 if (*arg == '\0' && !(option->flags & CL_MISSING_OK))
269 if (option->flags & CL_SEPARATE)
275 /* Missing argument. */
279 else if (option->flags & CL_SEPARATE)
285 /* Now we've swallowed any potential argument, complain if this
286 is a switch for a different front end. */
287 if (!(option->flags & (lang_mask | CL_COMMON | CL_TARGET)))
289 complain_wrong_lang (argv[0], option, lang_mask);
293 if (arg == NULL && (option->flags & (CL_JOINED | CL_SEPARATE)))
295 if (!lang_hooks.missing_argument (opt, opt_index))
296 error ("missing argument to \"%s\"", opt);
300 /* If the switch takes an integer, convert it. */
301 if (arg && (option->flags & CL_UINTEGER))
303 value = integral_argument (arg);
306 error ("argument to \"%s\" should be a non-negative integer",
312 if (option->flag_var)
313 switch (option->var_type)
316 *(int *) option->flag_var = value;
320 *(int *) option->flag_var = (value
322 : !option->var_value);
327 if ((value != 0) == (option->var_type == CLVC_BIT_SET))
328 *(int *) option->flag_var |= option->var_value;
330 *(int *) option->flag_var &= ~option->var_value;
331 ////if (option->flag_var == &target_flags)
332 //// target_flags_explicit |= option->var_value;
336 *(const char **) option->flag_var = arg;
340 if (option->flags & lang_mask)
341 if ((*lang_hooks.handle_option) (opt_index, arg, value) == 0)
344 if (result && (option->flags & CL_COMMON))
345 if (common_handle_option (opt_index, arg, value) == 0)
348 ////if (result && (option->flags & CL_TARGET))
349 //// if (!targetm.handle_option (opt_index, arg, value))
358 /* Handle FILENAME from the command line. */
360 add_input_filename (const char *filename)
363 in_fnames = xrealloc (in_fnames, num_in_fnames * sizeof (in_fnames[0]));
364 in_fnames[num_in_fnames - 1] = filename;
367 /* Decode and handle the vector of command line options. LANG_MASK
368 contains has a single bit set representing the current
371 handle_options (unsigned int argc, const char **argv, unsigned int lang_mask)
375 for (i = 1; i < argc; i += n)
377 const char *opt = argv[i];
379 /* Interpret "-" or a non-switch as a file name. */
380 if (opt[0] != '-' || opt[1] == '\0')
382 if (main_input_filename == NULL)
383 main_input_filename = opt;
384 add_input_filename (opt);
389 n = handle_option (argv + i, lang_mask);
394 error ("unrecognized command line option \"%s\"", opt);
399 /* Parse command line options and set default flag values. Do minimal
400 options processing. */
402 decode_options (unsigned int argc, const char **argv)
404 unsigned int lang_mask;
406 /* Perform language-specific options initialization. */
407 lang_mask = lang_hooks.init_options (argc, argv);
409 /* Scan to see what optimization level has been specified. That will
410 determine the default value of many flags. */
412 handle_options (argc, argv, lang_mask);
415 /* Handle target- and language-independent options. Return zero to
416 generate an "unknown option" message. Only options that need
417 extra handling need to be listed here; if you simply want
418 VALUE assigned to a variable, it happens automatically. */
421 common_handle_option (size_t scode, const char *arg,
422 int value ATTRIBUTE_UNUSED)
424 enum opt_code code = (enum opt_code) scode;
433 exit_after_options = true;
437 print_version (stderr, "");
438 exit_after_options = true;
442 warnings_are_errors = value;
445 case OPT_Wsystem_headers:
446 warn_system_headers = value;
450 decode_d_option (arg);
453 case OPT_pedantic_errors:
454 flag_pedantic_errors = 1;
458 inhibit_warnings = true;
465 /* Output --help text. */
472 GET_ENVIRONMENT (p, "COLUMNS");
475 int value = atoi (p);
480 puts (_("The following options are language-independent:\n"));
482 print_filtered_help (CL_COMMON);
484 for (i = 0; lang_names[i]; i++)
486 printf (_("The %s front end recognizes the following options:\n\n"),
488 print_filtered_help (1U << i);
492 /* Print help for a specific front-end, etc. */
494 print_filtered_help (unsigned int flag)
496 unsigned int i, len, filter, indent = 0;
497 bool duplicates = false;
498 const char *help, *opt, *tab;
499 static char *printed;
501 if (flag == CL_COMMON || flag == CL_TARGET)
505 printed = xmalloc (cl_options_count);
506 memset (printed, 0, cl_options_count);
510 /* Don't print COMMON options twice. */
511 filter = flag | CL_COMMON;
513 for (i = 0; i < cl_options_count; i++)
515 if ((cl_options[i].flags & filter) != flag)
518 /* Skip help for internal switches. */
519 if (cl_options[i].flags & CL_UNDOCUMENTED)
522 /* Skip switches that have already been printed, mark them to be
527 indent = print_switch (cl_options[i].opt_text, indent);
538 for (i = 0; i < cl_options_count; i++)
540 if ((cl_options[i].flags & filter) != flag)
543 /* Skip help for internal switches. */
544 if (cl_options[i].flags & CL_UNDOCUMENTED)
547 /* Skip switches that have already been printed. */
553 help = cl_options[i].help;
555 help = undocumented_msg;
557 /* Get the translation. */
560 tab = strchr (help, '\t');
569 opt = cl_options[i].opt_text;
573 wrap_help (help, opt, len);
579 /* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
580 word-wrapped HELP in a second column. */
582 print_switch (const char *text, unsigned int indent)
584 unsigned int len = strlen (text) + 1; /* trailing comma */
589 if (indent + len > columns)
600 fputs (text, stdout);
602 return indent + len + 1;
605 /* Output ITEM, of length ITEM_WIDTH, in the left column, followed by
606 word-wrapped HELP in a second column. */
608 wrap_help (const char *help, const char *item, unsigned int item_width)
610 unsigned int col_width = 27;
611 unsigned int remaining, room, len;
613 remaining = strlen (help);
617 room = columns - 3 - MAX (col_width, item_width);
626 for (i = 0; help[i]; i++)
628 if (i >= room && len != remaining)
632 else if ((help[i] == '-' || help[i] == '/')
633 && help[i + 1] != ' '
634 && i > 0 && ISALPHA (help[i - 1]))
639 printf( " %-*.*s %.*s\n", col_width, item_width, item, len, help);
641 while (help[len] == ' ')
649 /* Return 1 if OPTION is enabled, 0 if it is disabled, or -1 if it isn't
650 a simple on-off switch. */
653 option_enabled (int opt_idx)
655 const struct cl_option *option = &(cl_options[opt_idx]);
656 if (option->flag_var)
657 switch (option->var_type)
660 return *(int *) option->flag_var != 0;
663 return *(int *) option->flag_var == option->var_value;
666 return (*(int *) option->flag_var & option->var_value) == 0;
669 return (*(int *) option->flag_var & option->var_value) != 0;
677 /* Fill STATE with the current state of option OPTION. Return true if
678 there is some state to store. */
681 get_option_state (int option, struct cl_option_state *state)
683 if (cl_options[option].flag_var == 0)
686 switch (cl_options[option].var_type)
690 state->data = cl_options[option].flag_var;
691 state->size = sizeof (int);
696 state->ch = option_enabled (option);
697 state->data = &state->ch;
702 state->data = *(const char **) cl_options[option].flag_var;
703 if (state->data == 0)
705 state->size = strlen (state->data) + 1;