[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang / test / SemaCXX / warn-global-constructors.cpp
blob430f239a3ed7e1274e06729ff3d53a8c8f5804bc
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify
3 int opaque_int();
5 namespace test0 {
6 // These should never require global constructors.
7 int a;
8 int b = 20;
9 float c = 5.0f;
11 // This global constructor is avoidable based on initialization order.
12 int d = b; // expected-warning {{global constructor}}
14 // These global constructors are unavoidable.
15 int e = opaque_int(); // expected-warning {{global constructor}}
16 int f = b; // expected-warning {{global constructor}}
19 namespace test1 {
20 struct A { int x; };
21 A a;
22 A b = A();
23 A c = { 10 };
24 A d = { opaque_int() }; // expected-warning {{global constructor}}
25 A e = A(A());
26 A f = A(a); // expected-warning {{global constructor}}
27 A g(a); // expected-warning {{global constructor}}
28 A h((A())); // elided
29 A i((A(A()))); // elided
32 namespace test2 {
33 struct A { A(); };
34 A a; // expected-warning {{global constructor}}
35 A b[10]; // expected-warning {{global constructor}}
36 A c[10][10]; // expected-warning {{global constructor}}
38 A &d = a;
39 A &e = b[5];
40 A &f = c[5][7];
43 namespace test3 {
44 struct A { ~A(); };
45 A a; // expected-warning {{global destructor}}
46 A b[10]; // expected-warning {{global destructor}}
47 A c[10][10]; // expected-warning {{global destructor}}
49 A &d = a;
50 A &e = b[5];
51 A &f = c[5][7];
54 namespace test4 {
55 char a[] = "hello";
56 char b[6] = "hello";
57 char c[][6] = { "hello" };
60 namespace test5 {
61 struct A { A(); };
63 void f1() {
64 static A a;
66 void f2() {
67 static A& a = *new A;
71 namespace test6 {
72 struct A { ~A(); };
74 void f1() {
75 static A a;
77 void f2() {
78 static A& a = *new A;
82 namespace pr8095 {
83 struct Foo {
84 int x;
85 Foo(int x1) : x(x1) {}
87 void foo() {
88 static Foo a(0);
91 struct Bar {
92 ~Bar();
94 void bar() {
95 static Bar b;
99 namespace referencemember {
100 struct A { int &a; };
101 int a;
102 A b = { a };
105 namespace pr19253 {
106 struct A { ~A() = default; };
107 A a;
109 struct B { ~B(); };
110 struct C : B { ~C() = default; };
111 C c; // expected-warning {{global destructor}}
113 class D {
114 friend struct E;
115 ~D() = default;
117 struct E : D {
118 D d;
119 ~E() = default;
121 E e;
124 namespace pr20420 {
125 // No warning is expected. This used to crash.
126 void *array_storage[1];
127 const int &global_reference = *(int *)array_storage;
130 namespace bitfields {
131 struct HasUnnamedBitfield {
132 unsigned a;
133 unsigned : 20;
134 unsigned b;
136 constexpr HasUnnamedBitfield() : a(), b() {}
137 constexpr HasUnnamedBitfield(unsigned a, unsigned b) : a(a), b(b) {}
138 explicit HasUnnamedBitfield(unsigned a) {}
141 const HasUnnamedBitfield zeroConst{};
142 HasUnnamedBitfield zeroMutable{};
143 const HasUnnamedBitfield explicitConst{1, 2};
144 HasUnnamedBitfield explicitMutable{1, 2};
145 const HasUnnamedBitfield nonConstexprConst{1}; // expected-warning {{global constructor}}
146 HasUnnamedBitfield nonConstexprMutable{1}; // expected-warning {{global constructor}}