1
Fork 0

Refactor type_map::stub parameters

Push span lookup into `type_map::stub` and pass the `DefId` instead of
doing the lookup outside and passing in the location metadata.
This commit is contained in:
Matt Weber 2022-11-16 21:31:30 -05:00
parent 94669d9d47
commit f3da828185
6 changed files with 65 additions and 62 deletions

View file

@ -204,8 +204,7 @@ fn build_pointer_or_reference_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&ptr_type_debuginfo_name, &ptr_type_debuginfo_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
cx.size_and_align_of(ptr_type), cx.size_and_align_of(ptr_type),
NO_SCOPE_METADATA, NO_SCOPE_METADATA,
DIFlags::FlagZero, DIFlags::FlagZero,
@ -373,8 +372,7 @@ fn build_dyn_type_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&type_name, &type_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
cx.size_and_align_of(dyn_type), cx.size_and_align_of(dyn_type),
NO_SCOPE_METADATA, NO_SCOPE_METADATA,
DIFlags::FlagZero, DIFlags::FlagZero,
@ -845,8 +843,7 @@ fn build_foreign_type_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&compute_debuginfo_type_name(cx.tcx, t, false), &compute_debuginfo_type_name(cx.tcx, t, false),
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
cx.size_and_align_of(t), cx.size_and_align_of(t),
Some(get_namespace_for_item(cx, def_id)), Some(get_namespace_for_item(cx, def_id)),
DIFlags::FlagZero, DIFlags::FlagZero,
@ -1050,15 +1047,6 @@ fn build_struct_type_di_node<'ll, 'tcx>(
let struct_type_and_layout = cx.layout_of(struct_type); let struct_type_and_layout = cx.layout_of(struct_type);
let variant_def = adt_def.non_enum_variant(); let variant_def = adt_def.non_enum_variant();
let tcx = cx.tcx;
let struct_span = tcx.def_span(adt_def.did());
let (file_metadata, line_number) = if !struct_span.is_dummy() {
let loc = cx.lookup_debug_loc(struct_span.lo());
(file_metadata(cx, &loc.file), loc.line)
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
};
type_map::build_type_with_children( type_map::build_type_with_children(
cx, cx,
type_map::stub( type_map::stub(
@ -1066,8 +1054,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&compute_debuginfo_type_name(cx.tcx, struct_type, false), &compute_debuginfo_type_name(cx.tcx, struct_type, false),
file_metadata, Some(adt_def.did()),
line_number,
size_and_align_of(struct_type_and_layout), size_and_align_of(struct_type_and_layout),
Some(containing_scope), Some(containing_scope),
visibility_di_flags(cx, adt_def.did(), adt_def.did()), visibility_di_flags(cx, adt_def.did(), adt_def.did()),
@ -1171,8 +1158,7 @@ fn build_tuple_type_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&type_name, &type_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
size_and_align_of(tuple_type_and_layout), size_and_align_of(tuple_type_and_layout),
NO_SCOPE_METADATA, NO_SCOPE_METADATA,
DIFlags::FlagZero, DIFlags::FlagZero,
@ -1212,14 +1198,6 @@ fn build_closure_env_di_node<'ll, 'tcx>(
let containing_scope = get_namespace_for_item(cx, def_id); let containing_scope = get_namespace_for_item(cx, def_id);
let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false); let type_name = compute_debuginfo_type_name(cx.tcx, closure_env_type, false);
let closure_span = cx.tcx.def_span(def_id);
let (file_metadata, line_number) = if !closure_span.is_dummy() {
let loc = cx.lookup_debug_loc(closure_span.lo());
(file_metadata(cx, &loc.file), loc.line)
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
};
type_map::build_type_with_children( type_map::build_type_with_children(
cx, cx,
type_map::stub( type_map::stub(
@ -1227,8 +1205,7 @@ fn build_closure_env_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&type_name, &type_name,
file_metadata, Some(def_id),
line_number,
cx.size_and_align_of(closure_env_type), cx.size_and_align_of(closure_env_type),
Some(containing_scope), Some(containing_scope),
DIFlags::FlagZero, DIFlags::FlagZero,
@ -1260,8 +1237,7 @@ fn build_union_type_di_node<'ll, 'tcx>(
Stub::Union, Stub::Union,
unique_type_id, unique_type_id,
&type_name, &type_name,
unknown_file_metadata(cx), Some(union_def_id),
UNKNOWN_LINE_NUMBER,
size_and_align_of(union_ty_and_layout), size_and_align_of(union_ty_and_layout),
Some(containing_scope), Some(containing_scope),
DIFlags::FlagZero, DIFlags::FlagZero,
@ -1454,8 +1430,7 @@ fn build_vtable_type_di_node<'ll, 'tcx>(
Stub::VTableTy { vtable_holder }, Stub::VTableTy { vtable_holder },
unique_type_id, unique_type_id,
&vtable_type_name, &vtable_type_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
(size, pointer_align), (size, pointer_align),
NO_SCOPE_METADATA, NO_SCOPE_METADATA,
DIFlags::FlagArtificial, DIFlags::FlagArtificial,

View file

@ -199,8 +199,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
type_map::Stub::Union, type_map::Stub::Union,
unique_type_id, unique_type_id,
&enum_type_name, &enum_type_name,
unknown_file_metadata(cx), Some(enum_adt_def.did()),
UNKNOWN_LINE_NUMBER,
cx.size_and_align_of(enum_type), cx.size_and_align_of(enum_type),
NO_SCOPE_METADATA, NO_SCOPE_METADATA,
visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did()), visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did()),
@ -276,8 +275,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
type_map::Stub::Union, type_map::Stub::Union,
unique_type_id, unique_type_id,
&coroutine_type_name, &coroutine_type_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
size_and_align_of(coroutine_type_and_layout), size_and_align_of(coroutine_type_and_layout),
NO_SCOPE_METADATA, NO_SCOPE_METADATA,
DIFlags::FlagZero, DIFlags::FlagZero,
@ -332,6 +330,7 @@ fn build_single_variant_union_fields<'ll, 'tcx>(
variant_index, variant_index,
Cow::from(enum_adt_def.variant(variant_index).name.as_str()), Cow::from(enum_adt_def.variant(variant_index).name.as_str()),
)), )),
enum_adt_def.did(),
); );
let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node( let variant_struct_type_wrapper_di_node = build_variant_struct_wrapper_type_di_node(
@ -394,6 +393,7 @@ fn build_union_fields_for_enum<'ll, 'tcx>(
let variant_name = Cow::from(enum_adt_def.variant(variant_index).name.as_str()); let variant_name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
(variant_index, variant_name) (variant_index, variant_name)
}), }),
enum_adt_def.did(),
); );
let visibility_flags = visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did()); let visibility_flags = visibility_di_flags(cx, enum_adt_def.did(), enum_adt_def.did());
@ -451,6 +451,7 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
cx: &CodegenCx<'ll, 'tcx>, cx: &CodegenCx<'ll, 'tcx>,
containing_scope: &'ll DIType, containing_scope: &'ll DIType,
variants: impl Iterator<Item = (VariantIdx, Cow<'tcx, str>)>, variants: impl Iterator<Item = (VariantIdx, Cow<'tcx, str>)>,
enum_def_id: rustc_span::def_id::DefId,
) -> &'ll DIType { ) -> &'ll DIType {
// Create an enumerator for each variant. // Create an enumerator for each variant.
super::build_enumeration_type_di_node( super::build_enumeration_type_di_node(
@ -458,6 +459,7 @@ fn build_variant_names_type_di_node<'ll, 'tcx>(
"VariantNames", "VariantNames",
variant_names_enum_base_type(cx), variant_names_enum_base_type(cx),
variants.map(|(variant_index, variant_name)| (variant_name, variant_index.as_u32().into())), variants.map(|(variant_index, variant_name)| (variant_name, variant_index.as_u32().into())),
enum_def_id,
containing_scope, containing_scope,
) )
} }
@ -485,8 +487,7 @@ fn build_variant_struct_wrapper_type_di_node<'ll, 'tcx>(
variant_index, variant_index,
), ),
&variant_struct_wrapper_type_name(variant_index), &variant_struct_wrapper_type_name(variant_index),
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
// NOTE: We use size and align of enum_type, not from variant_layout: // NOTE: We use size and align of enum_type, not from variant_layout:
size_and_align_of(enum_or_coroutine_type_and_layout), size_and_align_of(enum_or_coroutine_type_and_layout),
Some(enum_or_coroutine_type_di_node), Some(enum_or_coroutine_type_di_node),
@ -690,6 +691,7 @@ fn build_union_fields_for_direct_tag_coroutine<'ll, 'tcx>(
variant_range variant_range
.clone() .clone()
.map(|variant_index| (variant_index, CoroutineArgs::variant_name(variant_index))), .map(|variant_index| (variant_index, CoroutineArgs::variant_name(variant_index))),
coroutine_def_id,
); );
let discriminants: IndexVec<VariantIdx, DiscrResult> = { let discriminants: IndexVec<VariantIdx, DiscrResult> = {

View file

@ -16,8 +16,8 @@ use super::{SmallVec, size_and_align_of};
use crate::common::{AsCCharPtr, CodegenCx}; use crate::common::{AsCCharPtr, CodegenCx};
use crate::debuginfo::metadata::type_map::{self, Stub}; use crate::debuginfo::metadata::type_map::{self, Stub};
use crate::debuginfo::metadata::{ use crate::debuginfo::metadata::{
UNKNOWN_LINE_NUMBER, build_field_di_node, build_generic_type_param_di_nodes, type_di_node, UNKNOWN_LINE_NUMBER, build_field_di_node, build_generic_type_param_di_nodes,
unknown_file_metadata, file_metadata_from_def_id, type_di_node, unknown_file_metadata,
}; };
use crate::debuginfo::utils::{DIB, create_DIArray, get_namespace_for_item}; use crate::debuginfo::utils::{DIB, create_DIArray, get_namespace_for_item};
use crate::llvm::debuginfo::{DIFlags, DIType}; use crate::llvm::debuginfo::{DIFlags, DIType};
@ -77,6 +77,7 @@ fn build_c_style_enum_di_node<'ll, 'tcx>(
let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str()); let name = Cow::from(enum_adt_def.variant(variant_index).name.as_str());
(name, discr.val) (name, discr.val)
}), }),
enum_adt_def.did(),
containing_scope, containing_scope,
), ),
already_stored_in_typemap: false, already_stored_in_typemap: false,
@ -92,6 +93,7 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
type_name: &str, type_name: &str,
base_type: Ty<'tcx>, base_type: Ty<'tcx>,
enumerators: impl Iterator<Item = (Cow<'tcx, str>, u128)>, enumerators: impl Iterator<Item = (Cow<'tcx, str>, u128)>,
def_id: rustc_span::def_id::DefId,
containing_scope: &'ll DIType, containing_scope: &'ll DIType,
) -> &'ll DIType { ) -> &'ll DIType {
let is_unsigned = match base_type.kind() { let is_unsigned = match base_type.kind() {
@ -115,14 +117,16 @@ fn build_enumeration_type_di_node<'ll, 'tcx>(
}) })
.collect(); .collect();
let (file_metadata, line_number) = file_metadata_from_def_id(cx, Some(def_id));
unsafe { unsafe {
llvm::LLVMRustDIBuilderCreateEnumerationType( llvm::LLVMRustDIBuilderCreateEnumerationType(
DIB(cx), DIB(cx),
containing_scope, containing_scope,
type_name.as_c_char_ptr(), type_name.as_c_char_ptr(),
type_name.len(), type_name.len(),
unknown_file_metadata(cx), file_metadata,
UNKNOWN_LINE_NUMBER, line_number,
size.bits(), size.bits(),
align.bits() as u32, align.bits() as u32,
create_DIArray(DIB(cx), &enumerator_di_nodes[..]), create_DIArray(DIB(cx), &enumerator_di_nodes[..]),
@ -204,8 +208,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
variant_index, variant_index,
), ),
variant_def.name.as_str(), variant_def.name.as_str(),
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
// NOTE: We use size and align of enum_type, not from variant_layout: // NOTE: We use size and align of enum_type, not from variant_layout:
size_and_align_of(enum_type_and_layout), size_and_align_of(enum_type_and_layout),
Some(enum_type_di_node), Some(enum_type_di_node),
@ -288,8 +291,7 @@ fn build_coroutine_variant_struct_type_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&variant_name, &variant_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
size_and_align_of(coroutine_type_and_layout), size_and_align_of(coroutine_type_and_layout),
Some(coroutine_type_di_node), Some(coroutine_type_di_node),
DIFlags::FlagZero, DIFlags::FlagZero,
@ -355,11 +357,7 @@ enum DiscrResult {
impl DiscrResult { impl DiscrResult {
fn opt_single_val(&self) -> Option<u128> { fn opt_single_val(&self) -> Option<u128> {
if let Self::Value(d) = *self { if let Self::Value(d) = *self { Some(d) } else { None }
Some(d)
} else {
None
}
} }
} }

View file

@ -62,8 +62,7 @@ pub(super) fn build_enum_type_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&enum_type_name, &enum_type_name,
unknown_file_metadata(cx), Some(enum_adt_def.did()),
UNKNOWN_LINE_NUMBER,
size_and_align_of(enum_type_and_layout), size_and_align_of(enum_type_and_layout),
Some(containing_scope), Some(containing_scope),
visibility_flags, visibility_flags,
@ -143,8 +142,7 @@ pub(super) fn build_coroutine_di_node<'ll, 'tcx>(
Stub::Struct, Stub::Struct,
unique_type_id, unique_type_id,
&coroutine_type_name, &coroutine_type_name,
unknown_file_metadata(cx), None,
UNKNOWN_LINE_NUMBER,
size_and_align_of(coroutine_type_and_layout), size_and_align_of(coroutine_type_and_layout),
Some(containing_scope), Some(containing_scope),
DIFlags::FlagZero, DIFlags::FlagZero,

View file

@ -8,10 +8,10 @@ use rustc_macros::HashStable;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::ty::{ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt}; use rustc_middle::ty::{ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
use super::{SmallVec, UNKNOWN_LINE_NUMBER, unknown_file_metadata}; use super::{SmallVec, UNKNOWN_LINE_NUMBER, file_metadata, unknown_file_metadata};
use crate::common::{AsCCharPtr, CodegenCx}; use crate::common::{AsCCharPtr, CodegenCx};
use crate::debuginfo::utils::{DIB, create_DIArray, debug_context}; use crate::debuginfo::utils::{DIB, create_DIArray, debug_context};
use crate::llvm::debuginfo::{DIFile, DIFlags, DIScope, DIType}; use crate::llvm::debuginfo::{DIFlags, DIScope, DIType};
use crate::llvm::{self}; use crate::llvm::{self};
mod private { mod private {
@ -174,8 +174,7 @@ pub(super) fn stub<'ll, 'tcx>(
kind: Stub<'ll>, kind: Stub<'ll>,
unique_type_id: UniqueTypeId<'tcx>, unique_type_id: UniqueTypeId<'tcx>,
name: &str, name: &str,
file_metadata: &'ll DIFile, def_id: Option<rustc_span::def_id::DefId>,
line_number: u32,
(size, align): (Size, Align), (size, align): (Size, Align),
containing_scope: Option<&'ll DIScope>, containing_scope: Option<&'ll DIScope>,
flags: DIFlags, flags: DIFlags,
@ -183,6 +182,18 @@ pub(super) fn stub<'ll, 'tcx>(
let empty_array = create_DIArray(DIB(cx), &[]); let empty_array = create_DIArray(DIB(cx), &[]);
let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx); let unique_type_id_str = unique_type_id.generate_unique_id_string(cx.tcx);
let (file_metadata, line_number) = if let Some(def_id) = def_id {
let span = cx.tcx.def_span(def_id);
if !span.is_dummy() {
let loc = cx.lookup_debug_loc(span.lo());
(file_metadata(cx, &loc.file), loc.line)
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
}
} else {
(unknown_file_metadata(cx), UNKNOWN_LINE_NUMBER)
};
let metadata = match kind { let metadata = match kind {
Stub::Struct | Stub::VTableTy { .. } => { Stub::Struct | Stub::VTableTy { .. } => {
let vtable_holder = match kind { let vtable_holder = match kind {

View file

@ -3,12 +3,31 @@
// compile-flags: -C debuginfo=2 // compile-flags: -C debuginfo=2
#![crate_type = "lib"] #![crate_type = "lib"]
// CHECK: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}src/test/codegen/issue-98678.rs{{".*}}) // The use of CHECK-DAG here is because the C++-like enum is emitted before the `DIFile` node
// CHECK-DAG: ![[#FILE:]] = !DIFile({{.*}}filename:{{.*}}src/test/codegen/issue-98678.rs{{".*}})
// CHECK-DAG: !DICompositeType({{.*"}}MyCppLikeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 2]],
#[repr(C)]
pub enum MyCppLikeEnum {
One,
}
// CHECK: !DICompositeType({{.*"}}MyType{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], // CHECK: !DICompositeType({{.*"}}MyType{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
pub struct MyType; pub struct MyType;
pub fn foo(_: MyType) { // CHECK: !DICompositeType({{.*"}}MyUnion{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
pub union MyUnion {
i: i32, // TODO fields are still wrong
f: f32,
}
// CHECK: !DICompositeType({{.*"}}MyNativeEnum{{".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
pub enum MyNativeEnum {
One,
}
pub fn foo(_: MyType, _: MyUnion, _: MyNativeEnum, _: MyCppLikeEnum) {
// CHECK: !DICompositeType({{.*"[{]}}closure_env#0{{[}]".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]], // CHECK: !DICompositeType({{.*"[{]}}closure_env#0{{[}]".*}}file: ![[#FILE]]{{.*}}line: [[# @LINE + 1]],
let closure = |x| x; let closure = |x| x;
closure(0); closure(0);