Skip to content

Commit 6d20876

Browse files
debuginfo: Fix issue #11083 and some minor clean up.
1 parent d459e80 commit 6d20876

File tree

3 files changed

+75
-64
lines changed

3 files changed

+75
-64
lines changed

src/librustc/lib/llvm.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1653,7 +1653,8 @@ pub mod llvm {
16531653
AlignInBits: c_ulonglong,
16541654
Flags: c_uint,
16551655
Elements: ValueRef,
1656-
RunTimeLang: c_uint)
1656+
RunTimeLang: c_uint,
1657+
UniqueId: *c_char)
16571658
-> ValueRef;
16581659

16591660
pub fn LLVMSetUnnamedAddr(GlobalVar: ValueRef, UnnamedAddr: Bool);

src/librustc/middle/trans/debuginfo.rs

Lines changed: 69 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ use syntax::parse::token::special_idents;
157157
static DW_LANG_RUST: c_uint = 0x9000;
158158

159159
static DW_TAG_auto_variable: c_uint = 0x100;
160-
// static DW_TAG_arg_variable: c_uint = 0x101;
160+
static DW_TAG_arg_variable: c_uint = 0x101;
161161

162162
static DW_ATE_boolean: c_uint = 0x02;
163163
static DW_ATE_float: c_uint = 0x04;
@@ -980,11 +980,11 @@ fn declare_local(bcx: @Block,
980980
let loc = span_start(cx, span);
981981
let type_metadata = type_metadata(cx, variable_type, span);
982982

983-
let argument_index = match variable_kind {
984-
ArgumentVariable(index) => index,
983+
let (argument_index, dwarf_tag) = match variable_kind {
984+
ArgumentVariable(index) => (index as c_uint, DW_TAG_arg_variable),
985985
LocalVariable |
986-
CapturedVariable => 0
987-
} as c_uint;
986+
CapturedVariable => (0, DW_TAG_auto_variable)
987+
};
988988

989989
let (var_alloca, var_metadata) = name.with_c_str(|name| {
990990
match variable_access {
@@ -993,7 +993,7 @@ fn declare_local(bcx: @Block,
993993
unsafe {
994994
llvm::LLVMDIBuilderCreateLocalVariable(
995995
DIB(cx),
996-
DW_TAG_auto_variable,
996+
dwarf_tag,
997997
scope_metadata,
998998
name,
999999
file_metadata,
@@ -1009,7 +1009,7 @@ fn declare_local(bcx: @Block,
10091009
unsafe {
10101010
llvm::LLVMDIBuilderCreateComplexVariable(
10111011
DIB(cx),
1012-
DW_TAG_auto_variable,
1012+
dwarf_tag,
10131013
scope_metadata,
10141014
name,
10151015
file_metadata,
@@ -1256,14 +1256,12 @@ impl RecursiveTypeDescription {
12561256
} => {
12571257
// Insert the stub into the cache in order to allow recursive references ...
12581258
{
1259-
let mut created_types = debug_context(cx).created_types
1260-
.borrow_mut();
1259+
let mut created_types = debug_context(cx).created_types.borrow_mut();
12611260
created_types.get().insert(cache_id, metadata_stub);
12621261
}
12631262

12641263
// ... then create the member descriptions ...
1265-
let member_descriptions = member_description_factory.
1266-
create_member_descriptions(cx);
1264+
let member_descriptions = member_description_factory.create_member_descriptions(cx);
12671265

12681266
// ... and attach them to the stub to complete it.
12691267
set_members_of_composite_type(cx,
@@ -1348,13 +1346,13 @@ impl MemberDescriptionFactory for GeneralMemberDescriptionFactory {
13481346
.enumerate()
13491347
.map(|(i, struct_def)| {
13501348
let (variant_type_metadata, variant_llvm_type, member_desc_factory) =
1351-
describe_variant(cx,
1352-
struct_def,
1353-
self.variants[i],
1354-
Some(self.discriminant_type_metadata),
1355-
self.containing_scope,
1356-
self.file_metadata,
1357-
self.span);
1349+
describe_enum_variant(cx,
1350+
struct_def,
1351+
self.variants[i],
1352+
Some(self.discriminant_type_metadata),
1353+
self.containing_scope,
1354+
self.file_metadata,
1355+
self.span);
13581356

13591357
let member_descriptions =
13601358
member_desc_factory.create_member_descriptions(cx);
@@ -1398,14 +1396,14 @@ impl MemberDescriptionFactory for EnumVariantMemberDescriptionFactory {
13981396
}
13991397
}
14001398

1401-
fn describe_variant(cx: &CrateContext,
1402-
struct_def: &adt::Struct,
1403-
variant_info: &ty::VariantInfo,
1404-
discriminant_type_metadata: Option<DIType>,
1405-
containing_scope: DIScope,
1406-
file_metadata: DIFile,
1407-
span: Span)
1408-
-> (DICompositeType, Type, @MemberDescriptionFactory) {
1399+
fn describe_enum_variant(cx: &CrateContext,
1400+
struct_def: &adt::Struct,
1401+
variant_info: &ty::VariantInfo,
1402+
discriminant_type_metadata: Option<DIType>,
1403+
containing_scope: DIScope,
1404+
file_metadata: DIFile,
1405+
span: Span)
1406+
-> (DICompositeType, Type, @MemberDescriptionFactory) {
14091407
let variant_name = token::ident_to_str(&variant_info.name);
14101408
let variant_llvm_type = Type::struct_(struct_def.fields.map(|&t| type_of::type_of(cx, t)),
14111409
struct_def.packed);
@@ -1538,13 +1536,13 @@ fn prepare_enum_metadata(cx: &CrateContext,
15381536
assert!(variants.len() == 1);
15391537
let (metadata_stub,
15401538
variant_llvm_type,
1541-
member_description_factory) = describe_variant(cx,
1542-
struct_def,
1543-
variants[0],
1544-
None,
1545-
containing_scope,
1546-
file_metadata,
1547-
span);
1539+
member_description_factory) = describe_enum_variant(cx,
1540+
struct_def,
1541+
variants[0],
1542+
None,
1543+
containing_scope,
1544+
file_metadata,
1545+
span);
15481546
UnfinishedMetadata {
15491547
cache_id: cache_id_for_type(enum_type),
15501548
metadata_stub: metadata_stub,
@@ -1557,21 +1555,25 @@ fn prepare_enum_metadata(cx: &CrateContext,
15571555
let discriminant_type_metadata = discriminant_type_metadata(inttype);
15581556
let enum_llvm_type = type_of::type_of(cx, enum_type);
15591557
let (enum_type_size, enum_type_align) = size_and_align_of(cx, enum_llvm_type);
1558+
let unique_id = generate_unique_type_id("DI_ENUM_");
15601559

15611560
let enum_metadata = enum_name.with_c_str(|enum_name| {
1562-
unsafe {
1563-
llvm::LLVMDIBuilderCreateUnionType(
1564-
DIB(cx),
1565-
containing_scope,
1566-
enum_name,
1567-
file_metadata,
1568-
loc.line as c_uint,
1569-
bytes_to_bits(enum_type_size),
1570-
bytes_to_bits(enum_type_align),
1571-
0, // Flags
1572-
ptr::null(),
1573-
0) // RuntimeLang
1574-
}
1561+
unique_id.with_c_str(|unique_id| {
1562+
unsafe {
1563+
llvm::LLVMDIBuilderCreateUnionType(
1564+
DIB(cx),
1565+
containing_scope,
1566+
enum_name,
1567+
file_metadata,
1568+
loc.line as c_uint,
1569+
bytes_to_bits(enum_type_size),
1570+
bytes_to_bits(enum_type_align),
1571+
0, // Flags
1572+
ptr::null(),
1573+
0, // RuntimeLang
1574+
unique_id)
1575+
}
1576+
})
15751577
});
15761578

15771579
UnfinishedMetadata {
@@ -1592,13 +1594,13 @@ fn prepare_enum_metadata(cx: &CrateContext,
15921594
adt::NullablePointer { nonnull: ref struct_def, nndiscr, .. } => {
15931595
let (metadata_stub,
15941596
variant_llvm_type,
1595-
member_description_factory) = describe_variant(cx,
1596-
struct_def,
1597-
variants[nndiscr],
1598-
None,
1599-
containing_scope,
1600-
file_metadata,
1601-
span);
1597+
member_description_factory) = describe_enum_variant(cx,
1598+
struct_def,
1599+
variants[nndiscr],
1600+
None,
1601+
containing_scope,
1602+
file_metadata,
1603+
span);
16021604
UnfinishedMetadata {
16031605
cache_id: cache_id_for_type(enum_type),
16041606
metadata_stub: metadata_stub,
@@ -1725,10 +1727,7 @@ fn create_struct_stub(cx: &CrateContext,
17251727

17261728
// We assign unique IDs to the type stubs so LLVM metadata uniquing does not reuse instances
17271729
// where we don't want it.
1728-
let unique_id = unsafe {
1729-
static mut unique_id_counter: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
1730-
format!("DiStructStub{}", unique_id_counter.fetch_add(1, atomics::SeqCst))
1731-
};
1730+
let unique_id = generate_unique_type_id("DI_STRUCT_");
17321731

17331732
return unsafe {
17341733
struct_type_name.with_c_str(|name| {
@@ -2059,10 +2058,6 @@ fn trait_metadata(cx: &CrateContext,
20592058
definition_span);
20602059
}
20612060

2062-
fn cache_id_for_type(t: ty::t) -> uint {
2063-
ty::type_id(t)
2064-
}
2065-
20662061
fn type_metadata(cx: &CrateContext,
20672062
t: ty::t,
20682063
usage_site_span: Span)
@@ -2244,6 +2239,19 @@ fn set_debug_location(cx: &CrateContext, debug_location: DebugLocation) {
22442239
// Utility Functions
22452240
//=-------------------------------------------------------------------------------------------------
22462241

2242+
fn cache_id_for_type(t: ty::t) -> uint {
2243+
ty::type_id(t)
2244+
}
2245+
2246+
// Used to avoid LLVM metadata uniquing problems. See `create_struct_stub()` and
2247+
// `prepare_enum_metadata()`.
2248+
fn generate_unique_type_id(prefix: &'static str) -> ~str {
2249+
unsafe {
2250+
static mut unique_id_counter: atomics::AtomicUint = atomics::INIT_ATOMIC_UINT;
2251+
format!("{}{}", prefix, unique_id_counter.fetch_add(1, atomics::SeqCst))
2252+
}
2253+
}
2254+
22472255
/// Return codemap::Loc corresponding to the beginning of the span
22482256
fn span_start(cx: &CrateContext, span: Span) -> codemap::Loc {
22492257
cx.sess.codemap.lookup_char_pos(span.lo)

src/rustllvm/RustWrapper.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,8 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateUnionType(
442442
uint64_t AlignInBits,
443443
unsigned Flags,
444444
LLVMValueRef Elements,
445-
unsigned RunTimeLang)
445+
unsigned RunTimeLang,
446+
const char* UniqueId)
446447
{
447448
return wrap(Builder->createUnionType(
448449
unwrapDI<DIDescriptor>(Scope),
@@ -453,7 +454,8 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateUnionType(
453454
AlignInBits,
454455
Flags,
455456
unwrapDI<DIArray>(Elements),
456-
RunTimeLang));
457+
RunTimeLang,
458+
UniqueId));
457459
}
458460

459461
extern "C" void LLVMSetUnnamedAddr(LLVMValueRef Value, LLVMBool Unnamed) {

0 commit comments

Comments
 (0)