-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[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
base: main
Are you sure you want to change the base?
Changes from all commits
93c8fc7
e92de51
541df49
0005379
b75af8f
169172f
f29ad4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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); | ||||||||||
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]() { }()) { } | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you know why we don't complain about this case? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... llvm-project/clang/test/SemaCXX/warn-loop-analysis.cpp Lines 40 to 41 in db0f754
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); | ||||||||||
} | ||||||||||
} |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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