[clang] [LifetimeSafety] Add details for `-Wlifetime-safety-return-stack-addr` diagnostic (PR #199432)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Mon May 25 02:01:41 PDT 2026
================
@@ -10967,16 +10967,27 @@ def warn_lifetime_safety_use_after_free : Warning<
"%select{allocated object|parameter}0 does not live long enough">,
InGroup<LifetimeSafetyUseAfterFree>, DefaultIgnore;
-def warn_lifetime_safety_return_stack_addr
- : Warning<"address of stack memory is returned later">,
- InGroup<LifetimeSafetyReturnStackAddr>,
- DefaultIgnore;
-def warn_lifetime_safety_return_stack_addr_moved
- : Warning<"address of stack memory may be returned later. "
- "This could be false positive as the storage may have been moved. "
- "Consider moving first and then aliasing later to resolve the issue">,
- InGroup<LifetimeSafetyReturnStackAddrMoved>,
- DefaultIgnore;
+def warn_lifetime_safety_return_stack_addr : Warning<
+ "%select{address of|reference to}0 stack memory associated with "
+ "%select{local variable|parameter}2 %1 returned">,
+ InGroup<LifetimeSafetyReturnStackAddr>, DefaultIgnore;
----------------
usx95 wrote:
I understand your concern about diagnostic readability, and you're right.
My main concern is the repetition of `{local variable|parameter|temporary|array|...}` across all diagnostics, not just return-stack-addr. These all describe the storage. We may also want similar detail in the "destroyed here" note later: "variable 'a' destroyed here" instead of just "destroyed here".
So I am seeing a need for abstracting the storage description here and having a single place in `SemaHelper` to format it consistently.
Some suggestions:
1. Let us merge the text about the temporary into this to avoid introducing more diags. E.g. "stack memory associated with the temporary ..." or "stack memory associated with temporary object..."
2. Remove the `%select{address of|reference to}0` for now. We can add it later if there is need. Looks like it is only going to be useful for return-stack-addr.
More concretely:
```
def warn_lifetime_safety_return_stack_addr : Warning<
"stack memory associated with %0 is returned">,
InGroup<LifetimeSafetyReturnStackAddr>, DefaultIgnore;
```
I think this also gives a clear idea of the diagnostic format just by looking at the definition, where %0 would be filled with "local variable 'x'", "parameter 'y'", "temporary object", etc.
WDYT?
https://github.com/llvm/llvm-project/pull/199432
More information about the cfe-commits
mailing list