Author: Dave Lee Date: 2024-12-09T10:48:28-08:00 New Revision: 53fd724b256e1ccfcb04c90f7740f54e1801986d
URL: https://github.com/llvm/llvm-project/commit/53fd724b256e1ccfcb04c90f7740f54e1801986d DIFF: https://github.com/llvm/llvm-project/commit/53fd724b256e1ccfcb04c90f7740f54e1801986d.diff LOG: [lldb] Add lookup by name to SBValue through new member property (#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")`. Added: Modified: lldb/bindings/interface/SBValueExtensions.i lldb/test/API/python_api/value/TestValueAPI.py Removed: ################################################################################ diff --git a/lldb/bindings/interface/SBValueExtensions.i b/lldb/bindings/interface/SBValueExtensions.i index bee9c27775d453..f0c8797bd5f78b 100644 --- a/lldb/bindings/interface/SBValueExtensions.i +++ b/lldb/bindings/interface/SBValueExtensions.i @@ -7,7 +7,7 @@ STRING_EXTENSION_OUTSIDE(SBValue) return self.GetDynamicValue (eDynamicCanRunTarget) class children_access(object): - '''A helper object that will lazily hand out thread for a process when supplied an index.''' + '''A helper object that will lazily hand out child values when supplied an index.''' def __init__(self, sbvalue): self.sbvalue = sbvalue @@ -29,6 +29,19 @@ STRING_EXTENSION_OUTSIDE(SBValue) '''An accessor function that returns a children_access() object which allows lazy member variable access from a lldb.SBValue object.''' return self.children_access (self) + def get_member_access_object(self): + '''An accessor function that returns an interface which provides subscript based lookup of child members.''' + class member_access: + def __init__(self, valobj): + self.valobj = valobj + + def __getitem__(self, key): + if isinstance(key, str): + return self.valobj.GetChildMemberWithName(key) + raise TypeError("member key must be a string") + + return member_access(self) + def get_value_child_list(self): '''An accessor function that returns a list() that contains all children in a lldb.SBValue object.''' children = [] @@ -50,6 +63,7 @@ STRING_EXTENSION_OUTSIDE(SBValue) 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.''') 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]).''') + member = property(get_member_access_object, None, doc='''A read only property that returns an object that can access child members by name.''') name = property(GetName, None, doc='''A read only property that returns the name of this value as a string.''') type = property(GetType, None, doc='''A read only property that returns a lldb.SBType object that represents the type for this value.''') size = property(GetByteSize, None, doc='''A read only property that returns the size in bytes of this value.''') diff --git a/lldb/test/API/python_api/value/TestValueAPI.py b/lldb/test/API/python_api/value/TestValueAPI.py index 512100912d6fe7..9eaf2c994d8463 100644 --- a/lldb/test/API/python_api/value/TestValueAPI.py +++ b/lldb/test/API/python_api/value/TestValueAPI.py @@ -140,10 +140,8 @@ def test(self): val_i = target.EvaluateExpression("i") val_s = target.EvaluateExpression("s") val_a = target.EvaluateExpression("a") - self.assertTrue( - val_s.GetChildMemberWithName("a").GetAddress().IsValid(), VALID_VARIABLE - ) - self.assertTrue(val_s.GetChildMemberWithName("a").AddressOf(), VALID_VARIABLE) + self.assertTrue(val_s.member["a"].GetAddress().IsValid(), VALID_VARIABLE) + self.assertTrue(val_s.member["a"].AddressOf(), VALID_VARIABLE) self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE) # Test some other cases of the Cast API. We allow casts from one struct type @@ -210,7 +208,7 @@ def test(self): weird_cast = f_var.Cast(val_s.GetType()) self.assertSuccess(weird_cast.GetError(), "Can cast from a larger to a smaller") self.assertEqual( - weird_cast.GetChildMemberWithName("a").GetValueAsSigned(0), + weird_cast.member["a"].GetValueAsSigned(0), 33, "Got the right value", ) _______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits