[cfe-commits] r123882 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/DeclSpec.h lib/Sema/SemaDeclCXX.cpp test/CXX/class/class.mem/p8-0x.cpp

Anders Carlsson andersca at mac.com
Wed Jan 19 20:34:22 PST 2011


Author: andersca
Date: Wed Jan 19 22:34:22 2011
New Revision: 123882

URL: http://llvm.org/viewvc/llvm-project?rev=123882&view=rev
Log:
Only allow virtual member functions to be marked 'override' and 'final'.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Sema/DeclSpec.h
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/test/CXX/class/class.mem/p8-0x.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=123882&r1=123881&r2=123882&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jan 19 22:34:22 2011
@@ -870,6 +870,10 @@
 // C++0x attributes
 def err_repeat_attribute : Error<"'%0' attribute cannot be repeated">;
 
+// C++0x override control
+def override_keyword_only_allowed_on_virtual_member_functions : Error<
+  "only virtual member functions can be marked '%0'">;
+
 // C++0x [[final]]
 def err_final_function_overridden : Error<
   "declaration of %0 overrides a 'final' function">;

Modified: cfe/trunk/include/clang/Sema/DeclSpec.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/DeclSpec.h?rev=123882&r1=123881&r2=123882&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/DeclSpec.h (original)
+++ cfe/trunk/include/clang/Sema/DeclSpec.h Wed Jan 19 22:34:22 2011
@@ -1483,6 +1483,15 @@
   bool SetVirtSpecifier(VirtSpecifier VS, SourceLocation Loc, 
                         const char *&PrevSpec);
 
+  bool isOverrideSpecified() const { return Specifiers & VS_Override; }
+  SourceLocation getOverrideLoc() const { return VS_overrideLoc; }
+
+  bool isFinalSpecified() const { return Specifiers & VS_Final; }
+  SourceLocation getFinalLoc() const { return VS_finalLoc; }
+
+  bool isNewSpecified() const { return Specifiers & VS_New; }
+  SourceLocation getNewLoc() const { return VS_newLoc; }
+
 private:
   unsigned Specifiers;
 

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=123882&r1=123881&r2=123882&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Wed Jan 19 22:34:22 2011
@@ -989,6 +989,22 @@
       FunTmpl->getTemplatedDecl()->setAccess(AS);
   }
 
+  if (VS.isOverrideSpecified()) {
+    CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member);
+    if (!MD || !MD->isVirtual()) {
+      Diag(Member->getLocStart(), 
+           diag::override_keyword_only_allowed_on_virtual_member_functions)
+        << "override" << FixItHint::CreateRemoval(VS.getOverrideLoc());
+    }
+  }
+  if (VS.isFinalSpecified()) {
+    CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member);
+    if (!MD || !MD->isVirtual()) {
+      Diag(Member->getLocStart(), 
+           diag::override_keyword_only_allowed_on_virtual_member_functions)
+      << "final" << FixItHint::CreateRemoval(VS.getFinalLoc());
+    }    
+  }
   assert((Name || isInstField) && "No identifier for non-field ?");
 
   if (Init)

Modified: cfe/trunk/test/CXX/class/class.mem/p8-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/class/class.mem/p8-0x.cpp?rev=123882&r1=123881&r2=123882&view=diff
==============================================================================
--- cfe/trunk/test/CXX/class/class.mem/p8-0x.cpp (original)
+++ cfe/trunk/test/CXX/class/class.mem/p8-0x.cpp Wed Jan 19 22:34:22 2011
@@ -4,3 +4,15 @@
   virtual void g() override override; // expected-error {{member function already marked 'override'}}
   virtual void h() final final; // expected-error {{member function already marked 'final'}}
 };
+
+struct B {
+  virtual void f() override;
+  void g() override; // expected-error {{only virtual member functions can be marked 'override'}}
+  int h override; // expected-error {{only virtual member functions can be marked 'override'}}
+};
+
+struct C {
+  virtual void f() final;
+  void g() final; // expected-error {{only virtual member functions can be marked 'final'}}
+  int h final; // expected-error {{only virtual member functions can be marked 'final'}}
+};





More information about the cfe-commits mailing list