1 // RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2 // RUN: -fsafe-buffer-usage-suggestions \
3 // RUN: -triple=arm-apple \
4 // RUN: -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
6 int f(unsigned long, void *);
8 [[clang::unsafe_buffer_usage
]]
9 int unsafe_f(unsigned long, void *);
11 void address_to_integer(int x
) {
12 int * p
= new int[10];
13 unsigned long n
= (unsigned long) &p
[5];
14 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:37-[[@LINE-1]]:42}:"&p.data()[5]"
15 unsigned long m
= (unsigned long) &p
[x
];
16 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:37-[[@LINE-1]]:42}:"&p.data()[x]"
19 void address_to_bool(int x
) {
20 int * p
= new int[10];
21 bool a
= (bool) &p
[5];
22 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:24}:"&p.data()[5]"
23 bool b
= (bool) &p
[x
];
24 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:19-[[@LINE-1]]:24}:"&p.data()[x]"
27 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:18}:"&p.data()[5]"
29 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:13-[[@LINE-1]]:18}:"&p.data()[x]"
32 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:12}:"&p.data()[5]"
37 void call_argument(int x
) {
38 int * p
= new int[10];
40 f((unsigned long) &p
[5], &p
[x
]);
41 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:21-[[@LINE-1]]:26}:"&p.data()[5]"
42 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:28-[[@LINE-2]]:33}:"&p.data()[x]"
45 void ignore_unsafe_calls(int x
) {
46 // Cannot fix `&p[x]` for now as it is an argument of an unsafe
47 // call. So no fix for variable `p`.
48 int * p
= new int[10];
49 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
50 unsafe_f((unsigned long) &p
[5],
51 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
54 int * q
= new int[10];
55 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> q"
56 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
57 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
58 unsafe_f((unsigned long) &q
[5],
59 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:28-[[@LINE-1]]:33}:"&q.data()[5]"
63 void odd_subscript_form() {
64 int * p
= new int[10];
65 unsigned long n
= (unsigned long) &5[p
];
66 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:37-[[@LINE-1]]:42}:"&p.data()[5]"
69 void index_is_zero() {
70 int * p
= new int[10];
73 f((unsigned long)&p
[0],
74 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:20-[[@LINE-1]]:25}:"p.data()"
76 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:5-[[@LINE-1]]:10}:"p.data()"
79 void pointer_subtraction(int x
) {
80 int * p
= new int[10];
81 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::span<int> p"
82 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:13-[[@LINE-2]]:13}:"{"
83 // CHECK: fix-it:"{{.*}}":{[[@LINE-3]]:24-[[@LINE-3]]:24}:", 10}"
85 int n
= &p
[9] - &p
[4];
86 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:16}:"&p.data()[9]"
87 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:19-[[@LINE-2]]:24}:"&p.data()[4]"
89 // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:12}:"&p.data()[9]"
90 // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:15-[[@LINE-2]]:20}:"&p.data()[x]"
95 // To test multiple function declarations, each of which carries
96 // different incomplete informations.
97 // no fix-it in the rest of this test:
99 [[clang::unsafe_buffer_usage
]]
100 void unsafe_g(void*);
102 void unsafe_g(void*);
104 void multiple_unsafe_fundecls() {
105 int * p
= new int[10];
106 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
108 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
111 void unsafe_h(void*);
113 [[clang::unsafe_buffer_usage
]]
114 void unsafe_h(void*);
116 void unsafe_h(void* p
) { ((char*)p
)[10]; }
118 void multiple_unsafe_fundecls2() {
119 int * p
= new int[10];
120 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]
122 // CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]