Skip to content

Commit ba7721e

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

File tree

7 files changed

+195
-2
lines changed

7 files changed

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,9 @@ struct ClangDocContext {
537537
std::vector<std::string> JsScripts;
538538
// Base directory for remote repositories.
539539
StringRef Base;
540+
// Maps mustache template types to specific mustache template files.
541+
// Ex. comment-template -> /path/to/comment-template.mustache
542+
llvm::StringMap<std::string> MustacheTemplates;
540543
Index Idx;
541544
};
542545

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)