Skip to content

Commit 397d5dc

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 f0ae4de commit 397d5dc

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
Error MustacheHTMLGenerator::generateDocs(
60+
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
61+
const clang::doc::ClangDocContext &CDCtx) {
62+
return Error::success();
63+
}
64+
Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
65+
const ClangDocContext &CDCtx) {
66+
return Error::success();
67+
}
68+
Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
69+
Error Err = Error::success();
70+
return Error::success();
71+
}
72+
73+
const char *MustacheHTMLGenerator::Format = "mhtml";
74+
75+
static GeneratorRegistry::Add<MustacheHTMLGenerator>
76+
MHTML(MustacheHTMLGenerator::Format, "Generator for mustache HTML output.");
77+
78+
// This anchor is used to force the linker to link in the generated object
79+
// file and thus register the generator.
80+
volatile int MHTMLGeneratorAnchorSource = 0;
81+
82+
} // namespace doc
83+
} // 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

0 commit comments

Comments
 (0)