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

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 14:53:02 PDT 2024


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

>From eeb334620df72c395a5ad27f44a864a6a0c194a5 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 6 Jun 2024 23:18:12 -0400
Subject: [PATCH 1/4] [clang][clang-doc] add asset path

---
 .../clang-doc/tool/ClangDocMain.cpp           | 73 ++++++++++++++-----
 1 file changed, 55 insertions(+), 18 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 21b581fa6df2e..df53c46b4a76e 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -81,6 +81,12 @@ static llvm::cl::list<std::string> UserStylesheets(
     llvm::cl::desc("CSS stylesheets to extend the default styles."),
     llvm::cl::cat(ClangDocCategory));
 
+static llvm::cl::opt<std::string>
+    UserAssetPath("asset",
+                  llvm::cl::desc("User supplied asset path for html output to "
+                                 "override the default css and js files"),
+                  llvm::cl::cat(ClangDocCategory));
+
 static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
 Directory where processed files are stored.
 Links to definition locations will only be
@@ -131,12 +137,54 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
 }
 
+void GetAssetFiles(clang::doc::ClangDocContext CDCtx) {
+  std::error_code Code;
+  for (auto DirIt = llvm::sys::fs::directory_iterator(
+                std::string(UserAssetPath), Code),
+            dir_end = llvm::sys::fs::directory_iterator();
+       !Code && DirIt != dir_end; DirIt.increment(Code)) {
+    llvm::SmallString<128> filePath = llvm::SmallString<128>(DirIt->path());
+    if (llvm::sys::fs::is_regular_file(filePath)) {
+      if (filePath.ends_with(".css")) {
+        CDCtx.UserStylesheets.push_back(std::string(filePath));
+      } else if (filePath.ends_with(".js")) {
+        CDCtx.FilesToCopy.push_back(std::string(filePath));
+      }
+    }
+  }
+}
+
+void 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");
+  CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+                               std::string(DefaultStylesheet));
+  CDCtx.FilesToCopy.emplace_back(IndexJS.str());
+
+  llvm::outs() << "No default asset path found using default asset path: "
+               << AssetsPath << "\n";
+}
+
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
   std::error_code OK;
 
   const char *Overview =
-    R"(Generates documentation from source code and comments.
+      R"(Generates documentation from source code and comments.
 
 Example usage for files without flags (default):
 
@@ -182,23 +230,12 @@ Example usage for a project using a compile commands database:
       {"index.js", "index_json.js"}};
 
   if (Format == "html") {
-    void *MainAddr = (void *)(intptr_t)GetExecutablePath;
-    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() &&
+        llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+      GetAssetFiles(CDCtx);
+    } else {
+      GetDefaultAssetFiles(argv[0], CDCtx);
+    }
   }
 
   // Mapping phase

>From 85581ed2f05974eac5697f43ab95a34ce17742a0 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Fri, 7 Jun 2024 01:54:43 -0400
Subject: [PATCH 2/4] [clang][clang-doc] fixes bug caused by not passing by
 reference

---
 clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index df53c46b4a76e..b72767d201073 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -137,7 +137,7 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
 }
 
-void GetAssetFiles(clang::doc::ClangDocContext CDCtx) {
+void GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
   std::error_code Code;
   for (auto DirIt = llvm::sys::fs::directory_iterator(
                 std::string(UserAssetPath), Code),
@@ -146,16 +146,17 @@ void GetAssetFiles(clang::doc::ClangDocContext CDCtx) {
     llvm::SmallString<128> filePath = llvm::SmallString<128>(DirIt->path());
     if (llvm::sys::fs::is_regular_file(filePath)) {
       if (filePath.ends_with(".css")) {
-        CDCtx.UserStylesheets.push_back(std::string(filePath));
+        CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
+                                     std::string(filePath));
       } else if (filePath.ends_with(".js")) {
-        CDCtx.FilesToCopy.push_back(std::string(filePath));
+        CDCtx.FilesToCopy.emplace_back(filePath.str());
       }
     }
   }
 }
 
 void GetDefaultAssetFiles(const char *Argv0,
-                          clang::doc::ClangDocContext CDCtx) {
+                          clang::doc::ClangDocContext &CDCtx) {
   void *MainAddr = (void *)(intptr_t)GetExecutablePath;
   std::string ClangDocPath = GetExecutablePath(Argv0, MainAddr);
   llvm::SmallString<128> NativeClangDocPath;

>From 6fee7d067d2923d971186b7e505452abaa2b92f0 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Fri, 7 Jun 2024 17:30:19 -0400
Subject: [PATCH 3/4] [clang][clang-doc] address pr comments about --asset
 option

---
 .../clang-doc/tool/ClangDocMain.cpp           | 66 +++++++++++++------
 1 file changed, 45 insertions(+), 21 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index b72767d201073..c2439734960d4 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -83,8 +83,8 @@ static llvm::cl::list<std::string> UserStylesheets(
 
 static llvm::cl::opt<std::string>
     UserAssetPath("asset",
-                  llvm::cl::desc("User supplied asset path for html output to "
-                                 "override the default css and js files"),
+                  llvm::cl::desc("User supplied asset path to "
+                                 "override the default css and js files for html output"),
                   llvm::cl::cat(ClangDocCategory));
 
 static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
@@ -137,25 +137,27 @@ std::string GetExecutablePath(const char *Argv0, void *MainAddr) {
   return llvm::sys::fs::getMainExecutable(Argv0, MainAddr);
 }
 
-void GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
+llvm::Error GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
   std::error_code Code;
-  for (auto DirIt = llvm::sys::fs::directory_iterator(
-                std::string(UserAssetPath), Code),
-            dir_end = llvm::sys::fs::directory_iterator();
-       !Code && DirIt != dir_end; DirIt.increment(Code)) {
-    llvm::SmallString<128> filePath = llvm::SmallString<128>(DirIt->path());
-    if (llvm::sys::fs::is_regular_file(filePath)) {
-      if (filePath.ends_with(".css")) {
+  for (auto DirIt = llvm::sys::fs::directory_iterator(UserAssetPath, Code),
+            DirEnd = llvm::sys::fs::directory_iterator();
+       !Code && DirIt != DirEnd; DirIt.increment(Code)) {
+    llvm::SmallString<128> FilePath = llvm::SmallString<128>(DirIt->path());
+    if (!Code) {
+        return llvm::createFileError(FilePath, Code);
+    }
+    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 (filePath.ends_with(".js")) {
-        CDCtx.FilesToCopy.emplace_back(filePath.str());
+                                     std::string(FilePath));
+      } else if (llvm::sys::path::extension(FilePath) == ".js") {
+        CDCtx.FilesToCopy.emplace_back(FilePath.str());
       }
     }
   }
 }
 
-void GetDefaultAssetFiles(const char *Argv0,
+llvm::Error GetDefaultAssetFiles(const char *Argv0,
                           clang::doc::ClangDocContext &CDCtx) {
   void *MainAddr = (void *)(intptr_t)GetExecutablePath;
   std::string ClangDocPath = GetExecutablePath(Argv0, MainAddr);
@@ -172,12 +174,35 @@ void GetDefaultAssetFiles(const char *Argv0,
   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 at " + IndexJS + "\n");
+  }
+
+  if (!llvm::sys::fs::is_regular_file(DefaultStylesheet)) {
+      return llvm::createStringError(llvm::inconvertibleErrorCode(),
+                                     "error default clang-doc-default-stylesheet.css file at " + DefaultStylesheet + "\n");
+  }
+
   CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
                                std::string(DefaultStylesheet));
   CDCtx.FilesToCopy.emplace_back(IndexJS.str());
 
-  llvm::outs() << "No default asset path found using default asset path: "
+  llvm::outs() << "Using default asset: "
                << AssetsPath << "\n";
+
+  return llvm::Error::success();
+}
+
+llvm::Error GetHTMLAssetFiles(const char *Argv0, clang::doc::ClangDocContext &CDCtx) {
+    if (!UserAssetPath.empty() && !llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+        llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath << " falling back to default\n";
+    }
+    if (llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+        return GetAssetFiles(CDCtx);
+    }
+    return GetDefaultAssetFiles(Argv0, CDCtx);
 }
 
 int main(int argc, const char **argv) {
@@ -231,12 +256,11 @@ Example usage for a project using a compile commands database:
       {"index.js", "index_json.js"}};
 
   if (Format == "html") {
-    if (!UserAssetPath.empty() &&
-        llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
-      GetAssetFiles(CDCtx);
-    } else {
-      GetDefaultAssetFiles(argv[0], CDCtx);
-    }
+      auto Err = GetHTMLAssetFiles(argv[0], CDCtx);
+      if (Err) {
+          llvm::errs() << toString(std::move(Err)) << "\n";
+    return 1;
+  }
   }
 
   // Mapping phase

>From a8462150b7fa4f80e5a3db5e9c7edac221781f4f Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Fri, 7 Jun 2024 17:52:44 -0400
Subject: [PATCH 4/4] [clang][clang-doc] add a test case

---
 .../clang-doc/tool/ClangDocMain.cpp           | 55 ++++++++++---------
 .../test/clang-doc/single-source-html.cpp     | 14 +++++
 2 files changed, 44 insertions(+), 25 deletions(-)
 create mode 100644 clang-tools-extra/test/clang-doc/single-source-html.cpp

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index c2439734960d4..f64612f085c30 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -81,11 +81,11 @@ static llvm::cl::list<std::string> UserStylesheets(
     llvm::cl::desc("CSS stylesheets to extend the default styles."),
     llvm::cl::cat(ClangDocCategory));
 
-static llvm::cl::opt<std::string>
-    UserAssetPath("asset",
-                  llvm::cl::desc("User supplied asset path to "
-                                 "override the default css and js files for html output"),
-                  llvm::cl::cat(ClangDocCategory));
+static llvm::cl::opt<std::string> UserAssetPath(
+    "asset",
+    llvm::cl::desc("User supplied asset path to "
+                   "override the default css and js files for html output"),
+    llvm::cl::cat(ClangDocCategory));
 
 static llvm::cl::opt<std::string> SourceRoot("source-root", llvm::cl::desc(R"(
 Directory where processed files are stored.
@@ -144,7 +144,7 @@ llvm::Error GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
        !Code && DirIt != DirEnd; DirIt.increment(Code)) {
     llvm::SmallString<128> FilePath = llvm::SmallString<128>(DirIt->path());
     if (!Code) {
-        return llvm::createFileError(FilePath, Code);
+      return llvm::createFileError(FilePath, Code);
     }
     if (llvm::sys::fs::is_regular_file(FilePath)) {
       if (llvm::sys::path::extension(FilePath) == ".css") {
@@ -158,7 +158,7 @@ llvm::Error GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
 }
 
 llvm::Error GetDefaultAssetFiles(const char *Argv0,
-                          clang::doc::ClangDocContext &CDCtx) {
+                                 clang::doc::ClangDocContext &CDCtx) {
   void *MainAddr = (void *)(intptr_t)GetExecutablePath;
   std::string ClangDocPath = GetExecutablePath(Argv0, MainAddr);
   llvm::SmallString<128> NativeClangDocPath;
@@ -177,32 +177,37 @@ llvm::Error GetDefaultAssetFiles(const char *Argv0,
 
   if (!llvm::sys::fs::is_regular_file(IndexJS)) {
     return llvm::createStringError(llvm::inconvertibleErrorCode(),
-                                   "error default index.js file at " + IndexJS + "\n");
+                                   "error default index.js file at " + IndexJS +
+                                       "\n");
   }
 
   if (!llvm::sys::fs::is_regular_file(DefaultStylesheet)) {
-      return llvm::createStringError(llvm::inconvertibleErrorCode(),
-                                     "error default clang-doc-default-stylesheet.css file at " + DefaultStylesheet + "\n");
+    return llvm::createStringError(
+        llvm::inconvertibleErrorCode(),
+        "error default clang-doc-default-stylesheet.css file at " +
+            DefaultStylesheet + "\n");
   }
 
   CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
                                std::string(DefaultStylesheet));
   CDCtx.FilesToCopy.emplace_back(IndexJS.str());
 
-  llvm::outs() << "Using default asset: "
-               << AssetsPath << "\n";
+  llvm::outs() << "Using default asset: " << AssetsPath << "\n";
 
   return llvm::Error::success();
 }
 
-llvm::Error GetHTMLAssetFiles(const char *Argv0, clang::doc::ClangDocContext &CDCtx) {
-    if (!UserAssetPath.empty() && !llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
-        llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath << " falling back to default\n";
-    }
-    if (llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
-        return GetAssetFiles(CDCtx);
-    }
-    return GetDefaultAssetFiles(Argv0, CDCtx);
+llvm::Error GetHTMLAssetFiles(const char *Argv0,
+                              clang::doc::ClangDocContext &CDCtx) {
+  if (!UserAssetPath.empty() &&
+      !llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+    llvm::outs() << "Asset path supply is not a directory: " << UserAssetPath
+                 << " falling back to default\n";
+  }
+  if (llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+    return GetAssetFiles(CDCtx);
+  }
+  return GetDefaultAssetFiles(Argv0, CDCtx);
 }
 
 int main(int argc, const char **argv) {
@@ -256,11 +261,11 @@ Example usage for a project using a compile commands database:
       {"index.js", "index_json.js"}};
 
   if (Format == "html") {
-      auto Err = GetHTMLAssetFiles(argv[0], CDCtx);
-      if (Err) {
-          llvm::errs() << toString(std::move(Err)) << "\n";
-    return 1;
-  }
+    auto Err = GetHTMLAssetFiles(argv[0], CDCtx);
+    if (Err) {
+      llvm::errs() << toString(std::move(Err)) << "\n";
+      return 1;
+    }
   }
 
   // Mapping phase
diff --git a/clang-tools-extra/test/clang-doc/single-source-html.cpp b/clang-tools-extra/test/clang-doc/single-source-html.cpp
new file mode 100644
index 0000000000000..9720acbaae989
--- /dev/null
+++ b/clang-tools-extra/test/clang-doc/single-source-html.cpp
@@ -0,0 +1,14 @@
+// RUN: rm -rf %t
+// RUN: mkdir %t
+// RUN: echo "" > %t/compile_flags.txt
+// RUN: cp "%s" "%t/test.cpp"
+// RUN: clang-doc --format=html --executor=standalone -p %t %t/test.cpp -output=%t/docs > %t/output.txt
+// RUN: cat %t/output.txt | FileCheck %s --check-prefix=CHECK
+
+// CHECK: Emiting docs in html format.
+// CHECK-NEXT: Using default asset: {{.*}}..\share\clang
+// CHECK-NEXT: Mapping decls...
+// CHECK-NEXT: Collecting infos...
+// CHECK-NEXT: Reducing 0 infos...
+// CHECK-NEXT: Generating docs...
+// CHECK-NEXT: Generating assets for docs...
\ No newline at end of file



More information about the cfe-commits mailing list