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