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>
This commit is contained in:
David Malcolm 2024-01-10 08:33:48 -05:00
parent 5daf9104ed
commit be2bf5dc93
3 changed files with 69 additions and 5 deletions

View file

@ -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

View file

@ -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.

View file

@ -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);