Skip to content

Commit 05b1ec3

Browse files
authored
[lldb/API] Add setters to SBStructuredData (#154445)
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]>
1 parent ff85dbd commit 05b1ec3

File tree

5 files changed

+179
-2
lines changed

5 files changed

+179
-2
lines changed

lldb/include/lldb/API/SBStructuredData.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,35 @@ class SBStructuredData {
109109
/// Return the generic pointer if this data structure is a generic type.
110110
lldb::SBScriptObject GetGenericValue() const;
111111

112+
/// Set the value corresponding to a key. If this data structure
113+
/// is not a dictionary type, reset the type to be dictionary and overwrite
114+
/// the previous data.
115+
void SetValueForKey(const char *key, SBStructuredData &value);
116+
117+
/// Change the type to unsigned interger and overwrite the previous data with
118+
/// the new value.
119+
void SetUnsignedIntegerValue(uint64_t value);
120+
121+
/// Change the type to signed interger and overwrite the previous data with
122+
/// the new value.
123+
void SetSignedIntegerValue(int64_t value);
124+
125+
/// Change the type to float and overwrite the previous data with the new
126+
/// value.
127+
void SetFloatValue(double value);
128+
129+
/// Change the type to boolean and overwrite the previous data with the new
130+
/// value.
131+
void SetBooleanValue(bool value);
132+
133+
/// Change the type to string and overwrite the previous data with the new
134+
/// value.
135+
void SetStringValue(const char *value);
136+
137+
/// Change the type to generic and overwrite the previous data with the new
138+
/// value.
139+
void SetGenericValue(SBScriptObject value);
140+
112141
protected:
113142
friend class SBAttachInfo;
114143
friend class SBCommandReturnObject;

lldb/include/lldb/Core/StructuredDataImpl.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,41 @@ class StructuredDataImpl {
8181

8282
void SetObjectSP(const StructuredData::ObjectSP &obj) { m_data_sp = obj; }
8383

84+
void SetValueForKey(llvm::StringRef key,
85+
const StructuredData::ObjectSP &value) {
86+
if (!m_data_sp ||
87+
m_data_sp->GetType() != lldb::eStructuredDataTypeDictionary) {
88+
m_data_sp = StructuredData::FromKeyValue(key, value);
89+
} else if (StructuredData::Dictionary *dict =
90+
m_data_sp->GetAsDictionary()) {
91+
dict->AddItem(key, value);
92+
}
93+
}
94+
95+
void SetUnsignedIntegerValue(uint64_t value) {
96+
m_data_sp = StructuredData::FromInteger(value);
97+
}
98+
99+
void SetSignedIntegerValue(int64_t value) {
100+
m_data_sp = StructuredData::FromInteger(value);
101+
}
102+
103+
void SetFloatValue(double value) {
104+
m_data_sp = StructuredData::FromFloat(value);
105+
}
106+
107+
void SetBooleanValue(bool value) {
108+
m_data_sp = StructuredData::FromBoolean(value);
109+
}
110+
111+
void SetStringValue(std::string value) {
112+
m_data_sp = StructuredData::FromString(std::move(value));
113+
}
114+
115+
void SetGenericValue(void *value) {
116+
m_data_sp = StructuredData::FromGeneric(value);
117+
}
118+
84119
lldb::StructuredDataType GetType() const {
85120
return (m_data_sp ? m_data_sp->GetType() :
86121
lldb::eStructuredDataTypeInvalid);

lldb/include/lldb/Utility/StructuredData.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class StructuredData {
432432
}
433433
return success;
434434
}
435-
435+
436436
template <class IntType>
437437
bool GetValueForKeyAsInteger(llvm::StringRef key, IntType &result) const {
438438
ObjectSP value_sp = GetValueForKey(key);
@@ -574,6 +574,30 @@ class StructuredData {
574574
void *m_object;
575575
};
576576

577+
template <typename T> static ObjectSP FromInteger(T value) {
578+
return std::make_shared<Integer<T>>(value);
579+
}
580+
581+
static StructuredData::ObjectSP FromFloat(double value) {
582+
return std::make_shared<StructuredData::Float>(value);
583+
}
584+
static StructuredData::ObjectSP FromBoolean(bool value) {
585+
return std::make_shared<StructuredData::Boolean>(value);
586+
}
587+
static StructuredData::ObjectSP FromString(std::string value) {
588+
return std::make_shared<StructuredData::String>(value);
589+
}
590+
static StructuredData::ObjectSP FromGeneric(void *value) {
591+
return std::make_shared<StructuredData::Generic>(value);
592+
}
593+
594+
static StructuredData::ObjectSP
595+
FromKeyValue(llvm::StringRef key, const StructuredData::ObjectSP &value_sp) {
596+
auto dict_sp = std::make_shared<StructuredData::Dictionary>();
597+
dict_sp->AddItem(key, value_sp);
598+
return dict_sp;
599+
}
600+
577601
static ObjectSP ParseJSON(llvm::StringRef json_text);
578602
static ObjectSP ParseJSONFromFile(const FileSpec &file, Status &error);
579603
static bool IsRecordType(const ObjectSP object_sp);

lldb/source/API/SBStructuredData.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,47 @@ lldb::SBScriptObject SBStructuredData::GetGenericValue() const {
232232

233233
return {m_impl_up->GetGenericValue(), eScriptLanguageDefault};
234234
}
235+
236+
void SBStructuredData::SetValueForKey(const char *key,
237+
SBStructuredData &value) {
238+
LLDB_INSTRUMENT_VA(this, key, value);
239+
240+
if (StructuredData::ObjectSP obj_sp = value.m_impl_up->GetObjectSP())
241+
m_impl_up->SetValueForKey(key, obj_sp);
242+
}
243+
244+
void SBStructuredData::SetUnsignedIntegerValue(uint64_t value) {
245+
LLDB_INSTRUMENT_VA(this, value);
246+
247+
m_impl_up->SetUnsignedIntegerValue(value);
248+
}
249+
250+
void SBStructuredData::SetSignedIntegerValue(int64_t value) {
251+
LLDB_INSTRUMENT_VA(this, value);
252+
253+
m_impl_up->SetSignedIntegerValue(value);
254+
}
255+
256+
void SBStructuredData::SetFloatValue(double value) {
257+
LLDB_INSTRUMENT_VA(this, value);
258+
259+
m_impl_up->SetFloatValue(value);
260+
}
261+
262+
void SBStructuredData::SetBooleanValue(bool value) {
263+
LLDB_INSTRUMENT_VA(this, value);
264+
265+
m_impl_up->SetBooleanValue(value);
266+
}
267+
268+
void SBStructuredData::SetStringValue(const char *value) {
269+
LLDB_INSTRUMENT_VA(this, value);
270+
271+
m_impl_up->SetStringValue(value);
272+
}
273+
274+
void SBStructuredData::SetGenericValue(SBScriptObject value) {
275+
LLDB_INSTRUMENT_VA(this, value);
276+
277+
m_impl_up->SetGenericValue(value.GetPointer());
278+
}

lldb/test/API/python_api/sbstructureddata/TestStructuredDataAPI.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import json
1212

13-
1413
class TestStructuredDataAPI(TestBase):
1514
NO_DEBUG_INFO_TESTCASE = True
1615

@@ -130,6 +129,52 @@ class MyRandomClass:
130129
self.assertSuccess(example.SetFromJSON("null"))
131130
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeNull)
132131

132+
example = lldb.SBStructuredData()
133+
example.SetUnsignedIntegerValue(1)
134+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeInteger)
135+
self.assertEqual(example.GetIntegerValue(), 1)
136+
137+
example.SetSignedIntegerValue(-42)
138+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeSignedInteger)
139+
self.assertEqual(example.GetSignedIntegerValue(), -42)
140+
141+
example.SetFloatValue(4.19)
142+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeFloat)
143+
self.assertEqual(example.GetFloatValue(), 4.19)
144+
145+
example.SetStringValue("Bonjour, 123!")
146+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeString)
147+
self.assertEqual(example.GetStringValue(42), "Bonjour, 123!")
148+
149+
value = lldb.SBStructuredData()
150+
example.SetValueForKey("Hello", value)
151+
self.assertEqual(example.GetSize(), 0)
152+
153+
nested_obj = lldb.SBStructuredData()
154+
nested_obj.SetStringValue("World")
155+
example.SetValueForKey("Hello", nested_obj)
156+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeDictionary)
157+
nested_obj = None
158+
nested_obj = example.GetValueForKey("Hello")
159+
self.assertTrue(nested_obj.IsValid())
160+
self.assertEqual(nested_obj.GetType(), lldb.eStructuredDataTypeString)
161+
self.assertEqual(nested_obj.GetStringValue(42), "World")
162+
163+
example.SetBooleanValue(True)
164+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeBoolean)
165+
self.assertTrue(example.GetBooleanValue())
166+
167+
rnd_obj = MyRandomClass()
168+
stp = lldb.SBScriptObject(rnd_obj, lldb.eScriptLanguagePython)
169+
self.assertEqual(stp.ptr, rnd_obj)
170+
171+
example.SetGenericValue(stp)
172+
self.assertEqual(example.GetType(), lldb.eStructuredDataTypeGeneric)
173+
174+
my_random_class = example.GetGenericValue()
175+
self.assertTrue(my_random_class)
176+
self.assertEqual(my_random_class.payload, MyRandomClass.payload)
177+
133178
example_arr = [1, 2.3, "4", {"5": False}]
134179
arr_str = json.dumps(example_arr)
135180
s.Clear()

0 commit comments

Comments
 (0)