Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / SemaCXX / unsafe-buffer-usage-diag-type.cpp
blobc73feb92848d4eef0bf5bcce20f579cdbb830761
1 // RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \
2 // RUN: -fsafe-buffer-usage-suggestions -verify %s
4 namespace localVar {
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}}
17 namespace globalVar {
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 {
45 struct Struct1 {
46 int * ptr; // FIXME: per-declaration warning aggregated at the struct definition?
49 void testRefersPtrStructFieldDecl(int i) {
50 Struct1 s1;
51 s1.ptr + i; // expected-warning{{unsafe pointer arithmetic}}
52 s1.ptr[i]; // expected-warning{{unsafe buffer access}}
55 struct Struct2 {
56 int array[10]; // FIXME: per-declaration warning aggregated at the struct definition?
59 void testRefersArrayStructFieldDecl(int i) {
60 Struct2 s2;
61 s2.array[i/2]; // expected-warning{{unsafe buffer access}}
65 namespace structFieldFromMethod {
66 struct Struct1 {
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}}
75 struct Struct2 {
76 int array[10]; // FIXME: per-declaration warning aggregated at the struct definition
78 void testRefersArrayStructFieldDecl(int i) {
79 Struct2 s2;
80 s2.array[i/2]; // expected-warning{{unsafe buffer access}}
85 namespace staticStructField {
86 struct Struct1 {
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}}
95 struct Struct2 {
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}}
104 int * return_ptr();
106 void testNoDeclRef(int i) {
107 return_ptr() + i; // expected-warning{{unsafe pointer arithmetic}}
108 return_ptr()[i]; // expected-warning{{unsafe buffer access}}