Skip to content

Commit 516f2b2

Browse files
ilovepiPeterChou1
andcommitted
[clang-doc] Add helpers for Template config
This patch adds or fills in some helper functions related to template setup when initializing the mustache backend. It was split from #133161. Co-authored-by: Peter Chou <[email protected]>
1 parent 5b34441 commit 516f2b2

File tree

2 files changed

+84
-3
lines changed

2 files changed

+84
-3
lines changed

clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MustacheTemplateFile : public Template {
4747
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
4848
MemoryBuffer::getFile(FileName);
4949
if (auto EC = BufferOrError.getError())
50-
return createFileError("cannot open file", EC);
50+
return createFileError("cannot open file: " + FileName, EC);
5151
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
5252
StringRef FileContent = Buffer->getBuffer();
5353
registerPartial(Name.str(), FileContent.str());
@@ -61,7 +61,41 @@ static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
6161

6262
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
6363

64+
static Error
65+
setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
66+
StringRef TemplatePath,
67+
std::vector<std::pair<StringRef, StringRef>> Partials) {
68+
auto T = MustacheTemplateFile::createMustacheFile(TemplatePath);
69+
if (auto EC = T.getError())
70+
return createFileError("cannot open file: " + TemplatePath, EC);
71+
Template = std::move(T.get());
72+
for (const auto [Name, FileName] : Partials) {
73+
if (auto Err = Template->registerPartialFile(Name, FileName))
74+
return Err;
75+
}
76+
return Error::success();
77+
}
78+
6479
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
80+
std::string NamespaceFilePath =
81+
CDCtx.MustacheTemplates.lookup("namespace-template");
82+
std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
83+
std::string CommentFilePath =
84+
CDCtx.MustacheTemplates.lookup("comments-template");
85+
std::string FunctionFilePath =
86+
CDCtx.MustacheTemplates.lookup("function-template");
87+
std::string EnumFilePath = CDCtx.MustacheTemplates.lookup("enum-template");
88+
std::vector<std::pair<StringRef, StringRef>> Partials = {
89+
{"Comments", CommentFilePath},
90+
{"FunctionPartial", FunctionFilePath},
91+
{"EnumPartial", EnumFilePath}};
92+
93+
if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
94+
return Err;
95+
96+
if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
97+
return Err;
98+
6599
return Error::success();
66100
}
67101

clang-tools-extra/unittests/clang-doc/HTMLMustacheGeneratorTest.cpp

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,47 @@ getClangDocContext(std::vector<std::string> UserStylesheets = {},
4242
return CDCtx;
4343
}
4444

45+
static llvm::SmallString<128> appendPathNative(StringRef Path,
46+
StringRef Asset) {
47+
llvm::SmallString<128> Default;
48+
llvm::sys::path::native(Path, Default);
49+
llvm::sys::path::append(Default, Asset);
50+
return Default;
51+
}
52+
53+
static void getMustacheHtmlFiles(StringRef AssetsPath,
54+
clang::doc::ClangDocContext &CDCtx) {
55+
ASSERT_FALSE(AssetsPath.empty());
56+
ASSERT_TRUE(llvm::sys::fs::is_directory(AssetsPath));
57+
58+
llvm::SmallString<128> DefaultStylesheet =
59+
appendPathNative(AssetsPath, "clang-doc-mustache.css");
60+
llvm::SmallString<128> NamespaceTemplate =
61+
appendPathNative(AssetsPath, "namespace-template.mustache");
62+
llvm::SmallString<128> ClassTemplate =
63+
appendPathNative(AssetsPath, "class-template.mustache");
64+
llvm::SmallString<128> EnumTemplate =
65+
appendPathNative(AssetsPath, "enum-template.mustache");
66+
llvm::SmallString<128> FunctionTemplate =
67+
appendPathNative(AssetsPath, "function-template.mustache");
68+
llvm::SmallString<128> CommentTemplate =
69+
appendPathNative(AssetsPath, "comments-template.mustache");
70+
llvm::SmallString<128> IndexJS =
71+
appendPathNative(AssetsPath, "mustache-index.js");
72+
73+
CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
74+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
75+
DefaultStylesheet.str().str());
76+
CDCtx.MustacheTemplates.insert(
77+
{"namespace-template", NamespaceTemplate.c_str()});
78+
CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
79+
CDCtx.MustacheTemplates.insert({"enum-template", EnumTemplate.c_str()});
80+
CDCtx.MustacheTemplates.insert(
81+
{"function-template", FunctionTemplate.c_str()});
82+
CDCtx.MustacheTemplates.insert(
83+
{"comments-template", CommentTemplate.c_str()});
84+
}
85+
4586
static void verifyFileContents(const Twine &Path, StringRef Contents) {
4687
auto Buffer = MemoryBuffer::getFile(Path);
4788
ASSERT_TRUE((bool)Buffer);
@@ -87,8 +128,14 @@ TEST(HTMLMustacheGeneratorTest, generateDocs) {
87128
assert(G && "Could not find HTMLMustacheGenerator");
88129
ClangDocContext CDCtx = getClangDocContext();
89130

90-
StringRef RootDir = "";
91-
EXPECT_THAT_ERROR(G->generateDocs(RootDir, {}, CDCtx), Succeeded())
131+
unittest::TempDir RootTestDirectory("generateDocsTest", /*Unique=*/true);
132+
CDCtx.OutDirectory = RootTestDirectory.path();
133+
134+
// This seems wrong, but its unclear how else we would test this...
135+
getMustacheHtmlFiles("../../../../../share/clang-doc", CDCtx);
136+
137+
EXPECT_THAT_ERROR(G->generateDocs(RootTestDirectory.path(), {}, CDCtx),
138+
Succeeded())
92139
<< "Failed to generate docs.";
93140
}
94141

0 commit comments

Comments
 (0)