[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-unsafe-buffer-usage-multi-decl-uuc.cpp
blob73fe20552d41017ab8bb32839cece3e117af5346
1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions -verify %s
2 void bar(int * param) {}
4 void foo1a() {
5 int *r = new int[7];
6 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
7 p = r;
8 int tmp = p[9]; // expected-note{{used in buffer access here}}
9 int *q;
10 q = r; // FIXME: we do not fix `q = r` here as the `.data()` fix-it is not generally correct
13 void uuc_if_body() {
14 int *r = new int[7];
15 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} // expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'r' to 'std::span' to propagate bounds information between them}}
16 if (true)
17 p = r;
18 p[5] = 4; // expected-note{{used in buffer access here}}
21 void uuc_if_body1(bool flag) {
22 int *r = new int[7];
23 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} // expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'r' to 'std::span' to propagate bounds information between them}}
24 if (flag) {
25 p = r;
27 p[5] = 4; // expected-note{{used in buffer access here}}
30 void uuc_if_body2(bool flag) {
31 int *r = new int[7];
32 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}} // expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'r' to 'std::span' to propagate bounds information between them}}
33 if (flag) {
34 } else {
35 p = r;
38 p[5] = 4; // expected-note{{used in buffer access here}}
41 void uuc_if_body2_ptr_init(bool flag) {
42 int *r = new int[7];
43 if (flag) {
44 } else {
45 int* p = r; // expected-warning{{'p' is an unsafe pointer used for buffer access}} // expected-note{{change type of 'p' to 'std::span' to preserve bounds information, and change 'r' to 'std::span' to propagate bounds information between them}}
46 p[5] = 4; // expected-note{{used in buffer access here}}
50 void uuc_if_cond_no_unsafe_op() {
51 int *r = new int[7];
52 int *p = new int[4];
53 if ((p = r)) {
54 int x = 0;
58 void uuc_if_cond_no_unsafe_op1() {
59 int *r = new int[7];
60 int *p = new int[4];
61 if (true) {
62 int x = 0;
63 } else if ((p = r))
64 int y = 10;
67 void uuc_if_cond_unsafe_op() {
68 int *r = new int[7];
69 int *p = new int[4]; //expected-warning{{'p' is an unsafe pointer used for buffer access}}
70 if ((p = r)) {
71 p[3] = 2; // expected-note{{used in buffer access here}}
75 void uuc_if_cond_unsafe_op1() {
76 int *r = new int[7]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
77 int *p = new int[4];
78 if ((p = r)) {
79 r[3] = 2; // expected-note{{used in buffer access here}}
83 void uuc_if_cond_unsafe_op2() {
84 int *r = new int[7]; // expected-warning{{'r' is an unsafe pointer used for buffer access}}
85 int *p = new int[4]; // expected-warning{{'p' is an unsafe pointer used for buffer access}}
86 if ((p = r)) {
87 r[3] = 2; // expected-note{{used in buffer access here}}
89 p[4] = 6; // expected-note{{used in buffer access here}}
92 void uuc_call1() {
93 int *w = new int[4]; // expected-warning{{'w' is an unsafe pointer used for buffer access}}
94 int *y = new int[4];
95 bar(w = y);
96 w[5] = 0; // expected-note{{used in buffer access here}}