1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wno-reorder -Wno-c99-designator -Winitializer-overrides %s
2 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify -Wno-reorder -Wno-c99-designator -Woverride-init %s
3
4 template <typename T> struct Foo {
5 struct SubFoo {
6 int bar1;
7 int bar2;
8 };
9
TestFoo10 static void Test() { SubFoo sf = {.bar1 = 10, .bar2 = 20}; } // Expected no warning
11 };
12
foo()13 void foo() {
14 Foo<int>::Test();
15 Foo<bool>::Test();
16 Foo<float>::Test();
17 }
18
19 template <typename T> struct Bar {
20 struct SubFoo {
21 int bar1;
22 int bar2;
23 };
24
TestBar25 static void Test() { SubFoo sf = {.bar1 = 10, // expected-note 2 {{previous initialization is here}}
26 .bar1 = 20}; } // expected-warning 2 {{initializer overrides prior initialization of this subobject}}
27 };
28
bar()29 void bar() {
30 Bar<int>::Test(); // expected-note {{in instantiation of member function 'Bar<int>::Test' requested here}}
31 Bar<bool>::Test(); // expected-note {{in instantiation of member function 'Bar<bool>::Test' requested here}}
32 }
33
34 namespace Reorder {
35 struct X {
36 X(int n);
37 private:
38 int i;
39 };
40
41 struct foo {
42 X x;
43 X y;
44 };
45
46 foo n = {.y = 4, .x = 5};
47 X arr[2] = {[1] = 1, [0] = 2};
48 }
49
50 namespace Reorder2 {
51 struct S {
52 S();
53 S(const S &);
54 ~S();
55 };
56
57 struct EF {
58 S s;
59 };
60
61 struct PN {
62 PN(const PN &);
63 };
64 extern PN pn;
65
66 struct FLN {
67 EF ef;
68 int it;
69 PN pn;
70 };
71
f()72 void f() {
73 FLN new_elem = {
74 .ef = EF(),
75 .pn = pn,
76 .it = 0,
77 };
78 }
79 }
80
81 namespace Reorder3 {
82 struct S {
83 int a, &b, &c; // expected-note 2{{here}}
84 };
85 S s1 = {
86 .a = 1, .c = s1.a, .b = s1.a
87 };
88 S s2 = {
89 .a = 1, .c = s2.a
90 }; // expected-error {{uninitialized}}
91 S s3 = {
92 .b = s3.a, .a = 1,
93 }; // expected-error {{uninitialized}}
94 }
95
96 // Check that we don't even think about whether holes in a designated
97 // initializer are zero-initializable if the holes are filled later.
98 namespace NoCheckingFilledHoles {
99 template<typename T> struct Error { using type = typename T::type; }; // expected-error 3{{'::'}}
100
101 template<int N>
102 struct DefaultInitIsError {
103 DefaultInitIsError(Error<int[N]> = {}); // expected-note 3{{instantiation}} expected-note 3{{passing}}
104 DefaultInitIsError(int, int);
105 };
106
107 template<int N>
108 struct X {
109 int a;
110 DefaultInitIsError<N> e;
111 int b;
112 };
113 X<1> x1 = {
114 .b = 2,
115 .a = 1,
116 {4, 4}
117 };
118 X<2> x2 = {
119 .e = {4, 4},
120 .b = 2,
121 .a = 1
122 };
123 X<3> x3 = {
124 .b = 2,
125 .a = 1
126 }; // expected-note {{default function argument}}
127 X<4> x4 = {
128 .a = 1,
129 .b = 2
130 }; // expected-note {{default function argument}}
131 X<5> x5 = {
132 .e = {4, 4},
133 .a = 1,
134 .b = 2
135 };
136 X<6> x6 = {
137 .a = 1,
138 .b = 2,
139 .e = {4, 4}
140 };
141
142 template<int N> struct Y { X<N> x; };
143 Y<7> y7 = {
144 .x = {.a = 1, .b = 2}, // expected-note {{default function argument}}
145 .x.e = {3, 4}
146 };
147 Y<8> y8 = {
148 .x = {.e = {3, 4}},
149 .x.a = 1,
150 .x.b = 2
151 };
152 }
153
154 namespace LargeArrayDesignator {
155 struct X {
156 int arr[1000000000];
157 };
158 struct Y {
159 int arr[3];
160 };
161 void f(X x);
162 void f(Y y) = delete;
g()163 void g() {
164 f({.arr[4] = 1});
165 }
166 }
167
168 namespace ADL {
169 struct A {};
170 void f(A, int);
171
172 namespace X {
173 void f(A, int);
174 // OK. Would fail if checking {} against type A set the type of the
175 // initializer list to A, because ADL would find ADL::f, resulting in
176 // ambiguity.
g()177 void g() { f({}, {}); }
178 }
179 }
180