1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify=expected,cxx11
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wglobal-constructors %s -verify=expected
7 // These should never require global constructors.
12 // This global constructor is avoidable based on initialization order.
13 int d
= b
; // expected-warning {{global constructor}}
15 // These global constructors are unavoidable.
16 int e
= opaque_int(); // expected-warning {{global constructor}}
17 int f
= b
; // expected-warning {{global constructor}}
25 A d
= { opaque_int() }; // expected-warning {{global constructor}}
27 A f
= A(a
); // expected-warning {{global constructor}}
28 A
g(a
); // expected-warning {{global constructor}}
30 A
i((A(A()))); // elided
35 A a
; // expected-warning {{global constructor}}
36 A b
[10]; // expected-warning {{global constructor}}
37 A c
[10][10]; // expected-warning {{global constructor}}
46 A a
; // expected-warning {{global destructor}}
47 A b
[10]; // expected-warning {{global destructor}}
48 A c
[10][10]; // expected-warning {{global destructor}}
58 char c
[][6] = { "hello" };
86 Foo(int x1
) : x(x1
) {}
100 namespace referencemember
{
101 struct A
{ int &a
; };
107 struct A
{ ~A() = default; };
111 struct C
: B
{ ~C() = default; };
112 C c
; // expected-warning {{global destructor}}
126 // No warning is expected. This used to crash.
127 void *array_storage
[1];
128 const int &global_reference
= *(int *)array_storage
;
131 namespace bitfields
{
132 struct HasUnnamedBitfield
{
137 constexpr HasUnnamedBitfield() : a(), b() {}
138 constexpr HasUnnamedBitfield(unsigned a
, unsigned b
) : a(a
), b(b
) {}
139 explicit HasUnnamedBitfield(unsigned a
) {}
142 const HasUnnamedBitfield zeroConst
{};
143 HasUnnamedBitfield zeroMutable
{};
144 const HasUnnamedBitfield explicitConst
{1, 2};
145 HasUnnamedBitfield explicitMutable
{1, 2};
146 const HasUnnamedBitfield nonConstexprConst
{1}; // expected-warning {{global constructor}}
147 HasUnnamedBitfield nonConstexprMutable
{1}; // expected-warning {{global constructor}}
151 #if __cplusplus >= 202002L
152 #define CPP20_CONSTEXPR constexpr
154 #define CPP20_CONSTEXPR
157 CPP20_CONSTEXPR
~S() {}
159 S s
; // cxx11-warning {{global destructor}}
162 CPP20_CONSTEXPR
~T() { if (b
) {} }
165 T t
; // expected-warning {{global destructor}}
166 #undef CPP20_CONSTEXPR