Commit faa650a9 authored by Michael Paquier's avatar Michael Paquier

Revert "Refactor compile-time assertion checks in c.h"

This reverts commit b7f64c64, which broke the fallback implementation for
C++.  We have discussed a couple of alternatives to reduce the number of
implementations for those asserts, but nothing allowing to reduce the
number of implementations down to three instead of four, so there is no
benefit in keeping this patch.

Thanks to Tom Lane for the discussion.

Discussion: https://postgr.es/m/20200313115033.GA183471@paquier.xyz
parent 33753ac9
...@@ -836,37 +836,43 @@ extern void ExceptionalCondition(const char *conditionName, ...@@ -836,37 +836,43 @@ extern void ExceptionalCondition(const char *conditionName,
* The macro StaticAssertDecl() is suitable for use at file scope (outside of * The macro StaticAssertDecl() is suitable for use at file scope (outside of
* any function). * any function).
* *
* On recent C++ compilers, we can use standard static_assert().
*
* Otherwise we fall back on a kluge that assumes the compiler will complain * Otherwise we fall back on a kluge that assumes the compiler will complain
* about a negative width for a struct bit-field. This will not include a * about a negative width for a struct bit-field. This will not include a
* helpful error message, but it beats not getting an error at all. * helpful error message, but it beats not getting an error at all.
*/ */
#if !defined(__cplusplus) && defined(HAVE__STATIC_ASSERT) #ifndef __cplusplus
/* Default C implementation */ #ifdef HAVE__STATIC_ASSERT
#define StaticAssertStmt(condition, errmessage) \ #define StaticAssertStmt(condition, errmessage) \
do { _Static_assert(condition, errmessage); } while(0) do { _Static_assert(condition, errmessage); } while(0)
#define StaticAssertExpr(condition, errmessage) \ #define StaticAssertExpr(condition, errmessage) \
((void) ({ StaticAssertStmt(condition, errmessage); true; })) ((void) ({ StaticAssertStmt(condition, errmessage); true; }))
#define StaticAssertDecl(condition, errmessage) \ #define StaticAssertDecl(condition, errmessage) \
_Static_assert(condition, errmessage) _Static_assert(condition, errmessage)
#elif defined(__cplusplus) && __cpp_static_assert >= 200410 #else /* !HAVE__STATIC_ASSERT */
/* Default C++ implementation */ #define StaticAssertStmt(condition, errmessage) \
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
#define StaticAssertExpr(condition, errmessage) \
StaticAssertStmt(condition, errmessage)
#define StaticAssertDecl(condition, errmessage) \
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
#endif /* HAVE__STATIC_ASSERT */
#else /* C++ */
#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
#define StaticAssertStmt(condition, errmessage) \ #define StaticAssertStmt(condition, errmessage) \
static_assert(condition, errmessage) static_assert(condition, errmessage)
#define StaticAssertExpr(condition, errmessage) \ #define StaticAssertExpr(condition, errmessage) \
({ static_assert(condition, errmessage); }) ({ static_assert(condition, errmessage); })
#define StaticAssertDecl(condition, errmessage) \ #define StaticAssertDecl(condition, errmessage) \
static_assert(condition, errmessage) static_assert(condition, errmessage)
#else #else /* !__cpp_static_assert */
/* Fallback implementation for C and C++ */
#define StaticAssertStmt(condition, errmessage) \ #define StaticAssertStmt(condition, errmessage) \
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; })) do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
#define StaticAssertExpr(condition, errmessage) \ #define StaticAssertExpr(condition, errmessage) \
StaticAssertStmt(condition, errmessage) ((void) ({ StaticAssertStmt(condition, errmessage); }))
#define StaticAssertDecl(condition, errmessage) \ #define StaticAssertDecl(condition, errmessage) \
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1]) extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
#endif #endif /* __cpp_static_assert */
#endif /* C++ */
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment