| ... | @@ -26,16 +26,13 @@ const Fmt = struct { | ... | @@ -26,16 +26,13 @@ const Fmt = struct { |
| 26 | color: Color, | 26 | color: Color, |
| 27 | gpa: Allocator, | 27 | gpa: Allocator, |
| 28 | arena: Allocator, | 28 | arena: Allocator, |
| 29 | out_buffer: std.ArrayList(u8), | 29 | out_buffer: std.ArrayListUnmanaged(u8), |
| | 30 | stdout: *std.io.BufferedWriter, |
| 30 | | 31 | |
| 31 | const SeenMap = std.AutoHashMap(fs.File.INode, void); | 32 | const SeenMap = std.AutoHashMap(fs.File.INode, void); |
| 32 | }; | 33 | }; |
| 33 | | 34 | |
| 34 | pub fn run( | 35 | pub fn run(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 35 | gpa: Allocator, | | |
| 36 | arena: Allocator, | | |
| 37 | args: []const []const u8, | | |
| 38 | ) !void { | | |
| 39 | var color: Color = .auto; | 36 | var color: Color = .auto; |
| 40 | var stdin_flag = false; | 37 | var stdin_flag = false; |
| 41 | var check_flag = false; | 38 | var check_flag = false; |
| ... | @@ -52,8 +49,7 @@ pub fn run( | ... | @@ -52,8 +49,7 @@ pub fn run( |
| 52 | const arg = args[i]; | 49 | const arg = args[i]; |
| 53 | if (mem.startsWith(u8, arg, "-")) { | 50 | if (mem.startsWith(u8, arg, "-")) { |
| 54 | if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { | 51 | if (mem.eql(u8, arg, "-h") or mem.eql(u8, arg, "--help")) { |
| 55 | const stdout = std.io.getStdOut().writer(); | 52 | try std.io.getStdOut().writeAll(usage_fmt); |
| 56 | try stdout.writeAll(usage_fmt); | | |
| 57 | return process.cleanExit(); | 53 | return process.cleanExit(); |
| 58 | } else if (mem.eql(u8, arg, "--color")) { | 54 | } else if (mem.eql(u8, arg, "--color")) { |
| 59 | if (i + 1 >= args.len) { | 55 | if (i + 1 >= args.len) { |
| ... | @@ -138,8 +134,11 @@ pub fn run( | ... | @@ -138,8 +134,11 @@ pub fn run( |
| 138 | try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color); | 134 | try std.zig.printAstErrorsToStderr(gpa, tree, "<stdin>", color); |
| 139 | process.exit(2); | 135 | process.exit(2); |
| 140 | } | 136 | } |
| 141 | const formatted = try tree.render(gpa); | 137 | var aw: std.io.AllocatingWriter = undefined; |
| 142 | defer gpa.free(formatted); | 138 | const bw = aw.init(gpa); |
| | 139 | defer aw.deinit(); |
| | 140 | try tree.render(gpa, bw, .{}); |
| | 141 | const formatted = aw.getWritten(); |
| 143 | | 142 | |
| 144 | if (check_flag) { | 143 | if (check_flag) { |
| 145 | const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code)); | 144 | const code: u8 = @intFromBool(mem.eql(u8, formatted, source_code)); |
| ... | @@ -153,6 +152,12 @@ pub fn run( | ... | @@ -153,6 +152,12 @@ pub fn run( |
| 153 | fatal("expected at least one source file argument", .{}); | 152 | fatal("expected at least one source file argument", .{}); |
| 154 | } | 153 | } |
| 155 | | 154 | |
| | 155 | var stdout_buffer: [4096]u8 = undefined; |
| | 156 | var stdout: std.io.BufferedWriter = .{ |
| | 157 | .buffer = &stdout_buffer, |
| | 158 | .unbuffered_writer = std.io.getStdOut().writer(), |
| | 159 | }; |
| | 160 | |
| 156 | var fmt: Fmt = .{ | 161 | var fmt: Fmt = .{ |
| 157 | .gpa = gpa, | 162 | .gpa = gpa, |
| 158 | .arena = arena, | 163 | .arena = arena, |
| ... | @@ -161,10 +166,11 @@ pub fn run( | ... | @@ -161,10 +166,11 @@ pub fn run( |
| 161 | .check_ast = check_ast_flag, | 166 | .check_ast = check_ast_flag, |
| 162 | .force_zon = force_zon, | 167 | .force_zon = force_zon, |
| 163 | .color = color, | 168 | .color = color, |
| 164 | .out_buffer = std.ArrayList(u8).init(gpa), | 169 | .out_buffer = .empty, |
| | 170 | .stdout = &stdout, |
| 165 | }; | 171 | }; |
| 166 | defer fmt.seen.deinit(); | 172 | defer fmt.seen.deinit(); |
| 167 | defer fmt.out_buffer.deinit(); | 173 | defer fmt.out_buffer.deinit(gpa); |
| 168 | | 174 | |
| 169 | // Mark any excluded files/directories as already seen, | 175 | // Mark any excluded files/directories as already seen, |
| 170 | // so that they are skipped later during actual processing | 176 | // so that they are skipped later during actual processing |
| ... | @@ -322,15 +328,19 @@ fn fmtPathFile( | ... | @@ -322,15 +328,19 @@ fn fmtPathFile( |
| 322 | | 328 | |
| 323 | // As a heuristic, we make enough capacity for the same as the input source. | 329 | // As a heuristic, we make enough capacity for the same as the input source. |
| 324 | fmt.out_buffer.shrinkRetainingCapacity(0); | 330 | fmt.out_buffer.shrinkRetainingCapacity(0); |
| 325 | try fmt.out_buffer.ensureTotalCapacity(source_code.len); | 331 | try fmt.out_buffer.ensureTotalCapacity(gpa, source_code.len); |
| 326 | | 332 | |
| 327 | try tree.renderToArrayList(&fmt.out_buffer, .{}); | 333 | { |
| | 334 | var aw: std.io.AllocatingWriter = undefined; |
| | 335 | const bw = aw.fromArrayList(gpa, &fmt.out_buffer); |
| | 336 | defer fmt.out_buffer = aw.toArrayList(); |
| | 337 | try tree.render(gpa, bw, .{}); |
| | 338 | } |
| 328 | if (mem.eql(u8, fmt.out_buffer.items, source_code)) | 339 | if (mem.eql(u8, fmt.out_buffer.items, source_code)) |
| 329 | return; | 340 | return; |
| 330 | | 341 | |
| 331 | if (check_mode) { | 342 | if (check_mode) { |
| 332 | const stdout = std.io.getStdOut().writer(); | 343 | try fmt.stdout.print("{s}\n", .{file_path}); |
| 333 | try stdout.print("{s}\n", .{file_path}); | | |
| 334 | fmt.any_error = true; | 344 | fmt.any_error = true; |
| 335 | } else { | 345 | } else { |
| 336 | var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); | 346 | var af = try dir.atomicFile(sub_path, .{ .mode = stat.mode }); |
| ... | @@ -338,8 +348,7 @@ fn fmtPathFile( | ... | @@ -338,8 +348,7 @@ fn fmtPathFile( |
| 338 | | 348 | |
| 339 | try af.file.writeAll(fmt.out_buffer.items); | 349 | try af.file.writeAll(fmt.out_buffer.items); |
| 340 | try af.finish(); | 350 | try af.finish(); |
| 341 | const stdout = std.io.getStdOut().writer(); | 351 | try fmt.stdout.print("{s}\n", .{file_path}); |
| 342 | try stdout.print("{s}\n", .{file_path}); | | |
| 343 | } | 352 | } |
| 344 | } | 353 | } |
| 345 | | 354 | |
| ... | @@ -350,3 +359,12 @@ const process = std.process; | ... | @@ -350,3 +359,12 @@ const process = std.process; |
| 350 | const Allocator = std.mem.Allocator; | 359 | const Allocator = std.mem.Allocator; |
| 351 | const Color = std.zig.Color; | 360 | const Color = std.zig.Color; |
| 352 | const fatal = std.process.fatal; | 361 | const fatal = std.process.fatal; |
| | 362 | |
| | 363 | /// Provided for debugging/testing purposes; unused by the compiler. |
| | 364 | pub fn main() !void { |
| | 365 | const gpa = std.heap.smp_allocator; |
| | 366 | var arena_instance = std.heap.ArenaAllocator.init(gpa); |
| | 367 | const arena = arena_instance.allocator(); |
| | 368 | const args = try process.argsAlloc(arena); |
| | 369 | return run(gpa, arena, args[1..]); |
| | 370 | } |