@@ -789,12 +789,50 @@ pub type DiagnosticHandlerTy = unsafe extern "C" fn(&DiagnosticInfo, *mut c_void
789789pub type InlineAsmDiagHandlerTy = unsafe extern "C" fn ( & SMDiagnostic , * const c_void , c_uint ) ;
790790
791791pub mod debuginfo {
792+ use std:: ptr;
793+
792794 use bitflags:: bitflags;
793795
794796 use super :: { InvariantOpaque , Metadata } ;
797+ use crate :: llvm:: { self , Module } ;
795798
799+ /// Opaque target type for references to an LLVM debuginfo builder.
800+ ///
801+ /// `&'_ DIBuilder<'ll>` corresponds to `LLVMDIBuilderRef`, which is the
802+ /// LLVM-C wrapper for `DIBuilder *`.
803+ ///
804+ /// Debuginfo builders are created and destroyed during codegen, so the
805+ /// builder reference typically has a shorter lifetime than the LLVM
806+ /// session (`'ll`) that it participates in.
796807 #[ repr( C ) ]
797- pub struct DIBuilder < ' a > ( InvariantOpaque < ' a > ) ;
808+ pub struct DIBuilder < ' ll > ( InvariantOpaque < ' ll > ) ;
809+
810+ /// Owning pointer to a `DIBuilder<'ll>` that will dispose of the builder
811+ /// when dropped. Use `.as_ref()` to get the underlying `&DIBuilder`
812+ /// needed for debuginfo FFI calls.
813+ pub ( crate ) struct DIBuilderBox < ' ll > {
814+ raw : ptr:: NonNull < DIBuilder < ' ll > > ,
815+ }
816+
817+ impl < ' ll > DIBuilderBox < ' ll > {
818+ pub ( crate ) fn new ( llmod : & ' ll Module ) -> Self {
819+ let raw = unsafe { llvm:: LLVMCreateDIBuilder ( llmod) } ;
820+ let raw = ptr:: NonNull :: new ( raw) . unwrap ( ) ;
821+ Self { raw }
822+ }
823+
824+ pub ( crate ) fn as_ref ( & self ) -> & DIBuilder < ' ll > {
825+ // SAFETY: This is an owning pointer, so `&DIBuilder` is valid
826+ // for as long as `&self` is.
827+ unsafe { self . raw . as_ref ( ) }
828+ }
829+ }
830+
831+ impl < ' ll > Drop for DIBuilderBox < ' ll > {
832+ fn drop ( & mut self ) {
833+ unsafe { llvm:: LLVMDisposeDIBuilder ( self . raw ) } ;
834+ }
835+ }
798836
799837 pub type DIDescriptor = Metadata ;
800838 pub type DILocation = Metadata ;
@@ -1672,6 +1710,13 @@ unsafe extern "C" {
16721710 ) -> & ' a Value ;
16731711}
16741712
1713+ // FFI bindings for `DIBuilder` functions in the LLVM-C API.
1714+ // Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
1715+ unsafe extern "C" {
1716+ pub ( crate ) fn LLVMCreateDIBuilder < ' ll > ( M : & ' ll Module ) -> * mut DIBuilder < ' ll > ;
1717+ pub ( crate ) fn LLVMDisposeDIBuilder < ' ll > ( Builder : ptr:: NonNull < DIBuilder < ' ll > > ) ;
1718+ }
1719+
16751720#[ link( name = "llvm-wrapper" , kind = "static" ) ]
16761721unsafe extern "C" {
16771722 pub fn LLVMRustInstallErrorHandlers ( ) ;
@@ -1939,10 +1984,6 @@ unsafe extern "C" {
19391984 ValueLen : size_t ,
19401985 ) ;
19411986
1942- pub fn LLVMRustDIBuilderCreate ( M : & Module ) -> & mut DIBuilder < ' _ > ;
1943-
1944- pub fn LLVMRustDIBuilderDispose < ' a > ( Builder : & ' a mut DIBuilder < ' a > ) ;
1945-
19461987 pub fn LLVMRustDIBuilderFinalize ( Builder : & DIBuilder < ' _ > ) ;
19471988
19481989 pub fn LLVMRustDIBuilderCreateCompileUnit < ' a > (
0 commit comments