11#include "llvm/Support/TimeProfiler.h"
16using namespace llvm::json;
17using namespace llvm::mustache;
22llvm::Expected<std::unique_ptr<Generator>>
24 for (
const auto &
Generator : GeneratorRegistry::entries()) {
29 return createStringError(llvm::inconvertibleErrorCode(),
30 "can't find generator: " + Format);
37 case TagTypeKind::Class:
39 case TagTypeKind::Union:
41 case TagTypeKind::Interface:
43 case TagTypeKind::Struct:
45 case TagTypeKind::Enum:
48 llvm_unreachable(
"Unknown TagTypeKind");
52 return createFileError(
"cannot open file " + FileName, EC);
56 std::unique_ptr<MustacheTemplateFile> &Template, StringRef TemplatePath,
57 std::vector<std::pair<StringRef, StringRef>> Partials) {
59 if (Error Err = T.takeError())
61 Template = std::move(T.get());
62 for (
const auto &[Name, FileName] : Partials)
63 if (
auto Err = Template->registerPartialFile(Name, FileName))
65 return Error::success();
69 StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos,
72 llvm::TimeTraceScope TS(
"Setup Templates");
78 llvm::TimeTraceScope TS(
"Generate JSON for Mustache");
81 RootDir, std::move(Infos), CDCtx))
87 SmallString<128> JSONPath;
88 sys::path::native(RootDir.str() +
"/json", JSONPath);
91 llvm::TimeTraceScope TS(
"Iterate JSON files");
93 sys::fs::recursive_directory_iterator JSONIter(JSONPath, EC);
94 std::vector<json::Value> JSONFiles;
95 JSONFiles.reserve(Infos.size());
97 return createStringError(
"Failed to create directory iterator.");
99 SmallString<128> DocsDirPath(RootDir.str() +
'/' + DirName);
100 sys::path::native(DocsDirPath);
101 if (
auto EC = sys::fs::create_directories(DocsDirPath))
102 return createFileError(DocsDirPath, EC);
103 while (JSONIter != sys::fs::recursive_directory_iterator()) {
105 if (JSONIter->type() == sys::fs::file_type::directory_file) {
106 SmallString<128> DocsClonedPath(JSONIter->path());
107 sys::path::replace_path_prefix(DocsClonedPath, JSONPath, DocsDirPath);
108 if (
auto EC = sys::fs::create_directories(DocsClonedPath)) {
109 return createFileError(DocsClonedPath, EC);
114 return createFileError(
"Failed to iterate: " + JSONIter->path(), EC);
116 auto Path = StringRef(JSONIter->path());
117 if (!Path.ends_with(
".json")) {
118 JSONIter.increment(EC);
122 auto File = MemoryBuffer::getFile(Path);
123 if (EC = File.getError(); EC) {
126 llvm::errs() <<
"Failed to open file: " << Path <<
" " << EC.message()
130 auto Parsed = json::parse((*File)->getBuffer());
132 return Parsed.takeError();
133 auto ValidJSON = Parsed.get();
135 std::error_code FileErr;
136 SmallString<128> DocsFilePath(JSONIter->path());
137 sys::path::replace_path_prefix(DocsFilePath, JSONPath, DocsDirPath);
138 sys::path::replace_extension(DocsFilePath, DirName);
139 raw_fd_ostream InfoOS(DocsFilePath, FileErr, sys::fs::OF_None);
145 getInfoTypeStr(Parsed->getAsObject(), sys::path::stem(DocsFilePath));
147 return InfoTypeStr.takeError();
149 InfoTypeStr.get(), RelativeRootPath))
151 JSONIter.increment(EC);
155 return Error::success();
159 StringRef Filename) {
160 auto StrValue = (*Info)[
"InfoType"];
161 if (StrValue.kind() != json::Value::Kind::String)
162 return createStringError(
"JSON file '%s' does not contain key: 'InfoType'.",
163 Filename.str().c_str());
164 auto ObjTypeStr = StrValue.getAsString();
165 if (!ObjTypeStr.has_value())
166 return createStringError(
167 "JSON file '%s' does not contain 'InfoType' field as a string.",
168 Filename.str().c_str());
169 return ObjTypeStr.value().str();
174 StringRef DocsRootPath) {
175 SmallString<128> PathVec(PathToFile);
177 sys::path::remove_filename(PathVec);
182 return llvm::Error::success();
204 auto It = llvm::find(I->
Children, R.USR);
210 I->
Children.emplace_back(R.USR, R.Name, R.RefType, R.Path);
227 if (It->Path.empty())
229 if (It->Name.empty())
static void addInfoToIndex(Index &Idx, const doc::Info *Info)
virtual llvm::Error createResources(ClangDocContext &CDCtx)
Error generateDocumentation(StringRef RootDir, llvm::StringMap< std::unique_ptr< doc::Info > > Infos, const ClangDocContext &CDCtx, std::string DirName) override
static Expected< std::unique_ptr< MustacheTemplateFile > > createMustacheFile(StringRef FileName)
std::string getTagType(TagTypeKind AS)
llvm::Expected< std::unique_ptr< Generator > > findGeneratorByName(llvm::StringRef Format)
volatile int JSONGeneratorAnchorSource
llvm::SmallString< 128 > computeRelativePath(llvm::StringRef Destination, llvm::StringRef Origin)
static int MDGeneratorAnchorDest
static int MHTMLGeneratorAnchorDest
volatile int HTMLGeneratorAnchorSource
volatile int YAMLGeneratorAnchorSource
Error createFileOpenError(StringRef FileName, std::error_code EC)
static int JSONGeneratorAnchorDest
volatile int MDGeneratorAnchorSource
volatile int MHTMLGeneratorAnchorSource
llvm::Registry< Generator > GeneratorRegistry
static int HTMLGeneratorAnchorDest
static int YAMLGeneratorAnchorDest
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.
std::vector< Index > Children
llvm::SmallString< 128 > Path
llvm::SmallString< 16 > extractName() const
llvm::SmallVector< Reference, 4 > Namespace
llvm::Error generateDocumentation(StringRef RootDir, llvm::StringMap< std::unique_ptr< 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.