[cfe-commits] r90895 - in /cfe/trunk: include/clang/AST/Expr.h lib/CodeGen/CGExprScalar.cpp lib/Sema/SemaCXXCast.cpp test/SemaObjCXX/cstyle-cast.mm

Fariborz Jahanian fjahanian at apple.com
Tue Dec 8 15:09:15 PST 2009


Author: fjahanian
Date: Tue Dec  8 17:09:15 2009
New Revision: 90895

URL: http://llvm.org/viewvc/llvm-project?rev=90895&view=rev
Log:
More detailed analysis of typecast to an objective-c pointer
in objective-c++ mode without being too lenient.

Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/Sema/SemaCXXCast.cpp
    cfe/trunk/test/SemaObjCXX/cstyle-cast.mm

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=90895&r1=90894&r2=90895&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Dec  8 17:09:15 2009
@@ -1571,7 +1571,11 @@
     CK_FloatingCast,
     
     /// CK_MemberPointerToBoolean - Member pointer to boolean
-    CK_MemberPointerToBoolean
+    CK_MemberPointerToBoolean,
+
+    /// CK_AnyPointerToObjCPointerCast - Casting any pointer to objective-c 
+    /// pointer
+    CK_AnyPointerToObjCPointerCast
 
   };
 

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=90895&r1=90894&r2=90895&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Tue Dec  8 17:09:15 2009
@@ -798,6 +798,7 @@
     //assert(0 && "Unknown cast kind!");
     break;
 
+  case CastExpr::CK_AnyPointerToObjCPointerCast:
   case CastExpr::CK_BitCast: {
     Value *Src = Visit(const_cast<Expr*>(E));
     return Builder.CreateBitCast(Src, ConvertType(DestTy));

Modified: cfe/trunk/lib/Sema/SemaCXXCast.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCXXCast.cpp?rev=90895&r1=90894&r2=90895&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaCXXCast.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCXXCast.cpp Tue Dec  8 17:09:15 2009
@@ -539,6 +539,11 @@
           return TC_Success;
         }
       }
+      else if (CStyle && DestType->isObjCObjectPointerType()) {
+        // allow c-style cast of objective-c pointers as they are pervasive.
+        Kind = CastExpr::CK_AnyPointerToObjCPointerCast;
+        return TC_Success;
+      }
     }
   }
 
@@ -1053,8 +1058,10 @@
     return TC_Failed;
   }
   
-  bool destIsPtr = DestType->isPointerType();
-  bool srcIsPtr = SrcType->isPointerType();
+  bool destIsPtr = 
+    CStyle? DestType->isAnyPointerType() : DestType->isPointerType();
+  bool srcIsPtr = 
+    CStyle ? SrcType->isAnyPointerType() : SrcType->isPointerType();
   if (!destIsPtr && !srcIsPtr) {
     // Except for std::nullptr_t->integer and lvalue->reference, which are
     // handled above, at least one of the two arguments must be a pointer.
@@ -1106,7 +1113,11 @@
     msg = diag::err_bad_cxx_cast_const_away;
     return TC_Failed;
   }
-
+  if (CStyle && DestType->isObjCObjectPointerType()) {
+    Kind = CastExpr::CK_AnyPointerToObjCPointerCast;
+    return TC_Success;
+  }
+  
   // Not casting away constness, so the only remaining check is for compatible
   // pointer categories.
   Kind = CastExpr::CK_BitCast;
@@ -1141,7 +1152,6 @@
   // Void pointers are not specified, but supported by every compiler out there.
   // So we finish by allowing everything that remains - it's got to be two
   // object pointers.
-  Kind = CastExpr::CK_BitCast;
   return TC_Success;
 }
 
@@ -1160,10 +1170,6 @@
   if (CastTy->isDependentType() || CastExpr->isTypeDependent())
     return false;
 
-  // allow c-style cast of objective-c pointers as they are pervasive.
-  if (CastTy->isObjCObjectPointerType())
-    return false;
-  
   if (!CastTy->isLValueReferenceType() && !CastTy->isRecordType())
     DefaultFunctionArrayConversion(CastExpr);
 

Modified: cfe/trunk/test/SemaObjCXX/cstyle-cast.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/cstyle-cast.mm?rev=90895&r1=90894&r2=90895&view=diff

==============================================================================
--- cfe/trunk/test/SemaObjCXX/cstyle-cast.mm (original)
+++ cfe/trunk/test/SemaObjCXX/cstyle-cast.mm Tue Dec  8 17:09:15 2009
@@ -3,7 +3,9 @@
 @protocol P @end
 @interface I @end
 
-int main() {
+struct X { X(); };
+
+void test1(X x) {
   void *cft;
   id oct = (id)cft;
 
@@ -12,9 +14,27 @@
 
   I* iict = (I*)cft;
 
-  id<P> pid = (id<P>)cft;
+  id<P> qid = (id<P>)cft;
 
   I<P> *ip = (I<P>*)cft;
+
+  (id)x; // expected-error {{C-style cast from 'struct X' to 'id' is not allowed}}
+
+  id *pid = (id*)ccct;
+
+  id<P> *qpid = (id<P>*)ccct;
+
+  int **pii;
+
+  ccct = (Class)pii;
+
+  qpid = (id<P>*)pii;
+
+  iict = (I*)pii;
+
+  pii = (int **)ccct;
+
+  pii = (int **)qpid;
   
 }
 





More information about the cfe-commits mailing list