[llvm-branch-commits] [clang-tools-extra] [clang-doc] Add helpers for Template config (PR #138062)
Paul Kirth via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue May 6 15:51:17 PDT 2025
https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/138062
>From 50755c006ea8151247c8eb5d4e4a348276fb6d96 Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Wed, 30 Apr 2025 08:10:20 -0700
Subject: [PATCH] [clang-doc] Add helpers for Template config
This patch adds or fills in some helper functions related to template
setup when initializing the mustache backend. It was split from #133161.
Co-authored-by: Peter Chou <peter.chou at mail.utoronto.ca>
---
.../clang-doc/HTMLMustacheGenerator.cpp | 36 +++++++++++-
.../clang-doc/support/CMakeLists.txt | 3 +-
clang-tools-extra/clang-doc/support/Utils.cpp | 55 +++++++++++++++++++
clang-tools-extra/clang-doc/support/Utils.h | 21 +++++++
.../clang-doc/HTMLMustacheGeneratorTest.cpp | 11 +++-
5 files changed, 122 insertions(+), 4 deletions(-)
create mode 100644 clang-tools-extra/clang-doc/support/Utils.cpp
create mode 100644 clang-tools-extra/clang-doc/support/Utils.h
diff --git a/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp b/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
index 2ef91823edc03..1c44465b22b02 100644
--- a/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp
@@ -47,7 +47,7 @@ class MustacheTemplateFile : public Template {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
MemoryBuffer::getFile(FileName);
if (auto EC = BufferOrError.getError())
- return createFileError("cannot open file", EC);
+ return createFileError("cannot open file: " + FileName, EC);
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
StringRef FileContent = Buffer->getBuffer();
registerPartial(Name.str(), FileContent.str());
@@ -61,7 +61,41 @@ static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;
static std::unique_ptr<MustacheTemplateFile> RecordTemplate = nullptr;
+static Error
+setupTemplate(std::unique_ptr<MustacheTemplateFile> &Template,
+ StringRef TemplatePath,
+ std::vector<std::pair<StringRef, StringRef>> Partials) {
+ auto T = MustacheTemplateFile::createMustacheFile(TemplatePath);
+ if (auto EC = T.getError())
+ return createFileError("cannot open file: " + TemplatePath, EC);
+ Template = std::move(T.get());
+ for (const auto [Name, FileName] : Partials) {
+ if (auto Err = Template->registerPartialFile(Name, FileName))
+ return Err;
+ }
+ return Error::success();
+}
+
static Error setupTemplateFiles(const clang::doc::ClangDocContext &CDCtx) {
+ std::string NamespaceFilePath =
+ CDCtx.MustacheTemplates.lookup("namespace-template");
+ std::string ClassFilePath = CDCtx.MustacheTemplates.lookup("class-template");
+ std::string CommentFilePath =
+ CDCtx.MustacheTemplates.lookup("comments-template");
+ std::string FunctionFilePath =
+ CDCtx.MustacheTemplates.lookup("function-template");
+ std::string EnumFilePath = CDCtx.MustacheTemplates.lookup("enum-template");
+ std::vector<std::pair<StringRef, StringRef>> Partials = {
+ {"Comments", CommentFilePath},
+ {"FunctionPartial", FunctionFilePath},
+ {"EnumPartial", EnumFilePath}};
+
+ if (Error Err = setupTemplate(NamespaceTemplate, NamespaceFilePath, Partials))
+ return Err;
+
+ if (Error Err = setupTemplate(RecordTemplate, ClassFilePath, Partials))
+ return Err;
+
return Error::success();
}
diff --git a/clang-tools-extra/clang-doc/support/CMakeLists.txt b/clang-tools-extra/clang-doc/support/CMakeLists.txt
index a4f7993d5c9d8..8ac913ffbe998 100644
--- a/clang-tools-extra/clang-doc/support/CMakeLists.txt
+++ b/clang-tools-extra/clang-doc/support/CMakeLists.txt
@@ -6,4 +6,5 @@ set(LLVM_LINK_COMPONENTS
add_clang_library(clangDocSupport STATIC
File.cpp
- )
\ No newline at end of file
+ Utils.cpp
+ )
diff --git a/clang-tools-extra/clang-doc/support/Utils.cpp b/clang-tools-extra/clang-doc/support/Utils.cpp
new file mode 100644
index 0000000000000..25a6dfa1b2808
--- /dev/null
+++ b/clang-tools-extra/clang-doc/support/Utils.cpp
@@ -0,0 +1,55 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Utils.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+
+using namespace llvm;
+
+llvm::SmallString<128> appendPathNative(StringRef Path, StringRef Asset) {
+ llvm::SmallString<128> Default;
+ llvm::sys::path::native(Path, Default);
+ llvm::sys::path::append(Default, Asset);
+ return Default;
+}
+
+void getMustacheHtmlFiles(StringRef AssetsPath,
+ clang::doc::ClangDocContext &CDCtx) {
+ assert(!AssetsPath.empty());
+ assert(llvm::sys::fs::is_directory(AssetsPath));
+
+ llvm::SmallString<128> DefaultStylesheet =
+ appendPathNative(AssetsPath, "clang-doc-mustache.css");
+ llvm::SmallString<128> NamespaceTemplate =
+ appendPathNative(AssetsPath, "namespace-template.mustache");
+ llvm::SmallString<128> ClassTemplate =
+ appendPathNative(AssetsPath, "class-template.mustache");
+ llvm::SmallString<128> EnumTemplate =
+ appendPathNative(AssetsPath, "enum-template.mustache");
+ llvm::SmallString<128> FunctionTemplate =
+ appendPathNative(AssetsPath, "function-template.mustache");
+ llvm::SmallString<128> CommentTemplate =
+ appendPathNative(AssetsPath, "comments-template.mustache");
+ llvm::SmallString<128> IndexJS =
+ appendPathNative(AssetsPath, "mustache-index.js");
+
+ CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
+ CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+ DefaultStylesheet.str().str());
+ CDCtx.MustacheTemplates.insert(
+ {"namespace-template", NamespaceTemplate.c_str()});
+ CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
+ CDCtx.MustacheTemplates.insert({"enum-template", EnumTemplate.c_str()});
+ CDCtx.MustacheTemplates.insert(
+ {"function-template", FunctionTemplate.c_str()});
+ CDCtx.MustacheTemplates.insert(
+ {"comments-template", CommentTemplate.c_str()});
+}
diff --git a/clang-tools-extra/clang-doc/support/Utils.h b/clang-tools-extra/clang-doc/support/Utils.h
new file mode 100644
index 0000000000000..1f1fee57bdf34
--- /dev/null
+++ b/clang-tools-extra/clang-doc/support/Utils.h
@@ -0,0 +1,21 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILE_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_FILE_H
+
+#include "../Representation.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+
+llvm::SmallString<128> appendPathNative(llvm::StringRef Path,
+ llvm::StringRef Asset);
+
+void getMustacheHtmlFiles(llvm::StringRef AssetsPath,
+ clang::doc::ClangDocContext &CDCtx);
+
+#endif
diff --git a/clang-tools-extra/unittests/clang-doc/HTMLMustacheGeneratorTest.cpp b/clang-tools-extra/unittests/clang-doc/HTMLMustacheGeneratorTest.cpp
index e1b06380cd25a..2f0986bf2569a 100644
--- a/clang-tools-extra/unittests/clang-doc/HTMLMustacheGeneratorTest.cpp
+++ b/clang-tools-extra/unittests/clang-doc/HTMLMustacheGeneratorTest.cpp
@@ -9,6 +9,7 @@
#include "ClangDocTest.h"
#include "Generators.h"
#include "Representation.h"
+#include "support/Utils.h"
#include "clang/Basic/Version.h"
#include "llvm/Support/Path.h"
#include "llvm/Testing/Support/Error.h"
@@ -86,8 +87,14 @@ TEST(HTMLMustacheGeneratorTest, generateDocs) {
assert(G && "Could not find HTMLMustacheGenerator");
ClangDocContext CDCtx = getClangDocContext();
- StringRef RootDir = "";
- EXPECT_THAT_ERROR(G->generateDocs(RootDir, {}, CDCtx), Succeeded())
+ unittest::TempDir RootTestDirectory("generateDocsTest", /*Unique=*/true);
+ CDCtx.OutDirectory = RootTestDirectory.path();
+
+ // This seems wrong, but its unclear how else we would test this...
+ getMustacheHtmlFiles("../../../../../share/clang-doc", CDCtx);
+
+ EXPECT_THAT_ERROR(G->generateDocs(RootTestDirectory.path(), {}, CDCtx),
+ Succeeded())
<< "Failed to generate docs.";
}
More information about the llvm-branch-commits
mailing list