Skip to content

Commit 59e0525

Browse files
authored
[[clang::always_destroy]] attribute silences warn-exit-time-destructor (#86486)
This attribute tells the compiler that the variable must have its exit-time destructor run, so it makes sense that it would silence the warning telling users that an exit-time destructor is required. Fixes #68686
1 parent 19ca79e commit 59e0525

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ Improvements to Clang's diagnostics
295295
- Clang now correctly diagnoses no arguments to a variadic macro parameter as a C23/C++20 extension.
296296
Fixes #GH84495.
297297

298+
- Clang no longer emits a ``-Wexit-time destructors`` warning on static variables explicitly
299+
annotated with the ``clang::always_destroy`` attribute.
300+
Fixes #GH68686, #GH86486
301+
298302
Improvements to Clang's time-trace
299303
----------------------------------
300304

clang/include/clang/Basic/AttrDocs.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6069,6 +6069,9 @@ def AlwaysDestroyDocs : Documentation {
60696069
The ``always_destroy`` attribute specifies that a variable with static or thread
60706070
storage duration should have its exit-time destructor run. This attribute is the
60716071
default unless clang was invoked with -fno-c++-static-destructors.
6072+
6073+
If a variable is explicitly declared with this attribute, Clang will silence
6074+
otherwise applicable ``-Wexit-time-destructors`` warnings.
60726075
}];
60736076
}
60746077

clang/lib/Sema/SemaDeclCXX.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16202,7 +16202,8 @@ void Sema::FinalizeVarWithDestructor(VarDecl *VD, const RecordType *Record) {
1620216202

1620316203
// Emit warning for non-trivial dtor in global scope (a real global,
1620416204
// class-static, function-static).
16205-
Diag(VD->getLocation(), diag::warn_exit_time_destructor);
16205+
if (!VD->hasAttr<AlwaysDestroyAttr>())
16206+
Diag(VD->getLocation(), diag::warn_exit_time_destructor);
1620616207

1620716208
// TODO: this should be re-enabled for static locals by !CXAAtExit
1620816209
if (!VD->isStaticLocal())

clang/test/SemaCXX/warn-exit-time-destructors.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ struct A { ~A(); };
5151
}
5252

5353
namespace test5 {
54+
struct A { ~A(); };
55+
[[clang::always_destroy]] A a; // no warning
56+
57+
void func() {
58+
[[clang::always_destroy]] static A a; // no warning
59+
}
60+
}
61+
62+
namespace test6 {
5463
#if __cplusplus >= 202002L
5564
#define CPP20_CONSTEXPR constexpr
5665
#else
@@ -68,3 +77,4 @@ namespace test5 {
6877
T t; // expected-warning {{exit-time destructor}}
6978
#undef CPP20_CONSTEXPR
7079
}
80+

0 commit comments

Comments
 (0)