<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/152508>152508</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LifetimeSafety] Increase C++ coverage & integrate annotations
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
usx95
</td>
</tr>
</table>
<pre>
Expand the analysis to correctly model more C++ semantics and integrate with existing lifetime annotations to increase coverage and accuracy.
# Enhance C++ Language Coverage
* View-Type Support: Extend the analysis beyond raw pointers to handle common view/span types that act like non-owning references.
```cpp
std::string_view foo() {
std::string a = "on stack";
std::string_view res = a;
return res; // warning: returning address of local variable 'a'.
}
```
* Temporary: Detect a common bug where a reference is taken to a temporary object that is destroyed at the end of the full expression. A warning for code such as `std::string_view sv = std::string("temporary string");`. This is noted as a lower priority since it is already detected by a pre-existing analysis.
# Model Function Calls and Annotations
* Function Call Semantics: Correctly model how lifetimes flow through function calls, from arguments to return values.
* Opaque Loans: To support gradual rollout and handle functions without annotations, the analysis will use a fallback mechanism. When a function's lifetime behavior is unknown, the analysis will assign an `OPAQUE` loan to its output. This prevents false negatives while acknowledging uncertainty. Passing a variable with a known origin to an opaque function that takes a mutable reference will kill the known loans and add an `OPAQUE` one to its origin.
* Integrate `[[clang::lifetimebound]]`: This existing Clang annotation is crucial for specifying that the lifetime of a function's output is tied to one of its inputs. The analysis must consume this information.
```cpp
std::string_view Trim(std::string_view input [[clang::lifetimebound]]);
std::string_view foo() {
std::string a = "on stack";
std::string_view res = Trim(a);
return res; // warning: returning address of local variable 'a'.
}
```
# Analysis Metrics
Coverage Statistics: To measure progress and identify weaknesses, the analysis will track its own coverage.
A set of analysis internal statistics tracking metrics like the number of pointer-like expressions that are fully tracked versus those that fall back to OPAQUE loans or are not even associated an origin.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy8Vltv4zoO_j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