authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-08 14:16:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-08 17:34:29-07:00
logcd00ebe2a7913927687336a80febaffa6beeb246
treedfa7521c91bac07d64e05f69b48577b8c57112cb
parente51bf822dd0a79bc946f956f0fcb7a7f9abde389

zig fmt --stats: add source size


1 files changed, 5 insertions(+), 0 deletions(-)

src/fmt.zig+5
......@@ -52,6 +52,7 @@ const Stats = struct {
5252 files: u32,
5353 tokens: u64,
5454 nodes: u64,
55 source_size: u64,
5556 /// Count per token tag index
5657 token_stats: []u64,
5758 /// Count per node tag index
......@@ -208,6 +209,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
208209 .files = 0,
209210 .tokens = 0,
210211 .nodes = 0,
212 .source_size = 0,
211213 .serializer = .{ .writer = &stdout_writer.interface },
212214 .root_struct = try stats.serializer.beginStruct(.{}),
213215 .token_stats = try arena.alloc(u64, @typeInfo(std.zig.Token.Tag).@"enum".field_names.len),
......@@ -243,6 +245,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
243245 try agg_struct.field("file_count", stats.files, .{});
244246 try agg_struct.field("token_count", stats.tokens, .{});
245247 try agg_struct.field("node_count", stats.nodes, .{});
248 try agg_struct.field("source_size", stats.source_size, .{});
246249
247250 const token_names = @typeInfo(std.zig.Token.Tag).@"enum".field_names;
248251 const node_names = @typeInfo(std.zig.Ast.Node.Tag).@"enum".field_names;
......@@ -430,6 +433,7 @@ fn fmtPathFile(
430433
431434 try zon_struct.field("token_count", tree.tokens.len, .{});
432435 try zon_struct.field("node_count", tree.nodes.len, .{});
436 try zon_struct.field("source_size", stat.size, .{});
433437
434438 const token_names = @typeInfo(std.zig.Token.Tag).@"enum".field_names;
435439 const node_names = @typeInfo(std.zig.Ast.Node.Tag).@"enum".field_names;
......@@ -480,6 +484,7 @@ fn fmtPathFile(
480484 stats.tokens += tree.tokens.len;
481485 stats.nodes += tree.nodes.len;
482486 stats.files += 1;
487 stats.source_size += stat.size;
483488 }
484489
485490 // As a heuristic, we make enough capacity for the same as the input source.