Skip to content

Commit 4b83038

Browse files
committed
Add a safe wrapper for WriteBitcodeToFile
1 parent b2cd1b8 commit 4b83038

File tree

1 file changed

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

1 file changed

+10
-8
lines changed

Diff for: 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

@@ -679,7 +683,6 @@ pub(crate) unsafe fn optimize(
679683
) -> Result<(), FatalError> {
680684
let _timer = cgcx.prof.generic_activity_with_arg("LLVM_module_optimize", &*module.name);
681685

682-
let llmod = module.module_llvm.llmod();
683686
let llcx = &*module.module_llvm.llcx;
684687
let _handlers = DiagnosticHandlers::new(cgcx, dcx, llcx, module, CodegenDiagnosticsStage::Opt);
685688

@@ -688,8 +691,7 @@ pub(crate) unsafe fn optimize(
688691

689692
if config.emit_no_opt_bc {
690693
let out = cgcx.output_filenames.temp_path_ext("no-opt.bc", module_name);
691-
let out = path_to_c_string(&out);
692-
unsafe { llvm::LLVMWriteBitcodeToFile(llmod, out.as_ptr()) };
694+
write_bitcode_to_file(module, &out)
693695
}
694696

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

0 commit comments

Comments
 (0)