As a first step towards fixing PR9641, add a CK_DynamicToNull cast kind which
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);
}
llvm-svn: 129256
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index 28f4212..227d656 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -987,6 +987,8 @@
return "UncheckedDerivedToBase";
case CK_Dynamic:
return "Dynamic";
+ case CK_DynamicToNull:
+ return "DynamicToNull";
case CK_ToUnion:
return "ToUnion";
case CK_ArrayToPointerDecay:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index e0f9958..aa8d91a 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -1768,6 +1768,7 @@
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 @@
case CK_DerivedToBase:
case CK_UncheckedDerivedToBase:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ToUnion:
case CK_ArrayToPointerDecay:
case CK_FunctionToPointerDecay:
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp
index a35f81c..ad51316 100644
--- a/clang/lib/CodeGen/CGExpr.cpp
+++ b/clang/lib/CodeGen/CGExpr.cpp
@@ -1867,7 +1867,8 @@
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);
diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp
index 75e3a78..5e1ac3c 100644
--- a/clang/lib/CodeGen/CGExprAgg.cpp
+++ b/clang/lib/CodeGen/CGExprAgg.cpp
@@ -255,7 +255,10 @@
}
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?
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index 822a999..578a37f 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -552,6 +552,7 @@
case CK_GetObjCProperty:
case CK_ToVoid:
case CK_Dynamic:
+ case CK_DynamicToNull:
case CK_ResolveUnknownAnyType:
return 0;
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 65aa46f..322b6e0 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -1053,7 +1053,8 @@
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);
diff --git a/clang/lib/Sema/SemaCXXCast.cpp b/clang/lib/Sema/SemaCXXCast.cpp
index 31a772a..52a13ef 100644
--- a/clang/lib/Sema/SemaCXXCast.cpp
+++ b/clang/lib/Sema/SemaCXXCast.cpp
@@ -522,6 +522,14 @@
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)) {
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
index 0a7f1e9..6a76370 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngine.cpp
@@ -2178,6 +2178,7 @@
// 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: