]> granicus.if.org Git - clang/commitdiff
Fix code generation of variables reference expressions when mixing
authorDouglas Gregor <dgregor@apple.com>
Wed, 16 May 2012 16:50:20 +0000 (16:50 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 16 May 2012 16:50:20 +0000 (16:50 +0000)
blocks and lambdas, based heavily on a patch from Meador Inge. Fixes
PR12746 / <rdar://problem/11465120>.

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

lib/CodeGen/CGBlocks.cpp
lib/Sema/SemaExpr.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/blocks.mm

index dad8e7ea7cf1846aa236fa9f3946c12de3ef0fc1..47c6c48018e20e49211eb45fac4f5f62fb240895 100644 (file)
@@ -698,7 +698,7 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const CGBlockInfo &blockInfo) {
     // Compute the address of the thing we're going to move into the
     // block literal.
     llvm::Value *src;
-    if (ci->isNested()) {
+    if (BlockInfo && ci->isNested()) {
       // We need to use the capture from the enclosing block.
       const CGBlockInfo::Capture &enclosingCapture =
         BlockInfo->getCapture(variable);
index e81787d5a4bb5aceba44e2b96cd76f9112a5d9c5..440382bb5a353611c44cc39d3e87a902940866c1 100644 (file)
@@ -10069,7 +10069,7 @@ static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI,
   // C++ [expr.prim.labda]p12:
   //   An entity captured by a lambda-expression is odr-used (3.2) in
   //   the scope containing the lambda-expression.
-  Expr *Ref = new (S.Context) DeclRefExpr(Var, false, DeclRefType,
+  Expr *Ref = new (S.Context) DeclRefExpr(Var, true, DeclRefType,
                                           VK_LValue, Loc);
   Var->setReferenced(true);
   Var->setUsed(true);
index 0c3fdb2d80eb39cc020661b0b71cc560d2d7765d..941443a0d6846c34d2089f218957f13167d0a335 100644 (file)
@@ -86,3 +86,25 @@ namespace overloading {
     int &ir = accept_lambda_conv([](int x) { return x + 1; });
   }
 }
+
+namespace PR12746 {
+  bool f1(int *x) {
+    bool (^outer)() = ^ {
+      auto inner = [&]() -> bool {
+       return x == 0;
+      };
+      return inner();
+    };
+    return outer();
+  }
+
+  bool f2(int *x) {
+    auto outer = [&]() -> bool {
+      bool (^inner)() = ^ {
+       return x == 0;
+      };
+      return inner();
+    };
+    return outer();
+  }
+}