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

Johnny Chen johnny.chen at apple.com
Fri Jul 22 15:01:35 PDT 2011


Author: johnny
Date: Fri Jul 22 17:01:35 2011
New Revision: 135815

URL: http://llvm.org/viewvc/llvm-project?rev=135815&view=rev
Log:
Add an additional formatter class RecursiveDecentFormatter which prints the
value and the decendents.  For an example,

rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
print rdf.format(g_table)

produces:

(const char **[2]) g_table = 0x00000001055a80f0 (location)
  (const char **) [0] = 0x00000001055a8080
    (const char *) *[0] = "Sunday"
  (const char **) [1] = 0x00000001055a80c0
    (const char *) *[1] = "Monday"


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=135815&r1=135814&r2=135815&view=diff
==============================================================================
--- lldb/trunk/test/lldbutil.py (original)
+++ lldb/trunk/test/lldbutil.py Fri Jul 22 17:01:35 2011
@@ -533,25 +533,34 @@
 # ======================================
 
 class BasicFormatter(object):
+    """The basic formatter inspects the value object and prints the value."""
     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(
+        # If there is a summary, it suffices.
+        val = value.GetSummary()
+        # Otherwise, get the value.
+        if val == None:
+            val = value.GetValue()
+        if val == None and value.GetNumChildren() > 0:
+            val = "%s (location)" % value.GetLocation()
+        print >> output, "{indentation}({type}) {name} = {value}".format(
             indentation = ' ' * indent,
             type = value.GetTypeName(),
             name = value.GetName(),
-            summary = sum)
+            value = val)
         return output.getvalue()
 
 class ChildVisitingFormatter(BasicFormatter):
-    def __init__(self, indent=2):
-        """Default indentation of 2 SPC's."""
-        self.indentation = indent
+    """The child visiting formatter prints the value and its immediate children.
+
+    The constructor takes a keyword arg: indent_child, which defaults to 2.
+    """
+    def __init__(self, indent_child=2):
+        """Default indentation of 2 SPC's for the children."""
+        self.cindent = indent_child
     def format(self, value, buffer=None):
         if not buffer:
             output = StringIO.StringIO()
@@ -560,6 +569,37 @@
 
         BasicFormatter.format(self, value, buffer=output)
         for child in value:
-            BasicFormatter.format(self, child, buffer=output, indent=self.indentation)
+            BasicFormatter.format(self, child, buffer=output, indent=self.cindent)
+
+        return output.getvalue()
+
+class RecursiveDecentFormatter(BasicFormatter):
+    """The recursive decent formatter prints the value and the decendents.
+
+    The constructor takes two keyword args: indent_level, which defaults to 0,
+    and indent_child, which defaults to 2.  The current indentation level is
+    determined by indent_level, while the immediate children has an additional
+    indentation by inden_child. 
+    """
+    def __init__(self, indent_level=0, indent_child=2):
+        self.lindent = indent_level
+        self.cindent = indent_child
+    def format(self, value, buffer=None):
+        if not buffer:
+            output = StringIO.StringIO()
+        else:
+            output = buffer
+
+        BasicFormatter.format(self, value, buffer=output, indent=self.lindent)
+        new_indent = self.lindent + self.cindent
+        for child in value:
+            if child.GetSummary() != None:
+                BasicFormatter.format(self, child, buffer=output, indent=new_indent)
+            else:
+                if child.GetNumChildren() > 0:
+                    rdf = RecursiveDecentFormatter(indent_level=new_indent)
+                    rdf.format(child, buffer=output)
+                else:
+                    BasicFormatter.format(self, child, buffer=output, indent=new_indent)
 
         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=135815&r1=135814&r2=135815&view=diff
==============================================================================
--- lldb/trunk/test/python_api/value/TestValueAPI.py (original)
+++ lldb/trunk/test/python_api/value/TestValueAPI.py Fri Jul 22 17:01:35 2011
@@ -66,11 +66,28 @@
         self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
         self.DebugSBValue(days_of_week)
 
+        # Get global variable 'weekdays'.
+        list = target.FindGlobalVariables('weekdays', 1)
+        weekdays = list.GetValueAtIndex(0)
+        self.assertTrue(weekdays, VALID_VARIABLE)
+        self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE)
+        self.DebugSBValue(weekdays)
+
+        # Get global variable 'g_table'.
+        list = target.FindGlobalVariables('g_table', 1)
+        g_table = list.GetValueAtIndex(0)
+        self.assertTrue(g_table, VALID_VARIABLE)
+        self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE)
+        self.DebugSBValue(g_table)
+
         fmt = lldbutil.BasicFormatter()
-        cvf = lldbutil.ChildVisitingFormatter(indent=2)
+        cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
+        rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
         if self.TraceOn():
             print fmt.format(days_of_week)
             print cvf.format(days_of_week)
+            print cvf.format(weekdays)
+            print rdf.format(g_table)
 
         # Get variable 'str_ptr'.
         value = frame0.FindVariable('str_ptr')

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=135815&r1=135814&r2=135815&view=diff
==============================================================================
--- lldb/trunk/test/python_api/value/main.c (original)
+++ lldb/trunk/test/python_api/value/main.c Fri Jul 22 17:01:35 2011
@@ -20,6 +20,14 @@
                                 "Friday",
                                 "Saturday" };
 
+const char *weekdays[5] = { "Monday",
+                            "Tuesday",
+                            "Wednesday",
+                            "Thursday",
+                            "Friday" };
+
+const char **g_table[2] = { days_of_week, weekdays };
+
 int main (int argc, char const *argv[])
 {
     int i;





More information about the lldb-commits mailing list