From c8a7ab7eff0f261c47926cf0637e919d42e41940 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 26 Dec 2016 16:04:14 -0500 Subject: IR: pass cStringConcatenation test --- src/ir.cpp | 5 +++-- src/ir_print.cpp | 2 ++ test/cases/misc.zig | 22 ++++++++++++++++++++++ test/self_hosted.zig | 16 ---------------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/ir.cpp b/src/ir.cpp index 7eac05def6..c964f5536b 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -6173,8 +6173,9 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp * TypeTableEntry *result_type; ConstExprValue *out_array_val; size_t new_len = (op1_array_end - op1_array_index) + (op2_array_end - op2_array_index); + TypeTableEntry *out_array_type = get_array_type(ira->codegen, child_type, new_len); if (op1_canon_type->id == TypeTableEntryIdArray || op2_canon_type->id == TypeTableEntryIdArray) { - result_type = get_array_type(ira->codegen, child_type, new_len); + result_type = out_array_type; out_array_val = out_val; } else { @@ -6182,7 +6183,7 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp * out_array_val = allocate(1); out_array_val->special = ConstValSpecialStatic; - out_array_val->type = result_type; + out_array_val->type = out_array_type; out_val->data.x_ptr.base_ptr = out_array_val; out_val->data.x_ptr.index = 0; out_val->data.x_ptr.special = ConstPtrSpecialCStr; diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 3db8b32e4f..ebbcf6feae 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -100,6 +100,8 @@ static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) { fprintf(irp->f, "&"); if (const_val->data.x_ptr.special == ConstPtrSpecialRuntime) { fprintf(irp->f, "(runtime pointer value)"); + } else if (const_val->data.x_ptr.special == ConstPtrSpecialCStr) { + fprintf(irp->f, "(c str lit)"); } else { ir_print_const_value(irp, const_ptr_pointee(const_val)); } diff --git a/test/cases/misc.zig b/test/cases/misc.zig index 62de3dd05c..5ae312c0dc 100644 --- a/test/cases/misc.zig +++ b/test/cases/misc.zig @@ -436,6 +436,28 @@ fn ptrEql(a: &[]const u8, b: &[]const u8) -> bool { } +fn cStringConcatenation() { + @setFnTest(this); + + const a = c"OK" ++ c" IT " ++ c"WORKED"; + const b = c"OK IT WORKED"; + + const len = cstrlen(b); + const len_with_null = len + 1; + {var i: u32 = 0; while (i < len_with_null; i += 1) { + assert(a[i] == b[i]); + }} + assert(a[len] == 0); + assert(b[len] == 0); +} + +// TODO import from std.cstr +pub fn cstrlen(ptr: &const u8) -> usize { + var count: usize = 0; + while (ptr[count] != 0; count += 1) {} + return count; +} + // TODO import from std.str pub fn memeql(a: []const u8, b: []const u8) -> bool { sliceEql(u8, a, b) diff --git a/test/self_hosted.zig b/test/self_hosted.zig index a6d3bcc001..539d14a446 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -58,22 +58,6 @@ fn returnsTen() -> %i32 { 10 } -// TODO not passing -fn cStringConcatenation() { - @setFnTest(this, true); - - const a = c"OK" ++ c" IT " ++ c"WORKED"; - const b = c"OK IT WORKED"; - - const len = cstrlen(b); - const len_with_null = len + 1; - {var i: u32 = 0; while (i < len_with_null; i += 1) { - assert(a[i] == b[i]); - }} - assert(a[len] == 0); - assert(b[len] == 0); -} - // TODO not passing fn castSliceToU8Slice() { @setFnTest(this); -- cgit v1.2.3