Skip to content

Commit b0d1c55

Browse files
committed
Auto merge of rust-lang#104342 - mweber15:add_file_location_to_more_types, r=wesleywiser
Require `type_map::stub` callers to supply file information This change attaches file information (`DIFile` reference and line number) to struct debug info nodes. Before: ``` ; foo.ll ... !5 = !DIFile(filename: "<unknown>", directory: "") ... !16 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyType", scope: !2, file: !5, size: 32, align: 32, elements: !17, templateParams: !19, identifier: "4cb373851db92e732c4cb5651b886dd0") ... ``` After: ``` ; foo.ll ... !3 = !DIFile(filename: "foo.rs", directory: "/home/matt/src/rust98678", checksumkind: CSK_SHA1, checksum: "bcb9f08512c8f3b8181ef4726012bc6807bc9be4") ... !16 = !DICompositeType(tag: DW_TAG_structure_type, name: "MyType", scope: !2, file: !3, line: 3, size: 32, align: 32, elements: !17, templateParams: !19, identifier: "9e5968c7af39c148acb253912b7f409f") ... ``` Fixes rust-lang#98678 r? `@wesleywiser`
2 parents ee03c28 + 9aa28cc commit b0d1c55

File tree

12 files changed

+337
-29
lines changed

12 files changed

+337
-29
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+68-10
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
211211
Stub::Struct,
212212
unique_type_id,
213213
&ptr_type_debuginfo_name,
214+
None,
214215
cx.size_and_align_of(ptr_type),
215216
NO_SCOPE_METADATA,
216217
DIFlags::FlagZero,
@@ -264,6 +265,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
264265
layout.fields.offset(abi::FAT_PTR_ADDR),
265266
DIFlags::FlagZero,
266267
data_ptr_type_di_node,
268+
None,
267269
),
268270
build_field_di_node(
269271
cx,
@@ -273,6 +275,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
273275
layout.fields.offset(abi::FAT_PTR_EXTRA),
274276
DIFlags::FlagZero,
275277
type_di_node(cx, extra_field.ty),
278+
None,
276279
),
277280
]
278281
},
@@ -376,6 +379,7 @@ fn build_dyn_type_di_node<'ll, 'tcx>(
376379
Stub::Struct,
377380
unique_type_id,
378381
&type_name,
382+
None,
379383
cx.size_and_align_of(dyn_type),
380384
NO_SCOPE_METADATA,
381385
DIFlags::FlagZero,
@@ -798,6 +802,7 @@ fn build_foreign_type_di_node<'ll, 'tcx>(
798802
Stub::Struct,
799803
unique_type_id,
800804
&compute_debuginfo_type_name(cx.tcx, t, false),
805+
None,
801806
cx.size_and_align_of(t),
802807
Some(get_namespace_for_item(cx, def_id)),
803808
DIFlags::FlagZero,
@@ -969,15 +974,22 @@ fn build_field_di_node<'ll, 'tcx>(
969974
offset: Size,
970975
flags: DIFlags,
971976
type_di_node: &'ll DIType,
977+
def_id: Option<DefId>,
972978
) -> &'ll DIType {
979+
let (file_metadata, line_number) = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers
980+
{
981+
file_metadata_from_def_id(cx, def_id)
982+
} else {
983+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
984+
};
973985
unsafe {
974986
llvm::LLVMRustDIBuilderCreateMemberType(
975987
DIB(cx),
976988
owner,
977989
name.as_ptr().cast(),
978990
name.len(),
979-
unknown_file_metadata(cx),
980-
UNKNOWN_LINE_NUMBER,
991+
file_metadata,
992+
line_number,
981993
size_and_align.0.bits(),
982994
size_and_align.1.bits() as u32,
983995
offset.bits(),
@@ -1021,6 +1033,11 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10211033
let containing_scope = get_namespace_for_item(cx, adt_def.did());
10221034
let struct_type_and_layout = cx.layout_of(struct_type);
10231035
let variant_def = adt_def.non_enum_variant();
1036+
let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1037+
Some(file_metadata_from_def_id(cx, Some(adt_def.did())))
1038+
} else {
1039+
None
1040+
};
10241041

10251042
type_map::build_type_with_children(
10261043
cx,
@@ -1029,6 +1046,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10291046
Stub::Struct,
10301047
unique_type_id,
10311048
&compute_debuginfo_type_name(cx.tcx, struct_type, false),
1049+
def_location,
10321050
size_and_align_of(struct_type_and_layout),
10331051
Some(containing_scope),
10341052
visibility_di_flags(cx, adt_def.did(), adt_def.did()),
@@ -1048,6 +1066,11 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10481066
Cow::Borrowed(f.name.as_str())
10491067
};
10501068
let field_layout = struct_type_and_layout.field(cx, i);
1069+
let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1070+
Some(f.did)
1071+
} else {
1072+
None
1073+
};
10511074
build_field_di_node(
10521075
cx,
10531076
owner,
@@ -1056,6 +1079,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
10561079
struct_type_and_layout.fields.offset(i),
10571080
visibility_di_flags(cx, f.did, adt_def.did()),
10581081
type_di_node(cx, field_layout.ty),
1082+
def_id,
10591083
)
10601084
})
10611085
.collect()
@@ -1107,6 +1131,7 @@ fn build_upvar_field_di_nodes<'ll, 'tcx>(
11071131
layout.fields.offset(index),
11081132
DIFlags::FlagZero,
11091133
type_di_node(cx, up_var_ty),
1134+
None,
11101135
)
11111136
})
11121137
.collect()
@@ -1132,6 +1157,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
11321157
Stub::Struct,
11331158
unique_type_id,
11341159
&type_name,
1160+
None,
11351161
size_and_align_of(tuple_type_and_layout),
11361162
NO_SCOPE_METADATA,
11371163
DIFlags::FlagZero,
@@ -1150,6 +1176,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
11501176
tuple_type_and_layout.fields.offset(index),
11511177
DIFlags::FlagZero,
11521178
type_di_node(cx, component_type),
1179+
None,
11531180
)
11541181
})
11551182
.collect()
@@ -1171,13 +1198,20 @@ fn build_closure_env_di_node<'ll, 'tcx>(
11711198
let containing_scope = get_namespace_for_item(cx, def_id);
11721199
let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
11731200

1201+
let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1202+
Some(file_metadata_from_def_id(cx, Some(def_id)))
1203+
} else {
1204+
None
1205+
};
1206+
11741207
type_map::build_type_with_children(
11751208
cx,
11761209
type_map::stub(
11771210
cx,
11781211
Stub::Struct,
11791212
unique_type_id,
11801213
&type_name,
1214+
def_location,
11811215
cx.size_and_align_of(closure_env_type),
11821216
Some(containing_scope),
11831217
DIFlags::FlagZero,
@@ -1201,6 +1235,11 @@ fn build_union_type_di_node<'ll, 'tcx>(
12011235
let containing_scope = get_namespace_for_item(cx, union_def_id);
12021236
let union_ty_and_layout = cx.layout_of(union_type);
12031237
let type_name = compute_debuginfo_type_name(cx.tcx, union_type, false);
1238+
let def_location = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1239+
Some(file_metadata_from_def_id(cx, Some(union_def_id)))
1240+
} else {
1241+
None
1242+
};
12041243

12051244
type_map::build_type_with_children(
12061245
cx,
@@ -1209,6 +1248,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12091248
Stub::Union,
12101249
unique_type_id,
12111250
&type_name,
1251+
def_location,
12121252
size_and_align_of(union_ty_and_layout),
12131253
Some(containing_scope),
12141254
DIFlags::FlagZero,
@@ -1221,6 +1261,11 @@ fn build_union_type_di_node<'ll, 'tcx>(
12211261
.enumerate()
12221262
.map(|(i, f)| {
12231263
let field_layout = union_ty_and_layout.field(cx, i);
1264+
let def_id = if cx.sess().opts.unstable_opts.debug_info_type_line_numbers {
1265+
Some(f.did)
1266+
} else {
1267+
None
1268+
};
12241269
build_field_di_node(
12251270
cx,
12261271
owner,
@@ -1229,6 +1274,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
12291274
Size::ZERO,
12301275
DIFlags::FlagZero,
12311276
type_di_node(cx, field_layout.ty),
1277+
def_id,
12321278
)
12331279
})
12341280
.collect()
@@ -1300,14 +1346,7 @@ pub fn build_global_var_di_node<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId, glo
13001346
// We may want to remove the namespace scope if we're in an extern block (see
13011347
// https://github.com/rust-lang/rust/pull/46457#issuecomment-351750952).
13021348
let var_scope = get_namespace_for_item(cx, def_id);
1303-
let span = tcx.def_span(def_id);
1304-
1305-
let (file_metadata, line_number) = if !span.is_dummy() {
1306-
let loc = cx.lookup_debug_loc(span.lo());
1307-
(file_metadata(cx, &loc.file), loc.line)
1308-
} else {
1309-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1310-
};
1349+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
13111350

13121351
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
13131352

@@ -1397,6 +1436,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
13971436
Stub::VTableTy { vtable_holder },
13981437
unique_type_id,
13991438
&vtable_type_name,
1439+
None,
14001440
(size, pointer_align),
14011441
NO_SCOPE_METADATA,
14021442
DIFlags::FlagArtificial,
@@ -1434,6 +1474,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
14341474
field_offset,
14351475
DIFlags::FlagZero,
14361476
field_type_di_node,
1477+
None,
14371478
))
14381479
})
14391480
.collect()
@@ -1589,3 +1630,20 @@ pub fn tuple_field_name(field_index: usize) -> Cow<'static, str> {
15891630
.map(|s| Cow::from(*s))
15901631
.unwrap_or_else(|| Cow::from(format!("__{field_index}")))
15911632
}
1633+
1634+
pub type DefinitionLocation<'ll> = (&'ll DIFile, c_uint);
1635+
1636+
pub fn file_metadata_from_def_id<'ll>(
1637+
cx: &CodegenCx<'ll, '_>,
1638+
def_id: Option<DefId>,
1639+
) -> DefinitionLocation<'ll> {
1640+
if let Some(def_id) = def_id
1641+
&& let span = cx.tcx.def_span(def_id)
1642+
&& !span.is_dummy()
1643+
{
1644+
let loc = cx.lookup_debug_loc(span.lo());
1645+
(file_metadata(cx, &loc.file), loc.line)
1646+
} else {
1647+
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
1648+
}
1649+
}

0 commit comments

Comments
 (0)