Skip to content

Commit 018032c

Browse files
committed
Create a safe wrapper around LLVMRustDIBuilderCreateBasicType
1 parent cc41dd4 commit 018032c

File tree

1 file changed

+36
-31
lines changed

1 file changed

+36
-31
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+36-31
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use crate::debuginfo::metadata::type_map::build_type_with_children;
3939
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
4040
use crate::llvm;
4141
use crate::llvm::debuginfo::{
42-
DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType,
43-
DebugEmissionKind, DebugNameTableKind,
42+
DIBasicType, DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock,
43+
DIScope, DIType, DebugEmissionKind, DebugNameTableKind,
4444
};
4545
use crate::value::Value;
4646

@@ -491,26 +491,22 @@ pub(crate) fn type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) ->
491491
// FIXME(mw): Cache this via a regular UniqueTypeId instead of an extra field in the debug context.
492492
fn recursion_marker_type_di_node<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>) -> &'ll DIType {
493493
*debug_context(cx).recursion_marker_type.get_or_init(move || {
494-
unsafe {
495-
// The choice of type here is pretty arbitrary -
496-
// anything reading the debuginfo for a recursive
497-
// type is going to see *something* weird - the only
498-
// question is what exactly it will see.
499-
//
500-
// FIXME: the name `<recur_type>` does not fit the naming scheme
501-
// of other types.
502-
//
503-
// FIXME: it might make sense to use an actual pointer type here
504-
// so that debuggers can show the address.
505-
let name = "<recur_type>";
506-
llvm::LLVMRustDIBuilderCreateBasicType(
507-
DIB(cx),
508-
name.as_c_char_ptr(),
509-
name.len(),
510-
cx.tcx.data_layout.pointer_size.bits(),
511-
dwarf_const::DW_ATE_unsigned,
512-
)
513-
}
494+
// The choice of type here is pretty arbitrary -
495+
// anything reading the debuginfo for a recursive
496+
// type is going to see *something* weird - the only
497+
// question is what exactly it will see.
498+
//
499+
// FIXME: the name `<recur_type>` does not fit the naming scheme
500+
// of other types.
501+
//
502+
// FIXME: it might make sense to use an actual pointer type here
503+
// so that debuggers can show the address.
504+
create_basic_type(
505+
cx,
506+
"<recur_type>",
507+
cx.tcx.data_layout.pointer_size,
508+
dwarf_const::DW_ATE_unsigned,
509+
)
514510
})
515511
}
516512

@@ -788,15 +784,7 @@ fn build_basic_type_di_node<'ll, 'tcx>(
788784
_ => bug!("debuginfo::build_basic_type_di_node - `t` is invalid type"),
789785
};
790786

791-
let ty_di_node = unsafe {
792-
llvm::LLVMRustDIBuilderCreateBasicType(
793-
DIB(cx),
794-
name.as_c_char_ptr(),
795-
name.len(),
796-
cx.size_of(t).bits(),
797-
encoding,
798-
)
799-
};
787+
let ty_di_node = create_basic_type(cx, name, cx.size_of(t), encoding);
800788

801789
if !cpp_like_debuginfo {
802790
return DINodeCreationResult::new(ty_di_node, false);
@@ -824,6 +812,23 @@ fn build_basic_type_di_node<'ll, 'tcx>(
824812
DINodeCreationResult::new(typedef_di_node, false)
825813
}
826814

815+
fn create_basic_type<'ll, 'tcx>(
816+
cx: &CodegenCx<'ll, 'tcx>,
817+
name: &str,
818+
size: Size,
819+
encoding: u32,
820+
) -> &'ll DIBasicType {
821+
unsafe {
822+
llvm::LLVMRustDIBuilderCreateBasicType(
823+
DIB(cx),
824+
name.as_c_char_ptr(),
825+
name.len(),
826+
size.bits(),
827+
encoding,
828+
)
829+
}
830+
}
831+
827832
fn build_foreign_type_di_node<'ll, 'tcx>(
828833
cx: &CodegenCx<'ll, 'tcx>,
829834
t: Ty<'tcx>,

0 commit comments

Comments
 (0)