Daniel Dunbar | 8fbe78f | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 2 | // RUN: %clang_cc1 -fsyntax-only -pedantic -verify -std=c++98 %s |
| 3 | // RUN: %clang_cc1 -fsyntax-only -pedantic -verify -std=c++11 %s |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 4 | // C++ [dcl.init.aggr]p2 |
| 5 | struct A { |
| 6 | int x; |
| 7 | struct B { |
| 8 | int i; |
| 9 | int j; |
| 10 | } b; |
| 11 | } a1 = { 1, { 2, 3 } }; |
| 12 | |
| 13 | struct NonAggregate { |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 14 | #if __cplusplus >= 201103L |
| 15 | // expected-note@-2 3 {{candidate constructor (the implicit copy constructor) not viable}} |
| 16 | // expected-note@-3 3 {{candidate constructor (the implicit move constructor) not viable}} |
| 17 | #endif |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 18 | NonAggregate(); |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 19 | #if __cplusplus >= 201103L |
| 20 | // expected-note@-2 3 {{candidate constructor not viable: requires 0 arguments, but 2 were provided}} |
| 21 | #endif |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 22 | int a, b; |
| 23 | }; |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 24 | NonAggregate non_aggregate_test = { 1, 2 }; |
| 25 | #if __cplusplus <= 199711L |
| 26 | // expected-error@-2 {{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}} |
| 27 | #else |
| 28 | // expected-error@-4 {{no matching constructor for initialization of 'NonAggregate'}} |
| 29 | #endif |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 30 | |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 31 | NonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; |
| 32 | #if __cplusplus <= 199711L |
| 33 | // expected-error@-2 2 {{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}} |
| 34 | #else |
| 35 | // expected-error@-4 2 {{no matching constructor for initialization of 'NonAggregate'}} |
| 36 | #endif |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 37 | |
| 38 | // C++ [dcl.init.aggr]p3 |
| 39 | A a_init = A(); |
| 40 | |
| 41 | // C++ [dcl.init.aggr]p4 |
| 42 | int x[] = { 1, 3, 5 }; |
| 43 | int x_sizecheck[(sizeof(x) / sizeof(int)) == 3? 1 : -1]; |
| 44 | int x2[] = { }; // expected-warning{{zero size arrays are an extension}} |
| 45 | |
| 46 | // C++ [dcl.init.aggr]p5 |
| 47 | struct StaticMemberTest { |
| 48 | int i; |
| 49 | static int s; |
| 50 | int *j; |
| 51 | } smt = { 1, &smt.i }; |
| 52 | |
| 53 | // C++ [dcl.init.aggr]p6 |
Douglas Gregor | d42a0fb | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 54 | char cv[4] = { 'a', 's', 'd', 'f', 0 }; // expected-error{{excess elements in array initializer}} |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 55 | |
| 56 | // C++ [dcl.init.aggr]p7 |
| 57 | struct TooFew { int a; char* b; int c; }; |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 58 | TooFew too_few = { 1, "asdf" }; |
| 59 | #if __cplusplus <= 199711L |
| 60 | // expected-warning@-2 {{conversion from string literal to 'char *' is deprecated}} |
| 61 | #else |
| 62 | // expected-warning@-4 {{ISO C++11 does not allow conversion from string literal to 'char *'}} |
| 63 | #endif |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 64 | |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 65 | struct NoDefaultConstructor { |
| 66 | #if __cplusplus <= 199711L |
| 67 | // expected-note@-2 3 {{candidate constructor (the implicit copy constructor)}} |
| 68 | // expected-note@-3 {{declared here}} |
| 69 | #else |
| 70 | // expected-note@-5 4 {{candidate constructor (the implicit copy constructor)}} |
| 71 | // expected-note@-6 4 {{candidate constructor (the implicit move constructor)}} |
| 72 | #endif |
| 73 | |
| 74 | NoDefaultConstructor(int); |
| 75 | #if __cplusplus <= 199711L |
| 76 | // expected-note@-2 3 {{candidate constructor not viable: requires 1 argument, but 0 were provided}} |
| 77 | #else |
| 78 | // expected-note@-4 4 {{candidate constructor not viable: requires 1 argument, but 0 were provided}} |
| 79 | #endif |
| 80 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 81 | }; |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 82 | struct TooFewError { |
| 83 | #if __cplusplus <= 199711L |
| 84 | // expected-error@-2 {{implicit default constructor for}} |
| 85 | #endif |
| 86 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 87 | int a; |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 88 | NoDefaultConstructor nodef; |
| 89 | #if __cplusplus <= 199711L |
| 90 | // expected-note@-2 {{member is declared here}} |
| 91 | // expected-note@-3 2{{in implicit initialization of field 'nodef' with omitted initializer}} |
| 92 | #else |
| 93 | // expected-note@-5 3{{in implicit initialization of field 'nodef' with omitted initializer}} |
| 94 | #endif |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 95 | }; |
| 96 | TooFewError too_few_okay = { 1, 1 }; |
| 97 | TooFewError too_few_error = { 1 }; // expected-error{{no matching constructor}} |
| 98 | |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 99 | TooFewError too_few_okay2[2] = { 1, 1 }; |
| 100 | #if __cplusplus <= 199711L |
| 101 | // expected-note@-2 {{implicit default constructor for 'TooFewError' first required here}} |
| 102 | #else |
| 103 | // expected-error@-4 {{no matching constructor for initialization of 'NoDefaultConstructor'}} |
| 104 | // expected-note@-5 {{in implicit initialization of array element 1 with omitted initializer}} |
| 105 | #endif |
| 106 | |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 107 | TooFewError too_few_error2[2] = { 1 }; // expected-error{{no matching constructor}} |
| 108 | |
Richard Smith | 6c3bbf4 | 2014-06-03 07:28:54 +0000 | [diff] [blame] | 109 | NoDefaultConstructor too_few_error3[3] = { }; // expected-error {{no matching constructor}} expected-note {{implicit initialization of array element 0}} |
Douglas Gregor | a5c9e1a | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 110 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 111 | // C++ [dcl.init.aggr]p8 |
| 112 | struct Empty { }; |
| 113 | struct EmptyTest { |
| 114 | Empty s; |
| 115 | int i; |
| 116 | } empty_test = { { }, 3 }; |
| 117 | |
| 118 | EmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}} |
| 119 | |
| 120 | struct NonEmpty { |
| 121 | int a; |
| 122 | Empty empty; |
| 123 | }; |
| 124 | struct NonEmptyTest { |
| 125 | NonEmpty a, b; |
| 126 | } non_empty_test = { { }, { } }; |
| 127 | |
| 128 | // C++ [dcl.init.aggr]p9 |
| 129 | struct HasReference { |
| 130 | int i; |
| 131 | int &j; // expected-note{{uninitialized reference member is here}} |
| 132 | }; |
| 133 | int global_int; |
| 134 | HasReference r1 = { 1, global_int }; |
Richard Trieu | 553b2b2 | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 135 | HasReference r2 = { 1 } ; // expected-error{{reference member of type 'int &' uninitialized}} |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 136 | |
| 137 | // C++ [dcl.init.aggr]p10 |
| 138 | // Note: the behavior here is identical to C |
| 139 | int xs[2][2] = { 3, 1, 4, 2 }; |
| 140 | float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } }; |
| 141 | |
| 142 | // C++ [dcl.init.aggr]p11 |
| 143 | // Note: the behavior here is identical to C |
| 144 | float y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } }; |
| 145 | float same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 }; |
| 146 | |
| 147 | // C++ [dcl.init.aggr]p12 |
| 148 | struct A2 { |
| 149 | int i; |
| 150 | operator int *(); |
| 151 | }; |
| 152 | struct B2 { |
| 153 | A2 a1, a2; |
| 154 | int *z; |
| 155 | }; |
| 156 | struct C2 { |
| 157 | operator A2(); |
| 158 | }; |
| 159 | struct D2 { |
| 160 | operator int(); |
| 161 | }; |
| 162 | A2 a2; |
| 163 | C2 c2; |
| 164 | D2 d2; |
| 165 | B2 b2 = { 4, a2, a2 }; |
| 166 | B2 b2_2 = { 4, d2, 0 }; |
Douglas Gregor | 576e98c | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 167 | B2 b2_3 = { c2, a2, a2 }; |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 168 | |
| 169 | // C++ [dcl.init.aggr]p15: |
John McCall | e1ac8d1 | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 170 | union u { int a; char* b; }; // expected-note{{candidate constructor (the implicit copy constructor)}} |
Charles Li | 64a1a81 | 2016-04-13 20:00:45 +0000 | [diff] [blame] | 171 | #if __cplusplus >= 201103L |
| 172 | // expected-note@-2 {{candidate constructor (the implicit move constructor)}} |
| 173 | #endif |
| 174 | |
Douglas Gregor | d14247a | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 175 | u u1 = { 1 }; |
| 176 | u u2 = u1; |
Douglas Gregor | a4b592a | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 177 | u u3 = 1; // expected-error{{no viable conversion}} |
Douglas Gregor | fc4f8a1 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 178 | u u4 = { 0, "asdf" }; // expected-error{{excess elements in union initializer}} |
Chris Lattner | 53fa049 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 179 | u u5 = { "asdf" }; // expected-error{{cannot initialize a member subobject of type 'int' with an lvalue of type 'const char [5]'}} |