[clang] Suggest lifetime annotations [Issue Id: 169375] (PR #169767)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 27 02:33:10 PST 2025


================
@@ -342,4 +347,27 @@ void FactsGenerator::markUseAsWrite(const DeclRefExpr *DRE) {
   UseFacts[DRE]->markAsWritten();
 }
 
+// Creates an IssueFact for a new placeholder loan for each pointer or reference
+// parameter at the function's entry.
+llvm::SmallVector<Fact *> FactsGenerator::createPlaceholderLoanFacts() {
+  llvm::SmallVector<Fact *> PlaceholderLoanFacts;
+  const auto *FD = dyn_cast<FunctionDecl>(AC.getDecl());
+  if (!FD)
+    return PlaceholderLoanFacts;
+
+  for (const ParmVarDecl *PVD : FD->parameters()) {
+    QualType ParamType = PVD->getType();
+    if (PVD->hasAttr<LifetimeBoundAttr>())
----------------
Xazax-hun wrote:

We do not necessarily need to address this now, but I think we might want to have loans for the `lifetimebound` annotated parameters as well. This would help as find invalidation style issues in the future:

```
int* func(std::vector<int>& v [[clang::lifetimebound]]) {
  v.push_back(0);
  return v[0];
}
```

Also, having loans for annotated parameters would help us potentially find erroneous annotations:

```
int* func(std::vector<int>& v1 [[clang::lifetimebound]], // oops, added to the wrong parameter.
          std::vector<int>& v2) {
  return v2[0];
}
```

https://github.com/llvm/llvm-project/pull/169767


More information about the cfe-commits mailing list