git: 0b82dac337e7 - main - cdefs.h: Add back fallback define for __printf0like

From: Warner Losh <imp_at_FreeBSD.org>
Date: Sat, 06 Jul 2024 16:16:45 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=0b82dac337e7db79fa1a78bb29f2de6825a877ab

commit 0b82dac337e7db79fa1a78bb29f2de6825a877ab
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2024-07-06 16:07:52 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-07-06 16:15:12 +0000

    cdefs.h: Add back fallback define for __printf0like
    
    The format function printf0 is originally a FreeBSD extension. clang has
    adopted it as an alias for printf. Starting with gcc 11, gcc doesn't do
    a NULL pointer check for fmtarg. Instead, it has to be tagged with a
    nonnull attribute, so this gives us the behavior we want.
    
    For earlier gcc and other cmopilers, define it away so do not get false
    positives for NULL pointers for the err*/warn* family of functions.
    This also fixes -Wsystem-headers by avoiding print0 entirely. My testing
    for 67d1a1cd9e77 didn't test that case, so I introduced a regression.
    
    All these compilers need to be considered because __printf0like is used
    in err.h and stdlib.h. Since it's used in system headers, it has to
    work on all the compilers we support on FreeBSD, not just the ones that
    can build FreeBSD itself.
    
    __printf0like will likely be deleted in the future, since the proper way
    to do this is with _Nullable or _Nonnull, but the compiler support for
    those hasn't been completely evaluated.
    
    Noticed by: jhb
    Fixes: 67d1a1cd9e77
    Sponsored by: Netflix
    Suggestions by: jhb
    Differential Revision:  https://reviews.freebsd.org/D45836
---
 sys/sys/cdefs.h | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/sys/sys/cdefs.h b/sys/sys/cdefs.h
index b8908138bd65..6b861b1903f6 100644
--- a/sys/sys/cdefs.h
+++ b/sys/sys/cdefs.h
@@ -338,9 +338,7 @@
 
 /*
  * Compiler-dependent macros to declare that functions take printf-like
- * or scanf-like arguments.  They are null except for versions of gcc
- * that are known to support the features properly (old versions of gcc-2
- * didn't permit keeping the keywords out of the application namespace).
+ * or scanf-like arguments.
  */
 #define	__printflike(fmtarg, firstvararg) \
 	    __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
@@ -352,8 +350,18 @@
 #define	__strftimelike(fmtarg, firstvararg) \
 	    __attribute__((__format__ (__strftime__, fmtarg, firstvararg)))
 
-#define	__printf0like(fmtarg, firstvararg) \
-	    __attribute__((__format__ (__printf0__, fmtarg, firstvararg)))
+/*
+ * Like __printflike, but allows fmtarg to be NULL. FreeBSD invented 'printf0'
+ * for this because older versions of gcc issued warnings for NULL first args.
+ * Clang has always had printf and printf0 as aliases. gcc 11.0 now follows
+ * clang. So now this is an alias for __printflike, or nothing. In the future
+ * _Nullable or _Nonnull will replace this.
+ */
+#if defined(__clang__) || __GNUC_PREREQ__(11, 0)
+#define	__printf0like(fmtarg, firstvararg) __printflike(fmtarg, firstvararg)
+#else
+#define	__printf0like(fmtarg, firstvararg)
+#endif
 
 #define	__strong_reference(sym,aliassym)	\
 	extern __typeof (sym) aliassym __attribute__ ((__alias__ (#sym)))