Revert "Replace const_cstr with cstr crate"
diff --git a/Cargo.lock b/Cargo.lock
index 2e46d43..32a06fc 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -905,16 +905,6 @@
]
[[package]]
-name = "cstr"
-version = "0.2.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
-dependencies = [
- "proc-macro2",
- "quote",
-]
-
-[[package]]
name = "ctor"
version = "0.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3720,7 +3710,6 @@
version = "0.0.0"
dependencies = [
"bitflags",
- "cstr",
"libc",
"measureme",
"rustc-demangle",
diff --git a/compiler/rustc_codegen_llvm/Cargo.toml b/compiler/rustc_codegen_llvm/Cargo.toml
index 4999cb3..6dad087 100644
--- a/compiler/rustc_codegen_llvm/Cargo.toml
+++ b/compiler/rustc_codegen_llvm/Cargo.toml
@@ -10,7 +10,6 @@
[dependencies]
bitflags = "1.0"
-cstr = "0.2"
libc = "0.2"
measureme = "9.1.0"
snap = "1"
diff --git a/compiler/rustc_codegen_llvm/src/abi.rs b/compiler/rustc_codegen_llvm/src/abi.rs
index d714ff1..a69241e 100644
--- a/compiler/rustc_codegen_llvm/src/abi.rs
+++ b/compiler/rustc_codegen_llvm/src/abi.rs
@@ -554,7 +554,7 @@
llvm::AddCallSiteAttrString(
callsite,
llvm::AttributePlace::Function,
- cstr::cstr!("cmse_nonsecure_call"),
+ rustc_data_structures::const_cstr!("cmse_nonsecure_call"),
);
}
}
diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs
index 09ece61..2611172 100644
--- a/compiler/rustc_codegen_llvm/src/attributes.rs
+++ b/compiler/rustc_codegen_llvm/src/attributes.rs
@@ -2,8 +2,8 @@
use std::ffi::CString;
-use cstr::cstr;
use rustc_codegen_ssa::traits::*;
+use rustc_data_structures::const_cstr;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId;
@@ -75,8 +75,8 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("frame-pointer"),
- cstr!("all"),
+ const_cstr!("frame-pointer"),
+ const_cstr!("all"),
);
}
}
@@ -95,7 +95,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("instrument-function-entry-inlined"),
+ const_cstr!("instrument-function-entry-inlined"),
&mcount_name,
);
}
@@ -129,16 +129,16 @@
StackProbeType::None => None,
// Request LLVM to generate the probes inline. If the given LLVM version does not support
// this, no probe is generated at all (even if the attribute is specified).
- StackProbeType::Inline => Some(cstr!("inline-asm")),
+ StackProbeType::Inline => Some(const_cstr!("inline-asm")),
// Flag our internal `__rust_probestack` function as the stack probe symbol.
// This is defined in the `compiler-builtins` crate for each architecture.
- StackProbeType::Call => Some(cstr!("__rust_probestack")),
+ StackProbeType::Call => Some(const_cstr!("__rust_probestack")),
// Pick from the two above based on the LLVM version.
StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
if llvm_util::get_version() < min_llvm_version_for_inline {
- Some(cstr!("__rust_probestack"))
+ Some(const_cstr!("__rust_probestack"))
} else {
- Some(cstr!("inline-asm"))
+ Some(const_cstr!("inline-asm"))
}
}
};
@@ -146,7 +146,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("probe-stack"),
+ const_cstr!("probe-stack"),
attr_value,
);
}
@@ -169,7 +169,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("target-cpu"),
+ const_cstr!("target-cpu"),
target_cpu.as_c_str(),
);
}
@@ -180,7 +180,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("tune-cpu"),
+ const_cstr!("tune-cpu"),
tune_cpu.as_c_str(),
);
}
@@ -289,7 +289,7 @@
Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
}
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
- llvm::AddFunctionAttrString(llfn, Function, cstr!("cmse_nonsecure_entry"));
+ llvm::AddFunctionAttrString(llfn, Function, const_cstr!("cmse_nonsecure_entry"));
}
sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
@@ -319,7 +319,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("target-features"),
+ const_cstr!("target-features"),
&val,
);
}
@@ -332,7 +332,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("wasm-import-module"),
+ const_cstr!("wasm-import-module"),
&module,
);
@@ -342,7 +342,7 @@
llvm::AddFunctionAttrStringValue(
llfn,
llvm::AttributePlace::Function,
- cstr!("wasm-import-name"),
+ const_cstr!("wasm-import-name"),
&name,
);
}
diff --git a/compiler/rustc_codegen_llvm/src/builder.rs b/compiler/rustc_codegen_llvm/src/builder.rs
index f4852c9..d2f4d3e 100644
--- a/compiler/rustc_codegen_llvm/src/builder.rs
+++ b/compiler/rustc_codegen_llvm/src/builder.rs
@@ -5,13 +5,13 @@
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
-use cstr::cstr;
use libc::{c_char, c_uint};
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::MemFlags;
+use rustc_data_structures::const_cstr;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId;
use rustc_middle::ty::layout::TyAndLayout;
@@ -979,7 +979,7 @@
}
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
- let name = cstr!("cleanuppad");
+ let name = const_cstr!("cleanuppad");
let ret = unsafe {
llvm::LLVMRustBuildCleanupPad(
self.llbuilder,
@@ -1003,7 +1003,7 @@
}
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
- let name = cstr!("catchpad");
+ let name = const_cstr!("catchpad");
let ret = unsafe {
llvm::LLVMRustBuildCatchPad(
self.llbuilder,
@@ -1022,7 +1022,7 @@
unwind: Option<&'ll BasicBlock>,
num_handlers: usize,
) -> &'ll Value {
- let name = cstr!("catchswitch");
+ let name = const_cstr!("catchswitch");
let ret = unsafe {
llvm::LLVMRustBuildCatchSwitch(
self.llbuilder,
diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs
index 9904683..16e1a8a 100644
--- a/compiler/rustc_codegen_llvm/src/consts.rs
+++ b/compiler/rustc_codegen_llvm/src/consts.rs
@@ -5,9 +5,9 @@
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
-use cstr::cstr;
use libc::c_uint;
use rustc_codegen_ssa::traits::*;
+use rustc_data_structures::const_cstr;
use rustc_hir::def_id::DefId;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::interpret::{
@@ -419,9 +419,9 @@
.all(|&byte| byte == 0);
let sect_name = if all_bytes_are_zero {
- cstr!("__DATA,__thread_bss")
+ const_cstr!("__DATA,__thread_bss")
} else {
- cstr!("__DATA,__thread_data")
+ const_cstr!("__DATA,__thread_data")
};
llvm::LLVMSetSection(g, sect_name.as_ptr());
}
diff --git a/compiler/rustc_codegen_llvm/src/context.rs b/compiler/rustc_codegen_llvm/src/context.rs
index 3ddc742..ee099f9 100644
--- a/compiler/rustc_codegen_llvm/src/context.rs
+++ b/compiler/rustc_codegen_llvm/src/context.rs
@@ -7,10 +7,10 @@
use crate::type_::Type;
use crate::value::Value;
-use cstr::cstr;
use rustc_codegen_ssa::base::wants_msvc_seh;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::base_n;
+use rustc_data_structures::const_cstr;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::small_c_str::SmallCStr;
use rustc_middle::bug;
@@ -414,8 +414,8 @@
}
fn create_used_variable(&self) {
- let name = cstr!("llvm.used");
- let section = cstr!("llvm.metadata");
+ let name = const_cstr!("llvm.used");
+ let section = const_cstr!("llvm.metadata");
let array =
self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow());
diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
index 85d1b70..1464784 100644
--- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
+++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
@@ -18,8 +18,8 @@
};
use crate::value::Value;
-use cstr::cstr;
use rustc_codegen_ssa::traits::*;
+use rustc_data_structures::const_cstr;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -1075,7 +1075,7 @@
gcov_cu_info.len() as c_uint,
);
- let llvm_gcov_ident = cstr!("llvm.gcov");
+ let llvm_gcov_ident = const_cstr!("llvm.gcov");
llvm::LLVMAddNamedMetadataOperand(
debug_context.llmod,
llvm_gcov_ident.as_ptr(),
@@ -1093,7 +1093,7 @@
);
llvm::LLVMAddNamedMetadataOperand(
debug_context.llmod,
- cstr!("llvm.ident").as_ptr(),
+ const_cstr!("llvm.ident").as_ptr(),
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
);
}
diff --git a/compiler/rustc_data_structures/src/const_cstr.rs b/compiler/rustc_data_structures/src/const_cstr.rs
new file mode 100644
index 0000000..1ebcb87
--- /dev/null
+++ b/compiler/rustc_data_structures/src/const_cstr.rs
@@ -0,0 +1,30 @@
+/// This macro creates a zero-overhead &CStr by adding a NUL terminator to
+/// the string literal passed into it at compile-time. Use it like:
+///
+/// ```
+/// let some_const_cstr = const_cstr!("abc");
+/// ```
+///
+/// The above is roughly equivalent to:
+///
+/// ```
+/// let some_const_cstr = CStr::from_bytes_with_nul(b"abc\0").unwrap()
+/// ```
+///
+/// Note that macro only checks the string literal for internal NULs if
+/// debug-assertions are enabled in order to avoid runtime overhead in release
+/// builds.
+#[macro_export]
+macro_rules! const_cstr {
+ ($s:expr) => {{
+ use std::ffi::CStr;
+
+ let str_plus_nul = concat!($s, "\0");
+
+ if cfg!(debug_assertions) {
+ CStr::from_bytes_with_nul(str_plus_nul.as_bytes()).unwrap()
+ } else {
+ unsafe { CStr::from_bytes_with_nul_unchecked(str_plus_nul.as_bytes()) }
+ }
+ }};
+}
diff --git a/compiler/rustc_data_structures/src/lib.rs b/compiler/rustc_data_structures/src/lib.rs
index fcb2bca..4ab493d 100644
--- a/compiler/rustc_data_structures/src/lib.rs
+++ b/compiler/rustc_data_structures/src/lib.rs
@@ -69,6 +69,7 @@
pub mod binary_search_util;
pub mod box_region;
pub mod captures;
+pub mod const_cstr;
pub mod flock;
pub mod functor;
pub mod fx;
diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs
index 651b478..c1395f3 100644
--- a/src/tools/tidy/src/deps.rs
+++ b/src/tools/tidy/src/deps.rs
@@ -85,7 +85,6 @@
"crossbeam-epoch",
"crossbeam-queue",
"crossbeam-utils",
- "cstr",
"datafrog",
"difference",
"digest",