Skip to content

Commit a17561f

Browse files
committed
use new c literals instead of cstr! macro
1 parent e4f7ad8 commit a17561f

File tree

13 files changed

+48
-71
lines changed

13 files changed

+48
-71
lines changed

Cargo.lock

-11
Original file line numberDiff line numberDiff line change
@@ -872,16 +872,6 @@ dependencies = [
872872
"typenum",
873873
]
874874

875-
[[package]]
876-
name = "cstr"
877-
version = "0.2.8"
878-
source = "registry+https://github.com/rust-lang/crates.io-index"
879-
checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
880-
dependencies = [
881-
"proc-macro2",
882-
"quote",
883-
]
884-
885875
[[package]]
886876
name = "ctrlc"
887877
version = "3.3.1"
@@ -3180,7 +3170,6 @@ name = "rustc_codegen_llvm"
31803170
version = "0.0.0"
31813171
dependencies = [
31823172
"bitflags",
3183-
"cstr",
31843173
"libc",
31853174
"measureme",
31863175
"object 0.31.1",

compiler/rustc_codegen_llvm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ test = false
88

99
[dependencies]
1010
bitflags = "1.0"
11-
cstr = "0.2"
1211
libc = "0.2"
1312
measureme = "10.0.0"
1413
object = { version = "0.31.1", default-features = false, features = [

compiler/rustc_codegen_llvm/src/allocator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) unsafe fn codegen(
7777
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
7878
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
7979

80-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
80+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
8181

8282
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
8383
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
@@ -129,7 +129,7 @@ pub(crate) unsafe fn codegen(
129129
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
130130
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
131131

132-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
132+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
133133

134134
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
135135
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);

compiler/rustc_codegen_llvm/src/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ pub(crate) fn run_pass_manager(
595595
llvm::LLVMRustAddModuleFlag(
596596
module.module_llvm.llmod(),
597597
llvm::LLVMModFlagBehavior::Error,
598-
"LTOPostLink\0".as_ptr().cast(),
598+
c"LTOPostLink".as_ptr().cast(),
599599
1,
600600
);
601601
}

compiler/rustc_codegen_llvm/src/back/write.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,11 @@ unsafe fn embed_bitcode(
891891
let llglobal = llvm::LLVMAddGlobal(
892892
llmod,
893893
common::val_ty(llconst),
894-
"rustc.embedded.module\0".as_ptr().cast(),
894+
c"rustc.embedded.module".as_ptr().cast(),
895895
);
896896
llvm::LLVMSetInitializer(llglobal, llconst);
897897

898-
let section = if is_apple { "__LLVM,__bitcode\0" } else { ".llvmbc\0" };
898+
let section = if is_apple { c"__LLVM,__bitcode" } else { c".llvmbc" };
899899
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
900900
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
901901
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
@@ -904,10 +904,10 @@ unsafe fn embed_bitcode(
904904
let llglobal = llvm::LLVMAddGlobal(
905905
llmod,
906906
common::val_ty(llconst),
907-
"rustc.embedded.cmdline\0".as_ptr().cast(),
907+
c"rustc.embedded.cmdline".as_ptr().cast(),
908908
);
909909
llvm::LLVMSetInitializer(llglobal, llconst);
910-
let section = if is_apple { "__LLVM,__cmdline\0" } else { ".llvmcmd\0" };
910+
let section = if is_apple { c"__LLVM,__cmdline" } else { c".llvmcmd" };
911911
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
912912
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
913913
} else {

compiler/rustc_codegen_llvm/src/base.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use crate::context::CodegenCx;
1919
use crate::llvm;
2020
use crate::value::Value;
2121

22-
use cstr::cstr;
23-
2422
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
2523
use rustc_codegen_ssa::mono_item::MonoItemExt;
2624
use rustc_codegen_ssa::traits::*;
@@ -110,11 +108,11 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen
110108

111109
// Create the llvm.used and llvm.compiler.used variables.
112110
if !cx.used_statics.borrow().is_empty() {
113-
cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow());
111+
cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow());
114112
}
115113
if !cx.compiler_used_statics.borrow().is_empty() {
116114
cx.create_used_variable_impl(
117-
cstr!("llvm.compiler.used"),
115+
c"llvm.compiler.used",
118116
&*cx.compiler_used_statics.borrow(),
119117
);
120118
}

compiler/rustc_codegen_llvm/src/builder.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True}
66
use crate::type_::Type;
77
use crate::type_of::LayoutLlvmExt;
88
use crate::value::Value;
9-
use cstr::cstr;
109
use libc::{c_char, c_uint};
1110
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
1211
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -25,7 +24,6 @@ use rustc_symbol_mangling::typeid::{kcfi_typeid_for_fnabi, typeid_for_fnabi, Typ
2524
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2625
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
2726
use std::borrow::Cow;
28-
use std::ffi::CStr;
2927
use std::iter;
3028
use std::ops::Deref;
3129
use std::ptr;
@@ -45,13 +43,10 @@ impl Drop for Builder<'_, '_, '_> {
4543
}
4644
}
4745

48-
// FIXME(eddyb) use a checked constructor when they become `const fn`.
49-
const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
50-
5146
/// Empty string, to be used where LLVM expects an instruction name, indicating
5247
/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
5348
// FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
54-
const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
49+
const UNNAMED: *const c_char = c"".as_ptr();
5550

5651
impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
5752
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
@@ -1010,14 +1005,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10101005
}
10111006

10121007
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
1013-
let name = cstr!("cleanuppad");
10141008
let ret = unsafe {
10151009
llvm::LLVMBuildCleanupPad(
10161010
self.llbuilder,
10171011
parent,
10181012
args.as_ptr(),
10191013
args.len() as c_uint,
1020-
name.as_ptr(),
1014+
c"cleanuppad".as_ptr(),
10211015
)
10221016
};
10231017
Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
@@ -1031,14 +1025,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10311025
}
10321026

10331027
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1034-
let name = cstr!("catchpad");
10351028
let ret = unsafe {
10361029
llvm::LLVMBuildCatchPad(
10371030
self.llbuilder,
10381031
parent,
10391032
args.as_ptr(),
10401033
args.len() as c_uint,
1041-
name.as_ptr(),
1034+
c"catchpad".as_ptr(),
10421035
)
10431036
};
10441037
Funclet::new(ret.expect("LLVM does not have support for catchpad"))
@@ -1050,14 +1043,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10501043
unwind: Option<&'ll BasicBlock>,
10511044
handlers: &[&'ll BasicBlock],
10521045
) -> &'ll Value {
1053-
let name = cstr!("catchswitch");
10541046
let ret = unsafe {
10551047
llvm::LLVMBuildCatchSwitch(
10561048
self.llbuilder,
10571049
parent,
10581050
unwind,
10591051
handlers.len() as c_uint,
1060-
name.as_ptr(),
1052+
c"catchswitch".as_ptr(),
10611053
)
10621054
};
10631055
let ret = ret.expect("LLVM does not have support for catchswitch");

compiler/rustc_codegen_llvm/src/consts.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::llvm::{self, True};
66
use crate::type_::Type;
77
use crate::type_of::LayoutLlvmExt;
88
use crate::value::Value;
9-
use cstr::cstr;
109
use rustc_codegen_ssa::traits::*;
1110
use rustc_hir::def_id::DefId;
1211
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
@@ -473,9 +472,9 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
473472
.all(|&byte| byte == 0);
474473

475474
let sect_name = if all_bytes_are_zero {
476-
cstr!("__DATA,__thread_bss")
475+
c"__DATA,__thread_bss"
477476
} else {
478-
cstr!("__DATA,__thread_data")
477+
c"__DATA,__thread_data"
479478
};
480479
llvm::LLVMSetSection(g, sect_name.as_ptr());
481480
}
@@ -504,7 +503,7 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
504503
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
505504
llvm::LLVMAddNamedMetadataOperand(
506505
self.llmod,
507-
"wasm.custom_sections\0".as_ptr().cast(),
506+
c"wasm.custom_sections".as_ptr().cast(),
508507
val,
509508
);
510509
}

compiler/rustc_codegen_llvm/src/context.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::llvm_util;
88
use crate::type_::Type;
99
use crate::value::Value;
1010

11-
use cstr::cstr;
1211
use rustc_codegen_ssa::base::wants_msvc_seh;
1312
use rustc_codegen_ssa::traits::*;
1413
use rustc_data_structures::base_n;
@@ -224,36 +223,42 @@ pub unsafe fn create_module<'ll>(
224223
// If skipping the PLT is enabled, we need to add some module metadata
225224
// to ensure intrinsic calls don't use it.
226225
if !sess.needs_plt() {
227-
let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
228-
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
226+
llvm::LLVMRustAddModuleFlag(
227+
llmod,
228+
llvm::LLVMModFlagBehavior::Warning,
229+
c"RtLibUseGOT".as_ptr().cast(),
230+
1,
231+
);
229232
}
230233

231234
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
232235
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
233-
let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast();
234236
llvm::LLVMRustAddModuleFlag(
235237
llmod,
236238
llvm::LLVMModFlagBehavior::Override,
237-
canonical_jump_tables,
239+
c"CFI Canonical Jump Tables".as_ptr().cast(),
238240
1,
239241
);
240242
}
241243

242244
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
243245
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
244-
let enable_split_lto_unit = "EnableSplitLTOUnit\0".as_ptr().cast();
245246
llvm::LLVMRustAddModuleFlag(
246247
llmod,
247248
llvm::LLVMModFlagBehavior::Override,
248-
enable_split_lto_unit,
249+
c"EnableSplitLTOUnit".as_ptr().cast(),
249250
1,
250251
);
251252
}
252253

253254
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
254255
if sess.is_sanitizer_kcfi_enabled() {
255-
let kcfi = "kcfi\0".as_ptr().cast();
256-
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
256+
llvm::LLVMRustAddModuleFlag(
257+
llmod,
258+
llvm::LLVMModFlagBehavior::Override,
259+
c"kcfi".as_ptr().cast(),
260+
1,
261+
);
257262
}
258263

259264
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
@@ -265,7 +270,7 @@ pub unsafe fn create_module<'ll>(
265270
llvm::LLVMRustAddModuleFlag(
266271
llmod,
267272
llvm::LLVMModFlagBehavior::Warning,
268-
"cfguard\0".as_ptr() as *const _,
273+
c"cfguard".as_ptr() as *const _,
269274
1,
270275
)
271276
}
@@ -274,7 +279,7 @@ pub unsafe fn create_module<'ll>(
274279
llvm::LLVMRustAddModuleFlag(
275280
llmod,
276281
llvm::LLVMModFlagBehavior::Warning,
277-
"cfguard\0".as_ptr() as *const _,
282+
c"cfguard".as_ptr() as *const _,
278283
2,
279284
)
280285
}
@@ -292,26 +297,26 @@ pub unsafe fn create_module<'ll>(
292297
llvm::LLVMRustAddModuleFlag(
293298
llmod,
294299
behavior,
295-
"branch-target-enforcement\0".as_ptr().cast(),
300+
c"branch-target-enforcement".as_ptr().cast(),
296301
bti.into(),
297302
);
298303
llvm::LLVMRustAddModuleFlag(
299304
llmod,
300305
behavior,
301-
"sign-return-address\0".as_ptr().cast(),
306+
c"sign-return-address".as_ptr().cast(),
302307
pac_ret.is_some().into(),
303308
);
304309
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
305310
llvm::LLVMRustAddModuleFlag(
306311
llmod,
307312
behavior,
308-
"sign-return-address-all\0".as_ptr().cast(),
313+
c"sign-return-address-all".as_ptr().cast(),
309314
pac_opts.leaf.into(),
310315
);
311316
llvm::LLVMRustAddModuleFlag(
312317
llmod,
313318
behavior,
314-
"sign-return-address-with-bkey\0".as_ptr().cast(),
319+
c"sign-return-address-with-bkey".as_ptr().cast(),
315320
u32::from(pac_opts.key == PAuthKey::B),
316321
);
317322
} else {
@@ -327,15 +332,15 @@ pub unsafe fn create_module<'ll>(
327332
llvm::LLVMRustAddModuleFlag(
328333
llmod,
329334
llvm::LLVMModFlagBehavior::Override,
330-
"cf-protection-branch\0".as_ptr().cast(),
335+
c"cf-protection-branch".as_ptr().cast(),
331336
1,
332337
)
333338
}
334339
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
335340
llvm::LLVMRustAddModuleFlag(
336341
llmod,
337342
llvm::LLVMModFlagBehavior::Override,
338-
"cf-protection-return\0".as_ptr().cast(),
343+
c"cf-protection-return".as_ptr().cast(),
339344
1,
340345
)
341346
}
@@ -344,7 +349,7 @@ pub unsafe fn create_module<'ll>(
344349
llvm::LLVMRustAddModuleFlag(
345350
llmod,
346351
llvm::LLVMModFlagBehavior::Error,
347-
"Virtual Function Elim\0".as_ptr().cast(),
352+
c"Virtual Function Elim".as_ptr().cast(),
348353
1,
349354
);
350355
}
@@ -476,14 +481,13 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
476481
}
477482

478483
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
479-
let section = cstr!("llvm.metadata");
480484
let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
481485

482486
unsafe {
483487
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
484488
llvm::LLVMSetInitializer(g, array);
485489
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
486-
llvm::LLVMSetSection(g, section.as_ptr());
490+
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
487491
}
488492
}
489493
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
3838
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
3939

4040
section_var.unwrap_or_else(|| {
41-
let section_name = b".debug_gdb_scripts\0";
4241
let mut section_contents = Vec::new();
4342

4443
// Add the pretty printers for the standard library first.
@@ -71,7 +70,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
7170
let section_var = cx
7271
.define_global(section_var_name, llvm_type)
7372
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
74-
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
73+
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr().cast());
7574
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
7675
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7776
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);

0 commit comments

Comments
 (0)