From 7ce5bb7ded0717d605030d0c2b2b5cd0f5ba1b53 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Wed, 5 Mar 2025 13:27:19 -0600 Subject: [PATCH 1/2] [Clang] Make '-Wglobal-constructors` work on the GNU attributes 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. --- clang/lib/Sema/SemaDeclAttr.cpp | 11 +++++++++++ .../SemaCXX/attr-require-constant-initialization.cpp | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 1405ee5341dcf..9843d981e7436 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -7727,6 +7727,17 @@ void Sema::ProcessDeclAttributeList( D->setInvalidDecl(); } + // Warn on global constructors and destructors created by attributes. + if (D->hasAttr() && + !getDiagnostics().isIgnored(diag::warn_global_constructor, + D->getLocation())) + Diag(D->getLocation(), diag::warn_global_constructor) + << D->getSourceRange(); + if (D->hasAttr() && + !getDiagnostics().isIgnored(diag::warn_global_destructor, + D->getLocation())) + Diag(D->getLocation(), diag::warn_global_destructor) << D->getSourceRange(); + // Do this check after processing D's attributes because the attribute // objc_method_family can change whether the given method is in the init // family, and it can be applied after objc_designated_initializer. This is a diff --git a/clang/test/SemaCXX/attr-require-constant-initialization.cpp b/clang/test/SemaCXX/attr-require-constant-initialization.cpp index 4c0a834551715..b1736ea7a3ebe 100644 --- a/clang/test/SemaCXX/attr-require-constant-initialization.cpp +++ b/clang/test/SemaCXX/attr-require-constant-initialization.cpp @@ -337,6 +337,11 @@ constexpr TestCtor inval_constexpr(42); // expected-error {{must be initia ATTR constexpr TestCtor 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 From ba90e46a5fb8578064408f969769aac3fe7bebd1 Mon Sep 17 00:00:00 2001 From: Joseph Huber Date: Thu, 6 Mar 2025 11:19:52 -0600 Subject: [PATCH 2/2] comments --- clang/docs/ReleaseNotes.rst | 3 +++ clang/lib/Sema/SemaDeclAttr.cpp | 14 +++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 86bf836b4a999..51642187f5bb4 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 -------------------------------------------------- diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 9843d981e7436..c1e97aa2dde5b 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -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)); } @@ -7727,17 +7730,6 @@ void Sema::ProcessDeclAttributeList( D->setInvalidDecl(); } - // Warn on global constructors and destructors created by attributes. - if (D->hasAttr() && - !getDiagnostics().isIgnored(diag::warn_global_constructor, - D->getLocation())) - Diag(D->getLocation(), diag::warn_global_constructor) - << D->getSourceRange(); - if (D->hasAttr() && - !getDiagnostics().isIgnored(diag::warn_global_destructor, - D->getLocation())) - Diag(D->getLocation(), diag::warn_global_destructor) << D->getSourceRange(); - // Do this check after processing D's attributes because the attribute // objc_method_family can change whether the given method is in the init // family, and it can be applied after objc_designated_initializer. This is a