]> granicus.if.org Git - clang/commitdiff
Implement John McCall's review of r159212 other than the this pointer not
authorRafael Espindola <rafael.espindola@gmail.com>
Wed, 27 Jun 2012 18:18:05 +0000 (18:18 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Wed, 27 Jun 2012 18:18:05 +0000 (18:18 +0000)
being updated. Will fix that in a second.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159280 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
lib/AST/Expr.cpp
lib/CodeGen/CGExprCXX.cpp
lib/Sema/SemaExpr.cpp

index 77684eee00c4ef7f99387f56a5a27d497aafb1a0..7a9cfaeebe4e2f3b27eaf2e9eeb5800d51f9b4d0 100644 (file)
@@ -665,12 +665,14 @@ public:
 
   static bool hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs);
 
-  /// \brief If we have class type (or pointer to class type), return the
-  /// class decl. Return NULL otherwise.
+  /// \brief For an expression of class type or pointer to class type,
+  /// return the most derived class decl the expression is known to refer to.
   ///
   /// If this expression is a cast, this method looks through it to find the
-  /// most derived decl that can be infered from the expression.
-  const CXXRecordDecl *getMostDerivedClassDeclForType() const;
+  /// most derived decl that can be inferred from the expression.
+  /// This is valid because derived-to-base conversions have undefined
+  /// behavior if the object isn't dynamically of the derived type.
+  const CXXRecordDecl *getBestDynamicClassType() const;
 
   static bool classof(const Stmt *T) {
     return T->getStmtClass() >= firstExprConstant &&
index 22d15be6a87c8908f2381ba42e42b900a2715d1c..15cf6602b71cbc82cfe57ea006587017f10f1472 100644 (file)
@@ -33,7 +33,7 @@
 #include <cstring>
 using namespace clang;
 
-const CXXRecordDecl *Expr::getMostDerivedClassDeclForType() const {
+const CXXRecordDecl *Expr::getBestDynamicClassType() const {
   const Expr *E = this;
 
   while (true) {
@@ -51,15 +51,10 @@ const CXXRecordDecl *Expr::getMostDerivedClassDeclForType() const {
   }
 
   QualType DerivedType = E->getType();
-  if (DerivedType->isDependentType())
-    return NULL;
   if (const PointerType *PTy = DerivedType->getAs<PointerType>())
     DerivedType = PTy->getPointeeType();
 
   const RecordType *Ty = DerivedType->castAs<RecordType>();
-  if (!Ty)
-    return NULL;
-
   Decl *D = Ty->getDecl();
   return cast<CXXRecordDecl>(D);
 }
index 372eb5407c9d1677ab5c83b2ef008530849be816..30324b97ef16723dcd0f5b76810a6f2278e2146d 100644 (file)
@@ -102,8 +102,7 @@ static bool canDevirtualizeMemberFunctionCalls(ASTContext &Context,
   //   b->f();
   // }
   //
-  const CXXRecordDecl *MostDerivedClassDecl =
-    Base->getMostDerivedClassDeclForType();
+  const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
   if (MostDerivedClassDecl->hasAttr<FinalAttr>())
     return true;
 
@@ -228,8 +227,7 @@ RValue CodeGenFunction::EmitCXXMemberCallExpr(const CXXMemberCallExpr *CE,
   bool UseVirtualCall = MD->isVirtual() && !ME->hasQualifier()
                         && !canDevirtualizeMemberFunctionCalls(getContext(),
                                                                Base, MD);
-  const CXXRecordDecl *MostDerivedClassDecl =
-    Base->getMostDerivedClassDeclForType();
+  const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
 
   llvm::Value *Callee;
   if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(MD)) {
index bc77e6fae7e87f18637192862c2d75ccb3ce3825..2a7a8b9f19554676870da39d8e762adaac3d1d8d 100644 (file)
@@ -10854,8 +10854,9 @@ static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc,
   if (!MD)
     return;
   const Expr *Base = ME->getBase();
-  const CXXRecordDecl *MostDerivedClassDecl
-    = Base->getMostDerivedClassDeclForType();
+  if (Base->getType()->isDependentType())
+    return;
+  const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType();
   if (!MostDerivedClassDecl)
     return;
   CXXMethodDecl *DM = MD->getCorrespondingMethodInClass(MostDerivedClassDecl);