[PATCH] Add diagnostic to require that virtual methods be tagged with C++11 'override'

Josh Gao jmg116@gmail.com
Wed Sep 24 19:59:00 GMT 2014


Hi all,

This patch adds a diagnostic that warns when virtual methods that override are not tagged 'override'.

commit c33643ab4f12148ed6cdc5a8a5b0be282c7b0451
Author: Josh Gao <jmg116@gmail.com>
Date:   Wed Sep 24 12:29:39 2014 -0700

    Add diagnostic to require virtual methods to be tagged override.
    
        * c-family/c.opt: Add -Wmissing-virtual-override.
        * cp/class.c: Implement -Wmissing-virtual-override.
        * testsuite/g++.dg/cpp0x/override5.C: New test.

diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt
index 72ac2ed..db350d7 100644
--- a/gcc/c-family/c.opt
+++ b/gcc/c-family/c.opt
@@ -558,6 +558,10 @@ Wmissing-field-initializers
 C ObjC C++ ObjC++ Var(warn_missing_field_initializers) Warning EnabledBy(Wextra)
 Warn about missing fields in struct initializers
 
+Wmissing-virtual-override
+C++ ObjC++ Var(warn_missing_virtual_override) Warning
+Warn about missing \"override\" specifier in virtual methods that override.
+
 Wsizeof-pointer-memaccess
 C ObjC C++ ObjC++ Var(warn_sizeof_pointer_memaccess) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
 Warn about suspicious length parameters to certain string functions if the argument uses sizeof
diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index 010ed25..f44cb82 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -2773,6 +2773,8 @@ check_for_override (tree decl, tree ctype)
     error ("%q+#D marked %<final%>, but is not virtual", decl);
   if (DECL_OVERRIDE_P (decl) && !overrides_found)
     error ("%q+#D marked %<override%>, but does not override", decl);
+  if (!DECL_OVERRIDE_P (decl) && overrides_found && !DECL_DESTRUCTOR_P(decl))
+    warning (OPT_Wmissing_virtual_override, "%q+#D overrides, but is not marked %<override%>", decl);
 }
 
 /* Warn about hidden virtual functions that are not overridden in t.
diff --git a/gcc/testsuite/g++.dg/cpp0x/override5.C b/gcc/testsuite/g++.dg/cpp0x/override5.C
new file mode 100644
index 0000000..07131e2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/override5.C
@@ -0,0 +1,36 @@
+// { dg-do compile { target c++11 } }
+// { dg-options "-Wmissing-virtual-override" }
+
+struct B
+{
+  virtual ~B();
+  virtual void f() final {}
+  virtual void x() {}
+};
+
+struct D : B
+{
+  virtual void x() final {} // { dg-warning "overrides, but is not marked 'override'" }
+};
+
+template <class T> struct D2 : T
+{
+  void x() override {}
+};
+
+template <class T> struct D3 : D2<T>
+{
+  void x() {} // { dg-warning "overrides, but is not marked 'override'" }
+};
+
+template <class T> struct D4 : D3<T>
+{
+  void x() override {}
+};
+
+int main()
+{
+  D2<B> d2;
+  D3<B> d3;
+  D4<B> d4;
+}


More information about the Gcc-patches mailing list