Rollup merge of #82057 - upsuper-forks:cstr, r=davidtwco,wesleywiser

Replace const_cstr with cstr crate

This PR replaces the `const_cstr` macro inside `rustc_data_structures` with `cstr` macro from [cstr](https://crates.io/crates/cstr) crate.

The two macros basically serve the same purpose, which is to generate `&'static CStr` from a string literal. `cstr` is better because it validates the literal at compile time, while the existing `const_cstr` does it at runtime when `debug_assertions` is enabled. In addition, the value `cstr` generates can be used in constant context (which is seemingly not needed anywhere currently, though).
This commit is contained in:
Dylan DPC 2021-02-27 02:34:21 +01:00 committed by GitHub
commit cabe97272d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 42 additions and 60 deletions

View File

@ -904,6 +904,16 @@ dependencies = [
"winapi 0.3.9", "winapi 0.3.9",
] ]
[[package]]
name = "cstr"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
dependencies = [
"proc-macro2",
"quote",
]
[[package]] [[package]]
name = "ctor" name = "ctor"
version = "0.1.15" version = "0.1.15"
@ -3698,6 +3708,7 @@ name = "rustc_codegen_llvm"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"cstr",
"libc", "libc",
"measureme", "measureme",
"rustc-demangle", "rustc-demangle",

View File

@ -10,6 +10,7 @@ doctest = false
[dependencies] [dependencies]
bitflags = "1.0" bitflags = "1.0"
cstr = "0.2"
libc = "0.2" libc = "0.2"
measureme = "9.0.0" measureme = "9.0.0"
snap = "1" snap = "1"

View File

@ -554,7 +554,7 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
llvm::AddCallSiteAttrString( llvm::AddCallSiteAttrString(
callsite, callsite,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
rustc_data_structures::const_cstr!("cmse_nonsecure_call"), cstr::cstr!("cmse_nonsecure_call"),
); );
} }
} }

View File

@ -2,8 +2,8 @@
use std::ffi::CString; use std::ffi::CString;
use cstr::cstr;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_data_structures::const_cstr;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
@ -75,8 +75,8 @@ pub fn set_frame_pointer_elimination(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value)
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("frame-pointer"), cstr!("frame-pointer"),
const_cstr!("all"), cstr!("all"),
); );
} }
} }
@ -95,7 +95,7 @@ fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("instrument-function-entry-inlined"), cstr!("instrument-function-entry-inlined"),
&mcount_name, &mcount_name,
); );
} }
@ -129,16 +129,16 @@ fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
StackProbeType::None => None, StackProbeType::None => None,
// Request LLVM to generate the probes inline. If the given LLVM version does not support // 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). // this, no probe is generated at all (even if the attribute is specified).
StackProbeType::Inline => Some(const_cstr!("inline-asm")), StackProbeType::Inline => Some(cstr!("inline-asm")),
// Flag our internal `__rust_probestack` function as the stack probe symbol. // Flag our internal `__rust_probestack` function as the stack probe symbol.
// This is defined in the `compiler-builtins` crate for each architecture. // This is defined in the `compiler-builtins` crate for each architecture.
StackProbeType::Call => Some(const_cstr!("__rust_probestack")), StackProbeType::Call => Some(cstr!("__rust_probestack")),
// Pick from the two above based on the LLVM version. // Pick from the two above based on the LLVM version.
StackProbeType::InlineOrCall { min_llvm_version_for_inline } => { StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
if llvm_util::get_version() < min_llvm_version_for_inline { if llvm_util::get_version() < min_llvm_version_for_inline {
Some(const_cstr!("__rust_probestack")) Some(cstr!("__rust_probestack"))
} else { } else {
Some(const_cstr!("inline-asm")) Some(cstr!("inline-asm"))
} }
} }
}; };
@ -146,7 +146,7 @@ fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("probe-stack"), cstr!("probe-stack"),
attr_value, attr_value,
); );
} }
@ -169,7 +169,7 @@ pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("target-cpu"), cstr!("target-cpu"),
target_cpu.as_c_str(), target_cpu.as_c_str(),
); );
} }
@ -180,7 +180,7 @@ pub fn apply_tune_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("tune-cpu"), cstr!("tune-cpu"),
tune_cpu.as_c_str(), tune_cpu.as_c_str(),
); );
} }
@ -289,7 +289,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn); Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
} }
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) { if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
llvm::AddFunctionAttrString(llfn, Function, const_cstr!("cmse_nonsecure_entry")); llvm::AddFunctionAttrString(llfn, Function, cstr!("cmse_nonsecure_entry"));
} }
sanitize(cx, codegen_fn_attrs.no_sanitize, llfn); sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
@ -319,7 +319,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("target-features"), cstr!("target-features"),
&val, &val,
); );
} }
@ -332,7 +332,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("wasm-import-module"), cstr!("wasm-import-module"),
&module, &module,
); );
@ -342,7 +342,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
llvm::AddFunctionAttrStringValue( llvm::AddFunctionAttrStringValue(
llfn, llfn,
llvm::AttributePlace::Function, llvm::AttributePlace::Function,
const_cstr!("wasm-import-name"), cstr!("wasm-import-name"),
&name, &name,
); );
} }

View File

@ -5,13 +5,13 @@ use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope};
use crate::type_::Type; use crate::type_::Type;
use crate::type_of::LayoutLlvmExt; use crate::type_of::LayoutLlvmExt;
use crate::value::Value; use crate::value::Value;
use cstr::cstr;
use libc::{c_char, c_uint}; use libc::{c_char, c_uint};
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind}; use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue}; use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
use rustc_codegen_ssa::mir::place::PlaceRef; use rustc_codegen_ssa::mir::place::PlaceRef;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_codegen_ssa::MemFlags; use rustc_codegen_ssa::MemFlags;
use rustc_data_structures::const_cstr;
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::ty::layout::TyAndLayout; use rustc_middle::ty::layout::TyAndLayout;
@ -979,7 +979,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> { fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
let name = const_cstr!("cleanuppad"); let name = cstr!("cleanuppad");
let ret = unsafe { let ret = unsafe {
llvm::LLVMRustBuildCleanupPad( llvm::LLVMRustBuildCleanupPad(
self.llbuilder, self.llbuilder,
@ -1003,7 +1003,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
} }
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> { fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
let name = const_cstr!("catchpad"); let name = cstr!("catchpad");
let ret = unsafe { let ret = unsafe {
llvm::LLVMRustBuildCatchPad( llvm::LLVMRustBuildCatchPad(
self.llbuilder, self.llbuilder,
@ -1022,7 +1022,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
unwind: Option<&'ll BasicBlock>, unwind: Option<&'ll BasicBlock>,
num_handlers: usize, num_handlers: usize,
) -> &'ll Value { ) -> &'ll Value {
let name = const_cstr!("catchswitch"); let name = cstr!("catchswitch");
let ret = unsafe { let ret = unsafe {
llvm::LLVMRustBuildCatchSwitch( llvm::LLVMRustBuildCatchSwitch(
self.llbuilder, self.llbuilder,

View File

@ -5,9 +5,9 @@ use crate::llvm::{self, True};
use crate::type_::Type; use crate::type_::Type;
use crate::type_of::LayoutLlvmExt; use crate::type_of::LayoutLlvmExt;
use crate::value::Value; use crate::value::Value;
use cstr::cstr;
use libc::c_uint; use libc::c_uint;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_data_structures::const_cstr;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::interpret::{ use rustc_middle::mir::interpret::{
@ -419,9 +419,9 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
.all(|&byte| byte == 0); .all(|&byte| byte == 0);
let sect_name = if all_bytes_are_zero { let sect_name = if all_bytes_are_zero {
const_cstr!("__DATA,__thread_bss") cstr!("__DATA,__thread_bss")
} else { } else {
const_cstr!("__DATA,__thread_data") cstr!("__DATA,__thread_data")
}; };
llvm::LLVMSetSection(g, sect_name.as_ptr()); llvm::LLVMSetSection(g, sect_name.as_ptr());
} }

View File

@ -7,10 +7,10 @@ use crate::llvm_util;
use crate::type_::Type; use crate::type_::Type;
use crate::value::Value; use crate::value::Value;
use cstr::cstr;
use rustc_codegen_ssa::base::wants_msvc_seh; use rustc_codegen_ssa::base::wants_msvc_seh;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_data_structures::base_n; use rustc_data_structures::base_n;
use rustc_data_structures::const_cstr;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::small_c_str::SmallCStr; use rustc_data_structures::small_c_str::SmallCStr;
use rustc_middle::bug; use rustc_middle::bug;
@ -414,8 +414,8 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
} }
fn create_used_variable(&self) { fn create_used_variable(&self) {
let name = const_cstr!("llvm.used"); let name = cstr!("llvm.used");
let section = const_cstr!("llvm.metadata"); let section = cstr!("llvm.metadata");
let array = let array =
self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow()); self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow());

View File

@ -18,8 +18,8 @@ use crate::llvm::debuginfo::{
}; };
use crate::value::Value; use crate::value::Value;
use cstr::cstr;
use rustc_codegen_ssa::traits::*; use rustc_codegen_ssa::traits::*;
use rustc_data_structures::const_cstr;
use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@ -1075,7 +1075,7 @@ pub fn compile_unit_metadata(
gcov_cu_info.len() as c_uint, gcov_cu_info.len() as c_uint,
); );
let llvm_gcov_ident = const_cstr!("llvm.gcov"); let llvm_gcov_ident = cstr!("llvm.gcov");
llvm::LLVMAddNamedMetadataOperand( llvm::LLVMAddNamedMetadataOperand(
debug_context.llmod, debug_context.llmod,
llvm_gcov_ident.as_ptr(), llvm_gcov_ident.as_ptr(),
@ -1093,7 +1093,7 @@ pub fn compile_unit_metadata(
); );
llvm::LLVMAddNamedMetadataOperand( llvm::LLVMAddNamedMetadataOperand(
debug_context.llmod, debug_context.llmod,
const_cstr!("llvm.ident").as_ptr(), cstr!("llvm.ident").as_ptr(),
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1), llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
); );
} }

View File

@ -1,30 +0,0 @@
/// 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()) }
}
}};
}

View File

@ -69,7 +69,6 @@ pub mod base_n;
pub mod binary_search_util; pub mod binary_search_util;
pub mod box_region; pub mod box_region;
pub mod captures; pub mod captures;
pub mod const_cstr;
pub mod flock; pub mod flock;
pub mod functor; pub mod functor;
pub mod fx; pub mod fx;

View File

@ -85,6 +85,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
"crossbeam-epoch", "crossbeam-epoch",
"crossbeam-queue", "crossbeam-queue",
"crossbeam-utils", "crossbeam-utils",
"cstr",
"datafrog", "datafrog",
"difference", "difference",
"digest", "digest",