Skip to content

Commit cc41dd4

Browse files
committed
Create a safe wrapper function around LLVMRustDIBuilderCreateFile
1 parent e19e4e3 commit cc41dd4

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+26-33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::borrow::Cow;
22
use std::fmt::{self, Write};
33
use std::hash::{Hash, Hasher};
44
use std::path::{Path, PathBuf};
5+
use std::sync::Arc;
56
use std::{iter, ptr};
67

78
use libc::{c_char, c_longlong, c_uint};
@@ -38,7 +39,7 @@ use crate::debuginfo::metadata::type_map::build_type_with_children;
3839
use crate::debuginfo::utils::{WidePtrKind, wide_pointer_kind};
3940
use crate::llvm;
4041
use crate::llvm::debuginfo::{
41-
DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType,
42+
DIBuilder, DICompositeType, DIDescriptor, DIFile, DIFlags, DILexicalBlock, DIScope, DIType,
4243
DebugEmissionKind, DebugNameTableKind,
4344
};
4445
use crate::value::Value;
@@ -623,42 +624,38 @@ pub(crate) fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFi
623624
let source =
624625
cx.sess().opts.unstable_opts.embed_source.then_some(()).and(source_file.src.as_ref());
625626

626-
unsafe {
627-
llvm::LLVMRustDIBuilderCreateFile(
628-
DIB(cx),
629-
file_name.as_c_char_ptr(),
630-
file_name.len(),
631-
directory.as_c_char_ptr(),
632-
directory.len(),
633-
hash_kind,
634-
hash_value.as_c_char_ptr(),
635-
hash_value.len(),
636-
source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
637-
source.map_or(0, |x| x.len()),
638-
)
639-
}
627+
create_file(DIB(cx), &file_name, &directory, &hash_value, hash_kind, source)
640628
}
641629
}
642630

643631
fn unknown_file_metadata<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll DIFile {
644-
debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| unsafe {
645-
let file_name = "<unknown>";
646-
let directory = "";
647-
let hash_value = "";
632+
debug_context(cx).created_files.borrow_mut().entry(None).or_insert_with(|| {
633+
create_file(DIB(cx), "<unknown>", "", "", llvm::ChecksumKind::None, None)
634+
})
635+
}
648636

637+
fn create_file<'ll>(
638+
builder: &DIBuilder<'ll>,
639+
file_name: &str,
640+
directory: &str,
641+
hash_value: &str,
642+
hash_kind: llvm::ChecksumKind,
643+
source: Option<&Arc<String>>,
644+
) -> &'ll DIFile {
645+
unsafe {
649646
llvm::LLVMRustDIBuilderCreateFile(
650-
DIB(cx),
647+
builder,
651648
file_name.as_c_char_ptr(),
652649
file_name.len(),
653650
directory.as_c_char_ptr(),
654651
directory.len(),
655-
llvm::ChecksumKind::None,
652+
hash_kind,
656653
hash_value.as_c_char_ptr(),
657654
hash_value.len(),
658-
ptr::null(),
659-
0,
655+
source.map_or(ptr::null(), |x| x.as_c_char_ptr()),
656+
source.map_or(0, |x| x.len()),
660657
)
661-
})
658+
}
662659
}
663660

664661
trait MsvcBasicName {
@@ -932,17 +929,13 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
932929
};
933930

934931
unsafe {
935-
let compile_unit_file = llvm::LLVMRustDIBuilderCreateFile(
932+
let compile_unit_file = create_file(
936933
debug_context.builder.as_ref(),
937-
name_in_debuginfo.as_c_char_ptr(),
938-
name_in_debuginfo.len(),
939-
work_dir.as_c_char_ptr(),
940-
work_dir.len(),
934+
&name_in_debuginfo,
935+
&work_dir,
936+
"",
941937
llvm::ChecksumKind::None,
942-
ptr::null(),
943-
0,
944-
ptr::null(),
945-
0,
938+
None,
946939
);
947940

948941
let unit_metadata = llvm::LLVMRustDIBuilderCreateCompileUnit(

0 commit comments

Comments
 (0)