https://github.com/medismailben updated https://github.com/llvm/llvm-project/pull/154445
>From 65b7e3a056f7b3c76217364d700e577f029e4190 Mon Sep 17 00:00:00 2001 From: Med Ismail Bennani <ism...@bennani.ma> Date: Wed, 20 Aug 2025 16:59:25 -0700 Subject: [PATCH] [lldb/API] Add setters to SBStructuredData 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 <ism...@bennani.ma> --- lldb/include/lldb/API/SBStructuredData.h | 29 +++++++++++++ lldb/include/lldb/Core/StructuredDataImpl.h | 33 ++++++++++++++ lldb/include/lldb/Utility/StructuredData.h | 26 ++++++++++- lldb/source/API/SBStructuredData.cpp | 43 +++++++++++++++++++ .../sbstructureddata/TestStructuredDataAPI.py | 32 ++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) diff --git a/lldb/include/lldb/API/SBStructuredData.h b/lldb/include/lldb/API/SBStructuredData.h index f96e169f236ed..8a0dcccbb879f 100644 --- a/lldb/include/lldb/API/SBStructuredData.h +++ b/lldb/include/lldb/API/SBStructuredData.h @@ -109,6 +109,35 @@ class SBStructuredData { /// Return the generic pointer if this data structure is a generic type. lldb::SBScriptObject GetGenericValue() const; + /// Set the value corresponding to a key. If this data structure + /// is not a dictionary type, reset the type to be dictionary and overwrite + /// previous data. + void SetValueForKey(const char *key, SBStructuredData &value); + + /// Change the type to unsigned interger and overwrite the previous data with + /// the new value. + void SetUnsignedIntegerValue(uint64_t value); + + /// Change the type to signed interger and overwrite the previous data with + /// the new value. + void SetSignedIntegerValue(int64_t value); + + /// Change the type to float and overwrite the previous data with the new + /// value. + void SetFloatValue(double value); + + /// Change the type to boolean and overwrite the previous data with the new + /// value. + void SetBooleanValue(bool value); + + /// Change the type to string and overwrite the previous data with the new + /// value. + void SetStringValue(const char *value); + + /// Change the type to generic and overwrite the previous data with the new + /// value. + void SetGenericValue(SBScriptObject value); + 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..48c0f6e44727a 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(llvm::StringRef key, + const StructuredData::ObjectSP &value) { + if (m_data_sp) { + if (StructuredData::Dictionary *dict = m_data_sp->GetAsDictionary()) + return dict->AddItem(key, value); + } + m_data_sp = StructuredData::FromKeyValue(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..59a2fd60b3989 100644 --- a/lldb/include/lldb/Utility/StructuredData.h +++ b/lldb/include/lldb/Utility/StructuredData.h @@ -432,7 +432,7 @@ class StructuredData { } return success; } - + template <class IntType> bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const { ObjectSP value_sp = GetValueForKey(key); @@ -574,6 +574,30 @@ 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 StructuredData::ObjectSP + FromKeyValue(llvm::StringRef key, const StructuredData::ObjectSP &value_sp) { + auto dict_sp = std::make_shared<StructuredData::Dictionary>(); + dict_sp->AddItem(key, value_sp); + return dict_sp; + } + 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..a44fb4ce3c430 100644 --- a/lldb/source/API/SBStructuredData.cpp +++ b/lldb/source/API/SBStructuredData.cpp @@ -232,3 +232,46 @@ lldb::SBScriptObject SBStructuredData::GetGenericValue() const { return {m_impl_up->GetGenericValue(), eScriptLanguageDefault}; } + +void SBStructuredData::SetValueForKey(const char *key, + SBStructuredData &value) { + LLDB_INSTRUMENT_VA(this, key, value); + + m_impl_up->SetValueForKey(key, value.m_impl_up->GetObjectSP()); +} + +void SBStructuredData::SetUnsignedIntegerValue(uint64_t value) { + LLDB_INSTRUMENT_VA(this, value); + + m_impl_up->SetUnsignedIntegerValue(value); +} + +void SBStructuredData::SetSignedIntegerValue(int64_t value) { + LLDB_INSTRUMENT_VA(this, value); + + m_impl_up->SetSignedIntegerValue(value); +} + +void SBStructuredData::SetFloatValue(double value) { + LLDB_INSTRUMENT_VA(this, value); + + m_impl_up->SetFloatValue(value); +} + +void SBStructuredData::SetBooleanValue(bool value) { + LLDB_INSTRUMENT_VA(this, value); + + m_impl_up->SetBooleanValue(value); +} + +void SBStructuredData::SetStringValue(const char *value) { + LLDB_INSTRUMENT_VA(this, value); + + m_impl_up->SetStringValue(value); +} + +void SBStructuredData::SetGenericValue(SBScriptObject value) { + 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..782236dfdde67 100644 --- a/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py +++ b/lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py @@ -130,6 +130,38 @@ 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()) + + rnd_obj = MyRandomClass() + stp = lldb.SBScriptObject(rnd_obj, lldb.eScriptLanguagePython) + self.assertEqual(stp.ptr, rnd_obj) + + example.SetGenericValue(stp) + self.assertEqual(example.GetType(), lldb.eStructuredDataTypeGeneric) + + my_random_class = example.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() _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits