implement -Wsuggest-override
authorTrevor Saunders <tsaunders@mozilla.com>
Tue, 6 Jan 2015 02:02:47 +0000 (02:02 +0000)
committerTrevor Saunders <tbsaunde@gcc.gnu.org>
Tue, 6 Jan 2015 02:02:47 +0000 (02:02 +0000)
c-family/

PR c++/31397
* c.opt (Wsuggest-override): New option.

cp/

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

gcc/

PR c++/31397
* doc/invoke.texi: Document -Wsuggest-override.

From-SVN: r219213

gcc/ChangeLog
gcc/c-family/ChangeLog
gcc/c-family/c.opt
gcc/cp/ChangeLog
gcc/cp/class.c
gcc/doc/invoke.texi
gcc/testsuite/g++.dg/warn/Wsuggest-override.C [new file with mode: 0644]

index a50e56a089a5e24c95022eec945541e953a10bdd..87d60b3dc2251de3269c9876f74d4284c8a3934d 100644 (file)
@@ -1,3 +1,8 @@
+2015-01-05  Trevor Saunders  <tsaunders@mozilla.com>
+
+       PR c++/31397
+       * doc/invoke.texi: Document -Wsuggest-override.
+
 2015-01-05  Radovan Obradovic  <radovan.obradovic@imgtec.com>
 
        PR rtl-optimization/64287
index 47eb1597c6f44a1572da76ad1df53feb33b5285a..4c86fa511d35800548c6f9cbe2f3807d958548d4 100644 (file)
@@ -1,3 +1,8 @@
+2015-01-05  Trevor Saunders  <tsaunders@mozilla.com>
+
+       PR c++/31397
+       * c.opt (Wsuggest-override): New option.
+
 2015-01-05  Jakub Jelinek  <jakub@redhat.com>
 
        Update copyright years.
index 6c23fa89a5576cdc891d61b3b02d2230ba41dd00..064c69e5a1d920c2d39d35da5ed4da7331239a75 100644 (file)
@@ -578,6 +578,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
index 1163df3d427c158f2b1b5c52de9a4dac0212aa59..004629b884897ba5479bcdf0dfd40d533dcbfc58 100644 (file)
@@ -1,3 +1,9 @@
+2015-01-05  Trevor Saunders  <tsaunders@mozilla.com>
+
+       PR c++/31397
+       * class.c (check_for_override): Warn when a virtual function is an
+       override not marked override.
+
 2015-01-05  Trevor Saunders  <tsaunders@mozilla.com>
 
        * class.c (warn_hidden): Use auto_vec<tree> instead of tree_list to
index 3b4aff89f7b219bb06b6e0c11f533b965a8137c0..b798a52702fd9f5010c5e86c45d666310353c3f8 100644 (file)
@@ -2811,6 +2811,10 @@ check_for_override (tree decl, tree ctype)
     {
       DECL_VINDEX (decl) = decl;
       overrides_found = true;
+      if (warn_override && !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))
index 21784aa2d8fc7c338e03f5eb120792972deefe06..c7a61b8fd53a54641dae9a6a4e2d110eb11ababd 100644 (file)
@@ -277,7 +277,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
@@ -4282,6 +4282,10 @@ class hierarchy graph is more complete. It is recommended to first consider
 suggestions 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 (file)
index 0000000..f820f4b
--- /dev/null
@@ -0,0 +1,23 @@
+// { dg-do compile }
+// { dg-options "-std=c++11 -Wsuggest-override" }
+struct A
+{
+       A();
+       virtual ~A();
+       virtual void f();
+       virtual int bar();
+       int c();
+       operator int();
+       virtual operator float();
+};
+
+struct B : A
+{
+       B();
+       virtual ~B();
+       virtual void f(); // { dg-warning "can be marked override" }
+virtual int bar() override;
+int c();
+operator int();
+virtual operator float(); // { dg-warning "can be marked override" }
+};