Only allow virtual member functions to be marked 'override' and 'final'.

llvm-svn: 123882
This commit is contained in:
Anders Carlsson 2011-01-20 04:34:22 +00:00
parent c432c1967b
commit 13a69102d7
4 changed files with 41 additions and 0 deletions

View File

@ -870,6 +870,10 @@ def err_trailing_return_without_auto : Error<
// 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">;

View File

@ -1483,6 +1483,15 @@ public:
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;

View File

@ -989,6 +989,22 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
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)

View File

@ -4,3 +4,15 @@ struct A {
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'}}
};