[gcc r14-7106] gcc-urlifier: handle option prefixes such as '-fno-'

David Malcolm dmalcolm@gcc.gnu.org
Wed Jan 10 13:36:41 GMT 2024


https://gcc.gnu.org/g:be2bf5dc93ca1ec148c605a5f25b3f7a3028bf3d

commit r14-7106-gbe2bf5dc93ca1ec148c605a5f25b3f7a3028bf3d
Author: David Malcolm <dmalcolm@redhat.com>
Date:   Wed Jan 10 08:33:48 2024 -0500

    gcc-urlifier: handle option prefixes such as '-fno-'
    
    Given e.g. this missppelled option (omitting the trailing 's'):
    $ LANG=C ./xgcc -B. -fno-inline-small-function
    xgcc: error: unrecognized command-line option '-fno-inline-small-function'; did you mean '-fno-inline-small-functions'?
    
    we weren't providing a documentation URL for the suggestion.
    
    The issue is the URLification code uses find_opt, which doesn't consider
    the various '-fno-' prefixes.
    
    This patch adds a way to find the pertinent prefix remapping and uses it
    when determining URLs.
    With this patch, the suggestion '-fno-inline-small-functions' now gets a
    documentation link (to that of '-finline-small-functions').
    
    gcc/ChangeLog:
            * gcc-urlifier.cc (gcc_urlifier::get_url_suffix_for_option):
            Handle prefix mappings before calling find_opt.
            (selftest::gcc_urlifier_cc_tests): Add example of urlifying a
            "-fno-"-prefixed command-line option.
            * opts-common.cc (get_option_prefix_remapping): New.
            * opts.h (get_option_prefix_remapping): New decl.
    
    Signed-off-by: David Malcolm <dmalcolm@redhat.com>

Diff:
---
 gcc/gcc-urlifier.cc | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
 gcc/opts-common.cc  | 22 ++++++++++++++++++++++
 gcc/opts.h          |  3 +++
 3 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/gcc/gcc-urlifier.cc b/gcc/gcc-urlifier.cc
index 6bd176fc248..be6459e8d7c 100644
--- a/gcc/gcc-urlifier.cc
+++ b/gcc/gcc-urlifier.cc
@@ -154,11 +154,46 @@ gcc_urlifier::get_url_suffix_for_option (const char *p, size_t sz) const
      and skipping the leading '-'.
 
      We have a (pointer,size) pair that doesn't necessarily have a
-     terminator, so create a 0-terminated clone of the string.  */
-  gcc_assert (sz > 0);
-  char *tmp = xstrndup (p + 1, sz - 1); // skip the leading '-'
-  size_t opt = find_opt (tmp, m_lang_mask);
-  free (tmp);
+     terminator.
+     Additionally, we could have one of the e.g. "-Wno-" variants of
+     the option, which find_opt doesn't handle.
+
+     Hence we need to create input for find_opt in a temporary buffer.  */
+  char *option_buffer;
+
+  const char *new_prefix;
+  if (const char *old_prefix = get_option_prefix_remapping (p, sz, &new_prefix))
+    {
+      /* We have one of the variants; generate a buffer containing a copy
+	 that maps from the old prefix to the new prefix
+	 e.g. given "-Wno-suffix", generate "-Wsuffix".  */
+      gcc_assert (old_prefix[0] == '-');
+      gcc_assert (new_prefix);
+      gcc_assert (new_prefix[0] == '-');
+
+      const size_t old_prefix_len = strlen (old_prefix);
+      gcc_assert (old_prefix_len <= sz);
+      const size_t suffix_len = sz - old_prefix_len;
+      const size_t new_prefix_len = strlen (new_prefix);
+      const size_t new_sz = new_prefix_len + suffix_len + 1;
+
+      option_buffer = (char *)xmalloc (new_sz);
+      memcpy (option_buffer, new_prefix, new_prefix_len);
+      /* Copy suffix.  */
+      memcpy (option_buffer + new_prefix_len, p + old_prefix_len, suffix_len);
+      /* Terminate.  */
+      option_buffer[new_prefix_len + suffix_len] = '\0';
+    }
+  else
+    {
+      /* Otherwise we can simply create a 0-terminated clone of the string.  */
+      gcc_assert (sz > 0);
+      gcc_assert (p[0] == '-');
+      option_buffer = xstrndup (p, sz);
+    }
+
+  size_t opt = find_opt (option_buffer + 1, m_lang_mask);
+  free (option_buffer);
 
   if (opt >= N_OPTS)
     /* Option not recognized.  */
@@ -221,6 +256,10 @@ gcc_urlifier_cc_tests ()
   /* Check an option.  */
   ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fpack-struct").get (),
 		"gcc/Code-Gen-Options.html#index-fpack-struct");
+
+  /* Check a "-fno-" variant of an option.  */
+  ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fno-inline").get (),
+		"gcc/Optimize-Options.html#index-finline");
 }
 
 } // namespace selftest
diff --git a/gcc/opts-common.cc b/gcc/opts-common.cc
index 73126cb74e0..4a2dff243b0 100644
--- a/gcc/opts-common.cc
+++ b/gcc/opts-common.cc
@@ -468,6 +468,28 @@ static const struct option_map option_map[] =
     { "--no-", NULL, "-f", false, true }
   };
 
+/* Given buffer P of size SZ, look for a prefix within OPTION_MAP;
+   if found, return the prefix and write the new prefix to *OUT_NEW_PREFIX.
+   Otherwise return nullptr.  */
+
+const char *
+get_option_prefix_remapping (const char *p, size_t sz,
+			     const char **out_new_prefix)
+{
+  for (unsigned i = 0; i < ARRAY_SIZE (option_map); i++)
+    {
+      const char * const old_prefix = option_map[i].opt0;
+      const size_t old_prefix_len = strlen (old_prefix);
+      if (old_prefix_len <= sz
+	  && !memcmp (p, old_prefix, old_prefix_len))
+	{
+	  *out_new_prefix = option_map[i].new_prefix;
+	  return old_prefix;
+	}
+    }
+  return nullptr;
+}
+
 /* Helper function for gcc.cc's driver::suggest_option, for populating the
    vec of suggestions for misspelled options.
 
diff --git a/gcc/opts.h b/gcc/opts.h
index 64f94f63d20..82800b8f87c 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -491,6 +491,9 @@ extern const struct zero_call_used_regs_opts_s
 
 extern vec<const char *> help_option_arguments;
 
+extern const char *get_option_prefix_remapping (const char *p, size_t sz,
+						const char **out_new_prefix);
+
 extern void add_misspelling_candidates (auto_vec<char *> *candidates,
 					const struct cl_option *option,
 					const char *base_option);


More information about the Gcc-cvs mailing list