]> granicus.if.org Git - clang/commitdiff
Add CXXDynamicCastExpr::isAlwaysNull() which will be replacing the cast kind I added.
authorAnders Carlsson <andersca@mac.com>
Mon, 11 Apr 2011 01:43:55 +0000 (01:43 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 11 Apr 2011 01:43:55 +0000 (01:43 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@129263 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ExprCXX.h
lib/AST/ExprCXX.cpp

index e926267f5c9b924552ae0bc62c646f2f1f8d1122..9bb1a50be90583406ddda04f954271d6eb91ff4c 100644 (file)
@@ -253,6 +253,8 @@ public:
   static CXXDynamicCastExpr *CreateEmpty(ASTContext &Context,
                                          unsigned pathSize);
 
+  bool isAlwaysNull() const;
+
   static bool classof(const Stmt *T) {
     return T->getStmtClass() == CXXDynamicCastExprClass;
   }
index 526c484677bc9532fb7f2e83ba406eb124b4ad17..692c2c37607fb9e8022250e98a207314ce0849d8 100644 (file)
@@ -482,6 +482,36 @@ CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(ASTContext &C,
   return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
 }
 
+/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
+/// to always be null. For example:
+///
+/// struct A { };
+/// struct B final : A { };
+/// struct C { };
+///
+/// C *f(B* b) { return dynamic_cast<C*>(b); }
+bool CXXDynamicCastExpr::isAlwaysNull() const
+{
+  QualType SrcType = getSubExpr()->getType();
+  QualType DestType = getType();
+
+  if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
+    SrcType = SrcPTy->getPointeeType();
+    DestType = DestType->castAs<PointerType>()->getPointeeType();
+  }
+
+  const CXXRecordDecl *SrcRD = 
+    cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
+
+  if (!SrcRD->hasAttr<FinalAttr>())
+    return false;
+
+  const CXXRecordDecl *DestRD = 
+    cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
+
+  return !DestRD->isDerivedFrom(SrcRD);
+}
+
 CXXReinterpretCastExpr *
 CXXReinterpretCastExpr::Create(ASTContext &C, QualType T, ExprValueKind VK,
                                CastKind K, Expr *Op,