From: Richard Trieu Date: Sat, 5 Mar 2016 04:04:57 +0000 (+0000) Subject: Add null check to diagnostic path for lambda captures. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1585d71ce11df5799d519a6af26ae256dff633c1;p=clang Add null check to diagnostic path for lambda captures. 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@262765 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index a90fda8bfc..4071acd906 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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(CSI)->Lambda->getLocStart(), - diag::note_lambda_decl); + if (cast(CSI)->Lambda) + Diag(cast(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 diff --git a/test/SemaCXX/lambda-expressions.cpp b/test/SemaCXX/lambda-expressions.cpp index 08446a0ee4..17808cef36 100644 --- a/test/SemaCXX/lambda-expressions.cpp +++ b/test/SemaCXX/lambda-expressions.cpp @@ -487,3 +487,15 @@ void foo() { }; } } + +namespace nested_lambda { +template +class S {}; + +void foo() { + const int num = 18; // expected-note {{'num' declared here}} + auto outer = []() { + auto inner = [](S &X) {}; // expected-error {{variable 'num' cannot be implicitly captured in a lambda with no capture-default specified}} + }; +} +}