https://github.com/ilovepi created https://github.com/llvm/llvm-project/pull/138061
Split from #133161. This patch fills in the implementation for a number of the MustacheHTMLGenerator methods. Many of these APIs are just stubbed out, and will have their implementation filled in by later patches. Co-authored-by: Peter Chou <peter.c...@mail.utoronto.ca> >From 57c7cf8d3789aa4ac05c51e96c1bd465ddbf5aa5 Mon Sep 17 00:00:00 2001 From: Paul Kirth <paulki...@google.com> Date: Wed, 30 Apr 2025 08:09:41 -0700 Subject: [PATCH] [clang-doc] Add HTMLMustacheGenerator methods Split from #133161. This patch fills in the implementation for a number of the MustacheHTMLGenerator methods. Many of these APIs are just stubbed out, and will have their implementation filled in by later patches. Co-authored-by: Peter Chou <peter.c...@mail.utoronto.ca> --- .../clang-doc/HTMLMustacheGenerator.cpp | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp b/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp index 65e8e34b581d2..1288e4fbbc983 100644 --- a/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp +++ b/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp @@ -56,17 +56,119 @@ class MustacheTemplateFile : public Template { MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {} }; + +static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr; + +static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr; + +static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) { + return Error::success(); +} + Error MustacheHTMLGenerator::generateDocs( StringRef RootDir, StringMap<std::unique_ptr<doc::Info>> Infos, const clang::doc::ClangDocContext &CDCtx) { + if (auto Err = setupTemplateFiles(CDCtx)) + return Err; + // Track which directories we already tried to create. + StringSet<> CreatedDirs; + // Collect all output by file name and create the necessary directories. + StringMap<std::vector<doc::Info *>> FileToInfos; + for (const auto &Group : Infos) { + doc::Info *Info = Group.getValue().get(); + + SmallString<128> Path; + sys::path::native(RootDir, Path); + sys::path::append(Path, Info->getRelativeFilePath("")); + if (!CreatedDirs.contains(Path)) { + if (std::error_code Err = sys::fs::create_directories(Path); + Err != std::error_code()) + return createStringError(Err, "Failed to create directory '%s'.", + Path.c_str()); + CreatedDirs.insert(Path); + } + + sys::path::append(Path, Info->getFileBaseName() + ".html"); + FileToInfos[Path].push_back(Info); + } + + for (const auto &Group : FileToInfos) { + std::error_code FileErr; + raw_fd_ostream InfoOS(Group.getKey(), FileErr, sys::fs::OF_None); + if (FileErr) + return createStringError(FileErr, "Error opening file '%s'", + Group.getKey().data()); + + for (const auto &Info : Group.getValue()) { + if (Error Err = generateDocForInfo(Info, InfoOS, CDCtx)) + return Err; + } + } return Error::success(); } + +static json::Value extractValue(const NamespaceInfo &I, + const ClangDocContext &CDCtx) { + Object NamespaceValue = Object(); + return NamespaceValue; +} + +static json::Value extractValue(const RecordInfo &I, + const ClangDocContext &CDCtx) { + Object RecordValue = Object(); + return RecordValue; +} + +static void setupTemplateValue(const ClangDocContext &CDCtx, json::Value &V, + Info *I) {} + Error MustacheHTMLGenerator::generateDocForInfo(Info *I, raw_ostream &OS, const ClangDocContext &CDCtx) { + switch (I->IT) { + case InfoType::IT_namespace: { + json::Value V = + extractValue(*static_cast<clang::doc::NamespaceInfo *>(I), CDCtx); + setupTemplateValue(CDCtx, V, I); + NamespaceTemplate->render(V, OS); + break; + } + case InfoType::IT_record: { + json::Value V = + extractValue(*static_cast<clang::doc::RecordInfo *>(I), CDCtx); + setupTemplateValue(CDCtx, V, I); + // Serialize the JSON value to the output stream in a readable format. + outs() << "Visit: " << I->Name << "\n"; + // outs() << formatv("{0:2}", V) << "\n"; + RecordTemplate->render(V, outs()); + break; + } + case InfoType::IT_enum: + outs() << "IT_enum\n"; + break; + case InfoType::IT_function: + outs() << "IT_Function\n"; + break; + case InfoType::IT_typedef: + outs() << "IT_typedef\n"; + break; + case InfoType::IT_default: + return createStringError(inconvertibleErrorCode(), "unexpected InfoType"); + } return Error::success(); } + Error MustacheHTMLGenerator::createResources(ClangDocContext &CDCtx) { Error Err = Error::success(); + for (const auto &FilePath : CDCtx.UserStylesheets) { + Err = copyFile(FilePath, CDCtx.OutDirectory); + if (Err) + return Err; + } + for (const auto &FilePath : CDCtx.JsScripts) { + Err = copyFile(FilePath, CDCtx.OutDirectory); + if (Err) + return Err; + } return Error::success(); } _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits