Skip to content

Commit 5840069

Browse files
committed
tidy up Corpus API
1 parent cd0c586 commit 5840069

File tree

12 files changed

+372
-131
lines changed

12 files changed

+372
-131
lines changed

README.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
== Install
44

55
This library depends on a recent version of LLVM.
6-
Here are the instructions to install LVVM with the settings required by this project.
6+
Here are the instructions to install LLVM with the settings required by this project.
77

88
[source,bash]
99
----

include/mrdox/Config.hpp

-3
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ class Config
9999
bool IgnoreMappingFailures = false;
100100

101101
public:
102-
Config(Config&&) = delete;
103-
Config& operator=(Config&&) = delete;
104-
105102
//--------------------------------------------
106103
//
107104
// Observers

include/mrdox/Corpus.hpp

+77-58
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <mrdox/meta/Types.hpp>
2020
#include <clang/Tooling/Execution.h>
2121
#include <llvm/Support/Mutex.h>
22+
#include <memory>
2223
#include <type_traits>
2324
#include <vector>
2425

@@ -29,67 +30,28 @@ namespace mrdox {
2930
*/
3031
class Corpus
3132
{
32-
Config const& config_;
33-
3433
explicit
3534
Corpus(
36-
Config const& config) noexcept
37-
: config_(config)
35+
std::shared_ptr<Config const> config) noexcept
36+
: config_(std::move(config))
3837
{
3938
}
4039

41-
public:
42-
/** Index of all emitted symbols.
43-
*/
44-
Index Idx;
45-
46-
/** Table of Info keyed on Symbol ID.
47-
*/
48-
llvm::StringMap<std::unique_ptr<Info>> InfoMap;
49-
50-
/** List of all symbols.
51-
*/
52-
std::vector<SymbolID> allSymbols;
53-
5440
public:
5541
//--------------------------------------------
5642
//
57-
// Modifiers
43+
// Observers
5844
//
5945
//--------------------------------------------
6046

61-
/** Build the intermediate representation of the code being documented.
62-
63-
@param config The configuration, whose lifetime
64-
must extend until the corpus is destroyed.
65-
66-
@param R The diagnostic reporting object to
67-
use for delivering errors and information.
68-
*/
69-
[[nodiscard]]
70-
static
71-
llvm::Expected<std::unique_ptr<Corpus>>
72-
build(
73-
tooling::ToolExecutor& ex,
74-
Config const& config,
75-
Reporter& R);
76-
77-
/** Sort an array of Info by fully qualified name
78-
*/
79-
80-
/** Store the Info in the tool results, keyed by SymbolID.
47+
/** Return the ID of the global namespace.
8148
*/
8249
static
83-
void
84-
reportResult(
85-
tooling::ExecutionContext& exc,
86-
Info const& I);
87-
88-
//--------------------------------------------
89-
//
90-
// Observers
91-
//
92-
//--------------------------------------------
50+
SymbolID
51+
globalNamespaceID() noexcept
52+
{
53+
return EmptySID;
54+
}
9355

9456
/** Return true if s0 is less than s1.
9557
@@ -106,11 +68,13 @@ class Corpus
10668
llvm::StringRef symbolName0,
10769
llvm::StringRef symbolName1) noexcept;
10870

109-
/** Return the ID of the global namespace.
71+
/** Return the Config used to generate this corpus.
11072
*/
111-
static
112-
SymbolID
113-
globalNamespaceID() noexcept;
73+
std::shared_ptr<Config const> const&
74+
config() const noexcept
75+
{
76+
return config_;
77+
}
11478

11579
/** Return the metadata for the global namespace.
11680
*/
@@ -119,11 +83,7 @@ class Corpus
11983

12084
/** Return true if an Info with the specified symbol ID exists.
12185
*/
122-
bool
123-
exists(SymbolID const& id) const noexcept
124-
{
125-
return find<Info>(id) != nullptr;
126-
}
86+
bool exists(SymbolID const& id) const noexcept;
12787

12888
/** Return a pointer to the Info with the specified symbol ID, or nullptr.
12989
*/
@@ -166,7 +126,55 @@ class Corpus
166126
assert(p != nullptr);
167127
return *p;
168128
}
169-
129+
130+
/** Return the list of all uniquely identified symbols.
131+
*/
132+
std::vector<SymbolID> const&
133+
allSymbols() const noexcept
134+
{
135+
return allSymbols_;
136+
}
137+
138+
//--------------------------------------------
139+
//
140+
// Modifiers
141+
//
142+
//--------------------------------------------
143+
144+
/** Build the intermediate representation of the code being documented.
145+
146+
@param config A shared pointer to the configuration.
147+
148+
@param R The diagnostic reporting object to
149+
use for delivering errors and information.
150+
*/
151+
[[nodiscard]]
152+
static
153+
llvm::Expected<std::unique_ptr<Corpus>>
154+
build(
155+
tooling::ToolExecutor& ex,
156+
std::shared_ptr<Config const> config,
157+
Reporter& R);
158+
159+
[[nodiscard]]
160+
static
161+
llvm::Expected<std::unique_ptr<Corpus>>
162+
build(
163+
tooling::ToolExecutor& ex,
164+
Config const& config,
165+
Reporter& R)
166+
{
167+
return build(ex, std::make_shared<Config const>(config), R);
168+
}
169+
170+
/** Store the Info in the tool results, keyed by SymbolID.
171+
*/
172+
static
173+
void
174+
reportResult(
175+
tooling::ExecutionContext& exc,
176+
Info const& I);
177+
170178
private:
171179
struct Temps;
172180

@@ -211,6 +219,17 @@ class Corpus
211219
bool canonicalize(llvm::SmallVectorImpl<MemberTypeInfo>& list, Temps& t, Reporter& R);
212220

213221
private:
222+
std::shared_ptr<Config const> config_;
223+
224+
// Index of all emitted symbols.
225+
Index Idx;
226+
227+
// Table of Info keyed on Symbol ID.
228+
llvm::StringMap<std::unique_ptr<Info>> InfoMap;
229+
230+
// list of all symbols
231+
std::vector<SymbolID> allSymbols_;
232+
214233
llvm::sys::Mutex infoMutex;
215234
llvm::sys::Mutex allSymbolsMutex;
216235
bool isCanonical_ = false;

include/mrdox/format/FlatWriter.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ struct Scope;
3838
class FlatWriter
3939
{
4040
protected:
41+
// The stream being written to
4142
llvm::raw_ostream& os_;
43+
44+
// Path to file being written, or empty
45+
llvm::StringRef filePath_;
46+
4247
Corpus const& corpus_;
4348
Config const& config_;
4449
Reporter& R_;
@@ -47,6 +52,7 @@ class FlatWriter
4752
*/
4853
FlatWriter(
4954
llvm::raw_ostream& os,
55+
llvm::StringRef filePath,
5056
Corpus const& corpus,
5157
Config const& config,
5258
Reporter& R) noexcept;

include/mrdox/meta/Javadoc.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,9 @@ struct Javadoc
359359
*/
360360
void merge(Javadoc&& other);
361361

362+
List<Block>::const_iterator
363+
findBrief() const noexcept;
364+
362365
/** Calculate the brief.
363366
364367
The implementation calls this function once,
@@ -425,7 +428,7 @@ struct Javadoc
425428
{
426429
append(blocks_, std::move(node));
427430
}
428-
431+
429432
void append(Param node)
430433
{
431434
append(params_, std::move(node));

source/lib/Corpus.cpp

+34-26
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ mergeInfos(std::vector<std::unique_ptr<Info>>& Values)
6262
}
6363
}
6464

65+
//------------------------------------------------
66+
//
67+
// Observers
68+
//
69+
//------------------------------------------------
70+
71+
/** Return the metadata for the global namespace.
72+
*/
73+
NamespaceInfo const&
74+
Corpus::
75+
globalNamespace() const noexcept
76+
{
77+
return get<NamespaceInfo>(globalNamespaceID());
78+
}
79+
80+
bool
81+
Corpus::
82+
exists(SymbolID const& id) const noexcept
83+
{
84+
return find<Info>(id) != nullptr;
85+
}
86+
6587
//------------------------------------------------
6688
//
6789
// Modifiers
@@ -72,30 +94,30 @@ llvm::Expected<std::unique_ptr<Corpus>>
7294
Corpus::
7395
build(
7496
tooling::ToolExecutor& ex,
75-
Config const& config,
97+
std::shared_ptr<Config const> config,
7698
Reporter& R)
7799
{
78-
std::unique_ptr<Corpus> corpus(new Corpus(config));
100+
std::unique_ptr<Corpus> corpus(new Corpus(std::move(config)));
79101

80102
// Traverse the AST for all translation units
81103
// and emit serializd bitcode into tool results.
82104
// This operation happens ona thread pool.
83-
if(config.verbose())
105+
if(corpus->config()->verbose())
84106
R.print("Mapping declarations");
85107
if(auto err = ex.execute(
86108
makeFrontendActionFactory(
87-
*ex.getExecutionContext(), config, R),
88-
config.ArgAdjuster))
109+
*ex.getExecutionContext(), *corpus->config(), R),
110+
corpus->config()->ArgAdjuster))
89111
{
90-
if(! config.IgnoreMappingFailures)
112+
if(! corpus->config()->IgnoreMappingFailures)
91113
return err;
92114
R.print("warning: mapping failed because ", toString(std::move(err)));
93115
}
94116

95117
// Collect the symbols. Each symbol will have
96118
// a vector of one or more bitcodes. These will
97119
// be merged later.
98-
if(config.verbose())
120+
if(corpus->config()->verbose())
99121
R.print("Collecting symbols");
100122
using USRToBitcodeType = llvm::StringMap<std::vector<StringRef>>;
101123
USRToBitcodeType USRToBitcode;
@@ -107,7 +129,7 @@ build(
107129
});
108130

109131
// First reducing phase (reduce all decls into one info per decl).
110-
if(config.verbose())
132+
if(corpus->config()->verbose())
111133
R.print("Reducing ", USRToBitcode.size(), " declarations");
112134
std::atomic<bool> GotFailure;
113135
GotFailure = false;
@@ -159,7 +181,7 @@ build(
159181

160182
Pool.wait();
161183

162-
if(config.verbose())
184+
if(corpus->config()->verbose())
163185
R.print("Collected ", corpus->InfoMap.size(), " symbols.\n");
164186

165187
if(GotFailure)
@@ -249,20 +271,6 @@ symbolCompare(
249271
return s_cmp < 0;
250272
}
251273

252-
SymbolID
253-
Corpus::
254-
globalNamespaceID() noexcept
255-
{
256-
return EmptySID;
257-
}
258-
259-
NamespaceInfo const&
260-
Corpus::
261-
globalNamespace() const noexcept
262-
{
263-
return get<NamespaceInfo>(globalNamespaceID());
264-
}
265-
266274
//------------------------------------------------
267275
//
268276
// Implementation
@@ -365,7 +373,7 @@ insertIntoIndex(
365373
}
366374

367375
// also insert into allSymbols
368-
allSymbols.emplace_back(I.USR);
376+
allSymbols_.emplace_back(I.USR);
369377
}
370378

371379
//------------------------------------------------
@@ -383,15 +391,15 @@ canonicalize(Reporter& R)
383391
return false;
384392
}
385393

386-
if(config_.verbose())
394+
if(config_->verbose())
387395
R.print("Canonicalizing...");
388396

389397
Temps t;
390398

391399
if(! canonicalize(*p, t, R))
392400
return false;
393401

394-
if(! canonicalize(allSymbols, t, R))
402+
if(! canonicalize(allSymbols_, t, R))
395403
return false;
396404

397405
isCanonical_ = true;

0 commit comments

Comments
 (0)