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 {
134134/// This data structure is used by the Zig language code generation and
135135/// therefore must be kept in sync with the compiler implementation.
136136pub const SourceLocation = struct {
137 file: []const u8,
138 fn_name: []const u8,
137 file: [:0]const u8,
138 fn_name: [:0]const u8,
139139 line: u32,
140140 column: u32,
141141};
src/ir.cpp+6-6
......@@ -30881,10 +30881,10 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr
3088130881 return ira->codegen->invalid_inst_gen;
3088230882 }
3088330883
30884 ZigType *u8_ptr = get_pointer_to_type_extra(
30884 ZigType *u8_ptr = get_pointer_to_type_extra2(
3088530885 ira->codegen, ira->codegen->builtin_types.entry_u8,
3088630886 true, false, PtrLenUnknown,
30887 0, 0, 0, false);
30887 0, 0, 0, false, VECTOR_INDEX_NONE, nullptr, ira->codegen->intern.for_zero_byte());
3088830888 ZigType *u8_slice = get_slice_type(ira->codegen, u8_ptr);
3088930889
3089030890 ZigType *source_location_type = get_builtin_type(ira->codegen, "SourceLocation");
......@@ -30899,23 +30899,23 @@ static IrInstGen *ir_analyze_instruction_src(IrAnalyze *ira, IrInstSrcSrc *instr
3089930899 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
3090030900 result->data.x_struct.fields = fields;
3090130901
30902 // file: []const u8
30902 // file: [:0]const u8
3090330903 ensure_field_index(source_location_type, "file", 0);
3090430904 fields[0]->special = ConstValSpecialStatic;
30905 fields[0]->type = u8_slice;
3090630905
3090730906 ZigType *import = instruction->base.base.source_node->owner;
3090830907 Buf *path = import->data.structure.root_struct->path;
3090930908 ZigValue *file_name = create_const_str_lit(ira->codegen, path)->data.x_ptr.data.ref.pointee;
3091030909 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 u8
30912 // fn_name: [:0]const u8
3091330913 ensure_field_index(source_location_type, "fn_name", 1);
3091430914 fields[1]->special = ConstValSpecialStatic;
30915 fields[1]->type = u8_slice;
3091630915
3091730916 ZigValue *fn_name = create_const_str_lit(ira->codegen, &fn_entry->symbol_name)->data.x_ptr.data.ref.pointee;
3091830917 init_const_slice(ira->codegen, fields[1], fn_name, 0, buf_len(&fn_entry->symbol_name), true);
30918 fields[1]->type = u8_slice;
3091930919
3092030920 // line: u32
3092130921 ensure_field_index(source_location_type, "line", 2);
test/stage1/behavior/src.zig+2
......@@ -12,4 +12,6 @@ fn doTheTest() void {
1212 expect(src.column == 17);
1313 expect(std.mem.endsWith(u8, src.fn_name, "doTheTest"));
1414 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);
1517}