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

via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 11 13:56:09 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/8] [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/8] [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/8] [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/8] [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

>From 39e06472bc2348971dd144a1cc2b3717cde29576 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Tue, 11 Jun 2024 15:45:15 -0400
Subject: [PATCH 5/8] [clang][clang-doc] PR comments

---
 clang-tools-extra/clang-doc/tool/ClangDocMain.cpp       | 1 +
 clang-tools-extra/test/clang-doc/single-source-html.cpp | 8 +-------
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index f64612f085c30..7b01b0b84857a 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -155,6 +155,7 @@ llvm::Error GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
       }
     }
   }
+  return llvm::Error::success();
 }
 
 llvm::Error GetDefaultAssetFiles(const char *Argv0,
diff --git a/clang-tools-extra/test/clang-doc/single-source-html.cpp b/clang-tools-extra/test/clang-doc/single-source-html.cpp
index 9720acbaae989..4f62c45f545fa 100644
--- a/clang-tools-extra/test/clang-doc/single-source-html.cpp
+++ b/clang-tools-extra/test/clang-doc/single-source-html.cpp
@@ -5,10 +5,4 @@
 // 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
+// CHECK: Using default asset: {{.*}}..\share\clang
\ No newline at end of file

>From 94b297955aca884a39b3cfc29d6e06c53155ee15 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Tue, 11 Jun 2024 16:39:04 -0400
Subject: [PATCH 6/8] [clang][clang-doc] fix asset option

---
 .../clang-doc/tool/ClangDocMain.cpp           | 33 ++++++++++---------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 7b01b0b84857a..fb3eb51e09a24 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -133,19 +133,17 @@ 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) {
+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)) {
-    llvm::SmallString<128> FilePath = llvm::SmallString<128>(DirIt->path());
-    if (!Code) {
-      return llvm::createFileError(FilePath, 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(),
@@ -155,13 +153,16 @@ llvm::Error GetAssetFiles(clang::doc::ClangDocContext &CDCtx) {
       }
     }
   }
+  if (Code) {
+    return llvm::createFileError(FilePath, Code);
+  }
   return llvm::Error::success();
 }
 
-llvm::Error 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);
+  void *MainAddr = (void *)(intptr_t)getExecutablePath;
+  std::string ClangDocPath = getExecutablePath(Argv0, MainAddr);
   llvm::SmallString<128> NativeClangDocPath;
   llvm::sys::path::native(ClangDocPath, NativeClangDocPath);
 
@@ -178,14 +179,14 @@ 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 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 at " +
+        "error default clang-doc-default-stylesheet.css file missing at " +
             DefaultStylesheet + "\n");
   }
 
@@ -198,7 +199,7 @@ llvm::Error GetDefaultAssetFiles(const char *Argv0,
   return llvm::Error::success();
 }
 
-llvm::Error GetHTMLAssetFiles(const char *Argv0,
+llvm::Error getHtmlAssetFiles(const char *Argv0,
                               clang::doc::ClangDocContext &CDCtx) {
   if (!UserAssetPath.empty() &&
       !llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
@@ -206,9 +207,9 @@ llvm::Error GetHTMLAssetFiles(const char *Argv0,
                  << " falling back to default\n";
   }
   if (llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
-    return GetAssetFiles(CDCtx);
+    return getAssetFiles(CDCtx);
   }
-  return GetDefaultAssetFiles(Argv0, CDCtx);
+  return getDefaultAssetFiles(Argv0, CDCtx);
 }
 
 int main(int argc, const char **argv) {
@@ -262,7 +263,7 @@ Example usage for a project using a compile commands database:
       {"index.js", "index_json.js"}};
 
   if (Format == "html") {
-    auto Err = GetHTMLAssetFiles(argv[0], CDCtx);
+    auto Err = getHtmlAssetFiles(argv[0], CDCtx);
     if (Err) {
       llvm::errs() << toString(std::move(Err)) << "\n";
       return 1;

>From f24609845dd4b28431114db52c01b08bd64d77f8 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Tue, 11 Jun 2024 16:53:23 -0400
Subject: [PATCH 7/8] [clang][clang-doc] address PR comments

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

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index fb3eb51e09a24..141f5ff80ea87 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -153,9 +153,8 @@ llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
       }
     }
   }
-  if (Code) {
+  if (Code)
     return llvm::createFileError(FilePath, Code);
-  }
   return llvm::Error::success();
 }
 
@@ -177,18 +176,16 @@ llvm::Error getDefaultAssetFiles(const char *Argv0,
   llvm::sys::path::native(AssetsPath, IndexJS);
   llvm::sys::path::append(IndexJS, "index.js");
 
-  if (!llvm::sys::fs::is_regular_file(IndexJS)) {
+  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)) {
+  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");
-  }
 
   CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
                                std::string(DefaultStylesheet));
@@ -202,13 +199,11 @@ llvm::Error getDefaultAssetFiles(const char *Argv0,
 llvm::Error getHtmlAssetFiles(const char *Argv0,
                               clang::doc::ClangDocContext &CDCtx) {
   if (!UserAssetPath.empty() &&
-      !llvm::sys::fs::is_directory(std::string(UserAssetPath))) {
+      !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))) {
+  if (llvm::sys::fs::is_directory(std::string(UserAssetPath)))
     return getAssetFiles(CDCtx);
-  }
   return getDefaultAssetFiles(Argv0, CDCtx);
 }
 
@@ -263,8 +258,7 @@ 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) {
+    if (auto Err = getHtmlAssetFiles(argv[0], CDCtx)) {
       llvm::errs() << toString(std::move(Err)) << "\n";
       return 1;
     }

>From 27bfe8da81c34acd8563849ee57dfcea82a8dbd7 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Tue, 11 Jun 2024 16:55:52 -0400
Subject: [PATCH 8/8] [clang][clang-doc] address PR comments

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

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 141f5ff80ea87..ca881b5b35ca0 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -145,12 +145,11 @@ llvm::Error getAssetFiles(clang::doc::ClangDocContext &CDCtx) {
        !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") {
+      if (llvm::sys::path::extension(FilePath) == ".css")
         CDCtx.UserStylesheets.insert(CDCtx.UserStylesheets.begin(),
                                      std::string(FilePath));
-      } else if (llvm::sys::path::extension(FilePath) == ".js") {
+      else if (llvm::sys::path::extension(FilePath) == ".js")
         CDCtx.FilesToCopy.emplace_back(FilePath.str());
-      }
     }
   }
   if (Code)



More information about the cfe-commits mailing list