postgres.git / summary / log / commit / refs
commit 76f4b92bac87fa54bd6dd8bd53e59f93127ec2ef
Author: Peter Eisentraut <peter@eisentraut.org>
Date: Wed Apr 01 06:03:01 2026 +0000
Use standard C23 and C++ attributes if available
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 | 2 +-
src/include/c.h | 38 ++++++++++++++++++--------------
src/include/lib/radixtree.h | 2 +-
src/test/modules/worker_spi/worker_spi.c | 2 +-
4 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/src/backend/utils/mmgr/slab.c b/src/backend/utils/mmgr/slab.c
index 36d0ad6d320..dd1db9566d1 100644
--- a/src/backend/utils/mmgr/slab.c
+++ b/src/backend/utils/mmgr/slab.c
@@ -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)
{
diff --git a/src/include/c.h b/src/include/c.h
index 0b2eb9ce866..88d13ec9993 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -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
/*
diff --git a/src/include/lib/radixtree.h b/src/include/lib/radixtree.h
index e6c9a591c17..694c1d1f835 100644
--- a/src/include/lib/radixtree.h
+++ b/src/include/lib/radixtree.h
@@ -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
diff --git a/src/test/modules/worker_spi/worker_spi.c b/src/test/modules/worker_spi/worker_spi.c
index f65f3e98600..b635a486349 100644
--- a/src/test/modules/worker_spi/worker_spi.c
+++ b/src/test/modules/worker_spi/worker_spi.c
@@ -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;
[parent: c05ad248f99c]