Skip to content

Commit 38e4233

Browse files
committed
Replace const_cstr with cstr crate
1 parent 21cbbdc commit 38e4233

File tree

11 files changed

+42
-60
lines changed

11 files changed

+42
-60
lines changed

Cargo.lock

+11
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,16 @@ dependencies = [
866866
"winapi 0.3.9",
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 = "ctor"
871881
version = "0.1.15"
@@ -3620,6 +3630,7 @@ name = "rustc_codegen_llvm"
36203630
version = "0.0.0"
36213631
dependencies = [
36223632
"bitflags",
3633+
"cstr",
36233634
"libc",
36243635
"measureme",
36253636
"rustc-demangle",

compiler/rustc_codegen_llvm/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ doctest = false
1010

1111
[dependencies]
1212
bitflags = "1.0"
13+
cstr = "0.2"
1314
libc = "0.2"
1415
measureme = "9.0.0"
1516
snap = "1"

compiler/rustc_codegen_llvm/src/abi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl<'tcx> FnAbiLlvmExt<'tcx> for FnAbi<'tcx, Ty<'tcx>> {
554554
llvm::AddCallSiteAttrString(
555555
callsite,
556556
llvm::AttributePlace::Function,
557-
rustc_data_structures::const_cstr!("cmse_nonsecure_call"),
557+
cstr::cstr!("cmse_nonsecure_call"),
558558
);
559559
}
560560
}

compiler/rustc_codegen_llvm/src/attributes.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
33
use std::ffi::CString;
44

5+
use cstr::cstr;
56
use rustc_codegen_ssa::traits::*;
6-
use rustc_data_structures::const_cstr;
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_data_structures::small_c_str::SmallCStr;
99
use rustc_hir::def_id::DefId;
@@ -75,8 +75,8 @@ pub fn set_frame_pointer_elimination(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value)
7575
llvm::AddFunctionAttrStringValue(
7676
llfn,
7777
llvm::AttributePlace::Function,
78-
const_cstr!("frame-pointer"),
79-
const_cstr!("all"),
78+
cstr!("frame-pointer"),
79+
cstr!("all"),
8080
);
8181
}
8282
}
@@ -95,7 +95,7 @@ fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
9595
llvm::AddFunctionAttrStringValue(
9696
llfn,
9797
llvm::AttributePlace::Function,
98-
const_cstr!("instrument-function-entry-inlined"),
98+
cstr!("instrument-function-entry-inlined"),
9999
&mcount_name,
100100
);
101101
}
@@ -129,24 +129,24 @@ fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
129129
StackProbeType::None => None,
130130
// Request LLVM to generate the probes inline. If the given LLVM version does not support
131131
// this, no probe is generated at all (even if the attribute is specified).
132-
StackProbeType::Inline => Some(const_cstr!("inline-asm")),
132+
StackProbeType::Inline => Some(cstr!("inline-asm")),
133133
// Flag our internal `__rust_probestack` function as the stack probe symbol.
134134
// This is defined in the `compiler-builtins` crate for each architecture.
135-
StackProbeType::Call => Some(const_cstr!("__rust_probestack")),
135+
StackProbeType::Call => Some(cstr!("__rust_probestack")),
136136
// Pick from the two above based on the LLVM version.
137137
StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
138138
if llvm_util::get_version() < min_llvm_version_for_inline {
139-
Some(const_cstr!("__rust_probestack"))
139+
Some(cstr!("__rust_probestack"))
140140
} else {
141-
Some(const_cstr!("inline-asm"))
141+
Some(cstr!("inline-asm"))
142142
}
143143
}
144144
};
145145
if let Some(attr_value) = attr_value {
146146
llvm::AddFunctionAttrStringValue(
147147
llfn,
148148
llvm::AttributePlace::Function,
149-
const_cstr!("probe-stack"),
149+
cstr!("probe-stack"),
150150
attr_value,
151151
);
152152
}
@@ -169,7 +169,7 @@ pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
169169
llvm::AddFunctionAttrStringValue(
170170
llfn,
171171
llvm::AttributePlace::Function,
172-
const_cstr!("target-cpu"),
172+
cstr!("target-cpu"),
173173
target_cpu.as_c_str(),
174174
);
175175
}
@@ -180,7 +180,7 @@ pub fn apply_tune_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
180180
llvm::AddFunctionAttrStringValue(
181181
llfn,
182182
llvm::AttributePlace::Function,
183-
const_cstr!("tune-cpu"),
183+
cstr!("tune-cpu"),
184184
tune_cpu.as_c_str(),
185185
);
186186
}
@@ -289,7 +289,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
289289
Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
290290
}
291291
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
292-
llvm::AddFunctionAttrString(llfn, Function, const_cstr!("cmse_nonsecure_entry"));
292+
llvm::AddFunctionAttrString(llfn, Function, cstr!("cmse_nonsecure_entry"));
293293
}
294294
sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
295295

@@ -319,7 +319,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
319319
llvm::AddFunctionAttrStringValue(
320320
llfn,
321321
llvm::AttributePlace::Function,
322-
const_cstr!("target-features"),
322+
cstr!("target-features"),
323323
&val,
324324
);
325325
}
@@ -332,7 +332,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
332332
llvm::AddFunctionAttrStringValue(
333333
llfn,
334334
llvm::AttributePlace::Function,
335-
const_cstr!("wasm-import-module"),
335+
cstr!("wasm-import-module"),
336336
&module,
337337
);
338338

@@ -342,7 +342,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
342342
llvm::AddFunctionAttrStringValue(
343343
llfn,
344344
llvm::AttributePlace::Function,
345-
const_cstr!("wasm-import-name"),
345+
cstr!("wasm-import-name"),
346346
&name,
347347
);
348348
}

compiler/rustc_codegen_llvm/src/builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ use crate::llvm::{AtomicOrdering, AtomicRmwBinOp, SynchronizationScope};
55
use crate::type_::Type;
66
use crate::type_of::LayoutLlvmExt;
77
use crate::value::Value;
8+
use cstr::cstr;
89
use libc::{c_char, c_uint};
910
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, TypeKind};
1011
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
1112
use rustc_codegen_ssa::mir::place::PlaceRef;
1213
use rustc_codegen_ssa::traits::*;
1314
use rustc_codegen_ssa::MemFlags;
14-
use rustc_data_structures::const_cstr;
1515
use rustc_data_structures::small_c_str::SmallCStr;
1616
use rustc_hir::def_id::DefId;
1717
use rustc_middle::ty::layout::TyAndLayout;
@@ -979,7 +979,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
979979
}
980980

981981
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
982-
let name = const_cstr!("cleanuppad");
982+
let name = cstr!("cleanuppad");
983983
let ret = unsafe {
984984
llvm::LLVMRustBuildCleanupPad(
985985
self.llbuilder,
@@ -1003,7 +1003,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10031003
}
10041004

10051005
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1006-
let name = const_cstr!("catchpad");
1006+
let name = cstr!("catchpad");
10071007
let ret = unsafe {
10081008
llvm::LLVMRustBuildCatchPad(
10091009
self.llbuilder,
@@ -1022,7 +1022,7 @@ impl BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10221022
unwind: Option<&'ll BasicBlock>,
10231023
num_handlers: usize,
10241024
) -> &'ll Value {
1025-
let name = const_cstr!("catchswitch");
1025+
let name = cstr!("catchswitch");
10261026
let ret = unsafe {
10271027
llvm::LLVMRustBuildCatchSwitch(
10281028
self.llbuilder,

compiler/rustc_codegen_llvm/src/consts.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use crate::llvm::{self, True};
55
use crate::type_::Type;
66
use crate::type_of::LayoutLlvmExt;
77
use crate::value::Value;
8+
use cstr::cstr;
89
use libc::c_uint;
910
use rustc_codegen_ssa::traits::*;
10-
use rustc_data_structures::const_cstr;
1111
use rustc_hir::def_id::DefId;
1212
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
1313
use rustc_middle::mir::interpret::{
@@ -419,9 +419,9 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
419419
.all(|&byte| byte == 0);
420420

421421
let sect_name = if all_bytes_are_zero {
422-
const_cstr!("__DATA,__thread_bss")
422+
cstr!("__DATA,__thread_bss")
423423
} else {
424-
const_cstr!("__DATA,__thread_data")
424+
cstr!("__DATA,__thread_data")
425425
};
426426
llvm::LLVMSetSection(g, sect_name.as_ptr());
427427
}

compiler/rustc_codegen_llvm/src/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use crate::llvm_util;
77
use crate::type_::Type;
88
use crate::value::Value;
99

10+
use cstr::cstr;
1011
use rustc_codegen_ssa::base::wants_msvc_seh;
1112
use rustc_codegen_ssa::traits::*;
1213
use rustc_data_structures::base_n;
13-
use rustc_data_structures::const_cstr;
1414
use rustc_data_structures::fx::FxHashMap;
1515
use rustc_data_structures::small_c_str::SmallCStr;
1616
use rustc_middle::bug;
@@ -414,8 +414,8 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
414414
}
415415

416416
fn create_used_variable(&self) {
417-
let name = const_cstr!("llvm.used");
418-
let section = const_cstr!("llvm.metadata");
417+
let name = cstr!("llvm.used");
418+
let section = cstr!("llvm.metadata");
419419
let array =
420420
self.const_array(&self.type_ptr_to(self.type_i8()), &*self.used_statics.borrow());
421421

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use crate::llvm::debuginfo::{
1818
};
1919
use crate::value::Value;
2020

21+
use cstr::cstr;
2122
use rustc_codegen_ssa::traits::*;
22-
use rustc_data_structures::const_cstr;
2323
use rustc_data_structures::fingerprint::Fingerprint;
2424
use rustc_data_structures::fx::FxHashMap;
2525
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -1072,7 +1072,7 @@ pub fn compile_unit_metadata(
10721072
gcov_cu_info.len() as c_uint,
10731073
);
10741074

1075-
let llvm_gcov_ident = const_cstr!("llvm.gcov");
1075+
let llvm_gcov_ident = cstr!("llvm.gcov");
10761076
llvm::LLVMAddNamedMetadataOperand(
10771077
debug_context.llmod,
10781078
llvm_gcov_ident.as_ptr(),
@@ -1090,7 +1090,7 @@ pub fn compile_unit_metadata(
10901090
);
10911091
llvm::LLVMAddNamedMetadataOperand(
10921092
debug_context.llmod,
1093-
const_cstr!("llvm.ident").as_ptr(),
1093+
cstr!("llvm.ident").as_ptr(),
10941094
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
10951095
);
10961096
}

compiler/rustc_data_structures/src/const_cstr.rs

-30
This file was deleted.

compiler/rustc_data_structures/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub mod base_n;
7070
pub mod binary_search_util;
7171
pub mod box_region;
7272
pub mod captures;
73-
pub mod const_cstr;
7473
pub mod flock;
7574
pub mod functor;
7675
pub mod fx;

src/tools/tidy/src/deps.rs

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const PERMITTED_DEPENDENCIES: &[&str] = &[
8585
"crossbeam-epoch",
8686
"crossbeam-queue",
8787
"crossbeam-utils",
88+
"cstr",
8889
"datafrog",
8990
"difference",
9091
"digest",

0 commit comments

Comments
 (0)