[clang] Adding use-after-return in Lifetime Analysis (PR #165370)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Thu Oct 30 12:56:45 PDT 2025
================
@@ -106,6 +120,63 @@ class LifetimeChecker {
/*ConfidenceLevel=*/CurConfidence};
}
+ void checkEscape(const OriginEscapesFact *OEF,
+ llvm::ArrayRef<const ExpireFact *> BlockExpires) {
----------------
usx95 wrote:
This seems important to choose the diagnostic loc and even kind of diagnostic
```cpp
std::string_view foo() {
std::string_view res;
{
std::string small = "small scope";
res = small;
}
return res;
}
```
I think for this we should have an existing use-after-scope (because `res` in `return res` is a "use" even before the `return`).
Also there could be cases where the loan is bad due to both use-after-scope and return-stack-addr
```cpp
std::string_view foo() {
std::string_view res;
{
std::string small = "small scope";
res = small;
if (cond) {
return res;
}
}
std::cout << res;
}
```
We would see two loan expiries here. One before `return` and other at end of scope. Both would be strict confidence (based on liveness). In this case we could just prefer the earliest gen responsible for liveness. In this case it would be the `EscapeOrigin` from `return res;`. So we would diagnose this as return-stack-addr.
On the other hand, following would be existing use-after-scope:
```cpp
std::string_view foo() {
std::string_view res;
{
std::string small = "small scope";
res = small; // does not live long enough.
}
std::cout << res; // later used here.
return res;
}
```
https://github.com/llvm/llvm-project/pull/165370
More information about the cfe-commits
mailing list