[cfe-commits] r149458 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp

Douglas Gregor dgregor at apple.com
Tue Jan 31 16:09:55 PST 2012


Author: dgregor
Date: Tue Jan 31 18:09:55 2012
New Revision: 149458

URL: http://llvm.org/viewvc/llvm-project?rev=149458&view=rev
Log:
Diagnose attempts to explicitly capture a __block variable in a lambda.

Added:
    cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprCXX.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=149458&r1=149457&r2=149458&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jan 31 18:09:55 2012
@@ -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<

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=149458&r1=149457&r2=149458&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Tue Jan 31 18:09:55 2012
@@ -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();

Added: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp?rev=149458&view=auto
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp (added)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp Tue Jan 31 18:09:55 2012
@@ -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}}
+}





More information about the cfe-commits mailing list