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"

llvm-svn: 135815
This commit is contained in:
Johnny Chen 2011-07-22 22:01:35 +00:00
parent c391a58b2b
commit 36d7d91616
3 changed files with 75 additions and 10 deletions

View File

@ -533,25 +533,34 @@ def get_ESRs(frame):
# ======================================
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 @@ class ChildVisitingFormatter(BasicFormatter):
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()

View File

@ -66,11 +66,28 @@ class ValueAPITestCase(TestBase):
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')

View File

@ -20,6 +20,14 @@ const char *days_of_week[7] = { "Sunday",
"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;