Skip to content

Commit 680eb71

Browse files
committed
auto merge of #8532 : kballard/rust/cstr-cleanup, r=erickt
Implement interior null checking in `.to_c_str()`, among other changes.
2 parents 8caf517 + 5ca4cdc commit 680eb71

34 files changed

+271
-152
lines changed

src/libextra/rl.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub mod rustrt {
3232

3333
/// Add a line to history
3434
pub unsafe fn add_history(line: &str) -> bool {
35-
do line.to_c_str().with_ref |buf| {
35+
do line.with_c_str |buf| {
3636
rustrt::linenoiseHistoryAdd(buf) == 1 as c_int
3737
}
3838
}
@@ -44,21 +44,21 @@ pub unsafe fn set_history_max_len(len: int) -> bool {
4444

4545
/// Save line history to a file
4646
pub unsafe fn save_history(file: &str) -> bool {
47-
do file.to_c_str().with_ref |buf| {
47+
do file.with_c_str |buf| {
4848
rustrt::linenoiseHistorySave(buf) == 1 as c_int
4949
}
5050
}
5151

5252
/// Load line history from a file
5353
pub unsafe fn load_history(file: &str) -> bool {
54-
do file.to_c_str().with_ref |buf| {
54+
do file.with_c_str |buf| {
5555
rustrt::linenoiseHistoryLoad(buf) == 1 as c_int
5656
}
5757
}
5858

5959
/// Print out a prompt and then wait for input and return it
6060
pub unsafe fn read(prompt: &str) -> Option<~str> {
61-
do prompt.to_c_str().with_ref |buf| {
61+
do prompt.with_c_str |buf| {
6262
let line = rustrt::linenoise(buf);
6363

6464
if line.is_null() { None }
@@ -80,7 +80,7 @@ pub unsafe fn complete(cb: CompletionCb) {
8080

8181
unsafe {
8282
do cb(str::raw::from_c_str(line)) |suggestion| {
83-
do suggestion.to_c_str().with_ref |buf| {
83+
do suggestion.with_c_str |buf| {
8484
rustrt::linenoiseAddCompletion(completions, buf);
8585
}
8686
}

src/librustc/back/link.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ pub fn WriteOutputFile(sess: Session,
7878
OptLevel: c_int,
7979
EnableSegmentedStacks: bool) {
8080
unsafe {
81-
do Triple.to_c_str().with_ref |Triple| {
82-
do Cpu.to_c_str().with_ref |Cpu| {
83-
do Feature.to_c_str().with_ref |Feature| {
84-
do Output.to_c_str().with_ref |Output| {
81+
do Triple.with_c_str |Triple| {
82+
do Cpu.with_c_str |Cpu| {
83+
do Feature.with_c_str |Feature| {
84+
do Output.with_c_str |Output| {
8585
let result = llvm::LLVMRustWriteOutputFile(
8686
PM,
8787
M,
@@ -152,7 +152,7 @@ pub mod jit {
152152

153153
debug!("linking: %s", path);
154154

155-
do path.to_c_str().with_ref |buf_t| {
155+
do path.with_c_str |buf_t| {
156156
if !llvm::LLVMRustLoadCrate(manager, buf_t) {
157157
llvm_err(sess, ~"Could not link");
158158
}
@@ -171,7 +171,7 @@ pub mod jit {
171171
// Next, we need to get a handle on the _rust_main function by
172172
// looking up it's corresponding ValueRef and then requesting that
173173
// the execution engine compiles the function.
174-
let fun = do "_rust_main".to_c_str().with_ref |entry| {
174+
let fun = do "_rust_main".with_c_str |entry| {
175175
llvm::LLVMGetNamedFunction(m, entry)
176176
};
177177
if fun.is_null() {
@@ -270,14 +270,14 @@ pub mod write {
270270
output_type_bitcode => {
271271
if opts.optimize != session::No {
272272
let filename = output.with_filetype("no-opt.bc");
273-
do filename.to_c_str().with_ref |buf| {
273+
do filename.with_c_str |buf| {
274274
llvm::LLVMWriteBitcodeToFile(llmod, buf);
275275
}
276276
}
277277
}
278278
_ => {
279279
let filename = output.with_filetype("bc");
280-
do filename.to_c_str().with_ref |buf| {
280+
do filename.with_c_str |buf| {
281281
llvm::LLVMWriteBitcodeToFile(llmod, buf);
282282
}
283283
}
@@ -340,7 +340,7 @@ pub mod write {
340340
// Always output the bitcode file with --save-temps
341341

342342
let filename = output.with_filetype("opt.bc");
343-
do filename.to_c_str().with_ref |buf| {
343+
do filename.with_c_str |buf| {
344344
llvm::LLVMWriteBitcodeToFile(llmod, buf)
345345
};
346346
// Save the assembly file if -S is used
@@ -401,13 +401,13 @@ pub mod write {
401401

402402
if output_type == output_type_llvm_assembly {
403403
// Given options "-S --emit-llvm": output LLVM assembly
404-
do output.to_c_str().with_ref |buf_o| {
404+
do output.with_c_str |buf_o| {
405405
llvm::LLVMRustAddPrintModulePass(pm.llpm, llmod, buf_o);
406406
}
407407
} else {
408408
// If only a bitcode file is asked for by using the
409409
// '--emit-llvm' flag, then output it here
410-
do output.to_c_str().with_ref |buf| {
410+
do output.with_c_str |buf| {
411411
llvm::LLVMWriteBitcodeToFile(llmod, buf);
412412
}
413413
}

src/librustc/back/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ pub fn populate_pass_manager(sess: Session, pm: &mut PassManager, pass_list:&[~s
173173
}
174174

175175
pub fn create_pass(name:&str) -> Option<PassRef> {
176-
do name.to_c_str().with_ref |s| {
176+
do name.with_c_str |s| {
177177
unsafe {
178178
let p = llvm::LLVMCreatePass(s);
179179
if p.is_null() {

src/librustc/lib/llvm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2271,7 +2271,7 @@ pub struct TargetData {
22712271
}
22722272

22732273
pub fn mk_target_data(string_rep: &str) -> TargetData {
2274-
let lltd = do string_rep.to_c_str().with_ref |buf| {
2274+
let lltd = do string_rep.with_c_str |buf| {
22752275
unsafe { llvm::LLVMCreateTargetData(buf) }
22762276
};
22772277

src/librustc/metadata/loader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ pub fn metadata_matches(extern_metas: &[@ast::MetaItem],
199199
fn get_metadata_section(os: os,
200200
filename: &Path) -> Option<@~[u8]> {
201201
unsafe {
202-
let mb = do filename.to_c_str().with_ref |buf| {
202+
let mb = do filename.with_c_str |buf| {
203203
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
204204
};
205205
if mb as int == 0 { return option::None::<@~[u8]>; }

src/librustc/middle/trans/asm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ pub fn trans_inline_asm(bcx: @mut Block, ia: &ast::inline_asm) -> @mut Block {
120120
ast::asm_intel => lib::llvm::AD_Intel
121121
};
122122

123-
let r = do ia.asm.to_c_str().with_ref |a| {
124-
do constraints.to_c_str().with_ref |c| {
123+
let r = do ia.asm.with_c_str |a| {
124+
do constraints.with_c_str |c| {
125125
InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
126126
}
127127
};

src/librustc/middle/trans/base.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'self> Drop for StatRecorder<'self> {
181181
}
182182

183183
pub fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv, ty: Type) -> ValueRef {
184-
let llfn: ValueRef = do name.to_c_str().with_ref |buf| {
184+
let llfn: ValueRef = do name.with_c_str |buf| {
185185
unsafe {
186186
llvm::LLVMGetOrInsertFunction(llmod, buf, ty.to_ref())
187187
}
@@ -221,7 +221,7 @@ pub fn get_extern_const(externs: &mut ExternMap, llmod: ModuleRef,
221221
None => ()
222222
}
223223
unsafe {
224-
let c = do name.to_c_str().with_ref |buf| {
224+
let c = do name.with_c_str |buf| {
225225
llvm::LLVMAddGlobal(llmod, ty.to_ref(), buf)
226226
};
227227
externs.insert(name, c);
@@ -523,7 +523,7 @@ pub fn get_res_dtor(ccx: @mut CrateContext,
523523
// Structural comparison: a rather involved form of glue.
524524
pub fn maybe_name_value(cx: &CrateContext, v: ValueRef, s: &str) {
525525
if cx.sess.opts.save_temps {
526-
do s.to_c_str().with_ref |buf| {
526+
do s.with_c_str |buf| {
527527
unsafe {
528528
llvm::LLVMSetValueName(v, buf)
529529
}
@@ -1136,7 +1136,7 @@ pub fn new_block(cx: @mut FunctionContext,
11361136
opt_node_info: Option<NodeInfo>)
11371137
-> @mut Block {
11381138
unsafe {
1139-
let llbb = do name.to_c_str().with_ref |buf| {
1139+
let llbb = do name.with_c_str |buf| {
11401140
llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf)
11411141
};
11421142
let bcx = @mut Block::new(llbb,
@@ -1553,7 +1553,7 @@ pub struct BasicBlocks {
15531553
pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
15541554
unsafe {
15551555
let cx = task_llcx();
1556-
do "static_allocas".to_c_str().with_ref | buf| {
1556+
do "static_allocas".with_c_str | buf| {
15571557
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
15581558
}
15591559
}
@@ -1562,7 +1562,7 @@ pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
15621562
pub fn mk_return_basic_block(llfn: ValueRef) -> BasicBlockRef {
15631563
unsafe {
15641564
let cx = task_llcx();
1565-
do "return".to_c_str().with_ref |buf| {
1565+
do "return".with_c_str |buf| {
15661566
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
15671567
}
15681568
}
@@ -2328,7 +2328,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23282328
};
23292329
decl_cdecl_fn(ccx.llmod, main_name, llfty)
23302330
};
2331-
let llbb = do "top".to_c_str().with_ref |buf| {
2331+
let llbb = do "top".with_c_str |buf| {
23322332
unsafe {
23332333
llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
23342334
}
@@ -2338,7 +2338,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23382338
llvm::LLVMPositionBuilderAtEnd(bld, llbb);
23392339

23402340
let crate_map = ccx.crate_map;
2341-
let opaque_crate_map = do "crate_map".to_c_str().with_ref |buf| {
2341+
let opaque_crate_map = do "crate_map".with_c_str |buf| {
23422342
llvm::LLVMBuildPointerCast(bld, crate_map, Type::i8p().to_ref(), buf)
23432343
};
23442344

@@ -2356,7 +2356,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
23562356
};
23572357

23582358
let args = {
2359-
let opaque_rust_main = do "rust_main".to_c_str().with_ref |buf| {
2359+
let opaque_rust_main = do "rust_main".with_c_str |buf| {
23602360
llvm::LLVMBuildPointerCast(bld, rust_main, Type::i8p().to_ref(), buf)
23612361
};
23622362

@@ -2438,7 +2438,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
24382438

24392439
unsafe {
24402440
let llty = llvm::LLVMTypeOf(v);
2441-
let g = do sym.to_c_str().with_ref |buf| {
2441+
let g = do sym.with_c_str |buf| {
24422442
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
24432443
};
24442444

@@ -2471,7 +2471,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
24712471

24722472
match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
24732473
Some(sect) => unsafe {
2474-
do sect.to_c_str().with_ref |buf| {
2474+
do sect.with_c_str |buf| {
24752475
llvm::LLVMSetSection(v, buf);
24762476
}
24772477
},
@@ -2512,7 +2512,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
25122512
}
25132513
ast::foreign_item_static(*) => {
25142514
let ident = token::ident_to_str(&ni.ident);
2515-
let g = do ident.to_c_str().with_ref |buf| {
2515+
let g = do ident.with_c_str |buf| {
25162516
unsafe {
25172517
let ty = type_of(ccx, ty);
25182518
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
@@ -2619,7 +2619,7 @@ pub fn trans_constant(ccx: &mut CrateContext, it: @ast::item) {
26192619
let s = mangle_exported_name(ccx, p, ty::mk_int()).to_managed();
26202620
let disr_val = vi[i].disr_val;
26212621
note_unique_llvm_symbol(ccx, s);
2622-
let discrim_gvar = do s.to_c_str().with_ref |buf| {
2622+
let discrim_gvar = do s.with_c_str |buf| {
26232623
unsafe {
26242624
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
26252625
}
@@ -2814,7 +2814,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
28142814
}
28152815

28162816
let gc_metadata_name = ~"_gc_module_metadata_" + llmod_id;
2817-
let gc_metadata = do gc_metadata_name.to_c_str().with_ref |buf| {
2817+
let gc_metadata = do gc_metadata_name.with_c_str |buf| {
28182818
unsafe {
28192819
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
28202820
}
@@ -2829,7 +2829,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
28292829
pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef {
28302830
let elttype = Type::struct_([ccx.int_type, ccx.int_type], false);
28312831
let maptype = Type::array(&elttype, (ccx.module_data.len() + 1) as u64);
2832-
let map = do "_rust_mod_map".to_c_str().with_ref |buf| {
2832+
let map = do "_rust_mod_map".with_c_str |buf| {
28332833
unsafe {
28342834
llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf)
28352835
}
@@ -2877,7 +2877,7 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
28772877
let sym_name = ~"_rust_crate_map_" + mapname;
28782878
let arrtype = Type::array(&int_type, n_subcrates as u64);
28792879
let maptype = Type::struct_([Type::i32(), Type::i8p(), int_type, arrtype], false);
2880-
let map = do sym_name.to_c_str().with_ref |buf| {
2880+
let map = do sym_name.with_c_str |buf| {
28812881
unsafe {
28822882
llvm::LLVMAddGlobal(llmod, maptype.to_ref(), buf)
28832883
}
@@ -2896,7 +2896,7 @@ pub fn fill_crate_map(ccx: @mut CrateContext, map: ValueRef) {
28962896
cdata.name,
28972897
cstore::get_crate_vers(cstore, i),
28982898
cstore::get_crate_hash(cstore, i));
2899-
let cr = do nm.to_c_str().with_ref |buf| {
2899+
let cr = do nm.with_c_str |buf| {
29002900
unsafe {
29012901
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
29022902
}
@@ -2959,21 +2959,21 @@ pub fn write_metadata(cx: &mut CrateContext, crate: &ast::Crate) {
29592959
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
29602960
let llmeta = C_bytes(encoder::encode_metadata(encode_parms, crate));
29612961
let llconst = C_struct([llmeta]);
2962-
let mut llglobal = do "rust_metadata".to_c_str().with_ref |buf| {
2962+
let mut llglobal = do "rust_metadata".with_c_str |buf| {
29632963
unsafe {
29642964
llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst).to_ref(), buf)
29652965
}
29662966
};
29672967
unsafe {
29682968
llvm::LLVMSetInitializer(llglobal, llconst);
2969-
do cx.sess.targ_cfg.target_strs.meta_sect_name.to_c_str().with_ref |buf| {
2969+
do cx.sess.targ_cfg.target_strs.meta_sect_name.with_c_str |buf| {
29702970
llvm::LLVMSetSection(llglobal, buf)
29712971
};
29722972
lib::llvm::SetLinkage(llglobal, lib::llvm::InternalLinkage);
29732973

29742974
let t_ptr_i8 = Type::i8p();
29752975
llglobal = llvm::LLVMConstBitCast(llglobal, t_ptr_i8.to_ref());
2976-
let llvm_used = do "llvm.used".to_c_str().with_ref |buf| {
2976+
let llvm_used = do "llvm.used".with_c_str |buf| {
29772977
llvm::LLVMAddGlobal(cx.llmod, Type::array(&t_ptr_i8, 1).to_ref(), buf)
29782978
};
29792979
lib::llvm::SetLinkage(llvm_used, lib::llvm::AppendingLinkage);
@@ -2987,7 +2987,7 @@ fn mk_global(ccx: &CrateContext,
29872987
internal: bool)
29882988
-> ValueRef {
29892989
unsafe {
2990-
let llglobal = do name.to_c_str().with_ref |buf| {
2990+
let llglobal = do name.with_c_str |buf| {
29912991
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llval).to_ref(), buf)
29922992
};
29932993
llvm::LLVMSetInitializer(llglobal, llval);

src/librustc/middle/trans/builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl Builder {
423423
if name.is_empty() {
424424
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
425425
} else {
426-
do name.to_c_str().with_ref |c| {
426+
do name.with_c_str |c| {
427427
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
428428
}
429429
}
@@ -739,7 +739,7 @@ impl Builder {
739739
let sanitized = text.replace("$", "");
740740
let comment_text = fmt!("# %s", sanitized.replace("\n", "\n\t# "));
741741
self.count_insn("inlineasm");
742-
let asm = do comment_text.to_c_str().with_ref |c| {
742+
let asm = do comment_text.with_c_str |c| {
743743
unsafe {
744744
llvm::LLVMConstInlineAsm(Type::func([], &Type::void()).to_ref(),
745745
c, noname(), False, False)
@@ -895,7 +895,7 @@ impl Builder {
895895
let BB: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
896896
let FN: ValueRef = llvm::LLVMGetBasicBlockParent(BB);
897897
let M: ModuleRef = llvm::LLVMGetGlobalParent(FN);
898-
let T: ValueRef = do "llvm.trap".to_c_str().with_ref |buf| {
898+
let T: ValueRef = do "llvm.trap".with_c_str |buf| {
899899
llvm::LLVMGetNamedFunction(M, buf)
900900
};
901901
assert!((T as int != 0));

0 commit comments

Comments
 (0)