r220727 - Objective-C ARC [qoi]. Issue diagnostic if __bridge casting
Fariborz Jahanian
fjahanian at apple.com
Mon Oct 27 15:33:06 PDT 2014
Author: fjahanian
Date: Mon Oct 27 17:33:06 2014
New Revision: 220727
URL: http://llvm.org/viewvc/llvm-project?rev=220727&view=rev
Log:
Objective-C ARC [qoi]. Issue diagnostic if __bridge casting
to C type a collection literal. rdar://18768214
Added:
cfe/trunk/test/SemaObjC/arc-cfbridge-warning.m
Modified:
cfe/trunk/include/clang/Basic/DiagnosticGroups.td
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/SemaExprObjC.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticGroups.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticGroups.td?rev=220727&r1=220726&r2=220727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticGroups.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticGroups.td Mon Oct 27 17:33:06 2014
@@ -422,6 +422,7 @@ def : DiagGroup<"arc-abi">;
def ARCUnsafeRetainedAssign : DiagGroup<"arc-unsafe-retained-assign">;
def ARCRetainCycles : DiagGroup<"arc-retain-cycles">;
def ARCNonPodMemAccess : DiagGroup<"arc-non-pod-memaccess">;
+def ARCBridgeCastRelease : DiagGroup<"arc-bridge-cast-release">;
def AutomaticReferenceCounting : DiagGroup<"arc",
[ARCUnsafeRetainedAssign,
ARCRetainCycles,
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=220727&r1=220726&r2=220727&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Mon Oct 27 17:33:06 2014
@@ -4459,6 +4459,11 @@ def note_arc_retain_cycle_owner : Note<
def warn_arc_object_memaccess : Warning<
"%select{destination for|source of}0 this %1 call is a pointer to "
"ownership-qualified type %2">, InGroup<ARCNonPodMemAccess>;
+
+def warn_arc_consumed_object_released : Warning<
+ "__bridge cast of collection literal of type %1 to \"bridgeable\" C "
+ "type %0 causes early release of the collection">,
+ InGroup<ARCBridgeCastRelease>;
let CategoryName = "ARC and @properties" in {
Modified: cfe/trunk/lib/Sema/SemaExprObjC.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprObjC.cpp?rev=220727&r1=220726&r2=220727&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprObjC.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprObjC.cpp Mon Oct 27 17:33:06 2014
@@ -4075,11 +4075,22 @@ ExprResult Sema::BuildObjCBridgedCast(So
// Okay: id -> CF
CK = CK_BitCast;
switch (Kind) {
- case OBC_Bridge:
- // Reclaiming a value that's going to be __bridge-casted to CF
- // is very dangerous, so we don't do it.
- SubExpr = maybeUndoReclaimObject(SubExpr);
- break;
+ case OBC_Bridge: {
+ const ImplicitCastExpr *implCE = dyn_cast<ImplicitCastExpr>(SubExpr);
+ while (implCE && implCE->getCastKind() == CK_BitCast)
+ implCE = dyn_cast<ImplicitCastExpr>(implCE->getSubExpr());
+ if (implCE && (implCE->getCastKind() == CK_ARCConsumeObject))
+ if (const Expr *LitExp = implCE->getSubExpr())
+ if ((isa<ObjCArrayLiteral>(LitExp) ||
+ isa<ObjCDictionaryLiteral>(LitExp)) &&
+ T->isCARCBridgableType())
+ Diag(BridgeKeywordLoc, diag::warn_arc_consumed_object_released)
+ << T << implCE->getType();
+ // Reclaiming a value that's going to be __bridge-casted to CF
+ // is very dangerous, so we don't do it.
+ SubExpr = maybeUndoReclaimObject(SubExpr);
+ break;
+ }
case OBC_BridgeRetained:
// Produce the object before casting it.
Added: cfe/trunk/test/SemaObjC/arc-cfbridge-warning.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/arc-cfbridge-warning.m?rev=220727&view=auto
==============================================================================
--- cfe/trunk/test/SemaObjC/arc-cfbridge-warning.m (added)
+++ cfe/trunk/test/SemaObjC/arc-cfbridge-warning.m Mon Oct 27 17:33:06 2014
@@ -0,0 +1,41 @@
+// RUN: %clang_cc1 -fsyntax-only -x objective-c -fobjc-arc -verify -Wno-objc-root-class %s
+// rdar://18768214
+
+
+ at class NSArray;
+typedef const struct __attribute__((objc_bridge(NSArray))) __CFArray * CFArrayRef;
+ at class NSString;
+typedef const void * CFTypeRef;
+
+typedef const struct __attribute__((objc_bridge(NSString))) __CFString * CFStringRef;
+
+typedef long NSInteger;
+typedef unsigned long NSUInteger;
+
+ at interface NSObject {
+ Class isa __attribute__((deprecated));
+}
++ (void)initialize;
+- (instancetype)init;
++ (instancetype)new;
++ (instancetype)alloc;
+- (void)dealloc;
+ at end
+
+ at interface NSArray : NSObject
+ at property (readonly) NSUInteger count;
+- (id)objectAtIndex:(NSUInteger)index;
+- (instancetype)init __attribute__((objc_designated_initializer));
+- (instancetype)initWithObjects:(const id [])objects count:(NSUInteger)cnt __attribute__((objc_designated_initializer));
++ (instancetype)array;
++ (instancetype)arrayWithObject:(id)anObject;
++ (instancetype)arrayWithObjects:(const id [])objects count:(NSUInteger)cnt;
++ (instancetype)arrayWithObjects:(id)firstObj, ... __attribute__((sentinel(0,1)));
+ at end
+
+static CFStringRef _s;
+
+CFArrayRef _array()
+{
+ return (__bridge CFArrayRef)@[(__bridge NSString *)_s]; // expected-warning {{__bridge cast of collection literal of type 'NSArray *' to "bridgeable" C type 'CFArrayRef' (aka 'const struct __CFArray *') causes early release of the collection}}
+}
More information about the cfe-commits
mailing list