Skip to content

Commit 1aa6aef

Browse files
committed
Auto merge of #118566 - klensy:cstr-new, r=WaffleLapkin
use c literals in compiler and library Relands refreshed #111647
2 parents 9d49eb7 + c013488 commit 1aa6aef

File tree

23 files changed

+68
-97
lines changed

23 files changed

+68
-97
lines changed

Cargo.lock

-11
Original file line numberDiff line numberDiff line change
@@ -866,16 +866,6 @@ 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-
879869
[[package]]
880870
name = "ctrlc"
881871
version = "3.4.0"
@@ -3582,7 +3572,6 @@ name = "rustc_codegen_llvm"
35823572
version = "0.0.0"
35833573
dependencies = [
35843574
"bitflags 1.3.2",
3585-
"cstr",
35863575
"itertools",
35873576
"libc",
35883577
"measureme",

compiler/rustc_codegen_llvm/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ test = false
99
[dependencies]
1010
# tidy-alphabetical-start
1111
bitflags = "1.0"
12-
cstr = "0.2"
1312
itertools = "0.11"
1413
libc = "0.2"
1514
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, "entry\0".as_ptr().cast());
149+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".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
@@ -631,7 +631,7 @@ pub(crate) fn run_pass_manager(
631631
llvm::LLVMRustAddModuleFlag(
632632
module.module_llvm.llmod(),
633633
llvm::LLVMModFlagBehavior::Error,
634-
"LTOPostLink\0".as_ptr().cast(),
634+
c"LTOPostLink".as_ptr().cast(),
635635
1,
636636
);
637637
}

compiler/rustc_codegen_llvm/src/back/write.rs

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

919+
//FIXME use c string literals here too
919920
pub(crate) fn bitcode_section_name(cgcx: &CodegenContext<LlvmCodegenBackend>) -> &'static str {
920921
if target_is_apple(cgcx) {
921922
"__LLVM,__bitcode\0"
@@ -994,7 +995,7 @@ unsafe fn embed_bitcode(
994995
let llglobal = llvm::LLVMAddGlobal(
995996
llmod,
996997
common::val_ty(llconst),
997-
"rustc.embedded.module\0".as_ptr().cast(),
998+
c"rustc.embedded.module".as_ptr().cast(),
998999
);
9991000
llvm::LLVMSetInitializer(llglobal, llconst);
10001001

@@ -1007,15 +1008,15 @@ unsafe fn embed_bitcode(
10071008
let llglobal = llvm::LLVMAddGlobal(
10081009
llmod,
10091010
common::val_ty(llconst),
1010-
"rustc.embedded.cmdline\0".as_ptr().cast(),
1011+
c"rustc.embedded.cmdline".as_ptr().cast(),
10111012
);
10121013
llvm::LLVMSetInitializer(llglobal, llconst);
10131014
let section = if is_apple {
1014-
"__LLVM,__cmdline\0"
1015+
c"__LLVM,__cmdline"
10151016
} else if is_aix {
1016-
".info\0"
1017+
c".info"
10171018
} else {
1018-
".llvmcmd\0"
1019+
c".llvmcmd"
10191020
};
10201021
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
10211022
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);

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
@@ -7,7 +7,6 @@ use crate::llvm_util;
77
use crate::type_::Type;
88
use crate::type_of::LayoutLlvmExt;
99
use crate::value::Value;
10-
use cstr::cstr;
1110
use libc::{c_char, c_uint};
1211
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
1312
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -27,7 +26,6 @@ use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2726
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
2827
use smallvec::SmallVec;
2928
use std::borrow::Cow;
30-
use std::ffi::CStr;
3129
use std::iter;
3230
use std::ops::Deref;
3331
use std::ptr;
@@ -47,13 +45,10 @@ impl Drop for Builder<'_, '_, '_> {
4745
}
4846
}
4947

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-
5348
/// Empty string, to be used where LLVM expects an instruction name, indicating
5449
/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
5550
// FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
56-
const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
51+
const UNNAMED: *const c_char = c"".as_ptr();
5752

5853
impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
5954
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
@@ -1012,14 +1007,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10121007
}
10131008

10141009
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
1015-
let name = cstr!("cleanuppad");
10161010
let ret = unsafe {
10171011
llvm::LLVMBuildCleanupPad(
10181012
self.llbuilder,
10191013
parent,
10201014
args.as_ptr(),
10211015
args.len() as c_uint,
1022-
name.as_ptr(),
1016+
c"cleanuppad".as_ptr(),
10231017
)
10241018
};
10251019
Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
@@ -1033,14 +1027,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10331027
}
10341028

10351029
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1036-
let name = cstr!("catchpad");
10371030
let ret = unsafe {
10381031
llvm::LLVMBuildCatchPad(
10391032
self.llbuilder,
10401033
parent,
10411034
args.as_ptr(),
10421035
args.len() as c_uint,
1043-
name.as_ptr(),
1036+
c"catchpad".as_ptr(),
10441037
)
10451038
};
10461039
Funclet::new(ret.expect("LLVM does not have support for catchpad"))
@@ -1052,14 +1045,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10521045
unwind: Option<&'ll BasicBlock>,
10531046
handlers: &[&'ll BasicBlock],
10541047
) -> &'ll Value {
1055-
let name = cstr!("catchswitch");
10561048
let ret = unsafe {
10571049
llvm::LLVMBuildCatchSwitch(
10581050
self.llbuilder,
10591051
parent,
10601052
unwind,
10611053
handlers.len() as c_uint,
1062-
name.as_ptr(),
1054+
c"catchswitch".as_ptr(),
10631055
)
10641056
};
10651057
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
@@ -8,7 +8,6 @@ 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;
1211
use rustc_codegen_ssa::traits::*;
1312
use rustc_hir::def_id::DefId;
1413
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

+16-18
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, wants_wasm_eh};
1312
use rustc_codegen_ssa::errors as ssa_errors;
1413
use rustc_codegen_ssa::traits::*;
@@ -215,13 +214,13 @@ pub unsafe fn create_module<'ll>(
215214
// If skipping the PLT is enabled, we need to add some module metadata
216215
// to ensure intrinsic calls don't use it.
217216
if !sess.needs_plt() {
218-
let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
217+
let avoid_plt = c"RtLibUseGOT".as_ptr().cast();
219218
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
220219
}
221220

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

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

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

@@ -256,7 +255,7 @@ pub unsafe fn create_module<'ll>(
256255
llvm::LLVMRustAddModuleFlag(
257256
llmod,
258257
llvm::LLVMModFlagBehavior::Warning,
259-
"cfguard\0".as_ptr() as *const _,
258+
c"cfguard".as_ptr() as *const _,
260259
1,
261260
)
262261
}
@@ -265,7 +264,7 @@ pub unsafe fn create_module<'ll>(
265264
llvm::LLVMRustAddModuleFlag(
266265
llmod,
267266
llvm::LLVMModFlagBehavior::Warning,
268-
"cfguard\0".as_ptr() as *const _,
267+
c"cfguard".as_ptr() as *const _,
269268
2,
270269
)
271270
}
@@ -283,26 +282,26 @@ pub unsafe fn create_module<'ll>(
283282
llvm::LLVMRustAddModuleFlag(
284283
llmod,
285284
behavior,
286-
"branch-target-enforcement\0".as_ptr().cast(),
285+
c"branch-target-enforcement".as_ptr().cast(),
287286
bti.into(),
288287
);
289288
llvm::LLVMRustAddModuleFlag(
290289
llmod,
291290
behavior,
292-
"sign-return-address\0".as_ptr().cast(),
291+
c"sign-return-address".as_ptr().cast(),
293292
pac_ret.is_some().into(),
294293
);
295294
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
296295
llvm::LLVMRustAddModuleFlag(
297296
llmod,
298297
behavior,
299-
"sign-return-address-all\0".as_ptr().cast(),
298+
c"sign-return-address-all".as_ptr().cast(),
300299
pac_opts.leaf.into(),
301300
);
302301
llvm::LLVMRustAddModuleFlag(
303302
llmod,
304303
behavior,
305-
"sign-return-address-with-bkey\0".as_ptr().cast(),
304+
c"sign-return-address-with-bkey".as_ptr().cast(),
306305
u32::from(pac_opts.key == PAuthKey::B),
307306
);
308307
} else {
@@ -318,15 +317,15 @@ pub unsafe fn create_module<'ll>(
318317
llvm::LLVMRustAddModuleFlag(
319318
llmod,
320319
llvm::LLVMModFlagBehavior::Override,
321-
"cf-protection-branch\0".as_ptr().cast(),
320+
c"cf-protection-branch".as_ptr().cast(),
322321
1,
323322
)
324323
}
325324
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
326325
llvm::LLVMRustAddModuleFlag(
327326
llmod,
328327
llvm::LLVMModFlagBehavior::Override,
329-
"cf-protection-return\0".as_ptr().cast(),
328+
c"cf-protection-return".as_ptr().cast(),
330329
1,
331330
)
332331
}
@@ -335,7 +334,7 @@ pub unsafe fn create_module<'ll>(
335334
llvm::LLVMRustAddModuleFlag(
336335
llmod,
337336
llvm::LLVMModFlagBehavior::Error,
338-
"Virtual Function Elim\0".as_ptr().cast(),
337+
c"Virtual Function Elim".as_ptr().cast(),
339338
1,
340339
);
341340
}
@@ -345,7 +344,7 @@ pub unsafe fn create_module<'ll>(
345344
llvm::LLVMRustAddModuleFlag(
346345
llmod,
347346
llvm::LLVMModFlagBehavior::Warning,
348-
"ehcontguard\0".as_ptr() as *const _,
347+
c"ehcontguard".as_ptr() as *const _,
349348
1,
350349
)
351350
}
@@ -363,7 +362,7 @@ pub unsafe fn create_module<'ll>(
363362
);
364363
llvm::LLVMAddNamedMetadataOperand(
365364
llmod,
366-
cstr!("llvm.ident").as_ptr(),
365+
c"llvm.ident".as_ptr(),
367366
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
368367
);
369368

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

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

517515
unsafe {
518516
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
519517
llvm::LLVMSetInitializer(g, array);
520518
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
521-
llvm::LLVMSetSection(g, section.as_ptr());
519+
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
522520
}
523521
}
524522
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@ 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 = "__rustc_debug_gdb_scripts_section__\0";
34-
let section_var_name = &c_section_var_name[..c_section_var_name.len() - 1];
33+
let c_section_var_name = c"__rustc_debug_gdb_scripts_section__";
34+
let section_var_name = c_section_var_name.to_str().unwrap();
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";
4140
let mut section_contents = Vec::new();
4241

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

0 commit comments

Comments
 (0)