clang-tools 23.0.0git
ClangDocMain.cpp
Go to the documentation of this file.
1//===-- ClangDocMain.cpp - ClangDoc -----------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This tool for generating C and C++ documentation from source code
10// and comments. Generally, it runs a LibTooling FrontendAction on source files,
11// mapping each declaration in those files to its USR and serializing relevant
12// information into LLVM bitcode. It then runs a pass over the collected
13// declaration information, reducing by USR. There is an option to dump this
14// intermediate result to bitcode. Finally, it hands the reduced information
15// off to a generator, which does the final parsing from the intermediate
16// representation to the desired output format.
17//
18//===----------------------------------------------------------------------===//
19
20#include "BitcodeReader.h"
21#include "ClangDoc.h"
22#include "Generators.h"
23#include "Representation.h"
24#include "support/Utils.h"
25#include "clang/Basic/Diagnostic.h"
26#include "clang/Basic/DiagnosticOptions.h"
27#include "clang/Frontend/TextDiagnosticPrinter.h"
28#include "clang/Tooling/AllTUsExecution.h"
29#include "clang/Tooling/CommonOptionsParser.h"
30#include "clang/Tooling/Execution.h"
31#include "llvm/ADT/APFloat.h"
32#include "llvm/ADT/ScopeExit.h"
33#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Error.h"
35#include "llvm/Support/FileSystem.h"
36#include "llvm/Support/Mutex.h"
37#include "llvm/Support/Path.h"
38#include "llvm/Support/Process.h"
39#include "llvm/Support/Signals.h"
40#include "llvm/Support/ThreadPool.h"
41#include "llvm/Support/TimeProfiler.h"
42#include "llvm/Support/raw_ostream.h"
43#include <atomic>
44#include <mutex>
45#include <string>
46
47using namespace clang::tooling;
48using namespace clang;
50
51static llvm::cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
52static llvm::cl::OptionCategory ClangDocCategory("clang-doc options");
53
54static llvm::cl::opt<std::string>
55 ProjectName("project-name", llvm::cl::desc("Name of project."),
56 llvm::cl::cat(ClangDocCategory));
57
58static llvm::cl::opt<bool> IgnoreMappingFailures(
59 "ignore-map-errors",
60 llvm::cl::desc("Continue if files are not mapped correctly."),
61 llvm::cl::init(true), llvm::cl::cat(ClangDocCategory));
62
63static llvm::cl::opt<std::string>
64 OutDirectory("output",
65 llvm::cl::desc("Directory for outputting generated files."),
66 llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory));
67
68static llvm::cl::opt<std::string>
70 llvm::cl::desc(R"(Base Directory for generated documentation.
71URLs will be rooted at this directory for HTML links.)"),
72 llvm::cl::init(""), llvm::cl::cat(ClangDocCategory));
73
74static llvm::cl::opt<bool>
75 PublicOnly("public", llvm::cl::desc("Document only public declarations."),
76 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
78static llvm::cl::opt<bool> DoxygenOnly(
79 "doxygen",
80 llvm::cl::desc("Use only doxygen-style comments to generate docs."),
81 llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
83static llvm::cl::list<std::string> UserStylesheets(
84 "stylesheets", llvm::cl::CommaSeparated,
85 llvm::cl::desc("CSS stylesheets to extend the default styles."),
86 llvm::cl::cat(ClangDocCategory));
88static llvm::cl::opt<std::string> UserAssetPath(
89 "asset",
90 llvm::cl::desc("User supplied asset path to "
91 "override the default css and js files for html output"),
92 llvm::cl::cat(ClangDocCategory));
94static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
95Directory where processed files are stored.
96Links to definition locations will only be
97generated if the file is in this dir.)"),
98 llvm::cl::cat(ClangDocCategory));
99
100static llvm::cl::opt<std::string>
101 RepositoryUrl("repository", llvm::cl::desc(R"(
102URL of repository that hosts code.
103Used for links to definition locations.)"),
104 llvm::cl::cat(ClangDocCategory));
106static llvm::cl::opt<std::string> RepositoryCodeLinePrefix(
107 "repository-line-prefix",
108 llvm::cl::desc("Prefix of line code for repository."),
109 llvm::cl::cat(ClangDocCategory));
111static llvm::cl::opt<bool> FTimeTrace("ftime-trace", llvm::cl::desc(R"(
112Turn on time profiler. Generates clang-doc-tracing.json)"),
113 llvm::cl::init(false),
114 llvm::cl::cat(ClangDocCategory));
115
116static llvm::cl::opt<bool>
117 Pretty("pretty-json", llvm::cl::desc("Serialize JSON with whitespace."),
118 llvm::cl::cat(ClangDocCategory));
119
120static llvm::cl::opt<OutputFormatTy> FormatEnum(
121 "format", llvm::cl::desc("Format for outputted docs."),
122 llvm::cl::values(clEnumValN(OutputFormatTy::yaml, "yaml",
123 "Documentation in YAML format."),
124 clEnumValN(OutputFormatTy::md, "md",
125 "Documentation in MD format."),
126 clEnumValN(OutputFormatTy::html, "html",
127 "Documentation in HTML format."),
128 clEnumValN(OutputFormatTy::json, "json",
129 "Documentation in JSON format"),
130 clEnumValN(OutputFormatTy::md_mustache, "md_mustache",
131 "Documentation in MD format.")),
132 llvm::cl::init(OutputFormatTy::yaml), llvm::cl::cat(ClangDocCategory));
133
134static llvm::ExitOnError ExitOnErr;
135
136static llvm::StringRef getFormatString() {
137 switch (FormatEnum) {
138 case OutputFormatTy::yaml:
139 return "yaml";
140 case OutputFormatTy::md:
141 return "md";
142 case OutputFormatTy::html:
143 return "html";
144 case OutputFormatTy::json:
145 return "json";
146 case OutputFormatTy::md_mustache:
147 return "md_mustache";
148 }
149 llvm_unreachable("Unknown OutputFormatTy");
151
152// This function isn't referenced outside its translation unit, but it
153// can't use the "static" keyword because its address is used for
154// GetMainExecutable (since some platforms don't support taking the
155// address of main, and some platforms can't implement GetMainExecutable
156// without being given the address of a function in the main executable).
157static std::string getExecutablePath(const char *Argv0, void *MainAddr) {
158 return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
159}
160
161// TODO: Rename this, since it only gets custom CSS/JS
162static llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
163 using DirIt = llvm::sys::fs::directory_iterator;
164 std::error_code FileErr;
165 llvm::SmallString<128> FilePath(UserAssetPath);
166 for (DirIt DirStart = DirIt(UserAssetPath, FileErr), DirEnd;
167 !FileErr && DirStart != DirEnd; DirStart.increment(FileErr)) {
168 FilePath = DirStart->path();
169 if (llvm::sys::fs::is_regular_file(FilePath)) {
170 if (llvm::sys::path::extension(FilePath) == ".css")
171 CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
172 std::string(FilePath));
173 else if (llvm::sys::path::extension(FilePath) == ".js")
174 CDCtx.JsScripts.emplace_back(FilePath.str());
176 }
177 if (FileErr)
178 return llvm::createFileError(FilePath, FileErr);
179 return llvm::Error::success();
180}
181
182static llvm::Error getHtmlFiles(const char *Argv0,
184 bool IsDir = llvm::sys::fs::is_directory(UserAssetPath);
185 if (!UserAssetPath.empty() && !IsDir)
186 llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
187 << " falling back to default\n";
188 if (IsDir) {
189 if (FormatEnum == OutputFormatTy::html) {
190 if (auto Err = getAssetFiles(CDCtx))
191 return Err;
192 }
193 }
194 void *MainAddr = (void *)(intptr_t)getExecutablePath;
195 std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
196 llvm::SmallString<128> NativeClangDocPath;
197 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
198
199 llvm::SmallString<128> AssetsPath;
200 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
201 llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
202
203 getHtmlFiles(AssetsPath, CDCtx);
204
205 return llvm::Error::success();
206}
207
208static llvm::Error getMdFiles(const char *Argv0,
210 bool IsDir = llvm::sys::fs::is_directory(UserAssetPath);
211 if (!UserAssetPath.empty() && !IsDir)
212 llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
213 << " falling back to default\n";
214
215 void *MainAddr = (void *)(intptr_t)getExecutablePath;
216 std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
217 llvm::SmallString<128> NativeClangDocPath;
218 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
219
220 llvm::SmallString<128> AssetsPath;
221 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
222 llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc", "md");
223
224 getMdFiles(AssetsPath, CDCtx);
225
226 return llvm::Error::success();
227}
228
229/// Make the output of clang-doc deterministic by sorting the children of
230/// namespaces and records.
231static void sortUsrToInfo(llvm::StringMap<doc::Info *> &USRToInfo) {
232 for (auto &I : USRToInfo) {
233 auto &Info = I.second;
235 auto *Namespace = static_cast<clang::doc::NamespaceInfo *>(Info);
236 Namespace->Children.sort();
237 }
238 if (Info->IT == doc::InfoType::IT_record) {
239 auto *Record = static_cast<clang::doc::RecordInfo *>(Info);
240 Record->Children.sort();
241 }
242 }
243}
244
245static llvm::Error handleMappingFailures(DiagnosticsEngine &Diags,
246 llvm::Error Err) {
247 if (!Err)
248 return llvm::Error::success();
250 unsigned ID = Diags.getCustomDiagID(
251 DiagnosticsEngine::Warning,
252 "Error mapping decls in files. Clang-doc will ignore these files and "
253 "continue:\n%0");
254 Diags.Report(ID) << toString(std::move(Err));
255 return llvm::Error::success();
256 }
257 return Err;
258}
259
260static llvm::Error createDirectories(llvm::StringRef OutDirectory) {
261 if (std::error_code Err = llvm::sys::fs::create_directories(OutDirectory))
262 return llvm::createFileError(OutDirectory, Err,
263 "failed to create directory.");
264 return llvm::Error::success();
265}
266
267int main(int argc, const char **argv) {
268 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
269 std::error_code OK;
270
271 ExitOnErr.setBanner("clang-doc error: ");
272
273 const char *Overview =
274 R"(Generates documentation from source code and comments.
275
276Example usage for files without flags (default):
277
278 $ clang-doc File1.cpp File2.cpp ... FileN.cpp
279
280Example usage for a project using a compile commands database:
281
282 $ clang-doc --executor=all-TUs compile_commands.json
283)";
284
285 auto Executor = ExitOnErr(clang::tooling::createExecutorFromCommandLineArgs(
286 argc, argv, ClangDocCategory, Overview));
287
288 // turns on ftime trace profiling
289 if (FTimeTrace)
290 llvm::timeTraceProfilerInitialize(200, "clang-doc");
291 {
292 llvm::TimeTraceScope("main");
293
294 // Fail early if an invalid format was provided.
295 llvm::StringRef Format = getFormatString();
296 llvm::outs() << "Emiting docs in " << Format << " format.\n";
297 auto G = ExitOnErr(doc::findGeneratorByName(Format));
298
299 ArgumentsAdjuster ArgAdjuster;
300 if (!DoxygenOnly)
301 ArgAdjuster = combineAdjusters(
302 getInsertArgumentAdjuster("-fparse-all-comments",
303 tooling::ArgumentInsertPosition::END),
304 ArgAdjuster);
305
306 auto DiagOpts = std::make_unique<DiagnosticOptions>();
307 TextDiagnosticPrinter *DiagClient =
308 new TextDiagnosticPrinter(llvm::errs(), *DiagOpts);
309 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
310 DiagnosticsEngine Diags(DiagID, *DiagOpts, DiagClient);
311
313 Executor->getExecutionContext(), ProjectName, PublicOnly, OutDirectory,
315 {UserStylesheets.begin(), UserStylesheets.end()}, Diags, FormatEnum,
317
318 if (Format == "html")
319 ExitOnErr(getHtmlFiles(argv[0], CDCtx));
320 else if (Format == "md_mustache")
321 ExitOnErr(getMdFiles(argv[0], CDCtx));
322
323 llvm::timeTraceProfilerBegin("Executor Launch", "total runtime");
324 // Mapping phase
325 llvm::outs() << "Mapping decls...\n";
327 Diags,
328 Executor->execute(doc::newMapperActionFactory(CDCtx), ArgAdjuster)));
329 llvm::timeTraceProfilerEnd();
330
331 // Collect values into output by key.
332 // In ToolResults, the Key is the hashed USR and the value is the
333 // bitcode-encoded representation of the Info object.
334 llvm::timeTraceProfilerBegin("Collect Info", "total runtime");
335 llvm::outs() << "Collecting infos...\n";
336 llvm::StringMap<std::vector<StringRef>> USRToBitcode;
337 Executor->getToolResults()->forEachResult(
338 [&](StringRef Key, StringRef Value) {
339 USRToBitcode[Key].emplace_back(Value);
340 });
341 llvm::timeTraceProfilerEnd();
342
343 // Collects all Infos according to their unique USR value. This map is added
344 // to from the thread pool below and is protected by the USRToInfoMutex.
345 llvm::sys::Mutex USRToInfoMutex;
346 llvm::StringMap<doc::Info *> USRToInfo;
347
348 // First reducing phase (reduce all decls into one info per decl).
349 llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
350 std::atomic<bool> Error;
351 Error = false;
352 llvm::sys::Mutex IndexMutex;
353 llvm::sys::Mutex DiagMutex;
354 unsigned DiagIDBitcodeReading = Diags.getCustomDiagID(
355 DiagnosticsEngine::Error, "error reading bitcode: %0");
356 unsigned DiagIDBitcodeMerging = Diags.getCustomDiagID(
357 DiagnosticsEngine::Error, "error merging bitcode: %0");
358 // Note: we use per-thread arenas, so Pool must outlive the last use of this
359 // memory in the generators.
360 llvm::DefaultThreadPool Pool(
361 // ExecutorConcurrency is a flag exposed by AllTUsExecution.h
362 llvm::hardware_concurrency(ExecutorConcurrency));
363 {
364 llvm::TimeTraceScope TS("Reduce");
365 for (const auto &Group : USRToBitcode) {
366 StringRef Key = Group.getKey();
367 std::vector<StringRef> Bitcodes = Group.getValue();
368 Pool.async([Key, Bitcodes, &CDCtx, &Diags, &USRToInfo, &USRToInfoMutex,
369 &IndexMutex, &DiagMutex, &Error, DiagIDBitcodeReading,
370 DiagIDBitcodeMerging]() {
371 if (CDCtx.FTimeTrace)
372 llvm::timeTraceProfilerInitialize(200, "clang-doc");
373
374 doc::Info *Reduced = nullptr;
375 {
376 llvm::TimeTraceScope Red("decoding and merging bitcode");
377 for (const auto &Bitcode : Bitcodes) {
378
379 llvm::scope_exit ArenaGuard(
380 [] { clang::doc::TransientArena.Reset(); });
381 llvm::BitstreamCursor Stream(Bitcode);
382 doc::ClangDocBitcodeReader Reader(Stream, Diags);
383 auto ReadInfos = Reader.readBitcode();
384 if (!ReadInfos) {
385 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
386
387 Diags.Report(DiagIDBitcodeReading)
388 << toString(ReadInfos.takeError());
389 Error = true;
390 return;
391 }
392 for (auto &I : *ReadInfos) {
393 if (auto Err = doc::mergeSingleInfo(
394 Reduced, std::move(I), clang::doc::PersistentArena)) {
395 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
396 Diags.Report(DiagIDBitcodeMerging)
397 << toString(std::move(Err));
398 return;
399 }
400 }
401 }
402 } // time trace decoding and merging bitcode
403
404 // Add a reference to this Info in the Index
405 {
406 llvm::TimeTraceScope Merge("addInfoToIndex");
407 std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
409 }
410 // Save in the result map (needs a lock due to threaded access).
411 {
412 llvm::TimeTraceScope Merge("USRToInfo");
413 std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
414 USRToInfo[Key] = std::move(Reduced);
415 }
416
417 if (CDCtx.FTimeTrace)
418 llvm::timeTraceProfilerFinishThread();
419 });
420 }
421
422 Pool.wait();
423 } // time trace reduce
424
425 if (Error)
426 return 1;
427
428 {
429 llvm::TimeTraceScope Sort("Sort USRToInfo");
430 sortUsrToInfo(USRToInfo);
431 }
432
433 llvm::timeTraceProfilerBegin("Writing output", "total runtime");
434 // Ensure the root output directory exists.
436
437 // Run the generator.
438 llvm::outs() << "Generating docs...\n";
439
440 ExitOnErr(
441 G->generateDocumentation(OutDirectory, std::move(USRToInfo), CDCtx));
442 llvm::outs() << "Generating assets for docs...\n";
443 ExitOnErr(G->createResources(CDCtx));
444 llvm::timeTraceProfilerEnd();
445 } // time trace main
446
447 if (FTimeTrace) {
448 std::error_code EC;
449 llvm::raw_fd_ostream OS("clang-doc-tracing.json", EC,
450 llvm::sys::fs::OF_Text);
451 if (!EC) {
452 llvm::timeTraceProfilerWrite(OS);
453 llvm::timeTraceProfilerCleanup();
454 } else
455 return 1;
456 }
457 return 0;
458}
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::extrahelp CommonHelp(CommonOptionsParser::HelpMessage)
static std::string getExecutablePath(const char *Argv0, void *MainAddr)
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< std::string > RepositoryCodeLinePrefix("repository-line-prefix", llvm::cl::desc("Prefix of line code for repository."), llvm::cl::cat(ClangDocCategory))
int main(int argc, const char **argv)
static llvm::cl::opt< std::string > ProjectName("project-name", llvm::cl::desc("Name of project."), llvm::cl::cat(ClangDocCategory))
static llvm::Error getHtmlFiles(const char *Argv0, clang::doc::ClangDocContext &CDCtx)
static llvm::Error createDirectories(llvm::StringRef OutDirectory)
static llvm::cl::opt< bool > Pretty("pretty-json", llvm::cl::desc("Serialize JSON with whitespace."), 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))
static llvm::StringRef getFormatString()
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::ExitOnError ExitOnErr
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< 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::json, "json", "Documentation in JSON format"), clEnumValN(OutputFormatTy::md_mustache, "md_mustache", "Documentation in MD format.")), llvm::cl::init(OutputFormatTy::yaml), 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 > 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::Error getMdFiles(const char *Argv0, clang::doc::ClangDocContext &CDCtx)
static llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx)
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 void sortUsrToInfo(llvm::StringMap< doc::Info * > &USRToInfo)
Make the output of clang-doc deterministic by sorting the children of namespaces and records.
static llvm::Error handleMappingFailures(DiagnosticsEngine &Diags, llvm::Error Err)
static llvm::cl::OptionCategory ClangDocCategory("clang-doc options")
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))
const char * Argv0
This file contains general utility functions and helpers used across the clang-doc tool,...
static void addInfoToIndex(Index &Idx, const doc::Info *Info)
@ Info
An information message.
Definition Protocol.h:755
@ Error
An error message.
Definition Protocol.h:751
std::unique_ptr< tooling::FrontendActionFactory > newMapperActionFactory(ClangDocContext CDCtx)
Definition ClangDoc.cpp:52
llvm::Expected< std::unique_ptr< Generator > > findGeneratorByName(llvm::StringRef Format)
thread_local llvm::BumpPtrAllocator PersistentArena
thread_local llvm::BumpPtrAllocator TransientArena
llvm::Error mergeSingleInfo(doc::Info *&Reduced, doc::Info *NewInfo, llvm::BumpPtrAllocator &Arena)
bool Merge(llvm::StringRef MergeDir, llvm::StringRef OutputFile)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::string > UserStylesheets
std::vector< std::string > JsScripts
A base struct for Infos.