Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src-self-hosted/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,37 @@ fn cmdFmt(allocator: &Allocator, args: []const []const u8) !void {
};

for (flags.positionals.toSliceConst()) |file_path| {
var file = try os.File.openRead(allocator, file_path);
defer file.close();

const source_code = io.readFileAlloc(allocator, file_path) catch |err| {
var file = os.File.openRead(allocator, file_path) catch |err| {
try stderr.print("unable to open '{}': {}", file_path, err);
continue;
};
defer file.close();
const size = try file.getEndPos();

var adaptater = io.FileInStream.init(&file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you meant adapter.


var source_code: []u8 = undefined;
var start_index: usize = 0;

if(size < 16){
source_code = try allocator.alloc(u8, size);
}
else {
source_code = try allocator.alloc(u8, 16);
start_index = try adaptater.stream.read(source_code);
if(mem.eql(u8, source_code, "// zig fmt: skip")){
try stderr.print("won't format {}\n", file_path);
allocator.free(source_code);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this free? Won't it happen at line 756? If you want to handle the potential stderr error, then use errdefer allocator.free(source_code), or even better, move the defer allocator.free(source_code) to before this new code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that continue on the next line will jump back to the top of the loop, without executing line 756.
I didn't move it to the beginning, because in the event that the allocation fails, I assume that source_code would still be undefined, and I didn't want to free(undefined).
I may be mistaken.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it is a fixed size, one thing you could do here is read into a local scope [16]u8 and if it doesn't match seek(0) on the file and then let the actual work happen. Seems like it would be cleaner since there's just one check and an early continue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I actually planned on doing that but couldn't find a seek function. Well, I checked again and found seekTo in file.zig and I now feel stupid 😃

continue;
}
else{
source_code = try allocator.realloc(u8, source_code, size);
}
}
defer allocator.free(source_code);

_ = try adaptater.stream.read(source_code[start_index..]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an error if _ = not used?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read returns the number of bytes read, which we don't care about in this case.


var tree = std.zig.parse(allocator, source_code) catch |err| {
try stderr.print("error parsing file '{}': {}\n", file_path, err);
continue;
Expand Down