Skip to content

Commit 10fe47d

Browse files
ilovepiPeterChou1
andcommitted
[clang-doc] Add HTMLMustacheGenerator methods
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 5773942 commit 10fe47d

File tree

3 files changed

+137
-3
lines changed

3 files changed

+137
-3
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,120 @@ class MustacheTemplateFile : public Template {
5757
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
5858
};
5959

60+
static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
61+
62+
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
63+
64+
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
65+
return Error::success();
66+
}
67+
6068
Error MustacheHTMLGenerator::generateDocs(
6169
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
6270
const clang::doc::ClangDocContext &CDCtx) {
71+
if (auto Err = setupTemplateFiles(CDCtx))
72+
return Err;
73+
// Track which directories we already tried to create.
74+
StringSet<> CreatedDirs;
75+
// Collect all output by file name and create the necessary directories.
76+
StringMap<std::vector<doc::Info *>> FileToInfos;
77+
for (const auto &Group : Infos) {
78+
doc::Info *Info = Group.getValue().get();
79+
80+
SmallString<128> Path;
81+
sys::path::native(RootDir, Path);
82+
sys::path::append(Path, Info->getRelativeFilePath(""));
83+
if (!CreatedDirs.contains(Path)) {
84+
if (std::error_code Err = sys::fs::create_directories(Path);
85+
Err != std::error_code())
86+
return createStringError(Err, "Failed to create directory '%s'.",
87+
Path.c_str());
88+
CreatedDirs.insert(Path);
89+
}
90+
91+
sys::path::append(Path, Info->getFileBaseName() + ".html");
92+
FileToInfos[Path].push_back(Info);
93+
}
94+
95+
for (const auto &Group : FileToInfos) {
96+
std::error_code FileErr;
97+
raw_fd_ostream InfoOS(Group.getKey(), FileErr, sys::fs::OF_None);
98+
if (FileErr)
99+
return createStringError(FileErr, "Error opening file '%s'",
100+
Group.getKey().data());
101+
102+
for (const auto &Info : Group.getValue()) {
103+
if (Error Err = generateDocForInfo(Info, InfoOS, CDCtx))
104+
return Err;
105+
}
106+
}
63107
return Error::success();
64108
}
65109

110+
static json::Value extractValue(const NamespaceInfo &I,
111+
const ClangDocContext &CDCtx) {
112+
Object NamespaceValue = Object();
113+
return NamespaceValue;
114+
}
115+
116+
static json::Value extractValue(const RecordInfo &I,
117+
const ClangDocContext &CDCtx) {
118+
Object RecordValue = Object();
119+
return RecordValue;
120+
}
121+
122+
static Error setupTemplateValue(const ClangDocContext &CDCtx, json::Value &V,
123+
Info *I) {
124+
return createStringError(inconvertibleErrorCode(),
125+
"setupTemplateValue is unimplemented");
126+
}
127+
66128
Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
67129
const ClangDocContext &CDCtx) {
130+
switch (I->IT) {
131+
case InfoType::IT_namespace: {
132+
json::Value V =
133+
extractValue(*static_cast<clang::doc::NamespaceInfo *>(I), CDCtx);
134+
if (auto Err = setupTemplateValue(CDCtx, V, I))
135+
return Err;
136+
NamespaceTemplate->render(V, OS);
137+
break;
138+
}
139+
case InfoType::IT_record: {
140+
json::Value V =
141+
extractValue(*static_cast<clang::doc::RecordInfo *>(I), CDCtx);
142+
if (auto Err = setupTemplateValue(CDCtx, V, I))
143+
return Err;
144+
// Serialize the JSON value to the output stream in a readable format.
145+
outs() << "Visit: " << I->Name << "\n";
146+
// outs() << formatv("{0:2}", V) << "\n";
147+
RecordTemplate->render(V, outs());
148+
break;
149+
}
150+
case InfoType::IT_enum:
151+
outs() << "IT_enum\n";
152+
break;
153+
case InfoType::IT_function:
154+
outs() << "IT_Function\n";
155+
break;
156+
case InfoType::IT_typedef:
157+
outs() << "IT_typedef\n";
158+
break;
159+
case InfoType::IT_default:
160+
return createStringError(inconvertibleErrorCode(), "unexpected InfoType");
161+
}
68162
return Error::success();
69163
}
70164

71165
Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
166+
for (const auto &FilePath : CDCtx.UserStylesheets) {
167+
if (Error Err = copyFile(FilePath, CDCtx.OutDirectory))
168+
return Err;
169+
}
170+
for (const auto &FilePath : CDCtx.JsScripts) {
171+
if (Error Err = copyFile(FilePath, CDCtx.OutDirectory))
172+
return Err;
173+
}
72174
return Error::success();
73175
}
74176

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> PathBuff;
72+
llvm::sys::path::append(PathBuff, RootTestDirectory.path(),
73+
"clang-doc-mustache.css");
74+
verifyFileContents(PathBuff, "CSS");
75+
}
76+
77+
{
78+
SmallString<256> PathBuff;
79+
llvm::sys::path::append(PathBuff, RootTestDirectory.path(), "mustache.js");
80+
verifyFileContents(PathBuff, "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)