forked from mgehre/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr-psets-annotation.cpp
303 lines (272 loc) · 11.5 KB
/
attr-psets-annotation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// RUN: %clang_cc1 -fcxx-exceptions -fsyntax-only -Wlifetime -Wlifetime-debug -verify %s
#include "../Analysis/Inputs/system-header-simulator-cxx.h"
template <typename T>
bool __lifetime_pset(const T &) { return true; }
template <typename T>
bool __lifetime_pset_ref(const T &) { return true; }
template <typename T>
void __lifetime_type_category() {}
template <typename T>
bool __lifetime_contracts(const T &) { return true; }
struct [[gsl::Pointer]] my_pointer {
my_pointer();
my_pointer &operator=(const my_pointer &);
int &operator*();
};
namespace gsl {
template <typename T>
using nullable = T;
template <typename T>
struct not_null {
constexpr operator T() const { return T(); }
constexpr T operator->() const { return T(); }
};
struct null_t {
int operator*() const;
template <typename T>
operator T() const { return T(nullptr); }
} Null;
struct static_t {
int operator*() const;
template <typename T>
operator T() const { return (T)(void *)this; }
} Static;
struct invalid_t {
int operator*() const;
template <typename T>
operator T() const { return (T)(void *)this; }
} Invalid;
struct return_t {
int operator*() const;
template <typename T>
operator T() const { return (T)(void *)this; }
// TODO: eliminate the methods below.
template <typename T>
return_t(const T&) {}
return_t() {}
bool operator==(return_t) const { return true; }
} Return;
template <typename T>
struct PointerTraits {
static auto deref(const T &t) -> decltype(*t) { return *t; }
static const bool isNull(const T &t) { return false; }
static const bool checked = false;
};
template <typename T>
struct PointerTraits<T *> {
static const T &deref(const T *t) { return *t; }
static const bool isNull(const T *t) { return !t; }
static const bool checked = true;
};
template <typename T>
struct PointerTraits<T &> {
static const T &deref(const T &t) { return t; }
static const bool isNull(const T &t) { return false; }
static const bool checked = true;
};
template <typename T>
struct CheckSingle {
CheckSingle(const T &t) : data(t) {}
T data;
template <typename S>
operator CheckSingle<S>() { return CheckSingle<S>(S(data)); }
};
template <typename T>
struct CheckVariadic {
CheckVariadic(std::initializer_list<T> ptrs) : ptrs(ptrs) {}
// We expect this to live only for a single expr.
std::initializer_list<T> ptrs;
};
template <typename T>
auto deref(const T &t) -> decltype(PointerTraits<T>::deref(t)) {
return PointerTraits<T>::deref(t);
}
// Hack to deduce reference types correclty.
#define deref(arg) deref<decltype(arg)>(arg)
template <typename T, typename S>
bool operator==(CheckSingle<T> lhs, CheckSingle<S> rhs) {
if (!PointerTraits<decltype(lhs.data)>::checked || !PointerTraits<decltype(rhs.data)>::checked)
return true;
// TODO: these cannot be checked right?
if ((void *)lhs.data == (void *)&Static || (void *)lhs.data == (void *)&Invalid || (void *)lhs.data == (void *)&Return ||
(void *)rhs.data == (void *)&Static || (void *)rhs.data == (void *)&Invalid || (void *)rhs.data == (void *)&Return)
return true;
if (PointerTraits<decltype(lhs.data)>::isNull(lhs.data))
return PointerTraits<decltype(rhs.data)>::isNull(rhs.data);
if (PointerTraits<decltype(rhs.data)>::isNull(rhs.data))
return false;
// TODO: User defined pointers might not have operator ==, convert them to raw pointers before checking.
// TODO: does this work well with references?
return lhs.data == rhs.data;
}
// TODO: requiring both arguments to be the same type is too restrictive, probable a variadic tuple might work better.
template <typename T>
bool lifetime(const T &lhs, const CheckVariadic<T> &rhs) {
CheckSingle<T> lhsToCheck(lhs);
return std::any_of(rhs.ptrs.begin(), rhs.ptrs.end(), [&lhsToCheck](const T &ptr) {
return CheckSingle<T>(ptr) == lhsToCheck;
});
}
// TODO: support member selection (change in Attr representation)
} // namespace gsl
using namespace gsl;
void basic(int *a, int *b) [[gsl::pre(lifetime(b, {a}))]] {
__lifetime_pset(b); // expected-warning {{((null), *a)}}
}
// Proper lifetime preconditions can only be checked with annotations.
void check_lifetime_preconditions() {
int a, b;
basic(&a, &a);
basic(&b, &b);
basic(&a,
&b); // expected-warning {{passing a pointer as argument with points-to set (b) where points-to set ((null), a) is expected}}
}
void specials(int *a, int *b, int *c)
[[gsl::pre(lifetime(a, {Null}))]]
[[gsl::pre(lifetime(b, {Static}))]]
[[gsl::pre(lifetime(c, {Invalid}))]] {
__lifetime_pset(a); // expected-warning {{((null))}}
__lifetime_pset(b); // expected-warning {{((static))}}
__lifetime_pset(c); // expected-warning {{((invalid))}}
}
void variadic(int *a, int *b, int *c)
[[gsl::pre(lifetime(b, {a, c}))]] {
__lifetime_pset(b); // expected-warning {{((null), *a, *c}}
}
void variadic_special(int *a, int *b, int *c)
[[gsl::pre(lifetime(b, {a, Null}))]] {
__lifetime_pset(b); // expected-warning {{((null), *a}}
}
void multiple_annotations(int *a, int *b, int *c)
[[gsl::pre(lifetime(b, {a}))]]
[[gsl::pre(lifetime(c, {a}))]] {
__lifetime_pset(b); // expected-warning {{((null), *a)}}
__lifetime_pset(c); // expected-warning {{((null), *a)}}
}
void multiple_annotations_chained(int *a, int *b, int *c)
[[gsl::pre(lifetime(b, {a}))]]
[[gsl::pre(lifetime(c, {b}))]] {
__lifetime_pset(b); // expected-warning {{((null), *a)}}
__lifetime_pset(c); // expected-warning {{((null), *a)}}
}
void annotate_forward_decl(int *a, int *b)
[[gsl::pre(lifetime(b, {a}))]];
void annotate_forward_decl(int *c, int *d) {
__lifetime_pset(d); // expected-warning {{((null), *c)}}
}
// Repeated annotations on redeclarations are not checked as
// they will automatically be checked with contracts.
namespace dump_contracts {
// Need to have bodies to fill the lifetime attr.
void p(int *a) {}
void p2(int *a, int &b) {}
void p3(int *a, int *&b) { b = 0; }
void parameter_psets(int value,
char *const *in,
int &int_ref,
const int &const_int_ref,
std::unique_ptr<int> owner_by_value,
const std::unique_ptr<int> &owner_const_ref,
std::unique_ptr<int> &owner_ref,
my_pointer ptr_by_value,
const my_pointer &ptr_const_ref,
my_pointer &ptr_ref,
my_pointer *ptr_ptr,
const my_pointer *ptr_const_ptr) {}
void p4(int *a, int *b, int *&c)
[[gsl::pre(lifetime(b, {a}))]] { c = 0; }
int *p5(int *a, int *b) { return a; }
int *p6(int *a, int *b)
[[gsl::post(lifetime(Return, {a}))]] { return a; }
struct S{
int *f(int * a, int *b, int *&c) { c = 0; return a; }
S *g(int * a, int *b, int *&c) { c = 0; return this; }
};
void p7(int *a, int *b, int *&c)
[[gsl::post(lifetime(deref(c), {a}))]] { c = a; }
void p8(int *a, int *b, int **c)
[[gsl::post(lifetime(deref(c), {a}))]] { if (c) *c = a; }
void p9(int *a, int *b, int *&c)
[[gsl::lifetime_in(deref(c))]] {}
// TODO: contracts for function pointers?
void f() {
__lifetime_contracts(p);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
__lifetime_contracts(p2);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = (*b)}}
__lifetime_contracts(p3);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = (*b)}}
// expected-warning@-3 {{pset(Pre(*b)) = ((invalid))}}
// expected-warning@-4 {{pset(Post(*b)) = ((null), *a)}}
__lifetime_contracts(parameter_psets);
// expected-warning@-1 {{pset(Pre(owner_by_value)) = (*owner_by_value)}}
// expected-warning@-2 {{pset(Pre(owner_ref)) = (*owner_ref)}}
// expected-warning@-3 {{pset(Pre(*owner_ref)) = (**owner_ref)}}
// expected-warning@-4 {{pset(Pre(ptr_ref)) = (*ptr_ref)}}
// expected-warning@-5 {{pset(Pre(*ptr_ref)) = ((invalid))}}
// expected-warning@-6 {{pset(Pre(ptr_const_ref)) = (*ptr_const_ref)}}
// expected-warning@-7 {{pset(Pre(*ptr_const_ref)) = ((null), **ptr_const_ref)}}
// expected-warning@-8 {{pset(Pre(ptr_const_ptr)) = ((null), *ptr_const_ptr)}}
// expected-warning@-9 {{pset(Pre(*ptr_const_ptr)) = ((null), **ptr_const_ptr)}}
// expected-warning@-10 {{pset(Pre(in)) = ((null), *in)}}
// expected-warning@-11 {{pset(Pre(*in)) = ((null), **in)}}
// expected-warning@-12 {{pset(Pre(owner_const_ref)) = (*owner_const_ref)}}
// expected-warning@-13 {{pset(Pre(*owner_const_ref)) = (**owner_const_ref)}}
// expected-warning@-14 {{pset(Pre(int_ref)) = (*int_ref)}}
// expected-warning@-15 {{pset(Pre(const_int_ref)) = (*const_int_ref)}}
// expected-warning@-16 {{pset(Pre(ptr_ptr)) = ((null), *ptr_ptr)}}
// expected-warning@-17 {{pset(Pre(*ptr_ptr)) = ((invalid))}}
// expected-warning@-18 {{pset(Pre(ptr_by_value)) = ((null), *ptr_by_value)}}
// expected-warning@-19 {{pset(Post(*ptr_ref)) = ((null), **owner_ref, **ptr_const_ref, *int_ref, *ptr_by_value)}}
// expected-warning@-20 {{pset(Post(*ptr_ptr)) = ((null), **owner_ref, **ptr_const_ref, *int_ref, *ptr_by_value)}}
__lifetime_contracts(p4);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *a)}}
// expected-warning@-3 {{pset(Pre(c)) = (*c)}}
// expected-warning@-4 {{pset(Pre(*c)) = ((invalid))}}
// expected-warning@-5 {{pset(Post(*c)) = ((null), *a)}}
__lifetime_contracts(p5);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Post((return value))) = ((null), *a, *b)}}
__lifetime_contracts(p6);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Post((return value))) = ((null), *a)}}
__lifetime_contracts(&S::f);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Pre(c)) = (*c)}}
// expected-warning@-4 {{pset(Pre(*c)) = ((invalid))}}
// expected-warning@-5 {{pset(Pre(this)) = (*this)}}
// expected-warning@-6 {{pset(Post(*c)) = ((null), *a, *b)}}
// expected-warning@-7 {{pset(Post((return value))) = ((null), *a, *b)}}
__lifetime_contracts(&S::g);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Pre(c)) = (*c)}}
// expected-warning@-4 {{pset(Pre(*c)) = ((invalid))}}
// expected-warning@-5 {{pset(Pre(this)) = (*this)}}
// expected-warning@-6 {{pset(Post(*c)) = ((null), *a, *b)}}
// expected-warning@-7 {{pset(Post((return value))) = ((null), *this)}}
__lifetime_contracts(p7);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Pre(c)) = (*c)}}
// expected-warning@-4 {{pset(Pre(*c)) = ((invalid))}}
// expected-warning@-5 {{pset(Post(*c)) = ((null), *a)}}
__lifetime_contracts(p8);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Pre(c)) = ((null), *c)}}
// expected-warning@-4 {{pset(Pre(*c)) = ((invalid))}}
// expected-warning@-5 {{pset(Post(*c)) = ((null), *a)}}
__lifetime_contracts(p9);
// expected-warning@-1 {{pset(Pre(a)) = ((null), *a)}}
// expected-warning@-2 {{pset(Pre(b)) = ((null), *b)}}
// expected-warning@-3 {{pset(Pre(c)) = (*c)}}
// expected-warning@-4 {{pset(Pre(*c)) = ((null), **c)}}
}
} // namespace dump_contracts