]> granicus.if.org Git - clang/commitdiff
ObjectiveC ARC. warn in presense of __bridge casting to
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 20 Nov 2013 22:55:41 +0000 (22:55 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 20 Nov 2013 22:55:41 +0000 (22:55 +0000)
or from a toll free bridge cast. // rdar://15454846

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

lib/Sema/SemaExprObjC.cpp
test/SemaObjC/objcbridge-attribute.m

index 6a0c09a4db200527a5a06bb03239060d5da7045b..65f584f657bfc08220ed0b34036ab9142e6ea366 100644 (file)
@@ -3617,6 +3617,15 @@ ExprResult Sema::ActOnObjCBridgedCast(Scope *S,
                                       Expr *SubExpr) {
   TypeSourceInfo *TSInfo = 0;
   QualType T = GetTypeFromParser(Type, &TSInfo);
+  if (Kind == OBC_Bridge) {
+    // warn in presense of __bridge casting to or from a toll free bridge cast.
+    ARCConversionTypeClass exprACTC = classifyTypeForARCConversion(SubExpr->getType());
+    ARCConversionTypeClass castACTC = classifyTypeForARCConversion(T);
+    if (castACTC == ACTC_retainable && exprACTC == ACTC_coreFoundation)
+      (void)CheckObjCBridgeNSCast(*this, T, SubExpr);
+    else if (castACTC == ACTC_coreFoundation && exprACTC == ACTC_retainable)
+      (void)CheckObjCBridgeCFCast(*this, T, SubExpr);
+  }
   if (!TSInfo)
     TSInfo = Context.getTrivialTypeSourceInfo(T, LParenLoc);
   return BuildObjCBridgedCast(LParenLoc, Kind, BridgeKeywordLoc, TSInfo, 
index 24b7289f8d9a8b57ead3c6633a9fc3a8f72e0970..67e09e5fb19972653a4c9c56fa5966e9dff8c15e 100644 (file)
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -fobjc-arc -verify -Wno-objc-root-class %s
 // rdar://15454846
 
-typedef struct __attribute__ ((objc_bridge(NSError))) __CFErrorRef * CFErrorRef; // expected-note 5 {{declared here}}
+typedef struct __attribute__ ((objc_bridge(NSError))) __CFErrorRef * CFErrorRef; // expected-note 7 {{declared here}}
 
 typedef struct __attribute__ ((objc_bridge(MyError))) __CFMyErrorRef * CFMyErrorRef; // expected-note 3 {{declared here}}
 
@@ -45,7 +45,7 @@ id Test1(CFTestingRef cf) {
 
 typedef CFErrorRef CFErrorRef1;
 
-typedef CFErrorRef1 CFErrorRef2; // expected-note {{declared here}}
+typedef CFErrorRef1 CFErrorRef2; // expected-note {{declared here}}
 
 @protocol P1 @end
 @protocol P2 @end
@@ -53,7 +53,7 @@ typedef CFErrorRef1 CFErrorRef2; // expected-note {{declared here}}
 @protocol P4 @end
 @protocol P5 @end
 
-@interface NSError<P1, P2, P3> @end // expected-note 5 {{declared here}}
+@interface NSError<P1, P2, P3> @end // expected-note 7 {{declared here}}
 
 @interface MyError : NSError // expected-note 3 {{declared here}}
 @end
@@ -127,3 +127,13 @@ void Test8(CFMyPersonalErrorRef cf) {
   (void)(id<P1, P2, P3, P4, P5>)cf; // expected-warning {{'CFMyPersonalErrorRef' (aka 'struct __CFMyPersonalErrorRef *') bridges to MyPersonalError, not 'id<P1,P2,P3,P4,P5>'}}
 }
 
+void Test9(CFErrorRef2 cf, NSError *ns, NSString *str, Class c, CFUColor2Ref cf2) {
+  (void)(__bridge NSString *)cf; // expected-warning {{'CFErrorRef2' (aka 'struct __CFErrorRef *') bridges to NSError, not 'NSString'}}
+  (void)(__bridge NSError *)cf; // okay
+  (void)(__bridge MyError*)cf; // okay,
+  (void)(__bridge NSUColor *)cf2; // okay
+  (void)(__bridge CFErrorRef)ns; // okay
+  (void)(__bridge CFErrorRef)str;  // expected-warning {{'NSString' cannot bridge to 'CFErrorRef' (aka 'struct __CFErrorRef *')}}
+  (void)(__bridge Class)cf; // expected-warning {{'CFErrorRef2' (aka 'struct __CFErrorRef *') bridges to NSError, not 'Class'}}
+  (void)(__bridge CFErrorRef)c; // expected-warning {{'__unsafe_unretained Class' cannot bridge to 'CFErrorRef' (aka 'struct __CFErrorRef *')}}
+}