llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) <details> <summary>Changes</summary> Fixes #<!-- -->132038 --- This PR extends `-Wloop-analysis` to handle variable modifications inside lambda expressions. --- Full diff: https://github.com/llvm/llvm-project/pull/135573.diff 3 Files Affected: - (modified) clang/docs/ReleaseNotes.rst (+2) - (modified) clang/lib/Sema/SemaStmt.cpp (+12-2) - (modified) clang/test/SemaCXX/warn-loop-analysis.cpp (+15) ``````````diff 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}} +} `````````` </details> https://github.com/llvm/llvm-project/pull/135573 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits