[cfe-commits] r71802 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/AST/DeclCXX.h lib/Frontend/PCHReaderDecl.cpp lib/Frontend/PCHWriterDecl.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaCXX/virtual-override.cpp

Anders Carlsson andersca at mac.com
Thu May 14 15:15:47 PDT 2009


Author: andersca
Date: Thu May 14 17:15:41 2009
New Revision: 71802

URL: http://llvm.org/viewvc/llvm-project?rev=71802&view=rev
Log:
Check that the function being overridden is virtual.

Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/include/clang/AST/DeclCXX.h
    cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
    cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
    cfe/trunk/test/SemaCXX/virtual-override.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=71802&r1=71801&r2=71802&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Thu May 14 17:15:41 2009
@@ -517,7 +517,7 @@
   unsigned SClass : 2;
   bool IsInline : 1;
   bool C99InlineDefinition : 1;
-  bool IsVirtual : 1;
+  bool IsVirtualAsWritten : 1;
   bool IsPure : 1;
   bool HasInheritedPrototype : 1;
   bool HasWrittenPrototype : 1;
@@ -546,7 +546,7 @@
       DeclContext(DK),
       ParamInfo(0), Body(), PreviousDeclaration(0),
       SClass(S), IsInline(isInline), C99InlineDefinition(false), 
-      IsVirtual(false), IsPure(false), HasInheritedPrototype(false), 
+      IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false), 
       HasWrittenPrototype(true), IsDeleted(false), TypeSpecStartLoc(TSSL),
       TemplateOrInstantiation() {}
 
@@ -590,10 +590,9 @@
   void setBody(Stmt *B) { Body = B; }
   void setLazyBody(uint64_t Offset) { Body = Offset; }
 
-  /// Whether this function is virtual, either by explicit marking, or by
-  /// overriding a virtual function. Only valid on C++ member functions.
-  bool isVirtual() { return IsVirtual; }
-  void setVirtual(bool V = true) { IsVirtual = V; }
+  /// Whether this function is marked as virtual explicitly.
+  bool isVirtualAsWritten() const { return IsVirtualAsWritten; }
+  void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; }
 
   /// Whether this virtual function is pure, i.e. makes the containing class
   /// abstract.

Modified: cfe/trunk/include/clang/AST/DeclCXX.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclCXX.h?rev=71802&r1=71801&r2=71802&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/DeclCXX.h (original)
+++ cfe/trunk/include/clang/AST/DeclCXX.h Thu May 14 17:15:41 2009
@@ -476,6 +476,11 @@
     return getLexicalDeclContext() != getDeclContext();
   }
 
+  bool isVirtual() const { 
+    // FIXME: Check if it's inherited virtual as well.
+    return isVirtualAsWritten();
+  }
+  
   /// getParent - Returns the parent of this method declaration, which
   /// is the class in which this method is defined.
   const CXXRecordDecl *getParent() const { 

Modified: cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderDecl.cpp?rev=71802&r1=71801&r2=71802&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Thu May 14 17:15:41 2009
@@ -149,7 +149,7 @@
   FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
   FD->setInline(Record[Idx++]);
   FD->setC99InlineDefinition(Record[Idx++]);
-  FD->setVirtual(Record[Idx++]);
+  FD->setVirtualAsWritten(Record[Idx++]);
   FD->setPure(Record[Idx++]);
   FD->setHasInheritedPrototype(Record[Idx++]);
   FD->setHasWrittenPrototype(Record[Idx++]);

Modified: cfe/trunk/lib/Frontend/PCHWriterDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHWriterDecl.cpp?rev=71802&r1=71801&r2=71802&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHWriterDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHWriterDecl.cpp Thu May 14 17:15:41 2009
@@ -149,7 +149,7 @@
   Record.push_back(D->getStorageClass()); // FIXME: stable encoding
   Record.push_back(D->isInline());
   Record.push_back(D->isC99InlineDefinition());
-  Record.push_back(D->isVirtual());
+  Record.push_back(D->isVirtualAsWritten());
   Record.push_back(D->isPure());
   Record.push_back(D->hasInheritedPrototype());
   Record.push_back(D->hasWrittenPrototype());

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Thu May 14 17:15:41 2009
@@ -2125,7 +2125,7 @@
                              SourceRange(D.getDeclSpec().getVirtualSpecLoc()));
     } else {
       // Okay: Add virtual to the method.
-      cast<CXXMethodDecl>(NewFD)->setVirtual();
+      cast<CXXMethodDecl>(NewFD)->setVirtualAsWritten(true);
       CXXRecordDecl *CurClass = cast<CXXRecordDecl>(DC);
       CurClass->setAggregate(false);
       CurClass->setPOD(false);
@@ -2152,6 +2152,8 @@
           // FIXME: Is this OK? Should it be done by LookupInBases?
           if (IsOverload(NewMD, OldMD, MatchedDecl))
             continue;
+          if (!OldMD->isVirtual())
+            continue;
          
           if (!CheckOverridingFunctionReturnType(NewMD, OldMD)) {
             // FIXME: Add OldMD to the list of methods NewMD overrides.
@@ -2490,7 +2492,7 @@
     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()) {
+      if (Method->isVirtualAsWritten()) {
         Method->setPure();
 
         // A class is abstract if at least one function is pure virtual.

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Thu May 14 17:15:41 2009
@@ -546,8 +546,8 @@
                                                   CXXMethodDecl *Tmpl) {
   CXXRecordDecl *Record = cast<CXXRecordDecl>(Owner);
   New->setAccess(Tmpl->getAccess());
-  if (Tmpl->isVirtual()) {
-    New->setVirtual();
+  if (Tmpl->isVirtualAsWritten()) {
+    New->setVirtualAsWritten(true);
     Record->setAggregate(false);
     Record->setPOD(false);
     Record->setPolymorphic(true);

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

==============================================================================
--- cfe/trunk/test/SemaCXX/virtual-override.cpp (original)
+++ cfe/trunk/test/SemaCXX/virtual-override.cpp Thu May 14 17:15:41 2009
@@ -91,3 +91,16 @@
 };
 
 }
+
+namespace T7 {
+  struct a { };
+  struct b { };
+
+  class A {
+    a* f();
+  };
+
+  class B : A {
+    virtual b* f();
+  };
+}





More information about the cfe-commits mailing list