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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
/* Automatic generation of links into GCC's documentation.
Copyright (C) 2023-2024 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/>. */
#define INCLUDE_MEMORY
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "pretty-print.h"
#include "pretty-print-urlifier.h"
#include "gcc-urlifier.h"
#include "opts.h"
#include "options.h"
#include "diagnostic.h"
#include "selftest.h"
#include "make-unique.h"
#include "target.h"
/* class attribute_urlifier : public urlifier. */
/* By default, use the target's documentation name. */
attribute_urlifier::attribute_urlifier ()
: m_target_docs_name (targetm.documentation_name)
{
}
/* Explicitly specify a target's documentation name, for use in selftests. */
attribute_urlifier::attribute_urlifier (const char *target_docs_name)
: m_target_docs_name (target_docs_name)
{
}
struct attr_url_entry
{
const char *m_name;
const char *m_url_suffix;
const char *m_target_docs_name;
size_t m_name_len;
};
#include "attr-urls.def"
/* We look in two passes: first for an exact match on target name (if any).
Otherwise, we look for one with an empty target name. */
/* Search for STR, LEN in the given TABLE.
If TARGET_DOCS_NAME is non-null, then look for an exact match on target name.
If TARGET_DOCS_NAME is null, then look for an empty string for the
target name. */
static const attr_url_entry *
find_attr_url_entry (const char *str,
size_t str_len,
const char *target_docs_name,
const attr_url_entry *table,
size_t table_sz)
{
/* This is linear search, but TABLE_SZ ought not to be very large. */
for (size_t i = 0; i < table_sz; i++)
if (str_len == table[i].m_name_len)
if (0 == strncmp (str, table[i].m_name, str_len))
{
if (target_docs_name)
{
/* Reject entries with m_target_docs_name that doesn't match. */
if (strcmp (target_docs_name, table[i].m_target_docs_name))
continue;
}
else
{
/* Reject entries for which m_target_docs_name is non-empty. */
if (table[i].m_target_docs_name[0])
continue;
}
return &table[i];
}
return nullptr;
}
/* Search for STR, LEN in all of the attribute tables, in order.
TARGET_DOCS_NAME works as above. */
static const attr_url_entry *
find_attr_url_entry (const char *str,
size_t str_len,
const char *target_docs_name)
{
for (size_t table_idx = 0; table_idx < ARRAY_SIZE (attr_url_tables);
table_idx++)
if (const attr_url_entry *entry
= find_attr_url_entry (str, str_len, target_docs_name,
attr_url_tables[table_idx].m_table,
attr_url_tables[table_idx].m_table_sz))
return entry;
return nullptr;
}
char *
attribute_urlifier::get_url_for_quoted_text (const char *p,
size_t sz) const
{
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;
}
label_text
attribute_urlifier::get_url_suffix_for_quoted_text (const char *p,
size_t sz) const
{
/* Skip any text after a non-identifier character, so that
e.g. given "access(read_write, 2, 3)" we only compare
against "access". */
for (size_t i = 0; i < sz; i++)
if (!ISIDNUM (p[i]))
{
/* Truncate to p[0..i). */
sz = i;
break;
}
if (m_target_docs_name)
if (const attr_url_entry *entry
= find_attr_url_entry (p, sz, m_target_docs_name))
return label_text::borrow (entry->m_url_suffix);
if (const attr_url_entry *entry = find_attr_url_entry (p, sz, nullptr))
return label_text::borrow (entry->m_url_suffix);
return label_text ();
}
label_text
attribute_urlifier::get_url_suffix_for_quoted_text (const char *p) const
{
return get_url_suffix_for_quoted_text (p, strlen (p));
}
#if CHECKING_P
namespace selftest {
/* Selftests. */
static void
test_attribute_urlifier ()
{
attribute_urlifier u;
ASSERT_EQ (u.get_url_suffix_for_quoted_text ("").get (), nullptr);
ASSERT_EQ (u.get_url_suffix_for_quoted_text (")").get (), nullptr);
/* Examples of function attributes. */
ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("alias").get (),
"gcc/Common-Function-Attributes.html"
"#index-alias-function-attribute");
ASSERT_STREQ (u.get_url_suffix_for_quoted_text
("access(read_write, 2, 3)").get (),
"gcc/Common-Function-Attributes.html"
"#index-access-function-attribute");
/* Example of enumerator attribute. */
ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("deprecated").get (),
"gcc/Enumerator-Attributes.html"
"#index-deprecated-enumerator-attribute");
/* We don't yet have an example of a label attribute, since all
label attributes have a matching function attribute of the same
name, which is found first. */
/* Example of statement attribute. */
ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("assume").get (),
"gcc/Statement-Attributes.html"
"#index-assume-statement-attribute");
/* Examples of type attributes. */
ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("hardbool").get (),
"gcc/Common-Type-Attributes.html"
"#index-hardbool-type-attribute");
ASSERT_STREQ (u.get_url_suffix_for_quoted_text
("packed").get (),
"gcc/Common-Type-Attributes.html"
"#index-packed-type-attribute");
/* Example of variable attribute. */
ASSERT_STREQ (u.get_url_suffix_for_quoted_text ("nonstring").get (),
"gcc/Common-Variable-Attributes.html"
"#index-nonstring-variable-attribute");
/* Example of target-specific attributes.
For example, "interrupt" has many target-specific documentation URLs. */
{
attribute_urlifier u_rl78 ("RL78");
attribute_urlifier u_x86 ("x86");
attribute_urlifier u_unrecognized ("not-a-target");
ASSERT_STREQ (u_rl78.get_url_suffix_for_quoted_text ("interrupt").get (),
"gcc/RL78-Function-Attributes.html"
"#index-interrupt-function-attribute_002c-RL78");
ASSERT_STREQ (u_x86.get_url_suffix_for_quoted_text ("interrupt").get (),
"gcc/x86-Function-Attributes.html"
"#index-interrupt-function-attribute_002c-x86");
ASSERT_STREQ (u_unrecognized.get_url_suffix_for_quoted_text
("interrupt").get (),
"gcc/Common-Function-Attributes.html"
"#index-interrupt-function-attribute");
}
}
/* Run all of the selftests within this file. */
void
gcc_attribute_urlifier_cc_tests ()
{
test_attribute_urlifier ();
}
} // namespace selftest
#endif /* #if CHECKING_P */
|