Diagnose attempts to explicitly capture a __block variable in a lambda.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149458 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index a67ccd9..8cf83fb 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4068,7 +4068,9 @@
"%0 cannot be captured because it does not have automatic storage duration">;
def err_implicit_this_capture : Error<
"'this' cannot be implicitly captured in this context">;
-
+def err_lambda_capture_block : Error<
+ "__block variable %0 cannot be captured in a lambda">;
+
def err_operator_arrow_circular : Error<
"circular pointer delegation detected">;
def err_pseudo_dtor_base_not_scalar : Error<
diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp
index 34827c4..a24063f 100644
--- a/lib/Sema/SemaExprCXX.cpp
+++ b/lib/Sema/SemaExprCXX.cpp
@@ -4933,9 +4933,14 @@
continue;
}
- // FIXME: This is completely wrong for nested captures and variables
- // with a non-trivial constructor.
- // FIXME: We should refuse to capture __block variables.
+ if (Var->hasAttr<BlocksAttr>()) {
+ Diag(C->Loc, diag::err_lambda_capture_block) << C->Id;
+ Diag(Var->getLocation(), diag::note_previous_decl) << C->Id;
+ continue;
+ }
+
+ // FIXME: If this is capture by copy, make sure that we can in fact copy
+ // the variable.
Captures.push_back(LambdaScopeInfo::Capture(Var, C->Kind == LCK_ByRef,
/*isNested*/false, 0));
CaptureMap[Var] = Captures.size();
diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp
new file mode 100644
index 0000000..faf686d
--- /dev/null
+++ b/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -std=c++11 -fblocks %s -verify
+
+void block_capture_errors() {
+ __block int var; // expected-note 2{{'var' declared here}}
+ (void)[var] { }; // expected-error{{__block variable 'var' cannot be captured in a lambda}} \
+ // expected-error{{lambda expressions are not supported yet}}
+
+ // FIXME: this should produce the same error as above
+ (void)[=] { var = 17; }; // expected-error{{reference to local variable 'var' declared in enclosed function 'block_capture_errors'}} \
+ // expected-error{{lambda expressions are not supported yet}}
+}