]> granicus.if.org Git - clang/commitdiff
Diagnose attempts to explicitly capture a __block variable in a lambda.
authorDouglas Gregor <dgregor@apple.com>
Wed, 1 Feb 2012 00:09:55 +0000 (00:09 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 1 Feb 2012 00:09:55 +0000 (00:09 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@149458 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExprCXX.cpp
test/CXX/expr/expr.prim/expr.prim.lambda/blocks.cpp [new file with mode: 0644]

index a67ccd9929da434319fe2ad9be71796a22820361..8cf83fba6af94fd4a4491a39b88e095b4e921318 100644 (file)
@@ -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<
index 34827c4e1ce42da698253fffc70b9b87f67d22c7..a24063f86080f53ba72d8def2923e786835f97e8 100644 (file)
@@ -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<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 (file)
index 0000000..faf686d
--- /dev/null
@@ -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}}
+}