remove Shebang (#!) support

Closes:  #2165
This commit is contained in:
Shawn Landden 2019-04-09 19:16:51 -05:00 committed by Andrew Kelley
parent fb2acaff06
commit 8ef7f6febb
11 changed files with 15 additions and 66 deletions

View File

@ -6072,7 +6072,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);
}
}

View File

@ -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));
}

View File

@ -7870,7 +7870,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);
}
@ -8299,7 +8299,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);
}
@ -8374,7 +8374,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);

View File

@ -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));
}

View File

@ -331,7 +331,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;
}
@ -346,7 +346,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;
}

View File

@ -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);
@ -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();
}
@ -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);
@ -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) {
@ -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;
}

View File

@ -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);

View File

@ -479,7 +479,6 @@ pub const Node = struct {
doc_comments: ?*DocComment,
decls: DeclList,
eof_token: TokenIndex,
shebang: ?TokenIndex,
pub const DeclList = SegmentedList(*Node, 4);
@ -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();
}
@ -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);

View File

@ -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,
};
@ -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;

View File

@ -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 {

View File

@ -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;