From 4a387996311a025a021409f08a61bab9e9885987 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 18 Jun 2020 17:09:10 -0400 Subject: [PATCH] make file and fn_name fields of SourceLocation also null-terminated One of the main motivating use cases for this language feature is tracing/profiling tools, which expect null-terminated strings for these values. Since the data is statically allocated, making them additionally null-terminated comes at no cost. This prevents the requirement of compile-time code to convert to null-termination, which could increase the compilation time of code with tracing enabled. See #2029 --- lib/std/builtin.zig | 4 ++-- src/ir.cpp | 12 ++++++------ test/stage1/behavior/src.zig | 2 ++ 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 822901be73..195b301840 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -134,8 +134,8 @@ pub const CallingConvention = enum { /// This data structure is used by the Zig language code generation and /// therefore must be kept in sync with the compiler implementation. pub const SourceLocation = struct { - file: []const u8, - fn_name: []const u8, + file: [:0]const u8, + fn_name: [:0]const u8, line: u32, column: u32, }; diff --git a/src/ir.cpp b/src/ir.cpp index 6406458bf5..48e5db8f28 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -30881,10 +30881,10 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr return ira->codegen->invalid_inst_gen; } - ZigType *u8_ptr = get_pointer_to_type_extra( + ZigType *u8_ptr = get_pointer_to_type_extra2( ira->codegen, ira->codegen->builtin_types.entry_u8, true, false, PtrLenUnknown, - 0, 0, 0, false); + 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte()); ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr); ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation"); @@ -30899,23 +30899,23 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4); result->data.x_struct.fields = fields; - // file: []const u8 + // file: [:0]const u8 ensure_field_index(source_location_type, "file", 0); fields[0]->special = ConstValSpecialStatic; - fields[0]->type = u8_slice; ZigType *import = instruction->base.base.source_node->owner; Buf *path = import->data.structure.root_struct->path; ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee; init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true); + fields[0]->type = u8_slice; - // fn_name: []const u8 + // fn_name: [:0]const u8 ensure_field_index(source_location_type, "fn_name", 1); fields[1]->special = ConstValSpecialStatic; - fields[1]->type = u8_slice; ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee; init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true); + fields[1]->type = u8_slice; // line: u32 ensure_field_index(source_location_type, "line", 2); diff --git a/test/stage1/behavior/src.zig b/test/stage1/behavior/src.zig index eec1154374..27fa144e54 100644 --- a/test/stage1/behavior/src.zig +++ b/test/stage1/behavior/src.zig @@ -12,4 +12,6 @@ fn doTheTest() void { expect(src.column == 17); expect(std.mem.endsWith(u8, src.fn_name, "doTheTest")); expect(std.mem.endsWith(u8, src.file, "src.zig")); + expect(src.fn_name[src.fn_name.len] == 0); + expect(src.file[src.file.len] == 0); }