diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 36b528d9e20f8..3938b898b7838 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -405,6 +405,7 @@ Improvements to Clang's diagnostics they're only triggered if the authors are already making the choice to use ``preferred_type`` attribute. +- The ``-Wloop-analysis`` warning now handles variable modifications inside lambda expressions (#GH132038). Improvements to Clang's time-trace ---------------------------------- diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp index 87a400e8a6291..54d8e487a8ae4 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -2000,9 +2000,24 @@ namespace { } void VisitDeclRefExpr(DeclRefExpr *E) { - if (VarDecl *VD = dyn_cast(E->getDecl())) + if (VarDecl *VD = dyn_cast(E->getDecl())) { if (Decls.count(VD)) FoundDecl = true; + } else if (CXXMethodDecl *MD = dyn_cast(E->getDecl()); + MD && isLambdaCallOperator(MD)) { + for (const auto &Capture : MD->getParent()->captures()) { + if (!Capture.capturesVariable()) + continue; + + LambdaCaptureKind CK = Capture.getCaptureKind(); + if (CK != LCK_ByRef) + continue; + + VarDecl *VD = dyn_cast(Capture.getCapturedVar()); + if (VD && Decls.count(VD)) + FoundDecl = true; + } + } } void VisitPseudoObjectExpr(PseudoObjectExpr *POE) { @@ -2019,7 +2034,7 @@ namespace { bool FoundDeclInUse() { return FoundDecl; } - }; // end class DeclMatcher + }; // end class DeclMatcher void CheckForLoopConditionalStatement(Sema &S, Expr *Second, Expr *Third, Stmt *Body) { diff --git a/clang/test/SemaCXX/warn-loop-analysis.cpp b/clang/test/SemaCXX/warn-loop-analysis.cpp index 324dd386292ac..7773bef0cd238 100644 --- a/clang/test/SemaCXX/warn-loop-analysis.cpp +++ b/clang/test/SemaCXX/warn-loop-analysis.cpp @@ -299,3 +299,45 @@ void test10() { for (auto[i, j, k] = arr; i < a; ++i) { } for (auto[i, j, k] = arr; i < a; ++arr[0]) { } }; + +namespace GH132038 { +extern void foo(int); +void test1() { + int a = 0; + auto incr_a = [&a]() { ++a; }; + + for (int b = 10; a <= b; incr_a()) + foo(a); + + for (int b = 10; a <= b;) + incr_a(); + + for (int b = 10; a <= b; [&a]() { ++a; }()) { } + for (int b = 10; a <= b; [&a]() { }()) { } +} + +void test2() { + int a = 0; + int *c = &a; + + auto incr_a = [a]() { }; + auto incr_b = [](int b) { }; + auto incr_c = [c]() { ++*c; }; + + for (int b = 10; a <= b; incr_a()) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}} + foo(a); + + for (int b = 10; a <= b;) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}} + incr_a(); + + for (int b = 10; a <= b; incr_b(b)) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}} + foo(a); + + for (int b = 10; a <= b;) // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}} + incr_b(b); + + // FIXME: handle modification of loop control variable inside lambda body + for (a = 10; a <= 20; incr_c()) // expected-warning {{variable 'a' used in loop condition not modified in loop body}} + foo(a); +} +}