From 257cf09472ce5f4a51bf39808e119717fa0e4280 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 1 Dec 2015 00:50:11 -0700 Subject: colored error messages that tell the source file --- src/os.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/os.cpp') diff --git a/src/os.cpp b/src/os.cpp index 61dd1c01ee..bfc8eaa776 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -180,3 +180,7 @@ int os_get_cwd(Buf *out_cwd) { return 0; } + +bool os_stderr_tty(void) { + return isatty(STDERR_FILENO); +} -- cgit v1.2.3 From dfb6682089ad758b7ba72733778a9aa8c544c164 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 1 Dec 2015 02:29:21 -0700 Subject: add test for bad import --- example/multiple_files/main.zig | 2 +- src/analyze.cpp | 2 +- src/analyze.hpp | 3 +++ src/codegen.cpp | 7 +++++- src/error.cpp | 6 +++++ src/error.hpp | 6 +++++ src/os.cpp | 56 +++++++++++++++++++++++++++++++++++------ src/util.hpp | 2 -- test/run_tests.cpp | 4 +++ 9 files changed, 75 insertions(+), 13 deletions(-) (limited to 'src/os.cpp') diff --git a/example/multiple_files/main.zig b/example/multiple_files/main.zig index c7cb2412e2..f5332316ad 100644 --- a/example/multiple_files/main.zig +++ b/example/multiple_files/main.zig @@ -1,4 +1,4 @@ -export executable "test"; +export executable "test-multiple-files"; use "libc.zig"; use "foo.zig"; diff --git a/src/analyze.cpp b/src/analyze.cpp index d7d5aeb5b0..5239835f0f 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -11,7 +11,7 @@ #include "zig_llvm.hpp" #include "os.hpp" -static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { +void add_node_error(CodeGen *g, AstNode *node, Buf *msg) { ErrorMsg *err = allocate(1); err->line_start = node->line; err->column_start = node->column; diff --git a/src/analyze.hpp b/src/analyze.hpp index 0dca23194d..110a3614e0 100644 --- a/src/analyze.hpp +++ b/src/analyze.hpp @@ -9,7 +9,10 @@ #define ZIG_ANALYZE_HPP struct CodeGen; +struct AstNode; +struct Buf; void semantic_analyze(CodeGen *g); +void add_node_error(CodeGen *g, AstNode *node, Buf *msg); #endif diff --git a/src/codegen.cpp b/src/codegen.cpp index 5ce95023e1..baae91a206 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -669,6 +669,7 @@ static void init(CodeGen *g, Buf *source_path) { } static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *source_code) { + int err; Buf full_path = BUF_INIT; os_path_join(g->root_source_dir, source_path, &full_path); @@ -736,7 +737,11 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou Buf full_path = BUF_INIT; os_path_join(g->root_source_dir, &top_level_decl->data.use.path, &full_path); Buf *import_code = buf_alloc(); - os_fetch_file_path(&full_path, import_code); + if ((err = os_fetch_file_path(&full_path, import_code))) { + add_node_error(g, top_level_decl, + buf_sprintf("unable to open \"%s\": %s", buf_ptr(&full_path), err_str(err))); + break; + } codegen_add_code(g, &top_level_decl->data.use.path, import_code); } } diff --git a/src/error.cpp b/src/error.cpp index 7c3064c143..7690dd0776 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -6,6 +6,12 @@ const char *err_str(int err) { case ErrorNoMem: return "out of memory"; case ErrorInvalidFormat: return "invalid format"; case ErrorSemanticAnalyzeFail: return "semantic analyze failed"; + case ErrorAccess: return "access denied"; + case ErrorInterrupted: return "interrupted"; + case ErrorSystemResources: return "lack of system resources"; + case ErrorFileNotFound: return "file not found"; + case ErrorFileSystem: return "file system error"; + case ErrorFileTooBig: return "file too big"; } return "(invalid error)"; } diff --git a/src/error.hpp b/src/error.hpp index 7da2cf8322..742f30700e 100644 --- a/src/error.hpp +++ b/src/error.hpp @@ -13,6 +13,12 @@ enum Error { ErrorNoMem, ErrorInvalidFormat, ErrorSemanticAnalyzeFail, + ErrorAccess, + ErrorInterrupted, + ErrorSystemResources, + ErrorFileNotFound, + ErrorFileSystem, + ErrorFileTooBig, }; const char *err_str(int err); diff --git a/src/os.cpp b/src/os.cpp index bfc8eaa776..be0b01f8a7 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -7,6 +7,7 @@ #include "os.hpp" #include "util.hpp" +#include "error.hpp" #include #include @@ -143,26 +144,65 @@ void os_write_file(Buf *full_path, Buf *contents) { int os_fetch_file(FILE *f, Buf *out_contents) { int fd = fileno(f); struct stat st; - if (fstat(fd, &st)) - zig_panic("unable to stat file: %s", strerror(errno)); + if (fstat(fd, &st)) { + switch (errno) { + case EACCES: + return ErrorAccess; + case ENOENT: + return ErrorFileNotFound; + case ENOMEM: + return ErrorSystemResources; + case EINTR: + return ErrorInterrupted; + case EINVAL: + zig_unreachable(); + default: + return ErrorFileSystem; + } + } off_t big_size = st.st_size; - if (big_size > INT_MAX) - zig_panic("file too big"); + if (big_size > INT_MAX) { + return ErrorFileTooBig; + } int size = (int)big_size; buf_resize(out_contents, size); ssize_t ret = read(fd, buf_ptr(out_contents), size); - if (ret != size) - zig_panic("unable to read file: %s", strerror(errno)); + if (ret != size) { + switch (errno) { + case EINTR: + return ErrorInterrupted; + case EINVAL: + case EISDIR: + zig_unreachable(); + default: + return ErrorFileSystem; + } + } return 0; } int os_fetch_file_path(Buf *full_path, Buf *out_contents) { FILE *f = fopen(buf_ptr(full_path), "rb"); - if (!f) - zig_panic("unable to open %s: %s\n", buf_ptr(full_path), strerror(errno)); + if (!f) { + switch (errno) { + case EACCES: + return ErrorAccess; + case EINTR: + return ErrorInterrupted; + case EINVAL: + zig_unreachable(); + case ENFILE: + case ENOMEM: + return ErrorSystemResources; + case ENOENT: + return ErrorFileNotFound; + default: + return ErrorFileSystem; + } + } int result = os_fetch_file(f, out_contents); fclose(f); return result; diff --git a/src/util.hpp b/src/util.hpp index b3180528dd..74fcf85020 100644 --- a/src/util.hpp +++ b/src/util.hpp @@ -16,8 +16,6 @@ #define BREAKPOINT __asm("int $0x03") -static const int COMPILE_FAILED_ERR_CODE = 10; // chosen with a random number generator - void zig_panic(const char *format, ...) __attribute__((cold)) __attribute__ ((noreturn)) diff --git a/test/run_tests.cpp b/test/run_tests.cpp index 7597ba761a..979ad38997 100644 --- a/test/run_tests.cpp +++ b/test/run_tests.cpp @@ -249,6 +249,10 @@ fn b() {} #version("aoeu") export executable "test"; )SOURCE", 1, ".tmp_source.zig:2:1: error: invalid version string"); + + add_compile_fail_case("bad import", R"SOURCE( +use "bogus-does-not-exist.zig"; + )SOURCE", 1, ".tmp_source.zig:2:1: error: unable to open \"./bogus-does-not-exist.zig\": file not found"); } static void print_compiler_invokation(TestCase *test_case, Buf *zig_stderr) { -- cgit v1.2.3