]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Use standard C23 and C++ attributes if available
authorPeter Eisentraut <peter@eisentraut.org>
Wed, 1 Apr 2026 06:03:01 +0000 (08:03 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 1 Apr 2026 06:15:02 +0000 (08:15 +0200)
Use the standard C23 and C++ attributes [[nodiscard]], [[noreturn]],
and [[maybe_unused]], if available.

This makes pg_nodiscard and pg_attribute_unused() available in
not-GCC-compatible compilers that support C23 as well as in C++.

For pg_noreturn, we can now drop the GCC-specific and MSVC-specific
fallbacks, because the C11 and the C++ implementation will now cover
all required cases.

Note, in a few places, we need to change the position of the attribute
because it's not valid in that place in C23.

Discussion: https://www.postgresql.org/message-id/flat/pxr5b3z7jmkpenssra5zroxi7qzzp6eswuggokw64axmdixpnk@zbwxuq7gbbcw

src/backend/utils/mmgr/slab.c
src/include/c.h
src/include/lib/radixtree.h
src/test/modules/worker_spi/worker_spi.c

index 36d0ad6d320ba1a6cf48327ee24740f553074ed9..dd1db9566d15411883522dd9524253686c4ad71b 100644 (file)
@@ -629,8 +629,8 @@ SlabAllocFromNewBlock(MemoryContext context, Size size, int flags)
  *             to setup the stack frame in SlabAlloc.  For performance reasons, we
  *             want to avoid that.
  */
-pg_noinline
 pg_noreturn
+pg_noinline
 static void
 SlabAllocInvalidSize(MemoryContext context, Size size)
 {
index 0b2eb9ce8661705ba04ea4e201ac36d9e962b339..88d13ec9993465b2a56a3fc6c1e5398fd3e3b131 100644 (file)
@@ -119,6 +119,8 @@ extern "C++"
 /*
  * Attribute macros
  *
+ * C23: https://en.cppreference.com/w/c/language/attributes.html
+ * C++: https://en.cppreference.com/w/cpp/language/attributes.html
  * GCC: https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
  * GCC: https://gcc.gnu.org/onlinedocs/gcc/Type-Attributes.html
  * Clang: https://clang.llvm.org/docs/AttributeReference.html
@@ -128,13 +130,20 @@ extern "C++"
  * For compilers which don't support __has_attribute, we just define
  * __has_attribute(x) to 0 so that we can define macros for various
  * __attribute__s more easily below.
+ *
+ * Note that __has_attribute only tells about GCC-style attributes, not C23 or
+ * C++ attributes.
  */
 #ifndef __has_attribute
 #define __has_attribute(attribute) 0
 #endif
 
-/* only GCC supports the unused attribute */
-#ifdef __GNUC__
+/*
+ * pg_attribute_unused() suppresses compiler warnings on unused entities.
+ */
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
+#define pg_attribute_unused() [[maybe_unused]]
+#elif defined(__GNUC__)
 #define pg_attribute_unused() __attribute__((unused))
 #else
 #define pg_attribute_unused()
@@ -154,11 +163,11 @@ extern "C++"
 
 /*
  * pg_nodiscard means the compiler should warn if the result of a function
- * call is ignored.  The name "nodiscard" is chosen in alignment with the C23
- * standard attribute with the same name.  For maximum forward compatibility,
- * place it before the declaration.
+ * call is ignored.
  */
-#ifdef __GNUC__
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || (defined(__cplusplus) && __cplusplus >= 201703L)
+#define pg_nodiscard [[nodiscard]]
+#elif defined(__GNUC__)
 #define pg_nodiscard __attribute__((warn_unused_result))
 #else
 #define pg_nodiscard
@@ -170,18 +179,15 @@ extern "C++"
  * uses __attribute__((noreturn)) in headers, which would get confused if
  * "noreturn" is defined to "_Noreturn", as is done by <stdnoreturn.h>.
  *
- * In a declaration, function specifiers go before the function name.  The
- * common style is to put them before the return type.  (The MSVC fallback has
- * the same requirement.  The GCC fallback is more flexible.)
+ * C23 attributes must be placed at the start of a declaration or statement.
+ * C11 function specifiers go before the function name in a declaration, but
+ * it is common style (and required for C23 compatibility) to put them before
+ * the return type.
  */
-#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) && !defined(__cplusplus)
-#define pg_noreturn _Noreturn
-#elif defined(__GNUC__)
-#define pg_noreturn __attribute__((noreturn))
-#elif defined(_MSC_VER)
-#define pg_noreturn __declspec(noreturn)
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L) || defined(__cplusplus)
+#define pg_noreturn [[noreturn]]
 #else
-#define pg_noreturn
+#define pg_noreturn _Noreturn
 #endif
 
 /*
index e6c9a591c173f20d7eaaea9985f0bb430a6e2f5e..694c1d1f835d7f3e1d528816b11a97e23b22749e 100644 (file)
@@ -2777,8 +2777,8 @@ RT_STATS(RT_RADIX_TREE * tree)
 /*
  * Print out debugging information about the given node.
  */
-static void
 pg_attribute_unused()
+static void
 RT_DUMP_NODE(RT_NODE * node)
 {
 #ifdef RT_SHMEM
index f65f3e98600df9a82430cb27ee6ff74cf3bd9c24..b635a486349474f0da9c38c9690feb51f14fa6f1 100644 (file)
@@ -45,7 +45,7 @@ PG_MODULE_MAGIC;
 
 PG_FUNCTION_INFO_V1(worker_spi_launch);
 
-PGDLLEXPORT pg_noreturn void worker_spi_main(Datum main_arg);
+pg_noreturn PGDLLEXPORT void worker_spi_main(Datum main_arg);
 
 /* GUC variables */
 static int     worker_spi_naptime = 10;