[cfe-commits] r90241 - in /cfe/trunk: lib/Sema/Sema.h lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclCXX.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaCXX/virtual-override.cpp

Douglas Gregor dgregor at apple.com
Tue Dec 1 09:24:26 PST 2009


Author: dgregor
Date: Tue Dec  1 11:24:26 2009
New Revision: 90241

URL: http://llvm.org/viewvc/llvm-project?rev=90241&view=rev
Log:
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.


Modified:
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/test/SemaCXX/virtual-override.cpp

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=90241&r1=90240&r2=90241&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Dec  1 11:24:26 2009
@@ -2208,6 +2208,7 @@
   bool CheckOverridingFunctionAttributes(const CXXMethodDecl *New,
                                          const CXXMethodDecl *Old);
 
+  bool CheckPureMethod(CXXMethodDecl *Method, SourceRange InitRange);
   //===--------------------------------------------------------------------===//
   // C++ Access Control
   //

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=90241&r1=90240&r2=90241&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue Dec  1 11:24:26 2009
@@ -2865,9 +2865,6 @@
     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 @@
       // 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 @@
     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();

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=90241&r1=90240&r2=90241&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue Dec  1 11:24:26 2009
@@ -4890,6 +4890,26 @@
   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

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=90241&r1=90240&r2=90241&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Tue Dec  1 11:24:26 2009
@@ -863,6 +863,9 @@
   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 @@
     Record->setEmpty(false);
     Record->setPolymorphic(true);
   }
-  if (Tmpl->isPure()) {
-    New->setPure();
-    Record->setAbstract(true);
-  }
 
   // FIXME: attributes
   // FIXME: New needs a pointer to Tmpl

Modified: cfe/trunk/test/SemaCXX/virtual-override.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/virtual-override.cpp?rev=90241&r1=90240&r2=90241&view=diff

==============================================================================
--- cfe/trunk/test/SemaCXX/virtual-override.cpp (original)
+++ cfe/trunk/test/SemaCXX/virtual-override.cpp Tue Dec  1 11:24:26 2009
@@ -112,3 +112,16 @@
 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}}
+}





More information about the cfe-commits mailing list