Skip to content

Commit 6a0e626

Browse files
ilovepiPeterChou1
andauthored
[clang-doc] Add HTMLMustacheGenerator methods (#138061)
Split from #133161. This patch fills in the implementation for a number of the MustacheHTMLGenerator methods. Many of these APIs are just stubbed out, and will have their implementation filled in by later patches. Co-authored-by: Peter Chou <[email protected]>
1 parent 437195e commit 6a0e626

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,113 @@ class MustacheTemplateFile : public Template {
7070
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
7171
};
7272

73+
static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
74+
75+
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
76+
77+
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
78+
return Error::success();
79+
}
80+
7381
Error MustacheHTMLGenerator::generateDocs(
7482
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
7583
const clang::doc::ClangDocContext &CDCtx) {
84+
if (auto Err = setupTemplateFiles(CDCtx))
85+
return Err;
86+
// Track which directories we already tried to create.
87+
StringSet<> CreatedDirs;
88+
// Collect all output by file name and create the necessary directories.
89+
StringMap<std::vector<doc::Info *>> FileToInfos;
90+
for (const auto &Group : Infos) {
91+
doc::Info *Info = Group.getValue().get();
92+
93+
SmallString<128> Path;
94+
sys::path::native(RootDir, Path);
95+
sys::path::append(Path, Info->getRelativeFilePath(""));
96+
if (!CreatedDirs.contains(Path)) {
97+
if (std::error_code EC = sys::fs::create_directories(Path))
98+
return createStringError(EC, "failed to create directory '%s'.",
99+
Path.c_str());
100+
CreatedDirs.insert(Path);
101+
}
102+
103+
sys::path::append(Path, Info->getFileBaseName() + ".html");
104+
FileToInfos[Path].push_back(Info);
105+
}
106+
107+
for (const auto &Group : FileToInfos) {
108+
std::error_code FileErr;
109+
raw_fd_ostream InfoOS(Group.getKey(), FileErr, sys::fs::OF_None);
110+
if (FileErr)
111+
return createFileOpenError(Group.getKey(), FileErr);
112+
113+
for (const auto &Info : Group.getValue())
114+
if (Error Err = generateDocForInfo(Info, InfoOS, CDCtx))
115+
return Err;
116+
}
76117
return Error::success();
77118
}
78119

120+
static json::Value extractValue(const NamespaceInfo &I,
121+
const ClangDocContext &CDCtx) {
122+
Object NamespaceValue = Object();
123+
return NamespaceValue;
124+
}
125+
126+
static json::Value extractValue(const RecordInfo &I,
127+
const ClangDocContext &CDCtx) {
128+
Object RecordValue = Object();
129+
return RecordValue;
130+
}
131+
132+
static Error setupTemplateValue(const ClangDocContext &CDCtx, json::Value &V,
133+
Info *I) {
134+
return createStringError(inconvertibleErrorCode(),
135+
"setupTemplateValue is unimplemented");
136+
}
137+
79138
Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
80139
const ClangDocContext &CDCtx) {
140+
switch (I->IT) {
141+
case InfoType::IT_namespace: {
142+
json::Value V =
143+
extractValue(*static_cast<clang::doc::NamespaceInfo *>(I), CDCtx);
144+
if (auto Err = setupTemplateValue(CDCtx, V, I))
145+
return Err;
146+
NamespaceTemplate->render(V, OS);
147+
break;
148+
}
149+
case InfoType::IT_record: {
150+
json::Value V =
151+
extractValue(*static_cast<clang::doc::RecordInfo *>(I), CDCtx);
152+
if (auto Err = setupTemplateValue(CDCtx, V, I))
153+
return Err;
154+
// Serialize the JSON value to the output stream in a readable format.
155+
RecordTemplate->render(V, OS);
156+
break;
157+
}
158+
case InfoType::IT_enum:
159+
OS << "IT_enum\n";
160+
break;
161+
case InfoType::IT_function:
162+
OS << "IT_Function\n";
163+
break;
164+
case InfoType::IT_typedef:
165+
OS << "IT_typedef\n";
166+
break;
167+
case InfoType::IT_default:
168+
return createStringError(inconvertibleErrorCode(), "unexpected InfoType");
169+
}
81170
return Error::success();
82171
}
83172

84173
Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
174+
for (const auto &FilePath : CDCtx.UserStylesheets)
175+
if (Error Err = copyFile(FilePath, CDCtx.OutDirectory))
176+
return Err;
177+
for (const auto &FilePath : CDCtx.JsScripts)
178+
if (Error Err = copyFile(FilePath, CDCtx.OutDirectory))
179+
return Err;
85180
return Error::success();
86181
}
87182

clang-tools-extra/unittests/clang-doc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ clang_target_link_libraries(ClangDocTests
3434
clangTooling
3535
clangToolingCore
3636
)
37+
3738
target_link_libraries(ClangDocTests
3839
PRIVATE
3940
clangDoc

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#include "Generators.h"
1111
#include "Representation.h"
1212
#include "clang/Basic/Version.h"
13+
#include "llvm/Support/Path.h"
1314
#include "llvm/Testing/Support/Error.h"
15+
#include "llvm/Testing/Support/SupportHelpers.h"
1416
#include "gmock/gmock.h"
1517
#include "gtest/gtest.h"
1618

@@ -40,13 +42,43 @@ getClangDocContext(std::vector<std::string> UserStylesheets = {},
4042
return CDCtx;
4143
}
4244

45+
static void verifyFileContents(const Twine &Path, StringRef Contents) {
46+
auto Buffer = MemoryBuffer::getFile(Path);
47+
ASSERT_TRUE((bool)Buffer);
48+
StringRef Data = Buffer.get()->getBuffer();
49+
ASSERT_EQ(Data, Contents);
50+
}
51+
4352
TEST(HTMLMustacheGeneratorTest, createResources) {
4453
auto G = getHTMLMustacheGenerator();
4554
ASSERT_THAT(G, NotNull()) << "Could not find HTMLMustacheGenerator";
4655
ClangDocContext CDCtx = getClangDocContext();
56+
EXPECT_THAT_ERROR(G->createResources(CDCtx), Failed())
57+
<< "Empty UserStylesheets or JsScripts should fail!";
58+
59+
unittest::TempDir RootTestDirectory("createResourcesTest", /*Unique=*/true);
60+
CDCtx.OutDirectory = RootTestDirectory.path();
61+
62+
unittest::TempFile CSS("clang-doc-mustache", "css", "CSS");
63+
unittest::TempFile JS("mustache", "js", "JavaScript");
64+
65+
CDCtx.UserStylesheets[0] = CSS.path();
66+
CDCtx.JsScripts[0] = JS.path();
4767

4868
EXPECT_THAT_ERROR(G->createResources(CDCtx), Succeeded())
49-
<< "Failed to create resources.";
69+
<< "Failed to create resources with valid UserStylesheets and JsScripts";
70+
{
71+
SmallString<256> PathBuf;
72+
llvm::sys::path::append(PathBuf, RootTestDirectory.path(),
73+
"clang-doc-mustache.css");
74+
verifyFileContents(PathBuf, "CSS");
75+
}
76+
77+
{
78+
SmallString<256> PathBuf;
79+
llvm::sys::path::append(PathBuf, RootTestDirectory.path(), "mustache.js");
80+
verifyFileContents(PathBuf, "JavaScript");
81+
}
5082
}
5183

5284
TEST(HTMLMustacheGeneratorTest, generateDocs) {
@@ -79,8 +111,7 @@ TEST(HTMLMustacheGeneratorTest, generateDocsForInfo) {
79111
I.Children.Functions.back().Name = "OneFunction";
80112
I.Children.Enums.emplace_back();
81113

82-
EXPECT_THAT_ERROR(G->generateDocForInfo(&I, Actual, CDCtx), Succeeded())
83-
<< "Failed to generate docs.";
114+
EXPECT_THAT_ERROR(G->generateDocForInfo(&I, Actual, CDCtx), Failed());
84115

85116
std::string Expected = R"raw()raw";
86117
EXPECT_THAT(Actual.str(), Eq(Expected));

0 commit comments

Comments
 (0)