Skip to content

Commit c7955f5

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 cf590a7 commit c7955f5

File tree

2 files changed

+522
-20
lines changed

2 files changed

+522
-20
lines changed

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

+41-20
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,11 +104,7 @@ 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-
};
107+
enum OutputFormatTy { md, yaml, html, mhtml };
118108

119109
static llvm::cl::opt<OutputFormatTy>
120110
FormatEnum("format", llvm::cl::desc("Format for outputted docs."),
@@ -123,7 +113,9 @@ static llvm::cl::opt<OutputFormatTy>
123113
clEnumValN(OutputFormatTy::md, "md",
124114
"Documentation in MD format."),
125115
clEnumValN(OutputFormatTy::html, "html",
126-
"Documentation in HTML format.")),
116+
"Documentation in HTML format."),
117+
clEnumValN(OutputFormatTy::mhtml, "mhtml",
118+
"Documentation in mHTML format")),
127119
llvm::cl::init(OutputFormatTy::yaml),
128120
llvm::cl::cat(ClangDocCategory));
129121

@@ -135,6 +127,8 @@ static std::string getFormatString() {
135127
return "md";
136128
case OutputFormatTy::html:
137129
return "html";
130+
case OutputFormatTy::mhtml:
131+
return "mhtml";
138132
}
139133
llvm_unreachable("Unknown OutputFormatTy");
140134
}
@@ -178,13 +172,9 @@ static llvm::Error getDefaultAssetFiles(const char *Argv0,
178172
llvm::SmallString<128> AssetsPath;
179173
AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
180174
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");
175+
llvm::SmallString<128> DefaultStylesheet =
176+
appendPathNative(AssetsPath, "clang-doc-default-stylesheet.css");
177+
llvm::SmallString<128> IndexJS = appendPathNative(AssetsPath, "index.js");
188178

189179
if (!llvm::sys::fs::is_regular_file(IndexJS))
190180
return llvm::createStringError(llvm::inconvertibleErrorCode(),
@@ -215,6 +205,30 @@ static llvm::Error getHtmlAssetFiles(const char *Argv0,
215205
return getDefaultAssetFiles(Argv0, CDCtx);
216206
}
217207

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

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

0 commit comments

Comments
 (0)