xiaobai created this revision.
xiaobai added reviewers: JDevlieghere, teemperor, labath, clayborg.
Herald added a reviewer: martong.
Herald added a reviewer: a.sidorin.
Herald added a reviewer: shafik.
Herald added a project: LLDB.

Target is one of the classes responsible for vending ClangASTImporter.
Target doesn't need to know anything about ClangASTImporter, so if we
instead create a map from Targets to ClangASTImporters, we can preserve
existing behavior while improving layering and removing dependencies.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72946

Files:
  lldb/include/lldb/Symbol/ClangASTImporter.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
  lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
  lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
  lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
  lldb/source/Symbol/ClangASTContext.cpp
  lldb/source/Symbol/ClangASTImporter.cpp
  lldb/source/Target/Target.cpp

Index: lldb/source/Target/Target.cpp
===================================================================
--- lldb/source/Target/Target.cpp
+++ lldb/source/Target/Target.cpp
@@ -37,7 +37,6 @@
 #include "lldb/Interpreter/OptionGroupWatchpoint.h"
 #include "lldb/Interpreter/OptionValues.h"
 #include "lldb/Interpreter/Property.h"
-#include "lldb/Symbol/ClangASTImporter.h"
 #include "lldb/Symbol/Function.h"
 #include "lldb/Symbol/ObjectFile.h"
 #include "lldb/Symbol/Symbol.h"
@@ -91,7 +90,7 @@
       m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(),
       m_breakpoint_list(false), m_internal_breakpoint_list(true),
       m_watchpoint_list(), m_process_sp(), m_search_filter_sp(),
-      m_image_search_paths(ImageSearchPathsChanged, this), m_ast_importer_sp(),
+      m_image_search_paths(ImageSearchPathsChanged, this),
       m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0),
       m_valid(true), m_suppress_stop_hooks(false),
       m_is_dummy_target(is_dummy_target),
@@ -1378,7 +1377,6 @@
   m_section_load_history.Clear();
   m_images.Clear();
   m_scratch_type_system_map.Clear();
-  m_ast_importer_sp.reset();
 }
 
 void Target::DidExec() {
@@ -2258,16 +2256,6 @@
   return utility_fn;
 }
 
-ClangASTImporterSP Target::GetClangASTImporter() {
-  if (m_valid) {
-    if (!m_ast_importer_sp) {
-      m_ast_importer_sp = std::make_shared<ClangASTImporter>();
-    }
-    return m_ast_importer_sp;
-  }
-  return ClangASTImporterSP();
-}
-
 void Target::SettingsInitialize() { Process::SettingsInitialize(); }
 
 void Target::SettingsTerminate() { Process::SettingsTerminate(); }
Index: lldb/source/Symbol/ClangASTImporter.cpp
===================================================================
--- lldb/source/Symbol/ClangASTImporter.cpp
+++ lldb/source/Symbol/ClangASTImporter.cpp
@@ -25,6 +25,41 @@
 using namespace lldb_private;
 using namespace clang;
 
+typedef std::map<Target *, lldb::ClangASTImporterSP> ClangASTImporterMap;
+
+static ClangASTImporterMap &GetImporterMap() {
+  static ClangASTImporterMap *g_map = nullptr;
+  static llvm::once_flag g_initialize;
+
+  llvm::call_once(g_initialize, [] { g_map = new ClangASTImporterMap(); });
+  return *g_map;
+}
+
+static std::mutex &GetImporterMapMutex() {
+  static std::mutex *g_mutex = nullptr;
+  static llvm::once_flag g_initialize;
+
+  llvm::call_once(g_initialize, [] { g_mutex = new std::mutex(); });
+
+  return *g_mutex;
+}
+
+lldb::ClangASTImporterSP ClangASTImporter::Get(Target *target) {
+  if (!target || !target->IsValid()) {
+    return lldb::ClangASTImporterSP();
+  }
+
+  std::lock_guard<std::mutex> guard(GetImporterMapMutex());
+  ClangASTImporterMap &map = GetImporterMap();
+  ClangASTImporterMap::iterator pos = map.find(target);
+  if (pos == map.end()) {
+    auto ast_importer_sp = std::make_shared<ClangASTImporter>();
+    map[target] = ast_importer_sp;
+    return ast_importer_sp;
+  }
+  return pos->second;
+}
+
 CompilerType ClangASTImporter::CopyType(ClangASTContext &dst_ast,
                                         const CompilerType &src_type) {
   clang::ASTContext &dst_clang_ast = dst_ast.getASTContext();
Index: lldb/source/Symbol/ClangASTContext.cpp
===================================================================
--- lldb/source/Symbol/ClangASTContext.cpp
+++ lldb/source/Symbol/ClangASTContext.cpp
@@ -9242,7 +9242,7 @@
     : ClangASTContext(triple), m_target_wp(target.shared_from_this()),
       m_persistent_variables(new ClangPersistentVariables) {
   m_scratch_ast_source_up.reset(new ClangASTSource(
-      target.shared_from_this(), target.GetClangASTImporter()));
+      target.shared_from_this(), ClangASTImporter::Get(&target)));
   m_scratch_ast_source_up->InstallASTContext(*this);
   llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(
       m_scratch_ast_source_up->CreateProxy());
Index: lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
===================================================================
--- lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
+++ lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp
@@ -56,7 +56,8 @@
       return;
     }
 
-    ClangASTImporterSP clang_ast_importer = target_sp->GetClangASTImporter();
+    ClangASTImporterSP clang_ast_importer =
+        ClangASTImporter::Get(target_sp.get());
 
     if (!clang_ast_importer) {
       return;
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp
@@ -161,5 +161,5 @@
     ExecutionContext &exe_ctx, bool keep_result_in_memory) {
   m_expr_decl_map_up.reset(new ClangExpressionDeclMap(
       keep_result_in_memory, nullptr, exe_ctx.GetTargetSP(),
-      exe_ctx.GetTargetRef().GetClangASTImporter(), nullptr));
+      ClangASTImporter::Get(exe_ctx.GetTargetPtr()), nullptr));
 }
Index: lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp
@@ -898,7 +898,7 @@
     ValueObject *ctx_obj) {
   m_expr_decl_map_up.reset(new ClangExpressionDeclMap(
       keep_result_in_memory, &delegate, exe_ctx.GetTargetSP(),
-      exe_ctx.GetTargetRef().GetClangASTImporter(), ctx_obj));
+      ClangASTImporter::Get(exe_ctx.GetTargetPtr()), ctx_obj));
 }
 
 clang::ASTConsumer *
Index: lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
===================================================================
--- lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
+++ lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp
@@ -459,7 +459,7 @@
     StringRef name = decl->getName();
     ConstString name_cs(name.str().c_str());
 
-    Decl *D_scratch = m_target.GetClangASTImporter()->DeportDecl(
+    Decl *D_scratch = ClangASTImporter::Get(&m_target)->DeportDecl(
         &scratch_ctx->getASTContext(), decl);
 
     if (!D_scratch) {
Index: lldb/include/lldb/Target/Target.h
===================================================================
--- lldb/include/lldb/Target/Target.h
+++ lldb/include/lldb/Target/Target.h
@@ -1054,8 +1054,6 @@
                                                  const char *name,
                                                  Status &error);
 
-  lldb::ClangASTImporterSP GetClangASTImporter();
-
   // Install any files through the platform that need be to installed prior to
   // launching or attaching.
   Status Install(ProcessLaunchInfo *launch_info);
@@ -1302,7 +1300,6 @@
   typedef std::map<lldb::LanguageType, lldb::REPLSP> REPLMap;
   REPLMap m_repl_map;
 
-  lldb::ClangASTImporterSP m_ast_importer_sp;
   lldb::ClangModulesDeclVendorUP m_clang_modules_decl_vendor_up;
 
   lldb::SourceManagerUP m_source_manager_up;
Index: lldb/include/lldb/Symbol/ClangASTImporter.h
===================================================================
--- lldb/include/lldb/Symbol/ClangASTImporter.h
+++ lldb/include/lldb/Symbol/ClangASTImporter.h
@@ -96,6 +96,8 @@
 
   ClangASTMetadata *GetDeclMetadata(const clang::Decl *decl);
 
+  static lldb::ClangASTImporterSP Get(Target *target);
+
   //
   // Namespace maps
   //
@@ -323,6 +325,7 @@
   RecordDeclToLayoutMap m_record_decl_to_layout_map;
 };
 
+
 } // namespace lldb_private
 
 #endif // liblldb_ClangASTImporter_h_
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to