[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 17 14:29:34 PDT 2025
https://github.com/a-tarasyuk updated https://github.com/llvm/llvm-project/pull/135573
>From 93c8fc7faba6ab9537039b8745e62f5d79785cd0 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Thu, 17 Apr 2025 23:58:35 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
inside lambdas
---
clang/docs/ReleaseNotes.rst | 2 ++
clang/lib/Sema/SemaStmt.cpp | 19 ++++++++++--
clang/test/SemaCXX/warn-loop-analysis.cpp | 35 +++++++++++++++++++++++
3 files changed, 54 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c75d83a6d1a7a..8a330c010a73d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -385,6 +385,8 @@ Improvements to Clang's diagnostics
Fixes #GH131127
+- 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 39c2e157591df..2bb46dd94cca2 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,24 @@ 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());
+ 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<VarDecl>(Capture.getCapturedVar());
+ if (VD && Decls.count(VD))
+ FoundDecl = true;
+ }
+ }
}
void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2036,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..ec2894a46ee77 100644
--- a/clang/test/SemaCXX/warn-loop-analysis.cpp
+++ b/clang/test/SemaCXX/warn-loop-analysis.cpp
@@ -299,3 +299,38 @@ 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;
+ auto incr_a = [a]() { };
+ auto incr_b = [](int b) { };
+
+ 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);
+}
+}
More information about the cfe-commits
mailing list