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"
50static llvm::cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
53static llvm::cl::opt<std::string>
54 ProjectName(
"project-name", llvm::cl::desc(
"Name of project."),
59 llvm::cl::desc(
"Continue if files are not mapped correctly."),
62static llvm::cl::opt<std::string>
64 llvm::cl::desc(
"Directory for outputting generated files."),
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.)"),
73static llvm::cl::opt<bool>
74 PublicOnly(
"public", llvm::cl::desc(
"Document only public declarations."),
79 llvm::cl::desc(
"Use only doxygen-style comments to generate docs."),
83 "stylesheets", llvm::cl::CommaSeparated,
84 llvm::cl::desc(
"CSS stylesheets to extend the default styles."),
89 llvm::cl::desc(
"User supplied asset path to "
90 "override the default css and js files for html output"),
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.)"),
99static llvm::cl::opt<std::string>
101URL of repository that hosts code.
102Used for links to definition locations.)"),
106 "repository-line-prefix",
107 llvm::cl::desc(
"Prefix of line code for repository."),
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),
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.")),
133 case OutputFormatTy::yaml:
135 case OutputFormatTy::md:
137 case OutputFormatTy::html:
139 case OutputFormatTy::json:
141 case OutputFormatTy::md_mustache:
142 return "md_mustache";
144 llvm_unreachable(
"Unknown OutputFormatTy");
153 return llvm::sys::fs::getMainExecutable(
Argv0, MainAddr);
158 using DirIt = llvm::sys::fs::directory_iterator;
159 std::error_code FileErr;
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")
167 std::string(FilePath));
168 else if (llvm::sys::path::extension(FilePath) ==
".js")
169 CDCtx.
JsScripts.emplace_back(FilePath.str());
173 return llvm::createFileError(FilePath, FileErr);
174 return llvm::Error::success();
181 llvm::outs() <<
"Asset path supply is not a directory: " <<
UserAssetPath
182 <<
" falling back to default\n";
191 llvm::SmallString<128> NativeClangDocPath;
192 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
194 llvm::SmallString<128> AssetsPath;
195 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
196 llvm::sys::path::append(AssetsPath,
"..",
"share",
"clang-doc");
200 return llvm::Error::success();
207 llvm::outs() <<
"Asset path supply is not a directory: " <<
UserAssetPath
208 <<
" falling back to default\n";
212 llvm::SmallString<128> NativeClangDocPath;
213 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
215 llvm::SmallString<128> AssetsPath;
216 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
217 llvm::sys::path::append(AssetsPath,
"..",
"share",
"clang-doc",
"md");
221 return llvm::Error::success();
228 for (
auto &I : USRToInfo) {
229 auto &
Info = I.second;
236 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::OwnedPtr<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");
355 llvm::DefaultThreadPool Pool(
356 llvm::hardware_concurrency(ExecutorConcurrency));
358 llvm::TimeTraceScope TS(
"Reduce");
359 for (
auto &Group : USRToBitcode) {
360 Pool.async([&, &Diags = Diags]() {
362 llvm::timeTraceProfilerInitialize(200,
"clang-doc");
366 llvm::TimeTraceScope Red(
"decoding bitcode");
367 for (
auto &Bitcode : Group.getValue()) {
368 llvm::BitstreamCursor Stream(Bitcode);
370 auto ReadInfos = Reader.readBitcode();
372 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
374 Diags.Report(DiagIDBitcodeReading)
375 << toString(ReadInfos.takeError());
379 std::move(ReadInfos->begin(), ReadInfos->end(),
380 std::back_inserter(Infos));
387 llvm::TimeTraceScope
Merge(
"merging bitcode");
391 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
392 Diags.Report(DiagIDBitcodeMerging)
393 << toString(ExpReduced.takeError());
396 Reduced = std::move(*ExpReduced);
401 llvm::TimeTraceScope
Merge(
"addInfoToIndex");
402 std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
407 llvm::TimeTraceScope
Merge(
"USRToInfo");
408 std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
409 USRToInfo[Group.getKey()] = std::move(Reduced);
413 llvm::timeTraceProfilerFinishThread();
424 llvm::TimeTraceScope Sort(
"Sort USRToInfo");
428 llvm::timeTraceProfilerBegin(
"Writing output",
"total runtime");
433 llvm::outs() <<
"Generating docs...\n";
436 G->generateDocumentation(
OutDirectory, std::move(USRToInfo), CDCtx));
437 llvm::outs() <<
"Generating assets for docs...\n";
439 llvm::timeTraceProfilerEnd();
444 llvm::raw_fd_ostream OS(
"clang-doc-tracing.json", EC,
445 llvm::sys::fs::OF_Text);
447 llvm::timeTraceProfilerWrite(OS);
448 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 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))
static void addInfoToIndex(Index &Idx, const doc::Info *Info)
@ Info
An information message.
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)
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