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) {...@@ -6072,7 +6072,7 @@ Error file_fetch(CodeGen *g, Buf *resolved_path, Buf *contents) {
6072 if (g->enable_cache) {6072 if (g->enable_cache) {
6073 return cache_add_file_fetch(&g->cache_hash, resolved_path, contents);6073 return cache_add_file_fetch(&g->cache_hash, resolved_path, contents);
6074 } else {6074 } else {
6075 return os_fetch_file_path(resolved_path, contents, false);6075 return os_fetch_file_path(resolved_path, contents);
6076 }6076 }
6077}6077}
60786078
src/cache_hash.cpp+1-1
...@@ -469,7 +469,7 @@ Error cache_add_file(CacheHash *ch, Buf *path) {...@@ -469,7 +469,7 @@ Error cache_add_file(CacheHash *ch, Buf *path) {
469Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {469Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
470 Error err;470 Error err;
471 Buf *contents = buf_alloc();471 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))) {
473 if (verbose) {473 if (verbose) {
474 fprintf(stderr, "unable to read .d file: %s\n", err_str(err));474 fprintf(stderr, "unable to read .d file: %s\n", err_str(err));
475 }475 }
src/codegen.cpp+3-3
...@@ -7870,7 +7870,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {...@@ -7870,7 +7870,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
7870 Buf *contents;7870 Buf *contents;
7871 if (hit) {7871 if (hit) {
7872 contents = buf_alloc();7872 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))) {
7874 fprintf(stderr, "Unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));7874 fprintf(stderr, "Unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
7875 exit(1);7875 exit(1);
7876 }7876 }
...@@ -8299,7 +8299,7 @@ static void gen_root_source(CodeGen *g) {...@@ -8299,7 +8299,7 @@ static void gen_root_source(CodeGen *g) {
8299 Error err;8299 Error err;
8300 // No need for using the caching system for this file fetch because it is handled8300 // No need for using the caching system for this file fetch because it is handled
8301 // separately.8301 // separately.
8302 if ((err = os_fetch_file_path(resolved_path, source_code, true))) {8302 if ((err = os_fetch_file_path(resolved_path, source_code))) {
8303 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(resolved_path), err_str(err));8303 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(resolved_path), err_str(err));
8304 exit(1);8304 exit(1);
8305 }8305 }
...@@ -8374,7 +8374,7 @@ static void gen_global_asm(CodeGen *g) {...@@ -8374,7 +8374,7 @@ static void gen_global_asm(CodeGen *g) {
8374 Buf *asm_file = g->assembly_files.at(i);8374 Buf *asm_file = g->assembly_files.at(i);
8375 // No need to use the caching system for these fetches because they8375 // No need to use the caching system for these fetches because they
8376 // are handled separately.8376 // are handled separately.
8377 if ((err = os_fetch_file_path(asm_file, &contents, false))) {8377 if ((err = os_fetch_file_path(asm_file, &contents))) {
8378 zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));8378 zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
8379 }8379 }
8380 buf_append_buf(&g->global_asm, &contents);8380 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...@@ -45,7 +45,7 @@ Error zig_libc_parse(ZigLibCInstallation *libc, Buf *libc_file, const ZigTarget
45 bool found_keys[array_length(zig_libc_keys)] = {};45 bool found_keys[array_length(zig_libc_keys)] = {};
4646
47 Buf *contents = buf_alloc();47 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))) {
49 if (err != ErrorFileNotFound && verbose) {49 if (err != ErrorFileNotFound && verbose) {
50 fprintf(stderr, "Unable to read '%s': %s\n", buf_ptr(libc_file), err_str(err));50 fprintf(stderr, "Unable to read '%s': %s\n", buf_ptr(libc_file), err_str(err));
51 }51 }
src/main.cpp+2-2
...@@ -331,7 +331,7 @@ int main(int argc, char **argv) {...@@ -331,7 +331,7 @@ int main(int argc, char **argv) {
331 os_path_split(cwd, nullptr, cwd_basename);331 os_path_split(cwd, nullptr, cwd_basename);
332332
333 Buf *build_zig_contents = buf_alloc();333 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))) {
335 fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(build_zig_path), err_str(err));335 fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(build_zig_path), err_str(err));
336 return EXIT_FAILURE;336 return EXIT_FAILURE;
337 }337 }
...@@ -346,7 +346,7 @@ int main(int argc, char **argv) {...@@ -346,7 +346,7 @@ int main(int argc, char **argv) {
346 }346 }
347347
348 Buf *main_zig_contents = buf_alloc();348 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))) {
350 fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(main_zig_path), err_str(err));350 fprintf(stderr, "Unable to read %s: %s\n", buf_ptr(main_zig_path), err_str(err));
351 return EXIT_FAILURE;351 return EXIT_FAILURE;
352 }352 }
src/os.cpp+5-30
...@@ -751,39 +751,15 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {...@@ -751,39 +751,15 @@ Buf os_path_resolve(Buf **paths_ptr, size_t paths_len) {
751#endif751#endif
752}752}
753753
754Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {754Error os_fetch_file(FILE *f, Buf *out_buf) {
755 static const ssize_t buf_size = 0x2000;755 static const ssize_t buf_size = 0x2000;
756 buf_resize(out_buf, buf_size);756 buf_resize(out_buf, buf_size);
757 ssize_t actual_buf_len = 0;757 ssize_t actual_buf_len = 0;
758758
759 bool first_read = true;
760
761 for (;;) {759 for (;;) {
762 size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);760 size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);
763 actual_buf_len += amt_read;761 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
787 if (amt_read != buf_size) {763 if (amt_read != buf_size) {
788 if (feof(f)) {764 if (feof(f)) {
789 buf_resize(out_buf, actual_buf_len);765 buf_resize(out_buf, actual_buf_len);
...@@ -794,7 +770,6 @@ Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {...@@ -794,7 +770,6 @@ Error os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
794 }770 }
795771
796 buf_resize(out_buf, actual_buf_len + buf_size);772 buf_resize(out_buf, actual_buf_len + buf_size);
797 first_read = false;
798 }773 }
799 zig_unreachable();774 zig_unreachable();
800}775}
...@@ -864,8 +839,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,...@@ -864,8 +839,8 @@ static Error os_exec_process_posix(const char *exe, ZigList<const char *> &args,
864839
865 FILE *stdout_f = fdopen(stdout_pipe[0], "rb");840 FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
866 FILE *stderr_f = fdopen(stderr_pipe[0], "rb");841 FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
867 Error err1 = os_fetch_file(stdout_f, out_stdout, false);842 Error err1 = os_fetch_file(stdout_f, out_stdout);
868 Error err2 = os_fetch_file(stderr_f, out_stderr, false);843 Error err2 = os_fetch_file(stderr_f, out_stderr);
869844
870 fclose(stdout_f);845 fclose(stdout_f);
871 fclose(stderr_f);846 fclose(stderr_f);
...@@ -1097,7 +1072,7 @@ Error os_copy_file(Buf *src_path, Buf *dest_path) {...@@ -1097,7 +1072,7 @@ Error os_copy_file(Buf *src_path, Buf *dest_path) {
1097 }1072 }
1098}1073}
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) {
1101 FILE *f = fopen(buf_ptr(full_path), "rb");1076 FILE *f = fopen(buf_ptr(full_path), "rb");
1102 if (!f) {1077 if (!f) {
1103 switch (errno) {1078 switch (errno) {
...@@ -1116,7 +1091,7 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {...@@ -1116,7 +1091,7 @@ Error os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
1116 return ErrorFileSystem;1091 return ErrorFileSystem;
1117 }1092 }
1118 }1093 }
1119 Error result = os_fetch_file(f, out_contents, skip_shebang);1094 Error result = os_fetch_file(f, out_contents);
1120 fclose(f);1095 fclose(f);
1121 return result;1096 return result;
1122}1097}
src/os.hpp+2-2
...@@ -126,8 +126,8 @@ void os_file_close(OsFile *file);...@@ -126,8 +126,8 @@ void os_file_close(OsFile *file);
126Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);126Error ATTRIBUTE_MUST_USE os_write_file(Buf *full_path, Buf *contents);
127Error ATTRIBUTE_MUST_USE os_copy_file(Buf *src_path, Buf *dest_path);127Error 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);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, bool skip_shebang);130Error ATTRIBUTE_MUST_USE os_fetch_file_path(Buf *full_path, Buf *out_contents);
131131
132Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);132Error ATTRIBUTE_MUST_USE os_get_cwd(Buf *out_cwd);
133133
std/zig/ast.zig-3
...@@ -479,7 +479,6 @@ pub const Node = struct {...@@ -479,7 +479,6 @@ pub const Node = struct {
479 doc_comments: ?*DocComment,479 doc_comments: ?*DocComment,
480 decls: DeclList,480 decls: DeclList,
481 eof_token: TokenIndex,481 eof_token: TokenIndex,
482 shebang: ?TokenIndex,
483482
484 pub const DeclList = SegmentedList(*Node, 4);483 pub const DeclList = SegmentedList(*Node, 4);
485484
...@@ -491,7 +490,6 @@ pub const Node = struct {...@@ -491,7 +490,6 @@ pub const Node = struct {
491 }490 }
492491
493 pub fn firstToken(self: *const Root) TokenIndex {492 pub fn firstToken(self: *const Root) TokenIndex {
494 if (self.shebang) |shebang| return shebang;
495 return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();493 return if (self.decls.len == 0) self.eof_token else (self.decls.at(0).*).firstToken();
496 }494 }
497495
...@@ -2235,7 +2233,6 @@ test "iterate" {...@@ -2235,7 +2233,6 @@ test "iterate" {
2235 .doc_comments = null,2233 .doc_comments = null,
2236 .decls = Node.Root.DeclList.init(std.debug.global_allocator),2234 .decls = Node.Root.DeclList.init(std.debug.global_allocator),
2237 .eof_token = 0,2235 .eof_token = 0,
2238 .shebang = null,
2239 };2236 };
2240 var base = &root.base;2237 var base = &root.base;
2241 testing.expect(base.iterate(0) == null);2238 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 {...@@ -22,7 +22,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
22 .base = ast.Node{ .id = ast.Node.Id.Root },22 .base = ast.Node{ .id = ast.Node.Id.Root },
23 .decls = ast.Node.Root.DeclList.init(arena),23 .decls = ast.Node.Root.DeclList.init(arena),
24 .doc_comments = null,24 .doc_comments = null,
25 .shebang = null,
26 // initialized when we get the eof token25 // initialized when we get the eof token
27 .eof_token = undefined,26 .eof_token = undefined,
28 };27 };
...@@ -43,15 +42,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {...@@ -43,15 +42,6 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree {
43 }42 }
44 var tok_it = tree.tokens.iterator(0);43 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
55 // skip over line comments at the top of the file45 // skip over line comments at the top of the file
56 while (true) {46 while (true) {
57 const next_tok = tok_it.peek() orelse break;47 const next_tok = tok_it.peek() orelse break;
std/zig/parser_test.zig-8
...@@ -50,14 +50,6 @@ test "zig fmt: linksection" {...@@ -50,14 +50,6 @@ test "zig fmt: linksection" {
50 );50 );
51}51}
5252
53test "zig fmt: shebang line" {
54 try testCanonical(
55 \\#!/usr/bin/env zig
56 \\pub fn main() void {}
57 \\
58 );
59}
60
61test "zig fmt: correctly move doc comments on struct fields" {53test "zig fmt: correctly move doc comments on struct fields" {
62 try testTransform(54 try testTransform(
63 \\pub const section_64 = extern struct {55 \\pub const section_64 = extern struct {
std/zig/render.zig-5
...@@ -73,11 +73,6 @@ fn renderRoot(...@@ -73,11 +73,6 @@ fn renderRoot(
73) (@typeOf(stream).Child.Error || Error)!void {73) (@typeOf(stream).Child.Error || Error)!void {
74 var tok_it = tree.tokens.iterator(0);74 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
81 // render all the line comments at the beginning of the file76 // render all the line comments at the beginning of the file
82 while (tok_it.next()) |token| {77 while (tok_it.next()) |token| {
83 if (token.id != Token.Id.LineComment) break;78 if (token.id != Token.Id.LineComment) break;