Change lldb.value.__int__() so that it takes into account the signedness of the value being cast to return a Python number with the proper value

The explicit APIs on SBValue obviously remain if one wants to be explicit in intent, or override this guess, but since __int__() has to pick one, an educated guess is definitely better than than always going to signed regardless

Fixes rdar://24556976

llvm-svn: 260349
This commit is contained in:
Enrico Granata 2016-02-10 02:12:42 +00:00
parent 4abe94fb6b
commit 14086ed75d
3 changed files with 61 additions and 3 deletions

View File

@ -134,3 +134,12 @@ class ValueAPITestCase(TestBase):
val_a = target.EvaluateExpression('a')
self.assertTrue(val_s.GetChildMemberWithName('a').AddressOf(), VALID_VARIABLE)
self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE)
self.assertTrue(int(lldb.value(frame0.FindVariable('uinthex'))) == 3768803088, 'uinthex == 3768803088')
self.assertTrue(int(lldb.value(frame0.FindVariable('sinthex'))) == -526164208, 'sinthex == -526164208')
self.assertTrue(frame0.FindVariable('uinthex').GetValueAsUnsigned() == 3768803088, 'unsigned uinthex == 3768803088')
self.assertTrue(frame0.FindVariable('sinthex').GetValueAsUnsigned() == 3768803088, 'unsigned sinthex == 3768803088')
self.assertTrue(frame0.FindVariable('uinthex').GetValueAsSigned() == -526164208, 'signed uinthex == -526164208')
self.assertTrue(frame0.FindVariable('sinthex').GetValueAsSigned() == -526164208, 'signed sinthex == -526164208')

View File

@ -7,6 +7,7 @@
//
//===----------------------------------------------------------------------===//
#include <stdio.h>
#include <stdint.h>
// This simple program is to test the lldb Python API SBValue.GetChildAtIndex().
@ -38,6 +39,9 @@ struct MyStruct
int main (int argc, char const *argv[])
{
uint32_t uinthex = 0xE0A35F10;
int32_t sinthex = 0xE0A35F10;
int i;
MyInt a = 12345;
struct MyStruct s = { 11, 22 };

View File

@ -1024,11 +1024,13 @@ class value(object):
return complex (int(self))
def __int__(self):
is_num,is_sign = is_numeric_type(self.sbvalue.GetType().GetCanonicalType().GetBasicType())
if is_num and not is_sign: return self.sbvalue.GetValueAsUnsigned()
return self.sbvalue.GetValueAsSigned()
def __long__(self):
return self.sbvalue.GetValueAsSigned()
return self.__int__()
def __float__(self):
return float (self.sbvalue.GetValueAsSigned())
@ -1084,4 +1086,47 @@ class SBSyntheticValueProvider(object):
return False
%}
%pythoncode %{
# given an lldb.SBBasicType it returns a tuple
# (is_numeric, is_signed)
# the value of is_signed is undefined if is_numeric == false
def is_numeric_type(basic_type):
if basic_type == eBasicTypeInvalid: return (False,False)
if basic_type == eBasicTypeVoid: return (False,False)
if basic_type == eBasicTypeChar: return (True,False)
if basic_type == eBasicTypeSignedChar: return (True,True)
if basic_type == eBasicTypeUnsignedChar: return (True,False)
if basic_type == eBasicTypeWChar: return (True,False)
if basic_type == eBasicTypeSignedWChar: return (True,True)
if basic_type == eBasicTypeUnsignedWChar: return (True,False)
if basic_type == eBasicTypeChar16: return (True,False)
if basic_type == eBasicTypeChar32: return (True,False)
if basic_type == eBasicTypeShort: return (True,True)
if basic_type == eBasicTypeUnsignedShort: return (True,False)
if basic_type == eBasicTypeInt: return (True,True)
if basic_type == eBasicTypeUnsignedInt: return (True,False)
if basic_type == eBasicTypeLong: return (True,True)
if basic_type == eBasicTypeUnsignedLong: return (True,False)
if basic_type == eBasicTypeLongLong: return (True,True)
if basic_type == eBasicTypeUnsignedLongLong: return (True,False)
if basic_type == eBasicTypeInt128: return (True,True)
if basic_type == eBasicTypeUnsignedInt128: return (True,False)
if basic_type == eBasicTypeBool: return (False,False)
if basic_type == eBasicTypeHalf: return (True,True)
if basic_type == eBasicTypeFloat: return (True,True)
if basic_type == eBasicTypeDouble: return (True,True)
if basic_type == eBasicTypeLongDouble: return (True,True)
if basic_type == eBasicTypeFloatComplex: return (True,True)
if basic_type == eBasicTypeDoubleComplex: return (True,True)
if basic_type == eBasicTypeLongDoubleComplex: return (True,True)
if basic_type == eBasicTypeObjCID: return (False,False)
if basic_type == eBasicTypeObjCClass: return (False,False)
if basic_type == eBasicTypeObjCSel: return (False,False)
if basic_type == eBasicTypeNullPtr: return (False,False)
#if basic_type == eBasicTypeOther:
return (False,False)
%}