Use _Static_assert() if available
authorAlireza Haghdoost <haghdoost@gmail.com>
Mon, 29 Jun 2015 19:09:37 +0000 (13:09 -0600)
committerJens Axboe <axboe@fb.com>
Mon, 29 Jun 2015 19:09:37 +0000 (13:09 -0600)
The current compiletime_assert() doesn't work properly if code
optimization is not enabled, causing all compile time asserts to fail
regardless of the condition. This is because the compiler doesn't
realize that the external function will never get called for the
false case.

Use _Static_assert() if available for compiletime_assert(), and
fallback to the old method if we don't have it.

Signed-off-by: Jens Axboe <axboe@fb.com>
compiler/compiler.h
configure

index 40e857c004c6e4304565ad1e446b4bffaf90eb35..a6a74322549fc66f3210dc9b52406d1fd43a5475 100644 (file)
@@ -1,5 +1,6 @@
 #ifndef FIO_COMPILER_H
 #define FIO_COMPILER_H
+#include <assert.h>
 
 #if __GNUC__ >= 4
 #include "compiler-gcc4.h"
        1; \
 })
 
+
+#if defined(CONFIG_STATIC_ASSERT)
+#define compiletime_assert(condition, msg) _Static_assert(condition, msg)
+
+#else
 #ifndef __compiletime_error
 #define __compiletime_error(message)
 #endif
@@ -56,3 +62,5 @@
        _compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)
 
 #endif
+
+#endif
index e5cf34d68a87fbb4c18e8d5c218e5766679eae2c..2e859c7a7e4e915f245ac939c23659d8cd571a7a 100755 (executable)
--- a/configure
+++ b/configure
@@ -1511,6 +1511,21 @@ if compile_prog "" "" "__sync_fetch_and_add"; then
 fi
 echo "__sync_fetch_and_add          $sfa"
 
+##########################################
+# Check whether we have _Static_assert
+static_assert="no"
+cat > $TMPC << EOF
+#include <assert.h>
+int main(int argc, char **argv)
+{
+  _Static_assert( 1 == 1 , "Check");
+  return 0 ;
+}
+EOF
+if compile_prog "" "" "static_assert"; then
+    static_assert="yes"
+fi
+echo "Static Assert                 $static_assert"
 #############################################################################
 
 if test "$wordsize" = "64" ; then
@@ -1693,6 +1708,9 @@ fi
 if test "$sfa" = "yes" ; then
   output_sym "CONFIG_SFA"
 fi
+if test "$static_assert" = "yes" ; then
+  output_sym "CONFIG_STATIC_ASSERT"
+fi
 
 if test "$zlib" = "no" ; then
   echo "Consider installing zlib-dev (zlib-devel), some fio features depend on it."