Permit Objective C object pointers to be const_casted.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@104019 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaCXXCast.cpp b/lib/Sema/SemaCXXCast.cpp
index 1dbdd06..c8eae2f 100644
--- a/lib/Sema/SemaCXXCast.cpp
+++ b/lib/Sema/SemaCXXCast.cpp
@@ -983,7 +983,9 @@
   // C++ 5.2.11p5: For a const_cast involving pointers to data members [...]
   //   the rules for const_cast are the same as those used for pointers.
 
-  if (!DestType->isPointerType() && !DestType->isMemberPointerType()) {
+  if (!DestType->isPointerType() &&
+      !DestType->isMemberPointerType() &&
+      !DestType->isObjCObjectPointerType()) {
     // Cannot cast to non-pointer, non-reference type. Note that, if DestType
     // was a reference type, we converted it to a pointer above.
     // The status of rvalue references isn't entirely clear, but it looks like
diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp
index e021663..59c699d 100644
--- a/lib/Sema/SemaType.cpp
+++ b/lib/Sema/SemaType.cpp
@@ -1619,6 +1619,16 @@
     T2 = T2MPType->getPointeeType();
     return true;
   }
+
+  if (getLangOptions().ObjC1) {
+    const ObjCObjectPointerType *T1OPType = T1->getAs<ObjCObjectPointerType>(),
+                                *T2OPType = T2->getAs<ObjCObjectPointerType>();
+    if (T1OPType && T2OPType) {
+      T1 = T1OPType->getPointeeType();
+      T2 = T2OPType->getPointeeType();
+      return true;
+    }
+  }
   return false;
 }
 
diff --git a/test/SemaObjCXX/const-cast.mm b/test/SemaObjCXX/const-cast.mm
new file mode 100644
index 0000000..933fd47
--- /dev/null
+++ b/test/SemaObjCXX/const-cast.mm
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+@class Foo;
+
+void test() {
+    const Foo *foo1 = 0;
+    Foo *foo2 = foo1; // expected-error {{cannot initialize}}
+}
+
+void test1() {
+    const Foo *foo1 = 0;
+    Foo *foo2 = const_cast<Foo*>(foo1);
+}