From 1111d3f8cb6e5d223cf2e9af8dd3b9a2ad28ed2c Mon Sep 17 00:00:00 2001
From: "benjamin.franksen" <benjamin.franksen@helmholtz-berlin.de>
Date: Tue, 24 Sep 2013 18:00:45 +0000
Subject: [PATCH] snc: added assert_at and assert_at_expr

---
 src/snc/gen_ss_code.c |  5 +----
 src/snc/main.c        | 32 ++++++++++++++++++++++++++++++++
 src/snc/main.h        |  8 ++++++++
 3 files changed, 41 insertions(+), 4 deletions(-)

diff --git a/src/snc/gen_ss_code.c b/src/snc/gen_ss_code.c
index 6a7c88a4..939cce8e 100644
--- a/src/snc/gen_ss_code.c
+++ b/src/snc/gen_ss_code.c
@@ -535,11 +535,8 @@ static void gen_expr(
 		gen_var_decl(ep->extra.e_decl);
 		break;
 	default:
-#ifdef DEBUG
-		report_at_expr(ep, "unhandled expression (%s:%s)\n",
+		assert_at_expr(impossible, ep, "unhandled expression (%s:%s)\n",
 			expr_type_name(ep), ep->value);
-#endif
-		assert(impossible);
 	}
 }
 
diff --git a/src/snc/main.c b/src/snc/main.c
index cf7bc13d..80be2ccb 100644
--- a/src/snc/main.c
+++ b/src/snc/main.c
@@ -270,6 +270,22 @@ void error_at(const char *src_file, int line_num, const char *format, ...)
 	va_end(args);
 }
 
+/* location plus message and report a bug in snc */
+void assert_at(int cond, const char *src_file, int line_num, const char *format, ...)
+{
+	if (!cond)
+	{
+		va_list args;
+
+		report_loc(src_file, line_num);
+		fprintf(stderr, "snc bug (assertion failed): ");
+		va_start(args, format);
+		vfprintf(stderr, format, args);
+		va_end(args);
+		exit(EXIT_FAILURE);
+	}
+}
+
 void report_at_expr(Expr *ep, const char *format, ...)
 {
 	va_list args;
@@ -307,6 +323,22 @@ void error_at_expr(Expr *ep, const char *format, ...)
 	va_end(args);
 }
 
+/* with location from this expression and report a bug in snc */
+void assert_at_expr(int cond, struct expression *ep, const char *format, ...)
+{
+	if (!cond)
+	{
+		va_list args;
+
+		report_loc(ep->src_file, ep->line_num);
+		fprintf(stderr, "snc bug (assertion failed): ");
+		va_start(args, format);
+		vfprintf(stderr, format, args);
+		va_end(args);
+		exit(EXIT_FAILURE);
+	}
+}
+
 void report(const char *format, ...)
 {
 	va_list args;
diff --git a/src/snc/main.h b/src/snc/main.h
index e6d1fe3a..0911de82 100644
--- a/src/snc/main.h
+++ b/src/snc/main.h
@@ -39,6 +39,10 @@ __attribute__((format(printf,3,4)));
 void error_at(const char *src_file, int line_num, const char *format, ...)
 __attribute__((format(printf,3,4)));
 
+/* location plus message and report a bug in snc */
+void assert_at(int cond, const char *src_file, int line_num, const char *format, ...)
+__attribute__((format(printf,4,5)));
+
 /* with location from this expression */
 struct expression;
 void report_at_expr(struct expression *ep, const char *format, ...)
@@ -52,6 +56,10 @@ __attribute__((format(printf,2,3)));
 void error_at_expr(struct expression *ep, const char *format, ...)
 __attribute__((format(printf,2,3)));
 
+/* with location from this expression and report a bug in snc */
+void assert_at_expr(int cond, struct expression *ep, const char *format, ...)
+__attribute__((format(printf,3,4)));
+
 /* message only */
 void report(const char *format, ...)
 __attribute__((format(printf,1,2)));
-- 
GitLab