[clang-tools-extra] [clang-doc] Improve performance by adding a short circuit (PR #96809)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 23 15:05:48 PDT 2024


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

>From 72911ee0060d300a393590b47231666e66e85ad5 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Sat, 13 Jul 2024 20:27:31 -0400
Subject: [PATCH 01/14] remove USR

---
 clang-tools-extra/clang-doc/HTMLGenerator.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index f6b5e8926f903..c4df336418484 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -983,7 +983,7 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) {
   llvm::json::OStream J(OS, 2);
   std::function<void(Index)> IndexToJSON = [&](const Index &I) {
     J.object([&] {
-      J.attribute("USR", toHex(llvm::toStringRef(I.USR)));
+      //J.attribute("USR", toHex(llvm::toStringRef(I.USR)));
       J.attribute("Name", I.Name);
       J.attribute("RefType", getRefType(I.RefType));
       J.attribute("Path", I.getRelativeFilePath(""));

>From 0175429ab5769dbba49741c543f54b7b93864372 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Mon, 15 Jul 2024 01:45:44 -0400
Subject: [PATCH 02/14] test

---
 .../clang-doc/tool/ClangDocMain.cpp           | 76 ++++++++++++-------
 1 file changed, 49 insertions(+), 27 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 6198a6e0cdcc3..08f06f8feed0f 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -45,6 +45,10 @@
 #include <atomic>
 #include <mutex>
 #include <string>
+#include <exception>
+#include <typeinfo>
+#include <stdexcept>
+
 
 using namespace clang::ast_matchers;
 using namespace clang::tooling;
@@ -205,6 +209,19 @@ llvm::Error getHtmlAssetFiles(const char *Argv0,
   return getDefaultAssetFiles(Argv0, CDCtx);
 }
 
+void handle_eptr(std::exception_ptr eptr) // passing by value is OK
+{
+  try
+  {
+    if (eptr)
+      std::rethrow_exception(eptr);
+  }
+  catch(const std::exception& e)
+  {
+    llvm::outs() << "Caught exception: '" << e.what() << "'\n";
+  }
+}
+
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
   std::error_code OK;
@@ -231,6 +248,7 @@ Example usage for a project using a compile commands database:
 
   // Fail early if an invalid format was provided.
   std::string Format = getFormatString();
+  llvm::outs() << "Unoptimized\n";
   llvm::outs() << "Emiting docs in " << Format << " format.\n";
   auto G = doc::findGeneratorByName(Format);
   if (!G) {
@@ -302,37 +320,41 @@ Example usage for a project using a compile commands database:
   llvm::DefaultThreadPool Pool(llvm::hardware_concurrency(ExecutorConcurrency));
   for (auto &Group : USRToBitcode) {
     Pool.async([&]() {
-      std::vector<std::unique_ptr<doc::Info>> Infos;
-
-      for (auto &Bitcode : Group.getValue()) {
-        llvm::BitstreamCursor Stream(Bitcode);
-        doc::ClangDocBitcodeReader Reader(Stream);
-        auto ReadInfos = Reader.readBitcode();
-        if (!ReadInfos) {
-          llvm::errs() << toString(ReadInfos.takeError()) << "\n";
-          Error = true;
-          return;
+      try {
+        std::vector<std::unique_ptr<doc::Info>> Infos;
+        for (auto &Bitcode : Group.getValue()) {
+          llvm::BitstreamCursor Stream(Bitcode);
+          doc::ClangDocBitcodeReader Reader(Stream);
+          auto ReadInfos = Reader.readBitcode();
+          if (!ReadInfos) {
+            llvm::errs() << toString(ReadInfos.takeError()) << "\n";
+            Error = true;
+            return;
+          }
+          std::move(ReadInfos->begin(), ReadInfos->end(),
+                    std::back_inserter(Infos));
         }
-        std::move(ReadInfos->begin(), ReadInfos->end(),
-                  std::back_inserter(Infos));
-      }
 
-      auto Reduced = doc::mergeInfos(Infos);
-      if (!Reduced) {
-        llvm::errs() << llvm::toString(Reduced.takeError());
-        return;
-      }
+        auto Reduced = doc::mergeInfos(Infos);
+        if (!Reduced) {
+          llvm::errs() << llvm::toString(Reduced.takeError());
+          return;
+        }
 
-      // Add a reference to this Info in the Index
-      {
-        std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
-        clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
-      }
+        // Add a reference to this Info in the Index
+        {
+          std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
+          clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
+        }
 
-      // Save in the result map (needs a lock due to threaded access).
-      {
-        std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
-        USRToInfo[Group.getKey()] = std::move(Reduced.get());
+        // Save in the result map (needs a lock due to threaded access).
+        {
+          std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
+          USRToInfo[Group.getKey()] = std::move(Reduced.get());
+        }
+      } catch (...) {
+        std::exception_ptr P = std::current_exception();
+        handle_eptr(P);
       }
     });
   }

>From b48acead75665ff809037a37267ec01bc4927407 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Wed, 26 Jun 2024 14:20:03 -0400
Subject: [PATCH 03/14] [clang-doc] add short circuit in mapper

---
 clang-tools-extra/clang-doc/Mapper.cpp        | 21 +++++++++++++++++++
 clang-tools-extra/clang-doc/Mapper.h          |  3 +++
 .../clang-doc/tool/ClangDocMain.cpp           | 13 ++++++------
 3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index bb8b7952980ac..ba6311e85a2ee 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -17,6 +17,11 @@
 namespace clang {
 namespace doc {
 
+
+static std::unordered_set<std::string> USRVisited;
+
+static llvm::sys::Mutex USRVisitedGuard;
+
 void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
   TraverseDecl(Context.getTranslationUnitDecl());
 }
@@ -34,6 +39,17 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
   // If there is an error generating a USR for the decl, skip this decl.
   if (index::generateUSRForDecl(D, USR))
     return true;
+
+  // Prevent Visiting USR twice
+  {
+    std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
+    std::string Visited = USR.str().str();
+    if (USRVisited.count(Visited)) {
+      return true;
+    }
+    USRVisited.insert(Visited);
+  }
+
   bool IsFileInRootDir;
   llvm::SmallString<128> File =
       getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
@@ -41,6 +57,11 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
                                getLine(D, D->getASTContext()), File,
                                IsFileInRootDir, CDCtx.PublicOnly);
 
+  // Bitcode
+  if (CDCtx.NoBitcode) {
+    return true;
+  }
+
   // A null in place of I indicates that the serializer is skipping this decl
   // for some reason (e.g. we're only reporting public decls).
   if (I.first)
diff --git a/clang-tools-extra/clang-doc/Mapper.h b/clang-tools-extra/clang-doc/Mapper.h
index cedde935ab743..1da7a66f1471a 100644
--- a/clang-tools-extra/clang-doc/Mapper.h
+++ b/clang-tools-extra/clang-doc/Mapper.h
@@ -20,6 +20,8 @@
 #include "Representation.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Tooling/Execution.h"
+#include "llvm/Support/Mutex.h"
+#include <unordered_set>
 
 using namespace clang::comments;
 using namespace clang::tooling;
@@ -53,6 +55,7 @@ class MapASTVisitor : public clang::RecursiveASTVisitor<MapASTVisitor>,
                                     const ASTContext &Context) const;
 
   ClangDocContext CDCtx;
+
 };
 
 } // namespace doc
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 08f06f8feed0f..83d2af976105b 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -18,12 +18,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "BitcodeReader.h"
-#include "BitcodeWriter.h"
 #include "ClangDoc.h"
 #include "Generators.h"
 #include "Representation.h"
-#include "clang/AST/AST.h"
-#include "clang/AST/Decl.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchersInternal.h"
 #include "clang/Driver/Options.h"
@@ -31,14 +28,11 @@
 #include "clang/Tooling/AllTUsExecution.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Execution.h"
-#include "clang/Tooling/Tooling.h"
-#include "llvm/ADT/APFloat.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/Path.h"
-#include "llvm/Support/Process.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/raw_ostream.h"
@@ -71,6 +65,11 @@ static llvm::cl::opt<std::string>
                  llvm::cl::desc("Directory for outputting generated files."),
                  llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory));
 
+static llvm::cl::opt<bool> NoBitcode(
+     "nobitcode",
+     llvm::cl::desc("Do not emit bitcode for faster processing time"),
+     llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
+
 static llvm::cl::opt<bool>
     PublicOnly("public", llvm::cl::desc("Document only public declarations."),
                llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
@@ -267,6 +266,7 @@ Example usage for a project using a compile commands database:
       Executor->get()->getExecutionContext(),
       ProjectName,
       PublicOnly,
+      NoBitcode,
       OutDirectory,
       SourceRoot,
       RepositoryUrl,
@@ -313,6 +313,7 @@ Example usage for a project using a compile commands database:
 
   // First reducing phase (reduce all decls into one info per decl).
   llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
+
   std::atomic<bool> Error;
   Error = false;
   llvm::sys::Mutex IndexMutex;

>From 969354a28099a683d042347203aafba2397022df Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Wed, 26 Jun 2024 14:36:09 -0400
Subject: [PATCH 04/14] [clang-doc] add short circuit to improve performance

---
 clang-tools-extra/clang-doc/Mapper.cpp            |  7 ++-----
 clang-tools-extra/clang-doc/Mapper.h              |  3 ---
 clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 13 ++++++-------
 3 files changed, 8 insertions(+), 15 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index ba6311e85a2ee..b9ca6edd03b8c 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -12,7 +12,8 @@
 #include "clang/AST/Comment.h"
 #include "clang/Index/USRGeneration.h"
 #include "llvm/ADT/StringExtras.h"
-#include "llvm/Support/Error.h"
+#include "llvm/Support/Mutex.h"
+#include <unordered_set>
 
 namespace clang {
 namespace doc {
@@ -57,10 +58,6 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
                                getLine(D, D->getASTContext()), File,
                                IsFileInRootDir, CDCtx.PublicOnly);
 
-  // Bitcode
-  if (CDCtx.NoBitcode) {
-    return true;
-  }
 
   // A null in place of I indicates that the serializer is skipping this decl
   // for some reason (e.g. we're only reporting public decls).
diff --git a/clang-tools-extra/clang-doc/Mapper.h b/clang-tools-extra/clang-doc/Mapper.h
index 1da7a66f1471a..cedde935ab743 100644
--- a/clang-tools-extra/clang-doc/Mapper.h
+++ b/clang-tools-extra/clang-doc/Mapper.h
@@ -20,8 +20,6 @@
 #include "Representation.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/Tooling/Execution.h"
-#include "llvm/Support/Mutex.h"
-#include <unordered_set>
 
 using namespace clang::comments;
 using namespace clang::tooling;
@@ -55,7 +53,6 @@ class MapASTVisitor : public clang::RecursiveASTVisitor<MapASTVisitor>,
                                     const ASTContext &Context) const;
 
   ClangDocContext CDCtx;
-
 };
 
 } // namespace doc
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 83d2af976105b..08f06f8feed0f 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -18,9 +18,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "BitcodeReader.h"
+#include "BitcodeWriter.h"
 #include "ClangDoc.h"
 #include "Generators.h"
 #include "Representation.h"
+#include "clang/AST/AST.h"
+#include "clang/AST/Decl.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
 #include "clang/ASTMatchers/ASTMatchersInternal.h"
 #include "clang/Driver/Options.h"
@@ -28,11 +31,14 @@
 #include "clang/Tooling/AllTUsExecution.h"
 #include "clang/Tooling/CommonOptionsParser.h"
 #include "clang/Tooling/Execution.h"
+#include "clang/Tooling/Tooling.h"
+#include "llvm/ADT/APFloat.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Error.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Mutex.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/ThreadPool.h"
 #include "llvm/Support/raw_ostream.h"
@@ -65,11 +71,6 @@ static llvm::cl::opt<std::string>
                  llvm::cl::desc("Directory for outputting generated files."),
                  llvm::cl::init("docs"), llvm::cl::cat(ClangDocCategory));
 
-static llvm::cl::opt<bool> NoBitcode(
-     "nobitcode",
-     llvm::cl::desc("Do not emit bitcode for faster processing time"),
-     llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
-
 static llvm::cl::opt<bool>
     PublicOnly("public", llvm::cl::desc("Document only public declarations."),
                llvm::cl::init(false), llvm::cl::cat(ClangDocCategory));
@@ -266,7 +267,6 @@ Example usage for a project using a compile commands database:
       Executor->get()->getExecutionContext(),
       ProjectName,
       PublicOnly,
-      NoBitcode,
       OutDirectory,
       SourceRoot,
       RepositoryUrl,
@@ -313,7 +313,6 @@ Example usage for a project using a compile commands database:
 
   // First reducing phase (reduce all decls into one info per decl).
   llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
-
   std::atomic<bool> Error;
   Error = false;
   llvm::sys::Mutex IndexMutex;

>From 0e3eacf656ac9fde42c967e16c812bd267a923ef Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Wed, 26 Jun 2024 15:45:53 -0400
Subject: [PATCH 05/14] [clang-doc] distinguish between declaration and
 definition

---
 clang-tools-extra/clang-doc/Mapper.cpp | 30 ++++++++++++++++----------
 clang-tools-extra/clang-doc/Mapper.h   |  2 +-
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index b9ca6edd03b8c..a460a506b27f3 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -23,11 +23,14 @@ static std::unordered_set<std::string> USRVisited;
 
 static llvm::sys::Mutex USRVisitedGuard;
 
+
+
 void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
   TraverseDecl(Context.getTranslationUnitDecl());
 }
 
-template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
+template <typename T> bool MapASTVisitor::mapDecl(const T *D,
+                            bool IsDefinition) {
   // If we're looking a decl not in user files, skip this decl.
   if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
     return true;
@@ -45,10 +48,11 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
   {
     std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
     std::string Visited = USR.str().str();
-    if (USRVisited.count(Visited)) {
+    if (USRVisited.count(Visited))
       return true;
-    }
-    USRVisited.insert(Visited);
+    // We considered a USR to be visited only when its defined
+    if (IsDefinition)
+      USRVisited.insert(Visited);
   }
 
   bool IsFileInRootDir;
@@ -71,30 +75,34 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D) {
 }
 
 bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
-  return mapDecl(D);
+  return mapDecl(D, true);
 }
 
-bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) { return mapDecl(D); }
+bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) {
+  return mapDecl(D, D->isThisDeclarationADefinition());
+}
 
-bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) { return mapDecl(D); }
+bool MapASTVisitor::VisitEnumDecl(const EnumDecl *D) {
+  return mapDecl(D, D->isThisDeclarationADefinition());
+}
 
 bool MapASTVisitor::VisitCXXMethodDecl(const CXXMethodDecl *D) {
-  return mapDecl(D);
+  return mapDecl(D, D->isThisDeclarationADefinition());
 }
 
 bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
   // Don't visit CXXMethodDecls twice
   if (isa<CXXMethodDecl>(D))
     return true;
-  return mapDecl(D);
+  return mapDecl(D, D->isThisDeclarationADefinition());
 }
 
 bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {
-  return mapDecl(D);
+  return mapDecl(D, true);
 }
 
 bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {
-  return mapDecl(D);
+  return mapDecl(D, true);
 }
 
 comments::FullComment *
diff --git a/clang-tools-extra/clang-doc/Mapper.h b/clang-tools-extra/clang-doc/Mapper.h
index cedde935ab743..c1315b93786c0 100644
--- a/clang-tools-extra/clang-doc/Mapper.h
+++ b/clang-tools-extra/clang-doc/Mapper.h
@@ -43,7 +43,7 @@ class MapASTVisitor : public clang::RecursiveASTVisitor<MapASTVisitor>,
   bool VisitTypeAliasDecl(const TypeAliasDecl *D);
 
 private:
-  template <typename T> bool mapDecl(const T *D);
+  template <typename T> bool mapDecl(const T *D, bool isDefinition);
 
   int getLine(const NamedDecl *D, const ASTContext &Context) const;
   llvm::SmallString<128> getFile(const NamedDecl *D, const ASTContext &Context,

>From d606b97d3b1cf94e51aa6068d0ddcd3482b66a7f Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 27 Jun 2024 14:53:17 -0400
Subject: [PATCH 06/14] [clang-doc] address pr comments

---
 clang-tools-extra/clang-doc/Mapper.cpp            | 14 ++++----------
 clang-tools-extra/clang-doc/tool/ClangDocMain.cpp |  6 ++++++
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index a460a506b27f3..9af7ac222da1d 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -12,19 +12,15 @@
 #include "clang/AST/Comment.h"
 #include "clang/Index/USRGeneration.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Mutex.h"
-#include <unordered_set>
 
 namespace clang {
 namespace doc {
 
-
-static std::unordered_set<std::string> USRVisited;
-
+static llvm::StringSet USRVisited;
 static llvm::sys::Mutex USRVisitedGuard;
 
-
-
 void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
   TraverseDecl(Context.getTranslationUnitDecl());
 }
@@ -47,12 +43,11 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D,
   // Prevent Visiting USR twice
   {
     std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
-    std::string Visited = USR.str().str();
-    if (USRVisited.count(Visited))
+    if (USRVisited.count(USR.str()))
       return true;
     // We considered a USR to be visited only when its defined
     if (IsDefinition)
-      USRVisited.insert(Visited);
+      USRVisited.insert(USR.str());
   }
 
   bool IsFileInRootDir;
@@ -62,7 +57,6 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D,
                                getLine(D, D->getASTContext()), File,
                                IsFileInRootDir, CDCtx.PublicOnly);
 
-
   // A null in place of I indicates that the serializer is skipping this decl
   // for some reason (e.g. we're only reporting public decls).
   if (I.first)
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 08f06f8feed0f..a3835fd78872d 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -313,6 +313,12 @@ Example usage for a project using a compile commands database:
 
   // First reducing phase (reduce all decls into one info per decl).
   llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
+  int i = 0;
+  for(auto &Group : USRToBitcode) {
+    i += USRToBitcode.size();
+  }
+  llvm::outs() << "USR: " << i << "\n";
+
   std::atomic<bool> Error;
   Error = false;
   llvm::sys::Mutex IndexMutex;

>From 382380729c970c9aaa3c189784fd81cc561fb1be Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 27 Jun 2024 14:54:14 -0400
Subject: [PATCH 07/14] [clang-doc] remove debug code

---
 clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index a3835fd78872d..08f06f8feed0f 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -313,12 +313,6 @@ Example usage for a project using a compile commands database:
 
   // First reducing phase (reduce all decls into one info per decl).
   llvm::outs() << "Reducing " << USRToBitcode.size() << " infos...\n";
-  int i = 0;
-  for(auto &Group : USRToBitcode) {
-    i += USRToBitcode.size();
-  }
-  llvm::outs() << "USR: " << i << "\n";
-
   std::atomic<bool> Error;
   Error = false;
   llvm::sys::Mutex IndexMutex;

>From d4fe3e83f30556bcbecdf95ad97d763cb89421c3 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 27 Jun 2024 14:57:31 -0400
Subject: [PATCH 08/14] [clang-doc] switch to string ref

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

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index 9af7ac222da1d..1d686c7cc9783 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -43,11 +43,12 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D,
   // Prevent Visiting USR twice
   {
     std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
-    if (USRVisited.count(USR.str()))
+    StringRef Visited = USR.str();
+    if (USRVisited.count(Visited))
       return true;
     // We considered a USR to be visited only when its defined
     if (IsDefinition)
-      USRVisited.insert(USR.str());
+      USRVisited.insert(Visited);
   }
 
   bool IsFileInRootDir;

>From de7305bcc0098beafaa1e7413c87f98c56307941 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 11 Jul 2024 02:36:37 -0400
Subject: [PATCH 09/14] [clang-doc] account anonymous typedef

---
 clang-tools-extra/clang-doc/Mapper.cpp | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index 1d686c7cc9783..0cbf94e16691a 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -21,6 +21,16 @@ namespace doc {
 static llvm::StringSet USRVisited;
 static llvm::sys::Mutex USRVisitedGuard;
 
+
+template <typename T> bool isTypedefAnonRecord(const T* D) {
+  if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
+    if (const TypedefNameDecl *TD = C->getTypedefNameForAnonDecl()) {
+      return true;
+    }
+  }
+  return false;
+}
+
 void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
   TraverseDecl(Context.getTranslationUnitDecl());
 }
@@ -44,7 +54,7 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D,
   {
     std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
     StringRef Visited = USR.str();
-    if (USRVisited.count(Visited))
+    if (USRVisited.count(Visited) && !isTypedefAnonRecord<T>(D))
       return true;
     // We considered a USR to be visited only when its defined
     if (IsDefinition)

>From 34824a92448704b3b24515fa63ebf16d3280e9ef Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 11 Jul 2024 18:31:56 -0400
Subject: [PATCH 10/14] [clang-doc] clang-format + addressing pr comments

---
 clang-tools-extra/clang-doc/Mapper.cpp | 17 +++++++----------
 clang-tools-extra/clang-doc/Mapper.h   |  2 +-
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index 0cbf94e16691a..eda6113626a27 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -21,12 +21,9 @@ namespace doc {
 static llvm::StringSet USRVisited;
 static llvm::sys::Mutex USRVisitedGuard;
 
-
-template <typename T> bool isTypedefAnonRecord(const T* D) {
+template <typename T> bool isTypedefAnonRecord(const T *D) {
   if (const auto *C = dyn_cast<CXXRecordDecl>(D)) {
-    if (const TypedefNameDecl *TD = C->getTypedefNameForAnonDecl()) {
-      return true;
-    }
+    return C->getTypedefNameForAnonDecl();
   }
   return false;
 }
@@ -35,8 +32,8 @@ void MapASTVisitor::HandleTranslationUnit(ASTContext &Context) {
   TraverseDecl(Context.getTranslationUnitDecl());
 }
 
-template <typename T> bool MapASTVisitor::mapDecl(const T *D,
-                            bool IsDefinition) {
+template <typename T>
+bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
   // If we're looking a decl not in user files, skip this decl.
   if (D->getASTContext().getSourceManager().isInSystemHeader(D->getLocation()))
     return true;
@@ -80,7 +77,7 @@ template <typename T> bool MapASTVisitor::mapDecl(const T *D,
 }
 
 bool MapASTVisitor::VisitNamespaceDecl(const NamespaceDecl *D) {
-  return mapDecl(D, true);
+  return mapDecl(D, /*isDefinition=*/true);
 }
 
 bool MapASTVisitor::VisitRecordDecl(const RecordDecl *D) {
@@ -103,11 +100,11 @@ bool MapASTVisitor::VisitFunctionDecl(const FunctionDecl *D) {
 }
 
 bool MapASTVisitor::VisitTypedefDecl(const TypedefDecl *D) {
-  return mapDecl(D, true);
+  return mapDecl(D, /*isDefinition=*/true);
 }
 
 bool MapASTVisitor::VisitTypeAliasDecl(const TypeAliasDecl *D) {
-  return mapDecl(D, true);
+  return mapDecl(D, /*isDefinition=*/true);
 }
 
 comments::FullComment *
diff --git a/clang-tools-extra/clang-doc/Mapper.h b/clang-tools-extra/clang-doc/Mapper.h
index c1315b93786c0..75c8e947c8f90 100644
--- a/clang-tools-extra/clang-doc/Mapper.h
+++ b/clang-tools-extra/clang-doc/Mapper.h
@@ -43,7 +43,7 @@ class MapASTVisitor : public clang::RecursiveASTVisitor<MapASTVisitor>,
   bool VisitTypeAliasDecl(const TypeAliasDecl *D);
 
 private:
-  template <typename T> bool mapDecl(const T *D, bool isDefinition);
+  template <typename T> bool mapDecl(const T *D, bool IsDefinition);
 
   int getLine(const NamedDecl *D, const ASTContext &Context) const;
   llvm::SmallString<128> getFile(const NamedDecl *D, const ASTContext &Context,

>From 5cb8482fcba939128ffdfc9f4142c5fad88b2b7d Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Thu, 11 Jul 2024 18:33:16 -0400
Subject: [PATCH 11/14] [clang-doc] fix compiler warning

---
 clang-tools-extra/clang-doc/Mapper.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index eda6113626a27..0ee827ebda498 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -18,7 +18,7 @@
 namespace clang {
 namespace doc {
 
-static llvm::StringSet USRVisited;
+static llvm::StringSet<> USRVisited;
 static llvm::sys::Mutex USRVisitedGuard;
 
 template <typename T> bool isTypedefAnonRecord(const T *D) {

>From 6de47d31490549f6d2356e4e388bfdbf85d67c62 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Sat, 13 Jul 2024 01:21:54 -0400
Subject: [PATCH 12/14] [clang-doc] status

---
 clang-tools-extra/clang-doc/Mapper.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index 0ee827ebda498..e0afd267ab3fb 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -46,7 +46,6 @@ bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
   // If there is an error generating a USR for the decl, skip this decl.
   if (index::generateUSRForDecl(D, USR))
     return true;
-
   // Prevent Visiting USR twice
   {
     std::lock_guard<llvm::sys::Mutex> Guard(USRVisitedGuard);
@@ -57,7 +56,6 @@ bool MapASTVisitor::mapDecl(const T *D, bool IsDefinition) {
     if (IsDefinition)
       USRVisited.insert(Visited);
   }
-
   bool IsFileInRootDir;
   llvm::SmallString<128> File =
       getFile(D, D->getASTContext(), CDCtx.SourceRoot, IsFileInRootDir);
@@ -145,5 +143,7 @@ llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
   return File;
 }
 
+
+
 } // namespace doc
 } // namespace clang

>From 3f09835b9d212db83b794f07f3a814a6a0968fd0 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Tue, 23 Jul 2024 17:28:52 -0400
Subject: [PATCH 13/14] [clang-doc] remove useless try catch

---
 clang-tools-extra/clang-doc/HTMLGenerator.cpp |  2 +-
 .../clang-doc/tool/ClangDocMain.cpp           | 74 +++++++------------
 2 files changed, 27 insertions(+), 49 deletions(-)

diff --git a/clang-tools-extra/clang-doc/HTMLGenerator.cpp b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
index c4df336418484..f6b5e8926f903 100644
--- a/clang-tools-extra/clang-doc/HTMLGenerator.cpp
+++ b/clang-tools-extra/clang-doc/HTMLGenerator.cpp
@@ -983,7 +983,7 @@ static llvm::Error serializeIndex(ClangDocContext &CDCtx) {
   llvm::json::OStream J(OS, 2);
   std::function<void(Index)> IndexToJSON = [&](const Index &I) {
     J.object([&] {
-      //J.attribute("USR", toHex(llvm::toStringRef(I.USR)));
+      J.attribute("USR", toHex(llvm::toStringRef(I.USR)));
       J.attribute("Name", I.Name);
       J.attribute("RefType", getRefType(I.RefType));
       J.attribute("Path", I.getRelativeFilePath(""));
diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
index 08f06f8feed0f..eeb2f0d9a70bc 100644
--- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
+++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp
@@ -45,10 +45,6 @@
 #include <atomic>
 #include <mutex>
 #include <string>
-#include <exception>
-#include <typeinfo>
-#include <stdexcept>
-
 
 using namespace clang::ast_matchers;
 using namespace clang::tooling;
@@ -209,19 +205,6 @@ llvm::Error getHtmlAssetFiles(const char *Argv0,
   return getDefaultAssetFiles(Argv0, CDCtx);
 }
 
-void handle_eptr(std::exception_ptr eptr) // passing by value is OK
-{
-  try
-  {
-    if (eptr)
-      std::rethrow_exception(eptr);
-  }
-  catch(const std::exception& e)
-  {
-    llvm::outs() << "Caught exception: '" << e.what() << "'\n";
-  }
-}
-
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
   std::error_code OK;
@@ -320,41 +303,36 @@ Example usage for a project using a compile commands database:
   llvm::DefaultThreadPool Pool(llvm::hardware_concurrency(ExecutorConcurrency));
   for (auto &Group : USRToBitcode) {
     Pool.async([&]() {
-      try {
-        std::vector<std::unique_ptr<doc::Info>> Infos;
-        for (auto &Bitcode : Group.getValue()) {
-          llvm::BitstreamCursor Stream(Bitcode);
-          doc::ClangDocBitcodeReader Reader(Stream);
-          auto ReadInfos = Reader.readBitcode();
-          if (!ReadInfos) {
-            llvm::errs() << toString(ReadInfos.takeError()) << "\n";
-            Error = true;
-            return;
-          }
-          std::move(ReadInfos->begin(), ReadInfos->end(),
-                    std::back_inserter(Infos));
-        }
-
-        auto Reduced = doc::mergeInfos(Infos);
-        if (!Reduced) {
-          llvm::errs() << llvm::toString(Reduced.takeError());
+      std::vector<std::unique_ptr<doc::Info>> Infos;
+      for (auto &Bitcode : Group.getValue()) {
+        llvm::BitstreamCursor Stream(Bitcode);
+        doc::ClangDocBitcodeReader Reader(Stream);
+        auto ReadInfos = Reader.readBitcode();
+        if (!ReadInfos) {
+          llvm::errs() << toString(ReadInfos.takeError()) << "\n";
+          Error = true;
           return;
         }
+        std::move(ReadInfos->begin(), ReadInfos->end(),
+                  std::back_inserter(Infos));
+      }
 
-        // Add a reference to this Info in the Index
-        {
-          std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
-          clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
-        }
+      auto Reduced = doc::mergeInfos(Infos);
+      if (!Reduced) {
+        llvm::errs() << llvm::toString(Reduced.takeError());
+        return;
+      }
 
-        // Save in the result map (needs a lock due to threaded access).
-        {
-          std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
-          USRToInfo[Group.getKey()] = std::move(Reduced.get());
-        }
-      } catch (...) {
-        std::exception_ptr P = std::current_exception();
-        handle_eptr(P);
+      // Add a reference to this Info in the Index
+      {
+        std::lock_guard<llvm::sys::Mutex> Guard(IndexMutex);
+        clang::doc::Generator::addInfoToIndex(CDCtx.Idx, Reduced.get().get());
+      }
+
+      // Save in the result map (needs a lock due to threaded access).
+      {
+        std::lock_guard<llvm::sys::Mutex> Guard(USRToInfoMutex);
+        USRToInfo[Group.getKey()] = std::move(Reduced.get());
       }
     });
   }

>From b2c358c1048e66dc827bf6994e838a1a67721d95 Mon Sep 17 00:00:00 2001
From: PeterChou1 <peter.chou at mail.utoronto.ca>
Date: Tue, 23 Jul 2024 17:40:41 -0400
Subject: [PATCH 14/14] [clang-doc] clang-format

---
 clang-tools-extra/clang-doc/Mapper.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/clang-tools-extra/clang-doc/Mapper.cpp b/clang-tools-extra/clang-doc/Mapper.cpp
index e0afd267ab3fb..6c90db03424c6 100644
--- a/clang-tools-extra/clang-doc/Mapper.cpp
+++ b/clang-tools-extra/clang-doc/Mapper.cpp
@@ -143,7 +143,5 @@ llvm::SmallString<128> MapASTVisitor::getFile(const NamedDecl *D,
   return File;
 }
 
-
-
 } // namespace doc
 } // namespace clang



More information about the cfe-commits mailing list