Skip to content

Commit 8551ecd

Browse files
committed
Move DIBuilderBox out of ffi.rs
1 parent 598f179 commit 8551ecd

File tree

3 files changed

+34
-31
lines changed

3 files changed

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

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_target::spec::DebuginfoKind;
2828
use smallvec::SmallVec;
2929
use tracing::debug;
3030

31+
use self::di_builder::DIBuilderBox;
3132
use self::metadata::{
3233
UNKNOWN_COLUMN_NUMBER, UNKNOWN_LINE_NUMBER, file_metadata, spanned_type_di_node, type_di_node,
3334
};
@@ -37,12 +38,13 @@ use crate::builder::Builder;
3738
use crate::common::{AsCCharPtr, CodegenCx};
3839
use crate::llvm;
3940
use crate::llvm::debuginfo::{
40-
DIArray, DIBuilderBox, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
41+
DIArray, DIFile, DIFlags, DILexicalBlock, DILocation, DISPFlags, DIScope,
4142
DITemplateTypeParameter, DIType, DIVariable,
4243
};
4344
use crate::value::Value;
4445

4546
mod create_scope_map;
47+
mod di_builder;
4648
mod dwarf_const;
4749
mod gdb;
4850
pub(crate) mod metadata;

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)