summaryrefslogtreecommitdiff
path: root/src/include/c.h
diff options
context:
space:
mode:
authorMichael Paquier <michael@paquier.xyz>2020-03-13 15:04:11 +0900
committerMichael Paquier <michael@paquier.xyz>2020-03-13 15:04:11 +0900
commitb7f64c64d3262c309e2c5e8c11fd111b1dbd1a4a (patch)
treedea82b98b8f78a87955348bfb59e79785055196f /src/include/c.h
parenta029a0641cce002acbb5e8ac6ec5533e599a9aea (diff)
downloadpostgresql-b7f64c64d3262c309e2c5e8c11fd111b1dbd1a4a.tar.gz
Refactor compile-time assertion checks in c.h
This commit refactors and simplifies the definitions of StaticAssertStmt, StaticAssertExpr and StaticAssertDecl. By unifying the C and C++ fallback implementations, this reduces the number of different implementations from four to three. Author: Michael Paquier Reviewed-by: Georgios Kokolatos, Tom Lane Discussion: https://postgr.es/m/20200204081503.GF2287@paquier.xyz
Diffstat (limited to 'src/include/c.h')
-rw-r--r--src/include/c.h28
1 files changed, 11 insertions, 17 deletions
diff --git a/src/include/c.h b/src/include/c.h
index 831c89f473..6558801e5f 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -836,43 +836,37 @@ extern void ExceptionalCondition(const char *conditionName,
* The macro StaticAssertDecl() is suitable for use at file scope (outside of
* 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
* 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.
*/
-#ifndef __cplusplus
-#ifdef HAVE__STATIC_ASSERT
+#if !defined(__cplusplus) && defined(HAVE__STATIC_ASSERT)
+/* Default C implementation */
#define StaticAssertStmt(condition, errmessage) \
do { _Static_assert(condition, errmessage); } while(0)
#define StaticAssertExpr(condition, errmessage) \
((void) ({ StaticAssertStmt(condition, errmessage); true; }))
#define StaticAssertDecl(condition, errmessage) \
_Static_assert(condition, errmessage)
-#else /* !HAVE__STATIC_ASSERT */
-#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
+#elif defined(__cplusplus) && __cpp_static_assert >= 200410
+/* Default C++ implementation */
#define StaticAssertStmt(condition, errmessage) \
static_assert(condition, errmessage)
#define StaticAssertExpr(condition, errmessage) \
({ static_assert(condition, errmessage); })
#define StaticAssertDecl(condition, errmessage) \
static_assert(condition, errmessage)
-#else /* !__cpp_static_assert */
+#else
+/* Fallback implementation for C and C++ */
#define StaticAssertStmt(condition, errmessage) \
- do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
+ ((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
#define StaticAssertExpr(condition, errmessage) \
- ((void) ({ StaticAssertStmt(condition, errmessage); }))
+ StaticAssertStmt(condition, errmessage)
#define StaticAssertDecl(condition, errmessage) \
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
-#endif /* __cpp_static_assert */
-#endif /* C++ */
+#endif
/*