]> granicus.if.org Git - clang/commitdiff
objc-arc: allow explicit unbridged casts if the source of the cast is a
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 20 Jun 2011 20:54:42 +0000 (20:54 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 20 Jun 2011 20:54:42 +0000 (20:54 +0000)
message sent to an objc method (or property access)
// rdar://9474349

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

include/clang/Sema/Sema.h
lib/Sema/SemaExprObjC.cpp
test/SemaObjC/arc-unbridged-cast.m [new file with mode: 0644]

index 06d7d58988c1efd43f7990e6f84f7f78a17d1b42..f43ff934ff364c310f8c8874f67e0e3eac53f337 100644 (file)
@@ -5613,7 +5613,11 @@ public:
   ExprResult CXXCheckCStyleCast(SourceRange R, QualType CastTy, ExprValueKind &VK,
                                 Expr *CastExpr, CastKind &Kind,
                                 CXXCastPath &BasePath, bool FunctionalStyle);
-  
+    
+  /// \brief Checks for valid expressions which can be cast to an ObjC
+  /// pointer without needing a bridge cast.
+  bool ValidObjCARCNoBridgeCastExpr(const Expr *Exp);
+    
   /// \brief Checks for invalid conversions and casts between
   /// retainable pointers and other pointer kinds.
   void CheckObjCARCConversion(SourceRange castRange, QualType castType, 
index 5aff7347a6d7f5d6c8ee3d94ee015dbbeef74101..9c9332a5f0e065f2a5a9b812eba72b8f8bd8c524 100644 (file)
@@ -1550,6 +1550,12 @@ namespace {
   };
 }
 
+bool
+Sema::ValidObjCARCNoBridgeCastExpr(const Expr *Exp) {
+  Exp = Exp->IgnoreParenImpCasts();
+  return isa<ObjCMessageExpr>(Exp) || isa<ObjCPropertyRefExpr>(Exp);
+}
+
 void 
 Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
                              Expr *castExpr, CheckedConversionKind CCK) {
@@ -1606,6 +1612,10 @@ Sema::CheckObjCARCConversion(SourceRange castRange, QualType castType,
     
     if (castType->isObjCARCBridgableType() && 
         castExprType->isCARCBridgableType()) {
+      // explicit unbridged casts are allowed if the source of the cast is a 
+      // message sent to an objc method (or property access)
+      if (ValidObjCARCNoBridgeCastExpr(castExpr))
+        return;
       Diag(loc, diag::err_arc_cast_requires_bridge)
         << 2
         << castExprType
diff --git a/test/SemaObjC/arc-unbridged-cast.m b/test/SemaObjC/arc-unbridged-cast.m
new file mode 100644 (file)
index 0000000..03c84cf
--- /dev/null
@@ -0,0 +1,18 @@
+// // RUN: %clang_cc1 -triple x86_64-apple-darwin11 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -verify %s
+// rdar://9744349
+
+typedef const struct __CFString * CFStringRef;
+
+@interface I 
+@property CFStringRef P;
+@end
+
+@implementation I
+@synthesize P;
+- (id) Meth {
+    I* p1 = (id)[p1 P];
+    id p2 = (__bridge_transfer id)[p1 P];
+    id p3 = (__bridge I*)[p1 P];
+    return (id) p1.P;
+}
+@end