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

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


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

user wants the warning silenced by the attribute
so no warning should be issued.

>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] 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
 }
+



More information about the cfe-commits mailing list