[Clang] Make '-Wglobal-constructors` work on the GNU attributes (#129917)

Summary:
The `-Wglobal-constructors` option is useful for restricting the usage
of global constructors / destructors. However, it currently ignores the
attributes that introduce global constructors, meaning that the module
can still have ctors if `-Werror` is set. If this is intentional by the
user, I believe it would be more correct to push the diagnostic.
This commit is contained in:
Joseph Huber 2025-03-06 20:43:42 -06:00 committed by GitHub
parent f5eeeec4d3
commit d5cef39d74
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 11 additions and 0 deletions

View File

@ -62,6 +62,9 @@ AST Dumping Potentially Breaking Changes
Clang Frontend Potentially Breaking Changes
-------------------------------------------
- The ``-Wglobal-constructors`` flag now applies to ``[[gnu::constructor]]`` and
``[[gnu::destructor]]`` attributes.
Clang Python Bindings Potentially Breaking Changes
--------------------------------------------------

View File

@ -2138,6 +2138,8 @@ static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
if (AL.getNumArgs() &&
!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority))
return;
S.Diag(D->getLocation(), diag::warn_global_constructor)
<< D->getSourceRange();
D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
}
@ -2147,6 +2149,7 @@ static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
if (AL.getNumArgs() &&
!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority))
return;
S.Diag(D->getLocation(), diag::warn_global_destructor) << D->getSourceRange();
D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
}

View File

@ -337,6 +337,11 @@ constexpr TestCtor<NotC> inval_constexpr(42); // expected-error {{must be initia
ATTR constexpr TestCtor<NotC> inval_constexpr2(42); // expected-error {{must be initialized by a constant expression}}
// expected-note@-1 {{in call to 'TestCtor(42)'}}
[[gnu::constructor]] void ctor() {}
// expected-warning@-1 {{declaration requires a global constructor}}
[[gnu::destructor]] void dtor() {}
// expected-warning@-1 {{declaration requires a global destructor}}
#elif defined(TEST_THREE)
#if defined(__cplusplus)
#error This test requires C