[clang-tools-extra] [llvm] [clang-doc] Add basic e2e test (PR #93928)

Paul Kirth via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 18:10:45 PDT 2024


================
@@ -186,19 +196,36 @@ Example usage for a project using a compile commands database:
     std::string ClangDocPath = GetExecutablePath(argv[0], MainAddr);
     llvm::SmallString<128> NativeClangDocPath;
     llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
-    llvm::SmallString<128> AssetsPath;
-    AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
-    llvm::sys::path::append(AssetsPath, "..", "share", "clang");
-    llvm::SmallString<128> DefaultStylesheet;
-    llvm::sys::path::native(AssetsPath, DefaultStylesheet);
-    llvm::sys::path::append(DefaultStylesheet,
-                            "clang-doc-default-stylesheet.css");
-    llvm::SmallString<128> IndexJS;
-    llvm::sys::path::native(AssetsPath, IndexJS);
-    llvm::sys::path::append(IndexJS, "index.js");
-    CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
-                                 std::string(DefaultStylesheet));
-    CDCtx.FilesToCopy.emplace_back(IndexJS.str());
+    if (!UserAssetPath.empty()) {
+        std::error_code code;
+        llvm::sys::fs::directory_iterator dirIt(std::string(UserAssetPath), code);
+        while (!code && dirIt != llvm::sys::fs::directory_iterator()) {
+            std::basic_string<char> filePath = dirIt->path();
+            if (llvm::sys::fs::is_regular_file(filePath)) {
+                if (ends_with(filePath, ".css")) {
+                    CDCtx.UserStylesheets.push_back(filePath);
+                }
+                else if (ends_with(filePath, ".js")) {
+                    CDCtx.UserStylesheets.push_back(filePath);
+                }
+            }
+        }
+    }
+    else {
+        llvm::SmallString<128> AssetsPath;
+        AssetsPath = llvm::sys::path::parent_path(NativeClangDocPath);
+        llvm::sys::path::append(AssetsPath, "..", "share", "clang");
+        llvm::SmallString<128> DefaultStylesheet;
+        llvm::sys::path::native(AssetsPath, DefaultStylesheet);
+        llvm::sys::path::append(DefaultStylesheet,
+                                "clang-doc-default-stylesheet.css");
+        llvm::SmallString<128> IndexJS;
+        llvm::sys::path::native(AssetsPath, IndexJS);
+        llvm::sys::path::append(IndexJS, "index.js");
+        CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+                                     std::string(DefaultStylesheet));
+        CDCtx.FilesToCopy.emplace_back(IndexJS.str());
----------------
ilovepi wrote:

probably each of these branches could be extracted into a helper function to make this a bit easier to follow, but you do want to be careful to ensure the user path can fall back to the default behavior, or give very clear errors about missing assets/paths.

https://github.com/llvm/llvm-project/pull/93928


More information about the cfe-commits mailing list