Skip to content

Commit e04a368

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 99ed84d commit e04a368

File tree

2 files changed

+79
-20
lines changed

2 files changed

+79
-20
lines changed

clang-tools-extra/clang-doc/tool/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ target_link_libraries(clang-doc
2121

2222
set(assets
2323
index.js
24+
mustache-index.js
25+
class-template.mustache
26+
comments-template.mustache
27+
enum-template.mustache
28+
function-template.mustache
29+
namespace-template.mustache
30+
clang-doc-mustache.css
2431
clang-doc-default-stylesheet.css
2532
)
2633

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

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,13 @@
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"
2824
#include "clang/ASTMatchers/ASTMatchersInternal.h"
29-
#include "clang/Driver/Options.h"
30-
#include "clang/Frontend/FrontendActions.h"
3125
#include "clang/Tooling/AllTUsExecution.h"
3226
#include "clang/Tooling/CommonOptionsParser.h"
3327
#include "clang/Tooling/Execution.h"
34-
#include "clang/Tooling/Tooling.h"
3528
#include "llvm/ADT/APFloat.h"
3629
#include "llvm/Support/CommandLine.h"
3730
#include "llvm/Support/Error.h"
@@ -110,11 +103,7 @@ static llvm::cl::opt<std::string> RepositoryCodeLinePrefix(
110103
llvm::cl::desc("Prefix of line code for repository."),
111104
llvm::cl::cat(ClangDocCategory));
112105

113-
enum OutputFormatTy {
114-
md,
115-
yaml,
116-
html,
117-
};
106+
enum OutputFormatTy { md, yaml, html, mhtml };
118107

119108
static llvm::cl::opt<OutputFormatTy>
120109
FormatEnum("format", llvm::cl::desc("Format for outputted docs."),
@@ -123,7 +112,9 @@ static llvm::cl::opt<OutputFormatTy>
123112
clEnumValN(OutputFormatTy::md, "md",
124113
"Documentation in MD format."),
125114
clEnumValN(OutputFormatTy::html, "html",
126-
"Documentation in HTML format.")),
115+
"Documentation in HTML format."),
116+
clEnumValN(OutputFormatTy::mhtml, "mhtml",
117+
"Documentation in mHTML format")),
127118
llvm::cl::init(OutputFormatTy::yaml),
128119
llvm::cl::cat(ClangDocCategory));
129120

@@ -135,6 +126,8 @@ static std::string getFormatString() {
135126
return "md";
136127
case OutputFormatTy::html:
137128
return "html";
129+
case OutputFormatTy::mhtml:
130+
return "mhtml";
138131
}
139132
llvm_unreachable("Unknown OutputFormatTy");
140133
}
@@ -168,6 +161,14 @@ static llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
168161
return llvm::Error::success();
169162
}
170163

164+
static llvm::SmallString<128> appendPathNative(StringRef Path,
165+
StringRef Asset) {
166+
llvm::SmallString<128> Default;
167+
llvm::sys::path::native(Path, Default);
168+
llvm::sys::path::append(Default, Asset);
169+
return Default;
170+
}
171+
171172
static llvm::Error getDefaultAssetFiles(const char *Argv0,
172173
clang::doc::ClangDocContext &CDCtx) {
173174
void *MainAddr = (void *)(intptr_t)getExecutablePath;
@@ -178,13 +179,9 @@ static llvm::Error getDefaultAssetFiles(const char *Argv0,
178179
llvm::SmallString<128> AssetsPath;
179180
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
180181
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");
182+
llvm::SmallString<128> DefaultStylesheet =
183+
appendPathNative(AssetsPath, "clang-doc-default-stylesheet.css");
184+
llvm::SmallString<128> IndexJS = appendPathNative(AssetsPath, "index.js");
188185

189186
if (!llvm::sys::fs::is_regular_file(IndexJS))
190187
return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -215,6 +212,54 @@ static llvm::Error getHtmlAssetFiles(const char *Argv0,
215212
return getDefaultAssetFiles(Argv0, CDCtx);
216213
}
217214

215+
static llvm::Error getMustacheHtmlFiles(const char *Argv0,
216+
clang::doc::ClangDocContext &CDCtx) {
217+
if (!UserAssetPath.empty() &&
218+
!llvm::sys::fs::is_directory(std::string(UserAssetPath)))
219+
llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
220+
<< " falling back to default\n";
221+
if (llvm::sys::fs::is_directory(std::string(UserAssetPath)))
222+
return getAssetFiles(CDCtx);
223+
224+
void *MainAddr = (void *)(intptr_t)getExecutablePath;
225+
std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
226+
llvm::SmallString<128> NativeClangDocPath;
227+
llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
228+
229+
llvm::SmallString<128> AssetsPath;
230+
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
231+
llvm::sys::path::append(AssetsPath, "..", "share", "clang-doc");
232+
233+
llvm::SmallString<128> DefaultStylesheet =
234+
appendPathNative(AssetsPath, "clang-doc-mustache.css");
235+
llvm::SmallString<128> NamespaceTemplate =
236+
appendPathNative(AssetsPath, "namespace-template.mustache");
237+
llvm::SmallString<128> ClassTemplate =
238+
appendPathNative(AssetsPath, "class-template.mustache");
239+
llvm::SmallString<128> EnumTemplate =
240+
appendPathNative(AssetsPath, "enum-template.mustache");
241+
llvm::SmallString<128> FunctionTemplate =
242+
appendPathNative(AssetsPath, "function-template.mustache");
243+
llvm::SmallString<128> CommentTemplate =
244+
appendPathNative(AssetsPath, "comments-template.mustache");
245+
llvm::SmallString<128> IndexJS =
246+
appendPathNative(AssetsPath, "mustache-index.js");
247+
248+
CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
249+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
250+
std::string(DefaultStylesheet));
251+
CDCtx.MustacheTemplates.insert(
252+
{"namespace-template", NamespaceTemplate.c_str()});
253+
CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
254+
CDCtx.MustacheTemplates.insert({"enum-template", EnumTemplate.c_str()});
255+
CDCtx.MustacheTemplates.insert(
256+
{"function-template", FunctionTemplate.c_str()});
257+
CDCtx.MustacheTemplates.insert(
258+
{"comments-template", CommentTemplate.c_str()});
259+
260+
return llvm::Error::success();
261+
}
262+
218263
/// Make the output of clang-doc deterministic by sorting the children of
219264
/// namespaces and records.
220265
static void
@@ -290,6 +335,13 @@ Example usage for a project using a compile commands database:
290335
}
291336
}
292337

338+
if (Format == "mhtml") {
339+
if (auto Err = getMustacheHtmlFiles(argv[0], CDCtx)) {
340+
llvm::errs() << toString(std::move(Err)) << "\n";
341+
return 1;
342+
}
343+
}
344+
293345
// Mapping phase
294346
llvm::outs() << "Mapping decls...\n";
295347
auto Err =

0 commit comments

Comments
 (0)