gollvm: enable gofrontend assertions for release builds

Change the definition of the 'go_assert' macro to insure that we still
get assertion checking in gofrontend even for release builds. Having
these asserts comes at fairly small overhead, and keeping them
compiled in for release builds helps catch more problems at compile
time.

Change-Id: Id0bed9bdb617d90d38cac63d1fd75c66b1f5500b
Reviewed-on: https://go-review.googlesource.com/c/gollvm/+/192098
Reviewed-by: Cherry Zhang <cherryyz@google.com>
diff --git a/bridge/go-llvm-diagnostics.cpp b/bridge/go-llvm-diagnostics.cpp
index d2c9825..bd51b70 100644
--- a/bridge/go-llvm-diagnostics.cpp
+++ b/bridge/go-llvm-diagnostics.cpp
@@ -103,3 +103,12 @@
   *open_qu = "'";
   *close_qu = "'";
 }
+
+void go_assert_fail(const char *expr, const char *filename,
+                    int line, const char *function)
+{
+  llvm::errs() << "llvm-goc: " << filename << ":" << line << ": "
+               << function << ": assertion '"
+               << expr << "' failed.\n";
+  abort();
+}
diff --git a/bridge/go-system.h b/bridge/go-system.h
index c7a8184..c802d76 100644
--- a/bridge/go-system.h
+++ b/bridge/go-system.h
@@ -41,10 +41,24 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-#define go_assert assert
-
 #define _(x) x
 
+// In a previous version of this file, go_assert was simply #define'd to
+// 'assert', meaning that gofrontend assertions would be compiled away for
+// release builds, which was problematic. This version of go_assert applies for
+// all build flavors, meaning that we'll get assertion checking for release
+// builds. FWIW turning on assertions for all of LLVM can be very expensive from
+// a compile time perspective, but keeping the assertion checks in gofrontend
+// comes with more modest overhead.
+
+extern void go_assert_fail(const char *expr, const char *filename,
+                           int line, const char *function);
+
+#define go_assert(expr) \
+     (static_cast <bool> (expr)						\
+      ? void (0)							\
+      : go_assert_fail (#expr, __FILE__, __LINE__, __PRETTY_FUNCTION__))
+
 #include "llvm-includes.h"
 
 #define go_unreachable() llvm_unreachable("unreachable")