Add null check to diagnostic path for lambda captures.
authorRichard Trieu <rtrieu@google.com>
Sat, 5 Mar 2016 04:04:57 +0000 (04:04 +0000)
committerRichard Trieu <rtrieu@google.com>
Sat, 5 Mar 2016 04:04:57 +0000 (04:04 +0000)
Previously, the failed capture of a variable in nested lambdas may crash when
the lambda pointer is null.  Only give the note if a location can be retreived
from the lambda pointer.

llvm-svn: 262765

clang/lib/Sema/SemaExpr.cpp
clang/test/SemaCXX/lambda-expressions.cpp

index a90fda8..4071acd 100644 (file)
@@ -13509,8 +13509,9 @@ bool Sema::tryCaptureVariable(
         Diag(ExprLoc, diag::err_lambda_impcap) << Var->getDeclName();
         Diag(Var->getLocation(), diag::note_previous_decl) 
           << Var->getDeclName();
-        Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(),
-             diag::note_lambda_decl);
+        if (cast<LambdaScopeInfo>(CSI)->Lambda)
+          Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(),
+               diag::note_lambda_decl);
         // FIXME: If we error out because an outer lambda can not implicitly
         // capture a variable that an inner lambda explicitly captures, we
         // should have the inner lambda do the explicit capture - because
index 08446a0..17808ce 100644 (file)
@@ -487,3 +487,15 @@ void foo() {
   };
 }
 }
+
+namespace nested_lambda {
+template <int N>
+class S {};
+
+void foo() {
+  const int num = 18;  // expected-note {{'num' declared here}}
+  auto outer = []() {
+    auto inner = [](S<num> &X) {};  // expected-error {{variable 'num' cannot be implicitly captured in a lambda with no capture-default specified}}
+  };
+}
+}