Browse Source

Force syntax-checking on assertions even in non-asserting builds.

tags/v0.9.3
Michael Brown 17 years ago
parent
commit
2e41bfd268
1 changed files with 23 additions and 9 deletions
  1. 23
    9
      src/include/assert.h

+ 23
- 9
src/include/assert.h View File

@@ -10,6 +10,25 @@
10 10
  *
11 11
  */
12 12
 
13
+#ifdef NDEBUG
14
+#define ASSERTING 0
15
+#else
16
+#define ASSERTING 1
17
+#endif
18
+
19
+/** printf() for assertions
20
+ *
21
+ * This function exists so that the assert() macro can expand to
22
+ * printf() calls without dragging the printf() prototype into scope.
23
+ *
24
+ * As far as the compiler is concerned, assert_printf() and printf() are
25
+ * completely unrelated calls; it's only at the assembly stage that
26
+ * references to the assert_printf symbol are collapsed into references
27
+ * to the printf symbol.
28
+ */
29
+extern int __attribute__ (( format ( printf, 1, 2 ) )) 
30
+assert_printf ( const char *fmt, ... ) asm ( "printf" );
31
+
13 32
 /**
14 33
  * Assert a condition at run-time.
15 34
  *
@@ -21,10 +40,10 @@
21 40
  */
22 41
 #define assert( condition ) 						   \
23 42
 	do { 								   \
24
-		if ( ! (condition) ) { 					   \
25
-			printf ( "assert(%s) failed at %s line %d [%s]\n", \
26
-				 #condition, __FILE__, __LINE__,	   \
27
-				 __FUNCTION__ );			   \
43
+		if ( ASSERTING && ! (condition) ) { 			   \
44
+			assert_printf ( "assert(%s) failed at %s line "	   \
45
+					"%d [%s]\n", #condition, __FILE__, \
46
+					__LINE__, __FUNCTION__ );	   \
28 47
 		} 							   \
29 48
 	} while ( 0 )
30 49
 
@@ -44,9 +63,4 @@
44 63
                 error_symbol();				\
45 64
         }
46 65
 
47
-#ifdef NDEBUG
48
-#undef assert
49
-#define assert(x) do {} while ( 0 )
50
-#endif
51
-
52 66
 #endif /* _ASSERT_H */

Loading…
Cancel
Save