Allow PG_PRINTF_ATTRIBUTE to be different in C and C++ code.

Although clang claims to be compatible with gcc's printf format
archetypes, this appears to be a falsehood: it likes __syslog__
(which gcc does not, on most platforms) and doesn't accept
gnu_printf.  This means that if you try to use gcc with clang++
or clang with g++, you get compiler warnings when compiling
printf-like calls in our C++ code.  This has been true for quite
awhile, but it's gotten more annoying with the recent appearance
of several buildfarm members that are configured like this.

To fix, run separate probes for the format archetype to use with the
C and C++ compilers, and conditionally define PG_PRINTF_ATTRIBUTE
depending on __cplusplus.

(We could alternatively insist that you not mix-and-match C and
C++ compilers; but if the case works otherwise, this is a poor
reason to insist on that.)

No back-patch for now, but we may want to do that if this
patch survives buildfarm testing.

Discussion: https://postgr.es/m/986485.1764825548@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2025-12-10 17:09:10 -05:00
parent bfb335df58
commit 0909380e4c
6 changed files with 169 additions and 12 deletions

View File

@@ -1897,6 +1897,8 @@ if cc.compiles('''
endif
# Select the format archetype to be used to check printf-type functions.
#
# Need to check a call with %m because netbsd supports gnu_printf but emits a
# warning for each use of %m.
printf_attributes = ['gnu_printf', '__syslog__', 'printf']
@@ -1911,11 +1913,24 @@ attrib_error_args = cc.get_supported_arguments('-Werror=format', '-Werror=ignore
foreach a : printf_attributes
if cc.compiles(testsrc.format(a),
args: test_c_args + attrib_error_args, name: 'format ' + a)
cdata.set('PG_PRINTF_ATTRIBUTE', a)
cdata.set('PG_C_PRINTF_ATTRIBUTE', a)
break
endif
endforeach
# We need to repeat the test for C++ because gcc and clang prefer different
# format archetypes.
if llvm.found()
attrib_error_args = cpp.get_supported_arguments('-Werror=format', '-Werror=ignored-attributes')
foreach a : printf_attributes
if cpp.compiles(testsrc.format(a),
args: attrib_error_args, name: 'cppformat ' + a)
cdata.set('PG_CXX_PRINTF_ATTRIBUTE', a)
break
endif
endforeach
endif
if cc.has_function_attribute('visibility:default') and \
cc.has_function_attribute('visibility:hidden')