[Lldb-commits] [lldb] 8280896 - [lldb] Remove RTTI in ClangExternalASTSourceCommon based on a global map of known instances

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Sun Dec 15 13:45:58 PST 2019


Author: Raphael Isemann
Date: 2019-12-15T22:39:50+01:00
New Revision: 8280896bd1b055a192d9e7d482b0ffa14ee88e3a

URL: https://github.com/llvm/llvm-project/commit/8280896bd1b055a192d9e7d482b0ffa14ee88e3a
DIFF: https://github.com/llvm/llvm-project/commit/8280896bd1b055a192d9e7d482b0ffa14ee88e3a.diff

LOG: [lldb] Remove RTTI in ClangExternalASTSourceCommon based on a global map of known instances

Summary:
Currently we do our RTTI check for ClangExternalASTSourceCommon by using this global map of
ClangExternalASTSourceCommon where every instance is registering and deregistering itself
on creation/destruction. Then we can do the RTTI check by looking up in this map from ClangASTContext.

This patch removes this whole thing and just adds LLVM-style RTTI support to ClangExternalASTSourceCommon
which is possible with D71397.

Reviewers: labath, aprantl

Reviewed By: labath

Subscribers: JDevlieghere, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D71398

Added: 
    

Modified: 
    lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
    lldb/source/Symbol/ClangASTContext.cpp
    lldb/source/Symbol/ClangExternalASTSourceCommon.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h b/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
index e7dd94d28286..5da486540bb9 100644
--- a/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
+++ b/lldb/include/lldb/Symbol/ClangExternalASTSourceCommon.h
@@ -122,8 +122,11 @@ class ClangASTMetadata {
 };
 
 class ClangExternalASTSourceCommon : public clang::ExternalASTSource {
+
+  /// LLVM-style RTTI.
+  static char ID;
+
 public:
-  ClangExternalASTSourceCommon();
   ~ClangExternalASTSourceCommon() override;
 
   ClangASTMetadata *GetMetadata(const clang::Decl *object);
@@ -138,8 +141,13 @@ class ClangExternalASTSourceCommon : public clang::ExternalASTSource {
     m_type_metadata[object] = metadata;
   }
 
-  static ClangExternalASTSourceCommon *Lookup(clang::ExternalASTSource *source);
-
+  /// LLVM-style RTTI.
+  /// \{
+  bool isA(const void *ClassID) const override {
+    return ClassID == &ID || ExternalASTSource::isA(ClassID);
+  }
+  static bool classof(const ExternalASTSource *S) { return S->isA(&ID); }
+  /// \}
 private:
   typedef llvm::DenseMap<const clang::Decl *, ClangASTMetadata> DeclMetadataMap;
   typedef llvm::DenseMap<const clang::Type *, ClangASTMetadata> TypeMetadataMap;

diff  --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp
index 2576a372076c..ed613528a2f4 100644
--- a/lldb/source/Symbol/ClangASTContext.cpp
+++ b/lldb/source/Symbol/ClangASTContext.cpp
@@ -2414,41 +2414,31 @@ void ClangASTContext::SetMetadataAsUserID(const clang::Type *type,
 
 void ClangASTContext::SetMetadata(const clang::Decl *object,
                                   ClangASTMetadata &metadata) {
-  ClangExternalASTSourceCommon *external_source =
-      ClangExternalASTSourceCommon::Lookup(
-          getASTContext()->getExternalSource());
-
-  if (external_source)
-    external_source->SetMetadata(object, metadata);
+  if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
+          getASTContext()->getExternalSource()))
+    A->SetMetadata(object, metadata);
 }
 
 void ClangASTContext::SetMetadata(const clang::Type *object,
                                   ClangASTMetadata &metadata) {
-  ClangExternalASTSourceCommon *external_source =
-      ClangExternalASTSourceCommon::Lookup(
-          getASTContext()->getExternalSource());
-
-  if (external_source)
-    external_source->SetMetadata(object, metadata);
+  if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
+          getASTContext()->getExternalSource()))
+    A->SetMetadata(object, metadata);
 }
 
 ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
                                                const clang::Decl *object) {
-  ClangExternalASTSourceCommon *external_source =
-      ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
-
-  if (external_source)
-    return external_source->GetMetadata(object);
+  if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
+          ast->getExternalSource()))
+    return A->GetMetadata(object);
   return nullptr;
 }
 
 ClangASTMetadata *ClangASTContext::GetMetadata(clang::ASTContext *ast,
                                                const clang::Type *object) {
-  ClangExternalASTSourceCommon *external_source =
-      ClangExternalASTSourceCommon::Lookup(ast->getExternalSource());
-
-  if (external_source)
-    return external_source->GetMetadata(object);
+  if (auto *A = llvm::dyn_cast_or_null<ClangExternalASTSourceCommon>(
+          ast->getExternalSource()))
+    return A->GetMetadata(object);
   return nullptr;
 }
 

diff  --git a/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp b/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp
index 66fb4f3b3f05..be015da872d0 100644
--- a/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp
+++ b/lldb/source/Symbol/ClangExternalASTSourceCommon.cpp
@@ -13,43 +13,9 @@
 
 using namespace lldb_private;
 
-typedef llvm::DenseMap<clang::ExternalASTSource *,
-                       ClangExternalASTSourceCommon *>
-    ASTSourceMap;
+char ClangExternalASTSourceCommon::ID;
 
-static ASTSourceMap &GetSourceMap(std::unique_lock<std::mutex> &guard) {
-  // Intentionally leaked to avoid problems with global destructors.
-  static ASTSourceMap *s_source_map = new ASTSourceMap;
-  static std::mutex s_mutex;
-  std::unique_lock<std::mutex> locked_guard(s_mutex);
-  guard.swap(locked_guard);
-  return *s_source_map;
-}
-
-ClangExternalASTSourceCommon *
-ClangExternalASTSourceCommon::Lookup(clang::ExternalASTSource *source) {
-  std::unique_lock<std::mutex> guard;
-  ASTSourceMap &source_map = GetSourceMap(guard);
-
-  ASTSourceMap::iterator iter = source_map.find(source);
-
-  if (iter != source_map.end()) {
-    return iter->second;
-  } else {
-    return nullptr;
-  }
-}
-
-ClangExternalASTSourceCommon::ClangExternalASTSourceCommon()
-    : clang::ExternalASTSource() {
-  std::unique_lock<std::mutex> guard;
-  GetSourceMap(guard)[this] = this;
-}
-
-ClangExternalASTSourceCommon::~ClangExternalASTSourceCommon() {
-  std::unique_lock<std::mutex> guard;
-  GetSourceMap(guard).erase(this);
-}
+ClangExternalASTSourceCommon::~ClangExternalASTSourceCommon() {}
 
 ClangASTMetadata *
 ClangExternalASTSourceCommon::GetMetadata(const clang::Decl *object) {


        


More information about the lldb-commits mailing list