1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify 2f4a2713aSLionel Sambuc 3f4a2713aSLionel Sambuc int opaque_int(); 4f4a2713aSLionel Sambuc 5f4a2713aSLionel Sambuc namespace test0 { 6f4a2713aSLionel Sambuc // These should never require global constructors. 7f4a2713aSLionel Sambuc int a; 8f4a2713aSLionel Sambuc int b = 20; 9f4a2713aSLionel Sambuc float c = 5.0f; 10f4a2713aSLionel Sambuc 11f4a2713aSLionel Sambuc // This global constructor is avoidable based on initialization order. 12f4a2713aSLionel Sambuc int d = b; // expected-warning {{global constructor}} 13f4a2713aSLionel Sambuc 14f4a2713aSLionel Sambuc // These global constructors are unavoidable. 15f4a2713aSLionel Sambuc int e = opaque_int(); // expected-warning {{global constructor}} 16f4a2713aSLionel Sambuc int f = b; // expected-warning {{global constructor}} 17f4a2713aSLionel Sambuc } 18f4a2713aSLionel Sambuc 19f4a2713aSLionel Sambuc namespace test1 { 20f4a2713aSLionel Sambuc struct A { int x; }; 21f4a2713aSLionel Sambuc A a; 22f4a2713aSLionel Sambuc A b = A(); 23f4a2713aSLionel Sambuc A c = { 10 }; 24f4a2713aSLionel Sambuc A d = { opaque_int() }; // expected-warning {{global constructor}} 25f4a2713aSLionel Sambuc A e = A(A()); 26f4a2713aSLionel Sambuc A f = A(a); // expected-warning {{global constructor}} 27f4a2713aSLionel Sambuc A g(a); // expected-warning {{global constructor}} 28f4a2713aSLionel Sambuc A h((A())); // elided 29f4a2713aSLionel Sambuc A i((A(A()))); // elided 30f4a2713aSLionel Sambuc } 31f4a2713aSLionel Sambuc 32f4a2713aSLionel Sambuc namespace test2 { 33f4a2713aSLionel Sambuc struct A { A(); }; 34f4a2713aSLionel Sambuc A a; // expected-warning {{global constructor}} 35f4a2713aSLionel Sambuc A b[10]; // expected-warning {{global constructor}} 36f4a2713aSLionel Sambuc A c[10][10]; // expected-warning {{global constructor}} 37f4a2713aSLionel Sambuc 38f4a2713aSLionel Sambuc A &d = a; 39f4a2713aSLionel Sambuc A &e = b[5]; 40f4a2713aSLionel Sambuc A &f = c[5][7]; 41f4a2713aSLionel Sambuc } 42f4a2713aSLionel Sambuc 43f4a2713aSLionel Sambuc namespace test3 { 44f4a2713aSLionel Sambuc struct A { ~A(); }; 45f4a2713aSLionel Sambuc A a; // expected-warning {{global destructor}} 46f4a2713aSLionel Sambuc A b[10]; // expected-warning {{global destructor}} 47f4a2713aSLionel Sambuc A c[10][10]; // expected-warning {{global destructor}} 48f4a2713aSLionel Sambuc 49f4a2713aSLionel Sambuc A &d = a; 50f4a2713aSLionel Sambuc A &e = b[5]; 51f4a2713aSLionel Sambuc A &f = c[5][7]; 52f4a2713aSLionel Sambuc } 53f4a2713aSLionel Sambuc 54f4a2713aSLionel Sambuc namespace test4 { 55f4a2713aSLionel Sambuc char a[] = "hello"; 56f4a2713aSLionel Sambuc char b[6] = "hello"; 57f4a2713aSLionel Sambuc char c[][6] = { "hello" }; 58f4a2713aSLionel Sambuc } 59f4a2713aSLionel Sambuc 60f4a2713aSLionel Sambuc namespace test5 { 61f4a2713aSLionel Sambuc struct A { A(); }; 62f4a2713aSLionel Sambuc f1()63f4a2713aSLionel Sambuc void f1() { 64f4a2713aSLionel Sambuc static A a; 65f4a2713aSLionel Sambuc } f2()66f4a2713aSLionel Sambuc void f2() { 67f4a2713aSLionel Sambuc static A& a = *new A; 68f4a2713aSLionel Sambuc } 69f4a2713aSLionel Sambuc } 70f4a2713aSLionel Sambuc 71f4a2713aSLionel Sambuc namespace test6 { 72f4a2713aSLionel Sambuc struct A { ~A(); }; 73f4a2713aSLionel Sambuc f1()74f4a2713aSLionel Sambuc void f1() { 75f4a2713aSLionel Sambuc static A a; 76f4a2713aSLionel Sambuc } f2()77f4a2713aSLionel Sambuc void f2() { 78f4a2713aSLionel Sambuc static A& a = *new A; 79f4a2713aSLionel Sambuc } 80f4a2713aSLionel Sambuc } 81f4a2713aSLionel Sambuc 82f4a2713aSLionel Sambuc namespace pr8095 { 83f4a2713aSLionel Sambuc struct Foo { 84f4a2713aSLionel Sambuc int x; Foopr8095::Foo85f4a2713aSLionel Sambuc Foo(int x1) : x(x1) {} 86f4a2713aSLionel Sambuc }; foo()87f4a2713aSLionel Sambuc void foo() { 88f4a2713aSLionel Sambuc static Foo a(0); 89f4a2713aSLionel Sambuc } 90f4a2713aSLionel Sambuc 91f4a2713aSLionel Sambuc struct Bar { 92f4a2713aSLionel Sambuc ~Bar(); 93f4a2713aSLionel Sambuc }; bar()94f4a2713aSLionel Sambuc void bar() { 95f4a2713aSLionel Sambuc static Bar b; 96f4a2713aSLionel Sambuc } 97f4a2713aSLionel Sambuc } 98f4a2713aSLionel Sambuc 99f4a2713aSLionel Sambuc namespace referencemember { 100f4a2713aSLionel Sambuc struct A { int &a; }; 101f4a2713aSLionel Sambuc int a; 102f4a2713aSLionel Sambuc A b = { a }; 103f4a2713aSLionel Sambuc } 104*0a6a1f1dSLionel Sambuc 105*0a6a1f1dSLionel Sambuc namespace pr19253 { 106*0a6a1f1dSLionel Sambuc struct A { ~A() = default; }; 107*0a6a1f1dSLionel Sambuc A a; 108*0a6a1f1dSLionel Sambuc 109*0a6a1f1dSLionel Sambuc struct B { ~B(); }; 110*0a6a1f1dSLionel Sambuc struct C : B { ~C() = default; }; 111*0a6a1f1dSLionel Sambuc C c; // expected-warning {{global destructor}} 112*0a6a1f1dSLionel Sambuc 113*0a6a1f1dSLionel Sambuc class D { 114*0a6a1f1dSLionel Sambuc friend struct E; 115*0a6a1f1dSLionel Sambuc ~D() = default; 116*0a6a1f1dSLionel Sambuc }; 117*0a6a1f1dSLionel Sambuc struct E : D { 118*0a6a1f1dSLionel Sambuc D d; 119*0a6a1f1dSLionel Sambuc ~E() = default; 120*0a6a1f1dSLionel Sambuc }; 121*0a6a1f1dSLionel Sambuc E e; 122*0a6a1f1dSLionel Sambuc } 123*0a6a1f1dSLionel Sambuc 124*0a6a1f1dSLionel Sambuc namespace pr20420 { 125*0a6a1f1dSLionel Sambuc // No warning is expected. This used to crash. 126*0a6a1f1dSLionel Sambuc void *array_storage[1]; 127*0a6a1f1dSLionel Sambuc const int &global_reference = *(int *)array_storage; 128*0a6a1f1dSLionel Sambuc } 129