[clang] [clang] Don't emit the warn_dangling_lifetime_pointer diagnostic for the assignment case. (PR #97408)
Haojian Wu via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 2 06:12:45 PDT 2024
https://github.com/hokein updated https://github.com/llvm/llvm-project/pull/97408
>From d1ea90918702fec85714bdd9caa68942eb4a4218 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 2 Jul 2024 14:12:36 +0200
Subject: [PATCH 1/3] [clang] Don't emit the warn_dangling_lifetime_pointer
diagnostic for the assignment case.
The lifetime_pointer case is handled before the assignment case. In some
scenario where we have the gsl::Pointer attr, we will end up with
emitting the `-Wdangling-gsl` warning for the assignment case. This
means we can not use `-Wno-dangling-assignment` to suppress the
newly-added warning.
---
clang/lib/Sema/CheckExprLifetime.cpp | 2 +-
.../test/SemaCXX/suppress-dangling-assignment.cpp | 15 +++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
create mode 100644 clang/test/SemaCXX/suppress-dangling-assignment.cpp
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index fea0239f7bc36..03bf87a7cf5e6 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1023,7 +1023,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return false;
}
- if (IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) {
+ if (InitEntity && IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) {
SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer)
<< DiagRange;
return false;
diff --git a/clang/test/SemaCXX/suppress-dangling-assignment.cpp b/clang/test/SemaCXX/suppress-dangling-assignment.cpp
new file mode 100644
index 0000000000000..11625d3272b83
--- /dev/null
+++ b/clang/test/SemaCXX/suppress-dangling-assignment.cpp
@@ -0,0 +1,15 @@
+
+// RUN: %clang_cc1 -std=c++20 -verify -Wno-dangling-assignment %s
+// expected-no-diagnostics
+
+namespace std {
+// std::basic_string has a hard-coded gsl::owner attr.
+struct basic_string {
+ const char* c_str();
+};
+} // namespace std
+
+void test(const char* a) {
+ // verify that the dangling-assignment diagnostic are suppressed.
+ a = std::basic_string().c_str();
+}
>From 95233e150d4344411d41539c57a3e921d621ea0b Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 2 Jul 2024 14:35:47 +0200
Subject: [PATCH 2/3] Refine the test, verifing that we emit the
"-Wdangling-assignment" warning.
---
.../test/SemaCXX/suppress-dangling-assignment.cpp | 15 ---------------
clang/test/SemaCXX/warn-dangling-local.cpp | 11 +++++++++++
2 files changed, 11 insertions(+), 15 deletions(-)
delete mode 100644 clang/test/SemaCXX/suppress-dangling-assignment.cpp
diff --git a/clang/test/SemaCXX/suppress-dangling-assignment.cpp b/clang/test/SemaCXX/suppress-dangling-assignment.cpp
deleted file mode 100644
index 11625d3272b83..0000000000000
--- a/clang/test/SemaCXX/suppress-dangling-assignment.cpp
+++ /dev/null
@@ -1,15 +0,0 @@
-
-// RUN: %clang_cc1 -std=c++20 -verify -Wno-dangling-assignment %s
-// expected-no-diagnostics
-
-namespace std {
-// std::basic_string has a hard-coded gsl::owner attr.
-struct basic_string {
- const char* c_str();
-};
-} // namespace std
-
-void test(const char* a) {
- // verify that the dangling-assignment diagnostic are suppressed.
- a = std::basic_string().c_str();
-}
diff --git a/clang/test/SemaCXX/warn-dangling-local.cpp b/clang/test/SemaCXX/warn-dangling-local.cpp
index a946b8a241a38..2808a4c01f88d 100644
--- a/clang/test/SemaCXX/warn-dangling-local.cpp
+++ b/clang/test/SemaCXX/warn-dangling-local.cpp
@@ -26,3 +26,14 @@ void g() {
const int a[] = {a[0]};
const int b[] = {a[0]};
}
+
+namespace std {
+// std::basic_string has a hard-coded gsl::owner attr.
+struct basic_string {
+ const char* c_str();
+};
+} // namespace std
+void test(const char* a) {
+ // verify we're emitting the `-Wdangling-assignment` warning.
+ a = std::basic_string().c_str(); // expected-warning {{object backing the pointer a will be destroyed at the end of the full-expression}}
+}
>From 9684db804e397e8615d8d1f1c09397f4e0c1ac20 Mon Sep 17 00:00:00 2001
From: Haojian Wu <hokein.wu at gmail.com>
Date: Tue, 2 Jul 2024 15:12:10 +0200
Subject: [PATCH 3/3] address review comment, restructure the code.
---
clang/lib/Sema/CheckExprLifetime.cpp | 35 ++++++++++++++--------------
1 file changed, 17 insertions(+), 18 deletions(-)
diff --git a/clang/lib/Sema/CheckExprLifetime.cpp b/clang/lib/Sema/CheckExprLifetime.cpp
index 03bf87a7cf5e6..2f9ef28da2c3e 100644
--- a/clang/lib/Sema/CheckExprLifetime.cpp
+++ b/clang/lib/Sema/CheckExprLifetime.cpp
@@ -1008,7 +1008,19 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return true;
}
}
+ if (AEntity) {
+ if (!MTE)
+ return false;
+ assert(shouldLifetimeExtendThroughPath(Path) ==
+ PathLifetimeKind::NoExtend &&
+ "No lifetime extension for assignments");
+ if (!pathContainsInit(Path))
+ SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
+ << AEntity->LHS << DiagRange;
+ return false;
+ }
+ assert(InitEntity && "only for initialization");
switch (LK) {
case LK_FullExpression:
llvm_unreachable("already handled this");
@@ -1023,7 +1035,7 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return false;
}
- if (InitEntity && IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) {
+ if (IsGslPtrInitWithGslTempOwner && DiagLoc.isValid()) {
SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer)
<< DiagRange;
return false;
@@ -1031,8 +1043,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
switch (shouldLifetimeExtendThroughPath(Path)) {
case PathLifetimeKind::Extend:
- assert(InitEntity && "Lifetime extension should happen only for "
- "initialization and not assignment");
// Update the storage duration of the materialized temporary.
// FIXME: Rebuild the expression instead of mutating it.
MTE->setExtendingDecl(ExtendingEntity->getDecl(),
@@ -1041,8 +1051,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
return true;
case PathLifetimeKind::ShouldExtend:
- assert(InitEntity && "Lifetime extension should happen only for "
- "initialization and not assignment");
// We're supposed to lifetime-extend the temporary along this path (per
// the resolution of DR1815), but we don't support that yet.
//
@@ -1060,23 +1068,16 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
if (pathContainsInit(Path))
return false;
- if (InitEntity) {
- SemaRef.Diag(DiagLoc, diag::warn_dangling_variable)
- << RK << !InitEntity->getParent()
- << ExtendingEntity->getDecl()->isImplicit()
- << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange;
- } else {
- SemaRef.Diag(DiagLoc, diag::warn_dangling_pointer_assignment)
- << AEntity->LHS << DiagRange;
- }
+ SemaRef.Diag(DiagLoc, diag::warn_dangling_variable)
+ << RK << !InitEntity->getParent()
+ << ExtendingEntity->getDecl()->isImplicit()
+ << ExtendingEntity->getDecl() << Init->isGLValue() << DiagRange;
break;
}
break;
}
case LK_MemInitializer: {
- assert(InitEntity && "Expect only on initializing the entity");
-
if (MTE) {
// Under C++ DR1696, if a mem-initializer (or a default member
// initializer used by the absence of one) would lifetime-extend a
@@ -1151,7 +1152,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
}
case LK_New:
- assert(InitEntity && "Expect only on initializing the entity");
if (isa<MaterializeTemporaryExpr>(L)) {
if (IsGslPtrInitWithGslTempOwner)
SemaRef.Diag(DiagLoc, diag::warn_dangling_lifetime_pointer)
@@ -1169,7 +1169,6 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
case LK_Return:
case LK_StmtExprResult:
- assert(InitEntity && "Expect only on initializing the entity");
if (auto *DRE = dyn_cast<DeclRefExpr>(L)) {
// We can't determine if the local variable outlives the statement
// expression.
More information about the cfe-commits
mailing list