Skip to content

Commit d0e8be3

Browse files
committed
Introduce DIBuilderBox, an owning pointer to DIBuilder
1 parent ff1c33d commit d0e8be3

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
@@ -944,7 +944,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
944944

945945
unsafe {
946946
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
947-
debug_context.builder,
947+
debug_context.builder.as_ref(),
948948
name_in_debuginfo.as_c_char_ptr(),
949949
name_in_debuginfo.len(),
950950
work_dir.as_c_char_ptr(),
@@ -957,7 +957,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
957957
);
958958

959959
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(
960-
debug_context.builder,
960+
debug_context.builder.as_ref(),
961961
DW_LANG_RUST,
962962
compile_unit_file,
963963
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
@@ -33,7 +33,7 @@ use crate::builder::Builder;
3333
use crate::common::{AsCCharPtr, CodegenCx};
3434
use crate::llvm;
3535
use crate::llvm::debuginfo::{
36-
DIArray, DIBuilder, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
36+
DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope, DIType,
3737
DIVariable,
3838
};
3939
use crate::value::Value;
@@ -55,26 +55,18 @@ const DW_TAG_arg_variable: c_uint = 0x101;
5555
/// A context object for maintaining all state needed by the debuginfo module.
5656
pub(crate) struct CodegenUnitDebugContext<'ll, 'tcx> {
5757
llmod: &'ll llvm::Module,
58-
builder: &'ll mut DIBuilder<'ll>,
58+
builder: DIBuilderBox<'ll>,
5959
created_files: RefCell<UnordMap<Option<(StableSourceFileId, SourceFileHash)>, &'ll DIFile>>,
6060

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

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

8981
pub(crate) fn finalize(&self, sess: &Session) {
90-
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder) };
82+
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder.as_ref()) };
9183
if !sess.target.is_like_msvc {
9284
// Debuginfo generation in LLVM by default uses a higher
9385
// 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
@@ -715,13 +715,40 @@ pub type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void
715715
pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn(&SMDiagnostic, *const c_void, c_uint);
716716

717717
pub mod debuginfo {
718+
use std::ptr;
719+
718720
use bitflags::bitflags;
719721

720722
use super::{InvariantOpaque, Metadata};
723+
use crate::llvm::{self, Module};
721724

722725
#[repr(C)]
723726
pub struct DIBuilder<'a>(InvariantOpaque<'a>);
724727

728+
pub(crate) struct DIBuilderBox<'ll> {
729+
raw: ptr::NonNull<DIBuilder<'ll>>,
730+
}
731+
732+
impl<'ll> DIBuilderBox<'ll> {
733+
pub(crate) fn new(llmod: &'ll Module) -> Self {
734+
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
735+
let raw = ptr::NonNull::new(raw).unwrap();
736+
Self { raw }
737+
}
738+
739+
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
740+
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
741+
// for as long as `&self` is.
742+
unsafe { self.raw.as_ref() }
743+
}
744+
}
745+
746+
impl<'ll> Drop for DIBuilderBox<'ll> {
747+
fn drop(&mut self) {
748+
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
749+
}
750+
}
751+
725752
pub type DIDescriptor = Metadata;
726753
pub type DILocation = Metadata;
727754
pub type DIScope = DIDescriptor;
@@ -1589,6 +1616,13 @@ unsafe extern "C" {
15891616
) -> &'a Value;
15901617
}
15911618

1619+
// FFI bindings for `DIBuilder` functions in the LLVM-C API.
1620+
// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
1621+
unsafe extern "C" {
1622+
pub(crate) fn LLVMCreateDIBuilder<'ll>(M: &'ll Module) -> *mut DIBuilder<'ll>;
1623+
pub(crate) fn LLVMDisposeDIBuilder<'ll>(Builder: ptr::NonNull<DIBuilder<'ll>>);
1624+
}
1625+
15921626
#[link(name = "llvm-wrapper", kind = "static")]
15931627
unsafe extern "C" {
15941628
pub fn LLVMRustInstallErrorHandlers();
@@ -1856,10 +1890,6 @@ unsafe extern "C" {
18561890
ValueLen: size_t,
18571891
);
18581892

1859-
pub fn LLVMRustDIBuilderCreate(M: &Module) -> &mut DIBuilder<'_>;
1860-
1861-
pub fn LLVMRustDIBuilderDispose<'a>(Builder: &'a mut DIBuilder<'a>);
1862-
18631893
pub fn LLVMRustDIBuilderFinalize(Builder: &DIBuilder<'_>);
18641894

18651895
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
@@ -986,14 +986,6 @@ extern "C" void LLVMRustGlobalAddMetadata(LLVMValueRef Global, unsigned Kind,
986986
unwrap<GlobalObject>(Global)->addMetadata(Kind, *unwrap<MDNode>(MD));
987987
}
988988

989-
extern "C" LLVMRustDIBuilderRef LLVMRustDIBuilderCreate(LLVMModuleRef M) {
990-
return new DIBuilder(*unwrap(M));
991-
}
992-
993-
extern "C" void LLVMRustDIBuilderDispose(LLVMRustDIBuilderRef Builder) {
994-
delete Builder;
995-
}
996-
997989
extern "C" void LLVMRustDIBuilderFinalize(LLVMRustDIBuilderRef Builder) {
998990
Builder->finalize();
999991
}

0 commit comments

Comments
 (0)