pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Paquier <michael@paquier.xyz>
To: Postgres hackers <pgsql-hackers@lists.postgresql.org>
Subject: Refactor compile-time assertion checks for C/C++
Date: Tue, 4 Feb 2020 17:15:03 +0900
Message-ID: <20200204081503.GF2287@paquier.xyz> (raw)

Hi all,

As of [1], I have been playing with the compile time assertions that
we have for expressions, declarations and statements.  And it happens
that it is visibly possible to consolidate the fallback
implementations for C and C++.  Attached is the result of what I am
getting at.  I am adding this patch to next CF.  Thoughts are
welcome.

[1]: https://www.postgresql.org/message-id/201DD0641B056142AC8C6645EC1B5F62014B8E8030@SYD1217

Thanks,
--
Michael

Attachments:

  [text/x-diff] 0001-Refactor-assertion-definitions-in-c.h.patch (2.8K, ../20200204081503.GF2287@paquier.xyz/2-0001-Refactor-assertion-definitions-in-c.h.patch)
  download | inline diff:
From 7596171651ced381a30534568220661c79d90d0f Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Tue, 4 Feb 2020 17:09:59 +0900
Subject: [PATCH] Refactor assertion definitions in c.h

This unifies the C and C++ fallback implementations.
---
 src/include/c.h | 29 +++++++++++------------------
 1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/src/include/c.h b/src/include/c.h
index d10b9812fb..905ecd948f 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -841,39 +841,32 @@ extern void ExceptionalCondition(const char *conditionName,
  * 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 */
+#elif defined(__cplusplus) && \
+	defined(__cpp_static_assert) && __cpp_static_assert >= 200410
+/* Default C++ implementation */
 #define StaticAssertStmt(condition, errmessage) \
-	((void) sizeof(struct { int static_assert_failure : (condition) ? 1 : -1; }))
+	static_assert(condition, errmessage)
 #define StaticAssertExpr(condition, errmessage) \
+	({ StaticAssertStmt(condition, errmessage); })
+#define StaticAssertDecl(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) \
-	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)
 #define StaticAssertExpr(condition, errmessage) \
 	((void) ({ 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
 
 
 /*
-- 
2.25.0

  [application/pgp-signature] signature.asc (832B, ../20200204081503.GF2287@paquier.xyz/3-signature.asc)
  download

view thread (21+ messages)  latest in thread

Message-ID: <20200204081503.GF2287@paquier.xyz>
Permalink:  ../20200204081503.GF2287@paquier.xyz/
Also on:    postgresql.org/message-id/20200204081503.GF2287@paquier.xyz

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: michael@paquier.xyz, pgsql-hackers@lists.postgresql.org
  Subject: Re: Refactor compile-time assertion checks for C/C++
  In-Reply-To: <20200204081503.GF2287@paquier.xyz>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox