Skip to content

Commit 2c95be8

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 b70cf43 commit 2c95be8

File tree

7 files changed

+140
-3
lines changed

7 files changed

+140
-3
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,41 @@ static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
7474

7575
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
7676

77+
static Error
78+
setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
79+
StringRef TemplatePath,
80+
std::vector<std::pair<StringRef, StringRef>> Partials) {
81+
auto T = MustacheTemplateFile::createMustacheFile(TemplatePath);
82+
if (Error Err = T.takeError())
83+
return Err;
84+
Template = std::move(T.get());
85+
for (const auto [Name, FileName] : Partials) {
86+
if (auto Err = Template->registerPartialFile(Name, FileName))
87+
return Err;
88+
}
89+
return Error::success();
90+
}
91+
7792
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
93+
std::string NamespaceFilePath =
94+
CDCtx.MustacheTemplates.lookup("namespace-template");
95+
std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
96+
std::string CommentFilePath =
97+
CDCtx.MustacheTemplates.lookup("comments-template");
98+
std::string FunctionFilePath =
99+
CDCtx.MustacheTemplates.lookup("function-template");
100+
std::string EnumFilePath = CDCtx.MustacheTemplates.lookup("enum-template");
101+
std::vector<std::pair<StringRef, StringRef>> Partials = {
102+
{"Comments", CommentFilePath},
103+
{"FunctionPartial", FunctionFilePath},
104+
{"EnumPartial", EnumFilePath}};
105+
106+
if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
107+
return Err;
108+
109+
if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
110+
return Err;
111+
78112
return Error::success();
79113
}
80114

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/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ set(LLVM_LINK_COMPONENTS
44
FrontendOpenMP
55
)
66

7+
# Unittests need access to mustache template files, so we use a config file to
8+
# inject those into a config.h header that can provide it to the unittests.
9+
set(CLANG_DOC_TEST_ASSET_DIR "${LLVM_RUNTIME_OUTPUT_INTDIR}/../share/clang-doc")
10+
configure_file(
11+
"${CMAKE_CURRENT_SOURCE_DIR}/config.h.cmake"
12+
"${CMAKE_CURRENT_BINARY_DIR}/config.h"
13+
)
14+
15+
# The config.h file is in ${CMAKE_CURRENT_BINARY_DIR}, so add it to
16+
# include_directories.
717
get_filename_component(CLANG_DOC_SOURCE_DIR
818
${CMAKE_CURRENT_SOURCE_DIR}/../../clang-doc REALPATH)
919
include_directories(
1020
${CLANG_DOC_SOURCE_DIR}
21+
${CMAKE_CURRENT_BINARY_DIR}
1122
)
1223

1324
add_extra_unittest(ClangDocTests
@@ -40,3 +51,4 @@ target_link_libraries(ClangDocTests
4051
clangDoc
4152
LLVMTestingSupport
4253
)
54+

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "ClangDocTest.h"
1010
#include "Generators.h"
1111
#include "Representation.h"
12+
#include "config.h"
13+
#include "support/Utils.h"
1214
#include "clang/Basic/Version.h"
1315
#include "llvm/Support/Path.h"
1416
#include "llvm/Testing/Support/Error.h"
@@ -86,8 +88,13 @@ TEST(HTMLMustacheGeneratorTest, generateDocs) {
8688
assert(G && "Could not find HTMLMustacheGenerator");
8789
ClangDocContext CDCtx = getClangDocContext();
8890

89-
StringRef RootDir = "";
90-
EXPECT_THAT_ERROR(G->generateDocs(RootDir, {}, CDCtx), Succeeded())
91+
unittest::TempDir RootTestDirectory("generateDocsTest", /*Unique=*/true);
92+
CDCtx.OutDirectory = RootTestDirectory.path();
93+
94+
getMustacheHtmlFiles(CLANG_DOC_TEST_ASSET_DIR, CDCtx);
95+
96+
EXPECT_THAT_ERROR(G->generateDocs(RootTestDirectory.path(), {}, CDCtx),
97+
Succeeded())
9198
<< "Failed to generate docs.";
9299
}
93100

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef CLANG_DOC_CONFIG_H
2+
#define CLANG_DOC_CONFIG_H
3+
4+
#define CLANG_DOC_TEST_ASSET_DIR "${CLANG_DOC_TEST_ASSET_DIR}"
5+
6+
#endif

0 commit comments

Comments
 (0)