]> granicus.if.org Git - clang/commitdiff
Permit Objective C object pointers to be const_casted.
authorJohn McCall <rjmccall@apple.com>
Tue, 18 May 2010 09:35:29 +0000 (09:35 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 18 May 2010 09:35:29 +0000 (09:35 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@104019 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaCXXCast.cpp
lib/Sema/SemaType.cpp
test/SemaObjCXX/const-cast.mm [new file with mode: 0644]

index 1dbdd06eeaba5dea71117dfe6cea16133488e80b..c8eae2fb0a82db73761f19543a11983e027e8593 100644 (file)
@@ -983,7 +983,9 @@ static TryCastResult TryConstCast(Sema &Self, Expr *SrcExpr, QualType DestType,
   // 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
index e0216635157f17c1e9080e34b670996163b619d7..59c699d498dafd1fea1d855e46585aa40ee3fe4e 100644 (file)
@@ -1619,6 +1619,16 @@ bool Sema::UnwrapSimilarPointerTypes(QualType& T1, QualType& T2) {
     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 (file)
index 0000000..933fd47
--- /dev/null
@@ -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);
+}