Skip to content

Commit 748621e

Browse files
committed
Move DIBuilderBox out of ffi.rs
1 parent dc69a4a commit 748621e

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
@@ -699,12 +699,9 @@ unsafe extern "C" {
699699
pub(crate) type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void);
700700

701701
pub(crate) mod debuginfo {
702-
use std::ptr;
703-
704702
use bitflags::bitflags;
705703

706704
use super::{InvariantOpaque, Metadata};
707-
use crate::llvm::{self, Module};
708705

709706
/// Opaque target type for references to an LLVM debuginfo builder.
710707
///
@@ -717,33 +714,6 @@ pub(crate) mod debuginfo {
717714
#[repr(C)]
718715
pub(crate) struct DIBuilder<'ll>(InvariantOpaque<'ll>);
719716

720-
/// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
721-
/// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
722-
/// needed for debuginfo FFI calls.
723-
pub(crate) struct DIBuilderBox<'ll> {
724-
raw: ptr::NonNull<DIBuilder<'ll>>,
725-
}
726-
727-
impl<'ll> DIBuilderBox<'ll> {
728-
pub(crate) fn new(llmod: &'ll Module) -> Self {
729-
let raw = unsafe { llvm::LLVMCreateDIBuilder(llmod) };
730-
let raw = ptr::NonNull::new(raw).unwrap();
731-
Self { raw }
732-
}
733-
734-
pub(crate) fn as_ref(&self) -> &DIBuilder<'ll> {
735-
// SAFETY: This is an owning pointer, so `&DIBuilder` is valid
736-
// for as long as `&self` is.
737-
unsafe { self.raw.as_ref() }
738-
}
739-
}
740-
741-
impl<'ll> Drop for DIBuilderBox<'ll> {
742-
fn drop(&mut self) {
743-
unsafe { llvm::LLVMDisposeDIBuilder(self.raw) };
744-
}
745-
}
746-
747717
pub(crate) type DIDescriptor = Metadata;
748718
pub(crate) type DILocation = Metadata;
749719
pub(crate) type DIScope = DIDescriptor;

0 commit comments

Comments
 (0)