[clang-tools-extra] [clang-doc] Add --asset option to clang-doc (PR #94717)

Paul Kirth via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 11 13:45:52 PDT 2024


================
@@ -127,16 +133,91 @@ std::string getFormatString() {
 // GetMainExecutable (since some platforms don't support taking the
 // address of main, and some platforms can't implement GetMainExecutable
 // without being given the address of a function in the main executable).
-std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
+std::string getExecutablePath(const char *Argv0, void *MainAddr) {
   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
 }
 
+llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
+  std::error_code Code;
+  llvm::SmallString<128> FilePath = llvm::SmallString<128>(UserAssetPath);
+  for (auto DirIt = llvm::sys::fs::directory_iterator(UserAssetPath, Code),
+            DirEnd = llvm::sys::fs::directory_iterator();
+       !Code && DirIt != DirEnd; DirIt.increment(Code)) {
+    FilePath = llvm::SmallString<128>(DirIt->path());
+    if (llvm::sys::fs::is_regular_file(FilePath)) {
+      if (llvm::sys::path::extension(FilePath) == ".css") {
+        CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+                                     std::string(FilePath));
+      } else if (llvm::sys::path::extension(FilePath) == ".js") {
+        CDCtx.FilesToCopy.emplace_back(FilePath.str());
+      }
+    }
+  }
+  if (Code) {
+    return llvm::createFileError(FilePath, Code);
+  }
+  return llvm::Error::success();
+}
+
+llvm::Error getDefaultAssetFiles(const char *Argv0,
+                                 clang::doc::ClangDocContext &CDCtx) {
+  void *MainAddr = (void *)(intptr_t)getExecutablePath;
+  std::string ClangDocPath = getExecutablePath(Argv0, 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");
+
+  if (!llvm::sys::fs::is_regular_file(IndexJS)) {
+    return llvm::createStringError(llvm::inconvertibleErrorCode(),
+                                   "error default index.js file missing at " +
+                                       IndexJS + "\n");
+  }
+
+  if (!llvm::sys::fs::is_regular_file(DefaultStylesheet)) {
+    return llvm::createStringError(
+        llvm::inconvertibleErrorCode(),
+        "error default clang-doc-default-stylesheet.css file missing at " +
+            DefaultStylesheet + "\n");
+  }
----------------
ilovepi wrote:

nit: braces

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


More information about the cfe-commits mailing list