Skip to content

Commit 6098ca3

Browse files
ilovepiPeterChou1
andcommitted
[clang-doc] Add HTMLMustacheGenerator.cpp
Split from #133161. This patch adds HTMLMustacheGenerator.cpp, and the most basic class defintion for the generator. Future patches will add functionality. Co-authored-by: Peter Chou <[email protected]>
1 parent 162890b commit 6098ca3

File tree

7 files changed

+181
-2
lines changed

7 files changed

+181
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_clang_library(clangDoc STATIC
1616
Representation.cpp
1717
Serialize.cpp
1818
YAMLGenerator.cpp
19+
HTMLMustacheGenerator.cpp
1920

2021
DEPENDS
2122
omp_gen
@@ -24,7 +25,7 @@ add_clang_library(clangDoc STATIC
2425

2526
clang_target_link_libraries(clangDoc
2627
PRIVATE
27-
clangDocSupport
28+
clangDocSupport
2829
clangAnalysis
2930
clangAST
3031
clangASTMatchers

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ static int LLVM_ATTRIBUTE_UNUSED MDGeneratorAnchorDest =
103103
MDGeneratorAnchorSource;
104104
static int LLVM_ATTRIBUTE_UNUSED HTMLGeneratorAnchorDest =
105105
HTMLGeneratorAnchorSource;
106-
106+
static int LLVM_ATTRIBUTE_UNUSED MHTMLGeneratorAnchorDest =
107+
MHTMLGeneratorAnchorSource;
107108
} // namespace doc
108109
} // namespace clang

clang-tools-extra/clang-doc/Generators.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ std::string getTagType(TagTypeKind AS);
5757
extern volatile int YAMLGeneratorAnchorSource;
5858
extern volatile int MDGeneratorAnchorSource;
5959
extern volatile int HTMLGeneratorAnchorSource;
60+
extern volatile int MHTMLGeneratorAnchorSource;
6061

6162
} // namespace doc
6263
} // namespace clang
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//===-- HTMLMustacheGenerator.cpp - HTML Mustache Generator -----*- C++ -*-===//
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+
#include "Generators.h"
9+
#include "Representation.h"
10+
#include "support/File.h"
11+
#include "llvm/Support/MemoryBuffer.h"
12+
#include "llvm/Support/Mustache.h"
13+
14+
using namespace llvm;
15+
using namespace llvm::json;
16+
using namespace llvm::mustache;
17+
18+
namespace clang {
19+
namespace doc {
20+
21+
class MustacheHTMLGenerator : public Generator {
22+
public:
23+
static const char *Format;
24+
Error generateDocs(StringRef RootDir,
25+
StringMap<std::unique_ptr<doc::Info>> Infos,
26+
const ClangDocContext &CDCtx) override;
27+
Error createResources(ClangDocContext &CDCtx) override;
28+
Error generateDocForInfo(Info *I, raw_ostream &OS,
29+
const ClangDocContext &CDCtx) override;
30+
};
31+
32+
class MustacheTemplateFile : public Template {
33+
public:
34+
static ErrorOr<std::unique_ptr<MustacheTemplateFile>>
35+
createMustacheFile(StringRef FileName) {
36+
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
37+
MemoryBuffer::getFile(FileName);
38+
if (auto EC = BufferOrError.getError())
39+
return EC;
40+
41+
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
42+
StringRef FileContent = Buffer->getBuffer();
43+
return std::make_unique<MustacheTemplateFile>(FileContent);
44+
}
45+
46+
Error registerPartialFile(StringRef Name, StringRef FileName) {
47+
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
48+
MemoryBuffer::getFile(FileName);
49+
if (auto EC = BufferOrError.getError())
50+
return createFileError("cannot open file", EC);
51+
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
52+
StringRef FileContent = Buffer->getBuffer();
53+
registerPartial(Name.str(), FileContent.str());
54+
return Error::success();
55+
}
56+
57+
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
58+
};
59+
60+
Error MustacheHTMLGenerator::generateDocs(
61+
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
62+
const clang::doc::ClangDocContext &CDCtx) {
63+
return Error::success();
64+
}
65+
66+
Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
67+
const ClangDocContext &CDCtx) {
68+
return Error::success();
69+
}
70+
71+
Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
72+
return Error::success();
73+
}
74+
75+
const char *MustacheHTMLGenerator::Format = "mhtml";
76+
77+
static GeneratorRegistry::Add<MustacheHTMLGenerator>
78+
MHTML(MustacheHTMLGenerator::Format, "Generator for mustache HTML output.");
79+
80+
// This anchor is used to force the linker to link in the generated object
81+
// file and thus register the generator.
82+
volatile int MHTMLGeneratorAnchorSource = 0;
83+
84+
} // namespace doc
85+
} // namespace clang

clang-tools-extra/clang-doc/Representation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,8 @@ struct ClangDocContext {
535535
// JavaScript files that will be imported in all HTML files.
536536
std::vector<std::string> JsScripts;
537537
StringRef Base;
538+
// Mustache Template files
539+
llvm::StringMap<std::string> MustacheTemplates;
538540
Index Idx;
539541
};
540542

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_extra_unittest(ClangDocTests
1515
ClangDocTest.cpp
1616
GeneratorTest.cpp
1717
HTMLGeneratorTest.cpp
18+
HTMLMustacheGeneratorTest.cpp
1819
MDGeneratorTest.cpp
1920
MergeTest.cpp
2021
SerializeTest.cpp
@@ -36,4 +37,5 @@ clang_target_link_libraries(ClangDocTests
3637
target_link_libraries(ClangDocTests
3738
PRIVATE
3839
clangDoc
40+
LLVMTestingSupport
3941
)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//===-- clang-doc/HTMLMustacheGeneratorTest.cpp ---------------------------===//
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 "ClangDocTest.h"
10+
#include "Generators.h"
11+
#include "Representation.h"
12+
#include "clang/Basic/Version.h"
13+
#include "llvm/Testing/Support/Error.h"
14+
#include "gmock/gmock.h"
15+
#include "gtest/gtest.h"
16+
17+
using namespace llvm;
18+
using namespace testing;
19+
using namespace clang::doc;
20+
21+
static const std::string ClangDocVersion =
22+
clang::getClangToolFullVersion("clang-doc");
23+
24+
static std::unique_ptr<Generator> getHTMLMustacheGenerator() {
25+
auto G = findGeneratorByName("mhtml");
26+
if (!G)
27+
return nullptr;
28+
return std::move(G.get());
29+
}
30+
31+
static ClangDocContext
32+
getClangDocContext(std::vector<std::string> UserStylesheets = {},
33+
StringRef RepositoryUrl = "",
34+
StringRef RepositoryLinePrefix = "", StringRef Base = "") {
35+
ClangDocContext CDCtx{
36+
{}, "test-project", {}, {}, {}, RepositoryUrl, RepositoryLinePrefix,
37+
Base, UserStylesheets};
38+
CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(), "");
39+
CDCtx.JsScripts.emplace_back("");
40+
return CDCtx;
41+
}
42+
43+
TEST(HTMLMustacheGeneratorTest, createResources) {
44+
auto G = getHTMLMustacheGenerator();
45+
ASSERT_THAT(G, NotNull()) << "Could not find HTMLMustacheGenerator";
46+
ClangDocContext CDCtx = getClangDocContext();
47+
48+
EXPECT_THAT_ERROR(G->createResources(CDCtx), Succeeded())
49+
<< "Failed to create resources.";
50+
}
51+
52+
TEST(HTMLMustacheGeneratorTest, generateDocs) {
53+
auto G = getHTMLMustacheGenerator();
54+
assert(G && "Could not find HTMLMustacheGenerator");
55+
ClangDocContext CDCtx = getClangDocContext();
56+
57+
StringRef RootDir = "";
58+
EXPECT_THAT_ERROR(G->generateDocs(RootDir, {}, CDCtx), Succeeded())
59+
<< "Failed to generate docs.";
60+
}
61+
62+
TEST(HTMLMustacheGeneratorTest, generateDocsForInfo) {
63+
auto G = getHTMLMustacheGenerator();
64+
assert(G && "Could not find HTMLMustacheGenerator");
65+
ClangDocContext CDCtx = getClangDocContext();
66+
std::string Buffer;
67+
llvm::raw_string_ostream Actual(Buffer);
68+
NamespaceInfo I;
69+
I.Name = "Namespace";
70+
I.Namespace.emplace_back(EmptySID, "A", InfoType::IT_namespace);
71+
72+
I.Children.Namespaces.emplace_back(EmptySID, "ChildNamespace",
73+
InfoType::IT_namespace,
74+
"Namespace::ChildNamespace", "Namespace");
75+
I.Children.Records.emplace_back(EmptySID, "ChildStruct", InfoType::IT_record,
76+
"Namespace::ChildStruct", "Namespace");
77+
I.Children.Functions.emplace_back();
78+
I.Children.Functions.back().Access = clang::AccessSpecifier::AS_none;
79+
I.Children.Functions.back().Name = "OneFunction";
80+
I.Children.Enums.emplace_back();
81+
82+
EXPECT_THAT_ERROR(G->generateDocForInfo(&I, Actual, CDCtx), Succeeded())
83+
<< "Failed to generate docs.";
84+
85+
std::string Expected = R"raw()raw";
86+
EXPECT_THAT(Actual.str(), Eq(Expected));
87+
}

0 commit comments

Comments
 (0)