Introduce pg_attribute_nonnull(...)
authorMichael Paquier <[email protected]>
Sat, 2 Jul 2022 03:30:45 +0000 (12:30 +0900)
committerMichael Paquier <[email protected]>
Sat, 2 Jul 2022 03:30:45 +0000 (12:30 +0900)
pg_attribute_nonnull(...) can be used to generate compiler warnings
when a function is called with the specified arguments set to NULL, as
per an idea from Andres Freund.  An empty argument list indicates that
no pointer arguments can be NULL.  pg_attribute_nonnull() only works for
compilers that support the nonnull function attribute.  If nonnull is
not supported, pg_attribute_nonnull() has no effect.

As a beginning, this commit uses it for the DefineCustomXXXVariable()
functions to generate warnings when the "name" and "value" arguments are
set to NULL.  This will likely be expanded to other places in the
future, where it makes sense.

Author: Nathan Bossart
Reviewed by: Michael Paquier, Tom Lane
Discussion: https://postgr.es/m/20220525061739[email protected]

src/include/c.h
src/include/utils/guc.h

index 4f16e589b3ea9e94c201acada3fee48ab8959228..863a16c6a6c52f9151b672983f19929d6e2cf49d 100644 (file)
 #define pg_attribute_no_sanitize_alignment()
 #endif
 
+/*
+ * pg_attribute_nonnull means the compiler should warn if the function is
+ * called with the listed arguments set to NULL.  If no arguments are
+ * listed, the compiler should warn if any pointer arguments are set to NULL.
+ */
+#if __has_attribute (nonnull)
+#define pg_attribute_nonnull(...) __attribute__((nonnull(__VA_ARGS__)))
+#else
+#define pg_attribute_nonnull(...)
+#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
index d5976ecbfb9d112b9db7663df84394886a6cbdf1..4d0920c42e2718b48679c0e477db7fecbf18bbfe 100644 (file)
@@ -307,7 +307,7 @@ extern void DefineCustomBoolVariable(const char *name,
                                     int flags,
                                     GucBoolCheckHook check_hook,
                                     GucBoolAssignHook assign_hook,
-                                    GucShowHook show_hook);
+                                    GucShowHook show_hook) pg_attribute_nonnull(1, 4);
 
 extern void DefineCustomIntVariable(const char *name,
                                    const char *short_desc,
@@ -320,7 +320,7 @@ extern void DefineCustomIntVariable(const char *name,
                                    int flags,
                                    GucIntCheckHook check_hook,
                                    GucIntAssignHook assign_hook,
-                                   GucShowHook show_hook);
+                                   GucShowHook show_hook) pg_attribute_nonnull(1, 4);
 
 extern void DefineCustomRealVariable(const char *name,
                                     const char *short_desc,
@@ -333,7 +333,7 @@ extern void DefineCustomRealVariable(const char *name,
                                     int flags,
                                     GucRealCheckHook check_hook,
                                     GucRealAssignHook assign_hook,
-                                    GucShowHook show_hook);
+                                    GucShowHook show_hook) pg_attribute_nonnull(1, 4);
 
 extern void DefineCustomStringVariable(const char *name,
                                       const char *short_desc,
@@ -344,7 +344,7 @@ extern void DefineCustomStringVariable(const char *name,
                                       int flags,
                                       GucStringCheckHook check_hook,
                                       GucStringAssignHook assign_hook,
-                                      GucShowHook show_hook);
+                                      GucShowHook show_hook) pg_attribute_nonnull(1, 4);
 
 extern void DefineCustomEnumVariable(const char *name,
                                     const char *short_desc,
@@ -356,7 +356,7 @@ extern void DefineCustomEnumVariable(const char *name,
                                     int flags,
                                     GucEnumCheckHook check_hook,
                                     GucEnumAssignHook assign_hook,
-                                    GucShowHook show_hook);
+                                    GucShowHook show_hook) pg_attribute_nonnull(1, 4);
 
 extern void MarkGUCPrefixReserved(const char *className);