authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-07 14:05:55-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-08-08 07:47:14-07:00
logcf87a1a7cf57123aad533a73d8e0fa4d4916a674
treeeec5763a778319fbc8aafe991a2ff504223fdc48
parentca012e5b69b9ff275c6e366c0348219d1c816f18

language: add module name field to `@src`

closes #20963

3 files changed, 38 insertions(+), 4 deletions(-)

lib/std/builtin.zig+3
......@@ -243,6 +243,9 @@ pub const AddressSpace = enum(u5) {
243243/// This data structure is used by the Zig language code generation and
244244/// therefore must be kept in sync with the compiler implementation.
245245pub const SourceLocation = struct {
246 /// The name chosen when compiling. Not a file path.
247 module: [:0]const u8,
248 /// Relative to the root directory of its module.
246249 file: [:0]const u8,
247250 fn_name: [:0]const u8,
248251 line: u32,
src/Sema.zig+33-4
......@@ -17769,11 +17769,12 @@ fn zirBuiltinSrc(
1776917769 defer tracy.end();
1777017770
1777117771 const pt = sema.pt;
17772 const mod = pt.zcu;
17772 const zcu = pt.zcu;
1777317773 const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data;
17774 const fn_owner_decl = mod.funcOwnerDeclPtr(sema.func_index);
17775 const ip = &mod.intern_pool;
17774 const fn_owner_decl = zcu.funcOwnerDeclPtr(sema.func_index);
17775 const ip = &zcu.intern_pool;
1777617776 const gpa = sema.gpa;
17777 const file_scope = fn_owner_decl.getFileScope(zcu);
1777717778
1777817779 const func_name_val = v: {
1777917780 const func_name_len = fn_owner_decl.name.length(ip);
......@@ -17799,8 +17800,34 @@ fn zirBuiltinSrc(
1779917800 } });
1780017801 };
1780117802
17803 const module_name_val = v: {
17804 const module_name = file_scope.mod.fully_qualified_name;
17805 const array_ty = try pt.intern(.{ .array_type = .{
17806 .len = module_name.len,
17807 .sentinel = .zero_u8,
17808 .child = .u8_type,
17809 } });
17810 break :v try pt.intern(.{ .slice = .{
17811 .ty = .slice_const_u8_sentinel_0_type,
17812 .ptr = try pt.intern(.{ .ptr = .{
17813 .ty = .manyptr_const_u8_sentinel_0_type,
17814 .base_addr = .{ .anon_decl = .{
17815 .orig_ty = .slice_const_u8_sentinel_0_type,
17816 .val = try pt.intern(.{ .aggregate = .{
17817 .ty = array_ty,
17818 .storage = .{
17819 .bytes = try ip.getOrPutString(gpa, pt.tid, module_name, .maybe_embedded_nulls),
17820 },
17821 } }),
17822 } },
17823 .byte_offset = 0,
17824 } }),
17825 .len = (try pt.intValue(Type.usize, module_name.len)).toIntern(),
17826 } });
17827 };
17828
1780217829 const file_name_val = v: {
17803 const file_name = fn_owner_decl.getFileScope(mod).sub_file_path;
17830 const file_name = file_scope.sub_file_path;
1780417831 const array_ty = try pt.intern(.{ .array_type = .{
1780517832 .len = file_name.len,
1780617833 .sentinel = .zero_u8,
......@@ -17827,6 +17854,8 @@ fn zirBuiltinSrc(
1782717854
1782817855 const src_loc_ty = try pt.getBuiltinType("SourceLocation");
1782917856 const fields = .{
17857 // module: [:0]const u8,
17858 module_name_val,
1783017859 // file: [:0]const u8,
1783117860 file_name_val,
1783217861 // fn_name: [:0]const u8,
test/behavior/src.zig+2
......@@ -7,11 +7,13 @@ fn doTheTest() !void {
77 try expect(std.mem.endsWith(u8, src.file, "src.zig"));
88 try expect(src.fn_name[src.fn_name.len] == 0);
99 try expect(src.file[src.file.len] == 0);
10 if (!std.mem.eql(u8, src.module, "test") and !std.mem.eql(u8, src.module, "root")) return error.TestFailure;
1011}
1112
1213const std = @import("std");
1314const builtin = @import("builtin");
1415const expect = std.testing.expect;
16const expectEqualStrings = std.testing.expectEqualStrings;
1517
1618test "@src" {
1719 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO