Makefile.in: Add opt-suggestions.o.
authorMartin Liska <mliska@suse.cz>
Thu, 28 Jun 2018 07:08:01 +0000 (09:08 +0200)
committerMartin Liska <marxin@gcc.gnu.org>
Thu, 28 Jun 2018 07:08:01 +0000 (07:08 +0000)
.
2018-06-28  Martin Liska  <mliska@suse.cz>

* Makefile.in: Add opt-suggestions.o.
* gcc-main.c: Include opt-suggestions.h.
* gcc.c (driver::driver): Likewise.
(driver::~driver): Remove m_option_suggestions.
(driver::build_option_suggestions): Moved to option_proposer.
(driver::suggest_option): Likewise.
(driver::handle_unrecognized_options): Use option_proposer.
* gcc.h (class driver): Add new memver m_option_proposer.
* opt-suggestions.c: New file.
* opt-suggestions.h: New file.
2018-06-28  Martin Liska  <mliska@suse.cz>

* cppspec.c: Include opt-suggestions.h.
2018-06-28  Martin Liska  <mliska@suse.cz>

* gfortranspec.c: Include opt-suggestions.h.
2018-06-28  Martin Liska  <mliska@suse.cz>

* jit-playback.c: Include opt-suggestions.h.

From-SVN: r262209

13 files changed:
gcc/ChangeLog
gcc/Makefile.in
gcc/c-family/ChangeLog
gcc/c-family/cppspec.c
gcc/fortran/ChangeLog
gcc/fortran/gfortranspec.c
gcc/gcc-main.c
gcc/gcc.c
gcc/gcc.h
gcc/jit/ChangeLog
gcc/jit/jit-playback.c
gcc/opt-suggestions.c [new file with mode: 0644]
gcc/opt-suggestions.h [new file with mode: 0644]

index a5a6ace9d04c45c9dba3c0df7272d999032691a6..aee4f0fbf7a956a3900c7bd81782d443a96f461a 100644 (file)
@@ -1,3 +1,16 @@
+2018-06-28  Martin Liska  <mliska@suse.cz>
+
+       * Makefile.in: Add opt-suggestions.o.
+       * gcc-main.c: Include opt-suggestions.h.
+       * gcc.c (driver::driver): Likewise.
+       (driver::~driver): Remove m_option_suggestions.
+       (driver::build_option_suggestions): Moved to option_proposer.
+       (driver::suggest_option): Likewise.
+       (driver::handle_unrecognized_options): Use option_proposer.
+       * gcc.h (class driver): Add new memver m_option_proposer.
+       * opt-suggestions.c: New file.
+       * opt-suggestions.h: New file.
+
 2018-06-28  Martin Liska  <mliska@suse.cz>
 
        * vec.h (class auto_string_vec): New (moved from auto_argvec).
index 1f38cacde7a16da62f129b068f97213d396db0b3..66c8b6e4d03411fb9b65033d40dc132ea9e45d7d 100644 (file)
@@ -1615,7 +1615,7 @@ OBJS-libcommon = diagnostic.o diagnostic-color.o diagnostic-show-locus.o \
 # compiler and containing target-dependent code.
 OBJS-libcommon-target = $(common_out_object_file) prefix.o params.o \
        opts.o opts-common.o options.o vec.o hooks.o common/common-targhooks.o \
-       hash-table.o file-find.o spellcheck.o selftest.o
+       hash-table.o file-find.o spellcheck.o selftest.o opt-suggestions.o
 
 # This lists all host objects for the front ends.
 ALL_HOST_FRONTEND_OBJS = $(foreach v,$(CONFIG_LANGUAGES),$($(v)_OBJS))
index fc7f7de1ed4bc5ed5c1a622b2efcb194f6fa0fa6..9596eec56bc4467a853b77f37acafa28ae2c8400 100644 (file)
@@ -1,3 +1,7 @@
+2018-06-28  Martin Liska  <mliska@suse.cz>
+
+       * cppspec.c: Include opt-suggestions.h.
+
 2018-06-20  Chung-Lin Tang <cltang@codesourcery.com>
            Thomas Schwinge <thomas@codesourcery.com>
            Cesar Philippidis  <cesar@codesourcery.com>
index 1e0a8bcd2940871e2be888d638005e8181dafadc..66540239f5356c41c31388ce7fdbca473f4b8554 100644 (file)
@@ -21,6 +21,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
+#include "opt-suggestions.h"
 #include "gcc.h"
 #include "opts.h"
 
index f052cfed2c86eada7eafd546540dbc1a4e855679..6e1af2e3053146b2db5c1e2a0b69580459cf9407 100644 (file)
@@ -1,3 +1,7 @@
+2018-06-28  Martin Liska  <mliska@suse.cz>
+
+       * gfortranspec.c: Include opt-suggestions.h.
+
 2018-06-25  Fritz Reese  <fritzoreese@gmail.com>
 
        PR fortran/82972
index fe1ec0447b304ae6bcf068bffbd39b59a7e4da11..4ba3a8dba60eda166d4accfe4bd8b13a860cf943 100644 (file)
@@ -47,6 +47,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
+#include "opt-suggestions.h"
 #include "gcc.h"
 #include "opts.h"
 
index 9e6de743adc8dee931b72b67c81d08447ada2cbd..6a759bbcc1c74614c116aa2f6b9677b73f71f4fd 100644 (file)
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "obstack.h"
 #include "intl.h"
 #include "prefix.h"
+#include "opt-suggestions.h"
 #include "gcc.h"
 
 /* Implement the top-level "main" within the driver in terms of
index 405d2e38d446c79eeb928f8549d7c7e1a267c02a..dda1fd353988c28d851de265cb809bfd98ecbb7a 100644 (file)
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -36,6 +36,7 @@ compilation is specified by a string called a "spec".  */
 #include "obstack.h"
 #include "intl.h"
 #include "prefix.h"
+#include "opt-suggestions.h"
 #include "gcc.h"
 #include "diagnostic.h"
 #include "flags.h"
@@ -7270,8 +7271,7 @@ compare_files (char *cmpfile[])
 
 driver::driver (bool can_finalize, bool debug) :
   explicit_link_files (NULL),
-  decoded_options (NULL),
-  m_option_suggestions (NULL)
+  decoded_options (NULL)
 {
   env.init (can_finalize, debug);
 }
@@ -7280,14 +7280,6 @@ driver::~driver ()
 {
   XDELETEVEC (explicit_link_files);
   XDELETEVEC (decoded_options);
-  if (m_option_suggestions)
-    {
-      int i;
-      char *str;
-      FOR_EACH_VEC_ELT (*m_option_suggestions, i, str)
-       free (str);
-      delete m_option_suggestions;
-    }
 }
 
 /* driver::main is implemented as a series of driver:: method calls.  */
@@ -7776,106 +7768,6 @@ driver::maybe_putenv_OFFLOAD_TARGETS () const
   offload_targets = NULL;
 }
 
-/* Helper function for driver::suggest_option.  Populate
-   m_option_suggestions with candidate strings for misspelled options.
-   The strings will be freed by the driver's dtor.  */
-
-void
-driver::build_option_suggestions (void)
-{
-  gcc_assert (m_option_suggestions == NULL);
-  m_option_suggestions = new auto_vec <char *> ();
-
-  /* We build a vec of m_option_suggestions, using add_misspelling_candidates
-     to add copies of strings, without a leading dash.  */
-
-  for (unsigned int i = 0; i < cl_options_count; i++)
-    {
-      const struct cl_option *option = &cl_options[i];
-      const char *opt_text = option->opt_text;
-      switch (i)
-       {
-       default:
-         if (option->var_type == CLVC_ENUM)
-           {
-             const struct cl_enum *e = &cl_enums[option->var_enum];
-             for (unsigned j = 0; e->values[j].arg != NULL; j++)
-               {
-                 char *with_arg = concat (opt_text, e->values[j].arg, NULL);
-                 add_misspelling_candidates (m_option_suggestions, option,
-                                             with_arg);
-                 free (with_arg);
-               }
-           }
-         else
-           add_misspelling_candidates (m_option_suggestions, option,
-                                       opt_text);
-         break;
-
-       case OPT_fsanitize_:
-       case OPT_fsanitize_recover_:
-         /* -fsanitize= and -fsanitize-recover= can take
-            a comma-separated list of arguments.  Given that combinations
-            are supported, we can't add all potential candidates to the
-            vec, but if we at least add them individually without commas,
-            we should do a better job e.g. correcting
-              "-sanitize=address"
-            to
-              "-fsanitize=address"
-            rather than to "-Wframe-address" (PR driver/69265).  */
-         {
-           for (int j = 0; sanitizer_opts[j].name != NULL; ++j)
-             {
-               struct cl_option optb;
-               /* -fsanitize=all is not valid, only -fno-sanitize=all.
-                  So don't register the positive misspelling candidates
-                  for it.  */
-               if (sanitizer_opts[j].flag == ~0U && i == OPT_fsanitize_)
-                 {
-                   optb = *option;
-                   optb.opt_text = opt_text = "-fno-sanitize=";
-                   optb.cl_reject_negative = true;
-                   option = &optb;
-                 }
-               /* Get one arg at a time e.g. "-fsanitize=address".  */
-               char *with_arg = concat (opt_text,
-                                        sanitizer_opts[j].name,
-                                        NULL);
-               /* Add with_arg and all of its variant spellings e.g.
-                  "-fno-sanitize=address" to candidates (albeit without
-                  leading dashes).  */
-               add_misspelling_candidates (m_option_suggestions, option,
-                                           with_arg);
-               free (with_arg);
-             }
-         }
-         break;
-       }
-    }
-}
-
-/* Helper function for driver::handle_unrecognized_options.
-
-   Given an unrecognized option BAD_OPT (without the leading dash),
-   locate the closest reasonable matching option (again, without the
-   leading dash), or NULL.
-
-   The returned string is owned by the driver instance.  */
-
-const char *
-driver::suggest_option (const char *bad_opt)
-{
-  /* Lazily populate m_option_suggestions.  */
-  if (!m_option_suggestions)
-    build_option_suggestions ();
-  gcc_assert (m_option_suggestions);
-
-  /* "m_option_suggestions" is now populated.  Use it.  */
-  return find_closest_string
-    (bad_opt,
-     (auto_vec <const char *> *) m_option_suggestions);
-}
-
 /* Reject switches that no pass was interested in.  */
 
 void
@@ -7884,7 +7776,7 @@ driver::handle_unrecognized_options ()
   for (size_t i = 0; (int) i < n_switches; i++)
     if (! switches[i].validated)
       {
-       const char *hint = suggest_option (switches[i].part1);
+       const char *hint = m_option_proposer.suggest_option (switches[i].part1);
        if (hint)
          error ("unrecognized command line option %<-%s%>;"
                 " did you mean %<-%s%>?",
index ddbf42f78ea7fcff60e31528ee9c1197e2e6d681..a760618339301e09d6dd68fb52cb9a987ef0564b 100644 (file)
--- a/gcc/gcc.h
+++ b/gcc/gcc.h
@@ -45,8 +45,6 @@ class driver
   void putenv_COLLECT_GCC (const char *argv0) const;
   void maybe_putenv_COLLECT_LTO_WRAPPER () const;
   void maybe_putenv_OFFLOAD_TARGETS () const;
-  void build_option_suggestions (void);
-  const char *suggest_option (const char *bad_opt);
   void handle_unrecognized_options ();
   int maybe_print_and_exit () const;
   bool prepare_infiles ();
@@ -59,7 +57,7 @@ class driver
   char *explicit_link_files;
   struct cl_decoded_option *decoded_options;
   unsigned int decoded_options_count;
-  auto_vec <char *> *m_option_suggestions;
+  option_proposer m_option_proposer;
 };
 
 /* The mapping of a spec function name to the C function that
index 059cbb886bf357a0067ce276018b01b58472ecdf..22679671a0152090014cafd0e7f1543fbdac75aa 100644 (file)
@@ -1,3 +1,7 @@
+2018-06-28  Martin Liska  <mliska@suse.cz>
+
+       * jit-playback.c: Include opt-suggestions.h.
+
 2018-06-28  Martin Liska  <mliska@suse.cz>
 
        * jit-playback.c (class auto_argvec): Moved to vec.h.
index 01c4567de0571e4b322168aacd038199b5b9d53d..f11642bf4c639864469fa8e4e23b0e0b40b6bcdf 100644 (file)
@@ -36,6 +36,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "attribs.h"
 #include "context.h"
 #include "fold-const.h"
+#include "opt-suggestions.h"
 #include "gcc.h"
 #include "diagnostic.h"
 
diff --git a/gcc/opt-suggestions.c b/gcc/opt-suggestions.c
new file mode 100644 (file)
index 0000000..90ab80e
--- /dev/null
@@ -0,0 +1,122 @@
+/* Provide option suggestion for --complete option and a misspelled
+   used by a user.
+   Copyright (C) 2016-2018 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#include "config.h"
+#include "system.h"
+#include "coretypes.h"
+#include "tm.h"
+#include "opts.h"
+#include "params.h"
+#include "spellcheck.h"
+#include "opt-suggestions.h"
+#include "selftest.h"
+
+option_proposer::~option_proposer ()
+{
+  delete m_option_suggestions;
+}
+
+const char *
+option_proposer::suggest_option (const char *bad_opt)
+{
+  /* Lazily populate m_option_suggestions.  */
+  if (!m_option_suggestions)
+    build_option_suggestions ();
+  gcc_assert (m_option_suggestions);
+
+  /* "m_option_suggestions" is now populated.  Use it.  */
+  return find_closest_string
+    (bad_opt,
+     (auto_vec <const char *> *) m_option_suggestions);
+}
+
+void
+option_proposer::build_option_suggestions (void)
+{
+  gcc_assert (m_option_suggestions == NULL);
+  m_option_suggestions = new auto_string_vec ();
+
+  /* We build a vec of m_option_suggestions, using add_misspelling_candidates
+     to add copies of strings, without a leading dash.  */
+
+  for (unsigned int i = 0; i < cl_options_count; i++)
+    {
+      const struct cl_option *option = &cl_options[i];
+      const char *opt_text = option->opt_text;
+      switch (i)
+       {
+       default:
+         if (option->var_type == CLVC_ENUM)
+           {
+             const struct cl_enum *e = &cl_enums[option->var_enum];
+             for (unsigned j = 0; e->values[j].arg != NULL; j++)
+               {
+                 char *with_arg = concat (opt_text, e->values[j].arg, NULL);
+                 add_misspelling_candidates (m_option_suggestions, option,
+                                             with_arg);
+                 free (with_arg);
+               }
+           }
+         else
+           add_misspelling_candidates (m_option_suggestions, option,
+                                       opt_text);
+         break;
+
+       case OPT_fsanitize_:
+       case OPT_fsanitize_recover_:
+         /* -fsanitize= and -fsanitize-recover= can take
+            a comma-separated list of arguments.  Given that combinations
+            are supported, we can't add all potential candidates to the
+            vec, but if we at least add them individually without commas,
+            we should do a better job e.g. correcting
+              "-sanitize=address"
+            to
+              "-fsanitize=address"
+            rather than to "-Wframe-address" (PR driver/69265).  */
+         {
+           for (int j = 0; sanitizer_opts[j].name != NULL; ++j)
+             {
+               struct cl_option optb;
+               /* -fsanitize=all is not valid, only -fno-sanitize=all.
+                  So don't register the positive misspelling candidates
+                  for it.  */
+               if (sanitizer_opts[j].flag == ~0U && i == OPT_fsanitize_)
+                 {
+                   optb = *option;
+                   optb.opt_text = opt_text = "-fno-sanitize=";
+                   optb.cl_reject_negative = true;
+                   option = &optb;
+                 }
+               /* Get one arg at a time e.g. "-fsanitize=address".  */
+               char *with_arg = concat (opt_text,
+                                        sanitizer_opts[j].name,
+                                        NULL);
+               /* Add with_arg and all of its variant spellings e.g.
+                  "-fno-sanitize=address" to candidates (albeit without
+                  leading dashes).  */
+               add_misspelling_candidates (m_option_suggestions, option,
+                                           with_arg);
+               free (with_arg);
+             }
+         }
+         break;
+       }
+    }
+}
diff --git a/gcc/opt-suggestions.h b/gcc/opt-suggestions.h
new file mode 100644 (file)
index 0000000..ccd4e45
--- /dev/null
@@ -0,0 +1,59 @@
+/* Provide suggestions to handle misspelled options, and implement the
+   --complete option for auto-completing options from a prefix.
+   Copyright (C) 2016-2018 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+#ifndef GCC_OPT_PROPOSER_H
+#define GCC_OPT_PROPOSER_H
+
+/* Option proposer is class used by driver in order to provide hints
+   for wrong options provided.  And it's used by --complete option that's
+   intended to be invoked by BASH in order to provide better option
+   completion support.  */
+
+class option_proposer
+{
+public:
+  /* Default constructor.  */
+  option_proposer (): m_option_suggestions (NULL)
+  {}
+
+  /* Default destructor.  */
+  ~option_proposer ();
+
+  /* Helper function for driver::handle_unrecognized_options.
+
+     Given an unrecognized option BAD_OPT (without the leading dash),
+     locate the closest reasonable matching option (again, without the
+     leading dash), or NULL.
+
+     The returned string is owned by the option_proposer instance.  */
+  const char *suggest_option (const char *bad_opt);
+
+private:
+  /* Helper function for option_proposer::suggest_option.  Populate
+     m_option_suggestions with candidate strings for misspelled options.
+     The strings will be freed by the option_proposer's dtor.  */
+  void build_option_suggestions ();
+
+private:
+  /* Cache with all suggestions.  */
+  auto_string_vec *m_option_suggestions;
+};
+
+#endif  /* GCC_OPT_PROPOSER_H */