Skip to content

Commit

Permalink
[Sema] Fix crash in unused-lambda-capture warning for VLAs
Browse files Browse the repository at this point in the history
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

llvm-svn: 320396
  • Loading branch information
pepsiman committed Dec 11, 2017
1 parent dbe6c45 commit d900a0c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Sema/ScopeInfo.h
Expand Up @@ -560,6 +560,7 @@ class CapturingScopeInfo : public FunctionScopeInfo {
void markUsed(bool IsODRUse) { (IsODRUse ? ODRUsed : NonODRUsed) = true; }

VarDecl *getVariable() const {
assert(isVariableCapture());
return VarAndNestedAndThis.getPointer();
}

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaLambda.cpp
Expand Up @@ -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'";
Expand Down
9 changes: 9 additions & 0 deletions clang/test/SemaCXX/warn-unused-lambda-capture.cpp
Expand Up @@ -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

0 comments on commit d900a0c

Please sign in to comment.