@@ -32,38 +32,68 @@ void foo(int *p){}
32
32
namespace std {
33
33
template <typename T> class span {
34
34
35
- T *elements;
35
+ T *elements;
36
36
37
- span (T *, unsigned ){}
37
+ span (T *, unsigned ){}
38
38
39
- public:
39
+ public:
40
40
41
- constexpr span<T> subspan (size_t offset, size_t count) const {
42
- return span<T> (elements+offset, count); // expected-warning{{unsafe pointer arithmetic}}
43
- }
41
+ constexpr span<T> subspan (size_t offset, size_t count) const {
42
+ return span<T> (elements+offset, count); // expected-warning{{unsafe pointer arithmetic}}
43
+ }
44
44
45
- constexpr T* data () const noexcept {
46
- return elements;
47
- }
45
+ constexpr T* data () const noexcept {
46
+ return elements;
47
+ }
48
+
49
+ constexpr T* hello () const noexcept {
50
+ return elements;
51
+ }
52
+ };
53
+
54
+ template <typename T> class vector {
55
+
56
+ T *elements;
57
+
58
+ public:
59
+
60
+ vector (size_t n) {
61
+ elements = new T[n];
62
+ }
63
+
64
+ constexpr T* data () const noexcept {
65
+ return elements;
66
+ }
67
+
68
+ ~vector () {
69
+ delete[] elements;
70
+ }
71
+ };
72
+
73
+ template <class T , size_t N>
74
+ class array {
75
+ T elements[N];
76
+
77
+ public:
78
+
79
+ constexpr const T* data () const noexcept {
80
+ return elements;
81
+ }
82
+
83
+ };
48
84
49
-
50
- constexpr T* hello () const noexcept {
51
- return elements;
52
- }
53
- };
54
-
55
85
template <typename T> class span_duplicate {
56
- span_duplicate (T *, unsigned ){}
86
+ span_duplicate (T *, unsigned ){}
57
87
58
- T array[10 ];
88
+ T array[10 ];
59
89
60
- public:
90
+ public:
61
91
62
- T* data () {
63
- return array;
64
- }
92
+ T* data () {
93
+ return array;
94
+ }
65
95
66
- };
96
+ };
67
97
}
68
98
69
99
using namespace std ;
@@ -89,21 +119,28 @@ void cast_without_data(int *ptr) {
89
119
float *p = (float *) ptr;
90
120
}
91
121
92
- void warned_patterns (std::span<int > span_ptr, std::span<Base> base_span, span<int > span_without_qual) {
93
- A *a1 = (A*)span_ptr.data (); // expected-warning{{unsafe invocation of span:: data}}
94
- a1 = (A*)span_ptr.data (); // expected-warning{{unsafe invocation of span:: data}}
122
+ void warned_patterns_span (std::span<int > span_ptr, std::span<Base> base_span, span<int > span_without_qual) {
123
+ A *a1 = (A*)span_ptr.data (); // expected-warning{{unsafe invocation of ' data' }}
124
+ a1 = (A*)span_ptr.data (); // expected-warning{{unsafe invocation of ' data' }}
95
125
96
- a1 = (A*)(span_ptr.data ()); // expected-warning{{unsafe invocation of span:: data}}
97
- A *a2 = (A*) (span_without_qual.data ()); // expected-warning{{unsafe invocation of span:: data}}
126
+ a1 = (A*)(span_ptr.data ()); // expected-warning{{unsafe invocation of ' data' }}
127
+ A *a2 = (A*) (span_without_qual.data ()); // expected-warning{{unsafe invocation of ' data' }}
98
128
99
- a2 = (A*) span_without_qual.data (); // expected-warning{{unsafe invocation of span:: data}}
129
+ a2 = (A*) span_without_qual.data (); // expected-warning{{unsafe invocation of ' data' }}
100
130
101
131
// TODO:: Should we warn when we cast from base to derived type?
102
- Derived *b = dynamic_cast <Derived*> (base_span.data ());// expected-warning{{unsafe invocation of span:: data}}
132
+ Derived *b = dynamic_cast <Derived*> (base_span.data ());// expected-warning{{unsafe invocation of ' data' }}
103
133
104
134
// TODO:: This pattern is safe. We can add special handling for it, if we decide this
105
135
// is the recommended fixit for the unsafe invocations.
106
- A *a3 = (A*)span_ptr.subspan (0 , sizeof (A)).data (); // expected-warning{{unsafe invocation of span::data}}
136
+ A *a3 = (A*)span_ptr.subspan (0 , sizeof (A)).data (); // expected-warning{{unsafe invocation of 'data'}}
137
+ }
138
+
139
+ void warned_patterns_array (std::array<int , 5 > array_ptr, std::array<Base, 10 > base_span, span<int > span_without_qual) {
140
+ const A *a1 = (A*)array_ptr.data (); // expected-warning{{unsafe invocation of 'data'}}
141
+ a1 = (A*)array_ptr.data (); // expected-warning{{unsafe invocation of 'data'}}
142
+
143
+ a1 = (A*)(array_ptr.data ()); // expected-warning{{unsafe invocation of 'data'}}
107
144
}
108
145
109
146
void not_warned_patterns (std::span<A> span_ptr, std::span<Base> base_span) {
0 commit comments