Skip to content

Commit 014593f

Browse files
committed
refactor Corpus
1 parent 23da910 commit 014593f

31 files changed

+945
-1042
lines changed

include/mrdox/Corpus.hpp

Lines changed: 51 additions & 213 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
#include <mrdox/MetadataFwd.hpp>
1717
#include <mrdox/Reporter.hpp>
1818
#include <mrdox/meta/Index.hpp>
19-
#include <mrdox/meta/Types.hpp>
19+
#include <mrdox/meta/Symbols.hpp>
2020
#include <clang/Tooling/Execution.h>
2121
#include <llvm/Support/Mutex.h>
22+
#include <cassert>
2223
#include <memory>
2324
#include <string>
2425
#include <type_traits>
@@ -31,98 +32,42 @@ namespace mrdox {
3132
*/
3233
class Corpus
3334
{
34-
explicit
35-
Corpus(
36-
std::shared_ptr<Config const> config) noexcept
37-
: config_(std::move(config))
38-
{
39-
}
40-
4135
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;
5237

53-
//--------------------------------------------
54-
//
55-
// Observers
56-
//
5738
//--------------------------------------------
5839

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-
8340
/** Return the Config used to generate this corpus.
8441
*/
42+
virtual
8543
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;
9051

9152
/** Return the metadata for the global namespace.
9253
*/
9354
NamespaceInfo const&
9455
globalNamespace() const noexcept;
9556

96-
/** Return true if an Info with the specified symbol ID exists.
57+
/** Return the Info with the matching ID, or nullptr.
9758
*/
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+
/** @} */
11563

116-
If the id does not exist, the behavior is undefined.
64+
/** Return true if an Info with the specified symbol ID exists.
11765
*/
118-
template<class T>
119-
T&
120-
get(
121-
SymbolID const& id) noexcept
66+
bool
67+
exists(
68+
SymbolID const& id) const noexcept
12269
{
123-
auto p = find<T>(id);
124-
assert(p != nullptr);
125-
return *p;
70+
return find(id) != nullptr;
12671
}
12772

12873
/** Return the Info with the specified symbol ID.
@@ -132,20 +77,22 @@ class Corpus
13277
template<class T>
13378
T const&
13479
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;
14181

142-
/** Return the list of all uniquely identified symbols.
82+
//--------------------------------------------
83+
84+
/** Base class used to visit elements of the corpus.
14385
*/
144-
std::vector<SymbolID> const&
145-
allSymbols() const noexcept
86+
struct Visitor
14687
{
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+
};
14996

15097
/** Visit the specified symbol ID or node.
15198
*/
@@ -156,13 +103,9 @@ class Corpus
156103
void visit(Info const& I, Visitor& f) const;
157104
/** @} */
158105

159-
//--------------------------------------------
160-
//
161-
// Modifiers
162-
//
163106
//--------------------------------------------
164107

165-
/** Build the intermediate representation of the code being documented.
108+
/** Build metadata for a set of translation units.
166109
167110
@param config A shared pointer to the configuration.
168111
@@ -176,135 +119,30 @@ class Corpus
176119
tooling::ToolExecutor& ex,
177120
std::shared_ptr<Config const> config,
178121
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;
253122
};
254123

255124
//------------------------------------------------
256125

257126
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&
286128
Corpus::
287-
find(
129+
get(
288130
SymbolID const& id) const noexcept
289131
{
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;
308146
}
309147

310148
} // mrdox

include/mrdox/Metadata.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include <mrdox/meta/Enum.hpp>
1919
#include <mrdox/meta/FieldType.hpp>
2020
#include <mrdox/meta/Function.hpp>
21-
#include <mrdox/meta/FunctionOverloads.hpp>
21+
#include <mrdox/meta/Overloads.hpp>
2222
#include <mrdox/meta/Index.hpp>
2323
#include <mrdox/meta/Info.hpp>
2424
#include <mrdox/meta/Javadoc.hpp>
@@ -29,10 +29,10 @@
2929
#include <mrdox/meta/Reference.hpp>
3030
#include <mrdox/meta/Scope.hpp>
3131
#include <mrdox/meta/Symbol.hpp>
32+
#include <mrdox/meta/Symbols.hpp>
3233
#include <mrdox/meta/Template.hpp>
3334
#include <mrdox/meta/TemplateParam.hpp>
3435
#include <mrdox/meta/Type.hpp>
3536
#include <mrdox/meta/Typedef.hpp>
36-
#include <mrdox/meta/Types.hpp>
3737

3838
#endif

include/mrdox/MetadataFwd.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// Forward declarations for all types
1616
// related to metadata extracted from AST
1717

18-
#include <mrdox/meta/Types.hpp>
18+
#include <mrdox/meta/Symbols.hpp>
1919

2020
namespace clang {
2121
namespace mrdox {
@@ -25,8 +25,8 @@ struct EnumValueInfo;
2525
struct EnumInfo;
2626
struct FieldTypeInfo;
2727
struct FunctionInfo;
28-
struct FunctionOverloads;
29-
struct FunctionOverloadsSet;
28+
struct Overloads;
29+
struct OverloadsSet;
3030
struct Index;
3131
struct Info;
3232
struct Javadoc;

include/mrdox/format/RecursiveWriter.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define MRDOX_RECURSIVEWRITER_HPP
1414

1515
#include <mrdox/MetadataFwd.hpp>
16-
#include <mrdox/meta/Types.hpp>
16+
#include <mrdox/meta/Symbols.hpp>
1717
#include <mrdox/Reporter.hpp>
1818
#include <llvm/ADT/StringRef.h>
1919
#include <llvm/Support/raw_ostream.h>

include/mrdox/meta/Function.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <mrdox/meta/Function.hpp>
1717
#include <mrdox/meta/Symbol.hpp>
1818
#include <mrdox/meta/Template.hpp>
19-
#include <mrdox/meta/Types.hpp>
19+
#include <mrdox/meta/Symbols.hpp>
2020
#include <clang/Basic/Specifiers.h>
2121
#include <llvm/ADT/Optional.h>
2222
#include <llvm/ADT/SmallString.h>

include/mrdox/meta/Info.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include <mrdox/meta/Javadoc.hpp>
1616
#include <mrdox/meta/Reference.hpp>
17-
#include <mrdox/meta/Types.hpp>
17+
#include <mrdox/meta/Symbols.hpp>
1818
#include <llvm/ADT/StringRef.h>
1919
#include <llvm/ADT/SmallString.h>
2020
#include <llvm/ADT/SmallVector.h>

0 commit comments

Comments
 (0)