110 lines · cpp
1// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage \2// RUN: -fsafe-buffer-usage-suggestions -verify %s3 4namespace localVar {5void 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}}9}10 11void 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}}14}15}16 17namespace globalVar {18int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}}19void testRefersPtrGlobalVarDecl(int i) {20 ptr + i; // expected-note{{used in pointer arithmetic here}}21 ptr[i]; // expected-note{{used in buffer access here}}22}23 24int array[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}}25void testRefersArrayGlobalVarDecl(int i) {26 array[i/2]; // expected-note{{used in buffer access here}}27}28}29 30namespace functionParm {31void 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}}35}36 37// FIXME: shall we explain the array to pointer decay to make the warning more understandable?38void 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}}41}42}43 44namespace structField {45struct Struct1 {46 int * ptr; // FIXME: per-declaration warning aggregated at the struct definition?47};48 49void testRefersPtrStructFieldDecl(int i) {50 Struct1 s1;51 s1.ptr + i; // expected-warning{{unsafe pointer arithmetic}}52 s1.ptr[i]; // expected-warning{{unsafe buffer access}}53}54 55struct Struct2 {56 int array[10]; // FIXME: per-declaration warning aggregated at the struct definition?57};58 59void testRefersArrayStructFieldDecl(int i) {60 Struct2 s2;61 s2.array[i/2]; // expected-warning{{unsafe buffer access}}62}63}64 65namespace structFieldFromMethod {66struct Struct1 {67 int * ptr; // FIXME: per-declaration warning aggregated at the struct definition68 69 void testRefersPtrStructFieldDecl(int i) {70 ptr + i; // expected-warning{{unsafe pointer arithmetic}}71 ptr[i]; // expected-warning{{unsafe buffer access}}72 }73};74 75struct Struct2 {76 int array[10]; // FIXME: per-declaration warning aggregated at the struct definition77 78 void testRefersArrayStructFieldDecl(int i) {79 Struct2 s2;80 s2.array[i/2]; // expected-warning{{unsafe buffer access}}81 }82};83}84 85namespace staticStructField {86struct Struct1 {87 static int * ptr; // expected-warning{{'ptr' is an unsafe pointer used for buffer access}}88};89 90void 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}}93}94 95struct Struct2 {96 static int array[10]; // expected-warning{{'array' is an unsafe buffer that does not perform bounds}}97};98 99void testRefersArrayStructFieldDecl(int i) {100 Struct2::array[i/2]; // expected-note{{used in buffer access here}}101}102}103 104int * return_ptr();105 106void testNoDeclRef(int i) {107 return_ptr() + i; // expected-warning{{unsafe pointer arithmetic}}108 return_ptr()[i]; // expected-warning{{unsafe buffer access}}109}110