Skip to content

Commit 88d84b3

Browse files
Introduce SmallCStr and use it where applicable.
1 parent 9585c5d commit 88d84b3

File tree

13 files changed

+200
-75
lines changed

13 files changed

+200
-75
lines changed

src/librustc_codegen_llvm/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
128128
llvm::AddFunctionAttrStringValue(
129129
llfn,
130130
llvm::AttributePlace::Function,
131-
cstr("target-cpu\0"),
131+
const_cstr!("target-cpu"),
132132
target_cpu.as_c_str());
133133
}
134134

src/librustc_codegen_llvm/back/write.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc::hir::def_id::{CrateNum, LOCAL_CRATE};
3131
use rustc::ty::TyCtxt;
3232
use rustc::util::common::{time_ext, time_depth, set_time_depth, print_time_passes_entry};
3333
use rustc_fs_util::{path2cstr, link_or_copy};
34+
use rustc_data_structures::small_c_str::SmallCStr;
3435
use errors::{self, Handler, Level, DiagnosticBuilder, FatalError, DiagnosticId};
3536
use errors::emitter::{Emitter};
3637
use syntax::attr;
@@ -170,11 +171,8 @@ pub fn target_machine_factory(sess: &Session, find_features: bool)
170171

171172
let singlethread = sess.target.target.options.singlethread;
172173

173-
let triple = &sess.target.target.llvm_target;
174-
175-
let triple = CString::new(triple.as_bytes()).unwrap();
176-
let cpu = sess.target_cpu();
177-
let cpu = CString::new(cpu.as_bytes()).unwrap();
174+
let triple = SmallCStr::new(&sess.target.target.llvm_target);
175+
let cpu = SmallCStr::new(sess.target_cpu());
178176
let features = attributes::llvm_target_features(sess)
179177
.collect::<Vec<_>>()
180178
.join(",");
@@ -522,7 +520,7 @@ unsafe fn optimize(cgcx: &CodegenContext,
522520
// If we're verifying or linting, add them to the function pass
523521
// manager.
524522
let addpass = |pass_name: &str| {
525-
let pass_name = CString::new(pass_name).unwrap();
523+
let pass_name = SmallCStr::new(pass_name);
526524
let pass = match llvm::LLVMRustFindAndCreatePass(pass_name.as_ptr()) {
527525
Some(pass) => pass,
528526
None => return false,

src/librustc_codegen_llvm/base.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ use type_::Type;
7373
use type_of::LayoutLlvmExt;
7474
use rustc::util::nodemap::{FxHashMap, FxHashSet, DefIdSet};
7575
use CrateInfo;
76+
use rustc_data_structures::small_c_str::SmallCStr;
7677
use rustc_data_structures::sync::Lrc;
7778

7879
use std::any::Any;
@@ -533,7 +534,7 @@ pub fn set_link_section(llval: &Value, attrs: &CodegenFnAttrs) {
533534
None => return,
534535
};
535536
unsafe {
536-
let buf = CString::new(sect.as_str().as_bytes()).unwrap();
537+
let buf = SmallCStr::new(&sect.as_str());
537538
llvm::LLVMSetSection(llval, buf.as_ptr());
538539
}
539540
}
@@ -681,7 +682,7 @@ fn write_metadata<'a, 'gcx>(tcx: TyCtxt<'a, 'gcx, 'gcx>,
681682
unsafe {
682683
llvm::LLVMSetInitializer(llglobal, llconst);
683684
let section_name = metadata::metadata_section_name(&tcx.sess.target.target);
684-
let name = CString::new(section_name).unwrap();
685+
let name = SmallCStr::new(section_name);
685686
llvm::LLVMSetSection(llglobal, name.as_ptr());
686687

687688
// Also generate a .section directive to force no

src/librustc_codegen_llvm/builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ use libc::{c_uint, c_char};
1818
use rustc::ty::TyCtxt;
1919
use rustc::ty::layout::{Align, Size};
2020
use rustc::session::{config, Session};
21+
use rustc_data_structures::small_c_str::SmallCStr;
2122

2223
use std::borrow::Cow;
23-
use std::ffi::CString;
2424
use std::ops::Range;
2525
use std::ptr;
2626

@@ -58,7 +58,7 @@ impl Builder<'a, 'll, 'tcx> {
5858
pub fn new_block<'b>(cx: &'a CodegenCx<'ll, 'tcx>, llfn: &'ll Value, name: &'b str) -> Self {
5959
let bx = Builder::with_cx(cx);
6060
let llbb = unsafe {
61-
let name = CString::new(name).unwrap();
61+
let name = SmallCStr::new(name);
6262
llvm::LLVMAppendBasicBlockInContext(
6363
cx.llcx,
6464
llfn,
@@ -118,7 +118,7 @@ impl Builder<'a, 'll, 'tcx> {
118118
}
119119

120120
pub fn set_value_name(&self, value: &'ll Value, name: &str) {
121-
let cname = CString::new(name.as_bytes()).unwrap();
121+
let cname = SmallCStr::new(name);
122122
unsafe {
123123
llvm::LLVMSetValueName(value, cname.as_ptr());
124124
}
@@ -436,7 +436,7 @@ impl Builder<'a, 'll, 'tcx> {
436436
let alloca = if name.is_empty() {
437437
llvm::LLVMBuildAlloca(self.llbuilder, ty, noname())
438438
} else {
439-
let name = CString::new(name).unwrap();
439+
let name = SmallCStr::new(name);
440440
llvm::LLVMBuildAlloca(self.llbuilder, ty,
441441
name.as_ptr())
442442
};

src/librustc_codegen_llvm/context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use type_::Type;
2626
use type_of::PointeeInfo;
2727

2828
use rustc_data_structures::base_n;
29+
use rustc_data_structures::small_c_str::SmallCStr;
2930
use rustc::mir::mono::Stats;
3031
use rustc::session::config::{self, DebugInfo};
3132
use rustc::session::Session;
@@ -34,7 +35,7 @@ use rustc::ty::{self, Ty, TyCtxt};
3435
use rustc::util::nodemap::FxHashMap;
3536
use rustc_target::spec::{HasTargetSpec, Target};
3637

37-
use std::ffi::{CStr, CString};
38+
use std::ffi::CStr;
3839
use std::cell::{Cell, RefCell};
3940
use std::iter;
4041
use std::str;
@@ -161,7 +162,7 @@ pub unsafe fn create_module(
161162
llcx: &'ll llvm::Context,
162163
mod_name: &str,
163164
) -> &'ll llvm::Module {
164-
let mod_name = CString::new(mod_name).unwrap();
165+
let mod_name = SmallCStr::new(mod_name);
165166
let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
166167

167168
// Ensure the data-layout values hardcoded remain the defaults.
@@ -201,11 +202,10 @@ pub unsafe fn create_module(
201202
}
202203
}
203204

204-
let data_layout = CString::new(&sess.target.target.data_layout[..]).unwrap();
205+
let data_layout = SmallCStr::new(&sess.target.target.data_layout);
205206
llvm::LLVMSetDataLayout(llmod, data_layout.as_ptr());
206207

207-
let llvm_target = sess.target.target.llvm_target.as_bytes();
208-
let llvm_target = CString::new(llvm_target).unwrap();
208+
let llvm_target = SmallCStr::new(&sess.target.target.llvm_target);
209209
llvm::LLVMRustSetNormalizedTarget(llmod, llvm_target.as_ptr());
210210

211211
if is_pie_binary(sess) {

src/librustc_codegen_llvm/debuginfo/metadata.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use rustc::ty::layout::{self, Align, LayoutOf, PrimitiveExt, Size, TyLayout};
3737
use rustc::session::config;
3838
use rustc::util::nodemap::FxHashMap;
3939
use rustc_fs_util::path2cstr;
40+
use rustc_data_structures::small_c_str::SmallCStr;
4041

4142
use libc::{c_uint, c_longlong};
4243
use std::ffi::CString;
@@ -274,7 +275,7 @@ impl RecursiveTypeDescription<'ll, 'tcx> {
274275
// ... and attach them to the stub to complete it.
275276
set_members_of_composite_type(cx,
276277
metadata_stub,
277-
&member_descriptions[..]);
278+
member_descriptions);
278279
return MetadataCreationResult::new(metadata_stub, true);
279280
}
280281
}
@@ -349,7 +350,7 @@ fn vec_slice_metadata(
349350
let (pointer_size, pointer_align) = cx.size_and_align_of(data_ptr_type);
350351
let (usize_size, usize_align) = cx.size_and_align_of(cx.tcx.types.usize);
351352

352-
let member_descriptions = [
353+
let member_descriptions = vec![
353354
MemberDescription {
354355
name: "data_ptr".to_string(),
355356
type_metadata: data_ptr_metadata,
@@ -374,7 +375,7 @@ fn vec_slice_metadata(
374375
slice_ptr_type,
375376
&slice_type_name[..],
376377
unique_type_id,
377-
&member_descriptions,
378+
member_descriptions,
378379
NO_SCOPE_METADATA,
379380
file_metadata,
380381
span);
@@ -460,7 +461,7 @@ fn trait_pointer_metadata(
460461

461462
let data_ptr_field = layout.field(cx, 0);
462463
let vtable_field = layout.field(cx, 1);
463-
let member_descriptions = [
464+
let member_descriptions = vec![
464465
MemberDescription {
465466
name: "pointer".to_string(),
466467
type_metadata: type_metadata(cx,
@@ -485,7 +486,7 @@ fn trait_pointer_metadata(
485486
trait_object_type,
486487
&trait_type_name[..],
487488
unique_type_id,
488-
&member_descriptions,
489+
member_descriptions,
489490
containing_scope,
490491
file_metadata,
491492
syntax_pos::DUMMY_SP)
@@ -746,8 +747,8 @@ fn file_metadata_raw(cx: &CodegenCx<'ll, '_>,
746747

747748
debug!("file_metadata: file_name: {}, directory: {}", file_name, directory);
748749

749-
let file_name = CString::new(file_name).unwrap();
750-
let directory = CString::new(directory).unwrap();
750+
let file_name = SmallCStr::new(file_name);
751+
let directory = SmallCStr::new(directory);
751752

752753
let file_metadata = unsafe {
753754
llvm::LLVMRustDIBuilderCreateFile(DIB(cx),
@@ -782,7 +783,7 @@ fn basic_type_metadata(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'ll DIType {
782783
};
783784

784785
let (size, align) = cx.size_and_align_of(t);
785-
let name = CString::new(name).unwrap();
786+
let name = SmallCStr::new(name);
786787
let ty_metadata = unsafe {
787788
llvm::LLVMRustDIBuilderCreateBasicType(
788789
DIB(cx),
@@ -813,7 +814,7 @@ fn pointer_type_metadata(
813814
) -> &'ll DIType {
814815
let (pointer_size, pointer_align) = cx.size_and_align_of(pointer_type);
815816
let name = compute_debuginfo_type_name(cx, pointer_type, false);
816-
let name = CString::new(name).unwrap();
817+
let name = SmallCStr::new(&name);
817818
unsafe {
818819
llvm::LLVMRustDIBuilderCreatePointerType(
819820
DIB(cx),
@@ -847,9 +848,9 @@ pub fn compile_unit_metadata(tcx: TyCtxt,
847848
let producer = format!("clang LLVM (rustc version {})",
848849
(option_env!("CFG_VERSION")).expect("CFG_VERSION"));
849850

850-
let name_in_debuginfo = name_in_debuginfo.to_string_lossy().into_owned();
851-
let name_in_debuginfo = CString::new(name_in_debuginfo).unwrap();
852-
let work_dir = CString::new(&tcx.sess.working_dir.0.to_string_lossy()[..]).unwrap();
851+
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
852+
let name_in_debuginfo = SmallCStr::new(&name_in_debuginfo);
853+
let work_dir = SmallCStr::new(&tcx.sess.working_dir.0.to_string_lossy());
853854
let producer = CString::new(producer).unwrap();
854855
let flags = "\0";
855856
let split_name = "\0";
@@ -1187,7 +1188,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
11871188

11881189
set_members_of_composite_type(cx,
11891190
variant_type_metadata,
1190-
&member_descriptions[..]);
1191+
member_descriptions);
11911192
vec![
11921193
MemberDescription {
11931194
name: "".to_string(),
@@ -1217,7 +1218,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
12171218

12181219
set_members_of_composite_type(cx,
12191220
variant_type_metadata,
1220-
&member_descriptions);
1221+
member_descriptions);
12211222
MemberDescription {
12221223
name: "".to_string(),
12231224
type_metadata: variant_type_metadata,
@@ -1244,7 +1245,7 @@ impl EnumMemberDescriptionFactory<'ll, 'tcx> {
12441245

12451246
set_members_of_composite_type(cx,
12461247
variant_type_metadata,
1247-
&variant_member_descriptions[..]);
1248+
variant_member_descriptions);
12481249

12491250
// Encode the information about the null variant in the union
12501251
// member's name.
@@ -1416,8 +1417,7 @@ fn prepare_enum_metadata(
14161417
let enumerators_metadata: Vec<_> = def.discriminants(cx.tcx)
14171418
.zip(&def.variants)
14181419
.map(|(discr, v)| {
1419-
let token = v.name.as_str();
1420-
let name = CString::new(token.as_bytes()).unwrap();
1420+
let name = SmallCStr::new(&v.name.as_str());
14211421
unsafe {
14221422
Some(llvm::LLVMRustDIBuilderCreateEnumerator(
14231423
DIB(cx),
@@ -1442,7 +1442,7 @@ fn prepare_enum_metadata(
14421442
type_metadata(cx, discr.to_ty(cx.tcx), syntax_pos::DUMMY_SP);
14431443
let discriminant_name = get_enum_discriminant_name(cx, enum_def_id).as_str();
14441444

1445-
let name = CString::new(discriminant_name.as_bytes()).unwrap();
1445+
let name = SmallCStr::new(&discriminant_name);
14461446
let discriminant_type_metadata = unsafe {
14471447
llvm::LLVMRustDIBuilderCreateEnumerationType(
14481448
DIB(cx),
@@ -1482,10 +1482,10 @@ fn prepare_enum_metadata(
14821482

14831483
let (enum_type_size, enum_type_align) = layout.size_and_align();
14841484

1485-
let enum_name = CString::new(enum_name).unwrap();
1486-
let unique_type_id_str = CString::new(
1487-
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1488-
).unwrap();
1485+
let enum_name = SmallCStr::new(&enum_name);
1486+
let unique_type_id_str = SmallCStr::new(
1487+
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id)
1488+
);
14891489
let enum_metadata = unsafe {
14901490
llvm::LLVMRustDIBuilderCreateUnionType(
14911491
DIB(cx),
@@ -1531,7 +1531,7 @@ fn composite_type_metadata(
15311531
composite_type: Ty<'tcx>,
15321532
composite_type_name: &str,
15331533
composite_type_unique_id: UniqueTypeId,
1534-
member_descriptions: &[MemberDescription<'ll>],
1534+
member_descriptions: Vec<MemberDescription<'ll>>,
15351535
containing_scope: Option<&'ll DIScope>,
15361536

15371537
// Ignore source location information as long as it
@@ -1555,7 +1555,7 @@ fn composite_type_metadata(
15551555

15561556
fn set_members_of_composite_type(cx: &CodegenCx<'ll, '_>,
15571557
composite_type_metadata: &'ll DICompositeType,
1558-
member_descriptions: &[MemberDescription<'ll>]) {
1558+
member_descriptions: Vec<MemberDescription<'ll>>) {
15591559
// In some rare cases LLVM metadata uniquing would lead to an existing type
15601560
// description being used instead of a new one created in
15611561
// create_struct_stub. This would cause a hard to trace assertion in
@@ -1574,10 +1574,9 @@ fn set_members_of_composite_type(cx: &CodegenCx<'ll, '_>,
15741574
}
15751575

15761576
let member_metadata: Vec<_> = member_descriptions
1577-
.iter()
1577+
.into_iter()
15781578
.map(|member_description| {
1579-
let member_name = member_description.name.as_bytes();
1580-
let member_name = CString::new(member_name).unwrap();
1579+
let member_name = CString::new(member_description.name).unwrap();
15811580
unsafe {
15821581
Some(llvm::LLVMRustDIBuilderCreateMemberType(
15831582
DIB(cx),
@@ -1613,10 +1612,10 @@ fn create_struct_stub(
16131612
) -> &'ll DICompositeType {
16141613
let (struct_size, struct_align) = cx.size_and_align_of(struct_type);
16151614

1616-
let name = CString::new(struct_type_name).unwrap();
1617-
let unique_type_id = CString::new(
1618-
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1619-
).unwrap();
1615+
let name = SmallCStr::new(struct_type_name);
1616+
let unique_type_id = SmallCStr::new(
1617+
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id)
1618+
);
16201619
let metadata_stub = unsafe {
16211620
// LLVMRustDIBuilderCreateStructType() wants an empty array. A null
16221621
// pointer will lead to hard to trace and debug LLVM assertions
@@ -1651,10 +1650,10 @@ fn create_union_stub(
16511650
) -> &'ll DICompositeType {
16521651
let (union_size, union_align) = cx.size_and_align_of(union_type);
16531652

1654-
let name = CString::new(union_type_name).unwrap();
1655-
let unique_type_id = CString::new(
1656-
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id).as_bytes()
1657-
).unwrap();
1653+
let name = SmallCStr::new(union_type_name);
1654+
let unique_type_id = SmallCStr::new(
1655+
debug_context(cx).type_map.borrow().get_unique_type_id_as_string(unique_type_id)
1656+
);
16581657
let metadata_stub = unsafe {
16591658
// LLVMRustDIBuilderCreateUnionType() wants an empty array. A null
16601659
// pointer will lead to hard to trace and debug LLVM assertions
@@ -1713,13 +1712,12 @@ pub fn create_global_var_metadata(
17131712
let is_local_to_unit = is_node_local_to_unit(cx, def_id);
17141713
let variable_type = Instance::mono(cx.tcx, def_id).ty(cx.tcx);
17151714
let type_metadata = type_metadata(cx, variable_type, span);
1716-
let var_name = tcx.item_name(def_id).to_string();
1717-
let var_name = CString::new(var_name).unwrap();
1715+
let var_name = SmallCStr::new(&tcx.item_name(def_id).as_str());
17181716
let linkage_name = if no_mangle {
17191717
None
17201718
} else {
17211719
let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id));
1722-
Some(CString::new(linkage_name.to_string()).unwrap())
1720+
Some(SmallCStr::new(&linkage_name.as_str()))
17231721
};
17241722

17251723
let global_align = cx.align_of(variable_type);

0 commit comments

Comments
 (0)