objective-C arc: Warn under arc about a use of an ivar inside a block
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 3 Oct 2012 17:55:29 +0000 (17:55 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 3 Oct 2012 17:55:29 +0000 (17:55 +0000)
that doesn't have a 'self' as this implicitly captures 'self' and could
create retain cycles. Provide fixit. // rdar://11194874

llvm-svn: 165133

clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/lib/Sema/SemaExpr.cpp
clang/test/FixIt/fixit-missing-self-in-block.m [new file with mode: 0644]
clang/test/SemaObjC/warn-implicit-self-in-block.m [new file with mode: 0644]
clang/test/SemaObjC/warn-retain-cycle.m

index 811be4e..20ced10 100644 (file)
@@ -717,6 +717,10 @@ def warn_arc_repeated_use_of_weak : Warning <
   "but may be unpredictably set to nil; assign to a strong variable to keep "
   "the object alive">,
   InGroup<ARCRepeatedUseOfWeak>, DefaultIgnore;
+def warn_implicitly_retains_self : Warning <
+  "block implicitily retains 'self' - explicitly mention 'self' to indicate "
+  "this is intended behavior">,
+  InGroup<DiagGroup<"implicit-retain-self">>;
 def warn_arc_possible_repeated_use_of_weak : Warning <
   "weak %select{variable|property|implicit property|instance variable}0 %1 may "
   "be accessed multiple times in this %select{function|method|block|lambda}2 "
index 75bc865..bbae55b 100644 (file)
@@ -2008,6 +2008,9 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S,
           if (Level != DiagnosticsEngine::Ignored)
             getCurFunction()->recordUseOfWeak(Result);
         }
+        if (CurContext->isClosure())
+          Diag(Loc, diag::warn_implicitly_retains_self)
+            << FixItHint::CreateInsertion(Loc, "self->");
       }
       
       return Owned(Result);
diff --git a/clang/test/FixIt/fixit-missing-self-in-block.m b/clang/test/FixIt/fixit-missing-self-in-block.m
new file mode 100644 (file)
index 0000000..8fd9564
--- /dev/null
@@ -0,0 +1,20 @@
+// RUN: cp %s %t
+// RUN: %clang_cc1 -x objective-c -fobjc-arc -fblocks -fixit %t
+// RUN: %clang_cc1 -x objective-c -fobjc-arc -fblocks -Werror %t
+// rdar://11194874
+
+@interface Root @end
+
+@interface I : Root
+{
+  int _bar;
+}
+@end
+
+@implementation I
+  - (void)foo{
+      ^{
+           _bar = 3;
+       }();
+  }
+@end
diff --git a/clang/test/SemaObjC/warn-implicit-self-in-block.m b/clang/test/SemaObjC/warn-implicit-self-in-block.m
new file mode 100644 (file)
index 0000000..6a19283
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -x objective-c -fobjc-arc -fblocks -verify %s
+// rdar://11194874
+
+@interface Root @end
+
+@interface I : Root
+{
+  int _bar;
+}
+@end
+
+@implementation I
+  - (void)foo{
+      ^{
+           _bar = 3; // expected-warning {{block implicitily retains 'self' - explicitly mention 'self' to indicate this is intended behavior}}
+       }();
+  }
+@end
index fb8f2b7..eb4e966 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -fblocks -verify -Wno-objc-root-class %s
+// RUN: %clang_cc1 -fsyntax-only -fobjc-runtime-has-weak -fobjc-arc -fblocks -verify -Wno-objc-root-class -Wno-implicit-retain-self %s
 
 void *_Block_copy(const void *block);