Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iytNl-0003GF-Ka for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Feb 2020 08:16:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iytMk-0003qU-5G for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Feb 2020 08:15:26 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iytMi-0003qE-BV for pgsql-hackers@lists.postgresql.org; Tue, 04 Feb 2020 08:15:25 +0000 Received: from [64.147.123.21] (helo=wout5-smtp.messagingengine.com) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iytMf-0000Sj-3F for pgsql-hackers@lists.postgresql.org; Tue, 04 Feb 2020 08:15:23 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id EEA747FE for ; Tue, 4 Feb 2020 03:15:07 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 04 Feb 2020 03:15:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:subject:message-id:mime-version:content-type; s= fm3; bh=/43/GcCb+BN0lfZstc7Uuk9fITSbw/viGvxptco2TTY=; b=S6FmwXEF jE3re6G0qS1bq7BiBapR6ub3IAIqIMi/dtLeu4ZLKXzkSKNh6E2y3gclFWxY7tyf GjcpY3f0wBwwiaULECSdEJgqPTysUKHWkSJ8VHhQ1wvWKjGI0yatgAyKeZTHu33/ iAiViSqVNU575M2lmhBAaX97ucUPOXRuXmHjygf2iIFmD4cpXQqRnxxmCTJFeLfy Gz02fxggelnetYsvhCDSRi2P4oZicJNucMBVwSj8ekyRtePdTiZBdXdsKAomM5Ws uUABEtazFNO947lLLWweKiWyZoBGdHAHkFtT8OKyvq5fmSLmA4S3ovwUwz7YZQF7 VQ26xv2XwJG0SA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:message-id :mime-version:subject:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; bh=/43/GcCb+BN0lfZstc7Uuk9fITSbw /viGvxptco2TTY=; b=q16PFo/SDy+4O1WACZDRmc5BMWn6FnrWl0i0nkBnx11Mw HA4pLZ4XjtnWUKJ4yiJBVuwT6l7fvHeYQ0jlhdNxjSQO9zoW+VGlnbA/E1CtEbzo MaZvcNW81eAezbuLcEeQ4mCk2sQ8EQ152bOEqYh9IrbZzAGBn3uqtLU+dWLhb7bw YX5cO5YNrhWmDQCeGeo0Jxvql9Dwf1YbAawJY3/F1a9Jkc8zPTdM9hQ3hBlZwWik O4+BpF8Btd2bxBl+YhPWeEamLIdS2poKigAQvolulmV0a0GbTSe3j0XawAQsq3M/ Xq/X9zKKU0/zLiMiOP7WRRF8+gPz57TclsdxLp2Ww== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedugedrgeekgdduudejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne gfrhhlucfvnfffucdlfeehmdenucfjughrpeffhffvuffkgggtugesghdtreertddtvden ucfhrhhomhepofhitghhrggvlhcurfgrqhhuihgvrhcuoehmihgthhgrvghlsehprghquh hivghrrdighiiiqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecukfhp peduudekrddvgedurddvgeelrddujedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrg hrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiihii X-ME-Proxy: Received: from paquier.xyz (mp76f1f9ac.ap.nuro.jp [118.241.249.172]) by mail.messagingengine.com (Postfix) with ESMTPA id 5A60A328005D for ; Tue, 4 Feb 2020 03:15:06 -0500 (EST) Date: Tue, 4 Feb 2020 17:15:03 +0900 From: Michael Paquier To: Postgres hackers Subject: Refactor compile-time assertion checks for C/C++ Message-ID: <20200204081503.GF2287@paquier.xyz> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="B8ONY/mu/bqBak9m" Content-Disposition: inline X-Host-Lookup-Failed: Reverse DNS lookup failed for 64.147.123.21 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --B8ONY/mu/bqBak9m Content-Type: multipart/mixed; boundary="hUH5gZbnpyIv7Mn4" Content-Disposition: inline --hUH5gZbnpyIv7Mn4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 --hUH5gZbnpyIv7Mn4 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Refactor-assertion-definitions-in-c.h.patch" Content-Transfer-Encoding: quoted-printable =46rom 7596171651ced381a30534568220661c79d90d0f Mon Sep 17 00:00:00 2001 =46rom: Michael Paquier 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 *conditio= nName, * 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 >=3D 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 >=3D 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 =20 =20 /* --=20 2.25.0 --hUH5gZbnpyIv7Mn4-- --B8ONY/mu/bqBak9m Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAl45KAcACgkQnvQgOdby QH0Ybw//ZVttERbKiTJznypWc3+uzvl+F39d8DJet0lBwetqvg2yLVCH1pEViFe8 E5DT/eLzSTnbpfiJVtVDdmrNWBq9HQjeoBwmMamnTHT353b6ccTJDgNiY7e4Ao14 hiVTUCyDkp08OUNXuwtKEZ5l2fZg0UPnB9kuFhFgUn9AeWekQ/RumQJN6dvvmPx8 K92nRXclDp+Ifgr0h+pSoC1Sui5a53b8rBGuFT4Ba2T577XXNS6WfAyaYIjy2xih LIQatq67besCy7aMIzF8zGgzfs+ABrs6fz+YXE5hTKE9qe6gvFQOQcf1tJjT4H+A /y9Rp1Wp7xl41A8cGr+Z3E8g1hDr3BhpSuMgJUQsUgZD07NFkREuLV6Mf/BqMyov zwe+dDok42KS6mpsHWv4fsRt+62+fpoGOv7+9gFmV+CrYqAo2XGP3JXvSnDtfXc+ GAIXskkt25vm7YTfpII3V6HJiFqY0BvqyIn5YuxSilpXl9GalCaJQd7WplPh92K2 CvywkPgcPXFFYwpEo0Occ7vrwTvbcHXUkvSTMiJkd1PPq+nDgl36KiOdOtB/7od+ z6TB109dRDZw477OltjKOBeDbl/PVbF0rUolLMgmlT3IzwbQVtB3Qf9CDun/Y1N2 rEIwmi6WqdhRT2MrYa+o/ZO5Izk7w0SUK8/eph9YFwywsxuLuy4= =hHm8 -----END PGP SIGNATURE----- --B8ONY/mu/bqBak9m--