Skip to content

Commit 91d6742

Browse files
committed
docs: expand build utilities javadoc
1 parent 113ecbc commit 91d6742

12 files changed

+459
-101
lines changed

src/lib/AST/Bitcode.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,20 @@ namespace clang {
2424
namespace mrdocs {
2525

2626
/** Return the serialized bitcode for a metadata node.
27+
28+
This function writes an Info variant to the buffer
29+
as bitcode.
2730
*/
2831
llvm::SmallString<0>
2932
writeBitcode(Info const& I);
3033

3134
/** Return an array of Info read from a bitstream.
35+
36+
This function reads a bitstream and returns an array
37+
of Info objects. The bitstream must have been written
38+
by `writeBitcode`.
39+
40+
@note Each bitcode might contain multiple Info objects.
3241
*/
3342
mrdocs::Expected<std::vector<std::unique_ptr<Info>>>
3443
readBitcode(llvm::StringRef bitcode);

src/lib/AST/BitcodeWriter.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -1247,10 +1247,8 @@ emitBlock(
12471247

12481248
//------------------------------------------------
12491249

1250-
/** Write an Info variant to the buffer as bitcode.
1251-
*/
12521250
llvm::SmallString<0>
1253-
writeBitcode(const Info& info)
1251+
writeBitcode(Info const& info)
12541252
{
12551253
llvm::SmallString<0> buffer;
12561254
llvm::BitstreamWriter stream(buffer);

src/lib/AST/BitcodeWriter.hpp

+30-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@
3030
namespace clang {
3131
namespace mrdocs {
3232

33+
/** A writer for serializing the mrdocs' `Info` representation to LLVM bitcode.
34+
35+
This class takes in a llvm::BitstreamWriter stream and
36+
emits the generated bitcode to that stream.
37+
38+
The function `dispatchInfoForWrite` is used to dispatch
39+
the appropriate write function for the given `Info` object.
40+
41+
After calling `dispatchInfoForWrite`, the buffer used to
42+
create the initial llvm::BitstreamWriter will be populated with
43+
the serialized bitcode for the given `Info` object.
44+
45+
*/
3346
class BitcodeWriter
3447
{
3548
public:
@@ -44,7 +57,23 @@ class BitcodeWriter
4457
explicit
4558
BitcodeWriter(llvm::BitstreamWriter &Stream);
4659

47-
// Write a specific info to a bitcode stream.
60+
/** Write a specific Info to a bitcode stream.
61+
62+
This function takes in an `Info` object and
63+
emits the generated bitcode to the internal
64+
llvm::BitstreamWriter stream.
65+
66+
The function will dispatch the appropriate
67+
write function for the given `Info` object.
68+
69+
After calling `dispatchInfoForWrite`, the
70+
buffer used to create the initial
71+
llvm::BitstreamWriter will be populated with
72+
the serialized bitcode for the given
73+
`Info` object.
74+
75+
@param I The info to write.
76+
*/
4877
bool dispatchInfoForWrite(const Info& I);
4978

5079
void emitHeader();

src/lib/Lib/CorpusImpl.cpp

+62-34
Original file line numberDiff line numberDiff line change
@@ -78,36 +78,50 @@ find(
7878

7979
//------------------------------------------------
8080

81+
namespace {
82+
template <class Rep, class Period>
83+
std::string
84+
format_duration(
85+
std::chrono::duration<Rep, Period> delta)
86+
{
87+
auto delta_ms = std::chrono::duration_cast<
88+
std::chrono::milliseconds>(delta).count();
89+
if (delta_ms < 1000)
90+
{
91+
return fmt::format("{} ms", delta_ms);
92+
}
93+
else
94+
{
95+
double const delta_s = static_cast<double>(delta_ms) / 1000.0;
96+
return fmt::format("{:.02f} s", delta_s);
97+
}
98+
}
99+
}
100+
81101
mrdocs::Expected<std::unique_ptr<Corpus>>
82102
CorpusImpl::
83103
build(
84104
report::Level reportLevel,
85-
std::shared_ptr<ConfigImpl const> config,
105+
std::shared_ptr<ConfigImpl const> const& config,
86106
tooling::CompilationDatabase const& compilations)
87107
{
88108
using clock_type = std::chrono::steady_clock;
89-
const auto format_duration =
90-
[](clock_type::duration delta)
91-
{
92-
auto delta_ms = std::chrono::duration_cast<
93-
std::chrono::milliseconds>(delta).count();
94-
if(delta_ms < 1000)
95-
return fmt::format("{} ms", delta_ms);
96-
else
97-
return fmt::format("{:.02f} s",
98-
delta_ms / 1000.0);
99-
};
100109
auto start_time = clock_type::now();
101110

102-
auto corpus = std::make_unique<CorpusImpl>(config);
103-
104-
// Traverse the AST for all translation units
105-
// and emit serializd bitcode into tool results.
106-
// This operation happens ona thread pool.
107-
report::print(reportLevel, "Extracting declarations");
111+
// ------------------------------------------
112+
// Create empty corpus
113+
// ------------------------------------------
114+
// The corpus will keep a reference to Config.
115+
std::unique_ptr<CorpusImpl> corpus = std::make_unique<CorpusImpl>(config);
108116

117+
// ------------------------------------------
118+
// Execution context
119+
// ------------------------------------------
120+
// Create an execution context to store the
121+
// results of the AST traversal.
122+
// Any new Info objects will be added to the
123+
// InfoSet in the execution context.
109124
#define USE_BITCODE
110-
111125
#ifdef USE_BITCODE
112126
BitcodeExecutionContext context(*config);
113127
#else
@@ -117,13 +131,9 @@ build(
117131
makeFrontendActionFactory(context, *config);
118132
MRDOCS_ASSERT(action);
119133

120-
// Get a copy of the filename strings
121-
std::vector<std::string> files =
122-
compilations.getAllFiles();
123-
124-
if(files.empty())
125-
return Unexpected(formatError("Compilations database is empty"));
126-
134+
// ------------------------------------------
135+
// "Process file" task
136+
// ------------------------------------------
127137
auto const processFile =
128138
[&](std::string path)
129139
{
@@ -144,15 +154,27 @@ build(
144154
formatError("Failed to run action on {}", path).Throw();
145155
};
146156

147-
// Run the action on all files in the database
157+
// ------------------------------------------
158+
// Run the process file task on all files
159+
// ------------------------------------------
160+
// Traverse the AST for all translation units
161+
// and emit serializd bitcode into tool results.
162+
// This operation happens on a thread pool.
163+
report::print(reportLevel, "Extracting declarations");
164+
165+
// Get a copy of the filename strings
166+
std::vector<std::string> files = compilations.getAllFiles();
167+
MRDOCS_CHECK(files, "Compilations database is empty");
148168
std::vector<Error> errors;
149-
if(files.size() == 1)
169+
170+
// Run the action on all files in the database
171+
if (files.size() == 1)
150172
{
151173
try
152174
{
153175
processFile(std::move(files.front()));
154176
}
155-
catch(Exception const& ex)
177+
catch (Exception const& ex)
156178
{
157179
errors.push_back(ex.error());
158180
}
@@ -169,16 +191,18 @@ build(
169191
report::format(reportLevel,
170192
"[{}/{}] \"{}\"", idx, files.size(), path);
171193

172-
processFile(std::move(path));
194+
processFile(path);
173195
});
174196
}
175197
errors = taskGroup.wait();
176198
}
177-
178-
// Report warning and error totals
199+
// Print diagnostics totals
179200
context.reportEnd(reportLevel);
180201

181-
if(! errors.empty())
202+
// ------------------------------------------
203+
// Report warning and error totals
204+
// ------------------------------------------
205+
if (!errors.empty())
182206
{
183207
Error err(errors);
184208
if(! (*config)->ignoreFailures)
@@ -207,6 +231,7 @@ build(
207231
"Reduced {} symbols in {}",
208232
corpus->info_.size(),
209233
format_duration(clock_type::now() - start_time));
234+
#undef USE_BITCODE
210235
#else
211236
auto results = context.results();
212237
if(! results)
@@ -219,9 +244,12 @@ build(
219244
format_duration(clock_type::now() - start_time));
220245
#endif
221246

247+
// ------------------------------------------
248+
// Finalize corpus
249+
// ------------------------------------------
222250
auto lookup = std::make_unique<SymbolLookup>(*corpus);
223-
224251
finalize(corpus->info_, *lookup);
252+
225253
return corpus;
226254
}
227255

src/lib/Lib/CorpusImpl.hpp

+33-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ namespace clang {
2626
namespace mrdocs {
2727

2828
/** Implements the Corpus.
29+
30+
The CorpusImpl class is the implementation of the Corpus interface.
31+
It is responsible for building the index of all symbols in the
32+
translation units, and providing access to the symbols via the
33+
iterator interface.
34+
35+
The CorpusImpl class is not intended to be used directly. Instead,
36+
the Corpus interface can used by plugins to access the symbols.
37+
2938
*/
3039
class CorpusImpl : public Corpus
3140
{
@@ -40,15 +49,36 @@ class CorpusImpl : public Corpus
4049
{
4150
}
4251

43-
iterator begin() const noexcept override;
44-
iterator end() const noexcept override;
52+
/** Iterator to the first Info.
53+
*/
54+
iterator
55+
begin() const noexcept override;
56+
57+
/** Iterator to one past the last Info.
58+
*/
59+
iterator
60+
end() const noexcept override;
61+
62+
/** Return the Info with the specified symbol ID.
4563
64+
If the id does not exist, the behavior is undefined.
65+
*/
4666
Info*
4767
find(
4868
SymbolID const& id) noexcept;
4969

5070
/** Build metadata for a set of translation units.
5171
72+
This is the main point of interaction between MrDocs
73+
and the Clang Tooling infrastructure. The compilation
74+
database is used to build the index of all symbols
75+
in the translation units.
76+
77+
Users of the MrDocs library via plugins will
78+
only have access to the Corpus interface whose
79+
instance will be already populated. They will
80+
not need to call this function directly.
81+
5282
@param reportLevel Error reporting level.
5383
@param config A shared pointer to the configuration.
5484
@param compilations A compilations database for the input files.
@@ -59,7 +89,7 @@ class CorpusImpl : public Corpus
5989
mrdocs::Expected<std::unique_ptr<Corpus>>
6090
build(
6191
report::Level reportLevel,
62-
std::shared_ptr<ConfigImpl const> config,
92+
std::shared_ptr<ConfigImpl const> const& config,
6393
tooling::CompilationDatabase const& compilations);
6494

6595
private:

0 commit comments

Comments
 (0)