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

2026-02-13 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder 
`sanitizer-x86_64-linux-fast` running on `sanitizer-buildbot3` while building 
`clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/169/builds/19934


Here is the relevant piece of the build log for the reference

```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using lld-link: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using ld64.lld: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using wasm-ld: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using ld.lld: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld.lld
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using lld-link: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/lld-link
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using ld64.lld: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/ld64.lld
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/llvm/config.py:561:
 note: using wasm-ld: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/wasm-ld
llvm-lit: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/utils/lit/lit/main.py:74:
 note: The test suite configuration requested an individual test timeout of 0 
seconds but a timeout of 900 seconds was requested on the command line. Forcing 
timeout to be 900 seconds.
-- Testing: 96236 tests, 64 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.
FAIL: LLVM :: ExecutionEngine/JITLink/x86-64/COFF_directive_include.s (75287 of 
96236)
 TEST 'LLVM :: 
ExecutionEngine/JITLink/x86-64/COFF_directive_include.s' FAILED 

Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-mc 
-filetype=obj -triple=x86_64-windows-msvc 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_include.s
 -o 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_include.s.tmp
# executed command: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-mc 
-filetype=obj -triple=x86_64-windows-msvc 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_include.s
 -o 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_include.s.tmp
# note: command had no output on stdout or stderr
# RUN: at line 2
not 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-jitlink
 -noexec 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_include.s.tmp
 2>&1 | 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_include.s
# executed command: not 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/llvm-jitlink
 -noexec 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/test/ExecutionEngine/JITLink/x86-64/Output/COFF_directive_include.s.tmp
# note: command had no output on stdout or stderr
# error: command failed with exit status: 1
# executed command: 
/home/b/sanitizer-x86_64-linux-fast/build/llvm_build_asan_ubsan/bin/FileCheck 
/home/b/sanitizer-x86_64-linux-fast/build/llvm-project/llvm/test/ExecutionEngine/JITLink/x86-64/COFF_directive_include.s
# note: command had no output on stdout or stderr

--


Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
Slowest Tests:
--
404.71s: LLVM :: CodeGen/AMDGPU/sched-group-barrier-pipeline-solver.mir
242.73s: Clang :: Preprocessor/riscv-target-features.c
217.80s: Clang :: OpenMP/target_update_codegen.cpp
213.29s: LLVM :: CodeGen/AMDGPU/amdgcn.bitcast.1024bit.ll
208.01s: Clang :: OpenMP/target_defaultmap_codegen_01.cpp
206.17s: Clang :: Driver/arm-cortex-cpus-1.c
201.06s: Cla

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

2026-02-13 Thread LLVM Continuous Integration via cfe-commits

llvm-ci wrote:

LLVM Buildbot has detected a new failure on builder `sanitizer-x86_64-linux` 
running on `sanitizer-buildbot1` while building `clang` at step 2 "annotate".

Full details are available at: 
https://lab.llvm.org/buildbot/#/builders/66/builds/26426


Here is the relevant piece of the build log for the reference

```
Step 2 (annotate) failure: 'python 
../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py'
 (failure)
...
llvm-lit: 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/lit.common.cfg.py:255:
 warning: COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=ON, but this test suite does 
not support testing the just-built runtime libraries when the test compiler is 
configured to use different runtime libraries. Either modify this test suite to 
support this test configuration, or set 
COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=OFF to test the runtime libraries 
included in the compiler instead.
llvm-lit: 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/lit.common.cfg.py:266:
 note: Testing using libraries in 
"/home/b/sanitizer-x86_64-linux/build/build_default/./lib/../lib/clang/23/lib/i386-unknown-linux-gnu"
llvm-lit: 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/lit.common.cfg.py:244:
 warning: Compiler lib dir != compiler-rt lib dir
Compiler libdir: 
"/home/b/sanitizer-x86_64-linux/build/build_default/lib/clang/23/lib/i386-unknown-linux-gnu"
compiler-rt libdir:  
"/home/b/sanitizer-x86_64-linux/build/build_default/lib/clang/23/lib/x86_64-unknown-linux-gnu"
llvm-lit: 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/lit.common.cfg.py:255:
 warning: COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=ON, but this test suite does 
not support testing the just-built runtime libraries when the test compiler is 
configured to use different runtime libraries. Either modify this test suite to 
support this test configuration, or set 
COMPILER_RT_TEST_STANDALONE_BUILD_LIBS=OFF to test the runtime libraries 
included in the compiler instead.
llvm-lit: 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/lit.common.cfg.py:266:
 note: Testing using libraries in 
"/home/b/sanitizer-x86_64-linux/build/build_default/./lib/../lib/clang/23/lib/x86_64-unknown-linux-gnu"
llvm-lit: 
/home/b/sanitizer-x86_64-linux/build/llvm-project/llvm/utils/lit/lit/main.py:74:
 note: The test suite configuration requested an individual test timeout of 0 
seconds but a timeout of 900 seconds was requested on the command line. Forcing 
timeout to be 900 seconds.
-- Testing: 11410 tests, 64 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80
FAIL: XRay-x86_64-linux :: TestCases/Posix/basic-filtering.cpp (9728 of 11410)
 TEST 'XRay-x86_64-linux :: 
TestCases/Posix/basic-filtering.cpp' FAILED 
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 4
/home/b/sanitizer-x86_64-linux/build/build_default/./bin/clang  
--driver-mode=g++ -fxray-instrument  -m64   -std=c++11 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/xray/TestCases/Posix/basic-filtering.cpp
 -o 
/home/b/sanitizer-x86_64-linux/build/build_default/runtimes/runtimes-bins/compiler-rt/test/xray/X86_64LinuxConfig/TestCases/Posix/Output/basic-filtering.cpp.tmp
 -g
# executed command: 
/home/b/sanitizer-x86_64-linux/build/build_default/./bin/clang 
--driver-mode=g++ -fxray-instrument -m64 -std=c++11 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/xray/TestCases/Posix/basic-filtering.cpp
 -o 
/home/b/sanitizer-x86_64-linux/build/build_default/runtimes/runtimes-bins/compiler-rt/test/xray/X86_64LinuxConfig/TestCases/Posix/Output/basic-filtering.cpp.tmp
 -g
# note: command had no output on stdout or stderr
# RUN: at line 5
rm -f basic-filtering-*
# executed command: rm -f 'basic-filtering-*'
# note: command had no output on stdout or stderr
# RUN: at line 6
env XRAY_OPTIONS="patch_premain=true xray_mode=xray-basic verbosity=1  
xray_logfile_base=basic-filtering-  
xray_naive_log_func_duration_threshold_us=1000  
xray_naive_log_max_stack_depth=2"  
/home/b/sanitizer-x86_64-linux/build/build_default/runtimes/runtimes-bins/compiler-rt/test/xray/X86_64LinuxConfig/TestCases/Posix/Output/basic-filtering.cpp.tmp
 2>&1 |  FileCheck 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/xray/TestCases/Posix/basic-filtering.cpp
# executed command: env 'XRAY_OPTIONS=patch_premain=true xray_mode=xray-basic 
verbosity=1  xray_logfile_base=basic-filtering-  
xray_naive_log_func_duration_threshold_us=1000  
xray_naive_log_max_stack_depth=2' 
/home/b/sanitizer-x86_64-linux/build/build_default/runtimes/runtimes-bins/compiler-rt/test/xray/X86_64LinuxConfig/TestCases/Posix/Output/basic-filtering.cpp.tmp
# note: command had no output on stdout or stderr
# executed command: FileCheck 
/home/b/sanitizer-x86_64-linux/build/llvm-project/compiler-rt/test/xray/TestCas

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

2026-02-13 Thread Oleksandr Tarasiuk via cfe-commits

https://github.com/a-tarasyuk closed 
https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-02-13 Thread Oleksandr Tarasiuk 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/6] [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/6] 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);
 }
 }

>From 5f3e4609d28a65e326eaa0a0b6c7ad13f886b92f Mon Sep 17 00:00:00 2001
From: Oleksandr 

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

2026-02-13 Thread Aaron Ballman via cfe-commits


@@ -1996,9 +1996,33 @@ 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());

AaronBallman wrote:

```suggestion
  } else if (const auto *MD = dyn_cast(E->getDecl());
```

https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-02-13 Thread Aaron Ballman via cfe-commits

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

LGTM with some coding style nits (take or leave them).

https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-02-13 Thread Aaron Ballman via cfe-commits

https://github.com/AaronBallman edited 
https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-02-13 Thread Aaron Ballman via cfe-commits


@@ -1996,9 +1996,33 @@ namespace {
 }
 
 void VisitDeclRefExpr(DeclRefExpr *E) {
-  if (VarDecl *VD = dyn_cast(E->getDecl()))
+  if (VarDecl *VD = dyn_cast(E->getDecl())) {

AaronBallman wrote:

```suggestion
  if (const auto *VD = dyn_cast(E->getDecl())) {
```

https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-02-13 Thread Aaron Ballman via cfe-commits


@@ -1996,9 +1996,33 @@ 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)) {
+// FIXME: This has limitations handling updates to the loop control
+// variable that occur indirectly inside a lambda called from the loop
+// body. For example:
+//
+//   int a = 0;
+//   int *c = &a;
+//   auto incr_c = [c]() { ++*c; };
+//   for (a = 10; a <= 20; incr_c())
+// foo(a);
+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());

AaronBallman wrote:

```suggestion
  const auto *VD = dyn_cast(Capture.getCapturedVar());
```

https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-01-08 Thread Oleksandr T. via cfe-commits


@@ -496,7 +496,9 @@ Improvements to Clang's diagnostics
   Now the include/import locations are written into 
`sarif.run.result.relatedLocations`.
 
 - Clang now generates a fix-it for C++20 designated initializers when the 
-  initializers do not match the declaration order in the structure. 
+  initializers do not match the declaration order in the structure.
+
+- The ``-Wloop-analysis`` warning now handles variable modifications inside 
lambda expressions (#GH132038).

a-tarasyuk wrote:

@zyn0217 Thanks for the feedback 👍. I’ve made the requested changes and will 
wait for @AaronBallman’s feedback before moving forward with the close/merge.

https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-01-07 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff origin/main HEAD --extensions cpp -- 
clang/lib/Sema/AnalysisBasedWarnings.cpp clang/lib/Sema/SemaStmt.cpp 
clang/test/SemaCXX/warn-loop-analysis.cpp --diff_from_common_commit
``

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:





View the diff from clang-format here.


``diff
diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp 
b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 0066bd853..fa66248bf 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -3034,14 +3034,14 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
 AC.getCFGBuildOptions().setAllAlwaysAdd();
   } else {
 AC.getCFGBuildOptions()
-  .setAlwaysAdd(Stmt::BinaryOperatorClass)
-  .setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
-  .setAlwaysAdd(Stmt::BlockExprClass)
-  .setAlwaysAdd(Stmt::CStyleCastExprClass)
-  .setAlwaysAdd(Stmt::DeclRefExprClass)
-  .setAlwaysAdd(Stmt::ImplicitCastExprClass)
-  .setAlwaysAdd(Stmt::UnaryOperatorClass)
-  .setAlwaysAdd(Stmt::ForStmtClass);
+.setAlwaysAdd(Stmt::BinaryOperatorClass)
+.setAlwaysAdd(Stmt::CompoundAssignOperatorClass)
+.setAlwaysAdd(Stmt::BlockExprClass)
+.setAlwaysAdd(Stmt::CStyleCastExprClass)
+.setAlwaysAdd(Stmt::DeclRefExprClass)
+.setAlwaysAdd(Stmt::ImplicitCastExprClass)
+.setAlwaysAdd(Stmt::UnaryOperatorClass)
+.setAlwaysAdd(Stmt::ForStmtClass);
   }
   if (EnableLifetimeSafetyAnalysis) {
 AC.getCFGBuildOptions().AddLifetime = true;
@@ -3181,7 +3181,8 @@ void clang::sema::AnalysisBasedWarnings::IssueWarnings(
   if (S.getLangOpts().CPlusPlus && !fscope->isCoroutine() && 
isNoexcept(FD))
 checkThrowInNonThrowingFunc(S, FD, AC);
 
-  if (!Diags.isIgnored(diag::warn_variables_not_in_loop_body, 
D->getBeginLoc())) {
+  if (!Diags.isIgnored(diag::warn_variables_not_in_loop_body,
+   D->getBeginLoc())) {
 D->dumpColor();
 DiagnoseForLoopConditionVarsNotModified();
   }
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 736df4001..695e10060 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2321,9 +2321,10 @@ StmtResult Sema::ActOnForStmt(SourceLocation ForLoc, 
SourceLocation LParenLoc,
   CheckBreakContinueBinding(third.get());
   CheckForRedundantIteration(*this, third.get(), Body);
 
-//  if (!Second.get().first)
-//CheckForLoopConditionalStatement(*this, Second.get().second, third.get(),
-// Body);
+  //  if (!Second.get().first)
+  //CheckForLoopConditionalStatement(*this, Second.get().second,
+  //third.get(),
+  // Body);
 
   if (Second.get().second &&
   !Diags.isIgnored(diag::warn_comma_operator,

``




https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


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

2026-01-07 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/5] [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/5] 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);
 }
 }

>From b96435a39883975abb598ff56103a547ff221887 Mon Sep 17 00:00:00 2001
From: Oleksandr 

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

2026-01-07 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)

2026-01-06 Thread Oleksandr T. via cfe-commits

a-tarasyuk wrote:

It seems this PR requires a decision on whether these changes are acceptable 
for fixing this particular case, or whether it should be closed in favor of a 
future CFG-based solution. If the latter, I’m okay with closing it.

I think a complete rework of -Wfor-loop-analysis is better done in a new PR — I 
looked into it a bit, and it would require refactoring that goes beyond this 
particular case, so it feels more appropriate to handle it as a separate PR 
with a complete redesign.

@cor3ntin @AaronBallman @zyn0217 WDYT?




https://github.com/llvm/llvm-project/pull/135573
___
cfe-commits mailing list
[email protected]
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-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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
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
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits