clang-tools 23.0.0git
Generators.h
Go to the documentation of this file.
1//===-- Generators.h - ClangDoc 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// Generator classes for converting declaration information into documentation
9// in a specified format.
10//===----------------------------------------------------------------------===//
11
12#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_GENERATOR_H
13#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_GENERATOR_H
14
15#include "Representation.h"
16#include "llvm/Support/Error.h"
17#include "llvm/Support/JSON.h"
18#include "llvm/Support/Mustache.h"
19#include "llvm/Support/Registry.h"
20
21namespace clang {
22namespace doc {
23
24// Abstract base class for generators.
25// This is expected to be implemented and exposed via the GeneratorRegistry.
26class Generator {
27public:
28 virtual ~Generator() = default;
29
30 // Write out the decl info for the objects in the given map in the specified
31 // format.
32 virtual llvm::Error generateDocumentation(StringRef RootDir,
33 llvm::StringMap<doc::Info *> Infos,
34 const ClangDocContext &CDCtx,
35 std::string DirName = "") = 0;
36
37 // This function writes a file with the index previously constructed.
38 // It can be overwritten by any of the inherited generators.
39 // If the override method wants to run this it should call
40 // Generator::createResources(CDCtx);
41 virtual llvm::Error createResources(ClangDocContext &CDCtx);
42
43 // Write out one specific decl info to the destination stream.
44 virtual llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS,
45 const ClangDocContext &CDCtx) = 0;
46
47 static void addInfoToIndex(Index &Idx, const doc::Info *Info);
48};
49
50typedef llvm::Registry<Generator> GeneratorRegistry;
51
52llvm::Expected<std::unique_ptr<Generator>>
53findGeneratorByName(llvm::StringRef Format);
54
55llvm::StringRef getTagType(TagTypeKind AS);
56
57llvm::Error createFileOpenError(StringRef FileName, std::error_code EC);
58
60 llvm::BumpPtrAllocator Allocator;
61 llvm::StringSaver Saver;
62 llvm::mustache::MustacheContext Ctx;
63 llvm::mustache::Template T;
64 std::unique_ptr<llvm::MemoryBuffer> Buffer;
65
66public:
67 static Expected<std::unique_ptr<MustacheTemplateFile>>
68 createMustacheFile(StringRef FileName) {
69 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrError =
70 llvm::MemoryBuffer::getFile(FileName);
71 if (auto EC = BufferOrError.getError())
72 return createFileOpenError(FileName, EC);
73 return std::make_unique<MustacheTemplateFile>(
74 std::move(BufferOrError.get()));
75 }
76
77 llvm::Error registerPartialFile(StringRef Name, StringRef FileName) {
78 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrError =
79 llvm::MemoryBuffer::getFile(FileName);
80 if (auto EC = BufferOrError.getError())
81 return createFileOpenError(FileName, EC);
82
83 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrError.get());
84 StringRef FileContent = Buffer->getBuffer();
85 T.registerPartial(Name.str(), FileContent.str());
86 return llvm::Error::success();
87 }
88
89 void render(llvm::json::Value &V, raw_ostream &OS) { T.render(V, OS); }
90
91 void setEscapeCharacters(const llvm::DenseMap<char, std::string> Characters) {
92 T.overrideEscapeCharacters(Characters);
93 }
94
95 MustacheTemplateFile(std::unique_ptr<llvm::MemoryBuffer> &&B)
96 : Saver(Allocator), Ctx(Allocator, Saver), T(B->getBuffer(), Ctx),
97 Buffer(std::move(B)) {}
98};
99
101 Expected<std::string> getInfoTypeStr(llvm::json::Object *Info,
102 StringRef Filename);
103
104 /// Used to find the relative path from the file to the format's docs root.
105 /// Mainly used for the HTML resource paths.
106 SmallString<128> getRelativePathToRoot(StringRef PathToFile,
107 StringRef DocsRootPath);
108 virtual ~MustacheGenerator() = default;
109
110 /// Initializes the template files from disk and calls setupTemplate to
111 /// register partials
112 virtual llvm::Error setupTemplateFiles(const ClangDocContext &CDCtx) = 0;
113
114 /// Populates templates with data from JSON and calls any specifics for the
115 /// format. For example, for HTML it will render the paths for CSS and JS.
116 virtual llvm::Error generateDocForJSON(llvm::json::Value &JSON,
117 llvm::raw_fd_ostream &OS,
118 const ClangDocContext &CDCtx,
119 StringRef ObjectTypeStr,
120 StringRef RelativeRootPath) = 0;
121
122 /// Registers partials to templates.
123 llvm::Error
124 setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
125 StringRef TemplatePath,
126 std::vector<std::pair<StringRef, StringRef>> Partials);
127
128 /// \brief The main orchestrator for Mustache-based documentation.
129 ///
130 /// 1. Initializes templates files from disk by calling setupTemplateFiles.
131 /// 2. Calls the JSON generator to write JSON to disk.
132 /// 3. Iterates over the JSON files, recreates the directory structure from
133 /// JSON, and calls generateDocForJSON for each file.
134 /// 4. A file of the desired format is created.
135 llvm::Error generateDocumentation(StringRef RootDir,
136 llvm::StringMap<doc::Info *> Infos,
137 const clang::doc::ClangDocContext &CDCtx,
138 std::string DirName) override;
139};
140
141// This anchor is used to force the linker to link in the generated object file
142// and thus register the generators.
143extern volatile int YAMLGeneratorAnchorSource;
144extern volatile int MDGeneratorAnchorSource;
145extern volatile int HTMLGeneratorAnchorSource;
146extern volatile int JSONGeneratorAnchorSource;
147extern volatile int MDMustacheGeneratorAnchorSource;
148
149} // namespace doc
150} // namespace clang
151
152namespace llvm {
153extern template class Registry<clang::doc::Generator>;
154} // namespace llvm
155
156#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_GENERATOR_H
virtual llvm::Error generateDocForInfo(Info *I, llvm::raw_ostream &OS, const ClangDocContext &CDCtx)=0
static void addInfoToIndex(Index &Idx, const doc::Info *Info)
virtual llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< doc::Info * > Infos, const ClangDocContext &CDCtx, std::string DirName="")=0
virtual ~Generator()=default
virtual llvm::Error createResources(ClangDocContext &CDCtx)
static Expected< std::unique_ptr< MustacheTemplateFile > > createMustacheFile(StringRef FileName)
Definition Generators.h:68
MustacheTemplateFile(std::unique_ptr< llvm::MemoryBuffer > &&B)
Definition Generators.h:95
llvm::Error registerPartialFile(StringRef Name, StringRef FileName)
Definition Generators.h:77
void setEscapeCharacters(const llvm::DenseMap< char, std::string > Characters)
Definition Generators.h:91
void render(llvm::json::Value &V, raw_ostream &OS)
Definition Generators.h:89
llvm::Expected< std::unique_ptr< Generator > > findGeneratorByName(llvm::StringRef Format)
volatile int JSONGeneratorAnchorSource
llvm::StringRef getTagType(TagTypeKind AS)
static GeneratorRegistry::Add< JSONGenerator > JSON(JSONGenerator::Format, "Generator for JSON output.")
volatile int HTMLGeneratorAnchorSource
volatile int YAMLGeneratorAnchorSource
Error createFileOpenError(StringRef FileName, std::error_code EC)
volatile int MDMustacheGeneratorAnchorSource
volatile int MDGeneratorAnchorSource
llvm::Registry< Generator > GeneratorRegistry
Definition Generators.h:50
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
Definition Generators.h:152
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.
Expected< std::string > getInfoTypeStr(llvm::json::Object *Info, StringRef Filename)
virtual llvm::Error setupTemplateFiles(const ClangDocContext &CDCtx)=0
Initializes the template files from disk and calls setupTemplate to register partials.
virtual llvm::Error generateDocForJSON(llvm::json::Value &JSON, llvm::raw_fd_ostream &OS, const ClangDocContext &CDCtx, StringRef ObjectTypeStr, StringRef RelativeRootPath)=0
Populates templates with data from JSON and calls any specifics for the format.
SmallString< 128 > getRelativePathToRoot(StringRef PathToFile, StringRef DocsRootPath)
Used to find the relative path from the file to the format's docs root.
llvm::Error setupTemplate(std::unique_ptr< MustacheTemplateFile > &Template, StringRef TemplatePath, std::vector< std::pair< StringRef, StringRef > > Partials)
Registers partials to templates.
virtual ~MustacheGenerator()=default