Add pg_nodiscard function declaration specifier
authorPeter Eisentraut <peter@eisentraut.org>
Sat, 17 Oct 2020 06:38:39 +0000 (08:38 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Wed, 11 Nov 2020 10:00:27 +0000 (11:00 +0100)
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.  It maps to the GCC
attribute warn_unused_result.

Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://www.postgresql.org/message-id/flat/e3753562-99cd-b65f-5aca-687dfd1ec2fc@2ndquadrant.com

src/include/c.h

index 9cd67f8f765517afaec787d7ef4f04a8b97ae3fa..d5dc3632f74dfb48e38d7aece57ec0fd549f0e12 100644 (file)
 #define pg_attribute_unused()
 #endif
 
+/*
+ * 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.
+ */
+#ifdef __GNUC__
+#define pg_nodiscard __attribute__((warn_unused_result))
+#else
+#define pg_nodiscard
+#endif
+
 /*
  * Append PG_USED_FOR_ASSERTS_ONLY to definitions of variables that are only
  * used in assert-enabled builds, to avoid compiler warnings about unused