[pushed,3/3] gcc-urlifier: handle option prefixes such as '-fno-'

Message ID 20240110134105.749310-3-dmalcolm@redhat.com
State Committed
Commit be2bf5dc93ca1ec148c605a5f25b3f7a3028bf3d
Headers
Series [pushed,1/3] pretty-print: add selftest coverage for numbered args |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Patch is already merged
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Patch is already merged

Commit Message

David Malcolm Jan. 10, 2024, 1:41 p.m. UTC
  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').

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r14-7106-gbe2bf5dc93ca1e.

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>
---
 gcc/gcc-urlifier.cc | 49 ++++++++++++++++++++++++++++++++++++++++-----
 gcc/opts-common.cc  | 22 ++++++++++++++++++++
 gcc/opts.h          |  3 +++
 3 files changed, 69 insertions(+), 5 deletions(-)
  

Patch

diff --git a/gcc/gcc-urlifier.cc b/gcc/gcc-urlifier.cc
index 6bd176fc2483..be6459e8d7c1 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 73126cb74e0e..4a2dff243b0c 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 64f94f63d206..82800b8f87cd 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);