Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit faa650a

Browse files
committed
Revert "Refactor compile-time assertion checks in c.h"
This reverts commit b7f64c6, 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
1 parent 33753ac commit faa650a

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

src/include/c.h

+17-11
Original file line numberDiff line numberDiff line change
@@ -836,37 +836,43 @@ extern void ExceptionalCondition(const char *conditionName,
836836
* The macro StaticAssertDecl() is suitable for use at file scope (outside of
837837
* any function).
838838
*
839-
* On recent C++ compilers, we can use standard static_assert().
840-
*
841839
* Otherwise we fall back on a kluge that assumes the compiler will complain
842840
* about a negative width for a struct bit-field. This will not include a
843841
* helpful error message, but it beats not getting an error at all.
844842
*/
845-
#if !defined(__cplusplus) && defined(HAVE__STATIC_ASSERT)
846-
/* Default C implementation */
843+
#ifndef __cplusplus
844+
#ifdef HAVE__STATIC_ASSERT
847845
#define StaticAssertStmt(condition, errmessage) \
848846
do { _Static_assert(condition, errmessage); } while(0)
849847
#define StaticAssertExpr(condition, errmessage) \
850848
((void) ({ StaticAssertStmt(condition, errmessage); true; }))
851849
#define StaticAssertDecl(condition, errmessage) \
852850
_Static_assert(condition, errmessage)
853-
#elif defined(__cplusplus) && __cpp_static_assert >= 200410
854-
/* Default C++ implementation */
851+
#else /* !HAVE__STATIC_ASSERT */
852+
#define StaticAssertStmt(condition, errmessage) \
853+
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
854+
#define StaticAssertExpr(condition, errmessage) \
855+
StaticAssertStmt(condition, errmessage)
856+
#define StaticAssertDecl(condition, errmessage) \
857+
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
858+
#endif /* HAVE__STATIC_ASSERT */
859+
#else /* C++ */
860+
#if defined(__cpp_static_assert) && __cpp_static_assert >= 200410
855861
#define StaticAssertStmt(condition, errmessage) \
856862
static_assert(condition, errmessage)
857863
#define StaticAssertExpr(condition, errmessage) \
858864
({ static_assert(condition, errmessage); })
859865
#define StaticAssertDecl(condition, errmessage) \
860866
static_assert(condition, errmessage)
861-
#else
862-
/* Fallback implementation for C and C++ */
867+
#else /* !__cpp_static_assert */
863868
#define StaticAssertStmt(condition, errmessage) \
864-
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
869+
do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
865870
#define StaticAssertExpr(condition, errmessage) \
866-
StaticAssertStmt(condition, errmessage)
871+
((void) ({ StaticAssertStmt(condition, errmessage); }))
867872
#define StaticAssertDecl(condition, errmessage) \
868873
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
869-
#endif
874+
#endif /* __cpp_static_assert */
875+
#endif /* C++ */
870876

871877

872878
/*

0 commit comments

Comments
 (0)