https://github.com/PeterChou1 updated https://github.com/llvm/llvm-project/pull/96809
>From 7733243a7b9660ce2bcbd43f6219314fcd1e4a73 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Wed, 26 Jun 2024 14:20:03 -0400 Subject: [PATCH 1/8] [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/Representation.cpp | 8 +++---- clang-tools-extra/clang-doc/Representation.h | 7 +++++-- .../clang-doc/tool/ClangDocMain.cpp | 13 ++++++------ 5 files changed, 40 insertions(+), 12 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/Representation.cpp b/clang-tools-extra/clang-doc/Representation.cpp index 2afff2929cf79..1ac662b4dd3d2 100644 --- a/clang-tools-extra/clang-doc/Representation.cpp +++ b/clang-tools-extra/clang-doc/Representation.cpp @@ -366,13 +366,13 @@ void Index::sort() { ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName, bool PublicOnly, - StringRef OutDirectory, StringRef SourceRoot, - StringRef RepositoryUrl, + bool NoBitcode, StringRef OutDirectory, + StringRef SourceRoot, StringRef RepositoryUrl, std::vector<std::string> UserStylesheets, std::vector<std::string> JsScripts) : ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly), - OutDirectory(OutDirectory), UserStylesheets(UserStylesheets), - JsScripts(JsScripts) { + NoBitcode(NoBitcode), OutDirectory(OutDirectory), + UserStylesheets(UserStylesheets), JsScripts(JsScripts) { llvm::SmallString<128> SourceRootDir(SourceRoot); if (SourceRoot.empty()) // If no SourceRoot was provided the current path is used as the default diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h index a6b144eb7fa2a..00f4f722f8b5a 100644 --- a/clang-tools-extra/clang-doc/Representation.h +++ b/clang-tools-extra/clang-doc/Representation.h @@ -480,12 +480,15 @@ mergeInfos(std::vector<std::unique_ptr<Info>> &Values); struct ClangDocContext { ClangDocContext() = default; ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName, - bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot, - StringRef RepositoryUrl, + bool NoBitcode, bool PublicOnly, StringRef OutDirectory, + StringRef SourceRoot, StringRef RepositoryUrl, std::vector<std::string> UserStylesheets, std::vector<std::string> JsScripts); + + llvm::StringMap<std::vector<std::unique_ptr<Info>>> Infos; tooling::ExecutionContext *ECtx; std::string ProjectName; // Name of project clang-doc is documenting. + bool NoBitcode; // Indicate if clang-doc will serialize Decl into Bitcode bool PublicOnly; // Indicates if only public declarations are documented. std::string OutDirectory; // Directory for outputting generated files. std::string SourceRoot; // Directory where processed files are stored. Links diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index 5517522d7967d..fc111e090d72a 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" @@ -67,6 +61,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)); @@ -175,6 +174,7 @@ Example usage for a project using a compile commands database: Executor->get()->getExecutionContext(), ProjectName, PublicOnly, + NoBitcode, OutDirectory, SourceRoot, RepositoryUrl, @@ -234,6 +234,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 59fa4d9d31961fd57fd5d28ef171b48fb7fb99d7 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Wed, 26 Jun 2024 14:36:09 -0400 Subject: [PATCH 2/8] [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/Representation.cpp | 8 ++++---- clang-tools-extra/clang-doc/Representation.h | 7 ++----- clang-tools-extra/clang-doc/tool/ClangDocMain.cpp | 13 ++++++------- 5 files changed, 14 insertions(+), 24 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/Representation.cpp b/clang-tools-extra/clang-doc/Representation.cpp index 1ac662b4dd3d2..2afff2929cf79 100644 --- a/clang-tools-extra/clang-doc/Representation.cpp +++ b/clang-tools-extra/clang-doc/Representation.cpp @@ -366,13 +366,13 @@ void Index::sort() { ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName, bool PublicOnly, - bool NoBitcode, StringRef OutDirectory, - StringRef SourceRoot, StringRef RepositoryUrl, + StringRef OutDirectory, StringRef SourceRoot, + StringRef RepositoryUrl, std::vector<std::string> UserStylesheets, std::vector<std::string> JsScripts) : ECtx(ECtx), ProjectName(ProjectName), PublicOnly(PublicOnly), - NoBitcode(NoBitcode), OutDirectory(OutDirectory), - UserStylesheets(UserStylesheets), JsScripts(JsScripts) { + OutDirectory(OutDirectory), UserStylesheets(UserStylesheets), + JsScripts(JsScripts) { llvm::SmallString<128> SourceRootDir(SourceRoot); if (SourceRoot.empty()) // If no SourceRoot was provided the current path is used as the default diff --git a/clang-tools-extra/clang-doc/Representation.h b/clang-tools-extra/clang-doc/Representation.h index 00f4f722f8b5a..a6b144eb7fa2a 100644 --- a/clang-tools-extra/clang-doc/Representation.h +++ b/clang-tools-extra/clang-doc/Representation.h @@ -480,15 +480,12 @@ mergeInfos(std::vector<std::unique_ptr<Info>> &Values); struct ClangDocContext { ClangDocContext() = default; ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName, - bool NoBitcode, bool PublicOnly, StringRef OutDirectory, - StringRef SourceRoot, StringRef RepositoryUrl, + bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot, + StringRef RepositoryUrl, std::vector<std::string> UserStylesheets, std::vector<std::string> JsScripts); - - llvm::StringMap<std::vector<std::unique_ptr<Info>>> Infos; tooling::ExecutionContext *ECtx; std::string ProjectName; // Name of project clang-doc is documenting. - bool NoBitcode; // Indicate if clang-doc will serialize Decl into Bitcode bool PublicOnly; // Indicates if only public declarations are documented. std::string OutDirectory; // Directory for outputting generated files. std::string SourceRoot; // Directory where processed files are stored. Links diff --git a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp index fc111e090d72a..5517522d7967d 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" @@ -61,11 +67,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)); @@ -174,7 +175,6 @@ Example usage for a project using a compile commands database: Executor->get()->getExecutionContext(), ProjectName, PublicOnly, - NoBitcode, OutDirectory, SourceRoot, RepositoryUrl, @@ -234,7 +234,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 54fe1e06e39ab6d5a865e6450f51cadbc7cd29f9 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Wed, 26 Jun 2024 15:45:53 -0400 Subject: [PATCH 3/8] [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 d85c34aeee9009c57fb04925600e878c790119df Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 27 Jun 2024 14:53:17 -0400 Subject: [PATCH 4/8] [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 5517522d7967d..c71d648348285 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -234,6 +234,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 7beb0934126798c9a1545c0fbd08795c0cd6a889 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 27 Jun 2024 14:54:14 -0400 Subject: [PATCH 5/8] [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 c71d648348285..5517522d7967d 100644 --- a/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp +++ b/clang-tools-extra/clang-doc/tool/ClangDocMain.cpp @@ -234,12 +234,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 19564b20612007df294edfaef00f10efdd5d3c8c Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 27 Jun 2024 14:57:31 -0400 Subject: [PATCH 6/8] [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 74c7e08211d8ddfe858dcdd610be6badf3afa4e7 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 11 Jul 2024 02:36:37 -0400 Subject: [PATCH 7/8] [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 d0cab22654ab69a6930a5ce8882fd4f68c866df1 Mon Sep 17 00:00:00 2001 From: PeterChou1 <peter.c...@mail.utoronto.ca> Date: Thu, 11 Jul 2024 18:31:56 -0400 Subject: [PATCH 8/8] [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, _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits