C++ PATCH for c++/91921 - stray warning with -Woverloaded-virtual

Message ID 20190927014601.GG2954@redhat.com
State New
Headers show
Series
  • C++ PATCH for c++/91921 - stray warning with -Woverloaded-virtual
Related show

Commit Message

Marek Polacek Sept. 27, 2019, 1:46 a.m.
Here we were emitting a stray "by" message if base_fndecl was in
a system header but fns was not.  As usually, we need to guard such
a use.  Further, use inform instead of warning_at.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2019-09-26  Marek Polacek  <polacek@redhat.com>

	PR c++/91921 - stray warning with -Woverloaded-virtual.
	* class.c (warn_hidden): Only emit the second part of
	-Woverloaded-virtual if the first part was issued.  Use inform instead
	warning_at.

	* g++.dg/warn/Woverloaded-2.C: New.
	* g++.dg/warn/Woverloaded-2.h: New.
	* g++.dg/warn/pr61945.C: Turn dg-warning into dg-message.
	* g++.old-deja/g++.mike/warn6.C: Likewise.
	* g++.old-deja/g++.warn/virt1.C: Likewise.

Comments

Jason Merrill Sept. 28, 2019, 2:09 a.m. | #1
On 9/26/19 9:46 PM, Marek Polacek wrote:
>   	  if (base_fndecl)

>   	    {

>   	      /* Here we know it is a hider, and no overrider exists.  */

> -	      warning_at (location_of (base_fndecl),

> -			  OPT_Woverloaded_virtual,

> -			  "%qD was hidden", base_fndecl);

> -	      warning_at (location_of (fns),

> -			  OPT_Woverloaded_virtual, "  by %qD", fns);

> +	      if (warning_at (location_of (base_fndecl),

> +			      OPT_Woverloaded_virtual,

> +			      "%qD was hidden", base_fndecl))

> +		inform (location_of (fns), "  by %qD", fns);


I believe it's also customary to add an auto_diagnostic_group variable 
in these situations.  OK with that change.

Jason

Patch

diff --git gcc/cp/class.c gcc/cp/class.c
index 59a3d1a0496..0fd5e8e188d 100644
--- gcc/cp/class.c
+++ gcc/cp/class.c
@@ -2915,11 +2915,10 @@  warn_hidden (tree t)
 	  if (base_fndecl)
 	    {
 	      /* Here we know it is a hider, and no overrider exists.  */
-	      warning_at (location_of (base_fndecl),
-			  OPT_Woverloaded_virtual,
-			  "%qD was hidden", base_fndecl);
-	      warning_at (location_of (fns),
-			  OPT_Woverloaded_virtual, "  by %qD", fns);
+	      if (warning_at (location_of (base_fndecl),
+			      OPT_Woverloaded_virtual,
+			      "%qD was hidden", base_fndecl))
+		inform (location_of (fns), "  by %qD", fns);
 	    }
       }
 }
diff --git gcc/testsuite/g++.dg/warn/Woverloaded-2.C gcc/testsuite/g++.dg/warn/Woverloaded-2.C
new file mode 100644
index 00000000000..84d65de05ce
--- /dev/null
+++ gcc/testsuite/g++.dg/warn/Woverloaded-2.C
@@ -0,0 +1,9 @@ 
+// PR c++/91921 - stray warning with -Woverloaded-virtual.
+// { dg-options "-Woverloaded-virtual" }
+
+#include "Woverloaded-2.h"
+
+struct B : A
+{
+  void f(int);
+};
diff --git gcc/testsuite/g++.dg/warn/Woverloaded-2.h gcc/testsuite/g++.dg/warn/Woverloaded-2.h
new file mode 100644
index 00000000000..b9e15b0c331
--- /dev/null
+++ gcc/testsuite/g++.dg/warn/Woverloaded-2.h
@@ -0,0 +1,6 @@ 
+#pragma GCC system_header
+
+struct A
+{
+  virtual void f();
+};
diff --git gcc/testsuite/g++.dg/warn/pr61945.C gcc/testsuite/g++.dg/warn/pr61945.C
index 5584d841692..3d40581e5e3 100644
--- gcc/testsuite/g++.dg/warn/pr61945.C
+++ gcc/testsuite/g++.dg/warn/pr61945.C
@@ -7,5 +7,5 @@  class A {
 };
 class B : A {
   template <typename>
-  void foo ();		// { dg-warning "by .B::foo\\(\\)." }
+  void foo ();		// { dg-message "by .B::foo\\(\\)." }
 };
diff --git gcc/testsuite/g++.old-deja/g++.mike/warn6.C gcc/testsuite/g++.old-deja/g++.mike/warn6.C
index 9c694d62559..26759cfb527 100644
--- gcc/testsuite/g++.old-deja/g++.mike/warn6.C
+++ gcc/testsuite/g++.old-deja/g++.mike/warn6.C
@@ -30,13 +30,13 @@  struct D : public B, public B2, public B3 {
 
   virtual void bothsame(int);
 
-  virtual void bothdiff(int);		// { dg-warning "" } 
+  virtual void bothdiff(int);		// { dg-message "" } 
 
   virtual void both2same(int);
   virtual void both2same(float);
 
-  virtual void both12diff(int);		// { dg-warning "" } 
+  virtual void both12diff(int);		// { dg-message "" } 
 
-  virtual void bothfardiff(int);	// { dg-warning "" } 
+  virtual void bothfardiff(int);	// { dg-message "" } 
 };
 
diff --git gcc/testsuite/g++.old-deja/g++.warn/virt1.C gcc/testsuite/g++.old-deja/g++.warn/virt1.C
index 4550dd5e054..c68de8a7e7c 100644
--- gcc/testsuite/g++.old-deja/g++.warn/virt1.C
+++ gcc/testsuite/g++.old-deja/g++.warn/virt1.C
@@ -6,5 +6,5 @@  struct A {
 };
 
 struct B: public A {
-  void f(int); // { dg-warning "" } by this
+  void f(int); // { dg-message "" } by this
 };