clang-tools 24.0.0git
MDGenerator.cpp
Go to the documentation of this file.
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 MDGenerator, which
11/// generates documentation in Markdown format using Mustache templates. It
12/// defines how the structured data in Info objects is mapped to template
13/// tags to produce readable markdown documents.
14///
15//===----------------------------------------------------------------------===//
16
17#include "Generators.h"
18
19using namespace llvm;
20using namespace clang::doc;
21
22static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
23
24static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
25
26static std::unique_ptr<MustacheTemplateFile> AllFilesTemplate = nullptr;
27
28static std::unique_ptr<MustacheTemplateFile> IndexTemplate = nullptr;
29
30namespace {
31struct MDGenerator : public MustacheGenerator {
32 static const char *Format;
33 Error generateDocumentation(StringRef RootDir, StringMap<Info *> Infos,
34 const ClangDocContext &CDCtx,
35 std::string DirName) override;
36 Error setupTemplateFiles(const ClangDocContext &CDCtx) override;
37 Error generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
38 const ClangDocContext &CDCtx,
39 StringRef ObjectTypeStr,
40 StringRef RelativeRootPath) override;
41 // This generator doesn't need this function, but it inherits from the
42 // original generator interface.
43 Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
44 const ClangDocContext &CDCtx) override;
45};
46} // namespace
47
48Error MDGenerator::setupTemplateFiles(const ClangDocContext &CDCtx) {
49 std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
50 std::string NamespaceFilePath =
51 CDCtx.MustacheTemplates.lookup("namespace-template");
52 std::string AllFilesPath = CDCtx.MustacheTemplates.lookup("all-files");
53 std::string IndexFilePath = CDCtx.MustacheTemplates.lookup("index");
54 std::string CommentsFilePath = CDCtx.MustacheTemplates.lookup("comments");
55 std::vector<std::pair<StringRef, StringRef>> Partials = {
56 {"Comments", CommentsFilePath}};
57
58 if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
59 return Err;
60 if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
61 return Err;
62 if (Error Err = setupTemplate(AllFilesTemplate, AllFilesPath, Partials))
63 return Err;
64 if (Error Err = setupTemplate(IndexTemplate, IndexFilePath, Partials))
65 return Err;
66
67 // Override the default HTML Mustache escape characters. We don't need to
68 // override `<` here.
69 static const DenseMap<char, std::string> EscapeChars;
70 RecordTemplate->setEscapeCharacters(EscapeChars);
71 NamespaceTemplate->setEscapeCharacters(EscapeChars);
72 AllFilesTemplate->setEscapeCharacters(EscapeChars);
73 IndexTemplate->setEscapeCharacters(EscapeChars);
74
75 return Error::success();
76}
77
78Error MDGenerator::generateDocumentation(
79 StringRef RootDir, StringMap<Info *> Infos,
80 const clang::doc::ClangDocContext &CDCtx, std::string Dirname) {
81 return MustacheGenerator::generateDocumentation(RootDir, std::move(Infos),
82 CDCtx, "md");
83}
84
85Error MDGenerator::generateDocForJSON(json::Value &JSON, raw_fd_ostream &OS,
86 const ClangDocContext &CDCtx,
87 StringRef ObjTypeStr,
88 StringRef RelativeRootPath) {
89 if (ObjTypeStr == "record") {
90 assert(RecordTemplate && "RecordTemplate is nullptr.");
91 RecordTemplate->render(JSON, OS);
92 } else if (ObjTypeStr == "namespace") {
93 assert(NamespaceTemplate && "NamespaceTemplate is nullptr.");
94 NamespaceTemplate->render(JSON, OS);
95 } else if (ObjTypeStr == "all_files") {
96 assert(AllFilesTemplate && "AllFilesTemplate is nullptr.");
97 AllFilesTemplate->render(JSON, OS);
98 } else if (ObjTypeStr == "index") {
99 assert(IndexTemplate && "IndexTemplate is nullptr");
100 IndexTemplate->render(JSON, OS);
101 }
102 return Error::success();
103}
104
105Error MDGenerator::generateDocForInfo(Info *I, raw_ostream &OS,
106 const ClangDocContext &CDCtx) {
107 return Error::success();
108}
109
110const char *MDGenerator::Format = "md";
111
112static GeneratorRegistry::Add<MDGenerator> MD(MDGenerator::Format,
113 "Generator for Markdown output.");
114
115namespace clang {
116namespace doc {
117volatile int MDGeneratorAnchorSource = 0;
118} // namespace doc
119} // namespace clang
static GeneratorRegistry::Add< JSONGenerator > JSON(JSONGenerator::Format, "Generator for JSON output.")
static std::unique_ptr< MustacheTemplateFile > RecordTemplate
static std::unique_ptr< MustacheTemplateFile > NamespaceTemplate
static std::unique_ptr< MustacheTemplateFile > IndexTemplate
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for Markdown output.")
static std::unique_ptr< MustacheTemplateFile > AllFilesTemplate
@ Error
An error message.
Definition Protocol.h:751
volatile int MDGeneratorAnchorSource
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
Definition Generators.h:150
llvm::StringMap< std::string > MustacheTemplates
A base struct for Infos.
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< doc::Info * > Infos, const clang::doc::ClangDocContext &CDCtx, std::string DirName) override
The main orchestrator for Mustache-based documentation.