https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/135573
>From 05125bafc45755624973feba0f4e6c2050fad36f Mon Sep 17 00:00:00 2001 From: Oleksandr Tarasiuk <oleksandr.taras...@outlook.com> Date: Mon, 14 Apr 2025 01:37:05 +0300 Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes inside lambdas --- clang/docs/ReleaseNotes.rst | 2 ++ clang/lib/Sema/SemaStmt.cpp | 14 ++++++++++++-- clang/test/SemaCXX/warn-loop-analysis.cpp | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 5217e04b5e83f..77d15d798097b 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -319,6 +319,8 @@ Improvements to Clang's diagnostics - ``-Wc++98-compat`` no longer diagnoses use of ``__auto_type`` or ``decltype(auto)`` as though it was the extension for ``auto``. (#GH47900) +- 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 e1b9ccc693bd5..a1718d5a549e4 100644 --- a/clang/lib/Sema/SemaStmt.cpp +++ b/clang/lib/Sema/SemaStmt.cpp @@ -1995,9 +1995,19 @@ namespace { } void VisitDeclRefExpr(DeclRefExpr *E) { - if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) + if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) { if (Decls.count(VD)) FoundDecl = true; + } else { + if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getDecl())) + if (isLambdaCallOperator(MD)) + Visit(MD->getBody()); + } + } + + void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { + if (Expr *Callee = E->getCallee()) + Visit(Callee); } void VisitPseudoObjectExpr(PseudoObjectExpr *POE) { @@ -2014,7 +2024,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..cfed3c23a2e9e 100644 --- a/clang/test/SemaCXX/warn-loop-analysis.cpp +++ b/clang/test/SemaCXX/warn-loop-analysis.cpp @@ -299,3 +299,18 @@ void test10() { for (auto[i, j, k] = arr; i < a; ++i) { } for (auto[i, j, k] = arr; i < a; ++arr[0]) { } }; + +extern void foo(int); +void test11() { + 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]() { }()) { } // expected-warning {{variables 'a' and 'b' used in loop condition not modified in loop body}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits