Skip to content

Commit af6b0de

Browse files
committed
Add file and line metadata for struct/union members
1 parent c07797a commit af6b0de

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
261261
layout.fields.offset(abi::WIDE_PTR_ADDR),
262262
DIFlags::FlagZero,
263263
data_ptr_type_di_node,
264+
None,
264265
),
265266
build_field_di_node(
266267
cx,
@@ -270,6 +271,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
270271
layout.fields.offset(abi::WIDE_PTR_EXTRA),
271272
DIFlags::FlagZero,
272273
type_di_node(cx, extra_field.ty),
274+
None,
273275
),
274276
]
275277
},
@@ -994,15 +996,17 @@ fn build_field_di_node<'ll, 'tcx>(
994996
offset: Size,
995997
flags: DIFlags,
996998
type_di_node: &'ll DIType,
999+
def_id: Option<DefId>,
9971000
) -> &'ll DIType {
1001+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, def_id);
9981002
unsafe {
9991003
llvm::LLVMRustDIBuilderCreateMemberType(
10001004
DIB(cx),
10011005
owner,
10021006
name.as_c_char_ptr(),
10031007
name.len(),
1004-
unknown_file_metadata(cx),
1005-
UNKNOWN_LINE_NUMBER,
1008+
file_metadata,
1009+
line_number,
10061010
size_and_align.0.bits(),
10071011
size_and_align.1.bits() as u32,
10081012
offset.bits(),
@@ -1082,6 +1086,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10821086
struct_type_and_layout.fields.offset(i),
10831087
visibility_di_flags(cx, f.did, adt_def.did()),
10841088
type_di_node(cx, field_layout.ty),
1089+
Some(f.did),
10851090
)
10861091
})
10871092
.collect()
@@ -1133,6 +1138,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
11331138
layout.fields.offset(index),
11341139
DIFlags::FlagZero,
11351140
type_di_node(cx, up_var_ty),
1141+
None,
11361142
)
11371143
})
11381144
.collect()
@@ -1177,6 +1183,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
11771183
tuple_type_and_layout.fields.offset(index),
11781184
DIFlags::FlagZero,
11791185
type_di_node(cx, component_type),
1186+
None,
11801187
)
11811188
})
11821189
.collect()
@@ -1258,6 +1265,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12581265
Size::ZERO,
12591266
DIFlags::FlagZero,
12601267
type_di_node(cx, field_layout.ty),
1268+
Some(f.did),
12611269
)
12621270
})
12631271
.collect()
@@ -1333,14 +1341,7 @@ pub(crate) fn build_global_var_di_node<'ll>(
13331341
// We may want to remove the namespace scope if we're in an extern block (see
13341342
// https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952).
13351343
let var_scope = get_namespace_for_item(cx, def_id);
1336-
let span = hygiene::walk_chain_collapsed(tcx.def_span(def_id), DUMMY_SP);
1337-
1338-
let (file_metadata, line_number) = if !span.is_dummy() {
1339-
let loc = cx.lookup_debug_loc(span.lo());
1340-
(file_metadata(cx, &loc.file), loc.line)
1341-
} else {
1342-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1343-
};
1344+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
13441345

13451346
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
13461347

@@ -1468,6 +1469,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
14681469
field_offset,
14691470
DIFlags::FlagZero,
14701471
field_type_di_node,
1472+
None,
14711473
))
14721474
})
14731475
.collect()
@@ -1619,3 +1621,18 @@ fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
16191621
.map(|s| Cow::from(*s))
16201622
.unwrap_or_else(|| Cow::from(format!("__{field_index}")))
16211623
}
1624+
1625+
pub(crate) fn file_metadata_from_def_id<'ll>(
1626+
cx: &CodegenCx<'ll, '_>,
1627+
def_id: Option<DefId>,
1628+
) -> (&'ll DIFile, c_uint) {
1629+
if let Some(def_id) = def_id
1630+
&& let span = hygiene::walk_chain_collapsed(cx.tcx.def_span(def_id), DUMMY_SP)
1631+
&& !span.is_dummy()
1632+
{
1633+
let loc = cx.lookup_debug_loc(span.lo());
1634+
(file_metadata(cx, &loc.file), loc.line)
1635+
} else {
1636+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1637+
}
1638+
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/cpp_like.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
360360
Size::ZERO,
361361
visibility_flags,
362362
variant_struct_type_wrapper_di_node,
363+
None,
363364
),
364365
unsafe {
365366
llvm::LLVMRustDIBuilderCreateStaticMemberType(
@@ -540,6 +541,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
540541
Size::ZERO,
541542
DIFlags::FlagZero,
542543
variant_struct_type_di_node,
544+
None,
543545
));
544546

545547
let build_assoc_const =
@@ -842,6 +844,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
842844
lo_offset,
843845
di_flags,
844846
type_di_node,
847+
None,
845848
));
846849

847850
unions_fields.push(build_field_di_node(
@@ -852,6 +855,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
852855
hi_offset,
853856
DIFlags::FlagZero,
854857
type_di_node,
858+
None,
855859
));
856860
} else {
857861
unions_fields.push(build_field_di_node(
@@ -862,6 +866,7 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
862866
enum_type_and_layout.fields.offset(tag_field),
863867
di_flags,
864868
tag_base_type_di_node,
869+
None,
865870
));
866871
}
867872

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
236236
variant_layout.fields.offset(field_index),
237237
di_flags,
238238
type_di_node(cx, field_layout.ty),
239+
None,
239240
)
240241
})
241242
.collect::<SmallVec<_>>()
@@ -318,6 +319,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
318319
variant_layout.fields.offset(field_index),
319320
DIFlags::FlagZero,
320321
type_di_node(cx, field_type),
322+
None,
321323
)
322324
})
323325
.collect();
@@ -337,6 +339,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
337339
coroutine_type_and_layout.fields.offset(index),
338340
DIFlags::FlagZero,
339341
type_di_node(cx, upvar_ty),
342+
None,
340343
)
341344
})
342345
.collect();

tests/codegen/issue-98678.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ pub enum MyCppLikeEnum {
1515
}
1616

1717
// CHECK: !DICompositeType({{.*"}}MyType{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
18-
pub struct MyType;
18+
pub struct MyType {
19+
// CHECK: !DIDerivedType({{.*"}}i{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
20+
i: i32,
21+
}
1922

2023
// CHECK: !DICompositeType({{.*"}}MyUnion{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
2124
pub union MyUnion {
22-
i: i32, // TODO fields are still wrong
25+
// CHECK: !DIDerivedType({{.*"}}i{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
26+
i: i32,
27+
// CHECK: !DIDerivedType({{.*"}}f{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
2328
f: f32,
2429
}
2530

0 commit comments

Comments
 (0)