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