Skip to content

Commit aa485fc

Browse files
committed
Add file and line metadata for enum variant and fields
1 parent af6b0de commit aa485fc

File tree

5 files changed

+27
-19
lines changed

5 files changed

+27
-19
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
@@ -338,6 +338,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
338338

339339
let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node(
340340
cx,
341+
enum_adt_def,
341342
enum_type_and_layout,
342343
enum_type_di_node,
343344
variant_index,
@@ -470,6 +471,7 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
470471

471472
fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
472473
cx: &CodegenCx<'ll, 'tcx>,
474+
enum_adt_def: AdtDef<'tcx>,
473475
enum_or_coroutine_type_and_layout: TyAndLayout<'tcx>,
474476
enum_or_coroutine_type_di_node: &'ll DIType,
475477
variant_index: VariantIdx,
@@ -491,7 +493,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
491493
variant_index,
492494
),
493495
&variant_struct_wrapper_type_name(variant_index),
494-
None,
496+
Some(enum_adt_def.variant(variant_index).def_id),
495497
// NOTE: We use size and align of enum_type, not from variant_layout:
496498
size_and_align_of(enum_or_coroutine_type_and_layout),
497499
Some(enum_or_coroutine_type_di_node),
@@ -778,8 +780,13 @@ fn build_union_fields_for_direct_tag_enum_or_coroutine<'ll, 'tcx>(
778780
let field_name = variant_union_field_name(variant_member_info.variant_index);
779781
let (size, align) = size_and_align_of(enum_type_and_layout);
780782

783+
let ty::Adt(enum_adt_def, _) = enum_type_and_layout.ty.kind() else {
784+
unreachable!();
785+
};
786+
781787
let variant_struct_type_wrapper = build_variant_struct_wrapper_type_di_node(
782788
cx,
789+
*enum_adt_def,
783790
enum_type_and_layout,
784791
enum_type_di_node,
785792
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
@@ -208,7 +208,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
208208
variant_index,
209209
),
210210
variant_def.name.as_str(),
211-
None,
211+
Some(variant_def.def_id),
212212
// NOTE: We use size and align of enum_type, not from variant_layout:
213213
size_and_align_of(enum_type_and_layout),
214214
Some(enum_type_di_node),

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use crate::common::{AsCCharPtr, CodegenCx};
1414
use crate::debuginfo::metadata::type_map::{self, Stub, StubInfo, UniqueTypeId};
1515
use crate::debuginfo::metadata::{
1616
DINodeCreationResult, NO_GENERICS, SmallVec, UNKNOWN_LINE_NUMBER, file_metadata,
17-
size_and_align_of, type_di_node, unknown_file_metadata, visibility_di_flags,
17+
file_metadata_from_def_id, size_and_align_of, type_di_node, unknown_file_metadata,
18+
visibility_di_flags,
1819
};
1920
use crate::debuginfo::utils::{DIB, create_DIArray, get_namespace_for_item};
2021
use crate::llvm::debuginfo::{DIFile, DIFlags, DIType};
@@ -85,14 +86,18 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
8586
enum_type_and_layout.for_variant(cx, variant_index),
8687
visibility_flags,
8788
),
88-
source_info: None,
89+
source_info: Some(file_metadata_from_def_id(
90+
cx,
91+
Some(enum_adt_def.variant(variant_index).def_id),
92+
)),
8993
})
9094
.collect();
9195

9296
smallvec![build_enum_variant_part_di_node(
9397
cx,
9498
enum_type_and_layout,
9599
enum_type_di_node,
100+
enum_adt_def.did(),
96101
&variant_member_infos[..],
97102
)]
98103
},
@@ -203,6 +208,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
203208
cx,
204209
coroutine_type_and_layout,
205210
coroutine_type_di_node,
211+
coroutine_def_id,
206212
&variant_struct_type_di_nodes[..],
207213
)]
208214
},
@@ -230,6 +236,7 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
230236
cx: &CodegenCx<'ll, 'tcx>,
231237
enum_type_and_layout: TyAndLayout<'tcx>,
232238
enum_type_di_node: &'ll DIType,
239+
enum_type_def_id: rustc_span::def_id::DefId,
233240
variant_member_infos: &[VariantMemberInfo<'_, 'll>],
234241
) -> &'ll DIType {
235242
let tag_member_di_node =
@@ -238,6 +245,8 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
238245
let variant_part_unique_type_id =
239246
UniqueTypeId::for_enum_variant_part(cx.tcx, enum_type_and_layout.ty);
240247

248+
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(enum_type_def_id));
249+
241250
let stub = StubInfo::new(
242251
cx,
243252
variant_part_unique_type_id,
@@ -248,8 +257,8 @@ fn build_enum_variant_part_di_node<'ll, 'tcx>(
248257
enum_type_di_node,
249258
variant_part_name.as_c_char_ptr(),
250259
variant_part_name.len(),
251-
unknown_file_metadata(cx),
252-
UNKNOWN_LINE_NUMBER,
260+
file_metadata,
261+
line_number,
253262
enum_type_and_layout.size.bits(),
254263
enum_type_and_layout.align.abi.bits() as u32,
255264
DIFlags::FlagZero,

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,7 @@ pub(super) fn stub<'ll, 'tcx>(
182182
let empty_array = create_DIArray(DIB(cx), &[]);
183183
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
184184

185-
let (file_metadata, line_number) = if let Some(def_id) = def_id {
186-
let span = cx.tcx.def_span(def_id);
187-
if !span.is_dummy() {
188-
let loc = cx.lookup_debug_loc(span.lo());
189-
(file_metadata(cx, &loc.file), loc.line)
190-
} else {
191-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
192-
}
193-
} else {
194-
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
195-
};
185+
let (file_metadata, line_number) = super::file_metadata_from_def_id(cx, def_id);
196186

197187
let metadata = match kind {
198188
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)