xref: /aosp_15_r20/external/clang/test/SemaCXX/aggregate-initialization.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++14 %s
3*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -verify -std=c++1z %s
4*67e74705SXin Li 
5*67e74705SXin Li // Verify that using an initializer list for a non-aggregate looks for
6*67e74705SXin Li // constructors..
7*67e74705SXin Li // Note that due to a (likely) standard bug, this is technically an aggregate,
8*67e74705SXin Li // but we do not treat it as one.
9*67e74705SXin Li struct NonAggr1 { // expected-note 2 {{candidate constructor}}
NonAggr1NonAggr110*67e74705SXin Li   NonAggr1(int, int) { } // expected-note {{candidate constructor}}
11*67e74705SXin Li 
12*67e74705SXin Li   int m;
13*67e74705SXin Li };
14*67e74705SXin Li 
15*67e74705SXin Li struct Base { };
16*67e74705SXin Li struct NonAggr2 : public Base { // expected-note 0-3 {{candidate constructor}}
17*67e74705SXin Li   int m;
18*67e74705SXin Li };
19*67e74705SXin Li 
20*67e74705SXin Li class NonAggr3 { // expected-note 3 {{candidate constructor}}
21*67e74705SXin Li   int m;
22*67e74705SXin Li };
23*67e74705SXin Li 
24*67e74705SXin Li struct NonAggr4 { // expected-note 3 {{candidate constructor}}
25*67e74705SXin Li   int m;
26*67e74705SXin Li   virtual void f();
27*67e74705SXin Li };
28*67e74705SXin Li 
29*67e74705SXin Li NonAggr1 na1 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr1'}}
30*67e74705SXin Li NonAggr2 na2 = { 17 };
31*67e74705SXin Li NonAggr3 na3 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr3'}}
32*67e74705SXin Li NonAggr4 na4 = { 17 }; // expected-error{{no matching constructor for initialization of 'NonAggr4'}}
33*67e74705SXin Li #if __cplusplus <= 201402L
34*67e74705SXin Li // expected-error@-4{{no matching constructor for initialization of 'NonAggr2'}}
35*67e74705SXin Li #else
36*67e74705SXin Li // expected-error@-6{{requires explicit braces}}
37*67e74705SXin Li NonAggr2 na2b = { {}, 17 }; // ok
38*67e74705SXin Li #endif
39*67e74705SXin Li 
40*67e74705SXin Li // PR5817
41*67e74705SXin Li typedef int type[][2];
42*67e74705SXin Li const type foo = {0};
43*67e74705SXin Li 
44*67e74705SXin Li // Vector initialization.
45*67e74705SXin Li typedef short __v4hi __attribute__ ((__vector_size__ (8)));
46*67e74705SXin Li __v4hi v1 = { (void *)1, 2, 3 }; // expected-error {{cannot initialize a vector element of type 'short' with an rvalue of type 'void *'}}
47*67e74705SXin Li 
48*67e74705SXin Li // Array initialization.
49*67e74705SXin Li int a[] = { (void *)1 }; // expected-error {{cannot initialize an array element of type 'int' with an rvalue of type 'void *'}}
50*67e74705SXin Li 
51*67e74705SXin Li // Struct initialization.
52*67e74705SXin Li struct S { int a; } s = { (void *)1 }; // expected-error {{cannot initialize a member subobject of type 'int' with an rvalue of type 'void *'}}
53*67e74705SXin Li 
54*67e74705SXin Li // Check that we're copy-initializing the structs.
55*67e74705SXin Li struct A {
56*67e74705SXin Li   A();
57*67e74705SXin Li   A(int);
58*67e74705SXin Li   ~A();
59*67e74705SXin Li 
60*67e74705SXin Li   A(const A&) = delete; // expected-note 2 {{'A' has been explicitly marked deleted here}}
61*67e74705SXin Li };
62*67e74705SXin Li 
63*67e74705SXin Li struct B {
64*67e74705SXin Li   A a;
65*67e74705SXin Li };
66*67e74705SXin Li 
67*67e74705SXin Li struct C {
68*67e74705SXin Li   const A& a;
69*67e74705SXin Li };
70*67e74705SXin Li 
f()71*67e74705SXin Li void f() {
72*67e74705SXin Li   A as1[1] = { };
73*67e74705SXin Li   A as2[1] = { 1 }; // expected-error {{copying array element of type 'A' invokes deleted constructor}}
74*67e74705SXin Li 
75*67e74705SXin Li   B b1 = { };
76*67e74705SXin Li   B b2 = { 1 }; // expected-error {{copying member subobject of type 'A' invokes deleted constructor}}
77*67e74705SXin Li 
78*67e74705SXin Li   C c1 = { 1 };
79*67e74705SXin Li }
80*67e74705SXin Li 
81*67e74705SXin Li class Agg {
82*67e74705SXin Li public:
83*67e74705SXin Li   int i, j;
84*67e74705SXin Li };
85*67e74705SXin Li 
86*67e74705SXin Li class AggAgg {
87*67e74705SXin Li public:
88*67e74705SXin Li   Agg agg1;
89*67e74705SXin Li   Agg agg2;
90*67e74705SXin Li };
91*67e74705SXin Li 
92*67e74705SXin Li AggAgg aggagg = { 1, 2, 3, 4 };
93*67e74705SXin Li 
94*67e74705SXin Li namespace diff_cpp14_dcl_init_aggr_example {
95*67e74705SXin Li   struct derived;
96*67e74705SXin Li   struct base {
97*67e74705SXin Li     friend struct derived;
98*67e74705SXin Li   private:
99*67e74705SXin Li     base();
100*67e74705SXin Li   };
101*67e74705SXin Li   struct derived : base {};
102*67e74705SXin Li 
103*67e74705SXin Li   derived d1{};
104*67e74705SXin Li #if __cplusplus > 201402L
105*67e74705SXin Li   // expected-error@-2 {{private}}
106*67e74705SXin Li   // expected-note@-7 {{here}}
107*67e74705SXin Li #endif
108*67e74705SXin Li   derived d2;
109*67e74705SXin Li }
110*67e74705SXin Li 
111*67e74705SXin Li namespace ProtectedBaseCtor {
112*67e74705SXin Li   // FIXME: It's unclear whether f() and g() should be valid in C++1z. What is
113*67e74705SXin Li   // the object expression in a constructor call -- the base class subobject or
114*67e74705SXin Li   // the complete object?
115*67e74705SXin Li   struct A {
116*67e74705SXin Li   protected:
117*67e74705SXin Li     A();
118*67e74705SXin Li   };
119*67e74705SXin Li 
120*67e74705SXin Li   struct B : public A {
121*67e74705SXin Li     friend B f();
122*67e74705SXin Li     friend B g();
123*67e74705SXin Li     friend B h();
124*67e74705SXin Li   };
125*67e74705SXin Li 
f()126*67e74705SXin Li   B f() { return {}; }
127*67e74705SXin Li #if __cplusplus > 201402L
128*67e74705SXin Li   // expected-error@-2 {{protected default constructor}}
129*67e74705SXin Li   // expected-note@-12 {{here}}
130*67e74705SXin Li #endif
131*67e74705SXin Li 
g()132*67e74705SXin Li   B g() { return {{}}; }
133*67e74705SXin Li #if __cplusplus <= 201402L
134*67e74705SXin Li   // expected-error@-2 {{no matching constructor}}
135*67e74705SXin Li   // expected-note@-15 3{{candidate}}
136*67e74705SXin Li #else
137*67e74705SXin Li   // expected-error@-5 {{protected default constructor}}
138*67e74705SXin Li   // expected-note@-21 {{here}}
139*67e74705SXin Li #endif
140*67e74705SXin Li 
h()141*67e74705SXin Li   B h() { return {A{}}; }
142*67e74705SXin Li #if __cplusplus <= 201402L
143*67e74705SXin Li   // expected-error@-2 {{no matching constructor}}
144*67e74705SXin Li   // expected-note@-24 3{{candidate}}
145*67e74705SXin Li #endif
146*67e74705SXin Li   // expected-error@-5 {{protected constructor}}
147*67e74705SXin Li   // expected-note@-30 {{here}}
148*67e74705SXin Li }
149