From 5018f79f6d8da82b6fafbbeeebdecb3799788bc3 Mon Sep 17 00:00:00 2001 From: Alireza Haghdoost Date: Mon, 29 Jun 2015 13:09:37 -0600 Subject: [PATCH] Use _Static_assert() if available 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 --- compiler/compiler.h | 8 ++++++++ configure | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/compiler/compiler.h b/compiler/compiler.h index 40e857c0..a6a74322 100644 --- a/compiler/compiler.h +++ b/compiler/compiler.h @@ -1,5 +1,6 @@ #ifndef FIO_COMPILER_H #define FIO_COMPILER_H +#include #if __GNUC__ >= 4 #include "compiler-gcc4.h" @@ -33,6 +34,11 @@ 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 diff --git a/configure b/configure index e5cf34d6..2e859c7a 100755 --- 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 +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." -- 2.25.1