Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -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 a ``-Wexit-time destructors`` warning on static variables explicitly
annotated with the ``clang::always_destroy`` attribute.
Fixes #GH68686, #GH86486

Improvements to Clang's time-trace
----------------------------------

Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Expand Up @@ -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 explicitly declared with this attribute, Clang will silence
otherwise applicable ``-Wexit-time-destructors`` warnings.
}];
}

Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDeclCXX.cpp
Expand Up @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions clang/test/SemaCXX/warn-exit-time-destructors.cpp
Expand Up @@ -51,6 +51,15 @@ struct A { ~A(); };
}

namespace test5 {
struct A { ~A(); };
[[clang::always_destroy]] A a; // no warning

void func() {
[[clang::always_destroy]] static A a; // no warning
}
}

namespace test6 {
#if __cplusplus >= 202002L
#define CPP20_CONSTEXPR constexpr
#else
Expand All @@ -68,3 +77,4 @@ namespace test5 {
T t; // expected-warning {{exit-time destructor}}
#undef CPP20_CONSTEXPR
}