0004-Support-pg_nodiscard-on-non-GNU-compilers-that-suppo.patch

text/plain

Filename: 0004-Support-pg_nodiscard-on-non-GNU-compilers-that-suppo.patch
Type: text/plain
Part: 3
Message: Re: pg_attribute_noreturn(), MSVC, C11

Patch

Format: format-patch
Series: patch 0004
Subject: Support pg_nodiscard on non-GNU compilers that support C23
File+
src/include/c.h 7 5
From 181ba6f67169d3fe80026b37adceea1f484218ee Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter@eisentraut.org>
Date: Sat, 28 Dec 2024 11:00:24 +0100
Subject: [PATCH 4/4] Support pg_nodiscard on non-GNU compilers that support
 C23

Support pg_nodiscard on compilers that support C23 attribute syntax.
Previously, only GCC-compatible compilers were supported.

Also, define pg_noreturn similarly using C23 attribute syntax if the
compiler supports C23.  This doesn't have much of an effect right now,
since all compilers that support C23 surely also support the existing
C11-compatible code.  But it keeps pg_nodiscard and pg_noreturn
consistent.
---
 src/include/c.h | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/include/c.h b/src/include/c.h
index eee2f476cb7..0c802f4ea82 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -132,11 +132,11 @@
 
 /*
  * pg_nodiscard means the compiler should warn if the result of a function
- * call is ignored.  The name "nodiscard" is chosen in alignment with
- * (possibly future) C and C++ standards.  For maximum compatibility, use it
- * as a function declaration specifier, so it goes before the return type.
+ * call is ignored.
  */
-#ifdef __GNUC__
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
+#define pg_nodiscard [[__nodiscard__]]
+#elif defined(__GNUC__)
 #define pg_nodiscard __attribute__((warn_unused_result))
 #else
 #define pg_nodiscard
@@ -148,7 +148,9 @@
  * uses __attribute__((noreturn)) in headers, which would get confused if
  * "noreturn" is defined to "_Noreturn", as is done by <stdnoreturn.h>.
  */
-#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
+#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
+#define pg_noreturn [[__noreturn__]]
+#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
 #define pg_noreturn _Noreturn
 #elif defined(__GNUC__)
 #define pg_noreturn __attribute__((noreturn))
-- 
2.47.1