llvm-project
llvm-project copied to clipboard
Add details about the storage like variable name, stack/temporary
For example, it helps the following case:
// TODO: Include the name of the variable in the diagnostic to improve
// clarity, especially for implicit lambda captures where multiple warnings
// can point to the same source location.
auto implicit_ref_capture_multiple() {
int local = 1, local2 = 2;
auto lambda = [&]() { return local + local2; }; // expected-warning 2 {{address of stack memory is returned later}}
return lambda; // expected-note 2 {{returned here}}
}
It can be similar to the pre-existing analysis, such as -Wreturn-stack-address. For example: https://godbolt.org/z/ecb4dMYKG
<source>:28:13: warning: address of stack memory associated with local variable 'integer' returned [-Wreturn-stack-address]
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^
<source>:28:24: note: implicitly captured by reference due to use here
28 | return [&]() { Foo(integer, ptr, str, view); };
| ~ ^
cc @usx95
@llvm/issue-subscribers-clang-temporal-safety
Author: Zhijie Wang (aeft)
For example, it helps the following case:
```cpp
// TODO: Include the name of the variable in the diagnostic to improve
// clarity, especially for implicit lambda captures where multiple warnings
// can point to the same source location.
auto implicit_ref_capture_multiple() {
int local = 1, local2 = 2;
auto lambda = [&]() { return local + local2; }; // expected-warning 2 {{address of stack memory is returned later}}
return lambda; // expected-note 2 {{returned here}}
}
```
It can be similar to the pre-existing analysis, such as -Wreturn-stack-address. For example: https://godbolt.org/z/ecb4dMYKG
<source>:28:13: warning: address of stack memory associated with local variable 'integer' returned [-Wreturn-stack-address]
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^
<source>:28:24: note: implicitly captured by reference due to use here
28 | return [&]() { Foo(integer, ptr, str, view); };
| ~ ^
cc @usx95
I would like to work on this.
This is now fixed:
<source>:28:13: warning: stack memory associated with local variable 'ptr' is returned [-Wlifetime-safety-return-stack-addr]
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^
<source>:28:12: note: returned here
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:28:13: warning: stack memory associated with local variable 'str' is returned [-Wlifetime-safety-return-stack-addr]
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^
<source>:28:12: note: returned here
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:28:13: warning: stack memory associated with local variable 'integer' is returned [-Wlifetime-safety-return-stack-addr]
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^
<source>:28:12: note: returned here
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:28:13: warning: stack memory associated with local variable 'view' is returned [-Wlifetime-safety-return-stack-addr]
28 | return [&]() { Foo(integer, ptr, str, view); };
| ^
<source>:28:12: note: returned here
28 | return [&]() { Foo(integer, ptr, str, view); };
Similarly for capture by value, https://godbolt.org/z/xvqzMje4E:
auto CreateCallback() {
int integer;
int* ptr;
std::string str;
std::string_view view;
ptr = &integer;
view = str;
return [=]() { Foo(integer, ptr, str, view); };
}
<source>:16:12: warning: stack memory associated with local variable 'integer' is returned [-Wlifetime-safety-return-stack-addr]
16 | ptr = &integer;
| ^~~~~~~
<source>:18:12: note: returned here
18 | return [=]() { Foo(integer, ptr, str, view); };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:17:12: warning: stack memory associated with local variable 'str' is returned [-Wlifetime-safety-return-stack-addr]
17 | view = str;
| ^~~
<source>:18:12: note: returned here
18 | return [=]() { Foo(integer, ptr, str, view); };
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Same for temporary. https://godbolt.org/z/qxs6r6jx9
std::string_view get(const std::string& str [[clang::lifetimebound]]) {
return str;
}
std::string_view foo() {
std::string str;
return get(std::string("temp"));
}
<source>:12:16: warning: stack memory associated with local temporary object is returned [-Wlifetime-safety-return-stack-addr]
12 | return get(std::string("temp"));
| ^~~~~~~~~~~~~~~~~~~
<source>:12:12: note: returned here
12 | return get(std::string("temp"));
| ^~~~~~~~~~~~~~~~~~~~~~~~