r261703 - [analyzer] Find ObjC 'self' decl even when block captures local named 'self'.

Devin Coughlin via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 23 14:26:05 PST 2016


Author: dcoughlin
Date: Tue Feb 23 16:26:04 2016
New Revision: 261703

URL: http://llvm.org/viewvc/llvm-project?rev=261703&view=rev
Log:
[analyzer] Find ObjC 'self' decl even when block captures local named 'self'.

When looking up the 'self' decl in block captures, make sure to find the actual
self declaration even when the block captures a local variable named 'self'.

rdar://problem/24751280

Modified:
    cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp
    cfe/trunk/test/Analysis/blocks.m
    cfe/trunk/test/Analysis/lambdas.mm

Modified: cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp?rev=261703&r1=261702&r2=261703&view=diff
==============================================================================
--- cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp (original)
+++ cfe/trunk/lib/Analysis/AnalysisDeclContext.cpp Tue Feb 23 16:26:04 2016
@@ -135,6 +135,10 @@ bool AnalysisDeclContext::isBodyAutosynt
   return Tmp && Body->getLocStart().isValid();
 }
 
+/// Returns true if \param VD is an Objective-C implicit 'self' parameter.
+static bool isSelfDecl(const VarDecl *VD) {
+  return isa<ImplicitParamDecl>(VD) && VD->getName() == "self";
+}
 
 const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
   if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
@@ -143,7 +147,7 @@ const ImplicitParamDecl *AnalysisDeclCon
     // See if 'self' was captured by the block.
     for (const auto &I : BD->captures()) {
       const VarDecl *VD = I.getVariable();
-      if (VD->getName() == "self")
+      if (isSelfDecl(VD))
         return dyn_cast<ImplicitParamDecl>(VD);
     }    
   }
@@ -161,7 +165,7 @@ const ImplicitParamDecl *AnalysisDeclCon
       continue;
 
     VarDecl *VD = LC.getCapturedVar();
-    if (VD->getName() == "self")
+    if (isSelfDecl(VD))
       return dyn_cast<ImplicitParamDecl>(VD);
   }
 

Modified: cfe/trunk/test/Analysis/blocks.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/blocks.m?rev=261703&r1=261702&r2=261703&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/blocks.m (original)
+++ cfe/trunk/test/Analysis/blocks.m Tue Feb 23 16:26:04 2016
@@ -210,3 +210,25 @@ void testCallContainingWithSignature5()
   });
 }
 
+__attribute__((objc_root_class))
+ at interface SuperClass
+- (void)someMethod;
+ at end
+
+ at interface SomeClass : SuperClass
+ at end
+
+// Make sure to properly handle super-calls when a block captures
+// a local variable named 'self'.
+ at implementation SomeClass
+-(void)foo; {
+  /*__weak*/ SomeClass *weakSelf = self;
+  (void)(^(void) {
+    SomeClass *self = weakSelf;
+    (void)(^(void) {
+      (void)self;
+      [super someMethod]; // no-warning
+    });
+  });
+}
+ at end

Modified: cfe/trunk/test/Analysis/lambdas.mm
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/lambdas.mm?rev=261703&r1=261702&r2=261703&view=diff
==============================================================================
--- cfe/trunk/test/Analysis/lambdas.mm (original)
+++ cfe/trunk/test/Analysis/lambdas.mm Tue Feb 23 16:26:04 2016
@@ -12,7 +12,6 @@ int clang_analyzer_eval(int);
 }
 @end
 
-
 @implementation Sub
 - (void)callMethodOnSuperInCXXLambda; {
   // Explicit capture.
@@ -26,6 +25,20 @@ int clang_analyzer_eval(int);
   }();
 }
 
+// Make sure to properly handle super-calls when a block captures
+// a local variable named 'self'.
+- (void)callMethodOnSuperInCXXLambdaWithRedefinedSelf; {
+  /*__weak*/ Sub *weakSelf = self;
+  // Implicit capture. (Sema outlaws explicit capture of a redefined self
+  // and a call to super [which uses the original self]).
+  [=]() {
+    Sub *self = weakSelf;
+    [=]() {
+      [super superMethod];
+    }();
+  }();
+}
+
 - (void)swapIvars {
   int tmp = _ivar1;
   _ivar1 = _ivar2;




More information about the cfe-commits mailing list