Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5f46729

Browse files
committed
Introduce DIBuilderBox, an owning pointer to DIBuilder
1 parent 5a74870 commit 5f46729

File tree

5 files changed

+41
-27
lines changed

5 files changed

+41
-27
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
945945

946946
unsafe {
947947
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
948-
debug_context.builder,
948+
debug_context.builder.as_ref(),
949949
name_in_debuginfo.as_c_char_ptr(),
950950
name_in_debuginfo.len(),
951951
work_dir.as_c_char_ptr(),
@@ -958,7 +958,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
958958
);
959959

960960
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
961-
debug_context.builder,
961+
debug_context.builder.as_ref(),
962962
DW_LANG_RUST,
963963
compile_unit_file,
964964
producer.as_c_char_ptr(),

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::builder::Builder;
3434
use crate::common::{AsCCharPtr, CodegenCx};
3535
use crate::llvm;
3636
use crate::llvm::debuginfo::{
37-
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
37+
DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
3838
DIVariable,
3939
};
4040
use crate::value::Value;
@@ -56,26 +56,18 @@ const DW_TAG_arg_variable: c_uint = 0x101;
5656
/// A context object for maintaining all state needed by the debuginfo module.
5757
pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> {
5858
llmod: &'ll llvm::Module,
59-
builder: &'ll mut DIBuilder<'ll>,
59+
builder: DIBuilderBox<'ll>,
6060
created_files: RefCell<UnordMap<Option<(StableSourceFileId, SourceFileHash)>, &'ll DIFile>>,
6161

6262
type_map: metadata::TypeMap<'ll, 'tcx>,
6363
namespace_map: RefCell<DefIdMap<&'ll DIScope>>,
6464
recursion_marker_type: OnceCell<&'ll DIType>,
6565
}
6666

67-
impl Drop for CodegenUnitDebugContext<'_, '_> {
68-
fn drop(&mut self) {
69-
unsafe {
70-
llvm::LLVMRustDIBuilderDispose(&mut *(self.builder as *mut _));
71-
}
72-
}
73-
}
74-
7567
impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
7668
pub(crate) fn new(llmod: &'ll llvm::Module) -> Self {
7769
debug!("CodegenUnitDebugContext::new");
78-
let builder = unsafe { llvm::LLVMRustDIBuilderCreate(llmod) };
70+
let builder = DIBuilderBox::new(llmod);
7971
// DIBuilder inherits context from the module, so we'd better use the same one
8072
CodegenUnitDebugContext {
8173
llmod,
@@ -88,7 +80,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
8880
}
8981

9082
pub(crate) fn finalize(&self, sess: &Session) {
91-
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder) };
83+
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder.as_ref()) };
9284
if !sess.target.is_like_msvc {
9385
// Debuginfo generation in LLVM by default uses a higher
9486
// version of dwarf than macOS currently understands. We can

compiler/rustc_codegen_llvm/src/debuginfo/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) fn debug_context<'a, 'll, 'tcx>(
4141
#[inline]
4242
#[allow(non_snake_case)]
4343
pub(crate) fn DIB<'a, 'll>(cx: &'a CodegenCx<'ll, '_>) -> &'a DIBuilder<'ll> {
44-
cx.dbg_cx.as_ref().unwrap().builder
44+
cx.dbg_cx.as_ref().unwrap().builder.as_ref()
4545
}
4646

4747
pub(crate) fn get_namespace_for_item<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll DIScope {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,13 +723,40 @@ pub type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void
723723
pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
724724

725725
pub mod debuginfo {
726+
use std::ptr;
727+
726728
use bitflags::bitflags;
727729

728730
use super::{InvariantOpaque, Metadata};
731+
use crate::llvm::{self, Module};
729732

730733
#[repr(C)]
731734
pub struct DIBuilder<'a>(InvariantOpaque<'a>);
732735

736+
pub(crate) struct DIBuilderBox<'ll> {
737+
raw: ptr::NonNull<DIBuilder<'ll>>,
738+
}
739+
740+
impl<'ll> DIBuilderBox<'ll> {
741+
pub(crate) fn new(llmod: &'ll Module) -> Self {
742+
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
743+
let raw = ptr::NonNull::new(raw).unwrap();
744+
Self { raw }
745+
}
746+
747+
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
748+
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
749+
// for as long as `&self` is.
750+
unsafe { self.raw.as_ref() }
751+
}
752+
}
753+
754+
impl<'ll> Drop for DIBuilderBox<'ll> {
755+
fn drop(&mut self) {
756+
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
757+
}
758+
}
759+
733760
pub type DIDescriptor = Metadata;
734761
pub type DILocation = Metadata;
735762
pub type DIScope = DIDescriptor;
@@ -1597,6 +1624,13 @@ unsafe extern "C" {
15971624
) -> &'a Value;
15981625
}
15991626

1627+
// FFI bindings for `DIBuilder` functions in the LLVM-C API.
1628+
// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
1629+
unsafe extern "C" {
1630+
pub(crate) fn LLVMCreateDIBuilder<'ll>(M: &'ll Module) -> *mut DIBuilder<'ll>;
1631+
pub(crate) fn LLVMDisposeDIBuilder<'ll>(Builder: ptr::NonNull<DIBuilder<'ll>>);
1632+
}
1633+
16001634
#[link(name = "llvm-wrapper", kind = "static")]
16011635
unsafe extern "C" {
16021636
pub fn LLVMRustInstallErrorHandlers();
@@ -1864,10 +1898,6 @@ unsafe extern "C" {
18641898
ValueLen: size_t,
18651899
);
18661900

1867-
pub fn LLVMRustDIBuilderCreate(M: &Module) -> &mut DIBuilder<'_>;
1868-
1869-
pub fn LLVMRustDIBuilderDispose<'a>(Builder: &'a mut DIBuilder<'a>);
1870-
18711901
pub fn LLVMRustDIBuilderFinalize(Builder: &DIBuilder<'_>);
18721902

18731903
pub fn LLVMRustDIBuilderCreateCompileUnit<'a>(

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -909,14 +909,6 @@ extern "C" void LLVMRustGlobalAddMetadata(LLVMValueRef Global, unsigned Kind,
909909
unwrap<GlobalObject>(Global)->addMetadata(Kind, *unwrap<MDNode>(MD));
910910
}
911911

912-
extern "C" LLVMRustDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
913-
return new DIBuilder(*unwrap(M));
914-
}
915-
916-
extern "C" void LLVMRustDIBuilderDispose(LLVMRustDIBuilderRef Builder) {
917-
delete Builder;
918-
}
919-
920912
extern "C" void LLVMRustDIBuilderFinalize(LLVMRustDIBuilderRef Builder) {
921913
Builder->finalize();
922914
}

0 commit comments

Comments
 (0)