https://github.com/a-tarasyuk updated 
https://github.com/llvm/llvm-project/pull/135573

>From 213a2377387d8fc032741e4f4ddf50f88fccaca5 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.taras...@outlook.com>
Date: Tue, 15 Apr 2025 01:07:05 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
 inside lambdas

---
 clang/docs/ReleaseNotes.rst               |  4 ++++
 clang/lib/Sema/SemaStmt.cpp               | 17 +++++++++++++++--
 clang/test/SemaCXX/warn-loop-analysis.cpp | 15 +++++++++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7ca04d9ebd44c..63f273dc6bbb9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -358,7 +358,11 @@ Improvements to Clang's diagnostics
 - Now correctly diagnose a tentative definition of an array with static
   storage duration in pedantic mode in C. (#GH50661)
 
+<<<<<<< Updated upstream
 - An error is now emitted when a ``musttail`` call is made to a function 
marked with the ``not_tail_called`` attribute. (#GH133509).
+=======
+- The ``-Wloop-analysis`` warning now handles variable modifications inside 
lambda expressions (#GH132038).
+>>>>>>> Stashed changes
 
 Improvements to Clang's time-trace
 ----------------------------------
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 39c2e157591df..9151661b0704f 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,22 @@ 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 (VarDecl *VD = dyn_cast<VarDecl>(Capture.getCapturedVar())) {
+            if (Decls.count(VD))
+              FoundDecl = true;
+          }
+        }
+      }
+    }
+
+    void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+      Visit(E->getCallee()->IgnoreParenImpCasts());
     }
 
     void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,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..4f7c70f65677b 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]() { }()) { }
+}

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to