summaryrefslogtreecommitdiffstats
path: root/clang
diff options
context:
space:
mode:
authorDouglas Gregor <dgregor@apple.com>2009-12-01 17:24:26 +0000
committerDouglas Gregor <dgregor@apple.com>2009-12-01 17:24:26 +0000
commit21920e375805cf0b45b0e78f577502ef0229dd1d (patch)
tree0ae7d8859fe61ff1beb197b1fe103ecda43767cd /clang
parentdefc47088a189fab9f6597d69f33b873d556e05d (diff)
downloadbcm5719-llvm-21920e375805cf0b45b0e78f577502ef0229dd1d.tar.gz
bcm5719-llvm-21920e375805cf0b45b0e78f577502ef0229dd1d.zip
Move the checking of overridden virtual functions into the code path
common to both parsing and template instantiation, so that we'll find overridden virtuals for member functions of class templates when they are instantiated. Additionally, factor out the checking for pure virtual functions, so that it will be executed both at parsing time and at template instantiation time. These changes fix PR5656 (for real), although one more tweak w.r.t. member function templates will be coming along shortly. llvm-svn: 90241
Diffstat (limited to 'clang')
-rw-r--r--clang/lib/Sema/Sema.h1
-rw-r--r--clang/lib/Sema/SemaDecl.cpp25
-rw-r--r--clang/lib/Sema/SemaDeclCXX.cpp20
-rw-r--r--clang/lib/Sema/SemaTemplateInstantiateDecl.cpp7
-rw-r--r--clang/test/SemaCXX/virtual-override.cpp13
5 files changed, 46 insertions, 20 deletions
diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h
index b594eceaf3c..f961406e3e5 100644
--- a/clang/lib/Sema/Sema.h
+++ b/clang/lib/Sema/Sema.h
@@ -2208,6 +2208,7 @@ public:
bool CheckOverridingFunctionAttributes(const CXXMethodDecl *New,
const CXXMethodDecl *Old);
+ bool CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange);
//===--------------------------------------------------------------------===//
// C++ Access Control
//
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 2087f8b8c6c..49be3408738 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -2865,9 +2865,6 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
NewFD->setAccess(AS_public);
}
- if (CXXMethodDecl *NewMD = dyn_cast<CXXMethodDecl>(NewFD))
- AddOverriddenMethods(cast<CXXRecordDecl>(DC), NewMD);
-
if (SC == FunctionDecl::Static && isa<CXXMethodDecl>(NewFD) &&
!CurContext->isRecord()) {
// C++ [class.static]p1:
@@ -3257,8 +3254,13 @@ void Sema::CheckFunctionDeclaration(FunctionDecl *NewFD,
// FIXME: C++0x: don't do this for "= default" destructors
Record->setHasTrivialDestructor(false);
} else if (CXXConversionDecl *Conversion
- = dyn_cast<CXXConversionDecl>(NewFD))
+ = dyn_cast<CXXConversionDecl>(NewFD)) {
ActOnConversionDeclarator(Conversion);
+ }
+
+ // Find any virtual functions that this function overrides.
+ if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(NewFD))
+ AddOverriddenMethods(Method->getParent(), Method);
// Extra checking for C++ overloaded operators (C++ [over.oper]).
if (NewFD->isOverloadedOperator() &&
@@ -3392,18 +3394,9 @@ void Sema::AddInitializerToDecl(DeclPtrTy dcl, ExprArg init, bool DirectInit) {
IntegerLiteral *IL;
Expr *Init = static_cast<Expr *>(init.get());
if ((IL = dyn_cast<IntegerLiteral>(Init)) && IL->getValue() == 0 &&
- Context.getCanonicalType(IL->getType()) == Context.IntTy) {
- if (Method->isVirtual()) {
- Method->setPure();
-
- // A class is abstract if at least one function is pure virtual.
- cast<CXXRecordDecl>(CurContext)->setAbstract(true);
- } else if (!Method->isInvalidDecl()) {
- Diag(Method->getLocation(), diag::err_non_virtual_pure)
- << Method->getDeclName() << Init->getSourceRange();
- Method->setInvalidDecl();
- }
- } else {
+ Context.getCanonicalType(IL->getType()) == Context.IntTy)
+ CheckPureMethod(Method, Init->getSourceRange());
+ else {
Diag(Method->getLocation(), diag::err_member_function_initialization)
<< Method->getDeclName() << Init->getSourceRange();
Method->setInvalidDecl();
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index e27b82d8000..df27679a5a5 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -4890,6 +4890,26 @@ bool Sema::CheckOverridingFunctionAttributes(const CXXMethodDecl *New,
return false;
}
+/// \brief Mark the given method pure.
+///
+/// \param Method the method to be marked pure.
+///
+/// \param InitRange the source range that covers the "0" initializer.
+bool Sema::CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange) {
+ if (Method->isVirtual() || Method->getParent()->isDependentContext()) {
+ Method->setPure();
+
+ // A class is abstract if at least one function is pure virtual.
+ Method->getParent()->setAbstract(true);
+ return false;
+ }
+
+ if (!Method->isInvalidDecl())
+ Diag(Method->getLocation(), diag::err_non_virtual_pure)
+ << Method->getDeclName() << InitRange;
+ return true;
+}
+
/// ActOnCXXEnterDeclInitializer - Invoked when we are about to parse an
/// initializer for the declaration 'Dcl'.
/// After this method is called, according to [C++ 3.4.1p13], if 'Dcl' is a
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index a1258572ccb..615c6f94567 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -863,6 +863,9 @@ TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
SemaRef.CheckFunctionDeclaration(Method, Previous, false, Redeclaration,
/*FIXME:*/OverloadableAttrRequired);
+ if (D->isPure())
+ SemaRef.CheckPureMethod(Method, SourceRange());
+
if (!FunctionTemplate && (!Method->isInvalidDecl() || Previous.empty()) &&
!Method->getFriendObjectKind())
Owner->addDecl(Method);
@@ -1359,10 +1362,6 @@ TemplateDeclInstantiator::InitMethodInstantiation(CXXMethodDecl *New,
Record->setEmpty(false);
Record->setPolymorphic(true);
}
- if (Tmpl->isPure()) {
- New->setPure();
- Record->setAbstract(true);
- }
// FIXME: attributes
// FIXME: New needs a pointer to Tmpl
diff --git a/clang/test/SemaCXX/virtual-override.cpp b/clang/test/SemaCXX/virtual-override.cpp
index 7ace886ff0d..6024dae8386 100644
--- a/clang/test/SemaCXX/virtual-override.cpp
+++ b/clang/test/SemaCXX/virtual-override.cpp
@@ -112,3 +112,16 @@ class X0 {
class X1 : public X0 {
void f0() = 0;
};
+
+template <typename Base>
+struct Foo : Base {
+ void f() = 0; // expected-error{{not virtual and cannot be declared pure}}
+};
+
+struct Base1 { virtual void f(); };
+struct Base2 { };
+
+void test() {
+ Foo<Base1> f1;
+ Foo<Base2> f2; // expected-note{{instantiation}}
+}
OpenPOWER on IntegriCloud