From 3f001ff471aec590a65a383a25367a4e1c82f5a3 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Wed, 3 Oct 2012 17:55:29 +0000 Subject: [PATCH] objective-C arc: Warn under arc about a use of an ivar inside a block that doesn't have a 'self' as this implicitly captures 'self' and could create retain cycles. Provide fixit. // rdar://11194874 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165133 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 ++++ lib/Sema/SemaExpr.cpp | 3 +++ test/FixIt/fixit-missing-self-in-block.m | 20 ++++++++++++++++++++ test/SemaObjC/warn-implicit-self-in-block.m | 18 ++++++++++++++++++ test/SemaObjC/warn-retain-cycle.m | 2 +- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 test/FixIt/fixit-missing-self-in-block.m create mode 100644 test/SemaObjC/warn-implicit-self-in-block.m diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 811be4e1b6..20ced10606 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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, DefaultIgnore; +def warn_implicitly_retains_self : Warning < + "block implicitily retains 'self' - explicitly mention 'self' to indicate " + "this is intended behavior">, + InGroup>; 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 " diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index 75bc8658c6..bbae55b598 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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/test/FixIt/fixit-missing-self-in-block.m b/test/FixIt/fixit-missing-self-in-block.m new file mode 100644 index 0000000000..8fd9564ed0 --- /dev/null +++ b/test/FixIt/fixit-missing-self-in-block.m @@ -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/test/SemaObjC/warn-implicit-self-in-block.m b/test/SemaObjC/warn-implicit-self-in-block.m new file mode 100644 index 0000000000..6a19283506 --- /dev/null +++ b/test/SemaObjC/warn-implicit-self-in-block.m @@ -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 diff --git a/test/SemaObjC/warn-retain-cycle.m b/test/SemaObjC/warn-retain-cycle.m index fb8f2b77f0..eb4e966c77 100644 --- a/test/SemaObjC/warn-retain-cycle.m +++ b/test/SemaObjC/warn-retain-cycle.m @@ -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); -- 2.40.0