diff options
| author | Martin Wickham <spexguy070@gmail.com> | 2021-09-30 21:43:30 -0500 |
|---|---|---|
| committer | Martin Wickham <spexguy070@gmail.com> | 2021-10-02 15:21:48 -0500 |
| commit | 0b8ddb4478ebf14811caf8a083ca493f9ed733b2 (patch) | |
| tree | 5e80c6730a113eebb2bf245b3ad819d415e6fa06 /src | |
| parent | 993dc2ae77514e61235f0920755fdb1ccc77c2f7 (diff) | |
| download | zig-0b8ddb4478ebf14811caf8a083ca493f9ed733b2.tar.gz zig-0b8ddb4478ebf14811caf8a083ca493f9ed733b2.zip | |
Improve debug names of decls
Diffstat (limited to 'src')
| -rw-r--r-- | src/Module.zig | 35 | ||||
| -rw-r--r-- | src/crash_report.zig | 2 |
2 files changed, 36 insertions, 1 deletions
diff --git a/src/Module.zig b/src/Module.zig index 0557a6dcb0..07ebb0823c 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -625,6 +625,14 @@ pub const Decl = struct { return try decl.namespace.renderFullyQualifiedName(unqualified_name, writer); } + pub fn renderFullyQualifiedDebugName(decl: *const Decl, writer: anytype) @TypeOf(writer).Error!void { + // Namespace decls (struct/enum/union/opaque) use their own namespace, + // which means the decl name and the namespace name are the same. + // In that case we want to omit the decl name, unless this is the root decl. + const unqualified_name = if (decl.namespace.getDecl() != decl or decl.namespace.parent == null) mem.spanZ(decl.name) else ""; + return try decl.namespace.renderFullyQualifiedDebugName(unqualified_name, writer); + } + pub fn getFullyQualifiedName(decl: *const Decl, gpa: *Allocator) ![:0]u8 { var buffer = std.ArrayList(u8).init(gpa); defer buffer.deinit(); @@ -1246,6 +1254,26 @@ pub const Scope = struct { } } + // This renders e.g. "std.fs:Dir.OpenOptions" + pub fn renderFullyQualifiedDebugName( + ns: Namespace, + name: []const u8, + writer: anytype, + ) @TypeOf(writer).Error!void { + var separator_char: u8 = '.'; + if (ns.parent) |parent| { + const decl = ns.getDecl(); + try parent.renderFullyQualifiedDebugName(mem.spanZ(decl.name), writer); + } else { + try ns.file_scope.renderFullyQualifiedDebugName(writer); + separator_char = ':'; + } + if (name.len != 0) { + try writer.writeByte(separator_char); + try writer.writeAll(name); + } + } + pub fn getDecl(ns: Namespace) *Decl { return ns.ty.getOwnerDecl(); } @@ -1400,6 +1428,13 @@ pub const Scope = struct { }; } + pub fn renderFullyQualifiedDebugName(file: File, writer: anytype) !void { + for (file.sub_file_path) |byte| switch (byte) { + '/', '\\' => try writer.writeByte('/'), + else => try writer.writeByte(byte), + }; + } + pub fn fullyQualifiedNameZ(file: File, gpa: *Allocator) ![:0]u8 { var buf = std.ArrayList(u8).init(gpa); defer buf.deinit(); diff --git a/src/crash_report.zig b/src/crash_report.zig index 84f4b8db84..4554e324cb 100644 --- a/src/crash_report.zig +++ b/src/crash_report.zig @@ -150,7 +150,7 @@ fn writeFilePath(file: *Scope.File, stream: anytype) !void { fn writeFullyQualifiedDeclWithFile(decl: *Decl, stream: anytype) !void { try writeFilePath(decl.getFileScope(), stream); try stream.writeAll(": "); - try decl.namespace.renderFullyQualifiedName(std.mem.sliceTo(decl.name, 0), stream); + try decl.renderFullyQualifiedDebugName(stream); } fn compilerPanic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace) noreturn { |
