Commit ff3cdbc3a0

Andrew Kelley <andrew@ziglang.org>
2019-04-17 21:58:20
stage1 assertions always on, and have stack traces
1 parent 4ad7d09
src/buffer.hpp
@@ -10,7 +10,6 @@
 
 #include "list.hpp"
 
-#include <assert.h>
 #include <stdint.h>
 #include <ctype.h>
 #include <stdarg.h>
src/error.hpp
@@ -8,8 +8,6 @@
 #ifndef ERROR_HPP
 #define ERROR_HPP
 
-#include <assert.h>
-
 enum Error {
     ErrorNone,
     ErrorNoMem,
@@ -56,8 +54,6 @@ enum Error {
 
 const char *err_str(Error err);
 
-static inline void assertNoError(Error err) {
-    assert(err == ErrorNone);
-}
+#define assertNoError(err) assert((err) == ErrorNone);
 
 #endif
src/list.hpp
@@ -10,8 +10,6 @@
 
 #include "util.hpp"
 
-#include <assert.h>
-
 template<typename T>
 struct ZigList {
     void deinit() {
src/userland.cpp
@@ -2,9 +2,23 @@
 // src-self-hosted/stage1.zig
 
 #include "userland.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+void stage2_translate_c(void) {
+    const char *msg = "stage0 called stage2_translate_c";
+    stage2_panic(msg, strlen(msg));
+}
 
-void stage2_translate_c(void) {}
 void stage2_zen(const char **ptr, size_t *len) {
-    *ptr = nullptr;
-    *len = 0;
+    const char *msg = "stage0 called stage2_zen";
+    stage2_panic(msg, strlen(msg));
+}
+
+void stage2_panic(const char *ptr, size_t len) {
+    fwrite(ptr, 1, len, stderr);
+    fprintf(stderr, "\n");
+    fflush(stderr);
+    abort();
 }
src/userland.h
@@ -20,4 +20,6 @@ ZIG_USERLAND_EXTERN_C void stage2_translate_c(void);
 
 ZIG_USERLAND_EXTERN_C void stage2_zen(const char **ptr, size_t *len);
 
+ZIG_USERLAND_EXTERN_C void stage2_panic(const char *ptr, size_t len);
+
 #endif
src/util.cpp
@@ -10,17 +10,25 @@
 #include <stdarg.h>
 
 #include "util.hpp"
+#include "userland.h"
 
 void zig_panic(const char *format, ...) {
     va_list ap;
     va_start(ap, format);
     vfprintf(stderr, format, ap);
-    fprintf(stderr, "\n");
     fflush(stderr);
     va_end(ap);
+    stage2_panic(nullptr, 0);
     abort();
 }
 
+void assert(bool ok) {
+    if (!ok) {
+        const char *msg = "Assertion failed. This is a bug in the Zig compiler.";
+        stage2_panic(msg, strlen(msg));
+    }
+}
+
 uint32_t int_hash(int i) {
     return (uint32_t)(i % UINT32_MAX);
 }
src/util.hpp
@@ -48,6 +48,10 @@ void zig_panic(const char *format, ...);
 
 #define zig_unreachable() zig_panic("unreachable: %s:%s:%d", __FILE__, __func__, __LINE__)
 
+// Assertions in stage1 are always on, and they call zig @panic.
+#undef assert
+void assert(bool ok);
+
 #if defined(_MSC_VER)
 static inline int clzll(unsigned long long mask) {
     unsigned long lz;
src-self-hosted/stage1.zig
@@ -25,3 +25,7 @@ export fn stage2_zen(ptr: *[*]const u8, len: *usize) void {
     ptr.* = &info_zen;
     len.* = info_zen.len;
 }
+
+export fn stage2_panic(ptr: [*]const u8, len: usize) void {
+    @panic(ptr[0..len]);
+}