| author | |
| committer | |
| log | 2c96f19fd3ed312e5cb0ac8006b73e73abf4a98d |
| tree | d3d21091ba024a1b2c6c560727d21c833c8286bd |
| parent | 15302e84a45a04cfe94a8842318f02a608055962 |
zig fmt only renames files and prints to stdout for files which changed4 files changed, 68 insertions(+), 10 deletions(-)
src-self-hosted/main.zig+8-4| ... | @@ -719,6 +719,9 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { | ... | @@ -719,6 +719,9 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { |
| 719 | }; | 719 | }; |
| 720 | defer tree.deinit(); | 720 | defer tree.deinit(); |
| 721 | 721 | ||
| 722 | var old_digest: [256]u8 = undefined; | ||
| 723 | std.crypto.Sha256.hash(source_code, old_digest[0..]); | ||
| 724 | |||
| 722 | var error_it = tree.errors.iterator(0); | 725 | var error_it = tree.errors.iterator(0); |
| 723 | while (error_it.next()) |parse_error| { | 726 | while (error_it.next()) |parse_error| { |
| 724 | const token = tree.tokens.at(parse_error.loc()); | 727 | const token = tree.tokens.at(parse_error.loc()); |
| ... | @@ -745,13 +748,14 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { | ... | @@ -745,13 +748,14 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void { |
| 745 | continue; | 748 | continue; |
| 746 | } | 749 | } |
| 747 | 750 | ||
| 748 | try stderr.print("{}\n", file_path); | ||
| 749 | |||
| 750 | const baf = try io.BufferedAtomicFile.create(allocator, file_path); | 751 | const baf = try io.BufferedAtomicFile.create(allocator, file_path); |
| 751 | defer baf.destroy(); | 752 | defer baf.destroy(); |
| 752 | 753 | ||
| 753 | try std.zig.render(allocator, baf.stream(), &tree); | 754 | const anything_changed = try std.zig.render(allocator, baf.stream(), &tree); |
| 754 | try baf.finish(); | 755 | if (anything_changed) { |
| 756 | try stderr.print("{}\n", file_path); | ||
| 757 | try baf.finish(); | ||
| 758 | } | ||
| 755 | } | 759 | } |
| 756 | } | 760 | } |
| 757 | 761 |
src-self-hosted/module.zig+1-1| ... | @@ -255,7 +255,7 @@ pub const Module = struct { | ... | @@ -255,7 +255,7 @@ pub const Module = struct { |
| 255 | const out_stream = &stderr_file_out_stream.stream; | 255 | const out_stream = &stderr_file_out_stream.stream; |
| 256 | 256 | ||
| 257 | warn("====fmt:====\n"); | 257 | warn("====fmt:====\n"); |
| 258 | try std.zig.render(self.allocator, out_stream, &tree); | 258 | _ = try std.zig.render(self.allocator, out_stream, &tree); |
| 259 | 259 | ||
| 260 | warn("====ir:====\n"); | 260 | warn("====ir:====\n"); |
| 261 | warn("TODO\n\n"); | 261 | warn("TODO\n\n"); |
std/zig/parser_test.zig+6-4| ... | @@ -1792,7 +1792,7 @@ const io = std.io; | ... | @@ -1792,7 +1792,7 @@ const io = std.io; |
| 1792 | 1792 | ||
| 1793 | var fixed_buffer_mem: [100 * 1024]u8 = undefined; | 1793 | var fixed_buffer_mem: [100 * 1024]u8 = undefined; |
| 1794 | 1794 | ||
| 1795 | fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 { | 1795 | fn testParse(source: []const u8, allocator: &mem.Allocator, changes_expected: bool) ![]u8 { |
| 1796 | var stderr_file = try io.getStdErr(); | 1796 | var stderr_file = try io.getStdErr(); |
| 1797 | var stderr = &io.FileOutStream.init(&stderr_file).stream; | 1797 | var stderr = &io.FileOutStream.init(&stderr_file).stream; |
| 1798 | 1798 | ||
| ... | @@ -1829,16 +1829,18 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 { | ... | @@ -1829,16 +1829,18 @@ fn testParse(source: []const u8, allocator: &mem.Allocator) ![]u8 { |
| 1829 | errdefer buffer.deinit(); | 1829 | errdefer buffer.deinit(); |
| 1830 | 1830 | ||
| 1831 | var buffer_out_stream = io.BufferOutStream.init(&buffer); | 1831 | var buffer_out_stream = io.BufferOutStream.init(&buffer); |
| 1832 | try std.zig.render(allocator, &buffer_out_stream.stream, &tree); | 1832 | const anything_changed = try std.zig.render(allocator, &buffer_out_stream.stream, &tree); |
| 1833 | std.debug.assert(anything_changed == changes_expected); | ||
| 1833 | return buffer.toOwnedSlice(); | 1834 | return buffer.toOwnedSlice(); |
| 1834 | } | 1835 | } |
| 1835 | 1836 | ||
| 1836 | fn testTransform(source: []const u8, expected_source: []const u8) !void { | 1837 | fn testTransform(source: []const u8, expected_source: []const u8) !void { |
| 1838 | const changes_expected = source.ptr != expected_source.ptr; | ||
| 1837 | const needed_alloc_count = x: { | 1839 | const needed_alloc_count = x: { |
| 1838 | // Try it once with unlimited memory, make sure it works | 1840 | // Try it once with unlimited memory, make sure it works |
| 1839 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | 1841 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); |
| 1840 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize)); | 1842 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, @maxValue(usize)); |
| 1841 | const result_source = try testParse(source, &failing_allocator.allocator); | 1843 | const result_source = try testParse(source, &failing_allocator.allocator, changes_expected); |
| 1842 | if (!mem.eql(u8, result_source, expected_source)) { | 1844 | if (!mem.eql(u8, result_source, expected_source)) { |
| 1843 | warn("\n====== expected this output: =========\n"); | 1845 | warn("\n====== expected this output: =========\n"); |
| 1844 | warn("{}", expected_source); | 1846 | warn("{}", expected_source); |
| ... | @@ -1855,7 +1857,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { | ... | @@ -1855,7 +1857,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { |
| 1855 | while (fail_index < needed_alloc_count) : (fail_index += 1) { | 1857 | while (fail_index < needed_alloc_count) : (fail_index += 1) { |
| 1856 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); | 1858 | var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]); |
| 1857 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index); | 1859 | var failing_allocator = std.debug.FailingAllocator.init(&fixed_allocator.allocator, fail_index); |
| 1858 | if (testParse(source, &failing_allocator.allocator)) |_| { | 1860 | if (testParse(source, &failing_allocator.allocator, changes_expected)) |_| { |
| 1859 | return error.NondeterministicMemoryUsage; | 1861 | return error.NondeterministicMemoryUsage; |
| 1860 | } else |err| switch (err) { | 1862 | } else |err| switch (err) { |
| 1861 | error.OutOfMemory => { | 1863 | error.OutOfMemory => { |
std/zig/render.zig+53-1| ... | @@ -12,9 +12,61 @@ pub const Error = error{ | ... | @@ -12,9 +12,61 @@ pub const Error = error{ |
| 12 | OutOfMemory, | 12 | OutOfMemory, |
| 13 | }; | 13 | }; |
| 14 | 14 | ||
| 15 | pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(stream).Child.Error || Error)!void { | 15 | /// Returns whether anything changed |
| 16 | pub fn render(allocator: &mem.Allocator, stream: var, tree: &ast.Tree) (@typeOf(stream).Child.Error || Error)!bool { | ||
| 16 | comptime assert(@typeId(@typeOf(stream)) == builtin.TypeId.Pointer); | 17 | comptime assert(@typeId(@typeOf(stream)) == builtin.TypeId.Pointer); |
| 17 | 18 | ||
| 19 | var anything_changed: bool = false; | ||
| 20 | |||
| 21 | // make a passthrough stream that checks whether something changed | ||
| 22 | const MyStream = struct { | ||
| 23 | const MyStream = this; | ||
| 24 | const StreamError = @typeOf(stream).Child.Error; | ||
| 25 | const Stream = std.io.OutStream(StreamError); | ||
| 26 | |||
| 27 | anything_changed_ptr: &bool, | ||
| 28 | child_stream: @typeOf(stream), | ||
| 29 | stream: Stream, | ||
| 30 | source_index: usize, | ||
| 31 | source: []const u8, | ||
| 32 | |||
| 33 | fn write(iface_stream: &Stream, bytes: []const u8) StreamError!void { | ||
| 34 | const self = @fieldParentPtr(MyStream, "stream", iface_stream); | ||
| 35 | |||
| 36 | if (!self.anything_changed_ptr.*) { | ||
| 37 | const end = self.source_index + bytes.len; | ||
| 38 | if (end > self.source.len) { | ||
| 39 | self.anything_changed_ptr.* = true; | ||
| 40 | } else { | ||
| 41 | const src_slice = self.source[self.source_index..end]; | ||
| 42 | self.source_index += bytes.len; | ||
| 43 | if (!mem.eql(u8, bytes, src_slice)) { | ||
| 44 | self.anything_changed_ptr.* = true; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | try self.child_stream.write(bytes); | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | var my_stream = MyStream{ | ||
| 53 | .stream = MyStream.Stream{ .writeFn = MyStream.write }, | ||
| 54 | .child_stream = stream, | ||
| 55 | .anything_changed_ptr = &anything_changed, | ||
| 56 | .source_index = 0, | ||
| 57 | .source = tree.source, | ||
| 58 | }; | ||
| 59 | |||
| 60 | try renderRoot(allocator, &my_stream.stream, tree); | ||
| 61 | |||
| 62 | return anything_changed; | ||
| 63 | } | ||
| 64 | |||
| 65 | fn renderRoot( | ||
| 66 | allocator: &mem.Allocator, | ||
| 67 | stream: var, | ||
| 68 | tree: &ast.Tree, | ||
| 69 | ) (@typeOf(stream).Child.Error || Error)!void { | ||
| 18 | // render all the line comments at the beginning of the file | 70 | // render all the line comments at the beginning of the file |
| 19 | var tok_it = tree.tokens.iterator(0); | 71 | var tok_it = tree.tokens.iterator(0); |
| 20 | while (tok_it.next()) |token| { | 72 | while (tok_it.next()) |token| { |