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 while emitting exit-time-destructors diagnostic #68686

Closed
ycdtosa opened this issue Oct 10, 2023 · 5 comments · Fixed by #86486
Closed

[[clang::always_destroy]] attribute while emitting exit-time-destructors diagnostic #68686

ycdtosa opened this issue Oct 10, 2023 · 5 comments · Fixed by #86486
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@ycdtosa
Copy link
Contributor

ycdtosa commented Oct 10, 2023

Compiling this code with the -Wexit-time-desctuctors diagnostic enabled will raise the warning, for good reason.

static Foo ResourceHandler;

here is the output

[build] /somefile.cpp:X:14: warning: declaration requires an exit-time destructor [-Wexit-time-destructors]
[build]   static Foo ResourceHandler;
[build]              ^

However, being happy with the destructor call on exit time, not only do I want to silence the warning but to flag the variable accordingly. For that I add the [[clang::always_destroy]] attribute to the static variable.

[[clang::always_destroy]]
static Foo ResourceHandler;

This is meant to tell the compiler that I am aware of the exit time destructor and willing to pay the price, and properly release the resource.

The [[always_destroy]] attibute documentation states that it

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.

I believe adding explicitly the attribute should be enough to stop the compiler from emitting the diagnostic, but it is not the case.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Oct 10, 2023
@EugeneZelenko EugeneZelenko added clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed clang Clang issues not falling into any other category labels Oct 10, 2023
@shafik
Copy link
Collaborator

shafik commented Oct 10, 2023

I think we just need to add a check for hasAttr<AlwaysDestroyAttr>() to here:

// Emit warning for non-trivial dtor in global scope (a real global,
// class-static, function-static).
Diag(VD->getLocation(), diag::warn_exit_time_destructor);

wdyt @AaronBallman

@AaronBallman
Copy link
Collaborator

Yes, I think that should do it, however, it's worth noting that there's some confusion here with our diagnostics: https://godbolt.org/z/nsMhGrxGq

I think that attribute should silence both diagnostics, but... why do we have two diagnostics to tell the user the same information?

@ycdtosa
Copy link
Contributor Author

ycdtosa commented Oct 12, 2023

@AaronBallman. There is not much about [global constructors] (https://clang.llvm.org/docs/DiagnosticsReference.html#wglobal-constructors) diagnostics in the Diagnostics Reference at the moment.

@ycdtosa
Copy link
Contributor Author

ycdtosa commented Oct 12, 2023

Also clang/test/SemaCXX/no_destroy.cpp should be updated.

@AaronBallman
Copy link
Collaborator

@AaronBallman. There is not much about [global constructors] (https://clang.llvm.org/docs/DiagnosticsReference.html#wglobal-constructors) diagnostics in the Diagnostics Reference at the moment.

Yeah, it seems this feature was added long ago: 47e40931
And the exit-time destructor was added not long after: 98766db

and there's no review or explanation as to why we wanted to issue the same diagnostic twice here. :-/

AaronBallman pushed a commit that referenced this issue Mar 26, 2024
#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants