Skip to content

Commit 33db209

Browse files
kxxtyujincheng08
andcommitted
Pass value and valueLen to create a StringRef
Instead of creating a cstring. Co-authored-by: LoveSy <[email protected]>
1 parent adec1a2 commit 33db209

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use libc::c_uint;
3535
use std::borrow::Borrow;
3636
use std::cell::{Cell, RefCell};
3737
use std::ffi::CStr;
38-
use std::ffi::CString;
3938
use std::str;
4039

4140
/// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
@@ -332,15 +331,14 @@ pub unsafe fn create_module<'ll>(
332331
// correctly setting target-abi for the LTO object
333332
// FIXME: https://github.com/llvm/llvm-project/issues/50591
334333
// If llvm_abiname is empty, emit nothing.
335-
if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64")
336-
&& !sess.target.options.llvm_abiname.is_empty()
337-
{
338-
let llvm_abiname = CString::new(sess.target.options.llvm_abiname.as_ref()).unwrap();
334+
let llvm_abiname = &sess.target.options.llvm_abiname;
335+
if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
339336
llvm::LLVMRustAddModuleFlagString(
340337
llmod,
341338
llvm::LLVMModFlagBehavior::Error,
342-
c"target-abi".as_ptr() as *const _,
343-
llvm_abiname.as_ptr() as *const _,
339+
c"target-abi".as_ptr(),
340+
llvm_abiname.as_ptr().cast(),
341+
llvm_abiname.len(),
344342
);
345343
}
346344

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,7 @@ extern "C" {
18131813
merge_behavior: LLVMModFlagBehavior,
18141814
name: *const c_char,
18151815
value: *const c_char,
1816+
value_len: size_t,
18161817
);
18171818

18181819
pub fn LLVMRustHasModuleFlag(M: &Module, name: *const c_char, len: size_t) -> bool;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,10 @@ extern "C" void LLVMRustAddModuleFlagString(
829829
LLVMModuleRef M,
830830
Module::ModFlagBehavior MergeBehavior,
831831
const char *Name,
832-
const char *Value) {
833-
llvm::LLVMContext &Ctx = unwrap(M)->getContext();
834-
unwrap(M)->addModuleFlag(MergeBehavior, Name, llvm::MDString::get(Ctx, Value));
832+
const char *Value,
833+
size_t ValueLen) {
834+
unwrap(M)->addModuleFlag(MergeBehavior, Name,
835+
MDString::get(unwrap(M)->getContext(), StringRef(Value, ValueLen)));
835836
}
836837

837838
extern "C" bool LLVMRustHasModuleFlag(LLVMModuleRef M, const char *Name,

0 commit comments

Comments
 (0)