Skip to content

Conversation

@evelez7
Copy link
Member

@evelez7 evelez7 commented Dec 1, 2025

Mustache wasn't tested in the assets lit test, which tests if
user-supplied assets are copied correctly. The Mustache HTML backend
initialy failed this test because it expected every asset, which
included Mustache templates, to be supplied. For now, we just expect
either CSS or JS to be supplied and use the default if one of them isn't
given.

We can allow custom templates in the future using the same checks.

Copy link
Member Author

evelez7 commented Dec 1, 2025

@llvmbot
Copy link
Member

llvmbot commented Dec 1, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Erick Velez (evelez7)

Changes

Mustache wasn't tested in the assets lit test, which tests if
user-supplied assets are copied correctly. The Mustache HTML backend
initialy failed this test because it expected every asset, which
included Mustache templates, to be supplied. For now, we just expect
either CSS or JS to be supplied and use the default if one of them isn't
given.

We can allow custom templates in the future using the same checks.


Full diff: https://github.com/llvm/llvm-project/pull/170198.diff

3 Files Affected:

  • (modified) clang-tools-extra/clang-doc/support/Utils.cpp (+14-6)
  • (modified) clang-tools-extra/clang-doc/tool/ClangDocMain.cpp (+3-2)
  • (modified) clang-tools-extra/test/clang-doc/assets.cpp (+10)
diff --git a/clang-tools-extra/clang-doc/support/Utils.cpp b/clang-tools-extra/clang-doc/support/Utils.cpp
index 6ed56033738b5..897a7ad0adb79 100644
--- a/clang-tools-extra/clang-doc/support/Utils.cpp
+++ b/clang-tools-extra/clang-doc/support/Utils.cpp
@@ -33,8 +33,20 @@ void getMustacheHtmlFiles(StringRef AssetsPath,
   assert(!AssetsPath.empty());
   assert(sys::fs::is_directory(AssetsPath));
 
-  SmallString<128> DefaultStylesheet =
-      appendPathPosix(AssetsPath, "clang-doc-mustache.css");
+  // TODO: Allow users to override default templates with their own. We would
+  // similarly have to check if a template file already exists in CDCtx.
+  if (CDCtx.UserStylesheets.empty()) {
+    SmallString<128> DefaultStylesheet =
+        appendPathPosix(AssetsPath, "clang-doc-mustache.css");
+    CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+                                 DefaultStylesheet.c_str());
+  }
+
+  if (CDCtx.JsScripts.empty()) {
+    SmallString<128> IndexJS = appendPathPosix(AssetsPath, "mustache-index.js");
+    CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
+  }
+
   SmallString<128> NamespaceTemplate =
       appendPathPosix(AssetsPath, "namespace-template.mustache");
   SmallString<128> ClassTemplate =
@@ -45,11 +57,7 @@ void getMustacheHtmlFiles(StringRef AssetsPath,
       appendPathPosix(AssetsPath, "function-template.mustache");
   SmallString<128> CommentTemplate =
       appendPathPosix(AssetsPath, "comment-template.mustache");
-  SmallString<128> IndexJS = appendPathPosix(AssetsPath, "mustache-index.js");
 
-  CDCtx.JsScripts.insert(CDCtx.JsScripts.begin(), IndexJS.c_str());
-  CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
-                               DefaultStylesheet.c_str());
   CDCtx.MustacheTemplates.insert(
       {"namespace-template", NamespaceTemplate.c_str()});
   CDCtx.MustacheTemplates.insert({"class-template", ClassTemplate.c_str()});
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 62fa6a17df2ee..8de7c8ad6f000 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -151,6 +151,7 @@ static std::string getExecutablePath(const char *Argv0, void *MainAddr) {
   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
 }
 
+// TODO: Rename this, since it only gets custom CSS/JS
 static llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
   using DirIt = llvm::sys::fs::directory_iterator;
   std::error_code FileErr;
@@ -221,8 +222,8 @@ static llvm::Error getMustacheHtmlFiles(const char *Argv0,
     llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
                  << " falling back to default\n";
   if (IsDir) {
-    getMustacheHtmlFiles(UserAssetPath, CDCtx);
-    return llvm::Error::success();
+    if (auto Err = getAssetFiles(CDCtx))
+      return Err;
   }
   void *MainAddr = (void *)(intptr_t)getExecutablePath;
   std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
diff --git a/clang-tools-extra/test/clang-doc/assets.cpp b/clang-tools-extra/test/clang-doc/assets.cpp
index c5933e504f6b9..00d0d32213965 100644
--- a/clang-tools-extra/test/clang-doc/assets.cpp
+++ b/clang-tools-extra/test/clang-doc/assets.cpp
@@ -1,9 +1,13 @@
 // RUN: rm -rf %t && mkdir %t
 // RUN: clang-doc --format=html --output=%t --asset=%S/Inputs/test-assets --executor=standalone %s --base base_dir
+// RUN: clang-doc --format=mustache --output=%t --asset=%S/Inputs/test-assets --executor=standalone %s --base base_dir
 // RUN: FileCheck %s -input-file=%t/index.html -check-prefix=INDEX
 // RUN: FileCheck %s -input-file=%t/test.css -check-prefix=CSS
 // RUN: FileCheck %s -input-file=%t/test.js -check-prefix=JS
 
+// RUN: FileCheck %s -input-file=%t/html/test.css -check-prefix=MUSTACHE-CSS
+// RUN: FileCheck %s -input-file=%t/html/test.js -check-prefix=MUSTACHE-JS
+
 // INDEX: <!DOCTYPE html>
 // INDEX-NEXT: <meta charset="utf-8"/>
 // INDEX-NEXT: <title>Index</title>
@@ -19,4 +23,10 @@
 // CSS-NEXT:     padding: 0;
 // CSS-NEXT: }
 
+// MUSTACHE-CSS: body {
+// MUSTACHE-CSS-NEXT:     padding: 0;
+// MUSTACHE-CSS-NEXT: }
+
 // JS: console.log("Hello, world!");
+
+// MUSTACHE-JS: console.log("Hello, world!");

Copy link
Member Author

evelez7 commented Dec 1, 2025

@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-mustache-dr131697 branch from f55da9f to 3589489 Compare December 4, 2025 17:30
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-mustache-assets-test branch 2 times, most recently from 11036c3 to d56b271 Compare December 4, 2025 17:55
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-mustache-dr131697 branch from 3589489 to 45b366a Compare December 4, 2025 17:55
Base automatically changed from users/evelez7/clang-doc-mustache-dr131697 to main December 4, 2025 18:08
Mustache wasn't tested in the assets lit test, which tests if
user-supplied assets are copied correctly. The Mustache HTML backend
initialy failed this test because it expected every asset, which
included Mustache templates, to be supplied. For now, we just expect
either CSS or JS to be supplied and use the default if one of them isn't
given.

We can allow custom templates in the future using the same checks.
@evelez7 evelez7 force-pushed the users/evelez7/clang-doc-mustache-assets-test branch from d56b271 to c9281da Compare December 4, 2025 18:13
@evelez7 evelez7 merged commit 203cd83 into main Dec 4, 2025
5 of 9 checks passed
@evelez7 evelez7 deleted the users/evelez7/clang-doc-mustache-assets-test branch December 4, 2025 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants