Author: Jiaqi He
Date: 2026-06-22T12:08:02Z
New Revision: b6a0f6c79fee527a5ef2826e30949cd8f27db889

URL: 
https://github.com/llvm/llvm-project/commit/b6a0f6c79fee527a5ef2826e30949cd8f27db889
DIFF: 
https://github.com/llvm/llvm-project/commit/b6a0f6c79fee527a5ef2826e30949cd8f27db889.diff

LOG: [LifetimeSafety] resolved lifetimeBound violation in constructor (#204797)

Fix https://github.com/llvm/llvm-project/issues/203839.

Constructor body does not produce `ReturnEscapeFact`, but a constructor
parameter marked [[clang::lifetimebound]] may still be valid if it
escapes into a field of the constructed object.

Update the `LifetimeBound` logic to accept `FieldEscapeFact` for
constructors, and add a test case for this pattern.

Added: 
    

Modified: 
    clang/lib/Analysis/LifetimeSafety/Checker.cpp
    clang/test/Sema/LifetimeSafety/lifetimebound-violation.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp 
b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
index d41d6f43f837b..c258a1dc3596c 100644
--- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp
@@ -134,8 +134,10 @@ class LifetimeChecker {
       if (IsMoved)
         return;
       if (PVD->hasAttr<LifetimeBoundAttr>()) {
-        // Track that this lifetimebound parameter correctly escapes.
-        if (isa<ReturnEscapeFact>(OEF))
+        // Track that this lifetimebound parameter correctly escapes
+        // (via return or via field assignment in a constructor).
+        if (isa<ReturnEscapeFact>(OEF) ||
+            (isa<FieldEscapeFact>(OEF) && isa<CXXConstructorDecl>(FD)))
           VerifiedLiftimeboundEscapes.insert(PVD);
       } else {
         // Otherwise, suggest lifetimebound for parameter escaping through

diff  --git a/clang/test/Sema/LifetimeSafety/lifetimebound-violation.cpp 
b/clang/test/Sema/LifetimeSafety/lifetimebound-violation.cpp
index 09b2a231efffe..fcf8e496de3d3 100644
--- a/clang/test/Sema/LifetimeSafety/lifetimebound-violation.cpp
+++ b/clang/test/Sema/LifetimeSafety/lifetimebound-violation.cpp
@@ -166,3 +166,8 @@ struct AssignementIncorr {
 void implicit_lifetimebound_in_nested_std_namespace() {
   (void)std::basic_string_view<char>("hello");
 }
+struct ViewWithMember {
+  // No warning. A lifetimebound constructor parameter may escape into a field 
of the constructed object.
+  ViewWithMember(const char *data [[clang::lifetimebound]]) : mData(data) {}
+  const char *mData;
+};


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to