Skip to content

Commit 4f355c9

Browse files
committed
Move DIBuilderBox out of ffi.rs
1 parent 16c6fbc commit 4f355c9

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

700700
pub(crate) mod debuginfo {
701-
use std::ptr;
702-
703701
use bitflags::bitflags;
704702

705703
use super::{InvariantOpaque, Metadata};
706-
use crate::llvm::{self, Module};
707704

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

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

0 commit comments

Comments
 (0)