[clang] [Lifetime Safety] Highlight lifetimebound calls in alias chain diagnostics (PR #206337)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 10 22:36:30 PDT 2026
================
@@ -670,14 +670,39 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
const Expr *LastExpr = OriginExprChain.back();
std::string IssueStr = getDiagSubjectDescription(LastExpr);
+ std::optional<LifetimeBoundParamInfo> HiddenLifetimeBound;
----------------
iitianpushkar wrote:
The reason I added this was because some call/constructor expressions are part of the origin flow chain but are not printed in the alias chain because they have the same source range as another expression.
The below test failed because of that :
```cpp
template <typename T> struct [[gsl::Pointer]] Pointer {
Pointer(const T &bar [[clang::lifetimebound]]);
};
void nested_local_pointer() {
Pointer<Pointer<Pointer<Bar>>> ppp;
Pointer<Pointer<Bar>> pp;
Pointer<Bar> p;
{
Bar v;
p = Pointer(v); // expected-warning {{local variable 'v' does not live long enough}}
pp = Pointer(p); // expected-note {{local variable 'p' aliases the storage of local variable 'v' because parameter 'bar' is marked 'lifetimebound'}}
ppp = Pointer(pp); // expected-note {{local variable 'pp' aliases the storage of local variable 'v'}}
} // expected-note {{local variable 'v' is destroyed here}}
use(***ppp); // expected-note {{later used here}}
}
```
For pp = Pointer(p), the lifetimebound info comes from the implicit constructor call Pointer(p) but that step can be skipped by shouldShowInAliasChain because it shares the same source range as the visible alias step. Without carrying the info over, the diagnostic becomes only:
```
local variable 'p' aliases the storage of local variable 'v'
```
The value is reset immediately after emitting the next visible alias note, so it cannot leak further down the chain.
I see your concern that this may attach the explanation to a visible edge that is not exactly the hidden edge. If you think that is too confusing, I can switch to the more conservative behaviour and only print the lifetimebound explanation when the currently visible edge itself re-derives it.
What are your thoughts on this ?
Cc @Xazax-hun
https://github.com/llvm/llvm-project/pull/206337
More information about the cfe-commits
mailing list