Create a safe wrapper function around LLVMRustDIBuilderCreateFile

This commit is contained in:
Oli Scherer 2025-03-17 16:43:20 +00:00
parent e19e4e3a4b
commit cc41dd4fa1

View file

@ -2,6 +2,7 @@ use std::borrow::Cow;
use std::fmt::{self, Write}; use std::fmt::{self, Write};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::{iter, ptr}; use std::{iter, ptr};
use libc::{c_char, c_longlong, c_uint}; use libc::{c_char, c_longlong, c_uint};
@ -38,7 +39,7 @@ use crate::debuginfo::metadata::type_map::build_type_with_children;
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind}; use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
use crate::llvm; use crate::llvm;
use crate::llvm::debuginfo::{ use crate::llvm::debuginfo::{
DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType, DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType,
DebugEmissionKind, DebugNameTableKind, DebugEmissionKind, DebugNameTableKind,
}; };
use crate::value::Value; use crate::value::Value;
@ -623,9 +624,27 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
let source = let source =
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref()); cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
}
}
fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| {
create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None)
})
}
fn create_file<'ll>(
builder: &DIBuilder<'ll>,
file_name: &str,
directory: &str,
hash_value: &str,
hash_kind: llvm::ChecksumKind,
source: Option<&Arc<String>>,
) -> &'ll DIFile {
unsafe { unsafe {
llvm::LLVMRustDIBuilderCreateFile( llvm::LLVMRustDIBuilderCreateFile(
DIB(cx), builder,
file_name.as_c_char_ptr(), file_name.as_c_char_ptr(),
file_name.len(), file_name.len(),
directory.as_c_char_ptr(), directory.as_c_char_ptr(),
@ -638,28 +657,6 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
) )
} }
} }
}
fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| unsafe {
let file_name = "<unknown>";
let directory = "";
let hash_value = "";
llvm::LLVMRustDIBuilderCreateFile(
DIB(cx),
file_name.as_c_char_ptr(),
file_name.len(),
directory.as_c_char_ptr(),
directory.len(),
llvm::ChecksumKind::None,
hash_value.as_c_char_ptr(),
hash_value.len(),
ptr::null(),
0,
)
})
}
trait MsvcBasicName { trait MsvcBasicName {
fn msvc_basic_name(self) -> &'static str; fn msvc_basic_name(self) -> &'static str;
@ -932,17 +929,13 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
}; };
unsafe { unsafe {
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile( let compile_unit_file = create_file(
debug_context.builder.as_ref(), debug_context.builder.as_ref(),
name_in_debuginfo.as_c_char_ptr(), &name_in_debuginfo,
name_in_debuginfo.len(), &work_dir,
work_dir.as_c_char_ptr(), "",
work_dir.len(),
llvm::ChecksumKind::None, llvm::ChecksumKind::None,
ptr::null(), None,
0,
ptr::null(),
0,
); );
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit( let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(