diff --git a/lldb/include/lldb/Symbol/ClangASTImporter.h b/lldb/include/lldb/Symbol/ClangASTImporter.h --- a/lldb/include/lldb/Symbol/ClangASTImporter.h +++ b/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_ diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h --- a/lldb/include/lldb/Target/Target.h +++ b/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 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; diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp --- a/lldb/source/Plugins/ExpressionParser/Clang/ASTResultSynthesizer.cpp +++ b/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) { diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangUserExpression.cpp +++ b/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 * diff --git a/lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp b/lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp --- a/lldb/source/Plugins/ExpressionParser/Clang/ClangUtilityFunction.cpp +++ b/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)); } diff --git a/lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp b/lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp --- a/lldb/source/Plugins/Language/CPlusPlus/BlockPointer.cpp +++ b/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; diff --git a/lldb/source/Symbol/ClangASTContext.cpp b/lldb/source/Symbol/ClangASTContext.cpp --- a/lldb/source/Symbol/ClangASTContext.cpp +++ b/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 proxy_ast_source( m_scratch_ast_source_up->CreateProxy()); diff --git a/lldb/source/Symbol/ClangASTImporter.cpp b/lldb/source/Symbol/ClangASTImporter.cpp --- a/lldb/source/Symbol/ClangASTImporter.cpp +++ b/lldb/source/Symbol/ClangASTImporter.cpp @@ -25,6 +25,41 @@ using namespace lldb_private; using namespace clang; +typedef std::map 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 guard(GetImporterMapMutex()); + ClangASTImporterMap &map = GetImporterMap(); + ClangASTImporterMap::iterator pos = map.find(target); + if (pos == map.end()) { + auto ast_importer_sp = std::make_shared(); + 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(); diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp --- a/lldb/source/Target/Target.cpp +++ b/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(); - } - return m_ast_importer_sp; - } - return ClangASTImporterSP(); -} - void Target::SettingsInitialize() { Process::SettingsInitialize(); } void Target::SettingsTerminate() { Process::SettingsTerminate(); }