https://gcc.gnu.org/g:8c635072ae68f60f14333cf113e75fa25a55becf
commit r14-11387-g8c635072ae68f60f14333cf113e75fa25a55becf Author: Marek Polacek <pola...@redhat.com> Date: Tue Mar 4 13:07:27 2025 -0500 c++: disable -Wnonnull in unevaluated context [PR115580] This PR complains that we issue a -Wnonnull even in a decltype. This fix disables even -Wformat and -Wrestrict. I think that's fine. PR c++/115580 gcc/c-family/ChangeLog: * c-common.cc (check_function_arguments): Return early if c_inhibit_evaluation_warnings. gcc/testsuite/ChangeLog: * g++.dg/warn/Wnonnull16.C: New test. Reviewed-by: Jason Merrill <ja...@redhat.com> (cherry picked from commit 459c8a55567b06522e4b9cc0a4ef62f9d3024526) Diff: --- gcc/c-family/c-common.cc | 3 +++ gcc/testsuite/g++.dg/warn/Wnonnull16.C | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc index d5b28efd6db4..0d05694b6d04 100644 --- a/gcc/c-family/c-common.cc +++ b/gcc/c-family/c-common.cc @@ -6160,6 +6160,9 @@ check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, { bool warned_p = false; + if (c_inhibit_evaluation_warnings) + return warned_p; + /* Check for null being passed in a pointer argument that must be non-null. In C++, this includes the this pointer. We also need to do this if format checking is enabled. */ diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull16.C b/gcc/testsuite/g++.dg/warn/Wnonnull16.C new file mode 100644 index 000000000000..8740f351ac76 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wnonnull16.C @@ -0,0 +1,16 @@ +// PR c++/115580 +// { dg-do compile { target c++11 } } + +class WithMember { +public: + int foo(); +}; + +decltype(((WithMember*)nullptr)->foo()) footype; // { dg-bogus "pointer is null" } + +int f(void*) __attribute__((nonnull)); + +void g() +{ + [[maybe_unused]] decltype(f(nullptr)) b; // { dg-bogus "non-null" } +}