Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Analysis / uninit-structured-binding-array.cpp
blobed5439492818b4703b90359faa6efbc1b3db7165
1 // RUN: %clang_analyze_cc1 -analyzer-checker=core,debug.ExprInspection -std=c++17 -verify %s
3 void clang_analyzer_eval(bool);
5 void array_value_a(void) {
6 int arr[2];
7 auto [a, b] = arr;
8 arr[0] = 0;
10 int x = a; // expected-warning{{Assigned value is garbage or undefined}}
13 void array_value_b(void) {
14 int arr[] = {1, 2};
15 auto [a, b] = arr;
17 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
18 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
20 int x = a; // no-warning
23 void array_value_c(void) {
24 int arr[3];
26 arr[1] = 1;
28 auto [a, b, c] = arr;
30 clang_analyzer_eval(b == arr[1]); // expected-warning{{TRUE}}
32 int y = b; // no-warning
33 int x = a; // expected-warning{{Assigned value is garbage or undefined}}
36 void array_value_d(void) {
37 int arr[3];
39 arr[1] = 1;
41 auto [a, b, c] = arr;
43 clang_analyzer_eval(b == arr[1]); // expected-warning{{TRUE}}
45 int y = b; // no-warning
46 int x = c; // expected-warning{{Assigned value is garbage or undefined}}
49 void array_value_e(void) {
50 int uninit[2];
51 int init[2] = {0};
53 uninit[0] = init[0];
55 auto [i, j] = init;
57 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
58 clang_analyzer_eval(j == 0); // expected-warning{{TRUE}}
60 int a = i; // no-warning
61 int b = j; // no-warning
64 void array_value_f(void) {
65 int uninit[2];
66 int init[2] = {0};
68 uninit[0] = init[0];
70 auto [i, j] = uninit;
72 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
74 int a = i; // no-warning
75 int b = j; // expected-warning{{Assigned value is garbage or undefined}}
78 void array_lref_a(void) {
79 int arr[2];
80 auto &[a, b] = arr;
81 int x = a; // expected-warning{{Assigned value is garbage or undefined}}
84 void array_lref_b(void) {
85 int arr[] = {1, 2};
86 auto &[a, b] = arr;
88 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
89 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
91 int x = a; // no-warning
94 void array_lref_c(void) {
95 int arr[2];
96 auto &[a, b] = arr;
98 arr[0] = 1;
100 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
102 int x = a; // no-warning
103 int y = b; // expected-warning{{Assigned value is garbage or undefined}}
106 void array_lref_d(void) {
107 int arr[3];
109 arr[1] = 1;
111 auto &[a, b, c] = arr;
113 clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
115 int y = b; // no-warning
116 int x = a; // expected-warning{{Assigned value is garbage or undefined}}
119 void array_lref_e(void) {
120 int arr[3];
122 arr[1] = 1;
124 auto &[a, b, c] = arr;
126 clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
128 int y = b; // no-warning
129 int x = c; // expected-warning{{Assigned value is garbage or undefined}}
132 void array_lref_f(void) {
133 int uninit[2];
134 int init[2] = {0};
136 uninit[0] = init[0];
138 auto &[i, j] = init;
140 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
141 clang_analyzer_eval(j == 0); // expected-warning{{TRUE}}
143 int a = i; // no-warning
144 int b = j; // no-warning
147 void array_lref_g(void) {
148 int uninit[2];
149 int init[2] = {0};
151 uninit[0] = init[0];
153 auto &[i, j] = uninit;
155 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
157 int a = i; // no-warning
158 int b = j; // expected-warning{{Assigned value is garbage or undefined}}
161 void array_rref_a(void) {
162 int arr[2];
163 auto &&[a, b] = arr;
164 int x = a; // expected-warning{{Assigned value is garbage or undefined}}
167 void array_rref_b(void) {
168 int arr[] = {1, 2};
169 auto &&[a, b] = arr;
171 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
172 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
174 int x = a; // no-warning
177 void array_rref_c(void) {
178 int arr[2];
179 auto &&[a, b] = arr;
181 arr[0] = 1;
183 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
185 int x = a; // no-warning
186 int y = b; // expected-warning{{Assigned value is garbage or undefined}}
189 void array_rref_d(void) {
190 int arr[3];
192 arr[1] = 1;
194 auto &&[a, b, c] = arr;
196 clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
198 int y = b; // no-warning
199 int x = a; // expected-warning{{Assigned value is garbage or undefined}}
202 void array_rref_e(void) {
203 int arr[3];
205 arr[1] = 1;
207 auto &&[a, b, c] = arr;
209 clang_analyzer_eval(b == 1); // expected-warning{{TRUE}}
211 int y = b; // no-warning
212 int x = c; // expected-warning{{Assigned value is garbage or undefined}}
215 void array_rref_f(void) {
216 int uninit[2];
217 int init[2] = {0};
219 uninit[0] = init[0];
221 auto &&[i, j] = init;
223 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
224 clang_analyzer_eval(j == 0); // expected-warning{{TRUE}}
226 int a = i; // no-warning
227 int b = j; // no-warning
230 void array_rref_g(void) {
231 int uninit[2];
232 int init[2] = {0};
234 uninit[0] = init[0];
236 auto &&[i, j] = uninit;
238 clang_analyzer_eval(i == 0); // expected-warning{{TRUE}}
240 int a = i; // no-warning
241 int b = j; // expected-warning{{Assigned value is garbage or undefined}}
244 void array_change_a(void) {
245 int arr[] = {1, 2};
247 auto [a, b] = arr;
249 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
250 a = 3;
251 clang_analyzer_eval(a == 3); // expected-warning{{TRUE}}
253 clang_analyzer_eval(arr[0] == 1); // expected-warning{{TRUE}}
254 clang_analyzer_eval(arr[1] == 2); // expected-warning{{TRUE}}
256 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
259 void array_change_b(void) {
260 int arr[] = {1, 2};
262 auto &[a, b] = arr;
264 clang_analyzer_eval(a == 1); // expected-warning{{TRUE}}
265 clang_analyzer_eval(b == 2); // expected-warning{{TRUE}}
267 a = 3;
268 clang_analyzer_eval(a == 3); // expected-warning{{TRUE}}
270 clang_analyzer_eval(arr[0] == 3); // expected-warning{{TRUE}}
271 clang_analyzer_eval(arr[1] == 2); // expected-warning{{TRUE}}
274 void array_small_a(void) {
275 int arr[5];
277 auto [a, b, c, d, e] = arr;
279 int x = e; // expected-warning{{Assigned value is garbage or undefined}}
282 void array_big_a(void) {
283 int arr[6];
285 auto [a, b, c, d, e, f] = arr;
287 // FIXME: These will be Undefined when we handle reading Undefined values from lazyCompoundVal.
288 clang_analyzer_eval(a == 1); // expected-warning{{UNKNOWN}}
289 clang_analyzer_eval(b == 2); // expected-warning{{UNKNOWN}}
290 clang_analyzer_eval(c == 3); // expected-warning{{UNKNOWN}}
291 clang_analyzer_eval(d == 4); // expected-warning{{UNKNOWN}}
292 clang_analyzer_eval(e == 5); // expected-warning{{UNKNOWN}}
293 clang_analyzer_eval(f == 6); // expected-warning{{UNKNOWN}}
296 struct S {
297 int a = 1;
298 int b = 2;
301 void non_pod_val(void) {
302 S arr[2];
304 auto [x, y] = arr;
306 clang_analyzer_eval(x.a == 1); // expected-warning{{TRUE}}
307 clang_analyzer_eval(x.b == 2); // expected-warning{{TRUE}}
309 clang_analyzer_eval(y.a == 1); // expected-warning{{TRUE}}
310 clang_analyzer_eval(y.b == 2); // expected-warning{{TRUE}}
313 void non_pod_val_syntax_2(void) {
314 S arr[2];
316 auto [x, y](arr);
318 clang_analyzer_eval(x.a == 1); // expected-warning{{TRUE}}
319 clang_analyzer_eval(x.b == 2); // expected-warning{{TRUE}}
321 clang_analyzer_eval(y.a == 1); // expected-warning{{TRUE}}
322 clang_analyzer_eval(y.b == 2); // expected-warning{{TRUE}}
325 void non_pod_lref(void) {
326 S arr[2];
328 auto &[x, y] = arr;
330 clang_analyzer_eval(x.a == 1); // expected-warning{{TRUE}}
331 clang_analyzer_eval(x.b == 2); // expected-warning{{TRUE}}
333 clang_analyzer_eval(y.a == 1); // expected-warning{{TRUE}}
334 clang_analyzer_eval(y.b == 2); // expected-warning{{TRUE}}
337 void non_pod_rref(void) {
338 S arr[2];
340 auto &&[x, y] = arr;
342 clang_analyzer_eval(x.a == 1); // expected-warning{{TRUE}}
343 clang_analyzer_eval(x.b == 2); // expected-warning{{TRUE}}
345 clang_analyzer_eval(y.a == 1); // expected-warning{{TRUE}}
346 clang_analyzer_eval(y.b == 2); // expected-warning{{TRUE}}
349 struct SUD {
350 inline static int c = 0;
352 int a = 1;
353 int b = 2;
355 SUD() { ++c; };
357 SUD(const SUD &copy) {
358 a = copy.a + 1;
359 b = copy.b + 1;
363 void non_pod_user_defined_val(void) {
364 SUD arr[2];
366 auto [x, y] = arr;
368 clang_analyzer_eval(x.a == 2); // expected-warning{{TRUE}}
369 clang_analyzer_eval(x.b == 3); // expected-warning{{TRUE}}
371 clang_analyzer_eval(y.a == 2); // expected-warning{{TRUE}}
372 clang_analyzer_eval(y.b == 3); // expected-warning{{TRUE}}
375 void non_pod_user_defined_val_syntax_2(void) {
376 SUD::c = 0;
377 SUD arr[2];
379 auto [x, y](arr);
381 clang_analyzer_eval(SUD::c == 2); // expected-warning{{TRUE}}
383 clang_analyzer_eval(x.a == 2); // expected-warning{{TRUE}}
384 clang_analyzer_eval(x.b == 3); // expected-warning{{TRUE}}
386 clang_analyzer_eval(y.a == 2); // expected-warning{{TRUE}}
387 clang_analyzer_eval(y.b == 3); // expected-warning{{TRUE}}