[Lldb-commits] [lldb] r260349 - 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

Enrico Granata via lldb-commits lldb-commits at lists.llvm.org
Tue Feb 9 18:12:42 PST 2016


Author: enrico
Date: Tue Feb  9 20:12:42 2016
New Revision: 260349

URL: http://llvm.org/viewvc/llvm-project?rev=260349&view=rev
Log:
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


Modified:
    lldb/trunk/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py
    lldb/trunk/packages/Python/lldbsuite/test/python_api/value/main.c
    lldb/trunk/scripts/Python/python-extensions.swig

Modified: lldb/trunk/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py?rev=260349&r1=260348&r2=260349&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py Tue Feb  9 20:12:42 2016
@@ -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')

Modified: lldb/trunk/packages/Python/lldbsuite/test/python_api/value/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/packages/Python/lldbsuite/test/python_api/value/main.c?rev=260349&r1=260348&r2=260349&view=diff
==============================================================================
--- lldb/trunk/packages/Python/lldbsuite/test/python_api/value/main.c (original)
+++ lldb/trunk/packages/Python/lldbsuite/test/python_api/value/main.c Tue Feb  9 20:12:42 2016
@@ -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 };

Modified: lldb/trunk/scripts/Python/python-extensions.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-extensions.swig?rev=260349&r1=260348&r2=260349&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-extensions.swig (original)
+++ lldb/trunk/scripts/Python/python-extensions.swig Tue Feb  9 20:12:42 2016
@@ -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)
+
 %}
\ No newline at end of file




More information about the lldb-commits mailing list