From 70f25790a1effc8f1cba3cb8a368b129c66a87b0 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Thu, 19 May 2016 00:52:08 +0000 Subject: PR driver/69265: add hint for options with misspelled arguments opts-common.c's cmdline_handle_error handles invalid arguments for options with CL_ERR_ENUM_ARG by building a string listing the valid arguments. By also building a vec of valid arguments, we can use find_closest_string and provide a hint if we see a close misspelling. gcc/ChangeLog: PR driver/69265 * Makefile.in (GCC_OBJS): Move spellcheck.o to... (OBJS-libcommon-target): ...here. * opts-common.c: Include spellcheck.h. (cmdline_handle_error): Build a vec of valid options and use it to suggest provide hints for misspelled arguments. gcc/testsuite/ChangeLog: PR driver/69265 * gcc.dg/spellcheck-options-11.c: New test case. From-SVN: r236439 --- gcc/opts-common.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'gcc/opts-common.c') diff --git a/gcc/opts-common.c b/gcc/opts-common.c index bb689827227..4e1ef497ed8 100644 --- a/gcc/opts-common.c +++ b/gcc/opts-common.c @@ -24,6 +24,7 @@ along with GCC; see the file COPYING3. If not see #include "opts.h" #include "options.h" #include "diagnostic.h" +#include "spellcheck.h" static void prune_options (struct cl_decoded_option **, unsigned int *); @@ -1113,6 +1114,7 @@ cmdline_handle_error (location_t loc, const struct cl_option *option, for (i = 0; e->values[i].arg != NULL; i++) len += strlen (e->values[i].arg) + 1; + auto_vec candidates; s = XALLOCAVEC (char, len); p = s; for (i = 0; e->values[i].arg != NULL; i++) @@ -1123,9 +1125,16 @@ cmdline_handle_error (location_t loc, const struct cl_option *option, memcpy (p, e->values[i].arg, arglen); p[arglen] = ' '; p += arglen + 1; + candidates.safe_push (e->values[i].arg); } p[-1] = 0; - inform (loc, "valid arguments to %qs are: %s", option->opt_text, s); + const char *hint = find_closest_string (arg, &candidates); + if (hint) + inform (loc, "valid arguments to %qs are: %s; did you mean %qs?", + option->opt_text, s, hint); + else + inform (loc, "valid arguments to %qs are: %s", option->opt_text, s); + return true; } -- cgit v1.2.1