From: Malcolm Parsons Date: Mon, 11 Dec 2017 18:00:36 +0000 (+0000) Subject: [Sema] Fix crash in unused-lambda-capture warning for VLAs X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=84bf555dbe0c7836c8f67eea29a7cc9390b897bf;p=clang [Sema] Fix crash in unused-lambda-capture warning for VLAs Summary: Clang was crashing when diagnosing an unused-lambda-capture for a VLA because From.getVariable() is null for the capture of a VLA bound. Warning about the VLA bound capture is not helpful, so only warn for the VLA itself. Fixes: PR35555 Reviewers: aaron.ballman, dim, rsmith Reviewed By: aaron.ballman, dim Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D41016 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@320396 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Sema/ScopeInfo.h b/include/clang/Sema/ScopeInfo.h index 6bb667deb2..c707a3e8ef 100644 --- a/include/clang/Sema/ScopeInfo.h +++ b/include/clang/Sema/ScopeInfo.h @@ -560,6 +560,7 @@ public: void markUsed(bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) = true; } VarDecl *getVariable() const { + assert(isVariableCapture()); return VarAndNestedAndThis.getPointer(); } diff --git a/lib/Sema/SemaLambda.cpp b/lib/Sema/SemaLambda.cpp index 5254fea28f..cbfc330ca6 100644 --- a/lib/Sema/SemaLambda.cpp +++ b/lib/Sema/SemaLambda.cpp @@ -1481,6 +1481,9 @@ void Sema::DiagnoseUnusedLambdaCapture(const LambdaScopeInfo::Capture &From) { if (CaptureHasSideEffects(From)) return; + if (From.isVLATypeCapture()) + return; + auto diag = Diag(From.getLocation(), diag::warn_unused_lambda_capture); if (From.isThisCapture()) diag << "'this'"; diff --git a/test/SemaCXX/warn-unused-lambda-capture.cpp b/test/SemaCXX/warn-unused-lambda-capture.cpp index 6ad8e26604..52ec390b0b 100644 --- a/test/SemaCXX/warn-unused-lambda-capture.cpp +++ b/test/SemaCXX/warn-unused-lambda-capture.cpp @@ -191,3 +191,12 @@ void test_templated() { void test_use_template() { test_templated(); // expected-note{{in instantiation of function template specialization 'test_templated' requested here}} } + +namespace pr35555 { +int a; +void b() { + int c[a]; + auto vla_used = [&c] { return c[0]; }; + auto vla_unused = [&c] {}; // expected-warning{{lambda capture 'c' is not used}} +} +} // namespace pr35555