]> granicus.if.org Git - clang/commitdiff
As a first step towards fixing PR9641, add a CK_DynamicToNull cast kind which
authorAnders Carlsson <andersca@mac.com>
Sun, 10 Apr 2011 20:33:22 +0000 (20:33 +0000)
committerAnders Carlsson <andersca@mac.com>
Sun, 10 Apr 2011 20:33:22 +0000 (20:33 +0000)
represents a dynamic cast where we know that the result is always null.

For example:

struct A {
  virtual ~A();
};
struct B final : A { };
struct C { };

bool f(B* b) {
  return dynamic_cast<C*>(b);
}

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

include/clang/AST/Expr.h
include/clang/AST/OperationKinds.h
lib/AST/Expr.cpp
lib/AST/ExprConstant.cpp
lib/CodeGen/CGExpr.cpp
lib/CodeGen/CGExprAgg.cpp
lib/CodeGen/CGExprConstant.cpp
lib/CodeGen/CGExprScalar.cpp
lib/Sema/SemaCXXCast.cpp
lib/StaticAnalyzer/Core/ExprEngine.cpp

index 110c4a73235ae675e92371e5022920f83a660f69..4ef29642d1cc761795a009ad4ddfb5b53a5c3ca5 100644 (file)
@@ -2183,6 +2183,7 @@ private:
     // These should not have an inheritance path.
     case CK_BitCast:
     case CK_Dynamic:
+    case CK_DynamicToNull:
     case CK_ToUnion:
     case CK_ArrayToPointerDecay:
     case CK_FunctionToPointerDecay:
index ab9d97dbb794d4e582f66015075c9663faec6d92..6e16d2196a311bfc9cd5c1389e52b5d56bc72da0 100644 (file)
@@ -82,6 +82,10 @@ enum CastKind {
   /// CK_Dynamic - A C++ dynamic_cast.
   CK_Dynamic,
 
+  /// CK_DynamicToNull - A C++ dynamic_cast that can be proven to
+  /// always yield a null result.
+  CK_DynamicToNull,
+
   /// CK_ToUnion - The GCC cast-to-union extension.
   ///   int   -> union { int x; float y; }
   ///   float -> union { int x; float y; }
index 28f42124f984189885af0125283efc60a9741272..227d6569ddc74aa063d8369e87866edb4fe919a8 100644 (file)
@@ -987,6 +987,8 @@ const char *CastExpr::getCastKindName() const {
     return "UncheckedDerivedToBase";
   case CK_Dynamic:
     return "Dynamic";
+  case CK_DynamicToNull:
+    return "DynamicToNull";
   case CK_ToUnion:
     return "ToUnion";
   case CK_ArrayToPointerDecay:
index e0f995829820fc436382917500b451f58b1a53cc..aa8d91a6d6d4da46807b3630cee89bedf8d895a2 100644 (file)
@@ -1768,6 +1768,7 @@ bool IntExprEvaluator::VisitCastExpr(CastExpr *E) {
   case CK_DerivedToBase:
   case CK_UncheckedDerivedToBase:
   case CK_Dynamic:
+  case CK_DynamicToNull:
   case CK_ToUnion:
   case CK_ArrayToPointerDecay:
   case CK_FunctionToPointerDecay:
@@ -2315,6 +2316,7 @@ bool ComplexExprEvaluator::VisitCastExpr(CastExpr *E) {
   case CK_DerivedToBase:
   case CK_UncheckedDerivedToBase:
   case CK_Dynamic:
+  case CK_DynamicToNull:
   case CK_ToUnion:
   case CK_ArrayToPointerDecay:
   case CK_FunctionToPointerDecay:
index a35f81ca206665c77ad1e0b25dff39f9aa67c8d8..ad51316a1f5c6afeab6d822a3cc99015a904359e 100644 (file)
@@ -1867,7 +1867,8 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) {
     return MakeAddrLValue(V, E->getType());
   }
 
-  case CK_Dynamic: {
+  case CK_Dynamic:
+  case CK_DynamicToNull: {
     LValue LV = EmitLValue(E->getSubExpr());
     llvm::Value *V = LV.getAddress();
     const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(E);
index 75e3a7879d42046c28642ea38e879e9ac69e3063..5e1ac3cbacb4dadc6af11dfc0617e32e8f3d1f33 100644 (file)
@@ -255,7 +255,10 @@ void AggExprEmitter::VisitCastExpr(CastExpr *E) {
   }
 
   switch (E->getCastKind()) {
-  case CK_Dynamic: {
+  case CK_Dynamic:
+  case CK_DynamicToNull: {
+
+    // FIXME: Actually handle DynamicToNull here.
     assert(isa<CXXDynamicCastExpr>(E) && "CK_Dynamic without a dynamic_cast?");
     LValue LV = CGF.EmitCheckedLValue(E->getSubExpr());
     // FIXME: Do we also need to handle property references here?
index 822a999b963d6af1a0f17ec14db4e2f2f31d5558..578a37f0fcf52e431b8d82165bfb5cf47611e031 100644 (file)
@@ -552,6 +552,7 @@ public:
     case CK_GetObjCProperty:
     case CK_ToVoid:
     case CK_Dynamic:
+    case CK_DynamicToNull:
     case CK_ResolveUnknownAnyType:
       return 0;
 
index 65aa46fff06de35009449a48e41e041874793bda..322b6e0c498ec83911e8294c8c9373dad727f3fe 100644 (file)
@@ -1053,7 +1053,8 @@ Value *ScalarExprEmitter::EmitCastExpr(CastExpr *CE) {
                                      CE->path_begin(), CE->path_end(),
                                      ShouldNullCheckClassCastValue(CE));
   }
-  case CK_Dynamic: {
+  case CK_Dynamic:
+  case CK_DynamicToNull: {
     Value *V = Visit(const_cast<Expr*>(E));
     const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(CE);
     return CGF.EmitDynamicCast(V, DCE);
index 31a772a5d8ef7f28cc2084f92b4f21923d399f56..52a13ef5b2981bb9e54ed683d18363a2bfc13ef7 100644 (file)
@@ -522,6 +522,14 @@ CheckDynamicCast(Sema &Self, ExprResult &SrcExpr, QualType DestType,
     return;
   }
 
+  // If the source class is marked 'final', and the destination class does not
+  // derive from the source class, then we know that the result is always null.
+  if (SrcRecord->getDecl()->hasAttr<FinalAttr>() &&
+      !Self.IsDerivedFrom(DestPointee, SrcPointee)) {
+    Kind = CK_DynamicToNull;
+    return;
+  }
+
   // C++ 5.2.7p5
   // Upcasts are resolved statically.
   if (DestRecord && Self.IsDerivedFrom(SrcPointee, DestPointee)) {
index 0a7f1e93af19d75d66fdd28997c5608a9c2adf77..6a7637097b0c7a67c0967325e340e392cb75680b 100644 (file)
@@ -2178,6 +2178,7 @@ void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex,
       // Various C++ casts that are not handled yet.
       case CK_ResolveUnknownAnyType:
       case CK_Dynamic:
+      case CK_DynamicToNull:
       case CK_ToUnion:
       case CK_BaseToDerived:
       case CK_NullToMemberPointer: