Skip to content

Commit 0a374b0

Browse files
committed
Add a safe wrapper for WriteBitcodeToFile
1 parent 5be8b78 commit 0a374b0

File tree

1 file changed

+10
-8
lines changed
  • compiler/rustc_codegen_llvm/src/back

1 file changed

+10
-8
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,17 @@ pub(crate) fn save_temp_bitcode(
328328
if !cgcx.save_temps {
329329
return;
330330
}
331+
let ext = format!("{name}.bc");
332+
let cgu = Some(&module.name[..]);
333+
let path = cgcx.output_filenames.temp_path_ext(&ext, cgu);
334+
write_bitcode_to_file(module, &path)
335+
}
336+
337+
fn write_bitcode_to_file(module: &ModuleCodegen<ModuleLlvm>, path: &Path) {
331338
unsafe {
332-
let ext = format!("{name}.bc");
333-
let cgu = Some(&module.name[..]);
334-
let path = cgcx.output_filenames.temp_path_ext(&ext, cgu);
335-
let cstr = path_to_c_string(&path);
339+
let path = path_to_c_string(&path);
336340
let llmod = module.module_llvm.llmod();
337-
llvm::LLVMWriteBitcodeToFile(llmod, cstr.as_ptr());
341+
llvm::LLVMWriteBitcodeToFile(llmod, path.as_ptr());
338342
}
339343
}
340344

@@ -664,7 +668,6 @@ pub(crate) unsafe fn optimize(
664668
) -> Result<(), FatalError> {
665669
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &*module.name);
666670

667-
let llmod = module.module_llvm.llmod();
668671
let llcx = &*module.module_llvm.llcx;
669672
let _handlers = DiagnosticHandlers::new(cgcx, dcx, llcx, module, CodegenDiagnosticsStage::Opt);
670673

@@ -673,8 +676,7 @@ pub(crate) unsafe fn optimize(
673676

674677
if config.emit_no_opt_bc {
675678
let out = cgcx.output_filenames.temp_path_ext("no-opt.bc", module_name);
676-
let out = path_to_c_string(&out);
677-
unsafe { llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr()) };
679+
write_bitcode_to_file(module, &out)
678680
}
679681

680682
// FIXME(ZuseZ4): support SanitizeHWAddress and prevent illegal/unsupported opts

0 commit comments

Comments
 (0)