Skip to content

Commit b1a5004

Browse files
committed
Revert "compiler: replace cstr macro with c str literals in compiler and few other c str replacements"
This reverts commit 26e69a8.
1 parent 4133996 commit b1a5004

File tree

14 files changed

+71
-42
lines changed

14 files changed

+71
-42
lines changed

Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,16 @@ dependencies = [
866866
"typenum",
867867
]
868868

869+
[[package]]
870+
name = "cstr"
871+
version = "0.2.8"
872+
source = "registry+https://github.com/rust-lang/crates.io-index"
873+
checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
874+
dependencies = [
875+
"proc-macro2",
876+
"quote",
877+
]
878+
869879
[[package]]
870880
name = "ctrlc"
871881
version = "3.4.0"
@@ -3553,6 +3563,7 @@ name = "rustc_codegen_llvm"
35533563
version = "0.0.0"
35543564
dependencies = [
35553565
"bitflags 1.3.2",
3566+
"cstr",
35563567
"itertools",
35573568
"libc",
35583569
"measureme",

compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ test = false
99
[dependencies]
1010
# tidy-alphabetical-start
1111
bitflags = "1.0"
12+
cstr = "0.2"
1213
itertools = "0.11"
1314
libc = "0.2"
1415
measureme = "10.0.0"

compiler/rustc_codegen_llvm/src/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn create_wrapper_function(
146146
}
147147
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
148148

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

151151
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
152152
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);

compiler/rustc_codegen_llvm/src/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ pub(crate) fn run_pass_manager(
610610
llvm::LLVMRustAddModuleFlag(
611611
module.module_llvm.llmod(),
612612
llvm::LLVMModFlagBehavior::Error,
613-
c"LTOPostLink".as_ptr().cast(),
613+
"LTOPostLink\0".as_ptr().cast(),
614614
1,
615615
);
616616
}

compiler/rustc_codegen_llvm/src/back/write.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,6 @@ fn target_is_aix(cgcx: &CodegenContext<LlvmCodegenBackend>) -> bool {
906906
cgcx.opts.target_triple.triple().contains("-aix")
907907
}
908908

909-
//FIXME use c string literals here too
910909
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static str {
911910
if target_is_apple(cgcx) {
912911
"__LLVM,__bitcode\0"
@@ -985,7 +984,7 @@ unsafe fn embed_bitcode(
985984
let llglobal = llvm::LLVMAddGlobal(
986985
llmod,
987986
common::val_ty(llconst),
988-
c"rustc.embedded.module".as_ptr().cast(),
987+
"rustc.embedded.module\0".as_ptr().cast(),
989988
);
990989
llvm::LLVMSetInitializer(llglobal, llconst);
991990

@@ -998,15 +997,15 @@ unsafe fn embed_bitcode(
998997
let llglobal = llvm::LLVMAddGlobal(
999998
llmod,
1000999
common::val_ty(llconst),
1001-
c"rustc.embedded.cmdline".as_ptr().cast(),
1000+
"rustc.embedded.cmdline\0".as_ptr().cast(),
10021001
);
10031002
llvm::LLVMSetInitializer(llglobal, llconst);
10041003
let section = if is_apple {
1005-
c"__LLVM,__cmdline"
1004+
"__LLVM,__cmdline\0"
10061005
} else if is_aix {
1007-
c".info"
1006+
".info\0"
10081007
} else {
1009-
c".llvmcmd"
1008+
".llvmcmd\0"
10101009
};
10111010
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
10121011
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);

compiler/rustc_codegen_llvm/src/base.rs

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

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

109111
// Create the llvm.used and llvm.compiler.used variables.
110112
if !cx.used_statics.borrow().is_empty() {
111-
cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow());
113+
cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow());
112114
}
113115
if !cx.compiler_used_statics.borrow().is_empty() {
114116
cx.create_used_variable_impl(
115-
c"llvm.compiler.used",
117+
cstr!("llvm.compiler.used"),
116118
&*cx.compiler_used_statics.borrow(),
117119
);
118120
}

compiler/rustc_codegen_llvm/src/builder.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::llvm_util;
77
use crate::type_::Type;
88
use crate::type_of::LayoutLlvmExt;
99
use crate::value::Value;
10+
use cstr::cstr;
1011
use libc::{c_char, c_uint};
1112
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
1213
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -26,6 +27,7 @@ use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2627
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
2728
use smallvec::SmallVec;
2829
use std::borrow::Cow;
30+
use std::ffi::CStr;
2931
use std::iter;
3032
use std::ops::Deref;
3133
use std::ptr;
@@ -45,10 +47,13 @@ impl Drop for Builder<'_, '_, '_> {
4547
}
4648
}
4749

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

5358
impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
5459
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
@@ -1007,13 +1012,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10071012
}
10081013

10091014
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
1015+
let name = cstr!("cleanuppad");
10101016
let ret = unsafe {
10111017
llvm::LLVMBuildCleanupPad(
10121018
self.llbuilder,
10131019
parent,
10141020
args.as_ptr(),
10151021
args.len() as c_uint,
1016-
c"cleanuppad".as_ptr(),
1022+
name.as_ptr(),
10171023
)
10181024
};
10191025
Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
@@ -1027,13 +1033,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10271033
}
10281034

10291035
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1036+
let name = cstr!("catchpad");
10301037
let ret = unsafe {
10311038
llvm::LLVMBuildCatchPad(
10321039
self.llbuilder,
10331040
parent,
10341041
args.as_ptr(),
10351042
args.len() as c_uint,
1036-
c"catchpad".as_ptr(),
1043+
name.as_ptr(),
10371044
)
10381045
};
10391046
Funclet::new(ret.expect("LLVM does not have support for catchpad"))
@@ -1045,13 +1052,14 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10451052
unwind: Option<&'ll BasicBlock>,
10461053
handlers: &[&'ll BasicBlock],
10471054
) -> &'ll Value {
1055+
let name = cstr!("catchswitch");
10481056
let ret = unsafe {
10491057
llvm::LLVMBuildCatchSwitch(
10501058
self.llbuilder,
10511059
parent,
10521060
unwind,
10531061
handlers.len() as c_uint,
1054-
c"catchswitch".as_ptr(),
1062+
name.as_ptr(),
10551063
)
10561064
};
10571065
let ret = ret.expect("LLVM does not have support for catchswitch");

compiler/rustc_codegen_llvm/src/consts.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::llvm::{self, True};
88
use crate::type_::Type;
99
use crate::type_of::LayoutLlvmExt;
1010
use crate::value::Value;
11+
use cstr::cstr;
1112
use rustc_codegen_ssa::traits::*;
1213
use rustc_hir::def_id::DefId;
1314
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
@@ -472,9 +473,9 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
472473
.all(|&byte| byte == 0);
473474

474475
let sect_name = if all_bytes_are_zero {
475-
c"__DATA,__thread_bss"
476+
cstr!("__DATA,__thread_bss")
476477
} else {
477-
c"__DATA,__thread_data"
478+
cstr!("__DATA,__thread_data")
478479
};
479480
llvm::LLVMSetSection(g, sect_name.as_ptr());
480481
}
@@ -503,7 +504,7 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
503504
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
504505
llvm::LLVMAddNamedMetadataOperand(
505506
self.llmod,
506-
c"wasm.custom_sections".as_ptr().cast(),
507+
"wasm.custom_sections\0".as_ptr().cast(),
507508
val,
508509
);
509510
}

compiler/rustc_codegen_llvm/src/context.rs

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

11+
use cstr::cstr;
1112
use rustc_codegen_ssa::base::{wants_msvc_seh, wants_wasm_eh};
1213
use rustc_codegen_ssa::errors as ssa_errors;
1314
use rustc_codegen_ssa::traits::*;
@@ -214,13 +215,13 @@ pub unsafe fn create_module<'ll>(
214215
// If skipping the PLT is enabled, we need to add some module metadata
215216
// to ensure intrinsic calls don't use it.
216217
if !sess.needs_plt() {
217-
let avoid_plt = c"RtLibUseGOT".as_ptr().cast();
218+
let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
218219
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
219220
}
220221

221222
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
222223
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
223-
let canonical_jump_tables = c"CFI Canonical Jump Tables".as_ptr().cast();
224+
let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast();
224225
llvm::LLVMRustAddModuleFlag(
225226
llmod,
226227
llvm::LLVMModFlagBehavior::Override,
@@ -231,7 +232,7 @@ pub unsafe fn create_module<'ll>(
231232

232233
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
233234
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
234-
let enable_split_lto_unit = c"EnableSplitLTOUnit".as_ptr().cast();
235+
let enable_split_lto_unit = "EnableSplitLTOUnit\0".as_ptr().cast();
235236
llvm::LLVMRustAddModuleFlag(
236237
llmod,
237238
llvm::LLVMModFlagBehavior::Override,
@@ -242,7 +243,7 @@ pub unsafe fn create_module<'ll>(
242243

243244
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
244245
if sess.is_sanitizer_kcfi_enabled() {
245-
let kcfi = c"kcfi".as_ptr().cast();
246+
let kcfi = "kcfi\0".as_ptr().cast();
246247
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
247248
}
248249

@@ -255,7 +256,7 @@ pub unsafe fn create_module<'ll>(
255256
llvm::LLVMRustAddModuleFlag(
256257
llmod,
257258
llvm::LLVMModFlagBehavior::Warning,
258-
c"cfguard".as_ptr() as *const _,
259+
"cfguard\0".as_ptr() as *const _,
259260
1,
260261
)
261262
}
@@ -264,7 +265,7 @@ pub unsafe fn create_module<'ll>(
264265
llvm::LLVMRustAddModuleFlag(
265266
llmod,
266267
llvm::LLVMModFlagBehavior::Warning,
267-
c"cfguard".as_ptr() as *const _,
268+
"cfguard\0".as_ptr() as *const _,
268269
2,
269270
)
270271
}
@@ -282,26 +283,26 @@ pub unsafe fn create_module<'ll>(
282283
llvm::LLVMRustAddModuleFlag(
283284
llmod,
284285
behavior,
285-
c"branch-target-enforcement".as_ptr().cast(),
286+
"branch-target-enforcement\0".as_ptr().cast(),
286287
bti.into(),
287288
);
288289
llvm::LLVMRustAddModuleFlag(
289290
llmod,
290291
behavior,
291-
c"sign-return-address".as_ptr().cast(),
292+
"sign-return-address\0".as_ptr().cast(),
292293
pac_ret.is_some().into(),
293294
);
294295
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
295296
llvm::LLVMRustAddModuleFlag(
296297
llmod,
297298
behavior,
298-
c"sign-return-address-all".as_ptr().cast(),
299+
"sign-return-address-all\0".as_ptr().cast(),
299300
pac_opts.leaf.into(),
300301
);
301302
llvm::LLVMRustAddModuleFlag(
302303
llmod,
303304
behavior,
304-
c"sign-return-address-with-bkey".as_ptr().cast(),
305+
"sign-return-address-with-bkey\0".as_ptr().cast(),
305306
u32::from(pac_opts.key == PAuthKey::B),
306307
);
307308
} else {
@@ -317,15 +318,15 @@ pub unsafe fn create_module<'ll>(
317318
llvm::LLVMRustAddModuleFlag(
318319
llmod,
319320
llvm::LLVMModFlagBehavior::Override,
320-
c"cf-protection-branch".as_ptr().cast(),
321+
"cf-protection-branch\0".as_ptr().cast(),
321322
1,
322323
)
323324
}
324325
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
325326
llvm::LLVMRustAddModuleFlag(
326327
llmod,
327328
llvm::LLVMModFlagBehavior::Override,
328-
c"cf-protection-return".as_ptr().cast(),
329+
"cf-protection-return\0".as_ptr().cast(),
329330
1,
330331
)
331332
}
@@ -334,7 +335,7 @@ pub unsafe fn create_module<'ll>(
334335
llvm::LLVMRustAddModuleFlag(
335336
llmod,
336337
llvm::LLVMModFlagBehavior::Error,
337-
c"Virtual Function Elim".as_ptr().cast(),
338+
"Virtual Function Elim\0".as_ptr().cast(),
338339
1,
339340
);
340341
}
@@ -344,7 +345,7 @@ pub unsafe fn create_module<'ll>(
344345
llvm::LLVMRustAddModuleFlag(
345346
llmod,
346347
llvm::LLVMModFlagBehavior::Warning,
347-
c"ehcontguard".as_ptr() as *const _,
348+
"ehcontguard\0".as_ptr() as *const _,
348349
1,
349350
)
350351
}
@@ -362,7 +363,7 @@ pub unsafe fn create_module<'ll>(
362363
);
363364
llvm::LLVMAddNamedMetadataOperand(
364365
llmod,
365-
c"llvm.ident".as_ptr(),
366+
cstr!("llvm.ident").as_ptr(),
366367
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
367368
);
368369

@@ -510,13 +511,14 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
510511
}
511512

512513
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
514+
let section = cstr!("llvm.metadata");
513515
let array = self.const_array(self.type_ptr(), values);
514516

515517
unsafe {
516518
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
517519
llvm::LLVMSetInitializer(g, array);
518520
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
519-
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
521+
llvm::LLVMSetSection(g, section.as_ptr());
520522
}
521523
}
522524
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ pub fn insert_reference_to_gdb_debug_scripts_section_global(bx: &mut Builder<'_,
3030
/// Allocates the global variable responsible for the .debug_gdb_scripts binary
3131
/// section.
3232
pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Value {
33-
let c_section_var_name = c"__rustc_debug_gdb_scripts_section__";
34-
let section_var_name = c_section_var_name.to_str().unwrap();
33+
let c_section_var_name = "__rustc_debug_gdb_scripts_section__\0";
34+
let section_var_name = &c_section_var_name[..c_section_var_name.len() - 1];
3535

3636
let section_var =
3737
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
3838

3939
section_var.unwrap_or_else(|| {
40+
let section_name = b".debug_gdb_scripts\0";
4041
let mut section_contents = Vec::new();
4142

4243
// Add the pretty printers for the standard library first.
@@ -69,7 +70,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
6970
let section_var = cx
7071
.define_global(section_var_name, llvm_type)
7172
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
72-
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr().cast());
73+
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
7374
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
7475
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7576
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);

0 commit comments

Comments
 (0)