[cfe-commits] r150237 - in /cfe/trunk: lib/Sema/SemaExpr.cpp test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp

Douglas Gregor dgregor at apple.com
Fri Feb 10 01:26:04 PST 2012


Author: dgregor
Date: Fri Feb 10 03:26:04 2012
New Revision: 150237

URL: http://llvm.org/viewvc/llvm-project?rev=150237&view=rev
Log:
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.

Added:
    cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp   (with props)
    cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp   (with props)
Modified:
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=150237&r1=150236&r2=150237&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Fri Feb 10 03:26:04 2012
@@ -9624,8 +9624,12 @@
   // 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 @@
   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.

Modified: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp?rev=150237&r1=150236&r2=150237&view=diff
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp (original)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p10.cpp Fri Feb 10 03:26:04 2012
@@ -23,3 +23,18 @@
     (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;
+        };
+      }
+    };
+  };
+}

Added: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp?rev=150237&view=auto
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp (added)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp Fri Feb 10 03:26:04 2012
@@ -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;
+        };
+      }
+    };
+  };
+}

Propchange: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p11.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp?rev=150237&view=auto
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp (added)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp Fri Feb 10 03:26:04 2012
@@ -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}}
+  }();
+}

Propchange: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp
------------------------------------------------------------------------------
    svn:keywords = Id

Propchange: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p12.cpp
------------------------------------------------------------------------------
    svn:mime-type = text/plain





More information about the cfe-commits mailing list