[lldb] Introduce DynamicRegisterInfo::CreateFromDict
authorAlex Langford <alangford@apple.com>
Fri, 9 Jun 2023 23:27:49 +0000 (16:27 -0700)
committerAlex Langford <alangford@apple.com>
Thu, 15 Jun 2023 17:51:17 +0000 (10:51 -0700)
I want to add some error handling to DynamicRegisterInfo because there
are many operations that can fail and many of these operations do not
give meaningful information back to the caller.

To begin that process, I want to add a static method that is responsible
for creating a DynamicRegisterInfo from a StructuredData::Dictionary
(and ArchSpec). This is meant to replace the equivalent constructor
because constructors are ill-equipped to perform error handling.

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

lldb/include/lldb/Target/DynamicRegisterInfo.h
lldb/source/Plugins/OperatingSystem/Python/OperatingSystemPython.cpp
lldb/source/Plugins/Process/scripted/ScriptedThread.cpp
lldb/source/Target/DynamicRegisterInfo.cpp

index 1b1df848315ced27cc6c92e6ddfeeb4d0aaffe1c..22ad6335fe438471fe1d0b3df52d3a14d9465906 100644 (file)
@@ -46,8 +46,8 @@ public:
 
   DynamicRegisterInfo() = default;
 
-  DynamicRegisterInfo(const lldb_private::StructuredData::Dictionary &dict,
-                      const lldb_private::ArchSpec &arch);
+  static std::unique_ptr<DynamicRegisterInfo>
+  Create(const StructuredData::Dictionary &dict, const ArchSpec &arch);
 
   virtual ~DynamicRegisterInfo() = default;
 
index c22003f24f7606c8b8fb73bc42b99ff65d2d1cc9..9560ae108f3e354dcf1441114304961b0af2a07e 100644 (file)
@@ -128,8 +128,9 @@ DynamicRegisterInfo *OperatingSystemPython::GetDynamicRegisterInfo() {
     if (!dictionary)
       return nullptr;
 
-    m_register_info_up = std::make_unique<DynamicRegisterInfo>(
+    m_register_info_up = DynamicRegisterInfo::Create(
         *dictionary, m_process->GetTarget().GetArchitecture());
+    assert(m_register_info_up);
     assert(m_register_info_up->GetNumRegisters() > 0);
     assert(m_register_info_up->GetNumRegisterSets() > 0);
   }
index ac707ffb21711d77a649fb3cfe5d28c852d97ed9..fa2ee723e093bda690af10bba63ab73a1adf002f 100644 (file)
@@ -341,7 +341,7 @@ std::shared_ptr<DynamicRegisterInfo> ScriptedThread::GetDynamicRegisterInfo() {
           LLVM_PRETTY_FUNCTION, "Failed to get scripted thread registers info.",
           error, LLDBLog::Thread);
 
-    m_register_info_sp = std::make_shared<DynamicRegisterInfo>(
+    m_register_info_sp = DynamicRegisterInfo::Create(
         *reg_info, m_scripted_process.GetTarget().GetArchitecture());
   }
 
index 700619959f5c8d5d235571f8a4c8fed39f7ca38b..d577e20b3740c280f99fc31d891e560968776975 100644 (file)
 using namespace lldb;
 using namespace lldb_private;
 
-DynamicRegisterInfo::DynamicRegisterInfo(
-    const lldb_private::StructuredData::Dictionary &dict,
-    const lldb_private::ArchSpec &arch) {
-  SetRegisterInfo(dict, arch);
+std::unique_ptr<DynamicRegisterInfo>
+DynamicRegisterInfo::Create(const StructuredData::Dictionary &dict,
+                            const ArchSpec &arch) {
+  auto dyn_reg_info = std::make_unique<DynamicRegisterInfo>();
+  if (!dyn_reg_info)
+    return nullptr;
+
+  if (dyn_reg_info->SetRegisterInfo(dict, arch) == 0)
+    return nullptr;
+
+  return dyn_reg_info;
 }
 
 DynamicRegisterInfo::DynamicRegisterInfo(DynamicRegisterInfo &&info) {