[clang] [[clang::always_destroy]] attribute silences warn-exit-time-destructor (PR #86486)

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 25 11:06:03 PDT 2024


https://github.com/ycdtosa updated https://github.com/llvm/llvm-project/pull/86486

>From 016152bc21c2625db77d3e31a7123bcf08114133 Mon Sep 17 00:00:00 2001
From: ycdtosa <ycdtosa at gmail.com>
Date: Mon, 25 Mar 2024 10:28:02 +0000
Subject: [PATCH 1/3] add test for [[clang::always-destroy]] attribute

user wants the warning silenced by the attribute
so no warning should be issued
---
 clang/lib/Sema/SemaDeclCXX.cpp                    | 3 ++-
 clang/test/SemaCXX/warn-exit-time-destructors.cpp | 6 ++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index ee732679417e37..7070ea00cff275 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -16202,7 +16202,8 @@ void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) {
 
   // Emit warning for non-trivial dtor in global scope (a real global,
   // class-static, function-static).
-  Diag(VD->getLocation(), diag::warn_exit_time_destructor);
+  if (!VD->hasAttr<AlwaysDestroyAttr>())
+    Diag(VD->getLocation(), diag::warn_exit_time_destructor);
 
   // TODO: this should be re-enabled for static locals by !CXAAtExit
   if (!VD->isStaticLocal())
diff --git a/clang/test/SemaCXX/warn-exit-time-destructors.cpp b/clang/test/SemaCXX/warn-exit-time-destructors.cpp
index 2f14243cb48c47..8bd798c036333e 100644
--- a/clang/test/SemaCXX/warn-exit-time-destructors.cpp
+++ b/clang/test/SemaCXX/warn-exit-time-destructors.cpp
@@ -51,6 +51,11 @@ struct A { ~A(); };
 }
 
 namespace test5 {
+struct A { ~A(); };
+[[clang::always_destroy]] A a; // no warning
+}
+
+namespace test6 {
 #if __cplusplus >= 202002L
 #define CPP20_CONSTEXPR constexpr
 #else
@@ -68,3 +73,4 @@ namespace test5 {
   T t; // expected-warning {{exit-time destructor}}
 #undef CPP20_CONSTEXPR
 }
+

>From b9492928df4e9864b664dc99e34d372215e2fab9 Mon Sep 17 00:00:00 2001
From: ycdtosa <ycdtosa at gmail.com>
Date: Mon, 25 Mar 2024 17:53:51 +0000
Subject: [PATCH 2/3] add test for static variable inside a function

---
 clang/test/SemaCXX/warn-exit-time-destructors.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/clang/test/SemaCXX/warn-exit-time-destructors.cpp b/clang/test/SemaCXX/warn-exit-time-destructors.cpp
index 8bd798c036333e..55ae37d7368f88 100644
--- a/clang/test/SemaCXX/warn-exit-time-destructors.cpp
+++ b/clang/test/SemaCXX/warn-exit-time-destructors.cpp
@@ -51,8 +51,12 @@ struct A { ~A(); };
 }
 
 namespace test5 {
-struct A { ~A(); };
-[[clang::always_destroy]] A a; // no warning
+  struct A { ~A(); };
+  [[clang::always_destroy]] A a; // no warning
+
+  void func() {
+    [[clang::always_destroy]] static A a; // no warning
+  }
 }
 
 namespace test6 {

>From 127abc398d4ed059d761e4b70f0c9d14a8ac1c29 Mon Sep 17 00:00:00 2001
From: ycdtosa <ycdtosa at gmail.com>
Date: Mon, 25 Mar 2024 18:05:26 +0000
Subject: [PATCH 3/3] update documentation

---
 clang/docs/ReleaseNotes.rst           | 4 ++++
 clang/include/clang/Basic/AttrDocs.td | 3 +++
 2 files changed, 7 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8054d90fc70f93..46e135123aada9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -289,6 +289,10 @@ Improvements to Clang's diagnostics
 - Clang now correctly diagnoses no arguments to a variadic macro parameter as a C23/C++20 extension.
   Fixes #GH84495.
 
+- Clang no longer emits the ``exit-time destructor`` warning on static variables explicitly 
+  annotated with the ``clang::always_destroy`` attribute. 
+  Fixes #GH68686, #GH86486
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td
index 9de14f608fd114..968f620dc3738c 100644
--- a/clang/include/clang/Basic/AttrDocs.td
+++ b/clang/include/clang/Basic/AttrDocs.td
@@ -6069,6 +6069,9 @@ def AlwaysDestroyDocs : Documentation {
 The ``always_destroy`` attribute specifies that a variable with static or thread
 storage duration should have its exit-time destructor run. This attribute is the
 default unless clang was invoked with -fno-c++-static-destructors.
+
+If a variable is explicity declared with this attribute clang will silence the
+exit-time destructor warning.
   }];
 }
 



More information about the cfe-commits mailing list