[cfe-commits] r123888 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclCXX.cpp test/CXX/class.derived/class.virtual/p3-0x.cpp
Anders Carlsson
andersca at mac.com
Wed Jan 19 21:57:14 PST 2011
Author: andersca
Date: Wed Jan 19 23:57:14 2011
New Revision: 123888
URL: http://llvm.org/viewvc/llvm-project?rev=123888&view=rev
Log:
Diagnose virtual member functions marked override but not overriding any virtual member functions.
Added:
cfe/trunk/test/CXX/class.derived/class.virtual/p3-0x.cpp
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaDeclCXX.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=123888&r1=123887&r2=123888&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jan 19 23:57:14 2011
@@ -874,16 +874,15 @@
def override_keyword_only_allowed_on_virtual_member_functions : Error<
"only virtual member functions can be marked '%0'">;
+def err_function_marked_override_not_overriding : Error<
+ "%0 marked 'override' but does not override any member functions">;
+
// C++0x [[final]]
def err_final_function_overridden : Error<
"declaration of %0 overrides a 'final' function">;
def err_final_base : Error<
"derivation from 'final' %0">;
-// C++0x [[override]]
-def err_override_function_not_overriding : Error<
- "%0 marked 'override' but does not override any member functions">;
-
def err_function_overriding_without_override : Error<
"%0 overrides function%s1 without being marked 'override'">;
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=123888&r1=123887&r2=123888&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Jan 19 23:57:14 2011
@@ -3388,7 +3388,8 @@
/// If a virtual member function f is marked override and does not override
/// a member function of a base class the program is ill-formed.
if (HasOverrideAttr && !HasOverriddenMethods) {
- SemaRef.Diag(MD->getLocation(), diag::err_override_function_not_overriding)
+ SemaRef.Diag(MD->getLocation(),
+ diag::err_function_marked_override_not_overriding)
<< MD->getDeclName();
return;
}
Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=123888&r1=123887&r2=123888&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Wed Jan 19 23:57:14 2011
@@ -852,6 +852,27 @@
return ASDecl;
}
+/// CheckOverrideControl - Check C++0x override control semantics.
+static void
+CheckOverrideControl(Sema& SemaRef, const Decl *D) {
+ const CXXMethodDecl *MD = llvm::dyn_cast<CXXMethodDecl>(D);
+ if (!MD || !MD->isVirtual())
+ return;
+
+ // C++0x [class.virtual]p3:
+ // If a virtual function is marked with the virt-specifier override and does
+ // not override a member function of a base class,
+ // the program is ill-formed.
+ bool HasOverriddenMethods =
+ MD->begin_overridden_methods() != MD->end_overridden_methods();
+ if (MD->isMarkedOverride() && !HasOverriddenMethods) {
+ SemaRef.Diag(MD->getLocation(),
+ diag::err_function_marked_override_not_overriding)
+ << MD->getDeclName();
+ return;
+ }
+}
+
/// ActOnCXXMemberDeclarator - This is invoked when a C++ class member
/// declarator is parsed. 'AS' is the access specifier, 'BW' specifies the
/// bitfield width if there is one and 'InitExpr' specifies the initializer if
@@ -995,7 +1016,8 @@
Diag(Member->getLocStart(),
diag::override_keyword_only_allowed_on_virtual_member_functions)
<< "override" << FixItHint::CreateRemoval(VS.getOverrideLoc());
- }
+ } else
+ MD->setIsMarkedOverride(true);
}
if (VS.isFinalSpecified()) {
CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Member);
@@ -1003,8 +1025,12 @@
Diag(Member->getLocStart(),
diag::override_keyword_only_allowed_on_virtual_member_functions)
<< "final" << FixItHint::CreateRemoval(VS.getFinalLoc());
- }
+ } else
+ MD->setIsMarkedFinal(true);
}
+
+ CheckOverrideControl(*this, Member);
+
assert((Name || isInstField) && "No identifier for non-field ?");
if (Init)
Added: cfe/trunk/test/CXX/class.derived/class.virtual/p3-0x.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/class.derived/class.virtual/p3-0x.cpp?rev=123888&view=auto
==============================================================================
--- cfe/trunk/test/CXX/class.derived/class.virtual/p3-0x.cpp (added)
+++ cfe/trunk/test/CXX/class.derived/class.virtual/p3-0x.cpp Wed Jan 19 23:57:14 2011
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++0x -verify %s
+
+struct B {
+ virtual void f(int);
+};
+
+struct D : B {
+ virtual void f(long) override; // expected-error {{'f' marked 'override' but does not override any member functions}}
+ void f(int) override;
+};
More information about the cfe-commits
mailing list