Skip to content

Commit 735a980

Browse files
committed
Move DIBuilderBox out of ffi.rs
1 parent c2f8ee9 commit 735a980

File tree

3 files changed

+32
-32
lines changed

3 files changed

+32
-32
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/di_builder.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,37 @@
1+
use std::ptr;
2+
13
use libc::c_uint;
24
use rustc_abi::Align;
35

46
use crate::llvm::debuginfo::DIBuilder;
5-
use crate::llvm::{self, ToLlvmBool};
7+
use crate::llvm::{self, Module, ToLlvmBool};
8+
9+
/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
10+
/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
11+
/// needed for debuginfo FFI calls.
12+
pub(crate) struct DIBuilderBox<'ll> {
13+
raw: ptr::NonNull<DIBuilder<'ll>>,
14+
}
15+
16+
impl<'ll> DIBuilderBox<'ll> {
17+
pub(crate) fn new(llmod: &'ll Module) -> Self {
18+
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
19+
let raw = ptr::NonNull::new(raw).unwrap();
20+
Self { raw }
21+
}
22+
23+
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
24+
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
25+
// for as long as `&self` is.
26+
unsafe { self.raw.as_ref() }
27+
}
28+
}
29+
30+
impl<'ll> Drop for DIBuilderBox<'ll> {
31+
fn drop(&mut self) {
32+
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
33+
}
34+
}
635

736
/// Extension trait for defining safe wrappers and helper methods on
837
/// `&DIBuilder<'ll>`, without requiring it to be defined in the same crate.

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ use self::namespace::mangled_name_of_instance;
3838
use self::utils::{DIB, create_DIArray, is_node_local_to_unit};
3939
use crate::builder::Builder;
4040
use crate::common::{AsCCharPtr, CodegenCx};
41+
use crate::debuginfo::di_builder::DIBuilderBox;
4142
use crate::llvm::debuginfo::{
42-
DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
43+
DIArray, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
4344
DITemplateTypeParameter, DIType, DIVariable,
4445
};
4546
use crate::llvm::{self, Value};

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -711,12 +711,9 @@ unsafe extern "C" {
711711
pub(crate) type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
712712

713713
pub(crate) mod debuginfo {
714-
use std::ptr;
715-
716714
use bitflags::bitflags;
717715

718716
use super::{InvariantOpaque, Metadata};
719-
use crate::llvm::{self, Module};
720717

721718
/// Opaque target type for references to an LLVM debuginfo builder.
722719
///
@@ -729,33 +726,6 @@ pub(crate) mod debuginfo {
729726
#[repr(C)]
730727
pub(crate) struct DIBuilder<'ll>(InvariantOpaque<'ll>);
731728

732-
/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
733-
/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
734-
/// needed for debuginfo FFI calls.
735-
pub(crate) struct DIBuilderBox<'ll> {
736-
raw: ptr::NonNull<DIBuilder<'ll>>,
737-
}
738-
739-
impl<'ll> DIBuilderBox<'ll> {
740-
pub(crate) fn new(llmod: &'ll Module) -> Self {
741-
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
742-
let raw = ptr::NonNull::new(raw).unwrap();
743-
Self { raw }
744-
}
745-
746-
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
747-
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
748-
// for as long as `&self` is.
749-
unsafe { self.raw.as_ref() }
750-
}
751-
}
752-
753-
impl<'ll> Drop for DIBuilderBox<'ll> {
754-
fn drop(&mut self) {
755-
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
756-
}
757-
}
758-
759729
pub(crate) type DIDescriptor = Metadata;
760730
pub(crate) type DILocation = Metadata;
761731
pub(crate) type DIScope = DIDescriptor;

0 commit comments

Comments
 (0)