hppa: Revise REG+D address support to allow long displacements before reload
[official-gcc.git] / gcc / gcc-urlifier.cc
blob0dbff9853132dbee0221a5658489202d6dba0164
1 /* Automatic generation of links into GCC's documentation.
2 Copyright (C) 2023 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "pretty-print.h"
25 #include "pretty-print-urlifier.h"
26 #include "gcc-urlifier.h"
27 #include "selftest.h"
29 namespace {
31 /* Concrete subclass of urlifier for generating links into
32 GCC's HTML documentation. */
34 class gcc_urlifier : public urlifier
36 public:
37 char *get_url_for_quoted_text (const char *p, size_t sz) const final override;
39 const char *get_url_suffix_for_quoted_text (const char *p, size_t sz) const;
40 /* We use ATTRIBUTE_UNUSED as this helper is called only from ASSERTs. */
41 const char *get_url_suffix_for_quoted_text (const char *p) const ATTRIBUTE_UNUSED;
43 private:
44 static char *
45 make_doc_url (const char *doc_url_suffix);
48 /* class gcc_urlifier : public urlifier. */
50 #define DOC_URL(QUOTED_TEXT, URL_SUFFIX) \
51 { (QUOTED_TEXT), (URL_SUFFIX) }
53 const struct
55 const char *quoted_text;
56 const char *url_suffix;
57 } doc_urls[] = {
59 #include "gcc-urlifier.def"
63 char *
64 gcc_urlifier::get_url_for_quoted_text (const char *p, size_t sz) const
66 if (const char *url_suffix = get_url_suffix_for_quoted_text (p, sz))
67 return make_doc_url (url_suffix);
68 return nullptr;
71 const char *
72 gcc_urlifier::get_url_suffix_for_quoted_text (const char *p, size_t sz) const
74 /* Binary search. This assumes that the quoted_text fields of doc_urls
75 are in sorted order. */
76 int min = 0;
77 int max = ARRAY_SIZE (doc_urls) - 1;
78 while (true)
80 if (min > max)
81 return nullptr;
82 int midpoint = (min + max) / 2;
83 gcc_assert ((size_t)midpoint < ARRAY_SIZE (doc_urls));
84 int cmp = strncmp (p, doc_urls[midpoint].quoted_text, sz);
85 if (cmp == 0)
87 if (doc_urls[midpoint].quoted_text[sz] == '\0')
88 return doc_urls[midpoint].url_suffix;
89 else
90 max = midpoint - 1;
92 else if (cmp < 0)
93 max = midpoint - 1;
94 else
95 min = midpoint + 1;
97 return nullptr;
100 const char *
101 gcc_urlifier::get_url_suffix_for_quoted_text (const char *p) const
103 return get_url_suffix_for_quoted_text (p, strlen (p));
106 char *
107 gcc_urlifier::make_doc_url (const char *doc_url_suffix)
109 if (!doc_url_suffix)
110 return nullptr;
112 return concat (DOCUMENTATION_ROOT_URL, doc_url_suffix, nullptr);
115 } // anonymous namespace
117 urlifier *
118 make_gcc_urlifier ()
120 return new gcc_urlifier ();
123 #if CHECKING_P
125 namespace selftest {
127 /* Selftests. */
129 /* Run all of the selftests within this file. */
131 void
132 gcc_urlifier_cc_tests ()
134 /* Check that doc_urls.quoted_text is sorted. */
135 for (size_t idx = 1; idx < ARRAY_SIZE (doc_urls); idx++)
136 gcc_assert (strcmp (doc_urls[idx - 1].quoted_text,
137 doc_urls[idx].quoted_text)
138 < 0);
140 gcc_urlifier u;
142 ASSERT_EQ (u.get_url_suffix_for_quoted_text (""), nullptr);
143 ASSERT_EQ (u.get_url_suffix_for_quoted_text (")"), nullptr);
145 ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("#pragma message"),
146 "gcc/Diagnostic-Pragmas.html");
148 // Incomplete prefix of a quoted_text
149 ASSERT_EQ (u.get_url_suffix_for_quoted_text ("#pragma mess"), nullptr);
151 /* Check that every element is findable. */
152 for (size_t idx = 0; idx < ARRAY_SIZE (doc_urls); idx++)
153 ASSERT_STREQ
154 (u.get_url_suffix_for_quoted_text (doc_urls[idx].quoted_text),
155 doc_urls[idx].url_suffix);
158 } // namespace selftest
160 #endif /* #if CHECKING_P */