Skip to content

Commit

Permalink
gdb: Fix pretty printer for nullable-opt enums with fat pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
luqmana committed Dec 2, 2014
1 parent 886ff4f commit 89d0995
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/etc/gdb_rust_pretty_printing.py
Expand Up @@ -54,21 +54,27 @@ def rust_pretty_printer_lookup_function(val):
return RustStructPrinter(val, false)

if enum_member_count == 1:
if enum_members[0].name == None:
first_variant_name = enum_members[0].name
if first_variant_name == None:
# This is a singleton enum
return rust_pretty_printer_lookup_function(val[enum_members[0]])
else:
assert enum_members[0].name.startswith("RUST$ENCODED$ENUM$")
assert first_variant_name.startswith("RUST$ENCODED$ENUM$")
# This is a space-optimized enum
last_separator_index = enum_members[0].name.rfind("$")
last_separator_index = first_variant_name.rfind("$")
second_last_separator_index = first_variant_name.rfind("$", 0, last_separator_index)
disr_field_index = first_variant_name[second_last_separator_index + 1 :
last_separator_index]
disr_field_index = int(disr_field_index)

sole_variant_val = val[enum_members[0]]
disr_field = get_field_at_index(sole_variant_val, disr_field_index)
discriminant = int(sole_variant_val[disr_field])
discriminant = sole_variant_val[disr_field]

# If the discriminant field is a fat pointer we have to consider the
# first word as the true discriminant
if discriminant.type.code == gdb.TYPE_CODE_STRUCT:
discriminant = discriminant[get_field_at_index(discriminant, 0)]

if discriminant == 0:
null_variant_name = first_variant_name[last_separator_index + 1:]
Expand Down Expand Up @@ -173,7 +179,7 @@ def to_string(self):

class IdentityPrinter:
def __init__(self, string):
self.string
self.string = string

def to_string(self):
return self.string
Expand Down
12 changes: 10 additions & 2 deletions src/test/debuginfo/gdb-pretty-struct-and-enums.rs
Expand Up @@ -58,11 +58,17 @@
// gdb-command: print none
// gdb-check:$12 = None

// gdb-command: print some_fat
// gdb-check:$13 = Some = {"abc"}

// gdb-command: print none_fat
// gdb-check:$14 = None

// gdb-command: print nested_variant1
// gdb-check:$13 = NestedVariant1 = {NestedStruct = {regular_struct = RegularStruct = {the_first_field = 111, the_second_field = 112.5, the_third_field = true, the_fourth_field = "NestedStructString1"}, tuple_struct = TupleStruct = {113.5, 114}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar2, mixed_enum = MixedEnumTupleVar = {115, 116, false}}}
// gdb-check:$15 = NestedVariant1 = {NestedStruct = {regular_struct = RegularStruct = {the_first_field = 111, the_second_field = 112.5, the_third_field = true, the_fourth_field = "NestedStructString1"}, tuple_struct = TupleStruct = {113.5, 114}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar2, mixed_enum = MixedEnumTupleVar = {115, 116, false}}}

// gdb-command: print nested_variant2
// gdb-check:$14 = NestedVariant2 = {abc = NestedStruct = {regular_struct = RegularStruct = {the_first_field = 117, the_second_field = 118.5, the_third_field = false, the_fourth_field = "NestedStructString10"}, tuple_struct = TupleStruct = {119.5, 120}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar3, mixed_enum = MixedEnumStructVar = {field1 = 121.5, field2 = -122}}}
// gdb-check:$16 = NestedVariant2 = {abc = NestedStruct = {regular_struct = RegularStruct = {the_first_field = 117, the_second_field = 118.5, the_third_field = false, the_fourth_field = "NestedStructString10"}, tuple_struct = TupleStruct = {119.5, 120}, empty_struct = EmptyStruct, c_style_enum = CStyleEnumVar3, mixed_enum = MixedEnumStructVar = {field1 = 121.5, field2 = -122}}}

use self::CStyleEnum::{CStyleEnumVar1, CStyleEnumVar2, CStyleEnumVar3};
use self::MixedEnum::{MixedEnumCStyleVar, MixedEnumTupleVar, MixedEnumStructVar};
Expand Down Expand Up @@ -129,6 +135,8 @@ fn main() {

let some = Some(110u);
let none: Option<int> = None;
let some_fat = Some("abc");
let none_fat: Option<&'static str> = None;

let nested_variant1 = NestedVariant1(
NestedStruct {
Expand Down

0 comments on commit 89d0995

Please sign in to comment.