Daniel Dunbar | a572887 | 2009-12-15 20:14:24 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 2 | // C++ [dcl.init.aggr]p2 |
| 3 | struct A { |
| 4 | int x; |
| 5 | struct B { |
| 6 | int i; |
| 7 | int j; |
| 8 | } b; |
| 9 | } a1 = { 1, { 2, 3 } }; |
| 10 | |
| 11 | struct NonAggregate { |
| 12 | NonAggregate(); |
| 13 | |
| 14 | int a, b; |
| 15 | }; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 16 | NonAggregate non_aggregate_test = { 1, 2 }; // expected-error{{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 17 | |
Richard Smith | 2059939 | 2012-07-07 08:35:56 +0000 | [diff] [blame] | 18 | NonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; // expected-error 2 {{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | // C++ [dcl.init.aggr]p3 |
| 22 | A a_init = A(); |
| 23 | |
| 24 | // C++ [dcl.init.aggr]p4 |
| 25 | int x[] = { 1, 3, 5 }; |
| 26 | int x_sizecheck[(sizeof(x) / sizeof(int)) == 3? 1 : -1]; |
| 27 | int x2[] = { }; // expected-warning{{zero size arrays are an extension}} |
| 28 | |
| 29 | // C++ [dcl.init.aggr]p5 |
| 30 | struct StaticMemberTest { |
| 31 | int i; |
| 32 | static int s; |
| 33 | int *j; |
| 34 | } smt = { 1, &smt.i }; |
| 35 | |
| 36 | // C++ [dcl.init.aggr]p6 |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 37 | char cv[4] = { 'a', 's', 'd', 'f', 0 }; // expected-error{{excess elements in array initializer}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 38 | |
| 39 | // C++ [dcl.init.aggr]p7 |
| 40 | struct TooFew { int a; char* b; int c; }; |
Douglas Gregor | a9bff30 | 2010-02-28 18:30:25 +0000 | [diff] [blame] | 41 | TooFew too_few = { 1, "asdf" }; // expected-warning{{conversion from string literal to 'char *' is deprecated}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 42 | |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 43 | struct NoDefaultConstructor { // expected-note 3 {{candidate constructor (the implicit copy constructor)}} \ |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 44 | // expected-note{{declared here}} |
John McCall | b1622a1 | 2010-01-06 09:43:14 +0000 | [diff] [blame] | 45 | NoDefaultConstructor(int); // expected-note 3 {{candidate constructor}} |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 46 | }; |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 47 | struct TooFewError { // expected-error{{implicit default constructor for}} |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 48 | int a; |
Douglas Gregor | 16006c9 | 2009-12-16 18:50:27 +0000 | [diff] [blame] | 49 | NoDefaultConstructor nodef; // expected-note{{member is declared here}} |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 50 | }; |
| 51 | TooFewError too_few_okay = { 1, 1 }; |
| 52 | TooFewError too_few_error = { 1 }; // expected-error{{no matching constructor}} |
| 53 | |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 54 | TooFewError too_few_okay2[2] = { 1, 1 }; // expected-note{{implicit default constructor for 'TooFewError' first required here}} |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 55 | TooFewError too_few_error2[2] = { 1 }; // expected-error{{no matching constructor}} |
| 56 | |
Douglas Gregor | 39da0b8 | 2009-09-09 23:08:42 +0000 | [diff] [blame] | 57 | NoDefaultConstructor too_few_error3[3] = { }; // expected-error {{no matching constructor}} |
Douglas Gregor | 87fd703 | 2009-02-02 17:43:21 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 59 | // C++ [dcl.init.aggr]p8 |
| 60 | struct Empty { }; |
| 61 | struct EmptyTest { |
| 62 | Empty s; |
| 63 | int i; |
| 64 | } empty_test = { { }, 3 }; |
| 65 | |
| 66 | EmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}} |
| 67 | |
| 68 | struct NonEmpty { |
| 69 | int a; |
| 70 | Empty empty; |
| 71 | }; |
| 72 | struct NonEmptyTest { |
| 73 | NonEmpty a, b; |
| 74 | } non_empty_test = { { }, { } }; |
| 75 | |
| 76 | // C++ [dcl.init.aggr]p9 |
| 77 | struct HasReference { |
| 78 | int i; |
| 79 | int &j; // expected-note{{uninitialized reference member is here}} |
| 80 | }; |
| 81 | int global_int; |
| 82 | HasReference r1 = { 1, global_int }; |
Richard Trieu | 2fe9b7f | 2011-12-15 00:38:15 +0000 | [diff] [blame] | 83 | HasReference r2 = { 1 } ; // expected-error{{reference member of type 'int &' uninitialized}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 84 | |
| 85 | // C++ [dcl.init.aggr]p10 |
| 86 | // Note: the behavior here is identical to C |
| 87 | int xs[2][2] = { 3, 1, 4, 2 }; |
| 88 | float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } }; |
| 89 | |
| 90 | // C++ [dcl.init.aggr]p11 |
| 91 | // Note: the behavior here is identical to C |
| 92 | float y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } }; |
| 93 | float same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 }; |
| 94 | |
| 95 | // C++ [dcl.init.aggr]p12 |
| 96 | struct A2 { |
| 97 | int i; |
| 98 | operator int *(); |
| 99 | }; |
| 100 | struct B2 { |
| 101 | A2 a1, a2; |
| 102 | int *z; |
| 103 | }; |
| 104 | struct C2 { |
| 105 | operator A2(); |
| 106 | }; |
| 107 | struct D2 { |
| 108 | operator int(); |
| 109 | }; |
| 110 | A2 a2; |
| 111 | C2 c2; |
| 112 | D2 d2; |
| 113 | B2 b2 = { 4, a2, a2 }; |
| 114 | B2 b2_2 = { 4, d2, 0 }; |
Douglas Gregor | 734d986 | 2009-01-30 23:27:23 +0000 | [diff] [blame] | 115 | B2 b2_3 = { c2, a2, a2 }; |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 116 | |
| 117 | // C++ [dcl.init.aggr]p15: |
John McCall | 220ccbf | 2010-01-13 00:25:19 +0000 | [diff] [blame] | 118 | union u { int a; char* b; }; // expected-note{{candidate constructor (the implicit copy constructor)}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 119 | u u1 = { 1 }; |
| 120 | u u2 = u1; |
Douglas Gregor | 7abfbdb | 2009-12-19 03:01:41 +0000 | [diff] [blame] | 121 | u u3 = 1; // expected-error{{no viable conversion}} |
Douglas Gregor | eeb15d4 | 2009-02-04 22:46:25 +0000 | [diff] [blame] | 122 | u u4 = { 0, "asdf" }; // expected-error{{excess elements in union initializer}} |
Chris Lattner | 58f9e13 | 2010-09-05 00:04:01 +0000 | [diff] [blame] | 123 | u u5 = { "asdf" }; // expected-error{{cannot initialize a member subobject of type 'int' with an lvalue of type 'const char [5]'}} |