Skip to content

Commit 7ce2630

Browse files
committed
stop asking LLVM to null-terminate strings
This was missed when dropping the null-termination from our string types. An explicit null byte can still be placed anywhere in a string if desired, but there's no reason to stick one at the end of every string constant.
1 parent 7bda3df commit 7ce2630

File tree

6 files changed

+17
-17
lines changed

6 files changed

+17
-17
lines changed

src/librustc/middle/trans/common.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ pub fn C_u8(ccx: &CrateContext, i: uint) -> ValueRef {
581581

582582
// This is a 'c-like' raw string, which differs from
583583
// our boxed-and-length-annotated strings.
584-
pub fn C_cstr(cx: &CrateContext, s: InternedString) -> ValueRef {
584+
pub fn C_cstr(cx: &CrateContext, s: InternedString, null_terminated: bool) -> ValueRef {
585585
unsafe {
586586
match cx.const_cstr_cache.borrow().find(&s) {
587587
Some(&llval) => return llval,
@@ -591,7 +591,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString) -> ValueRef {
591591
let sc = llvm::LLVMConstStringInContext(cx.llcx,
592592
s.get().as_ptr() as *c_char,
593593
s.get().len() as c_uint,
594-
False);
594+
!null_terminated as Bool);
595595

596596
let gsym = token::gensym("str");
597597
let g = format!("str{}", gsym).with_c_str(|buf| {
@@ -611,7 +611,7 @@ pub fn C_cstr(cx: &CrateContext, s: InternedString) -> ValueRef {
611611
pub fn C_str_slice(cx: &CrateContext, s: InternedString) -> ValueRef {
612612
unsafe {
613613
let len = s.get().len();
614-
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s), Type::i8p(cx).to_ref());
614+
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s, false), Type::i8p(cx).to_ref());
615615
C_struct(cx, [cs, C_uint(cx, len)], false)
616616
}
617617
}
@@ -940,7 +940,7 @@ pub fn filename_and_line_num_from_span(bcx: &Block, span: Span)
940940
-> (ValueRef, ValueRef) {
941941
let loc = bcx.sess().codemap().lookup_char_pos(span.lo);
942942
let filename_cstr = C_cstr(bcx.ccx(),
943-
token::intern_and_get_ident(loc.file.name));
943+
token::intern_and_get_ident(loc.file.name), true);
944944
let filename = build::PointerCast(bcx, filename_cstr, Type::i8p(bcx.ccx()));
945945
let line = C_int(bcx.ccx(), loc.line as int);
946946
(filename, line)

src/librustc/middle/trans/controlflow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ pub fn trans_fail<'a>(
331331
fail_str: InternedString)
332332
-> &'a Block<'a> {
333333
let ccx = bcx.ccx();
334-
let v_fail_str = C_cstr(ccx, fail_str);
334+
let v_fail_str = C_cstr(ccx, fail_str, true);
335335
let _icx = push_ctxt("trans_fail_value");
336336
let loc = bcx.sess().codemap().lookup_char_pos(sp.lo);
337-
let v_filename = C_cstr(ccx, token::intern_and_get_ident(loc.file.name));
337+
let v_filename = C_cstr(ccx, token::intern_and_get_ident(loc.file.name), true);
338338
let v_line = loc.line as int;
339339
let v_str = PointerCast(bcx, v_fail_str, Type::i8p(ccx));
340340
let v_filename = PointerCast(bcx, v_filename, Type::i8p(ccx));

src/librustc/middle/trans/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2129,7 +2129,7 @@ fn type_metadata(cx: &CrateContext,
21292129
let i8_t = ty::mk_i8();
21302130
match *vstore {
21312131
ty::vstore_fixed(len) => {
2132-
fixed_vec_metadata(cx, i8_t, len + 1, usage_site_span)
2132+
fixed_vec_metadata(cx, i8_t, len, usage_site_span)
21332133
},
21342134
ty::vstore_uniq => {
21352135
let vec_metadata = vec_metadata(cx, i8_t, usage_site_span);

src/librustc/middle/trans/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a> Reflector<'a> {
6262
let str_ty = ty::mk_str(bcx.tcx(), str_vstore);
6363
let scratch = rvalue_scratch_datum(bcx, str_ty, "");
6464
let len = C_uint(bcx.ccx(), s.get().len());
65-
let c_str = PointerCast(bcx, C_cstr(bcx.ccx(), s), Type::i8p(bcx.ccx()));
65+
let c_str = PointerCast(bcx, C_cstr(bcx.ccx(), s, false), Type::i8p(bcx.ccx()));
6666
Store(bcx, c_str, GEPi(bcx, scratch.val, [ 0, 0 ]));
6767
Store(bcx, len, GEPi(bcx, scratch.val, [ 0, 1 ]));
6868
scratch.val

src/librustc/middle/trans/tvec.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ pub fn trans_lit_str<'a>(
289289
unsafe {
290290
let bytes = str_lit.get().len();
291291
let llbytes = C_uint(bcx.ccx(), bytes);
292-
let llcstr = C_cstr(bcx.ccx(), str_lit);
292+
let llcstr = C_cstr(bcx.ccx(), str_lit, false);
293293
let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p(bcx.ccx()).to_ref());
294294
Store(bcx, llcstr,
295295
GEPi(bcx, lldest, [0u, abi::slice_elt_base]));
@@ -319,7 +319,7 @@ pub fn trans_uniq_vstore<'a>(bcx: &'a Block<'a>,
319319
ast::ExprLit(lit) => {
320320
match lit.node {
321321
ast::LitStr(ref s, _) => {
322-
let llptrval = C_cstr(bcx.ccx(), (*s).clone());
322+
let llptrval = C_cstr(bcx.ccx(), (*s).clone(), false);
323323
let llptrval = PointerCast(bcx,
324324
llptrval,
325325
Type::i8p(bcx.ccx()));
@@ -393,7 +393,7 @@ pub fn write_content<'a>(
393393
SaveIn(lldest) => {
394394
let bytes = s.get().len();
395395
let llbytes = C_uint(bcx.ccx(), bytes);
396-
let llcstr = C_cstr(bcx.ccx(), (*s).clone());
396+
let llcstr = C_cstr(bcx.ccx(), (*s).clone(), false);
397397
base::call_memcpy(bcx,
398398
lldest,
399399
llcstr,

src/test/debug-info/include_string.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
// debugger:rbreak zzz
1515
// debugger:run
1616
// debugger:finish
17-
// debugger:print string1
18-
// check:$1 = [...]"some text to include in another file as string 1", length = 48}
19-
// debugger:print string2
20-
// check:$2 = [...]"some text to include in another file as string 2", length = 48}
21-
// debugger:print string3
22-
// check:$3 = [...]"some text to include in another file as string 3", length = 48}
17+
// debugger:print string1.length
18+
// check:$1 = 48
19+
// debugger:print string2.length
20+
// check:$2 = 48
21+
// debugger:print string3.length
22+
// check:$3 = 48
2323
// debugger:continue
2424

2525
#[allow(unused_variable)];

0 commit comments

Comments
 (0)