|
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 |
|
@@ -42,6 +44,135 @@ std::string getTagType(TagTypeKind AS) { |
42 | 44 | llvm_unreachable("Unknown TagTypeKind"); |
43 | 45 | } |
44 | 46 |
|
| 47 | +Error createFileOpenError(StringRef FileName, std::error_code EC) { |
| 48 | + return createFileError("cannot open file " + FileName, EC); |
| 49 | +} |
| 50 | + |
| 51 | +Error MustacheGenerator::setupTemplate( |
| 52 | + std::unique_ptr<MustacheTemplateFile> &Template, StringRef TemplatePath, |
| 53 | + std::vector<std::pair<StringRef, StringRef>> Partials) { |
| 54 | + auto T = MustacheTemplateFile::createMustacheFile(TemplatePath); |
| 55 | + if (Error Err = T.takeError()) |
| 56 | + return Err; |
| 57 | + Template = std::move(T.get()); |
| 58 | + for (const auto &[Name, FileName] : Partials) |
| 59 | + if (auto Err = Template->registerPartialFile(Name, FileName)) |
| 60 | + return Err; |
| 61 | + return Error::success(); |
| 62 | +} |
| 63 | + |
| 64 | +Error MustacheGenerator::generateDocumentation( |
| 65 | + StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos, |
| 66 | + const clang::doc::ClangDocContext &CDCtx, std::string DirName) { |
| 67 | + { |
| 68 | + llvm::TimeTraceScope TS("Setup Templates"); |
| 69 | + if (auto Err = setupTemplateFiles(CDCtx)) |
| 70 | + return Err; |
| 71 | + } |
| 72 | + |
| 73 | + { |
| 74 | + llvm::TimeTraceScope TS("Generate JSON for Mustache"); |
| 75 | + if (auto JSONGenerator = findGeneratorByName("json")) { |
| 76 | + if (Error Err = JSONGenerator.get()->generateDocs( |
| 77 | + RootDir, std::move(Infos), CDCtx)) |
| 78 | + return Err; |
| 79 | + } else |
| 80 | + return JSONGenerator.takeError(); |
| 81 | + } |
| 82 | + |
| 83 | + SmallString<128> JSONPath; |
| 84 | + sys::path::native(RootDir.str() + "/json", JSONPath); |
| 85 | + |
| 86 | + StringMap<json::Value> JSONFileMap; |
| 87 | + { |
| 88 | + llvm::TimeTraceScope TS("Iterate JSON files"); |
| 89 | + std::error_code EC; |
| 90 | + sys::fs::recursive_directory_iterator JSONIter(JSONPath, EC); |
| 91 | + std::vector<json::Value> JSONFiles; |
| 92 | + JSONFiles.reserve(Infos.size()); |
| 93 | + if (EC) |
| 94 | + return createStringError("Failed to create directory iterator."); |
| 95 | + |
| 96 | + SmallString<128> DocsDirPath(RootDir.str() + '/' + DirName); |
| 97 | + sys::path::native(DocsDirPath); |
| 98 | + if (auto EC = sys::fs::create_directories(DocsDirPath)) |
| 99 | + return createFileError(DocsDirPath, EC); |
| 100 | + while (JSONIter != sys::fs::recursive_directory_iterator()) { |
| 101 | + // create the same directory structure in the docs format dir |
| 102 | + if (JSONIter->type() == sys::fs::file_type::directory_file) { |
| 103 | + SmallString<128> DocsClonedPath(JSONIter->path()); |
| 104 | + sys::path::replace_path_prefix(DocsClonedPath, JSONPath, DocsDirPath); |
| 105 | + if (auto EC = sys::fs::create_directories(DocsClonedPath)) |
| 106 | + return createFileError(DocsClonedPath, EC); |
| 107 | + } |
| 108 | + |
| 109 | + if (EC) |
| 110 | + return createFileError("Failed to iterate: " + JSONIter->path(), EC); |
| 111 | + |
| 112 | + auto Path = StringRef(JSONIter->path()); |
| 113 | + if (!Path.ends_with(".json")) { |
| 114 | + JSONIter.increment(EC); |
| 115 | + continue; |
| 116 | + } |
| 117 | + |
| 118 | + auto File = MemoryBuffer::getFile(Path); |
| 119 | + if (EC = File.getError(); EC) |
| 120 | + // TODO: Buffer errors to report later, look into using Clang |
| 121 | + // diagnostics. |
| 122 | + llvm::errs() << "Failed to open file: " << Path << " " << EC.message() |
| 123 | + << '\n'; |
| 124 | + |
| 125 | + auto Parsed = json::parse((*File)->getBuffer()); |
| 126 | + if (!Parsed) |
| 127 | + return Parsed.takeError(); |
| 128 | + auto ValidJSON = Parsed.get(); |
| 129 | + |
| 130 | + std::error_code FileErr; |
| 131 | + SmallString<128> DocsFilePath(JSONIter->path()); |
| 132 | + sys::path::replace_path_prefix(DocsFilePath, JSONPath, DocsDirPath); |
| 133 | + sys::path::replace_extension(DocsFilePath, DirName); |
| 134 | + raw_fd_ostream InfoOS(DocsFilePath, FileErr, sys::fs::OF_None); |
| 135 | + if (FileErr) |
| 136 | + return createFileOpenError(Path, FileErr); |
| 137 | + |
| 138 | + auto RelativeRootPath = getRelativePathToRoot(DocsFilePath, DocsDirPath); |
| 139 | + auto InfoTypeStr = |
| 140 | + getInfoTypeStr(Parsed->getAsObject(), sys::path::stem(DocsFilePath)); |
| 141 | + if (!InfoTypeStr) |
| 142 | + return InfoTypeStr.takeError(); |
| 143 | + if (Error Err = generateDocForJSON(*Parsed, InfoOS, CDCtx, |
| 144 | + InfoTypeStr.get(), RelativeRootPath)) |
| 145 | + return Err; |
| 146 | + JSONIter.increment(EC); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return Error::success(); |
| 151 | +} |
| 152 | + |
| 153 | +Expected<std::string> MustacheGenerator::getInfoTypeStr(Object *Info, |
| 154 | + StringRef Filename) { |
| 155 | + auto StrValue = (*Info)["InfoType"]; |
| 156 | + if (StrValue.kind() != json::Value::Kind::String) |
| 157 | + return createStringError("JSON file '%s' does not contain key: 'InfoType'.", |
| 158 | + Filename.str().c_str()); |
| 159 | + auto ObjTypeStr = StrValue.getAsString(); |
| 160 | + if (!ObjTypeStr.has_value()) |
| 161 | + return createStringError( |
| 162 | + "JSON file '%s' does not contain 'InfoType' field as a string.", |
| 163 | + Filename.str().c_str()); |
| 164 | + return ObjTypeStr.value().str(); |
| 165 | +} |
| 166 | + |
| 167 | +SmallString<128> |
| 168 | +MustacheGenerator::getRelativePathToRoot(StringRef PathToFile, |
| 169 | + StringRef DocsRootPath) { |
| 170 | + SmallString<128> PathVec(PathToFile); |
| 171 | + // Remove filename, or else the relative path will have an extra "../" |
| 172 | + sys::path::remove_filename(PathVec); |
| 173 | + return computeRelativePath(DocsRootPath, PathVec); |
| 174 | +} |
| 175 | + |
45 | 176 | llvm::Error Generator::createResources(ClangDocContext &CDCtx) { |
46 | 177 | return llvm::Error::success(); |
47 | 178 | } |
|
0 commit comments