File tree 3 files changed +54
-2
lines changed
3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -385,6 +385,8 @@ Improvements to Clang's diagnostics
385
385
386
386
Fixes #GH131127
387
387
388
+ - The ``-Wloop-analysis `` warning now handles variable modifications inside lambda expressions (#GH132038).
389
+
388
390
Improvements to Clang's time-trace
389
391
----------------------------------
390
392
Original file line number Diff line number Diff line change @@ -2002,9 +2002,24 @@ namespace {
2002
2002
}
2003
2003
2004
2004
void VisitDeclRefExpr (DeclRefExpr *E) {
2005
- if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl ()))
2005
+ if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl ())) {
2006
2006
if (Decls.count (VD))
2007
2007
FoundDecl = true ;
2008
+ } else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getDecl ());
2009
+ MD && isLambdaCallOperator (MD)) {
2010
+ for (const auto &Capture : MD->getParent ()->captures ()) {
2011
+ if (!Capture.capturesVariable ())
2012
+ continue ;
2013
+
2014
+ LambdaCaptureKind CK = Capture.getCaptureKind ();
2015
+ if (CK != LCK_ByRef)
2016
+ continue ;
2017
+
2018
+ VarDecl *VD = dyn_cast<VarDecl>(Capture.getCapturedVar ());
2019
+ if (VD && Decls.count (VD))
2020
+ FoundDecl = true ;
2021
+ }
2022
+ }
2008
2023
}
2009
2024
2010
2025
void VisitPseudoObjectExpr (PseudoObjectExpr *POE) {
@@ -2021,7 +2036,7 @@ namespace {
2021
2036
2022
2037
bool FoundDeclInUse () { return FoundDecl; }
2023
2038
2024
- }; // end class DeclMatcher
2039
+ }; // end class DeclMatcher
2025
2040
2026
2041
void CheckForLoopConditionalStatement (Sema &S, Expr *Second,
2027
2042
Expr *Third, Stmt *Body) {
Original file line number Diff line number Diff line change @@ -299,3 +299,38 @@ void test10() {
299
299
for (auto [i, j, k] = arr; i < a; ++i) { }
300
300
for (auto [i, j, k] = arr; i < a; ++arr[0 ]) { }
301
301
};
302
+
303
+ namespace GH132038 {
304
+ extern void foo (int );
305
+ void test1 () {
306
+ int a = 0 ;
307
+ auto incr_a = [&a]() { ++a; };
308
+
309
+ for (int b = 10 ; a <= b; incr_a ())
310
+ foo (a);
311
+
312
+ for (int b = 10 ; a <= b;)
313
+ incr_a ();
314
+
315
+ for (int b = 10 ; a <= b; [&a]() { ++a; }()) { }
316
+ for (int b = 10 ; a <= b; [&a]() { }()) { }
317
+ }
318
+
319
+ void test2 () {
320
+ int a = 0 ;
321
+ auto incr_a = [a]() { };
322
+ auto incr_b = [](int b) { };
323
+
324
+ for (int b = 10 ; a <= b; incr_a ()) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}}
325
+ foo (a);
326
+
327
+ for (int b = 10 ; a <= b;) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}}
328
+ incr_a ();
329
+
330
+ for (int b = 10 ; a <= b; incr_b (b)) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}}
331
+ foo (a);
332
+
333
+ for (int b = 10 ; a <= b;) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}}
334
+ incr_b (b);
335
+ }
336
+ }
You can’t perform that action at this time.
0 commit comments