Skip to content

Commit 888efe7

Browse files
cg_llvm: Switch llvm::add_global to &CStr
1 parent dd51276 commit 888efe7

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

compiler/rustc_codegen_llvm/src/coverageinfo/mapgen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
132132
.collect::<Vec<_>>();
133133
let initializer = cx.const_array(cx.type_ptr(), &name_globals);
134134

135-
let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), "__llvm_coverage_names");
135+
let array = llvm::add_global(cx.llmod, cx.val_ty(initializer), c"__llvm_coverage_names");
136136
llvm::set_global_constant(array, true);
137137
llvm::set_linkage(array, llvm::Linkage::InternalLinkage);
138138
llvm::set_initializer(array, initializer);

compiler/rustc_codegen_llvm/src/coverageinfo/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cell::RefCell;
2+
use std::ffi::CString;
23

34
use libc::c_uint;
45
use rustc_codegen_ssa::traits::{
@@ -284,10 +285,10 @@ pub(crate) fn save_cov_data_to_mod<'ll, 'tcx>(
284285
cx: &CodegenCx<'ll, 'tcx>,
285286
cov_data_val: &'ll llvm::Value,
286287
) {
287-
let covmap_var_name = llvm::build_string(|s| unsafe {
288+
let covmap_var_name = CString::new(llvm::build_byte_buffer(|s| unsafe {
288289
llvm::LLVMRustCoverageWriteMappingVarNameToString(s);
289-
})
290-
.expect("Rust Coverage Mapping var name failed UTF-8 conversion");
290+
}))
291+
.unwrap();
291292
debug!("covmap var name: {:?}", covmap_var_name);
292293

293294
let covmap_section_name = llvm::build_string(|s| unsafe {
@@ -322,7 +323,8 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
322323
// of descriptions play distinct roles in LLVM IR; therefore, assign them different names (by
323324
// appending "u" to the end of the function record var name, to prevent `linkonce_odr` merging.
324325
let func_record_var_name =
325-
format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" });
326+
CString::new(format!("__covrec_{:X}{}", func_name_hash, if is_used { "u" } else { "" }))
327+
.unwrap();
326328
debug!("function record var name: {:?}", func_record_var_name);
327329
debug!("function record section name: {:?}", covfun_section_name);
328330

@@ -334,7 +336,7 @@ pub(crate) fn save_func_record_to_mod<'ll, 'tcx>(
334336
llvm::set_section(llglobal, covfun_section_name);
335337
// LLVM's coverage mapping format specifies 8-byte alignment for items in this section.
336338
llvm::set_alignment(llglobal, Align::EIGHT);
337-
llvm::set_comdat(cx.llmod, llglobal, &func_record_var_name);
339+
llvm::set_comdat(cx.llmod, llglobal, func_record_var_name.to_str().unwrap());
338340
cx.add_used_global(llglobal);
339341
}
340342

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ pub fn set_section(llglobal: &Value, section_name: &str) {
217217
}
218218
}
219219

220-
pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name: &str) -> &'a Value {
221-
let name_cstr = CString::new(name).expect("unexpected CString error");
220+
pub fn add_global<'a>(llmod: &'a Module, ty: &'a Type, name_cstr: &CStr) -> &'a Value {
222221
unsafe { LLVMAddGlobal(llmod, ty, name_cstr.as_ptr()) }
223222
}
224223

0 commit comments

Comments
 (0)