blob: 8c5e654fca2eff87b1ca754cb909489e55044a4b [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -pedantic -verify %s
Douglas Gregor930d8b52009-01-30 22:09:00 +00002// C++ [dcl.init.aggr]p2
3struct A {
4 int x;
5 struct B {
6 int i;
7 int j;
8 } b;
9} a1 = { 1, { 2, 3 } };
10
11struct NonAggregate {
12 NonAggregate();
13
14 int a, b;
15};
John McCall7c2342d2010-03-10 11:27:22 +000016NonAggregate non_aggregate_test = { 1, 2 }; // expected-error{{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000017
Richard Smith20599392012-07-07 08:35:56 +000018NonAggregate non_aggregate_test2[2] = { { 1, 2 }, { 3, 4 } }; // expected-error 2 {{non-aggregate type 'NonAggregate' cannot be initialized with an initializer list}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000019
20
21// C++ [dcl.init.aggr]p3
22A a_init = A();
23
24// C++ [dcl.init.aggr]p4
25int x[] = { 1, 3, 5 };
26int x_sizecheck[(sizeof(x) / sizeof(int)) == 3? 1 : -1];
27int x2[] = { }; // expected-warning{{zero size arrays are an extension}}
28
29// C++ [dcl.init.aggr]p5
30struct StaticMemberTest {
31 int i;
32 static int s;
33 int *j;
34} smt = { 1, &smt.i };
35
36// C++ [dcl.init.aggr]p6
Douglas Gregorb574e562009-01-30 22:26:29 +000037char cv[4] = { 'a', 's', 'd', 'f', 0 }; // expected-error{{excess elements in array initializer}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000038
39// C++ [dcl.init.aggr]p7
40struct TooFew { int a; char* b; int c; };
Douglas Gregora9bff302010-02-28 18:30:25 +000041TooFew too_few = { 1, "asdf" }; // expected-warning{{conversion from string literal to 'char *' is deprecated}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000042
John McCall220ccbf2010-01-13 00:25:19 +000043struct NoDefaultConstructor { // expected-note 3 {{candidate constructor (the implicit copy constructor)}} \
Douglas Gregor16006c92009-12-16 18:50:27 +000044 // expected-note{{declared here}}
John McCallb1622a12010-01-06 09:43:14 +000045 NoDefaultConstructor(int); // expected-note 3 {{candidate constructor}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000046};
Douglas Gregor16006c92009-12-16 18:50:27 +000047struct TooFewError { // expected-error{{implicit default constructor for}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000048 int a;
Douglas Gregor16006c92009-12-16 18:50:27 +000049 NoDefaultConstructor nodef; // expected-note{{member is declared here}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000050};
51TooFewError too_few_okay = { 1, 1 };
52TooFewError too_few_error = { 1 }; // expected-error{{no matching constructor}}
53
John McCall7c2342d2010-03-10 11:27:22 +000054TooFewError too_few_okay2[2] = { 1, 1 }; // expected-note{{implicit default constructor for 'TooFewError' first required here}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000055TooFewError too_few_error2[2] = { 1 }; // expected-error{{no matching constructor}}
56
Douglas Gregor39da0b82009-09-09 23:08:42 +000057NoDefaultConstructor too_few_error3[3] = { }; // expected-error {{no matching constructor}}
Douglas Gregor87fd7032009-02-02 17:43:21 +000058
Douglas Gregor930d8b52009-01-30 22:09:00 +000059// C++ [dcl.init.aggr]p8
60struct Empty { };
61struct EmptyTest {
62 Empty s;
63 int i;
64} empty_test = { { }, 3 };
65
66EmptyTest empty_test2 = { 3 }; // expected-error{{initializer for aggregate with no elements requires explicit braces}}
67
68struct NonEmpty {
69 int a;
70 Empty empty;
71};
72struct NonEmptyTest {
73 NonEmpty a, b;
74} non_empty_test = { { }, { } };
75
76// C++ [dcl.init.aggr]p9
77struct HasReference {
78 int i;
79 int &j; // expected-note{{uninitialized reference member is here}}
80};
81int global_int;
82HasReference r1 = { 1, global_int };
Richard Trieu2fe9b7f2011-12-15 00:38:15 +000083HasReference r2 = { 1 } ; // expected-error{{reference member of type 'int &' uninitialized}}
Douglas Gregor930d8b52009-01-30 22:09:00 +000084
85// C++ [dcl.init.aggr]p10
86// Note: the behavior here is identical to C
87int xs[2][2] = { 3, 1, 4, 2 };
88float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };
89
90// C++ [dcl.init.aggr]p11
91// Note: the behavior here is identical to C
92float y2[4][3] = { { 1, 3, 5 }, { 2, 4, 6 }, { 3, 5, 7 } };
93float same_as_y2[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };
94
95// C++ [dcl.init.aggr]p12
96struct A2 {
97 int i;
98 operator int *();
99};
100struct B2 {
101 A2 a1, a2;
102 int *z;
103};
104struct C2 {
105 operator A2();
106};
107struct D2 {
108 operator int();
109};
110A2 a2;
111C2 c2;
112D2 d2;
113B2 b2 = { 4, a2, a2 };
114B2 b2_2 = { 4, d2, 0 };
Douglas Gregor734d9862009-01-30 23:27:23 +0000115B2 b2_3 = { c2, a2, a2 };
Douglas Gregor930d8b52009-01-30 22:09:00 +0000116
117// C++ [dcl.init.aggr]p15:
John McCall220ccbf2010-01-13 00:25:19 +0000118union u { int a; char* b; }; // expected-note{{candidate constructor (the implicit copy constructor)}}
Douglas Gregor930d8b52009-01-30 22:09:00 +0000119u u1 = { 1 };
120u u2 = u1;
Douglas Gregor7abfbdb2009-12-19 03:01:41 +0000121u u3 = 1; // expected-error{{no viable conversion}}
Douglas Gregoreeb15d42009-02-04 22:46:25 +0000122u u4 = { 0, "asdf" }; // expected-error{{excess elements in union initializer}}
Chris Lattner58f9e132010-09-05 00:04:01 +0000123u u5 = { "asdf" }; // expected-error{{cannot initialize a member subobject of type 'int' with an lvalue of type 'const char [5]'}}