1 //===- ThreadSafetyUtil.h ---------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines some basic utility classes for use by ThreadSafetyTIL.h
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
14 #define LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
15
16 #include "clang/AST/Decl.h"
17 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/Support/Allocator.h"
21 #include <cassert>
22 #include <cstddef>
23 #include <cstring>
24 #include <iterator>
25 #include <ostream>
26 #include <string>
27 #include <vector>
28
29 namespace clang {
30
31 class Expr;
32
33 namespace threadSafety {
34 namespace til {
35
36 // Simple wrapper class to abstract away from the details of memory management.
37 // SExprs are allocated in pools, and deallocated all at once.
38 class MemRegionRef {
39 private:
40 union AlignmentType {
41 double d;
42 void *p;
43 long double dd;
44 long long ii;
45 };
46
47 public:
48 MemRegionRef() = default;
MemRegionRef(llvm::BumpPtrAllocator * A)49 MemRegionRef(llvm::BumpPtrAllocator *A) : Allocator(A) {}
50
allocate(size_t Sz)51 void *allocate(size_t Sz) {
52 return Allocator->Allocate(Sz, alignof(AlignmentType));
53 }
54
allocateT()55 template <typename T> T *allocateT() { return Allocator->Allocate<T>(); }
56
allocateT(size_t NumElems)57 template <typename T> T *allocateT(size_t NumElems) {
58 return Allocator->Allocate<T>(NumElems);
59 }
60
61 private:
62 llvm::BumpPtrAllocator *Allocator = nullptr;
63 };
64
65 } // namespace til
66 } // namespace threadSafety
67
68 } // namespace clang
69
new(size_t Sz,clang::threadSafety::til::MemRegionRef & R)70 inline void *operator new(size_t Sz,
71 clang::threadSafety::til::MemRegionRef &R) {
72 return R.allocate(Sz);
73 }
74
75 namespace clang {
76 namespace threadSafety {
77
78 std::string getSourceLiteralString(const Expr *CE);
79
80 namespace til {
81
82 // A simple fixed size array class that does not manage its own memory,
83 // suitable for use with bump pointer allocation.
84 template <class T> class SimpleArray {
85 public:
86 SimpleArray() = default;
87 SimpleArray(T *Dat, size_t Cp, size_t Sz = 0)
Data(Dat)88 : Data(Dat), Size(Sz), Capacity(Cp) {}
SimpleArray(MemRegionRef A,size_t Cp)89 SimpleArray(MemRegionRef A, size_t Cp)
90 : Data(Cp == 0 ? nullptr : A.allocateT<T>(Cp)), Capacity(Cp) {}
91 SimpleArray(const SimpleArray<T> &A) = delete;
92
SimpleArray(SimpleArray<T> && A)93 SimpleArray(SimpleArray<T> &&A)
94 : Data(A.Data), Size(A.Size), Capacity(A.Capacity) {
95 A.Data = nullptr;
96 A.Size = 0;
97 A.Capacity = 0;
98 }
99
100 SimpleArray &operator=(SimpleArray &&RHS) {
101 if (this != &RHS) {
102 Data = RHS.Data;
103 Size = RHS.Size;
104 Capacity = RHS.Capacity;
105
106 RHS.Data = nullptr;
107 RHS.Size = RHS.Capacity = 0;
108 }
109 return *this;
110 }
111
112 // Reserve space for at least Ncp items, reallocating if necessary.
reserve(size_t Ncp,MemRegionRef A)113 void reserve(size_t Ncp, MemRegionRef A) {
114 if (Ncp <= Capacity)
115 return;
116 T *Odata = Data;
117 Data = A.allocateT<T>(Ncp);
118 Capacity = Ncp;
119 memcpy(Data, Odata, sizeof(T) * Size);
120 }
121
122 // Reserve space for at least N more items.
reserveCheck(size_t N,MemRegionRef A)123 void reserveCheck(size_t N, MemRegionRef A) {
124 if (Capacity == 0)
125 reserve(u_max(InitialCapacity, N), A);
126 else if (Size + N < Capacity)
127 reserve(u_max(Size + N, Capacity * 2), A);
128 }
129
130 using iterator = T *;
131 using const_iterator = const T *;
132 using reverse_iterator = std::reverse_iterator<iterator>;
133 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
134
size()135 size_t size() const { return Size; }
capacity()136 size_t capacity() const { return Capacity; }
137
138 T &operator[](unsigned i) {
139 assert(i < Size && "Array index out of bounds.");
140 return Data[i];
141 }
142
143 const T &operator[](unsigned i) const {
144 assert(i < Size && "Array index out of bounds.");
145 return Data[i];
146 }
147
back()148 T &back() {
149 assert(Size && "No elements in the array.");
150 return Data[Size - 1];
151 }
152
back()153 const T &back() const {
154 assert(Size && "No elements in the array.");
155 return Data[Size - 1];
156 }
157
begin()158 iterator begin() { return Data; }
end()159 iterator end() { return Data + Size; }
160
begin()161 const_iterator begin() const { return Data; }
end()162 const_iterator end() const { return Data + Size; }
163
cbegin()164 const_iterator cbegin() const { return Data; }
cend()165 const_iterator cend() const { return Data + Size; }
166
rbegin()167 reverse_iterator rbegin() { return reverse_iterator(end()); }
rend()168 reverse_iterator rend() { return reverse_iterator(begin()); }
169
rbegin()170 const_reverse_iterator rbegin() const {
171 return const_reverse_iterator(end());
172 }
173
rend()174 const_reverse_iterator rend() const {
175 return const_reverse_iterator(begin());
176 }
177
push_back(const T & Elem)178 void push_back(const T &Elem) {
179 assert(Size < Capacity);
180 Data[Size++] = Elem;
181 }
182
183 // drop last n elements from array
184 void drop(unsigned n = 0) {
185 assert(Size > n);
186 Size -= n;
187 }
188
setValues(unsigned Sz,const T & C)189 void setValues(unsigned Sz, const T& C) {
190 assert(Sz <= Capacity);
191 Size = Sz;
192 for (unsigned i = 0; i < Sz; ++i) {
193 Data[i] = C;
194 }
195 }
196
append(Iter I,Iter E)197 template <class Iter> unsigned append(Iter I, Iter E) {
198 size_t Osz = Size;
199 size_t J = Osz;
200 for (; J < Capacity && I != E; ++J, ++I)
201 Data[J] = *I;
202 Size = J;
203 return J - Osz;
204 }
205
reverse()206 llvm::iterator_range<reverse_iterator> reverse() {
207 return llvm::reverse(*this);
208 }
209
reverse()210 llvm::iterator_range<const_reverse_iterator> reverse() const {
211 return llvm::reverse(*this);
212 }
213
214 private:
215 // std::max is annoying here, because it requires a reference,
216 // thus forcing InitialCapacity to be initialized outside the .h file.
u_max(size_t i,size_t j)217 size_t u_max(size_t i, size_t j) { return (i < j) ? j : i; }
218
219 static const size_t InitialCapacity = 4;
220
221 T *Data = nullptr;
222 size_t Size = 0;
223 size_t Capacity = 0;
224 };
225
226 } // namespace til
227
228 // A copy on write vector.
229 // The vector can be in one of three states:
230 // * invalid -- no operations are permitted.
231 // * read-only -- read operations are permitted.
232 // * writable -- read and write operations are permitted.
233 // The init(), destroy(), and makeWritable() methods will change state.
234 template<typename T>
235 class CopyOnWriteVector {
236 class VectorData {
237 public:
238 unsigned NumRefs = 1;
239 std::vector<T> Vect;
240
241 VectorData() = default;
VectorData(const VectorData & VD)242 VectorData(const VectorData &VD) : Vect(VD.Vect) {}
243
244 // The copy assignment operator is defined as deleted pending further
245 // motivation.
246 VectorData &operator=(const VectorData &) = delete;
247 };
248
249 public:
250 CopyOnWriteVector() = default;
CopyOnWriteVector(CopyOnWriteVector && V)251 CopyOnWriteVector(CopyOnWriteVector &&V) : Data(V.Data) { V.Data = nullptr; }
252
253 CopyOnWriteVector &operator=(CopyOnWriteVector &&V) {
254 destroy();
255 Data = V.Data;
256 V.Data = nullptr;
257 return *this;
258 }
259
260 // No copy constructor or copy assignment. Use clone() with move assignment.
261 CopyOnWriteVector(const CopyOnWriteVector &) = delete;
262 CopyOnWriteVector &operator=(const CopyOnWriteVector &) = delete;
263
~CopyOnWriteVector()264 ~CopyOnWriteVector() { destroy(); }
265
266 // Returns true if this holds a valid vector.
valid()267 bool valid() const { return Data; }
268
269 // Returns true if this vector is writable.
writable()270 bool writable() const { return Data && Data->NumRefs == 1; }
271
272 // If this vector is not valid, initialize it to a valid vector.
init()273 void init() {
274 if (!Data) {
275 Data = new VectorData();
276 }
277 }
278
279 // Destroy this vector; thus making it invalid.
destroy()280 void destroy() {
281 if (!Data)
282 return;
283 if (Data->NumRefs <= 1)
284 delete Data;
285 else
286 --Data->NumRefs;
287 Data = nullptr;
288 }
289
290 // Make this vector writable, creating a copy if needed.
makeWritable()291 void makeWritable() {
292 if (!Data) {
293 Data = new VectorData();
294 return;
295 }
296 if (Data->NumRefs == 1)
297 return; // already writeable.
298 --Data->NumRefs;
299 Data = new VectorData(*Data);
300 }
301
302 // Create a lazy copy of this vector.
clone()303 CopyOnWriteVector clone() { return CopyOnWriteVector(Data); }
304
305 using const_iterator = typename std::vector<T>::const_iterator;
306
elements()307 const std::vector<T> &elements() const { return Data->Vect; }
308
begin()309 const_iterator begin() const { return elements().cbegin(); }
end()310 const_iterator end() const { return elements().cend(); }
311
312 const T& operator[](unsigned i) const { return elements()[i]; }
313
size()314 unsigned size() const { return Data ? elements().size() : 0; }
315
316 // Return true if V and this vector refer to the same data.
sameAs(const CopyOnWriteVector & V)317 bool sameAs(const CopyOnWriteVector &V) const { return Data == V.Data; }
318
319 // Clear vector. The vector must be writable.
clear()320 void clear() {
321 assert(writable() && "Vector is not writable!");
322 Data->Vect.clear();
323 }
324
325 // Push a new element onto the end. The vector must be writable.
push_back(const T & Elem)326 void push_back(const T &Elem) {
327 assert(writable() && "Vector is not writable!");
328 Data->Vect.push_back(Elem);
329 }
330
331 // Gets a mutable reference to the element at index(i).
332 // The vector must be writable.
elem(unsigned i)333 T& elem(unsigned i) {
334 assert(writable() && "Vector is not writable!");
335 return Data->Vect[i];
336 }
337
338 // Drops elements from the back until the vector has size i.
downsize(unsigned i)339 void downsize(unsigned i) {
340 assert(writable() && "Vector is not writable!");
341 Data->Vect.erase(Data->Vect.begin() + i, Data->Vect.end());
342 }
343
344 private:
CopyOnWriteVector(VectorData * D)345 CopyOnWriteVector(VectorData *D) : Data(D) {
346 if (!Data)
347 return;
348 ++Data->NumRefs;
349 }
350
351 VectorData *Data = nullptr;
352 };
353
354 inline std::ostream& operator<<(std::ostream& ss, const StringRef str) {
355 return ss.write(str.data(), str.size());
356 }
357
358 } // namespace threadSafety
359 } // namespace clang
360
361 #endif // LLVM_CLANG_ANALYSIS_ANALYSES_THREADSAFETYUTIL_H
362