diff options
author | Douglas Gregor <dgregor@apple.com> | 2012-02-10 09:26:04 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2012-02-10 09:26:04 +0000 |
commit | 73d90928c0462daf0665fd7f8e44ca00d896540d (patch) | |
tree | bec61ee71da76e2e8058e67a5ca9eb15f357a24d | |
parent | ef7d78bd5be466c369b04af742ed8268244d4fe7 (diff) |
Add various tests for captures and the reaching scope of the lambda
expression. Implement C++11 [expr.prim.lambda]p12's requirement that
capturing a variable will odr-use it.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150237 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 6 | ||||
-rw-r--r-- | test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp | 15 | ||||
-rw-r--r-- | test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp | 16 | ||||
-rw-r--r-- | test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp | 48 |
4 files changed, 84 insertions, 1 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index ae5dc55825..fc7f32ec6d 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -9624,8 +9624,12 @@ static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI, // to be re-"exported" from the lambda expression itself. S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); + // C++ [expr.prim.labda]p12: + // An entity captured by a lambda-expression is odr-used (3.2) in + // the scope containing the lambda-expression. Expr *Ref = new (S.Context) DeclRefExpr(Var, Type.getNonReferenceType(), VK_LValue, Loc); + Var->setUsed(true); // When the field has array type, create index variables for each // dimension of the array. We use these index variables to subscript @@ -9999,7 +10003,7 @@ void Sema::MarkMemberReferenced(MemberExpr *E) { MarkExprReferenced(*this, E->getMemberLoc(), E->getMemberDecl(), E); } -/// \brief Perform marking for a reference to an aribitrary declaration. It +/// \brief Perform marking for a reference to an arbitrary declaration. It /// marks the declaration referenced, and performs odr-use checking for functions /// and variables. This method should not be used when building an normal /// expression which refers to a variable. diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp index b596bd5324..245e27042b 100644 --- a/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp +++ b/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp @@ -23,3 +23,18 @@ class X0 { (void)[&Variable] () {}; // expected-error {{use of undeclared identifier 'Variable'; did you mean 'variable'}} } }; + +void test_reaching_scope() { + int local; // expected-note{{declared here}} + static int local_static; // expected-note{{'local_static' declared here}} + (void)[=]() { + struct InnerLocal { + void member() { + (void)[local, // expected-error{{reference to local variable 'local' declared in enclosing function 'test_reaching_scope'}} + local_static]() { // expected-error{{'local_static' cannot be captured because it does not have automatic storage duration}} + return 0; + }; + } + }; + }; +} diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp new file mode 100644 index 0000000000..d265dd7573 --- /dev/null +++ b/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp @@ -0,0 +1,16 @@ +// RUN: %clang_cc1 -std=c++11 %s -verify + +void test_reaching_scope() { + int local; // expected-note{{declared here}} + static int local_static; + (void)[=]() { + struct InnerLocal { + void member() { + (void)[=]() { + return local + // expected-error{{reference to local variable 'local' declared in enclosing function 'test_reaching_scope'}} + local_static; + }; + } + }; + }; +} diff --git a/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp b/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp new file mode 100644 index 0000000000..5a696d76a5 --- /dev/null +++ b/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp @@ -0,0 +1,48 @@ +// RUN: %clang_cc1 -std=c++11 %s -Wunused -verify + +void odr_used() { + int i = 17; + [i]{}(); +} + +struct ReachingThis { + static void static_foo() { + (void)[this](){}; // expected-error{{'this' cannot be captured in this context}} + + struct Local { + int i; + + void bar() { + (void)[this](){}; + (void)[&](){i = 7; }; + } + }; + } + + void foo() { + (void)[this](){}; + + struct Local { + int i; + + static void static_bar() { + (void)[this](){}; // expected-error{{'this' cannot be captured in this context}} + (void)[&](){i = 7; }; // expected-error{{invalid use of nonstatic data member 'i'}} + } + }; + } +}; + +void immediately_enclosing(int i) { // expected-note{{'i' declared here}} + [i]() { + [i] {}(); + }(); + + [=]() { + [i] {}(); + }(); + + []() { // expected-note{{lambda expression begins here}} + [i] {}(); // expected-error{{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}} + }(); +} |