diff --git a/lldb/include/lldb/ValueObject/ValueObject.h b/lldb/include/lldb/ValueObject/ValueObject.h index bf4c47a4b9fe..029563fa7785 100644 --- a/lldb/include/lldb/ValueObject/ValueObject.h +++ b/lldb/include/lldb/ValueObject/ValueObject.h @@ -1012,6 +1012,35 @@ protected: size_t m_children_count = 0; }; + using ValueObjectManagerSP = std::shared_ptr; + + /// The following two functions are helpers for Create methods + /// for ValueObject subclasses that need to optionally receive + /// a parent or external manager. + /// This returns a ValueObjectManagerSP that is either the SP of the + /// parent - if it is non-null, or a new manager if null. + static ValueObjectManagerSP ReuseManagerIfParent(ValueObject *parent) { + ValueObjectManagerSP manager_sp; + if (parent) + manager_sp = parent->GetManager()->shared_from_this(); + else + manager_sp = ValueObjectManager::Create(); + return manager_sp; + } + + /// If manager is null, makes a new ValueObjectManager and sets + /// manager to the new ValueObjectManager. It also returns the + /// shared pointer which is necessary to keep the new manager alive. + static ValueObjectManagerSP + CreateManagerIfEmpty(ValueObjectManager *&manager) { + ValueObjectManagerSP manager_sp; + if (!manager) { + manager_sp = ValueObjectManager::Create(); + manager = manager_sp.get(); + } + return manager_sp; + } + // Classes that inherit from ValueObject can see and modify these /// The parent value object, or nullptr if this has no parent. diff --git a/lldb/include/lldb/ValueObject/ValueObjectConstResult.h b/lldb/include/lldb/ValueObject/ValueObjectConstResult.h index d313e9f09de5..0ada4e13acb4 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectConstResult.h +++ b/lldb/include/lldb/ValueObject/ValueObjectConstResult.h @@ -174,16 +174,6 @@ private: ValueObjectConstResult(ExecutionContextScope *exe_scope, ValueObjectManager &manager, Status &&error); - static std::shared_ptr - CreateManagerIfEmpty(ValueObjectManager *&manager) { - std::shared_ptr manager_sp; - if (!manager) { - manager_sp = ValueObjectManager::Create(); - manager = manager_sp.get(); - } - return manager_sp; - } - ValueObject *CreateChildAtIndex(size_t idx) override { return m_impl.CreateChildAtIndex(idx); } diff --git a/lldb/include/lldb/ValueObject/ValueObjectMemory.h b/lldb/include/lldb/ValueObject/ValueObjectMemory.h index c1bd28434e32..71d100772a57 100644 --- a/lldb/include/lldb/ValueObject/ValueObjectMemory.h +++ b/lldb/include/lldb/ValueObject/ValueObjectMemory.h @@ -34,12 +34,14 @@ public: static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, - lldb::TypeSP &type_sp); + lldb::TypeSP &type_sp, + ValueObject *parent = nullptr); static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, - const CompilerType &ast_type); + const CompilerType &ast_type, + ValueObject *parent = nullptr); llvm::Expected GetByteSize() override; diff --git a/lldb/source/ValueObject/ValueObjectConstResult.cpp b/lldb/source/ValueObject/ValueObjectConstResult.cpp index f5a8cda3d512..e6a09b748c74 100644 --- a/lldb/source/ValueObject/ValueObjectConstResult.cpp +++ b/lldb/source/ValueObject/ValueObjectConstResult.cpp @@ -31,8 +31,7 @@ ValueObjectSP ValueObjectConstResult::Create(ExecutionContextScope *exe_scope, uint32_t addr_byte_size, lldb::addr_t address, ValueObjectManager *manager) { - std::shared_ptr manager_sp = - CreateManagerIfEmpty(manager); + ValueObjectManagerSP manager_sp = CreateManagerIfEmpty(manager); return (new ValueObjectConstResult(exe_scope, *manager, byte_order, addr_byte_size, address)) diff --git a/lldb/source/ValueObject/ValueObjectMemory.cpp b/lldb/source/ValueObject/ValueObjectMemory.cpp index 3d8d80c6ec48..c85b38215608 100644 --- a/lldb/source/ValueObject/ValueObjectMemory.cpp +++ b/lldb/source/ValueObject/ValueObjectMemory.cpp @@ -32,8 +32,11 @@ using namespace lldb_private; ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, - lldb::TypeSP &type_sp) { - auto manager_sp = ValueObjectManager::Create(); + lldb::TypeSP &type_sp, + ValueObject *parent) { + + std::shared_ptr manager_sp = + ValueObject::ReuseManagerIfParent(parent); return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp)) ->GetSP(); } @@ -41,8 +44,10 @@ ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope, ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, - const CompilerType &ast_type) { - auto manager_sp = ValueObjectManager::Create(); + const CompilerType &ast_type, + ValueObject *parent) { + std::shared_ptr manager_sp = + ValueObject::ReuseManagerIfParent(parent); return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, ast_type)) ->GetSP();