Debian Codegraph @

gcc/gcc-urlifier.cc

raw
1 /* Automatic generation of links into GCC's documentation.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 Contributed by David Malcolm <[email protected]>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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 "opts.h"
28 #include "options.h"
29 #include "diagnostic.h"
30 #include "selftest.h"
31 #include "make-unique.h"
32
33 char *
34 make_doc_url (const char *doc_url_suffix)
35 {
36 if (!doc_url_suffix)
37 return nullptr;
38
39 return concat (DOCUMENTATION_ROOT_URL, doc_url_suffix, nullptr);
40 }
41
42 namespace {
43
44 /* Concrete subclass of urlifier for generating links into
45 GCC's HTML documentation. */
46
47 class gcc_urlifier : public urlifier
48 {
49 public:
50 gcc_urlifier (unsigned int lang_mask)
51 : m_lang_mask (lang_mask)
52 {}
53
54 char *get_url_for_quoted_text (const char *p, size_t sz) const final override;
55
56 label_text get_url_suffix_for_quoted_text (const char *p, size_t sz) const;
57 /* We use ATTRIBUTE_UNUSED as this helper is called only from ASSERTs. */
58 label_text get_url_suffix_for_quoted_text (const char *p) const ATTRIBUTE_UNUSED;
59
60 private:
61 label_text get_url_suffix_for_option (const char *p, size_t sz) const;
62
63 unsigned int m_lang_mask;
64 };
65
66 /* class gcc_urlifier : public urlifier. */
67
68 /* Manage a hard-coded mapping from quoted string to URL suffixes
69 in gcc-urlifier.def */
70
71 #define DOC_URL(QUOTED_TEXT, URL_SUFFIX) \
72 { (QUOTED_TEXT), (URL_SUFFIX) }
73
74 static const struct
75 {
76 const char *quoted_text;
77 const char *url_suffix;
78 } doc_urls[] = {
79
80 #include "gcc-urlifier.def"
81
82 };
83
84 /* Implementation of urlifier::get_url_for_quoted_text vfunc for GCC
85 diagnostics. */
86
87 char *
88 gcc_urlifier::get_url_for_quoted_text (const char *p, size_t sz) const
89 {
90 label_text url_suffix = get_url_suffix_for_quoted_text (p, sz);
91 if (url_suffix.get ())
92 return make_doc_url (url_suffix.get ());
93 return nullptr;
94 }
95
96 /* Look for a URL for the quoted string (P, SZ).
97 Return the url suffix if found, or nullptr otherwise. */
98
99 label_text
100 gcc_urlifier::get_url_suffix_for_quoted_text (const char *p, size_t sz) const
101 {
102 if (sz == 0)
103 return label_text ();
104
105 /* If this is an option, look up the option and see if we have
106 a URL for it. */
107 if (p[0] == '-')
108 {
109 label_text suffix = get_url_suffix_for_option (p, sz);
110 if (suffix.get ())
111 return suffix;
112 }
113
114 /* Otherwise, look within the hard-coded data table in gcc-urlifier.def.
115
116 Binary search. This assumes that the quoted_text fields of doc_urls
117 are in sorted order. */
118 int min = 0;
119 int max = ARRAY_SIZE (doc_urls) - 1;
120 while (true)
121 {
122 if (min > max)
123 return label_text ();
124 int midpoint = (min + max) / 2;
125 gcc_assert ((size_t)midpoint < ARRAY_SIZE (doc_urls));
126 int cmp = strncmp (p, doc_urls[midpoint].quoted_text, sz);
127 if (cmp == 0)
128 {
129 if (doc_urls[midpoint].quoted_text[sz] == '\0')
130 return label_text::borrow (doc_urls[midpoint].url_suffix);
131 else
132 max = midpoint - 1;
133 }
134 else if (cmp < 0)
135 max = midpoint - 1;
136 else
137 min = midpoint + 1;
138 }
139
140 /* Not found. */
141 return label_text ();
142 }
143
144 /* For use in selftests. */
145
146 label_text
147 gcc_urlifier::get_url_suffix_for_quoted_text (const char *p) const
148 {
149 return get_url_suffix_for_quoted_text (p, strlen (p));
150 }
151
152 /* Look for a URL for the quoted string (P, SZ) that appears to be
153 an option.
154 Return the url suffix if found, or nullptr otherwise. */
155
156 label_text
157 gcc_urlifier::get_url_suffix_for_option (const char *p, size_t sz) const
158 {
159 /* Look up this option
160
161 find_opt does a binary search, taking a 0-terminated string,
162 and skipping the leading '-'.
163
164 We have a (pointer,size) pair that doesn't necessarily have a
165 terminator.
166 Additionally, we could have one of the e.g. "-Wno-" variants of
167 the option, which find_opt doesn't handle.
168
169 Hence we need to create input for find_opt in a temporary buffer. */
170 char *option_buffer;
171
172 const char *new_prefix;
173 if (const char *old_prefix = get_option_prefix_remapping (p, sz, &new_prefix))
174 {
175 /* We have one of the variants; generate a buffer containing a copy
176 that maps from the old prefix to the new prefix
177 e.g. given "-Wno-suffix", generate "-Wsuffix". */
178 gcc_assert (old_prefix[0] == '-');
179 gcc_assert (new_prefix);
180 gcc_assert (new_prefix[0] == '-');
181
182 const size_t old_prefix_len = strlen (old_prefix);
183 gcc_assert (old_prefix_len <= sz);
184 const size_t suffix_len = sz - old_prefix_len;
185 const size_t new_prefix_len = strlen (new_prefix);
186 const size_t new_sz = new_prefix_len + suffix_len + 1;
187
188 option_buffer = (char *)xmalloc (new_sz);
189 memcpy (option_buffer, new_prefix, new_prefix_len);
190 /* Copy suffix. */
191 memcpy (option_buffer + new_prefix_len, p + old_prefix_len, suffix_len);
192 /* Terminate. */
193 option_buffer[new_prefix_len + suffix_len] = '\0';
194 }
195 else
196 {
197 /* Otherwise we can simply create a 0-terminated clone of the string. */
198 gcc_assert (sz > 0);
199 gcc_assert (p[0] == '-');
200 option_buffer = xstrndup (p, sz);
201 }
202
203 size_t opt = find_opt (option_buffer + 1, m_lang_mask);
204 free (option_buffer);
205
206 if (opt >= N_OPTS)
207 /* Option not recognized. */
208 return label_text ();
209
210 return get_option_url_suffix (opt, m_lang_mask);
211 }
212
213 } // anonymous namespace
214
215 std::unique_ptr<urlifier>
216 make_gcc_urlifier (unsigned int lang_mask)
217 {
218 return ::make_unique<gcc_urlifier> (lang_mask);
219 }
220
221 /* class auto_override_urlifier. */
222
223 auto_override_urlifier::auto_override_urlifier (const urlifier &new_urlifier)
224 {
225 global_dc->push_borrowed_urlifier (new_urlifier);
226 }
227
228 auto_override_urlifier::~auto_override_urlifier ()
229 {
230 global_dc->pop_urlifier ();
231 }
232
233 #if CHECKING_P
234
235 namespace selftest {
236
237 /* Selftests. */
238
239 static void
240 test_gcc_urlifier ()
241 {
242 /* Check that doc_urls.quoted_text is sorted. */
243 for (size_t idx = 1; idx < ARRAY_SIZE (doc_urls); idx++)
244 gcc_assert (strcmp (doc_urls[idx - 1].quoted_text,
245 doc_urls[idx].quoted_text)
246 < 0);
247
248 gcc_urlifier u (0);
249
250 ASSERT_EQ (u.get_url_suffix_for_quoted_text ("").get (), nullptr);
251 ASSERT_EQ (u.get_url_suffix_for_quoted_text (")").get (), nullptr);
252
253 ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("#pragma message").get (),
254 "gcc/Diagnostic-Pragmas.html");
255
256 // Incomplete prefix of a quoted_text
257 ASSERT_EQ (u.get_url_suffix_for_quoted_text ("#pragma mess").get (), nullptr);
258
259 /* Check that every element is findable. */
260 for (size_t idx = 0; idx < ARRAY_SIZE (doc_urls); idx++)
261 ASSERT_STREQ
262 (u.get_url_suffix_for_quoted_text (doc_urls[idx].quoted_text).get (),
263 doc_urls[idx].url_suffix);
264
265 /* Check an option. */
266 ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fpack-struct").get (),
267 "gcc/Code-Gen-Options.html#index-fpack-struct");
268
269 /* Check a "-fno-" variant of an option. */
270 ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("-fno-inline").get (),
271 "gcc/Optimize-Options.html#index-finline");
272 }
273
274 /* Run all of the selftests within this file. */
275
276 void
277 gcc_urlifier_cc_tests ()
278 {
279 test_gcc_urlifier ();
280 }
281
282 } // namespace selftest
283
284 #endif /* #if CHECKING_P */