Fix PEP8 in gdb pretty printer

This commit is contained in:
Richo Healey
2015-01-26 23:50:24 -08:00
parent 9252525196
commit 6af36031e2

View File

@@ -14,10 +14,12 @@ import gdb
# GDB Pretty Printing Module for Rust # GDB Pretty Printing Module for Rust
#=============================================================================== #===============================================================================
def register_printers(objfile): def register_printers(objfile):
"Registers Rust pretty printers for the given objfile" "Registers Rust pretty printers for the given objfile"
objfile.pretty_printers.append(rust_pretty_printer_lookup_function) objfile.pretty_printers.append(rust_pretty_printer_lookup_function)
def rust_pretty_printer_lookup_function(val): def rust_pretty_printer_lookup_function(val):
"Returns the correct Rust pretty printer for the given value if there is one" "Returns the correct Rust pretty printer for the given value if there is one"
type_code = val.type.code type_code = val.type.code
@@ -55,20 +57,20 @@ def rust_pretty_printer_lookup_function(val):
if enum_member_count == 1: if enum_member_count == 1:
first_variant_name = enum_members[0].name first_variant_name = enum_members[0].name
if first_variant_name == None: if first_variant_name is None:
# This is a singleton enum # This is a singleton enum
return rust_pretty_printer_lookup_function(val[enum_members[0]]) return rust_pretty_printer_lookup_function(val[enum_members[0]])
else: else:
assert first_variant_name.startswith("RUST$ENCODED$ENUM$") assert first_variant_name.startswith("RUST$ENCODED$ENUM$")
# This is a space-optimized enum. # This is a space-optimized enum.
# This means this enum has only two states, and Rust uses one of the # This means this enum has only two states, and Rust uses one
# fields somewhere in the struct to determine which of the two states # of the fields somewhere in the struct to determine which of
# it's in. The location of the field is encoded in the name as something # the two states it's in. The location of the field is encoded
# like RUST$ENCODED$ENUM$(num$)*name_of_zero_state # in the name as something like
# RUST$ENCODED$ENUM$(num$)*name_of_zero_state
last_separator_index = first_variant_name.rfind("$") last_separator_index = first_variant_name.rfind("$")
start_index = len("RUST$ENCODED$ENUM$") start_index = len("RUST$ENCODED$ENUM$")
disr_field_indices = first_variant_name[start_index : disr_field_indices = first_variant_name[start_index:last_separator_index].split("$")
last_separator_index].split("$")
disr_field_indices = [int(index) for index in disr_field_indices] disr_field_indices = [int(index) for index in disr_field_indices]
sole_variant_val = val[enum_members[0]] sole_variant_val = val[enum_members[0]]
@@ -99,6 +101,7 @@ def rust_pretty_printer_lookup_function(val):
# Pretty Printer Classes # Pretty Printer Classes
#=------------------------------------------------------------------------------ #=------------------------------------------------------------------------------
class RustStructPrinter: class RustStructPrinter:
def __init__(self, val, hide_first_field): def __init__(self, val, hide_first_field):
self.val = val self.val = val
@@ -111,23 +114,25 @@ class RustStructPrinter:
cs = [] cs = []
for field in self.val.type.fields(): for field in self.val.type.fields():
field_name = field.name field_name = field.name
# Normally the field name is used as a key to access the field value, # Normally the field name is used as a key to access the field
# because that's also supported in older versions of GDB... # value, because that's also supported in older versions of GDB...
field_key = field_name field_key = field_name
if field_name == None: if field_name is None:
field_name = "" field_name = ""
# ... but for fields without a name (as in tuples), we have to fall back # ... but for fields without a name (as in tuples), we have to
# to the newer method of using the field object directly as key. In # fall back to the newer method of using the field object
# older versions of GDB, this will just fail. # directly as key. In older versions of GDB, this will just
# fail.
field_key = field field_key = field
name_value_tuple = ( field_name, self.val[field_key] ) name_value_tuple = (field_name, self.val[field_key])
cs.append( name_value_tuple ) cs.append(name_value_tuple)
if self.hide_first_field: if self.hide_first_field:
cs = cs[1:] cs = cs[1:]
return cs return cs
class RustTuplePrinter: class RustTuplePrinter:
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val
@@ -138,13 +143,14 @@ class RustTuplePrinter:
def children(self): def children(self):
cs = [] cs = []
for field in self.val.type.fields(): for field in self.val.type.fields():
cs.append( ("", self.val[field]) ) cs.append(("", self.val[field]))
return cs return cs
def display_hint(self): def display_hint(self):
return "array" return "array"
class RustTupleStructPrinter: class RustTupleStructPrinter:
def __init__(self, val, hide_first_field): def __init__(self, val, hide_first_field):
self.val = val self.val = val
@@ -156,7 +162,7 @@ class RustTupleStructPrinter:
def children(self): def children(self):
cs = [] cs = []
for field in self.val.type.fields(): for field in self.val.type.fields():
cs.append( ("", self.val[field]) ) cs.append(("", self.val[field]))
if self.hide_first_field: if self.hide_first_field:
cs = cs[1:] cs = cs[1:]
@@ -166,14 +172,15 @@ class RustTupleStructPrinter:
def display_hint(self): def display_hint(self):
return "array" return "array"
class RustStringSlicePrinter: class RustStringSlicePrinter:
def __init__(self, val): def __init__(self, val):
self.val = val self.val = val
def to_string(self): def to_string(self):
slice_byte_len = self.val["length"] slice_byte_len = self.val["length"]
return '"%s"' % self.val["data_ptr"].string(encoding = "utf-8", return '"%s"' % self.val["data_ptr"].string(encoding="utf-8", length=slice_byte_len)
length = slice_byte_len)
class RustCStyleEnumPrinter: class RustCStyleEnumPrinter:
def __init__(self, val): def __init__(self, val):
@@ -183,6 +190,7 @@ class RustCStyleEnumPrinter:
def to_string(self): def to_string(self):
return str(self.val) return str(self.val)
class IdentityPrinter: class IdentityPrinter:
def __init__(self, string): def __init__(self, string):
self.string = string self.string = string
@@ -198,6 +206,7 @@ STRUCT_KIND_STRUCT_VARIANT = 4
STRUCT_KIND_CSTYLE_VARIANT = 5 STRUCT_KIND_CSTYLE_VARIANT = 5
STRUCT_KIND_STR_SLICE = 6 STRUCT_KIND_STR_SLICE = 6
def classify_struct(type): def classify_struct(type):
if type.tag == "&str": if type.tag == "&str":
return STRUCT_KIND_STR_SLICE return STRUCT_KIND_STR_SLICE
@@ -211,12 +220,12 @@ def classify_struct(type):
if fields[0].name == "RUST$ENUM$DISR": if fields[0].name == "RUST$ENUM$DISR":
if field_count == 1: if field_count == 1:
return STRUCT_KIND_CSTYLE_VARIANT return STRUCT_KIND_CSTYLE_VARIANT
elif fields[1].name == None: elif fields[1].name is None:
return STRUCT_KIND_TUPLE_VARIANT return STRUCT_KIND_TUPLE_VARIANT
else: else:
return STRUCT_KIND_STRUCT_VARIANT return STRUCT_KIND_STRUCT_VARIANT
if fields[0].name == None: if fields[0].name is None:
if type.tag.startswith("("): if type.tag.startswith("("):
return STRUCT_KIND_TUPLE return STRUCT_KIND_TUPLE
else: else:
@@ -224,6 +233,7 @@ def classify_struct(type):
return STRUCT_KIND_REGULAR_STRUCT return STRUCT_KIND_REGULAR_STRUCT
def extract_discriminant_value(enum_val): def extract_discriminant_value(enum_val):
assert enum_val.type.code == gdb.TYPE_CODE_UNION assert enum_val.type.code == gdb.TYPE_CODE_UNION
for variant_descriptor in enum_val.type.fields(): for variant_descriptor in enum_val.type.fields():
@@ -231,10 +241,12 @@ def extract_discriminant_value(enum_val):
for field in variant_val.type.fields(): for field in variant_val.type.fields():
return (field.name, int(variant_val[field])) return (field.name, int(variant_val[field]))
def first_field(val): def first_field(val):
for field in val.type.fields(): for field in val.type.fields():
return field return field
def get_field_at_index(val, index): def get_field_at_index(val, index):
i = 0 i = 0
for field in val.type.fields(): for field in val.type.fields():