[clang] [LifetimeSafety] Prevent flase-negative lifetimebound verification when origin escapes in an unrelated manner (PR #200786)
Abhinav Pradeep via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 1 04:19:16 PDT 2026
https://github.com/AbhinavPradeep created https://github.com/llvm/llvm-project/pull/200786
This PR removes a flase-negative `[[lifetimebound]]` verification result that occurs when the annotated attribute escapes via unrelated origin escape kinds (escape through global, escape through field).
Change summary:
- `Checker.cpp` has function `checkAnnotations` which checks if an escaping `Origin` was an annotated parameter. Modified the logic there to only verify `[[lifetimebound]]` in the case that the escape was through a return statement.
>From 8eecd5edc92fee47a8249ca580f7bb380d1f6e78 Mon Sep 17 00:00:00 2001
From: Abhinav Pradeep <abhinav.pradeep at oracle.com>
Date: Mon, 1 Jun 2026 21:03:01 +1000
Subject: [PATCH] fix: addressed lifetimebound verification issue
Signed-off-by: Abhinav Pradeep <abhinav.pradeep at oracle.com>
---
clang/lib/Analysis/LifetimeSafety/Checker.cpp | 10 ++++++----
.../warn-lifetime-safety-lifetimebound.cpp | 18 ++++++++++++++++++
2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index ad928c3754fea..e61ec18ed890a 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -133,7 +133,8 @@ class LifetimeChecker {
return;
if (PVD->hasAttr<LifetimeBoundAttr>()) {
// Track that this lifetimebound parameter correctly escapes.
- VerifiedLiftimeboundEscapes.insert(PVD);
+ if (isa<ReturnEscapeFact>(OEF))
+ VerifiedLiftimeboundEscapes.insert(PVD);
} else {
// Otherwise, suggest lifetimebound for parameter escaping through
// return or a field in constructor.
@@ -147,9 +148,10 @@ class LifetimeChecker {
// field!
};
auto CheckImplicitThis = [&](const CXXMethodDecl *MD) {
- if (implicitObjectParamIsLifetimeBound(MD))
- VerifiedLiftimeboundEscapes.insert(MD);
- else if (auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
+ if (implicitObjectParamIsLifetimeBound(MD)) {
+ if (isa<ReturnEscapeFact>(OEF))
+ VerifiedLiftimeboundEscapes.insert(MD);
+ } else if (auto *ReturnEsc = dyn_cast<ReturnEscapeFact>(OEF))
AnnotationWarningsMap.try_emplace(MD, ReturnEsc->getReturnExpr());
};
auto MovedAtEscape = MovedLoans.getMovedLoans(OEF);
diff --git a/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp b/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
index 5e22677f5269d..b0320bcbf4334 100644
--- a/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
@@ -44,6 +44,24 @@ View return_alias_through_unannotated_passthrough(
return not_lb(alias);
}
+View global_view;
+
+View assign_param_to_global(
+ const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
+ global_view = obj; // Wrong kind of escape.
+ return View(); // Unrelated view.
+}
+
+struct EnclosingState {
+ View member;
+
+ View assign_param_to_field(
+ const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
+ member = obj; // Wrong kind of escape.
+ return View(); // Unrelated view.
+ }
+};
+
View not_lb_view(View v);
View lb_view(View v [[clang::lifetimebound]]);
More information about the cfe-commits
mailing list