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"
51static llvm::cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
54static llvm::cl::opt<std::string>
55 ProjectName(
"project-name", llvm::cl::desc(
"Name of project."),
60 llvm::cl::desc(
"Continue if files are not mapped correctly."),
63static llvm::cl::opt<std::string>
65 llvm::cl::desc(
"Directory for outputting generated files."),
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.)"),
74static llvm::cl::opt<bool>
75 PublicOnly(
"public", llvm::cl::desc(
"Document only public declarations."),
80 llvm::cl::desc(
"Use only doxygen-style comments to generate docs."),
84 "stylesheets", llvm::cl::CommaSeparated,
85 llvm::cl::desc(
"CSS stylesheets to extend the default styles."),
90 llvm::cl::desc(
"User supplied asset path to "
91 "override the default css and js files for html output"),
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.)"),
100static llvm::cl::opt<std::string>
102URL of repository that hosts code.
103Used for links to definition locations.)"),
107 "repository-line-prefix",
108 llvm::cl::desc(
"Prefix of line code for repository."),
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),
116static llvm::cl::opt<bool>
117 Pretty(
"pretty-json", llvm::cl::desc(
"Serialize JSON with whitespace."),
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.")),
138 case OutputFormatTy::yaml:
140 case OutputFormatTy::md:
142 case OutputFormatTy::html:
144 case OutputFormatTy::json:
146 case OutputFormatTy::md_mustache:
147 return "md_mustache";
149 llvm_unreachable(
"Unknown OutputFormatTy");
158 return llvm::sys::fs::getMainExecutable(
Argv0, MainAddr);
163 using DirIt = llvm::sys::fs::directory_iterator;
164 std::error_code FileErr;
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")
172 std::string(FilePath));
173 else if (llvm::sys::path::extension(FilePath) ==
".js")
174 CDCtx.
JsScripts.emplace_back(FilePath.str());
178 return llvm::createFileError(FilePath, FileErr);
179 return llvm::Error::success();
186 llvm::outs() <<
"Asset path supply is not a directory: " <<
UserAssetPath
187 <<
" falling back to default\n";
196 llvm::SmallString<128> NativeClangDocPath;
197 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
199 llvm::SmallString<128> AssetsPath;
200 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
201 llvm::sys::path::append(AssetsPath,
"..",
"share",
"clang-doc");
205 return llvm::Error::success();
212 llvm::outs() <<
"Asset path supply is not a directory: " <<
UserAssetPath
213 <<
" falling back to default\n";
217 llvm::SmallString<128> NativeClangDocPath;
218 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
220 llvm::SmallString<128> AssetsPath;
221 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
222 llvm::sys::path::append(AssetsPath,
"..",
"share",
"clang-doc",
"md");
226 return llvm::Error::success();
231static void sortUsrToInfo(llvm::StringMap<doc::Info *> &USRToInfo) {
232 for (
auto &I : USRToInfo) {
233 auto &
Info = I.second;
234 if (
auto *Namespace = dyn_cast<doc::NamespaceInfo>(Info))
235 Namespace->Children.sort();
236 else if (
auto *Record = dyn_cast<doc::RecordInfo>(Info))
237 Record->Children.sort();
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 "
250 Diags.Report(ID) << toString(std::move(Err));
251 return llvm::Error::success();
257 if (std::error_code Err = llvm::sys::fs::create_directories(
OutDirectory))
259 "failed to create directory.");
260 return llvm::Error::success();
263int main(
int argc,
const char **argv) {
264 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
267 ExitOnErr.setBanner(
"clang-doc error: ");
269 const char *Overview =
270 R
"(Generates documentation from source code and comments.
272Example usage for files without flags (default):
274 $ clang-doc File1.cpp File2.cpp ... FileN.cpp
276Example usage for a project using a compile commands database:
278 $ clang-doc --executor=all-TUs compile_commands.json
281 auto Executor =
ExitOnErr(clang::tooling::createExecutorFromCommandLineArgs(
286 llvm::timeTraceProfilerInitialize(200,
"clang-doc");
288 llvm::TimeTraceScope(
"main");
292 llvm::outs() <<
"Emiting docs in " << Format <<
" format.\n";
295 ArgumentsAdjuster ArgAdjuster;
297 ArgAdjuster = combineAdjusters(
298 getInsertArgumentAdjuster(
"-fparse-all-comments",
299 tooling::ArgumentInsertPosition::END),
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);
311 {UserStylesheets.begin(), UserStylesheets.end()}, Diags,
FormatEnum,
314 if (Format ==
"html")
316 else if (Format ==
"md_mustache")
319 llvm::timeTraceProfilerBegin(
"Executor Launch",
"total runtime");
321 llvm::outs() <<
"Mapping decls...\n";
325 llvm::timeTraceProfilerEnd();
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);
337 llvm::timeTraceProfilerEnd();
341 llvm::sys::Mutex USRToInfoMutex;
342 llvm::StringMap<doc::Info *> USRToInfo;
345 llvm::outs() <<
"Reducing " << USRToBitcode.size() <<
" infos...\n";
346 std::atomic<bool>
Error;
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");
356 llvm::DefaultThreadPool Pool(
358 llvm::hardware_concurrency(ExecutorConcurrency));
360 llvm::TimeTraceScope TS(
"Reduce");
361 for (
const auto &Group : USRToBitcode) {
362 StringRef
Key = Group.getKey();
363 std::vector<StringRef> Bitcodes = Group.getValue();
364 Pool.async([Key, Bitcodes, &CDCtx, &Diags, &USRToInfo, &USRToInfoMutex,
365 &IndexMutex, &DiagMutex, &Error, DiagIDBitcodeReading,
366 DiagIDBitcodeMerging]() {
368 llvm::timeTraceProfilerInitialize(200,
"clang-doc");
372 llvm::TimeTraceScope Red(
"decoding and merging bitcode");
373 for (
const auto &Bitcode : Bitcodes) {
375 llvm::scope_exit ArenaGuard(
377 llvm::BitstreamCursor Stream(Bitcode);
379 auto ReadInfos = Reader.readBitcode();
381 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
383 Diags.Report(DiagIDBitcodeReading)
384 << toString(ReadInfos.takeError());
388 for (
auto &I : *ReadInfos) {
390 Reduced, std::move(I),
392 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
393 Diags.Report(DiagIDBitcodeMerging)
394 << toString(std::move(Err));
403 llvm::TimeTraceScope
Merge(
"addInfoToIndex");
404 std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
409 llvm::TimeTraceScope
Merge(
"USRToInfo");
410 std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
411 USRToInfo[
Key] = std::move(Reduced);
415 llvm::timeTraceProfilerFinishThread();
426 llvm::TimeTraceScope Sort(
"Sort USRToInfo");
430 llvm::timeTraceProfilerBegin(
"Writing output",
"total runtime");
435 llvm::outs() <<
"Generating docs...\n";
438 G->generateDocumentation(
OutDirectory, std::move(USRToInfo), CDCtx));
439 llvm::outs() <<
"Generating assets for docs...\n";
441 llvm::timeTraceProfilerEnd();
446 llvm::raw_fd_ostream OS(
"clang-doc-tracing.json", EC,
447 llvm::sys::fs::OF_Text);
449 llvm::timeTraceProfilerWrite(OS);
450 llvm::timeTraceProfilerCleanup();
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))
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.
std::unique_ptr< tooling::FrontendActionFactory > newMapperActionFactory(ClangDocContext CDCtx)
llvm::Expected< std::unique_ptr< Generator > > findGeneratorByName(llvm::StringRef Format)
llvm::BumpPtrAllocator & getPersistentArena()
llvm::BumpPtrAllocator & getTransientArena()
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