Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 1 | // RUN: clang -fsyntax-only -pedantic -verify %s |
| 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 | }; |
| 16 | NonAggregate non_aggregate_test = { 1, 2 }; // expected-error{{initialization of non-aggregate type 'struct NonAggregate' with an initializer list}} |
| 17 | |
| 18 | NonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; // expected-error{{initialization of non-aggregate type 'struct NonAggregate' with an initializer list}} |
| 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; }; |
| 41 | TooFew too_few = { 1, "asdf" }; // okay |
| 42 | |
| 43 | // C++ [dcl.init.aggr]p8 |
| 44 | struct Empty { }; |
| 45 | struct EmptyTest { |
| 46 | Empty s; |
| 47 | int i; |
| 48 | } empty_test = { { }, 3 }; |
| 49 | |
| 50 | EmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}} |
| 51 | |
| 52 | struct NonEmpty { |
| 53 | int a; |
| 54 | Empty empty; |
| 55 | }; |
| 56 | struct NonEmptyTest { |
| 57 | NonEmpty a, b; |
| 58 | } non_empty_test = { { }, { } }; |
| 59 | |
| 60 | // C++ [dcl.init.aggr]p9 |
| 61 | struct HasReference { |
| 62 | int i; |
| 63 | int &j; // expected-note{{uninitialized reference member is here}} |
| 64 | }; |
| 65 | int global_int; |
| 66 | HasReference r1 = { 1, global_int }; |
| 67 | HasReference r2 = { 1 } ; // expected-error{{initialization leaves reference member of type 'int &' uninitialized}} |
| 68 | |
| 69 | // C++ [dcl.init.aggr]p10 |
| 70 | // Note: the behavior here is identical to C |
| 71 | int xs[2][2] = { 3, 1, 4, 2 }; |
| 72 | float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } }; |
| 73 | |
| 74 | // C++ [dcl.init.aggr]p11 |
| 75 | // Note: the behavior here is identical to C |
| 76 | float y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } }; |
| 77 | float same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 }; |
| 78 | |
| 79 | // C++ [dcl.init.aggr]p12 |
| 80 | struct A2 { |
| 81 | int i; |
| 82 | operator int *(); |
| 83 | }; |
| 84 | struct B2 { |
| 85 | A2 a1, a2; |
| 86 | int *z; |
| 87 | }; |
| 88 | struct C2 { |
| 89 | operator A2(); |
| 90 | }; |
| 91 | struct D2 { |
| 92 | operator int(); |
| 93 | }; |
| 94 | A2 a2; |
| 95 | C2 c2; |
| 96 | D2 d2; |
| 97 | B2 b2 = { 4, a2, a2 }; |
| 98 | B2 b2_2 = { 4, d2, 0 }; |
| 99 | // FIXME: B2 b2_3 = { c2, a2, a2 }; |
| 100 | |
| 101 | // C++ [dcl.init.aggr]p15: |
| 102 | union u { int a; char* b; }; |
| 103 | u u1 = { 1 }; |
| 104 | u u2 = u1; |
| 105 | u u3 = 1; // expected-error{{cannot initialize 'u3' with an rvalue of type 'int'}} |
Douglas Gregor | b574e56 | 2009-01-30 22:26:29 +0000 | [diff] [blame] | 106 | u u4 = { 0, "asdf" }; // expected-error{{excess elements in array initializer}} |
Douglas Gregor | 930d8b5 | 2009-01-30 22:09:00 +0000 | [diff] [blame] | 107 | u u5 = { "asdf" }; // expected-error{{incompatible type initializing 'char const [5]', expected 'int'}} |