]> granicus.if.org Git - clang/commitdiff
objc-arc: warn when a 'retain' block property is
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 14 Sep 2011 18:03:46 +0000 (18:03 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 14 Sep 2011 18:03:46 +0000 (18:03 +0000)
declared which does not force a 'copy' of the block literal
object. // rdar://9829425

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaObjCProperty.cpp
test/SemaObjC/arc-retain-block-property.m [new file with mode: 0644]
test/SemaObjC/warn-retain-cycle.m

index 186908252f019f58d4abd29768989922c92e8382..bb24f21e241c7e50954d500f3061d536c1beddf9 100644 (file)
@@ -494,6 +494,9 @@ def warn_property_attr_mismatch : Warning<
 def warn_objc_property_copy_missing_on_block : Warning<
     "'copy' attribute must be specified for the block property "
     "when -fobjc-gc-only is specified">;
+def warn_objc_property_retain_of_block : Warning<
+    "retain'ed block property does not copy the block "
+    "- use copy attribute instead">;
 def warn_atomic_property_rule : Warning<
   "writable atomic property %0 cannot pair a synthesized setter/getter "
   "with a user defined setter/getter">;
index fb0d13a66abf9ffa085291c90576dbddea1a5162..33c5e71b5eb47975488cd694f2fc4bc1b125df56 100644 (file)
@@ -1701,7 +1701,7 @@ void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
            (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
       Diag(Loc, diag::err_objc_property_attr_mutually_exclusive)
         << "retain" << "weak";
-      Attributes &= ~ObjCDeclSpec::DQ_PR_weak;
+      Attributes &= ~ObjCDeclSpec::DQ_PR_retain;
   }
   else if ((Attributes & ObjCDeclSpec::DQ_PR_strong) &&
            (Attributes & ObjCDeclSpec::DQ_PR_weak)) {
@@ -1743,4 +1743,10 @@ void Sema::CheckObjCPropertyAttributes(Decl *PDecl,
       && getLangOptions().getGC() == LangOptions::GCOnly
       && PropertyTy->isBlockPointerType())
     Diag(Loc, diag::warn_objc_property_copy_missing_on_block);
+  else if (getLangOptions().ObjCAutoRefCount &&
+           (Attributes & ObjCDeclSpec::DQ_PR_retain) &&
+           !(Attributes & ObjCDeclSpec::DQ_PR_readonly) &&
+           !(Attributes & ObjCDeclSpec::DQ_PR_strong) &&
+           PropertyTy->isBlockPointerType())
+      Diag(Loc, diag::warn_objc_property_retain_of_block);
 }
diff --git a/test/SemaObjC/arc-retain-block-property.m b/test/SemaObjC/arc-retain-block-property.m
new file mode 100644 (file)
index 0000000..a1b181c
--- /dev/null
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -fsyntax-only -fblocks -fobjc-arc -fobjc-nonfragile-abi -verify %s
+// rdar://9829425
+
+extern void doSomething();
+
+@interface Test
+{
+@public
+  void (^aBlock)(void);
+}
+@property (retain) void (^aBlock)(void); // expected-warning {{retain'ed block property does not copy the block - use copy attribute instead}}
+@property (weak, retain) void (^aBlockW)(void); // expected-error {{property attributes 'retain' and 'weak' are mutually exclusive}} 
+@property (strong, retain) void (^aBlockS)(void); // OK
+@property (readonly, retain) void (^aBlockR)(void); // OK
+@property (copy, retain) void (^aBlockC)(void); // expected-error {{property attributes 'copy' and 'retain' are mutually exclusive}}
+@property (assign, retain) void (^aBlockA)(void); // expected-error {{property attributes 'assign' and 'retain' are mutually exclusive}}
+@end
+
+@implementation Test
+@synthesize aBlock;
+@dynamic aBlockW, aBlockS, aBlockR, aBlockC, aBlockA;
+@end
+
+int main() {
+  Test *t;
+  t.aBlock = ^{ doSomething(); };
+  t.aBlockW = ^{ doSomething(); };
+  t.aBlockS = ^{ doSomething(); };
+}
+
index 71385b8400bb204687070ef605b64a75d49e55a1..8530195756ee935a06a1f50f98cf27ebb491f2f7 100644 (file)
@@ -27,7 +27,7 @@ void test0(Test0 *x) {
 }
 
 @interface BlockOwner
-@property (retain) void (^strong)(void);
+@property (retain) void (^strong)(void); // expected-warning {{retain'ed block property does not copy the block - use copy attribute instead}}
 @end
 
 @interface Test1 {