[clang] [LifetimeSafety] Prevent flase-negative lifetimebound verification when origin escapes in an unrelated manner (PR #200786)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 1 04:19:54 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Abhinav Pradeep (AbhinavPradeep)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/200786.diff


2 Files Affected:

- (modified) clang/lib/Analysis/LifetimeSafety/Checker.cpp (+6-4) 
- (modified) clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp (+18) 


``````````diff
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]]);

``````````

</details>


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


More information about the cfe-commits mailing list