Skip to content

Conversation

medismailben
Copy link
Member

This patch adds setters to the SBStruturedData class to be able to initialize said object from the client side directly.

@medismailben medismailben marked this pull request as ready for review August 20, 2025 00:21
@llvmbot llvmbot added the lldb label Aug 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 20, 2025

@llvm/pr-subscribers-lldb

Author: Med Ismail Bennani (medismailben)

Changes

This patch adds setters to the SBStruturedData class to be able to initialize said object from the client side directly.


Full diff: https://github.com/llvm/llvm-project/pull/154445.diff

5 Files Affected:

  • (modified) lldb/include/lldb/API/SBStructuredData.h (+16)
  • (modified) lldb/include/lldb/Core/StructuredDataImpl.h (+33)
  • (modified) lldb/include/lldb/Utility/StructuredData.h (+17)
  • (modified) lldb/source/API/SBStructuredData.cpp (+49)
  • (modified) lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py (+27)
diff --git a/lldb/include/lldb/API/SBStructuredData.h b/lldb/include/lldb/API/SBStructuredData.h
index f96e169f236ed..cd27610bbb4c6 100644
--- a/lldb/include/lldb/API/SBStructuredData.h
+++ b/lldb/include/lldb/API/SBStructuredData.h
@@ -109,6 +109,22 @@ class SBStructuredData {
   /// Return the generic pointer if this data structure is a generic type.
   lldb::SBScriptObject GetGenericValue() const;
 
+  void SetValueForKey(const char *key, SBStructuredData &value) const;
+
+  void SetIntegerValue(uint64_t value) const;
+
+  void SetUnsignedIntegerValue(uint64_t value) const;
+
+  void SetSignedIntegerValue(int64_t value) const;
+
+  void SetFloatValue(double value) const;
+
+  void SetBooleanValue(bool value) const;
+
+  void SetStringValue(const char *value) const;
+
+  void SetGenericValue(SBScriptObject value) const;
+
 protected:
   friend class SBAttachInfo;
   friend class SBCommandReturnObject;
diff --git a/lldb/include/lldb/Core/StructuredDataImpl.h b/lldb/include/lldb/Core/StructuredDataImpl.h
index fd0a7b94d3a6c..2b98aebeed654 100644
--- a/lldb/include/lldb/Core/StructuredDataImpl.h
+++ b/lldb/include/lldb/Core/StructuredDataImpl.h
@@ -81,6 +81,39 @@ class StructuredDataImpl {
 
   void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
 
+  void SetValueForKey(const char *key,
+                      const StructuredData::ObjectSP &value) const {
+    if (m_data_sp) {
+      auto dict = m_data_sp->GetAsDictionary();
+      if (dict)
+        return dict->AddItem(key, value);
+    }
+  }
+
+  void SetUnsignedIntegerValue(uint64_t value) {
+    m_data_sp = StructuredData::FromInteger(value);
+  }
+
+  void SetSignedIntegerValue(int64_t value) {
+    m_data_sp = StructuredData::FromInteger(value);
+  }
+
+  void SetFloatValue(double value) {
+    m_data_sp = StructuredData::FromFloat(value);
+  }
+
+  void SetBooleanValue(bool value) {
+    m_data_sp = StructuredData::FromBoolean(value);
+  }
+
+  void SetStringValue(std::string value) {
+    m_data_sp = StructuredData::FromString(value);
+  }
+
+  void SetGenericValue(void *value) {
+    m_data_sp = StructuredData::FromGeneric(value);
+  }
+
   lldb::StructuredDataType GetType() const {
     return (m_data_sp ? m_data_sp->GetType() :
         lldb::eStructuredDataTypeInvalid);
diff --git a/lldb/include/lldb/Utility/StructuredData.h b/lldb/include/lldb/Utility/StructuredData.h
index 5e63ef92fac3e..7018a81cd4c66 100644
--- a/lldb/include/lldb/Utility/StructuredData.h
+++ b/lldb/include/lldb/Utility/StructuredData.h
@@ -574,6 +574,23 @@ class StructuredData {
     void *m_object;
   };
 
+  template <typename T> static ObjectSP FromInteger(T value) {
+    return std::make_shared<Integer<T>>(value);
+  }
+
+  static StructuredData::ObjectSP FromFloat(double value) {
+    return std::make_shared<StructuredData::Float>(value);
+  }
+  static StructuredData::ObjectSP FromBoolean(bool value) {
+    return std::make_shared<StructuredData::Boolean>(value);
+  }
+  static StructuredData::ObjectSP FromString(std::string value) {
+    return std::make_shared<StructuredData::String>(value);
+  }
+  static StructuredData::ObjectSP FromGeneric(void *value) {
+    return std::make_shared<StructuredData::Generic>(value);
+  }
+
   static ObjectSP ParseJSON(llvm::StringRef json_text);
   static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error);
   static bool IsRecordType(const ObjectSP object_sp);
diff --git a/lldb/source/API/SBStructuredData.cpp b/lldb/source/API/SBStructuredData.cpp
index b891a34bd7c76..44874da594866 100644
--- a/lldb/source/API/SBStructuredData.cpp
+++ b/lldb/source/API/SBStructuredData.cpp
@@ -232,3 +232,52 @@ lldb::SBScriptObject SBStructuredData::GetGenericValue() const {
 
   return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
 }
+
+void SBStructuredData::SetValueForKey(const char *key,
+                                      SBStructuredData &value) const {
+  LLDB_INSTRUMENT_VA(this, key, value);
+
+  m_impl_up->SetValueForKey(key, value.m_impl_up->GetObjectSP());
+}
+
+void SBStructuredData::SetIntegerValue(uint64_t value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  SetUnsignedIntegerValue(value);
+}
+
+void SBStructuredData::SetUnsignedIntegerValue(uint64_t value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  m_impl_up->SetUnsignedIntegerValue(value);
+}
+
+void SBStructuredData::SetSignedIntegerValue(int64_t value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  m_impl_up->SetSignedIntegerValue(value);
+}
+
+void SBStructuredData::SetFloatValue(double value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  m_impl_up->SetFloatValue(value);
+}
+
+void SBStructuredData::SetBooleanValue(bool value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  m_impl_up->SetBooleanValue(value);
+}
+
+void SBStructuredData::SetStringValue(const char *value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  m_impl_up->SetStringValue(value);
+}
+
+void SBStructuredData::SetGenericValue(SBScriptObject value) const {
+  LLDB_INSTRUMENT_VA(this, value);
+
+  m_impl_up->SetGenericValue(value.GetPointer());
+}
diff --git a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
index 21256d6c6e743..8ad12b76e338e 100644
--- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
+++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
@@ -130,6 +130,33 @@ class MyRandomClass:
         self.assertSuccess(example.SetFromJSON("null"))
         self.assertEqual(example.GetType(), lldb.eStructuredDataTypeNull)
 
+        example = lldb.SBStructuredData()
+        example.SetUnsignedIntegerValue(1)
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeInteger)
+        self.assertEqual(example.GetIntegerValue(), 1)
+
+        example.SetSignedIntegerValue(-42)
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeSignedInteger)
+        self.assertEqual(example.GetSignedIntegerValue(), -42)
+
+        example.SetFloatValue(4.19)
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeFloat)
+        self.assertEqual(example.GetFloatValue(), 4.19)
+
+        example.SetStringValue("Bonjour, 123!")
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeString)
+        self.assertEqual(example.GetStringValue(42), "Bonjour, 123!")
+
+        example.SetBooleanValue(True)
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeBoolean)
+        self.assertTrue(example.GetBooleanValue())
+
+        example.SetGenericValue(stp)
+        self.assertEqual(example.GetType(), lldb.eStructuredDataTypeGeneric)
+        my_random_class = generic_sd.GetGenericValue()
+        self.assertTrue(my_random_class)
+        self.assertEqual(my_random_class.payload, MyRandomClass.payload)
+
         example_arr = [1, 2.3, "4", {"5": False}]
         arr_str = json.dumps(example_arr)
         s.Clear()

@jimingham
Copy link
Collaborator

One thing you are allowing but I don't think you test is:

value = SBStructuredData()
example.SetValueForKey("Hello", value)

Then getting this value back out and making sure that doesn't cause a problem. It won't but it would be good to make sure that's stays true.

@jimingham
Copy link
Collaborator

Other than that this LGTM.

This patch adds setters to the SBStruturedData class to be able to
initialize said object from the client side directly.

Signed-off-by: Med Ismail Bennani <[email protected]>
@medismailben
Copy link
Member Author

One thing you are allowing but I don't think you test is:

value = SBStructuredData()
example.SetValueForKey("Hello", value)

Then getting this value back out and making sure that doesn't cause a problem. It won't but it would be good to make sure that's stays true.

Done!

@medismailben medismailben merged commit 05b1ec3 into llvm:main Aug 21, 2025
9 checks passed
medismailben added a commit to medismailben/llvm-project that referenced this pull request Aug 22, 2025
This patch adds setters to the SBStruturedData class to be able to
initialize said object from the client side directly.

Signed-off-by: Med Ismail Bennani <[email protected]>
(cherry picked from commit 05b1ec3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants