Skip to content

[Clang] enhance loop analysis to handle variable changes inside lambdas #135573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ Improvements to Clang's diagnostics
they're only triggered if the authors are already making the choice to use
``preferred_type`` attribute.

- The ``-Wloop-analysis`` warning now handles variable modifications inside lambda expressions (#GH132038).

Improvements to Clang's time-trace
----------------------------------
Expand Down
19 changes: 17 additions & 2 deletions clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2000,9 +2000,24 @@ namespace {
}

void VisitDeclRefExpr(DeclRefExpr *E) {
if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl()))
if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
if (Decls.count(VD))
FoundDecl = true;
} else if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(E->getDecl());
MD && isLambdaCallOperator(MD)) {
for (const auto &Capture : MD->getParent()->captures()) {
if (!Capture.capturesVariable())
continue;

LambdaCaptureKind CK = Capture.getCaptureKind();
if (CK != LCK_ByRef)
continue;

VarDecl *VD = dyn_cast<VarDecl>(Capture.getCapturedVar());
if (VD && Decls.count(VD))
FoundDecl = true;
}
}
}

void VisitPseudoObjectExpr(PseudoObjectExpr *POE) {
Expand All @@ -2019,7 +2034,7 @@ namespace {

bool FoundDeclInUse() { return FoundDecl; }

}; // end class DeclMatcher
}; // end class DeclMatcher

void CheckForLoopConditionalStatement(Sema &S, Expr *Second,
Expr *Third, Stmt *Body) {
Expand Down
42 changes: 42 additions & 0 deletions clang/test/SemaCXX/warn-loop-analysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,45 @@ 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We normally wrap tests from bug reports in namespace GHXXXX where XXXX is the bug report number.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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]() { }()) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

for (int i; i < 1; ) { by_ref(i); }

void by_ref(int &value) { }

Copy link
Contributor

@zyn0217 zyn0217 Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test this case?

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

(Sorry, I mean for (a = 10; ...))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

// Dereferencing pointers is ignored for now.
for (int *i; *i; ) {}

rewritten using dataflow analysis

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

}

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

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

// 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);
}
}
Loading