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
This commit is contained in:
Andrew Kelley
2020-06-18 17:09:10 -04:00
parent e54ed9f638
commit 4a38799631
3 changed files with 10 additions and 8 deletions

View File

@@ -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,
};

View File

@@ -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);

View File

@@ -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);
}