Skip to content

Commit d1723d2

Browse files
committed
[lldb] Add lookup by name to SBValue through new member property (llvm#118814)
Introduces a `member` property to `SBValue`. This property provides pythonic access to a value's members, by name. The expression `value.member["name"]` will be an alternate form form of writing `value.GetChildMemberWithName("name")`. (cherry-picked from commit 53fd724)
1 parent d461eca commit d1723d2

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lldb/bindings/interface/SBValueExtensions.i

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ STRING_EXTENSION_OUTSIDE(SBValue)
77
return self.GetDynamicValue (eDynamicCanRunTarget)
88

99
class children_access(object):
10-
'''A helper object that will lazily hand out thread for a process when supplied an index.'''
10+
'''A helper object that will lazily hand out child values when supplied an index.'''
1111

1212
def __init__(self, sbvalue):
1313
self.sbvalue = sbvalue
@@ -29,6 +29,19 @@ STRING_EXTENSION_OUTSIDE(SBValue)
2929
'''An accessor function that returns a children_access() object which allows lazy member variable access from a lldb.SBValue object.'''
3030
return self.children_access (self)
3131

32+
def get_member_access_object(self):
33+
'''An accessor function that returns an interface which provides subscript based lookup of child members.'''
34+
class member_access:
35+
def __init__(self, valobj):
36+
self.valobj = valobj
37+
38+
def __getitem__(self, key):
39+
if isinstance(key, str):
40+
return self.valobj.GetChildMemberWithName(key)
41+
raise TypeError("member key must be a string")
42+
43+
return member_access(self)
44+
3245
def get_value_child_list(self):
3346
'''An accessor function that returns a list() that contains all children in a lldb.SBValue object.'''
3447
children = []
@@ -50,6 +63,7 @@ STRING_EXTENSION_OUTSIDE(SBValue)
5063

5164
children = property(get_value_child_list, None, doc='''A read only property that returns a list() of lldb.SBValue objects for the children of the value.''')
5265
child = property(get_child_access_object, None, doc='''A read only property that returns an object that can access children of a variable by index (child_value = value.children[12]).''')
66+
member = property(get_member_access_object, None, doc='''A read only property that returns an object that can access child members by name.''')
5367
name = property(GetName, None, doc='''A read only property that returns the name of this value as a string.''')
5468
type = property(GetType, None, doc='''A read only property that returns a lldb.SBType object that represents the type for this value.''')
5569
size = property(GetByteSize, None, doc='''A read only property that returns the size in bytes of this value.''')

lldb/test/API/python_api/value/TestValueAPI.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ def test(self):
140140
val_i = target.EvaluateExpression("i")
141141
val_s = target.EvaluateExpression("s")
142142
val_a = target.EvaluateExpression("a")
143-
self.assertTrue(
144-
val_s.GetChildMemberWithName("a").GetAddress().IsValid(), VALID_VARIABLE
145-
)
146-
self.assertTrue(val_s.GetChildMemberWithName("a").AddressOf(), VALID_VARIABLE)
143+
self.assertTrue(val_s.member["a"].GetAddress().IsValid(), VALID_VARIABLE)
144+
self.assertTrue(val_s.member["a"].AddressOf(), VALID_VARIABLE)
147145
self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE)
148146

149147
# Test some other cases of the Cast API. We allow casts from one struct type
@@ -210,7 +208,7 @@ def test(self):
210208
weird_cast = f_var.Cast(val_s.GetType())
211209
self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller")
212210
self.assertEqual(
213-
weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0),
211+
weird_cast.member["a"].GetValueAsSigned(0),
214212
33,
215213
"Got the right value",
216214
)

0 commit comments

Comments
 (0)