authorgravatar for shawn@git.icuShawn Landden <shawn@git.icu> 2019-04-09 19:16:51-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-24 23:34:19-04:00
log8ef7f6febb7132d7a1ee44199fd22006f326de5c
tree4ce5c3a8c7a54a344cee9a0e72de8fad1dd701fd
parentfb2acaff067dd6b385de7f2bd2726bdfebbf841f

remove Shebang (#!) support

Closes: #2165

11 files changed, 15 insertions(+), 66 deletions(-)

src/analyze.cpp+1-1
......@@ -6072,7 +6072,7 @@ Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {
60726072 if (g->enable_cache) {
60736073 return cache_add_file_fetch(&g->cache_hash, resolved_path, contents);
60746074 } else {
6075 return os_fetch_file_path(resolved_path, contents, false);
6075 return os_fetch_file_path(resolved_path, contents);
60766076 }
60776077}
60786078
src/cache_hash.cpp+1-1
......@@ -469,7 +469,7 @@ Error cache_add_file(CacheHash *ch, Buf *path) {
469469Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
470470 Error err;
471471 Buf *contents = buf_alloc();
472 if ((err = os_fetch_file_path(dep_file_path, contents, false))) {
472 if ((err = os_fetch_file_path(dep_file_path, contents))) {
473473 if (verbose) {
474474 fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
475475 }
src/codegen.cpp+3-3
......@@ -7870,7 +7870,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
78707870 Buf *contents;
78717871 if (hit) {
78727872 contents = buf_alloc();
7873 if ((err = os_fetch_file_path(builtin_zig_path, contents, false))) {
7873 if ((err = os_fetch_file_path(builtin_zig_path, contents))) {
78747874 fprintf(stderr, "Unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
78757875 exit(1);
78767876 }
......@@ -8299,7 +8299,7 @@ static void gen_root_source(CodeGen *g) {
82998299 Error err;
83008300 // No need for using the caching system for this file fetch because it is handled
83018301 // separately.
8302 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {
8302 if ((err = os_fetch_file_path(resolved_path, source_code))) {
83038303 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(resolved_path), err_str(err));
83048304 exit(1);
83058305 }
......@@ -8374,7 +8374,7 @@ static void gen_global_asm(CodeGen *g) {
83748374 Buf *asm_file = g->assembly_files.at(i);
83758375 // No need to use the caching system for these fetches because they
83768376 // are handled separately.
8377 if ((err = os_fetch_file_path(asm_file, &contents, false))) {
8377 if ((err = os_fetch_file_path(asm_file, &contents))) {
83788378 zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
83798379 }
83808380 buf_append_buf(&g->global_asm, &contents);
src/libc_installation.cpp+1-1
......@@ -45,7 +45,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
4545 bool found_keys[array_length(zig_libc_keys)] = {};
4646
4747 Buf *contents = buf_alloc();
48 if ((err = os_fetch_file_path(libc_file, contents, false))) {
48 if ((err = os_fetch_file_path(libc_file, contents))) {
4949 if (err != ErrorFileNotFound && verbose) {
5050 fprintf(stderr, "Unable to read '%s': %s\n", buf_ptr(libc_file), err_str(err));
5151 }
src/main.cpp+2-2
......@@ -331,7 +331,7 @@ int main(int argc, char **argv) {
331331 os_path_split(cwd, nullptr, cwd_basename);
332332
333333 Buf *build_zig_contents = buf_alloc();
334 if ((err = os_fetch_file_path(build_zig_path, build_zig_contents, false))) {
334 if ((err = os_fetch_file_path(build_zig_path, build_zig_contents))) {
335335 fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(build_zig_path), err_str(err));
336336 return EXIT_FAILURE;
337337 }
......@@ -346,7 +346,7 @@ int main(int argc, char **argv) {
346346 }
347347
348348 Buf *main_zig_contents = buf_alloc();
349 if ((err = os_fetch_file_path(main_zig_path, main_zig_contents, false))) {
349 if ((err = os_fetch_file_path(main_zig_path, main_zig_contents))) {
350350 fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(main_zig_path), err_str(err));
351351 return EXIT_FAILURE;
352352 }
src/os.cpp+5-30
......@@ -751,39 +751,15 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {
751751#endif
752752}
753753
754Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
754Error os_fetch_file(FILE *f, Buf *out_buf) {
755755 static const ssize_t buf_size = 0x2000;
756756 buf_resize(out_buf, buf_size);
757757 ssize_t actual_buf_len = 0;
758758
759 bool first_read = true;
760
761759 for (;;) {
762760 size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);
763761 actual_buf_len += amt_read;
764762
765 if (skip_shebang && first_read && buf_starts_with_str(out_buf, "#!")) {
766 size_t i = 0;
767 while (true) {
768 if (i > buf_len(out_buf)) {
769 zig_panic("shebang line exceeded %zd characters", buf_size);
770 }
771
772 size_t current_pos = i;
773 i += 1;
774
775 if (out_buf->list.at(current_pos) == '\n') {
776 break;
777 }
778 }
779
780 ZigList<char> *list = &out_buf->list;
781 memmove(list->items, list->items + i, list->length - i);
782 list->length -= i;
783
784 actual_buf_len -= i;
785 }
786
787763 if (amt_read != buf_size) {
788764 if (feof(f)) {
789765 buf_resize(out_buf, actual_buf_len);
......@@ -794,7 +770,6 @@ Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
794770 }
795771
796772 buf_resize(out_buf, actual_buf_len + buf_size);
797 first_read = false;
798773 }
799774 zig_unreachable();
800775}
......@@ -864,8 +839,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
864839
865840 FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
866841 FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
867 Error err1 = os_fetch_file(stdout_f, out_stdout, false);
868 Error err2 = os_fetch_file(stderr_f, out_stderr, false);
842 Error err1 = os_fetch_file(stdout_f, out_stdout);
843 Error err2 = os_fetch_file(stderr_f, out_stderr);
869844
870845 fclose(stdout_f);
871846 fclose(stderr_f);
......@@ -1097,7 +1072,7 @@ Error os_copy_file(Buf *src_path, Buf *dest_path) {
10971072 }
10981073}
10991074
1100Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
1075Error os_fetch_file_path(Buf *full_path, Buf *out_contents) {
11011076 FILE *f = fopen(buf_ptr(full_path), "rb");
11021077 if (!f) {
11031078 switch (errno) {
......@@ -1116,7 +1091,7 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
11161091 return ErrorFileSystem;
11171092 }
11181093 }
1119 Error result = os_fetch_file(f, out_contents, skip_shebang);
1094 Error result = os_fetch_file(f, out_contents);
11201095 fclose(f);
11211096 return result;
11221097}
src/os.hpp+2-2
......@@ -126,8 +126,8 @@ void os_file_close(OsFile *file);
126126Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);
127127Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);
128128
129Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
130Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
129Error ATTRIBUTE_MUST_USE os_fetch_file(FILE *file, Buf *out_contents);
130Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);
131131
132132Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
133133
std/zig/ast.zig-3
......@@ -479,7 +479,6 @@ pub const Node = struct {
479479 doc_comments: ?*DocComment,
480480 decls: DeclList,
481481 eof_token: TokenIndex,
482 shebang: ?TokenIndex,
483482
484483 pub const DeclList = SegmentedList(*Node, 4);
485484
......@@ -491,7 +490,6 @@ pub const Node = struct {
491490 }
492491
493492 pub fn firstToken(self: *const Root) TokenIndex {
494 if (self.shebang) |shebang| return shebang;
495493 return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
496494 }
497495
......@@ -2235,7 +2233,6 @@ test "iterate" {
22352233 .doc_comments = null,
22362234 .decls = Node.Root.DeclList.init(std.debug.global_allocator),
22372235 .eof_token = 0,
2238 .shebang = null,
22392236 };
22402237 var base = &root.base;
22412238 testing.expect(base.iterate(0) == null);
std/zig/parse.zig-10
......@@ -22,7 +22,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
2222 .base = ast.Node{ .id = ast.Node.Id.Root },
2323 .decls = ast.Node.Root.DeclList.init(arena),
2424 .doc_comments = null,
25 .shebang = null,
2625 // initialized when we get the eof token
2726 .eof_token = undefined,
2827 };
......@@ -43,15 +42,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
4342 }
4443 var tok_it = tree.tokens.iterator(0);
4544
46 // skip over shebang line
47 shebang: {
48 const shebang_tok_index = tok_it.index;
49 const shebang_tok_ptr = tok_it.peek() orelse break :shebang;
50 if (shebang_tok_ptr.id != Token.Id.ShebangLine) break :shebang;
51 root_node.shebang = shebang_tok_index;
52 _ = tok_it.next();
53 }
54
5545 // skip over line comments at the top of the file
5646 while (true) {
5747 const next_tok = tok_it.peek() orelse break;
std/zig/parser_test.zig-8
......@@ -50,14 +50,6 @@ test "zig fmt: linksection" {
5050 );
5151}
5252
53test "zig fmt: shebang line" {
54 try testCanonical(
55 \\#!/usr/bin/env zig
56 \\pub fn main() void {}
57 \\
58 );
59}
60
6153test "zig fmt: correctly move doc comments on struct fields" {
6254 try testTransform(
6355 \\pub const section_64 = extern struct {
std/zig/render.zig-5
......@@ -73,11 +73,6 @@ fn renderRoot(
7373) (@typeOf(stream).Child.Error || Error)!void {
7474 var tok_it = tree.tokens.iterator(0);
7575
76 // render the shebang line
77 if (tree.root_node.shebang) |shebang| {
78 try stream.write(tree.tokenSlice(shebang));
79 }
80
8176 // render all the line comments at the beginning of the file
8277 while (tok_it.next()) |token| {
8378 if (token.id != Token.Id.LineComment) break;