]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: disable -Wnonnull in unevaluated context [PR115580]
authorMarek Polacek <polacek@redhat.com>
Tue, 4 Mar 2025 18:07:27 +0000 (13:07 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 5 Mar 2025 21:03:30 +0000 (16:03 -0500)
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 <jason@redhat.com>
gcc/c-family/c-common.cc
gcc/testsuite/g++.dg/warn/Wnonnull16.C [new file with mode: 0644]

index 49508fe9ee60f73dbfd3ab5efc42c2db9b387fbe..587d76461e9e839fe3d4e3587c375da68bf0739e 100644 (file)
@@ -6261,6 +6261,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 (file)
index 0000000..8740f35
--- /dev/null
@@ -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" }
+}