|
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
9 | 9 | #include "Generators.h" |
| 10 | +#include "support/File.h" |
| 11 | +#include "llvm/Support/TimeProfiler.h" |
10 | 12 |
|
11 | 13 | LLVM_INSTANTIATE_REGISTRY(clang::doc::GeneratorRegistry) |
12 | 14 |
|
| 15 | +using namespace llvm; |
| 16 | +using namespace llvm::json; |
| 17 | +using namespace llvm::mustache; |
| 18 | + |
13 | 19 | namespace clang { |
14 | 20 | namespace doc { |
15 | 21 |
|
@@ -42,6 +48,136 @@ std::string getTagType(TagTypeKind AS) { |
42 | 48 | llvm_unreachable("Unknown TagTypeKind"); |
43 | 49 | } |
44 | 50 |
|
| 51 | +Error createFileOpenError(StringRef FileName, std::error_code EC) { |
| 52 | + return createFileError("cannot open file " + FileName, EC); |
| 53 | +} |
| 54 | + |
| 55 | +Error MustacheGenerator::setupTemplate( |
| 56 | + std::unique_ptr<MustacheTemplateFile> &Template, StringRef TemplatePath, |
| 57 | + std::vector<std::pair<StringRef, StringRef>> Partials) { |
| 58 | + auto T = MustacheTemplateFile::createMustacheFile(TemplatePath); |
| 59 | + if (Error Err = T.takeError()) |
| 60 | + return Err; |
| 61 | + Template = std::move(T.get()); |
| 62 | + for (const auto &[Name, FileName] : Partials) |
| 63 | + if (auto Err = Template->registerPartialFile(Name, FileName)) |
| 64 | + return Err; |
| 65 | + return Error::success(); |
| 66 | +} |
| 67 | + |
| 68 | +Error MustacheGenerator::generateDocumentation( |
| 69 | + StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos, |
| 70 | + const clang::doc::ClangDocContext &CDCtx, std::string DirName) { |
| 71 | + { |
| 72 | + llvm::TimeTraceScope TS("Setup Templates"); |
| 73 | + if (auto Err = setupTemplateFiles(CDCtx)) |
| 74 | + return Err; |
| 75 | + } |
| 76 | + |
| 77 | + { |
| 78 | + llvm::TimeTraceScope TS("Generate JSON for Mustache"); |
| 79 | + if (auto JSONGenerator = findGeneratorByName("json")) { |
| 80 | + if (Error Err = JSONGenerator.get()->generateDocumentation( |
| 81 | + RootDir, std::move(Infos), CDCtx)) |
| 82 | + return Err; |
| 83 | + } else |
| 84 | + return JSONGenerator.takeError(); |
| 85 | + } |
| 86 | + |
| 87 | + SmallString<128> JSONPath; |
| 88 | + sys::path::native(RootDir.str() + "/json", JSONPath); |
| 89 | + |
| 90 | + { |
| 91 | + llvm::TimeTraceScope TS("Iterate JSON files"); |
| 92 | + std::error_code EC; |
| 93 | + sys::fs::recursive_directory_iterator JSONIter(JSONPath, EC); |
| 94 | + std::vector<json::Value> JSONFiles; |
| 95 | + JSONFiles.reserve(Infos.size()); |
| 96 | + if (EC) |
| 97 | + return createStringError("Failed to create directory iterator."); |
| 98 | + |
| 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()) { |
| 104 | + // create the same directory structure in the docs format dir |
| 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); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (EC) |
| 114 | + return createFileError("Failed to iterate: " + JSONIter->path(), EC); |
| 115 | + |
| 116 | + auto Path = StringRef(JSONIter->path()); |
| 117 | + if (!Path.ends_with(".json")) { |
| 118 | + JSONIter.increment(EC); |
| 119 | + continue; |
| 120 | + } |
| 121 | + |
| 122 | + auto File = MemoryBuffer::getFile(Path); |
| 123 | + if (EC = File.getError(); EC) { |
| 124 | + // TODO: Buffer errors to report later, look into using Clang |
| 125 | + // diagnostics. |
| 126 | + llvm::errs() << "Failed to open file: " << Path << " " << EC.message() |
| 127 | + << '\n'; |
| 128 | + } |
| 129 | + |
| 130 | + auto Parsed = json::parse((*File)->getBuffer()); |
| 131 | + if (!Parsed) |
| 132 | + return Parsed.takeError(); |
| 133 | + auto ValidJSON = Parsed.get(); |
| 134 | + |
| 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); |
| 140 | + if (FileErr) |
| 141 | + return createFileOpenError(Path, FileErr); |
| 142 | + |
| 143 | + auto RelativeRootPath = getRelativePathToRoot(DocsFilePath, DocsDirPath); |
| 144 | + auto InfoTypeStr = |
| 145 | + getInfoTypeStr(Parsed->getAsObject(), sys::path::stem(DocsFilePath)); |
| 146 | + if (!InfoTypeStr) |
| 147 | + return InfoTypeStr.takeError(); |
| 148 | + if (Error Err = generateDocForJSON(*Parsed, InfoOS, CDCtx, |
| 149 | + InfoTypeStr.get(), RelativeRootPath)) |
| 150 | + return Err; |
| 151 | + JSONIter.increment(EC); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return Error::success(); |
| 156 | +} |
| 157 | + |
| 158 | +Expected<std::string> MustacheGenerator::getInfoTypeStr(Object *Info, |
| 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(); |
| 170 | +} |
| 171 | + |
| 172 | +SmallString<128> |
| 173 | +MustacheGenerator::getRelativePathToRoot(StringRef PathToFile, |
| 174 | + StringRef DocsRootPath) { |
| 175 | + SmallString<128> PathVec(PathToFile); |
| 176 | + // Remove filename, or else the relative path will have an extra "../" |
| 177 | + sys::path::remove_filename(PathVec); |
| 178 | + return computeRelativePath(DocsRootPath, PathVec); |
| 179 | +} |
| 180 | + |
45 | 181 | llvm::Error Generator::createResources(ClangDocContext &CDCtx) { |
46 | 182 | return llvm::Error::success(); |
47 | 183 | } |
|
0 commit comments