[committed] print -Wnonnull note only after warning (PR c++/96310)

Message ID 9b4e50eb-4514-53f8-e3d9-05302fdfe911@gmail.com
State New
Headers show
Series
  • [committed] print -Wnonnull note only after warning (PR c++/96310)
Related show

Commit Message

Mike Crowe via Gcc-patches July 25, 2020, 8:28 p.m.
I committed the patch below in r11-2322.

Martin

Fix PR c++/96310 - Ignoring -Wnonnull via pragma gcc diagnostics still 
produces an unwanted note.

gcc/c-family/ChangeLog:
	PR c++/96310
	* c-common.c (check_nonnull_arg): Print note only when warning was issued.

+}

Patch

diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c
index 51ecde69f2d..e2569c65827 100644
--- a/gcc/c-family/c-common.c
+++ b/gcc/c-family/c-common.c
@@ -5538,7 +5538,7 @@  check_nonnull_arg (void *ctx, tree param, unsigned 
HOST_WIDE_INT param_num)
      {
        warned = warning_at (loc, OPT_Wnonnull,
                            "%qs pointer null", "this");
-      if (pctx->fndecl)
+      if (warned && pctx->fndecl)
         inform (DECL_SOURCE_LOCATION (pctx->fndecl),
                 "in a call to non-static member function %qD",
                 pctx->fndecl);
@@ -5548,7 +5548,7 @@  check_nonnull_arg (void *ctx, tree param, unsigned 
HOST_WIDE_INT param_num)
        warned = warning_at (loc, OPT_Wnonnull,
                            "argument %u null where non-null expected",
                            (unsigned) param_num);
-      if (pctx->fndecl)
+      if (warned && pctx->fndecl)
         inform (DECL_SOURCE_LOCATION (pctx->fndecl),
                 "in a call to function %qD declared %qs",
                 pctx->fndecl, "nonnull");
diff --git a/gcc/testsuite/g++.dg/warn/Wnonnull8.C 
b/gcc/testsuite/g++.dg/warn/Wnonnull8.C
new file mode 100644
index 00000000000..7820b2e3b79
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wnonnull8.C
@@ -0,0 +1,20 @@ 
+/* PR c++/96310 - Ignoring -Wnonnull via pragma gcc diagnostics still 
produces
+   an unwanted note
+   { dg-do compile }
+   { dg-options "-Wall" } */
+
+struct C {
+  void f ();                  // { dg-message "in a call" }
+  void g ();                  // { dg-bogus "in a call" }
+};
+
+void f ()
+{
+  static_cast<C*>(0)->f ();   // { dg-warning "\\\[-Wnonnull" }
+}
+
+void g ()
+{
+#pragma GCC diagnostic ignored "-Wnonnull"
+  static_cast<C*>(0)->g ();