authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 17:09:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 17:09:10-04:00
log4a387996311a025a021409f08a61bab9e9885987
treeea36d6f87fc409b0eebc8f6149f248109b87735d
parente54ed9f638c33ec3091d207978ed856d92614caf

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

3 files changed, 10 insertions(+), 8 deletions(-)

lib/std/builtin.zig+2-2
...@@ -134,8 +134,8 @@ pub const CallingConvention = enum {...@@ -134,8 +134,8 @@ pub const CallingConvention = enum {
134/// This data structure is used by the Zig language code generation and134/// This data structure is used by the Zig language code generation and
135/// therefore must be kept in sync with the compiler implementation.135/// therefore must be kept in sync with the compiler implementation.
136pub const SourceLocation = struct {136pub const SourceLocation = struct {
137 file: []const u8,137 file: [:0]const u8,
138 fn_name: []const u8,138 fn_name: [:0]const u8,
139 line: u32,139 line: u32,
140 column: u32,140 column: u32,
141};141};
src/ir.cpp+6-6
...@@ -30881,10 +30881,10 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr...@@ -30881,10 +30881,10 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr
30881 return ira->codegen->invalid_inst_gen;30881 return ira->codegen->invalid_inst_gen;
30882 }30882 }
3088330883
30884 ZigType *u8_ptr = get_pointer_to_type_extra(30884 ZigType *u8_ptr = get_pointer_to_type_extra2(
30885 ira->codegen, ira->codegen->builtin_types.entry_u8,30885 ira->codegen, ira->codegen->builtin_types.entry_u8,
30886 true, false, PtrLenUnknown,30886 true, false, PtrLenUnknown,
30887 0, 0, 0, false);30887 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte());
30888 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);30888 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
3088930889
30890 ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation");30890 ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation");
...@@ -30899,23 +30899,23 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr...@@ -30899,23 +30899,23 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr
30899 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);30899 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
30900 result->data.x_struct.fields = fields;30900 result->data.x_struct.fields = fields;
3090130901
30902 // file: []const u830902 // file: [:0]const u8
30903 ensure_field_index(source_location_type, "file", 0);30903 ensure_field_index(source_location_type, "file", 0);
30904 fields[0]->special = ConstValSpecialStatic;30904 fields[0]->special = ConstValSpecialStatic;
30905 fields[0]->type = u8_slice;
3090630905
30907 ZigType *import = instruction->base.base.source_node->owner;30906 ZigType *import = instruction->base.base.source_node->owner;
30908 Buf *path = import->data.structure.root_struct->path;30907 Buf *path = import->data.structure.root_struct->path;
30909 ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;30908 ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;
30910 init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true);30909 init_const_slice(ira->codegen, fields[0], file_name, 0, buf_len(path), true);
30910 fields[0]->type = u8_slice;
3091130911
30912 // fn_name: []const u830912 // fn_name: [:0]const u8
30913 ensure_field_index(source_location_type, "fn_name", 1);30913 ensure_field_index(source_location_type, "fn_name", 1);
30914 fields[1]->special = ConstValSpecialStatic;30914 fields[1]->special = ConstValSpecialStatic;
30915 fields[1]->type = u8_slice;
3091630915
30917 ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;30916 ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
30918 init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true);30917 init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true);
30918 fields[1]->type = u8_slice;
3091930919
30920 // line: u3230920 // line: u32
30921 ensure_field_index(source_location_type, "line", 2);30921 ensure_field_index(source_location_type, "line", 2);
test/stage1/behavior/src.zig+2
...@@ -12,4 +12,6 @@ fn doTheTest() void {...@@ -12,4 +12,6 @@ fn doTheTest() void {
12 expect(src.column == 17);12 expect(src.column == 17);
13 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));13 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
14 expect(std.mem.endsWith(u8, src.file, "src.zig"));14 expect(std.mem.endsWith(u8, src.file, "src.zig"));
15 expect(src.fn_name[src.fn_name.len] == 0);
16 expect(src.file[src.file.len] == 0);
15}17}