Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 1 | // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wglobal-constructors %s -verify |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 2 | |
| 3 | int opaque_int(); |
| 4 | |
| 5 | namespace test0 { |
| 6 | // These should never require global constructors. |
| 7 | int a; |
| 8 | int b = 20; |
| 9 | float c = 5.0f; |
| 10 | |
| 11 | // This global constructor is avoidable based on initialization order. |
| 12 | int d = b; // expected-warning {{global constructor}} |
| 13 | |
| 14 | // These global constructors are unavoidable. |
| 15 | int e = opaque_int(); // expected-warning {{global constructor}} |
| 16 | int f = b; // expected-warning {{global constructor}} |
| 17 | } |
| 18 | |
| 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}} |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 25 | A e = A(A()); |
| 26 | A f = A(a); // expected-warning {{global constructor}} |
| 27 | A g(a); // expected-warning {{global constructor}} |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 28 | A h((A())); // elided |
| 29 | A i((A(A()))); // elided |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 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}} |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 37 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 38 | A &d = a; |
| 39 | A &e = b[5]; |
| 40 | A &f = c[5][7]; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 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}} |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 48 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 49 | A &d = a; |
| 50 | A &e = b[5]; |
| 51 | A &f = c[5][7]; |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | namespace test4 { |
| 55 | char a[] = "hello"; |
Eli Friedman | bc34b1d | 2011-04-11 00:23:45 +0000 | [diff] [blame] | 56 | char b[6] = "hello"; |
| 57 | char c[][6] = { "hello" }; |
John McCall | 626e96e | 2010-08-01 20:20:59 +0000 | [diff] [blame] | 58 | } |
Anders Carlsson | 4938f23 | 2010-09-03 01:11:38 +0000 | [diff] [blame] | 59 | |
| 60 | namespace test5 { |
| 61 | struct A { A(); }; |
| 62 | |
| 63 | void f1() { |
| 64 | static A a; |
| 65 | } |
| 66 | void f2() { |
| 67 | static A& a = *new A; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | namespace test6 { |
| 72 | struct A { ~A(); }; |
| 73 | |
| 74 | void f1() { |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 75 | static A a; |
Anders Carlsson | 4938f23 | 2010-09-03 01:11:38 +0000 | [diff] [blame] | 76 | } |
| 77 | void f2() { |
| 78 | static A& a = *new A; |
| 79 | } |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 80 | } |
Anders Carlsson | 4938f23 | 2010-09-03 01:11:38 +0000 | [diff] [blame] | 81 | |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 82 | namespace pr8095 { |
| 83 | struct Foo { |
| 84 | int x; |
| 85 | Foo(int x1) : x(x1) {} |
| 86 | }; |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 87 | void foo() { |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 88 | static Foo a(0); |
| 89 | } |
John McCall | ae79222 | 2010-09-18 05:25:11 +0000 | [diff] [blame] | 90 | |
| 91 | struct Bar { |
| 92 | ~Bar(); |
| 93 | }; |
| 94 | void bar() { |
| 95 | static Bar b; |
| 96 | } |
Sebastian Redl | 36281c6 | 2010-09-08 04:46:19 +0000 | [diff] [blame] | 97 | } |
Eli Friedman | 21cde05 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 98 | |
| 99 | namespace referencemember { |
| 100 | struct A { int &a; }; |
| 101 | int a; |
| 102 | A b = { a }; |
| 103 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 104 | |
| 105 | namespace pr19253 { |
| 106 | struct A { ~A() = default; }; |
| 107 | A a; |
| 108 | |
| 109 | struct B { ~B(); }; |
| 110 | struct C : B { ~C() = default; }; |
| 111 | C c; // expected-warning {{global destructor}} |
| 112 | |
| 113 | class D { |
| 114 | friend struct E; |
| 115 | ~D() = default; |
| 116 | }; |
| 117 | struct E : D { |
| 118 | D d; |
| 119 | ~E() = default; |
| 120 | }; |
| 121 | E e; |
| 122 | } |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 123 | |
| 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; |
| 128 | } |