[Lldb-commits] [lldb] [lldb] Change interface of StructuredData::Array::GetItemAtIndexAsInteger (PR #71993)

Alex Langford via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 10 14:28:02 PST 2023


https://github.com/bulbazord created https://github.com/llvm/llvm-project/pull/71993

This is a follow-up to (#71613) and (#71961).

>From c6b3f622c32634db628cb76eaca556ae3d8ad6f4 Mon Sep 17 00:00:00 2001
From: Alex Langford <alangford at apple.com>
Date: Fri, 10 Nov 2023 14:06:48 -0800
Subject: [PATCH] [lldb] Change interface of
 StructuredData::Array::GetItemAtIndexAsInteger

This is a follow-up to (#71613) and (#71961).
---
 lldb/include/lldb/Utility/StructuredData.h    | 28 +++++--------------
 .../Breakpoint/BreakpointResolverName.cpp     |  8 +++---
 .../TSan/InstrumentationRuntimeTSan.cpp       |  5 ++--
 lldb/source/Target/DynamicRegisterInfo.cpp    | 15 +++++-----
 4 files changed, 21 insertions(+), 35 deletions(-)

diff --git a/lldb/include/lldb/Utility/StructuredData.h b/lldb/include/lldb/Utility/StructuredData.h
index 8d0ae372f43c6bf..35fcfca8dd98c67 100644
--- a/lldb/include/lldb/Utility/StructuredData.h
+++ b/lldb/include/lldb/Utility/StructuredData.h
@@ -221,31 +221,17 @@ class StructuredData {
     }
 
     template <class IntType>
-    bool GetItemAtIndexAsInteger(size_t idx, IntType &result) const {
-      ObjectSP value_sp = GetItemAtIndex(idx);
-      if (value_sp.get()) {
+    std::optional<IntType> GetItemAtIndexAsInteger(size_t idx) const {
+      if (auto item_sp = GetItemAtIndex(idx)) {
         if constexpr (std::numeric_limits<IntType>::is_signed) {
-          if (auto signed_value = value_sp->GetAsSignedInteger()) {
-            result = static_cast<IntType>(signed_value->GetValue());
-            return true;
-          }
+          if (auto *signed_value = item_sp->GetAsSignedInteger())
+            return static_cast<IntType>(signed_value->GetValue());
         } else {
-          if (auto unsigned_value = value_sp->GetAsUnsignedInteger()) {
-            result = static_cast<IntType>(unsigned_value->GetValue());
-            return true;
-          }
+          if (auto *unsigned_value = item_sp->GetAsUnsignedInteger())
+            return static_cast<IntType>(unsigned_value->GetValue());
         }
       }
-      return false;
-    }
-
-    template <class IntType>
-    bool GetItemAtIndexAsInteger(size_t idx, IntType &result,
-                                 IntType default_val) const {
-      bool success = GetItemAtIndexAsInteger(idx, result);
-      if (!success)
-        result = default_val;
-      return success;
+      return {};
     }
 
     std::optional<llvm::StringRef> GetItemAtIndexAsString(size_t idx) const {
diff --git a/lldb/source/Breakpoint/BreakpointResolverName.cpp b/lldb/source/Breakpoint/BreakpointResolverName.cpp
index 0097046cf511b5d..07ff597e10a5ca0 100644
--- a/lldb/source/Breakpoint/BreakpointResolverName.cpp
+++ b/lldb/source/Breakpoint/BreakpointResolverName.cpp
@@ -161,14 +161,14 @@ BreakpointResolverSP BreakpointResolverName::CreateFromStructuredData(
         error.SetErrorString("BRN::CFSD: name entry is not a string.");
         return nullptr;
       }
-      std::underlying_type<FunctionNameType>::type fnt;
-      success = names_mask_array->GetItemAtIndexAsInteger(i, fnt);
-      if (!success) {
+      auto maybe_fnt = names_mask_array->GetItemAtIndexAsInteger<
+          std::underlying_type<FunctionNameType>::type>(i);
+      if (!maybe_fnt) {
         error.SetErrorString("BRN::CFSD: name mask entry is not an integer.");
         return nullptr;
       }
       names.push_back(std::string(*maybe_name));
-      name_masks.push_back(static_cast<FunctionNameType>(fnt));
+      name_masks.push_back(static_cast<FunctionNameType>(*maybe_fnt));
     }
 
     std::shared_ptr<BreakpointResolverName> resolver_sp =
diff --git a/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp b/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp
index 2a35256a6fb0bf3..72293c5331f40da 100644
--- a/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp
+++ b/lldb/source/Plugins/InstrumentationRuntime/TSan/InstrumentationRuntimeTSan.cpp
@@ -592,9 +592,10 @@ addr_t InstrumentationRuntimeTSan::GetFirstNonInternalFramePc(
     if (skip_one_frame && i == 0)
       continue;
 
-    addr_t addr;
-    if (!trace_array->GetItemAtIndexAsInteger(i, addr))
+    auto maybe_addr = trace_array->GetItemAtIndexAsInteger<addr_t>(i);
+    if (!maybe_addr)
       continue;
+    addr_t addr = *maybe_addr;
 
     lldb_private::Address so_addr;
     if (!process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
diff --git a/lldb/source/Target/DynamicRegisterInfo.cpp b/lldb/source/Target/DynamicRegisterInfo.cpp
index 7469c1d4259afc2..1a817449fa95896 100644
--- a/lldb/source/Target/DynamicRegisterInfo.cpp
+++ b/lldb/source/Target/DynamicRegisterInfo.cpp
@@ -349,10 +349,8 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
       const size_t num_regs = invalidate_reg_list->GetSize();
       if (num_regs > 0) {
         for (uint32_t idx = 0; idx < num_regs; ++idx) {
-          uint64_t invalidate_reg_num;
-          std::optional<llvm::StringRef> maybe_invalidate_reg_name =
-              invalidate_reg_list->GetItemAtIndexAsString(idx);
-          if (maybe_invalidate_reg_name) {
+          if (auto maybe_invalidate_reg_name =
+                  invalidate_reg_list->GetItemAtIndexAsString(idx)) {
             const RegisterInfo *invalidate_reg_info =
                 GetRegisterInfo(*maybe_invalidate_reg_name);
             if (invalidate_reg_info) {
@@ -365,10 +363,11 @@ DynamicRegisterInfo::SetRegisterInfo(const StructuredData::Dictionary &dict,
                      "\"%s\" while parsing register \"%s\"\n",
                      maybe_invalidate_reg_name->str().c_str(), reg_info.name);
             }
-          } else if (invalidate_reg_list->GetItemAtIndexAsInteger(
-                         idx, invalidate_reg_num)) {
-            if (invalidate_reg_num != UINT64_MAX)
-              m_invalidate_regs_map[i].push_back(invalidate_reg_num);
+          } else if (auto maybe_invalidate_reg_num =
+                         invalidate_reg_list->GetItemAtIndexAsInteger<uint64_t>(
+                             idx)) {
+            if (*maybe_invalidate_reg_num != UINT64_MAX)
+              m_invalidate_regs_map[i].push_back(*maybe_invalidate_reg_num);
             else
               printf("error: 'invalidate-regs' list value wasn't a valid "
                      "integer\n");



More information about the lldb-commits mailing list