]> granicus.if.org Git - clang/commitdiff
[Sema] Fix crash in unused-lambda-capture warning for VLAs
authorMalcolm Parsons <malcolm.parsons@gmail.com>
Mon, 11 Dec 2017 18:00:36 +0000 (18:00 +0000)
committerMalcolm Parsons <malcolm.parsons@gmail.com>
Mon, 11 Dec 2017 18:00:36 +0000 (18:00 +0000)
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

include/clang/Sema/ScopeInfo.h
lib/Sema/SemaLambda.cpp
test/SemaCXX/warn-unused-lambda-capture.cpp

index 6bb667deb2297c032e316ac35ca03c56a299a313..c707a3e8ef0745e7eddf23e16a7d70ce8ed50893 100644 (file)
@@ -560,6 +560,7 @@ public:
     void markUsed(bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) = true; }
 
     VarDecl *getVariable() const {
+      assert(isVariableCapture());
       return VarAndNestedAndThis.getPointer();
     }
     
index 5254fea28f9e520ddec257402d66a2bd9ed95f34..cbfc330ca60b2da4d4fbe32b1bc67e66e5758205 100644 (file)
@@ -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'";
index 6ad8e26604a453fac0943f3c5a5474569d5dddb5..52ec390b0bba65e21dddb3723643d756b7f97151 100644 (file)
@@ -191,3 +191,12 @@ void test_templated() {
 void test_use_template() {
   test_templated<int>(); // expected-note{{in instantiation of function template specialization 'test_templated<int>' 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