Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6070,7 +6070,7 @@ Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {
if (g->enable_cache) {
return cache_add_file_fetch(&g->cache_hash, resolved_path, contents);
} else {
return os_fetch_file_path(resolved_path, contents, false);
return os_fetch_file_path(resolved_path, contents);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cache_hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ Error cache_add_file(CacheHash *ch, Buf *path) {
Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
Error err;
Buf *contents = buf_alloc();
if ((err = os_fetch_file_path(dep_file_path, contents, false))) {
if ((err = os_fetch_file_path(dep_file_path, contents))) {
if (verbose) {
fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
}
Expand Down
6 changes: 3 additions & 3 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7814,7 +7814,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
Buf *contents;
if (hit) {
contents = buf_alloc();
if ((err = os_fetch_file_path(builtin_zig_path, contents, false))) {
if ((err = os_fetch_file_path(builtin_zig_path, contents))) {
fprintf(stderr, "Unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
exit(1);
}
Expand Down Expand Up @@ -8233,7 +8233,7 @@ static void gen_root_source(CodeGen *g) {
Error err;
// No need for using the caching system for this file fetch because it is handled
// separately.
if ((err = os_fetch_file_path(resolved_path, source_code, true))) {
if ((err = os_fetch_file_path(resolved_path, source_code))) {
fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(resolved_path), err_str(err));
exit(1);
}
Expand Down Expand Up @@ -8308,7 +8308,7 @@ static void gen_global_asm(CodeGen *g) {
Buf *asm_file = g->assembly_files.at(i);
// No need to use the caching system for these fetches because they
// are handled separately.
if ((err = os_fetch_file_path(asm_file, &contents, false))) {
if ((err = os_fetch_file_path(asm_file, &contents))) {
zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
}
buf_append_buf(&g->global_asm, &contents);
Expand Down
2 changes: 1 addition & 1 deletion src/libc_installation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
bool found_keys[array_length(zig_libc_keys)] = {};

Buf *contents = buf_alloc();
if ((err = os_fetch_file_path(libc_file, contents, false))) {
if ((err = os_fetch_file_path(libc_file, contents))) {
if (err != ErrorFileNotFound && verbose) {
fprintf(stderr, "Unable to read '%s': %s\n", buf_ptr(libc_file), err_str(err));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ int main(int argc, char **argv) {
os_path_split(cwd, nullptr, cwd_basename);

Buf *build_zig_contents = buf_alloc();
if ((err = os_fetch_file_path(build_zig_path, build_zig_contents, false))) {
if ((err = os_fetch_file_path(build_zig_path, build_zig_contents))) {
fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(build_zig_path), err_str(err));
return EXIT_FAILURE;
}
Expand All @@ -356,7 +356,7 @@ int main(int argc, char **argv) {
}

Buf *main_zig_contents = buf_alloc();
if ((err = os_fetch_file_path(main_zig_path, main_zig_contents, false))) {
if ((err = os_fetch_file_path(main_zig_path, main_zig_contents))) {
fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(main_zig_path), err_str(err));
return EXIT_FAILURE;
}
Expand Down
35 changes: 5 additions & 30 deletions src/os.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,39 +751,15 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {
#endif
}

Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
Error os_fetch_file(FILE *f, Buf *out_buf) {
static const ssize_t buf_size = 0x2000;
buf_resize(out_buf, buf_size);
ssize_t actual_buf_len = 0;

bool first_read = true;

for (;;) {
size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);
actual_buf_len += amt_read;

if (skip_shebang && first_read && buf_starts_with_str(out_buf, "#!")) {
size_t i = 0;
while (true) {
if (i > buf_len(out_buf)) {
zig_panic("shebang line exceeded %zd characters", buf_size);
}

size_t current_pos = i;
i += 1;

if (out_buf->list.at(current_pos) == '\n') {
break;
}
}

ZigList<char> *list = &out_buf->list;
memmove(list->items, list->items + i, list->length - i);
list->length -= i;

actual_buf_len -= i;
}

if (amt_read != buf_size) {
if (feof(f)) {
buf_resize(out_buf, actual_buf_len);
Expand All @@ -794,7 +770,6 @@ Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
}

buf_resize(out_buf, actual_buf_len + buf_size);
first_read = false;
}
zig_unreachable();
}
Expand Down Expand Up @@ -864,8 +839,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,

FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
Error err1 = os_fetch_file(stdout_f, out_stdout, false);
Error err2 = os_fetch_file(stderr_f, out_stderr, false);
Error err1 = os_fetch_file(stdout_f, out_stdout);
Error err2 = os_fetch_file(stderr_f, out_stderr);

fclose(stdout_f);
fclose(stderr_f);
Expand Down Expand Up @@ -1097,7 +1072,7 @@ Error os_copy_file(Buf *src_path, Buf *dest_path) {
}
}

Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
Error os_fetch_file_path(Buf *full_path, Buf *out_contents) {
FILE *f = fopen(buf_ptr(full_path), "rb");
if (!f) {
switch (errno) {
Expand All @@ -1116,7 +1091,7 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
return ErrorFileSystem;
}
}
Error result = os_fetch_file(f, out_contents, skip_shebang);
Error result = os_fetch_file(f, out_contents);
fclose(f);
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/os.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ void os_file_close(OsFile file);
Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);
Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);

Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents);
Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);

Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);

Expand Down
3 changes: 0 additions & 3 deletions std/zig/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,6 @@ pub const Node = struct {
doc_comments: ?*DocComment,
decls: DeclList,
eof_token: TokenIndex,
shebang: ?TokenIndex,

pub const DeclList = SegmentedList(*Node, 4);

Expand All @@ -491,7 +490,6 @@ pub const Node = struct {
}

pub fn firstToken(self: *const Root) TokenIndex {
if (self.shebang) |shebang| return shebang;
return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
}

Expand Down Expand Up @@ -2235,7 +2233,6 @@ test "iterate" {
.doc_comments = null,
.decls = Node.Root.DeclList.init(std.debug.global_allocator),
.eof_token = 0,
.shebang = null,
};
var base = &root.base;
testing.expect(base.iterate(0) == null);
Expand Down
10 changes: 0 additions & 10 deletions std/zig/parse.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
.base = ast.Node{ .id = ast.Node.Id.Root },
.decls = ast.Node.Root.DeclList.init(arena),
.doc_comments = null,
.shebang = null,
// initialized when we get the eof token
.eof_token = undefined,
};
Expand All @@ -43,15 +42,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
}
var tok_it = tree.tokens.iterator(0);

// skip over shebang line
shebang: {
const shebang_tok_index = tok_it.index;
const shebang_tok_ptr = tok_it.peek() orelse break :shebang;
if (shebang_tok_ptr.id != Token.Id.ShebangLine) break :shebang;
root_node.shebang = shebang_tok_index;
_ = tok_it.next();
}

// skip over line comments at the top of the file
while (true) {
const next_tok = tok_it.peek() orelse break;
Expand Down
8 changes: 0 additions & 8 deletions std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ test "zig fmt: linksection" {
);
}

test "zig fmt: shebang line" {
try testCanonical(
\\#!/usr/bin/env zig
\\pub fn main() void {}
\\
);
}

test "zig fmt: correctly move doc comments on struct fields" {
try testTransform(
\\pub const section_64 = extern struct {
Expand Down
5 changes: 0 additions & 5 deletions std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ fn renderRoot(
) (@typeOf(stream).Child.Error || Error)!void {
var tok_it = tree.tokens.iterator(0);

// render the shebang line
if (tree.root_node.shebang) |shebang| {
try stream.write(tree.tokenSlice(shebang));
}

// render all the line comments at the beginning of the file
while (tok_it.next()) |token| {
if (token.id != Token.Id.LineComment) break;
Expand Down