acomminos updated this revision to Diff 153968.
acomminos retitled this revision from "[Sema] Add fixit for
-Wno-unused-lambda-capture" to "[Sema] Add fixit for unused lambda captures".
acomminos edited the summary of this revision.
acomminos changed the visibility from "Custom Policy" to "Public (No Login
Required)".
acomminos added a subscriber: alexshap.
acomminos added a comment.
Herald added a subscriber: cfe-commits.
Added tests, add logic for removing a comma forward for beginning edge case.
Repository:
rC Clang
https://reviews.llvm.org/D48845
Files:
include/clang/Sema/Sema.h
lib/Sema/SemaLambda.cpp
test/FixIt/fixit-unused-lambda-capture.cpp
Index: test/FixIt/fixit-unused-lambda-capture.cpp
===================================================================
--- /dev/null
+++ test/FixIt/fixit-unused-lambda-capture.cpp
@@ -0,0 +1,31 @@
+// RUN: cp %s %t
+// RUN: %clang_cc1 -x c++ -fsyntax-only -Wunused-lambda-capture -std=c++1z -fixit %t
+// RUN: grep -v CHECK %t | FileCheck %s
+
+void test() {
+ int i = 0;
+ int j = 0;
+ int k = 0;
+ [i,j] { return i; };
+ // CHECK: [i] { return i; };
+ [i,j] { return j; };
+ // CHECK: [j] { return j; };
+ [i,j,k] {};
+ // CHECK: [] {};
+ [i,j,k] { return i + j; };
+ // CHECK: [i,j] { return i + j; };
+ [i,j,k] { return j + k; };
+ // CHECK: [j,k] { return j + k; };
+ [i,j,k] { return i + k; };
+ // CHECK: [i,k] { return i + k; };
+ [i,j,k] { return i + j + k; };
+ // CHECK: [i,j,k] { return i + j + k; };
+ [&,i] { return k; };
+ // CHECK: [&] { return k; };
+ [=,&i] { return k; };
+ // CHECK: [=] { return k; };
+ [=,&i,&j] { return j; };
+ // CHECK: [=,&j] { return j; };
+ [=,&i,&j] { return i; };
+ // CHECK: [=,&i] { return i; };
+}
Index: lib/Sema/SemaLambda.cpp
===================================================================
--- lib/Sema/SemaLambda.cpp
+++ lib/Sema/SemaLambda.cpp
@@ -1478,7 +1478,8 @@
return false;
}
-void Sema::DiagnoseUnusedLambdaCapture(const Capture &From) {
+void Sema::DiagnoseUnusedLambdaCapture(const SourceRange CaptureRange,
+ const Capture &From) {
if (CaptureHasSideEffects(From))
return;
@@ -1491,6 +1492,7 @@
else
diag << From.getVariable();
diag << From.isNonODRUsed();
+ diag << FixItHint::CreateRemoval(CaptureRange);
}
ExprResult Sema::BuildLambdaExpr(SourceLocation StartLoc, SourceLocation EndLoc,
@@ -1532,19 +1534,40 @@
// Translate captures.
auto CurField = Class->field_begin();
+ // True if the current capture has an initializer or default before it.
+ bool CurHasPreviousInitializer = CaptureDefault != LCD_None;
+ SourceLocation PrevCaptureLoc = CurHasPreviousInitializer ?
+ CaptureDefaultLoc : IntroducerRange.getBegin();
+
for (unsigned I = 0, N = LSI->Captures.size(); I != N; ++I, ++CurField) {
const Capture &From = LSI->Captures[I];
assert(!From.isBlockCapture() && "Cannot capture __block variables");
bool IsImplicit = I >= LSI->NumExplicitCaptures;
// Warn about unused explicit captures.
+ bool IsCaptureUsed = true;
if (!CurContext->isDependentContext() && !IsImplicit && !From.isODRUsed()) {
// Initialized captures that are non-ODR used may not be eliminated.
bool NonODRUsedInitCapture =
IsGenericLambda && From.isNonODRUsed() && From.getInitExpr();
- if (!NonODRUsedInitCapture)
- DiagnoseUnusedLambdaCapture(From);
+ if (!NonODRUsedInitCapture) {
+ // Delete either the preceding or next comma in the explicit capture
+ // list, depending on whether or not elements follow.
+ SourceRange FixItRange;
+ bool IsLast = I + 1 == LSI->NumExplicitCaptures;
+ if (!CurHasPreviousInitializer && !IsLast) {
+ FixItRange = SourceRange(From.getLocation(),
+ getLocForEndOfToken(From.getLocation()));
+ } else {
+ FixItRange = SourceRange(getLocForEndOfToken(PrevCaptureLoc),
+ From.getLocation());
+ }
+
+ DiagnoseUnusedLambdaCapture(FixItRange, From);
+ IsCaptureUsed = false;
+ }
}
+ CurHasPreviousInitializer |= IsCaptureUsed;
// Handle 'this' capture.
if (From.isThisCapture()) {
@@ -1574,6 +1597,8 @@
Init = InitResult.get();
}
CaptureInits.push_back(Init);
+
+ PrevCaptureLoc = From.getLocation();
}
// C++11 [expr.prim.lambda]p6:
Index: include/clang/Sema/Sema.h
===================================================================
--- include/clang/Sema/Sema.h
+++ include/clang/Sema/Sema.h
@@ -5604,7 +5604,8 @@
bool CaptureHasSideEffects(const sema::Capture &From);
/// Diagnose if an explicit lambda capture is unused.
- void DiagnoseUnusedLambdaCapture(const sema::Capture &From);
+ void DiagnoseUnusedLambdaCapture(const SourceRange CaptureRange,
+ const sema::Capture &From);
/// Complete a lambda-expression having processed and attached the
/// lambda body.
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits