Skip to content

Commit

Permalink
Auto merge of rust-lang#24351 - michaelwoerister:named-tuple-fields, …
Browse files Browse the repository at this point in the history
…r=alexcrichton

This PR makes `rustc` emit field names for tuple fields in DWARF. Formerly there was no way of directly accessing the fields of a tuple in GDB and LLDB since there is no C/C++ equivalent to this. Now, the debugger sees the name `__{field-index}` for tuple fields. So you can type for example `some_tuple_val.__2` to get the third tuple component.
When pretty printers are used (e.g. via `rust-gdb` or `rust-lldb`) these artificial field names will not clutter tuple rendering (which was the main motivation for not doing this in the past).

Solves rust-lang#21948.
  • Loading branch information
bors committed Apr 13, 2015
2 parents 3cac76b + 03f9269 commit b9ed9e2
Show file tree
Hide file tree
Showing 30 changed files with 161 additions and 142 deletions.
12 changes: 9 additions & 3 deletions src/etc/gdb_rust_pretty_printing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# except according to those terms.

import gdb
import re

#===============================================================================
# GDB Pretty Printing Module for Rust
Expand Down Expand Up @@ -299,12 +300,12 @@ def classify_struct(type):
if fields[0].name == "RUST$ENUM$DISR":
if field_count == 1:
return STRUCT_KIND_CSTYLE_VARIANT
elif fields[1].name is None:
elif all_fields_conform_to_tuple_field_naming(fields, 1):
return STRUCT_KIND_TUPLE_VARIANT
else:
return STRUCT_KIND_STRUCT_VARIANT

if fields[0].name is None:
if all_fields_conform_to_tuple_field_naming(fields, 0):
if type.tag.startswith("("):
return STRUCT_KIND_TUPLE
else:
Expand All @@ -325,7 +326,6 @@ def first_field(val):
for field in val.type.fields():
return field


def get_field_at_index(val, index):
i = 0
for field in val.type.fields():
Expand All @@ -334,6 +334,12 @@ def get_field_at_index(val, index):
i += 1
return None

def all_fields_conform_to_tuple_field_naming(fields, start_index):
for i in range(start_index, len(fields)):
if (fields[i].name is None) or (re.match(r"__\d+$", fields[i].name) is None):
return False
return True

def extract_length_and_data_ptr_from_std_vec(vec_val):
length = int(vec_val["len"])
vec_ptr_val = vec_val["ptr"]
Expand Down
28 changes: 15 additions & 13 deletions src/etc/lldb_rust_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# except according to those terms.

import lldb

import re

def print_val(val, internal_dict):
'''Prints the given value with Rust syntax'''
Expand Down Expand Up @@ -61,22 +61,22 @@ def print_struct_val_starting_from(field_start_index, val, internal_dict):
# The only field of this struct is the enum discriminant
return type_name

has_field_names = type_has_field_names(t)
is_tuple_like = type_is_tuple_like(t)

if has_field_names:
template = "%(type_name)s {\n%(body)s\n}"
separator = ", \n"
else:
if is_tuple_like:
template = "%(type_name)s(%(body)s)"
separator = ", "
else:
template = "%(type_name)s {\n%(body)s\n}"
separator = ", \n"

if type_name.startswith("("):
# this is a tuple, so don't print the type name
type_name = ""

def render_child(child_index):
this = ""
if has_field_names:
if not is_tuple_like:
field_name = t.GetFieldAtIndex(child_index).GetName()
this += field_name + ": "

Expand Down Expand Up @@ -233,13 +233,15 @@ def extract_type_name(qualified_type_name):
return qualified_type_name[index + 2:]


def type_has_field_names(ty):
def type_is_tuple_like(ty):
'''Returns true of this is a type with field names (struct, struct-like enum variant)'''
# This may also be an enum variant where the first field doesn't have a name but the rest has
if ty.GetNumberOfFields() > 1:
return ty.GetFieldAtIndex(1).GetName() is not None
else:
return ty.GetFieldAtIndex(0).GetName() is not None
for field in ty.fields:
if field.GetName() == "RUST$ENUM$DISR":
# Ignore the enum discriminant field if there is one.
continue
if (field.GetName() is None) or (re.match(r"__\d+$", field.GetName()) is None):
return False
return True


def is_vec_slice(val):
Expand Down
19 changes: 14 additions & 5 deletions src/librustc_trans/trans/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2029,7 +2029,7 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> {

self.fields.iter().enumerate().map(|(i, field)| {
let name = if field.name == special_idents::unnamed_field.name {
"".to_string()
format!("__{}", i)
} else {
token::get_name(field.name).to_string()
};
Expand Down Expand Up @@ -2107,9 +2107,12 @@ struct TupleMemberDescriptionFactory<'tcx> {
impl<'tcx> TupleMemberDescriptionFactory<'tcx> {
fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>)
-> Vec<MemberDescription> {
self.component_types.iter().map(|&component_type| {
self.component_types
.iter()
.enumerate()
.map(|(i, &component_type)| {
MemberDescription {
name: "".to_string(),
name: format!("__{}", i),
llvm_type: type_of::type_of(cx, component_type),
type_metadata: type_metadata(cx, component_type, self.span),
offset: ComputedMemberOffset,
Expand Down Expand Up @@ -2262,7 +2265,7 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
let sole_struct_member_description = MemberDescription {
name: match non_null_variant.arg_names {
Some(ref names) => token::get_name(names[0]).to_string(),
None => "".to_string()
None => "__0".to_string()
},
llvm_type: non_null_llvm_type,
type_metadata: non_null_type_metadata,
Expand Down Expand Up @@ -2432,7 +2435,13 @@ fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
.map(|&name| token::get_name(name).to_string())
.collect()
}
None => variant_info.args.iter().map(|_| "".to_string()).collect()
None => {
variant_info.args
.iter()
.enumerate()
.map(|(i, _)| format!("__{}", i))
.collect()
}
};

// If this is not a univariant enum, there is also the discriminant field.
Expand Down
2 changes: 1 addition & 1 deletion src/test/debuginfo/associated-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
// gdb-command:continue

// gdb-command:print arg
// gdb-check:$5 = {4, 5}
// gdb-check:$5 = {__0 = 4, __1 = 5}
// gdb-command:continue

// gdb-command:print a
Expand Down
6 changes: 3 additions & 3 deletions src/test/debuginfo/borrowed-enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
// gdb-command:run

// gdb-command:print *the_a_ref
// gdb-check:$1 = {{RUST$ENUM$DISR = TheA, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = TheA, 0, 2088533116, 2088533116}}
// gdb-check:$1 = {{RUST$ENUM$DISR = TheA, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = TheA, __0 = 0, __1 = 2088533116, __2 = 2088533116}}

// gdb-command:print *the_b_ref
// gdb-check:$2 = {{RUST$ENUM$DISR = TheB, x = 0, y = 1229782938247303441}, {RUST$ENUM$DISR = TheB, 0, 286331153, 286331153}}
// gdb-check:$2 = {{RUST$ENUM$DISR = TheB, x = 0, y = 1229782938247303441}, {RUST$ENUM$DISR = TheB, __0 = 0, __1 = 286331153, __2 = 286331153}}

// gdb-command:print *univariant_ref
// gdb-check:$3 = {{4820353753753434}}
// gdb-check:$3 = {{__0 = 4820353753753434}}


// === LLDB TESTS ==================================================================================
Expand Down
6 changes: 3 additions & 3 deletions src/test/debuginfo/borrowed-tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
// gdb-command:run

// gdb-command:print *stack_val_ref
// gdb-check:$1 = {-14, -19}
// gdb-check:$1 = {__0 = -14, __1 = -19}

// gdb-command:print *ref_to_unnamed
// gdb-check:$2 = {-15, -20}
// gdb-check:$2 = {__0 = -15, __1 = -20}

// gdb-command:print *unique_val_ref
// gdb-check:$3 = {-17, -22}
// gdb-check:$3 = {__0 = -17, __1 = -22}


// === LLDB TESTS ==================================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/test/debuginfo/box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// gdb-command:print *a
// gdb-check:$1 = 1
// gdb-command:print *b
// gdb-check:$2 = {2, 3.5}
// gdb-check:$2 = {__0 = 2, __1 = 3.5}


// === LLDB TESTS ==================================================================================
Expand Down
6 changes: 3 additions & 3 deletions src/test/debuginfo/by-value-non-immediate-argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@
// gdb-command:continue

// gdb-command:print a
// gdb-check:$5 = {7, 8, 9.5, 10.5}
// gdb-check:$5 = {__0 = 7, __1 = 8, __2 = 9.5, __3 = 10.5}
// gdb-command:continue

// gdb-command:print a
// gdb-check:$6 = {11.5, 12.5, 13, 14}
// gdb-check:$6 = {__0 = 11.5, __1 = 12.5, __2 = 13, __3 = 14}
// gdb-command:continue

// gdb-command:print x
// gdb-check:$7 = {{RUST$ENUM$DISR = Case1, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = Case1, 0, 2088533116, 2088533116}}
// gdb-check:$7 = {{RUST$ENUM$DISR = Case1, x = 0, y = 8970181431921507452}, {RUST$ENUM$DISR = Case1, __0 = 0, __1 = 2088533116, __2 = 2088533116}}
// gdb-command:continue


Expand Down
2 changes: 1 addition & 1 deletion src/test/debuginfo/by-value-self-argument-in-trait-impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
// gdb-command:continue

// gdb-command:print self
// gdb-check:$3 = {4444.5, 5555, 6666, 7777.5}
// gdb-check:$3 = {__0 = 4444.5, __1 = 5555, __2 = 6666, __3 = 7777.5}
// gdb-command:continue


Expand Down
8 changes: 4 additions & 4 deletions src/test/debuginfo/c-style-enum-in-composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
// gdb-command:run

// gdb-command:print tuple_interior_padding
// gdb-check:$1 = {0, OneHundred}
// gdb-check:$1 = {__0 = 0, __1 = OneHundred}

// gdb-command:print tuple_padding_at_end
// gdb-check:$2 = {{1, OneThousand}, 2}
// gdb-check:$2 = {__0 = {__0 = 1, __1 = OneThousand}, __1 = 2}

// gdb-command:print tuple_different_enums
// gdb-check:$3 = {OneThousand, MountainView, OneMillion, Vienna}
// gdb-check:$3 = {__0 = OneThousand, __1 = MountainView, __2 = OneMillion, __3 = Vienna}

// gdb-command:print padded_struct
// gdb-check:$4 = {a = 3, b = OneMillion, c = 4, d = Toronto, e = 5}
Expand All @@ -36,7 +36,7 @@
// gdb-check:$6 = {a = OneMillion, b = MountainView, c = OneThousand, d = Toronto}

// gdb-command:print struct_with_drop
// gdb-check:$7 = {{a = OneHundred, b = Vienna}, 9}
// gdb-check:$7 = {__0 = {a = OneHundred, b = Vienna}, __1 = 9}


// === LLDB TESTS ==================================================================================
Expand Down
4 changes: 2 additions & 2 deletions src/test/debuginfo/cross-crate-spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ extern crate cross_crate_spans;
// gdb-command:run

// gdb-command:print result
// gdb-check:$1 = {17, 17}
// gdb-check:$1 = {__0 = 17, __1 = 17}
// gdb-command:print a_variable
// gdb-check:$2 = 123456789
// gdb-command:print another_variable
// gdb-check:$3 = 123456789.5
// gdb-command:continue

// gdb-command:print result
// gdb-check:$4 = {1212, 1212}
// gdb-check:$4 = {__0 = 1212, __1 = 1212}
// gdb-command:print a_variable
// gdb-check:$5 = 123456789
// gdb-command:print another_variable
Expand Down
12 changes: 6 additions & 6 deletions src/test/debuginfo/destructured-fn-argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// gdb-command:print a
// gdb-check:$6 = 5
// gdb-command:print b
// gdb-check:$7 = {6, 7}
// gdb-check:$7 = {__0 = 6, __1 = 7}
// gdb-command:continue

// gdb-command:print h
Expand Down Expand Up @@ -95,29 +95,29 @@
// gdb-command:continue

// gdb-command:print aa
// gdb-check:$30 = {34, 35}
// gdb-check:$30 = {__0 = 34, __1 = 35}
// gdb-command:continue

// gdb-command:print bb
// gdb-check:$31 = {36, 37}
// gdb-check:$31 = {__0 = 36, __1 = 37}
// gdb-command:continue

// gdb-command:print cc
// gdb-check:$32 = 38
// gdb-command:continue

// gdb-command:print dd
// gdb-check:$33 = {40, 41, 42}
// gdb-check:$33 = {__0 = 40, __1 = 41, __2 = 42}
// gdb-command:continue

// gdb-command:print *ee
// gdb-check:$34 = {43, 44, 45}
// gdb-check:$34 = {__0 = 43, __1 = 44, __2 = 45}
// gdb-command:continue

// gdb-command:print *ff
// gdb-check:$35 = 46
// gdb-command:print gg
// gdb-check:$36 = {47, 48}
// gdb-check:$36 = {__0 = 47, __1 = 48}
// gdb-command:continue

// gdb-command:print *hh
Expand Down
2 changes: 1 addition & 1 deletion src/test/debuginfo/destructured-for-loop-variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
// gdb-command:continue

// gdb-command:print simple_tuple_ident
// gdb-check:$24 = {34903493, 232323}
// gdb-check:$24 = {__0 = 34903493, __1 = 232323}
// gdb-command:continue

// === LLDB TESTS ==================================================================================
Expand Down
12 changes: 6 additions & 6 deletions src/test/debuginfo/destructured-local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// gdb-command:print f
// gdb-check:$6 = 5
// gdb-command:print g
// gdb-check:$7 = {6, 7}
// gdb-check:$7 = {__0 = 6, __1 = 7}

// gdb-command:print h
// gdb-check:$8 = 8
Expand Down Expand Up @@ -85,25 +85,25 @@
// gdb-check:$29 = 33

// gdb-command:print aa
// gdb-check:$30 = {34, 35}
// gdb-check:$30 = {__0 = 34, __1 = 35}

// gdb-command:print bb
// gdb-check:$31 = {36, 37}
// gdb-check:$31 = {__0 = 36, __1 = 37}

// gdb-command:print cc
// gdb-check:$32 = 38

// gdb-command:print dd
// gdb-check:$33 = {40, 41, 42}
// gdb-check:$33 = {__0 = 40, __1 = 41, __2 = 42}

// gdb-command:print *ee
// gdb-check:$34 = {43, 44, 45}
// gdb-check:$34 = {__0 = 43, __1 = 44, __2 = 45}

// gdb-command:print *ff
// gdb-check:$35 = 46

// gdb-command:print gg
// gdb-check:$36 = {47, 48}
// gdb-check:$36 = {__0 = 47, __1 = 48}

// gdb-command:print *hh
// gdb-check:$37 = 50
Expand Down
6 changes: 3 additions & 3 deletions src/test/debuginfo/generic-function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@
// gdb-command:print *t1
// gdb-check:$2 = 2.5
// gdb-command:print ret
// gdb-check:$3 = {{1, 2.5}, {2.5, 1}}
// gdb-check:$3 = {__0 = {__0 = 1, __1 = 2.5}, __1 = {__0 = 2.5, __1 = 1}}
// gdb-command:continue

// gdb-command:print *t0
// gdb-check:$4 = 3.5
// gdb-command:print *t1
// gdb-check:$5 = 4
// gdb-command:print ret
// gdb-check:$6 = {{3.5, 4}, {4, 3.5}}
// gdb-check:$6 = {__0 = {__0 = 3.5, __1 = 4}, __1 = {__0 = 4, __1 = 3.5}}
// gdb-command:continue

// gdb-command:print *t0
// gdb-check:$7 = 5
// gdb-command:print *t1
// gdb-check:$8 = {a = 6, b = 7.5}
// gdb-command:print ret
// gdb-check:$9 = {{5, {a = 6, b = 7.5}}, {{a = 6, b = 7.5}, 5}}
// gdb-check:$9 = {__0 = {__0 = 5, __1 = {a = 6, b = 7.5}}, __1 = {__0 = {a = 6, b = 7.5}, __1 = 5}}
// gdb-command:continue


Expand Down
Loading

0 comments on commit b9ed9e2

Please sign in to comment.