From fe9b559f81535ade84d24c42569378f80df47847 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Wed, 1 Feb 2012 00:09:55 +0000 Subject: [PATCH] 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 --- include/clang/Basic/DiagnosticSemaKinds.td | 4 +++- lib/Sema/SemaExprCXX.cpp | 11 ++++++++--- test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp | 11 +++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index a67ccd9929..8cf83fba6a 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -4068,7 +4068,9 @@ def err_capture_non_automatic_variable : Error< "%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 34827c4e1c..a24063f860 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -4933,9 +4933,14 @@ void Sema::ActOnStartOfLambdaDefinition(LambdaIntroducer &Intro, 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()) { + 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 0000000000..faf686d111 --- /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}} +} -- 2.40.0