1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
///===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file contains the implementation of the MustacheHTMLGenerator class,
/// which is Clang-Doc generator for HTML using Mustache templates.
///
//===----------------------------------------------------------------------===//
#include "Generators.h"
#include "Representation.h"
#include "support/File.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Mustache.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TimeProfiler.h"
using namespace llvm;
using namespace llvm::json;
using namespace llvm::mustache;
namespace clang {
namespace doc {
static Error generateDocForJSON(json::Value &JSON, StringRef Filename,
StringRef Path, raw_fd_ostream &OS,
const ClangDocContext &CDCtx);
static Error createFileOpenError(StringRef FileName, std::error_code EC) {
return createFileError("cannot open file " + FileName, EC);
}
class MustacheHTMLGenerator : public Generator {
public:
static const char *Format;
Error generateDocs(StringRef RootDir,
StringMap<std::unique_ptr<doc::Info>> Infos,
const ClangDocContext &CDCtx) override;
Error createResources(ClangDocContext &CDCtx) override;
Error generateDocForInfo(Info *I, raw_ostream &OS,
const ClangDocContext &CDCtx) override;
};
class MustacheTemplateFile : public Template {
public:
static Expected<std::unique_ptr<MustacheTemplateFile>>
createMustacheFile(StringRef FileName) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
MemoryBuffer::getFile(FileName);
if (auto EC = BufferOrError.getError())
return createFileOpenError(FileName, EC);
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
StringRef FileContent = Buffer->getBuffer();
return std::make_unique<MustacheTemplateFile>(FileContent);
}
Error registerPartialFile(StringRef Name, StringRef FileName) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
MemoryBuffer::getFile(FileName);
if (auto EC = BufferOrError.getError())
return createFileOpenError(FileName, EC);
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
StringRef FileContent = Buffer->getBuffer();
registerPartial(Name.str(), FileContent.str());
return Error::success();
}
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
};
static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
static Error
setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
StringRef TemplatePath,
std::vector<std::pair<StringRef, StringRef>> Partials) {
auto T = MustacheTemplateFile::createMustacheFile(TemplatePath);
if (Error Err = T.takeError())
return Err;
Template = std::move(T.get());
for (const auto &[Name, FileName] : Partials)
if (auto Err = Template->registerPartialFile(Name, FileName))
return Err;
return Error::success();
}
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
// Template files need to use the native path when they're opened,
// but have to be used in POSIX style when used in HTML.
auto ConvertToNative = [](std::string &&Path) -> std::string {
SmallString<128> PathBuf(Path);
llvm::sys::path::native(PathBuf);
return PathBuf.str().str();
};
std::string NamespaceFilePath =
ConvertToNative(CDCtx.MustacheTemplates.lookup("namespace-template"));
std::string ClassFilePath =
ConvertToNative(CDCtx.MustacheTemplates.lookup("class-template"));
std::string CommentFilePath =
ConvertToNative(CDCtx.MustacheTemplates.lookup("comment-template"));
std::string FunctionFilePath =
ConvertToNative(CDCtx.MustacheTemplates.lookup("function-template"));
std::string EnumFilePath =
ConvertToNative(CDCtx.MustacheTemplates.lookup("enum-template"));
std::vector<std::pair<StringRef, StringRef>> Partials = {
{"Comments", CommentFilePath},
{"FunctionPartial", FunctionFilePath},
{"EnumPartial", EnumFilePath}};
if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
return Err;
if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
return Err;
return Error::success();
}
Error MustacheHTMLGenerator::generateDocs(
StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
const clang::doc::ClangDocContext &CDCtx) {
{
llvm::TimeTraceScope TS("Setup Templates");
if (auto Err = setupTemplateFiles(CDCtx))
return Err;
}
{
llvm::TimeTraceScope TS("Generate JSON for Mustache");
if (auto JSONGenerator = findGeneratorByName("json")) {
if (Error Err = JSONGenerator.get()->generateDocs(
RootDir, std::move(Infos), CDCtx))
return Err;
} else
return JSONGenerator.takeError();
}
StringMap<json::Value> JSONFileMap;
{
llvm::TimeTraceScope TS("Iterate JSON files");
std::error_code EC;
sys::fs::directory_iterator JSONIter(RootDir, EC);
std::vector<json::Value> JSONFiles;
JSONFiles.reserve(Infos.size());
if (EC)
return createStringError("Failed to create directory iterator.");
while (JSONIter != sys::fs::directory_iterator()) {
if (EC)
return createFileError("Failed to iterate: " + JSONIter->path(), EC);
auto Path = StringRef(JSONIter->path());
if (!Path.ends_with(".json")) {
JSONIter.increment(EC);
continue;
}
auto File = MemoryBuffer::getFile(Path);
if (EC = File.getError(); EC)
// TODO: Buffer errors to report later, look into using Clang
// diagnostics.
llvm::errs() << "Failed to open file: " << Path << " " << EC.message()
<< '\n';
auto Parsed = json::parse((*File)->getBuffer());
if (!Parsed)
return Parsed.takeError();
std::error_code FileErr;
SmallString<16> HTMLPath(Path.begin(), Path.end());
sys::path::replace_extension(HTMLPath, "html");
raw_fd_ostream InfoOS(HTMLPath, FileErr, sys::fs::OF_None);
if (FileErr)
return createFileOpenError(Path, FileErr);
if (Error Err = generateDocForJSON(*Parsed, sys::path::stem(HTMLPath),
HTMLPath, InfoOS, CDCtx))
return Err;
JSONIter.increment(EC);
}
}
return Error::success();
}
static Error setupTemplateValue(const ClangDocContext &CDCtx, json::Value &V) {
V.getAsObject()->insert({"ProjectName", CDCtx.ProjectName});
json::Value StylesheetArr = Array();
SmallString<128> RelativePath("./");
sys::path::native(RelativePath, sys::path::Style::posix);
auto *SSA = StylesheetArr.getAsArray();
SSA->reserve(CDCtx.UserStylesheets.size());
for (const auto &FilePath : CDCtx.UserStylesheets) {
SmallString<128> StylesheetPath = RelativePath;
sys::path::append(StylesheetPath, sys::path::Style::posix,
sys::path::filename(FilePath));
SSA->emplace_back(StylesheetPath);
}
V.getAsObject()->insert({"Stylesheets", StylesheetArr});
json::Value ScriptArr = Array();
auto *SCA = ScriptArr.getAsArray();
SCA->reserve(CDCtx.JsScripts.size());
for (auto Script : CDCtx.JsScripts) {
SmallString<128> JsPath = RelativePath;
sys::path::append(JsPath, sys::path::Style::posix,
sys::path::filename(Script));
SCA->emplace_back(JsPath);
}
V.getAsObject()->insert({"Scripts", ScriptArr});
return Error::success();
}
static Error generateDocForJSON(json::Value &JSON, StringRef Filename,
StringRef Path, raw_fd_ostream &OS,
const ClangDocContext &CDCtx) {
auto StrValue = (*JSON.getAsObject())["InfoType"];
if (StrValue.kind() != json::Value::Kind::String)
return createStringError("JSON file '%s' does not contain key: 'InfoType'.",
Filename.str().c_str());
auto ObjTypeStr = StrValue.getAsString();
if (!ObjTypeStr.has_value())
return createStringError(
"JSON file '%s' does not contain 'InfoType' field as a string.",
Filename.str().c_str());
if (ObjTypeStr.value() == "namespace") {
if (auto Err = setupTemplateValue(CDCtx, JSON))
return Err;
assert(NamespaceTemplate && "NamespaceTemplate is nullptr.");
NamespaceTemplate->render(JSON, OS);
} else if (ObjTypeStr.value() == "record") {
if (auto Err = setupTemplateValue(CDCtx, JSON))
return Err;
assert(RecordTemplate && "RecordTemplate is nullptr.");
RecordTemplate->render(JSON, OS);
}
return Error::success();
}
Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
const ClangDocContext &CDCtx) {
switch (I->IT) {
case InfoType::IT_enum:
case InfoType::IT_function:
case InfoType::IT_typedef:
case InfoType::IT_namespace:
case InfoType::IT_record:
case InfoType::IT_concept:
case InfoType::IT_variable:
case InfoType::IT_friend:
break;
case InfoType::IT_default:
return createStringError(inconvertibleErrorCode(), "unexpected InfoType");
}
return Error::success();
}
Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) {
for (const auto &FilePath : CDCtx.UserStylesheets)
if (Error Err = copyFile(FilePath, CDCtx.OutDirectory))
return Err;
for (const auto &FilePath : CDCtx.JsScripts)
if (Error Err = copyFile(FilePath, CDCtx.OutDirectory))
return Err;
return Error::success();
}
const char *MustacheHTMLGenerator::Format = "mustache";
static GeneratorRegistry::Add<MustacheHTMLGenerator>
MHTML(MustacheHTMLGenerator::Format, "Generator for mustache HTML output.");
// This anchor is used to force the linker to link in the generated object
// file and thus register the generator.
volatile int MHTMLGeneratorAnchorSource = 0;
} // namespace doc
} // namespace clang
|