#include "pretty-print.h"
#include "pretty-print-urlifier.h"
#include "gcc-urlifier.h"
+#include "opts.h"
+#include "options.h"
#include "selftest.h"
namespace {
class gcc_urlifier : public urlifier
{
public:
+ gcc_urlifier (unsigned int lang_mask)
+ : m_lang_mask (lang_mask)
+ {}
+
char *get_url_for_quoted_text (const char *p, size_t sz) const final override;
- const char *get_url_suffix_for_quoted_text (const char *p, size_t sz) const;
+ label_text get_url_suffix_for_quoted_text (const char *p, size_t sz) const;
/* We use ATTRIBUTE_UNUSED as this helper is called only from ASSERTs. */
- const char *get_url_suffix_for_quoted_text (const char *p) const ATTRIBUTE_UNUSED;
+ label_text get_url_suffix_for_quoted_text (const char *p) const ATTRIBUTE_UNUSED;
private:
+ label_text get_url_suffix_for_option (const char *p, size_t sz) const;
+
static char *
make_doc_url (const char *doc_url_suffix);
+
+ unsigned int m_lang_mask;
};
/* class gcc_urlifier : public urlifier. */
+/* Manage a hard-coded mapping from quoted string to URL suffixes
+ in gcc-urlifier.def */
+
#define DOC_URL(QUOTED_TEXT, URL_SUFFIX) \
{ (QUOTED_TEXT), (URL_SUFFIX) }
-const struct
+static const struct
{
const char *quoted_text;
const char *url_suffix;
};
+/* Implementation of urlifier::get_url_for_quoted_text vfunc for GCC
+ diagnostics. */
+
char *
gcc_urlifier::get_url_for_quoted_text (const char *p, size_t sz) const
{
- if (const char *url_suffix = get_url_suffix_for_quoted_text (p, sz))
- return make_doc_url (url_suffix);
+ label_text url_suffix = get_url_suffix_for_quoted_text (p, sz);
+ if (url_suffix.get ())
+ return make_doc_url (url_suffix.get ());
return nullptr;
}
-const char *
+/* Look for a URL for the quoted string (P, SZ).
+ Return the url suffix if found, or nullptr otherwise. */
+
+label_text
gcc_urlifier::get_url_suffix_for_quoted_text (const char *p, size_t sz) const
{
- /* Binary search. This assumes that the quoted_text fields of doc_urls
+ if (sz == 0)
+ return label_text ();
+
+ /* If this is an option, look up the option and see if we have
+ a URL for it. */
+ if (p[0] == '-')
+ {
+ label_text suffix = get_url_suffix_for_option (p, sz);
+ if (suffix.get ())
+ return suffix;
+ }
+
+ /* Otherwise, look within the hard-coded data table in gcc-urlifier.def.
+
+ Binary search. This assumes that the quoted_text fields of doc_urls
are in sorted order. */
int min = 0;
int max = ARRAY_SIZE (doc_urls) - 1;
while (true)
{
if (min > max)
- return nullptr;
+ return label_text ();
int midpoint = (min + max) / 2;
gcc_assert ((size_t)midpoint < ARRAY_SIZE (doc_urls));
int cmp = strncmp (p, doc_urls[midpoint].quoted_text, sz);
if (cmp == 0)
{
if (doc_urls[midpoint].quoted_text[sz] == '\0')
- return doc_urls[midpoint].url_suffix;
+ return label_text::borrow (doc_urls[midpoint].url_suffix);
else
max = midpoint - 1;
}
else
min = midpoint + 1;
}
- return nullptr;
+
+ /* Not found. */
+ return label_text ();
}
-const char *
+/* For use in selftests. */
+
+label_text
gcc_urlifier::get_url_suffix_for_quoted_text (const char *p) const
{
return get_url_suffix_for_quoted_text (p, strlen (p));
}
+/* Look for a URL for the quoted string (P, SZ) that appears to be
+ an option.
+ Return the url suffix if found, or nullptr otherwise. */
+
+label_text
+gcc_urlifier::get_url_suffix_for_option (const char *p, size_t sz) const
+{
+ /* Look up this option
+
+ find_opt does a binary search, taking a 0-terminated string,
+ 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);
+
+ if (opt >= N_OPTS)
+ /* Option not recognized. */
+ return label_text ();
+
+ return get_option_url_suffix (opt, m_lang_mask);
+}
+
char *
gcc_urlifier::make_doc_url (const char *doc_url_suffix)
{
} // anonymous namespace
urlifier *
-make_gcc_urlifier ()
+make_gcc_urlifier (unsigned int lang_mask)
{
- return new gcc_urlifier ();
+ return new gcc_urlifier (lang_mask);
}
#if CHECKING_P
doc_urls[idx].quoted_text)
< 0);
- gcc_urlifier u;
+ gcc_urlifier u (0);
- ASSERT_EQ (u.get_url_suffix_for_quoted_text (""), nullptr);
- ASSERT_EQ (u.get_url_suffix_for_quoted_text (")"), nullptr);
+ ASSERT_EQ (u.get_url_suffix_for_quoted_text ("").get (), nullptr);
+ ASSERT_EQ (u.get_url_suffix_for_quoted_text (")").get (), nullptr);
- ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("#pragma message"),
+ ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("#pragma message").get (),
"gcc/Diagnostic-Pragmas.html");
// Incomplete prefix of a quoted_text
- ASSERT_EQ (u.get_url_suffix_for_quoted_text ("#pragma mess"), nullptr);
+ ASSERT_EQ (u.get_url_suffix_for_quoted_text ("#pragma mess").get (), nullptr);
/* Check that every element is findable. */
for (size_t idx = 0; idx < ARRAY_SIZE (doc_urls); idx++)
ASSERT_STREQ
- (u.get_url_suffix_for_quoted_text (doc_urls[idx].quoted_text),
+ (u.get_url_suffix_for_quoted_text (doc_urls[idx].quoted_text).get (),
doc_urls[idx].url_suffix);
+
+ /* Check an option. */
+ ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fpack-struct").get (),
+ "gcc/Code-Gen-Options.html#index-fpack-struct");
}
} // namespace selftest