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"
49static llvm::cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
52static llvm::cl::opt<std::string>
53 ProjectName(
"project-name", llvm::cl::desc(
"Name of project."),
58 llvm::cl::desc(
"Continue if files are not mapped correctly."),
61static llvm::cl::opt<std::string>
63 llvm::cl::desc(
"Directory for outputting generated files."),
66static llvm::cl::opt<std::string>
68 llvm::cl::desc(R
"(Base Directory for generated documentation.
69URLs will be rooted at this directory for HTML links.)"),
72static llvm::cl::opt<bool>
73 PublicOnly(
"public", llvm::cl::desc(
"Document only public declarations."),
78 llvm::cl::desc(
"Use only doxygen-style comments to generate docs."),
82 "stylesheets", llvm::cl::CommaSeparated,
83 llvm::cl::desc(
"CSS stylesheets to extend the default styles."),
88 llvm::cl::desc(
"User supplied asset path to "
89 "override the default css and js files for html output"),
92static llvm::cl::opt<std::string>
SourceRoot(
"source-root", llvm::cl::desc(R
"(
93Directory where processed files are stored.
94Links to definition locations will only be
95generated if the file is in this dir.)"),
98static llvm::cl::opt<std::string>
100URL of repository that hosts code.
101Used for links to definition locations.)"),
105 "repository-line-prefix",
106 llvm::cl::desc(
"Prefix of line code for repository."),
109static llvm::cl::opt<bool>
FTimeTrace(
"ftime-trace", llvm::cl::desc(R
"(
110Turn on time profiler. Generates clang-doc-tracing.json)"),
111 llvm::cl::init(false),
116static llvm::cl::opt<OutputFormatTy>
117 FormatEnum(
"format", llvm::cl::desc(
"Format for outputted docs."),
119 "Documentation in YAML format."),
121 "Documentation in MD format."),
123 "Documentation in HTML format."),
125 "Documentation in JSON format")),
142 llvm_unreachable(
"Unknown OutputFormatTy");
151 return llvm::sys::fs::getMainExecutable(
Argv0, MainAddr);
156 using DirIt = llvm::sys::fs::directory_iterator;
157 std::error_code FileErr;
160 !FileErr && DirStart != DirEnd; DirStart.increment(FileErr)) {
161 FilePath = DirStart->path();
162 if (llvm::sys::fs::is_regular_file(FilePath)) {
163 if (llvm::sys::path::extension(FilePath) ==
".css")
165 std::string(FilePath));
166 else if (llvm::sys::path::extension(FilePath) ==
".js")
167 CDCtx.
JsScripts.emplace_back(FilePath.str());
171 return llvm::createFileError(FilePath, FileErr);
172 return llvm::Error::success();
179 llvm::outs() <<
"Asset path supply is not a directory: " <<
UserAssetPath
180 <<
" falling back to default\n";
187 llvm::SmallString<128> NativeClangDocPath;
188 llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
190 llvm::SmallString<128> AssetsPath;
191 AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
192 llvm::sys::path::append(AssetsPath,
"..",
"share",
"clang-doc");
196 return llvm::Error::success();
202sortUsrToInfo(llvm::StringMap<std::unique_ptr<doc::Info>> &USRToInfo) {
203 for (
auto &I : USRToInfo) {
204 auto &
Info = I.second;
211 Record->Children.sort();
219 return llvm::Error::success();
221 unsigned ID = Diags.getCustomDiagID(
222 DiagnosticsEngine::Warning,
223 "Error mapping decls in files. Clang-doc will ignore these files and "
225 Diags.Report(ID) << toString(std::move(Err));
226 return llvm::Error::success();
232 if (std::error_code Err = llvm::sys::fs::create_directories(
OutDirectory))
234 "failed to create directory.");
235 return llvm::Error::success();
238int main(
int argc,
const char **argv) {
239 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
242 ExitOnErr.setBanner(
"clang-doc error: ");
244 const char *Overview =
245 R
"(Generates documentation from source code and comments.
247Example usage for files without flags (default):
249 $ clang-doc File1.cpp File2.cpp ... FileN.cpp
251Example usage for a project using a compile commands database:
253 $ clang-doc --executor=all-TUs compile_commands.json
256 auto Executor =
ExitOnErr(clang::tooling::createExecutorFromCommandLineArgs(
261 llvm::timeTraceProfilerInitialize(200,
"clang-doc");
263 llvm::TimeTraceScope(
"main");
267 llvm::outs() <<
"Emiting docs in " << Format <<
" format.\n";
270 ArgumentsAdjuster ArgAdjuster;
272 ArgAdjuster = combineAdjusters(
273 getInsertArgumentAdjuster(
"-fparse-all-comments",
274 tooling::ArgumentInsertPosition::END),
277 auto DiagOpts = std::make_unique<DiagnosticOptions>();
278 TextDiagnosticPrinter *DiagClient =
279 new TextDiagnosticPrinter(llvm::errs(), *DiagOpts);
280 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(
new DiagnosticIDs());
281 DiagnosticsEngine Diags(DiagID, *DiagOpts, DiagClient);
286 {UserStylesheets.begin(), UserStylesheets.end()}, Diags,
FTimeTrace);
288 if (Format ==
"html")
291 llvm::timeTraceProfilerBegin(
"Executor Launch",
"total runtime");
293 llvm::outs() <<
"Mapping decls...\n";
297 llvm::timeTraceProfilerEnd();
302 llvm::timeTraceProfilerBegin(
"Collect Info",
"total runtime");
303 llvm::outs() <<
"Collecting infos...\n";
304 llvm::StringMap<std::vector<StringRef>> USRToBitcode;
305 Executor->getToolResults()->forEachResult(
306 [&](StringRef Key, StringRef Value) {
307 USRToBitcode[
Key].emplace_back(Value);
309 llvm::timeTraceProfilerEnd();
313 llvm::sys::Mutex USRToInfoMutex;
314 llvm::StringMap<std::unique_ptr<doc::Info>> USRToInfo;
317 llvm::outs() <<
"Reducing " << USRToBitcode.size() <<
" infos...\n";
318 std::atomic<bool>
Error;
320 llvm::sys::Mutex IndexMutex;
321 llvm::sys::Mutex DiagMutex;
322 unsigned DiagIDBitcodeReading = Diags.getCustomDiagID(
323 DiagnosticsEngine::Error,
"error reading bitcode: %0");
324 unsigned DiagIDBitcodeMerging = Diags.getCustomDiagID(
325 DiagnosticsEngine::Error,
"error merging bitcode: %0");
327 llvm::DefaultThreadPool Pool(
328 llvm::hardware_concurrency(ExecutorConcurrency));
330 llvm::TimeTraceScope TS(
"Reduce");
331 for (
auto &Group : USRToBitcode) {
332 Pool.async([&, &Diags = Diags]() {
334 llvm::timeTraceProfilerInitialize(200,
"clang-doc");
336 std::vector<std::unique_ptr<doc::Info>> Infos;
338 llvm::TimeTraceScope Red(
"decoding bitcode");
339 for (
auto &Bitcode : Group.getValue()) {
340 llvm::BitstreamCursor Stream(Bitcode);
342 auto ReadInfos = Reader.readBitcode();
344 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
346 Diags.Report(DiagIDBitcodeReading)
347 << toString(ReadInfos.takeError());
351 std::move(ReadInfos->begin(), ReadInfos->end(),
352 std::back_inserter(Infos));
356 std::unique_ptr<doc::Info> Reduced;
359 llvm::TimeTraceScope
Merge(
"merging bitcode");
363 std::lock_guard<llvm::sys::Mutex> Guard(DiagMutex);
364 Diags.Report(DiagIDBitcodeMerging)
365 << toString(ExpReduced.takeError());
368 Reduced = std::move(*ExpReduced);
373 llvm::TimeTraceScope
Merge(
"addInfoToIndex");
374 std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
379 llvm::TimeTraceScope
Merge(
"USRToInfo");
380 std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
381 USRToInfo[Group.getKey()] = std::move(Reduced);
385 llvm::timeTraceProfilerFinishThread();
396 llvm::TimeTraceScope Sort(
"Sort USRToInfo");
400 llvm::timeTraceProfilerBegin(
"Writing output",
"total runtime");
405 llvm::outs() <<
"Generating docs...\n";
408 G->generateDocumentation(
OutDirectory, std::move(USRToInfo), CDCtx));
409 llvm::outs() <<
"Generating assets for docs...\n";
411 llvm::timeTraceProfilerEnd();
416 llvm::raw_fd_ostream OS(
"clang-doc-tracing.json", EC,
417 llvm::sys::fs::OF_Text);
419 llvm::timeTraceProfilerWrite(OS);
420 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 void sortUsrToInfo(llvm::StringMap< std::unique_ptr< doc::Info > > &USRToInfo)
Make the output of clang-doc deterministic by sorting the children of namespaces and records.
static llvm::cl::opt< std::string > ProjectName("project-name", llvm::cl::desc("Name of project."), 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")), llvm::cl::init(OutputFormatTy::yaml), 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 > 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::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 std::string getFormatString()
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 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.
llvm::Expected< std::unique_ptr< Info > > mergeInfos(std::vector< std::unique_ptr< Info > > &Values)
std::unique_ptr< tooling::FrontendActionFactory > newMapperActionFactory(ClangDocContext CDCtx)
llvm::Expected< std::unique_ptr< Generator > > findGeneratorByName(llvm::StringRef Format)
bool Merge(llvm::StringRef MergeDir, llvm::StringRef OutputFile)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::string > UserStylesheets
std::vector< std::string > JsScripts