Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------------------------------------

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this firing always? I thought this is only supposed to fire on Global and ThreadLocal variables according to the GCC patch summary. But this will fire any time there is a ConstructorAttr or DestructorAttr somewhere.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How I see it is:

From GCC's docs: "The constructor attribute causes the function to be called automatically before execution enters main ()."

And -Wglobal-constructors is meant to tell you "this has a constructor which will fire before main" which matters for global and thread local variables, so the constructor attribute is sort of morally equivalent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! I missed that about the constructor attribute. Thanks! Makes sense to me.

<< D->getSourceRange();

D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
}
Expand All @@ -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));
}
Expand Down
5 changes: 5 additions & 0 deletions clang/test/SemaCXX/attr-require-constant-initialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down