refactor debugging utilities

This commit is contained in:
Techatrix 2023-05-30 19:05:26 +02:00
parent b958b258a3
commit 458da88f90

View File

@ -12,16 +12,16 @@ pub fn printTree(tree: std.zig.Ast) void {
\\----------------------------------------------- \\-----------------------------------------------
\\ \\
, .{}); , .{});
var i: usize = 0; for (
while (i < tree.nodes.len) : (i += 1) { tree.nodes.items(.tag),
std.debug.print(" {d:<3} {s:<20} {d:<3} {d:<3} {d:<3} {s}\n", .{ tree.nodes.items(.data),
i, tree.nodes.items(.main_token),
@tagName(tree.nodes.items(.tag)[i]), 0..,
tree.nodes.items(.data)[i].lhs, ) |tag, data, main_token, i| {
tree.nodes.items(.data)[i].rhs, std.debug.print(
tree.nodes.items(.main_token)[i], " {d:<3} {s:<20} {d:<3} {d:<3} {d:<3} {s}\n",
offsets.tokenToSlice(tree, tree.nodes.items(.main_token)[i]), .{ i, @tagName(tag), data.lhs, data.rhs, main_token, offsets.tokenToSlice(tree, main_token) },
}); );
} }
std.debug.print( std.debug.print(
@ -30,39 +30,42 @@ pub fn printTree(tree: std.zig.Ast) void {
\\---------------------------------- \\----------------------------------
\\ \\
, .{}); , .{});
i = 0; for (tree.tokens.items(.tag), tree.tokens.items(.start), 0..) |tag, start, i| {
while (i < tree.tokens.len) : (i += 1) { std.debug.print(
std.debug.print(" {d:<3} {s:<20} {d:<}\n", .{ " {d:<3} {s:<20} {d:<}\n",
i, .{ i, @tagName(tag), start },
@tagName(tree.tokens.items(.tag)[i]), );
tree.tokens.items(.start)[i],
});
} }
} }
pub fn printDocumentScope(doc_scope: analysis.DocumentScope) void { pub fn printDocumentScope(doc_scope: analysis.DocumentScope) void {
if (!std.debug.runtime_safety) @compileError("this function should only be used in debug mode!"); if (!std.debug.runtime_safety) @compileError("this function should only be used in debug mode!");
var index: usize = 0; for (0..doc_scope.scopes.len) |index| {
while (index < doc_scope.scopes.len) : (index += 1) {
const scope = doc_scope.scopes.get(index); const scope = doc_scope.scopes.get(index);
if (index != 0) std.debug.print("\n\n", .{}); if (index != 0) std.debug.print("\n\n", .{});
std.debug.print( std.debug.print(
\\[{d}, {d}] {} \\[{d}, {d}]
\\usingnamespaces: {d} \\ data: {}
\\Decls: \\ parent: {}
\\ child scopes: {any}
\\ usingnamespaces: {any}
\\ tests: {any}
\\ decls:
\\ \\
, .{ , .{
scope.loc.start, scope.loc.start,
scope.loc.end, scope.loc.end,
scope.data, scope.data,
scope.uses.len, scope.parent,
scope.child_scopes.items,
scope.uses,
scope.tests,
}); });
var decl_it = scope.decls.iterator(); var decl_it = scope.decls.iterator();
var idx: usize = 0; while (decl_it.next()) |entry| {
while (decl_it.next()) |entry| : (idx += 1) { std.debug.print(" - {s:<8} {}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
std.debug.print(" {s:<8} {}\n", .{ entry.key_ptr.*, entry.value_ptr.* });
} }
} }
} }
@ -136,3 +139,9 @@ pub const FailingAllocator = struct {
return 0 == self.random.random().intRangeAtMostBiased(u32, 0, self.likelihood); return 0 == self.random.random().intRangeAtMostBiased(u32, 0, self.likelihood);
} }
}; };
comptime {
if (std.debug.runtime_safety) {
std.testing.refAllDeclsRecursive(@This());
}
}