1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/* Automatic generation of links into GCC's documentation.
Copyright (C) 2023 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "pretty-print.h"
#include "pretty-print-urlifier.h"
#include "gcc-urlifier.h"
#include "selftest.h"
namespace {
/* Concrete subclass of urlifier for generating links into
GCC's HTML documentation. */
class gcc_urlifier : public urlifier
{
public:
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;
/* 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;
private:
static char *
make_doc_url (const char *doc_url_suffix);
};
/* class gcc_urlifier : public urlifier. */
#define DOC_URL(QUOTED_TEXT, URL_SUFFIX) \
{ (QUOTED_TEXT), (URL_SUFFIX) }
const struct
{
const char *quoted_text;
const char *url_suffix;
} doc_urls[] = {
#include "gcc-urlifier.def"
};
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);
return nullptr;
}
const char *
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
are in sorted order. */
int min = 0;
int max = ARRAY_SIZE (doc_urls) - 1;
while (true)
{
if (min > max)
return nullptr;
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;
else
max = midpoint - 1;
}
else if (cmp < 0)
max = midpoint - 1;
else
min = midpoint + 1;
}
return nullptr;
}
const char *
gcc_urlifier::get_url_suffix_for_quoted_text (const char *p) const
{
return get_url_suffix_for_quoted_text (p, strlen (p));
}
char *
gcc_urlifier::make_doc_url (const char *doc_url_suffix)
{
if (!doc_url_suffix)
return nullptr;
return concat (DOCUMENTATION_ROOT_URL, doc_url_suffix, nullptr);
}
} // anonymous namespace
urlifier *
make_gcc_urlifier ()
{
return new gcc_urlifier ();
}
#if CHECKING_P
namespace selftest {
/* Selftests. */
/* Run all of the selftests within this file. */
void
gcc_urlifier_cc_tests ()
{
/* Check that doc_urls.quoted_text is sorted. */
for (size_t idx = 1; idx < ARRAY_SIZE (doc_urls); idx++)
gcc_assert (strcmp (doc_urls[idx - 1].quoted_text,
doc_urls[idx].quoted_text)
< 0);
gcc_urlifier u;
ASSERT_EQ (u.get_url_suffix_for_quoted_text (""), nullptr);
ASSERT_EQ (u.get_url_suffix_for_quoted_text (")"), nullptr);
ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("#pragma message"),
"gcc/Diagnostic-Pragmas.html");
// Incomplete prefix of a quoted_text
ASSERT_EQ (u.get_url_suffix_for_quoted_text ("#pragma mess"), 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),
doc_urls[idx].url_suffix);
}
} // namespace selftest
#endif /* #if CHECKING_P */
|