Skip to content

Commit 442fe2f

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 6a0e626 commit 442fe2f

File tree

7 files changed

+162
-3
lines changed

7 files changed

+162
-3
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/Error.h"
1919
#include "llvm/Support/MemoryBuffer.h"
2020
#include "llvm/Support/Mustache.h"
21+
#include "llvm/Support/Path.h"
2122

2223
using namespace llvm;
2324
using namespace llvm::json;
@@ -74,7 +75,51 @@ static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
7475

7576
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
7677

78+
static Error
79+
setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
80+
StringRef TemplatePath,
81+
std::vector<std::pair<StringRef, StringRef>> Partials) {
82+
auto T = MustacheTemplateFile::createMustacheFile(TemplatePath);
83+
if (Error Err = T.takeError())
84+
return Err;
85+
Template = std::move(T.get());
86+
for (const auto [Name, FileName] : Partials) {
87+
if (auto Err = Template->registerPartialFile(Name, FileName))
88+
return Err;
89+
}
90+
return Error::success();
91+
}
92+
7793
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
94+
// Template files need to use the native path when they're opened,
95+
// but have to be used in Posix style when used in HTML.
96+
auto ConvertToNative = [](std::string &&Path) -> std::string {
97+
SmallString<128> PathBuf(Path);
98+
llvm::sys::path::native(PathBuf);
99+
return PathBuf.str().str();
100+
};
101+
102+
std::string NamespaceFilePath =
103+
ConvertToNative(CDCtx.MustacheTemplates.lookup("namespace-template"));
104+
std::string ClassFilePath =
105+
ConvertToNative(CDCtx.MustacheTemplates.lookup("class-template"));
106+
std::string CommentFilePath =
107+
ConvertToNative(CDCtx.MustacheTemplates.lookup("comments-template"));
108+
std::string FunctionFilePath =
109+
ConvertToNative(CDCtx.MustacheTemplates.lookup("function-template"));
110+
std::string EnumFilePath =
111+
ConvertToNative(CDCtx.MustacheTemplates.lookup("enum-template"));
112+
std::vector<std::pair<StringRef, StringRef>> Partials = {
113+
{"Comments", CommentFilePath},
114+
{"FunctionPartial", FunctionFilePath},
115+
{"EnumPartial", EnumFilePath}};
116+
117+
if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
118+
return Err;
119+
120+
if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
121+
return Err;
122+
78123
return Error::success();
79124
}
80125

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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
SmallString<128> appendPathNative(StringRef Base, StringRef Path) {
18+
SmallString<128> Default;
19+
sys::path::native(Base, Default);
20+
sys::path::append(Default, Path);
21+
return Default;
22+
}
23+
24+
SmallString<128> appendPathPosix(StringRef Base, StringRef Path) {
25+
SmallString<128> Default;
26+
sys::path::native(Base, Default, sys::path::Style::posix);
27+
sys::path::append(Default, Path);
28+
return Default;
29+
}
30+
31+
void getMustacheHtmlFiles(StringRef AssetsPath,
32+
clang::doc::ClangDocContext &CDCtx) {
33+
assert(!AssetsPath.empty());
34+
assert(sys::fs::is_directory(AssetsPath));
35+
36+
SmallString<128> DefaultStylesheet =
37+
appendPathPosix(AssetsPath, "clang-doc-mustache.css");
38+
SmallString<128> NamespaceTemplate =
39+
appendPathPosix(AssetsPath, "namespace-template.mustache");
40+
SmallString<128> ClassTemplate =
41+
appendPathPosix(AssetsPath, "class-template.mustache");
42+
SmallString<128> EnumTemplate =
43+
appendPathPosix(AssetsPath, "enum-template.mustache");
44+
SmallString<128> FunctionTemplate =
45+
appendPathPosix(AssetsPath, "function-template.mustache");
46+
SmallString<128> CommentTemplate =
47+
appendPathPosix(AssetsPath, "comments-template.mustache");
48+
SmallString<128> IndexJS = appendPathPosix(AssetsPath, "mustache-index.js");
49+
50+
CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
51+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
52+
DefaultStylesheet.str().str());
53+
CDCtx.MustacheTemplates.insert(
54+
{"namespace-template", NamespaceTemplate.c_str()});
55+
CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
56+
CDCtx.MustacheTemplates.insert({"enum-template", EnumTemplate.c_str()});
57+
CDCtx.MustacheTemplates.insert(
58+
{"function-template", FunctionTemplate.c_str()});
59+
CDCtx.MustacheTemplates.insert(
60+
{"comments-template", CommentTemplate.c_str()});
61+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
/// Appends \p Path to \p Base and returns the appended path.
16+
llvm::SmallString<128> appendPathNative(llvm::StringRef Base,
17+
llvm::StringRef Path);
18+
19+
/// Appends \p Path to \p Base and returns the appended path in posix style.
20+
llvm::SmallString<128> appendPathPosix(llvm::StringRef Base,
21+
llvm::StringRef Path);
22+
23+
void getMustacheHtmlFiles(llvm::StringRef AssetsPath,
24+
clang::doc::ClangDocContext &CDCtx);
25+
26+
#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_TOOLS_EXTRA_UNITTESTS_CLANG_DOC_CONFIG_H
2+
#define CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_DOC_CONFIG_H
3+
4+
#define CLANG_DOC_TEST_ASSET_DIR "${CLANG_DOC_TEST_ASSET_DIR}"
5+
6+
#endif // CLANG_TOOLS_EXTRA_UNITTESTS_CLANG_DOC_CONFIG_H

0 commit comments

Comments
 (0)