-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[lldb/API] Add setters to SBStructuredData #154445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-lldb Author: Med Ismail Bennani (medismailben) ChangesThis 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:
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()
|
e7616d2
to
fec6da5
Compare
fec6da5
to
65b7e3a
Compare
65b7e3a
to
a49d832
Compare
lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py
Outdated
Show resolved
Hide resolved
a49d832
to
07d00d1
Compare
One thing you are allowing but I don't think you test is:
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. |
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]>
07d00d1
to
6da6d25
Compare
Done! |
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)
This patch adds setters to the SBStruturedData class to be able to initialize said object from the client side directly.