Skip to content

Commit b7f64c6

Browse files
committed
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
1 parent a029a06 commit b7f64c6

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/include/c.h

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -836,43 +836,37 @@ 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+
*
839841
* Otherwise we fall back on a kluge that assumes the compiler will complain
840842
* about a negative width for a struct bit-field. This will not include a
841843
* helpful error message, but it beats not getting an error at all.
842844
*/
843-
#ifndef __cplusplus
844-
#ifdef HAVE__STATIC_ASSERT
845+
#if !defined(__cplusplus) && defined(HAVE__STATIC_ASSERT)
846+
/* Default C implementation */
845847
#define StaticAssertStmt(condition, errmessage) \
846848
do { _Static_assert(condition, errmessage); } while(0)
847849
#define StaticAssertExpr(condition, errmessage) \
848850
((void) ({ StaticAssertStmt(condition, errmessage); true; }))
849851
#define StaticAssertDecl(condition, errmessage) \
850852
_Static_assert(condition, errmessage)
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
853+
#elif defined(__cplusplus) && __cpp_static_assert >= 200410
854+
/* Default C++ implementation */
861855
#define StaticAssertStmt(condition, errmessage) \
862856
static_assert(condition, errmessage)
863857
#define StaticAssertExpr(condition, errmessage) \
864858
({ static_assert(condition, errmessage); })
865859
#define StaticAssertDecl(condition, errmessage) \
866860
static_assert(condition, errmessage)
867-
#else /* !__cpp_static_assert */
861+
#else
862+
/* Fallback implementation for C and C++ */
868863
#define StaticAssertStmt(condition, errmessage) \
869-
do { struct static_assert_struct { int static_assert_failure : (condition) ? 1 : -1; }; } while(0)
864+
((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
870865
#define StaticAssertExpr(condition, errmessage) \
871-
((void) ({ StaticAssertStmt(condition, errmessage); }))
866+
StaticAssertStmt(condition, errmessage)
872867
#define StaticAssertDecl(condition, errmessage) \
873868
extern void static_assert_func(int static_assert_failure[(condition) ? 1 : -1])
874-
#endif /* __cpp_static_assert */
875-
#endif /* C++ */
869+
#endif
876870

877871

878872
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy