https://github.com/usx95 updated 
https://github.com/llvm/llvm-project/pull/191831

>From d3b9e2b16702e5c605a9884f4a9722fd835e66ae Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <[email protected]>
Date: Mon, 13 Apr 2026 14:53:43 +0000
Subject: [PATCH 1/3] [LifetimeSafety] Detect dangling field of base class

---
 clang/lib/Analysis/LifetimeSafety/Origins.cpp  |  2 +-
 .../warn-lifetime-safety-dangling-field.cpp    | 12 ++++++++++++
 clang/test/Sema/warn-lifetime-safety.cpp       | 18 ++++++++++++++++++
 3 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Analysis/LifetimeSafety/Origins.cpp 
b/clang/lib/Analysis/LifetimeSafety/Origins.cpp
index abf9890b08522..fdd2671dee2e0 100644
--- a/clang/lib/Analysis/LifetimeSafety/Origins.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/Origins.cpp
@@ -236,7 +236,7 @@ OriginList *OriginManager::getOrCreateList(const Expr *E) {
     ReferencedDecl = DRE->getDecl();
   else if (auto *ME = dyn_cast<MemberExpr>(E))
     if (auto *Field = dyn_cast<FieldDecl>(ME->getMemberDecl());
-        Field && isa<CXXThisExpr>(ME->getBase()))
+        Field && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
       ReferencedDecl = Field;
   if (ReferencedDecl) {
     OriginList *Head = nullptr;
diff --git a/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp 
b/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
index fa45458371012..79b0183ed91ec 100644
--- a/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
@@ -184,3 +184,15 @@ struct IndirectEscape2 {
     p = s.data();
   }
 };
+
+namespace DanglingPointerFieldInBaseClass {
+struct BaseWithPointer {
+  std::string_view view; // expected-note {{this field dangles}}
+};
+
+struct DerivedWithCtor : BaseWithPointer {
+  DerivedWithCtor(std::string s) {
+    view = s; // expected-warning {{address of stack memory escapes to a 
field}}
+  }
+};
+} // namespace DanglingPointerFieldInBaseClass
diff --git a/clang/test/Sema/warn-lifetime-safety.cpp 
b/clang/test/Sema/warn-lifetime-safety.cpp
index b1dea7f7340b3..d5e48eb326a22 100644
--- a/clang/test/Sema/warn-lifetime-safety.cpp
+++ b/clang/test/Sema/warn-lifetime-safety.cpp
@@ -2531,3 +2531,21 @@ int *noreturn_dead_nested(bool cond, bool cond2, int 
*num) {
 }
 
 } // namespace conditional_operator_control_flow
+
+//===----------------------------------------------------------------------===//
+// Base Class Field Tests
+//===----------------------------------------------------------------------===//
+namespace base_class_fields {
+struct X { int* x; }; // expected-note {{this field dangles}}
+struct Y : X {
+  int* y;
+  void bar() {
+    {
+      int a;
+      x = &a; // expected-warning {{address of stack memory escapes to a 
field}}
+    }
+    (void)x;
+  }
+};
+} // namespace base_class_fields
+

>From 55c3e5d0c1bd28159d984d8025c29d5401214553 Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <[email protected]>
Date: Mon, 13 Apr 2026 14:58:08 +0000
Subject: [PATCH 2/3] remove comments

---
 clang/test/Sema/warn-lifetime-safety.cpp | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/clang/test/Sema/warn-lifetime-safety.cpp 
b/clang/test/Sema/warn-lifetime-safety.cpp
index d5e48eb326a22..9c8df51833a0a 100644
--- a/clang/test/Sema/warn-lifetime-safety.cpp
+++ b/clang/test/Sema/warn-lifetime-safety.cpp
@@ -2532,9 +2532,6 @@ int *noreturn_dead_nested(bool cond, bool cond2, int 
*num) {
 
 } // namespace conditional_operator_control_flow
 
-//===----------------------------------------------------------------------===//
-// Base Class Field Tests
-//===----------------------------------------------------------------------===//
 namespace base_class_fields {
 struct X { int* x; }; // expected-note {{this field dangles}}
 struct Y : X {

>From 78f51dfd3ed102faa19ae4da5ce90237a0ec2def Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <[email protected]>
Date: Mon, 13 Apr 2026 15:02:56 +0000
Subject: [PATCH 3/3] suggestions and inference

---
 .../Sema/warn-lifetime-safety-suggestions.cpp     | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp 
b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
index c96b92cfe0b5b..b3b13038dc344 100644
--- a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
@@ -513,4 +513,19 @@ CaptureRefToRef test_ref_to_ref() {
   CaptureRefToRef x(obj); // expected-warning {{address of stack memory is 
returned later}}
   return x; // expected-note {{returned here}}
 }
+
+struct BaseWithView {
+  View v; // expected-note {{escapes to this field}}
+};
+struct CaptureRefToBaseView : BaseWithView {
+  CaptureRefToBaseView(const MyObj& obj) { // expected-warning {{parameter in 
intra-TU function should be marked [[clang::lifetimebound]]}}
+    v = obj;
+  }
+};
+
+CaptureRefToBaseView test_ref_to_base_view() {
+  MyObj obj;
+  CaptureRefToBaseView x(obj); // expected-warning {{address of stack memory 
is returned later}}
+  return x; // expected-note {{returned here}}
+}
 } // namespace capturing_constructor

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

Reply via email to