Browse Source

Fix assert when used as an expression
Based on what musl does.

Tibor Nagy 6 years ago
parent
commit
e7f251fdb0
3 changed files with 8 additions and 5 deletions
  1. 3 4
      include/bits/assert.h
  2. 4 1
      tests/assert.c
  3. 1 0
      tests/expected/assert.stdout

+ 3 - 4
include/bits/assert.h

@@ -2,11 +2,10 @@
 #define _BITS_ASSERT_H
 
 #ifdef NDEBUG
-# define assert(cond)
+# define assert(cond) (void) 0
 #else
-# define assert(cond) if (!(cond)) { \
-    __assert(__func__, __FILE__, __LINE__, #cond); \
-  }
+# define assert(cond) \
+  ((void)((cond) || (__assert(__func__, __FILE__, __LINE__, #cond), 0)))
 #endif
 
 #endif

+ 4 - 1
tests/assert.c

@@ -5,9 +5,12 @@
 int main() {
     assert(1 == 1);
     assert(1 + 1 == 2);
-
     puts("yay!");
 
+    if (assert(0 == 0), 1) {
+        puts("groovy!");
+    }
+
     //This fails, but I can't test it because that'd
     //make the test fail lol
     //assert(42 == 1337);

+ 1 - 0
tests/expected/assert.stdout

@@ -1 +1,2 @@
 yay!
+groovy!