[lldb] Remove RTTI in ClangExternalASTSourceCommon based on a global map of known...
authorRaphael Isemann <risemann@apple.com>
Sun, 15 Dec 2019 21:39:36 +0000 (22:39 +0100)
committerRaphael Isemann <risemann@apple.com>
Sun, 15 Dec 2019 21:39:50 +0000 (22:39 +0100)
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

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

index e7dd94d..5da4865 100644 (file)
@@ -122,8 +122,11 @@ private:
 };
 
 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 @@ public:
     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;
index 2576a37..ed61352 100644 (file)
@@ -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;
 }
 
index 66fb4f3..be015da 100644 (file)
 
 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) {