[cfe-commits] r95968 - in /cfe/trunk: lib/CodeGen/CGVtable.cpp test/CodeGenCXX/vtable-layout.cpp

John McCall rjmccall at apple.com
Thu Feb 11 22:15:08 PST 2010


Author: rjmccall
Date: Fri Feb 12 00:15:07 2010
New Revision: 95968

URL: http://llvm.org/viewvc/llvm-project?rev=95968&view=rev
Log:
Fix a bug causing an assertion when a covariant return type differed from
an overriden type only by reduced qualification.


Modified:
    cfe/trunk/lib/CodeGen/CGVtable.cpp
    cfe/trunk/test/CodeGenCXX/vtable-layout.cpp

Modified: cfe/trunk/lib/CodeGen/CGVtable.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGVtable.cpp?rev=95968&r1=95967&r2=95968&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGVtable.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGVtable.cpp Fri Feb 12 00:15:07 2010
@@ -64,8 +64,8 @@
 TypeConversionRequiresAdjustment(ASTContext &Ctx,
                                  QualType DerivedType, QualType BaseType) {
   // Canonicalize the types.
-  QualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
-  QualType CanBaseType = Ctx.getCanonicalType(BaseType);
+  CanQualType CanDerivedType = Ctx.getCanonicalType(DerivedType);
+  CanQualType CanBaseType = Ctx.getCanonicalType(BaseType);
   
   assert(CanDerivedType->getTypeClass() == CanBaseType->getTypeClass() && 
          "Types must have same type class!");
@@ -75,26 +75,29 @@
     return false;
   }
   
-  if (const ReferenceType *RT = CanDerivedType->getAs<ReferenceType>()) {
-    CanDerivedType = RT->getPointeeType();
+  if (isa<ReferenceType>(CanDerivedType)) {
+    CanDerivedType = CanDerivedType->getAs<ReferenceType>()->getPointeeType();
     CanBaseType = CanBaseType->getAs<ReferenceType>()->getPointeeType();
-  } else if (const PointerType *PT = CanDerivedType->getAs<PointerType>()) {
-    CanDerivedType = PT->getPointeeType();
+  } else if (isa<PointerType>(CanDerivedType)) {
+    CanDerivedType = CanDerivedType->getAs<PointerType>()->getPointeeType();
     CanBaseType = CanBaseType->getAs<PointerType>()->getPointeeType();
   } else {
     assert(false && "Unexpected return type!");
   }
   
-  if (CanDerivedType == CanBaseType) {
+  // We need to compare unqualified types here; consider
+  //   const T *Base::foo();
+  //   T *Derived::foo();
+  if (CanDerivedType.getUnqualifiedType() == CanBaseType.getUnqualifiedType()) {
     // No adjustment needed.
     return false;
   }
   
   const CXXRecordDecl *DerivedDecl = 
-    cast<CXXRecordDecl>(CanDerivedType->getAs<RecordType>()->getDecl());
+    cast<CXXRecordDecl>(cast<RecordType>(CanDerivedType)->getDecl());
   
   const CXXRecordDecl *BaseDecl = 
-    cast<CXXRecordDecl>(CanBaseType->getAs<RecordType>()->getDecl());
+    cast<CXXRecordDecl>(cast<RecordType>(CanBaseType)->getDecl());
   
   return TypeConversionRequiresAdjustment(Ctx, DerivedDecl, BaseDecl);
 }

Modified: cfe/trunk/test/CodeGenCXX/vtable-layout.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/vtable-layout.cpp?rev=95968&r1=95967&r2=95968&view=diff

==============================================================================
--- cfe/trunk/test/CodeGenCXX/vtable-layout.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/vtable-layout.cpp Fri Feb 12 00:15:07 2010
@@ -113,3 +113,13 @@
 
 void D::f() { } 
 }
+
+// For now, just verify this doesn't crash.
+namespace test0 {
+  struct Obj {};
+
+  struct Base {           virtual const Obj *foo() = 0; };
+  struct Derived : Base { virtual       Obj *foo() { return new Obj(); } };
+
+  void test(Derived *D) { D->foo(); }
+}





More information about the cfe-commits mailing list