[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-05-19 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> > I question if -Wloop-analysis should be completely rewritten using dataflow 
> > analysis, rather than AST based matching.
> 
> should changes inside functions be handled in the following cases?
> 
> https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L23
> 
> https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L8
> 
> > rewritten using dataflow analysis
> 
> [`ExprMutationAnalyzer`](https://github.com/llvm/llvm-project/blob/main/clang/lib/Analysis/ExprMutationAnalyzer.cpp)?
> 
> /cc @AaronBallman @zyn0217 @cor3ntin

Oh that's a fun case. I think the behavior of that test makes sense if there 
was no definition for `by_ref` because we'd want to assume the non-`const` 
reference parameter is modified. But because we can see the definition for 
`by_ref`, we know the body is empty and so the variable is not modified, and so 
issuing the diagnostic there would be reasonable. WDYT?

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-05-17 Thread Oleksandr T. via cfe-commits

a-tarasyuk wrote:

> I question if -Wloop-analysis should be completely rewritten using dataflow 
> analysis, rather than AST based matching.

should changes inside functions be handled in the following cases?

https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L23

https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L8

> rewritten using dataflow analysis

[`ExprMutationAnalyzer`](https://github.com/llvm/llvm-project/blob/main/clang/lib/Analysis/ExprMutationAnalyzer.cpp)?

/cc @AaronBallman @zyn0217 @cor3ntin 




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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-21 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> @AaronBallman Should this issue ideally be resolved by CFG rather than the 
> current AST matcher fix?

I think so. CC @gribozavr @ymand @sgatev for additional opinions

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-21 Thread Oleksandr T. via cfe-commits

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 
Date: Thu, 17 Apr 2025 23:58:35 +0300
Subject: [PATCH 1/2] [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(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) {
@@ -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);
+}
+}

>From 00053790db55d3f26836e958f8624c0f4bf4fbb8 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Fri, 18 Apr 2025 18:28:16 +0300
Subject: [PATCH 2/2] add FIXME test case

---
 clang/test/SemaCXX/warn-loop-analysis.cpp | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/clang/test/SemaCXX/warn-loop-analysis.cpp 
b/clang/test/SemaCXX/warn-loop-analysis.cpp
index ec2894a46ee77..7773bef0cd238 100644
--- a/clang/test/SemaCXX/warn-loop-analysis.cpp
+++ b/clang/test/SemaCXX/warn-loop-analysis.cpp
@@ -318,8 +318,11 @@ void test1() {
 
 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);
@@ -332,5 +335,9 @@ void test2() {
 
   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);
 }
 }

___
cfe-commits mailing list
cfe-commits@lis

[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits

a-tarasyuk wrote:

@AaronBallman Should this issue ideally be resolved by CFG rather than the 
current AST matcher fix?

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits

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 
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(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) {
@@ -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);
+}
+}

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Aaron Ballman via cfe-commits

AaronBallman wrote:

> > @cor3ntin I question if `-Wloop-analysis` should be completely rewritten 
> > using dataflow analysis, rather than AST based matching.
> 
> That's an interesting idea, but I don't think that should stop us from 
> landing this improvement. Maybe we want to open an issue for that? 
> @AaronBallman

Yeah, that really should be reworked -- the diagnostic is already 
off-by-default, so users need to explicitly request it, which means making it 
an analysis-based diagnostic based on the CFG isn't going to cause compile time 
performance concerns IMO.

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits


@@ -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]() { }()) { }

a-tarasyuk wrote:

@zyn0217 I've added a test case with a FIXME. The original test already briefly 
mentions cases involving dereferencing...

https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L40-L41
 

> rewritten using dataflow analysis

I suppose it’s better to rely on dataflow analysis when handling more complex 
cases...

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits

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 
Date: Thu, 17 Apr 2025 23:58:35 +0300
Subject: [PATCH 1/2] [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(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) {
@@ -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);
+}
+}

>From 00053790db55d3f26836e958f8624c0f4bf4fbb8 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Fri, 18 Apr 2025 18:28:16 +0300
Subject: [PATCH 2/2] add FIXME test case

---
 clang/test/SemaCXX/warn-loop-analysis.cpp | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/clang/test/SemaCXX/warn-loop-analysis.cpp 
b/clang/test/SemaCXX/warn-loop-analysis.cpp
index ec2894a46ee77..7773bef0cd238 100644
--- a/clang/test/SemaCXX/warn-loop-analysis.cpp
+++ b/clang/test/SemaCXX/warn-loop-analysis.cpp
@@ -318,8 +318,11 @@ void test1() {
 
 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);
@@ -332,5 +335,9 @@ void test2() {
 
   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);
 }
 }

___
cfe-commits mailing list
cfe-commits@lis

[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits

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 
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(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) {
@@ -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);
+}
+}

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Younan Zhang via cfe-commits


@@ -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]() { }()) { }

zyn0217 wrote:

Or if that is something we can't handle at the moment, can you please add a 
FIXME?

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread via cfe-commits

cor3ntin wrote:

> @cor3ntin I question if `-Wloop-analysis` should be completely rewritten 
> using dataflow analysis, rather than AST based matching.

That's an interesting idea, but I don't think that should stop us from landing 
this improvement.
Maybe we want to open an issue for that?
@AaronBallman 

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits

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 
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(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) {
@@ -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);
+}
+}

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Younan Zhang via cfe-commits

https://github.com/zyn0217 commented:

@cor3ntin I question if `-Wloop-analysis` should be completely rewritten using 
dataflow analysis, rather than AST based matching.

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Younan Zhang via cfe-commits

https://github.com/zyn0217 edited 
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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Younan Zhang via cfe-commits


@@ -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]() { }()) { }

zyn0217 wrote:

Can you test this case?

```cpp
void test() {
  int a = 0;
  int *b = &a;
  auto increase = [b]() { ++*b; };
  for (int a = 10; a <= 20; increase()) {}
}
```


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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits


@@ -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]() { }()) { }

a-tarasyuk wrote:

@zyn0217, that's a good question. In this case, I followed a similar example 
with functions where the body isn't taken into account

https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L23

https://github.com/llvm/llvm-project/blob/db0f754c5af8e6c96770533520bf8b17fc0dc977/clang/test/SemaCXX/warn-loop-analysis.cpp#L8

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Younan Zhang via cfe-commits


@@ -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]() { }()) { }

zyn0217 wrote:

do you know why we don't complain about this case?

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-18 Thread Oleksandr T. via cfe-commits


@@ -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);

a-tarasyuk wrote:

@shafik, thanks for the review. I've updated tests

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-17 Thread via cfe-commits

https://github.com/cor3ntin approved this pull request.

LGTM, Thanks!

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-17 Thread Oleksandr T. via cfe-commits

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

>From b88f9364679db1d85b03f7552f5ed79949b0b775 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
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   | 26 +--
 clang/test/SemaCXX/warn-loop-analysis.cpp | 31 +++
 3 files changed, 57 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..3701cbf8e9a89 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,31 @@ 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 VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  Visit(E->getCallee());
+
+  for (auto *Arg : E->arguments())
+Visit(Arg->IgnoreParenImpCasts());
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2043,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..e725507b08af0 100644
--- a/clang/test/SemaCXX/warn-loop-analysis.cpp
+++ b/clang/test/SemaCXX/warn-loop-analysis.cpp
@@ -299,3 +299,34 @@ 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]() {  };
+
+  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; [a]() { }()) // expected-warning {{variables 'a' 
and 'b' used in loop condition not modified in loop body}}
+foo(a);
+}
+}

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-17 Thread Shafik Yaghmour via cfe-commits


@@ -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);

shafik wrote:

We normally wrap tests from bug reports in `namespace GH` where  is the 
bug report number.

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-17 Thread Shafik Yaghmour via cfe-commits


@@ -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);

shafik wrote:

Can we also get tests that show diagnostics for lambda cases as well.

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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-16 Thread Oleksandr T. via cfe-commits

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

>From 27e98b9fbfb808ab19cf688d48d688801d9647c1 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Thu, 17 Apr 2025 00:40:34 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
 inside lambdas

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

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 07ff1251fc1ad..e08cf382dcc9f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -379,6 +379,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..1a6d4c23c8570 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,28 @@ 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;
+
+  if (VarDecl *VD = dyn_cast(Capture.getCapturedVar())) {
+if (Decls.count(VD))
+  FoundDecl = true;
+  }
+}
+  }
+}
+
+void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  Visit(E->getCallee());
+
+  for (auto *Arg : E->arguments())
+Visit(Arg->IgnoreParenImpCasts());
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2040,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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-16 Thread Oleksandr T. via cfe-commits

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

>From 27e98b9fbfb808ab19cf688d48d688801d9647c1 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Thu, 17 Apr 2025 00:40:34 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
 inside lambdas

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

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 07ff1251fc1ad..e08cf382dcc9f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -379,6 +379,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..1a6d4c23c8570 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,28 @@ 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;
+
+  if (VarDecl *VD = dyn_cast(Capture.getCapturedVar())) {
+if (Decls.count(VD))
+  FoundDecl = true;
+  }
+}
+  }
+}
+
+void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  Visit(E->getCallee());
+
+  for (auto *Arg : E->arguments())
+Visit(Arg->IgnoreParenImpCasts());
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2040,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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-16 Thread Oleksandr T. via cfe-commits

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

>From 6a4bfc48c306928cc0fb516cd340e06fa83d54e8 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Wed, 16 Apr 2025 15:57:00 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
 inside lambdas

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

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0891fd058bb57..6ad62370c51ff 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -379,6 +379,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..b7571d474f134 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,30 @@ 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;
+
+  if (VarDecl *VD = dyn_cast(Capture.getCapturedVar())) {
+if (Decls.count(VD))
+  FoundDecl = true;
+  }
+}
+  }
+}
+
+void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  Expr *Callee = E->getCallee();
+  if (Callee) {
+Visit(E->getCallee());
+for (auto *Arg : E->arguments())
+  Visit(Arg->IgnoreParenImpCasts());
+  }
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2042,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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-16 Thread Oleksandr T. via cfe-commits

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

>From f3a021b0acbb028b647e5fab02ff053ad88b Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Wed, 16 Apr 2025 01:08:55 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
 inside lambdas

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

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 166f26921cb71..3e83041a9cd9c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -371,6 +371,8 @@ Improvements to Clang's diagnostics
 
 - 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).
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 39c2e157591df..b7571d474f134 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,30 @@ 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;
+
+  if (VarDecl *VD = dyn_cast(Capture.getCapturedVar())) {
+if (Decls.count(VD))
+  FoundDecl = true;
+  }
+}
+  }
+}
+
+void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  Expr *Callee = E->getCallee();
+  if (Callee) {
+Visit(E->getCallee());
+for (auto *Arg : E->arguments())
+  Visit(Arg->IgnoreParenImpCasts());
+  }
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2042,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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-15 Thread Oleksandr T. via cfe-commits

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

>From ab01bebfa99f635d80c234e19bf3aa73977ce149 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Wed, 16 Apr 2025 01:08:55 +0300
Subject: [PATCH] [Clang] enhance loop analysis to handle variable changes
 inside lambdas

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

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 166f26921cb71..3e83041a9cd9c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -371,6 +371,8 @@ Improvements to Clang's diagnostics
 
 - 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).
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 39c2e157591df..38bc6687ecb60 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,26 @@ 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;
+
+  if (VarDecl *VD = dyn_cast(Capture.getCapturedVar())) {
+if (Decls.count(VD))
+  FoundDecl = true;
+  }
+}
+  }
+}
+
+void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  if (Expr *Callee = E->getCallee())
+Visit(E->getCallee());
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2038,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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-14 Thread Oleksandr T. via cfe-commits

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

>From 30a626ee59fbdf96d055b13fe316ff2a8417c93b Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
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   |  2 ++
 clang/lib/Sema/SemaStmt.cpp   | 18 --
 clang/test/SemaCXX/warn-loop-analysis.cpp | 15 +++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7ca04d9ebd44c..6481c44f53e03 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -360,6 +360,8 @@ Improvements to Clang's diagnostics
 
 - 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).
+
 Improvements to Clang's time-trace
 --
 
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 39c2e157591df..6e0b307fc677a 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2002,9 +2002,23 @@ 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 (VarDecl *VD = dyn_cast(Capture.getCapturedVar())) {
+if (Decls.count(VD))
+  FoundDecl = true;
+  }
+}
+  }
+}
+
+void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) {
+  if (Expr *Callee = E->getCallee())
+Visit(E->getCallee());
 }
 
 void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
@@ -2021,7 +2035,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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-14 Thread Oleksandr T. via cfe-commits

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

>From 1bd4c4727596ad6255dd867fd3f2c9386b911bb5 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
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   |  2 ++
 clang/lib/Sema/SemaStmt.cpp   | 17 +++--
 clang/test/SemaCXX/warn-loop-analysis.cpp | 15 +++
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7ca04d9ebd44c..6481c44f53e03 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -360,6 +360,8 @@ Improvements to Clang's diagnostics
 
 - 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).
+
 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(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 (VarDecl *VD = dyn_cast(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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-14 Thread Oleksandr T. via cfe-commits

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 
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(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 (VarDecl *VD = dyn_cast(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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-14 Thread Oleksandr T. via cfe-commits

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

>From fd46c6b9af7193376f294a3adae47d579de8503b Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
Date: Mon, 14 Apr 2025 14:05:25 +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 fd9b9a80e9938..51663bfb7daa2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -356,6 +356,8 @@ Improvements to Clang's diagnostics
 - Now correctly diagnose a tentative definition of an array with static
   storage duration in pedantic mode in C. (#GH50661)
 
+- 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(E->getDecl()))
+  if (VarDecl *VD = dyn_cast(E->getDecl())) {
 if (Decls.count(VD))
   FoundDecl = true;
+  } else {
+if (CXXMethodDecl *MD = dyn_cast(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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-14 Thread Oleksandr T. via cfe-commits

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 
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(E->getDecl()))
+  if (VarDecl *VD = dyn_cast(E->getDecl())) {
 if (Decls.count(VD))
   FoundDecl = true;
+  } else {
+if (CXXMethodDecl *MD = dyn_cast(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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-13 Thread Oleksandr T. via cfe-commits

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

Fixes #132038 

--- 

This PR extends `-Wloop-analysis` to handle variable modifications inside 
lambda expressions.


>From 05125bafc45755624973feba0f4e6c2050fad36f Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk 
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(E->getDecl()))
+  if (VarDecl *VD = dyn_cast(E->getDecl())) {
 if (Decls.count(VD))
   FoundDecl = true;
+  } else {
+if (CXXMethodDecl *MD = dyn_cast(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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-13 Thread Oleksandr T. via cfe-commits

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 
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(E->getDecl()))
+  if (VarDecl *VD = dyn_cast(E->getDecl())) {
 if (Decls.count(VD))
   FoundDecl = true;
+  } else {
+if (CXXMethodDecl *MD = dyn_cast(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


[clang] [Clang] enhance loop analysis to handle variable changes inside lambdas (PR #135573)

2025-04-13 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)


Changes

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(E->getDecl()))
+  if (VarDecl *VD = dyn_cast(E->getDecl())) {
 if (Decls.count(VD))
   FoundDecl = true;
+  } else {
+if (CXXMethodDecl *MD = dyn_cast(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}}
+}

``




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