r206910 - Objective-C ARC. Under ARC, addition of 'bridge' attribute

Fariborz Jahanian fjahanian at apple.com
Tue Apr 22 10:42:01 PDT 2014


Author: fjahanian
Date: Tue Apr 22 12:42:01 2014
New Revision: 206910

URL: http://llvm.org/viewvc/llvm-project?rev=206910&view=rev
Log:
Objective-C ARC. Under ARC, addition of 'bridge' attribute
on CF type is not sufficient and bridge casting is
still required for proper ownership semantics.
// rdar://16650445

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprObjC.cpp
    cfe/trunk/test/SemaObjC/objcbridge-attribute-arc.m
    cfe/trunk/test/SemaObjCXX/objcbridge-attribute-arc.mm

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=206910&r1=206909&r2=206910&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Apr 22 12:42:01 2014
@@ -2551,6 +2551,8 @@ def warn_objc_invalid_bridge : Warning<
   "%0 bridges to %1, not %2">, InGroup<ObjCBridge>;
 def warn_objc_invalid_bridge_to_cf : Warning<
   "%0 cannot bridge to %1">, InGroup<ObjCBridge>;
+def err_objc_invalid_bridge : Error<
+  "under ARC %0 bridges to %1, but it still requires an explicit bridge cast">;
 
 // objc_bridge_related attribute diagnostics.
 def err_objc_bridged_related_invalid_class : Error<

Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=206910&r1=206909&r2=206910&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Tue Apr 22 12:42:01 2014
@@ -3289,7 +3289,8 @@ diagnoseObjCARCConversion(Sema &S, Sourc
 }
 
 template <typename TB>
-static bool CheckObjCBridgeNSCast(Sema &S, QualType castType, Expr *castExpr) {
+static bool CheckObjCBridgeNSCast(Sema &S, QualType castType, Expr *castExpr,
+                                  bool TollFreeBridgeCast) {
   QualType T = castExpr->getType();
   while (const TypedefType *TD = dyn_cast<TypedefType>(T.getTypePtr())) {
     TypedefNameDecl *TDNDecl = TD->getDecl();
@@ -3308,8 +3309,15 @@ static bool CheckObjCBridgeNSCast(Sema &
               ObjCInterfaceDecl *CastClass
                 = InterfacePointerType->getObjectType()->getInterface();
               if ((CastClass == ExprClass) ||
-                  (CastClass && ExprClass->isSuperClassOf(CastClass)))
+                  (CastClass && ExprClass->isSuperClassOf(CastClass))) {
+                if (!TollFreeBridgeCast && S.getLangOpts().ObjCAutoRefCount) {
+                  // bridge attribute is ok. However, under ARC, cast still requires
+                  // an explicit cast and should not compile under ARC.
+                  S.Diag(castExpr->getLocStart(), diag::err_objc_invalid_bridge)
+                    << T << Target->getName();
+                }
                 return true;
+              }
               S.Diag(castExpr->getLocStart(), diag::warn_objc_invalid_bridge)
                 << T << Target->getName() << castType->getPointeeType();
               return true;
@@ -3402,8 +3410,8 @@ void Sema::CheckTollFreeBridgeCast(QualT
   ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(castExpr->getType());
   ARCConversionTypeClass castACTC = classifyTypeForARCConversion(castType);
   if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation) {
-    (void)CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr);
-    (void)CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr);
+    (void)CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, true);
+    (void)CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, true);
   }
   else if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable) {
     (void)CheckObjCBridgeCFCast<ObjCBridgeAttr>(*this, castType, castExpr);
@@ -3624,8 +3632,8 @@ Sema::CheckObjCARCConversion(SourceRange
   
   if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation &&
       (CCK == CCK_CStyleCast || CCK == CCK_FunctionalCast))
-    if (CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr) ||
-        CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr))
+    if (CheckObjCBridgeNSCast<ObjCBridgeAttr>(*this, castType, castExpr, false) ||
+        CheckObjCBridgeNSCast<ObjCBridgeMutableAttr>(*this, castType, castExpr, false))
       return ACR_okay;
     
   if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable &&

Modified: cfe/trunk/test/SemaObjC/objcbridge-attribute-arc.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/objcbridge-attribute-arc.m?rev=206910&r1=206909&r2=206910&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjC/objcbridge-attribute-arc.m (original)
+++ cfe/trunk/test/SemaObjC/objcbridge-attribute-arc.m Tue Apr 22 12:42:01 2014
@@ -64,9 +64,9 @@ typedef CFErrorRef1 CFErrorRef2; // expe
 
 void Test2(CFErrorRef2 cf, NSError *ns, NSString *str, Class c, CFUColor2Ref cf2) {
   (void)(NSString *)cf; // expected-warning {{'CFErrorRef2' (aka 'struct __CFErrorRef *') bridges to NSError, not 'NSString'}}
-  (void)(NSError *)cf; // okay
-  (void)(MyError*)cf; // okay,
-  (void)(NSUColor *)cf2; // okay
+  (void)(NSError *)cf; // expected-error {{under ARC 'CFErrorRef2' (aka 'struct __CFErrorRef *') bridges to NSError, but it still requires an explicit bridge cast}}
+  (void)(MyError*)cf; // expected-error {{under ARC 'CFErrorRef2' (aka 'struct __CFErrorRef *') bridges to NSError, but it still requires an explicit bridge cast}}
+  (void)(NSUColor *)cf2; // expected-error {{under ARC 'CFUColor2Ref' (aka 'union __CFUPrimeColor *') bridges to NSUColor, but it still requires an explicit bridge cast}}
   (void)(CFErrorRef)ns; // okay
   (void)(CFErrorRef)str;  // expected-warning {{'NSString' cannot bridge to 'CFErrorRef' (aka 'struct __CFErrorRef *')}}
   (void)(Class)cf; // expected-warning {{'CFErrorRef2' (aka 'struct __CFErrorRef *') bridges to NSError, not 'Class'}}

Modified: cfe/trunk/test/SemaObjCXX/objcbridge-attribute-arc.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjCXX/objcbridge-attribute-arc.mm?rev=206910&r1=206909&r2=206910&view=diff
==============================================================================
--- cfe/trunk/test/SemaObjCXX/objcbridge-attribute-arc.mm (original)
+++ cfe/trunk/test/SemaObjCXX/objcbridge-attribute-arc.mm Tue Apr 22 12:42:01 2014
@@ -64,9 +64,9 @@ typedef CFErrorRef1 CFErrorRef2; // expe
 
 void Test2(CFErrorRef2 cf, NSError *ns, NSString *str, Class c, CFUColor2Ref cf2) {
   (void)(NSString *)cf; // expected-warning {{'CFErrorRef2' (aka '__CFErrorRef *') bridges to NSError, not 'NSString'}}
-  (void)(NSError *)cf; // okay
-  (void)(MyError*)cf; // okay,
-  (void)(NSUColor *)cf2; // okay
+  (void)(NSError *)cf; // expected-error {{under ARC 'CFErrorRef2' (aka '__CFErrorRef *') bridges to NSError, but it still requires an explicit bridge cast}}
+  (void)(MyError*)cf; // expected-error {{under ARC 'CFErrorRef2' (aka '__CFErrorRef *') bridges to NSError, but it still requires an explicit bridge cast}},
+  (void)(NSUColor *)cf2; // expected-error {{under ARC 'CFUColor2Ref' (aka '__CFUPrimeColor *') bridges to NSUColor, but it still requires an explicit bridge cast}}
   (void)(CFErrorRef)ns; // okay
   (void)(CFErrorRef)str;  // expected-warning {{'NSString' cannot bridge to 'CFErrorRef' (aka '__CFErrorRef *')}}
   (void)(Class)cf; // expected-warning {{'CFErrorRef2' (aka '__CFErrorRef *') bridges to NSError, not 'Class'}}





More information about the cfe-commits mailing list