Improve debug names of decls

This commit is contained in:
Martin Wickham
2021-09-30 21:43:30 -05:00
parent 993dc2ae77
commit 0b8ddb4478
2 changed files with 36 additions and 1 deletions

View File

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

View File

@@ -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 {