[clang] [LifetimeSafety] Improve dangling field/global diagnostics (PR #200147)
via cfe-commits
cfe-commits at lists.llvm.org
Thu May 28 02:55:15 PDT 2026
https://github.com/NeKon69 created https://github.com/llvm/llvm-project/pull/200147
Reuses the function for getting object information that was added in #199432
Comes as part of completing #186002
>From a250e901477a5a567f06607612302c5e9f22210a Mon Sep 17 00:00:00 2001
From: NeKon69 <nobodqwe at gmail.com>
Date: Thu, 28 May 2026 12:54:31 +0300
Subject: [PATCH] [LifetimeSafety] Improve dangling field/global diagnostics
---
.../clang/Basic/DiagnosticSemaKinds.td | 8 +--
clang/lib/Sema/SemaLifetimeSafety.h | 20 ++++---
.../Sema/warn-lifetime-analysis-nocfg.cpp | 12 ++---
.../warn-lifetime-safety-dangling-field.cpp | 54 +++++++++----------
.../warn-lifetime-safety-dangling-global.cpp | 6 +--
.../Sema/warn-lifetime-safety-suggestions.cpp | 2 +-
clang/test/Sema/warn-lifetime-safety.cpp | 10 ++--
7 files changed, 58 insertions(+), 54 deletions(-)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index e330ea03d0544..9bb64bb0f5b84 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -10999,21 +10999,21 @@ def warn_lifetime_safety_invalidated_global
DefaultIgnore;
def warn_lifetime_safety_dangling_field
- : Warning<"address of stack memory escapes to a field">,
+ : Warning<"%0 escapes to a field and will dangle">,
InGroup<LifetimeSafetyDanglingField>,
DefaultIgnore;
def warn_lifetime_safety_dangling_field_moved
- : Warning<"address of stack memory escapes to a field. "
+ : Warning<"%0 may escape to a field and dangle. "
"This could be a false positive as the storage may have been moved. "
"Consider moving first and then aliasing later to resolve the issue">,
InGroup<LifetimeSafetyDanglingFieldMoved>,
DefaultIgnore;
def warn_lifetime_safety_dangling_global
- : Warning<"address of stack memory escapes to global or static storage">,
+ : Warning<"%0 escapes to global or static storage and will dangle">,
InGroup<LifetimeSafetyDanglingGlobal>,
DefaultIgnore;
def warn_lifetime_safety_dangling_global_moved
- : Warning<"address of stack memory escapes to global or static storage. "
+ : Warning<"%0 may escape to global or static storage and dangle. "
"This could be a false positive as the storage may have been moved. "
"Consider moving first and then aliasing later to resolve the issue">,
InGroup<LifetimeSafetyDanglingGlobalMoved>,
diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h
index 0305510c1a233..005d3187d27ec 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -95,10 +95,12 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
const FieldDecl *DanglingField,
const Expr *MovedExpr,
SourceLocation ExpiryLoc) override {
- S.Diag(IssueExpr->getExprLoc(),
- MovedExpr ? diag::warn_lifetime_safety_dangling_field_moved
- : diag::warn_lifetime_safety_dangling_field)
- << IssueExpr->getSourceRange();
+ unsigned DiagID = MovedExpr
+ ? diag::warn_lifetime_safety_dangling_field_moved
+ : diag::warn_lifetime_safety_dangling_field;
+
+ S.Diag(IssueExpr->getExprLoc(), DiagID)
+ << getDiagSubjectDescription(IssueExpr) << IssueExpr->getSourceRange();
if (MovedExpr)
S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
<< MovedExpr->getSourceRange();
@@ -111,10 +113,12 @@ class LifetimeSafetySemaHelperImpl : public LifetimeSafetySemaHelper {
const VarDecl *DanglingGlobal,
const Expr *MovedExpr,
SourceLocation ExpiryLoc) override {
- S.Diag(IssueExpr->getExprLoc(),
- MovedExpr ? diag::warn_lifetime_safety_dangling_global_moved
- : diag::warn_lifetime_safety_dangling_global)
- << IssueExpr->getSourceRange();
+ unsigned DiagID = MovedExpr
+ ? diag::warn_lifetime_safety_dangling_global_moved
+ : diag::warn_lifetime_safety_dangling_global;
+
+ S.Diag(IssueExpr->getExprLoc(), DiagID)
+ << getDiagSubjectDescription(IssueExpr) << IssueExpr->getSourceRange();
if (MovedExpr)
S.Diag(MovedExpr->getExprLoc(), diag::note_lifetime_safety_moved_here)
<< MovedExpr->getSourceRange();
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 9c53129ff707d..c4acaf9092bae 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -86,13 +86,13 @@ struct DanglingGslPtrField {
MyLongPointerFromConversion p2; // expected-note {{pointer member declared here}} \
// cfg-note 2 {{this field dangles}}
- DanglingGslPtrField(int i) : p(&i) {} // cfg-warning {{address of stack memory escapes to a field}}
+ DanglingGslPtrField(int i) : p(&i) {} // cfg-warning {{parameter 'i' escapes to a field and will dangle}}
DanglingGslPtrField() : p2(MyLongOwnerWithConversion{}) {} // expected-warning {{initializing pointer member 'p2' to point to a temporary object whose lifetime is shorter than the lifetime of the constructed object}} \
- // cfg-warning {{address of stack memory escapes to a field}}
+ // cfg-warning {{local temporary object escapes to a field and will dangle}}
DanglingGslPtrField(double) : p(MyIntOwner{}) {} // expected-warning {{initializing pointer member 'p' to point to a temporary object whose lifetime is shorter than the lifetime of the constructed object}} \
- // cfg-warning {{address of stack memory escapes to a field}}
- DanglingGslPtrField(MyIntOwner io) : p(io) {} // cfg-warning {{address of stack memory escapes to a field}}
- DanglingGslPtrField(MyLongOwnerWithConversion lo) : p2(lo) {} // cfg-warning {{address of stack memory escapes to a field}}
+ // cfg-warning {{local temporary object escapes to a field and will dangle}}
+ DanglingGslPtrField(MyIntOwner io) : p(io) {} // cfg-warning {{parameter 'io' escapes to a field and will dangle}}
+ DanglingGslPtrField(MyLongOwnerWithConversion lo) : p2(lo) {} // cfg-warning {{parameter 'lo' escapes to a field and will dangle}}
};
MyIntPointer danglingGslPtrFromLocal() {
@@ -1124,7 +1124,7 @@ struct Foo2 {
};
struct Test {
- Test(Foo2 foo) : bar(foo.bar.get()), // cfg-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ Test(Foo2 foo) : bar(foo.bar.get()), // cfg-warning-re {{parameter 'foo' may escape to a field and dangle. {{.*}} may have been moved}}
storage(std::move(foo.bar)) {}; // cfg-note {{potentially moved here}}
Bar* bar; // cfg-note {{this field dangles}}
diff --git a/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp b/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
index 5a4de105f217d..21c3afe2aee89 100644
--- a/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-dangling-field.cpp
@@ -11,23 +11,23 @@ std::string_view construct_view(const std::string& str [[clang::lifetimebound]])
struct CtorInit {
std::string_view view; // expected-note {{this field dangles}}
- CtorInit(std::string s) : view(s) {} // expected-warning {{address of stack memory escapes to a field}}
+ CtorInit(std::string s) : view(s) {} // expected-warning {{parameter 's' escapes to a field and will dangle}}
};
struct CtorSet {
std::string_view view; // expected-note {{this field dangles}}
- CtorSet(std::string s) { view = s; } // expected-warning {{address of stack memory escapes to a field}}
+ CtorSet(std::string s) { view = s; } // expected-warning {{parameter 's' escapes to a field and will dangle}}
};
struct CtorInitLifetimeBound {
std::string_view view; // expected-note {{this field dangles}}
- CtorInitLifetimeBound(std::string s) : view(construct_view(s)) {} // expected-warning {{address of stack memory escapes to a field}}
+ CtorInitLifetimeBound(std::string s) : view(construct_view(s)) {} // expected-warning {{parameter 's' escapes to a field and will dangle}}
};
struct CtorInitButMoved {
std::string_view view; // expected-note {{this field dangles}}
CtorInitButMoved(std::string s)
- : view(s) { // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ : view(s) { // expected-warning-re {{parameter 's' may escape to a field and dangle. {{.*}} may have been moved}}
takeString(std::move(s)); // expected-note {{potentially moved here}}
}
};
@@ -36,7 +36,7 @@ struct CtorInitButMovedOwned {
std::string_view view; // expected-note {{this field dangles}}
std::string owned;
CtorInitButMovedOwned(std::string s)
- : view(s), // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ : view(s), // expected-warning-re {{parameter 's' may escape to a field and dangle. {{.*}} may have been moved}}
owned(std::move(s)) {} // expected-note {{potentially moved here}}
};
@@ -50,28 +50,28 @@ struct CtorInitButMovedOwnedOrderedCorrectly {
struct CtorInitMultipleViews {
std::string_view view1; // expected-note {{this field dangles}}
std::string_view view2; // expected-note {{this field dangles}}
- CtorInitMultipleViews(std::string s) : view1(s), // expected-warning {{address of stack memory escapes to a field}}
- view2(s) {} // expected-warning {{address of stack memory escapes to a field}}
+ CtorInitMultipleViews(std::string s) : view1(s), // expected-warning {{parameter 's' escapes to a field and will dangle}}
+ view2(s) {} // expected-warning {{parameter 's' escapes to a field and will dangle}}
};
struct CtorInitMultipleParams {
std::string_view view1; // expected-note {{this field dangles}}
std::string_view view2; // expected-note {{this field dangles}}
- CtorInitMultipleParams(std::string s1, std::string s2) : view1(s1), // expected-warning {{address of stack memory escapes to a field}}
- view2(s2) {} // expected-warning {{address of stack memory escapes to a field}}
+ CtorInitMultipleParams(std::string s1, std::string s2) : view1(s1), // expected-warning {{parameter 's1' escapes to a field and will dangle}}
+ view2(s2) {} // expected-warning {{parameter 's2' escapes to a field and will dangle}}
};
struct CtorRefField {
const std::string& str; // expected-note {{this field dangles}}
const std::string_view& view; // expected-note {{this field dangles}}
- CtorRefField(std::string s, std::string_view v) : str(s), // expected-warning {{address of stack memory escapes to a field}}
- view(v) {} // expected-warning {{address of stack memory escapes to a field}}
+ CtorRefField(std::string s, std::string_view v) : str(s), // expected-warning {{parameter 's' escapes to a field and will dangle}}
+ view(v) {} // expected-warning {{parameter 'v' escapes to a field and will dangle}}
CtorRefField(Dummy<1> ok, const std::string& s, const std::string_view& v): str(s), view(v) {}
};
struct CtorPointerField {
const char* ptr; // expected-note {{this field dangles}}
- CtorPointerField(std::string s) : ptr(s.data()) {} // expected-warning {{address of stack memory escapes to a field}}
+ CtorPointerField(std::string s) : ptr(s.data()) {} // expected-warning {{parameter 's' escapes to a field and will dangle}}
CtorPointerField(Dummy<1> ok, const std::string& s) : ptr(s.data()) {}
CtorPointerField(Dummy<2> ok, std::string_view view) : ptr(view.data()) {}
};
@@ -81,13 +81,13 @@ struct MemberSetters {
const char* p; // expected-note 6 {{this field dangles}}
void setWithParam(std::string s) {
- view = s; // expected-warning {{address of stack memory escapes to a field}}
- p = s.data(); // expected-warning {{address of stack memory escapes to a field}}
+ view = s; // expected-warning {{parameter 's' escapes to a field and will dangle}}
+ p = s.data(); // expected-warning {{parameter 's' escapes to a field and will dangle}}
}
void setWithParamAndReturn(std::string s) {
- view = s; // expected-warning {{address of stack memory escapes to a field}}
- p = s.data(); // expected-warning {{address of stack memory escapes to a field}}
+ view = s; // expected-warning {{parameter 's' escapes to a field and will dangle}}
+ p = s.data(); // expected-warning {{parameter 's' escapes to a field and will dangle}}
return;
}
@@ -104,14 +104,14 @@ struct MemberSetters {
void setWithLocal() {
std::string s;
- view = s; // expected-warning {{address of stack memory escapes to a field}}
- p = s.data(); // expected-warning {{address of stack memory escapes to a field}}
+ view = s; // expected-warning {{local variable 's' escapes to a field and will dangle}}
+ p = s.data(); // expected-warning {{local variable 's' escapes to a field and will dangle}}
}
void setWithLocalButMoved() {
std::string s;
- view = s; // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
- p = s.data(); // expected-warning-re {{address of stack memory escapes to a field. {{.*}} may have been moved}}
+ view = s; // expected-warning-re {{local variable 's' may escape to a field and dangle. {{.*}} may have been moved}}
+ p = s.data(); // expected-warning-re {{local variable 's' may escape to a field and dangle. {{.*}} may have been moved}}
takeString(std::move(s)); // expected-note 2 {{potentially moved here}}
}
@@ -134,15 +134,15 @@ struct MemberSetters {
p = kGlobal.data();
std::string local;
- view = local; // expected-warning {{address of stack memory escapes to a field}}
- p = local.data(); // expected-warning {{address of stack memory escapes to a field}}
+ view = local; // expected-warning {{local variable 'local' escapes to a field and will dangle}}
+ p = local.data(); // expected-warning {{local variable 'local' escapes to a field and will dangle}}
}
void use_after_scope() {
{
std::string local;
- view = local; // expected-warning {{address of stack memory escapes to a field}}
- p = local.data(); // expected-warning {{address of stack memory escapes to a field}}
+ view = local; // expected-warning {{local variable 'local' escapes to a field and will dangle}}
+ p = local.data(); // expected-warning {{local variable 'local' escapes to a field and will dangle}}
}
(void)view;
(void)p;
@@ -193,7 +193,7 @@ struct BaseWithPointer {
struct DerivedWithCtor : BaseWithPointer {
DerivedWithCtor(std::string s) {
- view = s; // expected-warning {{address of stack memory escapes to a field}}
+ view = s; // expected-warning {{parameter 's' escapes to a field and will dangle}}
}
};
} // namespace DanglingPointerFieldInBaseClass
@@ -205,7 +205,7 @@ struct HasCallback {
void set_callback() {
int local;
- callback = [&local]() { (void)local; }; // expected-warning {{address of stack memory escapes to a field}}
+ callback = [&local]() { (void)local; }; // expected-warning {{local variable 'local' escapes to a field and will dangle}}
}
};
@@ -222,7 +222,7 @@ struct HasUniquePtrField {
std::unique_ptr<LifetimeBoundCtor> field; // tu-note {{this field dangles}}
void setWithParam(MyObj obj) {
- field = std::make_unique<LifetimeBoundCtor>(obj); // tu-warning {{address of stack memory escapes to a field}}
+ field = std::make_unique<LifetimeBoundCtor>(obj); // tu-warning {{parameter 'obj' escapes to a field and will dangle}}
}
};
} // namespace MakeUnique
diff --git a/clang/test/Sema/warn-lifetime-safety-dangling-global.cpp b/clang/test/Sema/warn-lifetime-safety-dangling-global.cpp
index ac40c7df5a8f5..f8d5481377e47 100644
--- a/clang/test/Sema/warn-lifetime-safety-dangling-global.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-dangling-global.cpp
@@ -22,14 +22,14 @@ void invoke_function_with_side_effects() {
// We can however catch the inlined one of course!
void inlined() {
int local;
- global = &local; // expected-warning {{address of stack memory escapes to global or static storage}}
+ global = &local; // expected-warning {{local variable 'local' escapes to global or static storage and will dangle}}
global_backup = global;
global = nullptr;
}
void store_local_in_global() {
int local;
- global = &local; // expected-warning {{address of stack memory escapes to global or static storage}}
+ global = &local; // expected-warning {{local variable 'local' escapes to global or static storage and will dangle}}
}
void store_then_clear() {
@@ -40,5 +40,5 @@ void store_then_clear() {
void dangling_static_field() {
int local;
- ObjWithStaticField::static_field = &local; // expected-warning {{address of stack memory escapes to global or static storage}}
+ ObjWithStaticField::static_field = &local; // expected-warning {{local variable 'local' escapes to global or static storage and will dangle}}
}
\ No newline at end of file
diff --git a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
index 24aacc9480533..32ef6e54d7f1c 100644
--- a/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-suggestions.cpp
@@ -665,7 +665,7 @@ struct HasSetterField {
}
void reset() {
MyObj obj;
- field = new LifetimeBoundCtor(obj); // expected-warning {{address of stack memory escapes to a field}}
+ field = new LifetimeBoundCtor(obj); // expected-warning {{local variable 'obj' escapes to a field and will dangle}}
}
};
diff --git a/clang/test/Sema/warn-lifetime-safety.cpp b/clang/test/Sema/warn-lifetime-safety.cpp
index d5f558c7918b3..ee453b7341369 100644
--- a/clang/test/Sema/warn-lifetime-safety.cpp
+++ b/clang/test/Sema/warn-lifetime-safety.cpp
@@ -2448,7 +2448,7 @@ void from_template_instantiation() {
struct FieldInitFromLifetimebound {
S value; // expected-note {{this field dangles}}
- FieldInitFromLifetimebound() : value(getS(std::string("temp"))) {} // expected-warning {{address of stack memory escapes to a field}}
+ FieldInitFromLifetimebound() : value(getS(std::string("temp"))) {} // expected-warning {{local temporary object escapes to a field and will dangle}}
};
S S::return_self_after_registration() const {
@@ -2638,12 +2638,12 @@ void nested_local_pointer() {
struct PFieldFromParam {
Pointer<Bar> value; // expected-note {{this field dangles}}
- PFieldFromParam(Bar bar) : value(bar) {} // expected-warning {{address of stack memory escapes to a field}}
+ PFieldFromParam(Bar bar) : value(bar) {} // expected-warning {{parameter 'bar' escapes to a field and will dangle}}
};
struct PFieldFromTemp {
Pointer<Bar> value; // expected-note {{this field dangles}}
- PFieldFromTemp() : value(Bar{}) {} // expected-warning {{address of stack memory escapes to a field}}
+ PFieldFromTemp() : value(Bar{}) {} // expected-warning {{local temporary object escapes to a field and will dangle}}
};
} // namespace gslpointer_construction_from_lifetimebound
@@ -3198,7 +3198,7 @@ struct S2 : S {
namespace CXXDefaultInitExprTests {
struct Holder {
- std::string_view view = std::string("temporary"); // expected-warning {{address of stack memory escapes to a field}} expected-note {{this field dangles}}
+ std::string_view view = std::string("temporary"); // expected-warning {{local temporary object escapes to a field and will dangle}} expected-note {{this field dangles}}
Holder() {}
};
} // namespace CXXDefaultInitExprTests
@@ -3210,7 +3210,7 @@ struct Y : X {
void bar() {
{
int a;
- x = &a; // expected-warning {{address of stack memory escapes to a field}}
+ x = &a; // expected-warning {{local variable 'a' escapes to a field and will dangle}}
}
(void)x;
}
More information about the cfe-commits
mailing list