[cfe-commits] r150163 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td include/clang/Sema/Sema.h lib/Sema/SemaExpr.cpp lib/Sema/SemaLambda.cpp test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp

Douglas Gregor dgregor at apple.com
Thu Feb 9 00:14:44 PST 2012


Author: dgregor
Date: Thu Feb  9 02:14:43 2012
New Revision: 150163

URL: http://llvm.org/viewvc/llvm-project?rev=150163&view=rev
Log:
Implement C++ [expr.prim.lambda]p2, which bans lambda expressions in
unevaluated operands. Be certain that we're marking everything
referenced within a capture initializer as odr-used.

Added:
    cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp   (with props)
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/lib/Sema/SemaLambda.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=150163&r1=150162&r2=150163&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Thu Feb  9 02:14:43 2012
@@ -4125,7 +4125,8 @@
   "variable %0 cannot be implicitly captured in a lambda with no "
   "capture-default specified">;
 def note_lambda_decl : Note<"lambda expression begins here">;
-
+def err_lambda_unevaluated_operand : Error<
+  "lambda expression in an unevaluated operand">;
 
 def err_operator_arrow_circular : Error<
   "circular pointer delegation detected">;

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=150163&r1=150162&r2=150163&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Thu Feb  9 02:14:43 2012
@@ -102,6 +102,7 @@
   class InitializedEntity;
   class IntegerLiteral;
   class LabelStmt;
+  class LambdaExpr;
   class LangOptions;
   class LocalInstantiationScope;
   class LookupResult;
@@ -560,6 +561,10 @@
 
     llvm::SmallPtrSet<Expr*, 8> SavedMaybeODRUseExprs;
 
+    /// \brief The lambdas that are present within this context, if it
+    /// is indeed an unevaluated context.
+    llvm::SmallVector<LambdaExpr *, 2> Lambdas;
+
     ExpressionEvaluationContextRecord(ExpressionEvaluationContext Context,
                                       unsigned NumCleanupObjects,
                                       bool ParentNeedsCleanups)

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=150163&r1=150162&r2=150163&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Thu Feb  9 02:14:43 2012
@@ -9282,6 +9282,12 @@
       return BaseTransform::TransformUnaryOperator(E);
     }
 
+    /// \brief Transform the capture expressions in the lambda
+    /// expression.
+    ExprResult TransformLambdaExpr(LambdaExpr *E) {
+      // Lambdas never need to be transformed.
+      return E;
+    }
   };
 }
 
@@ -9307,6 +9313,29 @@
 void Sema::PopExpressionEvaluationContext() {
   ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back();
 
+  if (!Rec.Lambdas.empty()) {
+    if (Rec.Context == Unevaluated) {
+      // C++11 [expr.prim.lambda]p2:
+      //   A lambda-expression shall not appear in an unevaluated operand
+      //   (Clause 5).
+      for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I)
+        Diag(Rec.Lambdas[I]->getLocStart(), 
+             diag::err_lambda_unevaluated_operand);
+    } else {
+      // Mark the capture expressions odr-used. This was deferred
+      // during lambda expression creation.
+      for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) {
+        LambdaExpr *Lambda = Rec.Lambdas[I];
+        for (LambdaExpr::capture_init_iterator 
+                  C = Lambda->capture_init_begin(),
+               CEnd = Lambda->capture_init_end();
+             C != CEnd; ++C) {
+          MarkDeclarationsReferencedInExpr(*C);
+        }
+      }
+    }
+  }
+
   // When are coming out of an unevaluated context, clear out any
   // temporaries that we may have created as part of the evaluation of
   // the expression in that context: they aren't relevant because they
@@ -9318,6 +9347,8 @@
     CleanupVarDeclMarking();
     std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs);
 
+    if (Rec.Context == Unevaluated) {
+    }
   // Otherwise, merge the contexts together.
   } else {
     ExprNeedsCleanups |= Rec.ParentNeedsCleanups;
@@ -9589,8 +9620,9 @@
   //
   // FIXME: Introduce an initialization entity for lambda captures.
       
-  // Introduce a new evaluation context for the initialization, so that
-  // temporaries introduced as part of the capture
+  // Introduce a new evaluation context for the initialization, so
+  // that temporaries introduced as part of the capture are retained
+  // to be re-"exported" from the lambda expression itself.
   S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated);
 
   Expr *Ref = new (S.Context) DeclRefExpr(Var, Type.getNonReferenceType(),

Modified: cfe/trunk/lib/Sema/SemaLambda.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaLambda.cpp?rev=150163&r1=150162&r2=150163&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaLambda.cpp (original)
+++ cfe/trunk/lib/Sema/SemaLambda.cpp Thu Feb  9 02:14:43 2012
@@ -357,9 +357,28 @@
   if (LambdaExprNeedsCleanups)
     ExprNeedsCleanups = true;
 
-  Expr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, 
-                                    CaptureDefault, Captures, ExplicitParams, 
-                                    CaptureInits, Body->getLocEnd());
+  LambdaExpr *Lambda = LambdaExpr::Create(Context, Class, IntroducerRange, 
+                                          CaptureDefault, Captures, 
+                                          ExplicitParams, CaptureInits, 
+                                          Body->getLocEnd());
+
+  // C++11 [expr.prim.lambda]p2:
+  //   A lambda-expression shall not appear in an unevaluated operand
+  //   (Clause 5).
+  switch (ExprEvalContexts.back().Context) {
+  case Unevaluated:
+    // We don't actually diagnose this case immediately, because we
+    // could be within a context where we might find out later that
+    // the expression is potentially evaluated (e.g., for typeid).
+    ExprEvalContexts.back().Lambdas.push_back(Lambda);
+    break;
+
+  case ConstantEvaluated:
+  case PotentiallyEvaluated:
+  case PotentiallyEvaluatedIfUsed:
+    break;
+  }
+
   Diag(StartLoc, diag::err_lambda_unsupported);
 
   return MaybeBindToTemporary(Lambda);

Added: cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp?rev=150163&view=auto
==============================================================================
--- cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp (added)
+++ cfe/trunk/test/CXX/expr/expr.prim/expr.prim.lambda/p2.cpp Thu Feb  9 02:14:43 2012
@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
+
+// prvalue
+void prvalue() {
+  auto&& x = []()->void { }; // expected-error{{lambda expressions are not supported yet}}
+  auto& y = []()->void { }; // expected-error{{cannot bind to a temporary of type}} \
+  // expected-error{{lambda expressions are not supported yet}}
+}
+
+namespace std {
+  class type_info;
+}
+
+struct P {
+  virtual ~P();
+};
+
+void unevaluated_operand(P &p, int i) {
+  int i2 = sizeof([]()->void{}()); // expected-error{{lambda expression in an unevaluated operand}} \
+  // expected-error{{lambda expressions are not supported yet}}
+  const std::type_info &ti1 = typeid([&]() -> P& { return p; }()); // expected-error{{lambda expressions are not supported yet}}
+  const std::type_info &ti2 = typeid([&]() -> int { return i; }());  // expected-error{{lambda expression in an unevaluated operand}} \
+  // expected-error{{lambda expressions are not supported yet}}
+}
+
+template<typename T>
+struct Boom {
+  Boom(const Boom&) { 
+    T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} \
+    // expected-error{{cannot initialize a variable of type 'float *' with an rvalue of type 'int'}} \
+    // expected-error{{cannot initialize a variable of type 'double *' with an rvalue of type 'int'}}
+  }
+  void tickle() const;
+};
+
+void odr_used(P &p, Boom<int> boom_int, Boom<float> boom_float,
+              Boom<double> boom_double) {
+  const std::type_info &ti1
+    = typeid([=,&p]() -> P& { boom_int.tickle(); return p; }()); // expected-error{{lambda expressions are not supported yet}} \
+  // expected-note{{in instantiation of member function 'Boom<int>::Boom' requested here}}
+  const std::type_info &ti2
+    = typeid([=]() -> int { boom_float.tickle(); return 0; }()); // expected-error{{lambda expression in an unevaluated operand}} \
+  // expected-error{{lambda expressions are not supported yet}} \
+  // expected-note{{in instantiation of member function 'Boom<float>::Boom' requested here}}
+
+  auto foo = [=]() -> int { boom_double.tickle(); return 0; }; // expected-error{{lambda expressions are not supported yet}} \
+  // expected-note{{in instantiation of member function 'Boom<double>::Boom' requested here}}
+}

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

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

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





More information about the cfe-commits mailing list