]> granicus.if.org Git - clang/commitdiff
When capturing 'this' in a lambda, make sure to update the set of
authorDouglas Gregor <dgregor@apple.com>
Thu, 25 Oct 2012 18:39:16 +0000 (18:39 +0000)
committerDouglas Gregor <dgregor@apple.com>
Thu, 25 Oct 2012 18:39:16 +0000 (18:39 +0000)
array-index starting values for the 'this' capture. Fixes
<rdar://problem/12426831>.

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

include/clang/Sema/ScopeInfo.h
test/CXX/expr/expr.prim/expr.prim.lambda/p14.cpp

index 0bfc6dcae6d88ae9abe9299cdbfff31989230113..c49dee7b41e0d347391a1a5f14497dbb81daaa53 100644 (file)
@@ -420,11 +420,7 @@ public:
   }
 
   void addThisCapture(bool isNested, SourceLocation Loc, QualType CaptureType,
-                      Expr *Cpy) {
-    Captures.push_back(Capture(Capture::ThisCapture, isNested, Loc, CaptureType,
-                               Cpy));
-    CXXThisCaptureIndex = Captures.size();
-  }
+                      Expr *Cpy);
 
   /// \brief Determine whether the C++ 'this' is captured.
   bool isCXXThisCaptured() const { return CXXThisCaptureIndex != 0; }
@@ -535,12 +531,13 @@ public:
   void finishedExplicitCaptures() {
     NumExplicitCaptures = Captures.size();
   }
-  
-  static bool classof(const FunctionScopeInfo *FSI) { 
+
+  static bool classof(const FunctionScopeInfo *FSI) {
     return FSI->Kind == SK_Lambda; 
   }
 };
 
+
 FunctionScopeInfo::WeakObjectProfileTy::WeakObjectProfileTy()
   : Base(0, false), Property(0) {}
 
@@ -558,6 +555,17 @@ void FunctionScopeInfo::recordUseOfWeak(const ExprT *E, bool IsRead) {
   Uses.push_back(WeakUseTy(E, IsRead));
 }
 
+inline void
+CapturingScopeInfo::addThisCapture(bool isNested, SourceLocation Loc,
+                                   QualType CaptureType, Expr *Cpy) {
+  Captures.push_back(Capture(Capture::ThisCapture, isNested, Loc, CaptureType,
+                             Cpy));
+  CXXThisCaptureIndex = Captures.size();
+
+  if (LambdaScopeInfo *LSI = dyn_cast<LambdaScopeInfo>(this))
+    LSI->ArrayIndexStarts.push_back(LSI->ArrayIndexVars.size());
+}
+
 } // end namespace sema
 } // end namespace clang
 
index 678fa4b964d493bac317508089a70c4bfbb7e235..6358215a55594ff04bb14eefbd4dbcbf5a64a679 100644 (file)
@@ -73,3 +73,18 @@ struct ExpectedThisLayout {
     static_assert(sizeof(x) == sizeof(ExpectedThisLayout), "Layout mismatch!");
   }
 };
+
+struct CaptureArrayAndThis {
+  int value;
+
+  void f() {
+    int array[3];
+    [=]() -> int {
+      int result = value;
+      for (unsigned i = 0; i < 3; ++i)
+        result += array[i];
+      return result;
+    }();
+  }
+};
+