Skip to content

c_str cleanup #8532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/libextra/rl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub mod rustrt {

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

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

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

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

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

unsafe {
do cb(str::raw::from_c_str(line)) |suggestion| {
do suggestion.to_c_str().with_ref |buf| {
do suggestion.with_c_str |buf| {
rustrt::linenoiseAddCompletion(completions, buf);
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ pub fn WriteOutputFile(sess: Session,
OptLevel: c_int,
EnableSegmentedStacks: bool) {
unsafe {
do Triple.to_c_str().with_ref |Triple| {
do Cpu.to_c_str().with_ref |Cpu| {
do Feature.to_c_str().with_ref |Feature| {
do Output.to_c_str().with_ref |Output| {
do Triple.with_c_str |Triple| {
do Cpu.with_c_str |Cpu| {
do Feature.with_c_str |Feature| {
do Output.with_c_str |Output| {
let result = llvm::LLVMRustWriteOutputFile(
PM,
M,
Expand Down Expand Up @@ -152,7 +152,7 @@ pub mod jit {

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

do path.to_c_str().with_ref |buf_t| {
do path.with_c_str |buf_t| {
if !llvm::LLVMRustLoadCrate(manager, buf_t) {
llvm_err(sess, ~"Could not link");
}
Expand All @@ -171,7 +171,7 @@ pub mod jit {
// Next, we need to get a handle on the _rust_main function by
// looking up it's corresponding ValueRef and then requesting that
// the execution engine compiles the function.
let fun = do "_rust_main".to_c_str().with_ref |entry| {
let fun = do "_rust_main".with_c_str |entry| {
llvm::LLVMGetNamedFunction(m, entry)
};
if fun.is_null() {
Expand Down Expand Up @@ -270,14 +270,14 @@ pub mod write {
output_type_bitcode => {
if opts.optimize != session::No {
let filename = output.with_filetype("no-opt.bc");
do filename.to_c_str().with_ref |buf| {
do filename.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf);
}
}
}
_ => {
let filename = output.with_filetype("bc");
do filename.to_c_str().with_ref |buf| {
do filename.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf);
}
}
Expand Down Expand Up @@ -340,7 +340,7 @@ pub mod write {
// Always output the bitcode file with --save-temps

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

if output_type == output_type_llvm_assembly {
// Given options "-S --emit-llvm": output LLVM assembly
do output.to_c_str().with_ref |buf_o| {
do output.with_c_str |buf_o| {
llvm::LLVMRustAddPrintModulePass(pm.llpm, llmod, buf_o);
}
} else {
// If only a bitcode file is asked for by using the
// '--emit-llvm' flag, then output it here
do output.to_c_str().with_ref |buf| {
do output.with_c_str |buf| {
llvm::LLVMWriteBitcodeToFile(llmod, buf);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/back/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ pub fn populate_pass_manager(sess: Session, pm: &mut PassManager, pass_list:&[~s
}

pub fn create_pass(name:&str) -> Option<PassRef> {
do name.to_c_str().with_ref |s| {
do name.with_c_str |s| {
unsafe {
let p = llvm::LLVMCreatePass(s);
if p.is_null() {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ pub struct TargetData {
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/metadata/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub fn metadata_matches(extern_metas: &[@ast::MetaItem],
fn get_metadata_section(os: os,
filename: &Path) -> Option<@~[u8]> {
unsafe {
let mb = do filename.to_c_str().with_ref |buf| {
let mb = do filename.with_c_str |buf| {
llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf)
};
if mb as int == 0 { return option::None::<@~[u8]>; }
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ pub fn trans_inline_asm(bcx: @mut Block, ia: &ast::inline_asm) -> @mut Block {
ast::asm_intel => lib::llvm::AD_Intel
};

let r = do ia.asm.to_c_str().with_ref |a| {
do constraints.to_c_str().with_ref |c| {
let r = do ia.asm.with_c_str |a| {
do constraints.with_c_str |c| {
InlineAsmCall(bcx, a, c, inputs, output, ia.volatile, ia.alignstack, dialect)
}
};
Expand Down
42 changes: 21 additions & 21 deletions src/librustc/middle/trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<'self> Drop for StatRecorder<'self> {
}

pub fn decl_fn(llmod: ModuleRef, name: &str, cc: lib::llvm::CallConv, ty: Type) -> ValueRef {
let llfn: ValueRef = do name.to_c_str().with_ref |buf| {
let llfn: ValueRef = do name.with_c_str |buf| {
unsafe {
llvm::LLVMGetOrInsertFunction(llmod, buf, ty.to_ref())
}
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn get_extern_const(externs: &mut ExternMap, llmod: ModuleRef,
None => ()
}
unsafe {
let c = do name.to_c_str().with_ref |buf| {
let c = do name.with_c_str |buf| {
llvm::LLVMAddGlobal(llmod, ty.to_ref(), buf)
};
externs.insert(name, c);
Expand Down Expand Up @@ -523,7 +523,7 @@ pub fn get_res_dtor(ccx: @mut CrateContext,
// Structural comparison: a rather involved form of glue.
pub fn maybe_name_value(cx: &CrateContext, v: ValueRef, s: &str) {
if cx.sess.opts.save_temps {
do s.to_c_str().with_ref |buf| {
do s.with_c_str |buf| {
unsafe {
llvm::LLVMSetValueName(v, buf)
}
Expand Down Expand Up @@ -1136,7 +1136,7 @@ pub fn new_block(cx: @mut FunctionContext,
opt_node_info: Option<NodeInfo>)
-> @mut Block {
unsafe {
let llbb = do name.to_c_str().with_ref |buf| {
let llbb = do name.with_c_str |buf| {
llvm::LLVMAppendBasicBlockInContext(cx.ccx.llcx, cx.llfn, buf)
};
let bcx = @mut Block::new(llbb,
Expand Down Expand Up @@ -1553,7 +1553,7 @@ pub struct BasicBlocks {
pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
unsafe {
let cx = task_llcx();
do "static_allocas".to_c_str().with_ref | buf| {
do "static_allocas".with_c_str | buf| {
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
}
}
Expand All @@ -1562,7 +1562,7 @@ pub fn mk_staticallocas_basic_block(llfn: ValueRef) -> BasicBlockRef {
pub fn mk_return_basic_block(llfn: ValueRef) -> BasicBlockRef {
unsafe {
let cx = task_llcx();
do "return".to_c_str().with_ref |buf| {
do "return".with_c_str |buf| {
llvm::LLVMAppendBasicBlockInContext(cx, llfn, buf)
}
}
Expand Down Expand Up @@ -2332,7 +2332,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
};
decl_cdecl_fn(ccx.llmod, main_name, llfty)
};
let llbb = do "top".to_c_str().with_ref |buf| {
let llbb = do "top".with_c_str |buf| {
unsafe {
llvm::LLVMAppendBasicBlockInContext(ccx.llcx, llfn, buf)
}
Expand All @@ -2342,7 +2342,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
llvm::LLVMPositionBuilderAtEnd(bld, llbb);

let crate_map = ccx.crate_map;
let opaque_crate_map = do "crate_map".to_c_str().with_ref |buf| {
let opaque_crate_map = do "crate_map".with_c_str |buf| {
llvm::LLVMBuildPointerCast(bld, crate_map, Type::i8p().to_ref(), buf)
};

Expand All @@ -2360,7 +2360,7 @@ pub fn create_entry_wrapper(ccx: @mut CrateContext,
};

let args = {
let opaque_rust_main = do "rust_main".to_c_str().with_ref |buf| {
let opaque_rust_main = do "rust_main".with_c_str |buf| {
llvm::LLVMBuildPointerCast(bld, rust_main, Type::i8p().to_ref(), buf)
};

Expand Down Expand Up @@ -2442,7 +2442,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {

unsafe {
let llty = llvm::LLVMTypeOf(v);
let g = do sym.to_c_str().with_ref |buf| {
let g = do sym.with_c_str |buf| {
llvm::LLVMAddGlobal(ccx.llmod, llty, buf)
};

Expand Down Expand Up @@ -2475,7 +2475,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {

match (attr::first_attr_value_str_by_name(i.attrs, "link_section")) {
Some(sect) => unsafe {
do sect.to_c_str().with_ref |buf| {
do sect.with_c_str |buf| {
llvm::LLVMSetSection(v, buf);
}
},
Expand Down Expand Up @@ -2516,7 +2516,7 @@ pub fn get_item_val(ccx: @mut CrateContext, id: ast::NodeId) -> ValueRef {
}
ast::foreign_item_static(*) => {
let ident = token::ident_to_str(&ni.ident);
let g = do ident.to_c_str().with_ref |buf| {
let g = do ident.with_c_str |buf| {
unsafe {
let ty = type_of(ccx, ty);
llvm::LLVMAddGlobal(ccx.llmod, ty.to_ref(), buf)
Expand Down Expand Up @@ -2623,7 +2623,7 @@ pub fn trans_constant(ccx: &mut CrateContext, it: @ast::item) {
let s = mangle_exported_name(ccx, p, ty::mk_int()).to_managed();
let disr_val = vi[i].disr_val;
note_unique_llvm_symbol(ccx, s);
let discrim_gvar = do s.to_c_str().with_ref |buf| {
let discrim_gvar = do s.with_c_str |buf| {
unsafe {
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
}
Expand Down Expand Up @@ -2818,7 +2818,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
}

let gc_metadata_name = ~"_gc_module_metadata_" + llmod_id;
let gc_metadata = do gc_metadata_name.to_c_str().with_ref |buf| {
let gc_metadata = do gc_metadata_name.with_c_str |buf| {
unsafe {
llvm::LLVMAddGlobal(ccx.llmod, Type::i32().to_ref(), buf)
}
Expand All @@ -2833,7 +2833,7 @@ pub fn decl_gc_metadata(ccx: &mut CrateContext, llmod_id: &str) {
pub fn create_module_map(ccx: &mut CrateContext) -> ValueRef {
let elttype = Type::struct_([ccx.int_type, ccx.int_type], false);
let maptype = Type::array(&elttype, (ccx.module_data.len() + 1) as u64);
let map = do "_rust_mod_map".to_c_str().with_ref |buf| {
let map = do "_rust_mod_map".with_c_str |buf| {
unsafe {
llvm::LLVMAddGlobal(ccx.llmod, maptype.to_ref(), buf)
}
Expand Down Expand Up @@ -2881,7 +2881,7 @@ pub fn decl_crate_map(sess: session::Session, mapmeta: LinkMeta,
let sym_name = ~"_rust_crate_map_" + mapname;
let arrtype = Type::array(&int_type, n_subcrates as u64);
let maptype = Type::struct_([Type::i32(), Type::i8p(), int_type, arrtype], false);
let map = do sym_name.to_c_str().with_ref |buf| {
let map = do sym_name.with_c_str |buf| {
unsafe {
llvm::LLVMAddGlobal(llmod, maptype.to_ref(), buf)
}
Expand All @@ -2900,7 +2900,7 @@ pub fn fill_crate_map(ccx: @mut CrateContext, map: ValueRef) {
cdata.name,
cstore::get_crate_vers(cstore, i),
cstore::get_crate_hash(cstore, i));
let cr = do nm.to_c_str().with_ref |buf| {
let cr = do nm.with_c_str |buf| {
unsafe {
llvm::LLVMAddGlobal(ccx.llmod, ccx.int_type.to_ref(), buf)
}
Expand Down Expand Up @@ -2963,21 +2963,21 @@ pub fn write_metadata(cx: &mut CrateContext, crate: &ast::Crate) {
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
let llmeta = C_bytes(encoder::encode_metadata(encode_parms, crate));
let llconst = C_struct([llmeta]);
let mut llglobal = do "rust_metadata".to_c_str().with_ref |buf| {
let mut llglobal = do "rust_metadata".with_c_str |buf| {
unsafe {
llvm::LLVMAddGlobal(cx.llmod, val_ty(llconst).to_ref(), buf)
}
};
unsafe {
llvm::LLVMSetInitializer(llglobal, llconst);
do cx.sess.targ_cfg.target_strs.meta_sect_name.to_c_str().with_ref |buf| {
do cx.sess.targ_cfg.target_strs.meta_sect_name.with_c_str |buf| {
llvm::LLVMSetSection(llglobal, buf)
};
lib::llvm::SetLinkage(llglobal, lib::llvm::InternalLinkage);

let t_ptr_i8 = Type::i8p();
llglobal = llvm::LLVMConstBitCast(llglobal, t_ptr_i8.to_ref());
let llvm_used = do "llvm.used".to_c_str().with_ref |buf| {
let llvm_used = do "llvm.used".with_c_str |buf| {
llvm::LLVMAddGlobal(cx.llmod, Type::array(&t_ptr_i8, 1).to_ref(), buf)
};
lib::llvm::SetLinkage(llvm_used, lib::llvm::AppendingLinkage);
Expand All @@ -2991,7 +2991,7 @@ fn mk_global(ccx: &CrateContext,
internal: bool)
-> ValueRef {
unsafe {
let llglobal = do name.to_c_str().with_ref |buf| {
let llglobal = do name.with_c_str |buf| {
llvm::LLVMAddGlobal(ccx.llmod, val_ty(llval).to_ref(), buf)
};
llvm::LLVMSetInitializer(llglobal, llval);
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/trans/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl Builder {
if name.is_empty() {
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), noname())
} else {
do name.to_c_str().with_ref |c| {
do name.with_c_str |c| {
llvm::LLVMBuildAlloca(self.llbuilder, ty.to_ref(), c)
}
}
Expand Down Expand Up @@ -739,7 +739,7 @@ impl Builder {
let sanitized = text.replace("$", "");
let comment_text = fmt!("# %s", sanitized.replace("\n", "\n\t# "));
self.count_insn("inlineasm");
let asm = do comment_text.to_c_str().with_ref |c| {
let asm = do comment_text.with_c_str |c| {
unsafe {
llvm::LLVMConstInlineAsm(Type::func([], &Type::void()).to_ref(),
c, noname(), False, False)
Expand Down Expand Up @@ -895,7 +895,7 @@ impl Builder {
let BB: BasicBlockRef = llvm::LLVMGetInsertBlock(self.llbuilder);
let FN: ValueRef = llvm::LLVMGetBasicBlockParent(BB);
let M: ModuleRef = llvm::LLVMGetGlobalParent(FN);
let T: ValueRef = do "llvm.trap".to_c_str().with_ref |buf| {
let T: ValueRef = do "llvm.trap".with_c_str |buf| {
llvm::LLVMGetNamedFunction(M, buf)
};
assert!((T as int != 0));
Expand Down
Loading