[Lldb-commits] [lldb] r135736 - in /lldb/trunk/test: lldbutil.py python_api/value/TestValueAPI.py python_api/value/main.c

Johnny Chen johnny.chen at apple.com
Thu Jul 21 17:47:58 PDT 2011


Author: johnny
Date: Thu Jul 21 19:47:58 2011
New Revision: 135736

URL: http://llvm.org/viewvc/llvm-project?rev=135736&view=rev
Log:
Add BasicFormatter and ChildVisitingFormatter utility classes to the lldbutil.py module
which provide some convenient ways to print an SBValue object.  Use that in TestValueAPI.py
to print the 'days_of_week' char* array variable.

For an example:

cvf = lldbutil.ChildVisitingFormatter(indent=2)
print cvf.format(days_of_week)

produces:

(const char *[7]) days_of_week = 0x00000001026a5060 (location)
  (const char *) [0] = "Sunday"
  (const char *) [1] = "Monday"
  (const char *) [2] = "Tuesday"
  (const char *) [3] = "Wednesday"
  (const char *) [4] = "Thursday"
  (const char *) [5] = "Friday"
  (const char *) [6] = "Saturday"


Modified:
    lldb/trunk/test/lldbutil.py
    lldb/trunk/test/python_api/value/TestValueAPI.py
    lldb/trunk/test/python_api/value/main.c

Modified: lldb/trunk/test/lldbutil.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbutil.py?rev=135736&r1=135735&r2=135736&view=diff
==============================================================================
--- lldb/trunk/test/lldbutil.py (original)
+++ lldb/trunk/test/lldbutil.py Thu Jul 21 19:47:58 2011
@@ -527,3 +527,39 @@
         ...
     """
     return get_registers(frame, "exception state")
+
+# ===============================
+# Utility functions for SBValue's
+# ===============================
+
+class BasicFormatter(object):
+    def format(self, value, buffer=None, indent=0):
+        if not buffer:
+            output = StringIO.StringIO()
+        else:
+            output = buffer
+        sum = value.GetSummary()
+        if value.GetNumChildren() > 0 and sum == None:
+            sum = "%s (location)" % value.GetLocation()
+        print >> output, "{indentation}({type}) {name} = {summary}".format(
+            indentation = ' ' * indent,
+            type = value.GetTypeName(),
+            name = value.GetName(),
+            summary = sum)
+        return output.getvalue()
+
+class ChildVisitingFormatter(BasicFormatter):
+    def __init__(self, indent=2):
+        """Default indentation of 2 SPC's."""
+        self.indentation = indent
+    def format(self, value, buffer=None):
+        if not buffer:
+            output = StringIO.StringIO()
+        else:
+            output = buffer
+
+        BasicFormatter.format(self, value, buffer=output)
+        for child in value:
+            BasicFormatter.format(self, child, buffer=output, indent=self.indentation)
+
+        return output.getvalue()

Modified: lldb/trunk/test/python_api/value/TestValueAPI.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/value/TestValueAPI.py?rev=135736&r1=135735&r2=135736&view=diff
==============================================================================
--- lldb/trunk/test/python_api/value/TestValueAPI.py (original)
+++ lldb/trunk/test/python_api/value/TestValueAPI.py Thu Jul 21 19:47:58 2011
@@ -66,6 +66,12 @@
         self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
         self.DebugSBValue(days_of_week)
 
+        fmt = lldbutil.BasicFormatter()
+        cvf = lldbutil.ChildVisitingFormatter(indent=2)
+        if self.TraceOn():
+            print fmt.format(days_of_week)
+            print cvf.format(days_of_week)
+
         # Get variable 'str_ptr'.
         value = frame0.FindVariable('str_ptr')
         self.assertTrue(value, VALID_VARIABLE)

Modified: lldb/trunk/test/python_api/value/main.c
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/value/main.c?rev=135736&r1=135735&r2=135736&view=diff
==============================================================================
--- lldb/trunk/test/python_api/value/main.c (original)
+++ lldb/trunk/test/python_api/value/main.c Thu Jul 21 19:47:58 2011
@@ -10,6 +10,8 @@
 
 // This simple program is to test the lldb Python API SBValue.GetChildAtIndex().
 
+int g_my_int = 100;
+
 const char *days_of_week[7] = { "Sunday",
                                 "Monday",
                                 "Tuesday",





More information about the lldb-commits mailing list