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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
//===-- ClangDocMain.cpp - ClangDoc -----------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This tool for generating C and C++ documentation from source code
// and comments. Generally, it runs a LibTooling FrontendAction on source files,
// mapping each declaration in those files to its USR and serializing relevant
// information into LLVM bitcode. It then runs a pass over the collected
// declaration information, reducing by USR. There is an option to dump this
// intermediate result to bitcode. Finally, it hands the reduced information
// off to a generator, which does the final parsing from the intermediate
// representation to the desired output format.
//
//===----------------------------------------------------------------------===//
#include "BitcodeReader.h"
#include "ClangDoc.h"
#include "Generators.h"
#include "Representation.h"
#include "support/Utils.h"
#include "clang/ASTMatchers/ASTMatchersInternal.h"
#include "clang/Tooling/AllTUsExecution.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Execution.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ThreadPool.h"
#include "llvm/Support/TimeProfiler.h"
#include "llvm/Support/raw_ostream.h"
#include <atomic>
#include <mutex>
#include <string>
using namespace clang::ast_matchers;
using namespace clang::tooling;
using namespace clang;
static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static llvm::cl::OptionCategory ClangDocCategory("clang-doc options");
static llvm::cl::opt<std::string>
ProjectName("project-name", llvm::cl::desc("Name of project."),
llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<bool> IgnoreMappingFailures(
"ignore-map-errors",
llvm::cl::desc("Continue if files are not mapped correctly."),
llvm::cl::init(true), llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<std::string>
OutDirectory("output",
llvm::cl::desc("Directory for outputting generated files."),
llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<std::string>
BaseDirectory("base",
llvm::cl::desc(R"(Base Directory for generated documentation.
URLs will be rooted at this directory for HTML links.)"),
llvm::cl::init(""), llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<bool>
PublicOnly("public", llvm::cl::desc("Document only public declarations."),
llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<bool> DoxygenOnly(
"doxygen",
llvm::cl::desc("Use only doxygen-style comments to generate docs."),
llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
static llvm::cl::list<std::string> UserStylesheets(
"stylesheets", llvm::cl::CommaSeparated,
llvm::cl::desc("CSS stylesheets to extend the default styles."),
llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<std::string> UserAssetPath(
"asset",
llvm::cl::desc("User supplied asset path to "
"override the default css and js files for html output"),
llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
Directory where processed files are stored.
Links to definition locations will only be
generated if the file is in this dir.)"),
llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<std::string>
RepositoryUrl("repository", llvm::cl::desc(R"(
URL of repository that hosts code.
Used for links to definition locations.)"),
llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<std::string> RepositoryCodeLinePrefix(
"repository-line-prefix",
llvm::cl::desc("Prefix of line code for repository."),
llvm::cl::cat(ClangDocCategory));
static llvm::cl::opt<bool> FTimeTrace("ftime-trace", llvm::cl::desc(R"(
Turn on time profiler. Generates clang-doc-tracing.json)"),
llvm::cl::init(false),
llvm::cl::cat(ClangDocCategory));
enum OutputFormatTy { md, yaml, html, mustache, json };
static llvm::cl::opt<OutputFormatTy> FormatEnum(
"format", llvm::cl::desc("Format for outputted docs."),
llvm::cl::values(clEnumValN(OutputFormatTy::yaml, "yaml",
"Documentation in YAML format."),
clEnumValN(OutputFormatTy::md, "md",
"Documentation in MD format."),
clEnumValN(OutputFormatTy::html, "html",
"Documentation in HTML format."),
clEnumValN(OutputFormatTy::mustache, "mustache",
"Documentation in mustache HTML format"),
clEnumValN(OutputFormatTy::json, "json",
"Documentation in JSON format")),
llvm::cl::init(OutputFormatTy::yaml), llvm::cl::cat(ClangDocCategory));
static llvm::ExitOnError ExitOnErr;
static std::string getFormatString() {
switch (FormatEnum) {
case OutputFormatTy::yaml:
return "yaml";
case OutputFormatTy::md:
return "md";
case OutputFormatTy::html:
return "html";
case OutputFormatTy::mustache:
return "mustache";
case OutputFormatTy::json:
return "json";
}
llvm_unreachable("Unknown OutputFormatTy");
}
// This function isn't referenced outside its translation unit, but it
// can't use the "static" keyword because its address is used for
// GetMainExecutable (since some platforms don't support taking the
// address of main, and some platforms can't implement GetMainExecutable
// without being given the address of a function in the main executable).
static std::string getExecutablePath(const char *Argv0, void *MainAddr) {
return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
}
static llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
using DirIt = llvm::sys::fs::directory_iterator;
std::error_code FileErr;
llvm::SmallString<128> FilePath(UserAssetPath);
for (DirIt DirStart = DirIt(UserAssetPath, FileErr), DirEnd;
!FileErr && DirStart != DirEnd; DirStart.increment(FileErr)) {
FilePath = DirStart->path();
if (llvm::sys::fs::is_regular_file(FilePath)) {
if (llvm::sys::path::extension(FilePath) == ".css")
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
std::string(FilePath));
else if (llvm::sys::path::extension(FilePath) == ".js")
CDCtx.JsScripts.emplace_back(FilePath.str());
}
}
if (FileErr)
return llvm::createFileError(FilePath, FileErr);
return llvm::Error::success();
}
static llvm::Error getDefaultAssetFiles(const char *Argv0,
clang::doc::ClangDocContext &CDCtx) {
void *MainAddr = (void *)(intptr_t)getExecutablePath;
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
llvm::SmallString<128> NativeClangDocPath;
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
llvm::SmallString<128> AssetsPath;
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
llvm::SmallString<128> DefaultStylesheet =
appendPathNative(AssetsPath, "clang-doc-default-stylesheet.css");
llvm::SmallString<128> IndexJS = appendPathNative(AssetsPath, "index.js");
if (!llvm::sys::fs::is_regular_file(IndexJS))
return llvm::createStringError(llvm::inconvertibleErrorCode(),
"default index.js file missing at " +
IndexJS + "\n");
if (!llvm::sys::fs::is_regular_file(DefaultStylesheet))
return llvm::createStringError(
llvm::inconvertibleErrorCode(),
"default clang-doc-default-stylesheet.css file missing at " +
DefaultStylesheet + "\n");
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
std::string(DefaultStylesheet));
CDCtx.JsScripts.emplace_back(IndexJS.str());
return llvm::Error::success();
}
static llvm::Error getHtmlAssetFiles(const char *Argv0,
clang::doc::ClangDocContext &CDCtx) {
if (!UserAssetPath.empty() &&
!llvm::sys::fs::is_directory(std::string(UserAssetPath)))
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
<< " falling back to default\n";
if (llvm::sys::fs::is_directory(std::string(UserAssetPath)))
return getAssetFiles(CDCtx);
return getDefaultAssetFiles(Argv0, CDCtx);
}
static llvm::Error getMustacheHtmlFiles(const char *Argv0,
clang::doc::ClangDocContext &CDCtx) {
bool IsDir = llvm::sys::fs::is_directory(UserAssetPath);
if (!UserAssetPath.empty() && !IsDir)
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
<< " falling back to default\n";
if (IsDir) {
getMustacheHtmlFiles(UserAssetPath, CDCtx);
return llvm::Error::success();
}
void *MainAddr = (void *)(intptr_t)getExecutablePath;
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
llvm::SmallString<128> NativeClangDocPath;
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
llvm::SmallString<128> AssetsPath;
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
getMustacheHtmlFiles(AssetsPath, CDCtx);
return llvm::Error::success();
}
/// Make the output of clang-doc deterministic by sorting the children of
/// namespaces and records.
static void
sortUsrToInfo(llvm::StringMap<std::unique_ptr<doc::Info>> &USRToInfo) {
for (auto &I : USRToInfo) {
auto &Info = I.second;
if (Info->IT == doc::InfoType::IT_namespace) {
auto *Namespace = static_cast<clang::doc::NamespaceInfo *>(Info.get());
Namespace->Children.sort();
}
if (Info->IT == doc::InfoType::IT_record) {
auto *Record = static_cast<clang::doc::RecordInfo *>(Info.get());
Record->Children.sort();
}
}
}
static llvm::Error handleMappingFailures(llvm::Error Err) {
if (!Err)
return llvm::Error::success();
if (IgnoreMappingFailures) {
llvm::errs() << "Error mapping decls in files. Clang-doc will ignore these "
"files and continue:\n"
<< toString(std::move(Err)) << "\n";
return llvm::Error::success();
}
return Err;
}
static llvm::Error createDirectories(llvm::StringRef OutDirectory) {
if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory))
return llvm::createFileError(OutDirectory, Err,
"failed to create directory.");
return llvm::Error::success();
}
int main(int argc, const char **argv) {
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
std::error_code OK;
ExitOnErr.setBanner("clang-doc error: ");
const char *Overview =
R"(Generates documentation from source code and comments.
Example usage for files without flags (default):
$ clang-doc File1.cpp File2.cpp ... FileN.cpp
Example usage for a project using a compile commands database:
$ clang-doc --executor=all-TUs compile_commands.json
)";
auto Executor = ExitOnErr(clang::tooling::createExecutorFromCommandLineArgs(
argc, argv, ClangDocCategory, Overview));
// turns on ftime trace profiling
if (FTimeTrace)
llvm::timeTraceProfilerInitialize(200, "clang-doc");
{
llvm::TimeTraceScope("main");
// Fail early if an invalid format was provided.
std::string Format = getFormatString();
llvm::outs() << "Emiting docs in " << Format << " format.\n";
auto G = ExitOnErr(doc::findGeneratorByName(Format));
ArgumentsAdjuster ArgAdjuster;
if (!DoxygenOnly)
ArgAdjuster = combineAdjusters(
getInsertArgumentAdjuster("-fparse-all-comments",
tooling::ArgumentInsertPosition::END),
ArgAdjuster);
clang::doc::ClangDocContext CDCtx = {
Executor->getExecutionContext(),
ProjectName,
PublicOnly,
OutDirectory,
SourceRoot,
RepositoryUrl,
RepositoryCodeLinePrefix,
BaseDirectory,
{UserStylesheets.begin(), UserStylesheets.end()},
FTimeTrace};
if (Format == "html") {
ExitOnErr(getHtmlAssetFiles(argv[0], CDCtx));
} else if (Format == "mustache") {
ExitOnErr(getMustacheHtmlFiles(argv[0], CDCtx));
}
llvm::timeTraceProfilerBegin("Executor Launch", "total runtime");
// Mapping phase
llvm::outs() << "Mapping decls...\n";
ExitOnErr(handleMappingFailures(
Executor->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster)));
llvm::timeTraceProfilerEnd();
// Collect values into output by key.
// In ToolResults, the Key is the hashed USR and the value is the
// bitcode-encoded representation of the Info object.
llvm::timeTraceProfilerBegin("Collect Info", "total runtime");
llvm::outs() << "Collecting infos...\n";
llvm::StringMap<std::vector<StringRef>> USRToBitcode;
Executor->getToolResults()->forEachResult(
[&](StringRef Key, StringRef Value) {
USRToBitcode[Key].emplace_back(Value);
});
llvm::timeTraceProfilerEnd();
// Collects all Infos according to their unique USR value. This map is added
// to from the thread pool below and is protected by the USRToInfoMutex.
llvm::sys::Mutex USRToInfoMutex;
llvm::StringMap<std::unique_ptr<doc::Info>> USRToInfo;
// First reducing phase (reduce all decls into one info per decl).
llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
std::atomic<bool> Error;
Error = false;
llvm::sys::Mutex IndexMutex;
// ExecutorConcurrency is a flag exposed by AllTUsExecution.h
llvm::DefaultThreadPool Pool(
llvm::hardware_concurrency(ExecutorConcurrency));
{
llvm::TimeTraceScope TS("Reduce");
for (auto &Group : USRToBitcode) {
Pool.async([&]() { // time trace decoding bitcode
if (FTimeTrace)
llvm::timeTraceProfilerInitialize(200, "clang-doc");
std::vector<std::unique_ptr<doc::Info>> Infos;
{
llvm::TimeTraceScope Red("decoding bitcode");
for (auto &Bitcode : Group.getValue()) {
llvm::BitstreamCursor Stream(Bitcode);
doc::ClangDocBitcodeReader Reader(Stream);
auto ReadInfos = Reader.readBitcode();
if (!ReadInfos) {
llvm::errs() << toString(ReadInfos.takeError()) << "\n";
Error = true;
return;
}
std::move(ReadInfos->begin(), ReadInfos->end(),
std::back_inserter(Infos));
}
} // time trace decoding bitcode
std::unique_ptr<doc::Info> Reduced;
{
llvm::TimeTraceScope Merge("merging bitcode");
auto ExpReduced = doc::mergeInfos(Infos);
if (!ExpReduced) {
llvm::errs() << llvm::toString(ExpReduced.takeError());
return;
}
Reduced = std::move(*ExpReduced);
} // time trace merging bitcode
// Add a reference to this Info in the Index
{
llvm::TimeTraceScope Merge("addInfoToIndex");
std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get());
}
// Save in the result map (needs a lock due to threaded access).
{
llvm::TimeTraceScope Merge("USRToInfo");
std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
USRToInfo[Group.getKey()] = std::move(Reduced);
}
if (CDCtx.FTimeTrace)
llvm::timeTraceProfilerFinishThread();
});
}
Pool.wait();
} // time trace reduce
if (Error)
return 1;
{
llvm::TimeTraceScope Sort("Sort USRToInfo");
sortUsrToInfo(USRToInfo);
}
llvm::timeTraceProfilerBegin("Writing output", "total runtime");
// Ensure the root output directory exists.
ExitOnErr(createDirectories(OutDirectory));
// Run the generator.
llvm::outs() << "Generating docs...\n";
ExitOnErr(G->generateDocs(OutDirectory, std::move(USRToInfo), CDCtx));
llvm::outs() << "Generating assets for docs...\n";
ExitOnErr(G->createResources(CDCtx));
llvm::timeTraceProfilerEnd();
} // time trace main
if (FTimeTrace) {
std::error_code EC;
llvm::raw_fd_ostream OS("clang-doc-tracing.json", EC,
llvm::sys::fs::OF_Text);
if (!EC) {
llvm::timeTraceProfilerWrite(OS);
llvm::timeTraceProfilerCleanup();
} else
return 1;
}
return 0;
}
|