diff mbox

[2/2,C++] pr31397 - implement -Wsuggest-override

Message ID 1417069734-23041-2-git-send-email-tsaunders@mozilla.com
State New
Headers show

Commit Message

Trevor Saunders Nov. 27, 2014, 6:28 a.m. UTC
From: Trevor Saunders <tsaunders@mozilla.com>

Hi,

the idea in the pr of providing a warning that tells people where they can add
override seems reasonable to me.

bootstrapped + regtested x86_64-unknown--linux-gnu (new test passes), ok?

Trev

c-family/

	* c.opt (Wsuggest-override): New option.

cp/

	* class.c (check_for_override): Warn when a virtual function is an
	override not marked override.

gcc/

	* doc/invoke.texi: Document -Wsuggest-override.

Comments

Jason Merrill Dec. 17, 2014, 9:21 p.m. UTC | #1
On 11/27/2014 01:28 AM, tsaunders@mozilla.com wrote:
> +      if (warn_override && DECL_VIRTUAL_P (decl) && !DECL_OVERRIDE_P (decl)

Why check DECL_VIRTUAL_P here?

> +	warning_at(DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,

Space before the (.

Jason
Trevor Saunders Dec. 18, 2014, 4:41 p.m. UTC | #2
On Wed, Dec 17, 2014 at 04:21:31PM -0500, Jason Merrill wrote:
> On 11/27/2014 01:28 AM, tsaunders@mozilla.com wrote:
> >+      if (warn_override && DECL_VIRTUAL_P (decl) && !DECL_OVERRIDE_P (decl)
> 
> Why check DECL_VIRTUAL_P here?

I think it was to avoid warning when decl is hiding a non virtual method
on the parent class, but I guess that won't happen anyway? so I'll
remove it.

Trev

> 
> >+	warning_at(DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
> 
> Space before the (.
> 
> Jason
>
diff mbox

Patch

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 85dcb98..259b520 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -574,6 +574,11 @@  Wsuggest-attribute=format
 C ObjC C++ ObjC++ Var(warn_suggest_attribute_format) Warning
 Warn about functions which might be candidates for format attributes
 
+Wsuggest-override
+C++ ObjC++ Var(warn_override) Warning
+Suggest that the override keyword be used when the declaration of a virtual
+function overrides another.
+
 Wswitch
 C ObjC C++ ObjC++ Var(warn_switch) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
 Warn about enumerated switches, with no default, missing a case
diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 16279df..515f33f 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -2777,6 +2777,10 @@  check_for_override (tree decl, tree ctype)
     {
       DECL_VINDEX (decl) = decl;
       overrides_found = true;
+      if (warn_override && DECL_VIRTUAL_P (decl) && !DECL_OVERRIDE_P (decl)
+	  && !DECL_DESTRUCTOR_P (decl))
+	warning_at(DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override,
+		   "%q+D can be marked override", decl);
     }
 
   if (DECL_VIRTUAL_P (decl))
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 89edddb..8741e8e 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -275,7 +275,7 @@  Objective-C and Objective-C++ Dialects}.
 -Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
 -Wstrict-aliasing=n @gol -Wstrict-overflow -Wstrict-overflow=@var{n} @gol
 -Wsuggest-attribute=@r{[}pure@r{|}const@r{|}noreturn@r{|}format@r{]} @gol
--Wsuggest-final-types @gol -Wsuggest-final-methods @gol
+-Wsuggest-final-types @gol -Wsuggest-final-methods @gol -Wsuggest-override @gol
 -Wmissing-format-attribute @gol
 -Wswitch  -Wswitch-default  -Wswitch-enum -Wswitch-bool -Wsync-nand @gol
 -Wsystem-headers  -Wtrampolines  -Wtrigraphs  -Wtype-limits  -Wundef @gol
@@ -4255,6 +4255,10 @@  effective with link time optimization, where the information about the class
 hiearchy graph is more complete. It is recommended to first consider suggestins
 of @option{-Wsuggest-final-types} and then rebuild with new annotations.
 
+@item -Wsuggest-override
+Warn about overriding virtual functions that are not marked with the override
+keyword.
+
 @item -Warray-bounds
 @opindex Wno-array-bounds
 @opindex Warray-bounds
diff --git a/gcc/testsuite/g++.dg/warn/Wsuggest-override.C b/gcc/testsuite/g++.dg/warn/Wsuggest-override.C
new file mode 100644
index 0000000..929d365
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wsuggest-override.C
@@ -0,0 +1,21 @@ 
+// { dg-do compile }
+// { dg-options "-std=c++11 -Wsuggest-override" }
+struct A
+{
+	A();
+	virtual ~A();
+	virtual void f();
+	virtual int bar();
+	operator int();
+	virtual operator float();
+};
+
+struct B : A
+{
+	B();
+	virtual ~B();
+	virtual void f(); // { dg-warning "can be marked override" }
+virtual int bar() override;
+operator int();
+virtual operator float(); // { dg-warning "can be marked override" }
+};