From 93be3f75ccdf0687b06036fbc2975f1c51547980 Mon Sep 17 00:00:00 2001 From: John McCall Date: Mon, 7 Feb 2011 18:37:40 +0000 Subject: [PATCH] When copy-capturing values for a nested capture, use a BlockDeclRefExpr. llvm-svn: 125021 --- clang/lib/CodeGen/CGBlocks.cpp | 14 +++++++++++--- clang/test/CodeGenCXX/blocks.cpp | 10 ++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 clang/test/CodeGenCXX/blocks.cpp diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp index bdbc9d3b5551..1a7abd242aa8 100644 --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/clang/lib/CodeGen/CGBlocks.cpp @@ -583,10 +583,18 @@ llvm::Value *CodeGenFunction::EmitBlockLiteral(const BlockExpr *blockExpr) { // Otherwise, fake up a POD copy into the block field. } else { - DeclRefExpr declRef(const_cast(variable), type, VK_LValue, - SourceLocation()); + // We use one of these or the other depending on whether the + // reference is nested. + DeclRefExpr notNested(const_cast(variable), type, VK_LValue, + SourceLocation()); + BlockDeclRefExpr nested(const_cast(variable), type, + VK_LValue, SourceLocation(), /*byref*/ false); + + Expr *declRef = + (ci->isNested() ? static_cast(&nested) : ¬Nested); + ImplicitCastExpr l2r(ImplicitCastExpr::OnStack, type, CK_LValueToRValue, - &declRef, VK_RValue); + declRef, VK_RValue); EmitAnyExprToMem(&l2r, blockField, /*volatile*/ false, /*init*/ true); } diff --git a/clang/test/CodeGenCXX/blocks.cpp b/clang/test/CodeGenCXX/blocks.cpp new file mode 100644 index 000000000000..568f9b1e4b5d --- /dev/null +++ b/clang/test/CodeGenCXX/blocks.cpp @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s + +namespace test0 { + // CHECK: define void @_ZN5test04testEi( + // CHECK: define internal void @__test_block_invoke_{{.*}}( + // CHECK: define internal void @__block_global_{{.*}}( + void test(int x) { + ^{ ^{ (void) x; }; }; + } +}