Add details for -Wlifetime-safety-invalidation diags
https://godbolt.org/z/jTb1zEcfP
#include <vector>
void f(std::vector<int> a, std::vector<int> b, bool flag) {
int* pa = &a[0];
int* pb = &b[0];
std::vector<int>* pv = (flag ? &a : &b);
pv->push_back(42);
(void)pa;
(void)pb;
}
Current:
<source>:3:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
3 | int* pa = &a[0];
| ^
<source>:6:9: note: invalidated here
6 | pv->push_back(42);
| ~~~~^~~~~~~~~~~~~
<source>:7:11: note: later used here
7 | (void)pa;
| ^~
<source>:4:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
4 | int* pb = &b[0];
| ^
<source>:6:9: note: invalidated here
6 | pv->push_back(42);
| ~~~~^~~~~~~~~~~~~
<source>:8:11: note: later used here
8 | (void)pb;
| ^~
Expected:
- local variable 'a' is later invalidated
- local variable 'b' is later invalidated
@llvm/issue-subscribers-clang-temporal-safety
Author: Utkarsh Saxena (usx95)
#include <vector>
void f(std::vector<int> a, std::vector<int> b, bool flag) {
int* pa = &a[0];
int* pb = &b[0];
std::vector<int>* pv = (flag ? &a : &b);
pv->push_back(42);
(void)pa;
(void)pb;
}
Current:
<source>:3:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
3 | int* pa = &a[0];
| ^
<source>:6:9: note: invalidated here
6 | pv->push_back(42);
| ~~~~^~~~~~~~~~~~~
<source>:7:11: note: later used here
7 | (void)pa;
| ^~
<source>:4:16: warning: object whose reference is captured is later invalidated [-Wlifetime-safety-invalidation]
4 | int* pb = &b[0];
| ^
<source>:6:9: note: invalidated here
6 | pv->push_back(42);
| ~~~~^~~~~~~~~~~~~
<source>:8:11: note: later used here
8 | (void)pb;
| ^~
Expected:
- local variable 'a' is later invalidated
- local variable 'b' is later invalidated
Similarly for params, field and globals. https://godbolt.org/z/dcc3E6Wzn
#include <vector>
void f(std::vector<int> &a) {
int* pa = &a[0];
a.push_back(42);
(void)pa;
}
<source>:2:8: warning: parameter is later invalidated [-Wlifetime-safety-invalidation]
2 | void f(std::vector<int> &a) {
| ^~~~~~~~~~~~~~~~~~~
<source>:4:7: note: invalidated here
4 | a.push_back(42);
| ~~^~~~~~~~~~~~~
<source>:5:11: note: later used here
5 | (void)pa;
| ^~
expected:
- parameter 'a' is later invalidated.
Hey @usx95 , do you think this is already tackled in PR #204900 ?
Yes, I think we close this issue with your PR. We already added object description to the warnings, and now you've added it to notes too.
While looking at the lifetime safety project board on GitHub to see where I can start learning more about LSA I have found this issue and based on the comments and the PR listed solving this issue I think we can close this issue as completed. @NeKon69 @usx95