1 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
2 // RUN: -fsafe-buffer-usage-suggestions -verify %s
5 void testRefersPtrLocalVarDecl(int i
) {
6 int * ptr
; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}}
7 ptr
+ i
; // expected-note{{used in pointer arithmetic here}}
8 ptr
[i
]; // expected-note{{used in buffer access here}}
11 void testRefersArrayLocalVarDecl(int i
) {
12 int array
[i
]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}}
13 array
[i
/2]; // expected-note{{used in buffer access here}}
18 int * ptr
; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}}
19 void testRefersPtrGlobalVarDecl(int i
) {
20 ptr
+ i
; // expected-note{{used in pointer arithmetic here}}
21 ptr
[i
]; // expected-note{{used in buffer access here}}
24 int array
[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}}
25 void testRefersArrayGlobalVarDecl(int i
) {
26 array
[i
/2]; // expected-note{{used in buffer access here}}
30 namespace functionParm
{
31 void testRefersPtrParmVarDecl(int * ptr
) {
32 // expected-warning@-1{{'ptr' is an unsafe pointer used for buffer access}}
33 ptr
+ 5; // expected-note{{used in pointer arithmetic here}}
34 ptr
[5]; // expected-note{{used in buffer access here}}
37 // FIXME: shall we explain the array to pointer decay to make the warning more understandable?
38 void testRefersArrayParmVarDecl(int array
[10]) {
39 // expected-warning@-1{{'array' is an unsafe pointer used for buffer access}}
40 array
[2]; // expected-note{{used in buffer access here}}
44 namespace structField
{
46 int * ptr
; // FIXME: per-declaration warning aggregated at the struct definition?
49 void testRefersPtrStructFieldDecl(int i
) {
51 s1
.ptr
+ i
; // expected-warning{{unsafe pointer arithmetic}}
52 s1
.ptr
[i
]; // expected-warning{{unsafe buffer access}}
56 int array
[10]; // FIXME: per-declaration warning aggregated at the struct definition?
59 void testRefersArrayStructFieldDecl(int i
) {
61 s2
.array
[i
/2]; // expected-warning{{unsafe buffer access}}
65 namespace structFieldFromMethod
{
67 int * ptr
; // FIXME: per-declaration warning aggregated at the struct definition
69 void testRefersPtrStructFieldDecl(int i
) {
70 ptr
+ i
; // expected-warning{{unsafe pointer arithmetic}}
71 ptr
[i
]; // expected-warning{{unsafe buffer access}}
76 int array
[10]; // FIXME: per-declaration warning aggregated at the struct definition
78 void testRefersArrayStructFieldDecl(int i
) {
80 s2
.array
[i
/2]; // expected-warning{{unsafe buffer access}}
85 namespace staticStructField
{
87 static int * ptr
; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}}
90 void testRefersPtrStructFieldDecl(int i
) {
91 Struct1::ptr
+ i
; // expected-note{{used in pointer arithmetic here}}
92 Struct1::ptr
[i
]; // expected-note{{used in buffer access here}}
96 static int array
[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}}
99 void testRefersArrayStructFieldDecl(int i
) {
100 Struct2::array
[i
/2]; // expected-note{{used in buffer access here}}
106 void testNoDeclRef(int i
) {
107 return_ptr() + i
; // expected-warning{{unsafe pointer arithmetic}}
108 return_ptr()[i
]; // expected-warning{{unsafe buffer access}}