Skip to content

Commit 434ddec

Browse files
ilovepiPeterChou1
andcommitted
[clang-doc] Update clang-doc tool to enable mustache templates
This patch adds a command line option and enables the Mustache template HTML backend. This allows users to use the new, more flexible templates over the old and cumbersome HTML output. Split from #133161. Co-authored-by: Peter Chou <[email protected]>
1 parent 3e47a53 commit 434ddec

File tree

2 files changed

+531
-30
lines changed

2 files changed

+531
-30
lines changed

clang-tools-extra/clang-doc/tool/ClangDocMain.cpp

+50-30
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,14 @@
1818
//===----------------------------------------------------------------------===//
1919

2020
#include "BitcodeReader.h"
21-
#include "BitcodeWriter.h"
2221
#include "ClangDoc.h"
2322
#include "Generators.h"
2423
#include "Representation.h"
25-
#include "clang/AST/AST.h"
26-
#include "clang/AST/Decl.h"
27-
#include "clang/ASTMatchers/ASTMatchFinder.h"
24+
#include "support/Utils.h"
2825
#include "clang/ASTMatchers/ASTMatchersInternal.h"
29-
#include "clang/Driver/Options.h"
30-
#include "clang/Frontend/FrontendActions.h"
3126
#include "clang/Tooling/AllTUsExecution.h"
3227
#include "clang/Tooling/CommonOptionsParser.h"
3328
#include "clang/Tooling/Execution.h"
34-
#include "clang/Tooling/Tooling.h"
3529
#include "llvm/ADT/APFloat.h"
3630
#include "llvm/Support/CommandLine.h"
3731
#include "llvm/Support/Error.h"
@@ -110,22 +104,19 @@ static llvm::cl::opt<std::string> RepositoryCodeLinePrefix(
110104
llvm::cl::desc("Prefix of line code for repository."),
111105
llvm::cl::cat(ClangDocCategory));
112106

113-
enum OutputFormatTy {
114-
md,
115-
yaml,
116-
html,
117-
};
118-
119-
static llvm::cl::opt<OutputFormatTy>
120-
FormatEnum("format", llvm::cl::desc("Format for outputted docs."),
121-
llvm::cl::values(clEnumValN(OutputFormatTy::yaml, "yaml",
122-
"Documentation in YAML format."),
123-
clEnumValN(OutputFormatTy::md, "md",
124-
"Documentation in MD format."),
125-
clEnumValN(OutputFormatTy::html, "html",
126-
"Documentation in HTML format.")),
127-
llvm::cl::init(OutputFormatTy::yaml),
128-
llvm::cl::cat(ClangDocCategory));
107+
enum OutputFormatTy { md, yaml, html, mustache };
108+
109+
static llvm::cl::opt<OutputFormatTy> FormatEnum(
110+
"format", llvm::cl::desc("Format for outputted docs."),
111+
llvm::cl::values(clEnumValN(OutputFormatTy::yaml, "yaml",
112+
"Documentation in YAML format."),
113+
clEnumValN(OutputFormatTy::md, "md",
114+
"Documentation in MD format."),
115+
clEnumValN(OutputFormatTy::html, "html",
116+
"Documentation in HTML format."),
117+
clEnumValN(OutputFormatTy::mustache, "mustache",
118+
"Documentation in mustache HTML format")),
119+
llvm::cl::init(OutputFormatTy::yaml), llvm::cl::cat(ClangDocCategory));
129120

130121
static std::string getFormatString() {
131122
switch (FormatEnum) {
@@ -135,6 +126,8 @@ static std::string getFormatString() {
135126
return "md";
136127
case OutputFormatTy::html:
137128
return "html";
129+
case OutputFormatTy::mustache:
130+
return "mustache";
138131
}
139132
llvm_unreachable("Unknown OutputFormatTy");
140133
}
@@ -178,13 +171,9 @@ static llvm::Error getDefaultAssetFiles(const char *Argv0,
178171
llvm::SmallString<128> AssetsPath;
179172
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
180173
llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
181-
llvm::SmallString<128> DefaultStylesheet;
182-
llvm::sys::path::native(AssetsPath, DefaultStylesheet);
183-
llvm::sys::path::append(DefaultStylesheet,
184-
"clang-doc-default-stylesheet.css");
185-
llvm::SmallString<128> IndexJS;
186-
llvm::sys::path::native(AssetsPath, IndexJS);
187-
llvm::sys::path::append(IndexJS, "index.js");
174+
llvm::SmallString<128> DefaultStylesheet =
175+
appendPathNative(AssetsPath, "clang-doc-default-stylesheet.css");
176+
llvm::SmallString<128> IndexJS = appendPathNative(AssetsPath, "index.js");
188177

189178
if (!llvm::sys::fs::is_regular_file(IndexJS))
190179
return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -215,6 +204,30 @@ static llvm::Error getHtmlAssetFiles(const char *Argv0,
215204
return getDefaultAssetFiles(Argv0, CDCtx);
216205
}
217206

207+
static llvm::Error getMustacheHtmlFiles(const char *Argv0,
208+
clang::doc::ClangDocContext &CDCtx) {
209+
bool IsDir = llvm::sys::fs::is_directory(UserAssetPath);
210+
if (!UserAssetPath.empty() && !IsDir)
211+
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
212+
<< " falling back to default\n";
213+
if (IsDir) {
214+
getMustacheHtmlFiles(UserAssetPath, CDCtx);
215+
return llvm::Error::success();
216+
}
217+
void *MainAddr = (void *)(intptr_t)getExecutablePath;
218+
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
219+
llvm::SmallString<128> NativeClangDocPath;
220+
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
221+
222+
llvm::SmallString<128> AssetsPath;
223+
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
224+
llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
225+
226+
getMustacheHtmlFiles(AssetsPath, CDCtx);
227+
228+
return llvm::Error::success();
229+
}
230+
218231
/// Make the output of clang-doc deterministic by sorting the children of
219232
/// namespaces and records.
220233
static void
@@ -290,6 +303,13 @@ Example usage for a project using a compile commands database:
290303
}
291304
}
292305

306+
if (Format == "mustache") {
307+
if (auto Err = getMustacheHtmlFiles(argv[0], CDCtx)) {
308+
llvm::errs() << toString(std::move(Err)) << "\n";
309+
return 1;
310+
}
311+
}
312+
293313
// Mapping phase
294314
llvm::outs() << "Mapping decls...\n";
295315
auto Err =

0 commit comments

Comments
 (0)