Skip to content

Commit f8205f7

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 b67b6ac commit f8205f7

File tree

5 files changed

+125
-4
lines changed

5 files changed

+125
-4
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/clang-doc/support/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ set(LLVM_LINK_COMPONENTS
66

77
add_clang_library(clangDocSupport STATIC
88
File.cpp
9-
)
9+
Utils.cpp
10+
)
11+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "Utils.h"
10+
#include "llvm/ADT/SmallString.h"
11+
#include "llvm/ADT/StringRef.h"
12+
#include "llvm/Support/FileSystem.h"
13+
#include "llvm/Support/Path.h"
14+
15+
using namespace llvm;
16+
17+
llvm::SmallString<128> appendPathNative(StringRef Path, StringRef Asset) {
18+
llvm::SmallString<128> Default;
19+
llvm::sys::path::native(Path, Default);
20+
llvm::sys::path::append(Default, Asset);
21+
return Default;
22+
}
23+
24+
void getMustacheHtmlFiles(StringRef AssetsPath,
25+
clang::doc::ClangDocContext &CDCtx) {
26+
assert(!AssetsPath.empty());
27+
assert(llvm::sys::fs::is_directory(AssetsPath));
28+
29+
llvm::SmallString<128> DefaultStylesheet =
30+
appendPathNative(AssetsPath, "clang-doc-mustache.css");
31+
llvm::SmallString<128> NamespaceTemplate =
32+
appendPathNative(AssetsPath, "namespace-template.mustache");
33+
llvm::SmallString<128> ClassTemplate =
34+
appendPathNative(AssetsPath, "class-template.mustache");
35+
llvm::SmallString<128> EnumTemplate =
36+
appendPathNative(AssetsPath, "enum-template.mustache");
37+
llvm::SmallString<128> FunctionTemplate =
38+
appendPathNative(AssetsPath, "function-template.mustache");
39+
llvm::SmallString<128> CommentTemplate =
40+
appendPathNative(AssetsPath, "comments-template.mustache");
41+
llvm::SmallString<128> IndexJS =
42+
appendPathNative(AssetsPath, "mustache-index.js");
43+
44+
CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
45+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
46+
DefaultStylesheet.str().str());
47+
CDCtx.MustacheTemplates.insert(
48+
{"namespace-template", NamespaceTemplate.c_str()});
49+
CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
50+
CDCtx.MustacheTemplates.insert({"enum-template", EnumTemplate.c_str()});
51+
CDCtx.MustacheTemplates.insert(
52+
{"function-template", FunctionTemplate.c_str()});
53+
CDCtx.MustacheTemplates.insert(
54+
{"comments-template", CommentTemplate.c_str()});
55+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILE_H
9+
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILE_H
10+
11+
#include "../Representation.h"
12+
#include "llvm/ADT/SmallString.h"
13+
#include "llvm/ADT/StringRef.h"
14+
15+
llvm::SmallString<128> appendPathNative(llvm::StringRef Path,
16+
llvm::StringRef Asset);
17+
18+
void getMustacheHtmlFiles(llvm::StringRef AssetsPath,
19+
clang::doc::ClangDocContext &CDCtx);
20+
21+
#endif

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ClangDocTest.h"
1010
#include "Generators.h"
1111
#include "Representation.h"
12+
#include "support/Utils.h"
1213
#include "clang/Basic/Version.h"
1314
#include "llvm/Support/Path.h"
1415
#include "llvm/Testing/Support/Error.h"
@@ -86,8 +87,16 @@ TEST(HTMLMustacheGeneratorTest, generateDocs) {
8687
assert(G && "Could not find HTMLMustacheGenerator");
8788
ClangDocContext CDCtx = getClangDocContext();
8889

89-
StringRef RootDir = "";
90-
EXPECT_THAT_ERROR(G->generateDocs(RootDir, {}, CDCtx), Succeeded())
90+
unittest::TempDir RootTestDirectory("generateDocsTest", /*Unique=*/true);
91+
CDCtx.OutDirectory = RootTestDirectory.path();
92+
93+
// This seems wrong, but its unclear how else we would test this...
94+
SmallString<128> AssetPath("../../../../../share/clang-doc");
95+
llvm::sys::path::native(AssetPath);
96+
getMustacheHtmlFiles(AssetPath, CDCtx);
97+
98+
EXPECT_THAT_ERROR(G->generateDocs(RootTestDirectory.path(), {}, CDCtx),
99+
Succeeded())
91100
<< "Failed to generate docs.";
92101
}
93102

0 commit comments

Comments
 (0)