Skip to content

Commit a616535

Browse files
committed
Add file and line metadata for enum variant and fields
1 parent 09f25f4 commit a616535

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
350350

351351
let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node(
352352
cx,
353+
enum_adt_def,
353354
enum_type_and_layout,
354355
enum_type_di_node,
355356
variant_index,
@@ -479,6 +480,7 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
479480

480481
fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
481482
cx: &CodegenCx<'ll, 'tcx>,
483+
enum_adt_def: AdtDef<'tcx>,
482484
enum_or_generator_type_and_layout: TyAndLayout<'tcx>,
483485
enum_or_generator_type_di_node: &'ll DIType,
484486
variant_index: VariantIdx,
@@ -500,7 +502,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
500502
variant_index,
501503
),
502504
&variant_struct_wrapper_type_name(variant_index),
503-
None,
505+
Some(enum_adt_def.variant(variant_index).def_id),
504506
// NOTE: We use size and align of enum_type, not from variant_layout:
505507
size_and_align_of(enum_or_generator_type_and_layout),
506508
Some(enum_or_generator_type_di_node),
@@ -784,8 +786,13 @@ fn build_union_fields_for_direct_tag_enum_or_generator<'ll, 'tcx>(
784786
let field_name = variant_union_field_name(variant_member_info.variant_index);
785787
let (size, align) = size_and_align_of(enum_type_and_layout);
786788

789+
let ty::Adt(enum_adt_def, _) = enum_type_and_layout.ty.kind() else {
790+
unreachable!();
791+
};
792+
787793
let variant_struct_type_wrapper = build_variant_struct_wrapper_type_di_node(
788794
cx,
795+
*enum_adt_def,
789796
enum_type_and_layout,
790797
enum_type_di_node,
791798
variant_member_info.variant_index,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
265265
variant_index,
266266
),
267267
variant_def.name.as_str(),
268-
None,
268+
Some(variant_def.def_id),
269269
// NOTE: We use size and align of enum_type, not from variant_layout:
270270
size_and_align_of(enum_type_and_layout),
271271
Some(enum_type_di_node),

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
debuginfo::{
66
metadata::{
77
enums::tag_base_type,
8-
file_metadata, size_and_align_of, type_di_node,
8+
file_metadata, file_metadata_from_def_id, size_and_align_of, type_di_node,
99
type_map::{self, Stub, StubInfo, UniqueTypeId},
1010
unknown_file_metadata, DINodeCreationResult, SmallVec, NO_GENERICS,
1111
UNKNOWN_LINE_NUMBER,
@@ -94,14 +94,18 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
9494
enum_adt_def.variant(variant_index),
9595
enum_type_and_layout.for_variant(cx, variant_index),
9696
),
97-
source_info: None,
97+
source_info: Some(file_metadata_from_def_id(
98+
cx,
99+
Some(enum_adt_def.variant(variant_index).def_id),
100+
)),
98101
})
99102
.collect();
100103

101104
smallvec![build_enum_variant_part_di_node(
102105
cx,
103106
enum_type_and_layout,
104107
enum_type_di_node,
108+
enum_adt_def.did(),
105109
&variant_member_infos[..],
106110
)]
107111
},
@@ -209,6 +213,7 @@ pub(super) fn build_generator_di_node<'ll, 'tcx>(
209213
cx,
210214
generator_type_and_layout,
211215
generator_type_di_node,
216+
generator_def_id,
212217
&variant_struct_type_di_nodes[..],
213218
)]
214219
},
@@ -236,6 +241,7 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
236241
cx: &CodegenCx<'ll, 'tcx>,
237242
enum_type_and_layout: TyAndLayout<'tcx>,
238243
enum_type_di_node: &'ll DIType,
244+
enum_type_def_id: rustc_span::def_id::DefId,
239245
variant_member_infos: &[VariantMemberInfo<'_, 'll>],
240246
) -> &'ll DIType {
241247
let tag_member_di_node =
@@ -244,6 +250,8 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
244250
let variant_part_unique_type_id =
245251
UniqueTypeId::for_enum_variant_part(cx.tcx, enum_type_and_layout.ty);
246252

253+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(enum_type_def_id));
254+
247255
let stub = StubInfo::new(
248256
cx,
249257
variant_part_unique_type_id,
@@ -254,8 +262,8 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
254262
enum_type_di_node,
255263
variant_part_name.as_ptr().cast(),
256264
variant_part_name.len(),
257-
unknown_file_metadata(cx),
258-
UNKNOWN_LINE_NUMBER,
265+
file_metadata,
266+
line_number,
259267
enum_type_and_layout.size.bits(),
260268
enum_type_and_layout.align.abi.bits() as u32,
261269
DIFlags::FlagZero,

compiler/rustc_codegen_llvm/src/debuginfo/metadata/type_map.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
},
2121
};
2222

23-
use super::{file_metadata, unknown_file_metadata, SmallVec, UNKNOWN_LINE_NUMBER};
23+
use super::SmallVec;
2424

2525
mod private {
2626
// This type cannot be constructed outside of this module because
@@ -191,17 +191,7 @@ pub(super) fn stub<'ll, 'tcx>(
191191
let empty_array = create_DIArray(DIB(cx), &[]);
192192
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
193193

194-
let (file_metadata, line_number) = if let Some(def_id) = def_id {
195-
let span = cx.tcx.def_span(def_id);
196-
if !span.is_dummy() {
197-
let loc = cx.lookup_debug_loc(span.lo());
198-
(file_metadata(cx, &loc.file), loc.line)
199-
} else {
200-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
201-
}
202-
} else {
203-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
204-
};
194+
let (file_metadata, line_number) = super::file_metadata_from_def_id(cx, def_id);
205195

206196
let metadata = match kind {
207197
Stub::Struct | Stub::VTableTy { .. } => {

tests/codegen/issue-98678.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
// The use of CHECK-DAG here is because the C++-like enum is emitted before the `DIFile` node
88

9-
// CHECK-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}src/test/codegen/issue-98678.rs{{".*}})
9+
// CHECK-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}/codegen/issue-98678.rs{{".*}})
1010

1111
// CHECK-DAG: !DICompositeType({{.*"}}MyCppLikeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],
1212
#[repr(C)]
@@ -28,8 +28,10 @@ pub union MyUnion {
2828
f: f32,
2929
}
3030

31-
// CHECK: !DICompositeType({{.*"}}MyNativeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
31+
// CHECK: !DICompositeType({{.*"}}MyNativeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],
32+
// CHECK: !DICompositeType({{.*}}DW_TAG_variant_part{{.*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
3233
pub enum MyNativeEnum {
34+
// CHECK: !DIDerivedType({{.*"}}One{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
3335
One,
3436
}
3537

0 commit comments

Comments
 (0)