16
16
#include < mrdox/MetadataFwd.hpp>
17
17
#include < mrdox/Reporter.hpp>
18
18
#include < mrdox/meta/Index.hpp>
19
- #include < mrdox/meta/Types .hpp>
19
+ #include < mrdox/meta/Symbols .hpp>
20
20
#include < clang/Tooling/Execution.h>
21
21
#include < llvm/Support/Mutex.h>
22
+ #include < cassert>
22
23
#include < memory>
23
24
#include < string>
24
25
#include < type_traits>
@@ -31,98 +32,42 @@ namespace mrdox {
31
32
*/
32
33
class Corpus
33
34
{
34
- explicit
35
- Corpus (
36
- std::shared_ptr<Config const > config) noexcept
37
- : config_(std::move(config))
38
- {
39
- }
40
-
41
35
public:
42
- struct Visitor
43
- {
44
- virtual ~Visitor () = default ;
45
- virtual void visit (NamespaceInfo const &) {}
46
- virtual void visit (RecordInfo const &) {}
47
- virtual void visit (FunctionOverloads const &) {}
48
- virtual void visit (FunctionInfo const &) {}
49
- virtual void visit (EnumInfo const &) {}
50
- virtual void visit (TypedefInfo const &) {}
51
- };
36
+ virtual ~Corpus () = default ;
52
37
53
- // --------------------------------------------
54
- //
55
- // Observers
56
- //
57
38
// --------------------------------------------
58
39
59
- /* * Return the ID of the global namespace.
60
- */
61
- static
62
- SymbolID
63
- globalNamespaceID () noexcept
64
- {
65
- return EmptySID;
66
- }
67
-
68
- /* * Return true if s0 is less than s1.
69
-
70
- This function returns true if the string
71
- s0 is less than the string s1. The comparison
72
- is first made without regard to case, unless
73
- the strings compare equal and then they
74
- are compared with lowercase letters coming
75
- before uppercase letters.
76
- */
77
- static
78
- bool
79
- symbolCompare (
80
- llvm::StringRef symbolName0,
81
- llvm::StringRef symbolName1) noexcept ;
82
-
83
40
/* * Return the Config used to generate this corpus.
84
41
*/
42
+ virtual
85
43
std::shared_ptr<Config const > const &
86
- config () const noexcept
87
- {
88
- return config_;
89
- }
44
+ config () const noexcept = 0 ;
45
+
46
+ /* * Return the list of all uniquely identified symbols.
47
+ */
48
+ virtual
49
+ std::vector<SymbolID> const &
50
+ allSymbols () const noexcept = 0 ;
90
51
91
52
/* * Return the metadata for the global namespace.
92
53
*/
93
54
NamespaceInfo const &
94
55
globalNamespace () const noexcept ;
95
56
96
- /* * Return true if an Info with the specified symbol ID exists .
57
+ /* * Return the Info with the matching ID, or nullptr .
97
58
*/
98
- bool exists (SymbolID const & id) const noexcept ;
99
-
100
- /* * Return a pointer to the Info with the specified symbol ID, or nullptr.
101
- */
102
- template <class T >
103
- T*
104
- find (
105
- SymbolID const & id) noexcept ;
106
-
107
- /* * Return a pointer to the Info with the specified symbol ID, or nullptr.
108
- */
109
- template <class T >
110
- T const *
111
- find (
112
- SymbolID const & id) const noexcept ;
113
-
114
- /* * Return the Info with the specified symbol ID.
59
+ /* * @{ */
60
+ virtual Info* find (SymbolID const & id) noexcept = 0;
61
+ virtual Info const * find (SymbolID const & id) const noexcept = 0;
62
+ /* * @} */
115
63
116
- If the id does not exist, the behavior is undefined .
64
+ /* * Return true if an Info with the specified symbol ID exists .
117
65
*/
118
- template <class T >
119
- T&
120
- get (
121
- SymbolID const & id) noexcept
66
+ bool
67
+ exists (
68
+ SymbolID const & id) const noexcept
122
69
{
123
- auto p = find<T>(id);
124
- assert (p != nullptr );
125
- return *p;
70
+ return find (id) != nullptr ;
126
71
}
127
72
128
73
/* * Return the Info with the specified symbol ID.
@@ -132,20 +77,22 @@ class Corpus
132
77
template <class T >
133
78
T const &
134
79
get (
135
- SymbolID const & id) const noexcept
136
- {
137
- auto p = find<T>(id);
138
- assert (p != nullptr );
139
- return *p;
140
- }
80
+ SymbolID const & id) const noexcept ;
141
81
142
- /* * Return the list of all uniquely identified symbols.
82
+ // --------------------------------------------
83
+
84
+ /* * Base class used to visit elements of the corpus.
143
85
*/
144
- std::vector<SymbolID> const &
145
- allSymbols () const noexcept
86
+ struct Visitor
146
87
{
147
- return allSymbols_;
148
- }
88
+ virtual ~Visitor () = default ;
89
+ virtual void visit (NamespaceInfo const &) {}
90
+ virtual void visit (RecordInfo const &) {}
91
+ virtual void visit (Overloads const &) {}
92
+ virtual void visit (FunctionInfo const &) {}
93
+ virtual void visit (EnumInfo const &) {}
94
+ virtual void visit (TypedefInfo const &) {}
95
+ };
149
96
150
97
/* * Visit the specified symbol ID or node.
151
98
*/
@@ -156,13 +103,9 @@ class Corpus
156
103
void visit (Info const & I, Visitor& f) const ;
157
104
/* * @} */
158
105
159
- // --------------------------------------------
160
- //
161
- // Modifiers
162
- //
163
106
// --------------------------------------------
164
107
165
- /* * Build the intermediate representation of the code being documented .
108
+ /* * Build metadata for a set of translation units .
166
109
167
110
@param config A shared pointer to the configuration.
168
111
@@ -176,135 +119,30 @@ class Corpus
176
119
tooling::ToolExecutor& ex,
177
120
std::shared_ptr<Config const > config,
178
121
Reporter& R);
179
-
180
- /* * Store a key/value pair in the tool results.
181
-
182
- This function inserts the bitcode for the
183
- specified symbol ID into the tool results
184
- of the execution context.
185
-
186
- Each symbol ID can have multiple bitcodes.
187
- */
188
- static
189
- void
190
- reportResult (
191
- tooling::ExecutionContext& exc,
192
- SymbolID id,
193
- std::string bitcode);
194
-
195
- private:
196
- struct Temps ;
197
-
198
- // --------------------------------------------
199
- //
200
- // Implementation
201
- //
202
- // --------------------------------------------
203
-
204
- /* * Insert this element and all its children into the Corpus.
205
-
206
- @param Thread Safety
207
- May be called concurrently.
208
- */
209
- void insert (std::unique_ptr<Info> Ip);
210
-
211
- /* * Insert Info into the index
212
-
213
- @param Thread Safety
214
- May be called concurrently.
215
- */
216
- void insertIntoIndex (Info const & I);
217
-
218
- /* * Canonicalize the contents of the object.
219
-
220
- @return true upon success.
221
-
222
- @param R The diagnostic reporting object to
223
- use for delivering errors and information.
224
- */
225
- [[nodiscard]]
226
- bool canonicalize (Reporter& R);
227
-
228
- bool canonicalize (std::vector<SymbolID>& list, Temps& t, Reporter& R);
229
- bool canonicalize (NamespaceInfo& I, Temps& t, Reporter& R);
230
- bool canonicalize (RecordInfo& I, Temps& t, Reporter& R);
231
- bool canonicalize (FunctionInfo& I, Temps& t, Reporter& R);
232
- bool canonicalize (EnumInfo& I, Temps& t, Reporter& R);
233
- bool canonicalize (TypedefInfo& I, Temps& t, Reporter& R);
234
- bool canonicalize (Scope& I, Temps& t, Reporter& R);
235
- bool canonicalize (std::vector<Reference>& list, Temps& t, Reporter& R);
236
- bool canonicalize (llvm::SmallVectorImpl<MemberTypeInfo>& list, Temps& t, Reporter& R);
237
-
238
- private:
239
- std::shared_ptr<Config const > config_;
240
-
241
- // Index of all emitted symbols.
242
- Index Idx;
243
-
244
- // Table of Info keyed on Symbol ID.
245
- llvm::StringMap<std::unique_ptr<Info>> InfoMap;
246
-
247
- // list of all symbols
248
- std::vector<SymbolID> allSymbols_;
249
-
250
- llvm::sys::Mutex infoMutex;
251
- llvm::sys::Mutex allSymbolsMutex;
252
- bool isCanonical_ = false ;
253
122
};
254
123
255
124
// ------------------------------------------------
256
125
257
126
template <class T >
258
- T*
259
- Corpus::
260
- find (
261
- SymbolID const & id) noexcept
262
- {
263
- auto it = InfoMap.find (llvm::toStringRef (id));
264
- if (it != InfoMap.end ())
265
- {
266
- auto const t = static_cast <T*>(it->getValue ().get ());
267
- if constexpr (std::is_same_v<T, NamespaceInfo>)
268
- assert (t->IT == InfoType::IT_namespace);
269
- else if constexpr (std::is_same_v<T, RecordInfo>)
270
- assert (t->IT == InfoType::IT_record);
271
- else if constexpr (std::is_same_v<T, FunctionInfo>)
272
- assert (t->IT == InfoType::IT_function);
273
- else if constexpr (std::is_same_v<T, EnumInfo>)
274
- assert (t->IT == InfoType::IT_enum);
275
- else if constexpr (std::is_same_v<T, TypedefInfo>)
276
- assert (t->IT == InfoType::IT_typedef);
277
- return t;
278
- }
279
- return nullptr ;
280
- }
281
-
282
- /* * Return a pointer to the Info with the specified symbol ID, or nullptr.
283
- */
284
- template <class T >
285
- T const *
127
+ T const &
286
128
Corpus::
287
- find (
129
+ get (
288
130
SymbolID const & id) const noexcept
289
131
{
290
- auto it = InfoMap.find (llvm::toStringRef (id));
291
- if (it != InfoMap.end ())
292
- {
293
- auto const t = static_cast <
294
- T const *>(it->getValue ().get ());
295
- if constexpr (std::is_same_v<T, NamespaceInfo>)
296
- assert (t->IT == InfoType::IT_namespace);
297
- else if constexpr (std::is_same_v<T, RecordInfo>)
298
- assert (t->IT == InfoType::IT_record);
299
- else if constexpr (std::is_same_v<T, FunctionInfo>)
300
- assert (t->IT == InfoType::IT_function);
301
- else if constexpr (std::is_same_v<T, EnumInfo>)
302
- assert (t->IT == InfoType::IT_enum);
303
- else if constexpr (std::is_same_v<T, TypedefInfo>)
304
- assert (t->IT == InfoType::IT_typedef);
305
- return t;
306
- }
307
- return nullptr ;
132
+ auto I = find (id);
133
+ assert (I != nullptr );
134
+ auto const t = static_cast <T const *>(I);
135
+ if constexpr (std::is_same_v<T, NamespaceInfo>)
136
+ assert (t->IT == InfoType::IT_namespace);
137
+ else if constexpr (std::is_same_v<T, RecordInfo>)
138
+ assert (t->IT == InfoType::IT_record);
139
+ else if constexpr (std::is_same_v<T, FunctionInfo>)
140
+ assert (t->IT == InfoType::IT_function);
141
+ else if constexpr (std::is_same_v<T, EnumInfo>)
142
+ assert (t->IT == InfoType::IT_enum);
143
+ else if constexpr (std::is_same_v<T, TypedefInfo>)
144
+ assert (t->IT == InfoType::IT_typedef);
145
+ return *t;
308
146
}
309
147
310
148
} // mrdox
0 commit comments